Estimates the two nuisance functions required by the R-Loss (r_loss()):
the conditional outcome mean \(m(x) = E[Y | X = x]\) and the propensity
score \(e(x) = P(T = 1 | X = x)\), and returns the corresponding
residuals \(Y - \hat m(X)\) and \(T - \hat e(X)\). By default the
nuisance predictions are cross-fitted (out-of-fold), which avoids
overfitting bias; set folds = 1 for simple in-sample estimates.
rloss_nuisance(
data,
outcome = "y",
treatment = "t",
outcome_learner,
ps_learner,
covariates = NULL,
folds = 5,
ps_trim = NULL
)A data.frame with the outcome, treatment and covariates.
Typically a held-out validation set that was not used to train the
CATE models being evaluated.
Name of the outcome column. Default "y".
Name of the binary (0/1) treatment column. Default "t".
An mlr3 regression learner for m(x) (the
treatment is excluded from the features).
An mlr3 classification learner for e(x).
Optional character vector of covariate columns. Defaults to all columns except the outcome and the treatment.
Number of cross-fitting folds. Default 5; 1 disables
cross-fitting.
Optional trimming for the estimated propensity scores;
see ate_ipw().
An object of class "rloss_nuisance" with elements y_res,
t_res, m_hat and e_hat, to be passed to r_loss().
Fitting the nuisance models once and reusing the resulting object to score many candidate CATE models (e.g. across a hyperparameter grid) is both faster and methodologically cleaner, since all candidates are compared against the same residuals.
# \donttest{
library(mlr3)
data(synth_train)
set.seed(1)
nuis <- rloss_nuisance(synth_train, outcome = "y", treatment = "t",
outcome_learner = lrn("regr.rpart"),
ps_learner = lrn("classif.rpart"))
m <- s_learner(synth_train, outcome = "y", treatment = "t",
learner = lrn("regr.rpart"))
r_loss(nuis, predict(m, synth_train))
#> [1] 4.787972
# }