[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)

something like
lm( if (cflag) y ~ x-0 else y ~ x, ...

Kjetil

> 
> 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?
> 
> 				cheers,
> 
> 					Rolf Turner
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list