[R] Function environments lm() weights

Tobias Muhlhofer t.muhlhofer at lse.ac.uk
Fri May 27 13:38:00 CEST 2005

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

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.

	Tobias Muhlhofer

More information about the R-help mailing list