The longitudinal PBC dataset comes from the Mayo Clinic trial in primary biliary cirrhosis (PBC) of the liver conducted between 1974 and 1984 (Refer to https://stat.ethz.ch/R-manual/R-devel/library/survival/html/pbc.html)
In this notebook, we will apply Recurrent Deep Survival Machines for survival prediction on the PBC data.
The package includes helper functions to load the dataset.
X represents an np.array of features (covariates), T is the event/censoring times and, E is the censoring indicator.
from dsm import datasets
x, t, e = datasets.load_dataset('PBC', sequential = True)
Survival predictions are issued at certain time horizons. Here we will evaluate the performance of RDSM to issue predictions at the 25th, 50th and 75th event time quantile as is standard practice in Survival Analysis.
import numpy as np
horizons = [0.25, 0.5, 0.75]
times = np.quantile([t_[-1] for t_, e_ in zip(t, e) if e_[-1] == 1], horizons).tolist()
We will train RDSM on 70% of the Data, use a Validation set of 10% for Model Selection and report performance on the remaining 20% held out test set.
n = len(x)
tr_size = int(n*0.70)
vl_size = int(n*0.10)
te_size = int(n*0.20)
x_train, x_test, x_val = np.array(x[:tr_size], dtype = object), np.array(x[-te_size:], dtype = object), np.array(x[tr_size:tr_size+vl_size], dtype = object)
t_train, t_test, t_val = np.array(t[:tr_size], dtype = object), np.array(t[-te_size:], dtype = object), np.array(t[tr_size:tr_size+vl_size], dtype = object)
e_train, e_test, e_val = np.array(e[:tr_size], dtype = object), np.array(e[-te_size:], dtype = object), np.array(e[tr_size:tr_size+vl_size], dtype = object)
Lets set up the parameter grid to tune hyper-parameters. We will tune the number of underlying survival distributions, ($K$), the distribution choices (Log-Normal or Weibull), the learning rate for the Adam optimizer between $1\times10^{-3}$ and $1\times10^{-4}$, the number of hidden nodes per layer $50, 100$ and $2$, the number of layers $3, 2$ and $1$ and the type of recurrent cell (LSTM, GRU, RNN).
from sklearn.model_selection import ParameterGrid
param_grid = {'k' : [3, 4, 6],
'distribution' : ['LogNormal', 'Weibull'],
'learning_rate' : [1e-4, 1e-3],
'hidden': [50, 100],
'layers': [3, 2, 1],
'typ': ['LSTM', 'GRU', 'RNN'],
}
params = ParameterGrid(param_grid)
from dsm import DeepRecurrentSurvivalMachines
models = []
for param in params:
model = DeepRecurrentSurvivalMachines(k = param['k'],
distribution = param['distribution'],
hidden = param['hidden'],
typ = param['typ'],
layers = param['layers'])
# The fit method is called to train the model
model.fit(x_train, t_train, e_train, iters = 1, learning_rate = param['learning_rate'])
models.append([[model.compute_nll(x_val, t_val, e_val), model]])
best_model = min(models)
model = best_model[0][1]
out_risk = model.predict_risk(x_test, times)
out_survival = model.predict_survival(x_test, times)
We evaluate the performance of RDSM in its discriminative ability (Time Dependent Concordance Index and Cumulative Dynamic AUC) as well as Brier Score on the concatenated temporal data.
from sksurv.metrics import concordance_index_ipcw, brier_score, cumulative_dynamic_auc
cis = []
brs = []
et_train = np.array([(e_train[i][j], t_train[i][j]) for i in range(len(e_train)) for j in range(len(e_train[i]))],
dtype = [('e', bool), ('t', float)])
et_test = np.array([(e_test[i][j], t_test[i][j]) for i in range(len(e_test)) for j in range(len(e_test[i]))],
dtype = [('e', bool), ('t', float)])
et_val = np.array([(e_val[i][j], t_val[i][j]) for i in range(len(e_val)) for j in range(len(e_val[i]))],
dtype = [('e', bool), ('t', float)])
for i, _ in enumerate(times):
cis.append(concordance_index_ipcw(et_train, et_test, out_risk[:, i], times[i])[0])
brs.append(brier_score(et_train, et_test, out_survival, times)[1])
roc_auc = []
for i, _ in enumerate(times):
roc_auc.append(cumulative_dynamic_auc(et_train, et_test, out_risk[:, i], times[i])[0])
for horizon in enumerate(horizons):
print(f"For {horizon[1]} quantile,")
print("TD Concordance Index:", cis[horizon[0]])
print("Brier Score:", brs[0][horizon[0]])
print("ROC AUC ", roc_auc[horizon[0]][0], "\n")
For 0.25 quantile, TD Concordance Index: 0.5748031496062992 Brier Score: 0.0040254261016212795 ROC AUC 0.5770750988142292 For 0.5 quantile, TD Concordance Index: 0.8037750594183785 Brier Score: 0.012524285322743573 ROC AUC 0.8130810214146464 For 0.75 quantile, TD Concordance Index: 0.8507809756261016 Brier Score: 0.03105328491896606 ROC AUC 0.8674491502503145