Skip to contents

The R-learner (Nie and Wager, 2021) estimates the conditional average treatment effect (CATE) from residuals, in three steps:

Usage

r_learner(outcome_model, effect_model = NULL, propensity)

Arguments

outcome_model

An unfitted workflows::workflow() that predicts the outcome from the features, without the treatment column. Its model can be a regression (numeric outcome) or a classification with two classes (binary outcome).

effect_model

A parsnip model specification for the effect model, such as parsnip::linear_reg(), or NULL (default) to reuse outcome_model's model, with a warning. The effect model uses outcome_model's formula or recipe, so give only the model, not a workflow; the predictors are encoded the way the effect model expects (see fit.r_learner()). Effects are numbers, so this must be a regression model; a specification with no mode set is set to regression, with a warning. Required when the outcome is binary. Its engine must accept case weights unless propensity is 0.5.

propensity

Each unit's chance of being treated: a single number strictly between 0 and 1 when it is known (e.g., 0.5 in a 50/50 randomized experiment), or an unfitted classification workflows::workflow() that predicts the treatment column. There is no default.

Value

An unfitted r_learner object, to be passed to fit().

Details

  1. Cross-fit two models: the outcome model, which predicts the outcome from the features alone (without the treatment), and the propensity, each unit's chance of being treated. Each unit's predictions come from models fit without it.

  2. Compute each unit's residuals: its outcome minus the predicted outcome, and its treatment (1 or 0) minus its propensity. Their ratio, the pseudo-outcome, is the unit's effect plus noise.

  3. Fit an effect model to the pseudo-outcomes, weighting each unit by its squared treatment residual. This minimizes the R-loss, the squared error of predicting the outcome residual from the effect times the treatment residual.

Subtracting the predicted outcome removes the part of the outcome that doesn't depend on the treatment, which makes the pseudo-outcomes much less noisy, and working with residuals makes the estimated effects less sensitive to errors in the outcome and propensity models. The pseudo-outcomes are still noisy, so a simpler effect model than the outcome model often works better.

References

Nie, X., & Wager, S. (2021). Quasi-oracle estimation of heterogeneous treatment effects. Biometrika, 108(2), 299-319. doi:10.1093/biomet/asaa076

Examples

# A small randomized email campaign: the email raises spending more for
# customers who bought recently
set.seed(1)
n <- 200
recency <- sample(1:12, n, replace = TRUE)
emailed <- rbinom(n, size = 1, prob = 0.5)
spend <- 20 - recency + emailed * (10 - 0.75 * recency) + rnorm(n, sd = 5)
emails <- data.frame(recency, emailed, spend)

# The outcome model predicts spending without the treatment
wf <- workflows::workflow(spend ~ recency, parsnip::linear_reg())

# The email was a 50/50 coin flip, so the propensity is known. fit() makes
# random cross-fitting folds, so set the seed for reproducible results.
set.seed(2)
fitted <- r_learner(wf, effect_model = parsnip::linear_reg(), propensity = 0.5) |>
  fit(data = emails, treatment = emailed)
predict(fitted, new_data = head(emails))
#> # A tibble: 6 × 1
#>   .pred_cate
#>        <dbl>
#> 1       4.24
#> 2       7.46
#> 3       5.53
#> 4       9.40
#> 5       8.75
#> 6       5.53

# Or estimate the propensity with a classification workflow
propensity_wf <- workflows::workflow(emailed ~ recency, parsnip::logistic_reg())
set.seed(2)
estimated <- r_learner(
  wf,
  effect_model = parsnip::linear_reg(),
  propensity = propensity_wf
) |>
  fit(data = emails, treatment = emailed)
predict(estimated, new_data = head(emails))
#> # A tibble: 6 × 1
#>   .pred_cate
#>        <dbl>
#> 1       4.37
#> 2       7.58
#> 3       5.65
#> 4       9.51
#> 5       8.87
#> 6       5.65