`personalized`

To demonstrate how to use the `personalized`

package with multi-category treatments, we simulate data with three treatments. The treatment assignments will be based on covariates and hence mimic an observational setting with no unmeasured confounders.

```
set.seed(123)
n.obs <- 250
n.vars <- 15
x <- matrix(rnorm(n.obs * n.vars, sd = 3), n.obs, n.vars)
# simulated non-randomized treatment with multiple levels
# based off of a multinomial logistic model
xbetat_1 <- 0.1 + 0.5 * x[,1] - 0.25 * x[,5]
xbetat_2 <- 0.1 - 0.5 * x[,11] + 0.25 * x[,15]
trt.1.prob <- exp(xbetat_1) / (1 + exp(xbetat_1) + exp(xbetat_2))
trt.2.prob <- exp(xbetat_2) / (1 + exp(xbetat_1) + exp(xbetat_2))
trt.3.prob <- 1 - (trt.1.prob + trt.2.prob)
prob.mat <- cbind(trt.1.prob, trt.2.prob, trt.3.prob)
trt.mat <- apply(prob.mat, 1, function(rr) rmultinom(1, 1, prob = rr))
trt.num <- apply(trt.mat, 2, function(rr) which(rr == 1))
trt <- as.factor(paste0("Trt_", trt.num))
# simulate response
# effect of treatment 1 relative to treatment 3
delta1 <- 2 * (0.5 + x[,2] - 2 * x[,3] )
# effect of treatment 2 relative to treatment 3
delta2 <- (0.5 + x[,6] - 2 * x[,5] )
# main covariate effects with nonlinearities
xbeta <- x[,1] + x[,11] - 2 * x[,12]^2 + x[,13] +
0.5 * x[,15] ^ 2 + 2 * x[,2] - 3 * x[,5]
# create entire functional form of E(Y|T,X)
xbeta <- xbeta +
delta1 * ((trt.num == 1) - (trt.num == 3) ) +
delta2 * ((trt.num == 2) - (trt.num == 3) )
# simulate continuous outcomes E(Y|T,X)
y <- xbeta + rnorm(n.obs, sd = 2)
```

We will use the version of the treatment status vector in our analysis, however, the integer values vector, i.e. , could be used as well.

```
## [1] Trt_2 Trt_3 Trt_1 Trt_1 Trt_1
## Levels: Trt_1 Trt_2 Trt_3
```

```
## trt
## Trt_1 Trt_2 Trt_3
## 81 92 77
```

Then we construct a propensity score function that takes covariate information and the treatment statuses as input and generate a matrix of probabilities as output. Each row \(i\) of the output matrix represents an observation and each column \(j\) is the probability that the \(i\)th patient received the \(j\)th treatment. The treatment levels are ordered alphabetically (or numerically if the treatment assignment vector is a vector of integers). Our propensity score model in this example will be a multinomial logistic regression model with a lasso penalty for the probability of treatment assignments conditional on covariate information:

```
propensity.multinom.lasso <- function(x, trt)
{
if (!is.factor(trt)) trt <- as.factor(trt)
gfit <- cv.glmnet(y = trt, x = x, family = "multinomial")
# predict returns a matrix of probabilities:
# one column for each treatment level
propens <- drop(predict(gfit, newx = x,
type = "response", s = "lambda.min"))
# return the matrix probability of treatment assignments
probs <- propens[,match(levels(trt), colnames(propens))]
probs
}
```

An important assumption for the propensity score is that \(0 < Pr(T_i = t | \mathbf X) < 1\) for all \(\mathbf X\) and \(t\). This assumption, often called the positivity assumption, is impossible to verify. However, in practice validity of the assumption can be assessed via a visualization of the empirical overlap of our estimated propensity scores to determine if there is any evidence of positivity violations. The function also allows us to visualize the overlap of our propensity scores for multi-category treatment applications. The following code results in the plot shown Figure .

Each plot above is for a different treatment group, e.g.Â the plot in the first row of plots is the subset of patients who received treatment 1. There seems to be no obvious evidence against the positivity assumption.

As the outcome is continuous and there is a large number of covariates available for our construction of a benefit score, we will use the squared error loss and a lasso penalty. The model can be fit in the same manner as for the binary treatment setting, however only linear models and the weighting method are available. Here we can also specify the reference treatment (the treatment that the non-reference treatments are compared with by each benefit score).

```
set.seed(123)
subgrp.multi <- fit.subgroup(x = x, y = y,
trt = trt, propensity.func = propensity.multinom.lasso,
reference.trt = "Trt_3",
loss = "sq_loss_lasso")
summary(subgrp.multi)
```

```
## family: gaussian
## loss: sq_loss_lasso
## method: weighting
## cutpoint: 0
## propensity
## function: propensity.func
##
## benefit score: f_Trt_1(x): Trt_1 vs Trt_3, f_Trt_2(x): Trt_2 vs Trt_3
## f_Trt_3(x): 0
## maxval = max(f_Trt_1(x), f_Trt_2(x))
## which.max(maxval) = The trt level which maximizes maxval
## Trt recom = which.max(maxval)*I(maxval > c) + Trt_3*I(maxval <= c) where c is 'cutpoint'
##
## Average Outcomes:
## Recommended Trt_1 Recommended Trt_2 Recommended Trt_3
## Received Trt_1 4.3602 (n = 22) -16.7813 (n = 53) -18.0823 (n = 6)
## Received Trt_2 -11.3726 (n = 49) 0.4374 (n = 20) -19.28 (n = 23)
## Received Trt_3 -21.6418 (n = 41) -23.2637 (n = 28) 4.2641 (n = 8)
##
## Treatment effects conditional on subgroups:
## Est of E[Y|T=Trt_1,Recom=Trt_1]-E[Y|T=/=Trt_1,Recom=Trt_1]
## 20.1492 (n = 112)
## Est of E[Y|T=Trt_2,Recom=Trt_2]-E[Y|T=/=Trt_2,Recom=Trt_2]
## 20.8058 (n = 101)
## Est of E[Y|T=Trt_3,Recom=Trt_3]-E[Y|T=/=Trt_3,Recom=Trt_3]
## 23.1599 (n = 37)
##
## NOTE: The above average outcomes are biased estimates of
## the expected outcomes conditional on subgroups.
## Use 'validate.subgroup()' to obtain unbiased estimates.
##
## ---------------------------------------------------
##
## Benefit score 1 quantiles (f(X) for Trt_1 vs Trt_3):
## 0% 25% 50% 75% 100%
## -53.045 -6.620 3.967 16.523 55.446
##
## Benefit score 2 quantiles (f(X) for Trt_2 vs Trt_3):
## 0% 25% 50% 75% 100%
## -38.412 -7.542 2.107 12.696 55.281
##
## ---------------------------------------------------
##
## Summary of individual treatment effects:
## E[Y|T=trt, X] - E[Y|T=Trt_3, X]
## where 'trt' is Trt_1 and Trt_2
##
## Trt_1-vs-Trt_3 Trt_2-vs-Trt_3
## Min. :-106.090 Min. :-76.823
## 1st Qu.: -13.239 1st Qu.:-15.084
## Median : 7.933 Median : 4.213
## Mean : 8.641 Mean : 5.910
## 3rd Qu.: 33.045 3rd Qu.: 25.392
## Max. : 110.892 Max. :110.562
##
## ---------------------------------------------------
##
## 17 out of 30 interactions selected in total by the lasso (cross validation criterion).
##
## The first estimate is the treatment main effect, which is always selected.
## Any other variables selected represent treatment-covariate interactions.
##
##
## 9 out of 15 variables selected for delta 1 by the lasso (cross validation criterion).
##
## Trt_1 V1 V2 V3 V4
## Estimates for delta (Trt_1 vs Trt_3) 2.6945 -1.7711 1.8696 -3.6292 0.2637
## V5 V9 V10 V13 V15
## Estimates for delta (Trt_1 vs Trt_3) 2.2749 -2.7176 1.351 0.2342 0.5398
##
## 8 out of 15 variables selected for delta 2 by the lasso (cross validation criterion).
##
## Trt_2 V1 V2 V5 V6
## Estimates for delta (Trt_2 vs Trt_3) 2.5083 2.7408 1.3139 -3.055 1.6852
## V8 V11 V12 V13
## Estimates for delta (Trt_2 vs Trt_3) 0.7866 2.0719 1.7789 1.3184
```

The function now displays selected variables for each of the two benefit scores and shows the quantiles of each benefit score. We can also plot the empirical observations within the different subgroups using the function, however now it is slightly more complicated. It appears that the average outcome is higher for those who received the level of the treatment they were recommended than those who received a different treatment than they were recommended. Also note that returns a object and can thus be modified by the user. The below example yields Figure .

To obtain valid estimates of the subgroup-specific treatment effects, we perform the repeated training and testing resample procedure using the function (here we set the number of replications `B=5`

, but this should be a much larger number in practice) :

```
set.seed(123)
validation.multi <- validate.subgroup(subgrp.multi,
B = 5, # specify the number of replications
method = "training_test_replication",
train.fraction = 0.5)
print(validation.multi, digits = 2, sample.pct = TRUE)
```

```
## family: gaussian
## loss: sq_loss_lasso
## method: weighting
##
## validation method: training_test_replication
## cutpoint: 0
## replications: 5
##
## benefit score: f_Trt_1(x): Trt_1 vs Trt_3, f_Trt_2(x): Trt_2 vs Trt_3
## f_Trt_3(x): 0
## maxval = max(f_Trt_1(x), f_Trt_2(x))
## which.max(maxval) = The trt level which maximizes maxval
## Trt recom = which.max(maxval)*I(maxval > c) + Trt_3*I(maxval <= c) where c is 'cutpoint'
##
## Average Test Set Outcomes:
## Recommended Trt_1 Recommended Trt_2
## Received Trt_1 2.52 (SE = 8.53, 9.28%) -9.69 (SE = 6.25, 19.36%)
## Received Trt_2 -6.79 (SE = 4.04, 11.84%) -8.91 (SE = 9.44, 16.64%)
## Received Trt_3 -30.87 (SE = 8.16, 9.28%) -27.23 (SE = 11.57, 17.6%)
## Recommended Trt_3
## Received Trt_1 -41.02 (SE = 17.46, 3.04%)
## Received Trt_2 -17.2 (SE = 8.75, 9.76%)
## Received Trt_3 -7.56 (SE = 2.69, 3.2%)
##
## Treatment effects conditional on subgroups:
## Est of E[Y|T=Trt_1,Recom=Trt_1]-E[Y|T=/=Trt_1,Recom=Trt_1]
## 21.66 (SE = 13.79, 30.4%)
## Est of E[Y|T=Trt_2,Recom=Trt_2]-E[Y|T=/=Trt_2,Recom=Trt_2]
## 9.87 (SE = 14.88, 53.6%)
## Est of E[Y|T=Trt_3,Recom=Trt_3]-E[Y|T=/=Trt_3,Recom=Trt_3]
## 14.72 (SE = 8.38, 16%)
##
## Est of
## E[Y|Trt received = Trt recom] - E[Y|Trt received =/= Trt recom]:
## 11.77 (SE = 9.73)
```

Setting the argument above to prints out the average percent of all patients which are in each subgroup (as opposed to the average sample sizes). We can see that about 58% of patients were recommended treatment 1 and among those recommended treatment 1, we expect them to have larger outcomes if they actually receive treatment 1 as opposed to the other treatments. The estimated effects are positive within all three subgroups (meaning those recommended each of the different treatments have a positive benefit from receiving the treatment they are recommended as opposed to receiving any another treatments).

We can visualize the subgroup-specific treatment effects using as usual with results shown in Figure :

For cases with multi-category treatments, the user must specify a propensity function that returns \(Pr(T = T_i | \mathbf X= \mathbf x)\) for patient \(i\). In other words, it should return the probability of receiving the treatment that was actually received for each patient. For example:

```
propensity.func.multinom <- function(x, trt)
{
df <- data.frame(trt = trt, x)
mfit <- nnet::multinom(trt ~ . -trt, data = df)
# predict returns a matrix of probabilities:
# one column for each treatment level
propens <- nnet::predict.nnet(mfit, type = "probs")
if (is.factor(trt))
{
values <- levels(trt)[trt]
} else
{
values <- trt
}
# return the probability corresponding to the
# treatment that was observed
probs <- propens[cbind(1:nrow(propens),
match(values, colnames(propens)))]
probs
}
```

Optionally the user can specify the function to return a matrix of treatment probabilities, however, the columns *must* be ordered by the levels of . An example of this is the following:

```
propensity.func.multinom <- function(x, trt)
{
require(nnet)
df <- data.frame(trt = trt, x)
mfit <- multinom(trt ~ . -trt, data = df)
# predict returns a matrix of probabilities:
# one column for each treatment level
propens <- predict(mfit, type = "probs")
if (is.factor(trt))
{
levels <- levels(trt)
} else
{
levels <- sort(unique(trt))
}
# return the probability corresponding to the
# treatment that was observed
probs <- propens[,match(levels, colnames(propens))]
probs
}
```