-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathweek5.qmd
2626 lines (1385 loc) · 75.3 KB
/
week5.qmd
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Applied microeconometrics"
subtitle: "Weeks 5 and 6 - Differences-in-differences and synthetic control"
author: "Josh Merfeld"
institute: "KDI School"
date: "2024-10-14"
date-format: long
format:
revealjs:
self-contained: true
slide-number: false
progress: false
theme: [serif, custom.scss]
width: 1500
height: 1500*(9/16)
code-copy: true
code-fold: show
code-overflow: wrap
highlight-style: github
execute:
echo: true
warnings: false
message: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, dev = "png") # NOTE: switched to png instead of pdf to decrease size of the resulting pdf
def.chunk.hook <- knitr::knit_hooks$get("chunk")
knitr::knit_hooks$set(chunk = function(x, options) {
x <- def.chunk.hook(x, options)
#ifelse(options$size != "a", paste0("\n \\", "tiny","\n\n", x, "\n\n \\normalsize"), x)
ifelse(options$size != "normalsize", paste0("\n \\", options$size,"\n\n", x, "\n\n \\normalsize"), x)
})
library(tidyverse)
library(kableExtra)
library(fixest)
library(ggpubr)
library(RColorBrewer)
library(haven)
library(fwildclusterboot)
library(modelsummary)
ckdata <- read_csv("week5files/cardkruegerlong.csv")
```
# Introduction
## What are we doing today?
- Canonical differences-in-differences
- Inference
- Wild cluster bootstrap
- Fixed effects vs. random effects
- Bias in two-way fixed effects
# Differences-in-differences
## Differences-in-differences... in the 1800s?
![](week5assets/broadstreet.jpg){width=50% fig-align="center"}
## Differences-in-differences... in the 1800s? (from Cunningham's CI)
![](week5assets/snowmap.jpeg){width=50% fig-align="center"}
## Differences-in-differences
- More commonly referred to as "DID" or "diff-in-diff"
- Classic reference: Card and Krueger (1994)
- Most common method, likely because data requirements are least stringent
- Example in _Mostly Harmless_: offering credit to banks during the Great Depression (Richardson and Troost, 2009)
- Set up: Two different federal reserve banks lent to neighborhood banks in Mississippi
- Atlanta fed favored lending to banks in trouble
- St. Louis fed favored the exact opposite
## Richardson and Troost (2009) - Mississippi dividing line
![](week5assets/mississippi1){width=50% fig-align="center"}
## Did the policy of extra lending save banks?
- Basic idea: compare what happened to Atlanta fed banks (southern Mississippi) with St. Louis fed banks (northern Mississippi)
- Could compare after lending, but what's the assumption here?
. . .
- Assumption: same levels before intervention (very strict assumption)
## In fact, pre-intervention levels are different!
![](week5assets/mississippi4){width=50% fig-align="center"}
## Did the policy of extra lending save banks?
- Instead, compare _changes_ from before to after treatment
- Assumption: parallel trends
- If valid, the fact the districts were different prior to the treatment isn't a problem
## "Parallel trends"
![](week5assets/dd2){width=55% fig-align="center"}
## Why is it "differences in differences"?
- Difference 1: St. Louis post minus St. Louis pre
- Difference 2: Atlanta post minus Atlanta pre
- Difference-in-differences: Difference 2 minus difference 1
## "Differences in differences" graphically
![](week5assets/dd1){width=55% fig-align="center"}
## Parallel trends assumption
- The key assumption in differences-in-differences is the parallel trends assumption
- _If the treated group had not been treated, it would have changed by the same amount ("had the same trend") as the comparison group._
- This is a counterfactual assumption: We cannot explicitly test it
- What can we do instead?
. . .
- We can test trends _before_ treatment
- Or in the case of this article, _after_ treatment!
## Richardson and Troost (2009) - Testing the assumption
![](week5assets/mississippi2.png){width=55% fig-align="center"}
## Estimating diff-in-diff empirically
- Can be estimated in a straightforward regression:
$$ Y_{it} = \beta_0 + \beta_1 TREAT_i + \beta_2 POST_t + \beta_3 (POST_t \times TREAT_i) + \varepsilon_{it} $$
. . .
- $\beta_0$: pre mean for the comparison group
- $\beta_1$: difference in the pre mean between the treated and untreated group
- $\beta_2$: difference in means between the pre and post period for the comparison group
- $\beta_3$: difference-in-differences estimate
- This is the difference in the change from pre to post for the treated group relative to the comparison group
## Card and Krueger (1994) - Minimum wage and employment
```{r dd1, echo = TRUE, eval = TRUE, message = FALSE, warning = TRUE, size = "tiny", out.width = "55%", fig.align = "center"}
ckdata <- read_csv("week5files/cardkruegerlong.csv")
head(ckdata)
# note that state = 1 for NJ and 0 for PA.
# also note that post = 1 for 1993 and 0 for 1992
# NJ is treated group, so state = 1 means treat = 1
```
## Card and Krueger (1994) - Minimum wage and employment
```{r dd1b, echo = TRUE, eval = TRUE, message = FALSE, warning = TRUE, size = "tiny", out.width = "55%", fig.align = "center"}
# looks like fulltime is a character! let's try to make it numeric
ckdata$fulltime_num <- as.numeric(ckdata$fulltime)
ckdata$parttime_num <- as.numeric(ckdata$parttime)
```
## Card and Krueger (1994) - Minimum wage and employment
```{r dd2, echo = TRUE, eval = TRUE, message = FALSE, warning = TRUE, size = "tiny", out.width = "55%", fig.align = "center"}
# said there are NAs in the data, so let's see where they are
ckdata %>% filter(is.na(fulltime_num)) %>% dplyr::select(fulltime, fulltime_num)
# ah, so they are .! those are missing values in Stata, so leave as missing.
```
## Card and Krueger (1994) - Minimum wage and employment
```{r dd3, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
reg1 <- feols(fulltime_num ~ state + post + state:post, data = ckdata, vcov = "HC1")
summary(reg1)
```
## Card and Krueger (1994) - Minimum wage and employment
```{r dd4, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
reg1 <- feols(fulltime_num ~ state + post + state:post, data = ckdata, vcov = "HC1")
reg2 <- feols(parttime_num ~ state + post + state:post, data = ckdata, vcov = "HC1")
table <- etable(reg1, reg2,
# standard errors, digits, fit statistics, put SE below coefficients (the norm)
vcov = "HC1", digits = 3, fitstat = "", se.below = TRUE,
# change significance codes to the norm
signif.code = c("***" = 0.01, "**" = 0.05, "*" = 0.1),
# rename the variables
dict = c("Constant" = "Intercept", "state" = "Treat", "post" = "Post", "state:post" = "Treat x Post"))
table
# drop some rows
table <- table[-c(1:2, 11:nrow(table)),]
# rename columns
colnames(table) <- c("", "Full-time", "Part-time")
```
## Card and Krueger (1994) - Minimum wage and employment
```{r dd5, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
kable(table,
align = "lcc", booktabs = TRUE, linesep = "", escape = FALSE, row.names = FALSE) %>%
column_spec(1,width = "4cm") %>%
column_spec(c(2:3),width = "3cm") %>%
kable_styling() %>%
footnote("* p < 0.1, ** p < 0.05, *** p < 0.01.", general_title = "",
footnote_as_chunk = TRUE
) %>%
footnote("Note: Robust standard errors in parentheses.", general_title = "",
footnote_as_chunk = TRUE
)
```
## Card and Krueger (1994) - Poisson regression!
```{r dd6, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
reg1 <- feglm(fulltime_num ~ state + post + state:post, data = ckdata, vcov = "HC1", family = "poisson")
reg2 <- feglm(parttime_num ~ state + post + state:post, data = ckdata, vcov = "HC1", family = "poisson")
table <- etable(reg1, reg2,
# standard errors, digits, fit statistics, put SE below coefficients (the norm)
vcov = "HC1", digits = 3, fitstat = "", se.below = TRUE,
# change significance codes to the norm
signif.code = c("***" = 0.01, "**" = 0.05, "*" = 0.1),
# rename the variables
dict = c("Constant" = "Intercept", "state" = "Treat", "post" = "Post", "state:post" = "Treat x Post"))
table
# drop some rows
table <- table[-c(1:2, 11:nrow(table)),]
# rename columns
colnames(table) <- c("", "Full-time", "Part-time")
```
## Card and Krueger (1994) - Minimum wage and employment
```{r dd7, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
kable(table, caption = "Poisson regression", # adding a caption
align = "lcc", booktabs = TRUE, linesep = "", escape = FALSE, row.names = FALSE) %>%
column_spec(1,width = "4cm") %>%
column_spec(c(2:3),width = "3cm") %>%
kable_styling() %>%
footnote("* p < 0.1, ** p < 0.05, *** p < 0.01.", general_title = "",
footnote_as_chunk = TRUE
) %>%
footnote("Note: Robust standard errors in parentheses.", general_title = "",
footnote_as_chunk = TRUE
)
```
## Estimating diff-in-diff empirically - adding controls
- Can add control variables
$$ Y_{it} = \beta_0 + \beta_1 TREAT_i + \beta_2 POST_t + \beta_3 (POST_t \times TREAT_i) + X_{it} + \varepsilon_{it} $$
- Adding controls can help control for differing trends ("conditional" parallel trends)
- Note: the interpretation of $\beta_0$ is no longer the same; others stay the same
## Standard errors in differences-in-differences
- Card and Krueger did not cluster standard errors
- In fact, that would have been difficult because they really only had two "clusters"!
- But their robust standard errors are likely underestimated due to the clustering
- Classic reference: Bertrand, Duflo, and Mullainathan (2004)
- "How Much Should We Trust Differences-in-Differences Estimates?"
## Standard errors in differences-in-differences
- Bertrand, Duflo, and Mullainathan (2004) suggest three possibilities:
1. Cluster at the group level
2. Block bootstrap (not going to discuss)
3. Aggregating data into one pre and one post period (event studies later)
- Let's go through these
## Clustering
- The most common approach: cluster standard errors
- Cameron, Gelbach, and Miller (2008) show that this is problematic with few clusters
- "Bootstrap-based improvements for inference with clustered errors"
- The authors look at many possible approaches and find that the "wild cluster bootstrap" seems to perform best, on average
## Wild cluster bootstrap
- The wild cluster bootstrap is a "non-parametric" bootstrap
- I'll do the non-cluster as an example. Software makes this easy!
- Suppose we are interested in the following regression:
$$ y_i = \beta_0 + \beta_1 x_i + \varepsilon_i $$
- We want to test whether $\beta_1 = 0$ and we have relatively few clusters (say between 5 and 30)
## Wild cluster bootstrap
\begin{gather}\label{wcreg} y_i = \beta_0 + \beta_1 x_i + \varepsilon_i \end{gather}
1. Estimate above regression and obtain $\hat{\beta}$, $\hat{\varepsilon}$
2. Impose the null hypothesis ($\beta_1 = 0$) and estimate the restricted regression:
\begin{gather}\label{wcregres} \tilde{y}_i = \tilde{\beta}_0 + \tilde{\varepsilon}_i \end{gather}
## Wild cluster bootstrap
3. Bootstrap replications:
- Use equation \autoref{wcregres} to generate $\tilde{y}_i^b$, where $\tilde{y}_i^b = \tilde{\beta}_0^{b} + \tilde{\varepsilon}_i^{b}$
- Rademacher weights: The randomness comes from either adding $\hat{\varepsilon}_i$ or $-\hat{\varepsilon}_i$ with equal probability
- Estimate:
\begin{gather}\label{wcregB} \tilde{y}_i^{b} = \tilde{\beta}_0^{b*} + \tilde{\beta}_1^{b*}x_i + \tilde{\varepsilon}_i^{b*} \end{gather}
- Calculate the t-statistic for the bootstrap replication:
\begin{gather}\label{wctstat} t^{b*} = \frac{\tilde{\beta}_1^{b*}}{\sqrt{\tilde{V}^{b*}}} \end{gather}
## Wild cluster bootstrap
Two-tailed test:
- Reject the null hypothesis if
$$|t^{b*}| > |t^{*}|\;\mathrm{for}\;b = 1, \dots, B,$$
where $t^{*}$ is the t-statistic from the \textit{original regression}.
- P-value across $B$ bootstrap samples is:
\begin{gather}\frac{1}{B}\sum_{b=1}^B \mathbb{I}(|t^{b*}| > |t^{*}|),\end{gather}
where $\mathbb{I}$ is the indicator function.
## Implementing WCB in `R`
- Thankfully there's a package that allows us to do this!
- `fwildclusterboot` (Friedrich, 2019)
- This package works with `fixest` objects!
- Let's use the `castle.dta` data in the GitHub repo to test this
## Implementing WCB in `R`
```{r wcb1, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
library(haven) # to load .dta files
df <- read_dta("week5files/castle.dta")
head(df)
# key variables: state, year, cdl ("treatment"), and homicide_c (outcome)
# homicide_c to rate (per 100,000 people)
df$homicide_c <- (df$homicide_c/df$population)*100000
# and log
df$homicide_c <- log(df$homicide_c)
```
## Implementing WCB in `R`
```{r wcb2, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
# Note: this is not differences-in-differences.
# Just an example of the wild cluster bootstrap with fwildclusterboot
reg1 <- feols(homicide_c ~ cdl, data = df, cluster = "state")
summary(reg1)
```
## Implementing WCB in `R`
```{r wcb3, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
reg1 <- feols(homicide_c ~ cdl, data = df, cluster = "state")
boot_reg <- boottest(
reg1,
clustid = c("state"),
param = "cdl",
B = 10000,
type = "rademacher" # default weighting, by the way
)
boot_reg
```
## Add controls
```{r wcb4, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
reg1 <- feols(homicide_c ~ cdl + log(population) + log(income) + unemployrt, data = df, cluster = "state")
reg1
boot_reg <- boottest(
reg1,
clustid = c("state"),
param = "cdl",
B = 10000,
type = "rademacher" # default weighting, by the way
)
boot_reg
```
## Can change null hypothesis, like cdl = 0.1
```{r wcb5, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
reg1 <- feols(homicide_c ~ cdl + log(population) + log(income) + unemployrt, data = df, cluster = "state")
(boot_reg <- boottest(
reg1,
clustid = c("state"),
param = "cdl",
r = 0.1, # null hypothesis is cdl = 0.1
B = 10000,
type = "rademacher" # default weighting, by the way
))
```
## Multi-way clustering, too!
```{r wcb6, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
reg1 <- feols(homicide_c ~ cdl + log(population) + log(income) + unemployrt, data = df, cluster = c("state", "year"))
(boot_reg <- boottest(
reg1,
clustid = c("state", "year"),
param = "cdl",
B = 10000,
type = "rademacher" # default weighting, by the way
))
```
## Example with random subset of 12 clusters
```{r wcb7, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
set.seed(13489)
randomclusters <- unique(df$state)[sample(1:length(unique(df$state)), 8, replace = F)]
reg1 <- feols(homicide_c ~ cdl + log(population) + log(income) + unemployrt, data = df[df$state %in% randomclusters,], cluster = c("state", "year"))
(boot_reg <- boottest(
reg1,
clustid = c("state", "year"),
param = "cdl",
B = 10000,
type = "rademacher" # default weighting, by the way
))
```
## Finally, Webb weights (Webb, 2023) -- but using Rademacher weights is the norm
```{r wcb8, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
set.seed(13489)
randomclusters <- unique(df$state)[sample(1:length(unique(df$state)), 8, replace = F)]
reg1 <- feols(homicide_c ~ cdl + log(population) + log(income) + unemployrt, data = df[df$state %in% randomclusters,], cluster = c("state", "year"))
(boot_reg <- boottest(
reg1,
clustid = c("state", "year"),
param = "cdl",
B = 10000,
type = "webb" # webb weights, six-point distribution
))
```
## Some thoughts on clustering
- If you have more than ~30 clusters, you can probably just cluster at the group level
- We see that the standard errors are very similar in the state examples above
- Otherwise, consider using an alternative approach
- Also important when clusters have wildly different sample sizes, or where the treated clusters are relatively few (Moulton, 1990)
- Alternative approach with only one treated cluster: randomization inference
## Randomization inference in Buchmueller, DiNardo, and Valletta (2011)
- They are interested in the change in insurance coverage in Hawaii relative to other states:
\begin{gather} Y_{ist}=X_{ist}\beta^t+Z_{st}\gamma^t+H_{it}\delta^t+\phi_{st}+\eta_{it} \end{gather}
- They calculate the change as: $\Delta=\delta^1-\delta^0$
- The idea: see where the Hawaii effect sits in the distribution of the same effect across *all US states*
- "Placebo" effects
- Note that this is not true "randomization" inference
- I'll show you an example with one of my papers in a minute
## Randomization inference in Buchmueller, DiNardo, and Valletta (2011)
![](week5assets/randomization){fig-align="center"}
## Randomization inference in Merfeld (2023)
- I'm interested in the effects of pollution on agricultural productivity in India
- I have villages, which are nested within districts
- I cluster on villages
- Alternative: randomly assign pollution to villages *within the same district* and compare my effects to the distribution of effects
## Randomization inference in Merfeld (2023)
![](week5assets/randomization_merfeld){fig-align="center" width=25%}
## Placebo tests for the parallel trends assumption
- Above, we looked at the parallel trends assumption graphically in Richardson and Troost (2009)
- Another common way is to look at *leads* of treatment
- In my paper, for example, pollution next year should not affect agricultural productivity this year
## Leads of pollution in Merfeld (2023)
```{r yieldtableleads, include = TRUE, echo = FALSE, message = FALSE, warning = FALSE}
yield3ivmain_lead <- readRDS("week5assets/yield3ivmain_lead.rds")
# Table
colnames(yield3ivmain_lead) <- c("(1)", "(2)")
rownames(yield3ivmain_lead) <- c("particulate matter (one-year lead)", "",
"particulate matter (two-year lead)", "",
"weather (expanded)",
"fixed effects:", "village", "year",
"F",
"observations")
kable(
yield3ivmain_lead,
align = "cc", booktabs = TRUE, linesep = ""
) %>%
column_spec(1, width = "7.4cm") %>%
column_spec(c(2:3),width = "2cm") %>%
row_spec(c(8), hline_after = TRUE) %>%
row_spec(c(6), bold = TRUE) %>%
kable_classic_2() %>%
footnote("* p < 0.1, ** p < 0.05, *** p < 0.01.", general_title = "",
footnote_as_chunk = TRUE
) %>%
footnote("Note: Standard errors are in parentheses and are clustered at the village level.", general_title = "",
footnote_as_chunk = TRUE
)
```
## Convincing the reader is like writing a good story
- When you're writing a diff-in-diff paper, think about the possible threats to your identification strategy
- Then, think about how you can convince the reader that your strategy is valid
- Use placebos: is there somewhere we shouldn't expect an effect?
- In the case of my paper, the leads convinced some seminar participants!
- You can likewise think of heterogeneity we would *expect* to see, and test for that!
# Fixed and random effects
## Before moving on to some of the new literature...
- Let's talk about fixed and random effects
- Fixed effects will be important for the upcoming discussions
- Some nice (but old) slides from Oscar Tores-Reyna [here](https://www.princeton.edu/~otorres/Panel101.pdf).
## Panel data
- Both fixed and random effects are used in panel data
- Panel data: data with multiple observations for each unit
- Examples: individuals, firms, countries, etc.
- In our previous example of homicide and castle doctrine laws: unit is the state!
## Panel data
```{r panel1, echo = FALSE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "75%", fig.align = "center"}
df <- read_dta("week5files/castle.dta")
# homicide_c to rate
df$homicide_c <- (df$homicide_c/df$population)*100000
# and log
df$homicide_c <- log(df$homicide_c)
ggplot(df) +
geom_line(aes(x = year, y = homicide_c, color = state)) +
scale_color_viridis_d() +
theme_bw() +
theme(legend.position = "none") +
labs(x = "Year", y = "Homicide rate (log)", title = "Homicide rate by state and year") +
# and add x labels for each year
scale_x_continuous(breaks = c(2000:2010))
```
## Fixed effects
- Fixed effects are a way to control for omitted variables
- However there is a key assumption: the omitted variable is time-invariant
- Fixed effects are also called "within" effects
- Why? Because we are looking at the variation within each unit
- The regression is of the form:
\begin{gather} y_{it} = \alpha_i + \beta x_{it} + \varepsilon_{it}, \end{gather}
where $\alpha_i$ is the fixed effect for unit $i$. Note the subscript! No $t$!
## Time-invariant assumption
- The key assumption is that the omitted variable is time-invariant
- For example, in the case of the homicide data, we might think that there is some time-invariant variable that affects homicide rates
- However, this is a strong assumption
- Moreover, do you think it is more likely to hold in short or long panels?
. . .
- Nice paper on this by [Millimet and Bellemare (2023)](https://smu.app.box.com/s/wb2313n0vppxng448f6btj9c6tmrk3nt)
## Fixed effects, empirically
- Empirically, what are fixed effects doing?
- They are subtracting the mean of **each unit** from the outcome variable
- In a regression, we add a dummy variable for each unit
- We have to leave out one dummy variable, though
- Software will do this for us!
- Note that the intercept is usually meaningless in this case
- Cannot include time-invariant variables in the regression
- Why? Because the fixed effect will absorb them!
## Fixed effects with feols
- `feols` makes this easy on us. Let's return to our previous example.
```{r fe1, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
reg1 <- feols(homicide_c ~ cdl + log(population) + log(income) + unemployrt, data = df, cluster = c("state"))
reg2 <- feols(homicide_c ~ cdl + log(population) + log(income) + unemployrt | state, data = df, cluster = c("state"))
etable(reg1, reg2,
# standard errors, digits, fit statistics, put SE below coefficients (the norm)
digits = 3, fitstat = "", se.below = TRUE,
# change significance codes to the norm
signif.code = c("***" = 0.01, "**" = 0.05, "*" = 0.1))
```
## Fixed effects with feols
- Notice how different the coefficients are!
```{r fe1b, echo = FALSE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
etable(reg1, reg2,
# standard errors, digits, fit statistics, put SE below coefficients (the norm)
digits = 3, fitstat = "", se.below = TRUE,
# change significance codes to the norm
signif.code = c("***" = 0.01, "**" = 0.05, "*" = 0.1))
```
## Just a quick note: wild cluster bootstrap still works!
```{r fe2, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
# need to use a numeric variable for the bootstrap. sid is in our data, thankfully.
reg1 <- feols(homicide_c ~ cdl + log(population) + log(income) + unemployrt | sid, data = df, cluster = c("sid"))
reg1
boot_reg <- boottest(
reg1,
clustid = c("sid"), # note that it requires a numeric variable!
param = "cdl",
B = 10000
)
boot_reg
```
## Fixed effects are the norm with "differences-in-differences"
- It's not quite the same as the canonical differences-in-differences model
- We redefine treatment for the same unit
- Before treatment, the value is zero, and after it is one
- Note that this is different from the canonical model, where the value is zero for the comparison group and one for the treated group *at all points in time*
- In fact, the regression we just saw is like a differences-in-differences model of this form!
- In practice, we often tend to add time fixed effects, too:
\begin{gather} y_{it} = \alpha_i + \delta_{t} + \beta x_{it} + \varepsilon_{it}, \end{gather}
- This is colloquially called "two-way fixed effects (TWFE)"
## The "effect" of castle doctrine laws, TWFE
```{r fe3, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
reg1 <- feols(homicide_c ~ cdl + log(population) + log(income) + unemployrt | state, data = df, cluster = c("state"))
reg2 <- feols(homicide_c ~ cdl + log(population) + log(income) + unemployrt | state + year, data = df, cluster = c("state"))
etable(reg1, reg2,
# standard errors, digits, fit statistics, put SE below coefficients (the norm)
digits = 3, fitstat = "", se.below = TRUE,
# change significance codes to the norm
signif.code = c("***" = 0.01, "**" = 0.05, "*" = 0.1))
```
## The "effect" of castle doctrine laws, TWFE
```{r fe3b, echo = FALSE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
reg1 <- feols(homicide_c ~ cdl + log(population) + log(income) + unemployrt, data = df, cluster = c("state"))
reg2 <- feols(homicide_c ~ cdl + log(population) + log(income) + unemployrt | state, data = df, cluster = c("state"))
reg3 <- feols(homicide_c ~ cdl + log(population) + log(income) + unemployrt | state + year, data = df, cluster = c("state"))
table <- etable(reg1, reg2, reg3,
# standard errors, digits, fit statistics, put SE below coefficients (the norm)
digits = 3, fitstat = "n", se.below = TRUE, depvar = FALSE,
# change significance codes to the norm
signif.code = c("***" = 0.01, "**" = 0.05, "*" = 0.1),
# rename the variables
dict = c("cdl" = "Castle doctrine law", "log(population)" = "Population (log)", "log(income)" = "Income (log)", "unemployrt" = "Unemp. rate",
"state" = "State", "year" = "Year"))
table <- table[-c(1:2),]
table[9,2:4] <- ""
table <- table[c(1:11, 14),]
colnames(table) <- c("", "(1)", "(2)", "(3)")
kable(table, caption = "CDL laws and homicide rates",
align = "lccc", booktabs = TRUE, linesep = "", escape = FALSE, row.names = FALSE) %>%
column_spec(1,width = "6cm") %>%
column_spec(c(2:4),width = "2.5cm") %>%
row_spec(9, bold = TRUE) %>%
row_spec(11, hline_after = TRUE) %>%
kable_styling() %>%
footnote("* p < 0.1, ** p < 0.05, *** p < 0.01.", general_title = "",
footnote_as_chunk = TRUE
) %>%
footnote("Note: Standard errors clustered at the state level are in parentheses.", general_title = "",
footnote_as_chunk = TRUE
)
```