Skip to contents

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.2063275 0.01614023 0.09290525 0.7979658
    #> 2:      1826.25    2 0.2375402 0.02862108 0.12464454 0.8155937
    #> 3:      1826.25    3 0.2812969 0.04800369 0.17593657 0.8412916
    #> 4:      1826.25    4 0.3449011 0.09743606 0.25601876 0.8622891
    #> 5:      1826.25    5 0.3989089 0.15958324 0.32376690 0.8648500
  • 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.2083893 0.01427269 0.09243306 0.8038430
    #> 2:      1826.25    2 0.2388692 0.02644723 0.13056024 0.8197223
    #> 3:      1826.25    3 0.2822520 0.04253775 0.18133861 0.8453980
    #> 4:      1826.25    4 0.3450353 0.10570653 0.24846072 0.8603192
    #> 5:      1826.25    5 0.3981654 0.16237025 0.31272727 0.8540956
  • 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.2513328 0.01623621 0.1858685 0.8145688
    #> 2:      1826.25    2 0.2803739 0.03013140 0.2235447 0.8332727
    #> 3:      1826.25    3 0.3238922 0.05772089 0.2783950 0.8504448
    #> 4:      1826.25    4 0.3866085 0.10958261 0.3385069 0.8605818
    #> 5:      1826.25    5 0.4397618 0.16796347 0.4008159 0.8674983
  • 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.


set.seed(329730)

fit <- orsf(pbc_orsf, 
            Surv(time, status) ~ . -id,
            oobag_pred_horizon = pred_horizon)

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.3509013 0.04240587 0.2369675 0.9493653
#> 2:      1826.25   f 0.2966130 0.01080458 0.1540995 0.9593203

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.3509013 0.04240587 0.2369675 0.9493653
#> 2:      1826.25      sex    NA     f 0.2966130 0.01080458 0.1540995 0.9593203
#> 3:      1826.25     bili     1  <NA> 0.2364411 0.01276889 0.1226318 0.8588095
#> 4:      1826.25     bili     2  <NA> 0.2879870 0.03463426 0.1744362 0.8964507
#> 5:      1826.25     bili     3  <NA> 0.3452410 0.06562730 0.2499412 0.9177820
#> 6:      1826.25     bili     4  <NA> 0.3944734 0.10209053 0.3249542 0.9268768
#> 7:      1826.25     bili     5  <NA> 0.4364234 0.13598722 0.3764861 0.9332170

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.2318238 0.1222346 0.04767251 0.3721230
#>              1.4 0.2512932 0.1286997 0.05732668 0.3954552
#>              3.5 0.3732977 0.2921434 0.15691393 0.5446383
#> 
#> -- age (VI Rank: 2) --------------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>               41 0.2717280 0.1340402 0.04444057 0.4543710
#>               50 0.2998749 0.1695391 0.04897776 0.5221107
#>               57 0.3332681 0.2128529 0.07162152 0.5696046
#> 
#> -- copper (VI Rank: 3) -----------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>               43 0.2646185 0.1435889 0.04709242 0.4511820
#>               74 0.2811100 0.1526745 0.05619746 0.4846292
#>              130 0.3313980 0.2198289 0.09784018 0.5423921
#> 
#> -- protime (VI Rank: 4) ----------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>               10 0.2796367 0.1420143 0.04801291 0.5044994
#>               11 0.2937497 0.1539896 0.05545320 0.5179771
#>               11 0.3188917 0.1881430 0.06758212 0.5342607
#> 
#> -- ascites (VI Rank: 5) ----------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>                0 0.2965245 0.1490175 0.05083227 0.5474558
#>                1 0.4584861 0.3770756 0.24915590 0.6516096
#> 
#> -- chol (VI Rank: 6) -------------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>              249 0.2870698 0.1418911 0.04548872 0.4908843
#>              310 0.2948621 0.1532925 0.05011397 0.4929993
#>              402 0.3163312 0.1961059 0.07315576 0.5350033
#> 
#> -- albumin (VI Rank: 7) ----------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>              3.3 0.3188572 0.1858425 0.05537154 0.5901556
#>              3.5 0.2945722 0.1499564 0.04595295 0.5445703
#>              3.8 0.2777114 0.1357634 0.04519930 0.5060992
#> 
#> -- sex (VI Rank: 8) --------------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>                m 0.3509013 0.2369675 0.10909710 0.5789001
#>                f 0.2966130 0.1540995 0.05011817 0.5516241
#> 
#> -- stage (VI Rank: 9) ------------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median    25th %    75th %
#>                1 0.6449143 0.6001311 0.5026708 0.7885147
#>                2 0.6449143 0.6001311 0.5026708 0.7885147
#>                3 0.6449143 0.6001311 0.5026708 0.7885147
#>                4 0.6449143 0.6001311 0.5026708 0.7885147
#> 
#> -- spiders (VI Rank: 10) ---------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>                0 0.2941722 0.1444627 0.04622040 0.5208339
#>                1 0.3334233 0.2045931 0.07858647 0.5665165
#> 
#> -- ast (VI Rank: 11) -------------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>               82 0.2843700 0.1507387 0.04641969 0.5286145
#>              117 0.2993330 0.1582764 0.05440855 0.5475059
#>              153 0.3209115 0.1778443 0.06729029 0.5745942
#> 
#> -- edema (VI Rank: 12) -----------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>                0 0.2925595 0.1486548 0.04930299 0.5364573
#>              0.5 0.3528335 0.2450341 0.09878942 0.5957164
#>                1 0.4325310 0.3307460 0.22094200 0.6548856
#> 
#> -- hepato (VI Rank: 13) ----------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>                0 0.2873715 0.1444627 0.04576175 0.5268890
#>                1 0.3196291 0.1849676 0.06426068 0.5723111
#> 
#> -- platelet (VI Rank: 14) --------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>              200 0.3090734 0.1566667 0.05759065 0.5799625
#>              257 0.3029126 0.1570879 0.05249609 0.5687906
#>              318 0.3002985 0.1521838 0.05108378 0.5517121
#> 
#> -- alk.phos (VI Rank: 15) --------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>              921 0.3035428 0.1616683 0.05100614 0.5612870
#>             1278 0.3050857 0.1585186 0.05282983 0.5702896
#>             2072 0.3090111 0.1623250 0.05774306 0.5704368
#> 
#> -- trig (VI Rank: 16) ------------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>               85 0.2947248 0.1489677 0.04590042 0.5277393
#>              108 0.3026501 0.1591111 0.04970202 0.5412681
#>              152 0.3154152 0.1774285 0.06075334 0.5424042
#> 
#> -- trt (VI Rank: 17) -------------------------------------
#> 
#>                  |---------------- risk ----------------|
#>            Value      Mean    Median     25th %    75th %
#>  d_penicill_main 0.3084188 0.1652732 0.05595308 0.5575708
#>          placebo 0.3010561 0.1473525 0.04659117 0.5629357
#> 
#>  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.08402792  0.80 0.2318238 0.1222346 0.04767251 0.3721230
#> 2:     bili 0.08402792   1.4 0.2512932 0.1286997 0.05732668 0.3954552
#> 3:     bili 0.08402792   3.5 0.3732977 0.2921434 0.15691393 0.5446383
#> 4:      age 0.02510940    41 0.2717280 0.1340402 0.04444057 0.4543710
#> 5:      age 0.02510940    50 0.2998749 0.1695391 0.04897776 0.5221107
#> 6:      age 0.02510940    57 0.3332681 0.2128529 0.07162152 0.5696046
#>    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 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 ' ' 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.92672772
#>    2:      1826.25           1      2    1 0.08895044
#>    3:      1826.25           1      3    1 0.72322656
#>    4:      1826.25           1      4    1 0.31692019
#>    5:      1826.25           1      5    1 0.07310180
#>   ---                                                
#> 6896:      1826.25          25    272   10 0.45199905
#> 6897:      1826.25          25    273   10 0.48878233
#> 6898:      1826.25          25    274   10 0.50423132
#> 6899:      1826.25          25    275   10 0.37512287
#> 6900:      1826.25          25    276   10 0.52377195
  • 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

  1. 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