[Rd] AIC.default (PR#2518)

a.stephenson@lancaster.ac.uk a.stephenson@lancaster.ac.uk
Sat Feb 1 11:22:02 2003


There is a bug in AIC.default and AIC.lm, as illustrated below.
(I've only checked this under 1.6.1, and can't easily check if it has
already been reported since the site is down.)


> lm1 <- lm(y ~ x, list(x=1:10, y=jitter(1:10)))
> lm2 <- lm(y ~ x, list(x=1:10, y=jitter(1:10)))
> AIC(lm1, lm2)
    df        AIC
lm1  3 -18.662493
lm2  3  -7.265906
> AIC(lm1, lm2, k = 2)
Error in "row.names<-.data.frame"(*tmp*, value =
as.character(match.call()[-1])) : 
	invalid row.names length

The offending line is

row.names(val) <- as.character(match.call()[-1])

One possible (but probably overly complicated) solution is


> AIC.default
function (object, ..., k = 2) 
{
    if (length(list(...))) {
        dots <- as.list(substitute(list(...)))[-1]       ## added
        dots <- sapply(dots, function(x) deparse(x))     ## added
        rnm <- c(deparse(substitute(object)), dots)      ## added
        object <- list(object, ...)
        val <- lapply(object, logLik)
        val <- as.data.frame(t(sapply(val, function(el) c(attr(el, 
            "df"), AIC(el, k = k)))))
        names(val) <- c("df", "AIC")
        row.names(val) <- rnm                            ## altered
        val
    }
    else {
        AIC(logLik(object), k = k)
    }
}


Alec Stephenson                            tel +44 (0) 1524 593950
Department of Mathematics and Statistics   fax +44 (0) 1524 592681
Lancaster University                  a.stephenson@lancaster.ac.uk
Lancaster LA1 4YF          http://www.maths.lancs.ac.uk/~stephena/