Skip to contents

Predict effects from a fitted S-learner

Usage

# S3 method for class 's_learner_fit'
predict(object, new_data, type = c("cate", "potential_outcomes"), ...)

Arguments

object

A fitted s_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

"cate" (default) for the estimated effect, or "potential_outcomes" for the predicted outcome under control and under treatment.

...

Not currently used.

Value

A tibble with one row per row of new_data: a .pred_cate column for type = "cate", or .pred_control and .pred_treated columns for type = "potential_outcomes". For binary outcomes these are probabilities of the event level, so .pred_cate is a difference in probabilities.

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 interaction lets the estimated effect vary with recency
wf <- workflows::workflow(spend ~ recency * emailed, parsnip::linear_reg())

fitted <- s_learner(wf) |>
  fit(data = emails, treatment = emailed)

predict(fitted, new_data = head(emails))
#> # A tibble: 6 × 1
#>   .pred_cate
#>        <dbl>
#> 1       4.40
#> 2       7.43
#> 3       5.61
#> 4       9.24
#> 5       8.64
#> 6       5.61
predict(fitted, new_data = head(emails), type = "potential_outcomes")
#> # A tibble: 6 × 2
#>   .pred_control .pred_treated
#>           <dbl>         <dbl>
#> 1          11.0          15.4
#> 2          15.8          23.3
#> 3          12.9          18.5
#> 4          18.8          28.0
#> 5          17.8          26.4
#> 6          12.9          18.5