R/mstate.R
sim.fmsm.Rd
Simulate changes of state and transition times from a semi-Markov
multi-state model fitted using flexsurvreg
.
sim.fmsm( x, trans = NULL, t, newdata = NULL, start = 1, M = 10, tvar = "trans", tcovs = NULL, tidy = FALSE, debug = FALSE )
x | A model fitted with Alternatively |
---|---|
trans | Matrix indicating allowed transitions. See
|
t | Time, or vector of times for each of the |
newdata | A data frame specifying the values of covariates in the
fitted model, other than the transition number. See
|
start | Starting state, or vector of starting states for each of the
|
M | Number of individual trajectories to simulate. |
tvar | Variable in the data representing the transition type. Not
required if |
tcovs | Names of "predictable" time-dependent covariates in
|
tidy | If |
debug | Print intermediate outputs: for development use. |
If tidy=TRUE
, a data frame with one row for each simulated transition, giving the individual ID id
, start state start
, end state end
, transition label trans
, time of the transition since the start of the process (time
), and time since the previous transition (delay
).
If tidy=FALSE
, a list of two matrices named st
and t
. The rows of
each matrix represent simulated individuals. The columns of t
contain the times when the individual changes state, to the corresponding
states in st
.
The first columns will always contain the starting states and the starting
times. The last column of t
represents either the time when the
individual moves to an absorbing state, or right-censoring in a transient
state at the time given in the t
argument to sim.fmsm
.
sim.fmsm
relies on the presence of a function to sample random
numbers from the parametric survival distribution used in the fitted model
x
, for example rweibull
for Weibull models. If
x
was fitted using a custom distribution, called dist
say,
then there must be a function called (something like) rdist
either
in the working environment, or supplied through the dfns
argument to
flexsurvreg
. This must be in the same format as standard R
functions such as rweibull
, with first argument n
, and
remaining arguments giving the parameters of the distribution. It must be
vectorised with respect to the parameter arguments.
This function is only valid for semi-Markov ("clock-reset") models, though no warning or error is currently given if the model is not of this type. An equivalent for time-inhomogeneous Markov ("clock-forward") models has currently not been implemented.
Note the random sampling method for flexsurvspline
models is
currently very inefficient, so that looping over the M
individuals
will be very slow.
Christopher Jackson chris.jackson@mrc-bsu.cam.ac.uk.
bexp <- flexsurvreg(Surv(years, status) ~ trans, data=bosms3, dist="exp") tmat <- rbind(c(NA,1,2),c(NA,NA,3),c(NA,NA,NA)) sim.fmsm(bexp, M=10, t=5, trans=tmat)#> $st #> [,1] [,2] [,3] #> [1,] 1 2 3 #> [2,] 1 2 3 #> [3,] 1 3 3 #> [4,] 1 2 3 #> [5,] 1 1 1 #> [6,] 1 1 1 #> [7,] 1 2 2 #> [8,] 1 2 2 #> [9,] 1 1 1 #> [10,] 1 1 1 #> #> $t #> [,1] [,2] [,3] #> [1,] 0 1.4232209 1.5839374 #> [2,] 0 2.8293349 3.2697722 #> [3,] 0 0.4690095 0.4690095 #> [4,] 0 1.1345671 2.7151326 #> [5,] 0 5.0000000 5.0000000 #> [6,] 0 5.0000000 5.0000000 #> [7,] 0 3.5105227 5.0000000 #> [8,] 0 0.8213143 5.0000000 #> [9,] 0 5.0000000 5.0000000 #> [10,] 0 5.0000000 5.0000000 #> #> attr(,"trans") #> [,1] [,2] [,3] #> [1,] NA 1 2 #> [2,] NA NA 3 #> [3,] NA NA NA