forked from pourzanj/theophylline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint_estimate_comparison.R
More file actions
139 lines (108 loc) · 4.42 KB
/
point_estimate_comparison.R
File metadata and controls
139 lines (108 loc) · 4.42 KB
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
library(tidyverse)
library(PKPDmodels)
library(deSolve)
library(rstan)
options(mc.cores = parallel::detectCores())
# prepare data for first patient
theoph <- Theoph %>% as_tibble %>% mutate(DoseMass = Dose*Wt)
theoph %>% ggplot(aes(Time, conc, color = Subject)) + geom_point() + geom_line()
theoph.1 <- theoph %>% filter(Subject == 1)
theoph.1 %>% ggplot(aes(Time, conc)) + geom_point() + geom_line()
dat <- list(Nt = nrow(theoph.1),
ts = theoph.1 %>% pull(Time) %>% `[`(-1),
y_init = c(theoph.1 %>% pull(DoseMass) %>% head(1),
theoph.1 %>% pull(conc) %>% head(1)),
y = theoph.1 %>% pull(conc) %>% `[`(-1))
# get posterior samples
fit <- stan(file = "theoph_single_individual.stan",
data = dat,
iter = 2000, chains = 2, control = list(adapt_delta = 0.9))
post.samples <- extract(fit)
post.samples <- tibble(b = post.samples$b,
Ka = post.samples$theta[,1],
Cl = post.samples$theta[,2],
V = post.samples$theta[,3],
c_init = post.samples$c_init,
sigma = post.samples$sigma)
# forward simulate synthetic data with random parameter values from post.samples
ForwardSymTheo <- function(b, Ka, Cl, V, c_init, sigma) {
pkpd.two.compartment <- function(t, state, parameters) {
with(as.list(c(state, parameters)), {
dA <- -Ka*A
dc <- (1/V)*(Ka*A - (Cl/V)*(c*V))
list(c(dA, dc))
})
}
parameters <- c(b = b, Ka = Ka, Cl = Cl, V = V)
state <- c(A = 400*b, c = c_init)
times <- theoph.1 %>% pull(Time)
ode.soln <- ode(y = state, times = times,
func = pkpd.two.compartment, parms = parameters,
method = "bdf") %>%
as.data.frame %>%
as.tibble
synth.data <- ode.soln %>%
select(time,c) %>%
mutate(c = rnorm(nrow(.), mean = log(c), sd = sigma) %>% exp)
synth.data
}
GetKaConfIntervalsStanOptim <- function(tib) {
dat <- list(Nt = nrow(theoph.1),
ts = theoph.1 %>% pull(Time) %>% `[`(-1),
y_init = c(theoph.1 %>% pull(DoseMass) %>% head(1),
tib %>% pull(c) %>% head(1)),
y = tib %>% pull(c) %>% `[`(-1))
m <- stan_model(file = "theoph_single_individual.stan")
opt.object <- optimizing(model, data = dat, draws = 2000, hessian = TRUE)
quantiles <- opt.object$theta_tilde[,c("theta[1]")] %>%
quantile(probs = c(0.1,0.5,0.9))
quantiles
}
GetKaConfIntervalsStanNuts <- function(tib) {
dat <- list(Nt = nrow(theoph.1),
ts = theoph.1 %>% pull(Time) %>% `[`(-1),
y_init = c(theoph.1 %>% pull(DoseMass) %>% head(1),
tib %>% pull(c) %>% head(1)),
y = tib %>% pull(c) %>% `[`(-1))
m <- stan_model(file = "theoph_single_individual.stan")
fit <- stan(file = "theoph_single_individual.stan",
data = dat,
iter = 4000, chains = 1, control = list(adapt_delta = 0.9))
post.samples <- extract(fit)
quantiles <- post.samples$theta[,1] %>%
quantile(probs = c(0.1,0.5,0.9))
quantiles
}
post.sample.to.sim <- post.samples %>% sample_n(9)
synth.data <- post.sample.to.sim %>% pmap(ForwardSymTheo)
asymp.estimates <- synth.data %>%
map(GetKaConfIntervalsStanOptim) %>%
map2(1:9, function(quantiles,sample.num) quantiles %>%
t %>%
as_tibble %>%
set_names(c("Q10", "Q50", "Q90")) %>%
mutate(sample = sample.num) %>%
mutate(method = "Asymptotic")) %>%
bind_rows
nuts.estimates <- synth.data %>%
map(GetKaConfIntervalsStanNuts) %>%
map2(1:9, function(quantiles,sample.num) quantiles %>%
t %>%
as_tibble %>%
set_names(c("Q10", "Q50", "Q90")) %>%
mutate(sample = sample.num) %>%
mutate(method = "NUTS")) %>%
bind_rows
#save(asymp.estimates, nuts.estimates, file = "point_estimate_comparison.Rdata")
bind_rows(asymp.estimates, nuts.estimates) %>%
ggplot(aes(method, Q50)) +
geom_point() +
geom_errorbar(aes(ymin = Q10, ymax = Q90)) +
facet_wrap( ~ sample, scales = "free") +
# forgot to save true values so load manually from old plot
geom_hline(aes(x = NULL, y = NULL, yintercept = Ka),
color = "red",
data = tibble(Ka = c(1.41,2.4,1.51,1.51,1.5,1.14,0.94,1.4,1.3) , sample = 1:9)) +
xlab("Inference Method") +
ylab("Ka (Absorption Rate of Drug)") +
theme(text = element_text(size=20))