Checks the treatment column and the workflows, cross-fits the outcome model in each treatment group and (if it is a workflow) the propensity, computes each unit's pseudo-outcome, and fits the effect model.
Arguments
- object
A
dr_learnerobject fromdr_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 dr_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 is fit separately
in each treatment group, where the treatment is the same for every unit. It
also checks that a propensity workflow's outcome is the treatment column,
that folds is valid, and that every fold's training rows (the other folds)
include both treated and control units.
Cross-fitting: for each fold, the outcome model is fit on the other folds twice, once on their treated units and once on their control units, and both fits predict every unit in the fold. A propensity workflow is fit on the other folds too. A known propensity is used as is.
Each unit's pseudo-outcome is its predicted outcome if treated minus its
predicted outcome if control, plus its residual from its own group's
outcome model, divided by its propensity (treated units) or by 1 minus its
propensity (control units). The effect model is fit to all units'
pseudo-outcomes at once, without weights, so any engine can be used.
(Kennedy, 2023, instead fits the effect model on a separate sample from the
outcome and propensity models, optionally swapping the samples and averaging
the two estimates.) 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 pseudo-outcome 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; it is fit
# separately on the emailed and the not-emailed customers
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 <- dr_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.48
#> 3 5.63
#> 4 9.34
#> 5 8.72
#> 6 5.63
# Or estimate the propensity with a classification workflow
propensity_wf <- workflows::workflow(emailed ~ recency, parsnip::logistic_reg())
set.seed(2)
estimated <- dr_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.35
#> 2 7.57
#> 3 5.64
#> 4 9.50
#> 5 8.85
#> 6 5.64