Let's say I have some models stored in a list:
mods <- list()
mods[[1]] <- lm(mpg ~ disp, data = mtcars)
mods[[2]] <- lm(mpg ~ disp + factor(cyl), data = mtcars)
mods[[3]] <- lm(mpg ~ disp * factor(cyl), data = mtcars)
And I want to compare them using stats::AIC
. I'm looking for the output I would get from AIC(mods[[1]], mods[[2]], mods[[3]])
, but I'd like it to generalize to an arbitrarily long list. I thought that
do.call(AIC, mods)
would work, but it returns something that's very verbose and unhelpful. (If the list is named, an error results unless one of the names is object
, corresponding the the first argument of AIC
, but then you just get the verbose output again.)
After the failure of do.call
, I started thinking about an eval(parse())
solution, but I figured I should ask here first.
test = do.call(AIC, mods); rownames(test) = NULL
) – Babylonian