tests/testthat/test-gs_prob_mtp.R

# Expected values: inst/validation/python/results/gs_prob_mtp.csv, computed by
# ref_mtp.recursion() (numerical integration over the analyses of the joint
# density, with the decisions of the procedure applied on the sample path)
# and, for two looks, by the formulas of Glimm et al. (2010, equations (1)
# and (2)) written as one-dimensional integrals (ref_mtp.glimm_two_look()).
# Configurations: gs_prob_mtp_configs.csv. Rectangles over both endpoints
# with four or more coordinates are computed by the Genz-Bretz algorithm
# with the error tolerance 1e-6 (with_abseps()), and the probabilities,
# sums of up to ten such rectangles, are compared at a tolerance of 1e-5.

mtp_check_config <- function(id, method, mtp = NULL) {
  cf <- expected_csv("gs_prob_mtp_configs.csv")
  ex <- expected_csv("gs_prob_mtp.csv")
  cf <- cf[cf$config == id, ]
  ex <- ex[ex$config == id & ex$method == method, ]
  if (!is.null(mtp)) {
    ex <- ex[paste(ex$mtp, ex$hierarchical) == mtp, ]
  }
  runs <- unique(ex[, c("mtp", "hierarchical", "hypothesis")])
  a1 <- mtp_num(cf$a1)
  a2 <- mtp_num(cf$a2)
  n <- 0L
  for (r in seq_len(nrow(runs))) {
    e <- ex[ex$mtp == runs$mtp[r] & ex$hierarchical == runs$hierarchical[r] &
              ex$hypothesis == runs$hypothesis[r], ]
    cn <- mtp_canonical(mtp_num(cf$tau1), mtp_num(cf$tau2), cf$rho, e$theta1[1],
                        e$theta2[1], a1, a2)
    res <- with_abseps(gs_prob_mtp(list(mtp_num(cf$u1), mtp_num(cf$u2)),
                                   lower = mtp_num(cf$l1), mean = cn$mean,
                                   sigma = cn$sigma, analysis = list(a1, a2),
                                   mtp = runs$mtp[r], hierarchical = runs$hierarchical[r]))
    q <- mtp_quantities(res)
    expect_true(all(e$quantity %in% names(q)))
    expect_lt(max(abs(q[e$quantity] - e$value)), 1e-5)
    n <- n + nrow(e)
  }
  n
}

# three looks of each endpoint and futility bounds: 3 hypotheses, 13 quantities
test_that("gs_prob_mtp matches the recursion with three looks (overall)", {
  expect_identical(mtp_check_config("three_looks", "recursion", "fixed_sequence overall"), 39L)
})

test_that("gs_prob_mtp matches the recursion with three looks (stagewise)", {
  expect_identical(mtp_check_config("three_looks", "recursion", "fixed_sequence stagewise"), 39L)
})

test_that("gs_prob_mtp matches the recursion with three looks (Bonferroni)", {
  expect_identical(mtp_check_config("three_looks", "recursion", "bonferroni overall"), 39L)
})

test_that("gs_prob_mtp matches the recursion with different analyses (overall)", {
  expect_identical(mtp_check_config("unequal", "recursion", "fixed_sequence overall"), 33L)
})

test_that("gs_prob_mtp matches the recursion with different analyses (stagewise)", {
  expect_identical(mtp_check_config("unequal", "recursion", "fixed_sequence stagewise"), 33L)
})

test_that("gs_prob_mtp matches the recursion with different analyses (Bonferroni)", {
  expect_identical(mtp_check_config("unequal", "recursion", "bonferroni overall"), 33L)
})

test_that("gs_prob_mtp matches the recursion when the second endpoint starts later (overall)", {
  expect_identical(mtp_check_config("late_second", "recursion", "fixed_sequence overall"), 36L)
})

test_that("gs_prob_mtp matches the recursion when the second endpoint starts later (stagewise)", {
  expect_identical(mtp_check_config("late_second", "recursion", "fixed_sequence stagewise"),
                   36L)
})

test_that("gs_prob_mtp matches the recursion when the second endpoint starts later (Bonferroni)", {
  expect_identical(mtp_check_config("late_second", "recursion", "bonferroni overall"), 36L)
})

test_that("gs_prob_mtp matches the formulas of Glimm et al. for two looks", {
  # strategies (a) and (b): 5 and 3 quantities for each correlation
  n <- mtp_check_config("glimm_rho0", "glimm") + mtp_check_config("glimm_rho08", "glimm")
  expect_identical(n, 16L)
  n <- mtp_check_config("glimm_rho0", "recursion") + mtp_check_config("glimm_rho08", "recursion")
  expect_identical(n, 60L)
})

test_that("gs_prob_mtp agrees with gs_prob for the marginal probabilities", {
  cn <- mtp_canonical(c(0.4, 0.7, 1), c(0.4, 0.7, 1), 0.6, 3, 2.5, 1:3, 1:3)
  u1 <- c(3, 2.4, 2)
  u2 <- c(2.6, 2.3, 2.1)
  l1 <- c(0, 1, 2)
  m1 <- gs_prob(l1, u1, cn$mean[1:3], cn$sigma[1:3, 1:3])
  m2 <- gs_prob(rep(-Inf, 3), u2, cn$mean[4:6], cn$sigma[4:6, 4:6])
  for (h in c("overall", "stagewise")) {
    r <- gs_prob_mtp(list(u1, u2), l1, cn$mean, cn$sigma, hierarchical = h)
    expect_equal(r$looks$prob_upper[1:3], m1$prob_upper, tolerance = 1e-8)
    expect_equal(r$looks$prob_lower[1:2], m1$prob_lower[1:2], tolerance = 1e-8)
    expect_equal(unname(r$reject["any"]),
                 unname(r$reject["first"] + r$reject["second"] - r$reject["both"]))
  }
  # Bonferroni without futility: the second endpoint is tested at all looks
  # (sums of rectangles over both endpoints: tolerance 1e-5)
  rb <- with_abseps(gs_prob_mtp(list(u1, u2), NULL, cn$mean, cn$sigma, mtp = "bonferroni"))
  expect_lt(max(abs(rb$looks$prob_upper[4:6] - m2$prob_upper)), 1e-5)
  # fixed sequence with the first hypothesis always rejected at the first look
  ro <- with_abseps(gs_prob_mtp(list(c(-Inf, u1[2:3]), u2), NULL, cn$mean, cn$sigma))
  expect_lt(max(abs(ro$looks$prob_upper[4:6] - m2$prob_upper)), 1e-5)
  expect_equal(unname(ro$reject["both"]), unname(ro$reject["second"]), tolerance = 1e-7)
  # rejection of the second hypothesis: stagewise <= overall <= Bonferroni
  # (Glimm et al. 2010, inequality (5))
  rs <- gs_prob_mtp(list(u1, u2), NULL, cn$mean, cn$sigma, hierarchical = "stagewise")
  rv <- gs_prob_mtp(list(u1, u2), NULL, cn$mean, cn$sigma, hierarchical = "overall")
  expect_lt(rs$reject[["second"]], rv$reject[["second"]])
  expect_lt(rv$reject[["second"]], rb$reject[["second"]])
  # an infinite bound means no test at that look
  ri <- gs_prob_mtp(list(u1, c(Inf, u2[2:3])), NULL, cn$mean, cn$sigma)
  rj <- gs_prob_mtp(list(u1, u2[2:3]), NULL, cn$mean[-4], cn$sigma[-4, -4],
                    analysis = list(1:3, 2:3))
  expect_equal(ri$reject, rj$reject, tolerance = 1e-8)
  expect_identical(ri$looks$prob_upper[4], 0)
})

test_that("gs_prob_mtp checks its arguments", {
  s <- diag(4)
  expect_error(gs_prob_mtp(c(2, 2), sigma = s), "list of two")
  expect_error(gs_prob_mtp(list(c(2, 2), 2), sigma = diag(3)), "analysis")
  expect_error(gs_prob_mtp(list(c(2, 2), c(2, 2)), lower = c(3, 0), sigma = s), "exceed")
  expect_error(gs_prob_mtp(list(c(2, 2), c(2, 2)), lower = 0, sigma = s), "lower")
  expect_error(gs_prob_mtp(list(c(2, 2), c(2, 2)), mean = 1:3, sigma = s), "mean")
  expect_error(gs_prob_mtp(list(c(2, 2), c(2, 2)), sigma = diag(3)), "sigma")
  expect_error(gs_prob_mtp(list(c(2, 2), c(2, 2)), sigma = s, analysis = list(c(2, 1), 1:2)),
               "analysis")
  expect_error(gs_prob_mtp(list(rep(2, 11), rep(2, 10)), sigma = diag(21),
                           analysis = list(1:11, 1:10)), "20 looks")
  expect_error(gs_prob_mtp(list(c(2, 2), c(2, 2)), sigma = s, mtp = "holm"))
})

# Recycling of the level (upper_recycled). Expected values:
# inst/validation/python/results/gs_prob_graph.csv, computed by
# ref_graph.recursion() (numerical integration over the analyses of the joint
# density, with the decisions of Algorithm 1 of Maurer and Bretz (2013)
# applied on the sample path) and checked against a Monte Carlo simulation of
# the algorithm (gs_prob_graph_mc.csv). Configurations:
# gs_prob_graph_configs.csv. The error tolerance of the Genz-Bretz algorithm
# is eps for each rectangle, and the probabilities are compared at tol.
graph_check_config <- function(id, hypotheses, eps = 1e-6, tol = 1e-5) {
  cf <- expected_csv("gs_prob_graph_configs.csv")
  ex <- expected_csv("gs_prob_graph.csv")
  cf <- cf[cf$config == id, ]
  ex <- ex[ex$config == id & ex$hypothesis %in% hypotheses, ]
  a1 <- mtp_num(cf$a1)
  a2 <- mtp_num(cf$a2)
  n <- 0L
  for (h in hypotheses) {
    e <- ex[ex$hypothesis == h, ]
    cn <- mtp_canonical(mtp_num(cf$tau1), mtp_num(cf$tau2), cf$rho, e$theta1[1],
                        e$theta2[1], a1, a2)
    res <- with_abseps(gs_prob_mtp(list(mtp_num(cf$u1), mtp_num(cf$u2)),
                                   lower = mtp_num(cf$l1), mean = cn$mean,
                                   sigma = cn$sigma, analysis = list(a1, a2),
                                   mtp = "bonferroni",
                                   upper_recycled = list(mtp_num(cf$r1), mtp_num(cf$r2))),
                       eps)
    q <- mtp_quantities(res)
    expect_true(all(e$quantity %in% names(q)))
    expect_lt(max(abs(q[e$quantity] - e$value)), tol)
    n <- n + nrow(e)
  }
  n
}

# 13 quantities for three looks of each endpoint, 11 for two and three looks,
# 12 for three and two looks
test_that("gs_prob_mtp with recycling matches the recursion with three looks (1)", {
  # under the alternative, the 14 rectangles over both endpoints took about
  # 9 seconds with the error tolerance 1e-6; with the default tolerance 1e-5
  # the errors of the probabilities were at most 1.6e-5 in the Python port
  # (quasi-Monte Carlo of scipy with the same tolerance), so that the
  # comparison is at 5e-5
  expect_identical(graph_check_config("three_looks", "alternative", eps = 1e-5, tol = 5e-5),
                   13L)
})

test_that("gs_prob_mtp with recycling matches the recursion with three looks (2)", {
  expect_identical(graph_check_config("three_looks", "null_first"), 13L)
})

test_that("gs_prob_mtp with recycling matches the recursion with three looks (3)", {
  expect_identical(graph_check_config("three_looks", c("null_second", "null_both")), 26L)
})

test_that("gs_prob_mtp with recycling matches the recursion with different analyses (1)", {
  expect_identical(graph_check_config("unequal", c("alternative", "null_first")), 22L)
})

test_that("gs_prob_mtp with recycling matches the recursion with different analyses (2)", {
  expect_identical(graph_check_config("unequal", c("null_second", "null_both")), 22L)
})

test_that("gs_prob_mtp with recycling matches the recursion when the second starts later (1)", {
  expect_identical(graph_check_config("late_second", c("alternative", "null_first")), 24L)
})

test_that("gs_prob_mtp with recycling matches the recursion when the second starts later (2)", {
  expect_identical(graph_check_config("late_second", c("null_second", "null_both")), 24L)
})

test_that("gs_prob_mtp with recycling matches the recursion with a high futility bound (1)", {
  # futility bound above the bound after recycling, no test of the second
  # hypothesis at its initial level at look 1
  expect_identical(graph_check_config("lower_above", c("alternative", "null_first")), 26L)
})

test_that("gs_prob_mtp with recycling matches the recursion with a high futility bound (2)", {
  expect_identical(graph_check_config("lower_above", c("null_second", "null_both")), 26L)
})

test_that("gs_prob_mtp with recycling matches the recursion when the first starts later (1)", {
  expect_identical(graph_check_config("first_late", c("alternative", "null_first")), 22L)
})

test_that("gs_prob_mtp with recycling matches the recursion when the first starts later (2)", {
  expect_identical(graph_check_config("first_late", c("null_second", "null_both")), 22L)
})

# two looks of each endpoint with a futility bound
graph_two_looks <- function() {
  cn <- mtp_canonical(c(0.5, 1), c(0.5, 1), 0.6, 3, 2.5, 1:2, 1:2)
  list(mean = cn$mean, sigma = cn$sigma, u1 = c(2.8, 2), u2 = c(2.6, 2.1), l1 = c(0.5, 2))
}

# three looks of each endpoint with futility bounds (configuration
# three_looks of gs_prob_mtp_configs.csv)
graph_three_looks <- function() {
  cn <- mtp_canonical(c(0.4, 0.7, 1), c(0.4, 0.7, 1), 0.6, 3, 2.5, 1:3, 1:3)
  list(mean = cn$mean, sigma = cn$sigma, u1 = c(3, 2.4, 2), u2 = c(2.6, 2.3, 2.1),
       l1 = c(0, 1, 2))
}

test_that("gs_prob_mtp with recycling reduces to the Bonferroni procedure", {
  g <- graph_two_looks()
  # bounds after recycling equal to the initial bounds
  rb <- with_abseps(gs_prob_mtp(list(g$u1, g$u2), g$l1, g$mean, g$sigma, mtp = "bonferroni"))
  rg <- with_abseps(gs_prob_mtp(list(g$u1, g$u2), g$l1, g$mean, g$sigma, mtp = "bonferroni",
                                upper_recycled = list(g$u1, g$u2)))
  expect_lt(max(abs(mtp_quantities(rg) - mtp_quantities(rb))), 1e-5)
  expect_identical(rg$looks[, 1:3], rb$looks[, 1:3])
})

test_that("gs_prob_mtp with recycling reduces to the fixed-sequence procedure", {
  g <- graph_two_looks()
  # no test of the second hypothesis at its initial level: fixed sequence
  # (overall)
  rf <- with_abseps(gs_prob_mtp(list(g$u1, g$u2), g$l1, g$mean, g$sigma))
  rh <- with_abseps(gs_prob_mtp(list(g$u1, rep(Inf, 2)), g$l1, g$mean, g$sigma,
                                mtp = "bonferroni", upper_recycled = list(g$u1, g$u2)))
  expect_lt(max(abs(mtp_quantities(rh) - mtp_quantities(rf))), 1e-5)
})

test_that("gs_prob_mtp rejects more often with lower bounds after recycling", {
  # default error tolerance of the Genz-Bretz algorithm (1e-5), enough for
  # differences above 0.01
  g <- graph_three_looks()
  rb <- gs_prob_mtp(list(g$u1, g$u2), g$l1, g$mean, g$sigma, mtp = "bonferroni")
  rr <- gs_prob_mtp(list(g$u1, g$u2), g$l1, g$mean, g$sigma, mtp = "bonferroni",
                    upper_recycled = list(g$u1 - 0.3, g$u2 - 0.3))
  expect_gt(rr$reject[["first"]], rb$reject[["first"]] + 0.01)
  expect_gt(rr$reject[["second"]], rb$reject[["second"]] + 0.01)
  expect_equal(unname(rr$reject["any"]),
               unname(rr$reject["first"] + rr$reject["second"] - rr$reject["both"]))
})

test_that("gs_prob_mtp checks the bounds after recycling", {
  s <- diag(4)
  u <- list(c(2, 2), c(2, 2))
  expect_error(gs_prob_mtp(u, sigma = s, upper_recycled = u), "only with mtp")
  expect_error(gs_prob_mtp(u, sigma = s, mtp = "bonferroni", upper_recycled = c(2, 2)),
               "list of two")
  expect_error(gs_prob_mtp(u, sigma = s, mtp = "bonferroni",
                           upper_recycled = list(2, c(2, 2))), "one value per look")
  expect_error(gs_prob_mtp(u, sigma = s, mtp = "bonferroni",
                           upper_recycled = list(c(2, NA), c(2, 2))), "one value per look")
  expect_error(gs_prob_mtp(u, sigma = s, mtp = "bonferroni",
                           upper_recycled = list(c(2, 2.1), c(2, 2))), "must not exceed")
})

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.