Skip to contents

Checks the treatment column and the workflows, fits the outcome model in each treatment group, estimates each unit's effect, fits the effect model in each group, and fits the propensity workflow if one was given.

Usage

# S3 method for class 'x_learner'
fit(
  object,
  data,
  treatment,
  treated_level = NULL,
  event_level = c("first", "second"),
  ...
)

Arguments

object

An x_learner object from x_learner().

data

A data frame with the outcome, the predictors, and the treatment.

treatment

The treatment column, unquoted (e.g., treatment = emailed). It can be numeric 0/1, logical, or a factor or character column with exactly two values.

treated_level

For a factor or character treatment, the value that means treated. Required for those treatments and not allowed for 0/1 or TRUE/FALSE treatments, where 1 or TRUE means treated.

event_level

For a binary outcome, whether the "first" (default) or "second" level of the outcome is the event whose probability the effect is about, following the yardstick convention. Ignored for numeric outcomes.

...

Not currently used.

Value

A fitted x_learner_fit object, to be passed to predict().

Details

Before any model is fit, fit() checks that the treatment doesn't reach the outcome model, as for fit.t_learner(), and that a propensity workflow's outcome is the treatment column.

The effect models use the outcome model's formula or recipe, estimated on each group's data with the effect model in place of the outcome model. A formula therefore encodes the predictors the way the effect model expects: dummy variables for a linear model or xgboost, factors left as they are for a random forest. So, for example, a random forest outcome model can be paired with an xgboost effect model. New data is run through the same fitted preprocessing before the effect models predict. The outcome column is never changed, so recipe steps on the outcome are safe. Because each unit's estimated effect is matched to its own row, preprocessing that removes rows (such as recipes::step_naomit()) isn't supported: remove those rows from data first.

A propensity workflow is a classification model, so it is fit with the treatment converted to a factor (as factor() would convert it). If it uses a recipe, create the recipe from data where the treatment is already a factor.

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