Aggregation and its composition

Cohort and calendar aggregates of a Callaway-Sant’Anna fit on an unbalanced panel, with the cells and weights behind each estimate, then an event study restricted to the cohorts observed at event times 0 and 1.

from diff_stream import did_callaway_santanna
from diff_stream.simulate import simulate_conditional_panel

panel = simulate_conditional_panel(1200, 8, seed=99)
panel = panel.filter(~((panel["id"] % 4 == 0) & (panel["time"] == 7)))
fit = did_callaway_santanna(
    "y ~ x + z",
    panel,
    panel=("id", "time"),
    cohort="first_treat",
    incomplete_units="keep",
    n_threads=2,
)
for kind in ["cohort", "calendar"]:
    result = fit.aggregate(kind)
    result.summary()
    print(result.contributions())

# Cohorts with exposure 0 and 1 in this particular simulation.
window = fit.aggregate(balance_window=(0, 1))
print(window.composition())
print(window.diagnostics["excluded_cohorts"])

Output:

### y ~ x + z
estimator: cs; units: 1,200
vcov: CRV0
obs: 9,300
unit clustered; observed-row analytic scores
shape: (3, 5)
┌─────────────┬──────────┬────────────┬───────────┬────────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value   ┆ Pr(>|t|)   │
│ ---         ┆ ---      ┆ ---        ┆ ---       ┆ ---        │
│ str         ┆ f64      ┆ f64        ┆ f64       ┆ f64        │
╞═════════════╪══════════╪════════════╪═══════════╪════════════╡
│ cohort::6   ┆ 1.354359 ┆ 0.10287    ┆ 13.165736 ┆ 1.3818e-39 │
│ cohort::7   ┆ 1.310087 ┆ 0.10152    ┆ 12.90473  ┆ 4.2330e-38 │
│ cohort::8   ┆ 1.50667  ┆ 0.140605   ┆ 10.715651 ┆ 8.5950e-27 │
└─────────────┴──────────┴────────────┴───────────┴────────────┘
shape: (6, 13)
┌───────────┬───────────┬────────┬──────┬───┬─────────────┬─────────────┬─────────────┬────────────┐
│ aggregate ┆ cell      ┆ cohort ┆ time ┆ … ┆ weight_unit ┆ weight_tota ┆ observed_un ┆ cohort_uni │
│ ---       ┆ ---       ┆ ---    ┆ ---  ┆   ┆ s           ┆ l           ┆ its         ┆ ts         │
│ str       ┆ str       ┆ i64    ┆ i64  ┆   ┆ ---         ┆ ---         ┆ ---         ┆ ---        │
│           ┆           ┆        ┆      ┆   ┆ i64         ┆ f64         ┆ i64         ┆ i64        │
╞═══════════╪═══════════╪════════╪══════╪═══╪═════════════╪═════════════╪═════════════╪════════════╡
│ cohort::6 ┆ g::6:t::6 ┆ 6      ┆ 6    ┆ … ┆ 257         ┆ 257.0       ┆ 257         ┆ 257        │
│ cohort::6 ┆ g::6:t::7 ┆ 6      ┆ 7    ┆ … ┆ 257         ┆ 257.0       ┆ 200         ┆ 257        │
│ cohort::6 ┆ g::6:t::8 ┆ 6      ┆ 8    ┆ … ┆ 257         ┆ 257.0       ┆ 257         ┆ 257        │
│ cohort::7 ┆ g::7:t::7 ┆ 7      ┆ 7    ┆ … ┆ 367         ┆ 367.0       ┆ 270         ┆ 367        │
│ cohort::7 ┆ g::7:t::8 ┆ 7      ┆ 8    ┆ … ┆ 367         ┆ 367.0       ┆ 367         ┆ 367        │
│ cohort::8 ┆ g::8:t::8 ┆ 8      ┆ 8    ┆ … ┆ 290         ┆ 290.0       ┆ 290         ┆ 290        │
└───────────┴───────────┴────────┴──────┴───┴─────────────┴─────────────┴─────────────┴────────────┘
### y ~ x + z
estimator: cs; units: 1,200
vcov: CRV0
obs: 9,300
unit clustered; observed-row analytic scores
shape: (3, 5)
┌─────────────┬──────────┬────────────┬───────────┬────────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value   ┆ Pr(>|t|)   │
│ ---         ┆ ---      ┆ ---        ┆ ---       ┆ ---        │
│ str         ┆ f64      ┆ f64        ┆ f64       ┆ f64        │
╞═════════════╪══════════╪════════════╪═══════════╪════════════╡
│ calendar::6 ┆ 1.124944 ┆ 0.128573   ┆ 8.749427  ┆ 2.1444e-18 │
│ calendar::7 ┆ 1.338723 ┆ 0.104579   ┆ 12.801025 ┆ 1.6180e-37 │
│ calendar::8 ┆ 1.442314 ┆ 0.087016   ┆ 16.575184 ┆ 1.0534e-61 │
└─────────────┴──────────┴────────────┴───────────┴────────────┘
shape: (6, 13)
┌─────────────┬───────────┬────────┬──────┬───┬─────────────┬────────────┬────────────┬────────────┐
│ aggregate   ┆ cell      ┆ cohort ┆ time ┆ … ┆ weight_unit ┆ weight_tot ┆ observed_u ┆ cohort_uni │
│ ---         ┆ ---       ┆ ---    ┆ ---  ┆   ┆ s           ┆ al         ┆ nits       ┆ ts         │
│ str         ┆ str       ┆ i64    ┆ i64  ┆   ┆ ---         ┆ ---        ┆ ---        ┆ ---        │
│             ┆           ┆        ┆      ┆   ┆ i64         ┆ f64        ┆ i64        ┆ i64        │
╞═════════════╪═══════════╪════════╪══════╪═══╪═════════════╪════════════╪════════════╪════════════╡
│ calendar::6 ┆ g::6:t::6 ┆ 6      ┆ 6    ┆ … ┆ 257         ┆ 257.0      ┆ 257        ┆ 257        │
│ calendar::7 ┆ g::6:t::7 ┆ 6      ┆ 7    ┆ … ┆ 257         ┆ 257.0      ┆ 200        ┆ 257        │
│ calendar::7 ┆ g::7:t::7 ┆ 7      ┆ 7    ┆ … ┆ 367         ┆ 367.0      ┆ 270        ┆ 367        │
│ calendar::8 ┆ g::6:t::8 ┆ 6      ┆ 8    ┆ … ┆ 257         ┆ 257.0      ┆ 257        ┆ 257        │
│ calendar::8 ┆ g::7:t::8 ┆ 7      ┆ 8    ┆ … ┆ 367         ┆ 367.0      ┆ 367        ┆ 367        │
│ calendar::8 ┆ g::8:t::8 ┆ 8      ┆ 8    ┆ … ┆ 290         ┆ 290.0      ┆ 290        ┆ 290        │
└─────────────┴───────────┴────────┴──────┴───┴─────────────┴────────────┴────────────┴────────────┘
shape: (2, 10)
┌───────────┬───────────┬─────────┬───────────┬───┬────────────┬───────────┬───────────┬───────────┐
│ aggregate ┆ n_cohorts ┆ n_cells ┆ cohorts   ┆ … ┆ observed_u ┆ target_we ┆ cohorts_c ┆ cohort_we │
│ ---       ┆ ---       ┆ ---     ┆ ---       ┆   ┆ nit_period ┆ ight_tota ┆ hanged    ┆ ights_cha │
│ str       ┆ i64       ┆ i64     ┆ list[i64] ┆   ┆ s          ┆ l         ┆ ---       ┆ nged      │
│           ┆           ┆         ┆           ┆   ┆ ---        ┆ ---       ┆ bool      ┆ ---       │
│           ┆           ┆         ┆           ┆   ┆ i64        ┆ f64       ┆           ┆ bool      │
╞═══════════╪═══════════╪═════════╪═══════════╪═══╪════════════╪═══════════╪═══════════╪═══════════╡
│ event::0  ┆ 2         ┆ 2       ┆ [6, 7]    ┆ … ┆ 527        ┆ 624.0     ┆ false     ┆ false     │
│ event::1  ┆ 2         ┆ 2       ┆ [6, 7]    ┆ … ┆ 567        ┆ 624.0     ┆ false     ┆ false     │
└───────────┴───────────┴─────────┴───────────┴───┴────────────┴───────────┴───────────┴───────────┘
[{'cohort': 8, 'reason': 'missing required event-time support', 'missing_horizons': [1]}]