gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:48:08 25 4
gpt4 key购买 nike

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

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

关于r - R中的简单马尔可夫链(可视化),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8026597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com