[R] Obtaining the variable names of a glm object

John Fox jfox at mcmaster.ca
Fri Nov 29 15:14:20 CET 2002


Dear Kenneth,

At 05:35 AM 11/29/2002 -0500, you wrote:


>If I use:
>model1$coef, I obtain the name of the coef, but joined.
>I want them separated.

There may be a simpler -- and more transparent approach --  but if I 
understand correctly what you want, here's a start:

     predictors <- attr(terms(model), "variables")
     predictors <- as.character(predictors[3:length(predictors)])
     levels <- lapply(predictors, function(x) levels(eval(parse(text=x))))

Then predictors holds the predictor names and the list levels holds factor 
levels (or NULL for a numeric predictor). Now you need to consider the 
contrast coding for each factor to figure out how to use the levels:

     lapply(predictors, function(x) {
         x <- eval(parse(text=x))
         if (is.factor(x)) contrasts(x)
         })

and you also need to determine where the interactions are. The relevant 
information is in terms(model) and attr(model.matrix(model), "assign").

The programming from this point looks tricky to me but doable. An 
alternative would be to examine names(coef(model)) using some of the other 
information.

I hope that this helps,
John


-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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