Note that the help on the function logLik
in R says that for lm
models it includes 'all constants' ... so there will be a log(2*pi)
in there somewhere, as well as another constant term for the exponent in the likelihood. Also, you can't forget to count the fact that $\sigma^2$ is a parameter.
$\cal L(\hat\mu,\hat\sigma)=(\frac{1}{\sqrt{2\pi s_n^2}})^n\exp({-\frac{1}{2}\sum_i (e_i^2/s_n^2)})$
$-2\log \cal{L} = n\log(2\pi)+n\log{s_n^2}+\sum_i (e_i^2/s_n^2)$
$= n[\log(2\pi)+\log{s_n^2}+1]$
$\text{AIC} = 2p -2\log \cal{L}$
but note that for a model with 1 independent variable, p=3 (the x-coefficient, the constant and $\sigma^2$)
Which means this is how you get their answer:
nrow(mtcars)*(log(2*pi)+1+log((sum(lm_mtcars$residuals^2)/nrow(mtcars))))
+((length(lm_mtcars$coefficients)+1)*2)
AIC
uses is-2*as.numeric(logLik(lm_mtcars))+2*(length(lm_mtcars$coefficients)+1)
. $\endgroup$logLik
says that forlm
models it includes 'all constants' ... so there'll be alog(2*pi)
in there somewhere $\endgroup$