我想在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) ) ) )
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)