Calculating AIC “by hand” in R

I have tried calculating the AIC of a linear regression in R but without using the AIC function, like this:

lm_mtcars <- lm(mpg ~ drat, mtcars)

nrow(mtcars)*(log((sum(lm_mtcars$residuals^2)/nrow(mtcars))))+(length(lm_mtcars$coefficients)*2)
[1] 97.98786

However, AIC gives a different value:

AIC(lm_mtcars)
[1] 190.7999

Could somebody tell me what I’m doing wrong?

Answer

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 σ2 is a parameter.

L(ˆμ,ˆσ)=(12πs2n)nexp(12i(e2i/s2n))

2logL=nlog(2π)+nlogs2n+i(e2i/s2n)

=n[log(2π)+logs2n+1]

AIC=2p2logL

but note that for a model with 1 independent variable, p=3 (the x-coefficient, the constant and σ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)

Attribution
Source : Link , Question Author : luciano , Answer Author : Glen_b

Leave a Comment