## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(simFastBOIN)

## -----------------------------------------------------------------------------
tab <- boin12_decision_table(target = 0.35, target_resp = 0.25)
tab_3 <- as.data.frame(tab)[tab$n == 3, ]
tab_3[, c("n", "n_tox", "n_resp", "admissible", "score")]

## -----------------------------------------------------------------------------
p_true <- c(0.05, 0.12, 0.27, 0.35, 0.50)
p_resp <- c(0.20, 0.35, 0.36, 0.37, 0.38)

truth_1 <- oc_scenarios(list("Scenario 1" = list(p_true = p_true,
                                                 p_resp = p_resp)),
                        target = 0.35, target_resp = 0.25)
truth_1

## -----------------------------------------------------------------------------
oc <- sim_boin12(target = 0.35, target_resp = 0.25, p_true = p_true,
                 p_resp = p_resp, n_cohort = 12, cohort_size = 3,
                 n_trials = 2000, seed = 123)
oc

## -----------------------------------------------------------------------------
oc_article <- sim_boin12(target = 0.35, target_resp = 0.25, p_true = p_true,
                         p_resp = p_resp, n_cohort = 12, cohort_size = 3,
                         n_trials = 2000, tie_break = "random",
                         no_candidate = "stop", explore_from = "admissible",
                         seed = 123)
selection <- rbind(default = c(oc$sel_percent, oc$percent_no_obd),
                   article = c(oc_article$sel_percent,
                               oc_article$percent_no_obd))
colnames(selection) <- c(paste0("DL", 1:5), "None")
round(selection, 1)

## -----------------------------------------------------------------------------
tite <- sim_tite_boin12(target = 0.35, target_resp = 0.25, p_true = p_true,
                        p_resp = p_resp, n_cohort = 12, cohort_size = 3,
                        window = 45, resp_window = 60, accrual_rate = 3 / 30,
                        n_trials = 2000, seed = 123)
tite

## -----------------------------------------------------------------------------
wait <- sim_tite_boin12(target = 0.35, target_resp = 0.25, p_true = p_true,
                        p_resp = p_resp, n_cohort = 12, cohort_size = 3,
                        window = 45, resp_window = 60, accrual_rate = 3 / 30,
                        accrual = "fixed", min_completed_share = 1,
                        n_trials = 2000, seed = 123)
all.equal(wait$sel_percent, oc$sel_percent)
round(c(TITE = tite$duration, wait = wait$duration) / 30, 1)

## -----------------------------------------------------------------------------
p_true_ab <- matrix(c(0.05, 0.10, 0.15, 0.15, 0.20, 0.45), nrow = 2)
p_resp_ab <- matrix(c(0.10, 0.20, 0.30, 0.40, 0.50, 0.60), nrow = 2)

comb_design <- list(target = 0.33, target_resp = 0.2, p_true = p_true_ab,
                    p_resp = p_resp_ab, n_cohort = 14, cohort_size = 3,
                    psi00 = 100 * 0.11 / 1.11, psi11 = 100 / 1.11,
                    benchmark_weight = 0.6, extrasafe = TRUE, offset = 0.10,
                    n_trials = 1000, seed = 123)
oc_comb <- do.call(sim_comb_boin12, comb_design)
oc_comb

## -----------------------------------------------------------------------------
oc_contour <- do.call(sim_comb_boin12,
                      c(comb_design, list(obd_bound = "contour")))
labels <- paste0("A", rep(1:2, 3), "B", rep(1:3, each = 2))
bounds <- rbind(rectangle = as.vector(oc_comb$sel_percent),
                contour = as.vector(oc_contour$sel_percent))
colnames(bounds) <- labels
round(bounds, 1)

## -----------------------------------------------------------------------------
tite_comb_design <- c(comb_design[names(comb_design) != "n_trials"],
                      list(window = 1, resp_window = 2, accrual_rate = 2,
                           event_time = "uniform", n_trials = 500))
oc_tc <- do.call(sim_tite_comb_boin12, tite_comb_design)
oc_tc_utility <- do.call(sim_tite_comb_boin12,
                         c(tite_comb_design, list(pending_use = "utility")))
pending <- rbind(all = c(as.vector(oc_tc$sel_percent), oc_tc$duration),
                 utility = c(as.vector(oc_tc_utility$sel_percent),
                             oc_tc_utility$duration))
colnames(pending) <- c(labels, "Months")
round(pending, 1)

## -----------------------------------------------------------------------------
scenarios <- list(
  "Scenario 1" = list(p_true = c(0.05, 0.12, 0.27, 0.35, 0.50),
                      p_resp = c(0.20, 0.35, 0.36, 0.37, 0.38)),
  "Scenario 2" = list(p_true = c(0.05, 0.12, 0.27, 0.35, 0.50),
                      p_resp = c(0.01, 0.05, 0.30, 0.60, 0.60)),
  "Scenario 3" = list(p_true = c(0.03, 0.05, 0.20, 0.22, 0.45),
                      p_resp = c(0.05, 0.10, 0.50, 0.68, 0.70))
)
truth <- oc_scenarios(scenarios, target = 0.35, target_resp = 0.25)

boin <- sim_boin_multi(target = 0.35, scenarios = scenarios, n_cohort = 12,
                       cohort_size = 3, n_trials = 2000, seed = 123)
boin_et <- sim_boin_et_multi(target = 0.35, target_resp = 0.40,
                             scenarios = scenarios, n_cohort = 12,
                             cohort_size = 3, n_trials = 2000, resp_cor = 0,
                             seed = 123)
boin12 <- sim_boin12_multi(target = 0.35, target_resp = 0.25,
                           scenarios = scenarios, n_cohort = 12,
                           cohort_size = 3, n_trials = 2000, seed = 123)

comparison <- oc_compare(BOIN = boin, "BOIN-ET" = boin_et, BOIN12 = boin12,
                         truth = truth)
comparison

## ----include = FALSE----------------------------------------------------------
correct_1 <- function(design) {
  comparison$pct_correct[comparison$design == design &
                           comparison$scenario == "Scenario 1"]
}

## ----compare-plot, fig.width = 7, fig.height = 6, eval = requireNamespace("ggplot2", quietly = TRUE)----
plot(comparison)

## ----checks, include = FALSE--------------------------------------------------
read_result <- function(dir, file) {
  utils::read.csv(system.file(dir, "results", file, package = "simFastBOIN"),
                  stringsAsFactors = FALSE)
}
n_pass <- function(x) sum(x$judgement == "PASS")
n_fail <- function(x) sum(x$judgement == "FAIL")
n_explained <- function(x) sum(x$judgement == "EXPLAINED")
n_within <- function(x) sum(grepl("within the tolerance", x$reason))

lin <- read_result("reproduce", "reproduce_Lin2020.csv")
lin_exact <- lin[lin$source %in% c("Lin 2020 Table 3", "Lin 2020 Figure 1",
                                   "Lin 2020 SI Section 6"), ]
lin_sim <- lin[lin$source %in% c("Lin 2020 Figure 3", "Lin 2020 SI Figure S3",
                                 "Lin 2020 SI Figure S4"), ]
lin_article <- lin_sim[!grepl("default rules", lin_sim$item), ]
lin_default <- lin_sim[grepl("default rules", lin_sim$item), ]

zhou <- read_result("reproduce", "reproduce_Zhou2022.csv")
zhou_s3 <- zhou[zhou$source == "Zhou 2022 Table S.3", ]
zhou_fig <- zhou[zhou$source == "Zhou 2022 Figure 3" &
                   zhou$judgement %in% c("PASS", "FAIL"), ]
zhou_fail <- zhou_fig[zhou_fig$judgement == "FAIL", ]
zhou_fail_scenario <- sub(",.*$", "", zhou_fail$item[1])

lu <- read_result("reproduce", "reproduce_Lu2025.csv")
lu_exact <- lu[lu$source %in% c("Lu 2025 Table 1", "Lu 2025 Table 2",
                                "Lu 2025 SI Table S2", "Lu 2025 SI Table S3",
                                "Lu 2025 SI Figure S1"), ]
lu_sim <- lu[!lu$source %in% lu_exact$source &
               lu$judgement %in% c("PASS", "FAIL"), ]
lu_fail_tite <- sum(lu_sim$judgement == "FAIL" &
                      grepl("TITE-Comb-BOIN12", lu_sim$item))
lu_info_select <- lu[lu$source %in% c("Lu 2025 Table 3", "Lu 2025 Table 4") &
                       lu$judgement == "INFO", ]

esc <- read_result("validation", "compare-with-escalation.csv")

