[R] update.default evaluating in wrong environment?

Gabor Grothendieck ggrothendieck at gmail.com
Tue Oct 10 20:29:41 CEST 2006


It does seem strange to me too.  I guess what one could say
is that from an oo viewpoint the internal environment within f
is the object space and update needs to be a method of it which
could be arranged by including update in your lm object:

f <- function() {
   DF <- data.frame(y = 1:12, x1 = gl(2, 1, 12), x2 = gl(2,6))
   f.lm <- lm(y ~ x1, DF)
   f.lm$update <- function(object = f.lm, ...) update(object, ...)
   f.lm
}

f.lm <- f()
f.lm$update(formula = y ~ x2)


On 10/10/06, Martin C. Martin <martin at martincmartin.com> wrote:
>
>
> Gabor Grothendieck wrote:
> > As a workaround use evaluate=FALSE argument to update and
> > evaluate it yourself fetching the environment from the innards
> > of the lm structure:
> >
> > f <- function() {
> >   DF <- data.frame(y = 1:12, x1 = gl(2, 1, 12), x2 = gl(2,6))
> >   lm(y ~ x1, DF)
> > }
> >
> > f.lm <- f()
> > e <- attr(terms(f.lm), ".Environment")
> > eval(update(f.lm, formula = y ~ x2, evaluate = FALSE), e)
>
> Thanks, or even just:
>
> e <- environment(formula(f.lm))
>
> But this was more of a bug report.  Is update.default wrong?  Should it
> be changed?  I don't see how evaluating in update's parent environment
> would ever be better default behavior than the formula's environment.
>
> - Martin
> >
> >
> > On 10/10/06, Martin C. Martin <martin at martincmartin.com> wrote:
> >> Hi all,
> >>
> >> update.default, which is the method used to update "lm" objects (among
> >> others), extracts the "call" element from it's first argument, updates
> >> it, then evaluates it in the parent.frame().  Shouldn't it be evaluated
> >> in environment(formula(object)), if that's non-NULL?
> >>
> >> I ask because I call "lm" from within a function, and the data argument
> >> is a local variable of that function.  After that, I can't update the
> >> model any more, since the new lm() call (the one evaled in
> >> parent.frame()) can't find the data.
> >>
> >> Best,
> >> Martin
> >>
> >> ______________________________________________
> >> 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
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
>



More information about the R-help mailing list