tests/testthat/test-pa-algorithm.R

################################################################################

test_that("PA_algorithm gives the same result regardless of target position", {
  # Regression test for a reordering bug: the `ordr` computation used to
  # move the target individual to the first row/column relied on
  # `1:nrow(covmat)[-target_indx]`, which (due to `[` binding tighter than
  # `:` in R) silently produced a too-long, duplicated index vector whenever
  # target_indx != 1. In practice this package always calls PA_algorithm()
  # with a target that has lower = -Inf, upper = Inf (the latent genetic
  # liability row), which happened to make the bug a no-op -- but any call
  # where the target itself carries real threshold data exposed a wrong
  # answer. See dev/PA_algorithm_investigation.R for the full investigation.

  nm <- c("target", "a", "b")
  covmat <- matrix(
    c(1.00, 0.25, 0.25,
      0.25, 1.00, 0.00,
      0.25, 0.00, 1.00),
    nrow = 3, byrow = TRUE,
    dimnames = list(nm, nm)
  )
  mu    <- setNames(rep(0, 3), nm)
  lower <- c(target = 0.3, a = 1.5,  b = -Inf)
  upper <- c(target = Inf, a =  Inf, b = 0.5)

  res_first <- PA_algorithm(mu = mu, covmat = covmat, lower = lower, upper = upper,
                            target_id = "target")

  reorder <- c("a", "target", "b")
  res_second <- PA_algorithm(mu = mu[reorder], covmat = covmat[reorder, reorder],
                             lower = lower[reorder], upper = upper[reorder],
                             target_id = "target")

  expect_equal(res_first, res_second)
})

################################################################################

test_that("PA_algorithm is invariant to target position when target is uninformative", {
  # The everyday case for this package: target has lower = -Inf, upper = Inf
  # (the latent genetic liability row). Kept as a companion test since this
  # is the path actually exercised by estimate_liability().

  set.seed(1)
  n <- 5
  nm <- c("target", paste0("r", 1:(n - 1)))
  covmat <- diag(n)
  dimnames(covmat) <- list(nm, nm)
  for (i in 2:n) for (j in 2:n) if (i != j) covmat[i, j] <- 0.25
  for (i in 2:n) covmat[1, i] <- covmat[i, 1] <- 0.3

  mu_base    <- setNames(rep(0, n), nm)
  lower_base <- setNames(c(-Inf, stats::runif(n - 1, -1, 1)), nm)
  upper_base <- lower_base + c(0, stats::runif(n - 1, 0.1, 2))
  upper_base["target"] <- Inf

  results <- lapply(1:n, function(target_pos) {
    perm <- append(setdiff(nm, "target"), "target", after = target_pos - 1)
    PA_algorithm(mu = mu_base[perm], covmat = covmat[perm, perm],
                lower = lower_base[perm], upper = upper_base[perm],
                target_id = "target")
  })

  for (res in results[-1]) {
    expect_equal(res, results[[1]])
  }
})

################################################################################

Try the LTFGRS package in your browser

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

LTFGRS documentation built on Nov. 20, 2026, 5:10 p.m.