tests/Test-SS.R

require(NonCompart)

# Regression tests for steady-state analysis in sNCA() and tblNCA(): SS=TRUE with tau.
#
# Up to 0.8.3 SS=TRUE replaced AUCIFO by AUCLST in CL and Vz. AUClast equals AUCtau only
# when the last sample happens to fall exactly at tau; with a tau between two samples, or
# beyond the last one, the reported "steady-state clearance" was the dose over the wrong
# area. 0.8.4 computes AUCTAU over 0..tau through the interval-AUC engine and derives CL, Vz
# and CAVG from it. Every reference value below is a hand sum on an integer fixture, not
# engine output, so this file fails on the unpatched build with a precise message.
#
# Always pass R2ADJ=0 to sNCA 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))

## 1. Extravascular fixture: q12h at steady state, trough 2 at t=0 and again at t=12 -----
x = c(0, 1, 2, 4, 8, 12)
y = c(2, 6, 10, 8, 4, 2)
lz = log(2)/4                      # the terminal points 8, 4, 2 halve every 4 h
r = sNCA(x, y, dose=100, concUnit="mg/L", SS=TRUE, tau=12, R2ADJ=0)

if (!Same(r["AUCTAU"], 66)) Fail("AUCTAU should be the hand trapezoid 4+8+18+24+12 = 66")
if (!Same(r["CAVG"], 5.5)) Fail("CAVG should be AUCTAU/tau = 5.5")
if (!Same(r["CLFO"], 100/66)) Fail("CLFO should be dose/AUCTAU")
if (!Same(r["VZFO"], 100/66/lz, 1e-8)) Fail("VZFO should be dose/AUCTAU/LAMZ")
if (!Same(r["LAMZ"], lz, 1e-8)) Fail("LAMZ should be log(2)/4")
if (!all(is.na(r[c("VZFP", "CLFP")]))) Fail("Pred twins of Vz/F and CL/F must stay NA at steady state")
# MRT to infinity at steady state is (AUMCtau + tau*(AUCinf - AUCtau))/AUCtau. AUMCtau is
# the hand sum 3+13+52+128+112 = 308, and AUCinf - AUCtau is Clast/lz = 2/lz. Clast_pred is
# 2 as well on this fixture, so the Pred twin is the same number. NA up to 0.8.4.
if (!Same(r["MRTEVIFO"], (308 + 12*2/lz)/66, 1e-8)) Fail("MRTEVIFO at steady state")
if (!Same(r["MRTEVIFP"], (308 + 12*2/lz)/66, 1e-8)) Fail("MRTEVIFP at steady state")
u = attr(r, "units")
if (length(u) != length(r)) Fail("units attribute length")
if (u[names(r) == "AUCTAU"] != "h*mg/L") Fail("AUCTAU unit")
if (u[names(r) == "CAVG"] != "mg/L") Fail("CAVG unit")

## 2. tau different from Tlast: CL must follow AUCTAU, not AUCLST ------------------------
# tau=10 lies between the 8 h and 12 h samples: linear interpolation gives C(10)=3, so
# AUC(8..10) = (4+3)/2*2 = 7 and AUCTAU = 4+8+18+24+7 = 61, while AUCLST stays 66.
r10 = sNCA(x, y, dose=100, concUnit="mg/L", SS=TRUE, tau=10, R2ADJ=0)
if (!Same(r10["AUCTAU"], 61)) Fail("AUCTAU with tau between samples should interpolate: 61")
if (!Same(r10["AUCLST"], 66)) Fail("AUCLST must not change with tau")
if (!Same(r10["CLFO"], 100/61)) Fail("CLFO must be dose/AUCTAU, not dose/AUCLST as in 0.8.3")
if (!Same(r10["CAVG"], 6.1)) Fail("CAVG should be 61/10")
if (!Same(r10["VZFO"], 100/61/lz, 1e-8)) Fail("VZFO must be dose/AUCTAU/LAMZ")
# AUMC 0..10 is 3+13+52+128 plus (8*4 + 10*3)/2*2 = 62, i.e. 258, not AUMCLST = 308.
if (!Same(r10["MRTEVIFO"], (258 + 10*(66 + 2/lz - 61))/61, 1e-8)) Fail("MRTEVIFO must use the AUMC to tau, not AUMCLST")

# tau=16 lies beyond Tlast=12: the tail 12..16 is extrapolated with lambda z, a log
# trapezoid between C(12)=2 and the predicted C(16)=1, i.e. (2 - 1)/lz.
r16 = sNCA(x, y, dose=100, concUnit="mg/L", SS=TRUE, tau=16, R2ADJ=0)
if (!Same(r16["AUCTAU"], 66 + (2 - 2*exp(-lz*4))/lz, 1e-8)) Fail("AUCTAU beyond TLST should extrapolate with LAMZ")
if (!Same(r16["CLFO"], 100/r16["AUCTAU"])) Fail("CLFO with an extrapolated AUCTAU")
# The AUMC of the extrapolated piece 12..16 is the integral of t*2*exp(-lz*(t - 12)), which
# is 8/lz + 1/lz^2, and the area left beyond tau is 2/lz - 1/lz.
if (!Same(r16["MRTEVIFO"], (308 + 8/lz + 1/lz^2 + 16/lz)/(66 + 1/lz), 1e-8)) Fail("MRTEVIFO with tau beyond TLST")

## 3. The same value as the former manual route, iAUC from 0 to tau ----------------------
ri = sNCA(x, y, dose=100, concUnit="mg/L", iAUC=data.frame(Name="AUC0tau", Start=0, End=10), R2ADJ=0)
if (!Same(ri["AUC0tau"], r10["AUCTAU"])) Fail("AUCTAU must equal iAUC(0, tau)")
# An iAUC that borrows a standard name is refused rather than overwritten in silence.
e = tryCatch(sNCA(x, y, dose=100, SS=TRUE, tau=12, R2ADJ=0, iAUC=data.frame(Name="AUCTAU", Start=0, End=12)), error=function(e) conditionMessage(e))
if (!is.character(e) || !grepl("standard parameter name", e)) Fail("iAUC named AUCTAU with SS=TRUE must stop")
# Without SS the name AUCTAU is still free for a first-dose AUC over the interval.
r1st = sNCA(x, y, dose=100, concUnit="mg/L", iAUC=data.frame(Name="AUCTAU", Start=0, End=12), R2ADJ=0)
if (!Same(r1st["AUCTAU"], 66)) Fail("iAUC named AUCTAU with SS=FALSE must still work")
if (length(attr(r1st, "units")) != length(r1st)) Fail("units attribute length with iAUC")

## 4. tau unit conversion ---------------------------------------------------------------
rd = sNCA(x, y, dose=100, concUnit="mg/L", SS=TRUE, tau=0.5, unitTau="d", R2ADJ=0)
if (!Same(rd, r)) Fail("tau=0.5 d must give the same result as tau=12 h")
rmn = sNCA(x, y, dose=100, concUnit="mg/L", SS=TRUE, tau=720, unitTau="min", R2ADJ=0)
if (!Same(rmn, r)) Fail("tau=720 min must give the same result as tau=12 h")
rmin = sNCA(x*60, y, dose=100, concUnit="mg/L", timeUnit="min", SS=TRUE, tau=12, unitTau="h", R2ADJ=0)
if (!Same(rmin["AUCTAU"], 66*60)) Fail("tau in h with timeUnit min: AUCTAU in min*mg/L")
if (!Same(rmin["CAVG"], 5.5)) Fail("CAVG does not depend on the time unit")
e = tryCatch(sNCA(x, y, dose=100, SS=TRUE, tau=12, unitTau="fortnight"), error=function(e) conditionMessage(e))
if (!is.character(e) || !grepl("Unrecognized unitTau", e)) Fail("unknown unitTau must stop")

## 5. Argument checks --------------------------------------------------------------------
e = tryCatch(sNCA(x, y, dose=100, SS=TRUE), error=function(e) conditionMessage(e))
if (!is.character(e) || !grepl("requires tau", e)) Fail("SS=TRUE without tau must stop")
e = tryCatch(sNCA(x, y, dose=100, SS=TRUE, tau=-12), error=function(e) conditionMessage(e))
if (!is.character(e) || !grepl("requires tau", e)) Fail("negative tau must stop")
w = tryCatch(sNCA(x, y, dose=100, tau=12, R2ADJ=0), warning=function(w) conditionMessage(w))
if (!is.character(w) || !grepl("ignored", w)) Fail("tau with SS=FALSE must warn")
r0 = suppressWarnings(sNCA(x, y, dose=100, concUnit="mg/L", tau=12, R2ADJ=0))
if ("AUCTAU" %in% names(r0)) Fail("SS=FALSE must not add AUCTAU")
if (!Same(r0["CLFO"], 100/r0["AUCIFO"])) Fail("SS=FALSE keeps CL from AUCIFO")

## 6. Intravascular routes ---------------------------------------------------------------
xb = c(0.25, 0.5, 1, 2, 4, 8, 12)
yb = c(9, 8, 7, 6, 4, 2, 1)
rb = sNCA(xb, yb, dose=100, adm="Bolus", concUnit="mg/L", SS=TRUE, tau=12, R2ADJ=0)
# C0 is back-extrapolated from the first two points; AUC 0..12 is the trapezoid including it.
C0 = exp(log(9) - 0.25*(log(8) - log(9))/(0.5 - 0.25))
yb0 = c(C0, yb)
AUCb = sum(diff(c(0, xb)) * (yb0[-length(yb0)] + yb0[-1])/2)
if (!Same(rb["C0"], C0, 1e-8)) Fail("bolus C0")
if (!Same(rb["AUCTAU"], AUCb, 1e-8)) Fail("bolus AUCTAU should include the C0 segment")
if (!Same(rb["CLO"], 100/AUCb, 1e-8)) Fail("bolus CLO = dose/AUCTAU")
if (!Same(rb["VZO"], 100/AUCb/rb["LAMZ"], 1e-8)) Fail("bolus VZO = dose/AUCTAU/LAMZ")
if (!all(is.na(rb[c("CLP", "VZP")]))) Fail("bolus CLP and VZP stay NA at SS")
# The terminal points 4, 2, 1 halve every 4 h, so lambda z is lz and Clast_pred is Clast = 1.
# tau equals Tlast here, so AUCinf - AUCtau is 1/lz. Up to 0.8.4 VSSO was MRTIVLST*CLO.
xb0 = c(0, xb)
AUMCb = sum(diff(xb0) * (xb0[-1]*yb0[-1] + xb0[-length(xb0)]*yb0[-length(yb0)])/2)
MRTb = (AUMCb + 12/lz)/AUCb
if (!Same(rb["LAMZ"], lz, 1e-8)) Fail("bolus LAMZ")
if (!Same(rb["MRTIVIFO"], MRTb, 1e-8)) Fail("bolus MRTIVIFO at steady state")
if (!Same(rb["MRTIVIFP"], MRTb, 1e-8)) Fail("bolus MRTIVIFP at steady state")
if (!Same(rb["VSSO"], MRTb*100/AUCb, 1e-8)) Fail("bolus VSSO = MRTIVIFO*CLO")
if (!Same(rb["VSSP"], MRTb*100/AUCb, 1e-8)) Fail("bolus VSSP = MRTIVIFP*CLO, with the one steady-state clearance")

xi = c(0, 0.5, 1, 2, 4, 8, 12)
yi = c(1, 6, 8, 7, 5, 3, 1.5)
rinf = sNCA(xi, yi, dose=100, adm="Infusion", dur=1, concUnit="mg/L", SS=TRUE, tau=12, R2ADJ=0)
AUCi = sum(diff(xi) * (yi[-length(yi)] + yi[-1])/2)
if (!Same(rinf["AUCTAU"], AUCi)) Fail("infusion AUCTAU")
if (!Same(rinf["CLO"], 100/AUCi)) Fail("infusion CLO = dose/AUCTAU")
if (!Same(rinf["CAVG"], AUCi/12)) Fail("infusion CAVG")
# Half the infusion time comes off the MRT, at steady state as after a single dose.
AUMCi = sum(diff(xi) * (xi[-1]*yi[-1] + xi[-length(xi)]*yi[-length(yi)])/2)
if (!Same(rinf["MRTIVIFO"], (AUMCi + 12*(rinf["AUCIFO"] - AUCi))/AUCi - 1/2, 1e-8)) Fail("infusion MRTIVIFO at steady state")
if (!Same(rinf["MRTIVIFP"], (AUMCi + 12*(rinf["AUCIFP"] - AUCi))/AUCi - 1/2, 1e-8)) Fail("infusion MRTIVIFP at steady state")
if (!Same(rinf["VSSO"], rinf["MRTIVIFO"]*100/AUCi, 1e-8)) Fail("infusion VSSO")
if (!Same(rinf["VSSP"], rinf["MRTIVIFP"]*100/AUCi, 1e-8)) Fail("infusion VSSP")

## 7. tblNCA: scalar and per-subject tau, and the checks ---------------------------------
d = rbind(data.frame(ID=1, Time=x, conc=y), data.frame(ID=2, Time=x, conc=2*y))
t1 = tblNCA(d, key="ID", dose=100, concUnit="mg/L", SS=TRUE, tau=12)
if (!Same(t1$AUCTAU, c(66, 132))) Fail("tblNCA AUCTAU")
if (!Same(t1$CAVG, c(5.5, 11))) Fail("tblNCA CAVG")
if (!Same(t1$CLFO, c(100/66, 100/132))) Fail("tblNCA CLFO")
t2 = tblNCA(d, key="ID", dose=100, concUnit="mg/L", SS=TRUE, tau=c(12, 10))
if (!Same(t2$AUCTAU, c(66, 122))) Fail("tblNCA per-subject tau")
if (!Same(t2$CAVG, c(5.5, 12.2))) Fail("tblNCA per-subject CAVG")
if (length(attr(t1, "units")) != ncol(t1)) Fail("tblNCA units attribute length")
if (attr(t1, "units")[colnames(t1) == "CAVG"] != "mg/L") Fail("tblNCA CAVG unit")
e = tryCatch(tblNCA(d, key="ID", dose=100, SS=TRUE), error=function(e) conditionMessage(e))
if (!is.character(e) || !grepl("requires tau", e)) Fail("tblNCA SS=TRUE without tau must stop")
e = tryCatch(tblNCA(d, key="ID", dose=100, SS=TRUE, tau=c(12, 12, 12)), error=function(e) conditionMessage(e))
if (!is.character(e) || !grepl("Count of tau", e)) Fail("tblNCA tau of the wrong length must stop")
w = tryCatch(tblNCA(d, key="ID", dose=100, tau=12), warning=function(w) conditionMessage(w))
if (!is.character(w) || !grepl("ignored", w)) Fail("tblNCA tau with SS=FALSE must warn")
# The new arguments leave a non-steady-state table untouched.
t0a = tblNCA(d, key="ID", dose=100, concUnit="mg/L")
t0b = suppressWarnings(tblNCA(d, key="ID", dose=100, concUnit="mg/L", tau=12))
if (!isTRUE(all.equal(t0a, t0b))) Fail("tau with SS=FALSE must not change the table")

## 8. Flat profiles ----------------------------------------------------------------------
rf = sNCA(c(0, 6, 12), c(3, 3, 3), dose=10, SS=TRUE, tau=12)
if (!Same(rf["AUCTAU"], 36) || !Same(rf["CAVG"], 3)) Fail("flat profile AUCTAU/CAVG")
rz = sNCA(c(0, 6, 12), c(0, 0, 0), dose=10, SS=TRUE, tau=12)
if (!Same(rz["AUCTAU"], 0) || !Same(rz["CAVG"], 0)) Fail("all-zero profile AUCTAU/CAVG = 0")
# A bolus with a time-0 sample and an iAUC used to stop with "object 'x2' not found".
rfb = sNCA(c(0, 1, 2), c(3, 3, 3), dose=10, adm="Bolus", iAUC=data.frame(Name="A", Start=0, End=2))
if (!Same(rfb["A"], 6)) Fail("flat bolus with a time-0 sample and iAUC")
if (length(attr(rfb, "units")) != length(rfb)) Fail("flat profile units attribute length")

## 9. Unit() knows the new codes ---------------------------------------------------------
U = Unit(timeUnit="h", concUnit="ng/mL", doseUnit="mg")
if (U["AUCTAU", "Unit"] != "h*ug/L" || U["CAVG", "Unit"] != "ug/L") Fail("Unit() rows for AUCTAU and CAVG")
if (!Same(U[c("AUCTAU", "CAVG"), "Factor"], c(1, 1))) Fail("Unit() factors for AUCTAU and CAVG")

## 10. MRT and Vss at steady state against a closed form -----------------------------------
# A one-compartment bolus at steady state, dose 100 every 12 h, V = 20 L, half-life 4 h.
# With down="Log" every area is exact for a mono-exponential curve, so MRT must be 1/k and
# Vss must be V. Up to 0.8.4 VSSO was 14.06 L here.
k = log(2)/4
V = 20
Css = function(t) 100/V*exp(-k*t)/(1 - exp(-k*12))
for (tEnd in c(8, 12, 16)) {  # tau beyond, at and before the last sample
  tt = seq(0, tEnd, 2)
  rv = sNCA(tt, Css(tt), dose=100, adm="Bolus", concUnit="mg/L", down="Log", SS=TRUE, tau=12, R2ADJ=0)
  if (!Same(rv["MRTIVIFO"], 1/k, 1e-8)) Fail(paste("closed-form MRTIVIFO, last sample at", tEnd))
  if (!Same(rv["MRTIVIFP"], 1/k, 1e-8)) Fail(paste("closed-form MRTIVIFP, last sample at", tEnd))
  if (!Same(rv["VSSO"], V, 1e-8)) Fail(paste("closed-form VSSO, last sample at", tEnd))
  if (!Same(rv["VSSP"], V, 1e-8)) Fail(paste("closed-form VSSP, last sample at", tEnd))
  if (!Same(rv["CLO"], k*V, 1e-8)) Fail(paste("closed-form CLO, last sample at", tEnd))
}
# Without a terminal slope there is no AUCinf, hence no MRT to infinity and no Vss.
rn = sNCA(c(0, 6, 12), c(2, 10, 2), dose=100, adm="Infusion", dur=1, SS=TRUE, tau=12, R2ADJ=0)
if (!all(is.na(rn[c("LAMZ", "MRTIVIFO", "MRTIVIFP", "VSSO", "VSSP")]))) Fail("MRTinf and Vss must be NA without LAMZ")
# A single dose is untouched.
tt = seq(0, 12, 2)
r1d = sNCA(tt, 5*exp(-k*tt), dose=100, adm="Bolus", concUnit="mg/L", down="Log", R2ADJ=0)
if (!Same(r1d["MRTIVIFO"], 1/k, 1e-8) || !Same(r1d["VSSO"], V, 1e-8)) Fail("single-dose MRTIVIFO and VSSO")

## 11. No sample at dosing time ------------------------------------------------------------
# At steady state the point inserted at dosing time is the lowest concentration observed
# within the dosing interval, here 2, so dropping the pre-dose sample leaves AUCTAU at 66.
# Up to 0.8.4 the inserted point was 0, which gave 65.
rm0 = sNCA(x[-1], y[-1], dose=100, concUnit="mg/L", SS=TRUE, tau=12, R2ADJ=0)
if (!Same(rm0["AUCTAU"], 66) || !Same(rm0["CAVG"], 5.5) || !Same(rm0["CLFO"], 100/66)) Fail("SS without a time-0 sample: AUCTAU, CAVG, CLFO")
if (!Same(rm0["AUCLST"], 66) || !Same(rm0["AUCALL"], 66) || !Same(rm0["AUMCLST"], 308)) Fail("SS without a time-0 sample: AUCLST, AUCALL, AUMCLST")
if (!Same(rm0[c("LAMZ", "LAMZNPT", "TLAG")], r[c("LAMZ", "LAMZNPT", "TLAG")])) Fail("the inserted point must not enter LAMZ or TLAG")
# The lowest concentration is looked for within the dosing interval only.
rm1 = sNCA(c(x[-1], 16), c(y[-1], 1), dose=100, concUnit="mg/L", SS=TRUE, tau=12, R2ADJ=0)
if (!Same(rm1["AUCTAU"], 66)) Fail("the inserted point must be the minimum within tau, not after it")
# A partial area that starts before the first sample uses the same inserted point.
rm2 = sNCA(x[-1], y[-1], dose=100, concUnit="mg/L", SS=TRUE, tau=12, R2ADJ=0, iAUC=data.frame(Name="A0_1", Start=0, End=1))
if (!Same(rm2["A0_1"], 4)) Fail("iAUC from 0 at SS without a time-0 sample")
# After a single dose the inserted point stays 0.
rm3 = sNCA(x[-1], y[-1], dose=100, concUnit="mg/L", R2ADJ=0)
if (!Same(rm3["AUCLST"], 65)) Fail("single dose without a time-0 sample inserts 0")
# Infusion at steady state: the minimum within tau is 1.5, at 12 h.
rim = sNCA(xi[-1], yi[-1], dose=100, adm="Infusion", dur=1, concUnit="mg/L", SS=TRUE, tau=12, R2ADJ=0)
if (!Same(rim["AUCTAU"], AUCi - (1 + 6)/2*0.5 + (1.5 + 6)/2*0.5)) Fail("infusion SS without a time-0 sample")
# A flat profile: the inserted point is that same concentration.
rfm = sNCA(c(6, 12), c(3, 3), dose=10, SS=TRUE, tau=12)
if (!Same(rfm["AUCTAU"], 36) || !Same(rfm["CAVG"], 3)) Fail("flat SS profile without a time-0 sample")

## 12. Cmax and Tmax within the dosing interval --------------------------------------------
# A sample after tau belongs to the next interval. Up to 0.8.4 CMAX was 20 and TMAX 16 here
# while AUCTAU and CAVG stopped at tau.
rx = sNCA(c(x, 16), c(y, 20), dose=100, concUnit="mg/L", SS=TRUE, tau=12, R2ADJ=0)
if (!Same(rx["CMAX"], 10) || !Same(rx["TMAX"], 2) || !Same(rx["CMAXD"], 0.1)) Fail("SS CMAX, TMAX, CMAXD must come from within tau")
if (!Same(rx["AUCTAU"], 66) || !Same(rx["CAVG"], 5.5)) Fail("SS AUCTAU and CAVG with a sample after tau")
# The sample at tau itself is inside the interval; the first of two equal maxima is taken.
rx2 = sNCA(c(0, 1, 2, 4, 8, 12, 16), c(2, 6, 10, 8, 4, 11, 20), dose=100, SS=TRUE, tau=12, R2ADJ=0)
if (!Same(rx2["CMAX"], 11) || !Same(rx2["TMAX"], 12)) Fail("a sample at tau counts for CMAX")
rx3 = sNCA(c(0, 1, 2, 4, 8, 12), c(2, 10, 10, 8, 4, 2), dose=100, SS=TRUE, tau=12, R2ADJ=0)
if (!Same(rx3["TMAX"], 1)) Fail("first of two equal maxima")
# After a single dose the whole profile is searched, as before.
rx1 = sNCA(c(x, 16), c(y, 20), dose=100, concUnit="mg/L", R2ADJ=0)
if (!Same(rx1["CMAX"], 20) || !Same(rx1["TMAX"], 16)) Fail("single-dose CMAX and TMAX use the whole profile")

cat("Test-SS.R: all steady-state checks passed\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.