[R] stepAIC
Christian Hoffmann
christian.hoffmann at lagonda.wsl.ch
Mon Jul 9 16:34:09 CEST 2001
I am running R-1.2.3 on Unix:
>uname -a
SunOS fluke 5.7 Generic_106541-12 sun4d sparc SUNW,SPARCserver-1000
Inside a function I want to call stepAIC:
regrbest <- function(x,y,ind,wt=NULL,warni=FALSE,plo=VIEW)
{ ## Determine the best regression with one variable ( -> reg1),
## and the best regression according to AIC ( -> regopt) using stepwise
regression
## on y ~ x[,ind] with weights wt
form <- as.formula(paste(y,paste(ind, collapse = "+"), sep =
"~"),env=parent.frame())
if (!missing(wt)) {
lm1 <-
lm(form,data=eval.parent(x,2),weights=eval.parent(wt,2),na.action=na.omit)
stw <- stepAIC(lm1,direction="forward",trace=3, k=2)
......
Problem:
Although the linear model is built with data (evaluated at the top level
?!?), stepAIC gets stuck:
Error in terms.formula(formula, data = data) :
Object "x" 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 = x, 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)
The stumbling block is in 5:
update.default does not eval(call..) in the correct top level frame.
Question:
Models within functions seem to be very delicate. What can be done to
remedy the situation?
Define update.default locally with the correct eval.parent(call,2) ???
Thanks for help.
-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