# 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 problems`treatments`

: the treatment applied to each observation - this is a vector of the treatment labels similar to the target in a classification problem`outcomes`

: 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 = 123,
train_proportion = 0.5)
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 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 = 123,
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] "a" "b" "b" "b" "a" "a" "b" "a" "b" "b" "a" "a" "a" "a" "a" "b" "a" "b" "a"
[20] "b" "a" "a" "b" "b" "a" "a" "a" "a" "b" "b" "b" "b" "b" "a" "b" "a" "a" "a"
[39] "a" "b" "b" "b" "a" "b" "a" "b" "a" "a" "a" "b" "b" "b" "a" "b" "b" "a" "b"
[58] "a" "a" "b"
[ reached getOption("max.print") -- omitted 267 entries ]
```

```
pred$outcomes
```

```
[1] 0.90909091 0.75000000 0.75000000 0.75000000 0.80000000 0.90909091
[7] 0.93548387 0.90909091 1.00000000 0.93548387 1.00000000 1.00000000
[13] 1.00000000 1.00000000 1.00000000 1.00000000 0.80000000 1.00000000
[19] 1.00000000 0.93548387 0.90909091 1.00000000 0.07894737 0.07894737
[25] 0.90909091 0.90909091 0.80000000 0.90909091 0.93548387 0.93548387
[31] 1.00000000 0.93548387 0.93548387 1.00000000 1.00000000 0.90909091
[37] 0.90909091 0.90909091 1.00000000 0.75000000 0.75000000 1.00000000
[43] 0.80000000 1.00000000 0.90909091 0.93548387 0.90909091 0.90909091
[49] 0.90909091 1.00000000 1.00000000 0.93548387 1.00000000 0.93548387
[55] 0.93548387 1.00000000 0.93548387 0.90909091 1.00000000 0.93548387
[ reached getOption("max.print") -- omitted 267 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 0.41666667
2 0.0000000 0.75000000
3 0.0000000 0.75000000
4 0.0000000 0.75000000
5 0.8000000 0.28000000
6 0.9090909 0.41666667
7 1.0000000 0.93548387
8 0.9090909 0.41666667
9 0.7000000 1.00000000
10 1.0000000 0.93548387
11 1.0000000 0.80000000
12 1.0000000 0.80000000
13 1.0000000 0.80000000
14 1.0000000 0.80000000
15 1.0000000 0.80000000
16 0.7000000 1.00000000
17 0.8000000 0.28000000
18 0.7000000 1.00000000
19 1.0000000 0.80000000
20 1.0000000 0.93548387
21 0.9090909 0.41666667
22 1.0000000 0.80000000
23 0.0000000 0.07894737
24 0.0000000 0.07894737
25 0.9090909 0.41666667
26 0.9090909 0.41666667
27 0.8000000 0.28000000
28 0.9090909 0.41666667
29 1.0000000 0.93548387
30 1.0000000 0.93548387
[ reached 'max' / getOption("max.print") -- omitted 297 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_classification_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_classification_reward_estimator(
propensity_estimator = iai::random_forest_classifier(),
outcome_estimator = iai::random_forest_classifier(),
reward_estimator = "doubly_robust",
random_seed = 123,
)
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 0.6027619 1.1714377
2 1.1994289 0.7066667
3 0.7400000 1.1499867
4 0.8175000 1.0664644
5 0.7155000 1.0679181
6 0.6233333 1.1766472
7 0.8200000 1.0171730
8 0.7014286 1.3139850
9 1.1087486 0.5662857
10 1.5083790 0.6450000
11 1.3935686 0.7800000
12 0.9542857 1.0130374
13 0.8700000 1.0399533
14 0.9000000 1.0104069
15 0.7700000 1.0877628
16 1.0471866 0.7500000
17 0.8066667 1.0557882
18 1.6507572 0.7400000
19 0.9000000 1.0866270
20 1.0000000 1.0529102
21 0.6100000 1.1498051
22 0.8600000 1.0820338
23 0.1466667 1.0968707
24 0.0500000 1.2888514
25 2.0438714 0.5386667
26 0.5900000 1.1883981
27 0.6425641 1.0793621
28 0.8200000 1.1932089
29 1.2323624 0.7083333
30 1.1865466 0.6650000
[ reached 'max' / getOption("max.print") -- omitted 297 rows ]
```

```
test_rewards$score
```

```
$propensity
[1] 0.613628
$outcome
$outcome$b
[1] 0.9246304
$outcome$a
[1] 0.9197715
```

We can see that the internal outcome estimation models have AUCs of 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.61, 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.4362671`

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.4065159`

We can see that the prescriptive tree policy indeed improves on the actual assignments.