tests/testthat/test-matrix-utils.R

################################################################################
# correct_positive_definite_simplified() ------------------------------------
# (internal, but the one actually used by extract_estimation_info_*())

test_that("correct_positive_definite_simplified leaves an already-PD matrix untouched", {
  m <- diag(3)
  res <- LTFGRS:::correct_positive_definite_simplified(m)
  expect_identical(res$covmat, m)
  expect_equal(res$nitr, 0)
})

test_that("correct_positive_definite_simplified actually returns a corrected matrix on success", {
  # Regression test: the final success check used to re-test the *original*
  # eigenvalues (computed before the while loop ran), which were guaranteed
  # to still contain a negative value - the only reason the branch was
  # entered in the first place. That made the function stop() unconditionally
  # whenever correction was needed at all, even when the while loop had
  # already found a genuinely positive-definite matrix.
  m <- matrix(1.5, 3, 3)
  diag(m) <- 1
  expect_true(any(eigen(m)$values < 0))  # sanity check on the input itself

  res <- LTFGRS:::correct_positive_definite_simplified(m)
  expect_true(res$nitr > 0)
  expect_true(all(eigen(res$covmat)$values >= 0))
  # diagonal must be preserved exactly through every correction step
  expect_equal(diag(res$covmat), diag(m))
})

test_that("correct_positive_definite_simplified errors when correction_limit is exhausted", {
  m <- matrix(1.5, 3, 3)
  diag(m) <- 1
  # the matrix above needs ~41 iterations at the default correction_val to
  # become PD (verified separately) - a limit of 2 is nowhere near enough
  expect_error(
    LTFGRS:::correct_positive_definite_simplified(m, correction_limit = 2),
    "Unable to enforce"
  )
})

test_that("correct_positive_definite_simplified's correction_val controls convergence speed", {
  m <- matrix(1.5, 3, 3)
  diag(m) <- 1
  res_slow <- LTFGRS:::correct_positive_definite_simplified(m, correction_val = 0.99)
  res_fast <- LTFGRS:::correct_positive_definite_simplified(m, correction_val = 0.9)
  expect_true(res_fast$nitr < res_slow$nitr)
})

################################################################################
# correct_positive_definite() ------------------------------------------------

test_that("correct_positive_definite leaves an already-PD matrix untouched", {
  m <- diag(3)
  expect_message(res <- correct_positive_definite(m), NA)  # no message expected
  expect_identical(res, m)
})

test_that("correct_positive_definite errors on non-symmetric or non-numeric input", {
  expect_error(correct_positive_definite(matrix(1:6, 2, 3)), "symmetric")
  m <- diag(3); m[1, 2] <- 5  # break symmetry
  expect_error(correct_positive_definite(m), "symmetric")
})

test_that("correct_positive_definite warns and returns the original matrix when attributes are missing", {
  m <- matrix(1.5, 3, 3)
  diag(m) <- 1
  expect_true(any(eigen(m)$values < 0))  # not PD, so correction would be attempted
  expect_warning(res <- correct_positive_definite(m), "required attributes are missing")
  expect_identical(res, m)
})

test_that("correct_positive_definite warns and returns the original matrix for single-phenotype covmats", {
  m <- matrix(1.5, 3, 3)
  diag(m) <- 1
  attr(m, "add_ind") <- TRUE
  attr(m, "h2") <- 0.5  # length 1 -> can't be corrected
  attr(m, "genetic_corrmat") <- matrix(1)
  attr(m, "full_corrmat") <- matrix(1)
  expect_warning(res <- correct_positive_definite(m), "cannot be corrected")
  expect_identical(res, m)
})

test_that("correct_positive_definite successfully corrects a genuinely non-PD multi-phenotype covmat", {
  # A correlation structure that violates the triangle inequality (pairwise
  # correlations 0.9, 0.9, -0.9) reliably produces an indefinite covariance
  # matrix via construct_covmat(), giving a realistic (not hand-built)
  # regression case for the actual attribute-driven correction path.
  ntrait <- 3
  corrmat <- matrix(1, ntrait, ntrait)
  corrmat[1, 2] <- corrmat[2, 1] <- 0.9
  corrmat[2, 3] <- corrmat[3, 2] <- 0.9
  corrmat[1, 3] <- corrmat[3, 1] <- -0.9

  cov <- construct_covmat(fam_vec = c("m"), genetic_corrmat = corrmat, full_corrmat = corrmat,
                          h2 = rep(0.9, ntrait), phen_names = c("p1", "p2", "p3"))
  expect_true(any(eigen(cov)$values < 0))  # sanity check on the input itself

  expect_message(res <- correct_positive_definite(cov), "correction was performed successfully")
  expect_true(all(eigen(res)$values >= 0))
  expect_false(identical(res, cov))
})

test_that("correct_positive_definite gives up and returns the original matrix when correction_limit is exhausted", {
  ntrait <- 3
  corrmat <- matrix(1, ntrait, ntrait)
  corrmat[1, 2] <- corrmat[2, 1] <- 0.9
  corrmat[2, 3] <- corrmat[3, 2] <- 0.9
  corrmat[1, 3] <- corrmat[3, 1] <- -0.9

  cov <- construct_covmat(fam_vec = c("m"), genetic_corrmat = corrmat, full_corrmat = corrmat,
                          h2 = rep(0.9, ntrait), phen_names = c("p1", "p2", "p3"))

  expect_message(res <- correct_positive_definite(cov, correction_limit = 1),
                 "could not be corrected")
  expect_identical(res, cov)
})

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.