簡體   English   中英

R中的簡單馬爾可夫鏈(可視化)

[英]Simple Markov Chain in R (visualization)

我想在R中做一個簡單的一階馬爾可夫鏈。我知道有像MCMC這樣的包,但找不到一個以圖形方式顯示它。 這甚至可能嗎? 如果給定一個轉換矩陣和一個初始狀態,可以直觀地看到通過馬爾可夫鏈的路徑(也許我可以手工完成這個......)。

謝謝。

這顯示了如何將隨機轉換矩陣應用於特定的起始向量: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) ) ) )

你可以看到它接近平衡: 在此輸入圖像描述

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

您可以使用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)

也許Biostar上的這個查詢可以幫助您: 可視化HMMER3的HMM文件 它指向兩個外部應用程序, LogoMat-MHMMeditor ,用於可視化配置文件隱藏馬爾可夫模型(pHMM)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM