From ripley at stats.ox.ac.uk  Mon Jun  1 10:12:09 2009
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Mon, 1 Jun 2009 09:12:09 +0100 (BST)
Subject: [Rd] (PR#13735) LM/GLM can't find weights vector from within a
 function
In-Reply-To: <20090601013512.88D6A2830314@mail.pubhealth.ku.dk>
References: <20090601013512.88D6A2830314@mail.pubhealth.ku.dk>
Message-ID: <alpine.LFD.2.00.0906010907290.19524@gannet.stats.ox.ac.uk>

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:

> 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.
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


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