When every unit is eventually treated¶
No unit is never treated, so the last-treated cohort serves as the comparison until it is treated (last_treated_controls="if_needed", with a warning). BJS, the two-stage estimator and ETWFE on the same panel.
from pathlib import Path
import polars as pl
from diff_stream import did_bjs, did_extended_twfe, did_two_stage
ROOT = Path(__file__).resolve().parents[1]
data = pl.read_csv(ROOT / "tests/data/richer_unbalanced_panel.csv").filter(
pl.col("first_treat") != 0
)
for label, entry in [("BJS", did_bjs), ("Gardner", did_two_stage), ("ETWFE", did_extended_twfe)]:
options = dict(pretrend_leads=2) if label == "BJS" else {}
with entry(
"y~x+z",
data,
panel=("id", "time"),
cohort="first_treat",
last_treated_controls="if_needed",
incomplete_units="keep",
anticipation=1,
weights="w",
allow_time_varying_weights=True,
n_threads=2,
**options,
) as fit:
print(
label,
"reference:",
fit.diagnostics["reference_cohort"],
"last clean date:",
fit.diagnostics["reference_last_clean_time"],
)
print(fit.tidy())
if label == "BJS":
print(fit.placebos().tidy())
print(fit.placebos().support())
Output:
BJS reference: 7 last clean date: 5
shape: (3, 5)
┌─────────────┬───────────┬────────────┬───────────┬──────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value ┆ Pr(>|t|) │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
╞═════════════╪═══════════╪════════════╪═══════════╪══════════╡
│ g::5:t::4 ┆ 0.308597 ┆ 0.222941 ┆ 1.38421 ┆ 0.166294 │
│ g::5:t::5 ┆ 1.001209 ┆ 0.226703 ┆ 4.416401 ┆ 0.00001 │
│ g::6:t::5 ┆ -0.750864 ┆ 0.261615 ┆ -2.870108 ┆ 0.004103 │
└─────────────┴───────────┴────────────┴───────────┴──────────┘
shape: (2, 5)
┌─────────────┬──────────┬────────────┬──────────┬──────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value ┆ Pr(>|t|) │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
╞═════════════╪══════════╪════════════╪══════════╪══════════╡
│ event::-3 ┆ 0.220596 ┆ 0.174195 ┆ 1.266375 ┆ 0.205379 │
│ event::-2 ┆ 0.410283 ┆ 0.261021 ┆ 1.571838 ┆ 0.115988 │
└─────────────┴──────────┴────────────┴──────────┴──────────┘
shape: (6, 6)
┌───────────┬────────┬──────┬────────────┬────────────────┬──────────────┐
│ aggregate ┆ cohort ┆ time ┆ event_time ┆ observed_units ┆ weight_total │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ f64 │
╞═══════════╪════════╪══════╪════════════╪════════════════╪══════════════╡
│ event::-3 ┆ 5 ┆ 2 ┆ -3 ┆ 72 ┆ 90.986593 │
│ event::-2 ┆ 5 ┆ 3 ┆ -2 ┆ 62 ┆ 84.059008 │
│ event::-3 ┆ 6 ┆ 3 ┆ -3 ┆ 79 ┆ 86.040945 │
│ event::-2 ┆ 6 ┆ 4 ┆ -2 ┆ 105 ┆ 126.35765 │
│ event::-3 ┆ 7 ┆ 4 ┆ -3 ┆ 100 ┆ 172.474898 │
│ event::-2 ┆ 7 ┆ 5 ┆ -2 ┆ 100 ┆ 201.233534 │
└───────────┴────────┴──────┴────────────┴────────────────┴──────────────┘
Gardner reference: 7 last clean date: 5
shape: (5, 5)
┌─────────────┬───────────┬────────────┬───────────┬──────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value ┆ Pr(>|t|) │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
╞═════════════╪═══════════╪════════════╪═══════════╪══════════╡
│ event::-5 ┆ 0.032496 ┆ 0.129742 ┆ 0.250467 ┆ 0.802227 │
│ event::-4 ┆ -0.052307 ┆ 0.063734 ┆ -0.820722 ┆ 0.411805 │
│ event::-3 ┆ -0.034175 ┆ 0.090537 ┆ -0.377475 ┆ 0.705821 │
│ event::-1 ┆ -0.28874 ┆ 0.177875 ┆ -1.623273 ┆ 0.104531 │
│ event::0 ┆ 1.001209 ┆ 0.227029 ┆ 4.410056 ┆ 0.00001 │
└─────────────┴───────────┴────────────┴───────────┴──────────┘
ETWFE reference: 7 last clean date: 5
shape: (8, 5)
┌─────────────┬───────────┬────────────┬───────────┬──────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value ┆ Pr(>|t|) │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
╞═════════════╪═══════════╪════════════╪═══════════╪══════════╡
│ ATT::5::1 ┆ -0.520507 ┆ 0.300392 ┆ -1.732756 ┆ 0.083139 │
│ ATT::5::2 ┆ -0.237468 ┆ 0.289219 ┆ -0.821067 ┆ 0.411608 │
│ ATT::5::4 ┆ -0.054662 ┆ 0.386482 ┆ -0.141436 ┆ 0.887526 │
│ ATT::5::5 ┆ 0.641101 ┆ 0.355923 ┆ 1.801235 ┆ 0.071666 │
│ ATT::6::1 ┆ -0.295307 ┆ 0.312992 ┆ -0.943496 ┆ 0.345427 │
│ ATT::6::2 ┆ -0.176239 ┆ 0.280264 ┆ -0.628831 ┆ 0.52946 │
│ ATT::6::3 ┆ 0.283401 ┆ 0.331648 ┆ 0.854525 ┆ 0.392814 │
│ ATT::6::5 ┆ -0.649366 ┆ 0.274958 ┆ -2.361694 ┆ 0.018192 │
└─────────────┴───────────┴────────────┴───────────┴──────────┘