Reputation: 49
I would like to define a matrix of transition probabilities from edges with probabilities using define_transition from heemod. I am building a decision-tree where each edge represents a conditional probability of a decision. The end nodes in this tree are the edges that end with the .ts or .nts suffix.
In addition, this post provides information on using markovchain's createSequenceMatrix to address a slightly similar problem, but I couldn't figure out how to use this function to address my the edge to matrix issue. I am not sure if igraph could help in this scenario, but I used it to show what I think define transition should have to run.
Any help you can provide will be greatly appreciated!
I unsuccessfully attempted to build the transition matrix element by element.
Here is what the data looks like, what I've attempted, and what I want define_transition to output:
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 . . . . . . . . . . . . . . . . . . . . . .
Upvotes: 2
Views: 401
Reputation: 13108
Here's a first attempt, which may be a little convoluted. We first create a sparse adjacency matrix (as you did in your question). In the next step, we overwrite the 1s with the actual transition probabilities.
adj <- as_adjacency_matrix(graph.data.frame(data, directed = TRUE))
adj@x <- data$prob
adj <- as.matrix(adj)
This gives us a matrix with the transition probabilities. To use define_transition
, we can do
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>
Upvotes: 1