Skip to main content

Get started

Installation

Install from GitHub, or clone the repository to run the examples locally.

python3 -m pip install "git+https://github.com/Larsvanderlaan/causalCalibration.git#subdirectory=python"

For plotting, install the optional dependencies with python3 -m pip install matplotlib.

install.packages("remotes")
remotes::install_github(
  "Larsvanderlaan/causalCalibration",
  subdir = "r/causalCalibration"
)

For method="isotonic", also install reticulate and the Python package lightgbm:

install.packages("reticulate")
reticulate::py_install("lightgbm")

If you use an existing Python environment, configure reticulate to use the environment where lightgbm is installed.

git clone https://github.com/Larsvanderlaan/causalCalibration.git
cd causalCalibration
python3 -m pip install -e 'python[plot]'

From the same directory, install the R package:

install.packages("r/causalCalibration", repos = NULL, type = "source")

Prepare the inputs

The package calibrates predictions from a treatment-effect model you have already fitted. Each row needs a prediction, a binary treatment indicator, an observed outcome, and nuisance estimates for the chosen loss.

Input Meaning
predictions One treatment-effect prediction per observation
treatment, outcome Treatment coded as 0 or 1, and observed outcome
mu0, mu1 Estimated conditional outcome means under treatment 0 and 1; required for loss="dr"
propensity Estimated probability of treatment 1 given covariates; required for both losses
outcome_mean Estimated conditional outcome mean given covariates; required for loss="r"
fold_predictions For cross-calibration, an n × K matrix with predictions from each fold-specific model
fold_ids Optional fold indices, numbered 1 through K, identifying each row’s out-of-fold prediction

Use cross-fitted nuisance estimates when possible. In a randomized trial, you can supply the known treatment probability as propensity.

Fit and predict

Start with loss="dr" when you want calibration for the original population and overlap is adequate. Use method="isotonic" for a nondecreasing step function. See Losses and methods for alternatives.

The examples below assume tau_hat contains predictions on a calibration sample that was not used to train the treatment-effect model. All other fitting inputs refer to those same observations. tau_new contains predictions for new observations from the same treatment-effect model.

from causal_calibration import fit_calibrator

cal = fit_calibrator(
    predictions=tau_hat,
    treatment=a,
    outcome=y,
    mu0=mu0_hat,
    mu1=mu1_hat,
    propensity=e_hat,
    loss="dr",
    method="isotonic",
)

tau_calibrated = cal.predict(tau_new)
library(causalCalibration)

cal <- fit_calibrator(
  predictions = tau_hat,
  treatment = a,
  outcome = y,
  mu0 = mu0_hat,
  mu1 = mu1_hat,
  propensity = e_hat,
  loss = "dr",
  method = "isotonic"
)

tau_calibrated <- predict(cal, tau_new)

If your model was trained with cross-fitting, follow the cross-calibration guide to use the full sample without setting aside a calibration split.

Check calibration

Use diagnose_calibration() to estimate remaining calibration error, obtain a confidence interval, and compare raw and calibrated scores. All predictions, outcomes, treatment indicators, and nuisance estimates must refer to the same evaluation observations. See Diagnostics for examples and interpretation.

assess_overlap() summarizes propensity-score tails and effective sample sizes. Its recommendation can help you choose between the original and overlap-weighted populations; the screening cutoffs are practical defaults.

Full examples

  • Python notebook: generate data, fit nuisance models, cross-calibrate, and compute diagnostics.
  • R vignette: the corresponding analysis in R.

Optional bundle helpers collect predictions, nuisances, weights, and fold information in one object.