[R] pulling items out of a lm() call

Gabor Grothendieck ggrothendieck at gmail.com
Mon May 1 13:35:35 CEST 2006


Try this:

# test data
fo <- y ~ female + I(age^2) + female:black + (age + education) * female

# create a list of form list(y = as.name("z.y"), ...) for use with substitute
L <- sapply(all.vars(fo), function(nm) as.name(paste("z", nm, sep = ".")))
do.call(substitute, list(fo, L))

On 5/1/06, Andrew Gelman <gelman at stat.columbia.edu> wrote:
> I want to write a function to standardize regression predictors, which
> will require me to do some character-string manipulation to parse the
> variables in a call to lm() or glm().
>
> For example, consider the call
> lm (y ~ female + I(age^2) + female:black + (age + education)*female).
>
> I want to be able to parse this to pick out the input variables
> ("female", "age", "black", "education").  Then I can transform these as
> appropriate (to get "z.female", "z.age", etc), feed them back into the
> lm() function, and go from there.
>
> Does anyone know an easy way to pull out the variables?  I basically
> have to parse out the symbols "+", ":", "*", and " ", but there's also
> the problem of handling parentheses and the I() operator.
>
> Thanks!
> Andrew
>
> --
> Andrew Gelman
> Professor, Department of Statistics
> Professor, Department of Political Science
> gelman at stat.columbia.edu
> www.stat.columbia.edu/~gelman
>
> Statistics department office:
>  Social Work Bldg (Amsterdam Ave at 122 St), Room 1016
>  212-851-2142
> Political Science department office:
>  International Affairs Bldg (Amsterdam Ave at 118 St), Room 731
>  212-854-7075
>
> Mailing address:
>  1255 Amsterdam Ave, Room 1016
>  Columbia University
>  New York, NY 10027-5904
>  212-851-2142
>  (fax) 212-851-2164
>
> ______________________________________________
> 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