[R] Intercepts in linear models.
Kjetil Brinchmann Halvorsen
kjetilbrinchmannhalvorsen at gmail.com
Thu Mar 23 21:17:33 CET 2006
Rolf Turner wrote:
> A colleague asked me if there is a way to specify with a
> ***variable*** (say ``cflag'') whether there is an intercept in a
> linear model.
> She had in mind something like
> lm(y ~ x - cflag)
lm( if (cflag) y ~ x-0 else y ~ x, ...
> where cflag could be 0 or 1; if it's 0 an intercept should
> be fitted, if it's 1 then no intercept.
> This doesn't work ``of course''. The cflag just gets treated
> as another predictor and because it is of the wrong length
> an error is generated.
> The best I could come up with was
> lm(as.formula(paste("y ~ x -",cflag)))
> Which is kludgy. It (of course!) also be done using an
> if statement.
> Is there a sexier way?
> Rolf Turner
> R-help at stat.math.ethz.ch mailing list
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
More information about the R-help