tests/Test-TLAG.R

require(NonCompart)

# Regression tests for TLAG in sNCA().
#
# TLAG is the end of the lag phase: the last zero/BLQ time BEFORE the first measurable
# concentration. The old code used x0[max(which(y0 == 0))], i.e. the last zero ANYWHERE in
# the trailing-zero-stripped profile, so a single embedded BLQ sample occurring after
# measurable concentrations moved TLAG to that late time. It also tested y0[x0 == 0] > 0,
# which raised "the condition has length > 1" when several records shared time 0.
#
# Pass R2ADJ=0 throughout: the default (0.7) can route sNCA into the interactive DetSlope().

Fail = function(msg) stop(paste("Test Failed!", msg))

## 1. Embedded BLQ after measurable concentrations must not move TLAG.
x = c(0, 0.5, 1, 2, 4, 8, 12, 24)
y = c(0, 20, 15, 0, 6, 3, 1.5, 0.4)   # zero at t = 0 (lag) and an embedded zero at t = 2
r = sNCA(x, y, dose=100, R2ADJ=0)
if (r["TLAG"] != 0) Fail(paste("embedded BLQ moved TLAG to", r["TLAG"], "- expected 0"))

## 2. A genuine lag phase is reported as the last zero before the first measurable value.
x = c(0, 0.5, 1, 2, 4, 8, 12, 24)
y = c(0, 0, 0, 10, 8, 5, 2, 0.5)      # first measurable at t = 2
r = sNCA(x, y, dose=100, R2ADJ=0)
if (r["TLAG"] != 1) Fail(paste("TLAG should be 1, got", r["TLAG"]))

## 3. No lag phase at all -> 0.
x = c(0, 0.5, 1, 2, 4, 8, 12, 24)
y = c(5, 20, 15, 10, 6, 3, 1.5, 0.4)
r = sNCA(x, y, dose=100, R2ADJ=0)
if (r["TLAG"] != 0) Fail(paste("TLAG should be 0 with no lag, got", r["TLAG"]))

## 4. First sample after time 0 and already measurable -> 0.
x = c(0.5, 1, 2, 4, 8, 12, 24)
y = c(20, 15, 10, 6, 3, 1.5, 0.4)
r = sNCA(x, y, dose=100, R2ADJ=0)
if (r["TLAG"] != 0) Fail(paste("TLAG should be 0 without a t = 0 record, got", r["TLAG"]))

## 5. Duplicated time-0 records must not raise a condition-length error.
x = c(0, 0, 1, 2, 4, 8, 12, 24)
y = c(0, 0, 20, 15, 6, 3, 1.5, 0.4)
r = tryCatch(sNCA(x, y, dose=100, R2ADJ=0),
             error = function(e) Fail(paste("duplicated t = 0 raised:", conditionMessage(e))))
if (r["TLAG"] != 0) Fail(paste("TLAG should be 0 with duplicated t = 0, got", r["TLAG"]))

## 6. Trailing zeros are irrelevant to TLAG.
x = c(0, 0.5, 1, 2, 4, 8, 12, 24, 48, 96)
y = c(0, 20, 15, 10, 6, 3, 1.5, 0.4, 0, 0)
r = sNCA(x, y, dose=100, R2ADJ=0)
if (r["TLAG"] != 0) Fail(paste("trailing zeros moved TLAG to", r["TLAG"]))

cat("Test-TLAG.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.