Diagnostics and bootstrap¶
- diff_stream.bacon_decomp(fml, data, *, panel, cohort=None, treatment=None, weights=None, incomplete_units='error', n_threads=None, max_memory_gb=None, memory_reserve=None, workdir=None, profile=False, batch_units=1024, preprocess_mb=None)[source]¶
Goodman-Bacon’s decomposition of the two-way fixed effects estimate.
Writes the coefficient on treatment in a regression with unit and time fixed effects as a weighted average of every two-group, two-period comparison in the data, including the “forbidden” ones that use already-treated units as controls. It is a diagnostic of what the conventional estimate averages, not an estimator of a causal effect.
Goodman-Bacon, A. (2021). Difference-in-differences with variation in treatment timing. Journal of Econometrics 225(2), 254-277.
- Parameters:
fml (str) –
"y ~ 1", or"y ~ x + z"for the covariate-adjusted decomposition. Treatment comes fromcohort.{DATA} – Units treated before the first period (always treated) are allowed; a never-treated group is not required.
weights (str, optional) – A column of sampling weights, positive and constant within each unit.
incomplete_units ({"error", "drop"}, default "error") – Refuse a panel with missing unit-periods, or drop the incomplete units. The decomposition needs a balanced panel.
{RESOURCES}
- Returns:
BaconResult or Profile –
comparisons()lists each comparison’s estimate, weight and type;summary()totals them by type.
- diff_stream.twfe_weights(fml, data, *, panel, treatment=None, cohort=None, weights=None, allow_time_varying_weights=False, incomplete_units='error', keep_cells=False, zero_tolerance=1e-10, max_design_columns=2048, n_threads=None, max_memory_gb=None, memory_reserve=None, workdir=None, profile=False, batch_units=256, preprocess_mb=None)[source]¶
de Chaisemartin and D’Haultfoeuille’s weights behind a TWFE estimate.
Under heterogeneous effects, the coefficient on a binary treatment in a regression with unit and time fixed effects is a weighted sum of the effects in each treated unit-period, and some weights can be negative. This reports those weights and two sensitivity measures: how much effect heterogeneity would be needed for the estimate to have the opposite sign from every unit-period’s effect. It is a diagnostic, not an estimator of a causal effect. It follows the authors’ R package TwoWayFEWeights (
type="feTR").de Chaisemartin, C. and D’Haultfoeuille, X. (2020). Two-way fixed effects estimators with heterogeneous treatment effects. American Economic Review 110(9), 2964-2996.
- Parameters:
fml (str) –
"y ~ 1", or"y ~ x + z"with covariates. Unit and time fixed effects are always included.data (polars.DataFrame, polars.LazyFrame, str or Path) – The panel, or a Parquet path or glob.
panel (tuple of str) – The unit and time columns. Times are positive integers; gaps are allowed.
treatment (str, optional) – Exactly one: a binary treatment column (which may switch on and off), or a first-treated-period column (0 = never treated).
cohort (str, optional) – Exactly one: a binary treatment column (which may switch on and off), or a first-treated-period column (0 = never treated).
weights (str, optional) – A column of positive sampling weights.
allow_time_varying_weights (bool, default False) – Accept weights that change over time within a unit.
incomplete_units ({"error", "drop", "keep"}, default "error") – Refuse a panel with missing unit-periods, drop the incomplete units, or use the observed unit-periods.
keep_cells (bool, default False) – Keep every treated cell’s weight for
cells(),iter_cells()andwrite_cells()(on disk, untilcleanup()).zero_tolerance (float, default 1e-10) – Weights smaller than this in absolute value count as zero in the summary (R’s default).
max_design_columns (int, default 2048) – The limit on the expanded covariate design.
{RESOURCES}
- Returns:
TWFEWeightResult or Profile –
summary()reports the positive and negative weights and the sensitivity measures.
- diff_stream.refit_bootstrap(fml, data, *, panel, cohort=None, treatment=None, estimator, reps=199, seed=None, aggregate=None, aggregate_options=None, coefficients=None, n_workers=1, n_threads=None, max_memory_gb=None, memory_reserve=None, workdir=None, batch_units=256, preprocess_mb=None, **fit_options)[source]¶
Bootstrap by resampling units (or clusters) and refitting the estimator.
Draws
repssamples of the units with replacement (each copy of a unit gets its own ID) and refits the whole estimator on each, including its propensity scores, trimming and aggregation weights. Fordid_dcdhwithcluster=, whole clusters are drawn instead, as did_multiplegt_dyn’s bootstrap does, each copy’s groups with IDs of their own. Reports bootstrap standard errors and percentile, basic or normal intervals for each coefficient. The sample and comparison group chosen by the original fit (incomplete units dropped, a last-treated reference window) are held fixed across draws. This is slower than analytic standard errors, by aboutrepsfits; for CS, the multiplier bootstrap (multiplier_bootstrap_reps) is far cheaper.- Parameters:
fml – As for the estimator (
did_dcdhtakestreatment).data – As for the estimator (
did_dcdhtakestreatment).panel – As for the estimator (
did_dcdhtakestreatment).cohort – As for the estimator (
did_dcdhtakestreatment).treatment – As for the estimator (
did_dcdhtakestreatment).estimator (function) – One of the estimator functions:
did_callaway_santanna,did_sun_abraham,did_bjs,did_two_stage,did_extended_twfeordid_dcdh.reps (int, default 199) – Bootstrap draws.
seed (int, optional) – Seed for the draws. With the same data layout, the same seed gives the same draws.
aggregate ({"event_time", "overall", "cohort", "calendar"}, optional) – Bootstrap an aggregate (
result.aggregate(aggregate, **aggregate_options)) instead of the fitted coefficients. Not available fordid_two_stageanddid_dcdh, whose coefficients are reported directly.aggregate_options (dict, optional) – Options for
aggregate(), such as{"balanced_horizon": 2}.coefficients (list of str, optional) – Bootstrap only these coefficients (exact names).
n_workers (int, default 1) – Refits run in parallel, each in a fresh process.
n_threadsandmax_memory_gbare shared out among them.n_threads – As for the estimator; they cover the whole job.
max_memory_gb – As for the estimator; they cover the whole job.
memory_reserve – As for the estimator; they cover the whole job.
workdir – As for the estimator; they cover the whole job.
batch_units – As for the estimator; they cover the whole job.
preprocess_mb – As for the estimator; they cover the whole job.
**fit_options – The estimator’s other options, e.g.
adjustment="ipw".
- Returns:
RefitBootstrapResult –
tidy(),confint()andstandard_errors(). Failed draws are recorded infailures(), never redrawn; inference then requiresallow_incomplete=True.