PrePostNEGD#

class causalpy.experiments.prepostnegd.PrePostNEGD[source]#

A class to analyse data from pretest/posttest designs.

Parameters:
  • data (NativeDataFrame) – Any eager dataframe Narwhals supports, such as pandas, Polars, or PyArrow. Converted to pandas internally.

  • formula (str) – A statistical model formula.

  • group_variable_name (str) – Name of the column in data for the group variable; should be either binary or boolean.

  • pretreatment_variable_name (str) – Name of the column in data for the pretreatment variable.

  • model (PyMCModel | None) – A PyMC model. Defaults to LinearRegression.

Notes

Estimate extraction

The reported causal_impact is the posterior coefficient on the treatment-group term, conditional on the pretreatment outcome and any other formula covariates. Treated and untreated prediction curves are also computed for visualization, but they do not determine the reported scalar effect. With the current additive identity-link model, the treatment coefficient equals the corresponding conditional prediction contrast.

Lazy lifecycle: construction only validates inputs and builds design matrices — nothing is sampled. Call fit() to draw posterior samples (and sample_prior_predictive() for prior predictive checks); read methods such as plot(), summary(), and effect_summary() require the corresponding draw group.

Examples

>>> import causalpy as cp
>>> df = cp.load_data("anova1")
>>> seed = 42
>>> result = cp.PrePostNEGD(
...     df,
...     formula="post ~ 1 + C(group) + pre",
...     group_variable_name="group",
...     pretreatment_variable_name="pre",
...     model=cp.pymc_models.LinearRegression(
...         sample_kwargs={
...             "target_accept": 0.95,
...             "random_seed": seed,
...             "progressbar": False,
...         }
...     ),
... ).fit()
>>> result.summary(round_to=1)
==================Pretest/posttest Nonequivalent Group Design===================
Formula: post ~ 1 + C(group) + pre

Results:
Causal impact = 2, $CI_{94%}$[2, 2]
Model coefficients:
    Intercept      -0.5, 94% HDI [-1, 0.2]
    C(group)[T.1]  2, 94% HDI [2, 2]
    pre            1, 94% HDI [1, 1]
    y_hat_sigma    0.5, 94% HDI [0.5, 0.6]

Methods

PrePostNEGD.build()

Construct the model graph without sampling anything.

PrePostNEGD.effect_summary(*[, group, ...])

Generate a decision-ready summary of causal effects for PrePostNEGD.

PrePostNEGD.fit(**kwargs)

Run the posterior phase and populate result.

PrePostNEGD.generate_report(*[, ...])

Generate a self-contained HTML report for this experiment.

PrePostNEGD.input_validation()

Validate the input data and model formula for correctness.

PrePostNEGD.plot(*[, group, round_to, ...])

Plot the pre-post non-equivalent group design results.

PrePostNEGD.print_coefficients([round_to])

Ask the model to print its posterior coefficients.

PrePostNEGD.sample_prior_predictive(**kwargs)

Run the optional prior phase and populate prior_result.

PrePostNEGD.set_maketables_options(*[, hdi_prob])

Set optional maketables rendering options for this experiment.

PrePostNEGD.summary([round_to])

Print summary of main results and model coefficients.

Attributes

has_prior_predictive

Whether the prior phase has run (draws, and bundle where kept).

idata

Return fitted DataTree when the model backend supports it.

is_built

Whether the model graph / fit design exists (no draws implied).

is_configured

design matrices are ready.

is_fitted

Whether posterior draws and the posterior result bundle exist.

model

The underlying model instance.

prior_result

Prior-group result bundle; raises before prior sampling.

result

Posterior-group result bundle; raises before fit().

supports_bayes

supports_ols

supports_pymc_forecast

labels

data

__init__(data, formula, group_variable_name, pretreatment_variable_name, model=None)[source]#
Parameters:
Return type:

None

classmethod __new__(*args, **kwargs)#