R中的简单马尔可夫链(可视化)

时间:2011-11-06 11:06:05

标签: r statistics visualization markov

我想在R中做一个简单的一阶马尔可夫链。我知道有像MCMC这样的包,但找不到一个用图形显示它。这甚至可能吗?如果给出一个转移矩阵和一个初始状态,那么可以直观地看到通过马尔可夫链的路径(也许我会手工完成这个......)。

感谢。

4 个答案:

答案 0 :(得分:10)

这显示了如何将随机转移矩阵应用于特定的起始向量:c(1,0,0,0):

set.seed(123)
tmat <- matrix(rnorm(16)^2,ncol=4) 
   # need entries to be positive, could have used abs()
tmat <- tmat/rowSums(tmat) # need the rows to sum to 1
tmat
            [,1]       [,2]       [,3]        [,4]
[1,] 0.326123580 0.01735335 0.48977444 0.166748625
[2,] 0.016529424 0.91768404 0.06196453 0.003822008
[3,] 0.546050789 0.04774713 0.33676288 0.069439199
[4,] 0.001008839 0.32476060 0.02627217 0.647958394
require(expm)   # for the %^% function
matplot( t(         # need to transpose to get arguments to matplot correctly
       sapply(1:20, function(x) matrix(c(1,0,0,0), ncol=4) %*% (tmat %^% x) ) ) )

你可以看到它接近平衡: enter image description here

答案 1 :(得分:4)

包coda(http://cran.r-project.org/web/packages/coda/index.html)具有分析MCMC结果的工具,包括一些绘图功能。

答案 2 :(得分:2)

也许Biostar上的这个查询可以帮助您:Visualizing HMM files of HMMER3。它指向两个外部应用程序LogoMat-MHMMeditor,用于可视化配置文件隐藏马尔可夫模型(pHMM)。

答案 3 :(得分:2)

您可以使用markovchain R软件包,它为离散时间马尔可夫链建模,并包含基于igraph包的绘图工具。

library(markovchain) #loading the package
myMatr<-matrix(c(0,.2,.8,.1,.8,.1,.3,0,.7),byrow=TRUE,nrow = 3) #defining a transition matrix
rownames(myMatr)<-colnames(myMatr)<-c("a","b","c")
myMc<-as(myMatr, "markovchain")
plot(myMc)
相关问题