Skip to content
Prev 385472 / 398506 Next

Passing formula and weights error

This came up recently in a discussion of lm() on the R-devel list.  I'd 
assume the same issue applies to glm.

The problem is that the argument to weights is evaluated in the same way 
as arguments in the formula:  first in data, then in the environment of 
the formula.  The latter will eventually lead back to the global 
environment, but won't lead to the local evaluation frame in myglm().

The easiest solution is to add newweights to the data argument, but 
there are a few gotchas here.

First, if newweights is already a column in data, you'll mess things up. 
  So be sure to use a name that can't be there.  That's okay in your 
example.

The second problem is that a dot in the formula will cause problems, 
because it will try to include newweights as a predictor variable.  It's 
possible to work around this, but it's probably better to use a more 
complicated solution instead:  modify the formula environment so it 
starts with a small environment holding newweights.  You don't want to 
add newweights directly to environment(formula), because that will have 
side effects outside your function.

This version of your function takes this more complicated approach:

  myglm <- function(formula, data, weights){
      ## this works
      print(glm(formula, data, family=gaussian(), weights))
      env <- new.env(parent = environment(formula))
      env$newweights <- rep(1, n)
      environment(formula) <- env

      glm(formula, data, family=gaussian(), weights=newweights)
  }

Duncan Murdoch
On 28/08/2020 11:32 a.m., John Smith wrote: