hputter / mstate

https://hputter.github.io/mstate/
7 stars 5 forks source link

Problem with mssample in a clock reset approach with history and output "data" #8

Open MichaelStefan opened 3 years ago

MichaelStefan commented 3 years ago

Dear Dr. Putter,

I have been using the mstate package for years and am very glad for this great package. However, recently I had a problem when generating the output „data“ with the mssample function in a setting with a „clock reset“ approach and using history. To illustrate my problem, here is some code using a simple illness death model:

Haz <- data.frame(time = rep(seq(0, 9.9, 0.1), 3), Haz = rep(seq(0, 0.99, 0.01), 3), trans = rep(c(1,2,3), each = 100)) trans <- matrix(c(NA, 1, 2, NA, NA, 3, NA, NA, NA), ncol = 3, byrow = T) alt <- mstate::mssample(Haz = Haz, trans = trans, history = list(time = 2, state = 1), clock = "reset", M = 100, output = "data", do.trace = 100) table(alt$Tstop[alt$trans==1]) table(alt$duration[alt$trans==1])

It seems that the minimum simulated duration in the first state is the history time. As far as I understand the history setting, a history state 1 and time 2 means that the patient is still in state 1 at 2 time units after entering this state. Thus, it seems to me that there is no reason, why a patient should not enter another state earlier than time 4.

To tackle this problem, I had a look into the code and think I located the problem in the function mssample1. I replaced the line tcond <- t0 <- Tstart <- history$time by the lines tcond <- 0 t0 <- Tstart <- history$time and think this solved my problem. However, I am still not sure, if I understood anything completely wrong and have of course not tested, how this adjustment will influence other settings.

Thanks in advance and best regards, Michael

moedancer commented 2 years ago

Hello,

just today I encountered the very same problem as @MichaelStefan. My understanding of what information the history should contain (in the case clock="reset") is the same, i.e. the variable time in the history denotes the amount of time an individual has spent in the corresponding state so far. However, I fear that the proposal of @MichaelStefan might affect the behaviour of the function for clock="forward" if a history is specified. From my point of view one should rather replace the line

tcond <- Tstop <- crs$t + tcond

by

tcond <- Tstop <- crs$t + Tstop or just Tstop <- crs$t +Tstop

and set "Tstop <- 0" before entering the while-loop. The variable "tcond" is not needed in the clock="reset" case and the preceding line "crs <- crsample(Haz[whh, ], t0, censtime)" already ensures that the jump time is bigger than the time supplied in the "history argument". This line is only executed if clock="reset" is chosen. This change causes the same changes in the output as @MichaelStefan 's suggestion and does not affect the behaviour if clock="forward" is chosen.

However, I'm not completely sure if my understanding of how the function "mssample1" is supposed to behave in case a histroy is specified is right.

Thanks in advance and best regards, Moritz

hputter commented 2 years ago

Thanks for bringing this up. I will have a look at it for the next version of mstate.