## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(simFastBOIN)

## -----------------------------------------------------------------------------
bd <- boin_et_boundary(target = 0.30, target_resp = 0.60)
bd

## -----------------------------------------------------------------------------
boin_et_decision_table(target = 0.30, target_resp = 0.60, n = c(3, 6))

## -----------------------------------------------------------------------------
p_true <- c(0.05, 0.15, 0.25, 0.30, 0.35, 0.40)
p_resp <- c(0.05, 0.35, 0.55, 0.65, 0.75, 0.70)

oc <- sim_boin_et(target = 0.30, target_resp = 0.60, p_true = p_true,
                  p_resp = p_resp, n_cohort = 12, cohort_size = 3,
                  n_trials = 2000, seed = 123)
oc

## -----------------------------------------------------------------------------
scenarios <- list(
  list(name = "Scenario 1", p_true = p_true, p_resp = p_resp),
  list(name = "Scenario 7", p_true = c(0.05, 0.15, 0.25, 0.35, 0.45, 0.55),
       p_resp = c(0.30, 0.70, 0.60, 0.50, 0.40, 0.30))
)
sim_boin_et_multi(target = 0.30, target_resp = 0.60, scenarios = scenarios,
                  n_cohort = 12, cohort_size = 3, n_trials = 1000,
                  seed = 123)

## -----------------------------------------------------------------------------
oc_kept <- sim_boin_et(target = 0.30, target_resp = 0.60, p_true = p_true,
                       p_resp = p_resp, n_cohort = 12, cohort_size = 3,
                       n_trials = 2000, keep_trials = TRUE, seed = 123)
tr <- oc_kept$trials

select_percent <- function(est_method, obd_method) {
  obd <- boin_et_select_obd(n_pts = tr$n_pts, n_tox = tr$n_tox,
                            n_resp = tr$n_resp, target = 0.30,
                            target_resp = 0.60, est_method = est_method,
                            obd_method = obd_method,
                            early_stop = tr$early_stop)$obd
  as.numeric(table(factor(obd, levels = 1:6))) / length(obd) * 100
}

rules <- c("max_resp", "utility_weighted", "utility_truncated_linear",
           "utility_scoring")
selection <- rbind(
  t(sapply(rules, function(m) select_percent("observed", m))),
  "max_resp, multi_iso" = select_percent("multi_iso", "max_resp")
)
colnames(selection) <- paste0("DL", 1:6)
round(selection, 1)

all.equal(unname(selection["max_resp", ]), unname(oc_kept$sel_percent))

## ----eval = FALSE-------------------------------------------------------------
# sim_boin_et(target = 0.30, target_resp = 0.60, p_true = p_true,
#             p_resp = p_resp, n_cohort = 12, cohort_size = 3,
#             n_trials = 2000, est_method = "fp_logistic", seed = 123)

## -----------------------------------------------------------------------------
skip_design <- list(target = 0.30, target_resp = 0.60,
                    p_true = c(0.10, 0.20, 0.45, 0.50, 0.55),
                    p_resp = c(0.10, 0.20, 0.30, 0.50, 0.60),
                    n_cohort = 12, cohort_size = 3, n_trials = 2000,
                    seed = 123)
oc_default <- do.call(sim_boin_et, skip_design)
oc_boinet <- do.call(sim_boin_et,
                     c(skip_design, list(boinet_compatible = TRUE)))
round(rbind(default = oc_default$n_pts_dose,
            boinet = oc_boinet$n_pts_dose), 2)

## -----------------------------------------------------------------------------
tite_design <- list(target = 0.30, target_resp = 0.60, p_true = p_true,
                    p_resp = p_resp, n_cohort = 12, cohort_size = 3,
                    window = 28, resp_window = 56, accrual_rate = 1 / 14,
                    n_trials = 1000, seed = 123)
oc_tite <- do.call(sim_tite_boin_et, tite_design)
oc_tite

oc_tite_0 <- do.call(sim_tite_boin_et,
                     c(tite_design, list(min_completed_share = 0)))
round(rbind(
  "wait for half" = c(oc_tite$sel_percent, weeks = oc_tite$duration / 7),
  "no wait" = c(oc_tite_0$sel_percent, weeks = oc_tite_0$duration / 7)
), 1)

## -----------------------------------------------------------------------------
bf_design <- list(target = 0.30, target_resp = 0.25,
                  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),
                  n_cohort = 10, cohort_size = 3, window = 1,
                  accrual_rate = 3, n_trials = 1000, seed = 123)
oc_bf <- do.call(sim_bf_boin_et, bf_design)
oc_bf

## -----------------------------------------------------------------------------
oc_bf_a <- do.call(sim_bf_boin_et, c(bf_design, list(suspend = 0.33)))
round(rbind(
  "BF-BOIN-ET-C" = c(oc_bf$sel_percent, patients = oc_bf$total_n_pts,
                     backfilled = oc_bf$total_n_bf,
                     months = oc_bf$duration),
  "BF-BOIN-ET-A" = c(oc_bf_a$sel_percent, patients = oc_bf_a$total_n_pts,
                     backfilled = oc_bf_a$total_n_bf,
                     months = oc_bf_a$duration)
), 1)

## -----------------------------------------------------------------------------
p_true_ab <- matrix(c(0.05, 0.10, 0.10, 0.30, 0.30, 0.50), nrow = 2)
p_resp_ab <- matrix(c(0.10, 0.25, 0.25, 0.60, 0.60, 0.65), nrow = 2)

oc_etc <- sim_boin_etc(target = 0.30, target_resp = 0.60, p_true = p_true_ab,
                       p_resp = p_resp_ab, n_cohort = 8, cohort_size = 3,
                       n_trials = 2000, seed = 123)
oc_etc

## -----------------------------------------------------------------------------
etc_versions <- list(
  "BOIN-ETC1" = list(subtrials = "etc1"),
  "BOIN-ETC1, code" = list(subtrials = "etc1", etc1_start = "below",
                           rule4 = "authors_code"),
  "BOIN-ETC2" = list(subtrials = "etc2"),
  "BOIN-ETC3" = list(subtrials = "etc3")
)
etc_runs <- lapply(etc_versions, function(v) {
  do.call(sim_boin_etc,
          c(list(target = 0.30, target_resp = 0.60, p_true = p_true_ab,
                 p_resp = p_resp_ab, n_cohort = 8, cohort_size = 3,
                 n_trials = 2000, odc_tie = "all", seed = 123), v))
})
round(t(sapply(etc_runs, function(o) {
  c("ODC selected (%)" = o$pct_true_obd_selected,
    "Patients at ODC (%)" = o$pct_patients_true_obd,
    "Patients" = o$total_n_pts, "Subtrials" = o$mean_subtrials)
})), 1)

## ----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")

t18 <- read_result("reproduce", "reproduce_Takeda2018.csv")
t18_exact <- t18[!grepl("^Scenario", t18$item), ]
t18_sim <- t18[grepl("^Scenario", t18$item), ]
t18_s11 <- t18_sim[t18_sim$item == "Scenario 11", ]

t20 <- read_result("reproduce", "reproduce_Takeda2020.csv")
t20_et <- t20[t20$design == "BOIN-ET", ]
t20_tite <- t20[t20$design == "TITE-BOIN-ET", ]
t20_tite_s11 <- t20_tite[t20_tite$item == "Scenario 11", ]
t20_tite_late <- t20_tite[t20_tite$item %in%
                            paste("Scenario", c(8, 9, 10, 12)) &
                            t20_tite$quantity != "recommendation (%)", ]
t20_tite_other <- n_fail(t20_tite) - n_fail(t20_tite_s11) -
  n_fail(t20_tite_late)

t25 <- read_result("reproduce", "reproduce_Takeda2025.csv")

cb <- read_result("validation", "compare-with-boinet.csv")
cb_et <- cb[cb$design == "BOIN-ET", ]
cb_tite <- cb[cb$design == "TITE-BOIN-ET", ]
cf <- read_result("validation", "compare-with-bfboinet.csv")

n_explained <- function(x) sum(x$judgement == "EXPLAINED")
kak <- read_result("reproduce", "reproduce_Kakizume2024.csv")
kak_alloc <- kak[kak$judgement != "INFO", ]
kak_sel <- kak[kak$judgement == "INFO", ]
bc <- read_result("validation", "compare-with-BOINETC.csv")
bc_pub <- bc[grepl("^Kakizume", bc$source), ]
bc_pkg <- bc[bc$source == "simFastBOIN on the trials of the code", ]
bc_differ <- as.numeric(sub("^ODCs differ in ([0-9.]+)%.*$", "\\1",
                            bc_pkg$reason))
bc_other <- as.numeric(sub("^.*, ([0-9.]+)% otherwise$", "\\1",
                           bc_pkg$reason))
bc_other_text <- if (max(bc_other) == 0) {
  "no trial differs for another reason"
} else {
  paste("other causes account for at most", max(bc_other),
        "percent of the trials of a setting")
}

