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 from cohort.

  • {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() and write_cells() (on disk, until cleanup()).

  • 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 reps samples 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. For did_dcdh with cluster=, 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 about reps fits; for CS, the multiplier bootstrap (multiplier_bootstrap_reps) is far cheaper.

Parameters:
  • fml – As for the estimator (did_dcdh takes treatment).

  • data – As for the estimator (did_dcdh takes treatment).

  • panel – As for the estimator (did_dcdh takes treatment).

  • cohort – As for the estimator (did_dcdh takes treatment).

  • treatment – As for the estimator (did_dcdh takes treatment).

  • estimator (function) – One of the estimator functions: did_callaway_santanna, did_sun_abraham, did_bjs, did_two_stage, did_extended_twfe or did_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 for did_two_stage and did_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_threads and max_memory_gb are 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() and standard_errors(). Failed draws are recorded in failures(), never redrawn; inference then requires allow_incomplete=True.