Created
October 16, 2012 15:05
-
-
Save abelsonlive/3899832 to your computer and use it in GitHub Desktop.
coefplot.R
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
require("RColorBrewer") | |
# not run: | |
# model = lm(y~x1+x2+x3+x4...) | |
# Construct quantile table for varialbes of interest | |
# t value: n-p=df.residual=634, alpha=5%,50%. t(1-alpha/2, n-p) | |
t.05 <- qt(1-0.025, 634, lower.tail = TRUE, log.p=FALSE) | |
t.5 <- qt(1-0.25, 634, lower.tail = TRUE, log.p=FALSE) | |
coef.quantil <- data.frame(matrix(0, nrow=length(model$coefficients)-1, ncol=8)) | |
names(coef.quantil) <- c("coef", "mean", "sd", "X2.5", "X25", "X50", "X75", "X97.5") | |
for(i in 1:nrow(coef.quantil)) { | |
varname <- row.names(summary(model)$coefficients)[i+1] | |
varname <- gsub("genre", "", varname) | |
substring(varname , 1, 1) <- toupper(substring(varname, 1, 1)) | |
substring(varname, 2) <- tolower(substring(varname , 2)) | |
coef.quantil[i,1] <- varname | |
coef.quantil[i,c(2,3,6)] <- summary(gross_model)$coefficients[i+1,c(1,2,1)] | |
coef.quantil[i, c(4,8)] <- coef.quantil[i,2]+c(-1, 1)*t.05*coef.quantil[i,3] | |
coef.quantil[i, c(5,7)] <- coef.quantil[i,2]+c(-1, 1)*t.5*coef.quantil[i,3] | |
} | |
# order data to make pretty pattern | |
coef.quantil <- coef.quantil[order(coef.quantil$mean, decreasing=FALSE),] | |
################ plot coefficients ############################### | |
# set colors | |
dark_blue = brewer.pal(4,"Blues")[4] | |
dark_blue2 = brewer.pal(4,"Blues")[3] | |
light_blue = brewer.pal(4,"Blues")[2] | |
blues = c(dark_blue, dark_blue2, light_blue) | |
dark_red = brewer.pal(4,"Reds")[4] | |
dark_red2 = brewer.pal(4,"Reds")[3] | |
light_red = brewer.pal(4,"Reds")[2] | |
reds = c(dark_red, dark_red2, light_red) | |
png(filename = "~/Dropbox/PredictiveModel/coefplot.png", | |
width = 640, | |
height = 480, | |
units = "px", | |
pointsize = 12) | |
par(family="OpenSans-Semibold", #specify the font to use | |
bg="white", #specify the background color | |
fg="grey50", #specify the color of the "foreground" - axes, labels, etc. | |
col.axis=dark_blue, #specify the color of the "axis ticks" | |
col.lab=dark_blue, #specify the color of axis labels | |
ljoin=0, #make the axes have rounded edges | |
col.main="grey50", # specify the color of the title | |
mai=c(0,0.05,.3,0.05), | |
cex=1, | |
cex.main=1.5, | |
bg="white") | |
plot(0,0, | |
xlim=c(min(coef.quantil[,4])-1.3, | |
max(coef.quantil[,8])), | |
ylim=c(-1, 22.5), | |
type="n", | |
axes=F, | |
ylab="", | |
xlab="Gross (in millions of US dollars") | |
title("Coefficient ranges of a simple box office prediction model") | |
# vertical line | |
lines(c(0,0), c(22, 0), lwd=2, lty=1, col="grey75") | |
# ticks and text | |
range(coef.quantil[,4]) | |
range <- c(-4, -3, -2, -1, 0, 1, 2, 3, 4,5) | |
n.range <- length(range) | |
for (i in 1:n.range){ | |
# bottom ticks and text: | |
text(range[i], 0, range[i], pos=1, cex=0.8, col="grey50") | |
#top ticks and text: | |
text(range[i], 22.0, range[i], pos=3, cex=0.8, col="grey50") # this will have to be set custom based on the number of variables in your model | |
# gridlines: | |
segments(range[i], 0, range[i], 22.0, lwd=0.3, lty=1, col="grey50") | |
} | |
# Coef names, 50% & 95% intervals | |
# conditioned on significant variables | |
# I hand coded these. | |
for (i in 1:nrow(coef.quantil)) { | |
if (coef.quantil$coef[i]=="Sequel" | | |
coef.quantil$coef[i]=="Rotten" | | |
coef.quantil$coef[i]=="Budget"){ | |
cols = reds | |
}else{ | |
cols = blues | |
} | |
#label | |
text(min(coef.quantil[,4])-1.5, i, | |
coef.quantil[i,1], | |
adj=0, | |
cex=0.9, | |
col=cols[1]) | |
#95% CI | |
lines(c(coef.quantil[i,4], | |
coef.quantil[i,8]), | |
rep(i, 2), | |
lwd=5, | |
col=cols[3]) | |
#50% CI | |
lines(c(coef.quantil[i,5], | |
coef.quantil[i,7]), | |
rep(i,2), | |
col=cols[2], | |
lwd=8) | |
#coef | |
points(coef.quantil[i,2], i, | |
pch=20, | |
col=cols[1], | |
cex=2.75) | |
} | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment