I am trying to replicate the first example from stepAIC from the MASS package with my own dataset but am running into error. If someone can point where I have gone wrong, I would appreciate it very much. Here is an example : set.seed(1) df <- data.frame( x1=rnorm(1000), x2=rnorm(1000), x3=rnorm(1000) ) df$y <- 0.5*df$x1 + rnorm(1000, mean=8, sd=0.5) # pairs(df); head(df) lo <- aov( y ~ 1, data=df ) hi <- aov( y ~ .^2, data=df ) mid <- aov( y ~ x2 + x3, data=df ) Running any of the following commands stepAIC( mid, scope=list(upper = ~x1 + x2 + x3 , lower = ~1) ) stepAIC( mid, scope=list(upper = hi , lower = lo) ) addterm( mid, ~ x1 + x2 + x3 ) addterm( lo, hi ) gives the same error message : Error in eval(expr, envir, enclos) : invalid second argument Here is a traceback of the first failed command : 14: eval(predvars, data, env) 13: model.frame.default(formula = y ~ x2 + x3 + x1, data = df, drop.unused.levels = TRUE) 12: model.frame(formula = y ~ x2 + x3 + x1, data = df, drop.unused.levels = TRUE) 11: eval(expr, envir, enclos) 10: eval(mf, parent.frame()) 9: lm(formula = y ~ x2 + x3 + x1, data = df, method = "model.frame") 8: eval(expr, envir, enclos) 7: eval(fcall, env, parent.frame()) 6: model.frame.lm(fob, xlev = object$xlevels) 5: model.frame(fob, xlev = object$xlevels) 4: stats:::add1.lm(object, scope = scope, scale = scale) 3: addterm.lm(fit, scope$add, scale = scale, trace = max(0, trace - 1), k = k, ...) 2: addterm(fit, scope$add, scale = scale, trace = max(0, trace - 1), k = k, ...) 1: stepAIC(mid, scope = list(upper = ~x1 + x2 + x3, lower = ~1)) Any pointers would be much appreciated. Thank you. Regards, Adai
Try not to use the name of an R object ... the error is caused by using 'df' as the second argument to eval(). It works with DF in place of df. I don;t understand your subject line: that is not the error message you received. On Mon, 15 Aug 2005, Adaikalavan Ramasamy wrote:> I am trying to replicate the first example from stepAIC from the MASS > package with my own dataset but am running into error. If someone can > point where I have gone wrong, I would appreciate it very much. > > Here is an example : > > set.seed(1) > df <- data.frame( x1=rnorm(1000), x2=rnorm(1000), x3=rnorm(1000) ) > df$y <- 0.5*df$x1 + rnorm(1000, mean=8, sd=0.5) > # pairs(df); head(df) > > lo <- aov( y ~ 1, data=df ) > hi <- aov( y ~ .^2, data=df ) > mid <- aov( y ~ x2 + x3, data=df ) > > Running any of the following commands > > stepAIC( mid, scope=list(upper = ~x1 + x2 + x3 , lower = ~1) ) > stepAIC( mid, scope=list(upper = hi , lower = lo) ) > addterm( mid, ~ x1 + x2 + x3 ) > addterm( lo, hi ) > > gives the same error message : > Error in eval(expr, envir, enclos) : invalid second argument > > Here is a traceback of the first failed command : > 14: eval(predvars, data, env) > 13: model.frame.default(formula = y ~ x2 + x3 + x1, data = df, drop.unused.levels = TRUE) > 12: model.frame(formula = y ~ x2 + x3 + x1, data = df, drop.unused.levels = TRUE) > 11: eval(expr, envir, enclos) > 10: eval(mf, parent.frame()) > 9: lm(formula = y ~ x2 + x3 + x1, data = df, method = "model.frame") > 8: eval(expr, envir, enclos) > 7: eval(fcall, env, parent.frame()) > 6: model.frame.lm(fob, xlev = object$xlevels) > 5: model.frame(fob, xlev = object$xlevels) > 4: stats:::add1.lm(object, scope = scope, scale = scale) > 3: addterm.lm(fit, scope$add, scale = scale, trace = max(0, trace - 1), k = k, ...) > 2: addterm(fit, scope$add, scale = scale, trace = max(0, trace - 1), k = k, ...) > 1: stepAIC(mid, scope = list(upper = ~x1 + x2 + x3, lower = ~1)) > > Any pointers would be much appreciated. Thank you. > > Regards, Adai > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- 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
Adai, The following works.Perhaps you should define your 'upper' and 'lower' in the list as aov's, as you have done with your lo,hi and mid. John> stepAIC( mid, scope=list(upper = mid , lower = lo) )Start: AIC= -594.66 y ~ x2 + x3 Df Sum of Sq RSS AIC - x2 1 0.11 548.56 -596.45 - x3 1 0.95 549.40 -594.93 <none> 548.45 -594.66 Step: AIC= -596.45 y ~ x3 Adaikalavan Ramasamy wrote --- I am trying to replicate the first example from stepAIC from the MASS package with my own dataset but am running into error. If someone can point where I have gone wrong, I would appreciate it very much. Here is an example : set.seed(1) df <- data.frame( x1=rnorm(1000), x2=rnorm(1000), x3=rnorm(1000) ) df$y <- 0.5*df$x1 + rnorm(1000, mean=8, sd=0.5) # pairs(df); head(df) lo <- aov( y ~ 1, data=df ) hi <- aov( y ~ .^2, data=df ) mid <- aov( y ~ x2 + x3, data=df ) Running any of the following commands stepAIC( mid, scope=list(upper = ~x1 + x2 + x3 , lower = ~1) ) stepAIC( mid, scope=list(upper = hi , lower = lo) ) addterm( mid, ~ x1 + x2 + x3 ) addterm( lo, hi ) gives the same error message : Error in eval(expr, envir, enclos) : invalid second argumen