Re: [Rd] A problem with glm() and possibly model-using functions in general?

From: Byron Ellis <ellis_at_stat.harvard.edu>
Date: Sat 19 Nov 2005 - 01:30:20 GMT

On Nov 18, 2005, at 4:35 PM, Thomas Lumley wrote:

> On Fri, 18 Nov 2005, Byron Ellis wrote:
>
>> So, consider the following:
>>
>>> example(glm)
>>> g = function(model) { w = runif(9);glm(model,weights=w); }
>>> g(counts ~ outcome + treatment)
>> Error in eval(expr, envir, enclos) : object "w" not found
>>

>> Huh?! I suspect that somebody is lazily evaluating arguments in the
>> wrong environment (probably GlobalEnv in this case). I'm willing to
>> accept the fact that there's some mysterious reason you'd actually
>> want this behavior, but this looks like it should be filed as a bug
>> to me.
>
> Yes, there is a reason you'd actually want this behaviour, and
> it is documented. In help(model.frame) it says
>
> All the variables in 'formula', 'subset' and in '...' are looked
> for first in 'data' and then in the environment of
> 'formula' (see
> the help for 'formula()' for further details) and collected
> into a
> data frame.
>
> In your example the environment of 'formula' is the global
> environment,
> since that's where it was created.
>
> There isn't a set of scoping rules for formulas that will make
> everyone
> happy, but this lexical scope is what R has done for quite some time.
>
> -thomas
>

Hrmm, well at least I know why it does what it does. I can't claim to like it, but I suspect thats a religious debate that won't be particularly useful.

> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

---
Byron Ellis (ellis@stat.harvard.edu)
"Oook" -- The Librarian

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Sat Nov 19 12:35:14 2005

This archive was generated by hypermail 2.1.8 : Sat 19 Nov 2005 - 11:21:00 GMT