-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransition_probability.R
169 lines (156 loc) · 7.84 KB
/
transition_probability.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
########################################################################################
################# PROFOUND Naloxone Distribution model #### 2020 #######################
########################################################################################
# Function module for the microsimulation of the Profound Naloxone distribution model:
#
# Author: Xiao Zang, PhD; Shayla Nolen, MPH
# Marshall Lab, Department of Epidemiology, Brown University
#
# Created: May 16, 2020
# Last update: May 30, 2020
#
########################################################################################
################# Transistion probability function #########################
########################################################################################
trans.prob <- function(pop.t, params) {
list2env(params, environment())
# initialize a matrix to store transition probabilities of each individual
trans.prob.matrix <- matrix(NA, num_states + 1, nrow(pop.t)) # add one row for OD
rownames(trans.prob.matrix) <- c(agent_states, "od")
# create a vector to store baseline mortality (excluding od death) for each individual according to age and treatment
mor.rate <- numeric(nrow(pop.t))
mor.rate[filter(pop.t, age %in% c(10:14))$ind] <- mortality_probs["drug", "10to14"]
mor.rate[filter(pop.t, age %in% c(15:24))$ind] <- mortality_probs["drug", "15to24"]
mor.rate[filter(pop.t, age %in% c(25:34))$ind] <- mortality_probs["drug", "25to34"]
mor.rate[filter(pop.t, age %in% c(35:44))$ind] <- mortality_probs["drug", "35to44"]
mor.rate[filter(pop.t, age %in% c(45:54))$ind] <- mortality_probs["drug", "45to54"]
mor.rate[filter(pop.t, age %in% c(55:64))$ind] <- mortality_probs["drug", "55to64"]
mor.rate[filter(pop.t, age >= 65)$ind] <- mortality_probs["drug", "65over"]
# create a vector to store probability of overdose for each individual according to ever overdosed and fentanyl
# TO_REVIEW what does "multi" mean here
od.rate <- numeric(nrow(pop.t))
od.rate[filter(pop.t, curr.state == "preb" & ever.od == 0 & fx == 0)$ind] <- overdose_probs["preb", "first"]
od.rate[filter(pop.t, curr.state == "preb" & ever.od == 1 & fx == 0)$ind] <- overdose_probs["preb", "subs"]
od.rate[filter(pop.t, curr.state == "preb" & ever.od == 0 & fx == 1)$ind] <- overdose_probs["preb", "first"] * multi.fx
od.rate[filter(pop.t, curr.state == "preb" & ever.od == 1 & fx == 1)$ind] <- overdose_probs["preb", "subs"] * multi.fx
od.rate[filter(pop.t, curr.state == "il.lr" & ever.od == 0 & fx == 0)$ind] <- overdose_probs["il.lr", "first"]
od.rate[filter(pop.t, curr.state == "il.lr" & ever.od == 1 & fx == 0)$ind] <- overdose_probs["il.lr", "subs"]
od.rate[filter(pop.t, curr.state == "il.lr" & ever.od == 0 & fx == 1)$ind] <- overdose_probs["il.lr", "first"] * multi.fx
od.rate[filter(pop.t, curr.state == "il.lr" & ever.od == 1 & fx == 1)$ind] <- overdose_probs["il.lr", "subs"] * multi.fx
od.rate[filter(pop.t, curr.state == "il.hr" & ever.od == 0 & fx == 0)$ind] <- overdose_probs["il.hr", "first"]
od.rate[filter(pop.t, curr.state == "il.hr" & ever.od == 1 & fx == 0)$ind] <- overdose_probs["il.hr", "subs"]
od.rate[filter(pop.t, curr.state == "il.hr" & ever.od == 0 & fx == 1)$ind] <- overdose_probs["il.hr", "first"] * multi.fx
od.rate[filter(pop.t, curr.state == "il.hr" & ever.od == 1 & fx == 1)$ind] <- overdose_probs["il.hr", "subs"] * multi.fx
od.rate[filter(pop.t, curr.state == "NODU" & ever.od == 0 & fx == 0)$ind] <- overdose_probs["NODU", "first"]
od.rate[filter(pop.t, curr.state == "NODU" & ever.od == 1 & fx == 0)$ind] <- overdose_probs["NODU", "subs"]
od.rate[filter(pop.t, curr.state == "NODU" & ever.od == 0 & fx == 1)$ind] <- overdose_probs["il.lr", "first"] * multi.NODU.fx * multi.fx
od.rate[filter(pop.t, curr.state == "NODU" & ever.od == 1 & fx == 1)$ind] <- overdose_probs["il.lr", "subs"] * multi.NODU.fx * multi.fx
od.rate[filter(pop.t, curr.state == "relap" & ever.od == 0 & OU.state == "preb")$ind] <- overdose_probs["preb", "first"] * multi.relap
od.rate[filter(pop.t, curr.state == "relap" & ever.od == 1 & OU.state == "preb")$ind] <- overdose_probs["preb", "subs"] * multi.fx
od.rate[filter(pop.t, curr.state == "relap" & ever.od == 0 & OU.state == "il.lr")$ind] <- overdose_probs["il.lr", "first"] * multi.relap
od.rate[filter(pop.t, curr.state == "relap" & ever.od == 1 & OU.state == "il.hr")$ind] <- overdose_probs["il.lr", "subs"] * multi.fx
od.rate[filter(pop.t, curr.state == "relap" & ever.od == 0 & OU.state == "il.hr")$ind] <- overdose_probs["il.hr", "first"] * multi.relap
od.rate[filter(pop.t, curr.state == "relap" & ever.od == 1 & OU.state == "il.hr")$ind] <- overdose_probs["il.hr", "subs"] * multi.fx
# update the trans.prob matrix with the corresponding probabilities
ind.preb <- pop.t$curr.state == "preb"
if (sum(ind.preb) != 0) {
trans.prob.matrix[, ind.preb] <- rbind(
1 - p.preb2il.lr - p.preb2inact - mor.rate[ind.preb] - od.rate[ind.preb],
rep(p.preb2il.lr, sum(ind.preb)),
rep(0, sum(ind.preb)),
rep(p.preb2inact, sum(ind.preb)),
rep(0, sum(ind.preb)),
rep(0, sum(ind.preb)),
mor.rate[ind.preb],
od.rate[ind.preb]
)
}
ind.il.lr <- pop.t$curr.state == "il.lr"
if (sum(ind.il.lr) != 0) {
trans.prob.matrix[, ind.il.lr] <- rbind(
rep(0, sum(ind.il.lr)),
1 - p.il.lr2il.hr - p.il.lr2inact - mor.rate[ind.il.lr] - od.rate[ind.il.lr],
rep(p.il.lr2il.hr, sum(ind.il.lr)),
rep(p.il.lr2inact, sum(ind.il.lr)),
rep(0, sum(ind.il.lr)),
rep(0, sum(ind.il.lr)),
mor.rate[ind.il.lr],
od.rate[ind.il.lr]
)
}
ind.il.hr <- pop.t$curr.state == "il.hr"
if (sum(ind.il.hr) != 0) {
trans.prob.matrix[, ind.il.hr] <- rbind(
rep(0, sum(ind.il.hr)),
rep(p.il.hr2il.lr, sum(ind.il.hr)),
1 - p.il.hr2il.lr - p.il.hr2inact - mor.rate[ind.il.hr] - od.rate[ind.il.hr],
rep(p.il.hr2inact, sum(ind.il.hr)),
rep(0, sum(ind.il.hr)),
rep(0, sum(ind.il.hr)),
mor.rate[ind.il.hr],
od.rate[ind.il.hr]
)
}
ind.inact <- pop.t$curr.state == "inact"
if (sum(ind.inact) != 0) {
trans.prob.matrix[, ind.inact] <- rbind(
rep(0, sum(ind.inact)),
rep(0, sum(ind.inact)),
rep(0, sum(ind.inact)),
1 - p.inact2relap - mor.rate[ind.inact] - od.rate[ind.inact],
rep(0, sum(ind.inact)),
rep(p.inact2relap, sum(ind.inact)),
mor.rate[ind.inact],
od.rate[ind.inact]
)
}
ind.NODU <- pop.t$curr.state == "NODU"
if (sum(ind.NODU) != 0) {
trans.prob.matrix[, ind.NODU] <- rbind(
rep(0, sum(ind.NODU)),
rep(0, sum(ind.NODU)),
rep(0, sum(ind.NODU)),
rep(0, sum(ind.NODU)),
1 - mor.rate[ind.NODU] - od.rate[ind.NODU],
rep(0, sum(ind.NODU)),
mor.rate[ind.NODU],
od.rate[ind.NODU]
)
}
ind.relap <- pop.t$curr.state == "relap"
if (sum(ind.relap) != 0) {
OU.v <- filter(pop.t, curr.state == "relap")$OU.state
relap.m <- matrix(0, num_states + 1, sum(ind.relap))
for (r in 1:sum(ind.relap)) {
relap.m[which(OU.v[r] == agent_states), r] <- 1 - mor.rate[ind.relap][r] - od.rate[ind.relap][r]
relap.m[which("dead" == agent_states), r] <- mor.rate[ind.relap][r]
relap.m[num_states + 1, r] <- od.rate[ind.relap][r]
}
trans.prob.matrix[, ind.relap] <- relap.m
}
trans.prob.matrix[, pop.t$curr.state == "dead"] <- c(0, 0, 0, 0, 0, 0, 1, 0)
return(t(trans.prob.matrix))
}
# random sampling for vector of outcomes
samplev <- function(probs, m) {
d <- dim(probs)
n <- d[1]
k <- d[2]
lev <- dimnames(probs)[[2]]
if (!length(lev)) {
lev <- 1:k
}
ran <- matrix(lev[1], ncol = m, nrow = n)
U <- t(probs)
for (i in 2:k) {
U[i, ] <- U[i, ] + U[i - 1, ]
}
if (any((U[k, ] - 1) > 1e-05)) {
stop("error in multinom: probabilities do not sum to 1")
}
for (j in 1:m) {
un <- rep(runif(n), rep(k, n))
ran[, j] <- lev[1 + colSums(un > U)]
}
ran
}