# Quick Start Guide: Optimal Policy Trees with Categorical Treatment

*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 Policy Trees with categoric treatment options. 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 policy 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 ]
```

Policy trees are trained using a features matrix/dataframe `X`

as usual and a rewards matrix that has one column for each potential treatment that contains the outcome for each sample under that treatment.

There are two ways to get this rewards matrix:

- in rare cases, the problem may have full information about the outcome associated with each treatment for each sample
- more commonly, we have observational data, and use this partial data to train models to estimate the outcome associated with each treatment

Refer to the documentation on data preparation for more information on the data format.

In this case, the dataset is observational, and so we will use RewardEstimation to estimate our rewards matrix.

## Reward Estimation

First, we split into training and testing:

```
X <- subset(df, select = -c(A1, A16))
treatments <- df$A1
outcomes <- df$A16 == "+"
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.

The treatment, `A1`

, is a categoric variable, so we follow the process for estimating rewards with categorical treatments.

Our outcome is binary, so 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:

```
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,
)
train_rewards <- iai::fit_predict(
reward_lnr, train_X, train_treatments, train_outcomes,
propensity_score_criterion = "auc", outcome_score_criterion = "auc")
train_rewards$predictions$reward
```

```
a b
1 1.1597599 0.9800000
2 0.5000000 1.0034353
3 0.4000000 1.2674998
4 0.6000000 1.0675597
5 1.2616685 0.7800000
6 0.3000000 1.0754834
7 0.7000000 1.0499854
8 0.0300000 1.1795000
9 2.7208300 0.5845882
10 0.3133333 1.8281435
11 1.4972260 0.9000000
12 1.5506916 0.9700000
13 0.5500000 1.2890751
14 1.1879195 0.8300000
15 1.5939267 0.9800000
16 1.4590307 0.8650000
17 0.8100000 1.0473854
18 0.7400000 1.0896091
19 2.2676499 0.8466667
20 0.6200000 1.0457309
21 0.5700000 1.1194935
22 0.8600000 1.0083640
23 0.8600000 1.0156483
24 0.6700000 1.0042744
25 1.2875808 0.8700000
26 0.8300000 1.0548315
27 0.1400000 1.3888831
28 0.6200000 1.1043835
29 0.4700000 1.1903813
30 0.5433333 1.0585528
[ reached 'max' / getOption("max.print") -- omitted 296 rows ]
```

```
train_rewards$score
```

```
$propensity
[1] 0.6065933
$outcome
$outcome$b
[1] 0.9233721
$outcome$a
[1] 0.956213
```

We can see that the internal outcome estimation models have AUCs of 0.92 and 0.96, 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.

## Optimal Policy Trees

Now that we have a complete rewards matrix, we can train a tree to learn an optimal prescription policy that maximizes credit approval outcome. We will use a `grid_search`

to fit an `optimal_tree_policy_maximizer`

(note that if we were trying to minimize the outcomes, we would use `optimal_tree_policy_minimizer`

):

```
grid <- iai::grid_search(
iai::optimal_tree_policy_maximizer(
random_seed = 121,
max_categoric_levels_before_warning = 20,
),
max_depth = 1:5,
)
iai::fit(grid, train_X, train_rewards$predictions$reward)
iai::get_learner(grid)
```

```
Fitted OptimalTreePolicyMaximizer:
1) Split: A6 in [d,e,ff,i,k,m,q,r]
2) Split: A11 < 3.5
3) Prescribe: b, 128 points, error 5.442
4) Prescribe: a, 37 points, error 4.912
5) Split: A8 < 1.438
6) Prescribe: a, 83 points, error 5.259
7) Prescribe: b, 78 points, error 5.048
```

The resulting tree recommends different values for `A1`

based on other characteristics: namely, the values of `A6`

, `A8`

, and `A11`

. The intensity of the color in each leaf shows the difference in quality between the best and second-best values. We can see that both `a`

and `b`

values are prescribed by the tree, implying each treatment is better suited to certain subgroups.

We can make treatment prescriptions using `predict`

:

```
iai::predict(grid, train_X)
```

```
[1] "a" "b" "b" "b" "a" "b" "b" "b" "b" "b" "a" "a" "b" "a" "b" "b" "a" "b" "b"
[20] "b" "a" "b" "b" "b" "a" "b" "b" "a" "a" "b" "b" "b" "a" "a" "b" "b" "b" "b"
[39] "b" "b" "a" "b" "b" "b" "b" "a" "b" "b" "a" "a" "b" "b" "a" "b" "b" "a" "b"
[58] "b" "b" "b"
[ reached getOption("max.print") -- omitted 266 entries ]
```

If we want more information about the relative performance of treatments for these points, we can predict the full treatment ranking with `predict_treatment_rank`

:

```
iai::predict_treatment_rank(grid, train_X)
```

```
[,1] [,2]
[1,] "a" "b"
[2,] "b" "a"
[3,] "b" "a"
[4,] "b" "a"
[5,] "a" "b"
[6,] "b" "a"
[7,] "b" "a"
[8,] "b" "a"
[9,] "b" "a"
[10,] "b" "a"
[11,] "a" "b"
[12,] "a" "b"
[13,] "b" "a"
[14,] "a" "b"
[15,] "b" "a"
[16,] "b" "a"
[17,] "a" "b"
[18,] "b" "a"
[19,] "b" "a"
[20,] "b" "a"
[21,] "a" "b"
[22,] "b" "a"
[23,] "b" "a"
[24,] "b" "a"
[25,] "a" "b"
[26,] "b" "a"
[27,] "b" "a"
[28,] "a" "b"
[29,] "a" "b"
[30,] "b" "a"
[ reached getOption("max.print") -- omitted 296 rows ]
```

To quantify the difference in performance behind the treatment rankings, we can use `predict_treatment_outcome`

to extract the estimated quality of each treatment for each point:

```
iai::predict_treatment_outcome(grid, train_X)
```

```
a b
1 0.9035714 0.7600418
2 0.4848171 0.7673394
3 0.1105383 0.3738667
4 0.1105383 0.3738667
5 0.5572175 0.3203792
6 0.1105383 0.3738667
7 0.4848171 0.7673394
8 0.4848171 0.7673394
9 0.1105383 0.3738667
10 0.1105383 0.3738667
11 0.9035714 0.7600418
12 0.9035714 0.7600418
13 0.1105383 0.3738667
14 0.5572175 0.3203792
15 0.4848171 0.7673394
16 0.4848171 0.7673394
17 0.9035714 0.7600418
18 0.4848171 0.7673394
19 0.1105383 0.3738667
20 0.4848171 0.7673394
21 0.5572175 0.3203792
22 0.4848171 0.7673394
23 0.4848171 0.7673394
24 0.4848171 0.7673394
25 0.9035714 0.7600418
26 0.4848171 0.7673394
27 0.1105383 0.3738667
28 0.5572175 0.3203792
29 0.5572175 0.3203792
30 0.4848171 0.7673394
[ reached 'max' / getOption("max.print") -- omitted 296 rows ]
```

## Evaluating Optimal Policy Trees

It is critical for a fair evaluation that we do not evaluate the quality of the policy using rewards from our existing reward estimator trained on the training set. This is to avoid any information from the training set leaking through to the out-of-sample evaluation.

Instead, what we need to do is to estimate a new set of rewards using only the test set, and evaluate the policy against these rewards:

```
test_rewards <- iai::fit_predict(
reward_lnr, test_X, test_treatments, test_outcomes, propensity_score_criterion = "auc", outcome_score_criterion = "auc")
test_rewards$predictions$reward
```

```
a b
1 0.5980000 1.1768124
2 1.1997581 0.7066667
3 0.7400000 1.1599571
4 0.8200000 1.0654951
5 0.7100000 1.0622504
6 0.6200000 1.1830213
7 0.8200000 1.0157781
8 0.7000000 1.3037751
9 1.1095968 0.5662857
10 1.5166442 0.6450000
11 1.3735566 0.7800000
12 0.9500000 1.0130374
13 0.8700000 1.0362525
14 0.9000000 1.0115436
15 0.7700000 1.0891463
16 1.0456397 0.7500000
17 0.8100000 1.0488323
18 1.6339495 0.7400000
19 0.9000000 1.0797287
20 1.0000000 1.0505727
21 0.6100000 1.1454387
22 0.8600000 1.0820338
23 0.1466667 1.1019290
24 0.0500000 1.2922604
25 2.0824697 0.5303333
26 0.5900000 1.1802677
27 0.6392308 1.0798662
28 0.8200000 1.2005826
29 1.2205188 0.7200000
30 1.1946013 0.6650000
[ reached 'max' / getOption("max.print") -- omitted 297 rows ]
```

```
test_rewards$score
```

```
$propensity
[1] 0.6170773
$outcome
$outcome$b
[1] 0.92533
$outcome$a
[1] 0.9198603
```

We see the scores are similar to those on the training set, giving us confidence that the estimated rewards are a fair reflection of reality, and will serve as a good basis for evaluation. The AUC for the propensity model is again on the low side, but should be compensated for by means of the doubly-robust estimator.

We can now evaluate the quality using these new estimated rewards. First, we will calculate the average predicted credit approval probability under the treatments prescribed by the tree for the test set. To do this, we use `predict_outcomes`

which uses the model to make prescriptions and looks up the predicted outcomes under these prescriptions:

```
policy_outcomes <- iai::predict_outcomes(grid, test_X,
test_rewards$predictions$reward)
```

```
[1] 0.5980000 0.7066667 1.1599571 1.0654951 0.7100000 1.1830213
[7] 1.0157781 0.7000000 0.5662857 0.6450000 1.3735566 1.0130374
[13] 1.0362525 1.0115436 0.7700000 1.0456397 1.0488323 1.6339495
[19] 0.9000000 1.0505727 0.6100000 1.0820338 1.1019290 0.0500000
[25] 2.0824697 0.5900000 0.6392308 0.8200000 0.7200000 0.6650000
[31] 1.0499801 0.9800000 1.1071945 -0.1655813 -0.6792322 -1.0552431
[37] 0.6092308 -0.2852459 -0.2092509 -0.2007457 -0.1079119 -0.7808135
[43] -0.2972091 0.7800000 -1.0267581 -0.2612700 0.5700000 0.6700000
[49] 0.7000000 0.7066190 0.7800000 0.8600000 0.8866667 1.1156161
[55] 1.0011673 0.8400000 0.8800000 3.1262855 1.1517495 0.7900000
[ reached getOption("max.print") -- omitted 267 entries ]
```

We can then get the average estimated approval probability under our treatments:

```
mean(policy_outcomes)
```

`[1] 0.4077158`

We can compare this number to the average approval probability under the treatment assignments that were actually observed:

```
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(test_treatments, test_rewards$predictions$reward)
```

`[1] 0.4082059`

We see this policy is about the same as the real treatments.