Partial dependence (PD) shows the expected prediction from a model as a function of a single predictor or multiple predictors. The expectation is marginalized over the values of all other predictors, giving something like a multivariable adjusted estimate of the model’s prediction.
Begin by fitting an ORSF ensemble. Set a prediction horizon of 5
years when we fit the ensemble so that any aorsf function
that we pass this ensemble to will assume we want to compute predictions
at 5 years.
library(aorsf)
pred_horizon <- 365.25 * 5
set.seed(329730)
index_train <- sample(nrow(pbc_orsf), 150)
pbc_orsf_train <- pbc_orsf[index_train, ]
pbc_orsf_test <- pbc_orsf[-index_train, ]
fit <- orsf(data = pbc_orsf_train,
formula = Surv(time, status) ~ . - id,
oobag_pred_horizon = pred_horizon)
fit
#> ---------- Oblique random survival forest
#>
#> Linear combinations: Accelerated
#> N observations: 150
#> N events: 52
#> N trees: 500
#> N predictors total: 17
#> N predictors per node: 5
#> Average leaves per tree: 12
#> Min observations in leaf: 5
#> Min events in leaf: 1
#> OOB stat value: 0.83
#> OOB stat type: Harrell's C-statistic
#> Variable importance: anova
#>
#> -----------------------------------------You can compute PD three ways with aorsf:
using in-bag predictions for the training data
pd_inb <- orsf_pd_inb(fit, pred_spec = list(bili = 1:5))
pd_inb
#> pred_horizon bili mean lwr medn upr
#> 1: 1826.25 -0.452406674 0.2061119 0.01444274 0.09357446 0.8053158
#> 2: 1826.25 -0.224302469 0.2348236 0.02656555 0.12452704 0.8206148
#> 3: 1826.25 0.003801737 0.2749933 0.04342625 0.17565052 0.8406553
#> 4: 1826.25 0.231905942 0.3299184 0.09191382 0.24263728 0.8544871
#> 5: 1826.25 0.460010148 0.3837488 0.15153571 0.30165878 0.8663482using out-of-bag predictions for the training data
pd_oob <- orsf_pd_oob(fit, pred_spec = list(bili = 1:5))
pd_oob
#> pred_horizon bili mean lwr medn upr
#> 1: 1826.25 -0.452406674 0.2072237 0.01383381 0.08975103 0.7998756
#> 2: 1826.25 -0.224302469 0.2348821 0.02603647 0.12916899 0.8152149
#> 3: 1826.25 0.003801737 0.2746627 0.04234095 0.18723298 0.8371582
#> 4: 1826.25 0.231905942 0.3298811 0.08621202 0.24652942 0.8441472
#> 5: 1826.25 0.460010148 0.3843112 0.14684189 0.29917249 0.8562432using predictions for a new set of data
pd_test <- orsf_pd_new(fit,
new_data = pbc_orsf_test,
pred_spec = list(bili = 1:5))
pd_test
#> pred_horizon bili mean lwr medn upr
#> 1: 1826.25 -0.452406674 0.2540722 0.01564154 0.1912170 0.8103449
#> 2: 1826.25 -0.224302469 0.2823693 0.03037250 0.2304118 0.8413602
#> 3: 1826.25 0.003801737 0.3204294 0.04941981 0.2735839 0.8495418
#> 4: 1826.25 0.231905942 0.3742045 0.10428307 0.3494399 0.8619464
#> 5: 1826.25 0.460010148 0.4258066 0.16681425 0.4032790 0.8626002in-bag PD indicates relationships that the model has learned during training. This is helpful if your goal is to interpret the model.
out-of-bag PD indicates relationships that the model has learned during training but using the out-of-bag data simulates application of the model to new data. if you want to test your model’s reliability or fairness in new data but you don’t have access to a large testing set.
new data PD shows how the model predicts outcomes for observations it has not seen. This is helpful if you want to test your model’s reliability or fairness.
Let’s re-fit our ORSF to all available data before proceeding to the next sections.
set.seed(329730)
fit <- orsf(pbc_orsf,
Surv(time, status) ~ . -id,
oobag_pred_horizon = pred_horizon)Computing PD for a single variable is straightforward:
pd_sex <- orsf_pd_oob(fit, pred_spec = list(sex = c("m", "f")))
pd_sex
#> pred_horizon sex mean lwr medn upr
#> 1: 1826.25 m 0.3525690 0.03974647 0.2414356 0.9439140
#> 2: 1826.25 f 0.2927645 0.01115776 0.1417599 0.9591495The output shows that the expected predicted mortality risk for men is substantially higher than women at 5 years after baseline.
What if the effect of a predictor varies over time? PD can show this.
pd_sex_tv <- orsf_pd_oob(fit, pred_spec = list(sex = c("m", "f")),
pred_horizon = seq(365, 365*5))
ggplot(pd_sex_tv, aes(x = pred_horizon, y = mean, color = sex)) +
geom_line() +
labs(x = 'Time since baseline',
y = 'Expected risk')From inspection, we can see that males have higher risk than females and the difference in that risk grows over time. This can also be seen by viewing the ratio of expected risk over time:
library(data.table)
ratio_tv <- pd_sex_tv[
, .(ratio = mean[sex == 'm'] / mean[sex == 'f']), by = pred_horizon
]
ggplot(ratio_tv, aes(x = pred_horizon, y = ratio)) +
geom_line(color = 'grey') +
geom_smooth(color = 'black', se = FALSE) +
labs(x = 'time since baseline',
y = 'ratio in expected risk for males versus females')
#> `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'If you want to compute PD marginally for multiple variables, just
list the variable values in pred_spec and specify
expand_grid = FALSE.
pd_two_vars <-
orsf_pd_oob(fit,
pred_spec = list(sex = c("m", "f"), bili = 1:5),
expand_grid = FALSE)
pd_two_vars
#> pred_horizon variable value level mean lwr medn
#> 1: 1826.25 sex NA m 0.3525690 0.03974647 0.2414356
#> 2: 1826.25 sex NA f 0.2927645 0.01115776 0.1417599
#> 3: 1826.25 bili -0.50720676 <NA> 0.2338008 0.01296442 0.1171454
#> 4: 1826.25 bili -0.28986618 <NA> 0.2846721 0.03823950 0.1718471
#> 5: 1826.25 bili -0.07252561 <NA> 0.3427673 0.06659531 0.2543880
#> 6: 1826.25 bili 0.14481497 <NA> 0.3941397 0.10249873 0.3127158
#> 7: 1826.25 bili 0.36215555 <NA> 0.4405501 0.14114665 0.3826310
#> upr
#> 1: 0.9439140
#> 2: 0.9591495
#> 3: 0.8702835
#> 4: 0.9065770
#> 5: 0.9238422
#> 6: 0.9380414
#> 7: 0.9464794Now would it be tedious if you wanted to do this for all the variables? You bet. That’s why we made a function for that. As a bonus, the printed output is sorted from most to least important variables.
pd_smry <- orsf_summarize_uni(fit)
pd_smry
#>
#> -- bili (VI Rank: 1) -------------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> -0.55 0.2287969 0.1148601 0.04327406 0.3528169
#> -0.42 0.2492619 0.1362958 0.05627886 0.3771831
#> 0.05 0.3712178 0.2894392 0.15670680 0.5570160
#>
#> -- age (VI Rank: 2) --------------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> -0.79 0.2700824 0.1295873 0.04026421 0.4519136
#> -0.01 0.2981617 0.1625962 0.04439149 0.5203701
#> 0.65 0.3278651 0.2116316 0.06299766 0.5706579
#>
#> -- copper (VI Rank: 3) -----------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> -0.66 0.2604858 0.1357381 0.04000928 0.4757550
#> -0.30 0.2787113 0.1495856 0.05226547 0.5071585
#> 0.33 0.3334915 0.2176733 0.09961880 0.5442257
#>
#> -- protime (VI Rank: 4) ----------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> -0.73 0.2805566 0.1480830 0.04426790 0.5250508
#> -0.13 0.2939581 0.1494827 0.04729709 0.5394144
#> 0.46 0.3170366 0.1848578 0.06474437 0.5605459
#>
#> -- albumin (VI Rank: 5) ----------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> -0.51 0.3136264 0.1689580 0.04677649 0.5812663
#> 0.07 0.2919870 0.1431321 0.04390161 0.5324684
#> 0.64 0.2784801 0.1319210 0.04469055 0.4855513
#>
#> -- chol (VI Rank: 6) -------------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> -0.52 0.2847845 0.1438956 0.03801347 0.4928676
#> -0.26 0.2923852 0.1602519 0.04444628 0.5187961
#> 0.13 0.3157720 0.1882467 0.07259899 0.5447747
#>
#> -- ascites (VI Rank: 7) ----------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 0 0.2930612 0.1454747 0.04353648 0.542582
#> 1 0.4693988 0.3779503 0.27346004 0.649705
#>
#> -- sex (VI Rank: 8) --------------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> m 0.3525690 0.2414356 0.10938168 0.5906043
#> f 0.2927645 0.1417599 0.04241247 0.5316942
#>
#> -- ast (VI Rank: 9) --------------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> -0.74 0.2828259 0.1388635 0.04124336 0.5293067
#> -0.13 0.2966559 0.1541120 0.04724622 0.5467764
#> 0.52 0.3172462 0.1715826 0.06196440 0.5828348
#>
#> -- stage (VI Rank: 10) -----------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 1 0.6509735 0.5960104 0.5219782 0.7876622
#> 2 0.6509735 0.5960104 0.5219782 0.7876622
#> 3 0.6509735 0.5960104 0.5219782 0.7876622
#> 4 0.6509735 0.5960104 0.5219782 0.7876622
#>
#> -- spiders (VI Rank: 11) ---------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 0 0.2896234 0.1442807 0.04234382 0.5274333
#> 1 0.3346422 0.2164031 0.07881800 0.5616628
#>
#> -- edema (VI Rank: 12) -----------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 0 0.2896246 0.1454747 0.04216106 0.5435162
#> 0.5 0.3504477 0.2437012 0.09271219 0.6102717
#> 1 0.4373430 0.3381648 0.22289789 0.6563385
#>
#> -- alk.phos (VI Rank: 13) --------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> -0.51 0.3016681 0.1553594 0.04465530 0.5536559
#> -0.34 0.3024376 0.1630816 0.04488732 0.5534591
#> 0.04 0.3063790 0.1665361 0.05124448 0.5511105
#>
#> -- platelet (VI Rank: 14) --------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> -0.66 0.3055666 0.1594746 0.04869850 0.5749957
#> -0.05 0.3005557 0.1541210 0.04566221 0.5625947
#> 0.61 0.2974122 0.1524793 0.04545151 0.5504124
#>
#> -- hepato (VI Rank: 15) ----------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 0 0.2850006 0.1445350 0.03921781 0.5230005
#> 1 0.3179494 0.1777718 0.06326351 0.5512348
#>
#> -- trig (VI Rank: 16) ------------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> -0.61 0.2936587 0.1437749 0.04129360 0.5354742
#> -0.26 0.3001726 0.1559818 0.04400752 0.5448964
#> 0.41 0.3130174 0.1782903 0.05648899 0.5421498
#>
#> -- trt (VI Rank: 17) -------------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> d_penicill_main 0.3052962 0.1681729 0.04896262 0.5487566
#> placebo 0.2994143 0.1451760 0.04452267 0.5498884
#>
#> Predicted risk at time t = 1826.25 for top 17 predictorsIt’s easy enough to turn this ‘summary’ object into a
data.table for downstream plotting and tables.
head(as.data.table(pd_smry))
#> variable importance Value Mean Median 25th % 75th %
#> 1: bili 0.09397791 -0.55 0.2287969 0.1148601 0.04327406 0.3528169
#> 2: bili 0.09397791 -0.42 0.2492619 0.1362958 0.05627886 0.3771831
#> 3: bili 0.09397791 0.05 0.3712178 0.2894392 0.15670680 0.5570160
#> 4: age 0.02318191 -0.79 0.2700824 0.1295873 0.04026421 0.4519136
#> 5: age 0.02318191 -0.01 0.2981617 0.1625962 0.04439149 0.5203701
#> 6: age 0.02318191 0.65 0.3278651 0.2116316 0.06299766 0.5706579
#> pred_horizon level
#> 1: 1826.25 <NA>
#> 2: 1826.25 <NA>
#> 3: 1826.25 <NA>
#> 4: 1826.25 <NA>
#> 5: 1826.25 <NA>
#> 6: 1826.25 <NA>PD can show the expected value of a model’s predictions as a function
of a specific predictor, or as a function of multiple predictors. For
instance, we can estimate predicted risk as a joint function of
bili, edema, and trt:
pred_spec = list(bili = seq(1, 5, length.out = 20),
edema = levels(pbc_orsf_train$edema),
trt = levels(pbc_orsf$trt))
pd_bili_edema <- orsf_pd_oob(fit, pred_spec)
library(ggplot2)
ggplot(pd_bili_edema, aes(x = bili, y = medn, col = trt, linetype = edema)) +
geom_line() +
labs(y = 'Expected predicted risk')From inspection,
the model’s predictions indicate slightly lower risk for the
placebo group, and these do not seem to change much at different values
of bili or edema.
There is a clear increase in predicted risk with higher levels of
edema and with higher levels of bili
the slope of predicted risk as a function of bili
appears highest among patients with edema of 0.5. Is the
effect of bili modified by edema being 0.5? A
quick sanity check with coxph suggests there is.
library(survival)
pbc_orsf$edema_05 <- ifelse(pbc_orsf$edema == '0.5', 'yes', 'no')
fit_cph <- coxph(Surv(time,status) ~ edema_05 * bili,
data = pbc_orsf)
anova(fit_cph)
#> Analysis of Deviance Table
#> Cox model: response is Surv(time, status)
#> Terms added sequentially (first to last)
#>
#> loglik Chisq Df Pr(>|Chi|)
#> NULL -550.19
#> edema_05 -546.83 6.7248 1 0.009508 **
#> bili -513.59 66.4689 1 3.555e-16 ***
#> edema_05:bili -510.54 6.1112 1 0.013433 *
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1Unlike partial dependence, which shows the expected prediction as a function of one or multiple predictors, individual conditional expectations (ICE) show the prediction for an individual observation as a function of a predictor.
Just like PD, we can compute ICE using in-bag, out-of-bag, or testing data, and the same principles apply. We’ll use out-of-bag estimates here.
Inspecting the ICE curves for each observation can help identify whether there is heterogeneity in a model’s predictions. I.e., does the effect of the variable follow the same pattern for all the data, or are there groups where the variable impacts risk differently?
I am going to turn off boundary checking in orsf_ice_oob
by setting boundary_checks = FALSE, and this will allow me
to generate ICE curves that go beyond the 90th percentile of
bili.
pred_spec <- list(bili = seq(1, 10, length.out = 25))
ice_oob <- orsf_ice_oob(fit, pred_spec, boundary_checks = FALSE)
ice_oob
#> pred_horizon id_variable id_row bili pred
#> 1: 1826.25 1 1 -0.5072068 0.91930989
#> 2: 1826.25 1 2 -0.5072068 0.10293802
#> 3: 1826.25 1 3 -0.5072068 0.68484802
#> 4: 1826.25 1 4 -0.5072068 0.31309355
#> 5: 1826.25 1 5 -0.5072068 0.07395545
#> ---
#> 6896: 1826.25 25 272 1.4488584 0.45183506
#> 6897: 1826.25 25 273 1.4488584 0.46096744
#> 6898: 1826.25 25 274 1.4488584 0.48277410
#> 6899: 1826.25 25 275 1.4488584 0.38452767
#> 6900: 1826.25 25 276 1.4488584 0.51664794id_variable is an identifier for the current value
of the variable(s) that are in the data. It is redundant if you only
have one variable, but helpful if there are multiple variables.
id_row is an identifier for the observation in the
original data. It is used to group an observation’s predictions together
in plots.
For plots, it is helpful to scale the ICE data. I subtract the
initial value of predicted risk (i.e., when bili = 1) from
each observation’s conditional expectation values. So,
Every curve start at 0
The plot shows change in predicted risk as a function of
bili.
ice_oob[, pred_subtract := rep(pred[id_variable==1], times=25)]
ice_oob[, pred := pred - pred_subtract]Now we can visualize the curves.
library(ggplot2)
ggplot(ice_oob, aes(x = bili,
y = pred,
group = id_row)) +
geom_line(alpha = 0.15) +
labs(y = 'Change in predicted risk') +
geom_smooth(se = FALSE, aes(group = 1))
#> `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'From inspection of the figure,
Most of the individual slopes cluster around the overall trend - Good!
A small number of individual slopes appear to be flat. It may be helpful to investigate this further.
Partial dependence has a number of known limitations and assumptions that users should be aware of (see Hooker, 2021). In particular, partial dependence is less intuitive when >2 predictors are examined jointly, and it is assumed that the feature(s) for which the partial dependence is computed are not correlated with other features (this is likely not true in many cases). Accumulated local effect plots can be used (see here) in the case where feature independence is not a valid assumption.