[R] Addition to: lm weights

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


Hi all,

Sorry for the omission in the first post.

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

++++Also stepAIC will not behave as expected.

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
  nna <- apply(x[,c(ind,wt)],1,function(x) all(!is.na(x)))
  xx  <- x[nna,]
  wtt  <- !is.null(wt) && any(wt!=1)
  if (wtt) {  #  is TRUE here
    w <- xx[,wt]
    lm1 <- lm(form,data=xx,weights=w)
  else {
    lm1 <- lm(form,data=xx,na.action=na.omit) 
    stw <- stepAIC(lm1,direction="forward",trace=2, k=2)

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

Calling with
regrbest(Una,"vr3c1",ind)

will generate lm1 but will stall at stepAIC:
Error in terms.formula(formula, data = data) : 
	Object "xx" not found
> traceback()
9: terms.formula(formula, data = data)
8: terms(formula, data = data)
7: lm(formula = vr3c1 ~ vr91 + vr92 + vr18c1 + vr18c2 + vr191 + 
       vr192 + vr193 + vr211 + vr212 + vr221 + vr222 + vr223 + vr231 + 
       vr232 + F26REG.J + F27SEX + F28ALTER + F31PERS + vr361 + 
       vr362 + F37A + Urban + F6WISSEN + F7B1 + F7B2 + F7B3 + F7B4 + 
       F7B5 + F29CH + F30SCHUL + F33HUND + F35GART, data = xx, na.action =
na.omit)
6: eval(expr, envir, enclos)
5: eval(call, parent.frame())
4: update.default(fit, paste("~ .", change))
3: update(fit, paste("~ .", change))
2: stepAIC(lm1, direction = "forward", trace = 2, k = 2)
1: regrbest(Una, "vr3c1", ind.9P6)

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