[R] organizing stats from a list of models

hadley wickham h.wickham at gmail.com
Mon Dec 11 15:41:58 CET 2006


>   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




More information about the R-help mailing list