Created
December 31, 2016 21:12
-
-
Save gibsramen/e9ad52c2ded6703bdbb51f55e6e89ad9 to your computer and use it in GitHub Desktop.
Proportion of 'Yes' Votes on Proposition 62 vs. log of County Population (11/08/2016)
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
library(ggplot2) | |
votes <- read.csv('csv-ballot-measures.csv', header=TRUE, stringsAsFactors=FALSE) | |
votes <- votes[,-1:-2] | |
d.votes <- votes[which(votes$BALLOT_MEASURE_ID==62),] | |
drops <- c('COUNTY_ID', 'BALLOT_MEASURE_ID', 'BALLOT_MEASURE_NAME', 'BALLOT_MEASURE_TITLE') | |
d.votes <- d.votes[, !names(votes) %in% drops] | |
d.votes$YES_COUNT <- as.numeric(gsub(',', '', d.votes$YES_COUNT)) | |
d.votes$NO_COUNT <- as.numeric(gsub(',', '', d.votes$NO_COUNT)) | |
d.votes$POP <- d.votes$YES_COUNT + d.votes$NO_COUNT | |
d.votes$YES_PROP <- d.votes$YES_COUNT / d.votes$POP | |
d.votes$LOG_POP <- log10(d.votes$POP) | |
regr <- lm(YES_PROP ~ LOG_POP, data=d.votes) | |
regr.eqn <- paste0('y = ', | |
toString(round(coefficients(regr)[1],2)), | |
' + ', | |
toString(round(coefficients(regr)[2],2)), | |
'*log(x)') | |
r2 <- paste0('R^2 = ', round(summary(regr)$adj.r.squared,2)) | |
ggplot(data=d.votes, aes(x=LOG_POP, y=YES_PROP)) + | |
geom_point() + | |
geom_smooth(method=lm, se=FALSE, colour='red') + | |
ggtitle('Prop 62 \'Yes\' Proportion vs. log(County Population)') + | |
xlab('log(County Population)') + | |
ylab('Proportion of \'Yes\' Votes') + | |
geom_text(x=6.5, y=0.58, label=regr.eqn, parse=FALSE, colour='blue') + | |
geom_text(x=6.5, y=0.55, label=r2, parse=FALSE, colour='blue') |
Author
gibsramen
commented
Dec 31, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment