tests/Test-UsePoints.R

require(NonCompart)

# Regression tests for the UsePoints terminal-slope path in sNCA().
#
# Two defects fixed (both present 0.8.0 - 0.8.2):
#
#  1) sNCA.R CLSTP in the UsePoints branch was
#       exp(b0 - LAMZ*max(x[is.finite(log(y[UsePoints]))]))
#     The mask is length(UsePoints) but x is length(n), so R recycled it silently. Lambda z
#     points are always positive, so the mask was all TRUE, recycled to select the whole of
#     x, and CLSTP got anchored at the last SAMPLING time instead of TLST. Only profiles
#     with trailing BLQ/zero samples were affected. It propagates to AUCIFP, AUCIFPD,
#     AUCPEP, AUMCIFP, AUMCPEP, VZFP, CLFP, MRTEVIFP.
#
#  2) sNCA.R remapped BestSlope/DetSlope's UsedPoints from x1-space to x-space with a single
#     scalar offset measured at LAMZLL. Every zero dropped INSIDE the lambda z window shifts
#     the later indices by one, so the reported set named the wrong samples and could even
#     include a y == 0 sample. That made the documented
#     tblNCA -> attr(,"UsedPoints") -> UsePoints re-run round-trip non-idempotent.
#
# Always pass R2ADJ=0 here: the default (0.7) can route sNCA into the interactive DetSlope().

Fail = function(msg) stop(paste("Test Failed!", msg))
Same = function(a, b, Tol=1e-10) isTRUE(all.equal(unname(a), unname(b), tolerance=Tol))

PRED = c("CLSTP", "AUCIFP", "AUCIFPD", "AUCPEP", "AUMCIFP", "AUMCPEP",
         "VZFP", "CLFP", "MRTEVIFP")
CORE = c("b0", "LAMZ", "LAMZHL", "LAMZNPT", "R2", "R2ADJ", "CORRXY", "LAMZLL", "LAMZUL",
         "TMAX", "CMAX", "TLST", "CLST", "TLAG", "AUCLST", "AUCALL", "AUCIFO", "AUCPEO",
         "AUMCLST", "AUMCIFO", "AUMCPEO", "VZFO", "CLFO", "MRTEVLST", "MRTEVIFO")

## ---------------------------------------------------------------- 1. trailing zeros ----
# Clean mono-exponential terminal phase (k = 0.1/h) with 3 trailing BLQ samples after the
# last measurable concentration at t = 24.
x = c(0, 0.5, 1, 2, 4, 8, 12, 24, 36, 48, 96)
y = c(0, 0, 0, 10, 10*exp(-0.2), 10*exp(-0.6), 10*exp(-1.0), 10*exp(-2.2), 0, 0, 0)

auto = sNCA(x, y, dose=100, R2ADJ=0)
up   = attr(auto, "UsedPoints")
if (length(up) < 2) Fail("auto slope selection found no terminal phase")
forced = sNCA(x, y, dose=100, UsePoints=up)

# Forcing the very points the auto selector chose must reproduce the auto result exactly.
for (p in c(CORE, PRED)) {
  if (is.na(auto[p]) && is.na(forced[p])) next
  if (!Same(auto[p], forced[p])) Fail(paste("auto vs UsePoints differ for", p))
}

# CLSTP is Clast_pred: predicted at TLST, not at the last sampling time.
b0 = unname(forced["b0"]); lz = unname(forced["LAMZ"]); tlst = unname(auto["TLST"])
if (!(tlst < max(x))) Fail("fixture must have trailing zeros")
if (!Same(forced["CLSTP"], exp(b0 - lz*tlst)))     Fail("CLSTP is not anchored at TLST")
if ( Same(forced["CLSTP"], exp(b0 - lz*max(x))))   Fail("CLSTP is anchored at max(x)")

## -------------------------------------------------------------- 2. interior BLQ ---------
# A zero INSIDE the lambda z window must not corrupt the reported UsedPoints.
x = c(0, 1, 2, 3, 4, 5, 6)
y = c(0, 10, 5, 0, 1.2, 0.6, 0.3)

auto = sNCA(x, y, dose=100, R2ADJ=0)
up   = attr(auto, "UsedPoints")
if (length(up) < 2) Fail("auto slope selection found no terminal phase (interior BLQ)")

# UsedPoints are indices into x, so they must never name a zero-concentration sample ...
if (any(y[up] == 0)) Fail("UsedPoints names a y == 0 sample")
# ... and must be exactly the non-zero samples inside [LAMZLL, LAMZUL].
expected = which(y != 0 & x >= auto["LAMZLL"] & x <= auto["LAMZUL"])
if (!identical(as.integer(up), as.integer(expected))) {
  Fail(paste0("UsedPoints = ", paste(up, collapse=","),
              " but expected ", paste(expected, collapse=",")))
}
if (length(up) != auto["LAMZNPT"]) Fail("length(UsedPoints) != LAMZNPT")

# Round-trip must be idempotent: feeding UsedPoints back changes nothing.
back = sNCA(x, y, dose=100, UsePoints=up)
for (p in c(CORE, PRED)) {
  if (is.na(auto[p]) && is.na(back[p])) next
  if (!Same(auto[p], back[p])) Fail(paste("UsedPoints round-trip is not idempotent for", p))
}

## ------------------------------------------------------------------- 3. tblNCA ----------
# The same must hold through tblNCA, which is how UsedPoints is normally obtained and fed
# back (e.g. to ncar::pdfNCA).
d  = data.frame(ID="A", Time=x, conc=y)
ta = tblNCA(d, key="ID", dose=100)
tu = tblNCA(d, key="ID", dose=100, UsePoints=attr(ta, "UsedPoints"))
for (p in c("CLSTP", "AUCIFP", "AUCPEP", "CLFP", "VZFP", "LAMZ", "LAMZNPT")) {
  if (is.na(ta[[p]]) && is.na(tu[[p]])) next
  if (!Same(ta[[p]], tu[[p]])) Fail(paste("tblNCA round-trip differs for", p))
}

## ------------------------------------------------------- 4. R2ADJ at LAMZNPT == 2 -------
# Slope() divides by (n - 2). At n == 2 the old code produced NaN/+Inf/-Inf depending on
# rounding; it must be NA. Only the UsePoints path can request a 2-point fit, because
# .findBestSlope() keeps only fits with LAMZNPT > 2.
x = c(0, 0.5, 1, 2, 4, 8, 12, 24, 36, 48, 96)
y = c(0, 0, 0, 10, 10*exp(-0.2), 10*exp(-0.6), 10*exp(-1.0), 10*exp(-2.2), 0, 0, 0)

two = sNCA(x, y, dose=100, UsePoints=7:8)
if (two["LAMZNPT"] != 2)   Fail("fixture did not produce a 2-point fit")
if (!is.na(two["R2ADJ"]))  Fail(paste("R2ADJ at n == 2 should be NA, got", two["R2ADJ"]))
if (is.nan(two["R2ADJ"]))  Fail("R2ADJ at n == 2 is NaN, not NA")
# LAMZ itself is still well defined for 2 points.
if (!is.finite(two["LAMZ"])) Fail("LAMZ should still be finite at n == 2")

# The automatic path must never yield a 2-point fit, so R2ADJ stays finite there.
auto = sNCA(x, y, dose=100, R2ADJ=0)
if (auto["LAMZNPT"] <= 2)      Fail("automatic path returned a fit with LAMZNPT <= 2")
if (!is.finite(auto["R2ADJ"])) Fail("automatic path R2ADJ is not finite")

cat("Test-UsePoints.R: OK\n")

Try the NonCompart package in your browser

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

NonCompart documentation built on Nov. 15, 2026, 5:07 p.m.