Created
June 19, 2017 23:39
-
-
Save tomhopper/f52e0622d61703c001d2fbe58e06a36a to your computer and use it in GitHub Desktop.
Accepts one or more lm objects and returns a single data frame containing the fit statistics R-squared, adjusted R-squared, predictive R-squared and PRESS for each model.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Model Fit Statistics | |
#' @description Returns lm model fit statistics R-squared, adjusted R-squared, | |
#' predicted R-squared and PRESS. | |
#' Thanks to John Mount for his 6-June-2014 blog post, R style tip: prefer functions that return data frames" for | |
#' the idea \url{http://www.win-vector.com/blog/2014/06/r-style-tip-prefer-functions-that-return-data-frames} | |
#' @param ... One or more \code{lm()} models. | |
#' @return A data frame with rows for R-squared, adjusted R-squared, Predictive R-squared and PRESS statistics, and a column for each model passed to the function. | |
model_fit_stats <- function(...) { | |
var_names <- as.character(match.call())[-1] | |
dots <- list(...) | |
ndots <- length(dots) | |
if(all.equal(unlist(lapply(X = dots, FUN = class)), rep("lm", length.out = ndots))){ | |
return.df <- data.frame(parameter = c("r.squared", "adj.r.squared","pred.r.squared","PRESS")) | |
for(i in 1:ndots) { | |
r.sqr <- summary(dots[[i]])$r.squared | |
adj.r.sqr <- summary(dots[[i]])$adj.r.squared | |
pre.r.sqr <- pred_r_squared(dots[[i]]) | |
PRESS <- PRESS(dots[[i]]) | |
return.df[paste0(var_names[i])] <- matrix(c(r.sqr, adj.r.sqr, pre.r.sqr, PRESS), nrow = 4, byrow = FALSE) | |
} | |
# r.squared = r.sqr, adj.r.squared = adj.r.sqr, pred.r.squared = pre.r.sqr, press = PRESS) | |
return(return.df) | |
} else { | |
stop("model_fit_stats only works with objects of class 'lm.'") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment