stepAIC(object, scope, scale = 0, direction = c("both", "backward", "forward"), trace = 1, keep = NULL, steps = 1000, use.start = F, k = 2, ...) extractAIC(fit, scale, k = 2, ...)
upper
and
lower
, both formulae. See the details
for how to specify the formulae and how they are used.
lm
,
aov
and
glm
models.
"both"
,
"backward"
, or
"forward"
, with a default of
"both"
. If the
scope
argument is missing the default for
direction
is
"backward"
.
stepAIC
. Larger values may give more
information on the fitting process.
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.
glm
(and other fits),
but it can also slow them down.
k=2
gives the genuine AIC:
k = log(n)
is sometimes referred to as
BIC or SBC.
extractAIC
. (None are currently used.)
"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).
If the original fit is a formal (`S4' class) the result is a list with
components
fit
,
anova
and perhaps
keep
.
stepAIC
differs from
step
and especially
step.glm
in using the exact AIC rather
than potentially misleading one-step approximations. It is also much
more widely applicable: all that is required is a method for
extractAIC
, which should return a vector
c(modeldf, AIC)
.
The default method handles linear models (
lm
,
aov
and
glm
of family
"Gaussian"
with identity link) using
addterm.lm
and
dropterm.lm
: for these the results are similar to
step.glm
except
that the AIC quoted is Akaike
s not Hastie
s. (The additive constant
is chosen so that in that case AIC is identical to Mallows' Cp if the
scale is known.)
The set of models searched is determined by the
scope
argument. The right-hand-side of
its
lower
component is always included in
the model, and right-hand-side of the model is included in the
upper
component. If
scope
is a single formula, it specifes
the
upper
component, and the
lower
model is empty. If
scope
is missing, the initial model is
used as the
upper
model.
There is a potential problem in using
glm
fits with a variable
scale
, as in that
case the deviance is not simply related to the maximized
log-likelihood. The function
extractAIC.glm
makes the appropriate
adjustment for a
gaussian
family, but may
need to be amended for other cases. (The
binomial
and
poisson
families have fixed
scale
by default and do not correspond to
a particular maximum-likelihood problem for variable
scale
.)
Where a conventional deviance exists (e.g. for
lm
,
aov
and
glm
fits) this is quoted in the analysis
of variance table: it is the
unscaled deviance.
The model fitting must apply the models to the same dataset. This may
be a problem if there are missing values and an
na.action
other than
na.fail
is used (as may be the default if
options(na.action=)
has been set,
including in R). We suggest you remove the missing values first.
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 = F) quine.stp$anova cpus1 <- cpus attach(cpus) for(v in names(cpus)[2:7]) cpus1[[v]] <- cut(cpus[[v]], unique(quantile(cpus[[v]])), include.lowest = T) 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=F) cpus.lm2$anova birthwt.glm <- glm(low ~ ., family=binomial, data=bwt) birthwt.step <- stepAIC(birthwt.glm, trace=F) birthwt.step$anova birthwt.step2 <- stepAIC(birthwt.glm, ~ .^2 + I(scale(age)^2) + I(scale(lwt)^2), trace=F) birthwt.step2$anova quine.nb <- glm.nb(Days ~ .^4, data=quine) quine.nb2 <- stepAIC(quine.nb) quine.nb2$anova