Changing transition probability in a cycle for a Markov model

I wanted to make two Markov models in R. However, I am wondering if it possible to change some of the transition probabilities after a certain cycle (e.g. for cycle 14 up and until 25) in the model. Below, an example of my statement in R to create a Markov model.
For clarity, above this I defined the transition probabilities for the different combinations mentioned in the model.

  c(p_AA, p_AB, p_AC, 0, 0, 0,
    p_BA, p_BB, p_BC, p_BD, 0, 0,
    p_CA, p_CB, p_CC, p_CD, p_CE, 0,
    p_DA, p_DB, 0, p_DD, p_DE, 0,
    0, 0, 0, 0, p_EE, p_EF,
    0, 0, 0, 0, 0, 1),
  byrow= TRUE,
  nrow = 6, ncol = 6
)
state_names <- c("A", "B", "C", "D", "E", "F")
colnames(p_try) <- rownames(p_try) <- state_names
#creating markov model
x_init <- c(10, 0, 0, 0, 0, 0)
x_init %*% p_try
x_init %*% p_try %*% p_try

markov_chain <- function(x0, p, n_cycles = 100){
  x <- matrix(NA, ncol = length(x0), nrow = n_cycles)
  x <- rbind(x0, x)
  colnames(x) <- colnames(p)
  rownames(x) <- 0:n_cycles
  
  for (t in 1:n_cycles){
    x[t+1, ] <- x[t, ] %*% p
  }
return(x)
}
x_try <- markov_chain(x_init, p_try)

Thanks for your help in advance!

Are you getting an index out of bounds error. Maybe x should be one row longer? If you have two different transition matrices use the first one until cycle 14 and then use the other one from 15 to n_cycles.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.