rmatrixtransitionmarkov-chainsmarkov

calculate transition matrix for multiple sequences in matrix


Hi guys i am trying to calculate transition matrix for every sequences which is presented by each row in a matrix. For example, i have a matrix:

    dat<-matrix(c('a','b','c','a','a','a','b','b','a','c','a','a','c','c','a'),nrow = 3)

   > `
        [,1] [,2] [,3] [,4] [,5]
    [1,] "a"  "a"  "b"  "c"  "c" 
    [2,] "b"  "a"  "b"  "a"  "c" 
    [3,] "c"  "a"  "a"  "a"  "a" `

I can easily calculate transition matrix for a single row using

    > `mylistMc<-markovchainFit(data=dat[1,])`

I get a correct result

    > `      
     a   b  c
        a 0.5 0.5 0
        b 0.0 0.0 1
        c 0.0 0.0 1`

Then i tried to use

    > `mc<-markovchainListFit(data=dat[1,])`

But the results are weird, it gives 4 matrixes instead of 3 (i suppose it takes columns instead of rows maybe), i believe MarkovChainListFit is used for a multiple sequences, and i just don't getting how to use it. Any ideas guys? Thank you!


Solution

  • Try this:

    mc <- apply(t(dat),2,function(x) markovchainFit(x))
    trans_mat <- list(mc[[1]][[1]],mc[[2]][[1]],mc[[3]][[1]])