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')
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')
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
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:Split on dashes and arrange in columns:
Arrange in a data frame, identified by sequence:
Construct the transition matrix: