generate_Markov {cfda} | R Documentation |
Simulate individuals from a Markov process defined by a transition matrix, time spent in each time and initial probabilities.
generate_Markov( n = 5, K = 2, P = 1 - diag(K), lambda = rep(1, K), pi0 = c(1, rep(0, K - 1)), Tmax = 1, labels = NULL )
n |
number of trajectories to generate |
K |
number of states |
P |
matrix containing the transition probabilities from one state to another. Each row contains positive reals summing to 1. |
lambda |
time spent in each state |
pi0 |
initial distribution of states |
Tmax |
maximal duration of trajectories |
labels |
state names. If |
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,]
a data.frame with 3 columns: id
, id of the trajectory, time
, time at which a change occurs and state
, new state.
Cristian Preda
# 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)