Nothing
require(NonCompart)
# Regression tests for the terminal-slope point bookkeeping in BestSlope(), for the
# UsePoints length check in tblNCA(), and for unrecognized units in Unit().
#
# 1-3 BestSlope reported the fitted points by looking the lambda z limits back up with
# which(x == LAMZLL):which(x == LAMZUL). A duplicated sampling time makes which()
# return two positions, a:b then used only the first with a warning, and the reported
# UsedPoints named the wrong samples - so the documented round trip of feeding
# UsedPoints back in as UsePoints returned a different lambda z.
# 4-5 Two candidate windows tie on both R2ADJ and LAMZNPT when a zero sits at the start of
# a window (log() drops it, so the window fits exactly the points of the next one).
# Both rows were selected, r became a matrix, and BestSlope stopped with
# "missing value where TRUE/FALSE needed".
# 6-8 tblNCA silently discarded a UsePoints of the wrong length and fell back to automatic
# slope selection, reporting a different analysis than the one requested.
# 9-10 An unrecognized concentration unit produced a NA conversion factor, which was then
# used as a logical subscript: "missing values are not allowed in subscripted
# assignments of data frames".
#
# Pass R2ADJ=0 throughout: the default (0.7) can route sNCA into the interactive DetSlope().
Fail = function(msg) stop(paste("Test Failed!", msg))
noWarn = function(expr) {
w = NULL
v = withCallingHandlers(expr,
warning=function(cond) { w <<- c(w, conditionMessage(cond)); invokeRestart("muffleWarning") })
if (!is.null(w)) Fail(paste("unexpected warning:", paste(w, collapse="; ")))
v
}
## ---- duplicated sampling times -------------------------------------------------------
# The best fit starts at the SECOND of the two t = 4 samples, so a time lookup cannot
# distinguish it from the first.
x = c(0, 1, 2, 4, 4, 8, 12, 24)
y = c(0, 10, 16, 15, 12, 8, 4, 1)
## 1. No "numerical expression has 2 elements" warning, and the reported points are as many
## as the fit used.
r = noWarn(BestSlope(x, y, adm="Extravascular"))
up = attr(r, "UsedPoints")
if (length(up) != r["LAMZNPT"])
Fail(paste("BestSlope reported", length(up), "points for a", r["LAMZNPT"], "point fit"))
## 2. Same through sNCA.
s = noWarn(sNCA(x, y, dose=100, R2ADJ=0))
up = attr(s, "UsedPoints")
if (length(up) != s["LAMZNPT"])
Fail(paste("sNCA reported", length(up), "points for a", s["LAMZNPT"], "point fit"))
## 3. The round trip is idempotent: feeding UsedPoints back in reproduces the fit.
s2 = sNCA(x, y, dose=100, R2ADJ=0, UsePoints=up)
for (p in c("LAMZ", "R2ADJ", "LAMZNPT", "CLSTP", "AUCIFP")) {
if (!isTRUE(all.equal(unname(s[p]), unname(s2[p]))))
Fail(paste("round trip changed", p, ":", s[p], "->", s2[p]))
}
## ---- a zero at the start of a candidate window ---------------------------------------
x = c(0, 1, 2, 4, 8, 12, 16, 24)
y = c(0, 10, 16, 0, 8, 4, 2, 1) # zero at index 4 == which.max(y) + 1 == first window start
## 4. BestSlope returns the parameter vector instead of stopping.
r = tryCatch(BestSlope(x, y, adm="Extravascular"),
error=function(e) Fail(paste("tied candidate windows raised:", conditionMessage(e))))
if (is.matrix(r)) Fail("BestSlope returned a matrix, not a parameter vector")
if (length(r) != 9) Fail(paste("BestSlope returned", length(r), "values, expected 9"))
if (!(r["LAMZNPT"] > 0)) Fail("BestSlope found no terminal slope on a fittable profile")
## 5. The reported points are the points fitted - no dropped zero among them.
up = attr(r, "UsedPoints")
if (length(up) != r["LAMZNPT"])
Fail(paste("BestSlope reported", length(up), "points for a", r["LAMZNPT"], "point fit"))
if (any(y[up] == 0)) Fail("BestSlope reported a zero-concentration sample as fitted")
## ---- UsePoints length checking in tblNCA ---------------------------------------------
d = data.frame(ID = rep(1:3, each=6),
Time = rep(c(0, 1, 2, 4, 8, 24), 3),
Conc = c(0, 10, 16, 12, 8, 1,
0, 12, 18, 14, 9, 2,
0, 9, 15, 11, 7, 1.5))
auto = tblNCA(d, key="ID", colTime="Time", colConc="Conc", dose=1, R2ADJ=0)
## 6. The right length is accepted and does change the analysis.
good = tblNCA(d, key="ID", colTime="Time", colConc="Conc", dose=1, R2ADJ=0,
UsePoints=list(3:6, 3:6, 3:6))
if (isTRUE(all.equal(good$LAMZNPT, auto$LAMZNPT)))
Fail("UsePoints did not change the fit, so tests 7-8 would prove nothing")
## 7. A short list is refused instead of being dropped.
e = tryCatch({ tblNCA(d, key="ID", colTime="Time", colConc="Conc", dose=1, R2ADJ=0,
UsePoints=list(3:6, 3:6)); NULL },
error=function(e) conditionMessage(e))
if (is.null(e)) Fail("a 2-element UsePoints was accepted for 3 subjects")
if (!grepl("UsePoints", e)) Fail(paste("unhelpful error for a short UsePoints:", e))
## 8. A bare vector is refused: with one index per subject it used to look like a valid list.
e = tryCatch({ tblNCA(d, key="ID", colTime="Time", colConc="Conc", dose=1, R2ADJ=0,
UsePoints=c(3, 4, 5)); NULL },
error=function(e) conditionMessage(e))
if (is.null(e)) Fail("a bare vector UsePoints was accepted")
if (!grepl("list", e)) Fail(paste("unhelpful error for a vector UsePoints:", e))
## ---- unrecognized concentration units ------------------------------------------------
## 9. Unknown unit warns and returns NA factors rather than stopping.
u = tryCatch(suppressWarnings(Unit(concUnit="nonsense/unit")),
error=function(e) Fail(paste("unknown concUnit raised:", conditionMessage(e))))
if (!is.na(u["VZFO", 2])) Fail("unknown concUnit should leave the VZFO factor NA")
if (!is.na(u["CLFO", 2])) Fail("unknown concUnit should leave the CLFO factor NA")
## 10. NA unit is handled the same way.
u = tryCatch(suppressWarnings(Unit(concUnit=NA)),
error=function(e) Fail(paste("NA concUnit raised:", conditionMessage(e))))
if (!is.na(u["VZFO", 2])) Fail("NA concUnit should leave the VZFO factor NA")
cat("Test-BestSlope.R: OK\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.