直接从数据中获得马尔可夫模型图(makovchain或deemod包?) [英] Markov Model diagram directly from data (makovchain or deemod package?)

查看:193
本文介绍了直接从数据中获得马尔可夫模型图(makovchain或deemod包?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取一堆因子数据,并据此创建一个过渡矩阵,以便很好地可视化.我找到了一个非常甜美的包装,叫做"heemod",它与"diagram"一起做得不错.

I want to read a bunch of factor data and create a transition matrix from it that I can visualise nicely. I found a very sweet package, called 'heemod' which, together with 'diagram' does a decent job.

对于我的第一个快捷方法,运行了一段Python代码以获取矩阵,然后使用该R代码段绘制图形.请注意,转换概率来自该未公开且不太重要的Python代码,但您也可以假设我在纸上进行了计算.

For my first quick-and-dirty approach, a ran a piece of Python code to get to the matrix, then used this R sniplet to draw the graph. Note that the transition probabilities come from that undisclosed and less important Python code, but you can also just assume that I calculated it on paper.

library('heemod')
library('diagram')
mat_dim <- define_transition(
  state_names = c('State_A', 'State_B', 'State_C'),
  .18, .73, .09, 
  .22, .0, .78,
  .58, .08, .33);
plot(mat_dim)

但是,我想将所有内容集成到R中,并直接从序列数据中生成R中的转换矩阵和图.

However, I would like to integrate all in R and generate the transition matrix and the graph within R and from the sequence data directly.

这是我到目前为止所拥有的:

This is what I have so far:

library(markovchain)
library('heemod')
library('diagram')

# the data --- this is normally read from a file
data = c(1,2,1,1,1,2,3,1,3,1,2,3,1,2,1,2,3,3,3,1,2,3,2,3,1,2,3,3,1,2,3,3,1)
fdata = factor(data)
rdata = factor(data,labels=c("State_A","State_B","State_C"))

# create transition matrix
dimMatrix = createSequenceMatrix(rdata, toRowProbs = TRUE)
dimMatrix

问题:如何传输dimMatrix,以便define_transition可以处理它?<​​/p>

QUESTION: how can I transfer dimMatrix so that define_transition can process it?

mat_dim <- define_transition( ??? );
plot(mat_dim)

有什么想法吗?有更好/更简便的解决方案吗?

Any ideas? Are there better/easier solutions?

推荐答案

define_transition的输入似乎很尴尬.也许这是由于我对heemod软件包的经验不足,但似乎输入过渡的唯一方法是逐个元素.

The input to define_transition seems to be quite awkward. Perhaps this is due to my inexperience with the heemod package but it seems the only way to input transitions is element by element.

这是一种解决方法

library(heemod)
library(diagram)

首先将转换矩阵转换为列表.我使用了四舍五入的数字,这是可选的.这对应于define_transition

first convert the transition matrix to a list. I used rounding on the digits which is optional. This corresponds to the ... variables in define_transition

lis <- as.list(round(dimMatrix, 3))

现在将所需的所有其他命名参数添加到列表中:

now add to the list all other named arguments you wish:

lis$state_names = colnames(dimMatrix)

,现在使用do.call将这些参数传递给define_transition:

and now pass these arguments to define_transition using do.call:

plot(do.call(define_transition, lis))

更新:对评论中的问题:

Update: to the question in the comments:

lis <- as.list(t(round(dimMatrix, 3)))
lis$state_names = colnames(dimMatrix)
plot(do.call(define_transition, lis))

do.call

最明显的方法(在这里不起作用)是:

The most obvious way (which does not work here) is to do:

define_transition(dimMatrix, state_names = colnames(dimMatrix))

但是这会引发错误,因为define_transition希望将每个转换作为参数而不是矩阵或列表来提供.为了避免键入:

however this throws an error since the define_transition expects each transition to be supplied as an argument and not as a matrix or a list. In order to avoid typing:

define_transition(0.182, 0.222,..., state_names = colnames(dimMatrix))

一个人可以将所有参数放在列表中,然后像我一样在该列表上调用do.call.

one can put all the arguments in a list and then call do.call on that list as I have done.

这篇关于直接从数据中获得马尔可夫模型图(makovchain或deemod包?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆