tests/testthat/test-rank_edges.R

# tests/testthat/test-rank_edges.R

test_that("rank_edges orders by ascending p-value", {
  pv <- matrix(1, 4, 4)
  pv[1, 2] <- pv[2, 1] <- 0.01
  pv[1, 3] <- pv[3, 1] <- 0.50
  pv[1, 4] <- pv[4, 1] <- 0.20
  pv[2, 3] <- pv[3, 2] <- 0.05
  pv[2, 4] <- pv[4, 2] <- 0.80
  pv[3, 4] <- pv[4, 3] <- 0.001

  edf <- rank_edges(pv)

  expect_s3_class(edf, "data.frame")
  expect_equal(names(edf), c("node1", "node2", "p_value"))
  # 4 nodes -> 6 upper-triangle edges
  expect_equal(nrow(edf), 6L)
  # Sorted ascending
  expect_true(all(diff(edf$p_value) >= 0))
  # Most significant first: (3,4) with 0.001
  expect_equal(edf$node1[1], 3L)
  expect_equal(edf$node2[1], 4L)
  expect_equal(edf$p_value[1], 0.001)
})

test_that("rank_edges only reports upper-triangle edges (i < j)", {
  set.seed(1)
  pv <- matrix(runif(25), 5, 5)
  pv <- (pv + t(pv)) / 2            # symmetrise
  diag(pv) <- 1
  edf <- rank_edges(pv)
  expect_true(all(edf$node1 < edf$node2))
  expect_equal(nrow(edf), 5 * 4 / 2)
})

test_that("rank_edges returns consecutive row names", {
  pv <- matrix(1, 4, 4)
  pv[1, 2] <- pv[2, 1] <- 0.90
  pv[1, 3] <- pv[3, 1] <- 0.10
  pv[2, 4] <- pv[4, 2] <- 0.01
  edf <- rank_edges(pv)
  # Sorting used to leave the pre-sort positions as row names, so the ranking
  # printed as e.g. 31, 39, 41 instead of 1, 2, 3.
  expect_identical(rownames(edf), as.character(seq_len(nrow(edf))))
})

test_that("rank_edges handles the 2-node degenerate case", {
  pv <- matrix(c(1, 0.02, 0.02, 1), 2, 2)
  edf <- rank_edges(pv)
  expect_equal(nrow(edf), 1L)
  expect_equal(edf$node1, 1L)
  expect_equal(edf$node2, 2L)
  expect_equal(edf$p_value, 0.02)
})

test_that("distinct stored p-values sort before endpoint tie breaking", {
  pv <- matrix(1, 4, 4)
  pv[1, 2] <- 0.05 + .Machine$double.eps
  pv[1, 3] <- pv[2, 3] <- 0.05
  edf <- rank_edges(pv)
  expect_identical(edf$node1[1:3], c(1L, 2L, 1L))
  expect_identical(edf$node2[1:3], c(3L, 3L, 2L))
  expect_identical(edf$p_value[1:3], c(0.05, 0.05, pv[1, 2]))
})

test_that("meaningfully distinct p-values keep their original order", {
  pv <- matrix(1, 4, 4)
  pv[1, 2] <- 0.05 + 1e-12
  pv[1, 3] <- 0.05 - 1e-12
  pv[2, 3] <- 0.1 * (1 + 128 * .Machine$double.eps)
  pv[1, 4] <- 0.1
  edf <- rank_edges(pv)
  expect_identical(edf$node1[1:4], c(1L, 1L, 1L, 2L))
  expect_identical(edf$node2[1:4], c(3L, 2L, 4L, 3L))
  # The ranking policy must never round a p-value across a test threshold.
  expect_identical(edf$p_value < 0.05,
                   pv[cbind(edf$node1, edf$node2)] < 0.05)
})

test_that("adjusted p-value plateaus break ties by raw marginal evidence", {
  # Under Bonferroni on 6 edges, raw p = 0.20 and raw p = 1.00 both clamp to
  # adjusted p = 1.00, while raw p = 0.001 becomes 0.006. Passing raw p-values
  # as tie_values ranks the stronger raw edge (3, 4) ahead of dead edge (1, 2)
  # while keeping the adjusted p-values in `p_value`.
  raw <- matrix(1, 4, 4)
  raw[1, 3] <- raw[3, 1] <- 0.001
  raw[3, 4] <- raw[4, 3] <- 0.20
  adj <- matrix(1, 4, 4)
  adj[1, 3] <- adj[3, 1] <- stats::p.adjust(raw[upper.tri(raw)], "bonferroni")[2]

  edf <- rank_edges(adj, tie_values = raw)
  expect_identical(edf$node1[1:2], c(1L, 3L))
  expect_identical(edf$node2[1:2], c(3L, 4L))
  expect_identical(edf$p_value[1:2], c(0.006, 1))
  expect_error(rank_edges(adj, tie_values = matrix(1, 3, 3)), "same dimensions")
})

test_that("multiplicity adjustment never changes which edges are removed", {
  control <- generate_category_graphs(n_graphs = 15, n_nodes = 10,
    n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, seed = 1)
  patient <- generate_category_graphs(n_graphs = 15, n_nodes = 10,
    n_communities = 2, base_intra_prob = 0.5, base_inter_prob = 0.5, seed = 2)
  pops <- list(Control = control, Patient = patient)
  for (criterion in c("original", "combined")) {
    none <- identify_critical_links(pops, n_permutations = 99, seed = 3,
                                    criterion = criterion)
    expect_gt(length(none$edges_removed), 1L)
    for (adj in c("bonferroni", "holm", "BH")) {
      res <- identify_critical_links(pops, n_permutations = 99, seed = 3,
                                     criterion = criterion, adjust_method = adj)
      expect_identical(res$edges_removed, none$edges_removed,
                       info = paste(criterion, adj))
      expect_identical(res$p_value, none$p_value)
      expect_true(all(res$critical_edges$p_value >=
                        none$critical_edges$p_value))
    }
  }
})

Try the BrainNetTest package in your browser

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

BrainNetTest documentation built on Dec. 3, 2026, 5:08 p.m.