Type: | Package |
Title: | Interpretable Discovery and Inference of Heterogeneous Treatment Effects |
Version: | 0.2.7 |
Maintainer: | Falco Joannes Bargagli Stoffi <fbargaglistoffi@hsph.harvard.edu> |
Description: | Provides a new method for interpretable heterogeneous treatment effects characterization in terms of decision rules via an extensive exploration of heterogeneity patterns by an ensemble-of-trees approach, enforcing high stability in the discovery. It relies on a two-stage pseudo-outcome regression, and it is supported by theoretical convergence guarantees. Bargagli-Stoffi, F. J., Cadei, R., Lee, K., & Dominici, F. (2023) Causal rule ensemble: Interpretable Discovery and Inference of Heterogeneous Treatment Effects. arXiv preprint <doi:10.48550/arXiv.2009.09036>. |
License: | GPL-3 |
URL: | https://github.com/NSAPH-Software/CRE |
BugReports: | https://github.com/NSAPH-Software/CRE/issues |
Depends: | R (≥ 3.5.0) |
Imports: | MASS, stats, logger, gbm, randomForest, methods, xgboost, RRF, data.table, xtable, glmnet, bartCause, stabs, stringr, SuperLearner, magrittr, ggplot2, arules |
Suggests: | grf, BART, gnm, covr, knitr, rmarkdown, testthat (≥ 3.0.0) |
VignetteBuilder: | knitr |
Copyright: | Harvard University |
Encoding: | UTF-8 |
Language: | en-US |
RoxygenNote: | 7.3.2 |
NeedsCompilation: | no |
Packaged: | 2024-10-18 18:23:19 UTC; falco |
Author: | Naeem Khoshnevis |
Repository: | CRAN |
Date/Publication: | 2024-10-19 15:00:12 UTC |
The CRE package
Description
In health and social sciences, it is critically important to identify subgroups of the study population where a treatment has notable heterogeneity in the causal effects with respect to the average treatment effect. Data-driven discovery of heterogeneous treatment effects (HTE) via decision tree methods has been proposed for this task. Despite its high interpretability, the single-tree discovery of HTE tends to be highly unstable and to find an oversimplified representation of treatment heterogeneity. To accommodate these shortcomings, we propose Causal Rule Ensemble (CRE), a new method to discover heterogeneous subgroups through an ensemble-of-trees approach. CRE has the following features:
provides an interpretable representation of the HTE; 2) allows extensive exploration of complex heterogeneity patterns; and 3) guarantees high stability in the discovery. The discovered subgroups are defined in terms of interpretable decision rules, and we develop a general two-stage approach for subgroup-specific conditional causal effects estimation, providing theoretical guarantees.
Author(s)
Naeem Khoshnevis
Daniela Maria Garcia
Riccardo Cadei
Kwonsang Lee
Falco Joannes Bargagli Stoffi
References
Bargagli-Stoffi, F. J., Cadei, R., Lee, K. and Dominici, F. (2023). Causal rule ensemble: Interpretable Discovery and Inference of Heterogeneous Treatment Effects,arXiv preprint arXiv:2009.09036
See Also
Useful links:
A helper function for cre object
Description
A helper function to plot cre object using ggplot2 package.
Usage
## S3 method for class 'cre'
autoplot(object, ...)
Arguments
object |
A |
... |
Additional arguments passed to customize the plot. |
Value
Returns a ggplot object.
Check input parameters
Description
Checks consistency in input (hyper) parameters for the cre
function.
Usage
check_hyper_params(X_names, params)
Arguments
X_names |
The observed covariates names. |
params |
The list of parameters required to run the function. |
Value
A modified input params
. A list of parameters that might be changed during
the checks.
Check input data
Description
Conducts sanity checks for the input data.
Usage
check_input_data(y, z, X, ite = NULL)
Arguments
y |
The observed response vector. |
z |
The treatment vector. |
X |
The features matrix. |
ite |
The estimated ITE vector. |
Value
The number of data samples.
Check method-related parameters
Description
Checks method-related parameters.
Usage
check_method_params(y, ite, params)
Arguments
y |
The observed response vector. |
ite |
The estimated ITE vector. |
params |
The list of parameters required to run the method functions. |
Value
A modified input params
. A list of parameters that might be changed during
the checks.
Causal rule ensemble
Description
Performs the Causal Rule Ensemble on a data set with a response variable, a treatment variable, and various features.
Usage
cre(y, z, X, method_params = NULL, hyper_params = NULL, ite = NULL)
Arguments
y |
An observed response vector. |
z |
A treatment vector. |
X |
A covariate matrix (or a data frame). Should be provided as numerical values. |
method_params |
The list of parameters to define the models used, including:
|
hyper_params |
The list of hyper parameters to fine-tune the method, including:
|
ite |
The estimated ITE vector. If given both the ITE estimation steps
in Discovery and Inference are skipped (default: |
Value
An S3 object composed by:
M |
the number of Decision Rules extracted at each step, |
CATE |
the data.frame of Conditional Average Treatment Effect decomposition estimates with corresponding uncertainty quantification, |
method_params |
the list of method parameters, |
hyper_params |
the list of hyper parameters, |
rules |
the list of rules (implicit form) decomposing the CATE. |
Note
If
intervention_vars
are provided, it is important to note that the individual treatment effect will still be computed using all covariates.
Examples
set.seed(123)
dataset <- generate_cre_dataset(n = 400,
rho = 0,
n_rules = 2,
p = 10,
effect_size = 2,
binary_covariates = TRUE,
binary_outcome = FALSE,
confounding = "no")
y <- dataset[["y"]]
z <- dataset[["z"]]
X <- dataset[["X"]]
method_params <- list(ratio_dis = 0.5,
ite_method ="aipw",
learner_ps = "SL.xgboost",
learner_y = "SL.xgboost")
hyper_params <- list(intervention_vars = NULL,
offset = NULL,
ntrees = 20,
node_size = 20,
max_rules = 50,
max_depth = 3,
t_decay = 0.025,
t_ext = 0.025,
t_corr = 1,
stability_selection = "vanilla",
cutoff = 0.6,
pfer = 1,
B = 20,
subsample = 0.5)
cre_results <- cre(y, z, X, method_params, hyper_params)
Discover rules
Description
Discover the minimal set of rules linearly decomposing the Conditional Average Treatment Effect (CATE).
Usage
discover_rules(X, ite, method_params, hyper_params)
Arguments
X |
A covariate matrix. |
ite |
An estimated ITE. |
method_params |
A vector of method parameters. |
hyper_params |
A vector of hyper parameters. |
Value
A minimal set of rules linearly decomposing the Conditional Average Treatment Effect (CATE).
Estimate the Conditional Average Treatment Effect
Description
Estimates the Conditional Average Treatment Effect (CATE) by linearly modeling the Individual Treatment Effect by a set of rules.
Usage
estimate_cate(rules_matrix, rules_explicit, ite, B = 1, subsample = 1)
Arguments
rules_matrix |
A rules matrix. |
rules_explicit |
A list of select rules in terms of covariate names. |
ite |
The estimated ITEs. |
B |
The number of bootstrap samples for uncertainty quantification in estimation. |
subsample |
The bootstrap ratio subsample for uncertainty quantification in estimation. |
Value
A list with 2 elements:
summary
: A data frame summarizing the CATE linear decomposition:
-
Rule
: rule name, -
Estimate
: linear contribution to CATE, -
CI_lower
: lower bound 95% confidence interval on the estimate, -
CI_upper
: upper bound 95% confidence interval on the estimate, -
P_Value
: p-value (from Z-test).model
: A linear model for CATE-ATE estimation.
Estimate the Individual Treatment Effect (ITE)
Description
Estimates the Individual Treatment Effect given a response vector, a treatment vector, a covariate matrix, and a desired algorithm.
Usage
estimate_ite(y, z, X, ite_method, ...)
Arguments
y |
An observed response vector. |
z |
An observed treatment vector. |
X |
A covariate matrix. |
ite_method |
A method for estimating the Individual Treatment Effect. Some methods requires additional parameters. These parameters are mentioned in the indented blocks for each method and their definitions are provided at the end of this parameters list.
|
... |
Additional parameters passed to different models. |
Details
Additional parameters
-
learner_ps: An estimation method for the propensity score. This includes libraries for the SuperLearner package.
-
learner_y: An estimation model for the outcome. This includes libraries for the SuperLearner package.
-
offset: Name of the covariate to use as offset (i.e.
"x1"
) for Poisson ITE Estimation.NULL
if offset is not used.
Value
A list of ITE estimates.
Estimate the Individual Treatment Effect (ITE) using Augmented Inverse Probability Weighting (AIPW)
Description
Estimates the Individual Treatment Effect using Augmented Inverse Probability Weighting given a response vector, a treatment vector, a features matrix, an estimation model for the propensity score and estimation model for the outcome.
Usage
estimate_ite_aipw(y, z, X, learner_ps = "SL.xgboost", learner_y = "SL.xgboost")
Arguments
y |
An observed response vector. |
z |
A treatment vector. |
X |
A features matrix. |
learner_ps |
A estimation model for the propensity score. |
learner_y |
A estimation model for the outcome. |
Value
A list of ITE estimates.
Estimate the Individual Treatment Effect (ITE) using Bayesian Additive Regression Trees (BART)
Description
Estimates the Individual Treatment Effect using Bayesian Additive Regression Trees given a response vector, a treatment vector, and a features matrix.
Usage
estimate_ite_bart(y, z, X, learner_ps)
Arguments
y |
An observed response vector. |
z |
A treatment vector. |
X |
A features matrix. |
learner_ps |
Method for the estimation of the propensity score. |
Value
A list of ITE estimates.
Note
The number of samples and the number of burn are set by default equal to 500.
Estimate the Individual Treatment Effect (ITE) using Causal Forest (CF)
Description
Estimates the Individual Treatment Effect using Causal Forest given a response vector, a treatment vector, and a features matrix.
Usage
estimate_ite_cf(y, z, X, learner_ps)
Arguments
y |
An observed response vector. |
z |
A treatment vector. |
X |
A features matrix. |
learner_ps |
A method for the estimation of the propensity score. |
Value
A list of ITE estimates.
Estimate the Individual Treatment Effect (ITE) using S-Learner
Description
Estimates the Individual Treatment Effect using S-Learner given a response vector, a treatment vector, a features matrix and estimation model for the outcome.
Usage
estimate_ite_slearner(y, z, X, learner_y = "SL.xgboost")
Arguments
y |
An observed response vector. |
z |
A treatment vector. |
X |
A features matrix. |
learner_y |
An estimation model for the outcome. |
Value
A list of ITE estimates.
Estimate the Individual Treatment Effect (ITE) using T-Learner
Description
Estimates the Individual Treatment Effect using T-Learner given a response vector, a treatment vector, a features matrix and estimation model for the outcome.
Usage
estimate_ite_tlearner(y, z, X, learner_y = "SL.xgboost")
Arguments
y |
An observed response vector. |
z |
A treatment vector. |
X |
A features matrix. |
learner_y |
A estimation model for the outcome. |
Value
A list of ITE estimates.
Estimate the Individual Treatment Effect (ITE) using T-Poisson regression
Description
Estimates the Individual Treatment Effect using Poisson regression given a response vector, a treatment vector, and a features matrix.
Usage
estimate_ite_tpoisson(y, z, X, offset)
Arguments
y |
An observed response vector. |
z |
A treatment vector. |
X |
A features matrix. |
offset |
A name of the covariate to use as offset (i.e. “x1”) to
model the corresponding outcome rate. |
Value
A vector of ITE estimates.
Estimate the Individual Treatment Effect (ITE) using X-Learner
Description
Estimates the Individual Treatment Effect using X-Learner given a response vector, a treatment vector, a features matrix and an estimation model for the outcome.
Usage
estimate_ite_xlearner(y, z, X, learner_y = "SL.xgboost")
Arguments
y |
An observed response vector. |
z |
A treatment vector. |
X |
A features matrix. |
learner_y |
A estimation model for the outcome. |
Value
A list of ITE estimates.
Estimate the propensity score
Description
Estimates the Propensity Score given a treatment vector and features data frame.
Usage
estimate_ps(z, X, ps_method = "SL.xgboost")
Arguments
z |
A treatment vector. |
X |
A features data frame. |
ps_method |
An estimation model for the propensity score
(default: |
Value
A vector of propensity score estimates.
Discovery (performance) evaluation
Description
Computes different metrics for discovery evaluation.
Usage
evaluate(ground_truth, prediction)
Arguments
ground_truth |
List of true (CDR/EM) values. |
prediction |
List of predicted (CDR/EM) values. |
Value
Intersection over union, precision, recall.
Extract effect modifiers
Description
Extracts the effect modifiers from a list of (causal) decision rules.
Usage
extract_effect_modifiers(rules_list, X_names)
Arguments
rules_list |
A list of (causal) decision rules. |
X_names |
A list of the covariate names. |
Value
A list of the effect modifiers.
Extract (causal) decision rules
Description
Extracts causal rules from the random forest or the gradient boosting algorithms.
Usage
extract_rules(treelist, X, max_depth, digits = 2)
Arguments
treelist |
A list of decision trees. |
X |
Features matrix. |
max_depth |
A number of top levels from each tree considered. |
digits |
A Number of digits for rounding decision rules to extract conditions. |
Value
A vector of (causal) decision rules.
Filter correlated rules
Description
Discards highly correlated rules (i.e., Cov(rule_1,rule_2) > t_{corr}
).
Usage
filter_correlated_rules(rules_matrix, rules_list, t_corr)
Arguments
rules_matrix |
A rules matrix. |
rules_list |
A list of rules (names). |
t_corr |
A threshold to define correlated rules. |
Value
A rules matrix without the highly correlated rules (columns).
Filter extreme decision rules
Description
Discards rules with too few or too many observations.
Usage
filter_extreme_rules(rules_matrix, rules_list, t_ext)
Arguments
rules_matrix |
A rules matrix. |
rules_list |
A list of rules (names). |
t_ext |
A threshold in [0, 0.5) range to truncate too generic or too specific rules. |
Value
A rules matrix without the rare/common rules.
Filter irrelevant decision rules using leave-one-out pruning
Description
Filters the irrelevant decision rules. The irrelevant rules are interpreted as an error increase after removing a variable-value pair from the decision rules (see “Interpreting tree ensembles with the inTrees package” by Houtao Deng, 2019).
Usage
filter_irrelevant_rules(rules, X, ite, t_decay)
Arguments
rules |
A list of rules. |
X |
A features matrix. |
ite |
An estimated ITE. |
t_decay |
The decay threshold for rules pruning. |
Value
A list of “relevant” rules.
Generate CRE synthetic data
Description
Generates synthetic data sets to run simulation for causal inference
experiments composed by an outcome vector (y
), a treatment vector (z
),
a covariates matrix (X
), and an unobserved individual treatment effects
vector (ite
).
The arguments specify the data set characteristic, including the
number of individuals (n
), the number of covariates (p
), the correlation
within the covariates (rho
), the number of decision rules
(n_rules
) decomposing the Conditional Average Treatment Effect (CATE), the
treatment effect magnitude (effect_size
), the confounding mechanism
(confounding
), and whether the covariates and outcomes are binary or
continuous (binary_covariates
, binary_outcome
).
Usage
generate_cre_dataset(
n = 1000,
rho = 0,
n_rules = 2,
p = 10,
effect_size = 2,
binary_covariates = TRUE,
binary_outcome = TRUE,
confounding = "no"
)
Arguments
n |
An integer number that represents the number of observations. Non-integer values will be converted into an integer number. |
rho |
A positive double number that represents the correlation within the covariates (default: 0, range: [0,1)). |
n_rules |
The number of causal rules (default: 2, range: {1,2,3,4}). |
p |
The number of covariates (default: 10). |
effect_size |
The treatment effect size magnitude (default: 2,
range: |
binary_covariates |
Whether to use binary or continuous covariates
(default: |
binary_outcome |
Whether to use binary or continuous outcomes
(default: |
confounding |
Only for continuous outcome, add confounding variables:
|
Details
The covariates matrix is generated with the specified correlation among
individuals, and each covariate is sampled either from a
Bernoulli(0.5)
if binary, or a Gaussian(0,1)
if continuous.
The treatment vector is sampled from a
Bernoulli
(\frac{1}{1+ \exp(1-x_1+x_2-x_3)}
), enforcing the treatment
assignment probabilities to be a function of observed covariates.
The potential outcomes (y(0)
and y(1)
) are then sampled from a Bernoulli
if binary, or a Gaussian (with standard deviation equal to 1) if continuous.
Their mean is equal to a confounding term (null, linear or non-linear and
always null for binary outcome) plus 1-4 decision rules weighted by the
treatment effect magnitude. The two potential outcomes characterizes the CATE
(and then the unobserved individual treatment effects vector) as the sum of
different additive contributions for each decision rules considered
(plus an intercept).
The final expression of the CATE depends on the treatment effect magnitude
and the number of decision rules considered.
The 4 decision rules are:
Rule 1:
1\{x_1 > 0.5; x_2 \leq 0.5\}(\textbf{x})
Rule 2:
1\{x_5 > 0.5; x_6 \leq 0.5\}(\textbf{x})
Rule 3:
1\{x_4 \leq 0.5\}(\textbf{x})
Rule 4:
1\{x_5 \leq 0.5; x_7 > 0.5; x_8 \leq 0.5\}(\textbf{x})
with corresponding additive average treatment effect (AATE) equal to:Rule 1:
-
effect_size
,Rule 2:
+
effect_size
,Rule 3:
- 0.5 \cdot
effect_size
,Rule 4:
+ 2 \cdot
effect_size
.
In example, setting effect_size
=4 and n_rules
=2:
\text{CATE}(\textbf{x}) = -4 \cdot 1\{x_1 > 0.5; x_2 \leq 0.5\}(\textbf{x}) +
4 \cdot 1\{x_5 > 0.5; x_6 \leq 0.5\}(\textbf{x})
The final outcome vector y
is finally computed by combining the potential
outcomes according to the treatment assignment.
Value
A list, representing the generated synthetic data set, containing:
y |
an outcome vector, |
z |
a treatment vector, |
X |
a covariates matrix, |
ite |
an individual treatment vector. |
Note
Set the covariates domain (binary_covariates
) and outcome domain
(binary_outcome
) according to the experiment of interest.
Increase complexity in heterogeneity discovery:
decreasing the sample size (
n
),adding correlation among covariates (
rho
),increasing the number of rules (
n_rules
),increasing the number of covariates (
p
),decreasing the absolute value of the causal effect (
effect_size
),adding linear or not-linear confounders (
confounding
).
Examples
set.seed(123)
dataset <- generate_cre_dataset(n = 1000, rho = 0, n_rules = 2, p = 10,
effect_size = 2, binary_covariates = TRUE,
binary_outcome = TRUE, confounding = "no")
Generate rules
Description
Generates a list of rules characterizing the heterogeneity in the Conditional Average Treatment Effect (CATE) by tree-based methods (i.e., random forest).
Usage
generate_rules(X, ite, ntrees, node_size, max_rules, max_depth)
Arguments
X |
A covariate matrix. |
ite |
A vector of estimated ITE. |
ntrees |
The number of decision trees for the random forest algorithm. |
node_size |
Minimum size of the trees' terminal nodes. |
max_rules |
Maximum number of candidate decision rules. |
max_depth |
Maximum rules length. |
Value
A list of rules (names).
Generate rules matrix
Description
Generates the rules matrix from the feature covariate matrix and a vector of
rules. The number of rows in rules_matrix is equal to the number of samples
in X
, and the number of columns is equal to the number of rules in
rules_list
. Each element of rules_matrix corresponds to a specific data
sample and rule. If the data sample satisfies a rule, the corresponding
element in rules_matrix is set to 1. Otherwise, the element is set to 0.
Usage
generate_rules_matrix(X, rules_list)
Arguments
X |
Features matrix. |
rules_list |
A vector of rules. |
Value
A causal rules matrix.
Get Logger settings
Description
Returns current logger settings.
Usage
get_logger()
Value
Returns a list that includes logger_file_path and logger_level.
See Also
set_logger
for information on setting the log level and file path.
Examples
set_logger("mylogger.log", "INFO")
log_meta <- get_logger()
Honest splitting
Description
Splits data into discovery and inference sub-samples.
Usage
honest_splitting(y, z, X, ratio_dis, ite = NULL)
Arguments
y |
An observed response vector. |
z |
A treatment vector. |
X |
A features matrix. |
ratio_dis |
A double number indicating the ratio of data delegated to the discovery sub-sample. |
ite |
A vector of estimated ITE. |
Value
A list containing the discovery and inference sub-samples.
Interpret rules
Description
Replaces the column numbers in the rules vector with their real names.
Usage
interpret_rules(rules, X_names)
Arguments
rules |
A vector of rules. |
X_names |
A vector of real names of the covariates. |
Value
A list of explicit (human-readable) rules.
Extend generic plot functions for cre class
Description
A wrapper function to extend generic plot functions for cre class.
Usage
## S3 method for class 'cre'
plot(x, ...)
Arguments
x |
A |
... |
Additional arguments passed to customize the plot. |
Value
Returns a ggplot2 object, invisibly. This function is called for side effects.
Predict individual treatment effect via causal rule ensemble
Description
Predicts individual treatment effect via causal rule ensemble algorithm.
Usage
## S3 method for class 'cre'
predict(object, X, ...)
Arguments
object |
A |
X |
A covariate matrix (or data.frame) |
... |
Additional arguments passed to customize the prediction. |
Value
An array with the estimated Individual Treatment Effects
Extend print function for the CRE object
Description
Prints a brief summary of the CRE object
Usage
## S3 method for class 'cre'
print(x, verbose = 2, ...)
Arguments
x |
A cre object from running the CRE function. |
verbose |
Set level of results description details: 0 for only results summary, 1 for results and parameters summary, 2 for results and parameters and rules summary (default 2). |
... |
Additional arguments passed to customize the results description. |
Value
No return value. This function is called for side effects.
Select rules
Description
Given a set of rules, selects the minimal set linearly decomposing the Conditional Average Treatment Effect (CATE) by LASSO (optionally with Stability Selection).
Usage
select_rules(rules_matrix, rules, ite, stability_selection, cutoff, pfer, B)
Arguments
rules_matrix |
The rules matrix. |
rules |
A list of rules. |
ite |
The estimated ITE. |
stability_selection |
Stability selection method. |
cutoff |
Threshold (percentage) defining the minimum cutoff value for the stability scores. Only for stability selection. |
pfer |
Upper bound for the per-family error rate (tolerated amount of falsely selected rules). Only for stability selection. |
B |
Number of bootstrap samples. |
Value
A minimal set of rules linearly decomposing the CATE.
Set Logger settings
Description
Updates logger settings, including log level and location of the file.
Usage
set_logger(logger_file_path = "CRE.log", logger_level = "INFO")
Arguments
logger_file_path |
A path (including file name) to log the messages. (Default: CRE.log) |
logger_level |
The log level. When a log level is set, all log levels below it are also activated (if implemented). Available levels include:
|
Value
No return value. This function is called for side effects.
Note
Log levels are specified by developers during the initial implementation. Future developers or contributors can leverage these log levels to better capture and document the application's processes and events.
Examples
set_logger("Debug")
Standardize Rules Matrix
Description
Standardize (i.e. mean=0 and stdev=1) the rules matrix.
Usage
standardize_rules_matrix(rules_matrix)
Arguments
rules_matrix |
The rules matrix. |
Value
Standardized rules matrix
Print summary of CRE object
Description
Prints a brief summary of the CRE object
Usage
## S3 method for class 'cre'
summary(object, verbose = 2, ...)
Arguments
object |
A cre object from running the CRE function. |
verbose |
Set level of results description details: only results summary 0, results+parameters summary 1, results+parameters+rules summary (default 2). |
... |
Additional arguments passed to customize the results description. |
Value
A summary of the CRE object