Simulate individuals from a Markov process defined by a transition matrix, time spent in each time and initial probabilities.
number of trajectories to generate
number of states
matrix containing the transition probabilities from one state to another. Each row contains positive reals summing to 1.
time spent in each state
initial distribution of states
maximal duration of trajectories
state names. If NULL
, integers are used
a data.frame with 3 columns: id
, id of the trajectory, time
,
time at which a change occurs and state
, new state.
For one individual, assuming the current state is \(s_j\) at time \(t_j\), the next state and time is simulated as follows:
generate one sample, \(d\), of an exponential law of parameter lambda[s_j]
define the next time values as: \(t_{j+1} = t_j + d\)
generate the new state \(s_{j+1}\) using a multinomial law with probabilities Q[s_j,]
# Simulate the Jukes-Cantor model of nucleotide replacement
K <- 4
PJK <- matrix(1 / 3, nrow = K, ncol = K) - diag(rep(1 / 3, K))
lambda_PJK <- c(1, 1, 1, 1)
d_JK <- generate_Markov(
n = 100, K = K, P = PJK, lambda = lambda_PJK, Tmax = 10,
labels = c("A", "C", "G", "T")
)
head(d_JK)
#> id time state
#> 1 1 0.0000000 A
#> 2 1 0.2092139 T
#> 3 1 1.7666995 A
#> 4 1 2.1512726 C
#> 5 1 2.6233965 A
#> 6 1 3.2061763 G