[R] Intercepts in linear models.
Rolf Turner
rolf at math.unb.ca
Thu Mar 23 17:43:53 CET 2006
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)
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
More information about the R-help
mailing list