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

Adam D. I. Kramer adik at ilovebacon.org
Sun Feb 15 07:43:23 CET 2009


A fine answer, and indeed I am aware of the option to leave the model empty.
I like the option of consistently not being protected from myself.

That said, some other R functions (predict comes to mind) use $model if it
exists (which it usually does, though needn't) but allow new (or "other")
data to be provided.

So while I can't argue against trusting $call, I think there's a decent
argument that the data held within the model is the most trustworthy and
appropriate to use (though an explicitly named other data frame should
surely be useful as well).

In the meantime, I'll just modify l$call, and I thank you for that
suggestion.

--Adam


On Sun, 15 Feb 2009, Bill.Venables at csiro.au wrote:

> 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
>
> Call:
> lm(formula = y ~ x, data = ex, model = FALSE)
>
> Coefficients:
> (Intercept)            x
>     0.1310      -0.1736
>
>> l$model
> NULL
>
> 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
> http://www.cmis.csiro.au/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
>
> Hi,
>
> 	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
> improvement):
>
>> 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!
>
> --Adam
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> 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