Skip to contents

Bayesian causal forest (BCF) sampled with MCMC, optionally warm-started with the grow-from-root algorithm of accelerated BCF (XBCF). Calls stochtree::bcf() from stochtree.

The model decomposes the outcome into a prognostic forest and a treatment effect forest, i.e., y = mu(x) + tau(x) * z, where z is the treatment. As mlr3 has no task type for causal inference, the treatment is a feature of the regression task that is selected with the treatment parameter. The learner predicts the outcome given the covariates and the observed treatment, so resampling and tuning evaluate the outcome prediction and not the treatment effect. The estimated conditional average treatment effect is returned by the $treatment_effect() method.

The "se" predict type returns the posterior standard deviation of the conditional mean. Propensity scores are estimated internally by stochtree::bcf(). Random effects are not supported.

Custom mlr3 parameters

  • treatment: Name of the feature that contains the treatment. The feature must be logical, integer, or numeric, and is passed as Z_train to stochtree::bcf(). All other features are used as covariates. This parameter is required.

  • stochtree::bcf() takes most of its parameters in the lists general_params, prognostic_forest_params, treatment_effect_forest_params, and variance_forest_params. The entries of these lists are exposed as individual hyperparameters. The entries of general_params keep their names. The entries of the forest lists that occur in more than one list are prefixed with prognostic_, treatment_, and variance_, e.g., prognostic_num_trees, treatment_num_trees, and variance_num_trees. All other entries of the forest lists keep their names.

  • random_seed: If not set, the seed of the C++ random number generator is drawn from R's random number generator. Upstream seeds from std::random_device in this case.

Reproducibility

stochtree::bcf() estimates the propensity scores with an internal BART model that is always seeded from std::random_device. Results are therefore only reproducible with set.seed() or random_seed when propensity_covariate is "none", in which case no propensity model is fitted.

  • observation_weights is not exposed as a hyperparameter. Observation weights are taken from the task instead, see the weights property.

Dictionary

This Learner can be instantiated via lrn():

lrn("regr.stochtree_bcf")

Meta Information

  • Task type: “regr”

  • Predict Types: “response”, “se”

  • Feature Types: “logical”, “integer”, “numeric”, “factor”, “ordered”

  • Required Packages: mlr3, mlr3extralearners, stochtree

Parameters

IdTypeDefaultLevelsRange
treatmentuntyped--
num_gfrinteger5\([0, \infty)\)
num_burnininteger0\([0, \infty)\)
num_mcmcinteger100\([0, \infty)\)
cutpoint_grid_sizeinteger100\([1, \infty)\)
standardizelogicalTRUETRUE, FALSE-
sample_sigma2_globallogicalTRUETRUE, FALSE-
sigma2_global_initnumeric-\([0, \infty)\)
sigma2_global_shapenumeric0\([0, \infty)\)
sigma2_global_scalenumeric0\([0, \infty)\)
variable_weightsuntypedNULL-
propensity_covariatecharacterprognosticnone, prognostic, treatment_effect, both-
adaptive_codinglogicalFALSETRUE, FALSE-
control_coding_initnumeric-0.5\((-\infty, \infty)\)
treated_coding_initnumeric0.5\((-\infty, \infty)\)
random_seedinteger-\([0, \infty)\)
keep_burninlogicalFALSETRUE, FALSE-
keep_gfrlogicalFALSETRUE, FALSE-
keep_everyinteger1\([1, \infty)\)
num_chainsinteger1\([1, \infty)\)
verboselogicalFALSETRUE, FALSE-
num_threadsinteger1\([1, \infty)\)
prognostic_num_treesinteger250\([0, \infty)\)
prognostic_alphanumeric0.95\([0, 1]\)
prognostic_betanumeric2\([0, \infty)\)
prognostic_min_samples_leafinteger5\([1, \infty)\)
prognostic_max_depthinteger10\([-1, \infty)\)
prognostic_sample_sigma2_leaflogicalTRUETRUE, FALSE-
prognostic_sigma2_leaf_initnumeric-\([0, \infty)\)
prognostic_sigma2_leaf_shapenumeric3\([0, \infty)\)
prognostic_sigma2_leaf_scalenumeric-\([0, \infty)\)
prognostic_keep_varsuntypedNULL-
prognostic_drop_varsuntypedNULL-
prognostic_num_features_subsampleinteger-\([1, \infty)\)
treatment_num_treesinteger100\([0, \infty)\)
treatment_alphanumeric0.25\([0, 1]\)
treatment_betanumeric3\([0, \infty)\)
treatment_min_samples_leafinteger5\([1, \infty)\)
treatment_max_depthinteger5\([-1, \infty)\)
treatment_sample_sigma2_leaflogicalFALSETRUE, FALSE-
treatment_sigma2_leaf_initnumeric-\([0, \infty)\)
treatment_sigma2_leaf_shapenumeric3\([0, \infty)\)
treatment_sigma2_leaf_scalenumeric-\([0, \infty)\)
treatment_keep_varsuntypedNULL-
treatment_drop_varsuntypedNULL-
treatment_num_features_subsampleinteger-\([1, \infty)\)
sample_interceptlogicalTRUETRUE, FALSE-
tau_0_prior_varnumeric-\([0, \infty)\)
variance_num_treesinteger0\([0, \infty)\)
variance_alphanumeric0.95\([0, 1]\)
variance_betanumeric2\([0, \infty)\)
variance_min_samples_leafinteger5\([1, \infty)\)
variance_max_depthinteger10\([-1, \infty)\)
variance_keep_varsuntypedNULL-
variance_drop_varsuntypedNULL-
variance_num_features_subsampleinteger-\([1, \infty)\)
leaf_prior_calibration_paramnumeric1.5\([0, \infty)\)
variance_forest_initnumeric-\((-\infty, \infty)\)
var_forest_prior_shapenumeric-\([0, \infty)\)
var_forest_prior_scalenumeric-\([0, \infty)\)

Saving a Learner

The fitted model holds pointers to C++ objects. In order to save a trained learner, or to use it with encapsulation or parallelization, the model is marshaled to a JSON string with stochtree::saveBARTModelToJsonString(). Call $marshal() before saving the learner with saveRDS() and $unmarshal() after reading it.

References

Hahn, Richard P, Murray, S. J, Carvalho, M. C (2020). “Bayesian Regression Tree Models for Causal Inference: Regularization, Confounding, and Heterogeneous Effects (with Discussion).” Bayesian Analysis, 15(3), 965–1056. doi:10.1214/19-BA1195 .

Krantsevich, Nikolay, He, Jingyu, Hahn, Richard P (2023). “Stochastic Tree Ensembles for Estimating Heterogeneous Effects.” In Proceedings of The 26th International Conference on Artificial Intelligence and Statistics, volume 206 series Proceedings of Machine Learning Research, 6120–6131.

Herren, Drew, Hahn, Richard, Murray, Jared, Carvalho, Carlos, He, Jingyu (2026). stochtree: Stochastic Tree Ensembles (XBART and BART) for Supervised Learning and Causal Inference. doi:10.32614/CRAN.package.stochtree . R package version 0.4.5.

See also

Author

be-marc

Super classes

mlr3::Learner -> mlr3::LearnerRegr -> LearnerRegrStochtreeBCF

Active bindings

marshaled

(logical(1))
Whether the learner has been marshaled.

Methods

Inherited methods


LearnerRegrStochtreeBCF$new()

Creates a new instance of this R6 class.


LearnerRegrStochtreeBCF$treatment_effect()

Estimates the conditional average treatment effect tau(x).

Usage

LearnerRegrStochtreeBCF$treatment_effect(task, row_ids = NULL)

Arguments

task

(mlr3::TaskRegr)
Task with the same features as the training task.

row_ids

(integer())
Rows of the task to estimate the treatment effect for. If NULL (default), the rows with role "use" are used.

Returns

numeric() with the posterior mean of the treatment effect of each row.


LearnerRegrStochtreeBCF$marshal()

Marshal the learner's model.

Usage

LearnerRegrStochtreeBCF$marshal(...)

Arguments

...

(any)
Additional arguments passed to mlr3::marshal_model().


LearnerRegrStochtreeBCF$unmarshal()

Unmarshal the learner's model.

Usage

LearnerRegrStochtreeBCF$unmarshal(...)

Arguments

...

(any)
Additional arguments passed to mlr3::unmarshal_model().


LearnerRegrStochtreeBCF$clone()

The objects of this class are cloneable with this method.

Usage

LearnerRegrStochtreeBCF$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

# Define the Learner, the treatment is a feature of the task
learner = lrn("regr.stochtree_bcf", treatment = "am")
print(learner)
#> 
#> ── <LearnerRegrStochtreeBCF> (regr.stochtree_bcf): Stochastic Tree Ensemble Baye
#> • Model: -
#> • Parameters: treatment=am
#> • Packages: mlr3, mlr3extralearners, and stochtree
#> • Predict Types: [response] and se
#> • Feature Types: logical, integer, numeric, factor, and ordered
#> • Encapsulation: none (fallback: -)
#> • Properties: marshal and weights
#> • Other settings: use_weights = 'use', predict_raw = 'FALSE'

# Define a Task
task = tsk("mtcars")

# Create train and test set
ids = partition(task)

# Train the learner on the training ids
learner$train(task, row_ids = ids$train)

# Make predictions of the outcome for the test rows
predictions = learner$predict(task, row_ids = ids$test)

# Score the predictions
predictions$score()
#> regr.mse 
#> 6.524103 

# Estimate the conditional average treatment effect for the test rows
learner$treatment_effect(task, row_ids = ids$test)
#>  [1] 2.967242 2.293486 3.611397 3.287147 2.477382 2.458633 2.344248 2.399104
#>  [9] 3.805233 2.456142 3.723515