Skip to contents

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 'dr_learner_fit'
predict(object, new_data, type = "cate", ...)

Arguments

object

A fitted dr_learner_fit object 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 DR-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; it is fit
# separately on the emailed and the not-emailed customers
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 <- dr_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.40
#> 2       7.48
#> 3       5.63
#> 4       9.34
#> 5       8.72
#> 6       5.63

# Or estimate the propensity with a classification workflow
propensity_wf <- workflows::workflow(emailed ~ recency, parsnip::logistic_reg())
set.seed(2)
estimated <- dr_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.35
#> 2       7.57
#> 3       5.64
#> 4       9.50
#> 5       8.85
#> 6       5.64