## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5
)

## ----quick-start--------------------------------------------------------------
library(winratiosim)

quick_res <- winratiosim(
  nsim = 2,
  N = 20,
  Randomization.ratio = c(1, 1),
  alpha.JFM = 0,
  theta.JFM = 1,
  lambda_trt = 0.13,
  lambda_ctl = 0.15,
  ann.icr_trt = 0.32,
  ann.icr_ctl = 0.55,
  xbase_trt = 45,
  xfinal_trt = 52.5,
  xbase_ctl = 45,
  xfinal_ctl = 45,
  sd.delta.x_trt = 20,
  sd.delta.x_ctl = 20,
  censorrate_trt = 0.2,
  censorrate_ctl = 0.2,
  nc = 1,
  seed = 20250518
)

quick_res$df_WR.analysis.summary
quick_res$df_sample.size.summary

## ----result-names-------------------------------------------------------------
names(quick_res)

## ----power-summary------------------------------------------------------------
fs_success <- quick_res$df_FS.analysis.summary$p_value_FS < 0.025
wr_success <- quick_res$df_WR.analysis.summary$LB_R_w > 1

data.frame(
  Method = c("FS test", "YG win ratio test"),
  Estimated_power = c(
    mean(fs_success, na.rm = TRUE),
    mean(wr_success, na.rm = TRUE)
  )
)

binom.conf.exact(
  x = sum(wr_success, na.rm = TRUE),
  n = sum(!is.na(wr_success))
)

## ----paper-workflow, eval = FALSE---------------------------------------------
# library(winratiosim)
# 
# power.design_parameters <- list(
#   nsim = 10000,
#   N = 400,
#   Randomization.ratio = c(1, 1),
#   alpha.JFM = 0,
#   theta.JFM = 1,
#   lambda_trt = 0.13,
#   lambda_ctl = 0.15,
#   ann.icr_trt = 0.32,
#   ann.icr_ctl = 0.55,
#   xbase_trt = 45,
#   xfinal_trt = 45 + 7.5,
#   sd.delta.x_trt = 20,
#   xbase_ctl = 45,
#   xfinal_ctl = 45,
#   sd.delta.x_ctl = 20,
#   censorrate_trt = 0.2,
#   censorrate_ctl = 0.2,
#   nc = 10,
#   seed = 20250518
# )
# 
# power.sim_res <- do.call(winratiosim, power.design_parameters)
# 
# Power_binom_CI_one_sided_FS_Permutation <- binom.conf.exact(
#   x = sum(power.sim_res$df_FS.analysis.summary$p_value_FS < 0.025,
#           na.rm = TRUE),
#   n = sum(!is.na(power.sim_res$df_FS.analysis.summary$p_value_FS))
# )
# 
# Power_binom_CI_one_sided_WR_Ron_Yu <- binom.conf.exact(
#   x = sum(power.sim_res$df_WR.analysis.summary$LB_R_w > 1,
#           na.rm = TRUE),
#   n = sum(!is.na(power.sim_res$df_WR.analysis.summary$LB_R_w))
# )
# 
# t1e.design_parameters <- list(
#   nsim = power.design_parameters$nsim,
#   N = power.design_parameters$N,
#   Randomization.ratio = power.design_parameters$Randomization.ratio,
#   alpha.JFM = power.design_parameters$alpha.JFM,
#   theta.JFM = power.design_parameters$theta.JFM,
#   lambda_trt = power.design_parameters$lambda_ctl,
#   lambda_ctl = power.design_parameters$lambda_ctl,
#   ann.icr_trt = power.design_parameters$ann.icr_ctl,
#   ann.icr_ctl = power.design_parameters$ann.icr_ctl,
#   xbase_trt = power.design_parameters$xbase_ctl,
#   xfinal_trt = power.design_parameters$xfinal_ctl,
#   sd.delta.x_trt = power.design_parameters$sd.delta.x_trt,
#   xbase_ctl = power.design_parameters$xbase_ctl,
#   xfinal_ctl = power.design_parameters$xfinal_ctl,
#   sd.delta.x_ctl = power.design_parameters$sd.delta.x_ctl,
#   censorrate_trt = power.design_parameters$censorrate_trt,
#   censorrate_ctl = power.design_parameters$censorrate_ctl,
#   nc = power.design_parameters$nc,
#   seed = 20250518
# )
# 
# t1e.sim_res <- do.call(winratiosim, t1e.design_parameters)
# 
# t1e_binom_CI_one_sided_FS_Permutation <- binom.conf.exact(
#   x = sum(t1e.sim_res$df_FS.analysis.summary$p_value_FS < 0.025,
#           na.rm = TRUE),
#   n = sum(!is.na(t1e.sim_res$df_FS.analysis.summary$p_value_FS))
# )
# 
# t1e_binom_CI_one_sided_WR_Ron_Yu <- binom.conf.exact(
#   x = sum(t1e.sim_res$df_WR.analysis.summary$LB_R_w > 1,
#           na.rm = TRUE),
#   n = sum(!is.na(t1e.sim_res$df_WR.analysis.summary$LB_R_w))
# )
# 
# df.power.type1 <- data.frame(
#   Method = c("FS test", "YG test"),
#   Power = paste(
#     round(c(Power_binom_CI_one_sided_FS_Permutation[1],
#             Power_binom_CI_one_sided_WR_Ron_Yu[1]), 3),
#     "(",
#     round(c(Power_binom_CI_one_sided_FS_Permutation[2],
#             Power_binom_CI_one_sided_WR_Ron_Yu[2]), 3),
#     ", ",
#     round(c(Power_binom_CI_one_sided_FS_Permutation[3],
#             Power_binom_CI_one_sided_WR_Ron_Yu[3]), 3),
#     ")",
#     sep = ""
#   ),
#   Type_I_Error = paste(
#     round(c(t1e_binom_CI_one_sided_FS_Permutation[1],
#             t1e_binom_CI_one_sided_WR_Ron_Yu[1]), 3),
#     "(",
#     round(c(t1e_binom_CI_one_sided_FS_Permutation[2],
#             t1e_binom_CI_one_sided_WR_Ron_Yu[2]), 3),
#     ", ",
#     round(c(t1e_binom_CI_one_sided_FS_Permutation[3],
#             t1e_binom_CI_one_sided_WR_Ron_Yu[3]), 3),
#     ")",
#     sep = ""
#   )
# )
# 
# df.variance <- data.frame(
#   Median_Variance_under_Power = c(
#     median(power.sim_res$df_WR.analysis.summary$variance_log_R_w_permutation,
#            na.rm = TRUE),
#     median(power.sim_res$df_WR.analysis.summary$Var_logR_w,
#            na.rm = TRUE)
#   ),
#   Median_Variance_under_Type_I_Error = c(
#     median(t1e.sim_res$df_WR.analysis.summary$variance_log_R_w_permutation,
#            na.rm = TRUE),
#     median(t1e.sim_res$df_WR.analysis.summary$Var_logR_w,
#            na.rm = TRUE)
#   )
# )
# 
# df.combined <- cbind(df.power.type1, round(df.variance, 4))
# df.combined
# 
# median(power.sim_res$df_WR.analysis.summary$R_w, na.rm = TRUE)
# median(power.sim_res$df_Total_probability[, "Prob_of_tie"], na.rm = TRUE)

