0
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ 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). $\endgroup$
    – user377403
    Jan 12, 2023 at 11:57

1 Answer 1

2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ +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)). $\endgroup$
    – whuber
    Jan 12, 2023 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.