[R] Step function

Thomas Lumley thomas at biostat.washington.edu
Wed Nov 29 19:30:26 CET 2000

On Wed, 29 Nov 2000, Matt Pocernich wrote:

> I am having problem using the step function for a linear regression model.  I've created an initial model containing only the intercept.  Then using the step function, I've selected three variables to be considered for the model. 
> > x0.lm<- lm(MEDV~1, data = x)
> > 
> > anova(x0.lm)
> Analysis of Variance Table
> Response: MEDV
>            Df Sum Sq Mean Sq F value Pr(>F)
> Residuals 505  42716      85               
> > 
> > step(x0.lm, ~ CRIM + ZN + RM)
> Start:  AIC= 2246.51 
>  MEDV ~ 1 
> Error in eval(expr, envir, enclos) : Object "MEDV" not found
> >
> I am not clear as to why the MEDV object ( the variable I am trying to
> predict) is not found.  I receive an identical message using the
> command > step(x0.lm).

This is a bug, I think.  The problem is that the data frame is called
x. If it were called something else there wouldn't be a problem, so the
workaround is to change the name of your data frame.

Looking at traceback() the problem is that the data frame `x' is being
searched for in the environment of add1.lm by model.frame.lm().  In most
cases it doesn't find it there and goes back to the global environment,
but if the data frame is called `x' (or one of 17 other things) it finds
the wrong thing in environment(add1.lm).

In R1.1.1 I'm not sure what can be done about this -- model.frame.lm
doesn't know where to find `x' and there isn't a general solution.  In
R1.2 we have more information and should be able to look in
environment(formula(lmobject)), but we aren't doing that yet.


Thomas Lumley
Assistant Professor, Biostatistics
University of Washington, Seattle

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