通過step
給出的模式是「最優」的模式,而不是初始模型。
下面是一個說明性示例:
# Linear data generating process
set.seed(1)
n <- 100
X <- matrix(rnorm(n*5),nrow=n)
betas <- c(-1.5,2,0,-2,0,0)
y <- cbind(rep(1,n), X) %*% betas + 0.5*rnorm(n)
dtset <- data.frame(y, X)
# Initial full model
lmfit <- lm(y~., data=dtset)
# Backward selection
model.aic.backward <- step(lmfit, direction = "backward", trace = 1)
這裏是step
Start: AIC=-137.45
y ~ X1 + X2 + X3 + X4 + X5
Df Sum of Sq RSS AIC
- X2 1 0.01 22.44 -139.41
- X5 1 0.11 22.54 -138.97
- X4 1 0.22 22.66 -138.47
<none> 22.44 -137.44
- X1 1 294.14 316.57 125.24
- X3 1 422.44 444.87 159.26
Step: AIC=-139.41
y ~ X1 + X3 + X4 + X5
Df Sum of Sq RSS AIC
- X5 1 0.12 22.56 -140.89
- X4 1 0.22 22.66 -140.45
<none> 22.44 -139.41
- X1 1 294.37 316.82 123.32
- X3 1 423.21 445.65 157.44
Step: AIC=-140.89
y ~ X1 + X3 + X4
Df Sum of Sq RSS AIC
- X4 1 0.23 22.79 -141.89
<none> 22.56 -140.89
- X1 1 299.18 321.74 122.86
- X3 1 423.79 446.35 155.59
Step: AIC=-141.89
y ~ X1 + X3
Df Sum of Sq RSS AIC
<none> 22.79 -141.89
- X1 1 300.89 323.67 121.46
- X3 1 431.38 454.17 155.33
和這裏的模型中的model.aic.backward
物體內部的輸出:
summary(model.aic.backward)
############
Call:
lm(formula = y ~ X1 + X3, data = dtset)
Residuals:
Min 1Q Median 3Q Max
-1.2168 -0.3057 -0.0047 0.3693 1.0416
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.51531 0.04885 -31.02 <2e-16 ***
X1 1.94126 0.05424 35.79 <2e-16 ***
X3 -2.01862 0.04711 -42.85 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.4847 on 97 degrees of freedom
Multiple R-squared: 0.9693, Adjusted R-squared: 0.9687
F-statistic: 1531 on 2 and 97 DF, p-value: < 2.2e-16
這是後向後模型選擇,而不是最初的模式。
希望這可以幫助你。
無法重現。使用'mod = step(lm(mpg〜。,data = mtcars),direction =「backward」)可以正常工作。 COEF(MOD); attr(mod $ terms,「term.labels」)' – Gregor