RegressionKink#

class causalpy.experiments.regression_kink.RegressionKink[source]#

A class to analyse regression kink 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.

  • kink_point (float) – A scalar value at which the kink occurs.

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

  • running_variable_name (str) – The name of the running variable column.

  • epsilon (float) – A small scalar for evaluating the causal impact above/below the kink.

  • bandwidth (float) – Data outside of the bandwidth (relative to the kink) is not used to fit the model.

Notes

Lazy lifecycle

Construction only validates input and builds design matrices — nothing is sampled. Call fit() to run posterior inference (it returns self, so construction and fitting chain in one expression), and optionally sample_prior_predictive() first for prior predictive checks (plot(group="prior"), effect_summary(group="prior")). Results live on exp.result / exp.prior_result.

Estimate extraction

The class predicts the conditional expectation at kink_point - epsilon, kink_point, and kink_point + epsilon. It forms finite-difference slopes on the left and right and stores their difference as gradient_change. This is a local prediction contrast on derivatives, not a population-standardized effect.

Examples

>>> import causalpy as cp
>>> df = cp.load_data("rd")
>>> kink = 0.5
>>> result = cp.RegressionKink(
...     df,
...     formula=f"y ~ 1 + x + I((x - {kink}) * treated)",
...     kink_point=kink,
...     model=cp.pymc_models.LinearRegression(
...         sample_kwargs={"random_seed": 42, "progressbar": False}
...     ),
... ).fit()

Methods

RegressionKink.build()

Construct the model graph without sampling anything.

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

Generate a decision-ready summary of causal effects for Regression Kink.

RegressionKink.fit(**kwargs)

Run the posterior phase and populate result.

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

Generate a self-contained HTML report for this experiment.

RegressionKink.input_validation()

Validate the input data and model formula for correctness.

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

Plot the regression kink results.

RegressionKink.print_coefficients([round_to])

Ask the model to print its posterior coefficients.

RegressionKink.sample_prior_predictive(**kwargs)

Run the optional prior phase and populate prior_result.

RegressionKink.set_maketables_options(*[, ...])

Set optional maketables rendering options for this experiment.

RegressionKink.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, kink_point, model=None, running_variable_name='x', epsilon=0.001, bandwidth=inf)[source]#
Parameters:
Return type:

None

classmethod __new__(*args, **kwargs)#