Select a formula-based model by AIC.
Usage
# S3 method for default
step(object, ...)
Arguments
- object
an object representing a model of an appropriate class (mainly
"lm"
and"glm"
). This is used as the initial model in the stepwise search.- ...
any additional arguments to
extractAIC
.
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).
Details
step
uses add1
and drop1
repeatedly; it will work for any method for which they work, and that
is determined by having a valid method for extractAIC
.
When the additive constant can be chosen so that AIC is equal to
Mallows' \(C_p\), this is done and the tables are labelled
appropriately.
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
specifies the upper
component, and the lower
model is
empty. If scope
is missing, the initial model is used as the
upper
model.
Models specified by scope
can be templates to update
object
as used by update.formula
. So using
.
in a scope
formula means ‘what is
already there’, with .^2
indicating all interactions of
existing terms.
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 "glm"
method for
function extractAIC
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
.)
Examples
# \donttest{
## following on from example(lm)
utils::example("lm", echo = FALSE)
step(lm.D9)
#> Start: AIC=-12.58
#> weight ~ group
#>
#> Df Sum of Sq RSS AIC
#> - group 1 0.6882 9.4175 -13.063
#> <none> 8.7292 -12.581
#>
#> Step: AIC=-13.06
#> weight ~ 1
#>
#>
#> Call:
#> lm(formula = weight ~ 1)
#>
#> Coefficients:
#> (Intercept)
#> 4.847
#>
summary(lm1 <- lm(Fertility ~ ., data = swiss))
#>
#> Call:
#> lm(formula = Fertility ~ ., data = swiss)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -15.2743 -5.2617 0.5032 4.1198 15.3213
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 66.91518 10.70604 6.250 1.91e-07 ***
#> Agriculture -0.17211 0.07030 -2.448 0.01873 *
#> Examination -0.25801 0.25388 -1.016 0.31546
#> Education -0.87094 0.18303 -4.758 2.43e-05 ***
#> Catholic 0.10412 0.03526 2.953 0.00519 **
#> Infant.Mortality 1.07705 0.38172 2.822 0.00734 **
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 7.165 on 41 degrees of freedom
#> Multiple R-squared: 0.7067, Adjusted R-squared: 0.671
#> F-statistic: 19.76 on 5 and 41 DF, p-value: 5.594e-10
#>
slm1 <- step(lm1)
#> Start: AIC=190.69
#> Fertility ~ Agriculture + Examination + Education + Catholic +
#> Infant.Mortality
#>
#> Df Sum of Sq RSS AIC
#> - Examination 1 53.03 2158.1 189.86
#> <none> 2105.0 190.69
#> - Agriculture 1 307.72 2412.8 195.10
#> - Infant.Mortality 1 408.75 2513.8 197.03
#> - Catholic 1 447.71 2552.8 197.75
#> - Education 1 1162.56 3267.6 209.36
#>
#> Step: AIC=189.86
#> Fertility ~ Agriculture + Education + Catholic + Infant.Mortality
#>
#> Df Sum of Sq RSS AIC
#> <none> 2158.1 189.86
#> - Agriculture 1 264.18 2422.2 193.29
#> - Infant.Mortality 1 409.81 2567.9 196.03
#> - Catholic 1 956.57 3114.6 205.10
#> - Education 1 2249.97 4408.0 221.43
summary(slm1)
#>
#> Call:
#> lm(formula = Fertility ~ Agriculture + Education + Catholic +
#> Infant.Mortality, data = swiss)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -14.6765 -6.0522 0.7514 3.1664 16.1422
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 62.10131 9.60489 6.466 8.49e-08 ***
#> Agriculture -0.15462 0.06819 -2.267 0.02857 *
#> Education -0.98026 0.14814 -6.617 5.14e-08 ***
#> Catholic 0.12467 0.02889 4.315 9.50e-05 ***
#> Infant.Mortality 1.07844 0.38187 2.824 0.00722 **
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 7.168 on 42 degrees of freedom
#> Multiple R-squared: 0.6993, Adjusted R-squared: 0.6707
#> F-statistic: 24.42 on 4 and 42 DF, p-value: 1.717e-10
#>
slm1$anova
#> Step Df Deviance Resid. Df Resid. Dev AIC
#> 1 NA NA 41 2105.043 190.6913
#> 2 - Examination 1 53.02656 42 2158.069 189.8606
# }