Nothing
require(NonCompart)
# Regression tests for the trapezoids at a zero concentration and for interval (partial)
# AUCs at and after the last positive concentration. Every reference value below is a
# hand sum or a closed form, not engine output.
#
# 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. Log-down: a segment that comes down to zero is a linear triangle ---------------------
# Up to 0.8.4 AUC() sent it to the log branch, where log(0) made it contribute 0.
a = AUC(c(48, 60), c(0.13, 0), down="Log")
if (!Same(a[2, ], c(0.78, 37.44))) Fail("AUC, log-down, positive to zero: 12*0.13/2 and 12*48*0.13/2")
if (!Same(a[2, ], LogAUC(c(48, 60), c(0.13, 0)))) Fail("AUC and LogAUC must agree on a segment ending at zero")
# A trailing zero: AUCLST stops at the last positive concentration, AUCALL adds the triangle.
x = c(0, 1, 2, 3, 4)
y = c(0, 4, 2, 0, 0)
r = sNCA(x, y, dose=100, down="Log", R2ADJ=0)
if (!Same(r["AUCLST"], 2 + 2/log(2))) Fail("log-down AUCLST with trailing zeros")
if (!Same(r["AUCALL"], 2 + 2/log(2) + 1)) Fail("log-down AUCALL must add the triangle down to the first zero")
r = sNCA(x, y, dose=100, down="Linear", R2ADJ=0)
if (!Same(r["AUCLST"], 5) || !Same(r["AUCALL"], 6)) Fail("linear AUCLST and AUCALL with trailing zeros")
# A zero inside the profile: both segments around it are linear, the rest is log-down.
y = c(0, 4, 0, 2, 1)
r = sNCA(x, y, dose=100, down="Log", R2ADJ=0)
if (!Same(r["AUCLST"], 2 + 2 + 1 + 1/log(2))) Fail("log-down AUCLST with a zero inside the profile")
if (!Same(r["AUMCLST"], 2 + 2 + 3 + (3*2 - 4*1)/log(2) + (2 - 1)/log(2)^2)) Fail("log-down AUMCLST with a zero inside the profile")
# The cumulative table and the one-shot function are the same rule.
if (!Same(AUC(x, y, down="Log")[5, ], LogAUC(x, y))) Fail("AUC and LogAUC must agree")
## 2. Log trapezoid for nearly equal concentrations ----------------------------------------
# The textbook AUMC form subtracts two terms of size 1/log(C1/C2): 0.49988 up to 0.8.4.
# Exact values: AUC = d/(-log(1 - d)) = 1 - d/2, AUMC = (1 - d)*(1/2 + d/6) = 1/2 - d/3.
d = 1e-12
a = AUC(c(0, 1), c(1, 1 - d), down="Log")
if (abs(a[2, 1] - (1 - d/2)) > 1e-14) Fail("log AUC for nearly equal concentrations")
if (abs(a[2, 2] - (1/2 - d/3)) > 1e-14) Fail("log AUMC for nearly equal concentrations")
# An ordinary descent keeps the textbook value.
k = log(8/2)/4
a = AUC(c(0, 4), c(8, 2), down="Log")
if (!Same(a[2, ], c((8 - 2)/k, (0*8 - 4*2)/k + (8 - 2)/k^2), 1e-13)) Fail("log AUC and AUMC, ordinary descent")
# A short interval far from the time origin, against numerical integration.
k = log(5/4.999)/0.5
f = function(t) 5*exp(-k*(t - 1000))
a = AUC(c(1000, 1000.5), c(5, 4.999), down="Log")
if (!Same(a[2, 1], integrate(f, 1000, 1000.5, rel.tol=1e-13)$value, 1e-11)) Fail("log AUC far from the origin")
if (!Same(a[2, 2], integrate(function(t) t*f(t), 1000, 1000.5, rel.tol=1e-13)$value, 1e-11)) Fail("log AUMC far from the origin")
## 3. Interval AUC that starts after the last positive concentration ------------------------
# NA up to 0.8.4. Both ends are extrapolated with lambda z, and the area is the log
# trapezoid whatever 'down' is.
x = 0:4
y = 4*exp(-0.2*x)
C = function(t) 4*exp(-0.2*t)
i58 = data.frame(Name=c("A5_8", "A3_6", "A4_8"), Start=c(5, 3, 4), End=c(8, 6, 8))
for (dn in c("Linear", "Log")) {
r = sNCA(x, y, dose=100, adm="Bolus", down=dn, R2ADJ=0, iAUC=i58)
if (!Same(r["LAMZ"], 0.2, 1e-8)) Fail("LAMZ of the mono-exponential fixture")
if (!Same(r["A5_8"], (C(5) - C(8))/0.2, 1e-8)) Fail(paste("interval AUC beyond TLST,", dn))
if (!Same(r["A4_8"], (C(4) - C(8))/0.2, 1e-8)) Fail(paste("interval AUC from TLST on,", dn))
}
# An interval across TLST: 'down' up to TLST, the log trapezoid after it.
if (!Same(r["A3_6"], (C(3) - C(6))/0.2, 1e-8)) Fail("interval AUC across TLST, Log")
r = sNCA(x, y, dose=100, adm="Bolus", down="Linear", R2ADJ=0, iAUC=i58)
if (!Same(r["A3_6"], (C(3) + C(4))/2 + (C(4) - C(6))/0.2, 1e-8)) Fail("interval AUC across TLST, Linear")
# Without a terminal slope nothing can be extrapolated.
r = sNCA(c(0, 1, 2), c(0, 5, 3), dose=100, R2ADJ=0, iAUC=data.frame(Name=c("A1_2", "A1_3", "A3_4"), Start=c(1, 1, 3), End=c(2, 3, 4)))
if (!Same(r["A1_2"], 4)) Fail("interval AUC within the data needs no LAMZ")
if (!is.na(r["A1_3"]) || !is.na(r["A3_4"])) Fail("interval AUC beyond the data must be NA without LAMZ")
## 4. Trailing zero: interpolated up to it, extrapolated after it ---------------------------
# The last positive concentration is 0.13 at 48 h and the last observation is a zero at 60 h.
z = c(TLST=48, LAMZ=0.1, b0=log(0.13) + 0.1*48)
P = function(t) 0.13*exp(-0.1*(t - 48)) # the regression line
for (dn in c("Linear", "Log")) {
# 54 h lies between the two: linear interpolation gives 0.065, and the segment down to
# the zero is a triangle. NA up to 0.8.4.
if (!Same(IntAUC(c(48, 60), c(0.13, 0), 54, 60, z, down=dn), 0.065*6/2)) Fail(paste("interval AUC between TLST and a trailing zero,", dn))
if (!Same(IntAUC(c(48, 60), c(0.13, 0), 48, 60, z, down=dn), 0.78)) Fail(paste("interval AUC from TLST to the trailing zero,", dn))
# After the zero the concentration comes from the regression again. The segment from the
# zero is linear, a segment between two extrapolated points is log.
if (!Same(IntAUC(c(48, 60), c(0.13, 0), 60, 72, z, down=dn), 12*P(72)/2, 1e-8)) Fail(paste("interval AUC from a trailing zero on,", dn))
if (!Same(IntAUC(c(48, 60), c(0.13, 0), 66, 72, z, down=dn), (P(66) - P(72))/0.1, 1e-8)) Fail(paste("interval AUC beyond a trailing zero,", dn))
}
# An interval that starts before TLST follows 'down' up to the last observation: with
# down="Linear" the piece between TLST and the zero is a linear trapezoid.
if (!Same(IntAUC(c(0, 24, 48, 60), c(0, 1, 0.13, 0), 24, 54, z, down="Linear"), (1 + 0.13)/2*24 + (0.13 + 0.065)/2*6)) Fail("interval AUC across TLST into the trailing-zero segment, Linear")
# Within the observations no terminal slope is needed; beyond them it is.
z0 = c(TLST=48, LAMZ=NA, b0=NA)
if (!Same(IntAUC(c(48, 60), c(0.13, 0), 54, 60, z0), 0.195)) Fail("interval AUC before a trailing zero needs no LAMZ")
if (!is.na(IntAUC(c(48, 60), c(0.13, 0), 54, 72, z0))) Fail("interval AUC beyond the last observation must be NA without LAMZ")
## 5. Intervals that were right before stay as they were -----------------------------------
x = Theoph[Theoph$Subject == 1, "Time"]
y = Theoph[Theoph$Subject == 1, "conc"]
r = sNCA(x, y, dose=320, concUnit="mg/L", R2ADJ=0, iAUC=data.frame(Name=c("A0_last", "A0_12", "A12_last"), Start=c(0, 0, 12), End=c(max(x), 12, max(x))))
if (!Same(r["A0_last"], r["AUCLST"])) Fail("interval AUC over the whole profile equals AUCLST")
if (!Same(r["A0_12"] + r["A12_last"], r["AUCLST"])) Fail("linear interval AUCs add up")
cat("Test-IntAUC.R: all interval AUC checks passed\n")
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.