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
[1] "aic: 520.121593704369"
[1] "aic_s: 109.467296141423"
So, the residuals seem to be normally distributed. However, the AICs differ. Why is that?
AIC
orlogLik
method in R? $\endgroup$