tests/testthat/test-ms_size.R

# Expected values: inst/validation/python/results/size.csv

test_that("ms_size reproduces the Schoenfeld number of events for a fixed design", {
  res <- ms_size(scn_a(), looks = ms_looks("os"), test = ms_test(mean = "schoenfeld"),
                 power = 0.9, solve_for = "events", n = 500, round_up = FALSE)
  # (z_0.975 + z_0.9)^2 / (0.25 log(0.7)^2)
  d_ref <- (qnorm(0.975) + qnorm(0.9))^2 / (0.25 * log(0.7)^2)
  expect_equal(d_ref, 330.3779139638097, tolerance = 1e-12)
  expect_lt(abs(res$solution - d_ref) / d_ref, 1e-6)
  expect_lt(abs(res$power - 0.9), 1e-6)
  expect_s3_class(res, "ms_power")
})

test_that("ms_size matches the reference two-look design and rounds up", {
  lo <- ms_looks("os", fraction = c(0.5, 1))
  res <- ms_size(scn_a(), looks = lo, test = ms_test(), power = 0.9,
                 solve_for = "events", n = 500)
  expect_lt(abs(res$solution - 332.2830834262169), 0.01)
  expect_equal(res$events, ceiling(res$solution))
  expect_gte(res$power, 0.9)
})

test_that("solving for the follow-up gives the same final analysis time", {
  lo <- ms_looks("os", fraction = c(0.5, 1))
  res <- ms_size(scn_a(), looks = lo, test = ms_test(), power = 0.9,
                 solve_for = "followup", n = 500)
  # accrual of 500 patients at 20 per month ends at month 25
  expect_lt(abs(res$solution + 25 - 38.73277391908485), 1e-4)
  expect_lt(abs(res$power - 0.9), 1e-6)
})

test_that("ms_size solves for the sample size", {
  lo <- ms_looks("os", fraction = c(0.5, 1))
  te <- ms_test()
  res <- ms_size(scn_a(), looks = lo, test = te, power = 0.9, solve_for = "n",
                 followup = 12)
  expect_equal(res$n, ceiling(res$solution))
  expect_gte(res$power, 0.9)
  p_sol <- ms_power(scn_a(), n = res$solution, looks = lo, test = te, followup = 12)$power
  expect_lt(abs(p_sol - 0.9), 1e-6)
  p_less <- ms_power(scn_a(), n = res$n - 1, looks = lo, test = te, followup = 12)$power
  expect_lt(p_less, 0.9)
})

test_that("ms_size solves for the accrual duration", {
  s <- scn_a()
  s$accrual <- ms_accrual(prop = 1, duration = 20)
  lo <- ms_looks("os", fraction = c(0.5, 1))
  res <- ms_size(s, looks = lo, test = ms_test(), power = 0.85,
                 solve_for = "accrual_duration", n = 400, followup = 6)
  expect_lt(abs(res$power - 0.85), 1e-6)
  expect_equal(res$accrual_duration, res$solution)
})

test_that("ms_size checks its input", {
  lc <- ms_looks("calendar", time = c(24, 36))
  expect_error(ms_size(scn_a(), lc, ms_test(), solve_for = "events", n = 400),
               "event-driven")
  expect_error(ms_size(scn_a(), ms_looks("os"), ms_test(), solve_for = "n"), "followup")
  expect_error(ms_size(scn_a(), ms_looks("os"), ms_test(), solve_for = "accrual_duration",
                       n = 400, followup = 6), "prop")
  expect_error(ms_size(scn_a(), ms_looks("os"), ms_test(), solve_for = "events"), "'n'")
})

test_that("ms_size solves for the sample size with two endpoints", {
  te <- ms_test(endpoints = c("pfs", "os"), mtp = "fixed_sequence")
  lo <- ms_looks("os")
  res <- ms_size(scn_f(), looks = lo, test = te, power = 0.8, solve_for = "n",
                 followup = 6, interval = c(450, 520), target = "each")
  expect_identical(res$target, "each")
  expect_equal(res$n, ceiling(res$solution))
  expect_gte(min(res$power[c("pfs", "os")]), 0.8)
  p_sol <- ms_power(scn_f(), n = res$solution, looks = lo, test = te, followup = 6)$power
  expect_lt(abs(min(p_sol[c("pfs", "os")]) - 0.8), 1e-6)
  p_less <- ms_power(scn_f(), n = res$n - 1, looks = lo, test = te, followup = 6)$power
  expect_lt(min(p_less[c("pfs", "os")]), 0.8)
})

test_that("ms_size solves for the final numbers of events of two trigger endpoints", {
  te <- ms_test(endpoints = c("pfs", "os"), mtp = "fixed_sequence")
  lo <- ms_looks(c("pfs", "os"), fraction = c(0.8, 1), test = list("pfs", c("pfs", "os")))
  res <- ms_size(scn_f(), looks = lo, test = te, power = 0.8, solve_for = "events", n = 400)
  expect_named(res$solution, c("pfs", "os"))
  expect_equal(res$final_events, ceiling(res$solution))
  expect_gte(res$power[["pfs"]], 0.8)
  p_sol <- ms_power(scn_f(), n = 400, looks = lo, test = te, events = res$solution)$power
  expect_lt(abs(p_sol[["pfs"]] - 0.8), 1e-6)
})

test_that("ms_size checks the target and the null scenarios", {
  expect_error(ms_size(scn_a(), ms_looks("os"), ms_test(), solve_for = "events", n = 500,
                       target = "both"), "two endpoints")
  expect_error(ms_size(scn_a(), ms_looks("os"), ms_test(), solve_for = "events", n = 500,
                       null_scenarios = list(os = scn_a())), "two endpoints")
  lm <- ms_looks(c("os", "calendar"), fraction = c(0.5, NA), time = c(NA, 40))
  expect_error(ms_size(scn_a(), lm, ms_test(), solve_for = "events", n = 500),
               "event-driven final look")
})

test_that("ms_size checks the scope of the switching from a calendar time", {
  lo <- ms_looks("calendar", time = c(24, 36))
  expect_error(ms_size(scn_cal(), lo, ms_test(mean = "schoenfeld"), solve_for = "n",
                       interval = c(100, 1000)), "mean = \"direct\"")
  expect_error(ms_size(scn_cal(), lo, ms_test(endpoints = c("pfs", "os"), mtp = "bonferroni"),
                       solve_for = "n", interval = c(100, 1000)), "one endpoint only")
})

# Stage 3: recycling of the level and switching from a calendar time, with one look. Expected
# values: inst/validation/python/results/size_stage3.csv (expected_size_stage3.py: moments from
# the references of design2.csv and calendar_looks.csv, powers at one look by one-dimensional
# integrals, solutions by root finding over the time of the look)

size_expected <- function(design) {
  ex <- expected_csv("size_stage3.csv")
  e <- ex[ex$design == design, ]
  stats::setNames(e$value, e$quantity)
}

test_that("ms_size solves for the final number of events with recycling of the level", {
  e <- size_expected("recycling")
  te <- ms_test(endpoints = c("pfs", "os"), mtp = "bonferroni", weights = c(0.8, 0.2),
                transition = c(1, 1))
  res <- ms_size(scn_f(), looks = ms_looks("os"), test = te, power = 0.8, solve_for = "events",
                 n = 400, target = "each")
  # OS has the smaller power; without recycling, its power at the solution is about 0.63
  expect_lt(abs(res$solution - e[["solution"]]), 1e-4)
  expect_equal(unname(res$final_events), e[["events_rounded"]])
  expect_equal(res$study_duration, e[["time_rounded"]], tolerance = 1e-8)
  q <- c("pfs", "os", "both", "any")
  expect_lt(max(abs(res$power[q] - e[paste0("power_", q)])), 1e-6)
  expect_gte(min(res$power[c("pfs", "os")]), 0.8)
  # one look: the bounds after recycling are those at the level alpha
  expect_equal(res$looks$z_upper_recycled, rep(qnorm(0.975), 2), tolerance = 1e-10)
})

test_that("ms_size solves for the sample size with switching from a calendar time", {
  e <- size_expected("calendar_n")
  res <- ms_size(scn_cal(), looks = ms_looks("calendar", time = 36), test = ms_test(),
                 power = 0.8, solve_for = "n", interval = c(700, 850))
  # one look: n = ((z_0.975 + z_0.8) sqrt(v) / mu)^2 with the per-patient score mean mu and
  # information v at month 36
  expect_lt(abs(res$solution - e[["solution"]]), 1e-4)
  expect_equal(res$n, e[["n_rounded"]])
  expect_equal(res$events, e[["events_rounded"]], tolerance = 1e-9)
  expect_equal(res$power, e[["power"]], tolerance = 1e-8)
  expect_equal(res$type1[["nonbinding"]], 0.025, tolerance = 1e-10)
})

test_that("ms_size solves for the number of events with switching from a calendar time", {
  e <- size_expected("calendar_events")
  res <- ms_size(scn_cal(), looks = ms_looks("os"), test = ms_test(), power = 0.8,
                 solve_for = "events", n = 1000)
  expect_lt(abs(res$solution - e[["solution"]]), 1e-4)
  expect_equal(res$events, e[["events_rounded"]], tolerance = 1e-9)
  expect_equal(res$study_duration, e[["time_rounded"]], tolerance = 1e-8)
  expect_equal(res$power, e[["power"]], tolerance = 1e-8)
})

test_that("ms_size gives the expected values of the solved design", {
  lo <- ms_looks("os", fraction = c(0.5, 1))
  res <- ms_size(scn_a(), looks = lo, test = ms_test(), power = 0.9, solve_for = "events",
                 n = 500)
  chk <- ms_power(scn_a(), n = 500, looks = lo, test = ms_test(),
                  events = ceiling(res$solution - 1e-8))
  expect_false(anyNA(res$expected))
  expect_equal(res$expected, chk$expected)
  expect_equal(res$looks_null, chk$looks_null)
})

Try the DesignComplexSurvival package in your browser

Any scripts or data that you put into this service are public.

DesignComplexSurvival documentation built on Nov. 23, 2026, 5:07 p.m.