步进函数匹配全模型的 AIC

step function matching AIC of full model

不应该

AIC(full) = 275.93

当 step() 函数与低于 -9.86 的完整模型一起运行时,匹配 AIC 的输出

Start:  AIC=-9.86
y ~ x + x2

       Df Sum of Sq    RSS      AIC
- x2    1   0.03672 85.372 -11.8147
- x     1   1.03869 86.374 -10.6479
<none>              85.336  -9.8578

Step:  AIC=-11.81
y ~ x

       Df Sum of Sq    RSS     AIC
- x     1     1.004 86.376 -12.646
<none>              85.372 -11.815

Step:  AIC=-12.65
y ~ 1


Call:
lm(formula = y ~ 1, data = data)

Coefficients:
(Intercept)  
   -0.03719  

完整代码如下:

set.seed(101)
y = rnorm(100)
x = rnorm(100)
x2 = rnorm(100)
data = data.frame(y = y, x = x, x2 = x2)
null = lm(y~1, data = data)
full = lm(y~x+x2, data = data)
#step(null, scope= list(lower = null, upper = full) , direction="backward", trace = TRUE)
step(full, direction="backward", trace = TRUE)
AIC(full)

您需要 extractAIC 而不是 AIC

extractAIC(lm(y~x+x2, data = data), scale=0)
#OR
#extractAIC(full, scale=0)

如果您参考文档 ?AIC & ?extractAIC 它清楚地表明

The log-likelihood and hence the AIC/BIC is only defined up to an additive constant. Different constants have conventionally been used for different purposes and so extractAIC and AIC may give different values (and do for models of class "lm": see the help for extractAIC).


希望这对您有所帮助!