从边缘定义转移概率矩阵

问题描述 投票:2回答:1

我想使用来自define_transitionheemod来定义具有概率的边缘的转移概率矩阵。我正在构建一个决策树,其中每个边代表一个决策的条件概率。此树中的结束节点是以.ts或.nts后缀结尾的边。

此外,this post提供了有关使用markovchain的createSequenceMatrix来解决稍微类似的问题的信息,但我无法弄清楚如何使用此函数来解决我的边缘到矩阵问题。我不确定igraph在这种情况下是否有帮助,但我用它来表明我认为定义转换应该运行的内容。

您将提供的任何帮助将不胜感激!

我没有成功尝试按元素构建转换矩阵元素。

以下是数据的样子,我尝试过的内容以及我想要输出的define_transition:

if (("heemod" %in% rownames(installed.packages()))==FALSE) install.packages("heemod"); library(heemod)
if (("markovchain" %in% rownames(installed.packages()))==FALSE) install.packages("markovchain"); library(markovchain)
if (("igraph" %in% rownames(installed.packages()))==FALSE) install.packages("igraph"); library(igraph)


data<-dput(structure(list(from = c("alf", "alf", "alf", "t1", "t1", "t2", 
"t2", "t3", "t3", "t1.t", "t1.t", "t1.nt", "t1.nt", "t2.t", "t2.t", 
"t2.nt", "t2.nt", "t3.t", "t3.t", "t3.nt", "t3.nt"), to = c("t1", 
"t2", "t3", "t1.t", "t1.nt", "t2.t", "t2.nt", "t3.t", "t3.nt", 
"t1.t.ts", "t1.t.nts", "t1.nt.ts", "t1.nt.nts", "t2.t.ts", "t2.t.nts", 
"t2.nt.ts", "t2.nt.nts", "t3.t.ts", "t3.t.nts", "t3.nt.ts", "t3.nt.nts"
), prob = c(0.25, 0.314285714285714, 0.435714285714286, 0.976190476190476, 
0.0238095238095238, 0.88, 0.12, 0.961748633879781, 0.0382513661202186, 
0.560975609756098, 0.439024390243902, 0.2, 0.8, 0.8, 0.2, 0.04, 
0.96, 0.988636363636364, 0.0113636363636364, 0, 1)), row.names = c(NA, 
-21L), class = c("tbl_df", "tbl", "data.frame"))

# hopeless/unsuccessfull attempt at element by element approach
p.t1 = 210/840,
p.t2 = 264/840,
p.t3 = 1-(p.t1+p.t2),
p.t1.t = 205/210,
p.t1.nt = 1- p.t1.t,
heemod::define_transition(0,p.t1,p.t2,p.t3,0,0,
                               0,0,0,0,0,0,
                               0,0,0,0,0,0,
                               0,0,0,0,0,0,
                               0,0,0,0,0,0,
                               0,0,0,0,0,0
))

# Desired output that define transition reads, only probability values are the 1s
graph.data.frame(data,directed = TRUE)
as_adjacency_matrix(graph.data.frame(data,directed = TRUE))

#[[ suppressing 22 column names ‘alf’, ‘t1’, ‘t2’ ... ]]

alf       . 1 1 1 . . . . . . . . . . . . . . . . . .
t1        . . . . 1 1 . . . . . . . . . . . . . . . .
t2        . . . . . . 1 1 . . . . . . . . . . . . . .
t3        . . . . . . . . 1 1 . . . . . . . . . . . .
t1.t      . . . . . . . . . . 1 1 . . . . . . . . . .
t1.nt     . . . . . . . . . . . . 1 1 . . . . . . . .
t2.t      . . . . . . . . . . . . . . 1 1 . . . . . .
t2.nt     . . . . . . . . . . . . . . . . 1 1 . . . .
t3.t      . . . . . . . . . . . . . . . . . . 1 1 . .
t3.nt     . . . . . . . . . . . . . . . . . . . . 1 1
t1.t.ts   . . . . . . . . . . . . . . . . . . . . . .
t1.t.nts  . . . . . . . . . . . . . . . . . . . . . .
t1.nt.ts  . . . . . . . . . . . . . . . . . . . . . .
t1.nt.nts . . . . . . . . . . . . . . . . . . . . . .
t2.t.ts   . . . . . . . . . . . . . . . . . . . . . .
t2.t.nts  . . . . . . . . . . . . . . . . . . . . . .
t2.nt.ts  . . . . . . . . . . . . . . . . . . . . . .
t2.nt.nts . . . . . . . . . . . . . . . . . . . . . .
t3.t.ts   . . . . . . . . . . . . . . . . . . . . . .
t3.t.nts  . . . . . . . . . . . . . . . . . . . . . .
t3.nt.ts  . . . . . . . . . . . . . . . . . . . . . .
t3.nt.nts . . . . . . . . . . . . . . . . . . . . . .

r igraph markov-chains
1个回答
1
投票

这是第一次尝试,可能有点复杂。我们首先创建一个稀疏邻接矩阵(就像你在问题中所做的那样)。在下一步中,我们用实际转移概率覆盖1。

adj <- as_adjacency_matrix(graph.data.frame(data, directed = TRUE))
adj@x <- data$prob
adj <- as.matrix(adj)

这为我们提供了一个具有转移概率的矩阵。要使用define_transition,我们可以做到

do.call(define_transition, as.list(t(adj)))
# No named state -> generating names.
# A transition matrix, 22 states.

#   A B    C                 D                 E                
# A   0.25 0.314285714285714 0.435714285714286  
# <snip>
© www.soinside.com 2019 - 2024. All rights reserved.