[R] AIC, AICc, and K

Martin Maechler maechler at stat.math.ethz.ch
Mon Dec 6 12:31:58 CET 2004

>>>>> "Spencer" == Spencer Graves <spencer.graves at pdf.com>
>>>>>     on Sat, 04 Dec 2004 17:09:26 -0800 writes:

    Spencer> I don't know the "best" way, but the following looks like it will 
    Spencer> work: 

    Spencer> tstDF <- data.frame(x=1:3, y=c(1,1,2))
    Spencer> fit0 <- lm(y~1, tstDF)
    Spencer> fitDF <- lm(y~x, tstDF)
    Spencer> AIC(fitDF,fit0)
    Spencer> df      AIC
    Spencer> fitDF  3 5.842516
    Spencer> fit0   2 8.001399

    Spencer> The function AIC with only 1 argument returns only
    Spencer> a single number.  However, given nested models, it
    Spencer> returns a data.frame with colums df and AIC.  At
    Spencer> least in this example (and I would think in all
    Spencer> other contexts as well), "df" is the K you want.
yes, but Benjamin would hardly want to invent a second model
just in order to call AIC() with both models and get the data

As Benjamin hoped, the "df" is part of the logLik(.) result,
and if either of you had more carefully looked at  help(logLik),
you'd have seen

>> Value:
>>      Returns an object, say 'r', of class 'logLik' which is a number
>>      with attributes, 'attr(r, "df")' (*d*egrees of *f*reedom) giving
>>      the number of parameters in the model. There's a simple 'print'
>>      method for 'logLik' objects.

More directly, you can use

    > str(unclass(logLik(fit0)))
     atomic [1:1] -2
     - attr(*, "nall")= int 3
     - attr(*, "nobs")= int 3
     - attr(*, "df")= num 2


    > stats:::AIC.logLik
    function (object, ..., k = 2) 
    -2 * c(object) + k * attr(object, "df")

to see how these work.

Martin Maechler, ETH Zurich

    Spencer> hope this helps. 
    Spencer> Spencer Graves

    Spencer> Benjamin M. Osborne wrote:

    >> How can I extract K (number of parameters) from an AIC
    >> calculation, both to report K itself and to calculate
    >> AICc?  I'm aware of the conversion from AIC -> AICc,
    >> where AICc = AIC + 2K(K+1)/(n-K-1), but not sure of how K
    >> is calculated or how to extract that value from either an
    >> AIC or logLik calculation.

    >> This is probably more of a basic statistics question than
    >> an R question, but I thank you for your help.

    >> -Ben Osborne

More information about the R-help mailing list