Building markov chain in r

835 views Asked by At

I have a text in a column and i would like to build a markov chain. I was wondering of there is a way to build markov chain for states A, B,C, D and generate a markov chain with that states. Any thoughts?

A<- c('A-B-C-D', 'A-B-C-A', 'A-B-A-B')
2

There are 2 answers

0
Ben Bolker On BEST ANSWER

Since you mentioned that you know how to work with statetable.msm, here's a way to translate the data into a form it can handle:

dd <- c('A-B-C-D', 'A-B-C-A', 'A-B-A-B')

Split on dashes and arrange in columns:

d2 <- data.frame(do.call(cbind,strsplit(dd,"-")))

Arrange in a data frame, identified by sequence:

d3 <- tidyr::gather(d2)

Construct the transition matrix:

statetable.msm(value,key,data=d3)
0
Sandipan Dey On

If you want to compute the transition probability matrix (row stochastic) with MLE from the data, try this:

A <- c('A-B-C-D', 'A-B-C-A', 'A-B-A-B', 'D-B-C-A') # the data: by modifying your example data little bit
df <- as.data.frame(do.call(rbind, lapply(strsplit(A, split='-'), function(x) t(sapply(1:(length(x)-1), function(i) c(x[i], x[i+1]))))))
tr.mat <- table(df[,1], df[,2])
tr.mat <- tr.mat / rowSums(tr.mat) # make the matrix row-stochastic
tr.mat

  #           A         B         C         D
  # A 0.0000000 1.0000000 0.0000000 0.0000000 # P(A|A), P(B|A), P(C|A), P(D|A) with MLE from data
  # B 0.2500000 0.0000000 0.7500000 0.0000000
  # C 0.6666667 0.0000000 0.0000000 0.3333333
  # D 0.0000000 1.0000000 0.0000000 0.0000000