Examples

Stuart Lacy

2019-02-03

library(multistateutils)

Introduction

This vignette aims to provide a more thorough introduction to the features in multistateutils than the brief overview in the README. It uses the same example dataset and models, but has more examples and accompanying discussion.

Example data

This guide assumes familiarity with multi-state modelling in R, this section in particular glosses over the details and just prepares models and data in order to demonstrate the features of multistateutils. If you are unfamiliar with multi-state modelling then I would recommend reading de Wreede, Fiocco, and Putter (2011) or the mstate tutorial by Putter.

For these examples the ebmt3 data set from mstate will be used. This provides a simple illness-death model of patients following transplant. The initial state is patient having received transplantation, pr referring to platelet recovery (the ‘illness’), with relapse-free-survival (rfs) being the only sink state.

library(mstate)
#> Loading required package: survival
data(ebmt3)
head(ebmt3)
#>   id prtime prstat rfstime rfsstat dissub   age            drmatch    tcd
#> 1  1     23      1     744       0    CML   >40    Gender mismatch No TCD
#> 2  2     35      1     360       1    CML   >40 No gender mismatch No TCD
#> 3  3     26      1     135       1    CML   >40 No gender mismatch No TCD
#> 4  4     22      1     995       0    AML 20-40 No gender mismatch No TCD
#> 5  5     29      1     422       1    AML 20-40 No gender mismatch No TCD
#> 6  6     38      1     119       1    ALL   >40 No gender mismatch No TCD

mstate provides a host of utility functions for working with multi-state models. For example, the trans.illdeath() function provides the required transition matrix for this state structure (transMat should be used when greater flexibility is required).

tmat <- trans.illdeath(c('transplant', 'pr', 'rfs'))
tmat
#>             to
#> from         transplant pr rfs
#>   transplant         NA  1   2
#>   pr                 NA NA   3
#>   rfs                NA NA  NA

The final data preparation step is to form the data from a wide format (each row corresponding to a patient) to a long format, where each row represents a potential patient-transition. The msprep function from mstate handles this for us. We’ll keep both the age and dissub covariates in this example.

long <- msprep(time=c(NA, 'prtime', 'rfstime'), 
               status=c(NA, 'prstat', 'rfsstat'), 
               data=ebmt3, 
               trans=tmat, 
               keep=c('age', 'dissub'))
head(long)
#> An object of class 'msdata'
#> 
#> Data:
#>   id from to trans Tstart Tstop time status age dissub
#> 1  1    1  2     1      0    23   23      1 >40    CML
#> 2  1    1  3     2      0    23   23      0 >40    CML
#> 3  1    2  3     3     23   744  721      0 >40    CML
#> 4  2    1  2     1      0    35   35      1 >40    CML
#> 5  2    1  3     2      0    35   35      0 >40    CML
#> 6  2    2  3     3     35   360  325      1 >40    CML

Clock-reset Weibull models will be fitted to these 3 transitions, which are semi-Markov models. Simulation is therefore needed to obtain transition probabilities as the Kolmogorov forward differential equation is no longer valid with the violation of the Markov assumption. We are going to assume that the baseline hazard isn’t proportional between transitions and there are no shared transition effects for simplicity’s sake.

library(flexsurv)
models <- lapply(1:3, function(i) {
    flexsurvreg(Surv(time, status) ~ age + dissub, data=long, dist='weibull')
})

Estimating transition probabilities

Transition probabilities are defined as the probability of being in a state \(j\) at a time \(t\), given being in state \(h\) at time \(s\), as shown below where \(X(t)\) gives the state an individual is in at \(t\). This is all conditional on the individual parameterised by their covariates and history, which for this semi-Markov model only influences transition probabilities through state arrival times.

\[P_{h,j}(s, t) = \Pr(X(t) = j\ |\ X(s) = h)\]

We’ll estimate the transition probabilities of an individual with the covariates age=20-40 and dissub=AML at 1 year after transplant.

newdata <- data.frame(age="20-40", dissub="AML")

The function that estimates transition probabilities is called predict_transitions and has a very similar interface to flexsurv::pmatrix.simfs. The parameters in the above equation have the following argument names:

The code example below shows how to calculate transition probabilities for \(t=365\) (1 year) with \(s=0\); the transition probabilities for every state at 1 year after transplant given being in every state at transplant time. As with pmatrix.simfs, although all the probabilities for every pairwise combination of states are calculated, they are sometimes redundant. For example, \(P_{h,j}(0, 365)\) where \(h=j=\text{rfs}\) is hardly a useful prediction.

predict_transitions(models, newdata, tmat, times=365)
#>     age dissub start_time end_time start_state transplant        pr
#> 1 20-40    AML          0      365  transplant  0.4689727 0.1960009
#> 2 20-40    AML          0      365          pr  0.0000000 0.6860803
#> 3 20-40    AML          0      365         rfs  0.0000000 0.0000000
#>         rfs
#> 1 0.3350264
#> 2 0.3139197
#> 3 1.0000000

Note that this gives very similar responses to pmatrix.simfs.

pmatrix.simfs(models, tmat, newdata=newdata, t=365)
#>         [,1]    [,2]    [,3]
#> [1,] 0.47134 0.19118 0.33748
#> [2,] 0.00000 0.68671 0.31329
#> [3,] 0.00000 0.00000 1.00000

Confidence intervals can be constructed in the same fashion as pmatrix.simfs, using draws from the multivariate-normal distribution of the parameter estimates.

predict_transitions(models, newdata, tmat, times=365, ci=TRUE, M=10)
#>     age dissub start_time end_time start_state transplant_est    pr_est
#> 1 20-40    AML          0      365  transplant      0.4694436 0.1946468
#> 2 20-40    AML          0      365          pr      0.0000000 0.6889348
#> 3 20-40    AML          0      365         rfs      0.0000000 0.0000000
#>     rfs_est transplant_2.5%   pr_2.5%  rfs_2.5% transplant_97.5%  pr_97.5%
#> 1 0.3359096       0.4573342 0.1770981 0.3199084        0.4822301 0.2038494
#> 2 0.3110652       0.0000000 0.6706948 0.2923926        0.0000000 0.7076074
#> 3 1.0000000       0.0000000 0.0000000 1.0000000        0.0000000 0.0000000
#>   rfs_97.5%
#> 1 0.3535072
#> 2 0.3293052
#> 3 1.0000000

Which gives rather different results to those obtained from pmatrix.simfs which seem to be too wide and the estimate value is far different to that obtained when run without CIs. I’m unsure why this is the case.

pmatrix.simfs(models, tmat, newdata=newdata, t=365, ci=TRUE, M=9)
#>           [,1]      [,2]      [,3]
#> [1,] 0.4444444 0.1111111 0.4444444
#> [2,] 0.0000000 0.7777778 0.2222222
#> [3,] 0.0000000 0.0000000 1.0000000
#> attr(,"lower")
#>           [,1]      [,2] [,3]
#> [1,] 0.2194444 0.0000000    0
#> [2,] 0.0000000 0.3333333    0
#> [3,] 0.0000000 0.0000000    1
#> attr(,"upper")
#>           [,1]      [,2]      [,3]
#> [1,] 0.7777778 0.4444444 0.6666667
#> [2,] 0.0000000 1.0000000 0.6666667
#> [3,] 0.0000000 0.0000000 1.0000000
#> attr(,"class")
#> [1] "fs.msm.est"

Note that on a single individual the speed-up isn’t present, with multistateutils taking 4 times longer than flexsurv, although the difference between 1.2s and 0.3s isn’t that noticeable in interactive work. The main benefit comes when estimating more involved probabilities, as will be demonstrated next.

library(microbenchmark)
microbenchmark("multistateutils"=predict_transitions(models, newdata, tmat, times=365),
               "flexsurv"=pmatrix.simfs(models, tmat, newdata=newdata, t=365), times=10)
#> Unit: milliseconds
#>             expr       min        lq      mean    median        uq
#>  multistateutils 1557.9465 1571.9696 1712.5789 1692.9687 1786.3170
#>         flexsurv  247.1591  257.8414  298.3142  286.4939  320.8084
#>        max neval cld
#>  2026.1781    10   b
#>   434.6882    10  a

Estimating probabilities at multiple times

Frequently, it is desirable to estimate transition probabilities at multiple values of \(t\), in order to build up a picture of an individual’s disease progression. pmatrix.simfs only allows a scalar for \(t\), so estimating probabilities at multiple values requires manually iterating through the time-scale. In the example below we will calculate transition probabilities at yearly intervals for 9 years.

predict_transitions(models, newdata, tmat, times=seq(9)*365)
#>      age dissub start_time end_time start_state transplant        pr
#> 1  20-40    AML          0      365  transplant  0.4699190 0.1929019
#> 2  20-40    AML          0      365          pr  0.0000000 0.6845554
#> 3  20-40    AML          0      365         rfs  0.0000000 0.0000000
#> 4  20-40    AML          0      730  transplant  0.3535870 0.2067148
#> 5  20-40    AML          0      730          pr  0.0000000 0.5951183
#> 6  20-40    AML          0      730         rfs  0.0000000 0.0000000
#> 7  20-40    AML          0     1095  transplant  0.2846022 0.2090303
#> 8  20-40    AML          0     1095          pr  0.0000000 0.5358009
#> 9  20-40    AML          0     1095         rfs  0.0000000 0.0000000
#> 10 20-40    AML          0     1460  transplant  0.2382730 0.2051978
#> 11 20-40    AML          0     1460          pr  0.0000000 0.4907316
#> 12 20-40    AML          0     1460         rfs  0.0000000 0.0000000
#> 13 20-40    AML          0     1825  transplant  0.2043994 0.1994890
#> 14 20-40    AML          0     1825          pr  0.0000000 0.4544598
#> 15 20-40    AML          0     1825         rfs  0.0000000 0.0000000
#> 16 20-40    AML          0     2190  transplant  0.1774322 0.1943790
#> 17 20-40    AML          0     2190          pr  0.0000000 0.4237590
#> 18 20-40    AML          0     2190         rfs  0.0000000 0.0000000
#> 19 20-40    AML          0     2555  transplant  0.1565132 0.1875923
#> 20 20-40    AML          0     2555          pr  0.0000000 0.3985892
#> 21 20-40    AML          0     2555         rfs  0.0000000 0.0000000
#> 22 20-40    AML          0     2920  transplant  0.1395664 0.1813845
#> 23 20-40    AML          0     2920          pr  0.0000000 0.3763652
#> 24 20-40    AML          0     2920         rfs  0.0000000 0.0000000
#> 25 20-40    AML          0     3285  transplant  0.1244361 0.1759551
#> 26 20-40    AML          0     3285          pr  0.0000000 0.3565460
#> 27 20-40    AML          0     3285         rfs  0.0000000 0.0000000
#>          rfs
#> 1  0.3371791
#> 2  0.3154446
#> 3  1.0000000
#> 4  0.4396982
#> 5  0.4048817
#> 6  1.0000000
#> 7  0.5063675
#> 8  0.4641991
#> 9  1.0000000
#> 10 0.5565292
#> 11 0.5092684
#> 12 1.0000000
#> 13 0.5961116
#> 14 0.5455402
#> 15 1.0000000
#> 16 0.6281888
#> 17 0.5762410
#> 18 1.0000000
#> 19 0.6558944
#> 20 0.6014108
#> 21 1.0000000
#> 22 0.6790491
#> 23 0.6236348
#> 24 1.0000000
#> 25 0.6996088
#> 26 0.6434540
#> 27 1.0000000

In pmatrix.simfs it is up to the user to manipulate the output to make it interpretable. Again, the probabilities agree with each other.

By removing this boilerplate code, the speed increase starts to show, with the calculation of 8 additional time-points only increasing the runtime by 61% from 1.2s to 2s, while flexsurv has a twelve-fold increase from 0.3s to 3.7s.

Changing start time

pmatrix.simfs limits the user to using \(s=0\). In predict_transitions this is fully customisable. For example, the call below shows estimates the 1-year transition probabilities conditioned on the individual being alive at 6 months (technically it also calculates the transition probabilities conditioned on being dead at 6 months in the third row, but these aren’t helpful). Notice how the probabilities of being dead at 1 year have decreased as a result.

Multiple values of \(s\) can be provided, such as the quarterly predictions below.

Finally, any combination of number of \(s\) and \(t\) can be specified provided that all \(s\) are less than \(min(t)\).

Note that obtaining these additional probabilities does not increase the runtime of the function.

Multiple individuals

It’s useful to be able to estimating transition probabilities for multiple individuals at once, for example to see how the outcomes differ for patients with different characteristics. predict_transitions simply handles multiple rows supplied to newdata.

As with multiple times, pmatrix.simfs only handles a single individual at a time.

And the user has to manually iterate through each new individual they would like to estimate transition probabilities for.

Time-dependent covariates

The Markov assumption has already been violated by the use of a clock-reset time-scale, which is why we are using simulation in the first place. We can therefore add an other violation without it affecting our methodology. Owing to the use of clock-reset, the model does not take time-since-transplant into account for patients who have platelet recovery. This could be an important prognostic factor in that individual’s survival. Similar scenarios are common in multi-state modelling, and are termed state-arrival times. We’ll make a new set of models, where the transition from pr to rfs (transition 3) takes time-since-transplant into account. This information is already held in the Tstart variable produced by msprep.

Looking at the coefficient for this variable and it does seem to be prognostic for time-to-rfs.

To estimate transition probabilities for models with state-arrival times, the variables needs to be included in newdata with an initial value, i.e. the value this variable has when the global clock is 0.

Then in predict_transitions simply specify which variables in newdata are time-dependent, that is they increment at each transition along with the current clock value. This is particularly useful for modelling patient age at each state entry, rather than at the starting state. Notice how this slightly changes the probability of being in rfs from a person starting in transplant compared to the example below that omits the tcovs argument.

This functionality is implemented in pmatrix.simfs, but the tcovs argument actually has no impact on the transition probabilities, as evidenced below.

Mixture of distributions

Sometimes greater flexibility in the model structure is required, so that every transition isn’t obliged to use the same distribution. This could be useful if any transitions have few observations and would benefit from a simpler model such as an exponential, or if there is a requirement to use existing models from literature. Furthermore, if prediction is the goal, then it could be the case that allowing different distributions for each transition offers better overall fit.

An example is shown below, where each transition uses a different distribution family.

predict_transitions handles these cases with no problems; currently the following distributions are supported:

pmatrix.simfs does not seem to function correctly under these situations.

Length of stay

Similarly, the length of stay functionality provided by totlos.simfs has also been extended to allow for estimates at multiple time-points, states, and individuals to be calculated at the same time. As shown below, the function parameters are very similar and the estimates are very close to those produced by totlos.simf.

length_of_stay(models, 
               newdata=newdata,
               tmat, times=365.25*3,
               start_state='transplant')
#>     age dissub       t start_state transplant       pr      rfs
#> 1 20-40    AML 1095.75  transplant   485.8422 208.6771 401.2306
totlos.simfs(models, tmat, t=365.25*3, start=1, newdata=newdata)
#>        1        2        3 
#> 484.5698 204.9828 406.1974

Rather than provide a example for each argument like in the previous section, the code chunk below demonstrates that vectors can be provided to both times and start, and newdata accept a data frame with multiple rows.

length_of_stay(models, 
               newdata=data.frame(age=c(">40", ">40"),
                                  dissub=c('CML', 'AML')),
               tmat, times=c(1, 3, 5)*365.25,
               start_state=c('transplant', 'pr'))
#>    age dissub       t start_state transplant        pr       rfs
#> 1  >40    CML  365.25  transplant  104.35800  29.69458  48.57242
#> 2  >40    AML  365.25  transplant   96.76163  31.97558  53.88779
#> 3  >40    CML  365.25          pr         NA 136.89014  45.73486
#> 4  >40    AML  365.25          pr         NA 132.45761  50.16739
#> 5  >40    CML 1095.75  transplant  222.39434 103.71705 221.76361
#> 6  >40    AML 1095.75  transplant  197.74962 107.18295 242.94243
#> 7  >40    CML 1095.75          pr         NA 341.83419 206.04081
#> 8  >40    AML 1095.75          pr         NA 323.83676 224.03824
#> 9  >40    CML 1826.25  transplant  297.18278 175.16040 440.78182
#> 10 >40    AML 1826.25  transplant  258.79311 175.92531 478.40658
#> 11 >40    CML 1826.25          pr         NA 505.10349 408.02151
#> 12 >40    AML 1826.25          pr         NA 472.17028 440.95472

State flow diagram

Another feature in multistateutils is a visualization of a predicted pathway through the state transition model, calculated using dynamic prediction and provided in the function plot_predicted_pathway. It estimates state occupancy probabilities at discrete time-points and displays the flow between them in the manner of a Sankey diagram.

This visualization, an example of which is shown below for the 20-40 year old AML patient with biennial time-points, differs from traditional stacked line graph plots that only display estimates conditioned on a single time-point and starting state, i.e. a fixed \(s\) and \(h\) in the transition probability specification. plot_predicted_pathway instead displays dynamic predictions, where both \(s\) and \(h\) are allowed to vary and are updated at each time-point.

Note that the image below is actually an HTML widget and therefore interactive - try moving the states around. In the future I might try and implement a default optimal layout, along with explicitly displaying the time-scale.

\[P_{h,j}(s, t) = \Pr(X(t) = j\ |\ X(s) = h)\]

time_points <- seq(0, 10, by=2) * 365.25
plot_predicted_pathway(models, tmat, newdata, time_points, 1)