[R] Calculation of AIC BIC from mle

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jun 5 12:38:28 CEST 2006


You are mixing S3 and S4 classes and generics here.  AIC(logLik(ml1)) will 
work.

This a namespace issue: stats4 contains a version of logLik, but the 
default method for AIC is from stats, and so dispatches on stats::logLik 
and not stats4::logLik.  There is something fundamentally unsatisfactory 
about converting S3 generics to S4 generics where namespaces are involved.

I have put a workaround in R-devel.

On Mon, 5 Jun 2006, Rainer M KRug wrote:

> R 2.3.0, all packages up to date
> Linux, SuSE 10.0
>
> Hi
>
> I want to calculate AIC or BIC from several results from mle calculation.
>
> I found the AIC function, but it does not seem to work with objects of
> class mle -
> If I execute the following:
> ml1 <- mle(...)
> AIC(ml1)
>
> I get the following error messale:
> Error in logLik(object) : no applicable method for "logLik"
>
> Therefore I am using the following to calculate the AIC:
>
> #AICmle calculate AIC from mle object
> AICmle <- function( m, k=2)
> {
> 	lL <- logLik(m)
> 	edf <- attr(lL, "df")
> 	LL <- lL[1]
> 	- 2 * LL + k * edf
> }
>
> 1) Why is AIC not working with objects of class mle - am I doing
> something wrong, is it a bug or by design?
>
> 2) Just for confirmation - is my calculation of AIC correct?

Not quite.  The correct function is

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

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list