knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)

Package *survxai* contains functions for creating a unified representation of a survival models. Such representations can be further processed by various survival explainers. Tools implemented in *survxai* help to understand how input variables are used in the model and what impact do they have on final model prediction.

The analyses carried out using this package can be divided into two parts: local analyses of new observations and global analyses showing the structures of survival models. This vignette describes local explanations.

Methods and functions in *survxai* package are based on *DALEX* package.

In our use case we will use the data from the Mayo Clinic trial in primary biliary cirrhosis (PBC) of the liver conducted between 1974 and 1984. A total of 424 PBC patients, referred to Mayo Clinic during that ten-year interval, met eligibility criteria for the randomized placebo controlled trial of the drug D-penicillamine. The
first 312 cases in the data set participated in the randomized trial and contain largely complete data.
The `pbc`

data is included in the *randomForestSRC* package.

data(pbc, package = "randomForestSRC") pbc <- pbc[complete.cases(pbc),] head(pbc)

Our original data set contains only the numerical variables.
For this usecase we convert variables `sex`

and `stage`

to factor variables.

pbc$sex <- as.factor(pbc$sex) pbc$stage <- as.factor(pbc$stage)

We will create Cox proportional hazards model based on five variables from our data set: `age`

, `treatment`

, `status`

, `sex`

and `bili`

.

set.seed(1024) library(rms) library(survxai) pbc_smaller <- pbc[,c("days", "status", "treatment", "sex", "age", "bili", "stage")] head(pbc_smaller) cph_model <- cph(Surv(days/365, status) ~ treatment + sex + age + bili + stage , data = pbc_smaller, surv = TRUE, x = TRUE, y=TRUE)

In this section we will focus on the local explanations - the explanations for chosen new observations.

First, we have to create survival explainers - objects to wrap-up the black-box model with meta-data. Explainers unify model interfacing.

We have to define custom predict function which takes three arguments: model, data and vector with time points. Predict funcions may vary depending on the model. Examples for some models are in Explainations of different survival models vignette.

predict_times <- function(model, data, times){ prob <- rms::survest(model, data, times = times)$surv return(prob) } surve_cph <- explain(model = cph_model, data = pbc_smaller[,-c(1,2)], y = Surv(pbc_smaller$days/365, pbc_smaller$status), predict_function = predict_times) print(surve_cph)

Ceteris Paribus Plots (What-If Plots) are designed to present model responses around a single point in the feature space. For more details for generalised models of machine learning see: https://github.com/pbiecek/ceterisParibus.

Ceteris Paribus Plots for survival models are survival curves around one observation. Each curve represent observation with different value of chosen variable. For factor variables curves covers all possible values, for numeric variables values are divided into quantiles.

Ceteris Paribus Plot illustrates how will the survival curve change along with the changing variable.

Below, we plot Ceteris Paribus for one observation.

single_observation <- pbc_smaller[1,-c(1,2)] single_observation

cp_cph <- ceteris_paribus(surve_cph, single_observation) print(cp_cph)

After creating the `surv_ceteris_paribus`

object we can visualize it in a very convinient way using the generic `plot()`

function. Black line represent prediction of original observation.

plot(cp_cph, scale_type = "gradient", scale_col = c("red", "blue"), ncol = 2)

We can see that there are differences for stages. Next, we will plot Ceteris Paribus for sigle variable `stage`

.

plot(cp_cph, selected_variable = "stage", scale_type = "gradient", scale_col = c("red", "blue"))

We see a trend that a lower `stage`

means a higher probability of survival for chosen observation.

Break Down Plot presents variable contributions in final predictions. For more details for generalised models of machine learning see: https://github.com/pbiecek/breakDown.

Break Down Plots for survival models compare differences in predictions for median of time.

broken_prediction_cph <- prediction_breakdown(surve_cph, pbc_smaller[1,-c(1,2)]) print(broken_prediction_cph)

After creating the `surv_prediction_breakdown`

object we can visualize it in a very convinient way using the generic `plot()`

function.

plot(broken_prediction_cph, scale_col = c("red", "blue"))

This plot helps to understand the factors that drive survival probability for a single observation.

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

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.