-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplication_script.R
More file actions
176 lines (117 loc) · 7.16 KB
/
Copy pathreplication_script.R
File metadata and controls
176 lines (117 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Colorado analysis
# Bryan Wilcox-Archuleta
# Jan. 3, 2017
# header
# this script replicates EI estimates and graphs for Colorado precinct level analysis.
library(tidyverse)
# read data -----
data <- read_csv("/Users/bryanwilcox/Dropbox/2016 Voter Turnout/data/colorado/clean_results/analysis/merged_2016.csv")
# graphing -----
df <- gather(data,candidate, pct_vote ,c(pct_clinton, pct_trump))
weighted <- ggplot(df, aes(x=pct_latino, y = pct_vote, color = candidate, weight = votes, size = votes)) + geom_point(alpha = .10) +
scale_color_manual(values = c('blue', 'red'), breaks = c('pct_clinton', 'pct_trump'),labels = c('Clinton', 'Trump'), name = "Candidate") + stat_smooth(aes(wt = votes), se = F) +
theme_bw() + scale_y_continuous(limits=c(0,1), breaks = c(seq(0,1,.1))) +
labs(title = "Colorado Presidental Vote Among Latinos", x = "Percent Latino Registered Voter in Precinct", y = "2016 Presidental Vote Share") + guides(size=F)
ggsave("/Users/bryanwilcox/Dropbox/2016 Voter Turnout/data/colorado/weighted_co.png", weighted, height = 8, width = 8)
# EI ----------
library(ei)
library(eiCompare)
df <- data %>% dplyr::select(pct_clinton, pct_trump, votes , pct_latino) %>% na.omit()
COdf2 <- df %>% mutate(pct_other = 1 - (pct_trump+pct_clinton),pct_nonlatino = 1-pct_latino)
cands <- c("pct_clinton", "pct_trump", "pct_other")
groups <- c("~ pct_latino", "~ pct_nonlatino")
table_names <- c("EI: Pct Latino", "EI: Pct Non Latino")
# EI with compare -----
resultsCO <- ei_est_gen(cands, groups,
"votes", data = COdf2,
table_names = table_names)
write_csv(resultsCO, "/Users/bryanwilcox/Dropbox/2016 Voter Turnout/data/colorado/co_precinct/2016_ei_estimates.csv")
# EI with EI -----
model_clinton <- pct_clinton ~ pct_latino
model_trump <- pct_trump ~ pct_latino
ei_clinton <- ei(model_clinton, total="votes", erho=.5, data=df)
res_clinton <- eiread(ei_clinton, "maggs")
pe_clinton <- res_clinton[1]
se_clinton <- res_clinton[3]
ei_trump <- ei(model_trump, total="votes", erho=.5, data=df)
res_trump <- eiread(ei_trump, "maggs")
pe_trump <- res_trump[1]
se_trump <- res_trump[3]
# Density plot ------
model_clinton <- pct_clinton ~ pct_latino
ei_clinton <- ei(model_clinton, total="votes", erho=.5, data=df)
beta_clinton <- eiread(ei_clinton, "betab")
df_beta <- data.frame(beta = beta_clinton)
ei_est <- eiread(ei_clinton, "maggs")[1]
plot <- ggplot(df_beta, aes(x=beta)) + geom_density() +
geom_vline(xintercept = .67, col = "red", lty = 2) + theme_bw() +
geom_vline(xintercept = .81, col = "turquoise") +
geom_vline(xintercept = ei_est, lty = 2) +
annotate("text", x = .63, y = 5.5, label = "Exit Poll \n Estimate = .67", size = 3) +
annotate("text", x = .77, y = 7.5, label = "Latino\n Decisions\n Estimate=.81", size = 3) +
annotate("text", x = .9, y = 7.5, label = "EI Estimate\n = .83", size = 3) +
labs(x = "Estimated Latino Vote for Clinton", y = "Density", title = "Colorado Presidential Latino Vote")
ggsave("/Users/bryanwilcox/Dropbox/2016 Voter Turnout/data/colorado/density_co.png", plot, height = 8, width = 8)
pvalue_exit <- mean(df_beta$beta < .67, na.rm = T)
pvalue_ld <- mean(df_beta$beta < .81, na.rm = T)
pvalue_ei <- mean(df_beta$beta < ei_est, na.rm = T)
ei_all <- data.frame(geography = c("Entire Sample","Entire Sample"),
candidate = c("Clinton", "Trump"),
pct_latino_vote = c(pe_clinton, pe_trump),
std_error = c(se_clinton,se_trump))
# 2012 and 2016 stuff -----
data <- read_csv("/Users/bryanwilcox/Dropbox/2016 Voter Turnout/data/colorado/clean_results/analysis/joined_2012_2016_w_wts.csv")
# 2016 - 2012 difference
# net votes clinton
df <- data %>% dplyr::select(precinct,obama , romney, clinton,trump ,pct_latino)
df <- df %>% mutate(clinton_margin = clinton - obama,
direction = ifelse(clinton_margin > 0, "Clinton Improves", "Clinton Worsens"))
plot <- ggplot(df, aes(x=pct_latino, y = clinton_margin, color = direction)) + geom_point(alpha = .25) +
scale_color_manual(values = c('blue', 'red'), name = "Direction") +
theme_bw() + labs(title = "2016 Colorado Latino Vote",
y = "Net Clinton Difference \n (Clinton 16 - Obama 12)",
x = "Percent Latino Registered \n Voter in Precinct") +
geom_hline(yintercept = 0, lty =2 ) +
scale_y_continuous(limits=c(-750,750))
ggsave("/Users/bryanwilcox/Dropbox/2016 Voter Turnout/data/colorado/difference_co.png", plot, height = 8, width = 8)
# net votes dem
df <- data %>% dplyr::select(precinct,obama , romney, clinton,trump ,pct_latino)
df <- df %>% mutate(obama_raw_margin = obama - romney,
clinton_raw_margin = clinton - trump,
raw_net_dem = clinton_raw_margin - obama_raw_margin,
direction = ifelse(raw_net_dem > 0, "Clinton Improves", "Clinton Worsens"))
plot <- ggplot(df, aes(x=pct_latino, y = raw_net_dem, color = direction)) + geom_point(alpha = .25) +
scale_color_manual(values = c('blue', 'red'), name = "Direction") +
theme_bw() + labs(title = "2016 Colorado Latino Vote",
y = "Net Raw Difference \n (Difference 16 - Difference 12)",
x = "Percent Latino Registered \n Voter in Precinct") +
geom_hline(yintercept = 0, lty =2 ) +
scale_y_continuous(limits=c(-500,750))
# net votes
df <- data %>% dplyr::select(precinct,obama , romney, clinton,trump ,pct_latino)
df <- df %>% mutate(obama_raw_margin = obama - romney,
clinton_raw_margin = clinton - trump,
raw_net_dem = clinton_raw_margin - obama_raw_margin,
direction = ifelse(raw_net_dem > 0, "Clinton Improves", "Clinton Worsens"))
plot <- ggplot(df, aes(x=pct_latino, y = raw_net_dem, color = direction)) + geom_point(alpha = .25) +
scale_color_manual(values = c('blue', 'red'), name = "Direction") +
theme_bw() + labs(title = "2016 Colorado Latino Vote",
y = "Net Raw Difference \n (Difference 16 - Difference 12)",
x = "Percent Latino Registered \n Voter in Precinct") +
geom_hline(yintercept = 0, lty =2 ) +
scale_y_continuous(limits=c(-500,750))
# weighted comparison of 2012 and 2016
df <- gather(data ,candidate, pct_vote ,c(pct_obama, pct_romney, pct_trump, pct_clinton))
df$wt <- ifelse(df$candidate=='pct_obama'| df$candidate=="pct_romney",df$votes_2012, df$votes)
head(df)
weighted <- ggplot(df, aes(x=pct_latino, y = pct_vote, color = candidate, weight = wt, size = wt)) + geom_point(alpha = .15) +
scale_color_manual(values = c('blue', 'turquoise', 'coral3', 'red'),
breaks = c('pct_clinton', 'pct_obama', 'pct_romney', 'pct_trump'),
labels = c('Clinton', 'Obama', 'Romney', 'Trump'),
name = "Candidate") +
stat_smooth(se = F) +
theme_bw() + scale_y_continuous(aes(weight = wt), limits=c(0,1)) +
labs(title = "2012 & 2016 Colorado Latino Vote",
y = "Latino Vote Share",
x = "Percent Latino Registered \n Voter in Precinct") + guides(size=F)
ggsave("/Users/bryanwilcox/Dropbox/2016 Voter Turnout/data/colorado/all_candidate_co.png", weighted, height = 8, width = 8)