## ----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)

## -----------------------------------------------------------------------------
p_grade <- rbind(
  "Grade 0-1" = c(0.83, 0.75, 0.62, 0.51, 0.34, 0.19),
  "Grade 2" = c(0.12, 0.15, 0.18, 0.19, 0.16, 0.11),
  "Grade 3" = c(0.04, 0.07, 0.11, 0.14, 0.15, 0.11),
  "Grade 4" = c(0.01, 0.03, 0.09, 0.16, 0.35, 0.59)
)
cr <- c(0.05, 0.10, 0.15, 0.50, 0.55, 0.60)
pr <- c(0.05, 0.20, 0.35, 0.20, 0.20, 0.20)
p_resp_grade <- rbind("SD or PD" = 1 - cr - pr, PR = pr, CR = cr)

g_design <- list(target = 0.47 / 1.5, target_resp = 0.95 / 1.5,
                 p_grade = p_grade, p_resp_grade = p_resp_grade,
                 score = c(0, 0.5, 1, 1.5), score_resp = c(0, 1, 1.5),
                 n_cohort = 10, cohort_size = 3, n_trials = 2000,
                 seed = 123)
oc_g <- do.call(sim_gboin_et, g_design)
oc_g

## -----------------------------------------------------------------------------
oc_g_article <- do.call(sim_gboin_et,
                        c(g_design, list(est_method = "fp_linear",
                                         min_obd_sample = 6)))
round(rbind(
  default = c(oc_g$sel_percent, "No OBD" = oc_g$percent_no_obd),
  "article selection" = c(oc_g_article$sel_percent,
                          "No OBD" = oc_g_article$percent_no_obd)
), 1)

## -----------------------------------------------------------------------------
p_resp_grade_4 <- rbind(
  PD = c(0.60, 0.50, 0.30, 0.15, 0.10, 0.05),
  SD = c(0.30, 0.20, 0.20, 0.15, 0.15, 0.15),
  PR = c(0.05, 0.20, 0.35, 0.20, 0.20, 0.20),
  CR = c(0.05, 0.10, 0.15, 0.50, 0.55, 0.60)
)
tg_design <- list(target = 0.47 / 1.5, target_resp = 1.75 / 3,
                  p_grade = p_grade, p_resp_grade = p_resp_grade_4,
                  score = c(0, 0.5, 1, 1.5), score_resp = c(0, 0.25, 1, 3),
                  n_cohort = 12, cohort_size = 3, window = 30,
                  resp_window = 60, accrual_rate = 1 / 10, n_trials = 1000,
                  seed = 123)
oc_tg <- do.call(sim_tite_gboin_et, tg_design)
oc_tg

oc_tg_wait <- do.call(sim_tite_gboin_et,
                      c(tg_design, list(min_completed_share = 1)))
round(rbind(
  "wait for half" = c(oc_tg$sel_percent, weeks = oc_tg$duration / 7),
  "wait for all" = c(oc_tg_wait$sel_percent,
                     weeks = oc_tg_wait$duration / 7)
), 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")

# Scenario 11 as printed (0.55 at the lowest dose) is not judged; the
# values outside the tolerance are counted.
as_printed <- function(x) x[grepl("as printed", x$item), ]
n_outside <- function(x) {
  sum(abs(as.numeric(x$published) - as.numeric(x$reproduced)) >
        as.numeric(x$tolerance))
}

t18 <- read_result("reproduce", "reproduce_Takeda2018.csv")
t18_exact <- t18[!grepl("^Scenario", t18$item), ]
t18_sim <- t18[grepl("^Scenario", t18$item) & t18$judgement != "INFO", ]
t18_s11p <- as_printed(t18)

t20_all <- read_result("reproduce", "reproduce_Takeda2020.csv")
t20 <- t20_all[t20_all$judgement != "INFO", ]
t20_s11p <- as_printed(t20_all)
t20_et <- t20[t20$design == "BOIN-ET", ]
t20_tite <- t20[t20$design == "TITE-BOIN-ET", ]
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_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")
}

t22 <- read_result("reproduce", "reproduce_Takeda2022.csv")
t22_bd <- t22[grepl("^Section", t22$item), ]
t22_sim <- t22[t22$design == "gBOIN-ET" & grepl("^Scenario", t22$item), ]
t22_fail_pts <- sum(t22_sim$judgement == "FAIL" &
                      grepl("^Patients", t22_sim$quantity))
t23 <- read_result("reproduce", "reproduce_Takeda2023.csv")
t23_bd <- t23[grepl("^Section", t23$item), ]
t23_pub <- t23[t23$design == "TITE-gBOIN-ET" & grepl("^Table", t23$item), ]
t23_cor <- t23[t23$design == "TITE-gBOIN-ET against the corrected program", ]
cg <- read_result("validation", "compare-with-gboinet.csv")
cg <- cg[cg$design == "gBOIN-ET", ]
ctg <- read_result("validation", "compare-with-tite-gboinet.csv")
ctg <- ctg[ctg$design == "TITE-gBOIN-ET", ]

