Skip to contents

tidycate builds causal meta-learners from tidymodels workflows. Instead of predicting an outcome, a meta-learner estimates how the effect of a binary treatment varies across individuals: the conditional average treatment effect (CATE). If you can fit a model with a workflow, such as a boosted tree or a random forest, you can use it as a base learner.

The package website has an introduction to the methods, with worked examples, and a reference page for every function.

Status: tidycate is in early development. The S-, T-, X-, R-, and DR-learners and uplift metrics are available, and the interface may still change.

Why meta-learners?

For each individual, we only ever observe one outcome: the one under treatment or the one under control, never both. That means the treatment effect can’t be used directly as a prediction target. Meta-learners get around this by breaking CATE estimation into ordinary supervised learning problems, such as one outcome model per treatment group, and then combining those models’ predictions into an effect estimate.

Building a meta-learner by hand in tidymodels means splitting the data by treatment group, fitting several workflows, and combining their predictions, and some learners also need cross-fitting to avoid overfitting. tidycate aims to handle those steps for you.

As with any causal method, the estimates rely on the study design: they assume there are no unmeasured confounders and that every kind of individual could have received either treatment. A randomized experiment guarantees both.

“Meta-learner” here is meant in the causal-inference sense. It is unrelated to the meta-learner that blends models in stacks.

Features

Available now:

Planned:

  • Plots, such as the Qini curve
  • Diagnostics for propensity score overlap
  • Standard errors for the uplift metrics

Installation

You can install tidycate from CRAN with:

install.packages("tidycate")

And the development version from GitHub with:

# install.packages("pak")
pak::pak("jonahrosenberg/tidycate")

Example

A randomized email campaign, simulated so that the email raises spending more for customers who bought recently:

library(tidycate)
library(tidymodels)

set.seed(1)
n <- 10000
emails <- tibble(
  recency = sample(1:12, n, replace = TRUE), # months since last purchase
  history = runif(n, 0, 500),                # past-year spending
  emailed = rbinom(n, 1, 0.5),               # the treatment: a coin flip
  spend   = 20 + 0.1 * history - recency +
    emailed * (10 - 0.75 * recency) + rnorm(n, sd = 5)
)

set.seed(2)
split <- initial_split(emails, strata = emailed)
train <- training(split)
test  <- testing(split)

Define the base models first. The outcome model can be any workflow, here a random forest, and it leaves out the treatment, since the X-learner fits it separately for the emailed and not-emailed customers. The effect model is a model specification that reuses the outcome model’s formula.

outcome_model <- workflow(spend ~ recency + history, rand_forest(mode = "regression"))
effect_model  <- linear_reg()

Then build the X-learner and fit it, naming the treatment column. The email was a 50/50 coin flip, so the propensity (each customer’s chance of being emailed) is known:

set.seed(3)
x_fit <- x_learner(outcome_model, effect_model = effect_model, propensity = 0.5) |>
  fit(data = train, treatment = emailed)
x_fit
#> ══ X-learner [fitted] ══════════════════════════════════════════════════════════
#> Outcome:   spend (numeric)
#> Treatment: emailed (treated = 1, control = 0)
#> Training:  7,499 rows (3,727 treated, 3,772 control)
#> 
#> ── Models ──────────────────────────────────────────────────────────────────────
#> Outcome model: rand_forest (ranger), one per group
#>                spend ~ recency + history
#> Effect model:  linear_reg (lm), one per group
#>                same formula as the outcome model
#> Propensity:    0.5 (known)

predict() returns one estimated effect per customer:

predict(x_fit, new_data = test)
#> # A tibble: 2,501 × 1
#>    .pred_cate
#>         <dbl>
#>  1       8.20
#>  2       2.10
#>  3       7.60
#>  4       4.87
#>  5       5.47
#>  6       4.87
#>  7       3.33
#>  8       5.42
#>  9       8.32
#> 10       5.57
#> # ℹ 2,491 more rows

The uplift metrics check how well the estimates rank held-out customers, using only their observed spending. The Qini coefficient is the area between the Qini curve and the line a random ranking would give:

augment(x_fit, new_data = test) |>
  qini_coef(truth = spend, estimate = .pred_cate, treatment = emailed)
#> # A tibble: 1 × 3
#>   .metric   .estimator .estimate
#>   <chr>     <chr>          <dbl>
#> 1 qini_coef standard       0.588

The Qini coefficient is noisy on small test sets, so use as much held-out data as you can.

  • grf estimates heterogeneous treatment effects with causal forests.
  • tidyhte implements the doubly robust learner, with base learners from SuperLearner.