Calculate Transition Matrix (Markov) in R 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)))

 A: I have just uploaded a new R package, markovchain, based on S4 programming style. Along with various methods to handle S4 markovchain objects it contains a function to fit a Markov chain from a sequence of states. Have a look at:
library(markovchain) 
sequence <- c("a", "b", "a", "a", "a", "a", "b", "a", "b", "a", 
              "b", "a", "a", "b", "b", "b", "a")
mcFit <- markovchainFit(data=sequence)

It could help.
A: 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.
