r - r中的马尔可夫链和小数点?

标签 r markov-chains markov

我已经从 r 中的矩阵绘制了一个马尔可夫链。但是,我有许多低于 0.01 的概率,因此我的概率图如下所示:

enter image description here

我已经搜索了几个小时,但似乎找不到可以显示所有四个小数位的东西。有什么办法可以格式化这个还是我应该保持原样?

我的代码如下:

library(markovchain)

newtransition.matrix <- matrix( data = c(
  .9366, .0066, .0007, .0003 ,.0003, 0, .0015, 0, 
  .0583, .9172, .0225, .0026, .0006, .001, 0, 0,
  .004, .0694, .9176, .0483, .0044, .0032, .0029, 0,
  .0009, .0049, .0518, .8924, .0666, .0046, .0088, 0,
  .0002, .0006, .0049, .0444, .8323, .0572, .0191, 0,
  0, .0009, .002, .0081, .0746, .8362, .1028, 0,
  0, .0002, .0001, .0016, .0105, .0384, .6123, 0,
  0, .0002, .0004, .0023, .0107, .0594, .2526, 1),
  nrow = 8, ncol = 8,
  dimnames = list( c( "AAA", "AA", "A", "BBB", "BB", "B", "CCC", "Default" ), c( "AAA", "AA", "A", "BBB", "BB", "B", "CCC", "Default") ) )
 print( newtransition.matrix )

newtransition.matrix <- new( "markovchain", transitionMatrix = newtransition.matrix )
layout <- matrix(c(-3, 1, 2, 2, 2, -2, 4, 1, 0, 6, 0, -6, -3, -4, 3, -4), ncol = 2, byrow = TRUE)

plot(newtransition.matrix, vertex.size = 10, layout = layout, edge.arrow.size=0.25)

非常感谢!

最佳答案

您需要编辑 S4 方法。 2 位数限制在绘图函数中硬编码。我无法仅用代码来编辑函数的主体(如果其他人能弄清楚,请发表评论)。下面的代码需要一些用户输入。

# Digging to find the plotting function for markovchain
showMethods(plot)

# Find the source code
f <- getMethod("plot", signature = c(x="markovchain", y="missing"))

# Ahh it uses plot.igraph, and the labels are being specified with edge.label = edgeLabel
# But edgeLabel is being rounded to 2 digits
# Extract and edit the body of f, 
# Change round(E(netMc)$weight/100, 2) to round(E(netMc)$weight/100, 4) or something larger
g <- edit(body(f@.Data))

# Store the edited body again
body(f@.Data) <- g

# Call new plotting function to plot with more digits
f(newtransition.matrix, vertex.size = 10, layout = layout, edge.arrow.size=0.25)

enter image description here

关于r - r中的马尔可夫链和小数点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49762513/

相关文章:

r - R如何关闭特定错误

r - 在给定转移概率矩阵的情况下寻找马尔可夫过程的平稳分布

uml - 有条件转移的有限状态机可以表示为马尔可夫链吗?

R马尔可夫链包,是否可以设置节点的坐标和大小?

r - 保留具有不同名称的重复列。右

r - 为什么在不输入 "Arithmetic"的情况下键入 `?mgcv-faq`时,为什么要引用 `library(mgcv)`的手册页?

R库用于离散马尔可夫链仿真

隐马尔可夫模型预测下一个观察

r - 通过加入 R 进行条件分组

r - 计算概率后缀树中上下文状态关系的提升?