# What is the difference between AIC() and extractAIC() in R?

The R documentation for either does not shed much light. All that I can get from this link is that using either one should be fine. What I do not get is why they are not equal.

Fact: The stepwise regression function in R, step() uses extractAIC().

Interestingly, running a lm() model and a glm() 'null' model (only the intercept) on the 'mtcars' data set of R gives different results for AIC and extractAIC().

> null.glm = glm(mtcars$mpg~1) > null.lm = lm(mtcars$mpg~1)

> AIC(null.glm)
[1] 208.7555
> AIC(null.lm)
[1] 208.7555
> extractAIC(null.glm)
[1]   1.0000 208.7555
> extractAIC(null.lm)
[1]   1.0000 115.9434


It is weird, given that both the models above are the same, and AIC() gives the same results for both.

Can anyone throw some light on the issue?

There are two differences for a usual linear regression model (lm) between AIC and extractAIC:

• AIC accounts for the estimation of the unknown variance of the error (i.e., scale) while extractAIC does not, hence $$k$$ is one less with extractAIC.
• AIC uses the formula $$n\log\frac{RSS}{n}+n+n\log\left(2\pi\right)$$ for the -2 log likelihood, while extractAIC drops the additive constant, and uses only $$n\log\frac{RSS}{n}$$. (At least by default; you can set a custom scale.)

Of course, none of these matters, if the values are only used in comparison.

In addition to these, and as already noted above, extractAIC behaves differently for lm and glm (in contrast to AIC!), namely, it uses the above formula only for lm, for glm it switches to the aic function of the fitted model, which is different.

• The socond point does not seem to be correct, because AIC does not compute the log likelihood at all, but calls the logLik method on the model. Example: > m <- lm(mpg ~ disp, data=mtcars) > ll <- logLik(m) > -2*as.numeric(ll) + 2*attr(ll, "df") [1] 170.2094 > AIC(ll) [1] 170.2094 > extractAIC(m) [1] 2.00000 77.39732  Commented Jul 16, 2021 at 12:11
• @cdalitz "AIC does not compute the log likelihood at all, but calls the logLik method on the model" That is correct. But for lm (to which my two points refer), the formula used is just the one I have given: compare 32 * log(stats:::deviance.lm(m)/32)+32+32*log(2*pi) with -2*ll in your example. Commented Jul 19, 2021 at 20:19

According, to the help for these two function (use ?AIC and ?extractAIC) this is expected.

Note that the AIC is just defined up to an additive constant, because this is also the case for the log-likelihood. This means you should check whether

extractAIC(full.modell) - extractAIC(null.modell)


and

AIC(full.modell) - AIC(null.modell)


give the same result. As long as they do, both functions are equivalent for all practical purposes.

• I'm probably missing something, but I still don't understand why extractAIC(null.lm) != AIC(null.lm) while extractAIC(null.glm) == AIC(null.glm) even though null.lm is the same model as null.glm. Could you expand your answer a little? Commented Nov 16, 2012 at 18:26
• @smillig extractAIC uses different methods for lm fits and glm fits, i.e., extractAIC.lm and extractAIC.glm. You can use getAnywhere to study their code. AIC uses the same method for both. Commented Nov 17, 2012 at 15:18
• I have several pairs of models (with multiple predictors) for which both functions give different results. Model 1: y = x1 + x2, Model 2: y = z + x1 + x2*z. extractAIC() gives lower (negative) value for Model 1, while AIC gives lower (positive) value for Model 2. Commented Sep 10, 2013 at 11:25
• @Maxim.K You give little information on the type of variables and models used. If you did and there are some differences to this question it might be worthwhile to post this as a new question. Hard to say, without knowing the details.
– Erik
Commented Sep 10, 2013 at 12:47
• @Erik I doubt it will be worth much if I say that z is continuous and x2 is categorical (dummified). One would need the data to reproduce and I cannot publish them I'm afraid. Commented Sep 11, 2013 at 11:56