Estimates a classic difference-in-differences model of the form
outcome ~ D_it | fe using fixest::feols().
There are two ways to supply the treatment indicator:
Option A — pre-built D_it (maximum flexibility):
df$D <- as.integer(df$treated & df$year >= 2006)
run_did(df, outcome = y, treatment = D, fe = ~ id + year)Option B — timing-based construction (convenience; consistent with
run_es() and calc_att()):
run_did(df, outcome = y, treatment = treated, time = year, timing = 2006,
fe = ~ id + year)Here treatment is a binary group indicator (1 = treated unit, 0 = control),
time is the calendar-time variable, and timing is the scalar treatment
onset period. Internally D_it = treatment * (time >= timing) is constructed
automatically. For staggered-adoption settings use calc_att(); for dynamic
event-study estimates use run_es().
Usage
run_did(
data,
outcome,
treatment,
timing = NULL,
fe = NULL,
unit = NULL,
time = NULL,
covariates = NULL,
cluster = NULL,
weights = NULL,
conf.level = 0.95,
vcov = "HC1",
vcov_args = list()
)Arguments
- data
A data.frame (panel format).
- outcome
Unquoted outcome variable or expression (e.g.,
log(y)).- treatment
Unquoted column name. When
timing = NULL(default): a pre-built binaryD_itindicator (1 = treated unit-time, 0 = otherwise). Whentimingis provided: a binary group indicator (1 = treated unit, 0 = control unit; constant within units).- timing
Numeric scalar. When provided,
D_itis constructed astreatment * (time >= timing). Requirestimeto be specified. DefaultNULL(user supplies pre-builtD_itviatreatment).- fe
One-sided formula specifying fixed effects, e.g.
~ id + year. IfNULLand bothunitandtimeare supplied,feis auto-inferred as~ unit + time. IfNULLand neither is supplied, a pooled OLS model is estimated (with a message).- unit
Unquoted unit identifier column (for metadata and
feauto-inference).- time
Unquoted time variable column. Used for (a)
feauto-inference and (b)D_itconstruction whentimingis provided.- covariates
One-sided formula of additional controls, e.g.
~ x1 + x2.- cluster
Clustering specification: a one-sided formula (
~ id), a single character column name, or a numeric vector of lengthnrow(data). Whenclusteris specified andvcovis the default"HC1", cluster-robust standard errors are used automatically.- weights
Observation weights (formula or numeric vector).
- conf.level
Confidence level(s) for CIs. Scalar or vector (e.g.,
c(0.90, 0.95)). Default0.95.- vcov
VCOV type string passed to
fixest::vcov(). Default"HC1". Ignored in favour of cluster-robust SE whenclusteris supplied andvcovis left at its default"HC1".- vcov_args
Named list of additional arguments forwarded to
fixest::vcov().
Value
A did_result object (named list) with elements:
estimatesData frame with the treatment coefficient:
term,estimate,std.error,statistic,p.value, andconf_low_XX/conf_high_XXfor eachconf.levelentry.modelThe underlying
fixestmodel object.
Attributes: call, formula_str, outcome, treatment, timing,
fe, vcov_type, cluster_vars, conf.level, N, N_units,
N_treated, unit, time.
Examples
if (FALSE) { # \dontrun{
# Option A: pre-built D_it
df$D <- as.integer(df$treated & df$year >= 2006)
res <- run_did(df, outcome = y, treatment = D, fe = ~ id + year)
# Option B: timing-based construction
res <- run_did(df, outcome = y, treatment = treated, time = year,
timing = 2006, fe = ~ id + year)
# Cluster-robust SE
res <- run_did(df, outcome = y, treatment = D, fe = ~ id + year,
cluster = ~ id)
print(res)
broom::tidy(res)
broom::glance(res)
# modelsummary::modelsummary(res)
} # }