我想使用define_transition中的heemod定义具有概率的边的转移概率矩阵。我正在构建一个决策树,其中每个边代表一个决策的条件概率。该树的末端节点是以.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 . . . . . . . . . . . . . . . . . . . . . .
答案 0 :(得分: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>