sunab | R Documentation |
User-level method to implement staggered difference-in-difference estimations a la Sun and Abraham (Journal of Econometrics, 2021).
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)
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). |
period |
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). |
ref.c |
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). |
ref.p |
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 |
bin |
A list of values to be grouped, a vector, or the special value |
bin.rel |
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.c |
A list or a vector defining which values to bin. Only applies to the cohort.
Please refer to the help of the argument |
bin.p |
A list or a vector defining which values to bin. Only applies to the period.
Please refer to the help of the argument |
att |
Logical, default is |
no_agg |
Logical, default is |
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.
If not used within a fixest
estimation, this function will return a matrix of
interacted coefficients.
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
# Simple DiD example
data(base_stagg)
head(base_stagg)
# Note that the year_treated is set to 1000 for the never treated
table(base_stagg$year_treated)
table(base_stagg$time_to_treatment)
# The DiD estimation
res_sunab = feols(y ~ x1 + sunab(year_treated, year) | id + year, base_stagg)
etable(res_sunab)
# 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)) |
id + year, 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")
# You can also look at the total effect per cohort
summary(res_sunab, agg = "cohort")
#
# 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) | id + year, base_stagg)
# binning the period
est_bin.p = feols(y ~ x1 + sunab(year_treated, year, bin.p = 3:1) | id + year, base_stagg)
# binning both the cohort and the period
est_bin = feols(y ~ x1 + sunab(year_treated, year, bin = 3:1) | id + year, base_stagg)
# binning the relative period, grouping every two years
est_bin.rel = feols(y ~ x1 + sunab(year_treated, year, bin.rel = "bin::2") | id + year, base_stagg)
etable(est_bin.c, est_bin.p, est_bin, est_bin.rel, keep = "year")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.