PD and ICE curves with ORSF

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>", 
  fig.height = 5, 
  fig.width = 7
)

library(aorsf)
library(ggplot2)

Partial dependence (PD)

r aorsf:::roxy_pd_explain()

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,
            n_tree = 50,
            oobag_pred_horizon = pred_horizon)

fit

Three ways to compute PD

You can compute PD three ways with aorsf:

```r

pd_inb <- orsf_pd_inb(fit, pred_spec = list(bili = 1:5))

pd_inb

```

```r

pd_oob <- orsf_pd_oob(fit, pred_spec = list(bili = 1:5))

pd_oob

```

```r

pd_test <- orsf_pd_new(fit, new_data = pbc_orsf_test, pred_spec = list(bili = 1:5))

pd_test

```

Let's re-fit our ORSF model to all available data before proceeding to the next sections.

set.seed(329730)

fit <- orsf(pbc_orsf, 
            Surv(time, status) ~ . -id,
            n_tree = 50,
            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

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')

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

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, n_variables = 4)

pd_smry

It's easy enough to turn this 'summary' object into a data.table for downstream plotting and tables.

head(as.data.table(pd_smry))

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,

```r

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)

```

# in case pbc_orsf is used in downstream docs

pbc_orsf$edema_05 <- NULL

Individual conditional expectations (ICE)

r aorsf:::roxy_ice_explain()

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.

```r

pred_spec <- list(bili = seq(1, 10, length.out = 25))

ice_oob <- orsf_ice_oob(fit, pred_spec, boundary_checks = FALSE)

ice_oob

```

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,

```r

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))

From inspection of the figure,

Limitations of PD

r aorsf:::roxy_pd_limitations()

References

  1. r aorsf:::roxy_cite_hooker_2021()


Try the aorsf package in your browser

Any scripts or data that you put into this service are public.

aorsf documentation built on Oct. 26, 2023, 5:08 p.m.