forked from scunning1975/mixtape_learnr
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Difference_in_Differences.Rmd
386 lines (305 loc) · 12.4 KB
/
Difference_in_Differences.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
---
title: "Causal Inference: <br> *The Mixtape*"
subtitle: "<it>Difference-in-Differences</it>"
output:
learnr::tutorial:
css: css/style.css
highlight: "kate"
runtime: shiny_prerendered
---
## Welcome
This is material for the **Difference-in-Differences** chapter in Scott Cunningham's book, [Causal Inference: The Mixtape.](https://mixtape.scunning.com/)
### Packages needed
The first thing you need to do is install a few packages to make sure everything runs:
```{r, eval = FALSE}
install.packages("tidyverse")
install.packages("cli")
install.packages("haven")
install.packages("rmarkdown")
install.packages("learnr")
install.packages("haven")
install.packages("stargazer")
# This chapter only
install.packages("lfe")
install.packages("bacondecomp")
```
### Load
```{r load, warning=FALSE, message=FALSE}
library(learnr)
library(haven)
library(tidyverse)
library(stargazer)
# This chapter only
library(lfe)
library(bacondecomp)
# 10 minute code time limit
options(tutorial.exercise.timelimit = 600)
# read_data function
read_data <- function(df) {
full_path <- paste0("https://raw.github.com/scunning1975/mixtape/master/", df)
return(haven::read_dta(full_path))
}
```
## Cunningham and Cornwell (2013)
```{r abortion_dd, exercise=TRUE, echo=FALSE}
#-- DD estimate of 15-19 year olds in repeal states vs Roe states
abortion <- read_data("abortion.dta") %>%
mutate(
repeal = as_factor(repeal),
year = as_factor(year),
fip = as_factor(fip),
fa = as_factor(fa),
)
reg <- abortion %>%
filter(bf15 == 1) %>%
lm_robust(lnr ~ repeal*year + fip + acc + ir + pi + alcohol+ crack + poverty+ income+ ur,
data = ., weights = totpop, clusters = fip)
abortion_plot <- tibble(
sd = reg$std.error[-1:-75],
mean = reg$coefficients[-1:-75],
year = c(1986:2000))
abortion_plot %>%
ggplot(aes(x = year, y = mean)) +
geom_rect(aes(xmin=1985, xmax=1992, ymin=-Inf, ymax=Inf), fill = "cyan", alpha = 0.01)+
geom_point()+
geom_text(aes(label = year), hjust=-0.002, vjust = -0.03)+
geom_hline(yintercept = 0) +
geom_errorbar(aes(ymin = mean - sd*1.96, ymax = mean + sd*1.96), width = 0.2,
position = position_dodge(0.05)) +
labs(title= "Estimated effect of abortion legalization on gonorrhea")
```
#### Questions
- Describe in your own words the testable predictions from the roll out of repeal versus Roe in the population? In other words, describe the behavior of the DD coefficients under this regression.
- Do we find evidence consistent with this in our DD analysis? List all the evidence for and against the hypothesis in this analysis.
- Does it appear that there was an effect in the period where Roe has not fully caught up?
```{r abortion_ddd, exercise=TRUE, echo=FALSE}
abortion <- read_data("abortion.dta") %>%
mutate(
repeal = as_factor(repeal),
year = as_factor(year),
fip = as_factor(fip),
fa = as_factor(fa),
younger = as_factor(younger),
yr = as_factor(case_when(repeal == 1 & younger == 1 ~ 1, TRUE ~ 0)),
wm = as_factor(case_when(wht == 1 & male == 1 ~ 1, TRUE ~ 0)),
wf = as_factor(case_when(wht == 1 & male == 0 ~ 1, TRUE ~ 0)),
bm = as_factor(case_when(wht == 0 & male == 1 ~ 1, TRUE ~ 0)),
bf = as_factor(case_when(wht == 0 & male == 0 ~ 1, TRUE ~ 0))
) %>%
filter(bf == 1 & (age == 15 | age == 25))
regddd <- lm_robust(lnr ~ repeal*year + younger*repeal + younger*year + yr*year + fip*t + acc + ir + pi + alcohol + crack + poverty + income + ur,
data = abortion, weights = totpop, clusters = fip)
abortion_plot <- tibble(
sd = regddd$std.error[110:124],
mean = regddd$coefficients[110:124],
year = c(1986:2000))
abortion_plot %>%
ggplot(aes(x = year, y = mean)) +
geom_rect(aes(xmin=1986, xmax=1992, ymin=-Inf, ymax=Inf), fill = "cyan", alpha = 0.01)+
geom_point()+
geom_text(aes(label = year), hjust=-0.002, vjust = -0.03)+
geom_hline(yintercept = 0) +
geom_errorbar(aes(ymin = mean-sd*1.96, ymax = mean+sd*1.96), width = 0.2,
position = position_dodge(0.05)) +
labs(
title= "Estimated effect of abortion legalization on gonorrhea",
subtitle= "Black female 15-19 year-olds vs. Black female 25-29 year-olds")
```
#### Questions
- Why did we implement a triple difference? What problems does this solve and to what degree do you feel it is a necessary check?
- Describe the evidence for and against the abortion selection hypothesis when using triple difference? How is it consistent with our DD and how is it not?
```{r abortion_dd2, exercise=TRUE, echo=FALSE}
abortion <- read_data("abortion.dta") %>%
mutate(
repeal = as_factor(repeal),
year = as_factor(year),
fip = as_factor(fip),
fa = as_factor(fa),
)
reg <- abortion %>%
filter(race == 2 & sex == 2 & age == 20) %>%
lm_robust(lnr ~ repeal*year + fip + acc + ir + pi + alcohol+ crack + poverty+ income+ ur,
data = ., weights = totpop, clusters = fip)
reg
```
```{r abortion_ddd2, exercise=TRUE, echo=FALSE}
abortion <- read_data("abortion.dta") %>%
mutate(
repeal = as_factor(repeal),
year = as_factor(year),
fip = as_factor(fip),
fa = as_factor(fa),
younger2 = case_when(age == 20 ~ 1, TRUE ~ 0),
yr2 = as_factor(case_when(repeal == 1 & younger2 == 1 ~ 1, TRUE ~ 0)),
wm = as_factor(case_when(wht == 1 & male == 1 ~ 1, TRUE ~ 0)),
wf = as_factor(case_when(wht == 1 & male == 0 ~ 1, TRUE ~ 0)),
bm = as_factor(case_when(wht == 0 & male == 1 ~ 1, TRUE ~ 0)),
bf = as_factor(case_when(wht == 0 & male == 0 ~ 1, TRUE ~ 0))
)
regddd <- abortion %>%
filter(bf == 1 & (age == 20 | age ==25)) %>%
lm_robust(lnr ~ repeal*year + acc + ir + pi + alcohol + crack + poverty + income + ur,
data = ., weights = totpop, clusters = fip)
regddd
```
#### Questions
- Why did we suggest that conducting this additional analysis not conducted in the original study?
- How convinced are you now of the abortion selection hypothesis? Why/why not?
- Could you have concluded this had you not exploited all of the testable predictions of the original table showing roll out across cohort and time?
- How important was our ``model`` to forming testable predictions and falsifications?
## Cheng and Hoekstra (2013)
```{r castle_1, exercise=TRUE, echo=FALSE}
castle <- read_data("castle.dta")
#--- global variables
crime1 <- c("jhcitizen_c", "jhpolice_c",
"murder", "homicide",
"robbery", "assault", "burglary",
"larceny", "motor", "robbery_gun_r")
demo <- c("emo", "blackm_15_24", "whitem_15_24",
"blackm_25_44", "whitem_25_44")
# variables dropped to prevent colinearity
dropped_vars <- c("r20004", "r20014",
"r20024", "r20034",
"r20044", "r20054",
"r20064", "r20074",
"r20084", "r20094",
"r20101", "r20102", "r20103",
"r20104", "trend_9", "trend_46",
"trend_49", "trend_50", "trend_51"
)
lintrend <- castle %>%
select(starts_with("trend")) %>%
colnames %>%
# remove due to colinearity
subset(.,! . %in% dropped_vars)
region <- castle %>%
select(starts_with("r20")) %>%
colnames %>%
# remove due to colinearity
subset(.,! . %in% dropped_vars)
exocrime <- c("l_lacerny", "l_motor")
spending <- c("l_exp_subsidy", "l_exp_pubwelfare")
xvar <- c(
"blackm_15_24", "whitem_15_24", "blackm_25_44", "whitem_25_44",
"l_exp_subsidy", "l_exp_pubwelfare",
"l_police", "unemployrt", "poverty",
"l_income", "l_prisoner", "l_lagprisoner"
)
law <- c("cdl")
dd_formula <- as.formula(
paste("l_homicide ~ ",
paste(
paste(xvar, collapse = " + "),
paste(region, collapse = " + "),
paste(lintrend, collapse = " + "),
paste("post", collapse = " + "), sep = " + "),
"| year + sid | 0 | sid"
)
)
#Fixed effect regression using post as treatment variable
dd_reg <- lfe::felm(dd_formula, weights = castle$popwt, data = castle)
summary(dd_reg)
```
#### QUESTIONS
- What effect does this analysis say reforming castle doctrine laws has on homicides?
- What are the key parts of these legislative reforms that you think may be causing this result?
- Explain what SUTVA requires in order for these estimates to be causal?
- Assume there are spillovers to neighboring states created by castle doctrine reforms. Does that imply that Cheng and Hoekstra's result is too large or too small? Why/why not?
```{r castle_2, exercise=TRUE, echo=FALSE}
castle <- castle %>%
mutate(
time_til = year - treatment_date,
lead1 = case_when(time_til == -1 ~ 1, TRUE ~ 0),
lead2 = case_when(time_til == -2 ~ 1, TRUE ~ 0),
lead3 = case_when(time_til == -3 ~ 1, TRUE ~ 0),
lead4 = case_when(time_til == -4 ~ 1, TRUE ~ 0),
lead5 = case_when(time_til == -5 ~ 1, TRUE ~ 0),
lead6 = case_when(time_til == -6 ~ 1, TRUE ~ 0),
lead7 = case_when(time_til == -7 ~ 1, TRUE ~ 0),
lead8 = case_when(time_til == -8 ~ 1, TRUE ~ 0),
lead9 = case_when(time_til == -9 ~ 1, TRUE ~ 0),
lag0 = case_when(time_til == 0 ~ 1, TRUE ~ 0),
lag1 = case_when(time_til == 1 ~ 1, TRUE ~ 0),
lag2 = case_when(time_til == 2 ~ 1, TRUE ~ 0),
lag3 = case_when(time_til == 3 ~ 1, TRUE ~ 0),
lag4 = case_when(time_til == 4 ~ 1, TRUE ~ 0),
lag5 = case_when(time_til == 5 ~ 1, TRUE ~ 0)
)
event_study_formula <- as.formula(
paste("l_homicide ~ + ",
paste(
paste(region, collapse = " + "),
paste(paste("lead", 1:9, sep = ""), collapse = " + "),
paste(paste("lag", 1:5, sep = ""), collapse = " + "), sep = " + "),
"| year + state | 0 | sid"
),
)
event_study_reg <- lfe::felm(event_study_formula, weights = castle$popwt, data = castle)
summary(event_study_reg)
```
#### QUESTIONS
- Put into your own words why we estimated the pre-treatment leads?
- Put into your own words what we expected to find?
- How convinced are you by this analysis that parallel trends was likely to hold in Cheng and Hoekstra's data?
```{r castle_3, exercise=TRUE, echo=FALSE}
castle <- castle %>%
mutate(
time_til = year - treatment_date,
lead1 = case_when(time_til == -1 ~ 1, TRUE ~ 0),
lead2 = case_when(time_til == -2 ~ 1, TRUE ~ 0),
lead3 = case_when(time_til == -3 ~ 1, TRUE ~ 0),
lead4 = case_when(time_til == -4 ~ 1, TRUE ~ 0),
lead5 = case_when(time_til == -5 ~ 1, TRUE ~ 0),
lead6 = case_when(time_til == -6 ~ 1, TRUE ~ 0),
lead7 = case_when(time_til == -7 ~ 1, TRUE ~ 0),
lead8 = case_when(time_til == -8 ~ 1, TRUE ~ 0),
lead9 = case_when(time_til == -9 ~ 1, TRUE ~ 0),
lag0 = case_when(time_til == 0 ~ 1, TRUE ~ 0),
lag1 = case_when(time_til == 1 ~ 1, TRUE ~ 0),
lag2 = case_when(time_til == 2 ~ 1, TRUE ~ 0),
lag3 = case_when(time_til == 3 ~ 1, TRUE ~ 0),
lag4 = case_when(time_til == 4 ~ 1, TRUE ~ 0),
lag5 = case_when(time_til == 5 ~ 1, TRUE ~ 0)
)
event_study_formula <- as.formula(
paste("l_homicide ~ + ",
paste(
paste(region, collapse = " + "),
paste(paste("lead", 1:9, sep = ""), collapse = " + "),
paste(paste("lag", 1:5, sep = ""), collapse = " + "), sep = " + "),
"| year + state | 0 | sid"
),
)
event_study_reg <- lfe::felm(event_study_formula, weights = castle$popwt, data = castle)
# order of the coefficients for the plot
plot_order <- c("lead9", "lead8", "lead7",
"lead6", "lead5", "lead4", "lead3",
"lead2", "lead1", "lag1",
"lag2", "lag3", "lag4", "lag5")
# grab the clustered standard errors
# and average coefficient estimates
# from the regression, label them accordingly
# add a zero'th lag for plotting purposes
leadslags_plot <- tibble(
sd = c(event_study_reg$cse[plot_order], 0),
mean = c(coef(event_study_reg)[plot_order], 0),
label = c(-9,-8,-7,-6, -5, -4, -3, -2, -1, 1,2,3,4,5, 0)
)
# This version has a point-range at each
# estimated lead or lag
# comes down to stylistic preference at the
# end of the day!
leadslags_plot %>%
ggplot(aes(x = label, y = mean,
ymin = mean-1.96*sd,
ymax = mean+1.96*sd)) +
geom_hline(yintercept = 0.035169444, color = "red") +
geom_pointrange() +
theme_minimal() +
xlab("Years before and after castle doctrine expansion") +
ylab("log(Homicide Rate)") +
geom_hline(yintercept = 0,
linetype = "dashed") +
geom_vline(xintercept = 0,
linetype = "dashed")
```