Re: [R] update.default evaluating in wrong environment?

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Tue 10 Oct 2006 - 20:07:50 GMT

On 10/10/2006 1:32 PM, Martin C. Martin 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.

This is pretty old code: I think the particular line causing the problem you see was put there in 2000, but it was a workalike for code that was there already in 1998. This isn't to say that old code is necessarily right, but changing it could have lots of unwanted side effects. It's also likely that whatever was in the mind of whoever wrote the original shows up in other places, so tracking things down to make a consistent change would be a lot of work. You'd probably want to read the referenced 1992 Statistical Models definition of the expected behaviour here pretty closely (and recognize that the 1992 book is about S, not R, so we might not want to follow it exactly), etc., etc., etc.

All of which is to say that I wouldn't pick up this bug fix unless I was personally bitten by it: it looks like an awful lot of work. But there are others who may want to tackle it.

Duncan Murdoch

>
> - Martin

>> 
>> 
>> On 10/10/06, Martin C. Martin <martin@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@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.
>>>

>
> ______________________________________________
> R-help@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.


R-help@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. Received on Wed Oct 11 06:13:21 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 10 Oct 2006 - 20:30:36 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.