>   I have a list of models (about 600 glm models) and I included the prefix 
> "mod_" on their name. Now I would like retrieve the list from the R 
> environment and save their AICs (and other info) on a table. I´m trying 
> something like:

I have some (rather un)tested code:

findmodels <- function(modeltype = "lm", dataset, pattern) {
        ls <- ls(".GlobalEnv", pattern=pattern)
        mods <- ls[sapply(ls, function(x) inherits(get(x), modeltype))]
        if (!missing(dataset)) {
                data.name <- function(x) as.character(x$call[["data"]])
                mods <- mods[sapply(mods, function(x) data.name == dataset)]
        }
        
        models <- lapply(mods, get)
        class(models) <- c("ensemble", class(models))
        models
}

summary.ensemble <- function(object, ...) {
        fits <- data.frame(t(sapply(object, function(mod) {
                sum <- summary(mod)
                c(
                        df = .df(mod),
                        logL = logLik(mod),
                        AIC = -AIC(mod),
                        BIC = -AIC(mod, k=log(length(fitted(mod)))),
                        R2 = sum$r.squared,
                        adjR2 = sum$adj.r.squared
                )
        })))
        fits$model <- factor(names(object))
        rownames(fits) <- paste("m", fits$model, sep="")
        fits
}

Hopefully that should give you an idea on how to proceed.

Regards,

Hadley

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to