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)
nm<-names(df)
formula<-eval(parse(text=paste(nn[3],paste(nn[-(1:3)],collapse="+"),sep="~"))
glm(formula,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
eval(substitute(glm(formula,data=data),list(formula=formula,data=quote(df))))
but at some point you start wondering if it is worth the effort.
-thomas
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._