Markov Process in R – Calculate Transition Matrix

markov-processr

Is there a way in R (a built-in function) to calculate the transition matrix for a Markov Chain from a set of observations?

For example, taking a data set like the following and calculate the first order transition matrix?

dat<-data.frame(replicate(20,sample(c("A", "B", "C","D"), size = 100, replace=TRUE)))

Best Answer

I am not immediately aware of a "built-in" function (e.g., in base or similar), but we can do this very easily and efficiently in a couple of lines of code.

Here is a function that takes a matrix (not a data frame) as an input and produces either the transition counts (prob=FALSE) or, by default (prob=TRUE), the estimated transition probabilities.

# Function to calculate first-order Markov transition matrix.
# Each *row* corresponds to a single run of the Markov chain
trans.matrix <- function(X, prob=T)
{
    tt <- table( c(X[,-ncol(X)]), c(X[,-1]) )
    if(prob) tt <- tt / rowSums(tt)
    tt
}

If you need to call it on a data frame you can always do

trans.matrix(as.matrix(dat))

If you're looking for some third-party package, then Rseek or the R search site may provide additional resources.