Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 定义边的转移概率矩阵_R_Igraph_Markov Chains - Fatal编程技术网

R 定义边的转移概率矩阵

R 定义边的转移概率矩阵,r,igraph,markov-chains,R,Igraph,Markov Chains,我想用from定义一个从边到边的转移概率矩阵。我正在构建一个决策树,其中每条边表示一个决策的条件概率。此树中的结束节点是以.ts或.nts后缀结束的边 此外,还提供了有关使用markovchain的createSequenceMatrix来解决一个稍微类似的问题的信息,但我不知道如何使用此函数来解决边缘到矩阵的问题。我不确定igraph在这个场景中是否有帮助,但我用它来展示我认为define transition应该运行什么 我们将非常感谢您提供的任何帮助 我试图逐元素构建转换矩阵,但没有成功

我想用from定义一个从边到边的转移概率矩阵。我正在构建一个决策树,其中每条边表示一个决策的条件概率。此树中的结束节点是以.ts或.nts后缀结束的边

此外,还提供了有关使用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 . . . . . . . . . . . . . . . . . . . . . .

if((“%heemod”%in%rownames(installed.packages())==FALSE)install.packages(“heemod”);图书馆(heemod)
如果((“%markovchain”%in%rownames(installed.packages())==FALSE)install.packages(“markovchain”);图书馆(马尔可夫链)
如果(((%rownames(installed.packages())中的igraph”%==FALSE)install.packages(“igraph”);图书馆(igraph)

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

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

伟煌,非常感谢你,这正是我想要的。你们能帮我理解第一条as_邻接矩阵线在做什么吗?
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>