Skip to content

Instantly share code, notes, and snippets.

@mattblackwell
Last active January 1, 2017 09:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattblackwell/18ced3c73e85efb2ca3c6d0f1acb0bb9 to your computer and use it in GitHub Desktop.
Save mattblackwell/18ced3c73e85efb2ca3c6d0f1acb0bb9 to your computer and use it in GitHub Desktop.
Analysis of the shift from 2012 to 2016
group.mean <- function(x, group) {
out <- tapply(x, group, mean, na.rm = TRUE)
out[group]
}
data(state.fips, package = "maps")
state.fips <- unique(state.fips[,c("fips","abb")])
state.fips$abb <- as.character(state.fips$abb)
state.fips <- rbind(state.fips, c(2, "AK"))
state.fips <- rbind(state.fips, c(15, "HI"))
rownames(state.fips) <- state.fips$abb
fips.state <- state.fips
rownames(fips.state) <- fips.state$fips
# From the ACES 2014-2010
# C23002A SEX BY AGE BY EMPLOYMENT STATUS FOR THE POPULATION 16 YEARS AND OVER (WHITE ALONE)
unemp <- read.csv("~/workland/data/census/unemp-white-acs1410-C23002A.csv", stringsAsFactors = FALSE)
unemp$w.unemp2014 <- with(unemp, (m.unemp.1664 + m.unemp.65p + f.unemp.1664 + f.unemp.65p))
unemp$w.labfor2014 <- with(unemp, (m.labfor.1664 + m.labfor.65p + f.labfor.1664 + f.labfor.65p))
unemp$w.unemp.rate2014 <- unemp$w.unemp2014/unemp$w.labfor2014
countydata <- unemp[, c("fips", "w.unemp.rate2014")]
countydata$stfips <- as.numeric(substr(countydata$fips, start = 1, stop = nchar(countydata$fips)-3))
countydata$state.abb <- fips.state[as.character(countydata$stfips), "abb"]
## Census 2010 (Table DP-1)
census2010 <- read.csv("~/workland/data/census-tables/DEC_10_SF1_P9.csv")
census2010$totpop2010 <- census2010$D001
census2010$whtot2010 <- census2010$D005
census2010$fips <- census2010$GEO.id2
countydata <- merge(countydata, census2010[, c("fips", "whtot2010", "totpop2010")], by = "fips", all.x = TRUE)
countydata$whprop2010 <- countydata$whtot2010/countydata$totpop2010
## Election returns from uselectionatlas.org
gen2016 <- read.csv("~/workland/data/2016_0_0_2.csv", skip = 1)
gen2012 <- read.csv("~/workland/data/2012_0_0_2.csv", skip = 1)
gen2012$obama.votes2012 <- gen2012$vote1
gen2012$obama.share2012 <- gen2012$vote1/gen2012$totalvote
gen2012$romney.share2012 <- gen2012$vote2/gen2012$totalvote
gen2012$totalvote2012 <- gen2012$totalvote
vars2012 <- c("fips", "obama.share2012", "romney.share2012", "totalvote2012", "obama.votes2012")
gen2016$clinton.votes2016 <- gen2016$vote1
gen2016$trump.share2016 <- gen2016$vote2/gen2016$totalvote
gen2016$clinton.share2016 <- gen2016$vote1/gen2016$totalvote
gen2016$johnson.share2016 <- gen2016$vote4/gen2016$totalvote
gen2016$totalvote2016 <- gen2016$totalvote
vars <- c("fips", "trump.share2016", "clinton.share2016", "totalvote2016", "clinton.votes2016")
countydata <- merge(countydata, gen2016[,vars], by = "fips", all.x = TRUE)
countydata <- merge(countydata, gen2012[,vars2012], by = "fips", all.x = TRUE)
countydata$unemp.stmean <- group.mean(countydata$w.unemp.rate2014, countydata$state.abb)
countydata$w.unemp.st.demean <- countydata$w.unemp.rate2014-countydata$unemp.stmean
countydata$clinton.obama.change <- countydata$clinton.share2016-countydata$obama.share2012
countydata$co.diff.stmean <- group.mean(countydata$clinton.obama.change, countydata$state.abb)
countydata$clinton.obama.st.demean <- countydata$clinton.share2016-countydata$obama.share2012 - countydata$co.diff.stmean
rust.belt <- c("OH", "MI", "PA", "WI", "IN", "IL")
countydata$rust.belt <- 1 * (countydata$state.abb %in% rust.belt)
wh.counties <- countydata[which(countydata$whprop2010 > 0.95),]
my.cols <- ifelse(state.abb %in% rust.belt, adjustcolor("indianred", alpha = 0.5), adjustcolor("dodgerblue", alpha = 0.25))
par(mar = c(5, 10, 1, 1))
plot(clinton.obama.st.demean ~ w.unemp.st.demean, data = wh.counties, pch = 19, col = my.cols, xlab = "White Unemployment Rate, 2014 (State-demeaned)", cex = 0.75, bty = "n", ylab = "")
mtext(side = 2, las = 1, text = "Clinton-Obama\nVoteshare\nDifference\n(State-Demeaned)", line = 6, adj = 0.5)
out1 <- loess(clinton.obama.st.demean ~ w.unemp.st.demean, data = wh.counties, subset = rust.belt == 0)
j <- order(out1$x)
pred <- predict(out1, se = TRUE)
lines(x=out1$x[j], y = out1$fitted[j], col = "blue", lwd = 3)
##lines(x=out1$x[j], y = out1$fitted[j] + 2*pred$se.fit[j], col = "blue", lty = 2)
##lines(x=out1$x[j], y = out1$fitted[j] - 2*pred$se.fit[j], col = "blue", lty = 2)
out1 <- loess(clinton.obama.st.demean ~ w.unemp.st.demean, data = wh.counties, subset = rust.belt == 1)
j <- order(out1$x)
pred <- predict(out1, se = TRUE)
lines(x=out1$x[j], y = out1$fitted[j], col = "indianred", lwd = 3)
##lines(x=out1$x[j], y = out1$fitted[j] + 2*pred$se.fit[j], col = "indianred", lty = 2)
##lines(x=out1$x[j], y = out1$fitted[j] - 2*pred$se.fit[j], col = "indianred", lty = 2)
text(x = -0.06, y = 0.09, "Rust Belt", col = "indianred")
text(x = -0.07, y = 0.01, "All other\nstates", col = "dodgerblue")
## fixed effect regression (hide the FEs)
out <- lm(I(clinton.share2016-obama.share2012) ~ w.unemp.rate2014 + state.abb, data = wh.counties)
sout <- summary(out)$coefficients
sout[c(1,2),]
## fixed effect regression w/ interaction (hide the FEs)
out.inter <- lm(I(clinton.share2016-obama.share2012) ~ w.unemp.rate2014*rust.belt + state.abb, data = wh.counties)
sout.inter <- summary(out.inter)$coefficients
sout.inter[c(1,2, nrow(sout.inter)),]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment