Choose a model by AIC in a Stepwise Algorithm
Usage
stepAIC(object, scope, scale, direction=c("both", "backward", "forward"),
trace=1, keep=NULL, steps=1000, use.start=FALSE, k=2, ...)
Arguments
object
|
an object representing a model of an appropriate class.
This is used as the initial model in the stepwise search.
|
scope
|
defines the range of models examined in the stepwise search.
|
scale
|
used in the definition of the AIC statistic for selecting the models,
currently only for lm , aov and glm models.
|
direction
|
the mode of stepwise search, can be one of "both" , "backward" ,
or "forward" , with a default of "both" .
If the scope argument is missing,
the default for direction is "backward" .
|
trace
|
if positive, information is printed during the running of stepAIC() .
Larger values may give more information on the fitting process.
|
keep
|
a filter function whose input is a fitted model object and the
associated AIC statistic, and whose output is arbitrary.
Typically keep will select a subset of the components of
the object and return them. The default is not to keep anything.
|
steps
|
the maximum number of steps to be considered. The default is 1000
(essentially as many as required). It is typically used to stop the
process early.
|
use.start
|
Not used in R.
|
k
|
the multiple of the number of degrees of freedom used for the penalty.
Only k=2 gives the genuine AIC: k = log(n) is sometimes referred
to as BIC or SBC.
|
...
|
any additional arguments to extractAIC . (None are currently used.)
|
Description
Performs stepwise model selection by exact AIC.Value
the stepwise-selected model is returned, with up to two additional
components. There is an "anova"
component corresponding to the
steps taken in the search, as well as a "keep"
component if the
keep=
argument was supplied in the call. The "Resid. Dev"
column
of the analysis of deviance table refers to a constant minus twice the
maximized log likelihood: it will be a deviance only in cases where a
saturated model is well-defined (thus excluding lm
, aov
and
survreg
fits, for example).See Also
addterm
, dropterm
, step
, step.glm
Examples
data(quine)
quine.hi <- aov(log(Days + 2.5) ~ .^4, quine)
quine.nxt <- update(quine.hi, . ~ . - Eth:Sex:Age:Lrn)
quine.stp <- stepAIC(quine.nxt,
scope = list(upper = ~Eth*Sex*Age*Lrn, lower = ~1),
trace = FALSE)
quine.stp$anova
data(cpus)
cpus1 <- cpus
attach(cpus)
for(v in names(cpus)[2:6])
cpus1[[v]] <- cut(cpus[[v]], quantile(cpus[[v]]),
include.lowest = TRUE)
detach()
set.seed(123)
cpus0 <- cpus1[, 2:8] # excludes names, authors' predictions
cpus.samp <- sample(1:209, 100)
cpus.lm <- lm(log10(perf) ~ ., data=cpus1[cpus.samp,2:8])
cpus.lm2 <- stepAIC(cpus.lm, trace=FALSE)
cpus.lm2$anova
data(birthwt)
birthwt.glm <- glm(low ~ ., family=binomial, data=bwt)
birthwt.step <- stepAIC(birthwt.glm, trace=FALSE)
birthwt.step$anova
birthwt.step2 <- stepAIC(birthwt.glm, ~ .^2 + I(scale(age)^2)
+ I(scale(lwt)^2), trace=FALSE)
birthwt.step2$anova
quine.nb <- glm.nb(Days ~ .^4, data=quine)
quine.nb2 <- stepAIC(quine.nb)
quine.nb2$anova