Skip to content
Prev 310159 / 398502 Next

extract indep vars from formula

On Nov 7, 2012, at 4:03 PM, Alexander Shenkin <ashenkin at ufl.edu> wrote:

            
You might want to look at ?all.vars as one possible approach. The nice thing about all.vars() is that if you have transformations on the IV's in the formula (eg. poly(x, 3)), you will get the base variable names (eg. 'x', rather than 'poly(x, 3)').

A general approach would be:

  all.vars(formula(MODEL))

where the response variable would typically be the first element in the returned vector. So, if you just want the IV's you could use:

  all.vars(formula(MODEL))[-1]

An example:

# from ?cars
fm1 <- lm(log(dist) ~ log(speed), data = cars)
[1] "dist"  "speed"  


Regards,

Marc Schwartz