简体   繁体   English

在R中建立Markov链

[英]Building markov chain in r

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. 我想知道是否有一种方法可以为状态A,B,C,D建立马尔可夫链,并使用该状态生成马尔可夫链。 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: 如果要根据数据使用MLE计算转移概率矩阵(随机行),请尝试以下操作:

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: 既然您提到了您知道如何使用statetable.msm ,那么这是一种将数据转换为可以处理的形式的方法:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM