Checks the treatment column, checks that the outcome model doesn't use the treatment as a predictor, and fits the outcome model separately on the treated and the control units.
Arguments
- object
A
t_learnerobject fromt_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/FALSEtreatments, where 1 orTRUEmeans 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 t_learner_fit object, to be passed to
predict().
Details
Before any model is fit, only the workflow's preprocessing (its formula or
recipe) is run, and fit() stops if the treatment would reach the model.
To check, 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 differ, the model would be using the treatment. Leave it out
with a formula such as spend ~ . - emailed or a recipe step such as
recipes::step_rm(emailed).
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())
fitted <- t_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