Skip to content

LM/GLM can't find weights vector from within a function (PR#13735)

2 messages · gaidys at uol.com.br, Brian Ripley

#
Full_Name: Alberto Gaidys
Version: 2.9.0
OS: Mac OS X 10.5.7
Submission from: (NULL) (201.81.185.155)


When calling LM or GLM from within a function, R gives a message error that it
can't find the specified weights object "Erro em eval(expr, envir, enclos) :
objeto 'W' n?o encontrado" (Error in eval(expr, envir, enclos) : object 'W' not
found). 

The call from within the user-defined function where the error happens is:

   rReg<-lm(formula=rFormula,data=rData,subset=1:i,weights=W,x=TRUE,y=TRUE)

where rFormula is a text string, rData is a 1422 x 17 timeSeries data matrix,
both passed from the console, i is a counter, W is a 1422 x 1 numeric vector of
weights declared locally within the function.

The function definition is as follows:

   redls<-function(rFormula, rData, nStart, rBeta=1., nDelayed=3)

I already tried to define W externally to the function and parse it as a
parameter, change its name, declare it as an array, declare it as a timeSeries
object, with always the same error. Invoking glm instead of lm gives also the
same error. The ls.str listing of W immediately before calling lm, showing it
does exist, is:

   W :  num [1:1422(1d)] 0.00628 0.0064 0.00654 0.00667 0.00681 ...

The exactly same lm call, with the objects created exactly the same way from the
console produces no error though.
#
This seems to be as documented.  From ?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'.

We don't have a reproducible example and so do not know where rFormula 
was defined -- but it seems likely that W is not defined in its 
environment.  If you believe otherwise, we need a complete example.

Incidentally, str() reports that W is a 1D array, not a vector.
On Mon, 1 Jun 2009, gaidys at uol.com.br wrote: