## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4.5)

## ----setup--------------------------------------------------------------------
library(DesignComplexSurvival)

## ----helpers, include = FALSE-------------------------------------------------
f3 <- function(x) sprintf("%.3f", x)
f4 <- function(x) sprintf("%.4f", x)
f2s <- function(x) format(signif(x, 2), scientific = FALSE)

## ----scenario-----------------------------------------------------------------
lam <- log(2) / 12
scn <- ms_scenario(ms_model(death = lam, dropout = 0.003),
                   ms_model(time = c(0, 6), death = c(lam, 0.65 * lam), dropout = 0.003),
                   accrual = ms_accrual(time = c(0, 3), rate = c(10, 30)))
cols <- c("time", "events", "info_frac", "z_upper", "hr_upper", "cum_power")

## ----looks-events-------------------------------------------------------------
lk <- ms_looks("os", fraction = c(0.5, 0.75, 1))
pw <- ms_power(scn, n = 700, looks = lk, test = ms_test(), events = 420)
pw$looks[, cols]

## ----looks-calendar-----------------------------------------------------------
pw_cal <- ms_power(scn, n = 700, looks = ms_looks("calendar", time = c(24, 30, 40)),
                   test = ms_test())
pw_cal$looks[, cols]

## ----spending-----------------------------------------------------------------
t3 <- c(1 / 3, 2 / 3, 1)
spent <- rbind(OF = gs_spend(t3, type = "OF"),
               Pocock = gs_spend(t3, type = "Pocock"),
               HSD_minus4 = gs_spend(t3, type = "HSD", param = -4))
round(spent, 5)
bounds <- rbind(OF = gs_bounds(t3)$upper,
                Pocock = gs_bounds(t3, sf_eff = "Pocock")$upper,
                HSD_minus4 = gs_bounds(t3, sf_eff = "HSD", param_eff = -4)$upper)
round(bounds, 3)

## ----spending-time------------------------------------------------------------
pw_info <- ms_power(scn, n = 700, looks = lk,
                    test = ms_test(spending_time = "information"), events = 420)
rbind(event_fraction = pw$looks$z_upper, information_fraction = pw_info$looks$z_upper)

## ----futility-----------------------------------------------------------------
tests <- list(
  fixed_hr = ms_test(futility = "fixed", fut_bound = c(1, 1), fut_scale = "hr",
                     binding = TRUE),
  beta_nonbinding = ms_test(futility = "beta", sf_fut = "HSD", param_fut = -2),
  beta_binding = ms_test(futility = "beta", sf_fut = "HSD", param_fut = -2,
                         binding = TRUE))
fut <- lapply(tests, function(te) ms_power(scn, n = 700, looks = lk, test = te,
                                           events = 420))
t(sapply(fut, function(x) c(power = x$power, x$type1)))
fut$beta_nonbinding$looks[, c("time", "z_upper", "z_lower", "hr_lower", "prob_lower")]

## ----expected-----------------------------------------------------------------
des_nb <- fut$beta_nonbinding
des_nb$expected
ex_d <- des_nb$expected[des_nb$expected$hypothesis == "design", ]
ex_0 <- des_nb$expected[des_nb$expected$hypothesis == "null", ]

## ----plot-bounds, fig.alt = "Efficacy and futility bounds of the design on the scale of the standardized log-rank statistic against the expected deaths, with the mean of the statistic under the design scenario"----
plot(des_nb, type = "bounds")

## ----plot-power, fig.alt = "Cumulative probabilities of stopping for efficacy and for futility under the design scenario and cumulative type I error against calendar time"----
plot(des_nb, type = "power", x_axis = "time")

## ----cond-power---------------------------------------------------------------
cp_trend <- ms_cond_power(des_nb, look = 1, z = 1.5, under = "trend")
cp_trend

## ----cond-power-under---------------------------------------------------------
scn_small <- ms_scenario(ms_model(death = lam, dropout = 0.003),
                         ms_model(time = c(0, 6), death = c(lam, 0.8 * lam),
                                  dropout = 0.003),
                         accrual = ms_accrual(time = c(0, 3), rate = c(10, 30)))
cp <- c(design = ms_cond_power(des_nb, look = 1, z = 1.5)$cond_power,
        trend = cp_trend$cond_power,
        hr_0.65 = ms_cond_power(des_nb, look = 1, z = 1.5, under = "hr",
                                hr = 0.65)$cond_power,
        scenario_0.8 = ms_cond_power(des_nb, look = 1, z = 1.5, under = "scenario",
                                     scenario = scn_small)$cond_power,
        null = ms_cond_power(des_nb, look = 1, z = 1.5, under = "null")$cond_power)
round(cp, 3)

## ----variance-----------------------------------------------------------------
mo <- lapply(c(alternative = "alternative", studentized = "studentized"),
             function(v) lr_moments(scn, n = 700, calendar_time = pw$looks$time,
                                    variance = v))
sapply(mo, function(m) m$z_var)
pv <- sapply(c("null", "alternative", "studentized"), function(v)
  ms_power(scn, n = 700, looks = lk, test = ms_test(variance = v), events = 420)$power)
pv

## ----skew-package-------------------------------------------------------------
ctl_b <- ms_model(time = c(0, 6), death = c(0.05, 0.04), dropout = 0.01)
scn_b <- ms_scenario(ctl_b, ctl_b, allocation = 2,
                     accrual = ms_accrual(time = c(0, 3), rate = c(10, 30)))
pw_b <- ms_power(scn_b, n = 300, looks = ms_looks("os", fraction = c(0.5, 1)),
                 test = ms_test(), events = 150)
pw_b$type1[["nonbinding"]]

## ----skew-simulation----------------------------------------------------------
sim <- utils::read.csv(system.file("validation", "results", "sim_python_type1.csv",
                                   package = "DesignComplexSurvival"))
rate <- function(v, q) sim$value[sim$variant == v & sim$quantity == q]
groups <- c("B_1:2" = "100 control, 200 treatment",
            "B_2:1" = "200 control, 100 treatment",
            "B_1:1" = "150 in each group")
tab <- data.frame(groups = unname(groups),
                  trials = format(sapply(names(groups), function(v)
                    sim$n[sim$variant == v & sim$quantity == "reject_upper_any"]),
                    big.mark = ",", scientific = FALSE),
                  benefit = sapply(names(groups), rate, q = "reject_upper_any"),
                  opposite = sapply(names(groups), rate, q = "reject_lower_any"),
                  row.names = NULL)
knitr::kable(tab, digits = 4, format.args = list(big.mark = ","))

