[R] glm and lm can't find weights

Marc Schwartz marc_schwartz at me.com
Mon Mar 11 21:28:58 CET 2013


On Mar 11, 2013, at 1:46 PM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote:

> Hello, and apologies for not providing an example. However, my question is
> more general.
> 
> I have a lengthy function. This function is using another internal function
> that modifies the data frame I am reading in. This internal function is
> using the command model.frame (with data and weights inside) and returns a
> data frame I am using for further analyses.
> However, when I try to run my function (which has an lm or a glm  commmand)
> using that new data frame and separately defined weights, I get an error:
> can't find your weights object.
> This happens despite the fact that I actually print the weights object
> right before the glm command. The object is there - I can see it using
> ls(). I checked and rechecked - there are no typos.
> Interestingly, this happens only when I run it as a function.
> 
> When I rename my arguments, go inside the function and run it line by line
> - I don't get this problem. Clearly, something is happening with my weights
> in the function environment. I was thinking - can it be that once I've used
> model.frame - everything else - like glm and lm - is confused as to what
> the weights are and doesn't want to take the weights I hand over to it but
> is looking for them elsewhere?
> 
> Thank you!
> Dimitri


Many discussions on this over the years, along with the 'subset' argument. From the Details section of ?lm:

  All of weights, subset and offset are evaluated in the same way as variables in formula, 
  that is first in data and then in the environment of formula.

You might also review:

  http://developer.r-project.org/nonstandard-eval.pdf

This post by Thomas from 2006 may be helpful:

  http://tolstoy.newcastle.edu.au/R/devel/06/06/5869.html

and the reply from Peter also:

  http://tolstoy.newcastle.edu.au/R/devel/06/06/5868.html

Regards,

Marc Schwartz



More information about the R-help mailing list