DIDMultiplegtStat — class API¶
The scikit-learn style class is the recommended entry point.
Constructor¶
DIDMultiplegtStat ¶
DIDMultiplegtStat(estimator: str | Sequence[str] | None = None, order: int | list[int] = 1, noextrapolation: bool = False, placebo: int = 0, switchers: str | None = None, disaggregate: bool = False, as_vs_was: bool = False, exact_match: bool = False, by: Sequence[str] | None = None, by_fd: int | None = None, by_baseline: int | None = None, other_treatments: Sequence[str] | None = None, cluster: str | None = None, weight: str | None = None, controls: Sequence[str] | None = None, cross_fitting: int = 0, trimming: float = 0, on_placebo_sample: bool = False, bootstrap: int = 0, twfe: bool | dict[str, Any] = False, seed: int = 0, cross_validation: dict[str, Any] | None = None, iv_method: str = 'manual', asinstata: bool = False, model_deltay: Any | None = None, model_stayer: Any | None = None, **legacy_options: Any)
Difference-in-Differences estimator following de Chaisemartin & D'Haultfeuille (2024).
Implements AS (Average Slope), WAS (Weighted Average Slope), and IV-WAS estimators. Doubly robust estimation is used by default; exact matching activates regression adjustment internally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimator
|
str or list of str
|
Estimator type(s): 'as', 'was', or 'iv-was'. Default: ['as', 'was'], or ['iv-was'] if Z is provided in fit(). |
None
|
order
|
int or list of int
|
Polynomial order. Can be single int or list of 4 (reg, logit_bis, logit_Plus, logit_Minus) or 8 (4 for first-stage + 4 for reduced-form for IV-WAS). |
1
|
noextrapolation
|
bool
|
Restrict to common support without extrapolation. |
False
|
placebo
|
int
|
Number of placebo tests. |
0
|
switchers
|
str
|
Restrict to 'up' (increasing treatment) or 'down' (decreasing treatment). |
None
|
disaggregate
|
bool
|
Report period-specific estimates. |
False
|
as_vs_was
|
bool
|
Test equality between AS and WAS. |
False
|
exact_match
|
bool
|
Use exact matching on baseline treatment. |
False
|
by
|
list of str
|
Stratification variables. |
None
|
by_fd
|
int
|
Number of bins for first-difference quantiles. |
None
|
by_baseline
|
int
|
Number of bins for baseline treatment quantiles. |
None
|
other_treatments
|
list of str
|
Additional treatment variables to control for. |
None
|
cluster
|
str
|
Cluster variable for standard errors. |
None
|
weight
|
str
|
Observation weights variable. |
None
|
controls
|
list of str
|
Control variables. |
None
|
cross_fitting
|
int
|
Number of cross-fitting folds. |
0
|
trimming
|
float
|
Propensity score trimming threshold. |
0
|
on_placebo_sample
|
bool
|
Estimate only on stayer sample. |
False
|
bootstrap
|
int
|
Number of bootstrap replications. |
0
|
twfe
|
bool or dict
|
Compare with TWFE regression. |
False
|
seed
|
int
|
Random seed for reproducibility. |
0
|
cross_validation
|
dict
|
Cross-validation options for polynomial order selection. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
results_ |
dict
|
Full results dictionary after fitting. |
table_ |
DataFrame
|
Main results table with Estimate, SE, LB CI, UB CI, Switchers, Stayers. |
placebo_tables_ |
dict
|
Placebo test results by index. |
n_obs_ |
int
|
Number of observations. |
n_clusters_ |
int or None
|
Number of clusters (if clustered). |
by_levels_ |
list or None
|
Levels of by-group analysis. |
first_stage_ |
DIDMultiplegtStat or None
|
First-stage results for IV-WAS. |
is_fitted_ |
bool
|
Whether the model has been fitted. |
Examples:
>>> import pandas as pd
>>> from did_multiplegt_stat import DIDMultiplegtStat
>>>
>>> # Basic usage
>>> model = DIDMultiplegtStat(estimator=['as', 'was'])
>>> model.fit(df, Y='outcome', ID='unit_id', Time='time', D='treatment')
>>> model.summary()
>>>
>>> # With IV
>>> model_iv = DIDMultiplegtStat(estimator='iv-was')
>>> model_iv.fit(df, Y='outcome', ID='unit_id', Time='time', D='treatment', Z='instrument')
>>> model_iv.plot()
Initialize the estimator with configuration parameters.
Source code in src/did_multiplegt_stat/estimator.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
Fitting¶
fit ¶
Fit the DiD estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Panel data in long format. |
required |
Y
|
str
|
Column name for outcome variable. |
required |
ID
|
str
|
Column name for unit identifier. |
required |
Time
|
str
|
Column name for time variable. |
required |
D
|
str
|
Column name for treatment variable. |
required |
Z
|
str
|
Column name for instrument variable (required for IV-WAS). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
DIDMultiplegtStat
|
Fitted estimator (scikit-learn convention). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If invalid parameter combinations are specified. |
Source code in src/did_multiplegt_stat/estimator.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
Inspection¶
summary ¶
Print formatted summary mimicking Stata ADO output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show_header
|
bool
|
Display summary statistics header. |
True
|
show_placebo
|
bool
|
Display placebo results if available. |
True
|
show_warnings
|
bool
|
Display warnings about quasi-stayers, common support violations. |
True
|
Source code in src/did_multiplegt_stat/estimator.py
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
to_dataframe ¶
Return main results as a clean DataFrame.
Returns:
| Name | Type | Description |
|---|---|---|
df |
DataFrame
|
Results table with Estimate, SE, LB CI, UB CI, Switchers, Stayers. |
Source code in src/did_multiplegt_stat/estimator.py
get_coefficients ¶
Get coefficient estimates for specified estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimator
|
str
|
Which estimator ('as', 'was', or 'iv-was'). Default: first available. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
coeffs |
Series
|
Coefficient estimates. |
Source code in src/did_multiplegt_stat/estimator.py
get_confidence_intervals ¶
Get confidence intervals at specified level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimator
|
str
|
Which estimator. Default: first available. |
None
|
level
|
float
|
Confidence level (e.g., 0.95 for 95% CI). |
0.95
|
Returns:
| Name | Type | Description |
|---|---|---|
ci |
DataFrame
|
DataFrame with columns 'LB CI' and 'UB CI'. |
Source code in src/did_multiplegt_stat/estimator.py
Plotting¶
plot ¶
plot(estimator: str | None = None, show_ci: bool = True, ci_alpha: float = 0.2, figsize: tuple[float, float] = (10, 6), colors: dict[str, str] | None = None, title: str | None = None, xlabel: str = 'Relative Time', ylabel: str = 'Effect Estimate', show_zero_line: bool = True, separate_panels: bool = False, save_path: str | None = None, dpi: int = 150) -> plt.Figure | dict[str, plt.Figure]
Generate event-study style plots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimator
|
str
|
Which estimator to plot ('as', 'was', or 'iv-was'). If None, plots all. |
None
|
show_ci
|
bool
|
Display confidence interval bands. |
True
|
ci_alpha
|
float
|
Transparency for CI bands. |
0.2
|
figsize
|
tuple
|
Figure size in inches. |
(10, 6)
|
colors
|
dict
|
Custom colors for estimators {'as': 'blue', 'was': 'red', ...}. |
None
|
title
|
str
|
Custom title. Default: auto-generated based on estimator. |
None
|
xlabel
|
str
|
X-axis label. |
"Relative Time"
|
ylabel
|
str
|
Y-axis label. |
"Effect Estimate"
|
show_zero_line
|
bool
|
Show horizontal line at y=0. |
True
|
separate_panels
|
bool
|
Create separate subplots for AS/WAS/IV-WAS. |
False
|
save_path
|
str
|
Path to save figure. |
None
|
dpi
|
int
|
Resolution for saved figure. |
150
|
Returns:
| Name | Type | Description |
|---|---|---|
fig |
Figure or dict
|
Single figure or dict of figures if separate_panels=True. |
Source code in src/did_multiplegt_stat/estimator.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | |
plot_comparison ¶
plot_comparison(estimators: list[str] | None = None, figsize: tuple[float, float] = (10, 6), title: str = 'Estimator Comparison', save_path: str | None = None, dpi: int = 150) -> plt.Figure
Generate a side-by-side comparison plot of different estimators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimators
|
list
|
Which estimators to compare. Default: all available. |
None
|
figsize
|
tuple
|
Figure size. |
(10, 6)
|
title
|
str
|
Plot title. |
"Estimator Comparison"
|
save_path
|
str
|
Path to save figure. |
None
|
dpi
|
int
|
Resolution. |
150
|
Returns:
| Name | Type | Description |
|---|---|---|
fig |
Figure
|
Matplotlib figure. |
Source code in src/did_multiplegt_stat/estimator.py
Parameter management (sklearn-style)¶
get_params ¶
Get parameters for this estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deep
|
bool
|
If True, return parameters for sub-objects. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
params |
dict
|
Parameter names mapped to their values. |
Source code in src/did_multiplegt_stat/estimator.py
set_params ¶
Set the parameters of this estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**params
|
dict
|
Estimator parameters. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
DIDMultiplegtStat
|
Estimator instance. |
Source code in src/did_multiplegt_stat/estimator.py
Attributes (set after fit)¶
| Attribute | Type | Description |
|---|---|---|
results_ |
dict |
Full results dictionary (source of truth). |
table_ |
pd.DataFrame |
Main results table: Estimate, SE, LB CI, UB CI, Switchers, Stayers. |
placebo_tables_ |
dict[int, pd.DataFrame] \| None |
Placebo tables keyed by placebo index. |
n_obs_ |
int |
Number of observations. |
n_clusters_ |
int \| None |
Number of clusters, when cluster= is set. |
by_levels_ |
list \| None |
Levels of by-group analysis. |
first_stage_ |
DIDMultiplegtStat \| None |
Nested fitted model for the first stage of IV-WAS. |
is_fitted_ |
bool |
Whether .fit() has been called. |