`vignettes/Dynamic_Predictions.Rmd`

`Dynamic_Predictions.Rmd`

Based on the general framework of joint models presented earlier, we are interested in deriving cumulative risk probabilities for a new subject \(j\) that has survived up to time point \(t\) and has provided longitudinal measurements \(\mathcal Y_{kj}(t) = \{ y_{kj}(t_{jl}); 0 \leq t_{jl} \leq t, l = 1, \ldots, n_j, k = 1, \ldots, K\}\), with \(K\) denoting the number of longitudinal outcomes. The probabilities of interest are \[\begin{array}{l} \pi_j(u \mid t) = \mbox{Pr}\{T_j^* \leq u \mid T_j^* > t, \mathcal Y_j(t), \mathcal D_n\}\\\\ = \displaystyle 1 - \int\int \frac{S(u \mid b_j, \theta)}{S(t \mid b_j, \theta)} \; p\{b_j \mid T_j^* > t, \mathcal Y_j(t), \theta\} \; p(\theta \mid \mathcal D_n) \; db_j d\theta, \end{array}\] where \(S(\cdot)\) denotes the survival function conditional on the random effects, and \(\mathcal Y_j(t) = \{\mathcal Y_{1j}(t), \ldots, \mathcal Y_{Kj}(t)\}\). Combining the three terms in the integrand we can device a Monte Carlo scheme to obtain estimates of these probabilities, namely,

Sample a value \(\tilde \theta\) from the posterior of the parameters \([\theta \mid \mathcal D_n]\).

Sample a value \(\tilde b_j\) from the posterior of the random effects \([b_j \mid T_j^* > t, \mathcal Y_j(t), \tilde \theta]\).

Compute the ratio of survival probabilities \(S(u \mid \tilde b_j, \tilde \theta) \Big / S(t \mid \tilde b_j, \tilde \theta)\).

Replicating these steps \(L\) times, we can estimate the conditional cumulative risk probabilities by \[1 - \frac{1}{L} \sum_{l=1}^L \frac{S(u \mid \tilde b_j^{(l)}, \tilde \theta^{(l)})}{S(t \mid \tilde b_j^{(l)}, \tilde \theta^{(l)})},\] and their standard error by calculating the standard deviation across the Monte Carlo samples.

We will illustrate the calculation of dynamic predictions using
package **JMbayes2** from a trivariate joint model fitted
to the PBC dataset for the longitudinal outcomes `serBilir`

(continuous), `prothrombin`

time (continuous) and
`ascites`

(dichotomous). We start by fitting the univariate
mixed models. For the two continuous outcomes, we allow for nonlinear
subject-specific time effects using natural cubic splines. For
`ascites`

, we postulate linear subject-specific profiles for
the log odds. The code is:

```
fm1 <- lme(log(serBilir) ~ ns(year, 3) * sex, data = pbc2,
random = ~ ns(year, 3) | id, control = lmeControl(opt = 'optim'))
fm2 <- lme(prothrombin ~ ns(year, 2) * sex, data = pbc2,
random = ~ ns(year, 2) | id, control = lmeControl(opt = 'optim'))
fm3 <- mixed_model(ascites ~ year * sex, data = pbc2,
random = ~ year | id, family = binomial())
```

Following, we fit the Cox model for the time to either
transplantation or death. The first line defines the composite event
indicator, and the second one fits the Cox model in which we have also
included the baseline covariates `drug`

and `age`

.
The code is:

```
pbc2.id$event <- as.numeric(pbc2.id$status != "alive")
CoxFit <- coxph(Surv(years, event) ~ drug + age, data = pbc2.id)
```

The joint model is fitted with the following call to
`jm()`

:

We want to calculate predictions for the longitudinal and survival
outcomes for Patients 25 and 93. As a first step, we extract the data of
these patients and store them in the data.frame `ND`

with the
code:

```
t0 <- 5
ND <- pbc2[pbc2$id %in% c(25, 93), ]
ND <- ND[ND$year < t0, ]
ND$status2 <- 0
ND$years <- t0
```

We will only use the first five years of follow-up (line three), and further we specify that the patients were event-free up to this time point (lines four and five).

We start with predictions for the longitudinal outcomes. These are
produced by the `predict()`

method for class `jm`

objects, and follow the same lines as the procedure described above for
cumulative risk probabilities. The only difference is in Step 3, where
instead of calculating the cumulative risk we calculate the predicted
values for the longitudinal outcomes. There are two options controlled
by the `type_pred`

argument, namely predictions at the scale
of the response/outcome (default) or at the linear predictor level. The
`type`

argument controls if the predictions will be for the
mean subject (i.e., including only the fixed effects) or
subject-specific including both the fixed and random effects. In the
`newdata`

argument we provide the available measurements of
the two patients. This will be used to sample their random effects at
Step 2 presented above. This is done with a Metropolis-Hastings
algorithm that runs for `n_mcmc`

iterations; all iterations
but the last one are discarded as burn-in. Finally, argument
`n_samples`

corresponds to the value of \(L\) defined above and specifies the number
of Monte Carlo samples:

`predLong1 <- predict(jointFit, newdata = ND, return_newdata = TRUE)`

Argument `return_newdata`

specifies that the predictions
are returned as extra columns of the `newdata`

data.frame. By
default the 95% credible intervals are also included. Using the
`plot()`

method for objects returned by
`predict.jm(..., return_newdata = TRUE)`

, we can display the
predictions. With the following code we do that for the first
longitudinal outcome:

`plot(predLong1)`

When we want to calculate predictions for other, future time points,
we can accordingly specify the `times`

argument. In the
following example, we calculate predictions from time `t0`

to
time 12:

```
predLong2 <- predict(jointFit, newdata = ND,
times = seq(t0, 12, length.out = 51),
return_newdata = TRUE)
```

We show these predictions for the second outcome and the second
patient (i.e., Patient 93). This is achieved by suitably specifying the
`outcomes`

and `subject`

arguments of the
`plot()`

method:

`plot(predLong2, outcomes = 2, subject = 93)`

We continue with the predictions for the event outcome. To let
`predict()`

know that we want the cumulative risk
probabilities, we specify `process = "event"`

:

```
predSurv <- predict(jointFit, newdata = ND, process = "event",
times = seq(t0, 12, length.out = 51),
return_newdata = TRUE)
```

The predictions are included again as extra columns in the
corresponding data.frame. To depict the predictions of both the
longitudinal and survival outcomes combined, we provide both objects to
the `plot()`

method:

`plot(predLong2, predSurv)`

Again by default, the plot is for the predictions of the first
subject (i.e., Patient 25) and for the first longitudinal outcome (i.e.,
`log(serBilir)`

). However, the `plot()`

method has
a series of arguments that allows users to customize the plot. We
illustrate some of these capabilities with the following figure. First,
we specify that we want to depict all three outcomes using
`outcomes = 1:3`

(note: a max of three outcomes can be
simultaneously displayed). Next, we specify via the `subject`

argument that we want to show the predictions of Patient 93. Note, that
for serum bilirubin we used the log transformation in the specification
of the linear mixed model. Hence, we receive predictions on the
transformed scale. To show predictions on the original scale, we use the
`fun_long`

argument. Because we have three outcomes, this
needs to be a list of three functions. The first one, corresponding to
serum bilirubin is the `exp()`

and for the other two the
`identity()`

because we do not wish to transform the
predictions. Analogously, we also have the `fun_event`

argument to transform the predictions for the event outcome, and in the
example below we set that we want to obtain survival probabilities.
Using the arguments `bg`

, `col_points`

,
`col_line_long`

, `col_line_event`

,
`fill_CI_long`

, and `fill_CI_event`

we have
changed the appearance of the plot to a dark theme. Finally, the
`pos_ylab_long`

specifies the relative positive of the y-axis
labels for the three longitudinal outcomes.

```
cols <- c('#F25C78', '#D973B5', '#F28322')
plot(predLong2, predSurv, outcomes = 1:3, subject = 93,
fun_long = list(exp, identity, identity),
fun_event = function (x) 1 - x,
ylab_event = "Survival Probabilities",
ylab_long = c("Serum Bilirubin", "Prothrombin", "Ascites"),
bg = '#132743', col_points = cols, col_line_long = cols,
col_line_event = '#F7F7FF', col_axis = "white",
fill_CI_long = c("#F25C7880", "#D973B580", "#F2832280"),
fill_CI_event = "#F7F7FF80",
pos_ylab_long = c(1.9, 1.9, 0.08))
```

We evaluate the discriminative capability of the model using ROC
methodology. We calculate the components of the ROC curve using
information up to year five, and we are interested in events occurring
within a three-year window. That is discriminating between patients who
will get the event in the interval `(t0, t0 + Dt]`

, (i.e., in
our case \(T_j \in (5, 8]\)) from
patients who will survive at least 8 years (i.e., \(T_j > 8\)). The calculations are
performed with the following call to `tvROC()`

:

```
pbc2$event <- as.numeric(pbc2$status != "alive")
roc <- tvROC(jointFit, newdata = pbc2, Tstart = t0, Dt = 3)
roc
#>
#> Time-dependent Sensitivity and Specificity for the Joint Model jointFit
#>
#> At time: 8
#> Using information up to time: 5 (202 subjects still at risk)
#>
#> cut-off SN SP qSN qSP
#> 1 0.06 0.04195420 1.00000000 0.03237374 1.000000000
#> 2 0.08 0.06293130 1.00000000 0.04880463 1.000000000
#> 3 0.09 0.07418742 0.99699727 0.05548414 0.848361904
#> 4 0.10 0.11614162 0.99699727 0.08908473 0.898907936
#> 5 0.11 0.13711872 0.99699727 0.10614349 0.913349659
#> 6 0.14 0.13711872 0.99051760 0.10153599 0.760569411
#> 7 0.15 0.15809582 0.99051760 0.11883604 0.787172809
#> 8 0.16 0.17907292 0.98403794 0.13179439 0.706878450
#> 9 0.18 0.22102711 0.98403794 0.16744697 0.751974073
#> 10 0.19 0.23738168 0.98261007 0.18059095 0.749088147
#> 11 0.22 0.23738168 0.97613040 0.17620908 0.678556115
#> 12 0.24 0.25835878 0.97613040 0.19456168 0.698646358
#> 13 0.27 0.30031297 0.97613040 0.23186533 0.732130096
#> 14 0.28 0.30031297 0.96965074 0.22766787 0.677339442
#> 15 0.29 0.32129007 0.96965074 0.24670657 0.693472470
#> 16 0.31 0.34226717 0.96965074 0.26595563 0.708069019
#> 17 0.33 0.36324427 0.96317107 0.28142650 0.676545959
#> 18 0.36 0.38422137 0.96317107 0.30119504 0.690023211
#> 19 0.37 0.40275043 0.95593523 0.31452038 0.657650618
#> 20 0.40 0.40275043 0.94945556 0.31060335 0.621852731
#> 21 0.42 0.41367496 0.94635040 0.31932381 0.612956479
#> 22 0.43 0.41451785 0.94013110 0.31637344 0.582982137
#> 23 0.44 0.42018880 0.93540315 0.31905894 0.565047853
#> 24 0.45 0.43004991 0.93196950 0.32672563 0.556704515
#> 25 0.46 0.45102701 0.93196950 0.34769092 0.570557499
#> 26 0.47 0.47200411 0.93196950 0.36890432 0.583570908
#> 27 0.48 0.51395831 0.93196950 0.41209328 0.607366856
#> 28 0.54 0.53493541 0.93196950 0.43407803 0.618273333
#> 29 0.56 0.55591251 0.92548984 0.45301419 0.603919675
#> 30 0.60 0.57688961 0.92548984 0.47565460 0.614075581
#> 31 0.61 0.58367640 0.92110656 0.48088045 0.601588151
#> 32 0.62 0.60465350 0.92110656 0.50397520 0.611305513 *
#> 33 0.63 0.60465350 0.90814723 0.49773590 0.568505613
#> 34 0.64 0.60465350 0.90166757 0.49455701 0.548564751
#> 35 0.66 0.60838661 0.89634103 0.49614074 0.534686419
#> 36 0.67 0.62936371 0.89634103 0.52007353 0.544801931
#> 37 0.68 0.62936371 0.88986137 0.51697723 0.526638223
#> 38 0.69 0.62936371 0.88338170 0.51384071 0.509231337
#> 39 0.70 0.63297262 0.86505747 0.50900973 0.465521754
#> 40 0.72 0.63728450 0.84695038 0.50494236 0.427481066
#> 41 0.73 0.63728450 0.84047072 0.50157462 0.414092459
#> 42 0.74 0.65826160 0.83399105 0.52392305 0.411687595
#> 43 0.75 0.65826160 0.82751139 0.52061696 0.399263803
#> 44 0.76 0.65826160 0.81455206 0.51386509 0.375658599
#> 45 0.77 0.67923870 0.80807239 0.53718726 0.374687477
#> 46 0.78 0.68335404 0.80286393 0.53983825 0.367912912
#> 47 0.79 0.69188112 0.77957922 0.53896286 0.335447783
#> 48 0.80 0.71488489 0.77372558 0.56696804 0.337573425
#> 49 0.81 0.72513943 0.74449480 0.56623566 0.302539854
#> 50 0.82 0.72513943 0.73801513 0.56282019 0.294387424
#> 51 0.83 0.72963483 0.72644440 0.56308989 0.282360632
#> 52 0.84 0.75364393 0.71442163 0.59209896 0.278914614
#> 53 0.85 0.79723147 0.69548716 0.64992099 0.276334193
#> 54 0.86 0.80338074 0.65202897 0.63893554 0.235976649
#> 55 0.87 0.80766247 0.62743290 0.63346999 0.216056732
#> 56 0.88 0.85577735 0.60341723 0.70867024 0.214610590
#> 57 0.89 0.85704309 0.59732854 0.70831014 0.210294809
#> 58 0.90 0.86105345 0.55320966 0.69492170 0.179530459
#> 59 0.91 0.86105345 0.52729100 0.68105450 0.162392817
#> 60 0.92 0.86342477 0.47618616 0.65514755 0.132701674
#> 61 0.93 0.88627370 0.45732536 0.69772746 0.129997795
#> 62 0.94 0.88859968 0.39972685 0.66413635 0.101813510
#> 63 0.95 0.91134042 0.34195464 0.68580290 0.083274742
#> 64 0.96 0.93288701 0.26437457 0.69189036 0.059516857
#> 65 0.97 0.93565341 0.13563579 0.45841619 0.019092303
#> 66 0.98 1.00000000 0.02591866 1.00000000 0.006240249
```

In the first line we define the event indicator as we did in the
`pbc2.id`

data.frame. The cut-point with the asterisk on the
right maximizes the Youden’s
index. To depict the ROC curve, we use the corresponding
`plot()`

method:

The area under the ROC curve is calculated with the
`tvAUC()`

function:

```
tvAUC(roc)
#>
#> Time-dependent AUC for the Joint Model jointFit
#>
#> Estimated AUC: 0.8078
#> At time: 8
#> Using information up to time: 5 (202 subjects still at risk)
```

This function either accepts an object of class `tvROC`

or
of class `jm`

. In the latter case, the user must also provide
the `newdata`

, `Tstart`

and `Dt`

or
`Thoriz`

arguments. Here we have used the same dataset as the
one to fit the model, but, in principle, discrimination could be
(better) assessed in another dataset.

To assess the accuracy of the predictions we produce a calibration plot:

`calibration_plot(jointFit, newdata = pbc2, Tstart = t0, Dt = 3)`

The syntax of the `calibration_plot()`

function is almost
identical to that of `tvROC()`

. The kernel density estimation
is of the estimated probabilities \(\pi_j(t +
\Delta t \mid t) = \pi_j(8 \mid 5)\) for all individuals at risk
at year `t0`

in the data frame provided in the
`newdata`

argument. Using the
`calibration_metrics()`

function we can also calculate
metrics for the accuracy of predictions:

```
calibration_metrics(jointFit, pbc2, Tstart = 5, Dt = 3)
#> ICI E50 E90
#> 0.05118405 0.04263956 0.09949474
```

The ICI is the mean absolute difference between the observed and
predicted probabilities, E50 is the median absolute difference, and E90
is the 90% percentile of the absolute differences. Finally, we calculate
the Brier score as an overall measure of predictive performance. This is
computed with the `tvBrier()`

function:

```
tvBrier(jointFit, newdata = pbc2, Tstart = t0, Dt = 3)
#>
#> Prediction Error for the Joint Model 'jointFit'
#>
#> Estimated Brier score: 0.1281
#> At time: 8
#> For the 202 subjects at risk at time 5
#> Number of subjects with an event in [5, 8): 40
#> Number of subjects with a censored time in [5, 8): 58
#> Accounting for censoring using model-based weights
```

The Brier score evaluates the predictive accuracy at time
`Tstart + Dt`

. To summarize the predictive accuracy in the
interval `(t0, t0 + Dt]`

we can use the integrated Brier
score. The corresponding integral is approximated using the Simpson’s
rule:

```
tvBrier(jointFit, newdata = pbc2, Tstart = t0, Dt = 3, integrated = TRUE)
#>
#> Prediction Error for the Joint Model 'jointFit'
#>
#> Estimated Integrated Brier score: 0.0825
#> In the time interval: [5, 8)
#> For the 202 subjects at risk at time 5
#> Number of subjects with an event in [5, 8): 40
#> Number of subjects with a censored time in [5, 8): 58
#> Accounting for censoring using model-based weights
```

The `tvBrier()`

also implements inverse probability of
censoring weights to account for censoring in the interval
`(t0, t0 + Dt]`

using the Kaplan-Meier estimate of the
censoring distribution (however, see the note below):

```
tvBrier(jointFit, newdata = pbc2, Tstart = t0, Dt = 3, integrated = TRUE,
type_weights = "IPCW")
#>
#> Prediction Error for the Joint Model 'jointFit'
#>
#> Estimated Integrated Brier score: 0.0831
#> In the time interval: [5, 8)
#> For the 202 subjects at risk at time 5
#> Number of subjects with an event in [5, 8): 40
#> Number of subjects with a censored time in [5, 8): 58
#> Accounting for censoring using inverse probability of censoring Kaplan-Meier weights
```

**Notes:**

- To obtain valid estimates of the predictive accuracy measures (i.e.,
time-varying sensitivity, specificity, and Brier score) we need to
account for censoring. A popular method to achieve this is via inverse
probability of censoring weighting. For this approach to be valid, we
need the model for the weights to be correctly specified. In standard
survival analysis, this is achieved either using the Kaplan-Meier
estimator or a Cox model for the censoring distribution. However, in the
settings where joint models are used, it is often the case that the
censoring mechanism may depend on the history of the longitudinal
outcomes in a complex manner. This is especially the case when we
consider multiple longitudinal outcomes in the analysis. Also, these
outcomes may be recorded at different time points per patient and have
missing data. Because of these reasons, in these settings,
Kaplan-Meier-based or Cox-based censoring weights may be difficult to
derive or be biased. The functions in
**JMbayes2**that calculate the predictive accuracy measures use joint-model-based weights to account for censoring. These weights allow censoring to depend in any possible manner on the history of the longitudinal outcomes. However, they require that the model is appropriately calibrated. - The calibration curve, produced by
`calibration_plot()`

, and the calibration metrics, produced by`calibration_metrics())`

, are calculated using the procedure described in Austin et al., 2020.