简体   繁体   English

如何在stepAIC中计算AIC

[英]How is AIC calculated in stepAIC

Here is a very simple lm model from ?lm 这是一个非常简单的lm模型

ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2,10,20, labels=c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)

If I use stepAIC to lm.D9, on the very first line it says AIC = -12.58 如果我使用stepAIC到lm.D9,在第一行它说AIC = -12.58

require(MASS)
stepAIC(lm.D9)

If I use AIC directly on lm.D9, it gives a different value 46.17648 如果我直接在lm.D9上使用AIC,它会给出不同的值46.17648

AIC(lm.D9)

My question is why the 2 AIC values are different. 我的问题是为什么2个AIC值不同。 Thanks! 谢谢!

AIC is only defined up to an arbitrary constant. AIC仅定义为任意常量。 As long as the same value of the constant is used when comparing AICs for different models, it doesn't matter. 只要在比较不同模型的AIC时使用相同的常数值,就无所谓了。 If you look at ?extractAIC and ?AIC , you'll find the formulas used by both methods. 如果你看一下?extractAIC?AIC ,你会发现两种方法都使用的公式。

Basically, either use extractAIC or AIC , but not both at the same time. 基本上,要么使用extractAIC ,要么使用AIC ,但不能同时使用两者。

This was annoying me, so I decided to work it out from first principles. 这让我感到烦恼,所以我决定从第一原则开始。

Re-fit the model: 重新适合模型:

d <- data.frame(weight=
                c(ctl=c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14),
                  trt=c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)),
                group=gl(2,10,20, labels=c("Ctl","Trt")))
lm.D9 <- lm(weight ~ group, d)

Values returned by standard accessors: 标准访问器返回的值:

(AIC1 <- AIC(lm.D9))
> 46.17468
(LL1 <- logLik(lm.D9))
> -20.08824 (df=3)

Reconstruct from first principles: 从第一原则重建:

n <- nrow(d)
ss0 <- summary(lm.D9)$sigma
ss <- ss0*(n-1)/n
(LL2 <- sum(dnorm(d$weight,fitted(lm.D9),
                 sd=ss,log=TRUE)))
> -20.08828

This is a tiny bit off, haven't found the glitch. 这有点 ,没有发现故障。

Number of parameters: 参数数量:

npar <- length(coef(lm.D9))+1 


(AIC2 <- -2*LL2+2*npar)
> 46.1756

Still off by more than numeric fuzz, but only about one part in a million. 仍然不仅仅是数字模糊,而只有百万分之一。

Now let's see what stepAIC is doing: 现在让我们看看stepAIC正在做什么:

MASS::stepAIC(lm.D9)  ## start: AIC = -12.58
extractAIC(lm.D9)     ## same value (see MASS::stepAIC for details)
stats:::extractAIC.lm ## examine the code


RSS1 <- deviance(lm.D9)   ## UNSCALED sum of squares
RSS2 <- sum((d$weight-fitted(lm.D9))^2)  ## ditto, from first principles
AIC3 <- n*log(RSS1/n)+2*2 ## formula used within extractAIC

You can work out the formula used above from sigma-hat=RSS/n -- or see Venables and Ripley MASS for the derivation. 您可以从sigma-hat = RSS / n中找出上面使用的公式 - 或者参见Venables和Ripley MASS进行推导。

Add missing terms: uncounted variance parameter, plus normalization constant 添加缺少的术语:不计数的方差参数,加上规范化常数

(AIC3 + 2 - 2*(-n/2*(log(2*pi)+1)))

This is exactly the same (to 1e-14) as AIC1 above 这与上面的AIC1完全相同(对于1e-14)

Thank you @benbolker for detailed answer. 谢谢@benbolker的详细解答。 You mentioned: 你提到过:

This is a tiny bit off, haven't found the glitch. 这有点 ,没有发现故障。

I looked into it and found that if you modify this line: 我调查了一下,发现如果你修改这一行:

ss <- ss0*(n-1)/n

to this: 对此:

ss <- sqrt( (ss0)^2 * (n - length(coef(lm.D9))) / n )

then the result would be exactly the same. 然后结果将完全相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM