stulacy / multistateutils

Utility funtions for parametric multi-state modelling in R
GNU General Public License v3.0
6 stars 2 forks source link

Issues with transitions when the order of states is not right and when using start states #9

Open sdaza opened 4 years ago

sdaza commented 4 years ago

Hello,

I have some questions on how the function msprep2 create transitions. Here a toy example:

tmat <- trans.illdeath()
censors = data.frame(id = c(1, 3, 4), censor_time = c(200, 370, 390))
entry <- data.frame(id=c(2, 2, 2, 3),
    state=c(2, 1, 3, 2),
    time=c(35, 36, 360, 10))
start_states = data.frame(id = c(1, 2, 3, 4), start_state = c(1, 1, 2, 1))
a = msprep2(entry, tmat, censors, start_states = start_states)

The output in this case of id = 2 seems weird:

In the case of id = 3, the function creates a transition from state 2 to state 3, although the start state is 2, and there is no actual transition to state 3. Id = 3 should censor at time 370, not to move from state 2 to 3 at time 10.

   id from to trans Tstart Tstop time status
1   1    1  2     1      0   200  200      0
2   1    1  3     2      0   200  200      0
3   2    1  2     1      0    35   35      1
4   2    1  3     2      0    35   35      0
5   2    2  3     3     35    36    1      0
6   2    1  2     1     36   360  324      0
7   2    1  3     2     36   360  324      1
8   3    2  3     3      0    10   10      0
9   4    1  2     1      0   390  390      0
10  4    1  3     2      0   390  390      0

Any ideas? Thanks for your great package!

stulacy commented 4 years ago

Sorry it's taken me so long to look at this, I had the wrong email address for notifications!

The transition matrix is incompatible with some of the transitions you've specified, namely allowing id 2 to move from state 2 back to 1, and allowing id 3 to have a recurrent transition to state 2. Admittedly the code should probably flag up when this is the case, but you can modify the transition matrix to allow this behaviour:

tmat_2 <- matrix(NA, nrow=3, ncol=3)
tmat_2[1, 2] <- 1
tmat_2[1, 3] <- 2
tmat_2[2, 1] <- 3
tmat_2[2, 2] <- 4
tmat_2[2, 3] <- 5
tmat_2

     [,1] [,2] [,3]
[1,]   NA    1    2
[2,]    3    4    5
[3,]   NA   NA   NA

Running msprep with this transition matrix produces a nearly correct output, the only issue is row 10 with the transition from state 3 to NA. I'll have a look into why this is the case - I haven't used recurrent transition matrices before.

msprep2(entry, tmat_2, censors, start_states = start_states)
An object of class 'msdata'

Data:
   id from to trans Tstart Tstop time status
1   1    1  2     1      0   200  200      0
2   1    1  3     2      0   200  200      0
3   2    1  2     1      0    35   35      1
4   2    1  3     2      0    35   35      0
5   2    2  1     3     35    36    1      1
6   2    2  2     4     35    36    1      0
7   2    2  3     5     35    36    1      0
8   2    1  2     1     36   360  324      0
9   2    1  3     2     36   360  324      1
10  2    3 NA    NA    360    NA   NA      0
11  3    2  1     3      0    10   10      0
12  3    2  2     4      0    10   10      1
13  3    2  3     5      0    10   10      0
14  3    2  1     3     10   370  360      0
15  3    2  2     4     10   370  360      0
16  3    2  3     5     10   370  360      0
17  4    1  2     1      0   390  390      0
18  4    1  3     2      0   390  390      0