simulate.dsmm {dsmmR} | R Documentation |
Simulate a sequence given a drifting semi-Markov kernel.
Description
Generic function that simulates a sequence under the rule of a
drifting semi-Markov kernel. The number of simulated states is nsim
,
while the kernel is retrieved from the object obj
via inheritance
from the S3 class dsmm
.
Usage
## S3 method for class 'dsmm'
simulate(
object,
nsim = NULL,
seed = NULL,
max_seq_length = NULL,
klim = 100,
...
)
Arguments
object |
An object of S3 class |
nsim |
Optional. An integer specifying the number of simulations to be made
from the drifting semi-Markov kernel. The maximum value of |
seed |
Optional. An integer specifying the initialization of the random number generator. |
max_seq_length |
Optional. A positive integer that will ensure the simulated
sequence will not have a maximum total length greater than
|
klim |
Optional. Positive integer. Passed down to |
... |
Optional. Attributes passed down from the |
Value
Returns the simulated sequence for the given drifting
semi-Markov model. It is a character vector based on nsim
simulations,
with a maximum length of max_seq_length
.
This sequence is not to be confused with the embedded Markov chain. The user
can apply the base::rle()
function on this simulated sequence, if he wishes
to obtain the corresponding embedded Markov chain and the sojourn times.
See Also
About random number generation in R: RNG
.
Fitting a model through a sequence from this function: fit_dsmm.
For the theoretical background of drifting semi-Markov models: dsmmR.
For obtaining the lengths and values of equals values in a vector:
rle
.
Examples
# Setup.
sequence <- create_sequence("DNA", len = 1000)
states <- sort(unique(sequence))
d <- 1
obj_model_3 <- fit_dsmm(sequence = sequence,
states = states,
degree = d,
f_is_drifting = TRUE,
p_is_drifting = FALSE)
# Using the method `simulate.dsmm()`.
simulated_seq <- simulate(obj_model_3, seed = 1)
short_sim <- simulate(obj = obj_model_3, nsim = 10, seed = 1)
cut_sim <- simulate(obj = obj_model_3, max_seq_length = 10, seed = 1)
str(simulated_seq)
str(short_sim)
str(cut_sim)
# To obtain the embedded Markov chain (EMC) and the corresponding sojourn times
# of any simulated sequence, we can simply use the `base::rle()` function.
sim_seq_emc <- base::rle(cut_sim)$values # embedded Markov chain
sim_seq_sojourn_times <- base::rle(cut_sim)$lengths # sojourn times
cat("Start of the simulated sequence: ", head(cut_sim),
"...\nThe embedded Markov chain: ", head(sim_seq_emc),
"...\nThe sojourn times: ", head(sim_seq_sojourn_times), "...")