rnlsmodel-comparison

AIC on nls on R


I have a problem to calculate the AIC. Indeed, I estimate the parameters of my 3 models: "mod_linear", which is a linear model and "mod_exp" and "mod_logis" which are two non linear models.

I used the function AIC():

AIC(mod_linear,mod_exp,mod_logis)

          df        AIC
mod_linear  4   3.015378
mod_exp     5 -11.010469
mod_logis   5  54.015746

But I tried to calculate the AIC with the formule AIC=2k+nlog(RSS/n) where K is the number of parameters, n the number of the sample and RSS the residual sum of squares.

k=4
n=21
#Calcul of nls for the linear model:
mod_linear=nls(data$P~P_linear(P0,K0,a),data=data,
start=c(P0=4.2,K0=4.5,a=0.)

2*k+n*log(sum(residuals(mod_linear)^2)/n)
-56.58004

As you can see, is not the same result and It's the same thing for the two other models. Someone could help me?

Regards


Solution

  • You should always take care that you use consistent definitions of AIC.

    AIC uses the usual definition of 2k-2*ln(L). The log-likelihood is calculated, e.g., by stats:::logLik.lm as 0.5 * (- N * (log(2 * pi) + 1 - log(N) + log(sum(res^2)))).

    An example:

    fit <- lm(Sepal.Length ~ Sepal.Width, data = iris)
    AIC(fit)
    #[1] 371.9917
    logL <- 0.5 * (- length(residuals(fit)) * (log(2 * pi) + 1 - log(length(residuals(fit))) + log(sum(residuals(fit)^2))))
    
    2 * (fit$rank + 1) - 2 * logL
    #[1] 371.9917
    

    However, help("AIC") warns:

    The log-likelihood and hence the AIC/BIC is only defined up to an additive constant. Different constants have conventionally been used for different purposes ... Particular care is needed when comparing fits of different classes [...].

    See stats:::logLik.nls for how the log-likelihood is calculated for nls fits.