Skip to contents

Checks the treatment column, checks that the outcome model uses the treatment as a predictor, and fits the outcome model on data.

Usage

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

Arguments

object

An s_learner object from s_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 s_learner_fit object, to be passed to predict().

Details

Before the outcome model is fit, only the workflow's preprocessing (its formula or recipe) is run, and fit() stops if the treatment is not among the predictors or if preprocessing removed it. To check the second, the preprocessing is run on up to 1,000 rows twice, once with everyone treated and once with everyone in control: if the processed predictors are the same, the treatment was removed. This way a mistake is caught before a possibly slow model fit.

After fitting, a warning is given if flipping the treatment does not change any prediction on the training data. This means the model is ignoring the treatment (for example, a tree that never splits on it), so every estimated effect would be zero. To keep this cheap on large data, the first 1,000 rows are checked first, and the rest only if none of their predictions change, so every row is still checked before a warning is given.

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