-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDesign-methods.R
4779 lines (4058 loc) · 158 KB
/
Design-methods.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' @include Data-methods.R
#' @include Design-class.R
#' @include McmcOptions-class.R
#' @include Rules-methods.R
#' @include Simulations-class.R
#' @include helpers.R
#' @include mcmc.R
NULL
# nolint start
## ============================================================
##' Simulate outcomes from a CRM design
##'
##' @param object the \code{\linkS4class{Design}} object we want to simulate
##' data from
##' @param nsim the number of simulations (default: 1)
##' @param seed see \code{\link{set_seed}}
##' @param truth a function which takes as input a dose (vector) and returns the
##' true probability (vector) for toxicity. Additional arguments can be supplied
##' in \code{args}.
##' @param args data frame with arguments for the \code{truth} function. The
##' column names correspond to the argument names, the rows to the values of the
##' arguments. The rows are appropriately recycled in the \code{nsim}
##' simulations. In order to produce outcomes from the posterior predictive
##' distribution, e.g, pass an \code{object} that contains the data observed so
##' far, \code{truth} contains the \code{prob} function from the model in
##' \code{object}, and \code{args} contains posterior samples from the model.
##' @param firstSeparate enroll the first patient separately from the rest of
##' the cohort? (not default) If yes, the cohort will be closed if a DLT occurs
##' in this patient.
##' @param mcmcOptions object of class \code{\linkS4class{McmcOptions}},
##' giving the MCMC options for each evaluation in the trial. By default,
##' the standard options are used
##' @param parallel should the simulation runs be parallelized across the
##' clusters of the computer? (not default)
##' @param nCores how many cores should be used for parallel computing?
##' Defaults to the number of cores on the machine, maximum 5.
##' @param \dots not used
##' @param derive a named list of functions which derives statistics, based on the
##' vector of posterior MTD samples. Each list element must therefore accept
##' one and only one argument, which is a numeric vector, and return a number.
##'
##' @return an object of class \code{\linkS4class{Simulations}}
##'
##' @example examples/design-method-simulate-Design.R
##' @export
##' @importFrom parallel detectCores
##' @keywords methods
setMethod("simulate",
signature =
signature(
object = "Design",
nsim = "ANY",
seed = "ANY"
),
def =
function(object, nsim = 1L, seed = NULL,
truth, args = NULL, firstSeparate = FALSE,
mcmcOptions = McmcOptions(),
parallel = FALSE, nCores =
min(parallel::detectCores(), 5), derive = list(),
...) {
## checks and extracts
assert_function(truth)
assert_flag(firstSeparate)
assert_count(nsim, positive = TRUE)
assert_flag(parallel)
assert_count(nCores, positive = TRUE)
args <- as.data.frame(args)
nArgs <- max(nrow(args), 1L)
## seed handling
RNGstate <- set_seed(seed)
## from this,
## generate the individual seeds for the simulation runs
simSeeds <- sample.int(n = 2147483647, size = as.integer(nsim))
## the function to produce the run a single simulation
## with index "iterSim"
runSim <- function(iterSim) {
## set the seed for this run
set.seed(simSeeds[iterSim])
## what is now the argument for the truth?
## (appropriately recycled)
thisArgs <- args[(iterSim - 1) %% nArgs + 1, , drop = FALSE]
## start the simulated data with the provided one
thisData <- object@data
# In case there are placebo
if (thisData@placebo) {
## what is the probability for tox. at placebo?
thisProb.PL <- h_this_truth(
object@data@doseGrid[1],
thisArgs,
truth
)
}
## shall we stop the trial?
## First, we want to continue with the starting dose.
## This variable is updated after each cohort in the loop.
stopit <- FALSE
## what is the next dose to be used?
## initialize with starting dose
thisDose <- object@startingDose
## inside this loop we simulate the whole trial, until stopping
while (!stopit) {
## what is the probability for tox. at this dose?
thisProb <- h_this_truth(
thisDose,
thisArgs,
truth
)
## what is the cohort size at this dose?
thisSize <- size(object@cohort_size,
dose = thisDose,
data = thisData
)
## In case there are placebo
if (thisData@placebo) {
thisSize.PL <- size(object@pl_cohort_size,
dose = thisDose,
data = thisData
)
}
thisData <- h_determine_dlts(
data = thisData,
dose = thisDose,
prob = thisProb,
prob_placebo = thisProb.PL,
cohort_size = thisSize,
cohort_size_placebo = thisSize.PL,
dose_grid = object@data@doseGrid[1],
first_separate = firstSeparate
)
## what is the dose limit?
doselimit <- maxDose(object@increments,
data = thisData
)
## generate samples from the model
thisSamples <- mcmc(
data = thisData,
model = object@model,
options = mcmcOptions
)
## => what is the next best dose?
thisDose <- nextBest(object@nextBest,
doselimit = doselimit,
samples = thisSamples,
model = object@model,
data = thisData
)$value
## evaluate stopping rules
stopit <- stopTrial(object@stopping,
dose = thisDose,
samples = thisSamples,
model = object@model,
data = thisData
)
stopit_results <- h_unpack_stopit(stopit)
}
## get the fit
thisFit <- fit(
object = thisSamples,
model = object@model,
data = thisData
)
# Get the MTD estimate from the samples.
target_dose_samples <- dose(
mean(object@nextBest@target),
model = object@model,
samples = thisSamples
)
# Create a function for additional statistical summary.
additional_stats <- lapply(derive, function(f) f(target_dose_samples))
## return the results
thisResult <-
list(
data = thisData,
dose = thisDose,
fit =
subset(thisFit,
select = c(middle, lower, upper)
),
stop =
attr(
stopit,
"message"
),
report_results = stopit_results,
additional_stats = additional_stats
)
return(thisResult)
}
resultList <- get_result_list(
fun = runSim,
nsim = nsim,
vars =
c(
"simSeeds",
"args",
"nArgs",
"firstSeparate",
"truth",
"object",
"mcmcOptions"
),
parallel = parallel,
n_cores = nCores
)
# format simulation output
simulations_output <- h_simulations_output_format(resultList)
## return the results in the Simulations class object
ret <- Simulations(
data = simulations_output$dataList,
doses = simulations_output$recommendedDoses,
fit = simulations_output$fitList,
stop_report = simulations_output$stop_matrix,
stop_reasons = simulations_output$stopReasons,
additional_stats = simulations_output$additional_stats,
seed = RNGstate
)
return(ret)
}
)
##' Simulate outcomes from a rule-based design
##'
##' @param object the \code{\linkS4class{RuleDesign}} object we want to simulate
##' data from
##' @param nsim the number of simulations (default: 1)
##' @param seed see \code{\link{set_seed}}
##' @param truth a function which takes as input a dose (vector) and returns the
##' true probability (vector) for toxicity. Additional arguments can be supplied
##' in \code{args}.
##' @param args data frame with arguments for the \code{truth} function. The
##' column names correspond to the argument names, the rows to the values of the
##' arguments. The rows are appropriately recycled in the \code{nsim}
##' simulations.
##' @param parallel should the simulation runs be parallelized across the
##' clusters of the computer? (not default)
##' @param nCores how many cores should be used for parallel computing?
##' Defaults to the number of cores on the machine, maximum 5.
##' @param \dots not used
##'
##' @return an object of class \code{\linkS4class{GeneralSimulations}}
##'
##' @example examples/design-method-simulate-RuleDesign.R
##' @export
##' @keywords methods
setMethod("simulate",
signature =
signature(
object = "RuleDesign",
nsim = "ANY",
seed = "ANY"
),
def =
function(object, nsim = 1L, seed = NULL,
truth, args = NULL,
parallel = FALSE,
nCores =
min(parallel::detectCores(), 5L),
...) {
## checks and extracts
assert_function(truth)
assert_count(nsim, positive = TRUE)
assert_flag(parallel)
assert_count(nCores, positive = TRUE)
args <- as.data.frame(args)
nArgs <- max(nrow(args), 1L)
## seed handling
RNGstate <- set_seed(seed)
## from this,
## generate the individual seeds for the simulation runs
simSeeds <- sample(x = seq_len(1e5), size = as.integer(nsim))
## the function to produce the run a single simulation
## with index "iterSim"
runSim <- function(iterSim) {
## set the seed for this run
set.seed(simSeeds[iterSim])
## what is now the argument for the truth?
## (appropriately recycled)
thisArgs <- args[(iterSim - 1) %% nArgs + 1, , drop = FALSE]
## so this truth is...
thisTruth <- function(dose) {
do.call(
truth,
## First argument: the dose
c(
dose,
## Following arguments
thisArgs
)
)
}
## start the simulated data with the provided one
thisData <- object@data
## shall we stop the trial?
## First, we want to continue with the starting dose.
## This variable is updated after each cohort in the loop.
stopit <- FALSE
## what is the next dose to be used?
## initialize with starting dose
thisDose <- object@startingDose
## inside this loop we simulate the whole trial, until stopping
while (!stopit) {
## what is the probability for tox. at this dose?
thisProb <- thisTruth(thisDose)
## what is the cohort size at this dose?
thisSize <- size(object@cohort_size,
dose = thisDose,
data = thisData
)
## simulate DLTs
thisDLTs <- rbinom(
n = thisSize,
size = 1L,
prob = thisProb
)
## update the data with this cohort
thisData <- update(
object = thisData,
x = thisDose,
y = thisDLTs
)
## evaluate the rule
thisOutcome <- nextBest(object@nextBest,
data = thisData
)
thisDose <- thisOutcome$value
stopit <- thisOutcome$stopHere
}
## return the results
thisResult <-
list(
data = thisData,
dose = thisDose
)
return(thisResult)
}
resultList <- get_result_list(
fun = runSim,
nsim = nsim,
vars =
c(
"simSeeds",
"args",
"nArgs",
"truth",
"object"
),
parallel = parallel,
n_cores = nCores
)
## put everything in the GeneralSimulations format:
## setup the list for the simulated data objects
dataList <- lapply(resultList, "[[", "data")
## the vector of the final dose recommendations
recommendedDoses <- as.numeric(sapply(resultList, "[[", "dose"))
## return the results in the GeneralSimulations class object
ret <- GeneralSimulations(
data = dataList,
doses = recommendedDoses,
seed = RNGstate
)
return(ret)
}
)
##' Simulate outcomes from a dual-endpoint design
##'
##' @param object the \code{\linkS4class{DualDesign}} object we want to simulate
##' data from
##' @param nsim the number of simulations (default: 1)
##' @param seed see \code{\link{set_seed}}
##' @param trueTox a function which takes as input a dose (vector) and returns the
##' true probability (vector) for toxicity. Additional arguments can be supplied
##' in \code{args}.
##' @param trueBiomarker a function which takes as input a dose (vector) and
##' returns the true biomarker level (vector). Additional arguments can be
##' supplied in \code{args}.
##' @param args data frame with arguments for the \code{trueTox} and
##' \code{trueBiomarker} function. The column names correspond to the argument
##' names, the rows to the values of the arguments. The rows are appropriately
##' recycled in the \code{nsim} simulations.
##' @param sigma2W variance for the biomarker measurements
##' @param rho correlation between toxicity and biomarker measurements (default:
##' 0)
##' @param firstSeparate enroll the first patient separately from the rest of
##' the cohort? (not default) If yes, the cohort will be closed if a DLT occurs
##' in this patient.
##' @param mcmcOptions object of class \code{\linkS4class{McmcOptions}},
##' giving the MCMC options for each evaluation in the trial. By default,
##' the standard options are used
##' @param parallel should the simulation runs be parallelized across the
##' clusters of the computer? (not default)
##' @param nCores how many cores should be used for parallel computing?
##' Defaults to the number of cores on the machine, maximum 5.
##' @param \dots not used
##' @param derive a named list of functions which derives statistics, based on the
##' vector of posterior MTD samples. Each list element must therefore accept
##' one and only one argument, which is a numeric vector, and return a number.
##'
##' @return an object of class \code{\linkS4class{DualSimulations}}
##'
##' @example examples/design-method-simulate-DualDesign.R
##' @importFrom mvtnorm rmvnorm
##' @export
##' @keywords methods
setMethod("simulate",
signature =
signature(object = "DualDesign"),
def =
function(object, nsim = 1L, seed = NULL,
trueTox, trueBiomarker, args = NULL,
sigma2W, rho = 0,
firstSeparate = FALSE,
mcmcOptions = McmcOptions(),
parallel = FALSE,
nCores =
min(parallel::detectCores(), 5), derive = list(),
...) {
## checks and extracts
assert_function(trueTox)
assert_function(trueBiomarker)
assert_number(sigma2W, lower = 0)
assert_number(rho, lower = -1, upper = 1)
assert_flag(firstSeparate)
assert_count(nsim, positive = TRUE)
assert_flag(parallel)
assert_count(nCores, positive = TRUE)
args <- as.data.frame(args)
nArgs <- max(nrow(args), 1L)
## get names of arguments (excluding the first one which is the dose)
trueToxArgnames <- names(formals(trueTox))[-1]
trueBiomarkerArgnames <- names(formals(trueBiomarker))[-1]
## this is the covariance matrix we assume:
trueCov <- matrix(
c(
sigma2W, sqrt(sigma2W) * rho,
sqrt(sigma2W) * rho, 1
),
nrow = 2, byrow = TRUE
)
## seed handling
RNGstate <- set_seed(seed)
## from this,
## generate the individual seeds for the simulation runs
simSeeds <- sample(x = seq_len(1e5), size = as.integer(nsim))
## the function to produce the run a single simulation
## with index "iterSim"
runSim <- function(iterSim) {
## set the seed for this run
set.seed(simSeeds[iterSim])
## what is now the argument for the true functions?
## (appropriately recycled)
thisArgs <- args[(iterSim - 1) %% nArgs + 1, , drop = FALSE]
## so the true tox function is:
thisTrueTox <- function(dose) {
do.call(
trueTox,
## First argument: the dose
c(
dose,
## Following arguments: take only those that
## are required by the tox function
as.list(thisArgs)[trueToxArgnames]
)
)
}
## and the true biomarker function is:
thisTrueBiomarker <- function(dose) {
do.call(
trueBiomarker,
## First argument: the dose
c(
dose,
## Following arguments: take only those that
## are required by the biomarker function
as.list(thisArgs)[trueBiomarkerArgnames]
)
)
}
## start the simulated data with the provided one
thisData <- object@data
## shall we stop the trial?
## First, we want to continue with the starting dose.
## This variable is updated after each cohort in the loop.
stopit <- FALSE
## what is the next dose to be used?
## initialize with starting dose
thisDose <- object@startingDose
if (thisData@placebo) {
## what is the probability for tox. at placebo?
thisProb.PL <- thisTrueTox(object@data@doseGrid[1])
thisMeanZ.PL <- qlogis(thisProb.PL)
## what is the biomarker mean at placebo?
thisMeanBiomarker.PL <- thisTrueBiomarker(object@data@doseGrid[1])
}
# In case there are placebo, extract true Toxicity and Efficacy for placebo
## inside this loop we simulate the whole trial, until stopping
while (!stopit) {
## what is the probability for tox. at this dose?
thisProb <- thisTrueTox(thisDose)
## and the transformation to the z scale is:
thisMeanZ <- qlogis(thisProb)
## what is the biomarker mean at this dose?
thisMeanBiomarker <- thisTrueBiomarker(thisDose)
## what is the cohort size at this dose?
thisSize <- size(object@cohort_size,
dose = thisDose,
data = thisData
)
## In case there are placebo
## what is the cohort size at this dose for Placebo?
if (thisData@placebo) {
thisSize.PL <- size(object@pl_cohort_size,
dose = thisDose,
data = thisData
)
}
## simulate tox and biomarker response: depends on whether we
## separate the first patient or not.
tmp <-
if (firstSeparate && (thisSize > 1L)) {
## dose the first patient
tmpStart <- mvtnorm::rmvnorm(
n = 1,
mean =
c(
thisMeanBiomarker,
thisMeanZ
),
sigma = trueCov
)
if (thisData@placebo && (thisSize.PL > 0L)) {
tmpStart.PL <- mvtnorm::rmvnorm(
n = 1,
mean =
c(
thisMeanBiomarker.PL,
thisMeanZ.PL
),
sigma = trueCov
)
}
## if there is no DLT:
if (tmpStart[, 2] < 0) {
## enroll the remaining patients
tmpStart <-
rbind(
tmpStart,
mvtnorm::rmvnorm(
n = thisSize - 1,
mean =
c(
thisMeanBiomarker,
thisMeanZ
),
sigma = trueCov
)
)
if (thisData@placebo && (thisSize.PL > 0L)) {
tmpStart.PL <-
rbind(
tmpStart.PL,
mvtnorm::rmvnorm(
n = thisSize.PL,
mean =
c(
thisMeanBiomarker.PL,
thisMeanZ.PL
),
sigma = trueCov
)
)
}
}
if (thisData@placebo && (thisSize.PL > 0L)) {
list(tmpStart = tmpStart, tmpStart.PL = tmpStart.PL)
} else {
list(tmpStart = tmpStart)
}
} else {
## we can directly dose all patients
tmpStart <- mvtnorm::rmvnorm(
n = thisSize,
mean =
c(
thisMeanBiomarker,
thisMeanZ
),
sigma = trueCov
)
if (thisData@placebo && (thisSize.PL > 0L)) {
tmpStart.PL <- mvtnorm::rmvnorm(
n = thisSize.PL,
mean =
c(
thisMeanBiomarker.PL,
thisMeanZ.PL
),
sigma = trueCov
)
}
if (thisData@placebo && (thisSize.PL > 0L)) {
list(tmpStart = tmpStart, tmpStart.PL = tmpStart.PL)
} else {
list(tmpStart = tmpStart)
}
}
## extract biomarker and DLT samples
thisBiomarkers <- tmp$tmpStart[, 1]
thisDLTs <- as.integer(tmp$tmpStart[, 2] > 0)
# in case there are placebo
if (thisData@placebo && (thisSize.PL > 0L)) {
thisBiomarkers.PL <- tmp$tmpStart.PL[, 1]
thisDLTs.PL <- as.integer(tmp$tmpStart.PL[, 2] > 0)
## update the data first with placebo...
thisData <- update(
object = thisData,
x = object@data@doseGrid[1],
y = thisDLTs.PL,
w = thisBiomarkers.PL,
check = FALSE
)
### ... and then with active dose
thisData <- update(
object = thisData,
x = thisDose,
y = thisDLTs,
w = thisBiomarkers,
new_cohort = FALSE
)
} else {
thisData <- update(
object = thisData,
x = thisDose,
y = thisDLTs,
w = thisBiomarkers
)
}
## what is the dose limit?
doselimit <- maxDose(object@increments,
data = thisData
)
## generate samples from the model
thisSamples <- mcmc(
data = thisData,
model = object@model,
options = mcmcOptions
)
## => what is the next best dose?
thisDose <- nextBest(object@nextBest,
doselimit = doselimit,
samples = thisSamples,
model = object@model,
data = thisData
)$value
## evaluate stopping rules
stopit <- stopTrial(object@stopping,
dose = thisDose,
samples = thisSamples,
model = object@model,
data = thisData
)
stopit_results <- h_unpack_stopit(stopit)
}
## get the fit
thisFit <- fit(
object = thisSamples,
model = object@model,
data = thisData
)
# Get the MTD estimate from the samples.
target_dose_samples <- dose(
mean(object@nextBest@target),
model = object@model,
samples = thisSamples
)
# Create a function for additional statistical summary.
additional_stats <- lapply(derive, function(f) f(target_dose_samples))
## return the results
thisResult <-
list(
data = thisData,
dose = thisDose,
fitTox =
subset(thisFit,
select =
c(middle, lower, upper)
),
fit_biomarker =
subset(thisFit,
select =
c(
middleBiomarker, lowerBiomarker,
upperBiomarker
)
),
rho_est = median(thisSamples@data$rho),
sigma2w_est = median(1 / thisSamples@data$precW),
stop =
attr(
stopit,
"message"
),
additional_stats = additional_stats,
report_results = stopit_results
)
return(thisResult)
}
resultList <- get_result_list(
fun = runSim,
nsim = nsim,
vars =
c(
"simSeeds",
"args",
"nArgs",
"firstSeparate",
"trueTox",
"trueBiomarker",
"trueCov",
"object",
"mcmcOptions"
),
parallel = parallel,
n_cores = nCores
)
## put everything in the Simulations format:
## setup the list for the simulated data objects
dataList <- lapply(resultList, "[[", "data")
## the vector of the final dose recommendations
recommendedDoses <- as.numeric(sapply(resultList, "[[", "dose"))
## vector of rho estimates
rhoEstimates <- as.numeric(sapply(resultList, "[[", "rho_est"))
## vector of sigma2W estimates
sigma2Westimates <- as.numeric(sapply(resultList, "[[", "sigma2w_est"))
## setup the list for the final tox fits
fitToxList <- lapply(resultList, "[[", "fitTox")
## setup the list for the final biomarker fits
fitBiomarkerList <- lapply(resultList, "[[", "fit_biomarker")
## the reasons for stopping
stopReasons <- lapply(resultList, "[[", "stop")
# individual stopping rule results as matrix, labels as column names
stop_results <- lapply(resultList, "[[", "report_results")
stop_report <- as.matrix(do.call(rbind, stop_results))
## For dual simulations summary of additional statistics.
additional_stats <- lapply(resultList, "[[", "additional_stats")
## return the results in the DualSimulations class object
ret <- DualSimulations(
data = dataList,
doses = recommendedDoses,
rho_est = rhoEstimates,
sigma2w_est = sigma2Westimates,
fit = fitToxList,
fit_biomarker = fitBiomarkerList,
stop_report = stop_report,
stop_reasons = stopReasons,
additional_stats = additional_stats,
seed = RNGstate
)
return(ret)
}
)
## ============================================================
##' Obtain hypothetical trial course table for a design
##'
##' This generic function takes a design and generates a dataframe
##' showing the beginning of several hypothetical trial courses under
##' the design. This means, from the generated dataframe one can read off:
##' - how many cohorts are required in the optimal case (no DLTs observed) in
##' order to reach the highest dose of the specified dose grid (or until
##' the stopping rule is fulfilled)
##' - assuming no DLTs are observed until a certain dose level, what the next
##' recommended dose is for all possible number of DLTs observed
##' - the actual relative increments that will be used in these cases
##' - whether the trial would stop at a certain cohort
##' Examining the "single trial" behavior of a dose escalation design is
##' the first important step in evaluating a design, and cannot be replaced by
##' studying solely the operating characteristics in "many trials". The cohort
##' sizes are also taken from the design, assuming no DLTs occur until the dose
##' listed.
##'
##' @param object the design (\code{\linkS4class{Design}} or
##' \code{\linkS4class{RuleDesign}} object) we want to examine
##' @param \dots additional arguments (see methods)
##' @param maxNoIncrement maximum number of contiguous next doses at 0
##' DLTs that are the same as before, i.e. no increment (default to 100)
##'
##' @return The data frame
##'
##' @export
##' @keywords methods regression
setGeneric("examine",
def =
function(object, ..., maxNoIncrement = 100L) {
## check maxNoIncrement argument
assert_count(maxNoIncrement, positive = TRUE)
## there should be no default method,
## therefore just forward to next method!
standardGeneric("examine")
},
valueClass = "data.frame"
)
##' @describeIn examine Examine a model-based CRM
##'
##' @param mcmcOptions object of class \code{\linkS4class{McmcOptions}},
##' giving the MCMC options for each evaluation in the trial. By default,
##' the standard options are used
##'
##' @example examples/design-method-examine-Design.R
setMethod("examine",
signature =
signature(object = "Design"),
def =
function(object,
mcmcOptions = McmcOptions(),
...,
maxNoIncrement) {
## start with the empty table
ret <- data.frame(
dose = numeric(),
DLTs = integer(),
nextDose = numeric(),
stop = logical(),
increment = integer()
)
## start the base data with the provided one
baseData <- object@data
## are we finished and can stop?
stopit <- FALSE
## counter how many contiguous doses at 0 DLTs with
## no increment
noIncrementCounter <- 0L
## what is the next dose to be used?
## initialize with starting dose
thisDose <- object@startingDose
## inside this loop we continue filling up the table, until
## stopping
while (!stopit) {
## what is the cohort size at this dose?
thisSize <- size(object@cohort_size,
dose = thisDose,
data = baseData
)
if (baseData@placebo) {
thisSize.PL <- size(object@pl_cohort_size,
dose = thisDose,
data = baseData
)
}
## for all possible number of DLTs:
for (numDLTs in 0:thisSize)
{
## update data with corresponding DLT vector
if (baseData@placebo && (thisSize.PL > 0L)) {
thisData <- update(
object = baseData,
x = baseData@doseGrid[1],
y = rep(0, thisSize.PL),
check = FALSE
)
thisData <-
update(
object = thisData,
x = thisDose,
y =
rep(
x = c(0, 1),
times =
c(
thisSize - numDLTs,