tests/testthat/test-prepare-thresholds.R

################################################################################
# testing prepare_thresholds() default interpolation value
################################################################################

# Regression test: prepare_thresholds() defaulted interpolation to NULL,
# and is.na(NULL) is logical(0), which breaks the very first
# `if (!is.na(interpolation) && ...)` check with "argument is of length
# zero" / "missing value where TRUE/FALSE needed". The function's own
# @examples had to explicitly pass interpolation = NA to avoid this.
# Fixed by defaulting to NA instead. See R/prepare-thresholds.R.

test_that("prepare_thresholds does not error when interpolation is left at its default", {
  tbl <- data.frame(
    fid = c(1, 1, 1, 1), pid = c(1, 2, 3, 4), role = c("o", "m", "f", "pgf"),
    sex = c(1, 0, 1, 1), status = c(0, 0, 1, 1), age = c(22, 42, 48, 78),
    birth_year = 2023 - c(22, 42, 48, 78), aoo = c(NA, NA, 43, 45)
  )
  cip <- data.frame(
    age = c(22, 42, 43, 45, 48, 78), birth_year = c(2001, 1981, 1975, 1945, 1975, 1945),
    sex = c(1, 0, 1, 1, 1, 1), cip = c(0.1, 0.2, 0.3, 0.3, 0.3, 0.4)
  )
  expect_no_error(prepare_thresholds(.tbl = tbl, CIP = cip, age_col = "age"))
})

################################################################################
# testing prepare_thresholds()'s unified K_i/K_pop/personal_thr handling
################################################################################

# Regression tests for the prepare_thresholds() refactor: interpolation = NA
# (the raw-merge path) previously never computed K_i/K_pop at all, and
# always behaved as though personal_thr = TRUE regardless of what was
# passed. It now computes both, respects personal_thr like the xgboost
# path always did, and personal_thr now defaults to TRUE (previously
# FALSE), matching prepare_thresholds_multi()'s existing default. See
# R/prepare-thresholds.R.

test_that("prepare_thresholds (interpolation = NA) computes K_i and K_pop", {
  tbl <- data.frame(pid = 1, sex = 1, status = 0, age = 42, birth_year = 2000)
  # two CIP entries in the same (sex, birth_year) stratum, at different
  # ages, so K_pop (max in stratum) differs from this individual's own K_i
  cip <- data.frame(
    age = c(42, 50), birth_year = c(2000, 2000), sex = c(1, 1), cip = c(0.2, 0.35)
  )

  res <- prepare_thresholds(.tbl = tbl, CIP = cip, age_col = "age")

  expect_true(is.data.frame(res)) # regression: must not silently return NULL
  expect_equal(res$K_i, 0.2)
  expect_equal(res$K_pop, 0.35)
})

test_that("prepare_thresholds (interpolation = NA) respects personal_thr", {
  tbl <- data.frame(pid = 1, sex = 1, status = 0, age = 42, birth_year = 2000)
  cip <- data.frame(
    age = c(42, 50), birth_year = c(2000, 2000), sex = c(1, 1), cip = c(0.2, 0.35)
  )

  res_personal <- prepare_thresholds(.tbl = tbl, CIP = cip, age_col = "age", personal_thr = TRUE)
  res_pop      <- prepare_thresholds(.tbl = tbl, CIP = cip, age_col = "age", personal_thr = FALSE)

  expect_equal(res_personal$thr, qnorm(0.2, lower.tail = FALSE))
  expect_equal(res_pop$thr, qnorm(0.35, lower.tail = FALSE))
  expect_false(isTRUE(all.equal(res_personal$thr, res_pop$thr)))
})

test_that("prepare_thresholds defaults to personal_thr = TRUE", {
  tbl <- data.frame(pid = 1, sex = 1, status = 0, age = 42, birth_year = 2000)
  cip <- data.frame(
    age = c(42, 50), birth_year = c(2000, 2000), sex = c(1, 1), cip = c(0.2, 0.35)
  )

  res_default  <- prepare_thresholds(.tbl = tbl, CIP = cip, age_col = "age")
  res_explicit <- prepare_thresholds(.tbl = tbl, CIP = cip, age_col = "age", personal_thr = TRUE)

  expect_equal(res_default$thr, res_explicit$thr)
})

test_that("prepare_thresholds warns (not errors) when K_i exceeds K_pop", {
  tbl <- data.frame(pid = 1, sex = 1, status = 0, age = 42, birth_year = 2000)
  cip <- data.frame(age = 42, birth_year = 2000, sex = 1, cip = 0.5)
  # deliberately-too-low custom K_pop, lower than this individual's K_i
  Kpop <- data.frame(sex = 1, birth_year = 2000, K_pop = 0.1)

  expect_warning(
    prepare_thresholds(.tbl = tbl, CIP = cip, age_col = "age", Kpop = Kpop),
    "K_i is greater than K_pop"
  )
})

################################################################################
# smoke test for prepare_thresholds_multi() - it has no runnable @examples
# of its own, and depends entirely on prepare_thresholds() above
################################################################################

test_that("prepare_thresholds_multi runs end to end across two phenotypes", {
  tbl <- data.frame(
    pid = 1, sex = 1, birth_year = 2000,
    status_height = 0, age_eof_height = 42,
    status_bmi = 1, age_eof_bmi = 45
  )
  cip_height <- data.frame(age = c(42, 50), birth_year = c(2000, 2000), sex = c(1, 1), cip = c(0.2, 0.35))
  cip_bmi    <- data.frame(age = c(45, 50), birth_year = c(2000, 2000), sex = c(1, 1), cip = c(0.3, 0.4))

  res <- prepare_thresholds_multi(
    .tbl = tbl,
    CIP_list = list(cip_height, cip_bmi),
    phen_names = c("height", "bmi"),
    interpolation = NA
  )

  expect_true(is.data.frame(res))
  expect_true(all(c("lower_height", "upper_height", "K_i_height", "K_pop_height",
                    "lower_bmi", "upper_bmi", "K_i_bmi", "K_pop_bmi") %in% colnames(res)))
})

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.