## ----setup, include=FALSE, message=FALSE, warning=FALSE-----------------------
knitr::opts_chunk$set(echo = TRUE, comment = "#>", collapse = TRUE)
options(rmarkdown.html_vignette.check_title = FALSE)

## ----setup-package, message=FALSE---------------------------------------------
library(SimTOST)

## ----workflow-inputs----------------------------------------------------------
endpoints <- paste0("y", 1:3)
mu_t <- setNames(rep(1.00, 3), endpoints)
mu_r1 <- setNames(rep(1.05, 3), endpoints)
mu_r2 <- setNames(rep(1.04, 3), endpoints)
sigma_t <- setNames(rep(0.20, 3), endpoints)
sigma_r1 <- setNames(rep(0.20, 3), endpoints)
sigma_r2 <- setNames(rep(0.21, 3), endpoints)

comparators <- list(T_vs_R1 = c("T", "R1"), T_vs_R2 = c("T", "R2"))
lower <- list(T_vs_R1 = setNames(rep(0.80, 3), endpoints),
              T_vs_R2 = setNames(rep(0.80, 3), endpoints))
upper <- list(T_vs_R1 = setNames(rep(1.25, 3), endpoints),
              T_vs_R2 = setNames(rep(1.25, 3), endpoints))

## ----workflow-pilot, eval=TRUE------------------------------------------------
ss_pilot <- sampleSize(
  power = 0.80, alpha = 0.05,
  mu_list = list(T = mu_t, R1 = mu_r1, R2 = mu_r2),
  sigma_list = list(T = sigma_t, R1 = sigma_r1, R2 = sigma_r2),
  rho = 0.50, list_comparator = comparators,
  list_y_comparator = list(T_vs_R1 = endpoints, T_vs_R2 = endpoints),
  list_lequi.tol = lower, list_uequi.tol = upper,
  dtype = "parallel", adjust = "none", k = 3,
  distribution = "lnorm", ctype = "ROM", ncores = 1,
  nsim = 500, seed = 2026, keep_sim_data = TRUE
)

## ----workflow-pilot-k2, eval=TRUE---------------------------------------------
ss_pilot_k2 <- update(ss_pilot, adjust = "bon", k = 2, seed = 2026)

## ----workflow-pilot-k1, eval=TRUE---------------------------------------------
ss_pilot_k1 <- update(ss_pilot, adjust = "bon", k = 1, seed = 2027)

## ----workflow-parameter-mean, eval=TRUE, fig.width=8, fig.height=5, out.width="100%", fig.align="center"----
plot_distribution(ss_pilot, estimand = "mu")

## ----workflow-parameter-sigma, eval=TRUE, fig.width=8, fig.height=5, out.width="100%", fig.align="center"----
plot_distribution(ss_pilot, estimand = "sigma")

## ----workflow-correlation, eval=TRUE, fig.width=8, fig.height=5, out.width="100%", fig.align="center"----
plot_distribution(ss_pilot, estimand = "correlation")

## ----workflow-estimand, eval=TRUE, fig.width=8, fig.height=5, out.width="100%", fig.align="center"----
plot_distribution(ss_pilot, estimand = "ROM")

## ----workflow-t-values, eval=TRUE, fig.width=8, fig.height=5, out.width="100%", fig.align="center"----
plot_distribution(ss_pilot, estimand = "t_value",
                  arms = c("T", "R1", "R2"))

## ----workflow-stability, eval=TRUE, fig.width=8, fig.height=5, out.width="100%", fig.align="center"----
plot_stability(ss_pilot, overall = TRUE)

## ----workflow-mc-error, eval=TRUE, fig.width=8, fig.height=5, out.width="100%", fig.align="center"----
plot_mc_error(ss_pilot, overall = TRUE)

## ----workflow-type1-one-scenario-plot, eval=TRUE, fig.width=8, fig.height=5, out.width="100%", fig.align="center"----
ss_one <- sampleSize(
  power = 0.80, alpha = 0.05,
  mu_list = list(T = mu_t["y1"], R1 = mu_r1["y1"]),
  sigma_list = list(T = sigma_t["y1"], R1 = sigma_r1["y1"]),
  list_comparator = list(T_vs_R1 = c("T", "R1")),
  list_y_comparator = list(T_vs_R1 = "y1"),
  list_lequi.tol = list(T_vs_R1 = c(y1 = 0.80)),
  list_uequi.tol = list(T_vs_R1 = c(y1 = 1.25)),
  dtype = "parallel", distribution = "lnorm", ctype = "ROM",
  lower = 10, upper = 100, nsim = 1000, seed = 2026, ncores = 1
)
type1_one <- type1Error(x = ss_one, null = "both", joint = TRUE)
print(type1_one)
plot(type1_one)

## ----workflow-type1-allscenario-plot, eval=TRUE, fig.width=8, fig.height=10, out.width="100%", fig.align="center"----
type1_global <- type1Error(x = ss_pilot, null = "both", joint = TRUE)
print(type1_global)
plot(type1_global)

## ----workflow-type1-allscenario-k2-plot, eval=TRUE, fig.width=8, fig.height=10, out.width="100%", fig.align="center"----
type1_global_k2 <- type1Error(x = ss_pilot_k2, null = "both", joint = TRUE)
print(type1_global_k2)
plot(type1_global_k2)

## ----workflow-type1-allscenario-k1-plot, eval=TRUE, fig.width=8, fig.height=10, out.width="100%", fig.align="center"----
type1_global_k1 <- type1Error(x = ss_pilot_k1, null = "both", joint = TRUE)
print(type1_global_k1)
plot(type1_global_k1)

## ----workflow-sample-size, eval=TRUE------------------------------------------
ss <- sampleSize(
  power = 0.80, alpha = 0.05,
  mu_list = list(T = mu_t, R1 = mu_r1, R2 = mu_r2),
  sigma_list = list(T = sigma_t, R1 = sigma_r1, R2 = sigma_r2),
  rho = 0.50, list_comparator = comparators,
  list_y_comparator = list(T_vs_R1 = endpoints, T_vs_R2 = endpoints),
  list_lequi.tol = lower, list_uequi.tol = upper,
  dtype = "parallel", ctype = "ROM", distribution = "lnorm",
  adjust = "none", k = 3, ncores = 1, nsim = 500, seed = 2026,
  keep_sim_data = TRUE
)

## ----workflow-result, eval=TRUE, fig.width=8, fig.height=5, out.width="100%", fig.align="center"----
print(ss)
summary(ss)
confint(ss)
plot(ss)

## ----workflow-heatmap, eval=TRUE, fig.width=8, fig.height=5, out.width="100%", fig.align="center"----
plot_decision_heatmap(ss, display = c("T_vs_R1", "T_vs_R2"))

