## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(simFastBOIN)

## -----------------------------------------------------------------------------
scenarios <- list(
  "Scenario 1" = list(p_true = c(0.05, 0.10, 0.20, 0.30, 0.45),
                      p_resp = c(0.10, 0.20, 0.35, 0.40, 0.45)),
  "Scenario 2" = list(p_true = c(0.10, 0.30, 0.45, 0.55, 0.65),
                      p_resp = c(0.20, 0.35, 0.40, 0.45, 0.50)),
  "Scenario 3" = list(p_true = c(0.02, 0.05, 0.08, 0.15, 0.30),
                      p_resp = c(0.05, 0.10, 0.20, 0.30, 0.40))
)
truth <- oc_scenarios(lapply(scenarios, function(s) s$p_true),
                      target = 0.30)
truth

## ----scenarios-plot, fig.width = 7, fig.height = 3.5, eval = requireNamespace("ggplot2", quietly = TRUE)----
plot(truth)

## -----------------------------------------------------------------------------
design <- list(target = 0.30, scenarios = scenarios, n_cohort = 10,
               cohort_size = 3, n_trials = 1000, seed = 123)
timing <- list(window = 1, accrual_rate = 2)

boin <- do.call(sim_boin_multi, design)
iboin <- do.call(sim_iboin_multi,
                 c(design, list(skeleton = c(0.04, 0.10, 0.19, 0.30, 0.42),
                                prior_ess = 3)))
tite <- do.call(sim_tite_boin_multi, c(design, timing))
bf <- do.call(sim_bf_boin_multi, c(design, timing))
three <- lapply(scenarios, function(s) oc_3p3(p_true = s$p_true))

## -----------------------------------------------------------------------------
results <- oc_results(BOIN = boin, iBOIN = iboin, "TITE-BOIN" = tite,
                      "BF-BOIN" = bf, "3+3" = three)
results

## ----results-plot, fig.width = 7, fig.height = 4.5, eval = requireNamespace("ggplot2", quietly = TRUE)----
plot(results)

## -----------------------------------------------------------------------------
comparison <- oc_compare(BOIN = boin, iBOIN = iboin, "TITE-BOIN" = tite,
                         "BF-BOIN" = bf, "3+3" = three, truth = truth)
comparison

## ----compare-plot, fig.width = 7, fig.height = 6, eval = requireNamespace("ggplot2", quietly = TRUE)----
plot(comparison)

## ----include = FALSE----------------------------------------------------------
pick <- function(x, design, scenario, column = "pct_correct") {
  x[[column]][x$design == design & x$scenario == scenario]
}

## -----------------------------------------------------------------------------
grade_scenarios <- list(
  "Scenario 1" = rbind(c(0.83, 0.75, 0.62, 0.51, 0.34, 0.19),
                       c(0.12, 0.15, 0.18, 0.19, 0.16, 0.11),
                       c(0.04, 0.07, 0.11, 0.14, 0.15, 0.11),
                       c(0.01, 0.03, 0.09, 0.16, 0.35, 0.59)),
  "Scenario 7" = rbind(c(0.78, 0.58, 0.50, 0.40, 0.30, 0.16),
                       c(0.14, 0.18, 0.18, 0.17, 0.15, 0.09),
                       c(0.00, 0.00, 0.00, 0.00, 0.00, 0.00),
                       c(0.08, 0.24, 0.32, 0.43, 0.55, 0.75))
)
grade_design <- list(target = 0.47 / 1.5, scenarios = grade_scenarios,
                     score = c(0, 0.5, 1, 1.5), n_cohort = 10,
                     cohort_size = 3, n_earlystop = 100, n_trials = 1000,
                     seed = 123)
posterior <- do.call(sim_gboin_multi, grade_design)
count <- do.call(sim_gboin_multi, c(grade_design, list(elim_rule = "count")))

truth_grade <- oc_scenarios(lapply(posterior$results,
                                   function(r) r$p_true),
                            target = 0.47 / 1.5, tox_limit = 0.35)
oc_compare("gBOIN, posterior" = posterior, "gBOIN, count" = count,
           truth = truth_grade)

## -----------------------------------------------------------------------------
g_design <- list(target = 0.47 / 1.5, target_resp = 1.75 / 3,
                 p_grade = grade_scenarios[["Scenario 1"]],
                 p_resp_grade = rbind(c(0.60, 0.50, 0.30, 0.15, 0.10, 0.05),
                                      c(0.30, 0.20, 0.20, 0.15, 0.15, 0.15),
                                      c(0.05, 0.20, 0.35, 0.20, 0.20, 0.20),
                                      c(0.05, 0.10, 0.15, 0.50, 0.55, 0.60)),
                 score = c(0, 0.5, 1, 1.5), score_resp = c(0, 0.25, 1, 3),
                 n_cohort = 12, cohort_size = 3, n_trials = 1000,
                 seed = 123)
g_et <- do.call(sim_gboin_et, g_design)
g_tite <- do.call(sim_tite_gboin_et,
                  c(g_design, list(window = 30, resp_window = 60,
                                   accrual_rate = 1 / 10)))

truth_g <- oc_scenarios(
  list("Scenario 3" = list(p_true = g_et$p_true, p_resp = g_et$p_resp)),
  target = 0.47 / 1.5, tox_limit = 0.35, true_obd = list("Scenario 3" = 4)
)
oc_compare("gBOIN-ET" = list("Scenario 3" = g_et),
           "TITE-gBOIN-ET" = list("Scenario 3" = g_tite), truth = truth_g)

## -----------------------------------------------------------------------------
obd_scenarios <- list(
  "Scenario 1" = list(p_true = c(0.02, 0.15, 0.30, 0.45, 0.60),
                      p_resp = c(0.20, 0.65, 0.65, 0.65, 0.65)),
  "Scenario 2" = list(p_true = c(0.03, 0.08, 0.15, 0.28, 0.40),
                      p_resp = c(0.10, 0.22, 0.60, 0.60, 0.60)),
  "Scenario 3" = list(p_true = c(0.05, 0.15, 0.30, 0.45, 0.60),
                      p_resp = c(0.08, 0.46, 0.25, 0.20, 0.10))
)
truth_obd <- oc_scenarios(obd_scenarios, target = 0.30, target_resp = 0.20,
                          psi00 = 30, psi11 = 50)
truth_obd

obd_design <- list(scenarios = obd_scenarios, n_cohort = 18,
                   cohort_size = 3, n_trials = 1000, seed = 123)
boin12 <- do.call(sim_boin12_multi,
                  c(obd_design, list(target = 0.30, target_resp = 0.20,
                                     psi00 = 30, psi11 = 50)))
uboin <- do.call(sim_uboin_multi,
                 c(obd_design, list(tox_upper = 0.30, resp_lower = 0.20)))
boin_et <- do.call(sim_boin_et_multi,
                   c(obd_design, list(target = 0.30, target_resp = 0.50,
                                      resp_cor = 0)))

comparison_obd <- oc_compare(BOIN12 = boin12, "U-BOIN" = uboin,
                             "BOIN-ET" = boin_et, truth = truth_obd)
comparison_obd

## ----compare-obd-plot, fig.width = 7, fig.height = 6, eval = requireNamespace("ggplot2", quietly = TRUE)----
plot(comparison_obd)

## -----------------------------------------------------------------------------
comb_scenarios <- list(
  "Kakizume 1" = list(
    p_true = matrix(c(0.05, 0.10, 0.10, 0.30, 0.30, 0.50), nrow = 2),
    p_resp = matrix(c(0.10, 0.25, 0.25, 0.60, 0.60, 0.65), nrow = 2)
  ),
  "Lu 16" = list(
    p_true = matrix(c(0.05, 0.10, 0.15, 0.15, 0.20, 0.45), nrow = 2),
    p_resp = matrix(c(0.10, 0.20, 0.30, 0.40, 0.50, 0.60), nrow = 2)
  )
)
truth_comb <- oc_scenarios(comb_scenarios, target = 0.30, target_resp = 0.20)
truth_comb

comb_design <- list(target = 0.30, scenarios = comb_scenarios, n_cohort = 8,
                    cohort_size = 3, n_trials = 1000, seed = 123)
comb <- do.call(sim_comb_boin_multi, comb_design)
comb12 <- do.call(sim_comb_boin12_multi,
                  c(comb_design, list(target_resp = 0.20)))
etc <- do.call(sim_boin_etc_multi, c(comb_design, list(target_resp = 0.60)))

oc_compare("BOIN combination" = comb, "Comb-BOIN12" = comb12,
           "BOIN-ETC" = etc, truth = truth_comb)

## ----comb-results-plot, fig.width = 7, fig.height = 6, eval = requireNamespace("ggplot2", quietly = TRUE)----
plot(oc_results("BOIN combination" = comb, "Comb-BOIN12" = comb12,
                "BOIN-ETC" = etc))

## -----------------------------------------------------------------------------
print(results[results$scenario == "Scenario 2", ],
      metric = c("sel_percent", "n_tox"))

## ----more-plot, fig.width = 7, fig.height = 4, eval = requireNamespace("ggplot2", quietly = TRUE)----
plot(comparison, metrics = c("pct_no_selection", "total_n_pts"))

