Anticipation

Units may respond two periods before treatment (anticipation=2): only periods before cohort - 2 count as untreated. The cohort-period tables label those periods, and the event study reports them at event times -2 and -1.

from diff_stream import did_bjs, did_callaway_santanna, did_sun_abraham
from diff_stream.simulate import simulate_conditional_panel

panel = simulate_conditional_panel(1200, 8, seed=72)
for estimator in [did_callaway_santanna, did_sun_abraham, did_bjs]:
    formula = "y ~ x + z" if estimator is did_callaway_santanna else "y ~ 1"
    result = estimator(
        formula, panel, panel=("id", "time"), cohort="first_treat", anticipation=2, n_threads=2
    )
    try:
        print(result.cohort_time())
        print(result.aggregate(balance_window=(-2, 1)).composition())
        result.aggregate("overall").summary()
    finally:
        result.cleanup()

Output:

shape: (21, 16)
┌───────────┬───────────┬───────────┬───────────┬───┬───────────┬───────────┬───────────┬──────────┐
│ Coefficie ┆ Estimate  ┆ Std.      ┆ t value   ┆ … ┆ weight_un ┆ weight_to ┆ observed_ ┆ cohort_u │
│ nt        ┆ ---       ┆ Error     ┆ ---       ┆   ┆ its       ┆ tal       ┆ units     ┆ nits     │
│ ---       ┆ f64       ┆ ---       ┆ f64       ┆   ┆ ---       ┆ ---       ┆ ---       ┆ ---      │
│ str       ┆           ┆ f64       ┆           ┆   ┆ i64       ┆ f64       ┆ i64       ┆ i64      │
╞═══════════╪═══════════╪═══════════╪═══════════╪═══╪═══════════╪═══════════╪═══════════╪══════════╡
│ g::6:t::2 ┆ -0.069578 ┆ 0.125858  ┆ -0.552827 ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::6:t::3 ┆ 0.093926  ┆ 0.127258  ┆ 0.738073  ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::6:t::4 ┆ -0.051157 ┆ 0.121771  ┆ -0.420108 ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::6:t::5 ┆ -0.09706  ┆ 0.121041  ┆ -0.801872 ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::6:t::6 ┆ 1.116509  ┆ 0.120162  ┆ 9.291684  ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::6:t::7 ┆ 1.212873  ┆ 0.125901  ┆ 9.63352   ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ …         ┆ …         ┆ …         ┆ …         ┆ … ┆ …         ┆ …         ┆ …         ┆ …        │
│ g::8:t::3 ┆ 0.181785  ┆ 0.121773  ┆ 1.492817  ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
│ g::8:t::4 ┆ -0.022141 ┆ 0.114934  ┆ -0.192642 ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
│ g::8:t::5 ┆ -0.101241 ┆ 0.116894  ┆ -0.866093 ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
│ g::8:t::6 ┆ 0.134323  ┆ 0.115232  ┆ 1.165676  ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
│ g::8:t::7 ┆ 0.070589  ┆ 0.118308  ┆ 0.596654  ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
│ g::8:t::8 ┆ 1.268864  ┆ 0.122     ┆ 10.400566 ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
└───────────┴───────────┴───────────┴───────────┴───┴───────────┴───────────┴───────────┴──────────┘
shape: (4, 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::-2 ┆ 2         ┆ 2       ┆ [6, 7]    ┆ … ┆ 610        ┆ 610.0     ┆ false     ┆ false     │
│ event::-1 ┆ 2         ┆ 2       ┆ [6, 7]    ┆ … ┆ 610        ┆ 610.0     ┆ false     ┆ false     │
│ event::0  ┆ 2         ┆ 2       ┆ [6, 7]    ┆ … ┆ 610        ┆ 610.0     ┆ false     ┆ false     │
│ event::1  ┆ 2         ┆ 2       ┆ [6, 7]    ┆ … ┆ 610        ┆ 610.0     ┆ false     ┆ false     │
└───────────┴───────────┴─────────┴───────────┴───┴────────────┴───────────┴───────────┴───────────┘
### y ~ x + z
estimator: cs; units: 1,200
vcov: CRV0
obs: 9,600
unit clustered; nuisance-adjusted analytic scores
shape: (1, 5)
┌─────────────┬──────────┬────────────┬───────────┬────────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value   ┆ Pr(>|t|)   │
│ ---         ┆ ---      ┆ ---        ┆ ---       ┆ ---        │
│ str         ┆ f64      ┆ f64        ┆ f64       ┆ f64        │
╞═════════════╪══════════╪════════════╪═══════════╪════════════╡
│ ATT         ┆ 1.261175 ┆ 0.067519   ┆ 18.678901 ┆ 7.3518e-78 │
└─────────────┴──────────┴────────────┴───────────┴────────────┘
shape: (21, 14)
┌───────────┬───────────┬───────────┬───────────┬───┬───────────┬───────────┬───────────┬──────────┐
│ Coefficie ┆ Estimate  ┆ Std.      ┆ t value   ┆ … ┆ weight_un ┆ weight_to ┆ observed_ ┆ cohort_u │
│ nt        ┆ ---       ┆ Error     ┆ ---       ┆   ┆ its       ┆ tal       ┆ units     ┆ nits     │
│ ---       ┆ f64       ┆ ---       ┆ f64       ┆   ┆ ---       ┆ ---       ┆ ---       ┆ ---      │
│ str       ┆           ┆ f64       ┆           ┆   ┆ i64       ┆ f64       ┆ i64       ┆ i64      │
╞═══════════╪═══════════╪═══════════╪═══════════╪═══╪═══════════╪═══════════╪═══════════╪══════════╡
│ g::6:t::1 ┆ -0.203141 ┆ 0.134459  ┆ -1.510804 ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::6:t::2 ┆ -0.195196 ┆ 0.126128  ┆ -1.547599 ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::6:t::4 ┆ 0.040136  ┆ 0.122596  ┆ 0.327386  ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::6:t::5 ┆ 0.145716  ┆ 0.128704  ┆ 1.132185  ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::6:t::6 ┆ 1.431371  ┆ 0.142673  ┆ 10.032549 ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::6:t::7 ┆ 1.641748  ┆ 0.158936  ┆ 10.329601 ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ …         ┆ …         ┆ …         ┆ …         ┆ … ┆ …         ┆ …         ┆ …         ┆ …        │
│ g::8:t::2 ┆ -0.31803  ┆ 0.137224  ┆ -2.317594 ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
│ g::8:t::3 ┆ -0.056    ┆ 0.125252  ┆ -0.447094 ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
│ g::8:t::4 ┆ 0.002224  ┆ 0.115108  ┆ 0.019323  ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
│ g::8:t::6 ┆ 0.195212  ┆ 0.115282  ┆ 1.693345  ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
│ g::8:t::7 ┆ 0.219357  ┆ 0.123317  ┆ 1.778809  ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
│ g::8:t::8 ┆ 1.483519  ┆ 0.137716  ┆ 10.772318 ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
└───────────┴───────────┴───────────┴───────────┴───┴───────────┴───────────┴───────────┴──────────┘
shape: (4, 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::-2 ┆ 2         ┆ 2       ┆ [6, 7]    ┆ … ┆ 610        ┆ 610.0     ┆ false     ┆ false     │
│ event::-1 ┆ 2         ┆ 2       ┆ [6, 7]    ┆ … ┆ 610        ┆ 610.0     ┆ false     ┆ false     │
│ event::0  ┆ 2         ┆ 2       ┆ [6, 7]    ┆ … ┆ 610        ┆ 610.0     ┆ false     ┆ false     │
│ event::1  ┆ 2         ┆ 2       ┆ [6, 7]    ┆ … ┆ 610        ┆ 610.0     ┆ false     ┆ false     │
└───────────┴───────────┴─────────┴───────────┴───┴────────────┴───────────┴───────────┴───────────┘
### y ~ 1
estimator: sunab; units: 1,200
vcov: CRV0
obs: 9,600
unit clustered; normal; analytic scores
shape: (1, 5)
┌─────────────┬──────────┬────────────┬───────────┬────────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value   ┆ Pr(>|t|)   │
│ ---         ┆ ---      ┆ ---        ┆ ---       ┆ ---        │
│ str         ┆ f64      ┆ f64        ┆ f64       ┆ f64        │
╞═════════════╪══════════╪════════════╪═══════════╪════════════╡
│ ATT         ┆ 1.4028   ┆ 0.100389   ┆ 13.973668 ┆ 2.2570e-44 │
└─────────────┴──────────┴────────────┴───────────┴────────────┘
shape: (12, 14)
┌───────────┬───────────┬───────────┬───────────┬───┬───────────┬───────────┬───────────┬──────────┐
│ Coefficie ┆ Estimate  ┆ Std.      ┆ t value   ┆ … ┆ weight_un ┆ weight_to ┆ observed_ ┆ cohort_u │
│ nt        ┆ ---       ┆ Error     ┆ ---       ┆   ┆ its       ┆ tal       ┆ units     ┆ nits     │
│ ---       ┆ f64       ┆ ---       ┆ f64       ┆   ┆ ---       ┆ ---       ┆ ---       ┆ ---      │
│ str       ┆           ┆ f64       ┆           ┆   ┆ i64       ┆ f64       ┆ i64       ┆ i64      │
╞═══════════╪═══════════╪═══════════╪═══════════╪═══╪═══════════╪═══════════╪═══════════╪══════════╡
│ g::6:t::4 ┆ 0.139332  ┆ 0.091939  ┆ 1.515496  ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::6:t::5 ┆ 0.179348  ┆ 0.106138  ┆ 1.68976   ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::6:t::6 ┆ 1.537604  ┆ 0.136644  ┆ 11.252602 ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::6:t::7 ┆ 1.747981  ┆ 0.156897  ┆ 11.14098  ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::6:t::8 ┆ 2.208645  ┆ 0.175671  ┆ 12.572624 ┆ … ┆ 270       ┆ 270.0     ┆ 270       ┆ 270      │
│ g::7:t::5 ┆ -0.427586 ┆ 0.087834  ┆ -4.868133 ┆ … ┆ 340       ┆ 340.0     ┆ 340       ┆ 340      │
│ g::7:t::6 ┆ -0.243429 ┆ 0.123993  ┆ -1.963243 ┆ … ┆ 340       ┆ 340.0     ┆ 340       ┆ 340      │
│ g::7:t::7 ┆ 0.847596  ┆ 0.144335  ┆ 5.872428  ┆ … ┆ 340       ┆ 340.0     ┆ 340       ┆ 340      │
│ g::7:t::8 ┆ 0.901615  ┆ 0.158007  ┆ 5.706187  ┆ … ┆ 340       ┆ 340.0     ┆ 340       ┆ 340      │
│ g::8:t::6 ┆ 0.338471  ┆ 0.113345  ┆ 2.98621   ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
│ g::8:t::7 ┆ 0.362616  ┆ 0.13226   ┆ 2.741695  ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
│ g::8:t::8 ┆ 1.626779  ┆ 0.150343  ┆ 10.820456 ┆ … ┆ 299       ┆ 299.0     ┆ 299       ┆ 299      │
└───────────┴───────────┴───────────┴───────────┴───┴───────────┴───────────┴───────────┴──────────┘
shape: (4, 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::-2 ┆ 2         ┆ 2       ┆ [6, 7]    ┆ … ┆ 610        ┆ 610.0     ┆ false     ┆ false     │
│ event::-1 ┆ 2         ┆ 2       ┆ [6, 7]    ┆ … ┆ 610        ┆ 610.0     ┆ false     ┆ false     │
│ event::0  ┆ 2         ┆ 2       ┆ [6, 7]    ┆ … ┆ 610        ┆ 610.0     ┆ false     ┆ false     │
│ event::1  ┆ 2         ┆ 2       ┆ [6, 7]    ┆ … ┆ 610        ┆ 610.0     ┆ false     ┆ false     │
└───────────┴───────────┴─────────┴───────────┴───┴────────────┴───────────┴───────────┴───────────┘
### y ~ 1
estimator: bjs; units: 1,200
vcov: CRV0
obs: 9,600
unit clustered; normal; BJS smart residual averaging
shape: (1, 5)
┌─────────────┬──────────┬────────────┬───────────┬────────────┐
│ Coefficient ┆ Estimate ┆ Std. Error ┆ t value   ┆ Pr(>|t|)   │
│ ---         ┆ ---      ┆ ---        ┆ ---       ┆ ---        │
│ str         ┆ f64      ┆ f64        ┆ f64       ┆ f64        │
╞═════════════╪══════════╪════════════╪═══════════╪════════════╡
│ ATT         ┆ 1.433528 ┆ 0.111919   ┆ 12.808659 ┆ 1.4665e-37 │
└─────────────┴──────────┴────────────┴───────────┴────────────┘