File size: 7,525 Bytes
0369477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File name: figure5.R
# In: 
#   - replication_data.RData
# Out: 
#   - /Figures/figure5.pdf


require(lme4)
require(lfe)
require(MatchIt)
require(WeightIt)
require(tjbal)
require(optmatch)
require(stargazer)
require(cobalt)
require(tidyverse)
require(ggridges)

rm(list = ls())
gc()
####################################################################################################################### Loading data
load('../Data/replication_data.RData')


####################################################################################################################### Loading functions
source('./helper_functions.R')







D <- unlist(lapply(c("sc_","ln_"),function(x) paste0(x,paste0(c("county_","DMA_","state_"),"cases"))))
FE <- c("0","DMA_CODE","date")
covariates <- c("sc_CTY_LTHS","sc_CTY_CollUp",
                "sc_CTY_LT30yo","sc_CTY_60Up",
                "sc_CTY_Below_poverty_level_AGE_18_64","sc_CTY_Female_hher_no_husbandhh",
                "sc_CTY_Unem_rate_pop_16_over","sc_CTY_Labor_Force_Part_Rate_pop_16_over",
                "sc_CTY_Manufactur","sc_CTY_Md_inc_hhs",
                "sc_CTY_POPPCT_RURAL","sc_CTY_Speak_only_English","sc_CTY_White","sc_CTY_Black_or_African_American",
                "ln_CTY_tot_pop","sc_turnout_pct_20")







# Week-by-week
weeks <- c("2020-02-22","2020-02-29",
           "2020-03-03","2020-03-10","2020-03-17","2020-04-07")

resWkly <- list()
for(y in c("pcttw_sanders")) {
  for(d in c("DMA_March10Cases",
             "DMA_March17Cases")) {
    for(fe in "0") {
      
      wkly <- wklyBin <- wklyMatch <- wklyWeight <- wklyMatchBin <- wklyWeightBin <- NULL
      if(grepl("March",d)) {
        wks <- weeks
      } else {
        wks <- weeks[4:6]
      }
      for(week in wks) {
        if(fe == 'stab' & week == '2020-04-07') { next }
        cat(paste0(week,'/',y,'/',d,'/',fe,'\n'))
        if(grepl("county",d) & week == "2020-03-01") { next }
        if(week < as.Date("2020-03-01")) {
          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(.))
        } else {
          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(.))
        }
        tmp$treatBin <- ifelse(tmp[[gsub("sc_|ln_","",d)]] > 0,1,0)
        
        wkly <- bind_rows(wkly,data.frame(t(summary(felm(as.formula(paste0("scale(",y,") ~ scale(",gsub("sc_","",d),") + ",
                                                                           paste(unique(c(covariates,paste0(y,"16"))),collapse = "+"),
                                                                           "| ",fe," | 0 | 0")),tmp))$coefficients[paste0("scale(",gsub("sc_","",d),")"),]),date = week))
        
        wklyBin <- bind_rows(wklyBin,data.frame(t(summary(felm(as.formula(paste0("scale(",y,") ~ treatBin + ",
                                                                                 paste(unique(c(covariates,paste0(y,"16"))),collapse = "+"),
                                                                                 "| ",fe," | 0 | 0")),tmp))$coefficients["treatBin",]),date = week))
        
        if(length(unique(tmp$treatBin)) == 2) { 
          m.out <- try(matchit(formula = as.formula(paste("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
                               data = tmp,
                               method = "nearest",
                               distance = "mahalanobis"))
          if(class(m.out) != "try-error") {
            m.data <- match.data(m.out)
            wklyMatch <- bind_rows(wklyMatch,data.frame(t(summary(felm(as.formula(paste0("scale(",y,") ~ scale(",gsub("sc_","",d),") + ",
                                                                                         paste(c(covariates,paste0(y,"16")),collapse = " + ")," | ",fe," | 0 | ",fe)),
                                                                       m.data,weights = m.data$weights))$coefficients[paste0("scale(",gsub("sc_","",d),")"),]),date = week))
            wklyMatchBin <- bind_rows(wklyMatchBin,data.frame(t(summary(felm(as.formula(paste0("scale(",y,") ~ treatBin + ",
                                                                                               paste(c(covariates,paste0(y,"16")),collapse = " + ")," | ",fe," | 0 | ",fe)),
                                                                             m.data,weights = m.data$weights))$coefficients["treatBin",]),date = week))
          }
          
          W.out <- try(weightit(formula(paste0("treatBin ~ ",paste(c(covariates,paste0(y,"16")),collapse = " + "))),
                                data = tmp, estimand = "ATT", method = "cbps"))
          
          
          if(class(W.out) != "try_error") {
            wklyWeight <- bind_rows(wklyWeight,data.frame(t(summary(felm(as.formula(paste0("scale(",y,") ~ scale(",gsub("sc_","",d),") + ",
                                                                                           paste(c(covariates,paste0(y,"16")),collapse = " + ")," | ",fe," | 0 | ",fe)),
                                                                         tmp,weights = W.out$weights))$coefficients[paste0("scale(",gsub("sc_","",d),")"),]),date = week))
            
            wklyWeightBin <- bind_rows(wklyWeightBin,data.frame(t(summary(felm(as.formula(paste0("scale(",y,") ~ treatBin + ",
                                                                                                 paste(c(covariates,paste0(y,"16")),collapse = " + ")," | ",fe," | 0 | ",fe)),
                                                                               tmp,weights = W.out$weights))$coefficients["treatBin",]),date = week))
          }
        }
      }
      resWkly[[y]][[d]][[fe]]$basic$cont <- wkly
      resWkly[[y]][[d]][[fe]]$basic$bin <- wklyBin
      resWkly[[y]][[d]][[fe]]$matching$cont <- wklyMatch
      resWkly[[y]][[d]][[fe]]$matching$bin <- wklyMatchBin
      resWkly[[y]][[d]][[fe]]$weighting$cont <- wklyWeight
      resWkly[[y]][[d]][[fe]]$weighting$bin <- wklyWeightBin
    }
  }
}


pdf('../Figures/figure5.pdf',width = 7,height = 5)
bind_rows(resWkly$pcttw_sanders$DMA_March17Cases$`0`$basic$cont %>%
                      mutate(type = 'basic',
                             date = as.Date(date) - .25),
                    resWkly$pcttw_sanders$DMA_March17Cases$`0`$weighting$cont %>%
                      mutate(type = 'weighting',
                             date = as.Date(date) + .25)) %>%
  ggplot(aes(x = date,y = Estimate,color = type,fill = type)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  geom_point(size = 1.5) + 
  geom_rect(aes(xmin = date-.25,ymin = Estimate - 1.96*`Std..Error`,
                    xmax = date+.25,ymax = Estimate + 1.96*`Std..Error`),alpha = .5,color = NA) + 
  theme_ridges() + 
  scale_color_manual(values = c('basic' = 'grey50','weighting' = 'grey40'),
                     guide = F) + 
  scale_fill_manual(name = 'Method',values = c('basic' = 'grey70','weighting' = 'grey40'),
                    labels = c('basic' = 'Unweighted OLS','weighting' = 'CBPS Weights')) + 
  xlab('Date') + ylab('Coefficient on March 17 Cases') + 
  theme(legend.position = 'bottom')
dev.off()