DifferenceInDifferences#
- class causalpy.experiments.diff_in_diff.DifferenceInDifferences[source]#
A class to analyse data from Difference in Difference settings.
Note
There is no pre/post intervention data distinction for DiD, we fit all the data available.
- Parameters:
data (
NativeDataFrame) – Any eager dataframe Narwhals supports, such as pandas, Polars, or PyArrow. Converted to pandas internally.formula (
str) – A statistical model formula.time_variable_name (
str) – Name of the data column for the time variable.group_variable_name (
str) – Name of the data column for the group variable.post_treatment_variable_name (
str) – Name of the data column indicating post-treatment period. Defaults to “post_treatment”.model (
PyMCModel|RegressorMixin|None) – A PyMC model for difference in differences. Defaults to LinearRegression.
Notes
Estimate extraction
Both Bayesian and OLS backends store the fitted group-by-post interaction coefficient as
causal_impact. The class also constructs treated-post counterfactual predictions for visualization by setting that interaction term to zero, but those predictions do not determine the reported scalar effect. In an additive identity-link model, the coefficient and the corresponding predicted contrast are algebraically identical.Examples
>>> import causalpy as cp >>> df = cp.load_data("did") >>> seed = 42 >>> result = cp.DifferenceInDifferences( ... df, ... formula="y ~ 1 + group*post_treatment", ... time_variable_name="t", ... group_variable_name="group", ... model=cp.pymc_models.LinearRegression( ... sample_kwargs={ ... "target_accept": 0.95, ... "random_seed": seed, ... "progressbar": False, ... } ... ), ... )
Methods
Run the experiment algorithm: fit model, predict, and calculate causal impact.
DifferenceInDifferences.effect_summary(*[, ...])Generate a decision-ready summary of causal effects for Difference-in-Differences.
DifferenceInDifferences.generate_report(*[, ...])Generate a self-contained HTML report for this experiment.
Validate the input data and model formula for correctness.
DifferenceInDifferences.plot(*[, round_to, ...])Plot the difference-in-differences results.
Ask the model to print its coefficients.
Set optional maketables rendering options for this experiment.
DifferenceInDifferences.summary([round_to])Print summary of main results and model coefficients.
Attributes
idataReturn fitted DataTree when the model backend supports it.
supports_bayessupports_olssupports_pymc_forecastlabelsdata- __init__(data, formula, time_variable_name, group_variable_name, post_treatment_variable_name='post_treatment', model=None)[source]#
- Parameters:
data (DataFrameLike)
formula (str)
time_variable_name (str)
group_variable_name (str)
post_treatment_variable_name (str)
model (PyMCModel | RegressorMixin | None)
- Return type:
None
- classmethod __new__(*args, **kwargs)#