Learn R Programming

cfda (version 0.12.1)

generate_Markov: Generate Markov Trajectories

Description

Simulate individuals from a Markov process defined by a transition matrix, time spent in each time and initial probabilities.

Usage

generate_Markov(
  n = 5,
  K = 2,
  P = (1 - diag(K))/(K - 1),
  lambda = rep(1, K),
  pi0 = c(1, rep(0, K - 1)),
  Tmax = 1,
  labels = NULL
)

Value

a data.frame with 3 columns: id, id of the trajectory, time, time at which a change occurs and state, new state.

Arguments

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 NULL, integers are used

Author

Cristian Preda

Details

For one individual, assuming the current state is \(s_j\) at time \(t_j\), the next state and time is simulated as follows:

  1. generate one sample, \(d\), of an exponential law of parameter lambda[s_j]

  2. define the next time values as: \(t_{j+1} = t_j + d\)

  3. generate the new state \(s_{j+1}\) using a multinomial law with probabilities Q[s_j,]

Examples

Run this code
# 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)

Run the code above in your browser using DataLab