Re: [Rd] (PR#13735) LM/GLM can't find weights vector from within a function

From: Prof Brian Ripley <>
Date: Mon, 01 Jun 2009 09:12:09 +0100 (BST)

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, wrote:

> Full_Name: Alberto Gaidys
> Version: 2.9.0
> OS: Mac OS X 10.5.7
> Submission from: (NULL) (
> 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.
> ______________________________________________
> mailing list

Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list

Received on Mon 01 Jun 2009 - 08:21:50 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 01 Jun 2009 - 08:34:37 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive