TWFE treatment-effect weights¶
The weights behind the union wage premium in the authors’ wage panel, where union membership switches on and off: their sums, the sensitivity measures, and the cells with negative weights.
from pathlib import Path
import polars as pl
from diff_stream import twfe_weights
ROOT = Path(__file__).resolve().parents[1]
data = pl.read_csv(ROOT / "tests/data/twfe_wagepan.csv", null_values="NA")
with twfe_weights(
"lwage ~ 1",
data,
panel=("nr", "year"),
treatment="union",
keep_cells=True,
n_threads=2,
) as diagnostic:
print("TWFE coefficient:", diagnostic.twfe_estimate)
print(diagnostic.summary())
print(diagnostic.sensitivity())
print(diagnostic.cells().filter(pl.col("classified_weight") < 0).collect())
Output:
TWFE coefficient: 0.10662746555843503
shape: (3, 4)
┌──────────┬─────────┬────────────┬───────────────┐
│ sign ┆ n_cells ┆ weight_sum ┆ treated_share │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ f64 ┆ f64 │
╞══════════╪═════════╪════════════╪═══════════════╡
│ positive ┆ 820 ┆ 1.010529 ┆ 0.807087 │
│ negative ┆ 147 ┆ -0.010529 ┆ 0.144685 │
│ zero ┆ 49 ┆ 0.0 ┆ 0.048228 │
└──────────┴─────────┴────────────┴───────────────┘
shape: (2, 3)
┌──────────────────────┬──────────┬────────┐
│ measure ┆ value ┆ status │
│ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ str │
╞══════════════════════╪══════════╪════════╡
│ zero_average_sd ┆ 0.096869 ┆ finite │
│ all_opposite_sign_sd ┆ 3.175859 ┆ finite │
└──────────────────────┴──────────┴────────┘
shape: (147, 10)
┌───────┬──────┬───────────┬─────────────┬───┬───────────┬─────────────┬─────────────┬─────────────┐
│ unit ┆ time ┆ treatment ┆ sampling_we ┆ … ┆ weight ┆ treated_sha ┆ relative_we ┆ classified_ │
│ --- ┆ --- ┆ --- ┆ ight ┆ ┆ --- ┆ re ┆ ight ┆ weight │
│ i64 ┆ i64 ┆ f64 ┆ --- ┆ ┆ f64 ┆ --- ┆ --- ┆ --- │
│ ┆ ┆ ┆ f64 ┆ ┆ ┆ f64 ┆ f64 ┆ f64 │
╞═══════╪══════╪═══════════╪═════════════╪═══╪═══════════╪═════════════╪═════════════╪═════════════╡
│ 647 ┆ 1980 ┆ 1.0 ┆ 1.0 ┆ … ┆ -0.000074 ┆ 0.000984 ┆ -0.075281 ┆ -0.000074 │
│ 647 ┆ 1983 ┆ 1.0 ┆ 1.0 ┆ … ┆ -0.000022 ┆ 0.000984 ┆ -0.022584 ┆ -0.000022 │
│ 647 ┆ 1987 ┆ 1.0 ┆ 1.0 ┆ … ┆ -0.000119 ┆ 0.000984 ┆ -0.12045 ┆ -0.000119 │
│ 800 ┆ 1980 ┆ 1.0 ┆ 1.0 ┆ … ┆ -0.000074 ┆ 0.000984 ┆ -0.075281 ┆ -0.000074 │
│ 800 ┆ 1983 ┆ 1.0 ┆ 1.0 ┆ … ┆ -0.000022 ┆ 0.000984 ┆ -0.022584 ┆ -0.000022 │
│ 800 ┆ 1987 ┆ 1.0 ┆ 1.0 ┆ … ┆ -0.000119 ┆ 0.000984 ┆ -0.12045 ┆ -0.000119 │
│ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │
│ 11857 ┆ 1980 ┆ 1.0 ┆ 1.0 ┆ … ┆ -0.000074 ┆ 0.000984 ┆ -0.075281 ┆ -0.000074 │
│ 11857 ┆ 1983 ┆ 1.0 ┆ 1.0 ┆ … ┆ -0.000022 ┆ 0.000984 ┆ -0.022584 ┆ -0.000022 │
│ 11857 ┆ 1987 ┆ 1.0 ┆ 1.0 ┆ … ┆ -0.000119 ┆ 0.000984 ┆ -0.12045 ┆ -0.000119 │
│ 12477 ┆ 1980 ┆ 1.0 ┆ 1.0 ┆ … ┆ -0.000074 ┆ 0.000984 ┆ -0.075281 ┆ -0.000074 │
│ 12477 ┆ 1983 ┆ 1.0 ┆ 1.0 ┆ … ┆ -0.000022 ┆ 0.000984 ┆ -0.022584 ┆ -0.000022 │
│ 12477 ┆ 1987 ┆ 1.0 ┆ 1.0 ┆ … ┆ -0.000119 ┆ 0.000984 ┆ -0.12045 ┆ -0.000119 │
└───────┴──────┴───────────┴─────────────┴───┴───────────┴─────────────┴─────────────┴─────────────┘