Last data update: 2014.03.03
R: Stepwise Model Selection
Stepwise Model Selection
Description
This function is a front end to the stepAIC
function in the
MASS package.
Usage
stepwise(mod,
direction = c("backward/forward", "forward/backward", "backward", "forward"),
criterion = c("BIC", "AIC"), ...)
Arguments
mod
a model object of a class that can be handled by stepAIC
.
direction
if "backward/forward"
(the default), selection starts with
the full model and eliminates predictors one at a time, at each step considering whether the
criterion will be improved by adding back in a variable removed at a previous step;
if "forward/backwards"
, selection starts with a model including only a constant,
and adds predictors one at a time, at each step considering whether the criterion
will be improved by removing a previously added variable; "backwards"
and
"forward"
are similar without the reconsideration at each step.
criterion
for selection. Either "BIC"
(the default) or "AIC"
. Note that
stepAIC
labels the criterion in the output as "AIC"
regardless of which
criterion is employed.
...
arguments to be passed to stepAIC
.
Value
The model selected by stepAIC
.
Author(s)
John Fox jfox@mcmaster.ca
References
W. N. Venables and B. D. Ripley
Modern Applied Statistics Statistics with S, Fourth Edition
Springer, 2002.
See Also
stepAIC
Examples
# adapted from ?stepAIC in MASS
if (require(MASS)){
data(birthwt)
bwt <- with(birthwt, {
race <- factor(race, labels = c("white", "black", "other"))
ptd <- factor(ptl > 0)
ftv <- factor(ftv)
levels(ftv)[-(1:2)] <- "2+"
data.frame(low = factor(low), age, lwt, race, smoke = (smoke > 0),
ptd, ht = (ht > 0), ui = (ui > 0), ftv)
})
birthwt.glm <- glm(low ~ ., family = binomial, data = bwt)
print(stepwise(birthwt.glm, trace = FALSE))
print(stepwise(birthwt.glm, direction="forward/backward"))
}
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(RcmdrMisc)
Loading required package: car
Loading required package: sandwich
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/RcmdrMisc/stepwise.Rd_%03d_medium.png", width=480, height=480)
> ### Name: stepwise
> ### Title: Stepwise Model Selection
> ### Aliases: stepwise
> ### Keywords: models
>
> ### ** Examples
>
> # adapted from ?stepAIC in MASS
> if (require(MASS)){
+ data(birthwt)
+ bwt <- with(birthwt, {
+ race <- factor(race, labels = c("white", "black", "other"))
+ ptd <- factor(ptl > 0)
+ ftv <- factor(ftv)
+ levels(ftv)[-(1:2)] <- "2+"
+ data.frame(low = factor(low), age, lwt, race, smoke = (smoke > 0),
+ ptd, ht = (ht > 0), ui = (ui > 0), ftv)
+ })
+ birthwt.glm <- glm(low ~ ., family = binomial, data = bwt)
+ print(stepwise(birthwt.glm, trace = FALSE))
+ print(stepwise(birthwt.glm, direction="forward/backward"))
+ }
Loading required package: MASS
Direction: backward/forward
Criterion: BIC
Call: glm(formula = low ~ lwt + ptd + ht, family = binomial, data = bwt)
Coefficients:
(Intercept) lwt ptdTRUE htTRUE
1.01737 -0.01728 1.40677 1.89397
Degrees of Freedom: 188 Total (i.e. Null); 185 Residual
Null Deviance: 234.7
Residual Deviance: 210.1 AIC: 218.1
Direction: forward/backward
Criterion: BIC
Start: AIC=239.91
low ~ 1
Df Deviance AIC
+ ptd 1 221.90 232.38
+ lwt 1 228.69 239.17
<none> 234.67 239.91
+ ui 1 229.60 240.08
+ smoke 1 229.81 240.29
+ ht 1 230.65 241.13
+ age 1 231.91 242.40
+ race 2 229.66 245.39
+ ftv 2 232.09 247.81
Step: AIC=232.38
low ~ ptd
Df Deviance AIC
<none> 221.90 232.38
+ age 1 217.30 233.02
+ lwt 1 217.50 233.22
+ ht 1 217.66 233.39
+ ui 1 219.12 234.85
+ smoke 1 219.33 235.05
+ race 2 217.02 237.99
+ ftv 2 217.88 238.84
- ptd 1 234.67 239.91
Call: glm(formula = low ~ ptd, family = binomial, data = bwt)
Coefficients:
(Intercept) ptdTRUE
-1.057 1.463
Degrees of Freedom: 188 Total (i.e. Null); 187 Residual
Null Deviance: 234.7
Residual Deviance: 221.9 AIC: 225.9
>
>
>
>
>
> dev.off()
null device
1
>