Skip to content
Prev 58843 / 63424 Next

lm() takes weights from formula environment

This is fairly clearly documented in ?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."

There are lots of possible places to look for weights, but this seems to 
me like a pretty sensible search order.  In most cases the environment 
of the formula will have a parent environment chain that eventually 
leads to the global environment, so (with no conflicts) your strategy of 
defining w there will sometimes work, but looks pretty unreliable.

When you say you want to work around this search order, I think the 
obvious way is to add your w vector to your d dataframe.  That way it is 
guaranteed to be found even if there's a conflicting variable in the 
formula environment, or the global environment.

Duncan Murdoch
On 09/08/2020 2:13 p.m., John Mount wrote: