Skip to content

Commit bb8be20

Browse files
authored
Merge pull request #36 from vnery5/main
Added Weights in Event Study
2 parents 078f717 + 2dd498f commit bb8be20

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

R/event_study.R

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
#' @param xformla A formula for the covariates to include in the model.
1212
#' It should be of the form `~ X1 + X2`. Default is NULL.
1313
#' @param weights Variable name for estimation weights. This is used in
14-
#' estimating Y(0) and also augments treatment effect weights
14+
#' estimating Y(0) and also augments treatment effect weights.
15+
#' Implementation of Roth and Sant'Anna (2021) currently does not allow for weights.
1516
#' @param estimator Estimator you would like to use. Use "all" to estimate all.
1617
#' Otherwise see table to know advantages and requirements for each of these.
1718
#'
@@ -67,6 +68,11 @@ event_study = function(data, yname, idname, gname, tname,
6768
message(paste0("Warning: `", xformla, "` is ignored for the `staggered` estimator"))
6869
}
6970
}
71+
72+
# Checking weights
73+
if (!(is.null(weights) || is.character(weights))) {
74+
stop("Argument `weights` must be `NULL` or a character string")
75+
}
7076

7177
# Setup ------------------------------------------------------------------------
7278

@@ -114,7 +120,15 @@ if(estimator %in% c("TWFE", "all")) {
114120
yname, " ~ 1 + ", xformla_null, " + i(zz000event_time, ref = c(-1, -Inf)) | ", idname, " + ", tname
115121
)
116122
)
117-
est_twfe = fixest::feols(twfe_formula, data = data, warn = F, notes = F)
123+
if (is.null(weights)){
124+
est_twfe = fixest::feols(twfe_formula, data = data,
125+
warn = F, notes = F)
126+
} else {
127+
est_twfe = fixest::feols(twfe_formula, data = data,
128+
weights = as.matrix(data[weights]),
129+
warn = F, notes = F)
130+
}
131+
118132

119133
# Extract coefficients and standard errors
120134
tidy_twfe = broom::tidy(est_twfe)
@@ -143,7 +157,7 @@ if(estimator %in% c("did2s", "all")) {
143157
)
144158
)
145159

146-
est_did2s = did2s::did2s(data, yname = yname, first_stage = did2s_first_stage, second_stage = ~i(zz000event_time, ref=-Inf), treatment = "zz000treat", cluster_var = idname, verbose = FALSE)
160+
est_did2s = did2s::did2s(data, yname = yname, first_stage = did2s_first_stage, second_stage = ~i(zz000event_time, ref=-Inf), treatment = "zz000treat", cluster_var = idname, weights = weights, verbose = FALSE)
147161

148162
# Extract coefficients and standard errors
149163
tidy_did2s = broom::tidy(est_did2s)
@@ -166,7 +180,10 @@ if(estimator %in% c("did", "all")) {
166180
message("Estimating using Callaway and Sant'Anna (2020)")
167181

168182
try({
169-
est_did = did::att_gt(yname = yname, tname = tname, idname = idname, gname = gname, xformla = xformla, data = data)
183+
est_did = did::att_gt(yname = yname, tname = tname, idname = idname,
184+
gname = gname, xformla = xformla,
185+
data = data, weightsname = weights,
186+
allow_unbalanced_panel = FALSE)
170187

171188
est_did = did::aggte(est_did, type = "dynamic", na.rm = TRUE)
172189

@@ -198,8 +215,11 @@ if(estimator %in% c("sunab", "all")) {
198215
yname, " ~ ", sunab_xformla, " + sunab(", gname, ", zz000event_time, ref.c =0, ref.p = -1) | ", idname, " + ", tname
199216
)
200217
)
201-
202-
est_sunab = fixest::feols(sunab_formla, data = data)
218+
if (is.null(weights)){
219+
est_sunab = fixest::feols(sunab_formla, data = data)
220+
} else {
221+
est_sunab = fixest::feols(sunab_formla, data = data, weights = as.matrix(data[weights]))
222+
}
203223

204224
tidy_sunab = broom::tidy(est_sunab)
205225

@@ -228,7 +248,7 @@ if(estimator %in% c("impute", "all")) {
228248
)
229249

230250
tidy_impute = didimputation::did_imputation(data,
231-
yname = yname, gname = gname, tname = tname, idname = idname,
251+
yname = yname, gname = gname, tname = tname, idname = idname, wname = weights,
232252
first_stage = impute_first_stage, horizon = TRUE, pretrends = TRUE)
233253

234254
# Subset columns
@@ -275,6 +295,7 @@ if(estimator %in% c("staggered", "all")) {
275295
})
276296

277297
if(is.null(tidy_staggered)) warning("Roth and Sant'Anna (2021) Failed")
298+
if(!is.null(weights)) warning("Roth and Sant'Anna (2021) Does Not Allow Weights")
278299
}
279300

280301
# Bind results together --------------------------------------------------------

0 commit comments

Comments
 (0)