Last active
November 29, 2017 22:36
-
-
Save dsparks/818976 to your computer and use it in GitHub Desktop.
ggplot2 coefficient plot, with discrete interval bounds
This file contains 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
CoefficientPlot <- function(models, alpha = 0.05, modelnames = ""){ | |
# models must be a list() | |
Multiplier <- qnorm(1 - alpha / 2) | |
CoefficientTables <- lapply(models, function(x){summary(x)$coef}) | |
TableRows <- unlist(lapply(CoefficientTables, nrow)) | |
if(modelnames[1] == ""){ | |
ModelNameLabels <- rep(paste("Model", 1:length(TableRows)), TableRows) | |
} else { | |
ModelNameLabels <- rep(modelnames, TableRows) | |
} | |
MatrixofModels <- cbind(do.call(rbind, CoefficientTables), ModelNameLabels) | |
MatrixofModels <- data.frame(cbind(rownames(MatrixofModels), MatrixofModels)) | |
colnames(MatrixofModels) <- c("IV", "Estimate", "StandardError", "TValue", "PValue", "ModelName") | |
MatrixofModels$IV <- factor(MatrixofModels$IV, levels = MatrixofModels$IV) | |
MatrixofModels[, -c(1, 6)] <- apply(MatrixofModels[, -c(1, 6)], 2, function(x){as.numeric(as.character(x))}) | |
OutputPlot <- qplot(IV, Estimate, ymin = Estimate - Multiplier * StandardError, | |
ymax = Estimate + Multiplier * StandardError, data = MatrixofModels, geom = "pointrange", | |
ylab = NULL, xlab = NULL) | |
OutputPlot <- OutputPlot + geom_hline(yintercept = 0, lwd = I(7/12), colour = I(hsv(0/12, 7/12, 7/12)), alpha = I(5/12)) | |
OutputPlot <- OutputPlot + facet_grid(~ ModelName) + coord_flip() + theme_bw() | |
return(OutputPlot) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment