Checks the treatment column and the workflows, cross-fits the outcome model and (if it is a workflow) the propensity, computes each unit's pseudo-outcome and weight, and fits the effect model.
Arguments
- object
An
r_learnerobject fromr_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.- folds
The cross-fitting folds: a whole number of folds (default 5), made with
rsample::vfold_cv()so that each fold has a similar share of treated units, or an rsample object built fromdatathat holds out every row exactly once, such as one fromrsample::vfold_cv()orrsample::group_vfold_cv(). Making folds uses the random number generator, so callset.seed()first for reproducible results.- ...
Not currently used.
Value
A fitted r_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(): the outcome model predicts the
outcome from the features alone, so that the treatment's effect is left in
the residuals. It also checks that a propensity workflow's outcome is the
treatment column, that the effect model's engine accepts case weights when
they are needed, and that folds is valid.
Cross-fitting: for each fold, the outcome model (and a propensity workflow) is fit on the other folds and predicts the units in that fold. A known propensity is used as is.
Each unit's pseudo-outcome is its outcome residual divided by its treatment
residual, and its weight is the squared treatment residual, rescaled so the
weights average 1. With a propensity of 0.5 every weight is equal, so the
effect model is fit without weights and any engine can be used; otherwise
its engine must accept case weights. fit() warns when any propensity is
below 0.01 or above 0.99, since those units' pseudo-outcomes can be
extreme, and stops if any is exactly 0 or 1. Propensities are never clipped.
The effect model uses the outcome model's formula or recipe, estimated on
all of 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. New data is run through the same fitted preprocessing
before the effect model predicts.
Each unit's residual is matched to its own row, so preprocessing that
removes rows (such as recipes::step_naomit()) isn't supported: remove
those rows from data first. Recipe steps on the outcome should use
skip = TRUE, as recipes recommends. Avoid outcome steps that learn from
the data, such as normalizing the outcome: they would be estimated within
each fold for the predicted outcomes but on all of data for the observed
outcomes.
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 outcome model predicts spending without the treatment
wf <- workflows::workflow(spend ~ recency, parsnip::linear_reg())
# The email was a 50/50 coin flip, so the propensity is known. fit() makes
# random cross-fitting folds, so set the seed for reproducible results.
set.seed(2)
fitted <- r_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.24
#> 2 7.46
#> 3 5.53
#> 4 9.40
#> 5 8.75
#> 6 5.53
# Or estimate the propensity with a classification workflow
propensity_wf <- workflows::workflow(emailed ~ recency, parsnip::logistic_reg())
set.seed(2)
estimated <- r_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.37
#> 2 7.58
#> 3 5.65
#> 4 9.51
#> 5 8.87
#> 6 5.65