[R] lm weights

Christian Hoffmann christian.hoffmann at lagonda.wsl.ch
Mon Jul 16 17:20:46 CEST 2001


Hi all,

I have difficulties in specifying a weight vector inside lm which is called
inside a function.

I have had the hope that this problem would go away by switching to R from
S+, as a variable generated inside a function should be visible to lm
inside it, but the evaluation of formulae seems to get in the way.

regrbest(Una,"vr3c1",ind,"W")
with names(Una) containing the elements of ind:
....    "vr91"  "vr92" ..  and additionally.... "W"

regrbest <- function(x,y,ind,wt=NULL)
  form <- as.formula(paste(y,paste(ind, collapse = "+"), sep = "~"),
env=parent.frame())   #  is env WRONG here???
# form = vr3c1 ~ vr91 + vr92   with the actual call
  wtt  <- !is.null(wt) && any(wt!=1)
  if (wtt) {  #  is TRUE here
    nna <- apply(x[,c(ind,wt)],1,function(x) all(!is.na(x)))
    xx  <- x[nna,]
    w <- xx[,wt]
    lm1 <- lm(form,data=xx,weights=w)

Error in eval(expr, envir, enclos) : Object "w" not found
The weights w are not found ????

Is there a general rule on how to treat this kind of problems?
I very often build linear models inside functions and temporary variables
seem to be a problem for models.

Thanks for your consideration.
-christian
Dr.sc.math.Christian W. Hoffmann
Mathematics and Statistical Computing
Landscape Modeling and Web Applications
Swiss Federal Research Institute WSL 
Zuercherstrasse 111
CH-8903 Birmensdorf, Switzerland
phone: ++41-1-739 22 77    fax: ++41-1-739 22 15
e-mail: christian.hoffmann_at_wsl.ch__prevent_spamming
www: http://www.wsl.ch/staff/christian.hoffmann/

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list