tests/test-properties.R

library(posthocbounds)
set.seed(20261107)

for (m in c(2, 5, 40, 3000)) {
  z <- rnorm(m, mean = rep(c(3, 0), length.out = m))
  for (b in c(1, 2)) {
    if (b > m) next
    fit <- combined_bound(z, block_size = b)
    rb <- ranked_bounds(fit)
    ## the envelope is nondecreasing and never exceeds k
    stopifnot(all(diff(rb$true_at_least) >= 0), all(rb$true_at_least <= rb$k),
              all(rb$true_at_least >= 0))
    ## the envelope agrees with the bound for each selection
    k <- unique(c(1, ceiling(m / 2), m))
    td <- true_discoveries(fit, lapply(k, function(j) order(-z)[seq_len(j)]))
    stopifnot(identical(td$true_at_least, rb$true_at_least[k]))
    ## the FDP bound per hypothesis is nondecreasing in the rank
    ht <- hypothesis_table(fit, sort = TRUE)
    stopifnot(all(diff(ht$fdp_at_most) >= -1e-12))
    sel <- select_by_fdp(fit, 0.2)
    if (length(sel)) stopifnot(true_discoveries(fit, sel)$fdp_at_most <= 0.2)
  }
}

## no signal: the bound on false discoveries among all hypotheses is close to m
fit <- combined_bound(rnorm(1000))
stopifnot(fit$false_all >= 900)

## selections by labels, logical vectors and empty selections
z <- setNames(rnorm(50, 2), paste0("H", 1:50))
fit <- combined_bound(z)
a <- true_discoveries(fit, c("H1", "H2", "H3"))
b <- true_discoveries(fit, 1:3)
stopifnot(identical(a$true_at_least, b$true_at_least),
          true_discoveries(fit, integer(0))$selected == 0)

## extreme p-values are accepted
fit <- combined_bound(p = c(0, 1, runif(20)))
stopifnot(fit$false_all <= 22)

## input errors
stopifnot(inherits(try(combined_bound(c(1, NA)), silent = TRUE), "try-error"),
          inherits(try(combined_bound(1), silent = TRUE), "try-error"),
          inherits(try(combined_bound(rnorm(10), alpha = 2), silent = TRUE), "try-error"))

Try the posthocbounds package in your browser

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

posthocbounds documentation built on Nov. 28, 2026, 1:08 a.m.