Quick Start Guide: Optimal Prescriptive Trees
This is an R version of the corresponding OptimalTrees quick start guide.
In this guide we will give a demonstration of how to use Optimal Prescriptive Trees (OPT). For this example, we will use the Credit Approval dataset, where the task is to predict the approval outcome of credit card application. Because the features have been anonymized for confidentiality reasons, we will arbitrarily select one of the categoric variables A1
to be the treatment.
Note: this case is not intended to serve as a practical application of prescriptive trees, but rather to serve as an illustration of the training and evaluation process.
First we load in the data and drop 37 rows with missing values:
df <- read.csv('crx.data', na.strings = "?", stringsAsFactors = T, header = F,
col.names = paste("A", as.character(seq(1, 16)), sep=""))
df <- na.omit(df)
A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16
1 b 30.83 0.00 u g w v 1.25 t t 1 f g 202 0 +
2 a 58.67 4.46 u g q h 3.04 t t 6 f g 43 560 +
3 a 24.50 0.50 u g q h 1.50 t f 0 f g 280 824 +
[ reached 'max' / getOption("max.print") -- omitted 650 rows ]
Data for prescriptive problems
Prescriptive trees are trained on observational data, and require three distinct types of data:
X
: the features for each observation that can be used as the splits in the tree - this can be a matrix or a dataframe as for classification or regression problemstreatments
: the treatment applied to each observation - this is a vector of the treatment labels similar to the target in a classification problemoutcomes
: the outcome for each observation under the applied treatment - this is a vector of numeric values similar to the target in a regression problem
Refer to the documentation on data preparation for more information on the data format.
In this case, the treatment is the variable A1
, and the outcome is whether the application was approved (which we are trying to make as likely as possible):
X <- subset(df, select = -c(A1, A16))
treatments <- df$A1
outcomes <- df$A16 == '+'
We can now split into training and test datasets:
split <- iai::split_data("policy_maximize", X, treatments, outcomes, seed = 1,
train_proportion = 0.6)
train_X <- split$train$X
train_treatments <- split$train$treatments
train_outcomes <- split$train$outcomes
test_X <- split$test$X
test_treatments <- split$test$treatments
test_outcomes <- split$test$outcomes
Note that we have used a training/test split of 60%/40%, so that we save more data for testing to ensure high-quality reward estimation on the test set.
Fitting Optimal Prescriptive Trees
We will use a grid_search
to fit an optimal_tree_prescription_maximizer
(note that if we were trying to minimize the outcomes, we would use optimal_tree_prescription_minimizer
):
grid <- iai::grid_search(
iai::optimal_tree_prescription_maximizer(
prescription_factor = 0.6,
treatment_minbucket = 10,
random_seed = 234,
max_categoric_levels_before_warning = 15
),
max_depth = 1:5,
)
iai::fit(grid, train_X, train_treatments, train_outcomes)
iai::get_learner(grid)
Here, we have set prescription_factor = 0.6
to strike a balance between maximizing the outcome and estimating the approval probability, and treatment_minbucket = 10
so that the tree can only prescribe a treatment in a leaf if there are at least 10 subjects in that leaf that received this treatment. This is to ensure that we have sufficient data on how the treatment affects subjects in this leaf before we can prescribe it.
In the resulting tree, the color in each leaf indicates which treatment is deemed to be stronger in this leaf, and the color intensity indicates the size of the difference. Because the variables are anonymized, we cannot directly interpret the tree, but we can see that both treatments are prescribed based on a variety of the features.
We can make predictions on new data using predict
:
pred <- iai::predict(grid, test_X)
This returns the treatment prescribed for each subject as well as the outcome predicted for each subject under the prescribed treatment:
pred$treatments
[1] "b" "b" "a" "b" "a" "b" "a" "b" "b" "b" "b" "b" "b" "b" "b" "a" "b" "b" "b"
[20] "b" "a" "a" "a" "b" "a" "b" "b" "a" "a" "b" "b" "b" "a" "b" "b" "b" "b" "b"
[39] "b" "b" "b" "b" "b" "b" "a" "a" "b" "b" "b" "b" "a" "b" "b" "b" "a" "b" "b"
[58] "a" "a" "a"
[ reached getOption("max.print") -- omitted 201 entries ]
pred$outcomes
[1] 1.0000000 1.0000000 1.0000000 0.5333333 1.0000000 0.1111111 1.0000000
[8] 1.0000000 0.5333333 0.5333333 0.8181818 0.5333333 1.0000000 1.0000000
[15] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
[22] 0.0000000 0.6000000 0.7000000 1.0000000 0.7000000 1.0000000 1.0000000
[29] 1.0000000 1.0000000 0.5333333 0.5333333 0.7500000 0.5333333 0.5333333
[36] 0.5333333 0.5333333 0.5333333 0.5333333 0.5333333 0.9000000 0.7000000
[43] 0.5333333 0.7000000 1.0000000 1.0000000 1.0000000 1.0000000 0.5333333
[50] 0.8181818 1.0000000 0.5333333 1.0000000 0.8181818 1.0000000 1.0000000
[57] 1.0000000 1.0000000 0.7500000 1.0000000
[ reached getOption("max.print") -- omitted 201 entries ]
You can also use predict_outcomes
to get the predicted outcomes for all treatments:
iai::predict_outcomes(grid, test_X)
a b
1 0.9090909 1.0000000
2 0.9090909 1.0000000
3 1.0000000 0.6136364
4 0.3333333 0.5333333
5 1.0000000 0.6136364
6 0.0000000 0.1111111
7 1.0000000 0.9200000
8 0.9090909 1.0000000
9 0.3333333 0.5333333
10 0.3333333 0.5333333
11 0.6000000 0.8181818
12 0.3333333 0.5333333
13 0.9090909 1.0000000
14 0.9090909 1.0000000
15 0.9090909 1.0000000
16 1.0000000 0.9200000
17 0.9090909 1.0000000
18 0.9090909 1.0000000
19 0.9090909 1.0000000
20 0.9090909 1.0000000
21 1.0000000 0.9200000
22 0.0000000 0.0000000
23 0.6000000 0.0952381
24 0.0625000 0.7000000
25 1.0000000 0.6136364
26 0.0625000 0.7000000
27 0.9090909 1.0000000
28 1.0000000 0.9200000
29 1.0000000 0.6136364
30 0.9090909 1.0000000
[ reached 'max' / getOption("max.print") -- omitted 231 rows ]
Evaluating Optimal Prescriptive Trees
In prescription problems, it is complicated to evaluate the quality of a prescription policy because our data only contains the outcome for the treatment that was received. Because we don't know the outcomes for the treatments that were not received (known as the counterfactuals), we cannot simply evaluate our prescriptions against the test set as we normally do.
A common approach to resolve this problem is reward estimation, where so-called rewards are estimated for each treatment for each observation. These rewards indicate the relative credit a model should be given for prescribing each treatment to each observation, and thus can be used to evaluate the quality of the prescription policy. For more details on how the reward estimation procedure is conducted, refer to the reward estimation documentation.
We use a categorical_reward_estimator
to estimate the approval outcome under each option with a doubly-robust reward estimation method using random forests to estimate both propensity scores and outcomes. Note that we are passing in the test data rather the training data to ensure we get a fair out-of-sample evaluation:
reward_lnr <- iai::categorical_reward_estimator(
propensity_estimator = iai::random_forest_classifier(),
outcome_estimator = iai::random_forest_classifier(),
reward_estimator = "doubly_robust",
random_seed = 1,
)
test_rewards <- iai::fit_predict(
reward_lnr, test_X, test_treatments, test_outcomes, propensity_score_criterion = "auc", outcome_score_criterion = "auc")
test_rewards$rewards
a b
1 1.05874091 0.9589181
2 0.69471041 1.0404291
3 0.53809524 1.4717999
4 0.49954545 1.2965528
5 2.21111945 0.5886364
6 0.23000000 1.2790227
7 0.32408541 1.0823379
8 1.50441067 0.9214676
9 0.61850000 1.0825273
10 0.34881297 1.1061304
11 0.79583333 1.0271951
12 0.86009868 1.0567228
13 0.93245098 1.0073525
14 0.83729187 1.0203060
15 1.05977627 0.8547368
16 0.84566667 1.0336681
17 0.79666667 1.0249298
18 0.79199415 1.0021401
19 0.91333333 1.0041882
20 0.90827273 1.0126069
21 1.80807455 0.7938889
22 0.09090909 1.2545612
23 4.47151165 0.6182483
24 0.21229293 1.3720624
25 0.50138889 1.1985341
26 0.43333333 1.1443273
27 0.79699415 1.0877025
28 0.87276535 1.0095916
29 1.62058188 0.7776111
30 0.96000000 1.0741291
[ reached 'max' / getOption("max.print") -- omitted 231 rows ]
test_rewards$score
$propensity
[1] 0.5759206
$outcome
$outcome$b
[1] 0.9409791
$outcome$a
[1] 0.9158503
We can see that the internal outcome estimation models have AUCs of 0.94 and 0.92, which gives us confidence that the reward estimates are of decent quality, and good to base our training on. The AUC for the propensity model is lower at 0.58, which is not particularly high, and suggests difficulty in reliably estimating the propensity. The doubly-robust estimation method should help to alleviate this problem, as it is designed to deliver good results if either propensity scores or outcomes are estimated well. However, we should always pay attention to these scores and proceed with caution if the estimation quality is low.
We can now use these reward values to evaluate the prescription in many ways. For example, we might like to see the mean reward achieved across all prescriptions on the test set:
evaluate_reward_mean <- function(treatments, rewards) {
total <- 0.0
for (i in seq(treatments)) {
total <- total + rewards[i, treatments[i]]
}
total / length(treatments)
}
evaluate_reward_mean(pred$treatments, test_rewards$rewards)
[1] 0.4543409
For comparison's sake, we can compare this to the mean reward achieved under the actual treatment assignments that were observed in the data:
evaluate_reward_mean(test_treatments, test_rewards$rewards)
[1] 0.4234183
We can see that the prescriptive tree policy indeed improves on the actual assignments.