Winfried Theis
2003-Jan-29 10:55 UTC
[R] Scoping rule problem?!? step does not work in function
Hello! I would like to use step() in a function because I have a list of responses (independent!) with equal influence and want to apply stepwise regression on each of them. So the function I just tested is the following: fitlms <- function(y,infl,formel=NULL){ if(is.null(formel)){ var.names <- names(infl) dcv <- length(var.names) formel <- paste("y~",paste(rep("I(",dcv), var.names, rep("^2)",dcv),sep="",collapse="+"),"+",paste(var.names,collapse="*"),sep="") formel <- as.formula(formel) } erg <- lm(formel,infl) erg <- step(erg) return(erg)} Using this I get the following:> test <- fitlms(grob.erg[,1],grob.erg[,3:5])Start: AIC= -30.84 y ~ I(f^2) + I(vS^2) + I(o^2) + f + vS + o + f:vS + f:o + vS:o + f:vS:o Df Sum of Sq RSS AIC - f:vS:o 1 0.060 2.052 -32.188 <none> 1.992 -30.842 - I(o^2) 1 0.762 2.754 -25.717 - I(f^2) 1 2.581 4.573 -14.558 - I(vS^2) 1 5.341 7.333 -4.170 Error in model.frame.default(formula = y ~ I(f^2) + I(vS^2) + I(o^2) + : Object "infl" not found So the first step carried out correctly and then it seems to look into the wrong environment (presumably the top-level). I checked with the code of step, but could not spot the cause of this behaviour. At some place it looks at the parent.frame, which should be the environment created by function fitlms if I do unserstand things correctly. I checked also whether I can give step an environment in which to operate, but could not find any such option. I presume I could just use a for-loop, which might not be much slower than lapply in this case. I simply would like to understand things better to avoid similar errors. Data is given below. Thanks for your attention and any help appreciated, Winfried> grob.erggwRZ gwRA f vS o WV 1 3.562667 0.5742667 0.185 90.000 300.000 WV 2 3.585733 0.6028333 0.185 90.000 300.000 WV 3 3.893233 0.7162333 0.185 90.000 300.000 WV 4 4.035933 0.7603667 0.139 111.213 370.711 WV 5 2.250800 0.3411000 0.120 90.000 300.000 WV 6 4.342833 0.8782667 0.231 111.213 370.711 WV 7 2.537000 0.4682333 0.231 68.787 370.711 WV 8 3.058767 0.4457333 0.185 90.000 400.000 WV 9 3.613333 0.6000000 0.139 68.787 370.711 WV 10 4.757700 1.0705333 0.231 111.213 229.289 WV 11 4.491800 0.9555000 0.185 60.000 300.000 WV 12 3.316133 0.5622333 0.139 111.213 229.289 WV 13 3.364567 0.5709333 0.139 68.787 229.289 WV 14 3.834867 0.6982333 0.185 90.000 200.000 WV 15 4.116233 0.6855000 0.231 68.787 229.289 WV 16 3.769800 0.7355333 0.185 90.000 300.000 WV 17 6.631733 1.6468333 0.185 120.000 300.000 WV 18 4.282933 0.7365333 0.185 90.000 300.000 WV 19 3.784633 0.7029000 0.185 90.000 300.000 WV 20 3.669567 0.6854000 0.250 90.000 300.000 WV 21 4.548433 0.7231000 0.185 90.000 300.000 WV 21a 3.387467 0.5558667 0.185 90.000 300.000 --------------------------------------------------------------------- E-Mail: Winfried Theis <theis at statistik.uni-dortmund.de> Date: 29-Jan-03 Dipl.-Math. Winfried Theis SFB 475, Fachbereich Statistik, Universit"at Dortmund, 44221 Dortmund Tel.: +49-231-755-5903 FAX: +49-231-755-4387 ----------------------------------------------------------------------