[R] help with weights in lm and glm

Thomas Lumley tlumley at u.washington.edu
Fri Jun 13 01:12:57 CEST 2003


On Sun, 27 Jan 2002, Roseli A. Leandro wrote:

>
>
>       Dear all
>
>       Could someone explain to why weights does not works in lm and glm
> in the example below?
>       Thanks in advance, Roseli.
>
>
>        function(model){
>
>         www<-fitted(model)
>         lm(formula(model),weights=www)
>
>        }
>
>        The message error is: Error in eval(expr,envir,enclos): object
> "www" not found.
>

Because it's looking in the environment of the model formula.

That's why it can find the other variables in the model even though you
didn't specify a dataframe to search.

You can use

function(model){
  www<-fitted(model)
  update(model, weights=www)
}

which does work (although it arguably shouldn't).

The handling of weights= in these modelling functions is a horrible wart
on the language.  Although they look like ordinary arguments they have
very strange scoping rules.


	-thomas




More information about the R-help mailing list