-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLRM-code-Antonia-March.R
440 lines (365 loc) · 13.3 KB
/
BLRM-code-Antonia-March.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
library(tidyverse)
library(rjags)
library(runjags)
library(purrr)
library(dplyr)
library("xtable")
#dose levels
doses <- c(20,40,80,160,320,640,960)
#number of patients per current dose(MONO)
n.patients.per.dose.MONO <- c(0,0,2,3,0,0,0)
#number of patients per current dose (COMBO)
n.patients.per.dose.COMBO <- c(0,0,0,0,0,0,0)
#total number of patients across all dose levels
N <- sum(n.patients.per.dose.MONO + n.patients.per.dose.COMBO)
#whether a patient had a DLT or not
s <- c(0,0,0,0,0)
#current dose for each patient
d <- c(80,80,160,160, 160)#c(rep(doses, 2))
#reference dose
D.ref <- 960
#binary indicator I (0=monotherapy, 1 = combination therapy)
I.combo <- c(0,0,0,0,0)
#I.combo <- c(rep(0, length(doses)), rep(1, length(doses)))
#vector of prior means
mu0 <- -1.75
mu1 <- 0.00
mu2 <- 0.00
priorMean <- c(mu0, mu1, mu2)
#covariance matrix
sigma0 <- 1.60
sigma1 <- 0.2
sigma2 <- 1.5
priorVar <- matrix(c(sigma0^2, 0, 0,
0, sigma1^2, 0,
0, 0, sigma2^2), ncol = 3, byrow = TRUE)
priorPrec <- solve(priorVar)
#iterations
n.chains <- 3
iter.per.chain <- 1e6
burn.in <- 1e4
total.iter <- iter.per.chain + burn.in
#overdose probability threshold
c.overdose<-0.40
#number of DLTs on the current dose levels per each patient
no.DLT = c(0,0,0,0,0)
#####
############################################################################################################
blrm.data <- list(
D.ref = D.ref,
s = s,
d = d,
N = N,
priorPrec = priorPrec,
I.combo = I.combo,
priorMean = priorMean
)
# Model specification for JAGS
blrm.model.string <- "
model {
## Priors ##
theta[1:3] ~ dmnorm(priorMean[], priorPrec[1:3,1:3])
alpha0 <- theta[1]
alpha1 <- exp(theta[2])
alpha2 <- exp(theta[3])
## Likelihood ##
for (j in 1:N) {
lin.pred.p.1[j] <- alpha0 + alpha1 * log(d[j]/D.ref) + alpha2 * I.combo[j]
lin.pred.p.2[j] <- ifelse(lin.pred.p.1[j] < -10, -10, ifelse(lin.pred.p.1[j] > 10, 10, lin.pred.p.1[j]))
p[j] <- exp(lin.pred.p.2[j]) / (1 + exp(lin.pred.p.2[j]))
s[j] ~ dbern(p[j])
}
}
"
blrm.model <- jags.model(textConnection(blrm.model.string), data = blrm.data, n.chains = n.chains)
update(blrm.model, iter = burn.in)
samples <- jags.samples(blrm.model,
variable.names = c("alpha0", "alpha1", "alpha2"),
n.iter = iter.per.chain,
thin = 1)
# Extract posterior samples
a01 <- samples$alpha0[1, ,]
a11 <- samples$alpha1[1, , ]
a21 <- samples$alpha2[1, , ]
mean(a01)
mean(a11)
mean(a21)
########################################
#create empty vectors to store estimates
LCI<-UCI<-Pi.MTD<-Pi.MTD.store<-Pi.above<-toxicity<-mat.or.vec(length(doses)*2,1)
all.Pi <-mat.or.vec(iter.per.chain * n.chains,length(doses)*2)
allstop <-0
d <- c(rep(doses, 2))
#reference dose
D.ref <- 960
#binary indicator I (0=monotherapy, 1 = combination therapy)
I.combo <- c(rep(0, length(doses)), rep(1, length(doses)))
s <- rep(0, length(doses)*2)
for (j in 1:(length(doses)*2)) {
LP <- a01 + a11 * log(d[j]/D.ref) + a21 * I.combo[j]
#make sure LP stays between -10 and 10
LP <- pmin(pmax(LP, -10), 10)
#calculate the odds
odds <- exp(LP)
#calculate the predicted probabilities for specific j
Pi <- odds / (1 + odds)
#calculate Pi for DLT for individual patient j at their current dose level
Pi.MTD.store[j] <- Pi.MTD[j] <- mean(Pi < 0.30) - mean(Pi < 0.20)
#probability of toxicity greater than 30%
Pi.above[j] <- mean(Pi > 0.30)
#average predicted probability of toxicity across all patients j
toxicity[j] <- mean(Pi)
#Lower CI
LCI[j] <- quantile(Pi, 0.025)
#Upper CI
UCI[j] <- quantile(Pi, 0.975)
}
Pi.MTD[Pi.MTD == 0] <- 0.001
Pi.MTD[Pi.above > c.overdose] <- 0
#create empty vectors to store new variables for MONO
Pi.MTD.mono <- Pi.MTD[1:length(doses)]
Pi.MTD.mono.store <- Pi.MTD.store[1:length(doses)]
Pi.above.mono <- Pi.above[1:length(doses)]
toxicity.mono <- toxicity[1:length(doses)]
n.mono <- n.patients.per.dose.MONO[1:length(doses)]
s.mono <- s[1:length(doses)]
LCI.mono <- LCI[1:length(doses)]
UCI.mono <- UCI[1:length(doses)]
###
# vectors for COMBO
Pi.MTD.combo <- Pi.MTD[(length(doses)+1):(2*length(doses))]
Pi.MTD.combo.store<-Pi.MTD.store[(length(doses)+1):(2*length(doses))]
Pi.above.combo<-Pi.above[(length(doses)+1):(2*length(doses))]
toxicity.combo<-toxicity[(length(doses)+1):(2*length(doses))]
n.combo<- n.patients.per.dose.COMBO[1:length(doses)]
s.combo<-s[(length(doses)+1):(2*length(doses))]
LCI.combo<-LCI[(length(doses)+1):(2*length(doses))]
UCI.combo<-UCI[(length(doses)+1):(2*length(doses))]
###############################################################
coherence.up = T
prediction = FALSE
current.dose <- 4
# Decision logic based on MTD probabilities and overdose probabilities
if(all(Pi.MTD==0)){
# If no dose levels are considered admissible for both mono and combo therapy, set stop to 1 and reset the next dose and admissible doses variables to 0
stop <- 1
next.dose.mono <- next.dose.combo <- 0
admissible.doses.mono <- admissible.doses.combo <- c(0)
} else {
if(sum(s.mono)>0) {
increment<-2
}else{
increment<-3
}
##coherence constraint
if(all((no.DLT/N)>=1/3 & coherence.up)){
index <- which(doses>doses[current.dose])
Pi.MTD.mono[index]<-0
}else{
#no dose skip ##NB! it doesn't work for mono
if(current.dose<(length(doses)-1)){
index <-(current.dose+2):length(doses)
Pi.MTD.mono[index]<-0
}
}
admissible.doses.mono<-which(Pi.MTD.mono>0,arr=T)
next.dose.mono <- which.max(Pi.MTD.mono)
if(all(Pi.MTD.mono==0)){
admissible.doses.mono<-next.dose<-0
}
###combo
index.1<-which(Pi.MTD.mono==0)
Pi.MTD.combo[index.1]<-0
if(any(n.mono>=3)) {
index.2 <- max(which(n.mono>=3))
if(index.2<length(doses)) {
index.3<-(min(index.2+1, length(doses)):length(doses))
Pi.MTD.combo[index.3] <-0
}
} else{
Pi.MTD.combo[1:length(doses)] <-0
}
admissible.doses.combo<-which(Pi.MTD.combo>0, arr=T)
next.dose.combo<-which.max(Pi.MTD.combo)
if(all(Pi.MTD.combo==0)){
admissible.doses.combo<-next.dose.combo<-0
}
}
if(prediction){
d.predict<-seq(1, 30, 0.5)
combo.predict<-rep(0, length(d.predict))
Pi.MTD.predict<-Pi.above.predict<-toxicity.predict<-c()
for (j in 1:length(d.predict)){
LP.predict <- a01 + a11 * log(d.predict[j]/D.ref) + a21 * combo.predict[j]
LP <- pmin(pmax(LP, -10), 10)
odds <- exp(LP)
Pi <- odds / (1 + odds)
Pi.MTD.predict[j]<- mean(Pi < 0.30) - mean(Pi < 0.20)
Pi.above.predict[j] <- mean(Pi > 0.30)
toxicity.predict[j] <-mean(Pi)
}
Pi.MTD.predict[which(Pi.above.predict>c.overdose)] <-0
if(all((no.DLT/N)>=1/3 & coherence.up)) {
index<-which(d.predict>doses[current.dose])
Pi.MTD.predict[index]<-0
} else{
index <-which(d.predict>doses[current.dose+1])
Pi.MTD.predict[index]<-0
}
admissible.doses.predict <-d.predict[which(Pi.MTD.predict>0,arr=T)]
}
if(!prediction){
output <- list(
NextDose.Mono = next.dose.mono,
Target.Prob.Mono = Pi.MTD.mono.store,
Target.Prob.Const.Mono = Pi.MTD.mono,
Overdose.Mono = Pi.above.mono,
Toxicity.Est.Mono = toxicity.mono,
no.DLT = no.DLT,
Admissible.Mono = admissible.doses.mono,
DLTs.Mono = s.mono,
Data.Mono = n.mono,
Lower.Mono = LCI.mono,
Upper.Mono = UCI.mono,
NextDose.Combo = next.dose.combo,
Target.Prob.Combo = Pi.MTD.combo.store,
Target.Prob.Const.Combo = Pi.MTD.combo,
Overdose.Combo = Pi.above.combo,
Toxicity.Est.Combo = toxicity.combo,
Admissible.Combo = admissible.doses.combo,
DLTs.Combo = s.combo,
Data.Combo = n.combo,
Lower.Combo = LCI.combo,
Upper.Combo = UCI.combo)
} else {
output <- list(
NextDose.Mono = next.dose.mono,
Target.Prob.Mono = Pi.MTD.mono.store,
Target.Prob.Const.Mono = Pi.MTD.mono,
Overdose.Mono = Pi.above.mono,
Toxicity.Est.Mono = toxicity.mono,
no.DLT = no.DLT,
Admissible.Mono = admissible.doses.mono,
DLTs.Mono = s.mono,
Data.Mono = n.mono,
Lower.Mono = LCI.mono,
Upper.Mono = UCI.mono,
NextDose.Combo = next.dose.combo,
Target.Prob.Combo = Pi.MTD.combo.store,
Target.Prob.Const.Combo = Pi.MTD.combo,
Overdose.Combo = Pi.above.combo,
Toxicity.Est.Combo = toxicity.combo,
Admissible.Combo = admissible.doses.combo,
DLTs.Combo = s.combo,
Data.Combo = n.combo,
Lower.Combo = LCI.combo,
Upper.Combo = UCI.combo,
Target.Prob.Predict = Pi.MTD.predict,
Overdose.Predict = Pi.above.predict,
Tox.Est.Predict = toxicity.predict,
Admissible.Doses.Predict = admissible.doses.predict
)
}
example <- output
# Dose levels
doses <- c(20,40,80,160,320,640,960)
# Construct Monotherapy Table
output.table.mono <- matrix(ncol=length(doses), nrow=7)
rownames(output.table.mono) <- c("Dose", "Patients", "DLTs", "Mean Toxicity", "95% CI", "Overdose Probability", "Target Probability")
output.table.mono[1,] <- paste(doses, "mg", sep="")
output.table.mono[2,] <- paste("n=", example$Data.Mono, sep="")
output.table.mono[3,] <- paste("DLTs=", example$DLTs.Mono, sep="")
output.table.mono[4,] <- paste("Mean Tox=", round(example$Toxicity.Est.Mono, 2), sep="")
output.table.mono[5,] <- paste("95\\%CI=(", round(example$Lower.Mono, 2), ",", round(example$Upper.Mono, 2), ")", sep="")
output.table.mono[6,] <- paste("Overdose=", round(100 * example$Overdose.Mono), "\\%", sep="")
output.table.mono[7,] <- paste("Target=", round(100 * example$Target.Prob.Mono), "\\%", sep="")
# Construct Combination Therapy Table
output.table.combo <- matrix(ncol=length(doses), nrow=7)
rownames(output.table.combo) <- c("Dose", "Patients", "DLTs", "Mean Toxicity", "95% CI", "Overdose Probability", "Target Probability")
output.table.combo[1,] <- paste(doses, "mg", sep="")
output.table.combo[2,] <- paste("n=", example$Data.Combo, sep="")
output.table.combo[3,] <- paste("DLTs=", example$DLTs.Combo, sep="")
output.table.combo[4,] <- paste("Mean Tox=", round(example$Toxicity.Est.Combo, 2), sep="")
output.table.combo[5,] <- paste("95\\%CI=(", round(example$Lower.Combo, 2), ",", round(example$Upper.Combo, 2), ")", sep="")
output.table.combo[6,] <- paste("Overdose=", round(100 * example$Overdose.Combo), "\\%", sep="")
output.table.combo[7,] <- paste("Target=", round(100 * example$Target.Prob.Combo), "\\%", sep="")
# View the tables
print("Monotherapy Table")
print(output.table.mono)
print("Combination Therapy Table")
print(output.table.combo)
# Write to CSV files
write.csv(output.table.mono, "Monotherapy-SRC-Meeting-07.03-Results.csv", row.names = TRUE, na="")
write.csv(output.table.combo, "Combination-SRC-Meeting-07.03.Results.csv", row.names = TRUE, na="")
########
####
# Print Results Table in Latex Format (with colour coding)
####
output.table.mono.colour<-output.table.mono
for(ii in 1:length(doses)){
ttext<-output.table.mono[1,ii]
if(example$Overdose.Mono[ii]>c.overdose){
colour.name<-"coralred"
}else{
if(example$Target.Prob.Const.Mono[ii]==0){
colour.name<-"azure"
}else{
colour.name<-"brightgreen"
}
}
ttext.col<-paste0("\\cellcolor{", colour.name, "}", ttext)
output.table.mono.colour[1,ii]<-ttext.col
}
####
print(xtable(output.table.mono.colour), sanitize.text.function = identity,include.rownames=FALSE,include.colnames=FALSE, size="\\tiny")
latex<-print(xtable(output.table.mono.colour,), sanitize.text.function = identity,include.rownames=FALSE,include.colnames=FALSE, size="\\normalsize")
writeLines(
c(
"\\documentclass[12pt]{article}",
"\\usepackage[landscape,left=10mm,right=20mm,top=60mm,bottom=20mm]{geometry}",
"\\usepackage[table]{xcolor}",
"\\usepackage{color}",
"\\definecolor{azure}{rgb}{0.0, 0.5, 1.0}",
"\\definecolor{brightgreen}{rgb}{0.4, 1.0, 0.0}",
"\\definecolor{coralred}{rgb}{1.0, 0.25, 0.25}",
"\\begin{document}",
"\\thispagestyle{empty}",
latex,
"\\end{document}"
),
"table.tex"
)
####
output.table.combo.colour<-output.table.combo
for(ii in 1:length(doses)){
ttext<-output.table.combo[1,ii]
if(example$Overdose.Combo[ii]>c.overdose){
colour.name<-"coralred"
}else{
if(example$Target.Prob.Const.Combo[ii]==0){
colour.name<-"azure"
}else{
colour.name<-"brightgreen"
}
}
ttext.col<-paste0("\\cellcolor{", colour.name, "}", ttext, "+Combo")
output.table.combo.colour[1,ii]<-ttext.col
}
print(xtable(output.table.combo.colour), sanitize.text.function = identity,include.rownames=FALSE,include.colnames=FALSE, size="\\tiny")
latex<-print(xtable(output.table.combo.colour,), sanitize.text.function = identity,include.rownames=FALSE,include.colnames=FALSE, size="\\normalsize")
writeLines(
c(
"\\documentclass[12pt]{article}",
"\\usepackage[landscape,left=10mm,right=20mm,top=60mm,bottom=20mm]{geometry}",
"\\usepackage[table]{xcolor}",
"\\usepackage{color}",
"\\definecolor{azure}{rgb}{0.0, 0.5, 1.0}",
"\\definecolor{brightgreen}{rgb}{0.4, 1.0, 0.0}",
"\\definecolor{coralred}{rgb}{1.0, 0.25, 0.25}",
"\\begin{document}",
"\\thispagestyle{empty}",
latex,
"\\end{document}"
),
"table2.tex"
)