User-level method to implement staggered difference-in-difference estimations a la Sun and Abraham (Journal of Econometrics, forthcoming).

sunab(
cohort,
period,
ref.c = NULL,
ref.p = -1,
bin,
bin.rel,
bin.c,
bin.p,
att = FALSE,
no_agg = FALSE
)

sunab_att(cohort, period, ref.c = NULL, ref.p = -1)

## Arguments

cohort A vector representing the cohort. It should represent the period at which the treatment has been received (and thus be fixed for each unit). A vector representing the period. It can be either a relative time period (with negative values representing the before the treatment and positive values after the treatment), or a regular time period. In the latter case, the relative time period will be created from the cohort information (which represents the time at which the treatment has been received). A vector of references for the cohort. By default the never treated cohorts are taken as reference and the always treated are excluded from the estimation. You can add more references with this argument, which means that dummies will not be created for them (but they will remain in the estimation). A vector of references for the (relative!) period. By default the first relative period (RP) before the treatment, i.e. -1, is taken as reference. You can instead use your own references (i.e. RPs for which dummies will not be created -- but these observations remain in the sample). Please note that you will need at least two references. You can use the special variables .F and .L to access the first and the last relative periods. A list of values to be grouped, a vector, or the special value "bin::digit". The binning will be applied to both the cohort and the period (to bin them separately, see bin.c and bin.p). To create a new value from old values, use bin = list("new_value"=old_values) with old_values a vector of existing values. It accepts regular expressions, but they must start with an "@", like in bin="@Aug|Dec". The names of the list are the new names. If the new name is missing, the first value matched becomes the new name. Feeding in a vector is like using a list without name and only a single element. If the vector is numeric, you can use the special value "bin::digit" to group every digit element. For example if x represent years, using bin="bin::2" create bins of two years. Using "!bin::digit" groups every digit consecutive values starting from the first value. Using "!!bin::digit" is the same bu starting from the last value. In both cases, x is not required to be numeric. A list or a vector defining which values to bin. Only applies to the relative periods and not the cohorts. Please refer to the help of the argument bin to understand the different ways to do the binning (or look at the help of bin). A list or a vector defining which values to bin. Only applies to the cohort. Please refer to the help of the argument bin to understand the different ways to do the binning (or look at the help of bin). A list or a vector defining which values to bin. Only applies to the period. Please refer to the help of the argument bin to understand the different ways to do the binning (or look at the help of bin). Logical, default is FALSE. If TRUE: then the total average treatment effect for the treated is computed (instead of the ATT for each relative period). Logical, default is FALSE. If TRUE: then there is no aggregation, leading to the estimation of all cohort x time to treatment coefficients.

## Value

If not used within a fixest estimation, this function will return a matrix of interacted coefficients.

## Details

This function creates a matrix of cohort x relative_period interactions, and if used within a fixest estimation, the coefficients will automatically be aggregated to obtain the ATT for each relative period. In practice, the coefficients are aggregated with the aggregate.fixest function whose argument agg is automatically set to the appropriate value.

The SA method requires relative periods (negative/positive for before/after the treatment). Either the user can compute the RP (relative periods) by his/her own, either the RPs are computed on the fly from the periods and the cohorts (which then should represent the treatment period).

The never treated, which are the cohorts displaying only negative RPs are used as references (i.e. no dummy will be constructed for them). On the other hand, the always treated are removed from the estimation, by means of adding NAs for each of their observations.

If the RPs have to be constructed on the fly, any cohort that is not present in the period is considered as never treated. This means that if the period ranges from 1995 to 2005, cohort = 1994 will be considered as never treated, although it should be considered as always treated: so be careful.

If you construct your own relative periods, the controls cohorts should have only negative RPs.

## Binning

You can bin periods with the arguments bin, bin.c, bin.p and/or bin.rel.

The argument bin applies both to the original periods and cohorts (the cohorts will also be binned!). This argument only works when the period represent "calendar" periods (not relative ones!).

Alternatively you can bin the periods with bin.p (either "calendar" or relative); or the cohorts with bin.c.

The argument bin.rel applies only to the relative periods (hence not to the cohorts) once they have been created.

To understand how binning works, please have a look at the help and examples of the function bin.

Binning can be done in many different ways: just remember that it is not because it is possible that it does makes sense!

Laurent Berge

## Examples


# Simple DiD example
data(base_stagg)
#>   id year year_treated time_to_treatment treated treatment_effect_true
#> 2 90    1            2                -1       1                     0
#> 3 89    1            3                -2       1                     0
#> 4 88    1            4                -3       1                     0
#> 5 87    1            5                -4       1                     0
#> 6 86    1            6                -5       1                     0
#> 7 85    1            7                -6       1                     0
#>           x1           y
#> 2 -1.0947021  0.01722971
#> 3 -3.7100676 -4.58084528
#> 4  2.5274402  2.73817174
#> 5 -0.7204263 -0.65103066
#> 6 -3.6711678 -5.33381664
#> 7 -0.3152137  0.49562631

# Note that the year_treated is set to 1000 for the never treated
table(base_stagg$year_treated) #> #> 2 3 4 5 6 7 8 9 10 10000 #> 50 50 50 50 50 50 50 50 50 500 table(base_stagg$time_to_treatment)
#>
#> -1000    -9    -8    -7    -6    -5    -4    -3    -2    -1     0     1     2
#>   500     5    10    15    20    25    30    35    40    45    45    40    35
#>     3     4     5     6     7     8
#>    30    25    20    15    10     5

# The DiD estimation
res_sunab = feols(y ~ x1 + sunab(year_treated, year), base_stagg)
etable(res_sunab)
#>                          res_sunab
#> Dependent Var.:                  y
#>
#> (Intercept)        0.0063 (0.0443)
#> x1              0.9883*** (0.0166)
#> year = -9          0.3971 (0.4643)
#> year = -8         -0.4161 (0.3306)
#> year = -7         -0.0348 (0.2710)
#> year = -6         -0.0863 (0.2359)
#> year = -5         -0.3221 (0.2120)
#> year = -4         -0.1375 (0.1939)
#> year = -3         -0.0834 (0.1805)
#> year = -2          0.0754 (0.1693)
#> year = 0        -5.129*** (0.1604)
#> year = 1        -3.451*** (0.1698)
#> year = 2        -2.035*** (0.1803)
#> year = 3        -0.6104** (0.1950)
#> year = 4         1.147*** (0.2121)
#> year = 5         2.287*** (0.2359)
#> year = 6         4.559*** (0.2729)
#> year = 7         5.207*** (0.3301)
#> year = 8         6.958*** (0.4676)
#> _______________ __________________
#> S.E. type                      IID
#> Observations                   950
#> R2                         0.89654
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

# By default the reference periods are the first year and the year before the treatment
# i.e. ref.p = c(-1, .F); where .F is a shortcut for the first period.
# Say you want to set as references the first three periods on top of -1

res_sunab_3ref = feols(y ~ x1 + sunab(year_treated, year, ref.p = c(.F + 0:2, -1)), base_stagg)

# Display the two results
iplot(list(res_sunab, res_sunab_3ref))

# ... + show all refs
iplot(list(res_sunab, res_sunab_3ref), ref = "all")

#
# ATT
#

# To get the total ATT, you can use summary with the agg argument:
summary(res_sunab, agg = "ATT")
#> OLS estimation, Dep. Var.: y
#> Observations: 950
#> Standard-errors: IID
#>              Estimate Std. Error    t value  Pr(>|t|)
#> (Intercept)  0.006306   0.044297   0.142359   0.88683
#> x1           0.988344   0.016624  59.452798 < 2.2e-16 ***
#> ATT         -1.016516   0.083251 -12.210183 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 0.987344   Adj. R2: 0.88676

# You can also look at the total effect per cohort
summary(res_sunab, agg = "cohort")
#> OLS estimation, Dep. Var.: y
#> Observations: 950
#> Standard-errors: IID
#>              Estimate Std. Error    t value  Pr(>|t|)
#> (Intercept)  0.006306   0.044297   0.142359   0.88683
#> x1           0.988344   0.016624  59.452798 < 2.2e-16 ***
#> cohort::2    3.082786   0.160917  19.157673 < 2.2e-16 ***
#> cohort::3    1.429502   0.169888   8.414383 < 2.2e-16 ***
#> cohort::4    0.001684   0.180523   0.009328   0.99256
#> cohort::5   -1.702666   0.194170  -8.768955 < 2.2e-16 ***
#> cohort::6   -3.082949   0.211683 -14.564003 < 2.2e-16 ***
#> cohort::7   -4.859333   0.235792 -20.608551 < 2.2e-16 ***
#> cohort::8   -5.239574   0.272440 -19.232013 < 2.2e-16 ***
#> cohort::9   -7.760783   0.330684 -23.468871 < 2.2e-16 ***
#> cohort::10  -8.627732   0.465247 -18.544396 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 0.987344   Adj. R2: 0.88676

#
# Binning
#

# Binning can be done in many different ways

# binning the cohort
est_bin.c   = feols(y ~ x1 + sunab(year_treated, year, bin.c = 3:2), base_stagg)

# binning the period
est_bin.p   = feols(y ~ x1 + sunab(year_treated, year, bin.p = 3:1), base_stagg)
#> NOTE: 1 observation removed because of NA values (RHS: 1).

# binning both the cohort and the period
est_bin     = feols(y ~ x1 + sunab(year_treated, year, bin = 3:1), base_stagg)
#> NOTE: 1 observation removed because of NA values (RHS: 1).

# binning the relative period, grouping every two years
est_bin.rel = feols(y ~ x1 + sunab(year_treated, year, bin.rel = "bin::2"), base_stagg)

etable(est_bin.c, est_bin.p, est_bin, est_bin.rel, keep = "year")
#>                          est_bin.c          est_bin.p            est_bin
#> Dependent Var.:                  y                  y                  y
#>
#> year = -9          0.4122 (0.4893)
#> year = -8         -0.3966 (0.3484)
#> year = -7         -0.0163 (0.2856)   -0.1185 (0.3807)   -0.1151 (0.3807)
#> year = -6         -0.0678 (0.2486)   -0.2126 (0.3318)   -0.2085 (0.3318)
#> year = -5         -0.3037 (0.2234)  -0.6283* (0.2984)  -0.6241* (0.2984)
#> year = -4         -0.1208 (0.2043)   -0.4039 (0.2722)   -0.4007 (0.2722)
#> year = -3         -0.0660 (0.1902)  -0.4842. (0.2529)  -0.4811. (0.2529)
#> year = -2          0.1564 (0.1690)   -0.1895 (0.2375)   -0.1863 (0.2375)
#> year = 0        -4.865*** (0.1690) -6.429*** (0.2529) -6.427*** (0.2529)
#> year = 1        -3.478*** (0.1789) -4.881*** (0.2729) -4.879*** (0.2729)
#> year = 2        -1.928*** (0.1901) -3.429*** (0.2971) -3.427*** (0.2971)
#> year = 3         -0.3391. (0.2053) -1.986*** (0.3325) -1.985*** (0.3325)
#> year = 4         1.409*** (0.2234)   -0.0616 (0.3812)   -0.0599 (0.3812)
#> year = 5         2.514*** (0.2490)    0.7065 (0.4649)    0.7081 (0.4649)
#> year = 6         4.827*** (0.2869)  2.886*** (0.6556)  2.886*** (0.6557)
#> year = 7         5.740*** (0.3494)
#> year = 8
#> _______________ __________________ __________________ __________________
#> S.E. type                      IID                IID                IID
#> Observations                   950                949                949
#> R2                         0.88394            0.78670            0.78713
#> Adj. R2                    0.87427            0.77482            0.77528
#>
#>                        est_bin.rel
#> Dependent Var.:                  y
#>
#> year = -9          0.3946 (0.4773)
#> year = -8         -0.1772 (0.2182)
#> year = -7
#> year = -6         -0.2086 (0.1656)
#> year = -5
#> year = -4         -0.1044 (0.1396)
#> year = -3
#> year = -2          0.0764 (0.1740)
#> year = 0        -4.344*** (0.1242)
#> year = 1
#> year = 2        -1.384*** (0.1401)
#> year = 3
#> year = 4         1.645*** (0.1656)
#> year = 5
#> year = 6         4.804*** (0.2190)
#> year = 7
#> year = 8         6.929*** (0.4805)
#> _______________ __________________
#> S.E. type                      IID
#> Observations                   950
#> R2                         0.88667