-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.R
223 lines (161 loc) · 7.54 KB
/
Test.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
library(mrgsolve)
library(ggplot2)
library(dplyr)
library(plyr)
library(dmutate)
library(magrittr)
vanc_modl <- mread("Goti_et_al")
dosing <- ev(id=1,
amt=1000,
rate=1000/1,
cmt=2,
addl=0,
ii=12,
CrCL=120,
WT=70,
DIAL=0)
sim_res <- vanc_modl %>%
ev(dosing) %>%
zero_re %>%
mrgsim(end=12)
data <- as.data.frame(sim_res)
obs = data.frame(time=5, y=17)
ggplot(data) + geom_line(aes(x=time, y=IPRED), size=1) +
geom_point(data=obs, aes(x=time, y=y), size=2, shape=1, colour="red", stroke=2.5) + theme_bw() +
xlab("Time since last dose [h]") + ylab("Vancomycin Plasma concentration [mg/L]") +
theme(axis.text = element_text(size=12), axis.title = element_text(size=14)) + ylim(0,60)
estimate_etas_empiricalBayes <- function(mod = vanc_modl,
init = c(ETA1=0.1, ETA2=0.1, ETA3=0.1),
data = dosing,
obs = data.frame(time=5, y=17)) {
### This is the objective function to be minimized
mapbayes <- function(eta, y, d, m){
eta <- as.list(eta)
names(eta) <- names(init)
eta_m <- eta %>% unlist %>% matrix(nrow=1)
m %<>% param(eta)
out <- m %>%
param(eta) %>%
obsonly %>% #Simulate only for timepoint where tdm was performed
zero_re() %>% #Omega and Sigma Matrices are dropped from the model
data_set(d) %>% #Data set used to simulate the Data
mrgsim
sig2j <- (out$IPRED^2)*(0.227^2)+(3.4)^2 ## Residual variance
sqwres <- log(sig2j) + (1/sig2j)*(y-out$IPRED)^2 ## square sum for -2LL estimate Bauer et al. (2007) AAPSJ E60
nOn <- diag(eta_m %*% omega.inv %*% t(eta_m)) ##
return(sum(sqwres)+nOn) ## -2LL empirical bayes estimate objective function value, see http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3339294/
}
#Retrieve omega matrix from model for MAP Bayesian estimation
omega <- (omat(mod, make=T))
omega <- diag(c((omega[1,1]),(omega[2,2]), (omega[3,3])))
#Invert Omega Matrix for Standard Error estimation
omega.inv <- solve(omega)
sigma <- c(0.227, 3.4)
t.obs=obs$time
y.obs=obs$y
#simulate concentration at time of concentration measurement
#to get a data frame containing information for the fitting process
sim <- mod %>%
omat(omega) %>% #Omega matrix is reset, if it has been deleted from the model earlier
data_set(data) %>%
carry.out(amt, evid, cmt) %>% #important to carry out EVERY information on dosing and covariates
mrgsim(end=-1, add=t.obs) #only simulate for time(s) with an observed concentration
#Dataframe used for simulation process
sim <- as.data.frame(sim)
#Dataframe used for fitting IMPORTANT: drop parameters to be predicted
fit_data <- sim %>% select(ID, time, evid, amt, cmt, IPRED)
fit_data
#minimize the mapbayes function and save ETAs to fit$par
#y= measured concentration(s)
#m= model object to be used
#d= data.frame containing information on dosing and patient
fit <- optim(init, fn=mapbayes, y=y.obs, m=mod, d=fit_data)
# save individual etas for the report
fit$par
est_eta <- fit$par
names(est_eta) <- c("ETA1", "ETA2", "ETA3")
ret = list(post_etas = est_eta)
}
a <- estimate_etas_empiricalBayes()
sim_res <- vanc_modl %>%
ev(dosing) %>%
param(a$post_etas) %>%
zero_re %>%
mrgsim(end=12)
new_data <- as.data.frame(sim_res)
ggplot(data) + geom_line(aes(x=time, y=IPRED), linetype=2, size=1) +
geom_line(data=new_data, aes(x=time, y=IPRED), colour="red", size=1) +
geom_point(data=obs, aes(x=time, y=y), size=2, shape=1, colour="red", stroke=2.5) + theme_bw() +
xlab("Time since last dose [h]") + ylab("Vancomycin Plasma concentration [mg/L]") +
theme(axis.text = element_text(size=12), axis.title = element_text(size=14)) + ylim(0,100)
mcmc_data <- read.csv("temp.csv")
head(mcmc_data)
ggplot(data) + geom_line(aes(x=time, y=IPRED), linetype=2, size=1) +
geom_point(data=obs, aes(x=time, y=y), size=2, shape=1, colour="red", stroke=2.5) + theme_bw() +
xlab("Time since last dose [h]") + ylab("Vancomycin Plasma concentration [mg/L]") +
theme(axis.text = element_text(size=12), axis.title = element_text(size=14)) +
geom_line(data = mcmc_data, aes(x=time, y=max), colour="red", size=1) +
geom_ribbon(data= mcmc_data, aes(x=time, ymin=s1, ymax=s2), fill="red", alpha=0.1) +
geom_ribbon(data= mcmc_data, aes(x=time, ymin=s3, ymax=s4), fill="red", alpha=0.1) +
geom_ribbon(data= mcmc_data, aes(x=time, ymin=s5, ymax=s6), fill="red", alpha=0.1) +
geom_ribbon(data= mcmc_data, aes(x=time, ymin=s7, ymax=s8), fill="red", alpha=0.1) +
xlim(0,12) + ylim(0,100)
data <- data.frame(times=seq(0,12, by=0.1))
jags.mod <- jags.model('Goti_et_al.bug',
data = list('c' = c(17),
'amt' = c(1000),
'dosing_time' = c(0),
't_inf' = c(1),
'CLCR'=c(120),
'WT'=c(70),
'DIAL'=c(0),
'times'= seq(0,12,by=0.1),
'tdm_times' = c(5),
"theta"=c(4.5, ## THETA1 - clearance (L/h)
58.4, ## THETA2 - volume of central compartment (L)
38.4, ## THETA3 - volume of peripheral compartment (L)
6.5, ## THETA4 - intercompartmental clearance (L/h)
0.8, ## THETA5 - CrCl on CL
0.7, ## THETA6 - DIAL on CL
0.5), ## THETA7 - DIAL on Vc
"omega"= c(0.398,
0.816,
0.571),
'sigma'=c(0.227 , 3.4) ),
n.chains = 4,
n.adapt = 1000)
d <- coda.samples(jags.mod,
c('eta1', 'eta2', 'eta3'),
1000, thin=1)
df <- as.data.frame(d[[4]])
mcmc_se <- list()
for (i in 1:nrow(df)) {
temp_dat <- pk_2cmt_infusion(theta = thetas,
CLCR=time_dep_cov$ClCr, WT=time_dep_cov$WT, DIAL=time_dep_cov$DIAL,
eta = c(df$eta1[i], df$eta2[i], df$eta3[i]),
dosing_events = dosing_events,
times=seq(0,72,0.1))
mcmc_se[[i]] <- temp_dat$IPRED
}
df_temp <- NULL
for (k in 1:nrow(df)) {
df_temp <- rbind(df_temp, mcmc_se[[k]])
}
s <- apply(df_temp,2,function(x) quantile(x,probs=c(0.05, 0.10, 0.15, 0.20, 0.8, 0.85, 0.9, 0.95, 0.5)))
## Combine individual PK data and quantils in a data.frame
pk_data <- data.frame(time=seq(0,72,0.1),
s1=s[1,],s2=s[8,],
s3=s[2,],s4=s[7,],
s5=s[3,],s6=s[6,],
s7=s[4,],s8=s[5,],
max=s[9,]) # median
## Build raw individual PK plot
p <- ggplot(pk_data) +
geom_ribbon(aes(ymin=s1, ymax=s2, x=time), fill="red", alpha=0.15) +
geom_ribbon(aes(ymin=s3, ymax=s4, x=time), fill="red", alpha=0.15) +
geom_ribbon(aes(ymin=s5, ymax=s6, x=time), fill="red", alpha=0.15) +
geom_ribbon(aes(ymin=s7, ymax=s8, x=time), fill="red", alpha=0.15) +
geom_line(aes(y=max, x=time))+
geom_point(data=tdm_data, aes(x=time, y=conc)) +
geom_line(data=res, aes(x=times, y=IPRED), linetype=2)
plot(p)