# Calculation AIC without loglikelihood-function

I want to calculate the AIC without calculating the loglikelihood-function (which seems complicated). If the residuals are normally distributed, this can be done, according to wikipedia, as follows:

$AIC_{\sigma} = n log(\sigma^2_Z) + 2k$,

where $\sigma^2_Z$ is the variance of the residuals, $n$ the number of samples and $k$ the number of parameters.

In order to test this I wrote a simple R-script:

library(MASS)
data(cats)
l<-lm(Hwt ~ Bwt,data=cats)

l.resi <- resid(l)
qqnorm(l.resi)
qqline(l.resi)
shapiro.test(l.resi)

n<-nrow(cats)
k<-2 # one cefficient + intercept
aic_s<- n*log(mean(l.resi^2) ) + 2*k

print(paste("aic: ",AIC(l)))
print(paste("aic_s: ",aic_self))


The script returns:

Shapiro-Wilk normality test

data:  l.resi
W = 0.9845, p-value = 0.1046

 "aic:  520.121593704369"
 "aic_s:  109.467296141423"


So, the residuals seem to be normally distributed. However, the AICs differ. Why is that?

• What kind of model are you fitting that doesn't have an AIC or logLik method in R? – shadowtalker Sep 12 '14 at 16:46
• I a couple of issues, but it's possible that these don't explain all of the differences in your example: One is that you didn't count all the parameters. The second is that likelihood is only defined up to an additive constant (so you can add or drop constants as you see fit, it's all still AIC), but to compare AICs you must use the same constants. – Glen_b Sep 12 '14 at 22:31
• @Glen_b Thanks for your comment, but in the above example the number of parameters is clearly 2, isn't it? Second, let's say I have another model which has 3 instead of 2 parameters. Is it ok to compare their aic_s's using the formula above (no constant whatsoever)? – Funkwecker Sep 15 '14 at 9:57
• The model has 3 parameters - intercept, slope, and $\sigma^2$. As long as the constants being left out by using that formula are the same, you can leave constants out. – Glen_b Sep 15 '14 at 12:16
• @Glen_b Thank you for your comment again. Can you please clarify what you mean by $\sigma^2$ and whys this is a model parameter? Thank you. – Funkwecker Sep 15 '14 at 17:49

That formula will only equal the true AIC up to a constant. So if $AIC_1$ is the actual AIC and $AIC_s$ is the self-computed AIC you have, then $AIC_1 = AIC_s+c$ for some constant $c$.