A simulation demo

(1) create simulation data

time.periods <- 4
sp <- reset.sim()
sp$te <- 0     #create blank dataframe
# set seed so everything is reproducible
set.seed(1814)

# generate dataset with 4 time periods
time.periods <- 4

# add dynamic effects
sp$te.e <- 1:time.periods

# generate data set with these parameters
# here, we dropped all units who are treated in time period 1 as they do not help us recover ATT(g,t)'s.
dta <- build_sim_dataset(sp)

# How many observations remained after dropping the ``always-treated'' units
nrow(dta)
## [1] 15916
#This is what the data looks like
dta
## # A tibble: 15,916 × 7
##        G      X    id cluster period      Y treat
##    <dbl>  <dbl> <int>   <int>  <dbl>  <dbl> <dbl>
##  1     3 -0.876     1       5      1  5.56      1
##  2     3 -0.876     1       5      2  4.35      1
##  3     3 -0.876     1       5      3  7.13      1
##  4     3 -0.876     1       5      4  6.24      1
##  5     2 -0.874     2      36      1 -3.66      1
##  6     2 -0.874     2      36      2 -1.27      1
##  7     2 -0.874     2      36      3  3.50      1
##  8     2 -0.874     2      36      4 -0.165     1
##  9     2  0.510     4       6      1  1.90      1
## 10     2  0.510     4       6      2  4.10      1
## # … with 15,906 more rows

(2) Estimating Group-Time Average Treatment Effects

example_attgt <- att_gt(yname = "Y",
                        tname = "period",
                        idname = "id",
                        gname = "G",
                        xformla = ~X,
                        data = dta
                        )
summary(example_attgt)
## 
## Call:
## att_gt(yname = "Y", tname = "period", idname = "id", gname = "G", 
##     xformla = ~X, data = dta)
## 
## Reference: Callaway, Brantly and Pedro H.C. Sant'Anna.  "Difference-in-Differences with Multiple Time Periods." Journal of Econometrics, Vol. 225, No. 2, pp. 200-230, 2021. <https://doi.org/10.1016/j.jeconom.2020.12.001>, <https://arxiv.org/abs/1803.09015> 
## 
## Group-Time Average Treatment Effects:
##  Group Time ATT(g,t) Std. Error [95% Simult.  Conf. Band]  
##      2    2   0.9209     0.0618        0.7523      1.0896 *
##      2    3   1.9875     0.0676        1.8029      2.1720 *
##      2    4   2.9552     0.0626        2.7845      3.1259 *
##      3    2  -0.0433     0.0634       -0.2163      0.1297  
##      3    3   1.1080     0.0675        0.9239      1.2922 *
##      3    4   2.0590     0.0649        1.8820      2.2360 *
##      4    2   0.0023     0.0643       -0.1732      0.1779  
##      4    3   0.0615     0.0651       -0.1162      0.2393  
##      4    4   0.9523     0.0696        0.7624      1.1423 *
## ---
## Signif. codes: `*' confidence band does not cover 0
## 
## P-value for pre-test of parallel trends assumption:  0.60857
## Control Group:  Never Treated,  Anticipation Periods:  0
## Estimation Method:  Doubly Robust

(3) plot group-time average treatment effects

ggdid(example_attgt)

(4) Aggregating group-time average treatment effects

Method 1: Simple Aggregation (a weighted average of all group-time average treatment effects with weights proportional to the group size. This is available by calling the aggte function with type = simple)

agg.simple <- aggte(example_attgt, type = "simple")
summary(agg.simple)
## 
## Call:
## aggte(MP = example_attgt, type = "simple")
## 
## Reference: Callaway, Brantly and Pedro H.C. Sant'Anna.  "Difference-in-Differences with Multiple Time Periods." Journal of Econometrics, Vol. 225, No. 2, pp. 200-230, 2021. <https://doi.org/10.1016/j.jeconom.2020.12.001>, <https://arxiv.org/abs/1803.09015> 
## 
## 
##     ATT    Std. Error     [ 95%  Conf. Int.]  
##  1.6583        0.0364     1.5869      1.7297 *
## 
## 
## ---
## Signif. codes: `*' confidence band does not cover 0
## 
## Control Group:  Never Treated,  Anticipation Periods:  0
## Estimation Method:  Doubly Robust

Method 2: Dynamic Effects and Event Studies( aggregate group-time effects into an event study plot)

agg.es <- aggte(example_attgt, type = "dynamic")
summary(agg.es)
## 
## Call:
## aggte(MP = example_attgt, type = "dynamic")
## 
## Reference: Callaway, Brantly and Pedro H.C. Sant'Anna.  "Difference-in-Differences with Multiple Time Periods." Journal of Econometrics, Vol. 225, No. 2, pp. 200-230, 2021. <https://doi.org/10.1016/j.jeconom.2020.12.001>, <https://arxiv.org/abs/1803.09015> 
## 
## 
## Overall summary of ATT's based on event-study/dynamic aggregation:  
##     ATT    Std. Error     [ 95%  Conf. Int.]  
##  1.9904        0.0377     1.9165      2.0644 *
## 
## 
## Dynamic Effects:
##  Event time Estimate Std. Error [95% Simult.  Conf. Band]  
##          -2   0.0023     0.0672       -0.1784      0.1830  
##          -1   0.0105     0.0402       -0.0975      0.1185  
##           0   0.9929     0.0302        0.9118      1.0740 *
##           1   2.0231     0.0438        1.9053      2.1410 *
##           2   2.9552     0.0628        2.7863      3.1241 *
## ---
## Signif. codes: `*' confidence band does not cover 0
## 
## Control Group:  Never Treated,  Anticipation Periods:  0
## Estimation Method:  Doubly Robust
ggdid(agg.es)

Method 3:Group-Specific Effects(average effects specific to each group)

agg.gs <- aggte(example_attgt, type = "group")
summary(agg.gs)
## 
## Call:
## aggte(MP = example_attgt, type = "group")
## 
## Reference: Callaway, Brantly and Pedro H.C. Sant'Anna.  "Difference-in-Differences with Multiple Time Periods." Journal of Econometrics, Vol. 225, No. 2, pp. 200-230, 2021. <https://doi.org/10.1016/j.jeconom.2020.12.001>, <https://arxiv.org/abs/1803.09015> 
## 
## 
## Overall summary of ATT's based on group/cohort aggregation:  
##    ATT    Std. Error     [ 95%  Conf. Int.]  
##  1.488        0.0305     1.4283      1.5477 *
## 
## 
## Group Effects:
##  Group Estimate Std. Error [95% Simult.  Conf. Band]  
##      2   1.9545     0.0509        1.8264      2.0827 *
##      3   1.5835     0.0568        1.4405      1.7265 *
##      4   0.9523     0.0656        0.7870      1.1176 *
## ---
## Signif. codes: `*' confidence band does not cover 0
## 
## Control Group:  Never Treated,  Anticipation Periods:  0
## Estimation Method:  Doubly Robust
ggdid(agg.gs)

Method 4: Calendar Time Effects(aggregations across different time periods)

agg.ct <- aggte(example_attgt, type = "calendar")
summary(agg.ct)
## 
## Call:
## aggte(MP = example_attgt, type = "calendar")
## 
## Reference: Callaway, Brantly and Pedro H.C. Sant'Anna.  "Difference-in-Differences with Multiple Time Periods." Journal of Econometrics, Vol. 225, No. 2, pp. 200-230, 2021. <https://doi.org/10.1016/j.jeconom.2020.12.001>, <https://arxiv.org/abs/1803.09015> 
## 
## 
## Overall summary of ATT's based on calendar time aggregation:  
##     ATT    Std. Error     [ 95%  Conf. Int.]  
##  1.4808        0.0366      1.409      1.5526 *
## 
## 
## Time Effects:
##  Time Estimate Std. Error [95% Simult.  Conf. Band]  
##     2   0.9209     0.0662        0.7645      1.0774 *
##     3   1.5491     0.0508        1.4291      1.6692 *
##     4   1.9724     0.0501        1.8538      2.0910 *
## ---
## Signif. codes: `*' confidence band does not cover 0
## 
## Control Group:  Never Treated,  Anticipation Periods:  0
## Estimation Method:  Doubly Robust
ggdid(agg.ct)

## (5) other options

chose control group

# By default, the did package uses the group of units that never participate in the treatment as the control group.
#  The other option for the control group is to use the “not yet treated”

example_attgt_altcontrol <- att_gt(yname = "Y",
                                   tname = "period",
                                   idname = "id",
                                   gname = "G",
                                   xformla = ~X,
                                   data = dta,
                                   control_group = "notyettreated"          
                                   )
summary(example_attgt_altcontrol)
## 
## Call:
## att_gt(yname = "Y", tname = "period", idname = "id", gname = "G", 
##     xformla = ~X, data = dta, control_group = "notyettreated")
## 
## Reference: Callaway, Brantly and Pedro H.C. Sant'Anna.  "Difference-in-Differences with Multiple Time Periods." Journal of Econometrics, Vol. 225, No. 2, pp. 200-230, 2021. <https://doi.org/10.1016/j.jeconom.2020.12.001>, <https://arxiv.org/abs/1803.09015> 
## 
## Group-Time Average Treatment Effects:
##  Group Time ATT(g,t) Std. Error [95% Simult.  Conf. Band]  
##      2    2   0.9371     0.0538        0.7852      1.0889 *
##      2    3   1.9612     0.0565        1.8019      2.1205 *
##      2    4   2.9552     0.0630        2.7776      3.1329 *
##      3    2  -0.0393     0.0537       -0.1906      0.1121  
##      3    3   1.0747     0.0562        0.9163      1.2331 *
##      3    4   2.0590     0.0636        1.8795      2.2385 *
##      4    2   0.0207     0.0547       -0.1336      0.1751  
##      4    3   0.0615     0.0631       -0.1165      0.2395  
##      4    4   0.9523     0.0647        0.7698      1.1348 *
## ---
## Signif. codes: `*' confidence band does not cover 0
## 
## P-value for pre-test of parallel trends assumption:  0.65089
## Control Group:  Not Yet Treated,  Anticipation Periods:  0
## Estimation Method:  Doubly Robust

choose estimation method

# By default, the did package uses “doubly robust” estimators 
# The other options are “ipw” for inverse probability weighting and “reg” # for regression
example_attgt_reg <- att_gt(yname = "Y",
                            tname = "period",
                            idname = "id",
                            gname = "G",
                            xformla = ~X,
                            data = dta,
                            est_method = "reg"
                            )
summary(example_attgt_reg)
## 
## Call:
## att_gt(yname = "Y", tname = "period", idname = "id", gname = "G", 
##     xformla = ~X, data = dta, est_method = "reg")
## 
## Reference: Callaway, Brantly and Pedro H.C. Sant'Anna.  "Difference-in-Differences with Multiple Time Periods." Journal of Econometrics, Vol. 225, No. 2, pp. 200-230, 2021. <https://doi.org/10.1016/j.jeconom.2020.12.001>, <https://arxiv.org/abs/1803.09015> 
## 
## Group-Time Average Treatment Effects:
##  Group Time ATT(g,t) Std. Error [95% Simult.  Conf. Band]  
##      2    2   0.9191     0.0703        0.7317      1.1064 *
##      2    3   1.9861     0.0700        1.7998      2.1724 *
##      2    4   2.9537     0.0634        2.7848      3.1226 *
##      3    2  -0.0532     0.0688       -0.2365      0.1301  
##      3    3   1.1106     0.0681        0.9291      1.2920 *
##      3    4   2.0609     0.0666        1.8836      2.2382 *
##      4    2  -0.0206     0.0732       -0.2156      0.1744  
##      4    3   0.0672     0.0669       -0.1109      0.2452  
##      4    4   0.9510     0.0673        0.7718      1.1302 *
## ---
## Signif. codes: `*' confidence band does not cover 0
## 
## P-value for pre-test of parallel trends assumption:  0.64383
## Control Group:  Never Treated,  Anticipation Periods:  0
## Estimation Method:  Outcome Regression