# [R] question about the difference of AIC()

Søren Højsgaard @orenh @end|ng |rom m@th@@@u@dk
Wed May 12 14:10:31 CEST 2021

```In the first model, I believe you estimate two parameters: the mean and
the variance:
> fm <- lm(y ~ 1)
> 2*2 - 2 * logLik(fm)
'log Lik.' 40.49275 (df=2)
>
AIC(fm)
[1] 40.49275

A zero mean model:

fm0 <- lm(y ~ -1)
> 2*1 - 2 * logLik(fm0)
'log Lik.' 39.00611 (df=1)
> AIC(fm0)
[1] 39.00611

Regards
Søren

On Wed, 2021-05-12 at 19:49 +0800, Jinsong Zhao wrote:
> Hi there,
>
> I learned that AIC = 2 * npar - 2 * log(logLik(model)), where k is
> the
> number of estimated parameters in the model.
>
> For examle:
>  > set.seed(123)
>  > y <- rnorm(15)
>  > fm <- lm(y ~ 1)
> In this example, npar should be 1, so, AIC is:
>  > 2*1 - 2 * logLik(fm)
> 'log Lik.' 38.49275 (df=2)
>
> However, AIC() give:
>  > AIC(fm)
> [1] 40.49275
>
> I also try another AIC extract function:
>  > extractAIC(fm)
> [1]  1.000000 -4.075406
>
> Since extractAIC() does not include the constant: n + n * log(2 *
> pi), so:
>  > extractAIC(fm)[2] + 15 + 15 * log(2 * pi)
> [1] 38.49275
>
> It equals to the AIC calculated by 2*1 - 2 * logLik(fm), but
> different
> with the return of AIC().
>
> It seems that AIC use 2 * (npar + 1) instead of 2 * npar.
>
> In the help page of logLik, it said:
>   '"df"' (*d*egrees of *f*reedom), giving the number of (estimated)
> parameters in the model.
>
> The "df" is used by AIC() as npar, however, "df" is not number of
> estimated parameters in the model, df - 1 is. Am I correct?
>
> Best wishes,
> Jinsong
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help