0

Suppose that you have a bunch of states $A,B,C,D$ at time $t_1$ and a bunch of states $A,B,C,D$ at time $t_2$. How would you create a transition matrix in R to represent the probability of changing from various states across $t_1$ to $t_2$?

In a previous question, a package called markovchain is mentioned. But in the example given, it is not clear whether sequence represents states at one particular time or states at two different times. Would I have to concatenate the states at $t_1$ and $t_2$ in my example to get what is called sequence in the aforementioned question?

  • I have the same issue as you. How did you end up coding it? I have 2 time points - and it can transition from 5 state types (1-5). – user377403 Jan 12 '23 at 11:57

1 Answers1

2

Modeling the system as a Markov chain, the maximum likelihood estimate for the $A \rightarrow B$ transition probability is simply the number of times you saw $B$ following $A$, divided by the number of times you saw any state following $A$. The transition matrix is a matrix $P$ where $P_{ij}$ contains the $i \rightarrow j$ transition probability. A simple way to calculate this is to construct the transition count matrix $N$, where $N_{ij}$ contains the number of times you observed an $i \rightarrow j$ transition. Then, calculate $P$ by dividing each row of $N$ by its sum.

user20160
  • 32,439
  • 3
  • 76
  • 112
  • +1. To answer the R question, here's a sample dataset in a two-column data frame: n <- 1e3; states <- LETTERS[1:4]; f <- \(n) sample(states, n, replace = TRUE); X <- data.frame(t1 = f(n), t2 = f(n)). The algorithm to generate the MLE of the transition probabilities is dead simple: P <- table(X); (P / rowSums(P)). – whuber Jan 12 '23 at 15:34