|
1 | | -require(rjags) |
2 | | - |
3 | | -test_that("Test dose selection at interim", { |
4 | | - nsim <- 1000 |
5 | | - alpha <- 0.05 |
6 | | - |
| 1 | +test_that("Dose selection at interim (frequentist)", { |
| 2 | + nsim <- 1000 |
| 3 | + alpha <- 0.05 |
7 | 4 | trt_ref <- "Placebo" |
8 | | - n_pln <- 20 + 65*2 |
| 5 | + trt_act <- c("Drug A", "Drug B") |
| 6 | + n_pln <- 20 + 65*2 |
9 | 7 | th.fut <- 0.2 |
10 | 8 | th.eff <- 0.9 |
11 | | - th.prom <- 0.5 |
12 | 9 |
|
| 10 | + rejectH0_freq <- matrix(FALSE, nrow = nsim, ncol = length(trt_act), |
| 11 | + dimnames = list(NULL, trt_act)) |
13 | 12 |
|
14 | | - rejectH0 <- data.frame(freq = logical(), bayes = logical()) |
| 13 | + for (i in seq_len(nsim)) { |
| 14 | + dat_int <- sim_rct_normal( |
| 15 | + n = 20*3, |
| 16 | + mean = c("Placebo" = 0, "Drug A" = 0, "Drug B" = 0), |
| 17 | + sd = c("Placebo" = 1, "Drug A" = 1, "Drug B" = 1) |
| 18 | + ) |
15 | 19 |
|
16 | | - for (i in 1:nsim) { |
| 20 | + res <- dose_selection( |
| 21 | + dat_int = dat_int, |
| 22 | + n_pln = n_pln, |
| 23 | + trt_ref = trt_ref, |
| 24 | + trt_active = trt_act, |
| 25 | + gamma = 1 - alpha/2, |
| 26 | + th.fut = th.fut, |
| 27 | + th.eff = th.eff, |
| 28 | + method = "mcmc" |
| 29 | + ) |
17 | 30 |
|
18 | | - # Simulate trial data at interim stage |
19 | | - dat_int <- sim_rct_normal(n = 20*3, |
20 | | - mean = c("Placebo" = 0, "Drug A" = 0, "Drug B" = 0), |
21 | | - sd = c("Placebo" = 1, "Drug A" = 1, "Drug B" = 1)) |
| 31 | + rej <- res$rejectH0 |
| 32 | + # normalize to named logical vector over trt_act |
| 33 | + if (is.logical(rej) && length(rej) == 1L) rej <- setNames(rep(rej, length(trt_act)), trt_act) |
| 34 | + if (is.null(names(rej))) names(rej) <- trt_act |
| 35 | + rejectH0_freq[i, ] <- as.logical(rej[trt_act]) |
| 36 | + } |
22 | 37 |
|
23 | | - resultf <- dose_selection(dat_int = dat_int, |
24 | | - n_pln = n_pln, |
25 | | - trt_ref = "Placebo", |
26 | | - trt_active = c("Drug A", "Drug B"), |
27 | | - gamma = 1 - alpha/2, |
28 | | - th.fut = th.fut, th.eff = th.eff, |
29 | | - method = "mcmc") |
| 38 | + for (arm in trt_act) { |
| 39 | + ci <- mc_error_proportion(x = sum(rejectH0_freq[, arm]), |
| 40 | + n = nsim, level = 1 - alpha) |
| 41 | + expect_true(alpha >= ci$lower & alpha <= ci$upper, |
| 42 | + info = paste0("Type-I error not maintained (freq, ", arm, "): ", |
| 43 | + round(ci$lower, 4), "–", round(ci$upper, 4))) |
| 44 | + } |
| 45 | +}) |
30 | 46 |
|
31 | | - resultb <- dose_selection(dat_int = dat_int, |
32 | | - n_pln = n_pln, |
33 | | - trt_ref = "Placebo", |
34 | | - trt_active = c("Drug A", "Drug B"), |
35 | | - gamma = 1 - alpha/2, |
36 | | - th.fut = th.fut, th.eff = th.eff, |
37 | | - method = "bayes") |
| 47 | +test_that("Dose selection at interim (Bayesian)", { |
| 48 | + skip_if_not_installed("rjags") |
38 | 49 |
|
39 | | - rejectH0 <- rejectH0 %>% add_row(data.frame(freq = resultf$rejectH0, bayes = resultb$rejectH0)) |
40 | | - } |
| 50 | + nsim <- 1000 |
| 51 | + alpha <- 0.05 |
| 52 | + trt_ref <- "Placebo" |
| 53 | + trt_act <- c("Drug A", "Drug B") |
| 54 | + n_pln <- 20 + 65*2 |
| 55 | + th.fut <- 0.2 |
| 56 | + th.eff <- 0.9 |
| 57 | + |
| 58 | + rejectH0_bayes <- matrix(FALSE, nrow = nsim, ncol = length(trt_act), |
| 59 | + dimnames = list(NULL, trt_act)) |
41 | 60 |
|
42 | | - # Test whether type-I error is controlled |
43 | | - treat.ci <- mc_error_proportion(x = sum(rejectH0$freq), n = nsim, level = 1 - alpha) |
44 | | - expect_true(alpha >= treat.ci$lower & alpha <= treat.ci$upper, |
45 | | - info = paste0("Type-I error for treat is not maintained for frequentist analysis (", |
46 | | - round(treat.ci$lower,4), "; ", round(treat.ci$upper,4), ")")) |
| 61 | + for (i in seq_len(nsim)) { |
| 62 | + dat_int <- sim_rct_normal( |
| 63 | + n = 20*3, |
| 64 | + mean = c("Placebo" = 0, "Drug A" = 0, "Drug B" = 0), |
| 65 | + sd = c("Placebo" = 1, "Drug A" = 1, "Drug B" = 1) |
| 66 | + ) |
47 | 67 |
|
48 | | - treat.ci <- mc_error_proportion(x = sum(rejectH0$bayes), n = nsim, level = 1 - alpha) |
49 | | - expect_true(alpha >= treat.ci$lower & alpha <= treat.ci$upper, |
50 | | - info = paste0("Type-I error for treat is not maintained for Bayesian analysis (", |
51 | | - round(treat.ci$lower,4), "; ", round(treat.ci$upper,4), ")")) |
| 68 | + res <- dose_selection( |
| 69 | + dat_int = dat_int, |
| 70 | + n_pln = n_pln, |
| 71 | + trt_ref = trt_ref, |
| 72 | + trt_active = trt_act, |
| 73 | + gamma = 1 - alpha/2, |
| 74 | + th.fut = th.fut, |
| 75 | + th.eff = th.eff, |
| 76 | + method = "bayes" |
| 77 | + ) |
52 | 78 |
|
| 79 | + rej <- res$rejectH0 |
| 80 | + if (is.logical(rej) && length(rej) == 1L) rej <- setNames(rep(rej, length(trt_act)), trt_act) |
| 81 | + if (is.null(names(rej))) names(rej) <- trt_act |
| 82 | + rejectH0_bayes[i, ] <- as.logical(rej[trt_act]) |
| 83 | + } |
53 | 84 |
|
| 85 | + for (arm in trt_act) { |
| 86 | + ci <- mc_error_proportion(x = sum(rejectH0_bayes[, arm]), |
| 87 | + n = nsim, level = 1 - alpha) |
| 88 | + expect_true(alpha >= ci$lower & alpha <= ci$upper, |
| 89 | + info = paste0("Type-I error not maintained (Bayes, ", arm, "): ", |
| 90 | + round(ci$lower, 4), "–", round(ci$upper, 4))) |
| 91 | + } |
54 | 92 | }) |
| 93 | + |
0 commit comments