kylebutts / did2s

Two-stage Difference-in-Differences package following Gardner (2021)
http://kylebutts.github.io/did2s
Other
93 stars 21 forks source link

"horizon" arg not respected in `event_study` #16

Closed grantmcdermott closed 2 years ago

grantmcdermott commented 2 years ago

I think it might be an issue of expecting relative time periods, though these are calculated after the fact? However, the "horizon" arg does work as expected in plot_event_study.

Reprex:

library(data.table) 
library(did2s)     
#> Loading required package: fixest
#> ℹ did2s (v0.6.0). For more information on the methodology, visit <https://www.kylebutts.com/did2s>
#> To cite did2s in publications use:
#> 
#>   Butts, Kyle (2021).  did2s: Two-Stage Difference-in-Differences
#>   Following Gardner (2021). R package version 0.6.0.
#> 
#> A BibTeX entry for LaTeX users is
#> 
#>   @Manual{,
#>     title = {did2s: Two-Stage Difference-in-Differences Following Gardner (2021)},
#>     author = {Kyle Butts},
#>     year = {2021},
#>     url = {https://github.com/kylebutts/did2s/},
#>   }

# data import and prep
dat = fread("https://raw.githubusercontent.com/LOST-STATS/LOST-STATS.github.io/master/Model_Estimation/Data/Event_Study_DiD/bacon_example.csv") 
setnames(dat, "_nfd", "year_treated")

# event plot
out = event_study(data      = dat, 
                  yname     = "asmrs",
                  idname    = "stfips",
                  gname     = "year_treated",
                  tname     = "year",
                  xformla   = ~pcinc + asmrh + cases,
                  horizon   = c(-8, 20), 
                  estimator = "did2s")
#> Estimating using Gardner (2021)

# Horizon does not appear to be constrained
out
#>          estimator  term     estimate std.error
#>             <char> <num>        <num>     <num>
#>  1: Gardner (2021)   -21 -15.55208203  2.074955
#>  2: Gardner (2021)   -20  -5.74945142  9.466054
#>  3: Gardner (2021)   -19  14.16652730  5.713433
#>  4: Gardner (2021)   -18   4.40074938  2.769530
#>  5: Gardner (2021)   -17   0.46163339  4.212164
#>  6: Gardner (2021)   -16   3.65218896  1.432753
#>  7: Gardner (2021)   -15   4.64055267  3.526735
#>  8: Gardner (2021)   -14   8.33051168  4.619431
#>  9: Gardner (2021)   -13   1.21377878  4.223394
#> 10: Gardner (2021)   -12   3.50617166  4.309746
#> 11: Gardner (2021)   -11  -5.61675623  2.350250
#> 12: Gardner (2021)   -10   2.77324219  2.397910
#> 13: Gardner (2021)    -9  -1.95335549  1.539734
#> 14: Gardner (2021)    -8  -0.50962050  1.232027
#> 15: Gardner (2021)    -7   0.87687175  1.780879
#> 16: Gardner (2021)    -6  -0.03227035  1.372245
#> 17: Gardner (2021)    -5  -1.69128571  1.055959
#> 18: Gardner (2021)    -4   1.04107906  1.118773
#> 19: Gardner (2021)    -3  -1.34665273  1.491998
#> 20: Gardner (2021)    -2   0.09087091  1.469868
#> 21: Gardner (2021)    -1   0.60935125  1.863670
#> 22: Gardner (2021)     0   1.03576478  2.031174
#> 23: Gardner (2021)     1  -0.87772719  2.216546
#> 24: Gardner (2021)     2  -0.94231944  2.776114
#> 25: Gardner (2021)     3  -0.11913586  2.763868
#> 26: Gardner (2021)     4  -2.19367442  2.809341
#> 27: Gardner (2021)     5  -2.40863049  2.370357
#> 28: Gardner (2021)     6  -3.06770912  3.191968
#> 29: Gardner (2021)     7  -6.13585171  2.639534
#> 30: Gardner (2021)     8  -7.07520062  3.505784
#> 31: Gardner (2021)     9  -4.32417886  3.075393
#> 32: Gardner (2021)    10  -7.39829071  3.704624
#> 33: Gardner (2021)    11  -5.88414598  3.658832
#> 34: Gardner (2021)    12  -4.31121988  4.154406
#> 35: Gardner (2021)    13  -6.47801092  3.806313
#> 36: Gardner (2021)    14  -5.47686257  3.647392
#> 37: Gardner (2021)    15  -6.80087834  3.958293
#> 38: Gardner (2021)    16  -5.93903288  3.888136
#> 39: Gardner (2021)    17  -7.14733710  3.645845
#> 40: Gardner (2021)    18  -8.88881256  3.620076
#> 41: Gardner (2021)    19  -9.51797099  3.231651
#> 42: Gardner (2021)    20  -7.15944486  3.780501
#> 43: Gardner (2021)    21  -8.83413672  3.577299
#> 44: Gardner (2021)    22  -9.09674999  3.912211
#> 45: Gardner (2021)    23 -10.62013067  4.761694
#> 46: Gardner (2021)    24  -9.64521321  6.187071
#> 47: Gardner (2021)    25  -9.43505957  7.973742
#> 48: Gardner (2021)    26 -19.92247239 15.959928
#> 49: Gardner (2021)    27   9.80665559 10.325850
#>          estimator  term     estimate std.error
plot_event_study(out)


## Adding horizon in plot_event_study works fine, though
plot_event_study(out, horizon = c(-8, 20))

Created on 2022-05-12 by the reprex package (v2.0.1)

kylebutts commented 2 years ago

Ahhh good catch. I think I did it in event_study at first and then decided to switch to plot_event_study since it's not really an estimator option but a plotting one.

Fixed: bc0cad2d337cd7f383db10429bddc02a678c56e9