tests/testthat/test-calcu_adjZ.R

#Use the testthat package to test data
#Created by ZWu, Feb 14, 2025.

library(testthat)
library(DPComb)


### Testing the `computeZ` function.
test_that("computeZ works for fisher_mean with f_prev = 0", {
  f <- 0.3
  f_prev <- 0
  expected <- 2 * (1 - log(f))
  res <- computeZ(f, f_prev, "fisher_mean")
  expect_equal(res, expected)
})

test_that("computeZ works for fisher_mean with non-zero f_prev", {
  f <- 0.5
  f_prev <- 0.4
  eps <- 1e-10
  d <- f - f_prev
  if(d < eps) {
    expected <- -2 * log((f + f_prev)/2)
  } else {
    expected <- 2 * (1 - (f * log(f) - f_prev * log(f_prev)) / d)
  }
  res <- computeZ(f, f_prev, "fisher_mean")
  expect_equal(res, expected)
})

test_that("computeZ returns error for invalid p-values", {
  expect_error(computeZ(1.1, 0.5, "fisher_mean"))
  expect_error(computeZ(0.5, 1.1, "fisher_mean"))
})

test_that("computeZ works for pearson method", {
  f <- 0.6
  f_prev <- 0.5
  eps <- 1e-10
  if(f > 1 - eps) {
    expected <- 2 * (1 - log(f_prev))
  } else if((f - f_prev) < eps) {
    expected <- -2 * log(((1 - f) + (1 - f_prev)) / 2)
  } else {
    expected <- 2 - 2 * (((1 - f_prev) * log(1 - f_prev) - (1 - f) * log(1 - f)) / (f - f_prev))
  }
  res <- computeZ(f, f_prev, "pearson")
  expect_equal(res, expected)
})

test_that("computeZ works for stouffer method with d >= eps", {
  f <- 0.6
  f_prev <- 0.5
  # d = 0.1 >= 1e-10; use the second branch
  expected <- (-dnorm(qnorm(f)) + dnorm(qnorm(f_prev)))/(f - f_prev)
  res <- computeZ(f, f_prev, "stouffer")
  expect_equal(res, expected)
})

test_that("computeZ works for stouffer method with d < eps", {
  f <- 0.5
  f_prev <- f - 1e-11  # Force d < eps
  expected <- pnorm((qnorm(f) + qnorm(f_prev))/2)/sqrt(2*pi)
  res <- computeZ(f, f_prev, "stouffer")
  expect_equal(res, expected)
})


### Testing the `adjZ_moments` function 
test_that("adjZ_moments returns list with numeric Z, Zmean, and Zvar", {
  p_support <- c(0.1, 0.3, 1)
  res <- adjZ_moments(0.3, p_support, "fisher_mean")
  expect_true(is.numeric(res$Z))
  expect_true(is.numeric(res$Zmean))
  expect_true(is.numeric(res$Zvar))
})

test_that("adjZ_moments warns if p is not in p_support", {
  p_support <- c(0.1, 0.3, 1)
  expect_warning(adjZ_moments(0.2, p_support, "fisher_mean"))
})

Try the DPComb package in your browser

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

DPComb documentation built on Aug. 22, 2026, 5:08 p.m.