[R] Issue with step(): Fails to look for object$model

Bill.Venables at csiro.au Bill.Venables at csiro.au
Sun Feb 15 04:18:12 CET 2009

Without arguing you case I would point out that the data need not be there in l$model:

> l <- lm(y ~ x, data=ex, model = FALSE)
> l

lm(formula = y ~ x, data = ex, model = FALSE)

(Intercept)            x  
     0.1310      -0.1736  

> l$model

Model objects are often huge simply because by default they squirrel away a copy of the original data inside themselves.  One way to avoid memory problems can be only to keep this backup copy within the fitted model object only when you really need to do so, which is not all that often, in fact.

I can see why step() (and allies) do not work the way you think they should.  These functions use the call component of the fitted model object and modify that.  And if the call component says your data are in the data frame 'ex', they take you seriously.  They your word for it.  We're not dealing with Microsoft software here, you know.

Bill Venables

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Adam D. I. Kramer
Sent: Sunday, 15 February 2009 12:44 PM
To: r-help at r-project.org
Subject: [R] Issue with step(): Fails to look for object$model


 	I'm playing around with stepwise regression, using the step
function, and believe I have found a bug (or at least, a strong case for

> ex <- data.frame(y=rnorm(100),x=rnorm(100))
> l <- lm(y ~ x, data=ex)
> step(l) 
[output is correct]
> rm(ex)
> step(l)
Start:  AIC=11.79
y ~ x

        Df Sum of Sq     RSS     AIC
- x     1     0.120 108.221   9.900
<none>              108.100  11.789
Error in inherits(x, "data.frame") : object "ex" not found

...ex is not found, so step fails. However, all of the necessary data to run
the step function is present in l$model.

I would also argue that step() *should* use l$model if at all possible, as
it seems reasonable to expect that ex may undergo changes. Further, step()
does not appear to test any other columns present in ex (even if
direction="both" is specified), unless they are specified in scope.

If I am misunderstanding step() or if there is a good reason why it operates
this way, please let me know!


R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list