[R] specifying a model formula on the fly

Thomas Lumley thomas at biostat.washington.edu
Tue Jul 4 19:31:27 CEST 2000

On 4 Jul 2000, Allan Strand wrote:

> Hi All,
> I'm trying to specify the formula for a glm based upon the names in
> the data.frame.  I always discard the first two variables in the frame
> and the third will always be the only response variable.  The problem
> appears because I would like to specify the rest of the variables of
> the data.frame as independent variables in the formula.  I can't think
> of a way to do this cleanly and maintain the names of the independent
> variables in the glm object.  I know that I can use as.matrix to cast
> the rest of the data.frame as a design matrix, but the glm object
> returned has "as.matrix(x)" prepended to the variable names.  I
> suspect that I could iterate across the results of name(x) and somehow
> use quote() and eval() to solve this problem, but I cannot seem to
> make it work.  Any suggestions would be greatly appreciated.

I'm not sure this is the right way to approach the problem, but this seems
to work (with the dataframe being called df)




The drawback is that the call component of the glm object is
"glm(formula,df)" rather than giving the explicit formula.

An even more complicated way around this is to replace glm(formula,df) by


but at some point you start wondering if it is worth the effort.


r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list