## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(simFastBOIN)

## -----------------------------------------------------------------------------
p_true <- matrix(c(0.02, 0.04, 0.08, 0.14,
                   0.08, 0.25, 0.42, 0.48,
                   0.25, 0.45, 0.50, 0.60), nrow = 3, byrow = TRUE)

oc <- sim_comb_boin(
  target = 0.25,
  p_true = p_true,
  n_cohort = 16,
  cohort_size = 3,
  n_earlystop = 100,
  n_trials = 1000,
  seed = 6
)
oc

## -----------------------------------------------------------------------------
scenarios <- list(
  "Scenario 1" = matrix(c(0.05, 0.10, 0.15, 0.30, 0.45,
                          0.10, 0.15, 0.30, 0.45, 0.55,
                          0.15, 0.30, 0.45, 0.50, 0.60), nrow = 3, byrow = TRUE),
  "Scenario 4" = matrix(c(0.30, 0.45, 0.60, 0.70, 0.80,
                          0.45, 0.55, 0.65, 0.75, 0.85,
                          0.50, 0.60, 0.70, 0.80, 0.90), nrow = 3, byrow = TRUE),
  "Scenario 8" = matrix(c(0.02, 0.10, 0.15, 0.50, 0.60,
                          0.05, 0.12, 0.30, 0.55, 0.70,
                          0.08, 0.15, 0.45, 0.60, 0.80), nrow = 3, byrow = TRUE),
  "Scenario 10" = matrix(c(0.05, 0.10, 0.15, 0.30, 0.45,
                           0.45, 0.50, 0.60, 0.65, 0.70,
                           0.70, 0.75, 0.80, 0.85, 0.90), nrow = 3, byrow = TRUE)
)

multi <- sim_comb_boin_multi(
  target = 0.30,
  scenarios = scenarios,
  n_cohort = 20,
  cohort_size = 3,
  n_earlystop = 100,
  n_trials = 2000,
  seed = 123
)
multi

## -----------------------------------------------------------------------------
multi$results[["Scenario 10"]]$sel_percent

## ----include = FALSE----------------------------------------------------------
stops_4 <- multi$results[["Scenario 4"]]$stop_reason_percent
eliminated_4 <- if ("lowest_dose_eliminated" %in% names(stops_4)) {
  stops_4[["lowest_dose_eliminated"]]
} else {
  0
}

## -----------------------------------------------------------------------------
decisions <- boin_decision_table(target = 0.25, max_n = 18)

print(decisions, cohort_size = 3)

## ----comb-decision-plot, fig.width = 8, fig.height = 6, eval = requireNamespace("ggplot2", quietly = TRUE)----
plot(decisions)

## -----------------------------------------------------------------------------
n_pts <- matrix(c(6, 6, 0, 0,
                  3, 3, 0, 0,
                  0, 0, 0, 0), nrow = 3, byrow = TRUE)
n_tox <- matrix(c(0, 1, 0, 0,
                  1, 2, 0, 0,
                  0, 0, 0, 0), nrow = 3, byrow = TRUE)

nxt <- comb_boin_next(target = 0.25, n_pts = n_pts, n_tox = n_tox,
                      current = c(2, 2))
nxt

## ----comb-next-plot, fig.width = 7, fig.height = 5.5, eval = requireNamespace("ggplot2", quietly = TRUE)----
plot(nxt)

## -----------------------------------------------------------------------------
trials <- comb_boin_simulate(
  target = 0.25, p_true = p_true, n_cohort = 16, cohort_size = 3,
  n_trials = 5, seed = 1
)
trials$n_pts[1, , ]
trials$n_tox[1, , ]

comb_boin_select_mtd(trials$n_pts[1, , ], trials$n_tox[1, , ], target = 0.25)

## ----eval = FALSE-------------------------------------------------------------
# reference <- BOIN::get.oc.comb(
#   target = 0.25, p.true = p_true, ncohort = 16, cohortsize = 3,
#   n.earlystop = 18, ntrial = 1000, seed = 6
# )
# 
# ours <- sim_comb_boin(
#   target = 0.25, p_true = p_true, n_cohort = 16, cohort_size = 3,
#   n_earlystop = 18, n_trials = 1000, seed = 6
# )
# 
# all.equal(round(unname(ours$sel_percent), 2), unname(reference$selpercent))
# all.equal(round(unname(ours$n_pts_dose), 2), unname(reference$npatients))

