Commit
·
a204f16
1
Parent(s):
80b6508
add 29
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- 29/paper.pdf +3 -0
- 29/replication_package/Code/.Rhistory +512 -0
- 29/replication_package/Code/Figure 2, 3, Table SI1.R +183 -0
- 29/replication_package/Code/Figure 4.R +53 -0
- 29/replication_package/Code/Figure 5.R +145 -0
- 29/replication_package/Code/Figure 6, SI1.R +221 -0
- 29/replication_package/Code/Figure 7, SI2, SI17.R +125 -0
- 29/replication_package/Code/Figure 8, SI19.R +95 -0
- 29/replication_package/Code/Figure SI11, SI12, SI15, SI16.R +115 -0
- 29/replication_package/Code/Figure SI3, SI4, SI5.R +167 -0
- 29/replication_package/Code/Figure SI6, SI7, SI8, SI9, SI10.R +348 -0
- 29/replication_package/Code/LOG/log_Figure 2, 3, Table SI1.txt +3 -0
- 29/replication_package/Code/LOG/log_Figure 4.txt +3 -0
- 29/replication_package/Code/LOG/log_Figure 5.txt +3 -0
- 29/replication_package/Code/LOG/log_Figure 6, SI1.txt +3 -0
- 29/replication_package/Code/LOG/log_Figure 7, SI2, SI17.txt +3 -0
- 29/replication_package/Code/LOG/log_Figure 8, SI19.txt +3 -0
- 29/replication_package/Code/LOG/log_Figure SI11, SI12, SI15, SI16.txt +3 -0
- 29/replication_package/Code/LOG/log_Figure SI3, SI4, SI5.txt +3 -0
- 29/replication_package/Code/LOG/log_Figure SI6, SI7, SI8, SI9, SI10.txt +3 -0
- 29/replication_package/Code/LOG/log_SI_robust_prep.txt +3 -0
- 29/replication_package/Code/LOG/log_Table 1, SI3, SI4, SI5, Figure SI13, SI14.txt +3 -0
- 29/replication_package/Code/LOG/log_Table 2.txt +3 -0
- 29/replication_package/Code/LOG/log_Table 3, Figure SI18.txt +3 -0
- 29/replication_package/Code/LOG/log_zzSI_robust_prep.txt +3 -0
- 29/replication_package/Code/MASTER.R +13 -0
- 29/replication_package/Code/Table 1, SI3, SI4, SI5, Figure SI13, SI14.R +349 -0
- 29/replication_package/Code/Table 2.R +82 -0
- 29/replication_package/Code/Table 3, Figure SI18.R +109 -0
- 29/replication_package/Code/helper_functions.R +206 -0
- 29/replication_package/Code/zzSI_robust_prep.R +161 -0
- 29/replication_package/Data/Pew/biden_sanders_ideo_feb_march_PEW.csv +3 -0
- 29/replication_package/Data/Results/SI-data.RData +3 -0
- 29/replication_package/Data/Results/tjbalWgtsNEW.RData +3 -0
- 29/replication_package/Data/france_data.RData +3 -0
- 29/replication_package/Data/gtrends_data.RData +3 -0
- 29/replication_package/Data/mobility_data.RData +3 -0
- 29/replication_package/Data/nationscape_data.RData +3 -0
- 29/replication_package/Data/primary_data.RData +3 -0
- 29/replication_package/Data/replication_data.RData +3 -0
- 29/replication_package/Data/survey_experiment_data.RData +3 -0
- 29/replication_package/Figures/SI_figure1.pdf +3 -0
- 29/replication_package/Figures/SI_figure10.pdf +3 -0
- 29/replication_package/Figures/SI_figure11.pdf +3 -0
- 29/replication_package/Figures/SI_figure12.pdf +3 -0
- 29/replication_package/Figures/SI_figure13.pdf +3 -0
- 29/replication_package/Figures/SI_figure14.pdf +3 -0
- 29/replication_package/Figures/SI_figure15.pdf +3 -0
- 29/replication_package/Figures/SI_figure16.pdf +3 -0
- 29/replication_package/Figures/SI_figure17.pdf +3 -0
29/paper.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:15d29b2b15d48f28c8e6962fa1ba757e5175e6d2cf7ff1a146329c8c95a7aa2f
|
3 |
+
size 2599693
|
29/replication_package/Code/.Rhistory
ADDED
@@ -0,0 +1,512 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
}
|
2 |
+
# Determine intervals between values of the moderator
|
3 |
+
if (incr == "default"){
|
4 |
+
increment = (max_val - min_val)/(num_points - 1)
|
5 |
+
}else{
|
6 |
+
increment = incr
|
7 |
+
}
|
8 |
+
# Create list of moderator values at which marginal effect is evaluated
|
9 |
+
x_2 <- seq(from=min_val, to=max_val, by=increment)
|
10 |
+
# Compute marginal effects
|
11 |
+
delta_1 = beta_1 + beta_3*x_2
|
12 |
+
# Compute variances
|
13 |
+
var_1 = covMat[effect,effect] + (x_2^2)*covMat[interaction, interaction] + 2*x_2*covMat[effect, interaction]
|
14 |
+
# Standard errors
|
15 |
+
se_1 = sqrt(var_1)
|
16 |
+
# Upper and lower confidence bounds
|
17 |
+
z_score = qnorm(1 - ((1 - conf)/2))
|
18 |
+
upper_bound = sapply(1:length(z_score), function(x) delta_1 + z_score[x]*se_1)
|
19 |
+
lower_bound = sapply(1:length(z_score), function(x) delta_1 - z_score[x]*se_1)
|
20 |
+
# Determine the bounds of the graphing area
|
21 |
+
max_y = max(upper_bound)
|
22 |
+
min_y = min(lower_bound)
|
23 |
+
# Make the histogram color
|
24 |
+
hist_col = colr
|
25 |
+
stars <- ifelse(abs(summary(model)$coefficients[interaction,3]) >2.6,"***",
|
26 |
+
ifelse(abs(summary(model)$coefficients[interaction,3]) > 1.96,"**",
|
27 |
+
ifelse(abs(summary(model)$coefficients[interaction,3]) > 1.65,"*","")))
|
28 |
+
est <- paste("Interaction: ",round(summary(model)$coefficients[interaction,1],3),stars," (",
|
29 |
+
round(summary(model)$coefficients[interaction,2],3),")",sep="")
|
30 |
+
# Initialize plotting window
|
31 |
+
if(plot) {
|
32 |
+
plot(x=c(), y=c(), ylim=c(min_y, max_y), xlim=c(min_val, max_val),
|
33 |
+
xlab=xlabel, ylab=ylabel, main=title)
|
34 |
+
# Plot estimated effects
|
35 |
+
if(!pointsplot) {
|
36 |
+
lines(y=delta_1, x=x_2,col = colr)
|
37 |
+
for(i in ncol(upper_bound):1) {
|
38 |
+
polygon(c(x_2,rev(x_2)),c(upper_bound[,i],rev(lower_bound[,i])),border = NA,
|
39 |
+
col = colr)
|
40 |
+
}
|
41 |
+
}else{
|
42 |
+
points(y = delta_1,x = x_2,col = colr,pch = 19)
|
43 |
+
for(i in ncol(upper_bound):1) {
|
44 |
+
segments(x_2,upper_bound[,i],x_2,lower_bound[,i],col = colr,lwd = i)
|
45 |
+
}
|
46 |
+
}
|
47 |
+
# Add a dashed horizontal line for zero
|
48 |
+
abline(h=0, lty=3)
|
49 |
+
# Add a vertical line at the mean
|
50 |
+
if (mean){
|
51 |
+
abline(v = mean(mod_frame[[moderator]]), lty=2, col="red")
|
52 |
+
}
|
53 |
+
# Add a vertical line at the median
|
54 |
+
if (median){
|
55 |
+
abline(v = median(mod_frame[[moderator]]), lty=3, col="blue")
|
56 |
+
}
|
57 |
+
# Add Rug plot
|
58 |
+
if (rugplot){
|
59 |
+
rug(mod_frame[[moderator]])
|
60 |
+
}
|
61 |
+
if (show_est) {
|
62 |
+
text(par('usr')[ 2 ], par('usr')[ 4 ],adj=c(1.05,1.2),
|
63 |
+
labels = est)
|
64 |
+
}
|
65 |
+
#Add Histogram (Histogram only plots when minimum and maximum are the min/max of the moderator)
|
66 |
+
if (histogram & minimum=="min" & maximum=="max"){
|
67 |
+
par(new=T)
|
68 |
+
hist(mod_frame[[moderator]], axes=F, xlab="", ylab="",main="", border=hist_col, col=hist_col)
|
69 |
+
}
|
70 |
+
}
|
71 |
+
return(list(delta_1 = delta_1,x_2 = x_2,ub = upper_bound,lb = lower_bound,inc = increment,est = est))
|
72 |
+
}
|
73 |
+
####################################################################################################################### Preparing variables
|
74 |
+
Y <- paste0(c("pct_","VAP_","pcttw_"),"sanders")
|
75 |
+
D <- unlist(lapply(c("sc_","ln_"),function(x) paste0(x,paste0(c("county_","DMA_","state_"),"cases"))))
|
76 |
+
FE <- c("0","DMA_CODE","date")
|
77 |
+
covariates <- c("sc_CTY_LTHS","sc_CTY_CollUp",'caucus_switch','caucus',
|
78 |
+
"sc_CTY_LT30yo","sc_CTY_60Up",#"sc_CTY_Old_age_dep_ratio",
|
79 |
+
"sc_CTY_Below_poverty_level_AGE_18_64","sc_CTY_Female_hher_no_husbandhh",
|
80 |
+
"sc_CTY_Unem_rate_pop_16_over","sc_CTY_Labor_Force_Part_Rate_pop_16_over",
|
81 |
+
"sc_CTY_Manufactur","sc_CTY_Md_inc_hhs",
|
82 |
+
"sc_CTY_POPPCT_RURAL","sc_CTY_Speak_only_English","sc_CTY_White","sc_CTY_Black_or_African_American",
|
83 |
+
"ln_CTY_tot_pop","sc_turnout_pct_20")
|
84 |
+
####################################################################################################################### Looping across specifications
|
85 |
+
finalDat$sc_turnout_pct_chg <- scale(finalDat$turnout_pct_chg)
|
86 |
+
finalDat$sc_VAP_turnout_20 <- scale(finalDat$VAP_turnout_20)
|
87 |
+
####################################################################################################################### Looping across specifications
|
88 |
+
resRegs <- resBal <- resTabs <- list()
|
89 |
+
y
|
90 |
+
Y <- c('pct_sanders16')
|
91 |
+
D <- unlist(lapply(c("sc_","ln_"),function(x) paste0(x,paste0(c("county_","DMA_","state_"),"cases"))))
|
92 |
+
FE <- c("0","DMA_CODE","stab")
|
93 |
+
covariates <- c("sc_CTY_LTHS","sc_CTY_CollUp",'caucus_switch','caucus',
|
94 |
+
"sc_CTY_LT30yo","sc_CTY_60Up",#"sc_CTY_Old_age_dep_ratio",
|
95 |
+
"sc_CTY_Below_poverty_level_AGE_18_64","sc_CTY_Female_hher_no_husbandhh",
|
96 |
+
"sc_CTY_Unem_rate_pop_16_over","sc_CTY_Labor_Force_Part_Rate_pop_16_over",
|
97 |
+
"sc_CTY_Manufactur","sc_CTY_Md_inc_hhs",
|
98 |
+
"sc_CTY_POPPCT_RURAL","sc_CTY_Speak_only_English",
|
99 |
+
"sc_CTY_White","sc_CTY_Black_or_African_American",
|
100 |
+
"ln_CTY_tot_pop","sc_turnout_pct_20")
|
101 |
+
####################################################################################################################### Looping across specifications
|
102 |
+
resRegs <- resBal <- resTabs <- list()
|
103 |
+
for(y in Y) {
|
104 |
+
for(d in D[c(1:3)]) {
|
105 |
+
for(fe in FE) {
|
106 |
+
stars <- list()
|
107 |
+
for(dateThresh in c("2020-01-01","2020-03-01")) {
|
108 |
+
if(dateThresh == '2020-03-01') {
|
109 |
+
covs <- covariates[-which(covariates == 'caucus')]
|
110 |
+
} else {
|
111 |
+
covs <- covariates
|
112 |
+
}
|
113 |
+
if(!is.null(resRegs[[y]][[d]][[fe]][[dateThresh]]$basic$felm$cont)) { next }
|
114 |
+
finalDat$treatBin <- ifelse(finalDat[[gsub("sc_|ln_","",d)]] > 1,1,0)
|
115 |
+
foranal.weight <- finalDat %>% select(y,d,treatBin,gsub("sc_|ln_","",d),DMA_CODE,date,stab,covs) %>%
|
116 |
+
filter(complete.cases(.),date >= as.Date(dateThresh))
|
117 |
+
m.out <- matchit(formula = as.formula(paste("treatBin ~ ",paste(covs,collapse = " + "))),
|
118 |
+
data = foranal.weight,
|
119 |
+
method = "nearest",
|
120 |
+
distance = "mahalanobis")
|
121 |
+
m.data <- match.data(m.out)
|
122 |
+
W.out <- weightit(formula(paste0("treatBin ~ ",paste(covs,collapse = " + "))),
|
123 |
+
data = foranal.weight, estimand = "ATT", method = "cbps")
|
124 |
+
# Balance tables
|
125 |
+
balt.pre <- bal.tab(formula(paste0("treatBin ~ ",paste(covs,collapse = " + "))),
|
126 |
+
data = foranal.weight, estimand = "ATT",m.threshold = .05)
|
127 |
+
balt.post <- bal.tab(W.out, m.threshold = .05, disp.v.ratio = TRUE)
|
128 |
+
unm <- data.frame(Covs = rownames(balt.pre$Balance),
|
129 |
+
balt.pre$Balance %>% mutate(Diff_Unm = round(Diff.Un,2),
|
130 |
+
Bal_Test_Unm = M.Threshold.Un)) %>%
|
131 |
+
select(Covs,Diff_Unm,Bal_Test_Unm)
|
132 |
+
match <- data.frame(Covs = rownames(balt.post$Balance),
|
133 |
+
balt.post$Balance %>% mutate(Diff_Match = round(Diff.Adj,2),
|
134 |
+
Bal_Test_Match = M.Threshold)) %>%
|
135 |
+
select(Covs,Diff_Match,Bal_Test_Match)
|
136 |
+
# balTab <- match %>% left_join(unm) %>% select(Covs,Diff_Unm,Bal_Test_Unm,Diff_Match,Bal_Test_Match)
|
137 |
+
# balTab$Covs <- Hmisc::capitalize(trimws(gsub("manufactur","Manufacturing",gsub("labor force part rate","LFPR",
|
138 |
+
# gsub("hher|hhs","HH",gsub("bachelor s","Bachelor's",gsub("_"," ",gsub("sc_|cty_|age_18_64|pop_16_over|hh$|poppct","",tolower(balTab$Covs)))))))))
|
139 |
+
# resBal[[y]][[d]][[fe]][[dateThresh]]$balTab <- balTab
|
140 |
+
# resBal[[y]][[d]][[fe]][[dateThresh]]$balPlot <- qqprep(m.out)
|
141 |
+
# Basic
|
142 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$basic$felm$cont <- summary(stars$cont[[paste0(dateThresh,"_1")]] <- felm(as.formula(paste0("scale(",y,") ~ ",d," + ",paste(c(covs),collapse = " + ")," | ",fe," | 0 | ",fe)),finalDat %>% filter(date >= as.Date(dateThresh))))$coefficients
|
143 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$basic$felm$bin <- summary(stars$bin[[paste0(dateThresh,"_1")]] <- felm(as.formula(paste0("scale(",y,") ~ treatBin + ",paste(c(covs),collapse = " + ")," | ",fe," | 0 | ",fe)),finalDat %>% filter(date >= as.Date(dateThresh))))$coefficients
|
144 |
+
# Matching
|
145 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$matching$felm$cont <- summary(stars$cont[[paste0(dateThresh,"_2")]] <- felm(as.formula(paste0("scale(",y,") ~ ",d," + ",paste(c(covs),collapse = " + ")," | ",fe," | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients
|
146 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$matching$felm$bin <- summary(stars$bin[[paste0(dateThresh,"_2")]] <- felm(as.formula(paste0("scale(",y,") ~ treatBin + ",paste(c(covs),collapse = " + ")," | ",fe," | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients
|
147 |
+
# Weighting
|
148 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$weighting$felm$cont <- summary(stars$cont[[paste0(dateThresh,"_3")]] <- felm(as.formula(paste0("scale(",y,") ~ ",d," + ",paste(c(covs),collapse = " + ")," | ",fe," | 0 | ",fe)),foranal.weight,weights = W.out$weights))$coefficients
|
149 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$weighting$felm$bin <- summary(stars$bin[[paste0(dateThresh,"_3")]] <- felm(as.formula(paste0("scale(",y,") ~ treatBin + ",paste(c(covs),collapse = " + ")," | ",fe," | 0 | ",fe)),foranal.weight,weights = W.out$weights))$coefficients
|
150 |
+
if(fe != "0") {
|
151 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$basic$lmer <- summary(lmer(as.formula(paste0("scale(",y,") ~ ",d," + ",paste(c(covs),collapse = " + ")," + (1| ",fe,")")),finalDat %>% filter(date >= as.Date(dateThresh))))$coefficients
|
152 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$matching$lmer <- summary(lmer(as.formula(paste0("scale(",y,") ~ ",d," + ",paste(c(covs),collapse = " + ")," + (1| ",fe,")")),m.data,weights = m.data$weights))$coefficients
|
153 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$weighting$lmer <- summary(lmer(as.formula(paste0("scale(",y,") ~ ",d," + ",paste(c(covs),collapse = " + ")," + (1| ",fe,")")),foranal.weight,weights = W.out$weights))$coefficients
|
154 |
+
}
|
155 |
+
# covars <- rownames(stars$cont[[1]]$coefficients)
|
156 |
+
# labs <- gsub("Lths","LTHS",gsub("Collup","Coll. Up",gsub("Md inc HH","Med HH Inc",gsub("Sc ","",gsub(" or african american|band$","",Hmisc::capitalize(trimws(gsub("manufactur","manufacturing",gsub("hher|hhs","HH",gsub("labor force part rate","LFPR",gsub("bachelor s","bachelor's",gsub("age 18 64|pop 16 over|hh$|poppct","",gsub("\\_"," ",gsub("^sc_|^.*cty\\_","",tolower(covars)))))))))))))))
|
157 |
+
# resTabs[[y]][[d]][[fe]]$cont$html <- stargazer(stars$cont,type = "html",keep.stat = c("n","rsq"),covariate.labels = labs)
|
158 |
+
# resTabs[[y]][[d]][[fe]]$cont$tex <- stargazer(stars$cont,keep.stat = c("n","rsq"),covariate.labels = labs)
|
159 |
+
# covars <- rownames(stars$bin[[1]]$coefficients)
|
160 |
+
# labs <- gsub("Lths","LTHS",gsub("Collup","Coll. Up",gsub("Md inc HH","Med HH Inc",gsub("Sc ","",gsub(" or african american|band$","",Hmisc::capitalize(trimws(gsub("manufactur","manufacturing",gsub("hher|hhs","HH",gsub("labor force part rate","LFPR",gsub("bachelor s","bachelor's",gsub("age 18 64|pop 16 over|hh$|poppct","",gsub("\\_"," ",gsub("^sc_|^.*cty\\_","",tolower(covars)))))))))))))))
|
161 |
+
# resTabs[[y]][[d]][[fe]]$bin$html <- stargazer(stars$bin,type = "html",keep.stat = c("n","rsq"),covariate.labels = labs)
|
162 |
+
# resTabs[[y]][[d]][[fe]]$bin$tex <- stargazer(stars$bin,keep.stat = c("n","rsq"),covariate.labels = labs)
|
163 |
+
}
|
164 |
+
}
|
165 |
+
cat(y,"\n")
|
166 |
+
}
|
167 |
+
}
|
168 |
+
toplot <- NULL
|
169 |
+
for(fe in names(resRegs$pct_sanders16$sc_DMA_cases)) {
|
170 |
+
for(mod in c('basic','matching','weighting')) {
|
171 |
+
for(meas in c('bin','cont')) {
|
172 |
+
tmp <- resRegs$pct_sanders16$sc_DMA_cases[[fe]]$`2020-01-01`[[mod]]$felm[[meas]][2,]
|
173 |
+
if(is.null(tmp)) { next }
|
174 |
+
tmp <- data.frame(t(tmp))
|
175 |
+
colnames(tmp) <- c('est','se','tstat','pval')
|
176 |
+
tmp$fe <- fe
|
177 |
+
tmp$mod <- mod
|
178 |
+
tmp$meas <- meas
|
179 |
+
toplot <- bind_rows(toplot,tmp)
|
180 |
+
}
|
181 |
+
}
|
182 |
+
}
|
183 |
+
toplot %>%
|
184 |
+
filter(meas != 'cont') %>%
|
185 |
+
mutate(fe = factor(ifelse(fe == 0,'None',
|
186 |
+
ifelse(fe == 'DMA_CODE','DMA','State')),levels = c('None','DMA','State'))) %>%
|
187 |
+
ggplot(aes(x = fe,y = est,color = mod)) +
|
188 |
+
geom_point(position = position_dodge(width = .2)) +
|
189 |
+
geom_errorbar(aes(ymin = est - 2*se,ymax = est+2*se),width = .1,
|
190 |
+
position = position_dodge(width = .2)) +
|
191 |
+
geom_hline(yintercept = 0,linetype = 'dashed') +
|
192 |
+
scale_color_manual(name = 'Model',values = c('basic' = 'grey70',
|
193 |
+
'matching' = 'grey40',
|
194 |
+
'weighting' = 'black')) +
|
195 |
+
xlab('Fixed Effect') + ylab('Effect of Covid Exposure on Bernie 2016') +
|
196 |
+
theme_ridges()
|
197 |
+
list.files('./')
|
198 |
+
# Master script
|
199 |
+
for(f in list.files('./')) {
|
200 |
+
if(grepl('helper_functions|MASTER|LOG',f)) { next }
|
201 |
+
# stop()
|
202 |
+
con <- file(paste0('./LOG/log_',gsub('.R','.txt',f)))
|
203 |
+
cat(f,'\n')
|
204 |
+
sink(con,append = TRUE)
|
205 |
+
sink(con,append = TRUE,type = 'message')
|
206 |
+
source(f,echo = T,max.deparse.length = 10000)
|
207 |
+
sink()
|
208 |
+
sink(type = 'message')
|
209 |
+
}
|
210 |
+
list.files('./')
|
211 |
+
file.exits(paste0('./LOG/log_',gsub('.R','.txt',f)))
|
212 |
+
file.exists(paste0('./LOG/log_',gsub('.R','.txt',f)))
|
213 |
+
# Master script
|
214 |
+
for(f in list.files('./')) {
|
215 |
+
if(grepl('helper_functions|MASTER|LOG',f)) { next }
|
216 |
+
if(file.exists(paste0('./LOG/log_',gsub('.R','.txt',f)))) { next }
|
217 |
+
# stop()
|
218 |
+
con <- file(paste0('./LOG/log_',gsub('.R','.txt',f)))
|
219 |
+
cat(f,'\n')
|
220 |
+
sink(con,append = TRUE)
|
221 |
+
sink(con,append = TRUE,type = 'message')
|
222 |
+
source(f,echo = T,max.deparse.length = 10000)
|
223 |
+
sink()
|
224 |
+
sink(type = 'message')
|
225 |
+
}
|
226 |
+
require(lme4)
|
227 |
+
require(lfe)
|
228 |
+
require(MatchIt)
|
229 |
+
require(WeightIt)
|
230 |
+
require(tjbal)
|
231 |
+
require(optmatch)
|
232 |
+
require(stargazer)
|
233 |
+
require(cobalt)
|
234 |
+
require(gridExtra)
|
235 |
+
require(ggridges)
|
236 |
+
require(ggrepel)
|
237 |
+
require(CBPS)
|
238 |
+
require(tidyverse)
|
239 |
+
rm(list = ls())
|
240 |
+
gc()
|
241 |
+
####################################################################################################################### Loading data
|
242 |
+
load('../Data/replication_data.RData')
|
243 |
+
####################################################################################################################### Loading functions
|
244 |
+
source('./helper_functions.R')
|
245 |
+
# ####################################################################################################################### Command line arguments
|
246 |
+
# args <- commandArgs(trailingOnly = T)
|
247 |
+
# # args <- c(3,2,2,2)
|
248 |
+
# # Y: 1-3
|
249 |
+
# # D: 1-2
|
250 |
+
# # FE: 1-4
|
251 |
+
# # GEO: 1-2
|
252 |
+
# yInd <- as.numeric(args[1])
|
253 |
+
# dInd <- as.numeric(args[2])
|
254 |
+
# feInd <- as.numeric(args[3])
|
255 |
+
# geoInd <- as.numeric(args[4])
|
256 |
+
####################################################################################################################### Preparing variables
|
257 |
+
Y <- paste0(c("pct_","VAP_","pcttw_"),"sanders")
|
258 |
+
D <- unlist(lapply(c("sc_","ln_"),function(x) paste0(x,paste0(c("county_","DMA_","state_"),"cases"))))
|
259 |
+
FE <- c("0","DMA_CODE","date")
|
260 |
+
covariates <- c("sc_CTY_LTHS","sc_CTY_CollUp",
|
261 |
+
"sc_CTY_LT30yo","sc_CTY_60Up",
|
262 |
+
"sc_CTY_Below_poverty_level_AGE_18_64","sc_CTY_Female_hher_no_husbandhh",
|
263 |
+
"sc_CTY_Unem_rate_pop_16_over","sc_CTY_Labor_Force_Part_Rate_pop_16_over",
|
264 |
+
"sc_CTY_Manufactur","sc_CTY_Md_inc_hhs",
|
265 |
+
"sc_CTY_POPPCT_RURAL","sc_CTY_Speak_only_English","sc_CTY_White","sc_CTY_Black_or_African_American",
|
266 |
+
"ln_CTY_tot_pop","sc_turnout_pct_20")
|
267 |
+
set.seed(123)
|
268 |
+
resSI <- list()
|
269 |
+
for(y in 'pcttw_sanders') {
|
270 |
+
for(fe in c('DMA_CODE','0')) {
|
271 |
+
for(geo in 'DMA_') {
|
272 |
+
for(d in c('March10Cases','March17Cases')) {
|
273 |
+
for(pre in c("2020-03-01","2020-03-03","2020-03-10","2020-03-17")) {
|
274 |
+
for(post in c("2020-03-01","2020-03-03","2020-03-10","2020-03-17")) {
|
275 |
+
if(pre == post) { next }
|
276 |
+
if(pre == "2020-03-01") {
|
277 |
+
finalDat$post <- ifelse(finalDat$date == as.Date(post),1,
|
278 |
+
ifelse(finalDat$date <= as.Date(pre),0,NA))
|
279 |
+
} else if(post == "2020-03-01") {
|
280 |
+
finalDat$post <- ifelse(finalDat$date <= as.Date(post),1,
|
281 |
+
ifelse(finalDat$date == as.Date(pre),0,NA))
|
282 |
+
} else {
|
283 |
+
finalDat$post <- ifelse(finalDat$date == as.Date(post),1,
|
284 |
+
ifelse(finalDat$date == as.Date(pre),0,NA))
|
285 |
+
}
|
286 |
+
finalDat$treatBin <- ifelse(finalDat[[paste0(geo,d)]] > 1,1,0)
|
287 |
+
finalDat$treat <- ifelse(finalDat$post == 1 & finalDat$treatBin == 1,1,0)
|
288 |
+
tmpAnal <- finalDat %>% select(Y,treat,treatBin,c(covariates,paste0(y,"16")),pcttw_sanders16,VAP_sanders16,DMA_CODE,date,stab,matches("cases"),post) %>% filter(complete.cases(.))
|
289 |
+
m.out <- matchit(formula = as.formula(paste("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
|
290 |
+
data = tmpAnal,
|
291 |
+
method = "nearest",
|
292 |
+
distance = "mahalanobis")
|
293 |
+
m.data <- match.data(m.out)
|
294 |
+
W.out <- weightit(formula(paste0("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
|
295 |
+
data = tmpAnal, estimand = "ATT", method = "cbps")
|
296 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$bin <- summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients
|
297 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$cont <- summary(felm(as.formula(paste0('scale(',y,") ~ ",paste0("scale(",geo,d,")")," + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients
|
298 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$did$coefs <- summary(tmp <- felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients
|
299 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$did$mfx <- interaction_plot_continuous(tmp,num_points = 2,pointsplot = T)
|
300 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$bin <- summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients
|
301 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$cont <- summary(felm(as.formula(paste0('scale(',y,") ~ ",paste0("scale(",geo,d,")")," + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients
|
302 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$did$coefs <- summary(tmp <- felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients
|
303 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$did$mfx <- interaction_plot_continuous(tmp,num_points = 2,pointsplot = T)
|
304 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$bin <- summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients
|
305 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$cont <- summary(felm(as.formula(paste0('scale(',y,") ~ ",paste0("scale(",geo,d,")")," + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients
|
306 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$did$coefs <- summary(tmp <- felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients
|
307 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$did$mfx <- interaction_plot_continuous(tmp,num_points = 2,pointsplot = T)
|
308 |
+
# Permutation SHEEYAHT
|
309 |
+
basic.bin.plac <- basic.cont.plac <- basic.did.plac <- matching.bin.plac <- matching.cont.plac <- matching.did.plac <- weighting.bin.plac <- weighting.cont.plac <- weighting.did.plac <- NULL
|
310 |
+
for(bs in 1:100) {
|
311 |
+
tmpAnal$perm <- sample(tmpAnal[[paste0(geo,d)]],size = nrow(tmpAnal))
|
312 |
+
tmpAnal$treatBin <- ifelse(tmpAnal$perm > 1,1,0)
|
313 |
+
tmpAnal$treat <- ifelse(tmpAnal$post == 1 & tmpAnal$treatBin == 1,1,0)
|
314 |
+
m.out <- matchit(formula = as.formula(paste("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
|
315 |
+
data = tmpAnal,
|
316 |
+
method = "full",
|
317 |
+
distance = "mahalanobis")
|
318 |
+
m.data <- match.data(m.out)
|
319 |
+
W.out <- weightit(formula(paste0("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
|
320 |
+
data = tmpAnal, estimand = "ATT", method = "cbps")
|
321 |
+
test <- tryCatch(felm(as.formula(paste0('scale(',y,") ~ ",paste0("scale(",geo,d,")")," + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights),error = function(e) e)
|
322 |
+
if(inherits(test,"error")) { next }
|
323 |
+
basic.bin.plac <- c(basic.bin.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients['treat',1])
|
324 |
+
basic.cont.plac <- c(basic.cont.plac,summary(felm(as.formula(paste0('scale(',y,") ~ scale(perm) + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients["scale(perm)",1])
|
325 |
+
basic.did.plac <- c(basic.did.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients["treatBin:post",1])
|
326 |
+
matching.bin.plac <- c(matching.bin.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients['treat',1])
|
327 |
+
matching.cont.plac <- c(matching.cont.plac,summary(felm(as.formula(paste0('scale(',y,") ~ scale(perm) + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients["scale(perm)",1])
|
328 |
+
matching.did.plac <- c(matching.did.plac,
|
329 |
+
summary(felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients["treatBin:post",1])
|
330 |
+
weighting.bin.plac <- c(weighting.bin.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients['treat',1])
|
331 |
+
weighting.cont.plac <- c(weighting.cont.plac,summary(felm(as.formula(paste0('scale(',y,") ~ scale(perm) + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients["scale(perm)",1])
|
332 |
+
weighting.did.plac <- c(weighting.did.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients["treatBin:post",1])
|
333 |
+
cat(".")
|
334 |
+
}
|
335 |
+
cat('\n')
|
336 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$plac$bin <- basic.bin.plac
|
337 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$plac$cont <- basic.cont.plac
|
338 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$plac$did <- basic.did.plac
|
339 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$plac$bin <- matching.bin.plac
|
340 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$plac$cont <- matching.cont.plac
|
341 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$plac$did <- matching.did.plac
|
342 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$plac$bin <- weighting.bin.plac
|
343 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$plac$cont <- weighting.cont.plac
|
344 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$plac$did <- weighting.did.plac
|
345 |
+
}
|
346 |
+
}
|
347 |
+
}
|
348 |
+
}
|
349 |
+
}
|
350 |
+
cat(y," done\n")
|
351 |
+
}
|
352 |
+
require(lme4)
|
353 |
+
require(lfe)
|
354 |
+
require(MatchIt)
|
355 |
+
require(WeightIt)
|
356 |
+
require(tjbal)
|
357 |
+
require(optmatch)
|
358 |
+
require(stargazer)
|
359 |
+
require(cobalt)
|
360 |
+
require(gridExtra)
|
361 |
+
require(ggridges)
|
362 |
+
require(ggrepel)
|
363 |
+
require(CBPS)
|
364 |
+
require(tidyverse)
|
365 |
+
rm(list = ls())
|
366 |
+
gc()
|
367 |
+
####################################################################################################################### Loading data
|
368 |
+
load('../Data/replication_data.RData')
|
369 |
+
####################################################################################################################### Loading functions
|
370 |
+
source('./helper_functions.R')
|
371 |
+
# ####################################################################################################################### Command line arguments
|
372 |
+
# args <- commandArgs(trailingOnly = T)
|
373 |
+
# # args <- c(3,2,2,2)
|
374 |
+
# # Y: 1-3
|
375 |
+
# # D: 1-2
|
376 |
+
# # FE: 1-4
|
377 |
+
# # GEO: 1-2
|
378 |
+
# yInd <- as.numeric(args[1])
|
379 |
+
# dInd <- as.numeric(args[2])
|
380 |
+
# feInd <- as.numeric(args[3])
|
381 |
+
# geoInd <- as.numeric(args[4])
|
382 |
+
####################################################################################################################### Preparing variables
|
383 |
+
Y <- paste0(c("pct_","VAP_","pcttw_"),"sanders")
|
384 |
+
D <- unlist(lapply(c("sc_","ln_"),function(x) paste0(x,paste0(c("county_","DMA_","state_"),"cases"))))
|
385 |
+
FE <- c("0","DMA_CODE","date")
|
386 |
+
covariates <- c("sc_CTY_LTHS","sc_CTY_CollUp",
|
387 |
+
"sc_CTY_LT30yo","sc_CTY_60Up",
|
388 |
+
"sc_CTY_Below_poverty_level_AGE_18_64","sc_CTY_Female_hher_no_husbandhh",
|
389 |
+
"sc_CTY_Unem_rate_pop_16_over","sc_CTY_Labor_Force_Part_Rate_pop_16_over",
|
390 |
+
"sc_CTY_Manufactur","sc_CTY_Md_inc_hhs",
|
391 |
+
"sc_CTY_POPPCT_RURAL","sc_CTY_Speak_only_English","sc_CTY_White","sc_CTY_Black_or_African_American",
|
392 |
+
"ln_CTY_tot_pop","sc_turnout_pct_20")
|
393 |
+
set.seed(123)
|
394 |
+
resSI <- list()
|
395 |
+
for(y in 'pcttw_sanders') {
|
396 |
+
for(fe in c('DMA_CODE','0')) {
|
397 |
+
for(geo in 'DMA_') {
|
398 |
+
for(d in c('March10Cases','March17Cases')) {
|
399 |
+
for(pre in c("2020-03-01","2020-03-03","2020-03-10","2020-03-17")) {
|
400 |
+
for(post in c("2020-03-01","2020-03-03","2020-03-10","2020-03-17")) {
|
401 |
+
if(pre == post) { next }
|
402 |
+
if(pre == "2020-03-01") {
|
403 |
+
finalDat$post <- ifelse(finalDat$date == as.Date(post),1,
|
404 |
+
ifelse(finalDat$date <= as.Date(pre),0,NA))
|
405 |
+
} else if(post == "2020-03-01") {
|
406 |
+
finalDat$post <- ifelse(finalDat$date <= as.Date(post),1,
|
407 |
+
ifelse(finalDat$date == as.Date(pre),0,NA))
|
408 |
+
} else {
|
409 |
+
finalDat$post <- ifelse(finalDat$date == as.Date(post),1,
|
410 |
+
ifelse(finalDat$date == as.Date(pre),0,NA))
|
411 |
+
}
|
412 |
+
finalDat$treatBin <- ifelse(finalDat[[paste0(geo,d)]] > 1,1,0)
|
413 |
+
finalDat$treat <- ifelse(finalDat$post == 1 & finalDat$treatBin == 1,1,0)
|
414 |
+
tmpAnal <- finalDat %>% select(Y,treat,treatBin,c(covariates,paste0(y,"16")),pcttw_sanders16,VAP_sanders16,DMA_CODE,date,stab,matches("cases"),post) %>% filter(complete.cases(.))
|
415 |
+
m.out <- matchit(formula = as.formula(paste("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
|
416 |
+
data = tmpAnal,
|
417 |
+
method = "nearest",
|
418 |
+
distance = "mahalanobis")
|
419 |
+
m.data <- match.data(m.out)
|
420 |
+
W.out <- weightit(formula(paste0("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
|
421 |
+
data = tmpAnal, estimand = "ATT", method = "cbps")
|
422 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$bin <- summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients
|
423 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$cont <- summary(felm(as.formula(paste0('scale(',y,") ~ ",paste0("scale(",geo,d,")")," + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients
|
424 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$did$coefs <- summary(tmp <- felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients
|
425 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$did$mfx <- interaction_plot_continuous(tmp,num_points = 2,pointsplot = T)
|
426 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$bin <- summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients
|
427 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$cont <- summary(felm(as.formula(paste0('scale(',y,") ~ ",paste0("scale(",geo,d,")")," + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients
|
428 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$did$coefs <- summary(tmp <- felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients
|
429 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$did$mfx <- interaction_plot_continuous(tmp,num_points = 2,pointsplot = T)
|
430 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$bin <- summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients
|
431 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$cont <- summary(felm(as.formula(paste0('scale(',y,") ~ ",paste0("scale(",geo,d,")")," + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients
|
432 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$did$coefs <- summary(tmp <- felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients
|
433 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$did$mfx <- interaction_plot_continuous(tmp,num_points = 2,pointsplot = T)
|
434 |
+
# Permutation SHEEYAHT
|
435 |
+
basic.bin.plac <- basic.cont.plac <- basic.did.plac <- matching.bin.plac <- matching.cont.plac <- matching.did.plac <- weighting.bin.plac <- weighting.cont.plac <- weighting.did.plac <- NULL
|
436 |
+
for(bs in 1:100) {
|
437 |
+
tmpAnal$perm <- sample(tmpAnal[[paste0(geo,d)]],size = nrow(tmpAnal))
|
438 |
+
tmpAnal$treatBin <- ifelse(tmpAnal$perm > 1,1,0)
|
439 |
+
tmpAnal$treat <- ifelse(tmpAnal$post == 1 & tmpAnal$treatBin == 1,1,0)
|
440 |
+
m.out <- matchit(formula = as.formula(paste("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
|
441 |
+
data = tmpAnal,
|
442 |
+
method = "full",
|
443 |
+
distance = "mahalanobis")
|
444 |
+
m.data <- match.data(m.out)
|
445 |
+
W.out <- weightit(formula(paste0("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
|
446 |
+
data = tmpAnal, estimand = "ATT", method = "cbps")
|
447 |
+
test <- tryCatch(felm(as.formula(paste0('scale(',y,") ~ ",paste0("scale(",geo,d,")")," + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights),error = function(e) e)
|
448 |
+
if(inherits(test,"error")) { next }
|
449 |
+
basic.bin.plac <- c(basic.bin.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients['treat',1])
|
450 |
+
basic.cont.plac <- c(basic.cont.plac,summary(felm(as.formula(paste0('scale(',y,") ~ scale(perm) + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients["scale(perm)",1])
|
451 |
+
basic.did.plac <- c(basic.did.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients["treatBin:post",1])
|
452 |
+
matching.bin.plac <- c(matching.bin.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients['treat',1])
|
453 |
+
matching.cont.plac <- c(matching.cont.plac,summary(felm(as.formula(paste0('scale(',y,") ~ scale(perm) + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients["scale(perm)",1])
|
454 |
+
matching.did.plac <- c(matching.did.plac,
|
455 |
+
summary(felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients["treatBin:post",1])
|
456 |
+
weighting.bin.plac <- c(weighting.bin.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients['treat',1])
|
457 |
+
weighting.cont.plac <- c(weighting.cont.plac,summary(felm(as.formula(paste0('scale(',y,") ~ scale(perm) + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients["scale(perm)",1])
|
458 |
+
weighting.did.plac <- c(weighting.did.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients["treatBin:post",1])
|
459 |
+
cat(".")
|
460 |
+
}
|
461 |
+
cat('\n')
|
462 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$plac$bin <- basic.bin.plac
|
463 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$plac$cont <- basic.cont.plac
|
464 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$plac$did <- basic.did.plac
|
465 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$plac$bin <- matching.bin.plac
|
466 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$plac$cont <- matching.cont.plac
|
467 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$plac$did <- matching.did.plac
|
468 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$plac$bin <- weighting.bin.plac
|
469 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$plac$cont <- weighting.cont.plac
|
470 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$plac$did <- weighting.did.plac
|
471 |
+
}
|
472 |
+
}
|
473 |
+
}
|
474 |
+
}
|
475 |
+
}
|
476 |
+
cat(y," done\n")
|
477 |
+
}
|
478 |
+
# Master script
|
479 |
+
for(f in list.files('./')) {
|
480 |
+
if(grepl('helper_functions|MASTER|LOG',f)) { next }
|
481 |
+
if(file.exists(paste0('./LOG/log_',gsub('.R','.txt',f)))) { next }
|
482 |
+
stop()
|
483 |
+
con <- file(paste0('./LOG/log_',gsub('.R','.txt',f)))
|
484 |
+
cat(f,'\n')
|
485 |
+
sink(con,append = TRUE)
|
486 |
+
sink(con,append = TRUE,type = 'message')
|
487 |
+
source(f,echo = T,max.deparse.length = 10000)
|
488 |
+
sink()
|
489 |
+
sink(type = 'message')
|
490 |
+
}
|
491 |
+
f
|
492 |
+
# stop()
|
493 |
+
con <- file(paste0('./LOG/log_',gsub('.R','.txt',f)))
|
494 |
+
cat(f,'\n')
|
495 |
+
sink(con,append = TRUE)
|
496 |
+
sink(con,append = TRUE,type = 'message')
|
497 |
+
source(f,echo = T,max.deparse.length = 10000)
|
498 |
+
sink()
|
499 |
+
sink(type = 'message')
|
500 |
+
# Master script
|
501 |
+
for(f in list.files('./')) {
|
502 |
+
if(grepl('helper_functions|MASTER|LOG',f)) { next }
|
503 |
+
if(file.exists(paste0('./LOG/log_',gsub('.R','.txt',f)))) { next }
|
504 |
+
# stop()
|
505 |
+
con <- file(paste0('./LOG/log_',gsub('.R','.txt',f)))
|
506 |
+
cat(f,'\n')
|
507 |
+
sink(con,append = TRUE)
|
508 |
+
sink(con,append = TRUE,type = 'message')
|
509 |
+
source(f,echo = T,max.deparse.length = 10000)
|
510 |
+
sink()
|
511 |
+
sink(type = 'message')
|
512 |
+
}
|
29/replication_package/Code/Figure 2, 3, Table SI1.R
ADDED
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# File name: figure2_figure3_SItable1.R
|
2 |
+
# In:
|
3 |
+
# - replication_data.RData
|
4 |
+
# Out:
|
5 |
+
# - /Figures/figure2.pdf
|
6 |
+
# - /Figures/figure3.pdf
|
7 |
+
# - /Tables/SI-table1.tex
|
8 |
+
|
9 |
+
require(lme4)
|
10 |
+
require(lfe)
|
11 |
+
require(MatchIt)
|
12 |
+
require(WeightIt)
|
13 |
+
require(tjbal)
|
14 |
+
require(optmatch)
|
15 |
+
require(stargazer)
|
16 |
+
require(cobalt)
|
17 |
+
require(tidyverse)
|
18 |
+
require(sf)
|
19 |
+
require(leaflet)
|
20 |
+
require(ggridges)
|
21 |
+
require(ggthemes)
|
22 |
+
require(gridExtra)
|
23 |
+
|
24 |
+
rm(list = ls())
|
25 |
+
gc()
|
26 |
+
########################################################################################## Loading data
|
27 |
+
load('../Data/replication_data.RData')
|
28 |
+
|
29 |
+
|
30 |
+
# Figure 2: Map (compressed to PNG in manuscript)
|
31 |
+
toplot <- maps$county %>% filter(!state %in% c("Alaska","Hawaii"))
|
32 |
+
toplot$DMA.CODE[which(toplot$state == 'Texas' & toplot$name %in% c('Throckmorton','Knox'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Texas' & toplot$name %in% c('Baylor'))])
|
33 |
+
toplot$DMA.CODE[which(toplot$state == 'Texas' & toplot$name %in% c('Montague'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Texas' & toplot$name %in% c('Wise'))])
|
34 |
+
toplot$DMA.CODE[which(toplot$state == 'Colorado' & toplot$name %in% c('Cheyenne'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Colorado' & toplot$name %in% c('Kiowa'))])
|
35 |
+
toplot$DMA.CODE[which(toplot$state == 'Nebraska' & toplot$name %in% c('Hooker','Arthur','Blaine'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Nebraska' & toplot$name %in% c('Thomas'))])
|
36 |
+
toplot$DMA.CODE[which(toplot$state == 'Wyoming' & toplot$name %in% c('Johnson'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Wyoming' & toplot$name %in% c('Natrona'))])
|
37 |
+
toplot$DMA.CODE[which(toplot$state == 'Iowa' & toplot$name %in% c('Emmet'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Minnesota' & toplot$name %in% c('Martin'))])
|
38 |
+
toplot$DMA.CODE[which(toplot$state == 'Minnesota' & toplot$name %in% c('Nicollet'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Minnesota' & toplot$name %in% c('Sibley'))])
|
39 |
+
toplot$DMA.CODE[which(toplot$state == 'Illinois' & toplot$name %in% c('Edgar'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Illinois' & toplot$name %in% c('Clark'))])
|
40 |
+
toplot$DMA.CODE[which(toplot$state == 'Kentucky' & toplot$name %in% c('Adair','Cumberland'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Kentucky' & toplot$name %in% c('Metcalfe'))])
|
41 |
+
toplot$DMA.CODE[which(toplot$state == 'Ohio' & toplot$name %in% c('Athens'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Ohio' & toplot$name %in% c('Meigs'))])
|
42 |
+
toplot$DMA.CODE[which(toplot$state == 'Ohio' & toplot$name %in% c('Guernsey','Noble'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Ohio' & toplot$name %in% c('Belmont'))])
|
43 |
+
toplot$DMA.CODE[which(toplot$state == 'Arkansas' & toplot$name %in% c('Nevada'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Arkansas' & toplot$name %in% c('Columbia'))])
|
44 |
+
toplot$DMA.CODE[which(toplot$state == 'Tennessee' & toplot$name %in% c('Gibson','Chester'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Tennessee' & toplot$name %in% c('McNairy'))])
|
45 |
+
toplot$DMA.CODE[which(toplot$state == 'South Carolina' & toplot$name %in% c('Saluda'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'South Carolina' & toplot$name %in% c('Edgefield'))])
|
46 |
+
toplot$DMA.CODE[which(toplot$state == 'California' & toplot$name %in% c('Modoc'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'California' & toplot$name %in% c('Siskiyou'))])
|
47 |
+
toplot$DMA.CODE[which(toplot$state == 'Arkansas' & toplot$name %in% c('Johnson'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Arkansas' & toplot$name %in% c('Pope'))])
|
48 |
+
toplot$DMA.CODE[which(toplot$state == 'Colorado' & toplot$name %in% c('Custer'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Colorado' & toplot$name %in% c('Saguache'))])
|
49 |
+
toplot$DMA.CODE[which(toplot$state == 'Wyoming' & toplot$name %in% c('Lincoln'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Wyoming' & toplot$name %in% c('Teton'))])
|
50 |
+
toplot$DMA.CODE[which(toplot$state == 'Nebraska' & toplot$name %in% c('Banner'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Nebraska' & toplot$name %in% c('Morrill'))])
|
51 |
+
toplot$DMA.CODE[which(toplot$state == 'South Dakota' & toplot$name %in% c('Jones'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'South Dakota' & toplot$name %in% c('Lyman'))])
|
52 |
+
toplot$DMA.CODE[which(toplot$state == 'South Dakota' & toplot$name %in% c('Dewey','Campbell'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'South Dakota' & toplot$name %in% c('Corson'))])
|
53 |
+
toplot$DMA.CODE[which(toplot$state == 'Iowa' & toplot$name %in% c('Adams'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Iowa' & toplot$name %in% c('Montgomery'))])
|
54 |
+
toplot$DMA.CODE[which(toplot$state == 'Missouri' & toplot$name %in% c('Mercer'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Iowa' & toplot$name %in% c('Decatur'))])
|
55 |
+
toplot$DMA.CODE[which(toplot$state == 'Missouri' & toplot$name %in% c('Phelps'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Missouri' & toplot$name %in% c('Pulaski'))])
|
56 |
+
toplot$DMA.CODE[which(toplot$state == 'Missouri' & toplot$name %in% c('Reynolds'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Missouri' & toplot$name %in% c('Wayne'))])
|
57 |
+
toplot$DMA.CODE[which(toplot$state == 'Oklahoma' & toplot$name %in% c('Ottawa'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Kansas' & toplot$name %in% c('Cherokee'))])
|
58 |
+
toplot$DMA.CODE[which(toplot$state == 'Kentucky' & toplot$name %in% c('Magoffin'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Kentucky' & toplot$name %in% c('Johnson'))])
|
59 |
+
toplot$DMA.CODE[which(toplot$state == 'Pennsylvania' & toplot$name %in% c('Franklin'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Pennsylvania' & toplot$name %in% c('Fulton'))])
|
60 |
+
toplot$DMA.CODE[which(toplot$state == 'Ohio' & toplot$name %in% c('Auglaize'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Ohio' & toplot$name %in% c('Shelby'))])
|
61 |
+
toplot$DMA.CODE[which(toplot$state == 'Georgia' & toplot$name %in% c('Taylor'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Georgia' & toplot$name %in% c('Marion'))])
|
62 |
+
toplot$DMA.CODE[which(toplot$state == 'Illinois' & toplot$name %in% c('Cass'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Illinois' & toplot$name %in% c('Brown'))])
|
63 |
+
toplot$DMA.CODE[which(toplot$state == 'Tennessee' & toplot$name %in% c('Wayne'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Alabama' & toplot$name %in% c('Lauderdale'))])
|
64 |
+
toplot$DMA.CODE[which(toplot$state == 'Texas' & toplot$name %in% c('Red River','Camp'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Texas' & toplot$name %in% c('Titus'))])
|
65 |
+
toplot$DMA.CODE[which(toplot$state == 'Nebraska' & toplot$name %in% c('Butler','Pawnee'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Nebraska' & toplot$name %in% c('Johnson'))])
|
66 |
+
toplot$DMA.CODE[which(toplot$state == 'New Mexico' & toplot$name %in% c('Lea'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Texas' & toplot$name %in% c('Winkler'))])
|
67 |
+
toplot$DMA.CODE[which(toplot$state == 'Kansas' & toplot$name %in% c('Morton'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Oklahoma' & toplot$name %in% c('Texas'))])
|
68 |
+
toplot$DMA.CODE[which(toplot$state == 'Georgia' & toplot$name %in% c('Seminole'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'Georgia' & toplot$name %in% c('Early'))])
|
69 |
+
toplot$DMA.CODE[which(toplot$state == 'Virginia' & toplot$name %in% c('Grayson'))] <- unique(toplot$DMA.CODE[which(toplot$state == 'North Carolina' & toplot$name %in% c('Alleghany'))])
|
70 |
+
|
71 |
+
as.data.frame(toplot) %>% ungroup() %>% select(state,DMA.CODE) %>% distinct() %>%
|
72 |
+
group_by(DMA.CODE) %>% summarise(n=n()) -> multiStateDMA
|
73 |
+
|
74 |
+
toplot %>% left_join(multiStateDMA %>% mutate(DMA.CODE,multiState = (n > 1)+0) %>% select(-n)) -> toplot
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
toplot$treated <- ifelse(toplot$state %in% c('Iowa','New Hampshire','South Carolina','Nevada',
|
79 |
+
'Alabama','Arkansas','California','Colorado','Maine',
|
80 |
+
'Massachusetts','Minnesota','North Carolina','Oklahoma',
|
81 |
+
'Tennessee','Texas','Utah','Vermont','Virginia'),0,1)
|
82 |
+
comparisons <- as.data.frame(toplot) %>% select(DMA.CODE,multiState,stab,treated) %>%
|
83 |
+
filter(multiState == 1) %>%
|
84 |
+
group_by(DMA.CODE) %>%
|
85 |
+
summarise(IDvar = mean(treated)) %>%
|
86 |
+
filter(IDvar > 0 & IDvar < 1)
|
87 |
+
|
88 |
+
|
89 |
+
pdf('../Figures/figure2.pdf',width = 16,height = 12)
|
90 |
+
toplot %>%
|
91 |
+
left_join(comparisons) %>%
|
92 |
+
mutate(IDvar = ifelse(is.na(IDvar),0,IDvar)) %>%
|
93 |
+
mutate(treated2 = ifelse(treated == 1 & IDvar > 0,4,
|
94 |
+
ifelse(treated == 0 & IDvar > 0,3,
|
95 |
+
ifelse(treated == 1 & IDvar == 0,2,1)))) %>%
|
96 |
+
mutate(treated2 = ifelse(IDvar > 0,treated2,'drop')) %>%
|
97 |
+
ggplot() +
|
98 |
+
geom_sf(aes(fill = factor(treated2),color = treated2),size = .1) +
|
99 |
+
scale_fill_manual(name = 'Primary Date',
|
100 |
+
values = c('4' = '#1e1e1e','3' = '#878787',
|
101 |
+
'2' = 'white','1' = 'white'),
|
102 |
+
breaks = c('4','3'),
|
103 |
+
labels = c('4' = 'Post-Outbreak','3' = 'Pre-Outbreak')) +
|
104 |
+
scale_color_manual(guide = F,values = c('4' = 'white','3' = 'white','drop' = 'grey80'),na.translate = F) +
|
105 |
+
geom_sf(data = maps$DMA,color = 'black',fill = NA) +
|
106 |
+
theme_map()
|
107 |
+
dev.off()
|
108 |
+
|
109 |
+
|
110 |
+
|
111 |
+
# Figure 3
|
112 |
+
p1 <- finalDat %>%
|
113 |
+
mutate(treat = ifelse(county_March17Cases > 0,1,0),
|
114 |
+
post = ifelse(date > as.Date('2020-03-10'),'Post March 10','Pre March 17')) %>%
|
115 |
+
group_by(treat,post) %>%
|
116 |
+
summarise(outcome = mean(pcttw_sanders,na.rm=T),
|
117 |
+
sd = sd(pcttw_sanders,na.rm=T)) %>%
|
118 |
+
ggplot(aes(x = factor(post,levels = c("Pre March 17","Post March 10")),y = outcome,
|
119 |
+
fill = factor(treat))) +
|
120 |
+
stat_summary(fun.y = mean,geom = "bar",position = position_dodge(width = .9),
|
121 |
+
size = 3,alpha = .7) +
|
122 |
+
scale_fill_manual(name = '',values = c('0' = 'grey80','1' = 'grey30'),
|
123 |
+
labels = c('0' = 'Insulated on March 17th','1' = 'Exposed on March 17th')) +
|
124 |
+
theme_ridges() + xlab('Period') + ylab('Sanders Two-Way Vote Share')
|
125 |
+
|
126 |
+
p2 <- finalDat %>%
|
127 |
+
mutate(post = ifelse(date > as.Date('2020-03-10'),'Post','Pre'),
|
128 |
+
logcases = log(county_March17Cases+1)) %>%
|
129 |
+
ggplot(aes(x = logcases,y = pcttw_sanders,color = factor(post,levels = c("Pre","Post")),size = turnout_20,weight = log(turnout_20+1))) +
|
130 |
+
geom_point(alpha = .6) +
|
131 |
+
geom_smooth(method = 'lm') +
|
132 |
+
scale_color_manual(name = "Period",values = c('Post' = 'grey30',"Pre" = "grey70")) +
|
133 |
+
scale_size_continuous(guide = F) +
|
134 |
+
theme_ridges() + xlab("March 17 Cases (logged)") + ylab('Sanders Two-Way Vote Share')
|
135 |
+
|
136 |
+
|
137 |
+
pdf('../Figures/figure3.pdf',width = 7,height = 5)
|
138 |
+
grid.arrange(p1 + theme(legend.position = 'bottom'),
|
139 |
+
p2 + theme(legend.position = 'bottom'),ncol = 2)
|
140 |
+
dev.off()
|
141 |
+
|
142 |
+
|
143 |
+
|
144 |
+
# SI Table 1
|
145 |
+
covariates <- c("sc_CTY_LTHS","sc_CTY_CollUp",'caucus_switch','caucus',
|
146 |
+
"sc_CTY_LT30yo","sc_CTY_60Up",
|
147 |
+
"sc_CTY_Below_poverty_level_AGE_18_64","sc_CTY_Female_hher_no_husbandhh",
|
148 |
+
"sc_CTY_Unem_rate_pop_16_over","sc_CTY_Labor_Force_Part_Rate_pop_16_over",
|
149 |
+
"sc_CTY_Manufactur","sc_CTY_Md_inc_hhs",
|
150 |
+
"sc_CTY_POPPCT_RURAL","sc_CTY_Speak_only_English","sc_CTY_White","sc_CTY_Black_or_African_American",
|
151 |
+
"ln_CTY_tot_pop","sc_turnout_pct_20")
|
152 |
+
|
153 |
+
maps$county %>% left_join(multiStateDMA %>% mutate(DMA.CODE,multiState = (n > 1)+0) %>% select(-n)) -> maps$county
|
154 |
+
as.data.frame(maps$county) %>% ungroup() %>% select(FIPS,multiState) %>%
|
155 |
+
distinct() %>%
|
156 |
+
left_join(finalDat,by = c("FIPS" = "stcou")) %>%
|
157 |
+
select(multiState,gsub("sc_|ln_","",covariates),pct_sanders16) -> tocompare
|
158 |
+
|
159 |
+
zeroOne <- function(x) {
|
160 |
+
return(x > 0 & x < 1)
|
161 |
+
}
|
162 |
+
|
163 |
+
div100 <- function(x) {
|
164 |
+
return(x/100)
|
165 |
+
}
|
166 |
+
tocompare %>%
|
167 |
+
mutate(turnout_pct_20 = turnout_pct_20*100) %>%
|
168 |
+
gather(variable,value,-multiState) %>%
|
169 |
+
filter(complete.cases(.)) %>%
|
170 |
+
group_by(multiState,variable) %>%
|
171 |
+
summarise(value = list(value)) %>%
|
172 |
+
spread(multiState,value,sep = "_") %>%
|
173 |
+
group_by(variable) %>%
|
174 |
+
mutate(p_value = round(t.test(unlist(multiState_0), unlist(multiState_1))$p.value,2),
|
175 |
+
t_value = round(t.test(unlist(multiState_0), unlist(multiState_1))$statistic,1),
|
176 |
+
avg_0 = round(mean(unlist(multiState_0),na.rm=T),1),
|
177 |
+
avg_1 = round(mean(unlist(multiState_1),na.rm=T),1),
|
178 |
+
sd_0 = round(sd(unlist(multiState_0),na.rm=T),1),
|
179 |
+
sd_1 = round(sd(unlist(multiState_1),na.rm=T),1)) %>%
|
180 |
+
select(variable,matches("avg|sd"),p_value,t_value) %>%
|
181 |
+
arrange(t_value) -> dma_fe_balance
|
182 |
+
dma_fe_balance$variable <- gsub("_"," ",gsub("CTY_","",dma_fe_balance$variable))
|
183 |
+
stargazer(data.frame(dma_fe_balance %>% select(-matches("sd_"))),summary = F,digits = 1,out = '../Tables/SI-table1.tex')
|
29/replication_package/Code/Figure 4.R
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# File name: figure4.R
|
2 |
+
# In:
|
3 |
+
# - replication_data.RData
|
4 |
+
# Out:
|
5 |
+
# - /Figures/figure4.pdf
|
6 |
+
|
7 |
+
require(lme4)
|
8 |
+
require(lfe)
|
9 |
+
require(MatchIt)
|
10 |
+
require(WeightIt)
|
11 |
+
require(tjbal)
|
12 |
+
require(optmatch)
|
13 |
+
require(stargazer)
|
14 |
+
require(cobalt)
|
15 |
+
require(tidyverse)
|
16 |
+
require(ggridges)
|
17 |
+
rm(list = ls())
|
18 |
+
gc()
|
19 |
+
####################################################################################################################### Loading data
|
20 |
+
load('../Data/replication_data.RData')
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
####################################################################################################################### Loading functions
|
31 |
+
source('./helper_functions.R')
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
# Figure 4
|
38 |
+
finalDat$post <- ifelse(finalDat$date > as.Date('2020-03-10'),1,0)
|
39 |
+
finalDat$treat <- ifelse(finalDat$DMA_March17Cases > 1,1,0)
|
40 |
+
summary(tmp <- lmer(as.formula(paste0("pcttw_sanders ~ treat*post + (1|DMA_CODE)")),finalDat))$coefficients
|
41 |
+
toplot <- data.frame(interaction_plot_continuous(tmp,num_points = 2,pointsplot = T))
|
42 |
+
|
43 |
+
|
44 |
+
pdf('../Figures/figure4.pdf',width = 7,height = 5)
|
45 |
+
toplot %>%
|
46 |
+
mutate(x = factor(ifelse(x_2 == 0,'Pre','Post'),levels = c('Pre','Post'))) %>%
|
47 |
+
ggplot(aes(x = x,y = delta_1))+
|
48 |
+
geom_point() +
|
49 |
+
geom_errorbar(aes(ymin = lb,ymax = ub),width= .1) +
|
50 |
+
geom_hline(yintercept = 0,linetype = 'dashed') +
|
51 |
+
xlab("Period") + ylab("Marginal Effect of Exposure") +
|
52 |
+
theme_ridges()
|
53 |
+
dev.off()
|
29/replication_package/Code/Figure 5.R
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# File name: figure5.R
|
2 |
+
# In:
|
3 |
+
# - replication_data.RData
|
4 |
+
# Out:
|
5 |
+
# - /Figures/figure5.pdf
|
6 |
+
|
7 |
+
|
8 |
+
require(lme4)
|
9 |
+
require(lfe)
|
10 |
+
require(MatchIt)
|
11 |
+
require(WeightIt)
|
12 |
+
require(tjbal)
|
13 |
+
require(optmatch)
|
14 |
+
require(stargazer)
|
15 |
+
require(cobalt)
|
16 |
+
require(tidyverse)
|
17 |
+
require(ggridges)
|
18 |
+
|
19 |
+
rm(list = ls())
|
20 |
+
gc()
|
21 |
+
####################################################################################################################### Loading data
|
22 |
+
load('../Data/replication_data.RData')
|
23 |
+
|
24 |
+
|
25 |
+
####################################################################################################################### Loading functions
|
26 |
+
source('./helper_functions.R')
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
D <- unlist(lapply(c("sc_","ln_"),function(x) paste0(x,paste0(c("county_","DMA_","state_"),"cases"))))
|
35 |
+
FE <- c("0","DMA_CODE","date")
|
36 |
+
covariates <- c("sc_CTY_LTHS","sc_CTY_CollUp",
|
37 |
+
"sc_CTY_LT30yo","sc_CTY_60Up",
|
38 |
+
"sc_CTY_Below_poverty_level_AGE_18_64","sc_CTY_Female_hher_no_husbandhh",
|
39 |
+
"sc_CTY_Unem_rate_pop_16_over","sc_CTY_Labor_Force_Part_Rate_pop_16_over",
|
40 |
+
"sc_CTY_Manufactur","sc_CTY_Md_inc_hhs",
|
41 |
+
"sc_CTY_POPPCT_RURAL","sc_CTY_Speak_only_English","sc_CTY_White","sc_CTY_Black_or_African_American",
|
42 |
+
"ln_CTY_tot_pop","sc_turnout_pct_20")
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
# Week-by-week
|
51 |
+
weeks <- c("2020-02-22","2020-02-29",
|
52 |
+
"2020-03-03","2020-03-10","2020-03-17","2020-04-07")
|
53 |
+
|
54 |
+
resWkly <- list()
|
55 |
+
for(y in c("pcttw_sanders")) {
|
56 |
+
for(d in c("DMA_March10Cases",
|
57 |
+
"DMA_March17Cases")) {
|
58 |
+
for(fe in "0") {
|
59 |
+
|
60 |
+
wkly <- wklyBin <- wklyMatch <- wklyWeight <- wklyMatchBin <- wklyWeightBin <- NULL
|
61 |
+
if(grepl("March",d)) {
|
62 |
+
wks <- weeks
|
63 |
+
} else {
|
64 |
+
wks <- weeks[4:6]
|
65 |
+
}
|
66 |
+
for(week in wks) {
|
67 |
+
if(fe == 'stab' & week == '2020-04-07') { next }
|
68 |
+
cat(paste0(week,'/',y,'/',d,'/',fe,'\n'))
|
69 |
+
if(grepl("county",d) & week == "2020-03-01") { next }
|
70 |
+
if(week < as.Date("2020-03-01")) {
|
71 |
+
tmp <- finalDat %>% select(y,matches(gsub("sc_|ln_","",d)),DMA_CODE,stab,date,c(covariates,paste0(y,"16"))) %>% filter(date < as.Date(week)) %>% filter(complete.cases(.))
|
72 |
+
} else {
|
73 |
+
tmp <- finalDat %>% select(y,matches(gsub("sc_|ln_","",d)),DMA_CODE,stab,date,c(covariates,paste0(y,"16"))) %>% filter(date == as.Date(week)) %>% filter(complete.cases(.))
|
74 |
+
}
|
75 |
+
tmp$treatBin <- ifelse(tmp[[gsub("sc_|ln_","",d)]] > 0,1,0)
|
76 |
+
|
77 |
+
wkly <- bind_rows(wkly,data.frame(t(summary(felm(as.formula(paste0("scale(",y,") ~ scale(",gsub("sc_","",d),") + ",
|
78 |
+
paste(unique(c(covariates,paste0(y,"16"))),collapse = "+"),
|
79 |
+
"| ",fe," | 0 | 0")),tmp))$coefficients[paste0("scale(",gsub("sc_","",d),")"),]),date = week))
|
80 |
+
|
81 |
+
wklyBin <- bind_rows(wklyBin,data.frame(t(summary(felm(as.formula(paste0("scale(",y,") ~ treatBin + ",
|
82 |
+
paste(unique(c(covariates,paste0(y,"16"))),collapse = "+"),
|
83 |
+
"| ",fe," | 0 | 0")),tmp))$coefficients["treatBin",]),date = week))
|
84 |
+
|
85 |
+
if(length(unique(tmp$treatBin)) == 2) {
|
86 |
+
m.out <- try(matchit(formula = as.formula(paste("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
|
87 |
+
data = tmp,
|
88 |
+
method = "nearest",
|
89 |
+
distance = "mahalanobis"))
|
90 |
+
if(class(m.out) != "try-error") {
|
91 |
+
m.data <- match.data(m.out)
|
92 |
+
wklyMatch <- bind_rows(wklyMatch,data.frame(t(summary(felm(as.formula(paste0("scale(",y,") ~ scale(",gsub("sc_","",d),") + ",
|
93 |
+
paste(c(covariates,paste0(y,"16")),collapse = " + ")," | ",fe," | 0 | ",fe)),
|
94 |
+
m.data,weights = m.data$weights))$coefficients[paste0("scale(",gsub("sc_","",d),")"),]),date = week))
|
95 |
+
wklyMatchBin <- bind_rows(wklyMatchBin,data.frame(t(summary(felm(as.formula(paste0("scale(",y,") ~ treatBin + ",
|
96 |
+
paste(c(covariates,paste0(y,"16")),collapse = " + ")," | ",fe," | 0 | ",fe)),
|
97 |
+
m.data,weights = m.data$weights))$coefficients["treatBin",]),date = week))
|
98 |
+
}
|
99 |
+
|
100 |
+
W.out <- try(weightit(formula(paste0("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
|
101 |
+
data = tmp, estimand = "ATT", method = "cbps"))
|
102 |
+
|
103 |
+
|
104 |
+
if(class(W.out) != "try_error") {
|
105 |
+
wklyWeight <- bind_rows(wklyWeight,data.frame(t(summary(felm(as.formula(paste0("scale(",y,") ~ scale(",gsub("sc_","",d),") + ",
|
106 |
+
paste(c(covariates,paste0(y,"16")),collapse = " + ")," | ",fe," | 0 | ",fe)),
|
107 |
+
tmp,weights = W.out$weights))$coefficients[paste0("scale(",gsub("sc_","",d),")"),]),date = week))
|
108 |
+
|
109 |
+
wklyWeightBin <- bind_rows(wklyWeightBin,data.frame(t(summary(felm(as.formula(paste0("scale(",y,") ~ treatBin + ",
|
110 |
+
paste(c(covariates,paste0(y,"16")),collapse = " + ")," | ",fe," | 0 | ",fe)),
|
111 |
+
tmp,weights = W.out$weights))$coefficients["treatBin",]),date = week))
|
112 |
+
}
|
113 |
+
}
|
114 |
+
}
|
115 |
+
resWkly[[y]][[d]][[fe]]$basic$cont <- wkly
|
116 |
+
resWkly[[y]][[d]][[fe]]$basic$bin <- wklyBin
|
117 |
+
resWkly[[y]][[d]][[fe]]$matching$cont <- wklyMatch
|
118 |
+
resWkly[[y]][[d]][[fe]]$matching$bin <- wklyMatchBin
|
119 |
+
resWkly[[y]][[d]][[fe]]$weighting$cont <- wklyWeight
|
120 |
+
resWkly[[y]][[d]][[fe]]$weighting$bin <- wklyWeightBin
|
121 |
+
}
|
122 |
+
}
|
123 |
+
}
|
124 |
+
|
125 |
+
|
126 |
+
pdf('../Figures/figure5.pdf',width = 7,height = 5)
|
127 |
+
bind_rows(resWkly$pcttw_sanders$DMA_March17Cases$`0`$basic$cont %>%
|
128 |
+
mutate(type = 'basic',
|
129 |
+
date = as.Date(date) - .25),
|
130 |
+
resWkly$pcttw_sanders$DMA_March17Cases$`0`$weighting$cont %>%
|
131 |
+
mutate(type = 'weighting',
|
132 |
+
date = as.Date(date) + .25)) %>%
|
133 |
+
ggplot(aes(x = date,y = Estimate,color = type,fill = type)) +
|
134 |
+
geom_hline(yintercept = 0,linetype = 'dashed') +
|
135 |
+
geom_point(size = 1.5) +
|
136 |
+
geom_rect(aes(xmin = date-.25,ymin = Estimate - 1.96*`Std..Error`,
|
137 |
+
xmax = date+.25,ymax = Estimate + 1.96*`Std..Error`),alpha = .5,color = NA) +
|
138 |
+
theme_ridges() +
|
139 |
+
scale_color_manual(values = c('basic' = 'grey50','weighting' = 'grey40'),
|
140 |
+
guide = F) +
|
141 |
+
scale_fill_manual(name = 'Method',values = c('basic' = 'grey70','weighting' = 'grey40'),
|
142 |
+
labels = c('basic' = 'Unweighted OLS','weighting' = 'CBPS Weights')) +
|
143 |
+
xlab('Date') + ylab('Coefficient on March 17 Cases') +
|
144 |
+
theme(legend.position = 'bottom')
|
145 |
+
dev.off()
|
29/replication_package/Code/Figure 6, SI1.R
ADDED
@@ -0,0 +1,221 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# File name: figure6_SIfigure1.R
|
2 |
+
# In:
|
3 |
+
# - replication_data.RData
|
4 |
+
# Out:
|
5 |
+
# - /Data/Results/tjbalWgtsNEW.RData (saving time re-estimating the weights)
|
6 |
+
# - /Figures/figure6.pdf
|
7 |
+
# - /Figures/SI_figure1.pdf
|
8 |
+
|
9 |
+
require(lme4)
|
10 |
+
require(lfe)
|
11 |
+
require(MatchIt)
|
12 |
+
require(WeightIt)
|
13 |
+
require(tjbal)
|
14 |
+
require(optmatch)
|
15 |
+
require(stargazer)
|
16 |
+
require(cobalt)
|
17 |
+
require(tidyverse)
|
18 |
+
require(ggridges)
|
19 |
+
|
20 |
+
|
21 |
+
rm(list = ls())
|
22 |
+
gc()
|
23 |
+
####################################################################################################################### Loading data
|
24 |
+
load('../Data/replication_data.RData')
|
25 |
+
|
26 |
+
|
27 |
+
####################################################################################################################### Loading functions
|
28 |
+
source('./helper_functions.R')
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
####################################################################################################################### Preparing variables
|
36 |
+
Y <- paste0(c("pct_","VAP_","pcttw_"),"sanders")
|
37 |
+
D <- unlist(lapply(c("sc_","ln_"),function(x) paste0(x,paste0(c("county_","DMA_","state_"),"cases"))))
|
38 |
+
FE <- c("0","DMA_CODE","date")
|
39 |
+
covariates <- c("sc_CTY_LTHS","sc_CTY_CollUp",
|
40 |
+
"sc_CTY_LT30yo","sc_CTY_60Up",
|
41 |
+
"sc_CTY_Below_poverty_level_AGE_18_64","sc_CTY_Female_hher_no_husbandhh",
|
42 |
+
"sc_CTY_Unem_rate_pop_16_over","sc_CTY_Labor_Force_Part_Rate_pop_16_over",
|
43 |
+
"sc_CTY_Manufactur","sc_CTY_Md_inc_hhs",
|
44 |
+
"sc_CTY_POPPCT_RURAL","sc_CTY_Speak_only_English","sc_CTY_White","sc_CTY_Black_or_African_American",
|
45 |
+
"ln_CTY_tot_pop","sc_turnout_pct_20")
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
# SI Figure 1
|
51 |
+
set.seed(123)
|
52 |
+
treatInd <- "March17Cases"
|
53 |
+
vd <- '2020-03-17'
|
54 |
+
d <- 'ln_DMA_cases'
|
55 |
+
est <- 'meanfirst'
|
56 |
+
treatInd <- paste0("DMA_",treatInd)
|
57 |
+
forTjbalAnal$treatGroup <- ifelse(forTjbalAnal$voteDate == vd & forTjbalAnal[[treatInd]] > 0,1,0)
|
58 |
+
forTjbalAnal$treat <- ifelse(forTjbalAnal$treatGroup == 1 & forTjbalAnal$date > as.Date("2020-04-17"),1,0)
|
59 |
+
|
60 |
+
inds <- which(forTjbalAnal$treatGroup == 1)
|
61 |
+
inds <- unique(forTjbalAnal$stcou[inds])
|
62 |
+
ctrlInds <- unique(forTjbalAnal$stcou[-c(which(forTjbalAnal$stcou %in% inds),which(forTjbalAnal$voteDate >= as.Date(vd)))])
|
63 |
+
|
64 |
+
tmpTjbalAnal <- forTjbalAnal %>% filter(stcou %in% c(inds,ctrlInds))
|
65 |
+
tjout <- try(tjbal(data = as.data.frame(tmpTjbalAnal),Y = d,D = "treat",X = unique(c(covariates,"pct_sanders16")),
|
66 |
+
index = c("stcou","dateNum"),estimator = est,demean = T,vce = "fixed"))
|
67 |
+
|
68 |
+
|
69 |
+
toplot <- as.data.frame(tjout$Y.bar)
|
70 |
+
toplot$date <- seq.Date(from = as.Date("2020-01-23"),to = as.Date("2020-05-01"),length.out = nrow(tjout$Y.bar))
|
71 |
+
getwd()
|
72 |
+
pdf("../Figures/SI_figure1.pdf")
|
73 |
+
toplot %>%
|
74 |
+
gather(type,value,-date) %>%
|
75 |
+
ggplot(aes(x = date,y = value,color = type,size = type,linetype = type,alpha = type)) +
|
76 |
+
geom_line() +
|
77 |
+
geom_vline(xintercept = as.Date(unique(finalDat$Date)[which(as.Date(unique(finalDat$Date)) < as.Date("2020-03-17"))]),linetype = "dashed",alpha = .6) +
|
78 |
+
geom_vline(xintercept = as.Date("2020-03-17"),size = 1.2) +
|
79 |
+
xlab("Date") + ylab("Number of Cases (logged)") +
|
80 |
+
annotate(geom = "text",label = c("Iowa Caucuses","New Hampshire","Nevada","South Carolina","Super Tuesday","March 10","March 17"),
|
81 |
+
x = as.Date(c("2020-02-03","2020-02-11","2020-02-22","2020-02-29",
|
82 |
+
"2020-03-03","2020-03-10","2020-03-17")),y = Inf,angle = 90,vjust = 1,hjust = 1,size = 3.5,color = "grey30") +
|
83 |
+
annotate(geom = "text",label = c("Treated","Weighted","Control"),
|
84 |
+
y = c(7.8,7.5,6.6),x = as.Date("2020-05-02"),angle = 0,
|
85 |
+
parse = F,hjust = 0,vjust = .3,size = 3.5,color = "grey30") +
|
86 |
+
scale_linetype_manual(values = c("solid","dotted","solid")) +
|
87 |
+
scale_size_manual(values = c(1.2,1.2,2.5)) +
|
88 |
+
scale_color_manual(values = c("red","black","black")) +
|
89 |
+
scale_alpha_manual(values = c(1,1,.3)) +
|
90 |
+
xlim(as.Date("2020-01-23"),as.Date("2020-05-10")) + theme_ridges() +
|
91 |
+
theme(legend.position = "none") +
|
92 |
+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
|
93 |
+
panel.background = element_blank(), axis.line = element_line(colour = "black"))
|
94 |
+
dev.off()
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
+
# Figure 6
|
99 |
+
set.seed(123)
|
100 |
+
tjBalWgts <- list()
|
101 |
+
for(vd in c("2020-03-03","2020-03-10","2020-03-17")) {
|
102 |
+
for(d in c("county_cases")) {
|
103 |
+
for(est in c("kernel")) {
|
104 |
+
|
105 |
+
if(vd == "2020-03-03") {
|
106 |
+
treatInd <- "March3Cases"
|
107 |
+
} else if(vd == "2020-03-10") {
|
108 |
+
treatInd <- "March10Cases"
|
109 |
+
} else {
|
110 |
+
treatInd <- "March17Cases"
|
111 |
+
}
|
112 |
+
if(grepl("county",d)) {
|
113 |
+
treatInd <- paste0("county_",treatInd)
|
114 |
+
} else {
|
115 |
+
treatInd <- paste0("DMA_",treatInd)
|
116 |
+
}
|
117 |
+
|
118 |
+
forTjbalAnal$treatGroup <- ifelse(forTjbalAnal$voteDate == vd & forTjbalAnal[[treatInd]] > 0,1,0)
|
119 |
+
forTjbalAnal$treat <- ifelse(forTjbalAnal$treatGroup == 1 & forTjbalAnal$date > as.Date("2020-04-17"),1,0)
|
120 |
+
|
121 |
+
inds <- which(forTjbalAnal$treatGroup == 1)
|
122 |
+
inds <- unique(forTjbalAnal$stcou[inds])
|
123 |
+
ctrlInds <- unique(forTjbalAnal$stcou[-c(which(forTjbalAnal$stcou %in% inds),which(forTjbalAnal$voteDate >= as.Date(vd)))])
|
124 |
+
|
125 |
+
ests <- finalDat %>% select(stcou)
|
126 |
+
for(i in 1:length(inds)) {
|
127 |
+
tmpTjbalAnal <- forTjbalAnal %>% filter(stcou %in% c(inds[-i],ctrlInds))
|
128 |
+
sink <- capture.output(tjout <- try(tjbal(data = as.data.frame(tmpTjbalAnal),Y = d,D = "treat",
|
129 |
+
X = unique(c(covariates,"pct_sanders16")),seed = 123,
|
130 |
+
index = c("stcou","dateNum"),estimator = est,demean = T,vce = "fixed")))
|
131 |
+
|
132 |
+
if(class(tjout) == "try-error") { next }
|
133 |
+
tjout$data.wide$w <- tjout$w
|
134 |
+
ests <- ests %>% left_join(tjout$data.wide %>% select(stcou = unit,w),by = 'stcou')
|
135 |
+
colnames(ests)[which(colnames(ests) == "w")] <- paste0("w",i)
|
136 |
+
cat('.')
|
137 |
+
}
|
138 |
+
cat('\n')
|
139 |
+
|
140 |
+
tjBalWgts[[vd]][[d]][[est]]$jacknife <- ests
|
141 |
+
tmpTjbalAnal <- forTjbalAnal %>% filter(stcou %in% c(inds,ctrlInds))
|
142 |
+
sink <- capture.output(tjout <- try(tjbal(data = as.data.frame(tmpTjbalAnal),Y = d,D = "treat",X = unique(c(covariates,"pct_sanders16")),
|
143 |
+
index = c("stcou","dateNum"),estimator = est,demean = T,seed = 123,
|
144 |
+
vce = "fixed")))
|
145 |
+
|
146 |
+
if(class(tjout) == "try-error") { next }
|
147 |
+
tjout$data.wide$w <- tjout$w
|
148 |
+
tjBalWgts[[vd]][[d]][[est]]$full <- tjout$data.wide %>% select(stcou = unit,w)
|
149 |
+
}
|
150 |
+
}
|
151 |
+
}
|
152 |
+
|
153 |
+
save(tjBalWgts,file = "../Data/Results/tjbalWgtsNEW.RData")
|
154 |
+
load("../Data/Results/tjbalWgtsNEW.RData")
|
155 |
+
|
156 |
+
|
157 |
+
|
158 |
+
|
159 |
+
|
160 |
+
|
161 |
+
|
162 |
+
|
163 |
+
|
164 |
+
resTjbal <- list()
|
165 |
+
for(y in Y) {
|
166 |
+
for(geo in c("county_","DMA_")) {
|
167 |
+
for(d in c("March3Cases","March10Cases","March17Cases")) {
|
168 |
+
if(d == "March3Cases") {
|
169 |
+
int <- "2020-03-03"
|
170 |
+
} else if(d == "March10Cases") {
|
171 |
+
int <- "2020-03-10"
|
172 |
+
} else {
|
173 |
+
int <- "2020-03-17"
|
174 |
+
}
|
175 |
+
for(balCases in names(tjBalWgts[[int]])) {
|
176 |
+
for(est in names(tjBalWgts[[int]][[balCases]])) {
|
177 |
+
|
178 |
+
if(is.null(tjBalWgts[[int]][[balCases]][[est]]$full)) { next }
|
179 |
+
|
180 |
+
finalDat %>% left_join(tjBalWgts[[int]][[balCases]][[est]]$full) %>%
|
181 |
+
left_join(tjBalWgts[[int]][[balCases]][[est]]$jacknife) %>% filter(!is.na(w)) -> tmpAnal
|
182 |
+
tmpAnal$post <- ifelse(tmpAnal$date == as.Date(int),1,0)
|
183 |
+
tmpAnal$treatGroup <- ifelse(tmpAnal[[paste0(geo,d)]] > 0,1,0)
|
184 |
+
tmpAnal$treat <- ifelse(tmpAnal$post == 1 & tmpAnal$treatGroup == 1,1,0)
|
185 |
+
tmpAnal %>% select(post,treatGroup,treat,date)
|
186 |
+
|
187 |
+
ests <- NULL
|
188 |
+
for(i in colnames(tmpAnal %>% select(matches("^w\\d")))) {
|
189 |
+
ests <- c(ests,sum(tmpAnal[[y]]*tmpAnal[[i]],na.rm=T))
|
190 |
+
}
|
191 |
+
resTjbal[[y]][[geo]][[d]][[balCases]][[est]]$bootstrapped <- ests
|
192 |
+
resTjbal[[y]][[geo]][[d]][[balCases]][[est]]$lmCont <- summary(lm(as.formula(paste0(y," ~ ",balCases)),tmpAnal,weights = abs(tmpAnal$w)))$coefficients
|
193 |
+
resTjbal[[y]][[geo]][[d]][[balCases]][[est]]$lmBin <- summary(lm(as.formula(paste0(y," ~ treat")),tmpAnal,weights = abs(tmpAnal$w)))$coefficients
|
194 |
+
}
|
195 |
+
}
|
196 |
+
}
|
197 |
+
}
|
198 |
+
}
|
199 |
+
|
200 |
+
|
201 |
+
toplot <- NULL
|
202 |
+
for(period in names(resTjbal$pcttw_sanders$DMA_)) {
|
203 |
+
toplot <- bind_rows(toplot,
|
204 |
+
data.frame(bs = resTjbal$pcttw_sanders$county_[[period]]$county_cases$kernel$bootstrapped,
|
205 |
+
period = period))
|
206 |
+
}
|
207 |
+
|
208 |
+
|
209 |
+
|
210 |
+
|
211 |
+
# Figure 6:
|
212 |
+
pdf('../Figures/figure6.pdf',width = 7,height = 5)
|
213 |
+
toplot %>%
|
214 |
+
mutate(period = factor(gsub('Cases','',
|
215 |
+
gsub('March','March ',period)),levels = rev(c('March 3','March 10','March 17')))) %>%
|
216 |
+
ggplot(aes(x = bs,y = period)) +
|
217 |
+
geom_density_ridges(alpha = .7,color = 'black') +
|
218 |
+
theme_ridges() +
|
219 |
+
geom_vline(xintercept = 0,linetype = 'dashed') +
|
220 |
+
xlab('ATT of Exposure on Sanders Support') + ylab('Outbreak Date')
|
221 |
+
dev.off()
|
29/replication_package/Code/Figure 7, SI2, SI17.R
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# File name: figure7_SIfigure2_SIfigure17.R
|
2 |
+
# In:
|
3 |
+
# - /Data/gtrends_data.RData: DMA-level data on google searches for "coronavirus" linked with DMA-level cases and deaths
|
4 |
+
# - /Data/mobility_data.RData: county-level data on physical mobility linked with county-level cases and deaths
|
5 |
+
# - /Data/pew/biden_sanders_ideo_feb_march_PEW.csv: Pew survey data on public's placement of Sanders and Biden on an ideological scale
|
6 |
+
# Out:
|
7 |
+
# - /Figures/figure7.pdf
|
8 |
+
# - /Figures/SI_figure2.pdf
|
9 |
+
# - /Figures/SI_figure17.pdf
|
10 |
+
|
11 |
+
require(lme4)
|
12 |
+
require(lfe)
|
13 |
+
require(MatchIt)
|
14 |
+
require(WeightIt)
|
15 |
+
require(tjbal)
|
16 |
+
require(optmatch)
|
17 |
+
require(stargazer)
|
18 |
+
require(cobalt)
|
19 |
+
require(tidyverse)
|
20 |
+
require(ggridges)
|
21 |
+
|
22 |
+
rm(list = ls())
|
23 |
+
gc()
|
24 |
+
|
25 |
+
# Figure 7
|
26 |
+
load('../Data/gtrends_data.RData')
|
27 |
+
coefs <- NULL
|
28 |
+
for(day in as.character(unique(gtrends_cases$date))[5:95]) {
|
29 |
+
tmp <- summary(lmer(hits ~ log(cases+1) + (1|stab),gtrends_cases %>% filter(date == as.Date(day))))$coefficients
|
30 |
+
if(nrow(tmp) == 1) { next }
|
31 |
+
txttmp <- unique(trimws(gsub("coronavirus|the","",queries[[day]]$value)))
|
32 |
+
if(any(txttmp == "")) {
|
33 |
+
txttmp <- txttmp[-which(txttmp == "")]
|
34 |
+
}
|
35 |
+
txttmp <- txttmp[1:3]
|
36 |
+
if(any(txttmp == "DGSGsymptoms")) {
|
37 |
+
txttmp <- ifelse(txttmp == "symptoms",'"symptoms"',paste0('phantom("',txttmp,'")'))
|
38 |
+
txttmp <- paste(txttmp,collapse = " ")
|
39 |
+
txttmp <- gsub(' "symptoms"',' * "symptoms"',txttmp)
|
40 |
+
txttmp <- gsub('"symptoms" ','"symptoms" * ',txttmp)
|
41 |
+
txttmp <- gsub('"\\) phantom\\("', ' ',txttmp)
|
42 |
+
} else {
|
43 |
+
txttmp <- paste(txttmp,collapse = " ")
|
44 |
+
}
|
45 |
+
tq <- txttmp
|
46 |
+
|
47 |
+
coefs <- bind_rows(coefs,data.frame(t(tmp[2,]),date = as.Date(day),topQ = tq,stringsAsFactors = F))
|
48 |
+
}
|
49 |
+
|
50 |
+
pdf("../figures/figure7.pdf",width = 7,height = 5)
|
51 |
+
coefs %>%
|
52 |
+
ggplot(aes(x = date,y = Estimate)) +
|
53 |
+
geom_point() +
|
54 |
+
geom_errorbar(aes(ymin = Estimate - 2*Std..Error,ymax = Estimate + 2*Std..Error)) +
|
55 |
+
theme_bw() +
|
56 |
+
geom_hline(yintercept = 0) +
|
57 |
+
theme_bw() +
|
58 |
+
geom_vline(xintercept = as.Date(c("2020-02-03","2020-02-11","2020-02-22","2020-02-29",
|
59 |
+
"2020-03-03","2020-03-10","2020-03-17")),linetype = "dashed",alpha = .3) +
|
60 |
+
annotate(geom = "text",label = c("IA","NH","NV","SC","SupTues","Mar 10th","AZ, FL, IL"),
|
61 |
+
x = as.Date(c("2020-02-03","2020-02-11","2020-02-22","2020-02-29",
|
62 |
+
"2020-03-03","2020-03-10","2020-03-17")),y = -Inf,angle = 90,vjust = 1,hjust = 0) +
|
63 |
+
theme_ridges() +
|
64 |
+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
|
65 |
+
panel.background = element_blank(), axis.line = element_line(colour = "black")) + xlab("Date") + ylab("Coefficient on Exposure") +
|
66 |
+
annotate(geom = "text",label = coefs$topQ,y = coefs$Estimate + 2*coefs$Std..Error + 1,x = coefs$date,angle = 90,
|
67 |
+
parse = F,hjust = 0,vjust = .3,size = 2,color = "grey30") +
|
68 |
+
ylim(c(-10,45))
|
69 |
+
dev.off()
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
# SI Figure 2
|
74 |
+
load('../Data/mobility_data.RData')
|
75 |
+
|
76 |
+
dma_coefs <- cty_coefs <- NULL
|
77 |
+
for(week in as.character(unique(mobility_cases$weeks))[-13]) {
|
78 |
+
tmp <- summary(felm(raw ~ log(cases+1) | stab | 0 | stab,mobility_cases %>% filter(weeks == as.Date(week))))$coefficients[1,1:3]
|
79 |
+
names(tmp) <- c("Estimate","Std..Error","tstat")
|
80 |
+
cty_coefs <- bind_rows(cty_coefs,
|
81 |
+
data.frame(t(tmp),date = as.Date(week),stringsAsFactors = F))
|
82 |
+
}
|
83 |
+
|
84 |
+
pdf("../figures/SI_figure2.pdf",width= 7,height = 5)
|
85 |
+
cty_coefs %>%
|
86 |
+
ggplot(aes(x = date,y = Estimate)) +
|
87 |
+
geom_point(position = position_dodge(width = 4)) +
|
88 |
+
geom_errorbar(aes(ymin = Estimate - 2*Std..Error,
|
89 |
+
ymax = Estimate + 2*Std..Error),width = 1) +
|
90 |
+
geom_hline(yintercept = 0) +
|
91 |
+
theme_bw() +
|
92 |
+
geom_vline(xintercept = as.Date(c("2020-02-03","2020-02-11","2020-02-22","2020-02-29",
|
93 |
+
"2020-03-03","2020-03-10","2020-03-17")),linetype = "dashed",alpha = .5) +
|
94 |
+
annotate(geom = "text",label = c("IA","NH","NV","SC","SupTues","Mar 10th","AZ, FL, IL"),
|
95 |
+
x = as.Date(c("2020-02-03","2020-02-11","2020-02-22","2020-02-29",
|
96 |
+
"2020-03-03","2020-03-10","2020-03-17")),y = Inf,angle = 90,vjust = 1,hjust = 1) +
|
97 |
+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
|
98 |
+
panel.background = element_blank(), axis.line = element_line(colour = "black")) + xlab("Date") + ylab("Coefficient on Exposure") +
|
99 |
+
theme_ridges()
|
100 |
+
dev.off()
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
|
105 |
+
|
106 |
+
|
107 |
+
|
108 |
+
# SI Figure 17
|
109 |
+
toplot <- read.csv('../Data/pew/biden_sanders_ideo_feb_march_PEW.csv',stringsAsFactors = F)
|
110 |
+
|
111 |
+
pdf('../figures/SI_figure17.pdf',width = 8,height = 4)
|
112 |
+
toplot %>%
|
113 |
+
mutate_at(vars(matches('\\.|Refused|Moderate')),function(x) as.numeric(gsub('%','',x))) %>%
|
114 |
+
gather(key,value,-Group,-Candidate) %>%
|
115 |
+
filter(!grepl('Refused|Not\\.sure',key)) %>%
|
116 |
+
mutate(key = factor(gsub('\\.','\n',key),levels = c('Very\nliberal','Mostly\nliberal','Slightly\nliberal',
|
117 |
+
'Moderate','Slightly\nconservative','Mostly\nconservative','Very\nconservative'))) %>%
|
118 |
+
filter(grepl('liberal dem',tolower(Group))) %>%
|
119 |
+
ggplot(aes(x = key,y = value,group = Candidate,fill = Candidate)) +
|
120 |
+
geom_bar(stat = 'identity',position= position_dodge(.8),alpha = .6) +
|
121 |
+
ylab('% Agreeing') + xlab('Candidate Ideology') +
|
122 |
+
scale_fill_manual(name = '',values = c('Biden' = 'grey40','Sanders' = 'grey70')) +
|
123 |
+
theme_ridges() +
|
124 |
+
theme(legend.position = 'bottom')
|
125 |
+
dev.off()
|
29/replication_package/Code/Figure 8, SI19.R
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# File name: figure8_SIfigure19.R
|
2 |
+
# In:
|
3 |
+
# - /Data/france_data.RData: Data on 2020 municipal elections in France, linked with local-level cases and deaths
|
4 |
+
# Out:
|
5 |
+
# - /Figures/figure8.pdf
|
6 |
+
# - /Figures/SI_figure19.pdf
|
7 |
+
|
8 |
+
|
9 |
+
require(lfe)
|
10 |
+
require(ggridges)
|
11 |
+
require(tidyverse)
|
12 |
+
require(gridExtra)
|
13 |
+
rm(list =ls())
|
14 |
+
gc()
|
15 |
+
####################################################################################################################### Loading functions
|
16 |
+
source('./helper_functions.R')
|
17 |
+
|
18 |
+
|
19 |
+
load('../Data/france_data.RData')
|
20 |
+
|
21 |
+
# Figure 8
|
22 |
+
p1 <- toanal %>%
|
23 |
+
mutate(mainstream = ifelse(mainstream == 0,'Non-Mainstream','Mainstream'),
|
24 |
+
period = factor(ifelse(date == as.Date('2020-03-15'),'March Elections','June Elections'),
|
25 |
+
levels = c('March Elections','June Elections'))) %>%
|
26 |
+
group_by(mainstream,period) %>%
|
27 |
+
summarise(votes = mean(votes)) %>%
|
28 |
+
ungroup() %>%
|
29 |
+
filter(complete.cases(.)) %>%
|
30 |
+
ggplot(aes(x = period,y = votes,fill = factor(mainstream))) +
|
31 |
+
stat_summary(fun.y = mean,geom = "bar",position = position_dodge(width = .9),
|
32 |
+
size = 3,alpha = .7) +
|
33 |
+
scale_fill_manual(name = '',values = c('Mainstream' = 'grey80','Non-Mainstream' = 'grey30')) +
|
34 |
+
theme_ridges() + xlab('Period') + ylab('Aggregate Vote Share')
|
35 |
+
|
36 |
+
p2 <- toanal %>%
|
37 |
+
mutate(mainstream = ifelse(mainstream == 0,'Non-Mainstream','Mainstream'),
|
38 |
+
period = factor(ifelse(date == as.Date('2020-03-15'),'March Elections','June Elections'),
|
39 |
+
levels = c('March Elections','June Elections')),
|
40 |
+
logcases = log(deaths+1)) %>%
|
41 |
+
filter(!is.na(period)) %>%
|
42 |
+
ggplot(aes(x = logcases,y = votes,color = mainstream)) +
|
43 |
+
geom_point(alpha = .7,size = 2) +
|
44 |
+
geom_smooth(method = 'lm') +
|
45 |
+
scale_color_manual(name = "",values = c('Mainstream' = 'grey80',"Non-Mainstream" = "grey30")) +
|
46 |
+
theme_ridges() + xlab("Deaths (logged)") + ylab('Aggregate Vote Share')
|
47 |
+
|
48 |
+
pdf('../figures/figure8.pdf',width = 7,height = 4)
|
49 |
+
grid.arrange(p1 + theme(legend.position = 'bottom'),
|
50 |
+
p2 + theme(legend.position = 'bottom'),ncol = 2)
|
51 |
+
dev.off()
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
# SI Figure 19
|
56 |
+
toanal$post <- ifelse(toanal$date == '2020-03-15',0,1)
|
57 |
+
|
58 |
+
toanal$antiEst <- (toanal$mainstream - 1)^2
|
59 |
+
summary(mod1 <- felm(votes ~ antiEst*post | dpt_code | 0 | dpt_code,toanal))
|
60 |
+
summary(mod2 <- felm(votes ~ antiEst*log(deaths+1) | post + dpt_code | 0 | dpt_code,toanal))
|
61 |
+
|
62 |
+
toplot <- data.frame(interaction_plot_continuous(mod1,plot = T,pointsplot = T,colr = 'black',alph = 200,num_points = 2,
|
63 |
+
xlabel = "Election Wave",ylabel = 'Marginal Coefficient of Mainstream')) %>%
|
64 |
+
mutate(type = 'Temporal')
|
65 |
+
|
66 |
+
toplot <- bind_rows(toplot,data.frame(interaction_plot_continuous(mod2,plot = T,pointsplot = T,
|
67 |
+
colr = 'black',alph = 200,num_points = 20,
|
68 |
+
xlabel = "Election Wave",ylabel = 'Marginal Coefficient of Mainstream')) %>%
|
69 |
+
mutate(type = 'Geographic'))
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
p1 <- toplot %>%
|
74 |
+
filter(type == 'Temporal') %>%
|
75 |
+
mutate(x = factor(ifelse(x_2 == 0,'March Elections','June Elections'),levels = c('March Elections','June Elections'))) %>%
|
76 |
+
ggplot(aes(x = x,y = delta_1)) +
|
77 |
+
geom_point() +
|
78 |
+
geom_errorbar(aes(ymin = lb,ymax = ub),width = .1) +
|
79 |
+
geom_hline(yintercept = 0,linetype = 'dashed') +
|
80 |
+
ylab('Margnial Coefficient on Non-Mainstream') + xlab('') +
|
81 |
+
theme_ridges() +
|
82 |
+
ggtitle('Temporal Variation')
|
83 |
+
p2 <- toplot %>%
|
84 |
+
filter(type == 'Geographic') %>%
|
85 |
+
ggplot(aes(x = x_2,y = delta_1)) +
|
86 |
+
geom_point() +
|
87 |
+
geom_errorbar(aes(ymin = lb,ymax = ub),width = .1) +
|
88 |
+
geom_hline(yintercept = 0,linetype = 'dashed') +
|
89 |
+
ylab('Margnial Coefficient on Non-Mainstream') + xlab('Logged Deaths') +
|
90 |
+
theme_ridges() +
|
91 |
+
ggtitle('Geographic Variation')
|
92 |
+
|
93 |
+
pdf('../Figures/SI_figure19.pdf',width = 8,height = 4)
|
94 |
+
grid.arrange(p1,p2,ncol = 2)
|
95 |
+
dev.off()
|
29/replication_package/Code/Figure SI11, SI12, SI15, SI16.R
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# File name: SIfigure11_SIfigure12_SIfigure15_SIfigure16.R
|
2 |
+
# In:
|
3 |
+
# - nationscape_prepped.RData: Nationscape survey data merged with NYT covid data, aggregated to the congressional district level
|
4 |
+
# Out:
|
5 |
+
# - /Figures/SI_figure11.pdf
|
6 |
+
# - /Figures/SI_figure12.pdf
|
7 |
+
# - /Figures/SI_figure15.pdf
|
8 |
+
# - /Figures/SI_figure16.pdf
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
rm(list = ls())
|
13 |
+
gc()
|
14 |
+
require(tidyverse)
|
15 |
+
require(stargazer)
|
16 |
+
require(lubridate)
|
17 |
+
require(ggridges)
|
18 |
+
require(lfe)
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
####################################################################################################################### Loading data
|
23 |
+
load('../Data/nationscape_data.RData')
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
####################################################################################################################### Loading functions
|
29 |
+
source('./helper_functions.R')
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
# SI Figure 11
|
34 |
+
pdf('../Figures/SI_figure11.pdf',width = 7,height = 5)
|
35 |
+
forAnal %>%
|
36 |
+
filter(dateMo < as.Date('2020-07-01') & dateMo > as.Date('2019-07-01')) %>%
|
37 |
+
group_by(stcd,dateMo,exposedMean) %>%
|
38 |
+
summarise(ideoM = mean(economy_better,na.rm=T)) %>%
|
39 |
+
ggplot(aes(x = ideoM,y = fct_rev(factor(dateMo)),group = paste(dateMo,exposedMean),
|
40 |
+
fill = paste(dateMo,exposedMean))) +
|
41 |
+
geom_density_ridges(alpha = .6,color = 'white',quantile_lines = T,quantiles = 2) +
|
42 |
+
scale_fill_cyclical(
|
43 |
+
breaks = c("2019-08-01 0", "2019-08-01 1"),
|
44 |
+
labels = c(`2019-08-01 0` = "Insulated", `2019-08-01 1` = "Exposed"),
|
45 |
+
values = rev(c("#000000", "#b0b0b0", "#000000", "#b0b0b0")),
|
46 |
+
name = "", guide = "legend"
|
47 |
+
) +
|
48 |
+
theme_ridges() +
|
49 |
+
xlab("Economic Evaluation") +
|
50 |
+
ylab("") +
|
51 |
+
scale_x_continuous(limits = c(1,3),breaks = 1:3,labels = c('Improved','The Same','Worse'))
|
52 |
+
dev.off()
|
53 |
+
|
54 |
+
# SI Figure 12
|
55 |
+
forAnal %>%
|
56 |
+
filter(dateMo < as.Date('2020-07-01') & dateMo > as.Date('2019-07-01')) %>%
|
57 |
+
group_by(stcd,dateWk,dateMo,exposedMean) %>%
|
58 |
+
summarise(econ = mean(economy_better,na.rm=T)) %>%
|
59 |
+
mutate(post = ifelse(dateMo > as.Date('2020-02-01'),1,0)) -> forDID
|
60 |
+
|
61 |
+
summary(mEcon <- felm(scale(econ) ~ exposedMean*post | 0 | 0 | stcd,forDID %>% filter(dateMo < as.Date('2020-04-01'))))
|
62 |
+
toplot <- data.frame(interaction_plot_continuous(mEcon,pointsplot = T,num_points = 2,plot = T))
|
63 |
+
|
64 |
+
pdf('../Figures/SI_figure12.pdf',width = 7,height = 4)
|
65 |
+
toplot %>%
|
66 |
+
mutate(x_2 = factor(ifelse(x_2 == 0,'Pre-March','March'),levels = c('Pre-March','March'))) %>%
|
67 |
+
ggplot(aes(x = x_2,y = delta_1)) +
|
68 |
+
geom_point() +
|
69 |
+
geom_errorbar(aes(ymin = lb,ymax = ub),width = .1) +
|
70 |
+
geom_hline(yintercept = 0,linetype = 'dashed') +
|
71 |
+
theme_ridges() +
|
72 |
+
xlab('Period') + ylab('Effect of Covid Exposure')
|
73 |
+
dev.off()
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
# SI Figure 15
|
78 |
+
(pSanders <- forAnal %>%
|
79 |
+
mutate(sanders = (cand_favorability_sanders - 5)*-1) %>% #ifelse(cand_favorability_sanders < 3,1,0)) %>%
|
80 |
+
filter(dateMo < as.Date('2020-05-01') & dateMo > as.Date('2019-07-01')) %>%
|
81 |
+
group_by(stcd,dateMo,exposedMean) %>%
|
82 |
+
summarise(ideoM = mean(sanders,na.rm=T)) %>%
|
83 |
+
ggplot(aes(x = ideoM,y = fct_rev(factor(dateMo)),group = paste(dateMo,exposedMean),
|
84 |
+
fill = paste(dateMo,exposedMean))) +
|
85 |
+
geom_density_ridges(alpha = .6,color = 'white',quantile_lines = T,quantiles = 2) +
|
86 |
+
scale_fill_cyclical(
|
87 |
+
breaks = c("2019-08-01 0", "2019-08-01 1"),
|
88 |
+
labels = c(`2019-08-01 0` = "Insulated", `2019-08-01 1` = "Exposed"),
|
89 |
+
values = rev(c("#000000", "#b0b0b0", "#000000", "#b0b0b0")),
|
90 |
+
name = "", guide = "legend"
|
91 |
+
) +
|
92 |
+
theme_ridges() +
|
93 |
+
xlab("Sanders Approval") +
|
94 |
+
ylab("") +
|
95 |
+
scale_x_continuous(breaks = 1:4,labels = c('Very\nunfavorable','Somewhat\nunfavorable','Somewhat\nfavorable','Very\nfavorable')))
|
96 |
+
|
97 |
+
pdf('../Figures/SI_figure15.pdf',width = 7,height = 5)
|
98 |
+
print(pSanders)
|
99 |
+
dev.off()
|
100 |
+
|
101 |
+
# SI Figure 16
|
102 |
+
pdf('../Figures/SI_figure16.pdf',width = 8,height = 5)
|
103 |
+
forAnal %>%
|
104 |
+
mutate(sanders = (cand_favorability_sanders - 5)*-1) %>% #ifelse(cand_favorability_sanders < 3,1,0)) %>%
|
105 |
+
filter(dateWk %in% as.Date(c('2020-02-02','2020-02-09','2020-02-16','2020-02-23','2020-03-01','2020-03-08','2020-03-15','2020-03-22','2020-03-29'))) %>%
|
106 |
+
group_by(stcd,dateWk) %>%
|
107 |
+
summarise(sanders = mean(sanders,na.rm=T),
|
108 |
+
cases = log(mean(casesSum,na.rm=T)+1)) %>%
|
109 |
+
ggplot(aes(x = cases,y = sanders)) +
|
110 |
+
geom_point() +
|
111 |
+
geom_smooth(method = 'lm') +
|
112 |
+
facet_wrap(~dateWk) +
|
113 |
+
theme_ridges() +
|
114 |
+
xlab('Logged Cases') + ylab('Sanders Approval')
|
115 |
+
dev.off()
|
29/replication_package/Code/Figure SI3, SI4, SI5.R
ADDED
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# File name: SIfigure3_SIfigure4_SIfigure_5.R
|
2 |
+
# In:
|
3 |
+
# - replication_data.RData
|
4 |
+
# -
|
5 |
+
# Out:
|
6 |
+
# - /Figures/SI_figure3.pdf
|
7 |
+
# - /Figures/SI_figure4.pdf
|
8 |
+
# - /Figures/SI_figure5.pdf
|
9 |
+
|
10 |
+
require(lme4)
|
11 |
+
require(lfe)
|
12 |
+
require(MatchIt)
|
13 |
+
require(WeightIt)
|
14 |
+
require(tjbal)
|
15 |
+
require(optmatch)
|
16 |
+
require(stargazer)
|
17 |
+
require(cobalt)
|
18 |
+
require(tidyverse)
|
19 |
+
require(gridExtra)
|
20 |
+
require(ggridges)
|
21 |
+
require(ggrepel)
|
22 |
+
|
23 |
+
rm(list = ls())
|
24 |
+
gc()
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
####################################################################################################################### Loading data
|
30 |
+
load(paste0('../Data/Results/SI-data.RData')) ## SI_robust_prep.R script which is designed to run on a computing cluster for speed.
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
####################################################################################################################### Loading functions
|
37 |
+
source('./helper_functions.R')
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
# SI Figure 3
|
43 |
+
toplot <- NULL
|
44 |
+
for(post in c('2020-03-03','2020-03-10','2020-03-17')) {
|
45 |
+
for(mod in c('basic','matching','weighting')) {
|
46 |
+
toplot <- bind_rows(toplot,data.frame(est = resSI$pcttw_sanders$DMA_$March17Cases$DMA_CODE$`2020-03-01`[[post]]$felm[[mod]]$plac$did,
|
47 |
+
post = factor(ifelse(post == '2020-03-03','Control: February\nTreated: March 3rd',
|
48 |
+
ifelse(post == '2020-03-10','Control: February\nTreated: March 10th',
|
49 |
+
'Control: February\nTreated: March 17th')),
|
50 |
+
levels = c('Control: February\nTreated: March 3rd',
|
51 |
+
'Control: February\nTreated: March 10th',
|
52 |
+
'Control: February\nTreated: March 17th')),
|
53 |
+
model = Hmisc::capitalize(mod),stringsAsFactors = F))
|
54 |
+
}
|
55 |
+
}
|
56 |
+
|
57 |
+
|
58 |
+
pdf('../Figures/SI_figure3.pdf',width = 8,height = 5)
|
59 |
+
toplot %>%
|
60 |
+
ggplot(aes(x = est,y = factor(model,levels = rev(c('Basic','Matching','Weighting'))))) +
|
61 |
+
geom_vline(xintercept = 0,linetype = 'dashed') +
|
62 |
+
geom_density_ridges(alpha = .4,color = 'white') +
|
63 |
+
facet_wrap(~post) + ylab('Method') + xlab("Bootstrapped Estimates") +
|
64 |
+
theme_ridges() +
|
65 |
+
theme(
|
66 |
+
strip.text.x = element_text(
|
67 |
+
size = 10, color = "black", face = "bold",hjust = 0,vjust = .5
|
68 |
+
),
|
69 |
+
strip.background = element_rect(
|
70 |
+
color="white", fill="white", size=0, linetype="solid"
|
71 |
+
)
|
72 |
+
)
|
73 |
+
dev.off()
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
# SI Figure 4
|
79 |
+
toplot <- NULL
|
80 |
+
for(pre in c('2020-03-01','2020-03-03','2020-03-10','2020-03-17')) {
|
81 |
+
for(post in c('2020-03-01','2020-03-03','2020-03-10','2020-03-17')) {
|
82 |
+
for(mod in c('basic','weighting','matching')) {
|
83 |
+
tmp <- resSI$pcttw_sanders$DMA_$March10Cases$DMA_CODE[[pre]][[post]]$felm[[mod]]$bin['treat',]
|
84 |
+
if(is.null(tmp)) { next }
|
85 |
+
tmp <- data.frame(t(tmp))
|
86 |
+
colnames(tmp) <- c('est','se','tstat','pval')
|
87 |
+
tmp$pre <- pre
|
88 |
+
tmp$post <- post
|
89 |
+
tmp$mod <- mod
|
90 |
+
toplot <- bind_rows(toplot,tmp)
|
91 |
+
}
|
92 |
+
}
|
93 |
+
}
|
94 |
+
|
95 |
+
|
96 |
+
pdf('../Figures/SI_figure4.pdf',width = 8,height = 5)
|
97 |
+
toplot %>%
|
98 |
+
mutate(post = factor(gsub('03-03','ST',
|
99 |
+
gsub('03-01','Feb',
|
100 |
+
gsub('2020-','',post))),levels = c('Feb','ST','03-10','03-17')),
|
101 |
+
pre = factor(paste0('Pre Election: ',gsub('03-03','Super Tuesday',
|
102 |
+
gsub('03-01','February',
|
103 |
+
gsub('2020-','',pre)))),
|
104 |
+
levels = paste0('Pre Election: ',c('February','Super Tuesday','03-10','03-17')))) %>%
|
105 |
+
ggplot(aes(x = post,y = est,shape = mod)) +
|
106 |
+
geom_point(position = position_dodge(width = .2),size = 2,alpha = .7) +
|
107 |
+
geom_errorbar(aes(ymin = est-2*se,ymax = est+2*se),width = .2,
|
108 |
+
position = position_dodge(width = .2),alpha = .4) +
|
109 |
+
geom_hline(yintercept = 0,linetype = 'dashed') +
|
110 |
+
facet_wrap(~pre) +
|
111 |
+
scale_shape_manual(name = 'Model',
|
112 |
+
values = c('basic' = 19,'matching' = 17,'weighting' = 15),
|
113 |
+
labels = c('Basic','Matching','Weighting')) +
|
114 |
+
theme_ridges() +
|
115 |
+
theme(legend.position = 'bottom') +
|
116 |
+
xlab('Post Election') + ylab('Effect of DMA Exposure')
|
117 |
+
dev.off()
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
# SI Figure 5
|
123 |
+
toplot <- NULL
|
124 |
+
for(pre in c('2020-03-01','2020-03-03','2020-03-10','2020-03-17')) {
|
125 |
+
for(post in c('2020-03-01','2020-03-03','2020-03-10','2020-03-17')) {
|
126 |
+
for(mod in c('basic','weighting','matching')) {
|
127 |
+
tmp <- resSI$pcttw_sanders$DMA_$March17Cases$`0`[[pre]][[post]]$felm[[mod]]$did$mfx
|
128 |
+
if(is.null(tmp)) { next }
|
129 |
+
tmp <- data.frame(tmp)
|
130 |
+
tmp$pre <- pre
|
131 |
+
tmp$post <- post
|
132 |
+
tmp$mod <- mod
|
133 |
+
toplot <- bind_rows(toplot,tmp)
|
134 |
+
}
|
135 |
+
}
|
136 |
+
}
|
137 |
+
|
138 |
+
|
139 |
+
pdf('../Figures/SI_figure5.pdf',width = 8,height = 5)
|
140 |
+
toplot %>%
|
141 |
+
filter(pre < post,
|
142 |
+
mod == 'weighting') %>%
|
143 |
+
mutate(x_2 = ifelse(x_2 == 0,gsub('2020-','',pre),gsub('2020-','',post)),
|
144 |
+
sig = ifelse(grepl('\\*',est),'Sig','Insig')) %>%
|
145 |
+
mutate(x_2 = factor(ifelse(x_2 == '03-01','Feb',
|
146 |
+
ifelse(x_2 == '03-03','ST',x_2)),
|
147 |
+
levels = c('Feb','ST','03-10','03-17'))) %>%
|
148 |
+
mutate(pre = factor(paste0('Pre: ',ifelse(pre == '2020-03-01','February',
|
149 |
+
ifelse(pre == '2020-03-03','Super Tuesday',pre))),
|
150 |
+
levels = paste0('Pre: ',c('February','Super Tuesday','2020-03-10','2020-03-17'))),
|
151 |
+
post = factor(paste0('Post: ',ifelse(post == '2020-03-01','February',
|
152 |
+
ifelse(post == '2020-03-03','Super Tuesday',post))),
|
153 |
+
levels = paste0('Post: ',c('February','Super Tuesday','2020-03-10','2020-03-17')))) %>%
|
154 |
+
ggplot(aes(x = x_2,y = delta_1,color = sig)) +
|
155 |
+
geom_point(position = position_dodge(width = .2),size = 2) +
|
156 |
+
geom_errorbar(aes(ymin = lb,ymax = ub),width = .1,size = 1,
|
157 |
+
position = position_dodge(width = .2)) +
|
158 |
+
geom_hline(yintercept = 0,linetype = 'dashed') +
|
159 |
+
facet_wrap(~pre+post,scales = 'free',ncol = 3) +
|
160 |
+
scale_color_manual(name = 'Sig.',values = c('Sig' = 'black','Insig' = 'grey50')) +
|
161 |
+
scale_linetype_manual(name = 'Model',
|
162 |
+
values = c('basic' = 'dotdash','matching' = 'dashed','weighting' = 'solid')) +
|
163 |
+
scale_shape_manual(name = 'Model',
|
164 |
+
values = c('basic' = 19,'matching' = 17,'weighting' = 15)) +
|
165 |
+
theme_ridges() + xlab('Comparison Periods') + ylab('Marginal Effect of Exposure') +
|
166 |
+
theme(legend.position = 'bottom')
|
167 |
+
dev.off()
|
29/replication_package/Code/Figure SI6, SI7, SI8, SI9, SI10.R
ADDED
@@ -0,0 +1,348 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# File name: SIfigure6_SIfigure7_SIfigure8_SIfigure9_SIfigure10.R
|
2 |
+
# In:
|
3 |
+
# - replication_data.RData
|
4 |
+
# Out:
|
5 |
+
# - /Figures/SI_figure6.pdf
|
6 |
+
# - /Figures/SI_figure7.pdf
|
7 |
+
# - /Figures/SI_figure8.pdf
|
8 |
+
# - /Figures/SI_figure9.pdf
|
9 |
+
# - /Figures/SI_figure10.pdf
|
10 |
+
|
11 |
+
require(lme4)
|
12 |
+
require(lfe)
|
13 |
+
require(MatchIt)
|
14 |
+
require(WeightIt)
|
15 |
+
require(tjbal)
|
16 |
+
require(optmatch)
|
17 |
+
require(stargazer)
|
18 |
+
require(cobalt)
|
19 |
+
require(tidyverse)
|
20 |
+
require(gridExtra)
|
21 |
+
require(ggridges)
|
22 |
+
require(ggrepel)
|
23 |
+
|
24 |
+
rm(list = ls())
|
25 |
+
gc()
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
####################################################################################################################### Loading data
|
31 |
+
load('../Data/replication_data.RData')
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
####################################################################################################################### Loading functions
|
37 |
+
source('./helper_functions.R')
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
####################################################################################################################### Preparing variables
|
46 |
+
Y <- paste0(c("pct_","VAP_","pcttw_"),"sanders")
|
47 |
+
D <- unlist(lapply(c("sc_","ln_"),function(x) paste0(x,paste0(c("county_","DMA_","state_"),"cases"))))
|
48 |
+
FE <- c("0","DMA_CODE","date")
|
49 |
+
covariates <- c("sc_CTY_LTHS","sc_CTY_CollUp",'caucus_switch','caucus',
|
50 |
+
"sc_CTY_LT30yo","sc_CTY_60Up",
|
51 |
+
"sc_CTY_Below_poverty_level_AGE_18_64","sc_CTY_Female_hher_no_husbandhh",
|
52 |
+
"sc_CTY_Unem_rate_pop_16_over","sc_CTY_Labor_Force_Part_Rate_pop_16_over",
|
53 |
+
"sc_CTY_Manufactur","sc_CTY_Md_inc_hhs",
|
54 |
+
"sc_CTY_POPPCT_RURAL","sc_CTY_Speak_only_English","sc_CTY_White","sc_CTY_Black_or_African_American",
|
55 |
+
"ln_CTY_tot_pop","sc_turnout_pct_20")
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
####################################################################################################################### Looping across specifications
|
63 |
+
finalDat$sc_turnout_pct_chg <- scale(finalDat$turnout_pct_chg)
|
64 |
+
finalDat$sc_VAP_turnout_20 <- scale(finalDat$VAP_turnout_20)
|
65 |
+
|
66 |
+
Y <- c("sc_turnout_pct_20","sc_turnout_pct_chg","sc_VAP_turnout_20")
|
67 |
+
resTurnout <- list()
|
68 |
+
for(y in Y) {
|
69 |
+
for(d in D) {
|
70 |
+
for(fe in FE) {
|
71 |
+
for(m in c("sc_CTY_LT30yo","sc_CTY_60Up","sc_CTY_Unem_rate_pop_16_over","sc_CTY_Old_age_dep_ratio","sc_CTY_POPPCT_RURAL","sc_CTY_Manufactur","sc_CTY_LTHS","sc_CTY_CollUp")) {
|
72 |
+
resTurnout[[y]][[d]][[fe]][[m]]$felm$coefs <- summary(tmp <- felm(as.formula(paste0(y," ~ ",d,"*",m," + ",
|
73 |
+
paste(c(covariates)[-which(c(covariates) %in% c("sc_CTY_LT30yo","sc_CTY_60Up","sc_turnout_pct_20"))],collapse = "+")," | ",fe," | 0 | ",fe)),finalDat))$coefficients
|
74 |
+
resTurnout[[y]][[d]][[fe]][[m]]$felm$mfx <- interaction_plot_continuous(tmp)
|
75 |
+
if(fe != "0") {
|
76 |
+
resTurnout[[y]][[d]][[fe]][[m]]$lmer$coefs <- summary(tmp <- lmer(as.formula(paste0(y," ~ ",d,"*",m," + ",
|
77 |
+
paste(c(covariates)[-which(c(covariates) %in% c("sc_CTY_LT30yo","sc_CTY_60Up","sc_turnout_pct_20"))],collapse = "+")," + (1| ",fe,")")),finalDat))$coefficients
|
78 |
+
resTurnout[[y]][[d]][[fe]][[m]]$lmer$mfx <- interaction_plot_continuous(tmp)
|
79 |
+
|
80 |
+
}
|
81 |
+
|
82 |
+
}
|
83 |
+
}
|
84 |
+
}
|
85 |
+
}
|
86 |
+
|
87 |
+
|
88 |
+
p1 <- data.frame(resTurnout$sc_turnout_pct_20$sc_DMA_cases$DMA_CODE$sc_CTY_60Up$felm$mfx) %>%
|
89 |
+
ggplot(aes(x = x_2,y = delta_1)) +
|
90 |
+
geom_point() +
|
91 |
+
geom_errorbar(aes(ymin = lb,ymax = ub),width = .1) +
|
92 |
+
geom_hline(yintercept = 0,linetype = 'dashed') +
|
93 |
+
xlab(expression(Younger %<->% Older)) +
|
94 |
+
ylab('MFX of Exposure on Turnout') +
|
95 |
+
theme_ridges() +
|
96 |
+
ggtitle(label = '',subtitle = '% of county 60 years or older')
|
97 |
+
|
98 |
+
p2 <- data.frame(resTurnout$sc_turnout_pct_20$sc_DMA_cases$DMA_CODE$sc_CTY_LT30yo$felm$mfx) %>%
|
99 |
+
ggplot(aes(x = x_2,y = delta_1)) +
|
100 |
+
geom_point() +
|
101 |
+
geom_errorbar(aes(ymin = lb,ymax = ub),width = .1) +
|
102 |
+
geom_hline(yintercept = 0,linetype = 'dashed') +
|
103 |
+
xlab(expression(Older %<->% Younger)) +
|
104 |
+
ylab('MFX of Exposure on Turnout') +
|
105 |
+
theme_ridges() +
|
106 |
+
ggtitle(label = '',subtitle = '% of county 30 years or younger')
|
107 |
+
|
108 |
+
|
109 |
+
# SI Figure 6: Turnout and age
|
110 |
+
pdf('../Figures/SI_figure6.pdf',width = 8,height = 5)
|
111 |
+
grid.arrange(p1,p2,ncol = 2)
|
112 |
+
dev.off()
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
# SI Figure 7
|
117 |
+
pdf('../Figures/SI_figure7.pdf',width = 8,height = 5)
|
118 |
+
finalDat %>%
|
119 |
+
group_by(date,stab) %>%
|
120 |
+
summarise(pct_sanders16 = mean(pct_sanders16,na.rm=T),
|
121 |
+
turnout_pct_20 = mean(turnout_pct_20*100,na.rm=T)) %>%
|
122 |
+
ggplot(aes(x = date,y = turnout_pct_20,size = pct_sanders16,label = stab)) +
|
123 |
+
geom_point(alpha = .3) +
|
124 |
+
geom_text_repel(size = 4) +
|
125 |
+
theme_ridges() +
|
126 |
+
xlab('Date') + ylab('Turnout 2020 (%)') +
|
127 |
+
scale_size(name = 'Sanders 2016 Vote Share (%)') +
|
128 |
+
theme(legend.position = 'bottom')
|
129 |
+
dev.off()
|
130 |
+
|
131 |
+
|
132 |
+
# SI Figure 8
|
133 |
+
p1 <- finalDat %>%
|
134 |
+
filter(date %in% as.Date(c('2020-03-03',
|
135 |
+
'2020-03-10',
|
136 |
+
'2020-03-17'))) %>%
|
137 |
+
mutate(turnout_pct_20 = turnout_pct_20*100) %>%
|
138 |
+
ggplot(aes(x = pct_sanders16,y = turnout_pct_20)) +
|
139 |
+
geom_point() +
|
140 |
+
geom_smooth() +
|
141 |
+
theme_ridges() +
|
142 |
+
xlab('Sanders 2016 Vote Share (%)') +
|
143 |
+
ylab('Turnout 2020 (%)') + ylim(c(0,50)) +
|
144 |
+
ggtitle(label = '',subtitle = 'March Data')
|
145 |
+
|
146 |
+
p2 <- finalDat %>%
|
147 |
+
filter(date < as.Date('2020-03-03')) %>%
|
148 |
+
mutate(turnout_pct_20 = turnout_pct_20*100) %>%
|
149 |
+
ggplot(aes(x = pct_sanders16,y = turnout_pct_20)) +
|
150 |
+
geom_point() +
|
151 |
+
geom_smooth() +
|
152 |
+
theme_ridges() +
|
153 |
+
xlab('Sanders 2016 Vote Share (%)') +
|
154 |
+
ylab('Turnout 2020 (%)') + ylim(c(0,50)) +
|
155 |
+
ggtitle(label = '',subtitle = 'Pre-March Data')
|
156 |
+
|
157 |
+
pdf('../Figures/SI_figure8.pdf',width = 9,height = 5)
|
158 |
+
grid.arrange(p2,p1,ncol = 2)
|
159 |
+
dev.off()
|
160 |
+
|
161 |
+
|
162 |
+
|
163 |
+
|
164 |
+
# SI Figure 9
|
165 |
+
finalDat$post <- ifelse(finalDat$date > as.Date('2020-02-29'),1,0)
|
166 |
+
toplot <- data.frame(summary(felm(as.formula(paste0("turnout_pct_20 ~ scale(pct_sanders16)*factor(date) + ",
|
167 |
+
paste(covariates[c(1:2,5:17)],collapse = '+'),'| 0 | 0 | 0')),finalDat))$coefficients)
|
168 |
+
colnames(toplot) <- c('est','se','tstat','pval')
|
169 |
+
toplot$vars <- rownames(toplot)
|
170 |
+
rownames(toplot) <- NULL
|
171 |
+
|
172 |
+
|
173 |
+
toplot %>%
|
174 |
+
filter(grepl(':factor',vars)) %>%
|
175 |
+
mutate(date = as.Date(gsub('scale.*?date\\)','',vars))) %>%
|
176 |
+
ggplot(aes(x = date,y = est)) +
|
177 |
+
geom_point() +
|
178 |
+
geom_errorbar(aes(ymin = est - 2*se,ymax = est+2*se)) +
|
179 |
+
geom_hline(yintercept = 0,linetype = 'dashed')
|
180 |
+
|
181 |
+
|
182 |
+
finalDat$dateTmp <- as.Date(ifelse(finalDat$Date %in% as.Date(c('2020-02-11','2020-02-22')),as.Date('2020-02-22'),finalDat$Date),origin = '1970-01-01')
|
183 |
+
toplot <- NULL
|
184 |
+
for(d in unique(finalDat$Date)) {
|
185 |
+
if(nrow(finalDat %>% filter(Date == d)) < 50) {
|
186 |
+
covs <- covariates[c(2,6,9,15)]
|
187 |
+
} else {
|
188 |
+
covs <- covariates[c(2,6,9,15)]
|
189 |
+
}
|
190 |
+
tmp <- data.frame(t(summary(felm(as.formula(paste0("scale(turnout_pct_20) ~ scale(pct_sanders16) + ",
|
191 |
+
paste(covs,collapse = '+'),'| 0 | 0 | 0')),finalDat %>%
|
192 |
+
filter(Date == d)))$coefficients[2,]))
|
193 |
+
colnames(tmp) <- c('est','se','tstat','pval')
|
194 |
+
tmp$date <- as.Date(d,origin = '1970-01-01')
|
195 |
+
toplot <- bind_rows(toplot,tmp)
|
196 |
+
}
|
197 |
+
|
198 |
+
|
199 |
+
labs <- finalDat %>%
|
200 |
+
select(stab,Date) %>% distinct()
|
201 |
+
toplot$lab <- NA
|
202 |
+
for(d in unique(toplot$date)) {
|
203 |
+
toplot$lab[which(toplot$date == d)] <- paste(unique(labs$stab[which(labs$Date == d)])[order(unique(labs$stab[which(labs$Date == d)]))],collapse = '\n')
|
204 |
+
}
|
205 |
+
|
206 |
+
|
207 |
+
pdf('../Figures/SI_figure9.pdf',width = 8,height = 5)
|
208 |
+
toplot %>%
|
209 |
+
filter(date < as.Date('2020-04-15')) %>%
|
210 |
+
ggplot(aes(x = date,y = est,label = lab,color = ifelse(lab == 'SC','SC','black'))) +
|
211 |
+
geom_point() +
|
212 |
+
geom_errorbar(aes(ymin = est - 2*se,ymax = est+2*se),width = .5) +
|
213 |
+
geom_hline(yintercept = 0,linetype = 'dashed') +
|
214 |
+
theme_ridges() +
|
215 |
+
xlab('Election Date') + ylab('Relationship between Sanders 2016 and Turnout') +
|
216 |
+
scale_color_manual(values = c('SC' = 'red','black' = 'black')) +
|
217 |
+
geom_text_repel(size = 3,hjust = 0,vjust = .5,alpha = .8) +
|
218 |
+
theme(legend.position = 'none')
|
219 |
+
dev.off()
|
220 |
+
|
221 |
+
|
222 |
+
|
223 |
+
|
224 |
+
|
225 |
+
|
226 |
+
|
227 |
+
|
228 |
+
|
229 |
+
|
230 |
+
|
231 |
+
|
232 |
+
|
233 |
+
|
234 |
+
# SI Figure 10
|
235 |
+
set.seed(123)
|
236 |
+
Y <- c('pct_sanders16')
|
237 |
+
|
238 |
+
D <- unlist(lapply(c("sc_","ln_"),function(x) paste0(x,paste0(c("county_","DMA_","state_"),"cases"))))
|
239 |
+
FE <- c("0","DMA_CODE","stab")
|
240 |
+
covariates <- c("sc_CTY_LTHS","sc_CTY_CollUp",'caucus_switch','caucus',
|
241 |
+
"sc_CTY_LT30yo","sc_CTY_60Up",
|
242 |
+
"sc_CTY_Below_poverty_level_AGE_18_64","sc_CTY_Female_hher_no_husbandhh",
|
243 |
+
"sc_CTY_Unem_rate_pop_16_over","sc_CTY_Labor_Force_Part_Rate_pop_16_over",
|
244 |
+
"sc_CTY_Manufactur","sc_CTY_Md_inc_hhs",
|
245 |
+
"sc_CTY_POPPCT_RURAL","sc_CTY_Speak_only_English",
|
246 |
+
"sc_CTY_White","sc_CTY_Black_or_African_American",
|
247 |
+
"ln_CTY_tot_pop","sc_turnout_pct_20")
|
248 |
+
|
249 |
+
|
250 |
+
resRegs <- resBal <- resTabs <- list()
|
251 |
+
for(y in Y) {
|
252 |
+
for(d in D[c(1:3)]) {
|
253 |
+
|
254 |
+
for(fe in FE) {
|
255 |
+
stars <- list()
|
256 |
+
for(dateThresh in c("2020-01-01","2020-03-01")) {
|
257 |
+
if(dateThresh == '2020-03-01') {
|
258 |
+
covs <- covariates[-which(covariates == 'caucus')]
|
259 |
+
} else {
|
260 |
+
covs <- covariates
|
261 |
+
}
|
262 |
+
if(!is.null(resRegs[[y]][[d]][[fe]][[dateThresh]]$basic$felm$cont)) { next }
|
263 |
+
finalDat$treatBin <- ifelse(finalDat[[gsub("sc_|ln_","",d)]] > 1,1,0)
|
264 |
+
foranal.weight <- finalDat %>% select(y,d,treatBin,gsub("sc_|ln_","",d),DMA_CODE,date,stab,covs) %>%
|
265 |
+
filter(complete.cases(.),date >= as.Date(dateThresh))
|
266 |
+
|
267 |
+
m.out <- matchit(formula = as.formula(paste("treatBin ~ ",paste(covs,collapse = " + "))),
|
268 |
+
data = foranal.weight,
|
269 |
+
method = "nearest",
|
270 |
+
distance = "mahalanobis")
|
271 |
+
|
272 |
+
m.data <- match.data(m.out)
|
273 |
+
W.out <- weightit(formula(paste0("treatBin ~ ",paste(covs,collapse = " + "))),
|
274 |
+
data = foranal.weight, estimand = "ATT", method = "cbps")
|
275 |
+
|
276 |
+
# Balance tables
|
277 |
+
balt.pre <- bal.tab(formula(paste0("treatBin ~ ",paste(covs,collapse = " + "))),
|
278 |
+
data = foranal.weight, estimand = "ATT",m.threshold = .05)
|
279 |
+
|
280 |
+
balt.post <- bal.tab(W.out, m.threshold = .05, disp.v.ratio = TRUE)
|
281 |
+
unm <- data.frame(Covs = rownames(balt.pre$Balance),
|
282 |
+
balt.pre$Balance %>% mutate(Diff_Unm = round(Diff.Un,2),
|
283 |
+
Bal_Test_Unm = M.Threshold.Un)) %>%
|
284 |
+
select(Covs,Diff_Unm,Bal_Test_Unm)
|
285 |
+
|
286 |
+
match <- data.frame(Covs = rownames(balt.post$Balance),
|
287 |
+
balt.post$Balance %>% mutate(Diff_Match = round(Diff.Adj,2),
|
288 |
+
Bal_Test_Match = M.Threshold)) %>%
|
289 |
+
select(Covs,Diff_Match,Bal_Test_Match)
|
290 |
+
|
291 |
+
|
292 |
+
# Basic
|
293 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$basic$felm$cont <- summary(stars$cont[[paste0(dateThresh,"_1")]] <- felm(as.formula(paste0("scale(",y,") ~ ",d," + ",paste(c(covs),collapse = " + ")," | ",fe," | 0 | ",fe)),finalDat %>% filter(date >= as.Date(dateThresh))))$coefficients
|
294 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$basic$felm$bin <- summary(stars$bin[[paste0(dateThresh,"_1")]] <- felm(as.formula(paste0("scale(",y,") ~ treatBin + ",paste(c(covs),collapse = " + ")," | ",fe," | 0 | ",fe)),finalDat %>% filter(date >= as.Date(dateThresh))))$coefficients
|
295 |
+
|
296 |
+
# Matching
|
297 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$matching$felm$cont <- summary(stars$cont[[paste0(dateThresh,"_2")]] <- felm(as.formula(paste0("scale(",y,") ~ ",d," + ",paste(c(covs),collapse = " + ")," | ",fe," | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients
|
298 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$matching$felm$bin <- summary(stars$bin[[paste0(dateThresh,"_2")]] <- felm(as.formula(paste0("scale(",y,") ~ treatBin + ",paste(c(covs),collapse = " + ")," | ",fe," | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients
|
299 |
+
|
300 |
+
# Weighting
|
301 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$weighting$felm$cont <- summary(stars$cont[[paste0(dateThresh,"_3")]] <- felm(as.formula(paste0("scale(",y,") ~ ",d," + ",paste(c(covs),collapse = " + ")," | ",fe," | 0 | ",fe)),foranal.weight,weights = W.out$weights))$coefficients
|
302 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$weighting$felm$bin <- summary(stars$bin[[paste0(dateThresh,"_3")]] <- felm(as.formula(paste0("scale(",y,") ~ treatBin + ",paste(c(covs),collapse = " + ")," | ",fe," | 0 | ",fe)),foranal.weight,weights = W.out$weights))$coefficients
|
303 |
+
|
304 |
+
if(fe != "0") {
|
305 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$basic$lmer <- summary(lmer(as.formula(paste0("scale(",y,") ~ ",d," + ",paste(c(covs),collapse = " + ")," + (1| ",fe,")")),finalDat %>% filter(date >= as.Date(dateThresh))))$coefficients
|
306 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$matching$lmer <- summary(lmer(as.formula(paste0("scale(",y,") ~ ",d," + ",paste(c(covs),collapse = " + ")," + (1| ",fe,")")),m.data,weights = m.data$weights))$coefficients
|
307 |
+
resRegs[[y]][[d]][[fe]][[dateThresh]]$weighting$lmer <- summary(lmer(as.formula(paste0("scale(",y,") ~ ",d," + ",paste(c(covs),collapse = " + ")," + (1| ",fe,")")),foranal.weight,weights = W.out$weights))$coefficients
|
308 |
+
}
|
309 |
+
}
|
310 |
+
}
|
311 |
+
cat(y,"\n")
|
312 |
+
}
|
313 |
+
}
|
314 |
+
|
315 |
+
|
316 |
+
toplot <- NULL
|
317 |
+
for(fe in names(resRegs$pct_sanders16$sc_DMA_cases)) {
|
318 |
+
for(mod in c('basic','matching','weighting')) {
|
319 |
+
for(meas in c('bin','cont')) {
|
320 |
+
tmp <- resRegs$pct_sanders16$sc_DMA_cases[[fe]]$`2020-01-01`[[mod]]$felm[[meas]][2,]
|
321 |
+
if(is.null(tmp)) { next }
|
322 |
+
tmp <- data.frame(t(tmp))
|
323 |
+
colnames(tmp) <- c('est','se','tstat','pval')
|
324 |
+
tmp$fe <- fe
|
325 |
+
tmp$mod <- mod
|
326 |
+
tmp$meas <- meas
|
327 |
+
toplot <- bind_rows(toplot,tmp)
|
328 |
+
}
|
329 |
+
}
|
330 |
+
}
|
331 |
+
|
332 |
+
|
333 |
+
pdf('../Figures/SI_figure10.pdf',width = 8,height = 5)
|
334 |
+
toplot %>%
|
335 |
+
filter(meas != 'cont') %>%
|
336 |
+
mutate(fe = factor(ifelse(fe == 0,'None',
|
337 |
+
ifelse(fe == 'DMA_CODE','DMA','State')),levels = c('None','DMA','State'))) %>%
|
338 |
+
ggplot(aes(x = fe,y = est,color = mod)) +
|
339 |
+
geom_point(position = position_dodge(width = .2)) +
|
340 |
+
geom_errorbar(aes(ymin = est - 2*se,ymax = est+2*se),width = .1,
|
341 |
+
position = position_dodge(width = .2)) +
|
342 |
+
geom_hline(yintercept = 0,linetype = 'dashed') +
|
343 |
+
scale_color_manual(name = 'Model',values = c('basic' = 'grey70',
|
344 |
+
'matching' = 'grey40',
|
345 |
+
'weighting' = 'black')) +
|
346 |
+
xlab('Fixed Effect') + ylab('Effect of Covid Exposure on Bernie 2016') +
|
347 |
+
theme_ridges()
|
348 |
+
dev.off()
|
29/replication_package/Code/LOG/log_Figure 2, 3, Table SI1.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ac9be1e8f990ed341bee6e7d18f48117865ad78bda8e09611524ee337a8c700d
|
3 |
+
size 17430
|
29/replication_package/Code/LOG/log_Figure 4.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0cc2dad199e651b3b18aeac4e0872d2227f1e3fd5fdcf0bbb5d97f3bf79f78b4
|
3 |
+
size 1906
|
29/replication_package/Code/LOG/log_Figure 5.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a0e002d85754f3ce56b44690a936549dac1aab32673faeab1d2b5117b07148c3
|
3 |
+
size 9547
|
29/replication_package/Code/LOG/log_Figure 6, SI1.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d85f5f426954e63384f029e51fc8bad3a0fa568e2e96c823912ac5ec3ce3b53e
|
3 |
+
size 25431
|
29/replication_package/Code/LOG/log_Figure 7, SI2, SI17.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5c7e9bea12b383efed75d618abbad1fe8b0fc400af78494907a990126013633a
|
3 |
+
size 6005
|
29/replication_package/Code/LOG/log_Figure 8, SI19.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b7f087095576a80be99dd9bef648177e9769b00e426d980ac0cdb581e51e7eb4
|
3 |
+
size 6385
|
29/replication_package/Code/LOG/log_Figure SI11, SI12, SI15, SI16.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9d32418d31e1abf7167109380c2c6a5dbd91821699226279b298073d65c5fe2b
|
3 |
+
size 6545
|
29/replication_package/Code/LOG/log_Figure SI3, SI4, SI5.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1e920a955b865e9923d5e317755c889c2ee835f017e75b054ada4534cd8668d9
|
3 |
+
size 7395
|
29/replication_package/Code/LOG/log_Figure SI6, SI7, SI8, SI9, SI10.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:60754321979b0db06d5eb13c89490b8d40c981341013b6ca81f43f839770694b
|
3 |
+
size 16586
|
29/replication_package/Code/LOG/log_SI_robust_prep.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:360ac1137e6197ce131aa3056d3a23d609bf8eb2ace11b14b7c2f0e6abfeaf17
|
3 |
+
size 13355
|
29/replication_package/Code/LOG/log_Table 1, SI3, SI4, SI5, Figure SI13, SI14.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:00d48e16d2aa71185aefc9e5f5b1a4d5c67d1168f975854b424970d30cd49210
|
3 |
+
size 63166
|
29/replication_package/Code/LOG/log_Table 2.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:72a6b1087b7f0f5c2195a5cfecb70dc38aeee23444503dccbca5eb450c7e72d5
|
3 |
+
size 21934
|
29/replication_package/Code/LOG/log_Table 3, Figure SI18.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:95ac713d5fe9453df1f122c481132f92d7f51dd38bf7d4df8fa6d6f6b3bd6917
|
3 |
+
size 18257
|
29/replication_package/Code/LOG/log_zzSI_robust_prep.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ef673370a716a15af5283f51187228e456867c6b8a528d6c480942e97c6c5ff0
|
3 |
+
size 18906
|
29/replication_package/Code/MASTER.R
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Master script
|
2 |
+
for(f in list.files('./')) {
|
3 |
+
if(grepl('helper_functions|MASTER|LOG',f)) { next }
|
4 |
+
if(file.exists(paste0('./LOG/log_',gsub('.R','.txt',f)))) { next }
|
5 |
+
# stop()
|
6 |
+
con <- file(paste0('./LOG/log_',gsub('.R','.txt',f)))
|
7 |
+
cat(f,'\n')
|
8 |
+
sink(con,append = TRUE)
|
9 |
+
sink(con,append = TRUE,type = 'message')
|
10 |
+
source(f,echo = T,max.deparse.length = 10000)
|
11 |
+
sink()
|
12 |
+
sink(type = 'message')
|
13 |
+
}
|
29/replication_package/Code/Table 1, SI3, SI4, SI5, Figure SI13, SI14.R
ADDED
@@ -0,0 +1,349 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# File name: table1_SItable3_SItable5_SItable4_SIfigure13_SIfigure14.R
|
2 |
+
# In:
|
3 |
+
# - replication_data.RData
|
4 |
+
# Out:
|
5 |
+
# - /Tables/table1.tex
|
6 |
+
# - /Tables/SI-table3.tex
|
7 |
+
# - /Figures/SI_figure13.pdf
|
8 |
+
# - /Tables/SI-table4.tex
|
9 |
+
# - /Tables/SI-table5.tex
|
10 |
+
# - /Figures/SI_figure14.pdf
|
11 |
+
|
12 |
+
require(lme4)
|
13 |
+
require(lfe)
|
14 |
+
require(MatchIt)
|
15 |
+
require(WeightIt)
|
16 |
+
require(tjbal)
|
17 |
+
require(optmatch)
|
18 |
+
require(stargazer)
|
19 |
+
require(cobalt)
|
20 |
+
require(tidyverse)
|
21 |
+
|
22 |
+
rm(list = ls())
|
23 |
+
gc()
|
24 |
+
####################################################################################################################### Loading data
|
25 |
+
load('../Data/replication_data.RData')
|
26 |
+
|
27 |
+
|
28 |
+
####################################################################################################################### Loading functions
|
29 |
+
source('./helper_functions.R')
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
####################################################################################################################### Preparing variables
|
36 |
+
Y <- c('pct_sanders16',paste0(c("pct_","VAP_","pcttw_"),"sanders"))
|
37 |
+
D <- unlist(lapply(c("sc_","ln_"),function(x) paste0(x,paste0(c("county_","DMA_","state_"),"cases"))))
|
38 |
+
FE <- c("0","DMA_CODE","date")
|
39 |
+
covariates <- c("sc_CTY_LTHS","sc_CTY_CollUp",
|
40 |
+
"sc_CTY_LT30yo","sc_CTY_60Up",
|
41 |
+
"sc_CTY_Below_poverty_level_AGE_18_64","sc_CTY_Female_hher_no_husbandhh",
|
42 |
+
"sc_CTY_Unem_rate_pop_16_over","sc_CTY_Labor_Force_Part_Rate_pop_16_over",
|
43 |
+
"sc_CTY_Manufactur","sc_CTY_Md_inc_hhs",
|
44 |
+
"sc_CTY_POPPCT_RURAL","sc_CTY_Speak_only_English","sc_CTY_White","sc_CTY_Black_or_African_American",
|
45 |
+
"ln_CTY_tot_pop","sc_turnout_pct_20",'caucus_switch','caucus')
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
# Function for generating matched data + weighted data
|
50 |
+
wgtMatchFun <- function(dat,formMatch,formWgt,matchMethod = 'nearest',wgtMethod = 'cbps') {
|
51 |
+
m.out <- matchit(formula = formula(formMatch),
|
52 |
+
data = dat,
|
53 |
+
method = matchMethod,
|
54 |
+
distance = "mahalanobis")
|
55 |
+
|
56 |
+
m.data <- match.data(m.out)
|
57 |
+
W.out <- weightit(formula = formula(formWgt),
|
58 |
+
data = dat,
|
59 |
+
estimand = "ATT",
|
60 |
+
method = wgtMethod)
|
61 |
+
|
62 |
+
# Balance tables
|
63 |
+
balt.pre <- bal.tab(formula(formWgt),
|
64 |
+
data = dat,
|
65 |
+
estimand = "ATT",
|
66 |
+
m.threshold = .05)
|
67 |
+
|
68 |
+
balt.post <- bal.tab(W.out, m.threshold = .05, disp.v.ratio = TRUE)
|
69 |
+
unm <- data.frame(Covs = gsub('_$','',rownames(balt.pre$Balance)),
|
70 |
+
balt.pre$Balance %>%
|
71 |
+
mutate(Diff_Unm = round(Diff.Un,2),
|
72 |
+
Bal_Test_Unm = M.Threshold.Un)) %>%
|
73 |
+
select(Covs,Diff_Unm,Bal_Test_Unm)
|
74 |
+
|
75 |
+
match <- data.frame(Covs = rownames(balt.post$Balance),
|
76 |
+
balt.post$Balance %>% mutate(Diff_Match = round(Diff.Adj,2),
|
77 |
+
Bal_Test_Match = M.Threshold)) %>%
|
78 |
+
select(Covs,Diff_Match,Bal_Test_Match)
|
79 |
+
|
80 |
+
|
81 |
+
balTab <- match %>% left_join(unm) %>% select(Covs,Diff_Unm,Bal_Test_Unm,Diff_Match,Bal_Test_Match)
|
82 |
+
balPlot <- qqprep(x = m.out)
|
83 |
+
|
84 |
+
return(list(balTab = balTab,
|
85 |
+
m.data = m.data,
|
86 |
+
W.out = W.out,
|
87 |
+
dat = dat,
|
88 |
+
balPlot = balPlot))
|
89 |
+
}
|
90 |
+
|
91 |
+
|
92 |
+
set.seed(123)
|
93 |
+
finalDat$treatBin <- ifelse(finalDat$DMA_cases > 1,1,0)
|
94 |
+
|
95 |
+
# Full Sample Regressions: Nearest neighbor + CBPS
|
96 |
+
summary(mFullRaw <- felm(as.formula(paste0("scale(pcttw_sanders) ~ treatBin + ",
|
97 |
+
paste(c(covariates,"pcttw_sanders16"),collapse = " + "),
|
98 |
+
" | DMA_CODE | 0 | DMA_CODE")),
|
99 |
+
finalDat))
|
100 |
+
|
101 |
+
|
102 |
+
foranal.weight <- finalDat %>%
|
103 |
+
select(pcttw_sanders,DMA_cases,treatBin,DMA_CODE,date,stab,covariates,pcttw_sanders16) %>%
|
104 |
+
filter(complete.cases(.))
|
105 |
+
|
106 |
+
fullPrepped <- wgtMatchFun(dat = foranal.weight,
|
107 |
+
formMatch = paste0('treatBin ~ ',paste(covariates,collapse = " + ")),
|
108 |
+
formWgt = paste0('treatBin ~ ',paste(covariates,collapse = " + ")),
|
109 |
+
matchMethod = 'nearest',
|
110 |
+
wgtMethod = 'cbps')
|
111 |
+
|
112 |
+
summary(mFullMatch <- felm(as.formula(paste0("scale(pcttw_sanders) ~ treatBin + ",
|
113 |
+
paste(c(covariates,"pcttw_sanders16"),collapse = " + "),
|
114 |
+
" | DMA_CODE | 0 | DMA_CODE")),
|
115 |
+
fullPrepped$m.data,weights = fullPrepped$m.data$weights))
|
116 |
+
|
117 |
+
|
118 |
+
summary(mFullWgt <- felm(as.formula(paste0("scale(pcttw_sanders) ~ treatBin + ",
|
119 |
+
paste(c(covariates,"pcttw_sanders16"),collapse = " + "),
|
120 |
+
" | DMA_CODE | 0 | DMA_CODE")),
|
121 |
+
fullPrepped$dat,weights = fullPrepped$W.out$weights))
|
122 |
+
|
123 |
+
|
124 |
+
# March + April Regressions
|
125 |
+
foranal.weight <- finalDat %>%
|
126 |
+
select(pcttw_sanders,DMA_cases,treatBin,
|
127 |
+
DMA_CODE,date,stab,covariates,
|
128 |
+
pcttw_sanders16) %>%
|
129 |
+
filter(complete.cases(.),
|
130 |
+
date >= as.Date('2020-03-01'))
|
131 |
+
|
132 |
+
covariates <- covariates[-which(covariates == 'caucus')]
|
133 |
+
|
134 |
+
marPrepped <- wgtMatchFun(dat = foranal.weight,
|
135 |
+
formMatch = paste0('treatBin ~ ',paste(covariates,collapse = " + ")),
|
136 |
+
formWgt = paste0('treatBin ~ ',paste(covariates,collapse = " + ")),
|
137 |
+
matchMethod = 'nearest',
|
138 |
+
wgtMethod = 'cbps')
|
139 |
+
|
140 |
+
summary(mMarRaw <- felm(as.formula(paste0("scale(pcttw_sanders) ~ treatBin + ",
|
141 |
+
paste(c(covariates,"pcttw_sanders16"),collapse = " + ")," | DMA_CODE | 0 | DMA_CODE")),
|
142 |
+
finalDat %>% filter(date >= as.Date("2020-03-01"))))
|
143 |
+
|
144 |
+
summary(mMarMatch <- felm(as.formula(paste0("scale(pcttw_sanders) ~ treatBin + ",
|
145 |
+
paste(c(covariates,"pcttw_sanders16"),collapse = " + ")," | DMA_CODE | 0 | DMA_CODE")),
|
146 |
+
marPrepped$m.data,weights = marPrepped$m.data$weights))
|
147 |
+
|
148 |
+
|
149 |
+
summary(mMarWgt <- felm(as.formula(paste0("scale(pcttw_sanders) ~ treatBin + ",
|
150 |
+
paste(c(covariates,"pcttw_sanders16"),collapse = " + ")," | DMA_CODE | 0 | DMA_CODE")),
|
151 |
+
marPrepped$dat,weights = marPrepped$W.out$weights))
|
152 |
+
|
153 |
+
|
154 |
+
# Table 1
|
155 |
+
covars <- rownames(mFullRaw$coefficients)
|
156 |
+
labs <- gsub('Treatbin','Exposure Dummy',gsub('Turnout pct 20','Turnout 2020',gsub('Pcttw sanders16','Sanders 2016',gsub('Caucus$','Caucus dummy',gsub("Lths","LTHS",gsub("Collup","Coll. Up",gsub("Md inc HH","Med HH Inc",gsub("Sc ","",gsub(" or african american|band$","",Hmisc::capitalize(trimws(gsub("manufactur","manufacturing",gsub("hher|hhs","HH",gsub("labor force part rate","LFPR",gsub("bachelor s","bachelor's",gsub("age 18 64|pop 16 over|hh$|poppct","",gsub("\\_"," ",gsub("^sc_|^.*cty\\_","",tolower(covars)))))))))))))))))))
|
157 |
+
|
158 |
+
prepTable <- function(tab,keeps = c('Exposure Dummy','Turnout 2020','Sanders 2016',
|
159 |
+
'Caucus dummy','Caucus switch'),tops = 1:14,bottoms = (length(tab) - 8):length(tab)) {
|
160 |
+
inds <- NULL
|
161 |
+
for(keep in keeps) {
|
162 |
+
tmpInds <- which(grepl(keep,tab))
|
163 |
+
for(i in tmpInds) {
|
164 |
+
inds <- c(inds,i:(i+2))
|
165 |
+
}
|
166 |
+
}
|
167 |
+
inds <- unique(c(tops,inds,bottoms))
|
168 |
+
|
169 |
+
return(tab[inds])
|
170 |
+
}
|
171 |
+
|
172 |
+
tex <- stargazer(mFullRaw,mFullMatch,mFullWgt,
|
173 |
+
mMarRaw,mMarMatch,mMarWgt,
|
174 |
+
keep.stat = c("n","rsq"),
|
175 |
+
star.cutoffs = c(.1,.05,.01,.001),
|
176 |
+
star.char = c('\\dag','*','**','***'),
|
177 |
+
covariate.labels = labs)
|
178 |
+
|
179 |
+
cat(paste(prepTable(tex,keeps = c('Exposure Dummy','Turnout 2020','Sanders 2016',
|
180 |
+
'Caucus switch','Caucus dummy')),
|
181 |
+
collapse = '\n'),file = '../Tables/table1.tex')
|
182 |
+
|
183 |
+
|
184 |
+
|
185 |
+
# SI Table 3
|
186 |
+
stargazer(marPrepped$balTab,summary = F,out = '../Tables/SI-table3.tex')
|
187 |
+
|
188 |
+
|
189 |
+
|
190 |
+
# SI Figure 13
|
191 |
+
toplot <- fullPrepped$balPlot$toplot
|
192 |
+
# rr <- marPrepped$balPlot$rr
|
193 |
+
|
194 |
+
dist2d <- function(x,y) {
|
195 |
+
m <- cbind(c(-1,-1),c(x,y))
|
196 |
+
abs(det(m))/sqrt(sum(c(-1,-1)^2))
|
197 |
+
}
|
198 |
+
vdist2d <- Vectorize(dist2d,vectorize.args = c("x","y"))
|
199 |
+
toplot$type <- as.character(toplot$type)
|
200 |
+
toplot$cov <- as.character(toplot$cov)
|
201 |
+
toplot <- toplot %>% bind_rows(toplot %>% group_by(cov) %>% summarise(x = min(x,y),y = min(x,y)),
|
202 |
+
toplot %>% group_by(cov) %>% summarise(x = max(x,y),y = max(x,y)))
|
203 |
+
|
204 |
+
|
205 |
+
toplot$cov <- Hmisc::capitalize(tolower(trimws(gsub("Manufactur","Manufacturing",gsub("Labor Force Part Rate","LFPR",gsub("hher|hhs","HH",gsub("Bachelor s","Bachelor's",gsub("_"," ",gsub("CTY_|AGE_18_64|pop_16_over|hh$|POPPCT","",toplot$cov)))))))))
|
206 |
+
toplot %>% group_by(type,cov) %>%
|
207 |
+
summarise(eucdist = mean(vdist2d(x = x,y= y))) %>%
|
208 |
+
filter(!is.na(type)) -> sum.euc
|
209 |
+
|
210 |
+
|
211 |
+
|
212 |
+
pdf('../Figures/SI_figure13.pdf',width = 7,height = 7)
|
213 |
+
toplot %>%
|
214 |
+
filter(!grepl('caucus',tolower(cov))) %>%
|
215 |
+
ggplot(aes(x = x,y = y,color = factor(type,levels = c("Raw","Matched")),
|
216 |
+
text = paste0(type,"\nEuc Dist = ",round(vdist2d(x = x,y = y),2)))) +
|
217 |
+
geom_point(alpha = .3,size = .5) +
|
218 |
+
geom_abline() +
|
219 |
+
geom_abline(aes(intercept = (rrlb - rrub) * 0.1,slope = 1),linetype = "dashed") +
|
220 |
+
geom_abline(aes(intercept = -(rrlb - rrub) * 0.1,slope = 1),linetype = "dashed") +
|
221 |
+
theme_bw() +
|
222 |
+
xlab("Control") + ylab("Treated") +
|
223 |
+
theme(legend.position = "bottom") +
|
224 |
+
facet_wrap(~cov,scales = "free") +
|
225 |
+
scale_color_discrete("",na.translate = F) +
|
226 |
+
guides(color = guide_legend(override.aes = list(size = 5,alpha = 1)))
|
227 |
+
dev.off()
|
228 |
+
|
229 |
+
|
230 |
+
|
231 |
+
|
232 |
+
|
233 |
+
|
234 |
+
|
235 |
+
|
236 |
+
# SI Tables 4-5 and Figure 14
|
237 |
+
# Full Sample Regressions: CEM + optweights
|
238 |
+
summary(mFullRaw <- felm(as.formula(paste0("scale(pcttw_sanders) ~ treatBin + ",
|
239 |
+
paste(c(covariates,"pcttw_sanders16"),collapse = " + "),
|
240 |
+
" | DMA_CODE | 0 | DMA_CODE")),
|
241 |
+
finalDat))
|
242 |
+
|
243 |
+
|
244 |
+
foranal.weight <- finalDat %>% select(pcttw_sanders,DMA_cases,treatBin,DMA_CODE,date,stab,covariates,pcttw_sanders16) %>% filter(complete.cases(.))
|
245 |
+
fullPrepped <- wgtMatchFun(dat = foranal.weight,
|
246 |
+
formMatch = paste0('treatBin ~ ',paste(covariates[c(4,2,10,12,7,13)],collapse = " + ")),
|
247 |
+
formWgt = paste0('treatBin ~ ',paste(covariates,collapse = " + ")),
|
248 |
+
matchMethod = 'cem',
|
249 |
+
wgtMethod = 'optweight')
|
250 |
+
|
251 |
+
summary(mFullMatch <- felm(as.formula(paste0("scale(pcttw_sanders) ~ treatBin + ",
|
252 |
+
paste(c(covariates,"pcttw_sanders16"),collapse = " + "),
|
253 |
+
" | DMA_CODE | 0 | DMA_CODE")),
|
254 |
+
fullPrepped$m.data,weights = fullPrepped$m.data$weights))
|
255 |
+
|
256 |
+
|
257 |
+
summary(mFullWgt <- felm(as.formula(paste0("scale(pcttw_sanders) ~ treatBin + ",
|
258 |
+
paste(c(covariates,"pcttw_sanders16"),collapse = " + "),
|
259 |
+
" | DMA_CODE | 0 | DMA_CODE")),
|
260 |
+
fullPrepped$dat,weights = fullPrepped$W.out$weights))
|
261 |
+
|
262 |
+
|
263 |
+
# March + April Regressions
|
264 |
+
foranal.weight <- finalDat %>%
|
265 |
+
select(pcttw_sanders,DMA_cases,treatBin,
|
266 |
+
DMA_CODE,date,stab,covariates,
|
267 |
+
pcttw_sanders16) %>%
|
268 |
+
filter(complete.cases(.),
|
269 |
+
date >= as.Date('2020-03-01'))
|
270 |
+
|
271 |
+
covs <- covariates[-which(grepl('caucus',covariates))]
|
272 |
+
|
273 |
+
marPrepped <- wgtMatchFun(dat = foranal.weight,
|
274 |
+
formMatch = paste0('treatBin ~ ',paste(covs[c(4,2,10,12,7,13)],collapse = " + ")), # CEM fails with the full set of covariates. See SI page 13-15 for discussion.
|
275 |
+
formWgt = paste0('treatBin ~ ',paste(covs,collapse = " + ")),
|
276 |
+
matchMethod = 'cem',
|
277 |
+
wgtMethod = 'optweight')
|
278 |
+
|
279 |
+
summary(mMarRaw <- felm(as.formula(paste0("scale(pcttw_sanders) ~ treatBin + ",
|
280 |
+
paste(c(covs,"pcttw_sanders16"),collapse = " + "),
|
281 |
+
" | DMA_CODE | 0 | DMA_CODE")),
|
282 |
+
finalDat %>% filter(date >= as.Date("2020-03-01"))))
|
283 |
+
|
284 |
+
summary(mMarMatch <- felm(as.formula(paste0("scale(pcttw_sanders) ~ treatBin + ",
|
285 |
+
paste(c(covs,"pcttw_sanders16"),collapse = " + "),
|
286 |
+
" | DMA_CODE | 0 | DMA_CODE")),
|
287 |
+
marPrepped$m.data,weights = marPrepped$m.data$weights))
|
288 |
+
|
289 |
+
|
290 |
+
summary(mMarWgt <- felm(as.formula(paste0("scale(pcttw_sanders) ~ treatBin + ",
|
291 |
+
paste(c(covs,"pcttw_sanders16"),collapse = " + "),
|
292 |
+
" | DMA_CODE | 0 | DMA_CODE")),
|
293 |
+
marPrepped$dat,weights = marPrepped$W.out$weights))
|
294 |
+
|
295 |
+
|
296 |
+
# SI Table 4
|
297 |
+
stargazer(fullPrepped$balTab,summary = F,out = '../Tables/SI-table4.tex')
|
298 |
+
|
299 |
+
|
300 |
+
# SI Table 5
|
301 |
+
covars <- rownames(mFullRaw$coefficients)
|
302 |
+
labs <- gsub('Treatbin','Exposure Dummy',gsub('Turnout pct 20','Turnout 2020',gsub('Pcttw sander16','Sanders 2016',gsub('Caucus$','Caucus dummy',gsub("Lths","LTHS",gsub("Collup","Coll. Up",gsub("Md inc HH","Med HH Inc",gsub("Sc ","",gsub(" or african american|band$","",Hmisc::capitalize(trimws(gsub("manufactur","manufacturing",gsub("hher|hhs","HH",gsub("labor force part rate","LFPR",gsub("bachelor s","bachelor's",gsub("age 18 64|pop 16 over|hh$|poppct","",gsub("\\_"," ",gsub("^sc_|^.*cty\\_","",tolower(covars)))))))))))))))))))
|
303 |
+
|
304 |
+
stargazer(mFullRaw,mFullMatch,mFullWgt,
|
305 |
+
mMarRaw,mMarMatch,mMarWgt,
|
306 |
+
keep.stat = c("n","rsq"),
|
307 |
+
star.cutoffs = c(.1,.05,.01,.001),
|
308 |
+
star.char = c('\\dag','*','**','***'),
|
309 |
+
covariate.labels = labs,out = '../Tables/SI-table5.tex')
|
310 |
+
|
311 |
+
|
312 |
+
|
313 |
+
|
314 |
+
# SI Figure 14
|
315 |
+
toplot <- fullPrepped$balPlot$toplot
|
316 |
+
|
317 |
+
dist2d <- function(x,y) {
|
318 |
+
m <- cbind(c(-1,-1),c(x,y))
|
319 |
+
abs(det(m))/sqrt(sum(c(-1,-1)^2))
|
320 |
+
}
|
321 |
+
vdist2d <- Vectorize(dist2d,vectorize.args = c("x","y"))
|
322 |
+
toplot$type <- as.character(toplot$type)
|
323 |
+
toplot$cov <- as.character(toplot$cov)
|
324 |
+
toplot <- toplot %>% bind_rows(toplot %>% group_by(cov) %>% summarise(x = min(x,y),y = min(x,y)),
|
325 |
+
toplot %>% group_by(cov) %>% summarise(x = max(x,y),y = max(x,y)))
|
326 |
+
|
327 |
+
|
328 |
+
toplot$cov <- Hmisc::capitalize(tolower(trimws(gsub("Manufactur","Manufacturing",gsub("Labor Force Part Rate","LFPR",gsub("hher|hhs","HH",gsub("Bachelor s","Bachelor's",gsub("_"," ",gsub("CTY_|AGE_18_64|pop_16_over|hh$|POPPCT","",toplot$cov)))))))))
|
329 |
+
toplot %>% group_by(type,cov) %>%
|
330 |
+
summarise(eucdist = mean(vdist2d(x = x,y= y))) %>%
|
331 |
+
filter(!is.na(type)) -> sum.euc
|
332 |
+
|
333 |
+
|
334 |
+
|
335 |
+
pdf('../Figures/SI_figure14.pdf',width = 7,height = 5)
|
336 |
+
toplot %>%
|
337 |
+
filter(!grepl('caucus',tolower(cov))) %>%
|
338 |
+
ggplot(aes(x = x,y = y,color = factor(type,levels = c("Raw","Matched")),text = paste0(type,"\nEuc Dist = ",round(vdist2d(x = x,y = y),2)))) +
|
339 |
+
geom_point(alpha = .3,size = .5) +
|
340 |
+
geom_abline() +
|
341 |
+
geom_abline(aes(intercept = (rrlb - rrub) * 0.1,slope = 1),linetype = "dashed") +
|
342 |
+
geom_abline(aes(intercept = -(rrlb - rrub) * 0.1,slope = 1),linetype = "dashed") +
|
343 |
+
theme_bw() +
|
344 |
+
xlab("Control") + ylab("Treated") +
|
345 |
+
theme(legend.position = "bottom") +
|
346 |
+
facet_wrap(~cov,scales = "free") +
|
347 |
+
scale_color_discrete("",na.translate = F) +
|
348 |
+
guides(color = guide_legend(override.aes = list(size = 5,alpha = 1)))
|
349 |
+
dev.off()
|
29/replication_package/Code/Table 2.R
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# File name: table2.R
|
2 |
+
# In:
|
3 |
+
# - /Data/survey_experiment_data.RData
|
4 |
+
# Out:
|
5 |
+
# - /Tables/table2.tex
|
6 |
+
|
7 |
+
rm(list = ls())
|
8 |
+
gc()
|
9 |
+
require(tidyverse)
|
10 |
+
|
11 |
+
load('../Data/survey_experiment_data.RData')
|
12 |
+
# Basic test: Does pessimistic treatment increase support for mainstream candidate?
|
13 |
+
summary(main <- lm(scale(y) ~ z + factor(polAge) + factor(polOcc) + factor(polPlat) +
|
14 |
+
factor(age) + factor(sex) + factor(hisp) + white + black + asian + factor(pid),
|
15 |
+
foranal %>%
|
16 |
+
filter(duration > 180) %>%
|
17 |
+
mutate(sex = factor(sex,levels = c("Neither male nor female describes me accurately OR prefer not to say","Female","Male")),
|
18 |
+
polPlat = factor(polPlat,levels = c("none","hlth","educ")),
|
19 |
+
pid = factor(pid,levels = c("Strong Democrat","Democrat","Lean Democrat","Independent","Lean Republican","Republican","Strong Republican")),
|
20 |
+
covid_self = factor(covid_self,levels = c("No","Maybe","Yes")),
|
21 |
+
covid_pers = factor(covid_pers,levels = c("No","Maybe","Yes")))))
|
22 |
+
|
23 |
+
summary(biv <- lm(yBin ~ z,
|
24 |
+
foranal %>%
|
25 |
+
filter(duration > 0) %>%
|
26 |
+
mutate(sex = factor(sex,levels = c("Neither male nor female describes me accurately OR prefer not to say","Female","Male")),
|
27 |
+
polPlat = factor(polPlat,levels = c("none","hlth","educ")),
|
28 |
+
pid = factor(pid,levels = c("Strong Democrat","Democrat","Lean Democrat","Independent","Lean Republican","Republican","Strong Republican")),
|
29 |
+
covid_self = factor(covid_self,levels = c("No","Maybe","Yes")),
|
30 |
+
covid_pers = factor(covid_pers,levels = c("No","Maybe","Yes")))))
|
31 |
+
|
32 |
+
summary(ind <- lm(yBin ~ z +
|
33 |
+
factor(age) + factor(sex) + factor(hisp) + white + black + asian + factor(pid),
|
34 |
+
foranal %>%
|
35 |
+
filter(duration > 0) %>%
|
36 |
+
mutate(sex = factor(sex,levels = c("Neither male nor female describes me accurately OR prefer not to say","Female","Male")),
|
37 |
+
polPlat = factor(polPlat,levels = c("none","hlth","educ")),
|
38 |
+
pid = factor(pid,levels = c("Strong Democrat","Democrat","Lean Democrat","Independent","Lean Republican","Republican","Strong Republican")),
|
39 |
+
covid_self = factor(covid_self,levels = c("No","Maybe","Yes")),
|
40 |
+
covid_pers = factor(covid_pers,levels = c("No","Maybe","Yes")))))
|
41 |
+
|
42 |
+
summary(indcov <- lm(yBin ~ z +
|
43 |
+
factor(age) + factor(sex) + factor(hisp) + white + black + asian + factor(pid),
|
44 |
+
foranal %>%
|
45 |
+
filter(duration > 0) %>%
|
46 |
+
mutate(sex = factor(sex,levels = c("Neither male nor female describes me accurately OR prefer not to say","Female","Male")),
|
47 |
+
polPlat = factor(polPlat,levels = c("none","hlth","educ")),
|
48 |
+
pid = factor(pid,levels = c("Strong Democrat","Democrat","Lean Democrat","Independent","Lean Republican","Republican","Strong Republican")),
|
49 |
+
covid_self = factor(covid_self,levels = c("No","Maybe","Yes")),
|
50 |
+
covid_pers = factor(covid_pers,levels = c("No","Maybe","Yes")))))
|
51 |
+
|
52 |
+
summary(full <- lm(yBin ~ z +
|
53 |
+
factor(age) + factor(sex) + factor(hisp) + white + black + asian + factor(pid) +
|
54 |
+
factor(polAge) + factor(polOcc) + factor(polPlat),
|
55 |
+
foranal %>%
|
56 |
+
filter(duration > 0) %>%
|
57 |
+
mutate(sex = factor(sex,levels = c("Neither male nor female describes me accurately OR prefer not to say","Female","Male")),
|
58 |
+
polPlat = factor(polPlat,levels = c("none","hlth","educ")),
|
59 |
+
pid = factor(pid,levels = c("Strong Democrat","Democrat","Lean Democrat","Independent","Lean Republican","Republican","Strong Republican")),
|
60 |
+
covid_self = factor(covid_self,levels = c("No","Maybe","Yes")),
|
61 |
+
covid_pers = factor(covid_pers,levels = c("No","Maybe","Yes")))))
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
summary(attentive <- lm(yBin ~ z +
|
66 |
+
factor(age) + factor(sex) + factor(hisp) + white + black + asian + factor(pid) +
|
67 |
+
factor(polAge) + factor(polOcc) + factor(polPlat),
|
68 |
+
foranal %>%
|
69 |
+
filter(duration > 150) %>%
|
70 |
+
mutate(sex = factor(sex,levels = c("Neither male nor female describes me accurately OR prefer not to say","Female","Male")),
|
71 |
+
polPlat = factor(polPlat,levels = c("none","hlth","educ")),
|
72 |
+
pid = factor(pid,levels = c("Strong Democrat","Democrat","Lean Democrat","Independent","Lean Republican","Republican","Strong Republican")),
|
73 |
+
covid_self = factor(covid_self,levels = c("No","Maybe","Yes")),
|
74 |
+
covid_pers = factor(covid_pers,levels = c("No","Maybe","Yes")))))
|
75 |
+
require(stargazer)
|
76 |
+
stargazer(biv,ind,full,attentive,keep.stat = c("n","rsq"),keep = "pid|covid|z",
|
77 |
+
covariate.labels = c("Anxiety Prime","Democrat","Lean Dem","Independent","Lean GOP","Republican","Strong GOP"),
|
78 |
+
star.char = c('\\dag','*','**','***'),
|
79 |
+
star.cutoffs = c(.1,.05,.01,.001),
|
80 |
+
out = "../Tables/table2.tex")
|
81 |
+
|
82 |
+
|
29/replication_package/Code/Table 3, Figure SI18.R
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# File name: table3_SIfigure18.R
|
2 |
+
# In:
|
3 |
+
# - /Data/primary_data.RData: Primary election results for the house of representatives, linked with district-level cases and deaths
|
4 |
+
# Out:
|
5 |
+
# - /Tables/table3.tex
|
6 |
+
# - /Figures/SI_figure18.pdf
|
7 |
+
|
8 |
+
require(lme4)
|
9 |
+
require(lfe)
|
10 |
+
require(MatchIt)
|
11 |
+
require(WeightIt)
|
12 |
+
require(tjbal)
|
13 |
+
require(optmatch)
|
14 |
+
require(stargazer)
|
15 |
+
require(cobalt)
|
16 |
+
require(tidyverse)
|
17 |
+
require(stargazer)
|
18 |
+
require(gridExtra)
|
19 |
+
require(ggridges)
|
20 |
+
require(ggrepel)
|
21 |
+
|
22 |
+
rm(list = ls())
|
23 |
+
gc()
|
24 |
+
|
25 |
+
|
26 |
+
load('../Data/primary_data.RData')
|
27 |
+
|
28 |
+
# Table 3
|
29 |
+
summary(mVScaseEXT <- felm(voteSh ~ extreme*log(casesSum+1) | stcd | 0 | stcd,toAnal))
|
30 |
+
summary(mVScaseJST <- felm(voteSh ~ justice*log(casesSum+1) | stcd | 0 | stcd,toAnal))
|
31 |
+
summary(mVScaseTEA <- felm(voteSh ~ tea*log(casesSum+1) | stcd | 0 | stcd,toAnal))
|
32 |
+
|
33 |
+
summary(mVSdthEXT <- felm(voteSh ~ extreme*log(deathsSum+1) | stcd | 0 | stcd,toAnal))
|
34 |
+
summary(mVSdthJST <- felm(voteSh ~ justice*log(deathsSum+1) | stcd | 0 | stcd,toAnal))
|
35 |
+
summary(mVSdthTEA <- felm(voteSh ~ tea*log(deathsSum+1) | stcd | 0 | stcd,toAnal))
|
36 |
+
|
37 |
+
summary(mVTcaseEXT <- felm(log(votes+1) ~ extreme*log(casesSum+1) + log(totVotes+1) | stcd | 0 | stcd,toAnal %>% filter(votes != 0)))
|
38 |
+
summary(mVTcaseJST <- felm(log(votes+1) ~ justice*log(casesSum+1) + log(totVotes+1) | stcd | 0 | stcd,toAnal %>% filter(votes != 0)))
|
39 |
+
summary(mVTcaseTEA <- felm(log(votes+1) ~ tea*log(casesSum+1) + log(totVotes+1) | stcd | 0 | stcd,toAnal %>% filter(votes != 0)))
|
40 |
+
|
41 |
+
summary(mVTdthEXT <- felm(log(votes+1) ~ extreme*log(deathsSum+1) + log(totVotes+1) | stcd | 0 | stcd,toAnal %>% filter(votes != 0)))
|
42 |
+
summary(mVTdthJST <- felm(log(votes+1) ~ justice*log(deathsSum+1) + log(totVotes+1) | stcd | 0 | stcd,toAnal %>% filter(votes != 0)))
|
43 |
+
summary(mVTdthTEA <- felm(log(votes+1) ~ tea*log(deathsSum+1) + log(totVotes+1) | stcd | 0 | stcd,toAnal %>% filter(votes != 0)))
|
44 |
+
|
45 |
+
regs <- list()
|
46 |
+
for(m in objects(pattern = '^mVScase')) {
|
47 |
+
regs[[m]] <- get(m)
|
48 |
+
rownames(regs[[m]]$coefficients) <- rownames(regs[[m]]$beta) <- gsub(':',' X ',
|
49 |
+
gsub('tea|justice|extreme','Anti-Est.',
|
50 |
+
gsub('log\\(deathsSum \\+ 1\\)','Deaths (ln)',
|
51 |
+
gsub('log\\(casesSum \\+ 1\\)','Cases (ln)',
|
52 |
+
gsub('log\\(totVotes \\+ 1\\)','Total Votes (ln)',rownames(regs[[m]]$coefficients))))))
|
53 |
+
}
|
54 |
+
|
55 |
+
stargazer(regs,keep.stat = c('n','rsq'),
|
56 |
+
star.char = c('\\dag','*','**','***'),
|
57 |
+
star.cutoffs = c(.1,.05,.01,.001),
|
58 |
+
add.lines = list(c('District FE','Y','Y','Y','Y')), out = '../Tables/table3.tex')
|
59 |
+
|
60 |
+
# SI Figure 18
|
61 |
+
toplot <- toAnal %>%
|
62 |
+
filter(date < as.Date('2020-11-01'),
|
63 |
+
votes > 0)
|
64 |
+
|
65 |
+
toplot$lab <- NA
|
66 |
+
for(d in unique(toplot$date)) {
|
67 |
+
toplot$lab[which(toplot$date == d)] <- paste(unique(toplot$stab[which(toplot$date == d)]),collapse = '\n')
|
68 |
+
}
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
p1 <- toplot %>%
|
73 |
+
ggplot(aes(x = date,y = voteSh,color = factor(extreme),size = casesSum,weight = log(casesSum+1),
|
74 |
+
alpha = factor(extreme),
|
75 |
+
label = lab)) +
|
76 |
+
geom_jitter(width = 0) +
|
77 |
+
geom_smooth(method = 'lm') +
|
78 |
+
geom_text(data = toplot %>%
|
79 |
+
group_by(date) %>%
|
80 |
+
slice(1) %>%
|
81 |
+
filter(!stab %in% c('HI','TN')) %>%
|
82 |
+
ungroup() %>%
|
83 |
+
mutate(voteSh = 1.01,
|
84 |
+
date = date - 3),
|
85 |
+
size = 2.5,hjust = 0,vjust = 0,color = 'black',alpha = .8) +
|
86 |
+
theme_ridges() +
|
87 |
+
scale_color_manual(name = 'Type',values = c(`0` = 'grey60',`1` = 'black'),label = c('Mainstream','Extreme')) +
|
88 |
+
scale_alpha_manual(guide = F,values = c(`0` = .15,`1` = .6)) +
|
89 |
+
scale_size_continuous(guide = F) +
|
90 |
+
xlab('Date') + ylab('Vote Share') +
|
91 |
+
scale_y_continuous(breaks = seq(0,1,by = .25),limits = c(0,1.25)) +
|
92 |
+
theme(legend.position = 'bottom')
|
93 |
+
|
94 |
+
|
95 |
+
p2 <- toplot %>%
|
96 |
+
ggplot(aes(x = log(casesSum+1),y = voteSh,color = factor(extreme))) +
|
97 |
+
geom_jitter(alpha = .2,width = .2) +
|
98 |
+
geom_smooth(method = 'lm') +
|
99 |
+
theme_ridges() +
|
100 |
+
scale_color_manual(name = 'Type',values = c(`0` = 'grey60',`1` = 'black'),label = c('Mainstream','Extreme')) +
|
101 |
+
scale_alpha_manual(guide = F,values = c(`0` = .15,`1` = .6)) +
|
102 |
+
scale_size_continuous(guide = F) +
|
103 |
+
xlab('Logged Cases') + ylab('Vote Share') +
|
104 |
+
scale_y_continuous(breaks = seq(0,1,by = .25),limits = c(0,1.25)) +
|
105 |
+
theme(legend.position = 'bottom')
|
106 |
+
|
107 |
+
pdf('../Figures/SI_figure18.pdf',width = 12,height = 8)
|
108 |
+
grid.arrange(p1,p2,ncol = 2)
|
109 |
+
dev.off()
|
29/replication_package/Code/helper_functions.R
ADDED
@@ -0,0 +1,206 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# QQ plot to evaluate balance
|
2 |
+
qqprep <- function (x, discrete.cutoff, which.subclass = NULL, numdraws = 5000,
|
3 |
+
interactive = T, which.xs = NULL, ...) {
|
4 |
+
X <- x$X
|
5 |
+
varnames <- colnames(X)
|
6 |
+
for (var in varnames) {
|
7 |
+
if (is.factor(X[, var])) {
|
8 |
+
tempX <- X[, !colnames(X) %in% c(var)]
|
9 |
+
form <- formula(substitute(~dummy - 1, list(dummy = as.name(var))))
|
10 |
+
X <- cbind(tempX, model.matrix(form, X))
|
11 |
+
}
|
12 |
+
}
|
13 |
+
covariates <- X
|
14 |
+
if (!is.null(which.xs)) {
|
15 |
+
if (sum(which.xs %in% dimnames(covariates)[[2]]) != length(which.xs)) {
|
16 |
+
stop("which.xs is incorrectly specified")
|
17 |
+
}
|
18 |
+
covariates <- covariates[, which.xs, drop = F]
|
19 |
+
}
|
20 |
+
treat <- x$treat
|
21 |
+
matched <- x$weights != 0
|
22 |
+
ratio <- x$call$ratio
|
23 |
+
if (is.null(ratio)) {
|
24 |
+
ratio <- 1
|
25 |
+
}
|
26 |
+
if (identical(x$call$method, "full") | (ratio != 1)) {
|
27 |
+
t.plot <- sample(names(treat)[treat == 1], numdraws/2,
|
28 |
+
replace = TRUE, prob = x$weights[treat == 1])
|
29 |
+
c.plot <- sample(names(treat)[treat == 0], numdraws/2,
|
30 |
+
replace = TRUE, prob = x$weights[treat == 0])
|
31 |
+
m.covariates <- x$X[c(t.plot, c.plot), ]
|
32 |
+
m.treat <- x$treat[c(t.plot, c.plot)]
|
33 |
+
}
|
34 |
+
else {
|
35 |
+
m.covariates <- covariates[matched, , drop = F]
|
36 |
+
m.treat <- treat[matched]
|
37 |
+
}
|
38 |
+
if (!is.null(which.subclass)) {
|
39 |
+
subclass <- x$subclass
|
40 |
+
sub.index <- subclass == which.subclass & !is.na(subclass)
|
41 |
+
sub.covariates <- covariates[sub.index, , drop = F]
|
42 |
+
sub.treat <- treat[sub.index]
|
43 |
+
sub.matched <- matched[sub.index]
|
44 |
+
m.covariates <- sub.covariates[sub.matched, , drop = F]
|
45 |
+
m.treat <- sub.treat[sub.matched]
|
46 |
+
}
|
47 |
+
nn <- dimnames(covariates)[[2]]
|
48 |
+
nc <- length(nn)
|
49 |
+
covariates <- data.matrix(covariates)
|
50 |
+
toplot <- NULL
|
51 |
+
for (i in 1:nc) {
|
52 |
+
xi <- covariates[, i]
|
53 |
+
m.xi <- m.covariates[, i]
|
54 |
+
rr <- range(xi)
|
55 |
+
|
56 |
+
|
57 |
+
eqqplot <- function(x,y) {
|
58 |
+
sx <- sort(x)
|
59 |
+
sy <- sort(y)
|
60 |
+
lenx <- length(sx)
|
61 |
+
leny <- length(sy)
|
62 |
+
if (leny < lenx)
|
63 |
+
sx <- approx(1:lenx, sx, n = leny, method = "constant")$y
|
64 |
+
if (leny > lenx)
|
65 |
+
sy <- approx(1:leny, sy, n = lenx, method = "constant")$y
|
66 |
+
return(list(x = sx,y = sy))
|
67 |
+
}
|
68 |
+
toplot <- bind_rows(toplot,
|
69 |
+
bind_rows(data.frame(eqqplot(x = xi[treat == 0],y = xi[treat == 1]),type = "Raw",cov = nn[i],rrlb = rr[1],rrub = rr[2]),
|
70 |
+
data.frame(eqqplot(x = m.xi[m.treat == 0],y = m.xi[m.treat == 1]),type = "Matched",cov = nn[i],rrlb = rr[1],rrub = rr[2])))
|
71 |
+
}
|
72 |
+
return(list(toplot = toplot))
|
73 |
+
}
|
74 |
+
|
75 |
+
|
76 |
+
# Ineration plot
|
77 |
+
interaction_plot_continuous <- function(model, effect = "", moderator = "", varcov="default", minimum="min", maximum="max",colr = "grey",
|
78 |
+
incr="default", num_points = 10, conf=.95, mean=FALSE, median=FALSE, alph=80, rugplot=T,
|
79 |
+
histogram=F, title="Marginal effects plot", xlabel="Value of moderator",
|
80 |
+
ylabel="Estimated marginal coefficient",pointsplot = F,plot = F,show_est = F) {
|
81 |
+
# Extract Variance Covariance matrix
|
82 |
+
if (varcov == "default"){
|
83 |
+
covMat = vcov(model)
|
84 |
+
}else{
|
85 |
+
covMat = varcov
|
86 |
+
}
|
87 |
+
|
88 |
+
# Extract the data frame of the model
|
89 |
+
mod_frame = model.frame(model)
|
90 |
+
|
91 |
+
# Get coefficients of variables
|
92 |
+
if(effect == "") {
|
93 |
+
int.string <- rownames(summary(model)$coefficients)[grepl(":",rownames(summary(model)$coefficients))]
|
94 |
+
effect <- substr(int.string,1,regexpr(":",int.string)[1]-1)
|
95 |
+
}
|
96 |
+
if(moderator == "") {
|
97 |
+
int.string <- rownames(summary(model)$coefficients)[grepl(":",rownames(summary(model)$coefficients))]
|
98 |
+
moderator <- substr(int.string,regexpr(":",int.string)[1]+1,nchar(int.string))
|
99 |
+
}
|
100 |
+
interaction <- paste(effect,":",moderator,sep="")
|
101 |
+
beta_1 = summary(model)$coefficients[effect,1]
|
102 |
+
beta_3 = summary(model)$coefficients[interaction,1]
|
103 |
+
|
104 |
+
# Set range of the moderator variable
|
105 |
+
# Minimum
|
106 |
+
if (minimum == "min"){
|
107 |
+
min_val = min(mod_frame[[moderator]])
|
108 |
+
}else{
|
109 |
+
min_val = minimum
|
110 |
+
}
|
111 |
+
# Maximum
|
112 |
+
if (maximum == "max"){
|
113 |
+
max_val = max(mod_frame[[moderator]])
|
114 |
+
}else{
|
115 |
+
max_val = maximum
|
116 |
+
}
|
117 |
+
|
118 |
+
# Check if minimum smaller than maximum
|
119 |
+
if (min_val > max_val){
|
120 |
+
stop("Error: Minimum moderator value greater than maximum value.")
|
121 |
+
}
|
122 |
+
|
123 |
+
# Determine intervals between values of the moderator
|
124 |
+
if (incr == "default"){
|
125 |
+
increment = (max_val - min_val)/(num_points - 1)
|
126 |
+
}else{
|
127 |
+
increment = incr
|
128 |
+
}
|
129 |
+
|
130 |
+
# Create list of moderator values at which marginal effect is evaluated
|
131 |
+
x_2 <- seq(from=min_val, to=max_val, by=increment)
|
132 |
+
|
133 |
+
# Compute marginal effects
|
134 |
+
delta_1 = beta_1 + beta_3*x_2
|
135 |
+
|
136 |
+
# Compute variances
|
137 |
+
var_1 = covMat[effect,effect] + (x_2^2)*covMat[interaction, interaction] + 2*x_2*covMat[effect, interaction]
|
138 |
+
|
139 |
+
# Standard errors
|
140 |
+
se_1 = sqrt(var_1)
|
141 |
+
|
142 |
+
# Upper and lower confidence bounds
|
143 |
+
z_score = qnorm(1 - ((1 - conf)/2))
|
144 |
+
upper_bound = sapply(1:length(z_score), function(x) delta_1 + z_score[x]*se_1)
|
145 |
+
lower_bound = sapply(1:length(z_score), function(x) delta_1 - z_score[x]*se_1)
|
146 |
+
|
147 |
+
# Determine the bounds of the graphing area
|
148 |
+
max_y = max(upper_bound)
|
149 |
+
min_y = min(lower_bound)
|
150 |
+
|
151 |
+
# Make the histogram color
|
152 |
+
hist_col = colr
|
153 |
+
|
154 |
+
stars <- ifelse(abs(summary(model)$coefficients[interaction,3]) >2.6,"***",
|
155 |
+
ifelse(abs(summary(model)$coefficients[interaction,3]) > 1.96,"**",
|
156 |
+
ifelse(abs(summary(model)$coefficients[interaction,3]) > 1.65,"*","")))
|
157 |
+
est <- paste("Interaction: ",round(summary(model)$coefficients[interaction,1],3),stars," (",
|
158 |
+
round(summary(model)$coefficients[interaction,2],3),")",sep="")
|
159 |
+
# Initialize plotting window
|
160 |
+
if(plot) {
|
161 |
+
plot(x=c(), y=c(), ylim=c(min_y, max_y), xlim=c(min_val, max_val),
|
162 |
+
xlab=xlabel, ylab=ylabel, main=title)
|
163 |
+
|
164 |
+
# Plot estimated effects
|
165 |
+
if(!pointsplot) {
|
166 |
+
lines(y=delta_1, x=x_2,col = colr)
|
167 |
+
for(i in ncol(upper_bound):1) {
|
168 |
+
polygon(c(x_2,rev(x_2)),c(upper_bound[,i],rev(lower_bound[,i])),border = NA,col = colr)
|
169 |
+
}
|
170 |
+
}else{
|
171 |
+
points(y = delta_1,x = x_2,col = colr,pch = 19)
|
172 |
+
for(i in ncol(upper_bound):1) {
|
173 |
+
segments(x_2,upper_bound[,i],x_2,lower_bound[,i],col = colr,lwd = i)
|
174 |
+
}
|
175 |
+
}
|
176 |
+
# Add a dashed horizontal line for zero
|
177 |
+
abline(h=0, lty=3)
|
178 |
+
|
179 |
+
# Add a vertical line at the mean
|
180 |
+
if (mean){
|
181 |
+
abline(v = mean(mod_frame[[moderator]]), lty=2, col="red")
|
182 |
+
}
|
183 |
+
|
184 |
+
# Add a vertical line at the median
|
185 |
+
if (median){
|
186 |
+
abline(v = median(mod_frame[[moderator]]), lty=3, col="blue")
|
187 |
+
}
|
188 |
+
|
189 |
+
# Add Rug plot
|
190 |
+
if (rugplot){
|
191 |
+
rug(mod_frame[[moderator]])
|
192 |
+
}
|
193 |
+
|
194 |
+
if (show_est) {
|
195 |
+
text(par('usr')[ 2 ], par('usr')[ 4 ],adj=c(1.05,1.2),
|
196 |
+
labels = est)
|
197 |
+
|
198 |
+
}
|
199 |
+
#Add Histogram (Histogram only plots when minimum and maximum are the min/max of the moderator)
|
200 |
+
if (histogram & minimum=="min" & maximum=="max"){
|
201 |
+
par(new=T)
|
202 |
+
hist(mod_frame[[moderator]], axes=F, xlab="", ylab="",main="", border=hist_col, col=hist_col)
|
203 |
+
}
|
204 |
+
}
|
205 |
+
return(list(delta_1 = delta_1,x_2 = x_2,ub = upper_bound,lb = lower_bound,inc = increment,est = est))
|
206 |
+
}
|
29/replication_package/Code/zzSI_robust_prep.R
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# File name: SI_robust_prep.R
|
2 |
+
# In:
|
3 |
+
# - replication_data.RData
|
4 |
+
# Out:
|
5 |
+
# - /Data/Results/SI-data.RData
|
6 |
+
|
7 |
+
require(lme4)
|
8 |
+
require(lfe)
|
9 |
+
require(MatchIt)
|
10 |
+
require(WeightIt)
|
11 |
+
require(tjbal)
|
12 |
+
require(optmatch)
|
13 |
+
require(stargazer)
|
14 |
+
require(cobalt)
|
15 |
+
require(gridExtra)
|
16 |
+
require(ggridges)
|
17 |
+
require(ggrepel)
|
18 |
+
require(CBPS)
|
19 |
+
require(tidyverse)
|
20 |
+
rm(list = ls())
|
21 |
+
gc()
|
22 |
+
|
23 |
+
####################################################################################################################### Loading data
|
24 |
+
load('../Data/replication_data.RData')
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
####################################################################################################################### Loading functions
|
30 |
+
source('./helper_functions.R')
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
# ####################################################################################################################### Command line arguments
|
35 |
+
# args <- commandArgs(trailingOnly = T)
|
36 |
+
# # args <- c(3,2,2,2)
|
37 |
+
# # Y: 1-3
|
38 |
+
# # D: 1-2
|
39 |
+
# # FE: 1-4
|
40 |
+
# # GEO: 1-2
|
41 |
+
# yInd <- as.numeric(args[1])
|
42 |
+
# dInd <- as.numeric(args[2])
|
43 |
+
# feInd <- as.numeric(args[3])
|
44 |
+
# geoInd <- as.numeric(args[4])
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
####################################################################################################################### Preparing variables
|
49 |
+
Y <- paste0(c("pct_","VAP_","pcttw_"),"sanders")
|
50 |
+
D <- unlist(lapply(c("sc_","ln_"),function(x) paste0(x,paste0(c("county_","DMA_","state_"),"cases"))))
|
51 |
+
FE <- c("0","DMA_CODE","date")
|
52 |
+
covariates <- c("sc_CTY_LTHS","sc_CTY_CollUp",
|
53 |
+
"sc_CTY_LT30yo","sc_CTY_60Up",
|
54 |
+
"sc_CTY_Below_poverty_level_AGE_18_64","sc_CTY_Female_hher_no_husbandhh",
|
55 |
+
"sc_CTY_Unem_rate_pop_16_over","sc_CTY_Labor_Force_Part_Rate_pop_16_over",
|
56 |
+
"sc_CTY_Manufactur","sc_CTY_Md_inc_hhs",
|
57 |
+
"sc_CTY_POPPCT_RURAL","sc_CTY_Speak_only_English","sc_CTY_White","sc_CTY_Black_or_African_American",
|
58 |
+
"ln_CTY_tot_pop","sc_turnout_pct_20")
|
59 |
+
|
60 |
+
|
61 |
+
set.seed(123)
|
62 |
+
resSI <- list()
|
63 |
+
for(y in 'pcttw_sanders') {
|
64 |
+
for(fe in c('DMA_CODE','0')) {
|
65 |
+
for(geo in 'DMA_') {
|
66 |
+
for(d in c('March10Cases','March17Cases')) {
|
67 |
+
for(pre in c("2020-03-01","2020-03-03","2020-03-10","2020-03-17")) {
|
68 |
+
for(post in c("2020-03-01","2020-03-03","2020-03-10","2020-03-17")) {
|
69 |
+
if(pre == post) { next }
|
70 |
+
|
71 |
+
if(pre == "2020-03-01") {
|
72 |
+
finalDat$post <- ifelse(finalDat$date == as.Date(post),1,
|
73 |
+
ifelse(finalDat$date <= as.Date(pre),0,NA))
|
74 |
+
} else if(post == "2020-03-01") {
|
75 |
+
finalDat$post <- ifelse(finalDat$date <= as.Date(post),1,
|
76 |
+
ifelse(finalDat$date == as.Date(pre),0,NA))
|
77 |
+
} else {
|
78 |
+
finalDat$post <- ifelse(finalDat$date == as.Date(post),1,
|
79 |
+
ifelse(finalDat$date == as.Date(pre),0,NA))
|
80 |
+
}
|
81 |
+
finalDat$treatBin <- ifelse(finalDat[[paste0(geo,d)]] > 1,1,0)
|
82 |
+
finalDat$treat <- ifelse(finalDat$post == 1 & finalDat$treatBin == 1,1,0)
|
83 |
+
tmpAnal <- finalDat %>% select(Y,treat,treatBin,c(covariates,paste0(y,"16")),pcttw_sanders16,VAP_sanders16,DMA_CODE,date,stab,matches("cases"),post) %>% filter(complete.cases(.))
|
84 |
+
m.out <- matchit(formula = as.formula(paste("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
|
85 |
+
data = tmpAnal,
|
86 |
+
method = "nearest",
|
87 |
+
distance = "mahalanobis")
|
88 |
+
|
89 |
+
m.data <- match.data(m.out)
|
90 |
+
W.out <- weightit(formula(paste0("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
|
91 |
+
data = tmpAnal, estimand = "ATT", method = "cbps")
|
92 |
+
|
93 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$bin <- summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients
|
94 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$cont <- summary(felm(as.formula(paste0('scale(',y,") ~ ",paste0("scale(",geo,d,")")," + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients
|
95 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$did$coefs <- summary(tmp <- felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients
|
96 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$did$mfx <- interaction_plot_continuous(tmp,num_points = 2,pointsplot = T)
|
97 |
+
|
98 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$bin <- summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients
|
99 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$cont <- summary(felm(as.formula(paste0('scale(',y,") ~ ",paste0("scale(",geo,d,")")," + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients
|
100 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$did$coefs <- summary(tmp <- felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients
|
101 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$did$mfx <- interaction_plot_continuous(tmp,num_points = 2,pointsplot = T)
|
102 |
+
|
103 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$bin <- summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients
|
104 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$cont <- summary(felm(as.formula(paste0('scale(',y,") ~ ",paste0("scale(",geo,d,")")," + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients
|
105 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$did$coefs <- summary(tmp <- felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients
|
106 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$did$mfx <- interaction_plot_continuous(tmp,num_points = 2,pointsplot = T)
|
107 |
+
|
108 |
+
# Permutation SHEEYAHT
|
109 |
+
basic.bin.plac <- basic.cont.plac <- basic.did.plac <- matching.bin.plac <- matching.cont.plac <- matching.did.plac <- weighting.bin.plac <- weighting.cont.plac <- weighting.did.plac <- NULL
|
110 |
+
for(bs in 1:100) {
|
111 |
+
tmpAnal$perm <- sample(tmpAnal[[paste0(geo,d)]],size = nrow(tmpAnal))
|
112 |
+
tmpAnal$treatBin <- ifelse(tmpAnal$perm > 1,1,0)
|
113 |
+
tmpAnal$treat <- ifelse(tmpAnal$post == 1 & tmpAnal$treatBin == 1,1,0)
|
114 |
+
m.out <- matchit(formula = as.formula(paste("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
|
115 |
+
data = tmpAnal,
|
116 |
+
method = "full",
|
117 |
+
distance = "mahalanobis")
|
118 |
+
|
119 |
+
m.data <- match.data(m.out)
|
120 |
+
W.out <- weightit(formula(paste0("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
|
121 |
+
data = tmpAnal, estimand = "ATT", method = "cbps")
|
122 |
+
|
123 |
+
test <- tryCatch(felm(as.formula(paste0('scale(',y,") ~ ",paste0("scale(",geo,d,")")," + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights),error = function(e) e)
|
124 |
+
if(inherits(test,"error")) { next }
|
125 |
+
basic.bin.plac <- c(basic.bin.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients['treat',1])
|
126 |
+
basic.cont.plac <- c(basic.cont.plac,summary(felm(as.formula(paste0('scale(',y,") ~ scale(perm) + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients["scale(perm)",1])
|
127 |
+
basic.did.plac <- c(basic.did.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal))$coefficients["treatBin:post",1])
|
128 |
+
|
129 |
+
matching.bin.plac <- c(matching.bin.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients['treat',1])
|
130 |
+
matching.cont.plac <- c(matching.cont.plac,summary(felm(as.formula(paste0('scale(',y,") ~ scale(perm) + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients["scale(perm)",1])
|
131 |
+
matching.did.plac <- c(matching.did.plac,
|
132 |
+
summary(felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),m.data,weights = m.data$weights))$coefficients["treatBin:post",1])
|
133 |
+
|
134 |
+
weighting.bin.plac <- c(weighting.bin.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treat + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients['treat',1])
|
135 |
+
weighting.cont.plac <- c(weighting.cont.plac,summary(felm(as.formula(paste0('scale(',y,") ~ scale(perm) + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients["scale(perm)",1])
|
136 |
+
weighting.did.plac <- c(weighting.did.plac,summary(felm(as.formula(paste0('scale(',y,") ~ treatBin*post + ",paste(c(covariates,paste0(y,"16")),collapse = "+"),"| 0 | 0 | ",fe)),tmpAnal,weights = W.out$weights))$coefficients["treatBin:post",1])
|
137 |
+
|
138 |
+
cat(".")
|
139 |
+
}
|
140 |
+
cat('\n')
|
141 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$plac$bin <- basic.bin.plac
|
142 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$plac$cont <- basic.cont.plac
|
143 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$basic$plac$did <- basic.did.plac
|
144 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$plac$bin <- matching.bin.plac
|
145 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$plac$cont <- matching.cont.plac
|
146 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$matching$plac$did <- matching.did.plac
|
147 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$plac$bin <- weighting.bin.plac
|
148 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$plac$cont <- weighting.cont.plac
|
149 |
+
resSI[[y]][[geo]][[d]][[fe]][[pre]][[post]]$felm$weighting$plac$did <- weighting.did.plac
|
150 |
+
}
|
151 |
+
}
|
152 |
+
}
|
153 |
+
}
|
154 |
+
}
|
155 |
+
cat(y," done\n")
|
156 |
+
}
|
157 |
+
|
158 |
+
|
159 |
+
save(resSI,file = paste0("../Data/Results/SI-data.RData"))
|
160 |
+
|
161 |
+
|
29/replication_package/Data/Pew/biden_sanders_ideo_feb_march_PEW.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:159d10048cec7aa71a84121e6cef5bc270bbfd91af5da4f82cbd7a4288479289
|
3 |
+
size 758
|
29/replication_package/Data/Results/SI-data.RData
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ab476e6942ab301cc9c95915213a0f0c1503878dc5cd1139c0dbe96579a0d2d2
|
3 |
+
size 612519
|
29/replication_package/Data/Results/tjbalWgtsNEW.RData
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:08ae796fec1e3fc0255d86b57fe7aa2cd06d6395ea53a5f7f1757f4467e4008d
|
3 |
+
size 694122
|
29/replication_package/Data/france_data.RData
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a6f0c86de924e700ca3bf8758cf1bd89256c7b7820ae0a52b2243a900fcb9d1b
|
3 |
+
size 4168
|
29/replication_package/Data/gtrends_data.RData
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:27e035cbf2594a8efdc2b95f2e3171d435ed835cd498c8577d785c5a3c83e4d1
|
3 |
+
size 197370
|
29/replication_package/Data/mobility_data.RData
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ae2ed4bc824bd34c0af6e2093730363c61fff688686ece88ba953d6825433e30
|
3 |
+
size 469100
|
29/replication_package/Data/nationscape_data.RData
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:788e8ad26b105cd3aa19e0960a47fc44a0d37c32292d7c4a205ea3a075ea9cd6
|
3 |
+
size 27497508
|
29/replication_package/Data/primary_data.RData
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2cc73e721d0b93cb122a077fbf0711c0388668d1a6bf66ea468f96423ca87b49
|
3 |
+
size 60772
|
29/replication_package/Data/replication_data.RData
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f9e444e6793f7dd569fc17e9ca8b42df53960580c8144456648ca3379d1ed82f
|
3 |
+
size 9279609
|
29/replication_package/Data/survey_experiment_data.RData
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:edab07dfde669772bac532ec9b86741db8ff6f367a0c56397928d8bb10a09de1
|
3 |
+
size 23389
|
29/replication_package/Figures/SI_figure1.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7192c65016253a89ff73933b49c0848a63e427512d6bebf07b5dbb91699c0b95
|
3 |
+
size 6336
|
29/replication_package/Figures/SI_figure10.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7c1ab6e919d917bc6b968e548f16759b1ed3e0823172a7afc22df391ede2745d
|
3 |
+
size 5579
|
29/replication_package/Figures/SI_figure11.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1d04e684de9173271a239189e07d683b05227fbf95e64243b1a465f021f8b8fa
|
3 |
+
size 62125
|
29/replication_package/Figures/SI_figure12.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6b1ab107ee1d2fae07f5b450291aefd3749efb07f0a8a9778dbb37c94b7dbe63
|
3 |
+
size 4449
|
29/replication_package/Figures/SI_figure13.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7bb03b2674f7465f06b2a3eef9c65d101817e2367b14fe963b564bab9fef56ff
|
3 |
+
size 515152
|
29/replication_package/Figures/SI_figure14.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2c3b82229f49e0e1d322910ada6ddc2731f280a100a0b648037f4fc6340cf83c
|
3 |
+
size 134261
|
29/replication_package/Figures/SI_figure15.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:40dedefec9dc40ccb32f4dcb04b2ffd8f6b796e0c6a98e615461f4d4bc7c6f4f
|
3 |
+
size 64107
|
29/replication_package/Figures/SI_figure16.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cbd3b1a7a87b2224dcb0ada95c68aec54129a0b64c79d11d804f52e211c0a106
|
3 |
+
size 176520
|
29/replication_package/Figures/SI_figure17.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:dfd85007fd61ae94a998ff23d0046a0f2d84da9bb520e7101e226dd71b4c2c55
|
3 |
+
size 4881
|