Skip to content

Function environments lm() weights

1 message · Tobias Muhlhofer

#
I am writing a function of weighted regression, as a procedure for 
heteroskedasticity.

The function runs an auxiliary regression whose fitted values I assign 
to fit, and then I go:

     w <- 1/(exp(fit/2))

     ## Rerun the old regression ##
     if(gls) {
       wtd.model <- glm(model, weights=w)
     }

     if(!gls) {
       wtd.model <- lm(model, weights=w, x=TRUE)
     }

In this version, R complains that it can't find w. How can I tell it to 
look for w in the function's environment, rather than in environment 1 
or whatever?

An easy workaround, of course, is to superassign w and remove it 
afterwards, but that's a little messy, in case the user already has a 
variable called w in his environment.

Thanks,
	Tobias Muhlhofer