| Title: | Causal Machine Learning in R |
|---|---|
| Description: | Estimation and evaluation of average and conditional average treatment effects (ATE/CATE) from observational data using machine learning. Provides classical estimators (naive difference in means, inverse propensity weighting, doubly robust / AIPW, double machine learning) and meta-learners (S-, T- and X-Learner), all built on top of the 'mlr3' ecosystem so that any regression or classification learner can be plugged in as a nuisance model. Also includes causal model evaluation utilities (ATE error, PEHE, R-Loss) with optional cross-fitting, and a collection of benchmark datasets commonly used for teaching machine learning for causal inference. |
| Authors: | Damian Machlanski [aut, cre] |
| Maintainer: | Damian Machlanski <[email protected]> |
| License: | LGPL (>= 2.1) |
| Version: | 0.1.0 |
| Built: | 2026-07-15 17:00:46 UTC |
| Source: | https://github.com/dmachlanski/causalmlr |
The classic UCI abalone dataset: predict the number of rings (a proxy for age) of abalone from physical measurements. Included as standard supervised-learning practice data; it has no causal structure.
abaloneabalone
A data frame with 4,177 rows and 9 columns: sex (factor),
seven physical measurements and the target rings (integer).
https://archive.ics.uci.edu/dataset/1/abalone
data(abalone) head(abalone)data(abalone) head(abalone)
Averages the CATE predictions of a fitted meta-learner over the rows of
newdata:
ate(object, ...) ## S3 method for class 'cate_learner' ate(object, newdata, ...)ate(object, ...) ## S3 method for class 'cate_learner' ate(object, newdata, ...)
object |
A fitted CATE model, e.g. from |
... |
Passed on to methods. |
newdata |
A |
A single numeric value.
library(mlr3) data(synth_train) m <- s_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart")) ate(m, synth_train)library(mlr3) data(synth_train) m <- s_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart")) ate(m, synth_train)
Estimates the average treatment effect in the partially linear model
using the residual-on-residual
(orthogonalised) estimator of Chernozhukov et al. (2018). Two nuisance
functions are estimated with machine learning and cross-fitting:
the conditional outcome mean and the propensity
score . The ATE is then
ate_dml( data, outcome = "y", treatment = "t", outcome_learner, ps_learner, covariates = NULL, folds = 5, ps_trim = NULL )ate_dml( data, outcome = "y", treatment = "t", outcome_learner, ps_learner, covariates = NULL, folds = 5, ps_trim = NULL )
data |
A |
outcome |
Name of the outcome column. Default |
treatment |
Name of the binary (0/1) treatment column. Default |
outcome_learner |
An mlr3 regression learner for the conditional
outcome mean |
ps_learner |
An mlr3 classification learner used as the propensity
score model, e.g. |
covariates |
Optional character vector of covariate columns. Defaults to all columns except the outcome and the treatment. |
folds |
Number of cross-fitting folds. Default |
ps_trim |
Optional trimming for the estimated propensity scores.
Either a single value |
Note that unlike ate_dr(), the outcome model here regresses Y on the
covariates only (the treatment is excluded), and the model assumes a
constant (homogeneous) treatment effect.
An object of class "causalmlr_ate"; see ate_naive().
Chernozhukov, V., Chetverikov, D., Demirer, M., Duflo, E., Hansen, C., Newey, W., & Robins, J. (2018). Double/debiased machine learning for treatment and structural parameters. The Econometrics Journal, 21(1), C1-C68.
library(mlr3) data(sodium) set.seed(1) ate_dml(sodium, outcome = "bp", treatment = "sodium", outcome_learner = lrn("regr.rpart"), ps_learner = lrn("classif.rpart"), ps_trim = 0.01)library(mlr3) data(sodium) set.seed(1) ate_dml(sodium, outcome = "bp", treatment = "sodium", outcome_learner = lrn("regr.rpart"), ps_learner = lrn("classif.rpart"), ps_trim = 0.01)
Combines an outcome model with a
propensity score model in the augmented
inverse propensity weighting (AIPW) estimator:
The estimator is consistent if either the outcome model or the propensity
model is correctly specified (hence "doubly robust"). With folds > 1
both nuisance models are cross-fitted, which yields the cross-fitted AIPW
estimator (also known as DML for the interactive/fully heterogeneous
model).
ate_dr( data, outcome = "y", treatment = "t", outcome_learner, ps_learner, covariates = NULL, folds = 1, ps_trim = NULL )ate_dr( data, outcome = "y", treatment = "t", outcome_learner, ps_learner, covariates = NULL, folds = 1, ps_trim = NULL )
data |
A |
outcome |
Name of the outcome column. Default |
treatment |
Name of the binary (0/1) treatment column. Default |
outcome_learner |
An mlr3 regression learner for the outcome model,
e.g. |
ps_learner |
An mlr3 classification learner used as the propensity
score model, e.g. |
covariates |
Optional character vector of covariate columns. Defaults to all columns except the outcome and the treatment. |
folds |
Number of cross-fitting folds for the propensity model.
|
ps_trim |
Optional trimming for the estimated propensity scores.
Either a single value |
An object of class "causalmlr_ate"; see ate_naive().
Robins, J. M., Rotnitzky, A., & Zhao, L. P. (1994). Estimation of regression coefficients when some regressors are not always observed. Journal of the American Statistical Association, 89(427), 846-866.
library(mlr3) data(sodium) ate_dr(sodium, outcome = "bp", treatment = "sodium", outcome_learner = lrn("regr.rpart"), ps_learner = lrn("classif.rpart"), folds = 5, ps_trim = 0.01)library(mlr3) data(sodium) ate_dr(sodium, outcome = "bp", treatment = "sodium", outcome_learner = lrn("regr.rpart"), ps_learner = lrn("classif.rpart"), folds = 5, ps_trim = 0.01)
Estimates the average treatment effect by weighting outcomes with the
inverse of the estimated propensity score
:
The propensity score is estimated with any mlr3 classification learner.
ate_ipw( data, outcome = "y", treatment = "t", ps_learner, covariates = NULL, folds = 1, ps_trim = NULL )ate_ipw( data, outcome = "y", treatment = "t", ps_learner, covariates = NULL, folds = 1, ps_trim = NULL )
data |
A |
outcome |
Name of the outcome column. Default |
treatment |
Name of the binary (0/1) treatment column. Default |
ps_learner |
An mlr3 classification learner used as the propensity
score model, e.g. |
covariates |
Optional character vector of covariate columns. Defaults to all columns except the outcome and the treatment. |
folds |
Number of cross-fitting folds for the propensity model.
|
ps_trim |
Optional trimming for the estimated propensity scores.
Either a single value |
An object of class "causalmlr_ate"; see ate_naive().
library(mlr3) data(sodium) ate_ipw(sodium, outcome = "bp", treatment = "sodium", ps_learner = lrn("classif.rpart"))library(mlr3) data(sodium) ate_ipw(sodium, outcome = "bp", treatment = "sodium", ps_learner = lrn("classif.rpart"))
Estimates the average treatment effect as the difference between the mean
outcome of the treated and the mean outcome of the controls. This estimator
is unbiased only when the treatment is randomised; under confounding it is
typically biased and serves as a baseline for the adjusted estimators
(ate_ipw(), ate_dr(), ate_dml()).
ate_naive(data, outcome = "y", treatment = "t")ate_naive(data, outcome = "y", treatment = "t")
data |
A |
outcome |
Name of the outcome column. Default |
treatment |
Name of the binary (0/1) treatment column. Default |
An object of class "causalmlr_ate" with elements estimate,
se (standard error), n and method. Use coef() to extract the
point estimate and confint() for a confidence interval.
ate_ipw(), ate_dr(), ate_dml()
data(sodium) ate_naive(sodium, outcome = "bp", treatment = "sodium")data(sodium) ate_naive(sodium, outcome = "bp", treatment = "sodium")
Attaches pointwise confidence intervals to the CATE predictions of any
fitted meta-learner (s_learner(), t_learner(), x_learner(),
dr_learner(), r_learner()) using the nonparametric bootstrap. For each
of n_boot replicates the training data is resampled with replacement, the
entire learner pipeline (including any nuisance models and cross-fitting) is
refitted, and CATEs are predicted for newdata. A pointwise interval is
then formed from the bootstrap distribution at each row of newdata.
cate_ci( object, newdata, train_data, n_boot = 200, level = 0.95, type = c("percentile", "normal"), ... )cate_ci( object, newdata, train_data, n_boot = 200, level = 0.95, type = c("percentile", "normal"), ... )
object |
A fitted meta-learner of class |
newdata |
A |
train_data |
The |
n_boot |
Number of bootstrap replicates. Default |
level |
Confidence level. Default |
type |
Interval type. |
... |
Ignored. |
Because the meta-learners accept arbitrary (and typically non-smooth)
machine-learning base learners, no closed-form standard error is available
for ; the bootstrap is the one mechanism that applies
uniformly across all five learners. Two caveats are worth keeping in mind.
First, bootstrap coverage for CATEs estimated with flexible learners is only
approximate and can be anti-conservative, so the intervals are best read as
a measure of estimation stability rather than exact frequentist coverage.
Second, refitting the full pipeline n_boot times is computationally
expensive. Set the seed with set.seed() beforehand for reproducibility.
A data.frame with one row per row of newdata and columns
estimate (the point CATE from object), se (the bootstrap standard
error) and lower/upper (the confidence limits).
s_learner(), t_learner(), x_learner(), dr_learner(),
r_learner()
library(mlr3) data(synth_train) data(synth_test) set.seed(1) m <- t_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart")) ci <- cate_ci(m, synth_test, train_data = synth_train, n_boot = 50) head(ci)library(mlr3) data(synth_train) data(synth_test) set.seed(1) m <- t_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart")) ci <- cate_ci(m, synth_test, train_data = synth_train, n_boot = 50) head(ci)
Diagnostic measurements for predicting diabetes onset. Included as standard supervised-learning practice data; it has no causal structure.
diabetesdiabetes
A data frame with 768 rows and 9 columns: eight numeric
diagnostic measurements and the target diabetes (factor, 0/1).
data(diabetes) head(diabetes)data(diabetes) head(diabetes)
The DR-Learner ("doubly robust learner"; Kennedy, 2023) is a two-stage
meta-learner that turns the augmented inverse propensity weighting (AIPW)
score used by ate_dr() into a model that predicts individual CATEs.
Cross-fit the nuisance functions: the propensity score
and the potential-outcome models
, from a single outcome model that
includes the treatment as a feature.
Form the doubly robust pseudo-outcome
and regress it on the covariates to obtain the final CATE model
.
dr_learner( data, outcome = "y", treatment = "t", outcome_learner, ps_learner, tau_learner = NULL, covariates = NULL, folds = 5, ps_trim = NULL ) ## S3 method for class 'dr_learner' predict(object, newdata, ...)dr_learner( data, outcome = "y", treatment = "t", outcome_learner, ps_learner, tau_learner = NULL, covariates = NULL, folds = 5, ps_trim = NULL ) ## S3 method for class 'dr_learner' predict(object, newdata, ...)
data |
A |
outcome |
Name of the outcome column. Default |
treatment |
Name of the binary (0/1) treatment column. Default |
outcome_learner |
An mlr3 regression learner for the stage-1 outcome
model, e.g. |
ps_learner |
An mlr3 classification learner used as the propensity
score model, e.g. |
tau_learner |
Optional mlr3 regression learner for the second-stage
pseudo-outcome regression. Defaults to a clone of |
covariates |
Optional character vector of covariate columns. Defaults to all columns except the outcome and the treatment. |
folds |
Number of cross-fitting folds for the nuisance models.
Default |
ps_trim |
Optional trimming for the estimated propensity scores.
Either a single value |
object |
A fitted |
newdata |
A |
... |
Ignored. |
Because the pseudo-outcome has conditional mean equal to the true CATE
whenever either nuisance is correctly specified, the second-stage
regression targets the CATE directly. Cross-fitting the nuisances (the
default folds = 5) makes the pseudo-outcome orthogonal to nuisance
estimation error, so flexible learners can be used without overfitting
bias. Unlike ate_dr(), which averages to a scalar ATE, the
DR-Learner keeps the second-stage model and can therefore predict CATEs on
new test data.
A fitted CATE model of class c("dr_learner", "cate_learner").
Use predict() to obtain CATE estimates for new data and ate() for
their average.
predict(dr_learner): Predict CATEs for new data.
Kennedy, E. H. (2023). Towards optimal doubly robust estimation of heterogeneous causal effects. Electronic Journal of Statistics, 17(2), 3008-3049.
s_learner(), t_learner(), x_learner(), r_learner(),
ate_dr()
library(mlr3) data(synth_train) data(synth_test) set.seed(1) m <- dr_learner(synth_train, outcome = "y", treatment = "t", outcome_learner = lrn("regr.rpart"), ps_learner = lrn("classif.rpart"), ps_trim = 0.01) tau_hat <- predict(m, synth_test) pehe(synth_test$tau, tau_hat)library(mlr3) data(synth_train) data(synth_test) set.seed(1) m <- dr_learner(synth_train, outcome = "y", treatment = "t", outcome_learner = lrn("regr.rpart"), ps_learner = lrn("classif.rpart"), ps_trim = 0.01) tau_hat <- predict(m, synth_test) pehe(synth_test$tau, tau_hat)
Measures the absolute difference between an estimated and the true average treatment effect:
Requires the true ATE, so it is only usable with (semi-)synthetic benchmark data such as sodium or ihdp_train.
eps_ate(ate_true, ate_pred)eps_ate(ate_true, ate_pred)
ate_true |
True ATE (a single number). |
ate_pred |
Estimated ATE. Either a number or a |
A single non-negative number.
data(sodium) est <- ate_naive(sodium, outcome = "bp", treatment = "sodium") eps_ate(1.05, est)data(sodium) est <- ate_naive(sodium, outcome = "bp", treatment = "sodium") eps_ate(1.05, est)
The Boston housing dataset (Harrison & Rubinfeld, 1978): predict median
house value (MEDV) from neighbourhood characteristics. Included as
standard supervised-learning practice data. Note that this dataset has
known ethical concerns (the B column encodes a racial statistic) and is
provided for teaching purposes only.
housinghousing
A data frame with 506 rows and 14 columns.
data(housing) head(housing)data(housing) head(housing)
Held-out portion of the IHDP benchmark; see ihdp_train for details.
ihdp_testihdp_test
A data frame with 75 rows and 28 columns; same columns as ihdp_train.
Hill, J. L. (2011). Bayesian nonparametric modeling for causal inference. Journal of Computational and Graphical Statistics, 20(1), 217-240.
data(ihdp_test) head(ihdp_test)data(ihdp_test) head(ihdp_test)
The Infant Health and Development Program (IHDP) benchmark introduced by Hill (2011): real covariates describing children and their mothers from a randomised experiment, combined with a simulated outcome, which provides ground-truth treatment effects. The treatment is intensive, high-quality childcare and home visits; the (simulated) outcome mimics future cognitive test scores.
ihdp_trainihdp_train
A data frame with 672 rows and 28 columns:
Continuous covariates (standardised).
Binary covariates.
Binary treatment indicator (0/1).
Continuous (simulated) outcome.
True conditional average treatment effect.
Hill, J. L. (2011). Bayesian nonparametric modeling for causal inference. Journal of Computational and Graphical Statistics, 20(1), 217-240.
data(ihdp_train) head(ihdp_train)data(ihdp_train) head(ihdp_train)
Held-out portion of the Jobs benchmark; see jobs_train for details.
jobs_testjobs_test
A data frame with 321 rows and 20 columns; same columns as jobs_train.
LaLonde, R. J. (1986). Evaluating the econometric evaluations of training programs with experimental data. The American Economic Review, 76(4), 604-620.
Shalit, U., Johansson, F. D., & Sontag, D. (2017). Estimating individual treatment effect: generalization bounds and algorithms. Proceedings of the 34th International Conference on Machine Learning.
data(jobs_test) head(jobs_test)data(jobs_test) head(jobs_test)
The Jobs benchmark (LaLonde, 1986; composition of Shalit et al., 2017)
combines a randomised experiment (the National Supported Work programme)
with observational controls. The treatment is participation in a job
training programme and the outcome is employment status. The e column
flags units belonging to the randomised subsample, which enables
evaluation on real data via the experimental subset.
jobs_trainjobs_train
A data frame with 2,891 rows and 20 columns:
Covariates (standardised continuous and binary).
Binary treatment indicator: job training (0/1).
Binary outcome: employment (0/1).
Indicator of membership in the randomised experimental subsample (0/1).
LaLonde, R. J. (1986). Evaluating the econometric evaluations of training programs with experimental data. The American Economic Review, 76(4), 604-620.
Shalit, U., Johansson, F. D., & Sontag, D. (2017). Estimating individual treatment effect: generalization bounds and algorithms. Proceedings of the 34th International Conference on Machine Learning.
data(jobs_train) head(jobs_train)data(jobs_train) head(jobs_train)
The root mean squared error between true and predicted conditional average treatment effects:
Requires the true CATEs, so it is only usable with (semi-)synthetic benchmark data such as synth_test or ihdp_test.
pehe(tau_true, tau_pred)pehe(tau_true, tau_pred)
tau_true |
Numeric vector of true CATEs. |
tau_pred |
Numeric vector of predicted CATEs (same length). |
A single non-negative number.
Hill, J. L. (2011). Bayesian nonparametric modeling for causal inference. Journal of Computational and Graphical Statistics, 20(1), 217-240.
pehe(c(1, 2, 3), c(1.1, 1.8, 3.2))pehe(c(1, 2, 3), c(1.1, 1.8, 3.2))
The R-Learner (Nie & Wager, 2021) is the heterogeneous-effect
generalisation of the partially linear DML estimator (ate_dml()). It
builds on the Robinson decomposition of the model
:
Cross-fit the two nuisance functions, the conditional outcome mean
(the treatment is excluded from the
features) and the propensity score ,
and form the residuals and
.
Estimate the CATE by minimising the R-Loss
which is fitted as a weighted regression of the pseudo-outcome
on the covariates with weights
.
r_learner( data, outcome = "y", treatment = "t", outcome_learner, ps_learner, tau_learner = NULL, covariates = NULL, folds = 5, ps_trim = NULL ) ## S3 method for class 'r_learner' predict(object, newdata, ...)r_learner( data, outcome = "y", treatment = "t", outcome_learner, ps_learner, tau_learner = NULL, covariates = NULL, folds = 5, ps_trim = NULL ) ## S3 method for class 'r_learner' predict(object, newdata, ...)
data |
A |
outcome |
Name of the outcome column. Default |
treatment |
Name of the binary (0/1) treatment column. Default |
outcome_learner |
An mlr3 regression learner for the conditional
outcome mean |
ps_learner |
An mlr3 classification learner used as the propensity
score model, e.g. |
tau_learner |
Optional mlr3 regression learner for the second-stage
weighted pseudo-outcome regression. Defaults to a clone of
|
covariates |
Optional character vector of covariate columns. Defaults to all columns except the outcome and the treatment. |
folds |
Number of cross-fitting folds for the nuisance models.
Default |
ps_trim |
Optional trimming for the estimated propensity scores.
Either a single value |
object |
A fitted |
newdata |
A |
... |
Ignored. |
The objective minimised here is exactly the r_loss() used elsewhere for
model selection, so the R-Learner is the estimator that directly targets
that score. When the treatment effect is constant it reduces to
ate_dml(); unlike that scalar estimator, the R-Learner keeps the
second-stage model and can predict CATEs on new test data.
The weighted formulation requires a tau_learner that supports
observation weights ("weights" %in% learner$properties). If it does not,
the R-Learner falls back to an unweighted regression of the pseudo-outcome
and issues a warning. As with ate_dml(), small inflate
the pseudo-outcome, so ps_trim is recommended with flexible propensity
learners.
A fitted CATE model of class c("r_learner", "cate_learner").
Use predict() to obtain CATE estimates for new data and ate() for
their average.
predict(r_learner): Predict CATEs for new data.
Nie, X., & Wager, S. (2021). Quasi-oracle estimation of heterogeneous treatment effects. Biometrika, 108(2), 299-319.
s_learner(), t_learner(), x_learner(), dr_learner(),
ate_dml(), r_loss()
library(mlr3) data(synth_train) data(synth_test) set.seed(1) m <- r_learner(synth_train, outcome = "y", treatment = "t", outcome_learner = lrn("regr.rpart"), ps_learner = lrn("classif.rpart"), ps_trim = 0.01) tau_hat <- predict(m, synth_test) pehe(synth_test$tau, tau_hat)library(mlr3) data(synth_train) data(synth_test) set.seed(1) m <- r_learner(synth_train, outcome = "y", treatment = "t", outcome_learner = lrn("regr.rpart"), ps_learner = lrn("classif.rpart"), ps_trim = 0.01) tau_hat <- predict(m, synth_test) pehe(synth_test$tau, tau_hat)
Computes the R-Loss (Nie & Wager, 2021), also known as
, of a vector of CATE predictions:
Unlike pehe(), the R-Loss does not require the true treatment effects,
so it can be computed on real observational data. This makes it suitable
for hyperparameter tuning and model selection of causal estimators:
lower values indicate better CATE models.
r_loss(nuisance, tau_pred)r_loss(nuisance, tau_pred)
nuisance |
An |
tau_pred |
Numeric vector of CATE predictions for the same rows that
|
A single non-negative number (lower is better).
Nie, X., & Wager, S. (2021). Quasi-oracle estimation of heterogeneous treatment effects. Biometrika, 108(2), 299-319.
Machlanski, D., Samothrakis, S., & Clarke, P. (2023). Hyperparameter tuning and model evaluation in causal effect estimation. arXiv preprint arXiv:2303.01412.
rloss_nuisance(), pehe(), eps_ate()
library(mlr3) data(synth_train) set.seed(1) nuis <- rloss_nuisance(synth_train, outcome = "y", treatment = "t", outcome_learner = lrn("regr.rpart"), ps_learner = lrn("classif.rpart")) # compare two candidate CATE models by their R-Loss m_s <- s_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart")) m_t <- t_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart")) r_loss(nuis, predict(m_s, synth_train)) r_loss(nuis, predict(m_t, synth_train))library(mlr3) data(synth_train) set.seed(1) nuis <- rloss_nuisance(synth_train, outcome = "y", treatment = "t", outcome_learner = lrn("regr.rpart"), ps_learner = lrn("classif.rpart")) # compare two candidate CATE models by their R-Loss m_s <- s_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart")) m_t <- t_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart")) r_loss(nuis, predict(m_s, synth_train)) r_loss(nuis, predict(m_t, synth_train))
Estimates the two nuisance functions required by the R-Loss (r_loss()):
the conditional outcome mean and the propensity
score , and returns the corresponding
residuals and . By default the
nuisance predictions are cross-fitted (out-of-fold), which avoids
overfitting bias; set folds = 1 for simple in-sample estimates.
rloss_nuisance( data, outcome = "y", treatment = "t", outcome_learner, ps_learner, covariates = NULL, folds = 5, ps_trim = NULL )rloss_nuisance( data, outcome = "y", treatment = "t", outcome_learner, ps_learner, covariates = NULL, folds = 5, ps_trim = NULL )
data |
A |
outcome |
Name of the outcome column. Default |
treatment |
Name of the binary (0/1) treatment column. Default |
outcome_learner |
An mlr3 regression learner for |
ps_learner |
An mlr3 classification learner for |
covariates |
Optional character vector of covariate columns. Defaults to all columns except the outcome and the treatment. |
folds |
Number of cross-fitting folds. Default |
ps_trim |
Optional trimming for the estimated propensity scores;
see |
Fitting the nuisance models once and reusing the resulting object to score many candidate CATE models (e.g. across a hyperparameter grid) is both faster and methodologically cleaner, since all candidates are compared against the same residuals.
An object of class "rloss_nuisance" with elements y_res,
t_res, m_hat and e_hat, to be passed to r_loss().
library(mlr3) data(synth_train) set.seed(1) nuis <- rloss_nuisance(synth_train, outcome = "y", treatment = "t", outcome_learner = lrn("regr.rpart"), ps_learner = lrn("classif.rpart")) m <- s_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart")) r_loss(nuis, predict(m, synth_train))library(mlr3) data(synth_train) set.seed(1) nuis <- rloss_nuisance(synth_train, outcome = "y", treatment = "t", outcome_learner = lrn("regr.rpart"), ps_learner = lrn("classif.rpart")) m <- s_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart")) r_loss(nuis, predict(m, synth_train))
The S-Learner ("single learner") fits one outcome model
that includes the treatment
indicator as a regular feature. CATEs are obtained by contrasting the
predictions with the treatment switched on and off:
s_learner(data, outcome = "y", treatment = "t", learner, covariates = NULL) ## S3 method for class 's_learner' predict(object, newdata, ...)s_learner(data, outcome = "y", treatment = "t", learner, covariates = NULL) ## S3 method for class 's_learner' predict(object, newdata, ...)
data |
A |
outcome |
Name of the outcome column. Default |
treatment |
Name of the binary (0/1) treatment column. Default |
learner |
An mlr3 regression learner for the outcome model, e.g.
|
covariates |
Optional character vector of covariate columns. Defaults to all columns except the outcome and the treatment. |
object |
A fitted |
newdata |
A |
... |
Ignored. |
A fitted CATE model of class c("s_learner", "cate_learner").
Use predict() to obtain CATE estimates for new data and ate() for
their average.
predict(s_learner): Predict CATEs for new data. Returns a numeric
vector with one element per row of newdata
(only the covariate columns are used).
Künzel, S. R., Sekhon, J. S., Bickel, P. J., & Yu, B. (2019). Metalearners for estimating heterogeneous treatment effects using machine learning. Proceedings of the National Academy of Sciences, 116(10), 4156-4165.
t_learner(), x_learner(), pehe(), r_loss()
library(mlr3) data(synth_train) data(synth_test) m <- s_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart")) tau_hat <- predict(m, synth_test) pehe(synth_test$tau, tau_hat)library(mlr3) data(synth_train) data(synth_test) m <- s_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart")) tau_hat <- predict(m, synth_test) pehe(synth_test$tau, tau_hat)
A simplified simulation of the effect of sodium intake on blood pressure, proposed by Luque-Fernandez et al. (2019). Age confounds both the treatment (high sodium intake) and the outcome (blood pressure). The true ATE used in the data generating process is 1.05.
sodiumsodium
A data frame with 10,000 rows and 3 columns:
Age in years (confounder).
Binary treatment: high sodium intake (1) or not (0).
Systolic blood pressure (outcome).
Luque-Fernandez, M. A., Schomaker, M., Redondo-Sanchez, D., Jose Sanchez Perez, M., Vaidya, A., & Schnitzer, M. E. (2019). Educational Note: Paradoxical collider effect in the analysis of non-communicable disease epidemiological data. International Journal of Epidemiology, 48(2), 640-653.
data(sodium) ate_naive(sodium, outcome = "bp", treatment = "sodium")data(sodium) ate_naive(sodium, outcome = "bp", treatment = "sodium")
A synthetic two-dimensional binary classification problem where the two classes form interleaved spirals - a simple example of data that linear models cannot separate. Included as supervised-learning practice data.
spiralsspirals
A data frame with 2,000 rows and 3 columns: coordinates x0,
x1 and the class label y (factor, 0/1).
data(spirals) head(spirals)data(spirals) head(spirals)
Test covariates and ground-truth treatment effects accompanying
synth_train. Contains only the covariates and the true CATE (tau),
mimicking the deployment setting where a fitted CATE model predicts
effects for new individuals.
synth_testsynth_test
A data frame with 250 rows and 6 columns:
Continuous covariates.
True conditional average treatment effect.
Künzel, S. R., Sekhon, J. S., Bickel, P. J., & Yu, B. (2019). Metalearners for estimating heterogeneous treatment effects using machine learning. Proceedings of the National Academy of Sciences, 116(10), 4156-4165.
data(synth_test) head(synth_test)data(synth_test) head(synth_test)
Fully synthetic data generated with the data generating process of Künzel et al. (2019), with five Gaussian covariates, a binary treatment and a continuous outcome. The true CATE is a step function of one of the covariates. See synth_test for the accompanying test set with ground truth effects.
synth_trainsynth_train
A data frame with 1,000 rows and 7 columns:
Continuous covariates.
Binary treatment indicator (0/1).
Continuous outcome.
Künzel, S. R., Sekhon, J. S., Bickel, P. J., & Yu, B. (2019). Metalearners for estimating heterogeneous treatment effects using machine learning. Proceedings of the National Academy of Sciences, 116(10), 4156-4165.
data(synth_train) head(synth_train)data(synth_train) head(synth_train)
The T-Learner ("two learners") fits separate outcome models for the
control and treated groups,
and
, and estimates the CATE as their
difference:
t_learner( data, outcome = "y", treatment = "t", learner, learner1 = NULL, covariates = NULL ) ## S3 method for class 't_learner' predict(object, newdata, ...)t_learner( data, outcome = "y", treatment = "t", learner, learner1 = NULL, covariates = NULL ) ## S3 method for class 't_learner' predict(object, newdata, ...)
data |
A |
outcome |
Name of the outcome column. Default |
treatment |
Name of the binary (0/1) treatment column. Default |
learner |
An mlr3 regression learner used for the control-group
model (and, if |
learner1 |
Optional separate mlr3 regression learner for the treated
group. Defaults to a clone of |
covariates |
Optional character vector of covariate columns. Defaults to all columns except the outcome and the treatment. |
object |
A fitted |
newdata |
A |
... |
Ignored. |
A fitted CATE model of class c("t_learner", "cate_learner");
see s_learner().
predict(t_learner): Predict CATEs for new data.
Künzel, S. R., Sekhon, J. S., Bickel, P. J., & Yu, B. (2019). Metalearners for estimating heterogeneous treatment effects using machine learning. Proceedings of the National Academy of Sciences, 116(10), 4156-4165.
library(mlr3) data(synth_train) data(synth_test) m <- t_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart")) tau_hat <- predict(m, synth_test) pehe(synth_test$tau, tau_hat)library(mlr3) data(synth_train) data(synth_test) m <- t_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart")) tau_hat <- predict(m, synth_test) pehe(synth_test$tau, tau_hat)
The X-Learner (Künzel et al., 2019) proceeds in three stages:
Fit group-specific outcome models and
as in the T-Learner.
Impute individual treatment effects,
for controls and
for the treated, and regress them on
the covariates to obtain and .
Combine the two estimates with propensity score weights:
x_learner( data, outcome = "y", treatment = "t", learner, ps_learner, tau_learner = NULL, covariates = NULL ) ## S3 method for class 'x_learner' predict(object, newdata, ...)x_learner( data, outcome = "y", treatment = "t", learner, ps_learner, tau_learner = NULL, covariates = NULL ) ## S3 method for class 'x_learner' predict(object, newdata, ...)
data |
A |
outcome |
Name of the outcome column. Default |
treatment |
Name of the binary (0/1) treatment column. Default |
learner |
An mlr3 regression learner for the stage-1 outcome models. |
ps_learner |
An mlr3 classification learner for the propensity score
model, e.g. |
tau_learner |
Optional mlr3 regression learner for the stage-2
imputed-effect models. Defaults to a clone of |
covariates |
Optional character vector of covariate columns. Defaults to all columns except the outcome and the treatment. |
object |
A fitted |
newdata |
A |
... |
Ignored. |
The X-Learner is particularly effective when the treated and control groups are of very different sizes.
A fitted CATE model of class c("x_learner", "cate_learner");
see s_learner().
predict(x_learner): Predict CATEs for new data.
Künzel, S. R., Sekhon, J. S., Bickel, P. J., & Yu, B. (2019). Metalearners for estimating heterogeneous treatment effects using machine learning. Proceedings of the National Academy of Sciences, 116(10), 4156-4165.
library(mlr3) data(synth_train) data(synth_test) m <- x_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart"), ps_learner = lrn("classif.rpart")) tau_hat <- predict(m, synth_test) pehe(synth_test$tau, tau_hat)library(mlr3) data(synth_train) data(synth_test) m <- x_learner(synth_train, outcome = "y", treatment = "t", learner = lrn("regr.rpart"), ps_learner = lrn("classif.rpart")) tau_hat <- predict(m, synth_test) pehe(synth_test$tau, tau_hat)