-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path1b. TWFE + binary DiD.R
733 lines (666 loc) · 29.7 KB
/
1b. TWFE + binary DiD.R
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
library(tidyverse)
library(RPostgres)
library(fixest)
library(e1071)
library(kableExtra)
library(ggthemes)
library(patchwork)
library(did)
library(furrr)
library(latex2exp)
library(bacondecomp)
library(ggforce)
library(fastDummies)
library(progressr)
# set plot theme
theme_set(theme_clean() + theme(plot.background = element_blank(),
legend.background = element_blank()))
# load in compustat data
comp <- read_rds(here::here("Data", "simulation_data.rds"))
# estimate the fixed effects regression of ROA on firm and year fixed effects
mod <- feols(roa ~ 1 | gvkey + fyear, cluster = "incorp", data = comp)
# get the moments for the residuals from the baseline model
resid_sd <- sd(mod$residuals)
resid_skew <- skewness(mod$residuals)
resid_kurtosis <- kurtosis(mod$residuals)
# get firm and years and state of incorporation
shell <- comp %>% select(gvkey, fyear)
# get the firm and year fes, as well as the standard deviation of ROA
firm_fes <- fixef(mod)$gvkey
n_firm_fes <- length(fixef(mod)$gvkey)
year_fes <- fixef(mod)$fyear
n_year_fes <- length(fixef(mod)$fyear)
sd_roa <- sd(comp$roa)
# function to run simulation, pull firm and year FE, as well as the residuals from their empirical distributions
# then add in treatment effects following DGP in our six simulations
run_sim <- function(i, p) {
p()
# pull firm FEfrom empirical distribution with replacement,
# also uniformly assign state of incorporation
sim_firm_fe <- tibble(
gvkey = unique(shell$gvkey),
firm_fe = sample(firm_fes, n_firm_fes, replace = TRUE),
incorp = sample(state.abb, n_firm_fes, replace = TRUE)
)
# pull year FE from the empirical distribution with replacement
sim_year_fe <- tibble(
fyear = unique(shell$fyear),
year_fe = sample(year_fes, n_year_fes, replace = TRUE)
)
# merge in the FE to the firm/year observations and add in residuals from the
# empirical distribution. ROA is the linear combination of the FEs and the residual
data <- shell %>%
left_join(sim_firm_fe, by = "gvkey") %>%
left_join(sim_year_fe, by = "fyear") %>%
mutate(resid = sample(mod$residuals, length(mod$residuals), replace = TRUE),
roa = firm_fe + year_fe + resid)
# save the moments of the residuals from this dataset
sim_mod <- feols(roa ~ 1 | gvkey + fyear, cluster = "incorp", data = data)
mom <- c(sd(sim_mod$residuals), skewness(sim_mod$residuals), kurtosis(sim_mod$residuals))
# randomly assign the state of incorporation into treatment groups
# put random states into a vector
random_states <- sample(state.abb, length(state.abb), replace = FALSE)
# now add in the treatment effect - One Treatment Period, Constant Treatment Effects
data1 <- data %>%
mutate(
# figure out treatment group based on random ordering of states of incorporation
group = case_when(
incorp %in% random_states[1:25] ~ "T",
incorp %in% random_states[26:50] ~ "C"),
# add in treatment effects - constant half of a standard deviation of ROA
delta = case_when(fyear >= 1998 & group == "T" ~ 0.5*sd_roa,
TRUE ~ 0),
# new ROA is the sum of the old ROA and the treatment effect
treat_roa = roa + delta,
# make indicator variable for obs when treatment is turned on for the TWFE regs
treat = ifelse(group == "T" & fyear >= 1998, 1, 0),
# make a relative-to-treatment year variable
rel_year = fyear - 1998,
# get a first treat variable for CS
first_treat = if_else(group == "T", 1998, 0))
# One Treatment Period, Dynamic Treatment Effects
data2 <- data %>%
mutate(
# figure out treatment group based on random ordering of states of incorporation
group = case_when(
incorp %in% random_states[1:25] ~ "T",
incorp %in% random_states[26:50] ~ "C"),
# add in treatment effects - percentage of standard deviation of ROA added per year
delta_base = case_when(fyear >= 1998 & group == "T" ~ 0.05*sd_roa,
TRUE ~ 0),
# true treatment effect is the cumulative sum of this - dynamic trend break treatment effect
delta = delta_base * (fyear - 1998 + 1),
# new ROA is the sum of the old ROA and the treatment effect
treat_roa = roa + delta,
# make indicator variable for obs when treatment is turned on for the TWFE regs
treat = ifelse(group == "T" & fyear >= 1998, 1, 0),
# make a relative-to-treatment year variable
rel_year = fyear - 1998,
# get a first treat variable for CS
first_treat = if_else(group == "T", 1998, 0))
# Multiple Treatment Periods and Constant Equal Treatment Effects
data3 <- data %>%
mutate(
# figure out treatment group based on random ordering of states of incorporation
group = case_when(
incorp %in% random_states[1:17] ~ 1989,
incorp %in% random_states[18:35] ~ 1998,
incorp %in% random_states[36:50] ~ 2007
),
# add in treatment effects - half a percentage of standard deviation of ROA
delta = case_when(
fyear >= group & group == 1989 ~ .5*sd_roa,
fyear >= group & group == 1998 ~ .5*sd_roa,
fyear >= group & group == 2007 ~ .5*sd_roa,
TRUE ~ 0
),
# new ROA is the sum of the old ROA and the treatment effect
treat_roa = roa + delta,
# make indicator variable for obs when treatment is turned on for the TWFE regs
treat = ifelse(fyear >= group, 1, 0),
# make a relative-to-treatment year variable
rel_year = fyear - group,
# get a first treat variable for CS
first_treat = group)
# Multiple Treatment Periods and Constant Different Treatment Effects
data4 <- data %>%
mutate(
# figure out treatment group based on random ordering of states of incorporation
group = case_when(
incorp %in% random_states[1:17] ~ 1989,
incorp %in% random_states[18:35] ~ 1998,
incorp %in% random_states[36:50] ~ 2007
),
# add in treatment effects - varying percentage of standard deviation of ROA
delta = case_when(
fyear >= group & group == 1989 ~ .5*sd_roa,
fyear >= group & group == 1998 ~ .3*sd_roa,
fyear >= group & group == 2007 ~ .1*sd_roa,
TRUE ~ 0
),
# new ROA is the sum of the old ROA and the treatment effect
treat_roa = roa + delta,
# make indicator variable for obs when treatment is turned on for the TWFE regs
treat = ifelse(fyear >= group, 1, 0),
# make a relative-to-treatment year variable
rel_year = fyear - group,
# get a first treat variable for CS
first_treat = group)
# Multiple Treatment Periods and Dynamic Equal Treatment Effects
data5 <- data %>%
mutate(
# figure out treatment group based on random ordering of states of incorporation
group = case_when(
incorp %in% random_states[1:17] ~ 1989,
incorp %in% random_states[18:35] ~ 1998,
incorp %in% random_states[36:50] ~ 2007
),
# add in treatment effects - 3% of standard deviation of ROA added per year
delta_base = case_when(
fyear >= group & group == 1989 ~ .03*sd_roa,
fyear >= group & group == 1998 ~ .03*sd_roa,
fyear >= group & group == 2007 ~ .03*sd_roa,
TRUE ~ 0
),
# true treatment effect is the cumulative sum of this - dynamic trend break treatment effect
delta = delta_base * (fyear - group + 1),
# new ROA is the sum of the old ROA and the treatment effect
treat_roa = roa + delta,
# make indicator variable for obs when treatment is turned on for the TWFE regs
treat = ifelse(fyear >= group, 1, 0),
# make a relative-to-treatment year variable
rel_year = fyear - group,
# get a first treat variable for CS
first_treat = group)
# Multiple Treatment Periods and Dynamic Unequal Treatment Effects
data6 <- data %>%
mutate(
# figure out treatment group based on random ordering of states of incorporation
group = case_when(
incorp %in% random_states[1:17] ~ 1989,
incorp %in% random_states[18:35] ~ 1998,
incorp %in% random_states[36:50] ~ 2007
),
# add in treatment effects - varying percent of standard deviation of ROA added per year
delta_base = case_when(
fyear >= group & group == 1989 ~ .05*sd_roa,
fyear >= group & group == 1998 ~ .03*sd_roa,
fyear >= group & group == 2007 ~ .01*sd_roa,
TRUE ~ 0
),
# true treatment effect is the cumulative sum of this - dynamic trend break treatment effect
delta = delta_base * (fyear - group + 1),
# new ROA is the sum of the old ROA and the treatment effect
treat_roa = roa + delta,
# make indicator variable for obs when treatment is turned on for the TWFE regs
treat = ifelse(fyear >= group, 1, 0),
# make a relative-to-treatment year variable
rel_year = fyear - group,
# get a first treat variable for CS
first_treat = group)
# make function to get estimates and treatment effects from data for k in 1:6
get_est <- function(k) {
# load in k-specific data
dt <- get(paste0("data", k))
# get values
# full treatment effect
# observation level
full_te_1 <- dt %>% filter(treat == 1) %>% pull(delta) %>% mean()
# firm level average
full_te_2 <- dt %>% filter(treat == 1) %>% group_by(gvkey) %>%
summarize(m = mean(delta)) %>% pull(m) %>% mean()
# full treatment effects with no 2007 treatment group
full_te_no_2007_1 <- dt %>% filter(treat == 1 & first_treat < 2007) %>% pull(delta) %>% mean()
full_te_no_2007_2 <- dt %>% filter(treat == 1 & first_treat < 2007) %>%
group_by(gvkey) %>% summarize(m = mean(delta)) %>% pull(m) %>% mean()
# treatment effect for just years 1 - 5 after treatment
te_5_1 <- dt %>% filter(treat == 1 & rel_year %in% 0:5 & first_treat < 2007) %>%
pull(delta) %>% mean()
te_5_2 <- dt %>% filter(treat == 1 & rel_year %in% 0:5 & first_treat < 2007) %>%
group_by(gvkey) %>% summarize(m = mean(delta)) %>% pull(m) %>% mean()
# get twfe estimates on full data
twfe <- feols(treat_roa ~ treat | gvkey + fyear, cluster = "incorp", data = dt)$coefficients[1]
# get CS estimates
# first full the full set of attgts
CS_out <- att_gt(yname = "treat_roa",
data = dt,
gname = "first_treat",
idname = "gvkey",
tname = "fyear",
bstrap = F,
cband = F,
est_method = "reg",
control_group = "notyettreated",
print_details = F,
panel = TRUE,
allow_unbalanced_panel = TRUE)
# aggregate by group and then take average over number in groups
cs_aggte_group <- aggte(CS_out, type = "group", min_e = 0, max_e = 5, bstrap = FALSE, cband = FALSE)
cs <- weighted.mean(
cs_aggte_group$att.egt,
dt %>%
filter(treat == 1 & rel_year %in% 0:5 & first_treat < 2007) %>%
group_by(first_treat) %>%
summarize(n = length(unique(gvkey))) %>%
arrange(first_treat) %>%
pull(n)
)
# Stacked regressions
# first make the stacked datasets
# get the treatment cohorts
cohorts <- dt %>%
# drop never treateds, and also 2007 when everyone is treated
filter(!(first_treat %in% c(0, 2007))) %>%
pull(first_treat) %>%
unique()
# make formula to create the dataset
getdata <- function(j) {
#keep what we need
dt %>%
# keep treated units and all units not treated within -5 to 5
filter(first_treat == j | first_treat == 0 | first_treat > j + 5) %>%
# keep just year -5 to 5
filter(fyear >= j - 5 & fyear <= j + 5) %>%
# create an indicator for the dataset
mutate(df = j)
}
# get data stacked
stacked_data <- map_df(cohorts, getdata)
# get stacked value
stacked <- feols(treat_roa ~ treat | gvkey^df + fyear^df, data = stacked_data)$coefficients[1]
# finally get the sun and abraham value
# need to make a dataset without observations more than 5 years after treatment
sa_data <- dt %>%
filter(treat == 0 | rel_year <= 5) %>%
filter(fyear < 2007)
# run SA model through the fixest package
sun_ab <- feols(treat_roa ~ 1 + sunab(first_treat, fyear) | gvkey + fyear, sa_data)
# get the overall att
sa <- summary(sun_ab, agg = "att")$coeftable[1]
tibble(sim = i, dt = k, full_te_1 = full_te_1, full_te_2 = full_te_2, full_te_no_2007_1 = full_te_no_2007_1,
full_te_no_2007_2 = full_te_no_2007_2, te_5_1 = te_5_1, te_5_2 = te_5_2,
twfe = twfe, cs = cs, stacked = stacked, sa = sa)
}
# run it for our six simulations and store results in a dataset
estimates <- map_dfr(1:6, get_est)
# get moments into a tibble as well
moments <- tibble(
moment = 1:3,
value = mom
) %>%
mutate(sim = i)
# output a list of both objects that we want
list(moments = moments,
estimates = estimates)
}
# parallelize and do 500 simulations
x <- 1:500
options(future.globals.maxSize= 891289600)
set.seed(28101695)
plan(multisession, workers = 6)
with_progress({
p <- progressor(steps = length(x))
out <- future_map(
.x = x,
.f = run_sim,
p = p,
.options = furrr_options(globals = c("mod", "shell", "firm_fes", "n_firm_fes",
"year_fes", "n_year_fes", "sd_roa"),
packages = c("tidyverse", "fixest", "e1071", "did", "fastDummies"),
seed = TRUE)
)})
# unpack the two datasets
moments <- do.call(function(...) mapply(bind_rows, ..., SIMPLIFY=F), args = out)$moments
estimates <- do.call(function(...) mapply(bind_rows, ..., SIMPLIFY=F), args = out)$estimates
# save a version
saveRDS(moments, here::here("Data", "moments.rds"))
saveRDS(estimates, here::here("Data", "estimates.rds"))
## Next - do one pull of the simulation to plot the group means
# pull firm FE from empirical distribution with replacement
set.seed(28101695)
sim_firm_fe <- tibble(
gvkey = unique(shell$gvkey),
firm_fe = sample(firm_fes, n_firm_fes, replace = TRUE),
incorp = sample(state.abb, n_firm_fes, replace = TRUE)
)
# pull year FE from the empirical distribution with replacement
sim_year_fe <- tibble(
fyear = unique(comp$fyear),
year_fe = sample(year_fes, n_year_fes, replace = TRUE)
)
# merge in the FE to the firm/year/state observations and add in residuals from the
# empirical distribution. ROA is the linear combination of the FEs and the residual
data <- shell %>%
left_join(sim_firm_fe, by = "gvkey") %>%
left_join(sim_year_fe, by = "fyear") %>%
mutate(resid = sample(mod$residuals, length(mod$residuals), replace = TRUE),
roa = firm_fe + year_fe + resid)
# randomly assign the state of incorporation into treatment groups
random_states <- sample(state.abb, length(state.abb), replace = FALSE)
# One Treatment Period, Constant Treatment Effects
data1 <- data %>%
mutate(
# figure out treatment group based on random ordering of states of incorporation
group = case_when(
incorp %in% random_states[1:25] ~ "T",
incorp %in% random_states[26:50] ~ "C"),
# add in treatment effects - percentage of standard deviation of ROA added per year
delta = case_when(fyear >= 1998 & group == "T" ~ 0.5*sd_roa,
TRUE ~ 0),
# new ROA is the sum of the old ROA and the treatment effect
treat_roa = roa + delta,
# make indicator variable for obs when treatment is turned on for the TWFE regs
treat = ifelse(group == "T" & fyear >= 1998, 1, 0),
dt = "Simulation 1")
# One Treatment Period, Dynamic Treatment Effects
data2 <- data %>%
mutate(
# figure out treatment group based on random ordering of states of incorporation
group = case_when(
incorp %in% random_states[1:25] ~ "T",
incorp %in% random_states[26:50] ~ "C"),
# add in treatment effects - percentage of standard deviation of ROA added per year
delta_base = case_when(fyear >= 1998 & group == "T" ~ 0.05*sd_roa,
TRUE ~ 0),
# true treatment effect is the cumulative sum of this - dynamic trend break treatment effect
delta = delta_base * (fyear - 1998 + 1),
# new ROA is the sum of the old ROA and the treatment effect
treat_roa = roa + delta,
# make indicator variable for obs when treatment is turned on for the TWFE regs
treat = ifelse(group == "T" & fyear >= 1998, 1, 0),
dt = "Simulation 2")
# Multiple Treatment Periods and Constant Equal Treatment Effects
data3 <- data %>%
mutate(
# figure out treatment group based on random ordering of states of incorporation
group = case_when(
incorp %in% random_states[1:17] ~ 1989,
incorp %in% random_states[18:35] ~ 1998,
incorp %in% random_states[36:50] ~ 2007
),
# add in treatment effects - percentage of standard deviation of ROA added per year
delta = case_when(
fyear >= group & group == 1989 ~ .5*sd_roa,
fyear >= group & group == 1998 ~ .5*sd_roa,
fyear >= group & group == 2007 ~ .5*sd_roa,
TRUE ~ 0
),
# new ROA is the sum of the old ROA and the treatment effect
treat_roa = roa + delta,
# make indicator variable for obs when treatment is turned on for the TWFE regs
treat = ifelse(fyear >= group, 1, 0),
group = as.character(group),
dt = "Simulation 3")
# Multiple Treatment Periods and Constant Different Treatment Effects
data4 <- data %>%
mutate(
# figure out treatment group based on random ordering of states of incorporation
group = case_when(
incorp %in% random_states[1:17] ~ 1989,
incorp %in% random_states[18:35] ~ 1998,
incorp %in% random_states[36:50] ~ 2007
),
# add in treatment effects - percentage of standard deviation of ROA added per year
delta = case_when(
fyear >= group & group == 1989 ~ .5*sd_roa,
fyear >= group & group == 1998 ~ .3*sd_roa,
fyear >= group & group == 2007 ~ .1*sd_roa,
TRUE ~ 0
),
# new ROA is the sum of the old ROA and the treatment effect
treat_roa = roa + delta,
# make indicator variable for obs when treatment is turned on for the TWFE regs
treat = ifelse(fyear >= group, 1, 0))
# Multiple Treatment Periods and Dynamic Equal Treatment Effects
data5 <- data %>%
mutate(
# figure out treatment group based on random ordering of states of incorporation
group = case_when(
incorp %in% random_states[1:17] ~ 1989,
incorp %in% random_states[18:35] ~ 1998,
incorp %in% random_states[36:50] ~ 2007
),
# add in treatment effects - percentage of standard deviation of ROA added per year
delta_base = case_when(
fyear >= group & group == 1989 ~ .03*sd_roa,
fyear >= group & group == 1998 ~ .03*sd_roa,
fyear >= group & group == 2007 ~ .03*sd_roa,
TRUE ~ 0
),
# true treatment effect is the cumulative sum of this - dynamic trend break treatment effect
delta = delta_base * (fyear - group + 1),
# new ROA is the sum of the old ROA and the treatment effect
treat_roa = roa + delta,
# make indicator variable for obs when treatment is turned on for the TWFE regs
treat = ifelse(fyear >= group, 1, 0))
# Multiple Treatment Periods and Unequal Dynamic Treatment Effects
data6 <- data %>%
mutate(
# figure out treatment group based on random ordering of states of incorporation
group = case_when(
incorp %in% random_states[1:17] ~ 1989,
incorp %in% random_states[18:35] ~ 1998,
incorp %in% random_states[36:50] ~ 2007
),
# add in treatment effects - percentage of standard deviation of ROA added per year
delta_base = case_when(
fyear >= group & group == 1989 ~ .05*sd_roa,
fyear >= group & group == 1998 ~ .03*sd_roa,
fyear >= group & group == 2007 ~ .01*sd_roa,
TRUE ~ 0
),
# true treatment effect is the cumulative sum of this - dynamic trend break treatment effect
delta = delta_base * (fyear - group + 1),
# new ROA is the sum of the old ROA and the treatment effect
treat_roa = roa + delta,
# make indicator variable for obs when treatment is turned on for the TWFE regs
treat = ifelse(fyear >= group, 1, 0))
# plot for simulation 1
sim1_means <- data1 %>%
ggplot(aes(x = fyear, y = treat_roa, group = gvkey)) +
# unit specific lines
geom_line(alpha = 1/30, color = "grey") +
# group specific averages
geom_line(
data = . %>%
group_by(group, fyear) %>%
summarize(treat_roa = mean(treat_roa)),
aes(x = fyear, y = treat_roa, group = factor(group),
color = factor(group)), size = 1) +
labs(x = "", y = "ROA", color = "Group") +
geom_vline(xintercept = 1997.5, color = '#4B5F6C',
linetype = "dashed", size = 1) +
scale_y_continuous(limits = c(-0.5*sd_roa, 1.5*sd_roa)) +
scale_color_manual(values = c("#A7473A", "#4B5F6C")) +
ggtitle("Simulation 1") +
labs(subtitle = expression(paste("Not Staggered + Constant ", delta))) +
theme(legend.position = 'bottom',
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.title.y = element_text(hjust = 0.5, vjust = 0.5, angle = 360))
# plot for simulation 2
sim2_means <- data2 %>%
ggplot(aes(x = fyear, y = treat_roa, group = gvkey)) +
# unit specific lines
geom_line(alpha = 1/30, color = "grey") +
# group specific averages
geom_line(
data = . %>%
group_by(group, fyear) %>%
summarize(treat_roa = mean(treat_roa)),
aes(x = fyear, y = treat_roa, group = factor(group),
color = factor(group)), size = 1) +
labs(x = "", y = "", color = "Group") +
geom_vline(xintercept = 1997.5, color = '#4B5F6C',
linetype = "dashed", size = 1) +
scale_color_manual(values = c("#A7473A", "#4B5F6C")) +
scale_y_continuous(limits = c(-0.5*sd_roa, 1.5*sd_roa)) +
ggtitle("Simulation 2") +
labs(subtitle = expression(paste("Not Staggered + Dynamic ", delta))) +
theme(legend.position = 'bottom',
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.title.y = element_text(hjust = 0.5, vjust = 0.5, angle = 360))
# plot for simulation 3
sim3_means <- data3 %>%
ggplot(aes(x = fyear, y = treat_roa, group = gvkey)) +
# unit specific lines
geom_line(alpha = 1/30, color = "grey") +
# group specific averages
geom_line(
data = . %>%
group_by(group, fyear) %>%
summarize(treat_roa = mean(treat_roa)),
aes(x = fyear, y = treat_roa, group = factor(group),
color = factor(group)), size = 1) +
labs(x = "", y = "", color = "Group") +
scale_y_continuous(limits = c(-0.5*sd_roa, 1.5*sd_roa)) +
geom_vline(xintercept = 1988.5, color = "#A7473A",
linetype = "dashed", size = 1) +
geom_vline(xintercept = 1997.5, color = "#4B5F6C",
linetype = "dashed", size = 1) +
geom_vline(xintercept = 2006.5, color = "#51806a",
linetype = "dashed", size = 1) +
scale_color_manual(values = c("#A7473A", "#4B5F6C", "#51806a")) +
ggtitle("Simulation 3") +
labs(subtitle = expression(paste("Staggered + Constant/Equal ", delta))) +
theme(legend.position = 'bottom',
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.title.y = element_text(hjust = 0.5, vjust = 0.5, angle = 360))
# plot for simulation 4
sim4_means <- data4 %>%
ggplot(aes(x = fyear, y = treat_roa, group = gvkey)) +
# unit specific lines
geom_line(alpha = 1/30, color = "grey") +
# group specific averages
geom_line(
data = . %>%
group_by(group, fyear) %>%
summarize(treat_roa = mean(treat_roa)),
aes(x = fyear, y = treat_roa, group = factor(group),
color = factor(group)), size = 1) +
labs(x = "", y = "ROA", color = "Group") +
scale_y_continuous(limits = c(-.5*sd_roa, 1.5*sd_roa)) +
geom_vline(xintercept = 1988.5, color = "#A7473A",
linetype = "dashed", size = 1) +
geom_vline(xintercept = 1997.5, color = "#4B5F6C",
linetype = "dashed", size = 1) +
geom_vline(xintercept = 2006.5, color = "#51806a",
linetype = "dashed", size = 1) +
scale_color_manual(values = c("#A7473A", "#4B5F6C", "#51806a")) +
ggtitle("Simulation 4") +
labs(subtitle = expression(paste("Staggered + Constant/Unequal ", delta))) +
theme(legend.position = 'none',
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.title.y = element_text(hjust = 0.5, vjust = 0.5, angle = 360))
# plot for simulation 5
sim5_means <- data5 %>%
ggplot(aes(x = fyear, y = treat_roa, group = gvkey)) +
# unit specific lines
geom_line(alpha = 1/30, color = "grey") +
# group specific averages
geom_line(
data = . %>%
group_by(group, fyear) %>%
summarize(treat_roa = mean(treat_roa)),
aes(x = fyear, y = treat_roa, group = factor(group),
color = factor(group)), size = 1) +
labs(x = "", y = "", color = "Group") +
scale_y_continuous(limits = c(-.5*sd_roa, 1.5*sd_roa)) +
geom_vline(xintercept = 1988.5, color = "#A7473A",
linetype = "dashed", size = 1) +
geom_vline(xintercept = 1997.5, color = "#4B5F6C",
linetype = "dashed", size = 1) +
geom_vline(xintercept = 2006.5, color = "#51806a",
linetype = "dashed", size = 1) +
scale_color_manual(values = c("#A7473A", "#4B5F6C", "#51806a")) +
ggtitle("Simulation 5") +
labs(subtitle = expression(paste("Staggered + Dynamic/Equal ", delta))) +
theme(legend.position = 'bottom',
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.title.y = element_text(hjust = 0.5, vjust = 0.5, angle = 360))
# plot for simulation 6
sim6_means <- data6 %>%
ggplot(aes(x = fyear, y = treat_roa, group = gvkey)) +
# unit specific lines
geom_line(alpha = 1/30, color = "grey") +
# group specific averages
geom_line(
data = . %>%
group_by(group, fyear) %>%
summarize(treat_roa = mean(treat_roa)),
aes(x = fyear, y = treat_roa, group = factor(group),
color = factor(group)), size = 1) +
labs(x = "", y = "", color = "Group") +
scale_y_continuous(limits = c(-.5*sd_roa, 1.5*sd_roa)) +
geom_vline(xintercept = 1988.5, color = "#A7473A",
linetype = "dashed", size = 1) +
geom_vline(xintercept = 1997.5, color = "#4B5F6C",
linetype = "dashed", size = 1) +
geom_vline(xintercept = 2006.5, color = "#51806a",
linetype = "dashed", size = 1) +
scale_color_manual(values = c("#A7473A", "#4B5F6C", "#51806a")) +
ggtitle("Simulation 6") +
labs(subtitle = expression(paste("Staggered + Dynamic/Unequal ", delta))) +
theme(legend.position = 'none',
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.title.y = element_text(hjust = 0.5, vjust = 0.5, angle = 360))
# plot treatment paths - sims 1 - 3
Sims_1_3_trends <- sim1_means + sim2_means + sim3_means
#save
ggsave(Sims_1_3_trends, filename = here::here("Figs_Tables", "Sims_1_3_trends.png"), dpi = 500,
width = 10, height = 4)
# plot treatment paths - sims 4 - 6
Sims_4_6_trends <- sim4_means + sim5_means + sim6_means
#save
ggsave(Sims_4_6_trends, filename = here::here("Figs_Tables", "Sims_4_6_trends.png"), dpi = 500,
width = 10, height = 4)
# now plot the distributions for the bottom panels
# make formula to make the plots
make_dist_plot <- function(i, name) {
estimates %>%
filter(dt == i) %>%
ggplot(aes(x = twfe)) +
geom_density(aes(fill = "TWFE Estimates"), alpha = 3/5) +
geom_vline(aes(xintercept = mean(estimates %>% filter(dt == i) %>% pull(full_te_1)),
color = "Observation Average"),
linetype = "dashed", size = 1, alpha = 3/5,) +
geom_vline(aes(xintercept = mean(estimates %>% filter(dt == i) %>% pull(full_te_2)),
color = "Firm Average"),
linetype = "dashed", size = 1, alpha = 3/5) +
ggtitle(paste0("Simulation ", i)) +
labs(subtitle = TeX(paste0(name, "$\\delta$"))) +
labs(y = "", x = if_else(i %in% c(2, 5), expression(widehat(delta^'DD')), expression(""))) +
scale_color_manual(name = "", values = c("Observation Average" = "#A7473A",
"Firm Average" = "#4B5F6C")) +
scale_fill_manual(name = "", values = c("TWFE Estimates" = "#CACFD0")) +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.title.y = element_text(hjust = 0.5, vjust = 0.5, angle = 360),
legend.position = if_else(i %in% c(2, 5), "bottom", "none"))
}
# run all the densities
sim1_estimates <- make_dist_plot(1, "Not Staggered + Constant ")
sim2_estimates <- make_dist_plot(2, "Not Staggered + Dynamic ")
sim3_estimates <- make_dist_plot(3, "Staggered + Constant/Equal ")
sim4_estimates <- make_dist_plot(4, "Staggered + Constant/Unequal ")
sim5_estimates <- make_dist_plot(5, "Staggered + Dynamic/Equal ")
sim6_estimates <- make_dist_plot(6, "Staggered + Dynamic/Unequal ")
# plot estimates of TWFE DD
Sims_1_3_dist <- sim1_estimates + sim2_estimates + sim3_estimates
Sims_4_6_dist <- sim4_estimates + sim5_estimates + sim6_estimates
# save
ggsave(Sims_1_3_dist, filename = here::here("Figs_Tables", "Sims_1_3_dist.png"), dpi = 500,
width = 10, height = 4)
ggsave(Sims_4_6_dist, filename = here::here("Figs_Tables", "Sims_4_6_dist.png"), dpi = 500,
width = 10, height = 4)