[R] stepwise regression

Spencer Graves spencer.graves at pdf.com
Fri Jun 20 23:38:53 CEST 2003


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:
> Have you considered stepAIC in library(MASS) or step?
> 
> hth.  spencer graves
> 
> Andrew Taylor wrote:
> 
>> Hi,
>>
>> S-PLUS includes the function "stepwise" which can use a variety of
>> methods to conduct stepwise multiple linear regression on a set of
>> predictors.  Does a similar function exist in R?  I'm having difficulty
>> finding one.  If there is one it must be under a different name because
>> I get an error message when I try 'help(stepwise)' in R.
>>
>> Thanks for your help,
>> Andy Taylor
>>
>>
>> ------------------------------------------------------------------------
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 
> 
>




More information about the R-help mailing list