Sun-Abraham on the Castle Doctrine panel

The event study, the overall ATT, a pre-trend test of three event-time averages, and the composition of an event study balanced over event times 0 to 2.

from pathlib import Path

import polars as pl

from diff_stream import did_sun_abraham

data = pl.read_csv(Path(__file__).resolve().parents[1] / "tests/data/castle.csv", null_values="NA")
data = data.with_columns(pl.col("effyear").fill_null(0).cast(pl.Int64).alias("first_treat"))
fit = did_sun_abraham(
    "l_homicide ~ 1", data, panel=("state", "year"), cohort="first_treat", n_threads=2
)
try:
    print(fit.aggregate().tidy())
    print(fit.aggregate("overall").tidy())
    print(fit.pretrend_test(level="event_time", horizons=[-4, -3, -2]))
    print(fit.aggregate(balanced_horizon=2, horizons=[0, 1, 2]).composition())
finally:
    fit.cleanup()

Output:

shape: (14, 5)
┌─────────────┬───────────┬────────────┬───────────┬────────────┐
│ Coefficient ┆ Estimate  ┆ Std. Error ┆ t value   ┆ Pr(>|t|)   │
│ ---         ┆ ---       ┆ ---        ┆ ---       ┆ ---        │
│ str         ┆ f64       ┆ f64        ┆ f64       ┆ f64        │
╞═════════════╪═══════════╪════════════╪═══════════╪════════════╡
│ event::-9   ┆ -0.403967 ┆ 0.057146   ┆ -7.069    ┆ 1.5605e-12 │
│ event::-8   ┆ -0.123811 ┆ 0.062625   ┆ -1.977017 ┆ 0.04804    │
│ event::-7   ┆ -0.233131 ┆ 0.104256   ┆ -2.236146 ┆ 0.025342   │
│ event::-6   ┆ 0.04534   ┆ 0.068557   ┆ 0.661347  ┆ 0.50839    │
│ event::-5   ┆ 0.031626  ┆ 0.059873   ┆ 0.528215  ┆ 0.59735    │
│ event::-4   ┆ -0.007685 ┆ 0.043489   ┆ -0.176716 ┆ 0.859731   │
│ …           ┆ …         ┆ …          ┆ …         ┆ …          │
│ event::0    ┆ 0.097215  ┆ 0.037725   ┆ 2.576916  ┆ 0.009969   │
│ event::1    ┆ 0.111549  ┆ 0.043358   ┆ 2.572772  ┆ 0.010089   │
│ event::2    ┆ 0.111566  ┆ 0.056306   ┆ 1.981437  ┆ 0.047542   │
│ event::3    ┆ 0.136825  ┆ 0.05716    ┆ 2.393708  ┆ 0.016679   │
│ event::4    ┆ 0.092587  ┆ 0.053584   ┆ 1.727875  ┆ 0.084011   │
│ event::5    ┆ 0.111942  ┆ 0.050854   ┆ 2.201238  ┆ 0.027719   │
└─────────────┴───────────┴────────────┴───────────┴────────────┘
shape: (1, 5)
┌─────────────┬──────────┬────────────┬──────────┬──────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value  ┆ Pr(>|t|) │
│ ---         ┆ ---      ┆ ---        ┆ ---      ┆ ---      │
│ str         ┆ f64      ┆ f64        ┆ f64      ┆ f64      │
╞═════════════╪══════════╪════════════╪══════════╪══════════╡
│ ATT         ┆ 0.110383 ┆ 0.038602   ┆ 2.859522 ┆ 0.004243 │
└─────────────┴──────────┴────────────┴──────────┴──────────┘
{'statistic': 5.178334562041286, 'pvalue': 0.1591947784456328, 'df1': 3, 'df2': None, 'distribution': 'chi2', 'level': 'event_time', 'coefficients': ['event::-4', 'event::-3', 'event::-2'], 'horizons': [-4, -3, -2], 'cells': [{'cohort': 2005, 'time': 2001}, {'cohort': 2005, 'time': 2002}, {'cohort': 2005, 'time': 2003}, {'cohort': 2006, 'time': 2002}, {'cohort': 2006, 'time': 2003}, {'cohort': 2006, 'time': 2004}, {'cohort': 2007, 'time': 2003}, {'cohort': 2007, 'time': 2004}, {'cohort': 2007, 'time': 2005}, {'cohort': 2008, 'time': 2004}, {'cohort': 2008, 'time': 2005}, {'cohort': 2008, 'time': 2006}, {'cohort': 2009, 'time': 2005}, {'cohort': 2009, 'time': 2006}, {'cohort': 2009, 'time': 2007}], 'n_restrictions': 3, 'anticipation': 0, 'reference_event_time': -1, 'null': 'all selected clean pre-treatment effects equal zero', 'base_period': 'g-1'}
shape: (3, 10)
┌───────────┬───────────┬─────────┬────────────┬───┬───────────┬───────────┬───────────┬───────────┐
│ aggregate ┆ n_cohorts ┆ n_cells ┆ cohorts    ┆ … ┆ observed_ ┆ target_we ┆ cohorts_c ┆ cohort_we │
│ ---       ┆ ---       ┆ ---     ┆ ---        ┆   ┆ unit_peri ┆ ight_tota ┆ hanged    ┆ ights_cha │
│ str       ┆ i64       ┆ i64     ┆ list[i64]  ┆   ┆ ods       ┆ l         ┆ ---       ┆ nged      │
│           ┆           ┆         ┆            ┆   ┆ ---       ┆ ---       ┆ bool      ┆ ---       │
│           ┆           ┆         ┆            ┆   ┆ i64       ┆ f64       ┆           ┆ bool      │
╞═══════════╪═══════════╪═════════╪════════════╪═══╪═══════════╪═══════════╪═══════════╪═══════════╡
│ event::0  ┆ 4         ┆ 4       ┆ [2005,     ┆ … ┆ 20        ┆ 20.0      ┆ false     ┆ false     │
│           ┆           ┆         ┆ 2006, …    ┆   ┆           ┆           ┆           ┆           │
│           ┆           ┆         ┆ 2008]      ┆   ┆           ┆           ┆           ┆           │
│ event::1  ┆ 4         ┆ 4       ┆ [2005,     ┆ … ┆ 20        ┆ 20.0      ┆ false     ┆ false     │
│           ┆           ┆         ┆ 2006, …    ┆   ┆           ┆           ┆           ┆           │
│           ┆           ┆         ┆ 2008]      ┆   ┆           ┆           ┆           ┆           │
│ event::2  ┆ 4         ┆ 4       ┆ [2005,     ┆ … ┆ 20        ┆ 20.0      ┆ false     ┆ false     │
│           ┆           ┆         ┆ 2006, …    ┆   ┆           ┆           ┆           ┆           │
│           ┆           ┆         ┆ 2008]      ┆   ┆           ┆           ┆           ┆           │
└───────────┴───────────┴─────────┴────────────┴───┴───────────┴───────────┴───────────┴───────────┘