Covariates and fixed effects in Sun-Abraham and BJS

Time-varying weights on an unbalanced panel; covariates in Callaway-Sant’Anna; covariates and region-by-time fixed effects in Sun-Abraham and BJS, with BJS’s pre-trend test.

from pathlib import Path

import polars as pl

from diff_stream import did_bjs, did_callaway_santanna, did_sun_abraham


def main():
    root = Path(__file__).resolve().parents[1]
    data = pl.read_csv(root / "tests/data/richer_unbalanced_panel.csv")
    common = dict(
        panel=("id", "time"),
        cohort="first_treat",
        weights="w",
        allow_time_varying_weights=True,
        incomplete_units="keep",
        n_threads=2,
    )
    fits = [
        ("CS", did_callaway_santanna, "y ~ x+z", {}),
        ("SA", did_sun_abraham, "y ~ x + I(x**2) | region^time", {}),
        ("BJS", did_bjs, "y ~ x + I(x**2) | region^time", {"pretrend_leads": 2}),
    ]
    for label, estimator, fml, options in fits:
        fit = estimator(fml, data, **common, **options)
        try:
            print(label, "—", fit.diagnostics["unit_weight_anchor"])
            print(fit.aggregate("overall").summary())
            if label == "BJS":
                print(fit.placebos().tidy())
                print(fit.pretrend_test())
        finally:
            fit.cleanup()


if __name__ == "__main__":
    main()

Output:

CS — mean observed weight per retained unit
### y ~ x+z
estimator: cs; units: 360
vcov: CRV0
obs: 2,362
unit clustered; observed-row analytic scores
Low effective sample size: 10 CS coefficients have a required treated/control stratum below 30 (minimum 15.6). Inspect effective_sample_size(); suppress(min_effective_sample=...) masks coefficients without changing aggregation weights.
shape: (1, 5)
┌─────────────┬──────────┬────────────┬──────────┬────────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value  ┆ Pr(>|t|)   │
│ ---         ┆ ---      ┆ ---        ┆ ---      ┆ ---        │
│ str         ┆ f64      ┆ f64        ┆ f64      ┆ f64        │
╞═════════════╪══════════╪════════════╪══════════╪════════════╡
│ ATT         ┆ 1.121771 ┆ 0.159175   ┆ 7.047428 ┆ 1.8226e-12 │
└─────────────┴──────────┴────────────┴──────────┴────────────┘
None
SA — mean observed weight per retained unit
### y ~ x + I(x**2) | region^time
estimator: sunab; units: 360
vcov: CRV0
obs: 2,362
unit clustered; observed-row analytic scores
shape: (1, 5)
┌─────────────┬──────────┬────────────┬──────────┬────────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value  ┆ Pr(>|t|)   │
│ ---         ┆ ---      ┆ ---        ┆ ---      ┆ ---        │
│ str         ┆ f64      ┆ f64        ┆ f64      ┆ f64        │
╞═════════════╪══════════╪════════════╪══════════╪════════════╡
│ ATT         ┆ 1.189554 ┆ 0.154011   ┆ 7.723837 ┆ 1.1288e-14 │
└─────────────┴──────────┴────────────┴──────────┴────────────┘
None
BJS — mean observed weight per retained unit
### y ~ x + I(x**2) | region^time
estimator: bjs; units: 360
vcov: CRV0
obs: 2,362
unit clustered; observed-row analytic scores
shape: (1, 5)
┌─────────────┬──────────┬────────────┬──────────┬────────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value  ┆ Pr(>|t|)   │
│ ---         ┆ ---      ┆ ---        ┆ ---      ┆ ---        │
│ str         ┆ f64      ┆ f64        ┆ f64      ┆ f64        │
╞═════════════╪══════════╪════════════╪══════════╪════════════╡
│ ATT         ┆ 1.298125 ┆ 0.139445   ┆ 9.309197 ┆ 1.2880e-20 │
└─────────────┴──────────┴────────────┴──────────┴────────────┘
None
shape: (2, 5)
┌─────────────┬──────────┬────────────┬──────────┬──────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value  ┆ Pr(>|t|) │
│ ---         ┆ ---      ┆ ---        ┆ ---      ┆ ---      │
│ str         ┆ f64      ┆ f64        ┆ f64      ┆ f64      │
╞═════════════╪══════════╪════════════╪══════════╪══════════╡
│ event::-2   ┆ 0.2574   ┆ 0.124446   ┆ 2.06837  ┆ 0.038605 │
│ event::-1   ┆ 0.053938 ┆ 0.138927   ┆ 0.388244 ┆ 0.697836 │
└─────────────┴──────────┴────────────┴──────────┴──────────┘
{'statistic': 4.507671691334256, 'pvalue': 0.10499570381942967, 'df1': 2, 'df2': None, 'distribution': 'chi2', 'level': 'event_time', 'coefficients': ['event::-2', 'event::-1'], 'horizons': [-2, -1], 'cells': [{'cohort': 5, 'time': 3}, {'cohort': 5, 'time': 4}, {'cohort': 6, 'time': 4}, {'cohort': 6, 'time': 5}, {'cohort': 7, 'time': 5}, {'cohort': 7, 'time': 6}], 'n_restrictions': 2, 'anticipation': 0, 'reference_event_time': None, 'null': 'all selected clean pre-treatment effects equal zero', 'base_period': 'all other clean observations; no fixed event-time reference', 'fitted_horizons': [-2, -1], 'sample': 'separate clean-sample lead regression'}