Skip to content
Prev 33596 / 398513 Next

stepwise regression

Consider the following:

 > library(MASS)
 > Dat <- data.frame(x1=rnorm(9), x2=rnorm(9), y1=rnorm(9), y2=rnorm(9))
 > mdl <- "~x1+x2+I(x1^2)+I(x2^2)+x1:x2"
 > y <- names(Dat)[3:4]
 > fits <- list()
 > for(i in 1:2){
+  fit0 <- lm(formula(paste(y[i], "~1")), Dat)
+  fits[[i]] <- stepAIC(fit0, mdl)
+ }
Start:  AIC= 0.49
  y1 ~ 1

           Df Sum of Sq    RSS    AIC
<none>                 7.6059 0.4854
+ I(x2^2)  1    0.6293 6.9766 1.7081
+ I(x1^2)  1    0.4443 7.1616 1.9436
+ x2       1    0.2894 7.3165 2.1362
+ x1       1    0.1996 7.4064 2.2460
Start:  AIC= 2.36
  y2 ~ 1

           Df Sum of Sq     RSS     AIC
+ I(x2^2)  1    6.3086  3.0593 -5.7113
<none>                  9.3679  2.3606
+ x2       1    1.1277  8.2402  3.2062
+ x1       1    0.5651  8.8028  3.8006
+ I(x1^2)  1    0.2631  9.1048  4.1042

Step:  AIC= -5.71
  y2 ~ I(x2^2)

           Df Sum of Sq     RSS     AIC
+ x2       1    1.1870  1.8723 -8.1306
+ I(x1^2)  1    0.9861  2.0732 -7.2132
+ x1       1    0.6241  2.4353 -5.7645
<none>                  3.0593 -5.7113
- I(x2^2)  1    6.3086  9.3679  2.3606

Step:  AIC= -8.13
  y2 ~ I(x2^2) + x2

           Df Sum of Sq     RSS     AIC
+ I(x1^2)  1    0.3976  1.4747 -8.2790
<none>                  1.8723 -8.1306
+ x1       1    0.0153  1.8569 -6.2046
- x2       1    1.1870  3.0593 -5.7113
- I(x2^2)  1    6.3679  8.2402  3.2062

Step:  AIC= -8.28
  y2 ~ I(x2^2) + x2 + I(x1^2)

           Df Sum of Sq     RSS     AIC
<none>                  1.4747 -8.2790
- I(x1^2)  1    0.3976  1.8723 -8.1306
- x2       1    0.5985  2.0732 -7.2132
+ x1       1    0.0009  1.4738 -6.2845
- I(x2^2)  1    6.7490  8.2236  5.1881
 > fits
[[1]]

Call:
lm(formula = y1 ~ 1, data = Dat)

Coefficients:
(Intercept)
      0.1444


[[2]]

Call:
lm(formula = y2 ~ I(x2^2) + x2 + I(x1^2), data = Dat)

Coefficients:
(Intercept)      I(x2^2)           x2      I(x1^2)
      1.1374      -0.6926       0.2962      -0.3413
#############
Does this answer the question?
Spencer Graves
###################################
Hi Spencer,

The step and stepAIC functions are certainly different than the
"stepwise" function in S-PLUS, but I think they will help.  I had found them
before emailing my question to the list but was hoping to find a function in
R exactly like "stepwise" in S-PLUS.  As far as I can tell right now it
doesn't exist.  I have on the order of 60 predictor variables that I would
like to run through a stepwise procedure.  Is there a shortcut available to
avoid typing the name of each of these 60 variables when specifying the 
model
formula?  All of the variables are in successive columns in the same matrix.
If there is no shortcut I suppose I'll just write a function to do the job.

Thanks again,
Andy Taylor
Spencer Graves wrote: