-
Notifications
You must be signed in to change notification settings - Fork 9
/
Model-class.R
3422 lines (3143 loc) · 112 KB
/
Model-class.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 helpers.R
#' @include helpers_jags.R
#' @include Model-validity.R
#' @include ModelParams-class.R
#' @include CrmPackClass-class.R
NULL
# GeneralModel-class ----
#' `GeneralModel`
#'
#' @description `r lifecycle::badge("stable")`
#'
#' [`GeneralModel`] is a general model class, from which all other specific
#' model-like classes inherit.
#'
#' @note The `datamodel` must obey the convention that the data input is
#' called exactly in the same way as in the corresponding data class.
#' All prior distributions for parameters should be contained in the
#' model function `priormodel`. The background is that this can
#' be used to simulate from the prior distribution, before obtaining any data.
#'
#' @slot datamodel (`function`)\cr a function representing the `JAGS` data model
#' specification.
#' @slot priormodel (`function`)\cr a function representing the `JAGS` prior
#' specification.
#' @slot modelspecs (`function`)\cr a function computing the list of the data
#' model and prior model specifications that are required to be specified
#' completely (e.g. prior parameters, reference dose, etc.), based on the data
#' slots that are required as arguments of this function.
#' Apart of data arguments, this function can be specified with one additional
#' (optional) argument `from_prior` of type `logical` and length one. This
#' `from_prior` flag can be used to differentiate the output of the `modelspecs`,
#' as its value is taken directly from the `from_prior` argument of the `mcmc`
#' method that invokes `modelspecs` function. That is, when `from_prior` is
#' `TRUE`, then only `priormodel` JAGS model is used (`datamodel` is not used)
#' by the `mcmc`, and hence `modelspecs` function should return all the parameters
#' that are required by the `priormodel` only. If the value of `from_prior` is
#' `FALSE`, then both JAGS models `datamodel` and `priormodel` are used in the
#' MCMC sampler, and hence `modelspecs` function should return all the parameters
#' required by both `datamodel` and `priormodel`.
#' @slot init (`function`)\cr a function computing the list of starting values
#' for parameters required to be initialized in the MCMC sampler, based on the
#' data slots that are required as arguments of this function.
#' @slot datanames (`character`)\cr the names of all data slots that are used
#' by `datamodel` JAGS function. No other names should be specified here.
#' @slot datanames_prior (`character`)\cr the names of all data slots that are
#' used by `priormodel` JAGS function. No other names should be specified here.
#' @slot sample (`character`)\cr names of all parameters from which you would
#' like to save the MCMC samples.
#'
#' @seealso [`ModelPseudo`].
#'
#' @aliases GeneralModel
#' @export
#'
.GeneralModel <- setClass(
Class = "GeneralModel",
slots = c(
datamodel = "function",
priormodel = "function",
modelspecs = "function",
init = "function",
datanames = "character",
datanames_prior = "character",
sample = "character"
),
prototype = prototype(
datamodel = I,
priormodel = I,
init = function() {
list()
}
),
contains = "CrmPackClass",
validity = v_general_model
)
## default constructor ----
#' @rdname GeneralModel-class
#' @note Typically, end users will not use the `.DefaultGeneralModel()` function.
#' @export
.DefaultGeneralModel <- function() {
stop(paste0("Class GeneralModel should not be instantiated directly. Please use one of its subclasses instead."))
}
# ModelLogNormal ----
## class ----
#' `ModelLogNormal`
#'
#' @description `r lifecycle::badge("stable")`
#'
#' [`ModelLogNormal`] is the class for a model with a reference dose and bivariate
#' normal prior on the model parameters `alpha0` and natural logarithm of `alpha1`,
#' i.e.: \deqn{(alpha0, log(alpha1)) ~ Normal(mean, cov),}. Transformations other
#' than `log`, e.g. identity, can be specified too in `priormodel` slot.
#' The parameter `alpha1` has a log-normal distribution by default to ensure
#' positivity of `alpha1` which further guarantees `exp(alpha1) > 1`.
#' The slots of this class contain the mean vector, the covariance and
#' precision matrices of the bivariate normal distribution, as well as the
#' reference dose. Note that the precision matrix is an inverse of the
#' covariance matrix in the `JAGS`.
#' All ("normal") model specific classes inherit from this class.
#'
#' @slot params (`ModelParamsNormal`)\cr bivariate normal prior parameters.
#' @slot ref_dose (`positive_number`)\cr the reference dose.
#'
#' @seealso [`ModelParamsNormal`], [`LogisticNormal`], [`LogisticLogNormal`],
#' [`LogisticLogNormalSub`], [`ProbitLogNormal`], [`ProbitLogNormalRel`].
#'
#' @aliases ModelLogNormal
#' @export
#'
.ModelLogNormal <- setClass(
Class = "ModelLogNormal",
contains = "GeneralModel",
slots = c(
params = "ModelParamsNormal",
ref_dose = "positive_number"
)
)
## constructor ----
#' @rdname ModelLogNormal-class
#'
#' @param mean (`numeric`)\cr the prior mean vector.
#' @param cov (`matrix`)\cr the prior covariance matrix. The precision matrix
#' `prec` is internally calculated as an inverse of `cov`.
#' @param ref_dose (`number`)\cr the reference dose \eqn{x*} (strictly positive
#' number).
#'
#' @export
#'
ModelLogNormal <- function(mean, cov, ref_dose = 1) {
params <- ModelParamsNormal(mean, cov)
.ModelLogNormal(
params = params,
ref_dose = positive_number(ref_dose),
priormodel = function() {
theta ~ dmnorm(mean, prec)
alpha0 <- theta[1]
alpha1 <- exp(theta[2])
},
modelspecs = function(from_prior) {
ms <- list(mean = params@mean, prec = params@prec)
if (!from_prior) {
ms$ref_dose <- ref_dose
}
ms
},
init = function() {
list(theta = c(0, 1))
},
datanames = c("nObs", "y", "x"),
sample = c("alpha0", "alpha1")
)
}
## default constructor ----
#' @rdname ModelLogNormal-class
#' @note Typically, end users will not use the `.DefaultModelLogNormal()` function.
#' @export
.DefaultModelLogNormal <- function() {
ModelLogNormal(mean = c(-0.85, 1), cov = matrix(c(1, -0.5, -0.5, 1), nrow = 2))
}
# LogisticNormal ----
## class ----
#' `LogisticNormal`
#'
#' @description `r lifecycle::badge("stable")`
#'
#' [`LogisticNormal`] is the class for the usual logistic regression model with
#' a bivariate normal prior on the intercept and slope.
#'
#' @details The covariate is the natural logarithm of the dose \eqn{x} divided by
#' the reference dose \eqn{x*}, i.e.:
#' \deqn{logit[p(x)] = alpha0 + alpha1 * log(x/x*),}
#' where \eqn{p(x)} is the probability of observing a DLT for a given dose \eqn{x}.
#' The prior \deqn{(alpha0, alpha1) ~ Normal(mean, cov).}
#'
#' @seealso [`ModelLogNormal`], [`LogisticLogNormal`], [`LogisticLogNormalSub`],
#' [`ProbitLogNormal`], [`ProbitLogNormalRel`], [`LogisticNormalMixture`].
#'
#' @aliases LogisticNormal
#' @export
#'
.LogisticNormal <- setClass(
Class = "LogisticNormal",
contains = "ModelLogNormal"
)
## constructor ----
#' @rdname LogisticNormal-class
#'
#' @inheritParams ModelLogNormal
#'
#' @export
#' @example examples/Model-class-LogisticNormal.R
#'
LogisticNormal <- function(mean, cov, ref_dose = 1) {
model_ln <- ModelLogNormal(mean = mean, cov = cov, ref_dose = ref_dose)
.LogisticNormal(
model_ln,
datamodel = function() {
for (i in 1:nObs) {
logit(p[i]) <- alpha0 + alpha1 * log(x[i] / ref_dose)
y[i] ~ dbern(p[i])
}
},
priormodel = function() {
theta ~ dmnorm(mean, prec)
alpha0 <- theta[1]
alpha1 <- theta[2]
}
)
}
## default constructor ----
#' @rdname LogisticNormal-class
#' @note Typically, end users will not use the `.DefaultLogisticNormal()` function.
#' @export
.DefaultLogisticNormal <- function() {
LogisticNormal(mean = c(-0.85, 1), cov = matrix(c(1, -0.5, -0.5, 1), nrow = 2))
}
# LogisticLogNormal ----
## class ----
#' `LogisticLogNormal`
#'
#' @description `r lifecycle::badge("stable")`
#'
#' [`LogisticLogNormal`] is the class for the usual logistic regression model
#' with a bivariate normal prior on the intercept and log slope.
#'
#' @details The covariate is the natural logarithm of the dose \eqn{x} divided by
#' the reference dose \eqn{x*}, i.e.:
#' \deqn{logit[p(x)] = alpha0 + alpha1 * log(x/x*),}
#' where \eqn{p(x)} is the probability of observing a DLT for a given dose \eqn{x}.
#' The prior \deqn{(alpha0, log(alpha1)) ~ Normal(mean, cov).}
#'
#' @seealso [`ModelLogNormal`], [`LogisticNormal`], [`LogisticLogNormalSub`],
#' [`ProbitLogNormal`], [`ProbitLogNormalRel`], [`LogisticLogNormalMixture`],
#' [`DALogisticLogNormal`].
#'
#' @aliases LogisticLogNormal
#' @export
#'
.LogisticLogNormal <- setClass(
Class = "LogisticLogNormal",
contains = "ModelLogNormal"
)
## constructor ----
#' @rdname LogisticLogNormal-class
#'
#' @inheritParams ModelLogNormal
#'
#' @export
#' @example examples/Model-class-LogisticLogNormal.R
#'
LogisticLogNormal <- function(mean, cov, ref_dose = 1) {
model_ln <- ModelLogNormal(mean = mean, cov = cov, ref_dose = ref_dose)
.LogisticLogNormal(
model_ln,
datamodel = function() {
for (i in 1:nObs) {
logit(p[i]) <- alpha0 + alpha1 * log(x[i] / ref_dose)
y[i] ~ dbern(p[i])
}
}
)
}
## default constructor ----
#' @rdname LogisticLogNormal-class
#' @note Typically, end users will not use the `.DefaultLogisticLogNormal()` function.
#' @export
.DefaultLogisticLogNormal <- function() {
LogisticLogNormal(
mean = c(-0.85, 1),
cov = matrix(c(1, -0.5, -0.5, 1), nrow = 2),
ref_dose = 50
)
}
# LogisticLogNormalSub ----
## class ----
#' `LogisticLogNormalSub`
#'
#' @description `r lifecycle::badge("stable")`
#'
#' [`LogisticLogNormalSub`] is the class for a standard logistic model with
#' bivariate (log) normal prior with subtractive dose standardization.
#'
#' @details The covariate is the dose \eqn{x} minus the reference dose \eqn{x*},
#' i.e.:
#' \deqn{logit[p(x)] = alpha0 + alpha1 * (x - x*),}
#' where \eqn{p(x)} is the probability of observing a DLT for a given dose \eqn{x}.
#' The prior \deqn{(alpha0, log(alpha1)) ~ Normal(mean, cov).}
#'
#' @slot params (`ModelParamsNormal`)\cr bivariate normal prior parameters.
#' @slot ref_dose (`number`)\cr the reference dose \eqn{x*}.
#'
#' @seealso [`LogisticNormal`], [`LogisticLogNormal`], [`ProbitLogNormal`],
#' [`ProbitLogNormalRel`].
#'
#' @aliases LogisticLogNormalSub
#' @export
#'
.LogisticLogNormalSub <- setClass(
Class = "LogisticLogNormalSub",
slots = c(
params = "ModelParamsNormal",
ref_dose = "numeric"
),
contains = "GeneralModel"
)
## constructor ----
#' @rdname LogisticLogNormalSub-class
#'
#' @param mean (`numeric`)\cr the prior mean vector.
#' @param cov (`matrix`)\cr the prior covariance matrix. The precision matrix
#' `prec` is internally calculated as an inverse of `cov`.
#' @param ref_dose (`number`)\cr the reference dose \eqn{x*}.
#'
#' @export
#' @example examples/Model-class-LogisticLogNormalSub.R
#'
LogisticLogNormalSub <- function(mean, cov, ref_dose = 0) {
params <- ModelParamsNormal(mean, cov)
.LogisticLogNormalSub(
params = params,
ref_dose = ref_dose,
datamodel = function() {
for (i in 1:nObs) {
logit(p[i]) <- alpha0 + alpha1 * (x[i] - ref_dose)
y[i] ~ dbern(p[i])
}
},
priormodel = function() {
theta ~ dmnorm(mean, prec)
alpha0 <- theta[1]
alpha1 <- exp(theta[2])
},
modelspecs = function(from_prior) {
ms <- list(mean = params@mean, prec = params@prec)
if (!from_prior) {
ms$ref_dose <- ref_dose
}
ms
},
init = function() {
list(theta = c(0, -20))
},
datanames = c("nObs", "y", "x"),
sample = c("alpha0", "alpha1")
)
}
## default constructor ----
#' @rdname LogisticLogNormalSub-class
#' @note Typically, end-users will not use the `.DefaultLogisticLogNormalSub()` function.
#' @export
.DefaultLogisticLogNormalSub <- function() {
LogisticLogNormalSub(
mean = c(-0.85, 1),
cov = matrix(c(1, -0.5, -0.5, 1), nrow = 2),
ref_dose = 50
)
}
# ProbitLogNormal ----
## class ----
#' `ProbitLogNormal`
#'
#' @description `r lifecycle::badge("stable")`
#'
#' [`ProbitLogNormal`] is the class for probit regression model with a
#' bivariate normal prior on the intercept and log slope.
#'
#' @details The covariate is the natural logarithm of dose \eqn{x} divided by a
#' reference dose \eqn{x*}, i.e.:
#' \deqn{probit[p(x)] = alpha0 + alpha1 * log(x/x*),}
#' where \eqn{p(x)} is the probability of observing a DLT for a given dose \eqn{x}.
#' The prior \deqn{(alpha0, log(alpha1)) ~ Normal(mean, cov).}
#'
#' @note This model is also used in the [`DualEndpoint`] classes, so this class
#' can be used to check the prior assumptions on the dose-toxicity model, even
#' when sampling from the prior distribution of the dual endpoint model is not
#' possible.
#'
#' @seealso [`ModelLogNormal`], [`LogisticNormal`], [`LogisticLogNormal`],
#' [`LogisticLogNormalSub`], [`ProbitLogNormalRel`].
#'
#' @aliases ProbitLogNormalLogDose
#' @export
#'
.ProbitLogNormal <- setClass(
Class = "ProbitLogNormal",
contains = "ModelLogNormal"
)
## constructor ----
#' @rdname ProbitLogNormal-class
#'
#' @inheritParams ModelLogNormal
#'
#' @export
#' @example examples/Model-class-ProbitLogNormal.R
#'
ProbitLogNormal <- function(mean, cov, ref_dose = 1) {
model_ln <- ModelLogNormal(mean = mean, cov = cov, ref_dose = ref_dose)
.ProbitLogNormal(
model_ln,
datamodel = function() {
for (i in 1:nObs) {
probit(p[i]) <- alpha0 + alpha1 * log(x[i] / ref_dose)
y[i] ~ dbern(p[i])
}
}
)
}
## default constructor ----
#' @rdname ProbitLogNormal-class
#' @note Typically, end users will not use the `.DefaultProbitLogNormal()` function.
#' @export
.DefaultProbitLogNormal <- function() {
ProbitLogNormal(
mean = c(-0.85, 1),
cov = matrix(c(1, -0.5, -0.5, 1), nrow = 2),
ref_dose = 7.2
)
}
# ProbitLogNormalRel ----
## class ----
#' `ProbitLogNormalRel`
#'
#' @description `r lifecycle::badge("stable")`
#'
#' [`ProbitLogNormalRel`] is the class for probit regression model with a bivariate
#' normal prior on the intercept and log slope.
#'
#' @details The covariate is the dose \eqn{x} divided by a reference dose \eqn{x*},
#' i.e.:
#' \deqn{probit[p(x)] = alpha0 + alpha1 * x/x*,}
#' where \eqn{p(x)} is the probability of observing a DLT for a given dose \eqn{x}.
#' The prior \deqn{(alpha0, log(alpha1)) ~ Normal(mean, cov).}
#'
#' @note This model is also used in the [`DualEndpoint`] classes, so this class
#' can be used to check the prior assumptions on the dose-toxicity model, even
#' when sampling from the prior distribution of the dual endpoint model is not
#' possible.
#'
#' @seealso [`ModelLogNormal`], [`LogisticNormal`], [`LogisticLogNormal`],
#' [`LogisticLogNormalSub`], [`ProbitLogNormal`].
#'
#' @aliases ProbitLogNormalRel
#' @export
#'
.ProbitLogNormalRel <- setClass(
Class = "ProbitLogNormalRel",
contains = "ModelLogNormal"
)
## constructor ----
#' @rdname ProbitLogNormalRel-class
#'
#' @inheritParams ModelLogNormal
#'
#' @export
#' @example examples/Model-class-ProbitLogNormalRel.R
#'
ProbitLogNormalRel <- function(mean, cov, ref_dose = 1) {
model_ln <- ModelLogNormal(mean = mean, cov = cov, ref_dose = ref_dose)
.ProbitLogNormalRel(
model_ln,
datamodel = function() {
for (i in 1:nObs) {
probit(p[i]) <- alpha0 + alpha1 * (x[i] / ref_dose)
y[i] ~ dbern(p[i])
}
}
)
}
## default constructor ----
#' @rdname ProbitLogNormalRel-class
#' @note Typically, end users will not use the `.DefaultProbitLogNormalRel()` function.
#' @export
.DefaultProbitLogNormalRel <- function() {
ProbitLogNormalRel(mean = c(-0.85, 1), cov = matrix(c(1, -0.5, -0.5, 1), nrow = 2))
}
# LogisticLogNormalGrouped ----
## class ----
#' `LogisticLogNormalGrouped`
#'
#' @description `r lifecycle::badge("experimental")`
#'
#' [`LogisticLogNormalGrouped`] is the class for a logistic regression model
#' for both the mono and the combo arms of the simultaneous dose escalation
#' design.
#'
#' @details The continuous covariate is the natural logarithm of the dose \eqn{x} divided by
#' the reference dose \eqn{x*} as in [`LogisticLogNormal`]. In addition,
#' \eqn{I_c} is a binary indicator covariate which is 1 for the combo arm and 0 for the mono arm.
#' The model is then defined as:
#' \deqn{logit[p(x)] = (alpha0 + I_c * delta0) + (alpha1 + I_c * delta1) * log(x / x*),}
#' where \eqn{p(x)} is the probability of observing a DLT for a given dose \eqn{x},
#' and `delta0` and `delta1` are the differences in the combo arm compared to the mono intercept
#' and slope parameters `alpha0` and `alpha1`.
#' The prior is defined as \deqn{(alpha0, log(delta0), log(alpha1), log(delta1)) ~ Normal(mean, cov).}
#'
#' @seealso [`ModelLogNormal`], [`LogisticLogNormal`].
#'
#' @aliases LogisticLogNormalGrouped
#' @export
#'
.LogisticLogNormalGrouped <- setClass(
Class = "LogisticLogNormalGrouped",
contains = "ModelLogNormal"
)
## constructor ----
#' @rdname LogisticLogNormalGrouped-class
#'
#' @inheritParams ModelLogNormal
#'
#' @export
#' @example examples/Model-class-LogisticLogNormalGrouped.R
#'
LogisticLogNormalGrouped <- function(mean, cov, ref_dose = 1) {
params <- ModelParamsNormal(mean, cov)
.LogisticLogNormalGrouped(
params = params,
ref_dose = positive_number(ref_dose),
priormodel = function() {
theta ~ dmnorm(mean, prec)
alpha0 <- theta[1]
delta0 <- exp(theta[2])
alpha1 <- exp(theta[3])
delta1 <- exp(theta[4])
},
datamodel = function() {
for (i in 1:nObs) {
logit(p[i]) <- (alpha0 + is_combo[i] * delta0) +
(alpha1 + is_combo[i] * delta1) * log(x[i] / ref_dose)
y[i] ~ dbern(p[i])
}
},
modelspecs = function(group, from_prior) {
ms <- list(
mean = params@mean,
prec = params@prec
)
if (!from_prior) {
ms$ref_dose <- ref_dose
ms$is_combo <- as.integer(group == "combo")
}
ms
},
init = function() {
list(theta = c(0, 1, 1, 1))
},
datanames = c("nObs", "y", "x"),
sample = c("alpha0", "delta0", "alpha1", "delta1")
)
}
## default constructor ----
#' @rdname LogisticLogNormalGrouped-class
#' @note Typically, end users will not use the `.DefaultLogisticLogNormalGrouped()` function.
#' @export
.DefaultLogisticLogNormalGrouped <- function() {
LogisticLogNormalGrouped(
mean = rep(0, 4),
cov = diag(rep(1, 4)),
)
}
# LogisticKadane ----
## class ----
#' `LogisticKadane`
#'
#' @description `r lifecycle::badge("stable")`
#'
#' [`LogisticKadane`] is the class for the logistic model in the parametrization
#' of Kadane et al. (1980).
#'
#' @details Let `rho0 = p(xmin)` be the probability of a DLT at the minimum dose
#' `xmin`, and let `gamma` be the dose with target toxicity probability `theta`,
#' i.e. \eqn{p(gamma) = theta}. Then it can easily be shown that the logistic
#' regression model has intercept
#' \deqn{[gamma * logit(rho0) - xmin * logit(theta)] / [gamma - xmin]}
#' and slope
#' \deqn{[logit(theta) - logit(rho0)] / [gamma - xmin].}
#'
#' The priors are \deqn{gamma ~ Unif(xmin, xmax).} and
#' \deqn{rho0 ~ Unif(0, theta).}
#'
#' @note The slots of this class, required for creating the model, are the target
#' toxicity, as well as the minimum and maximum of the dose range. Note that
#' these can be different from the minimum and maximum of the dose grid in the
#' data later on.
#'
#' @slot theta (`proportion`)\cr the target toxicity probability.
#' @slot xmin (`number`)\cr the minimum of the dose range.
#' @slot xmax (`number`)\cr the maximum of the dose range.
#'
#' @seealso [`ModelLogNormal`]
#'
#' @aliases LogisticKadane
#' @export
#'
.LogisticKadane <- setClass(
Class = "LogisticKadane",
contains = "GeneralModel",
slots = c(
theta = "numeric",
xmin = "numeric",
xmax = "numeric"
),
prototype = prototype(
theta = 0.3,
xmin = 0.1,
xmax = 1
),
validity = v_model_logistic_kadane
)
## constructor ----
#' @rdname LogisticKadane-class
#'
#' @param theta (`proportion`)\cr the target toxicity probability.
#' @param xmin (`number`)\cr the minimum of the dose range.
#' @param xmax (`number`)\cr the maximum of the dose range.
#'
#' @export
#' @example examples/Model-class-LogisticKadane.R
#'
LogisticKadane <- function(theta, xmin, xmax) {
.LogisticKadane(
theta = theta,
xmin = xmin,
xmax = xmax,
datamodel = function() {
for (i in 1:nObs) {
logit(p[i]) <- (1 / (gamma - xmin)) *
(gamma * logit(rho0) - xmin * logit(theta) + x[i] * (logit(theta) - logit(rho0)))
y[i] ~ dbern(p[i])
}
},
priormodel = function() {
rho0 ~ dunif(0, theta)
gamma ~ dunif(xmin, xmax)
},
modelspecs = function() {
list(theta = theta, xmin = xmin, xmax = xmax)
},
init = function() {
list(rho0 = theta / 10, gamma = (xmax - xmin) / 2)
},
datanames = c("nObs", "y", "x"),
sample = c("rho0", "gamma")
)
}
## default constructor ----
#' @rdname LogisticKadane-class
#' @note Typically, end-users will not use the `.DefaultLogisticKadane()` function.
#' @export
.DefaultLogisticKadane <- function() {
LogisticKadane(theta = 0.33, xmin = 1, xmax = 200)
}
# LogisticKadaneBetaGamma ----
## class ----
#' `LogisticKadaneBetaGamma`
#'
#' @description `r lifecycle::badge("experimental")`
#'
#' [`LogisticKadaneBetaGamma`] is the class for the logistic model in the parametrization
#' of Kadane et al. (1980), using a beta and a gamma distribution as the model priors.
#'
#' @details Let `rho0 = p(xmin)` be the probability of a DLT at the minimum dose
#' `xmin`, and let `gamma` be the dose with target toxicity probability `theta`,
#' i.e. \eqn{p(gamma) = theta}. Then it can easily be shown that the logistic
#' regression model has intercept
#' \deqn{[gamma * logit(rho0) - xmin * logit(theta)] / [gamma - xmin]}
#' and slope
#' \deqn{[logit(theta) - logit(rho0)] / [gamma - xmin].}
#'
#' The prior for `gamma`, is \deqn{gamma ~ Gamma(shape, rate).}.
#' The prior for `rho0 = p(xmin)`, is \deqn{rho0 ~ Beta(alpha, beta).}
#'
#' @note The slots of this class, required for creating the model, are the same
#' as in the `LogisticKadane` class. In addition, the shape parameters of the
#' Beta prior distribution of `rho0` and the shape and rate parameters of the
#' Gamma prior distribution of `gamma`, are required for creating the prior model.
#'
#' @slot theta (`proportion`)\cr the target toxicity probability.
#' @slot xmin (`number`)\cr the minimum of the dose range.
#' @slot xmax (`number`)\cr the maximum of the dose range.
#' @slot alpha (`number`)\cr the first shape parameter of the Beta prior distribution
#' of `rho0 = p(xmin)` the probability of a DLT at the minimum dose `xmin`.
#' @slot beta (`number`)\cr the second shape parameter of the Beta prior distribution
#' of `rho0 = p(xmin)` the probability of a DLT at the minimum dose `xmin`.
#' @slot shape (`number`)\cr the shape parameter of the Gamma prior distribution
#' of `gamma` the dose with target toxicity probability `theta`.
#' @slot rate (`number`)\cr the rate parameter of the Gamma prior distribution
#' of `gamma` the dose with target toxicity probability `theta`.
#'
#' @seealso [`ModelLogNormal`], [`LogisticKadane`].
#'
#' @aliases LogisticKadaneBetaGamma
#' @export
#'
.LogisticKadaneBetaGamma <- setClass(
Class = "LogisticKadaneBetaGamma",
contains = "LogisticKadane",
slots = c(
alpha = "numeric",
beta = "numeric",
shape = "numeric",
rate = "numeric"
),
prototype = prototype(
theta = 0.3,
xmin = 0.1,
xmax = 1,
alpha = 1,
beta = 0.5,
shape = 1.2,
rate = 2.5
),
validity = v_model_logistic_kadane_beta_gamma
)
## constructor ----
#' @rdname LogisticKadaneBetaGamma-class
#'
#' @inheritParams LogisticKadane
#'
#' @param alpha (`number`)\cr the first shape parameter of the Beta prior distribution
#' `rho0 = p(xmin)` the probability of a DLT at the minimum dose `xmin`.
#' @param beta (`number`)\cr the second shape parameter of the Beta prior distribution
#' `rho0 = p(xmin)` the probability of a DLT at the minimum dose `xmin`.
#' @param shape (`number`)\cr the shape parameter of the Gamma prior distribution
#' `gamma` the dose with target toxicity probability `theta`.
#' @param rate (`number`)\cr the rate parameter of the Gamma prior distribution
#' `gamma` the dose with target toxicity probability `theta`.
#'
#' @export
#' @example examples/Model-class-LogisticKadaneBetaGamma.R
#'
LogisticKadaneBetaGamma <- function(theta, xmin, xmax, alpha, beta, shape, rate) {
model_lk <- LogisticKadane(theta = theta, xmin = xmin, xmax = xmax)
.LogisticKadaneBetaGamma(
model_lk,
alpha = alpha,
beta = beta,
shape = shape,
rate = rate,
priormodel = function() {
rho0 ~ dbeta(alpha, beta)
gamma ~ dgamma(shape, rate)
lowestdose <- xmin
highestdose <- xmax
DLTtarget <- theta
},
modelspecs = function() {
list(
theta = theta,
xmin = xmin,
xmax = xmax,
alpha = alpha,
beta = beta,
shape = shape,
rate = rate
)
}
)
}
## default constructor ----
#' @rdname LogisticKadaneBetaGamma-class
#' @note Typically, end users will not use the `.Default()` function.
#' @export
.DefaultLogisticKadaneBetaGamma <- function() {
LogisticKadaneBetaGamma(
theta = 0.3,
xmin = 0,
xmax = 7,
alpha = 1,
beta = 19,
shape = 0.5625,
rate = 0.125
)
}
# LogisticNormalMixture ----
## class ----
#' `LogisticNormalMixture`
#'
#' @description `r lifecycle::badge("stable")`
#'
#' [`LogisticNormalMixture`] is the class for standard logistic regression model
#' with a mixture of two bivariate normal priors on the intercept and slope parameters.
#'
#' @details The covariate is the natural logarithm of the dose \eqn{x} divided by
#' the reference dose \eqn{x*}, i.e.:
#' \deqn{logit[p(x)] = alpha0 + alpha1 * log(x/x*),}
#' where \eqn{p(x)} is the probability of observing a DLT for a given dose \eqn{x}.
#' The prior
#' \deqn{(alpha0, alpha1) ~ w * Normal(mean1, cov1) + (1 - w) * Normal(mean2, cov2).}
#' The weight w for the first component is assigned a beta prior `B(a, b)`.
#'
#' @note The weight of the two normal priors is a model parameter, hence it is a
#' flexible mixture. This type of prior is often used with a mixture of a minimal
#' informative and an informative component, in order to make the CRM more robust
#' to data deviations from the informative component.
#'
#' @slot comp1 (`ModelParamsNormal`)\cr bivariate normal prior specification of
#' the first component.
#' @slot comp2 (`ModelParamsNormal`)\cr bivariate normal prior specification of
#' the second component.
#' @slot weightpar (`numeric`)\cr the beta parameters for the weight of the
#' first component. It must a be a named vector of length 2 with names `a` and
#' `b` and with strictly positive values.
#' @slot ref_dose (`positive_number`)\cr the reference dose.
#'
#' @seealso [`ModelParamsNormal`], [`ModelLogNormal`],
#' [`LogisticNormalFixedMixture`], [`LogisticLogNormalMixture`].
#'
#' @aliases LogisticNormalMixture
#' @export
#'
.LogisticNormalMixture <- setClass(
Class = "LogisticNormalMixture",
contains = "GeneralModel",
slots = c(
comp1 = "ModelParamsNormal",
comp2 = "ModelParamsNormal",
weightpar = "numeric",
ref_dose = "numeric"
),
prototype = prototype(
comp1 = ModelParamsNormal(mean = c(0, 1), cov = diag(2)),
comp2 = ModelParamsNormal(mean = c(-1, 1), cov = diag(2)),
weightpar = c(a = 1, b = 1),
ref_dose = 1
),
validity = v_model_logistic_normal_mix
)
## constructor ----
#' @rdname LogisticNormalMixture-class
#'
#' @param comp1 (`ModelParamsNormal`)\cr bivariate normal prior specification of
#' the first component. See [`ModelParamsNormal`] for more details.
#' @param comp2 (`ModelParamsNormal`)\cr bivariate normal prior specification of
#' the second component. See [`ModelParamsNormal`] for more details.
#' @param weightpar (`numeric`)\cr the beta parameters for the weight of the
#' first component. It must a be a named vector of length 2 with names `a` and
#' `b` and with strictly positive values.
#' @param ref_dose (`number`)\cr the reference dose \eqn{x*}
#' (strictly positive number).
#'
#' @export
#' @example examples/Model-class-LogisticNormalMixture.R
#'
LogisticNormalMixture <- function(comp1,
comp2,
weightpar,
ref_dose) {
assert_number(ref_dose)
.LogisticNormalMixture(
comp1 = comp1,
comp2 = comp2,
weightpar = weightpar,
ref_dose = ref_dose,
datamodel = function() {
# The logistic likelihood - the same as for non-mixture case.
for (i in 1:nObs) {
logit(p[i]) <- alpha0 + alpha1 * log(x[i] / ref_dose)
y[i] ~ dbern(p[i])
}
},
priormodel = function() {
w ~ dbeta(weightpar[1], weightpar[2])
wc <- 1 - w
comp0 ~ dbern(wc)
comp <- comp0 + 1
# Conditional on the component index "comp", which is 1 or 2.
# comp = 1 with probability "w" and comp = 2 with probability "1 - w".
theta ~ dmnorm(mean[1:2, comp], prec[1:2, 1:2, comp])
alpha0 <- theta[1]
alpha1 <- theta[2]
},
modelspecs = function(from_prior) {
ms <- list(
mean = cbind(comp1@mean, comp2@mean),
prec = array(data = c(comp1@prec, comp2@prec), dim = c(2, 2, 2)),
weightpar = weightpar
)
if (!from_prior) {
ms$ref_dose <- ref_dose
}
ms
},
init = function() {
list(theta = c(0, 1))
},
datanames = c("nObs", "y", "x"),
sample = c("alpha0", "alpha1", "w")
)
}
## default constructor ----
#' @rdname LogisticNormalMixture-class
#' @note Typically, end-users will not use the `.DefaultLogisticNormalMixture()` function.
#' @export
.DefaultLogisticNormalMixture <- function() { # nolint
LogisticNormalMixture(
comp1 = ModelParamsNormal(
mean = c(-0.85, 1),
cov = matrix(c(1, -0.5, -0.5, 1), nrow = 2)
),
comp2 = ModelParamsNormal(
mean = c(1, 1.5),
cov = matrix(c(1.2, -0.45, -0.45, 0.6), nrow = 2)
),
weightpar = c(a = 1, b = 1),
ref_dose = 50
)
}
# LogisticNormalFixedMixture ----
## class ----
#' `LogisticNormalFixedMixture`
#'
#' @description `r lifecycle::badge("stable")`
#'
#' [`LogisticNormalFixedMixture`] is the class for standard logistic regression