【问题标题】:Markov Chains and decimal points in r?r中的马尔可夫链和小数点?
【发布时间】:2018-04-10 20:38:40
【问题描述】:

我已经从 r 中的矩阵绘制了马尔可夫链。但是,我有很多低于 0.01 的概率,因此我的概率图看起来像这样:

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

我的代码如下:

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)

非常感谢!

【问题讨论】:

  • 我在mran.revolutionanalytics.com/web/packages/markovchain/vignettes/… 浏览了该软件包的文档,看来您最多只能显示 2 位小数。我不知道这是否可以接受,但您可以将所有内容乘以 100 或类似的值吗?
  • 这可能是故意的,因为该图在小数点后 2 位将难以阅读。我认为您最好查看转换矩阵本身。
  • @Seymour 将所有内容乘以 100(从而将概率显示为百分比)似乎是最好的解决方案。下面的解决方案(尽管它们令人印象深刻)太杂乱而无法阅读。
  • @John Coleman 我同意这些解决方案似乎过于混乱,有很多小数位。还有一些其他的包可以完成整理东西的任务(见stackoverflow.com/a/21035960/9598813),但我个人喜欢你使用百分比的想法。

标签: r markov-chains markov


【解决方案1】:

您需要编辑 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)

【讨论】:

  • 我非常喜欢这个答案,因为整个思考过程——使用showMethods()getMethod() 来调查函数的行为——在许多情况下都非常有帮助,而不仅仅是这个。
【解决方案2】:

这是一个使用igraph 包的简单解决方案。如果您查看markovchain 的文档,markovchain 的绘图函数只是从igraph 调用绘图函数,因此您会发现许多(如果不是全部)绘图参数在两种类型之间是交叉兼容的对象。

在这里,我们只需从您的转换矩阵创建一个igraph 对象,然后将其绘制出来。您可能会发现这比直接绘制 markovchain 对象更方便。

library(markovchain)
library(igraph)
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)

# create an igraph object from your transition matrix
graph <- as(newtransition.matrix, "igraph") 

plot(graph, vertex.size = 15,
            edge.label = E(graph)$prob, # We add the proper edge labels here
            layout = layout,
            edge.arrow.size=0.25)

【讨论】:

    猜你喜欢
    • 2015-11-15
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 2019-09-13
    相关资源
    最近更新 更多