## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4.5)

## ----setup--------------------------------------------------------------------
library(DesignComplexSurvival)

## ----delayed-scenario---------------------------------------------------------
lam <- log(2) / 12
ctl <- ms_model(death = lam, dropout = 0.003)
trt <- ms_model(time = c(0, 6), death = c(lam, 0.65 * lam), dropout = 0.003)
acc <- ms_accrual(time = c(0, 3), rate = c(10, 30))
scn <- ms_scenario(ctl, trt, allocation = 1, accrual = acc)
ev <- ms_events(scn, n = 700, calendar_time = c(12, 24, 36))
ev[, c("time", "enrolled", "events_control", "events_treatment", "events")]

## ----delayed-size-------------------------------------------------------------
looks <- ms_looks("os", fraction = c(0.5, 0.75, 1))
test <- ms_test(futility = "beta", sf_fut = "HSD", param_fut = -2)
des <- ms_size(scn, looks = looks, test = test, power = 0.9,
               solve_for = "events", n = 700)
des

## ----delayed-survival, fig.alt = "Overall survival functions of the control and treatment groups with the delayed treatment effect"----
plot(des, type = "survival")

## ----delayed-mean-------------------------------------------------------------
t_fin <- des$study_duration
rbind(direct = lr_moments(scn, 700, t_fin)$z_mean,
      schoenfeld = lr_moments(scn, 700, t_fin, mean = "schoenfeld")$z_mean)

## ----delayed-summary----------------------------------------------------------
summary(des)
as.data.frame(des)[, c("time", "events", "z_upper", "hr_upper", "z_lower",
                       "cum_power")]

## ----delayed-followup---------------------------------------------------------
pw12 <- ms_power(scn, n = 700, looks = looks, test = test, followup = 12)
c(power = pw12$power, final_analysis = pw12$study_duration,
  events = pw12$events)

