Predicts each unit's effect from the effect model. new_data needs only
the outcome model's predictors: not the treatment column, and not the
propensity workflow's predictors.
Usage
# S3 method for class 'r_learner_fit'
predict(object, new_data, type = "cate", ...)Arguments
- object
A fitted
r_learner_fitobject from fit().- new_data
A data frame of units to predict for. It does not need to contain the treatment column.
- type
Only
"cate"(default), the estimated effect. The R-learner's effect model is fit directly to pseudo-outcomes, so it has no potential outcomes to return.- ...
Not currently used.
Value
A tibble with one row per row of new_data and one column,
.pred_cate. For a binary outcome, it is a difference in probabilities of
the event level.
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