Skip to contents

Combines the two treatment groups' effect models, weighting the control group's by the propensity. new_data doesn't need the treatment column, but it needs the propensity workflow's predictors if the propensity was estimated.

Usage

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

Arguments

object

A fitted x_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 X-learner's estimate combines two effect models rather than subtracting two predicted 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 formula leaves out the treatment: fit() splits the data by it
wf <- workflows::workflow(spend ~ recency, parsnip::linear_reg())

# The email was a 50/50 coin flip, so the propensity is known
fitted <- x_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.43
#> 3       5.61
#> 4       9.24
#> 5       8.64
#> 6       5.61

# Or estimate the propensity with a classification workflow
propensity_wf <- workflows::workflow(emailed ~ recency, parsnip::logistic_reg())
estimated <- x_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.40
#> 2       7.43
#> 3       5.61
#> 4       9.24
#> 5       8.64
#> 6       5.61