Partial dependence (PD)
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
#>
#> -----------------------------------------
Three ways to compute PD
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 1 0.2058219 0.01599363 0.09351003 0.8077278 #> 2: 1826.25 2 0.2373355 0.02549869 0.12767905 0.8227315 #> 3: 1826.25 3 0.2812921 0.05046720 0.17286132 0.8457834 #> 4: 1826.25 4 0.3432185 0.09778988 0.25558695 0.8575243 #> 5: 1826.25 5 0.3996903 0.16412752 0.32619731 0.8634269
-
using 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 1 0.2072748 0.01479443 0.08852908 0.8053317 #> 2: 1826.25 2 0.2381647 0.02469718 0.12623031 0.8258154 #> 3: 1826.25 3 0.2815486 0.04242975 0.18952701 0.8484846 #> 4: 1826.25 4 0.3421912 0.09076851 0.24968438 0.8611884 #> 5: 1826.25 5 0.3984215 0.16098228 0.32147532 0.8554402
-
using 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 1 0.2510900 0.01631318 0.1872414 0.8162621 #> 2: 1826.25 2 0.2807327 0.02903956 0.2269297 0.8332956 #> 3: 1826.25 3 0.3247386 0.05860235 0.2841853 0.8481825 #> 4: 1826.25 4 0.3850799 0.10741224 0.3405760 0.8588955 #> 5: 1826.25 5 0.4394952 0.17572657 0.4050864 0.8657886
in-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.
One variable, one 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.3559202 0.03995954 0.2427792 0.9486137
#> 2: 1826.25 f 0.2961114 0.01132241 0.1447474 0.9626828
The output shows that the expected predicted mortality risk for men is substantially higher than women at 5 years after baseline.
One variable, moving horizon
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")'
Multiple variables, marginally
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 upr
#> 1: 1826.25 sex NA m 0.3559202 0.03995954 0.2427792 0.9486137
#> 2: 1826.25 sex NA f 0.2961114 0.01132241 0.1447474 0.9626828
#> 3: 1826.25 bili 1 <NA> 0.2346450 0.01251527 0.1212992 0.8733544
#> 4: 1826.25 bili 2 <NA> 0.2900929 0.04965949 0.1920790 0.9097945
#> 5: 1826.25 bili 3 <NA> 0.3481106 0.07248908 0.2557485 0.9213889
#> 6: 1826.25 bili 4 <NA> 0.3965248 0.09116718 0.3278895 0.9334875
#> 7: 1826.25 bili 5 <NA> 0.4421490 0.12358330 0.3921014 0.9375022
Now 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.80 0.2298980 0.1157904 0.04132253 0.3678720
#> 1.4 0.2513220 0.1351933 0.05504898 0.3887824
#> 3.5 0.3735709 0.2923891 0.16348936 0.5577971
#>
#> -- age (VI Rank: 2) --------------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 42 0.2736346 0.1338475 0.03915902 0.4605820
#> 50 0.3013697 0.1548903 0.04441111 0.5430635
#> 57 0.3329590 0.2082692 0.06778463 0.5781336
#>
#> -- copper (VI Rank: 3) -----------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 43 0.2665941 0.1280400 0.04283502 0.4648884
#> 74 0.2825327 0.1479470 0.05335137 0.4942437
#> 129 0.3330772 0.2113133 0.09796624 0.5333395
#>
#> -- protime (VI Rank: 4) ----------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 10 0.2824586 0.1474621 0.04428083 0.5111052
#> 11 0.2954482 0.1516848 0.04821692 0.5311810
#> 11 0.3181342 0.1843402 0.06312932 0.5486052
#>
#> -- ascites (VI Rank: 5) ----------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 0 0.2968722 0.1447474 0.04608949 0.5397718
#> 1 0.4687087 0.3841361 0.26174441 0.6480594
#>
#> -- stage (VI Rank: 6) ------------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 1 0.6583672 0.6070399 0.5189756 0.7865766
#> 2 0.6583672 0.6070399 0.5189756 0.7865766
#> 3 0.6583672 0.6070399 0.5189756 0.7865766
#> 4 0.6583672 0.6070399 0.5189756 0.7865766
#>
#> -- chol (VI Rank: 7) -------------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 250 0.2888222 0.1345402 0.03932400 0.5148838
#> 310 0.2963786 0.1468409 0.04355530 0.5328728
#> 401 0.3167786 0.1858660 0.07206203 0.5462129
#>
#> -- sex (VI Rank: 8) --------------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> m 0.3559202 0.2427792 0.11136359 0.5918001
#> f 0.2961114 0.1447474 0.04519829 0.5286689
#>
#> -- albumin (VI Rank: 9) ----------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 3.3 0.3179870 0.1792141 0.05618604 0.5867335
#> 3.5 0.2949167 0.1504756 0.04419944 0.5431911
#> 3.8 0.2796421 0.1442010 0.04322619 0.4983203
#>
#> -- spiders (VI Rank: 10) ---------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 0 0.2938741 0.1409483 0.04142812 0.5359432
#> 1 0.3340425 0.1980884 0.07789822 0.5684732
#>
#> -- edema (VI Rank: 11) -----------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 0 0.2924324 0.1412817 0.04519829 0.5371075
#> 0.5 0.3551633 0.2253930 0.09232238 0.6292695
#> 1 0.4483613 0.3452986 0.24076168 0.6666047
#>
#> -- ast (VI Rank: 12) -------------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 82 0.2865611 0.1445626 0.04299327 0.5266939
#> 117 0.3005119 0.1613674 0.04946382 0.5545645
#> 153 0.3200434 0.1727147 0.06368975 0.5874615
#>
#> -- platelet (VI Rank: 13) --------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 200 0.3092421 0.1609407 0.04686314 0.5939281
#> 257 0.3030627 0.1558075 0.04495160 0.5733261
#> 318 0.3000361 0.1472874 0.04767242 0.5563967
#>
#> -- hepato (VI Rank: 14) ----------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 0 0.2892663 0.1461239 0.0424868 0.5374039
#> 1 0.3194366 0.1847444 0.0611695 0.5538878
#>
#> -- trig (VI Rank: 15) ------------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 85 0.2962616 0.1432713 0.03963869 0.5358985
#> 108 0.3017943 0.1540859 0.04217322 0.5560627
#> 151 0.3138737 0.1796233 0.04994688 0.5590738
#>
#> -- trt (VI Rank: 16) -------------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> d_penicill_main 0.3092348 0.1775864 0.04966480 0.5487896
#> placebo 0.3016658 0.1552409 0.04556513 0.5621203
#>
#> -- alk.phos (VI Rank: 17) --------------------------------
#>
#> |---------------- risk ----------------|
#> Value Mean Median 25th % 75th %
#> 922 0.3013807 0.1433016 0.04407730 0.5567570
#> 1278 0.3036860 0.1476446 0.04521654 0.5569806
#> 2068 0.3088907 0.1540810 0.04858680 0.5654406
#>
#> Predicted risk at time t = 1826.25 for top 17 predictors
It’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.08991457 0.80 0.2298980 0.1157904 0.04132253 0.3678720
#> 2: bili 0.08991457 1.4 0.2513220 0.1351933 0.05504898 0.3887824
#> 3: bili 0.08991457 3.5 0.3735709 0.2923891 0.16348936 0.5577971
#> 4: age 0.02156699 42 0.2736346 0.1338475 0.03915902 0.4605820
#> 5: age 0.02156699 50 0.3013697 0.1548903 0.04441111 0.5430635
#> 6: age 0.02156699 57 0.3329590 0.2082692 0.06778463 0.5781336
#> 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>
Multiple variables, jointly
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
oredema
.There is a clear increase in predicted risk with higher levels of
edema
and with higher levels ofbili
-
the slope of predicted risk as a function of
bili
appears highest among patients withedema
of 0.5. Is the effect ofbili
modified byedema
being 0.5? A quick sanity check withcoxph
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 ' ' 1
Individual conditional expectations (ICE)
Unlike 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.
Visualizing ICE curves
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 1 0.9035317
#> 2: 1826.25 1 2 1 0.1012207
#> 3: 1826.25 1 3 1 0.6983324
#> 4: 1826.25 1 4 1 0.3368231
#> 5: 1826.25 1 5 1 0.1054981
#> ---
#> 6896: 1826.25 25 272 10 0.4456932
#> 6897: 1826.25 25 273 10 0.5718185
#> 6898: 1826.25 25 274 10 0.4973769
#> 6899: 1826.25 25 275 10 0.3913366
#> 6900: 1826.25 25 276 10 0.5321202
id_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.
Limitations of PD
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.
References
- Giles Hooker, Lucas Mentch, Siyu Zhou. Unrestricted Permutation forces Extrapolation: Variable Importance Requires at least One More Model, or There Is No Free Variable Importance. arXiv e-prints 2021 Oct; arXiv-1905. URL: https://doi.org/10.48550/arXiv.1905.03151