EventStudy#
- class causalpy.experiments.event_study.EventStudy[source]#
A class to analyze data from Event Study / Dynamic DiD settings.
Event studies estimate dynamic treatment effects over “event time” (time relative to treatment). This allows researchers to:
Examine pre-treatment trends (placebo checks for parallel trends assumption)
Estimate how treatment effects evolve over time after treatment
Visualize the full time path of causal effects
The model estimates:
\[Y_{it} = \alpha_i + \lambda_t + \sum_{k \neq k_0} \beta_k \cdot \mathbf{1}\{E_{it} = k\} + \varepsilon_{it}\]where: - \(\alpha_i\) are unit fixed effects - \(\lambda_t\) are time fixed effects - \(E_{it} = t - G_i\) is event time (time relative to treatment) - \(\beta_k\) are the dynamic treatment effects at event time k - \(k_0\) is the reference (omitted) event time - \(\mathbf{1}\{E_{it} = k\}\) is the indicator function: equals 1 when the
condition \(E_{it} = k\) is true (i.e., when observation it is at event time k), and 0 otherwise
Implementation via dummy variables: The indicator function notation is equivalent to creating dummy (binary) variables for each event time. Internally, this class creates one dummy variable for each event time k in the event window, where the dummy equals 1 for treated observations at that specific event time and 0 otherwise. One event time (the reference period, typically k=-1) is omitted to avoid perfect multicollinearity. The estimated regression coefficient \(\beta_k\) for each dummy variable represents the Average Treatment Effect on the Treated (ATT) at event time k, measured relative to the reference period.
Warning
This implementation uses a standard two-way fixed effects (TWFE) estimator, which requires simultaneous treatment timing (all treated units receive treatment at the same time). Staggered adoption designs, where different units are treated at different times, can produce biased estimates when treatment effects vary across cohorts. See Sun & Abraham (2021) for details.
- Parameters:
data (pd.DataFrame) – Panel data with unit, time, outcome, and treatment time columns.
formula (str) – A patsy-style formula specifying the model. Should include the outcome variable on the left-hand side and fixed effects on the right-hand side. Use
C(column)syntax for categorical fixed effects. Example:"y ~ C(unit) + C(time)". Event-time dummies are added automatically by the class.unit_col (str) – Name of the column identifying units (must match a term in the formula).
time_col (str) – Name of the column identifying time periods (must match a term in the formula).
treat_time_col (str) – Name of the column containing treatment time for each unit. Use NaN or np.inf for never-treated (control) units.
event_window (tuple[int, int]) – Range of event times to include: (K_min, K_max). Default is (-5, 5).
reference_event_time (int) – Event time to use as reference (omitted) category. Default is -1 (one period before treatment).
model (PyMCModel or RegressorMixin, optional) – Model for estimation. Defaults to None.
Example
>>> import causalpy as cp >>> from causalpy.data.simulate_data import generate_event_study_data >>> df = generate_event_study_data( ... n_units=20, n_time=20, treatment_time=10, seed=42 ... ) >>> result = cp.EventStudy( ... df, ... formula="y ~ C(unit) + C(time)", ... unit_col="unit", ... time_col="time", ... treat_time_col="treat_time", ... event_window=(-5, 5), ... reference_event_time=-1, ... model=cp.pymc_models.LinearRegression( ... sample_kwargs={ ... "tune": 20, ... "draws": 20, ... "chains": 2, ... "progressbar": False, ... "random_seed": 42, ... } ... ), ... )
Methods
EventStudy.__init__(data, formula, unit_col, ...)EventStudy.effect_summary([window, ...])Generate a decision-ready summary of causal effects.
EventStudy.fit(*args, **kwargs)Get event-time coefficients as a DataFrame.
EventStudy.get_plot_data(*args, **kwargs)Recover the data of an experiment along with the prediction and causal impact information.
EventStudy.get_plot_data_bayesian([hdi_prob])Get plot data for Bayesian model.
EventStudy.get_plot_data_ols(**kwargs)Get plot data for OLS model.
Validate input data, formula, and parameters.
EventStudy.plot(*args, **kwargs)Plot the model.
EventStudy.print_coefficients([round_to])Ask the model to print its coefficients.
EventStudy.summary([round_to, hdi_prob])Print summary of event-time coefficients.
Attributes
idataReturn the InferenceData object of the model.
supports_bayessupports_olslabels- __init__(data, formula, unit_col, time_col, treat_time_col, event_window=(-5, 5), reference_event_time=-1, model=None, **kwargs)[source]#
- classmethod __new__(*args, **kwargs)#