[R] How to include an externally defined NULL value in lm
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Mar 4 01:34:44 CET 2008
It could be done like this:
> a <- if (TRUE) . ~ . + freeny.x[,4] else . ~ .
> fo <- update(freey.y ~ freeny.x[, -4], a)
> fo
freey.y ~ freeny.x[, -4] + freeny.x[, 4]
On Mon, Mar 3, 2008 at 6:24 PM, Rolf Turner <r.turner at auckland.ac.nz> wrote:
>
> Matthieu wants to manipulate the *formula* --- i.e. control
> what predictors go into the formula --- rather than to add something
> to one of the predictors (or not, as the case maybe).
>
> I.e. what is wanted is that if ``condition'' is TRUE then the
> formula should be
>
> freeny.y ~ freeny.x[,-4] + freeny.x[,4]
>
> and otherwise it should be
>
> freeny.y ~ freeny.x[,-4]
>
> The subtlety is that `` y ~ x + NULL'' gives the model ``y = beta_0 +
> beta_1 * x + E''
> Whereas ``y ~ x + a'' gives the model ``y = beta_0 + beta_1 * x +
> beta_2 * a + E''.
> Irrespective of what the object a happens to be --- or even of
> whether it exists.
> (This is a bit of an oversimplification I think, but close enuff.)
>
> Then when lm() tries to fit y = beta_0 + beta_1 * x + beta_2 * a + E
> it gets all in a dither when a is NULL, because the model does not then
> make sense.
>
> I'm afraid I don't know how to get around the problem using the NULL
> concept.
> Wiser heads than mine may be able to suggest a way.
>
> There are however different approaches to the problem, ***not***
> using NULL.
>
> E.g.
>
> fff <- if(connie) as.formula(freeny.y ~ freeny.x[,-4] + freeny.x[,
> 4]) else as.formula(freeny.y ~ freeny.x[,-4])
> fit <- lm(fff)
>
> or (more sensibly?)
>
> fff <- if(connie) as.formula(freeny.y ~ freeny.x) else as.formula
> (freeny.y ~ freeny.x[,-4])
> fit <- lm(fff)
>
> More sensibly still:
>
> fff <- if(connie) y ~ . else y ~ . - market.potential
> fit <- lm(fff,data=freeny)
>
> Where ``connie'' is some condition (with value either TRUE or FALSE).
>
> Formulae can also be constructed as text strings (e.g. using
> ``paste'') and then converted
> to actual formulae using as.formula().
>
> HTH.
>
> cheers,
>
> Rolf Turner
>
>
>
>
> On 4/03/2008, at 11:34 AM, <markleeds at verizon.net> wrote:
>
> >> From: Matthieu Stigler <stigler3 at etu.unige.ch>
> >> Date: 2008/03/03 Mon PM 04:07:09 CST
> >> To: r-help at r-project.org
> >> Subject: [R] How to include an externally defined NULL value in lm
> >
> > maybe you should a<-0 unless there's special
> > behavior of NULL that's unknownst to me ?
> >
> >
> >> Hello!
> >>
> >> I would love to be able to include an external variable to a lm
> >> call, I
> >> mean something:
> >>
> >> if(TRUE)
> >> a<-freeny.x[,4]
> >> else
> >> a<-NULL
> >>
> >> lm(freeny.y~freeny.x[,-4] +a)
> >>
> >> but it does not work with a<-NULL, whereas
> >>
> >> lm(freeny.y~freeny.x[,-4] +NULL)
> >>
> >> I don't understand why and did not find an answer in the
> >> manuals... do
> >> you see it? Any idea?
> >>
> >> Thanks!!
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide http://www.R-project.org/posting-
> >> guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-
> > guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
>
> ######################################################################
> Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list