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

## ----setup----------------------------------------------------------
library(gsDesign)

lambdaC <- log(2) / 12
hr <- 0.7

# Four enrollment periods with equal relative rate increments.
gamma_ramp <- 1:4
R_ramp <- rep(1, 4)

## ----fixed-duration-------------------------------------------------
fixed_duration <- gsSurv(
  k = 3,
  lambdaC = lambdaC,
  hr = hr,
  T = 26,
  minfup = 12,
  gamma = gamma_ramp,
  R = R_ramp
)

data.frame(
  period = seq_along(fixed_duration$R),
  duration = fixed_duration$R,
  rate = as.vector(fixed_duration$gamma)
)

fixed_duration$N

## ----fixed-rates----------------------------------------------------
fixed_rates <- gsSurv(
  k = 3,
  lambdaC = lambdaC,
  hr = hr,
  T = NULL,
  minfup = 12,
  gamma = gamma_ramp,
  R = R_ramp
)

data.frame(
  period = seq_along(fixed_rates$R),
  duration = fixed_rates$R,
  rate = as.vector(fixed_rates$gamma)
)

c(
  enrollment_duration = sum(fixed_rates$R),
  minimum_follow_up = fixed_rates$minfup,
  total_duration = max(fixed_rates$T)
)

## ----fixed-enrollment-----------------------------------------------
fixed_enrollment <- gsSurv(
  k = 3,
  lambdaC = lambdaC,
  hr = hr,
  T = NULL,
  minfup = NULL,
  gamma = 50 * gamma_ramp,
  R = R_ramp
)

c(
  enrollment_duration = sum(fixed_enrollment$R),
  minimum_follow_up = fixed_enrollment$minfup,
  total_duration = max(fixed_enrollment$T)
)

## ----calendar-design------------------------------------------------
calendar_design <- gsSurvCalendar(
  calendarTime = c(12, 18, 26),
  lambdaC = lambdaC,
  hr = hr,
  minfup = 12,
  gamma = gamma_ramp,
  R = R_ramp
)

data.frame(
  analysis_month = calendar_design$T,
  expected_events = calendar_design$n.I,
  expected_enrollment = calendar_design$N
)

## ----power-sensitivity----------------------------------------------
slower_enrollment <- gsSurvPower(
  x = fixed_duration,
  gamma = 0.8 * fixed_duration$gamma,
  plannedCalendarTime = fixed_duration$T
)

c(
  planned_power = 1 - fixed_duration$beta,
  slower_enrollment_power = slower_enrollment$power
)

## ----integer-plan---------------------------------------------------
integer_design <- toInteger(fixed_duration)

data.frame(
  analysis = seq_len(integer_design$k),
  events = integer_design$n.I,
  enrollment = integer_design$N
)

## ----stratified-----------------------------------------------------
lambda_strata <- matrix(log(2) / c(10, 16), nrow = 1)
gamma_strata <- cbind(
  0.6 * gamma_ramp,
  0.4 * gamma_ramp
)

stratified_design <- gsSurv(
  k = 3,
  lambdaC = lambda_strata,
  hr = hr,
  eta = matrix(c(0.001, 0.001), nrow = 1),
  T = 26,
  minfup = 12,
  gamma = gamma_strata,
  R = R_ramp
)

data.frame(
  analysis = seq_len(stratified_design$k),
  control = rowSums(stratified_design$eNC),
  experimental = rowSums(stratified_design$eNE),
  total = stratified_design$N
)

