Skip to content

fix: pk.calc.tlag() skips missing concentrations, as its siblings do - #727

Merged
billdenney merged 28 commits into
mainfrom
fix/tlag-missing-conc
Nov 25, 2026
Merged

billdenney merged 28 commits into
mainfrom
fix/tlag-missing-conc

Conversation

@billdenney

Copy link
Copy Markdown
Member

pk.calc.tlag() now treats a missing concentration as pk.calc.cmax() and the other simple parameters do (Bill, 2026-11-15: "Same as its siblings").

  • It gains check = TRUE, which runs assert_conc_time().
  • It looks for the first increase among the measured concentrations, skipping missing ones.
  • It returns NA when none increases, including when every concentration is missing.
  • Because the lag is found among measured samples only, a missing sample between the last zero and the first increase can move it: pk.calc.tlag(c(0, NA, 5, 8), c(0, 1, 2, 4)) is 0.

Two results change:

  • A missing value before or between measured ones gave NA by accident: pk.calc.tlag(c(NA, 40, 80, 10), c(0, 2, 4, 8)) was NA and is now 2.
  • A missing value with no increase after it, or every value missing, stopped with "missing value where TRUE/FALSE needed". In pk.nca() that stopped the interval with "Please report a bug". It now gives NA.

pk.nca() gets no cleaning step. Tests cover a missing value at the start, in the middle, at the end, and everywhere, directly (with and without check) and through pk.nca() at conc.na = "drop" and at a number.

🤖 Generated with Claude Code

billdenney and others added 28 commits November 14, 2026 11:17
PKNCAdose() refuses a negative dose amount with pknca_error_dose_negative,
naming the first rows and their count, for a dose column, a one-row-per-group
dose (dose ~ .), and a units column.  pk.nca() checks again, because a
mutate() or join on the dose object after PKNCAdose() can make one.

A zero dose and a missing dose are allowed and give what they gave before
(dose-normalized values Inf and NA); a row excluded with `exclude` is not
checked, as for the dose times.  NEWS lists it under breaking changes, since
code that passed a negative dose now stops.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r dose

A dose column that is not numeric is not checked by
assert_dose_not_negative(); the documentation and NEWS say so, and a test
pins that a character dose does not raise pknca_error_dose_negative.  A test
pins the message with exactly five offending rows (no "...").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PKNCAdose.data.frame() passed the dose amounts to setDuration.PKNCAdose(),
which expects the name of the dose column, so `!is.na(dose)` had the length
of the data and stopped with "'length = 2' in coercion to 'logical(1)'" from
two rows on (one row worked by accident).  The column name default now
applies, and each dose's duration is its amount divided by its rate.

A rate with no dose amount (a formula without a left side) is the typed
error pknca_error_rate_without_dose.  A missing rate, a rate of zero, or a
missing dose amount give the existing pknca_error_dose_invalid_duration.
Tests cover one, two, and three rows, a scalar rate, those three invalid
cases, and the formula without a dose for one and two rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Bill's answer (2026-11-15) for a missing concentration in pk.calc.tlag():
"Same as its siblings".  It gains check = TRUE (assert_conc_time()) and
looks for the first increase among the measured concentrations, returning
NA when none increases or every concentration is missing.  Before, a missing
value made the comparison of neighbours NA:  tlag(c(NA, 40, 80, 10)) was NA
by accident instead of 2, and tlag(c(NA, 40, 20, 10)) or an all-missing
interval stopped with "missing value where TRUE/FALSE needed" (in pk.nca(),
"Please report a bug").  pk.nca() gets no cleaning step.

Tests:  a missing value at the start, in the middle, at the end, and
everywhere, directly (with and without check) and through pk.nca() at
conc.na "drop" and at 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…o concentration

Bill (2026-11-16):  "Make them NA too".  ertlst, ermax, ertmax, and erlst
were calculated from the other collections, with the reason, when a
collection with a volume had a missing concentration, while ae, fe, and
erint were NA.  The amount in that collection is unknown, so the rate of the
interval is too:  urine_rate_result() makes them NA, keeping the existing
reasons and Bill's sentence.  A collection missing both its concentration and
its volume, or a known concentration with a missing volume, leaves the rates
as they were.

A comparison of every parameter against the base branch over seven
imputation chains, both conc.na settings, and plasma, multiple-dose, and
urine profiles changes only these four rates on the urine profile, from a
number to NA, with no change to any exclusion text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… direct test

- setDuration()'s @PARAM dose says it is the name of the dose column by
  default, or the dose amounts.
- NEWS says what a rate with no dose amount gave before
  (pknca_error_dose_invalid_duration with one row, an error without a class
  with more) and that a direct setDuration(object, rate = 5, dose = NA),
  which returned a duration of 0, is now pknca_error_rate_without_dose.
- A test calls setDuration() directly with the default dose column, with
  dose amounts, and with a missing dose.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The lag is found among the measured samples only, so a missing sample between
the last zero and the first increase moves it to the time of the last
measured zero; the documentation says so and a test pins
pk.calc.tlag(c(0, NA, 5, 8), c(0, 1, 2, 4)) at 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
urine_missing_messages() and urine_rate_result() both tested whether a
collection has a volume but no concentration; urine_collection_amount_missing()
is that test, used by both.  No result changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pk.calc.swing() tested cmin > 0 in an if, so a missing cmin stopped with
"missing value where TRUE/FALSE needed"; in pk.nca() an interval whose
concentrations are all missing stopped with "Please report a bug" when swing
was requested.  A missing cmin now gives NA (Bill, 2026-11-16).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The swing fix joins the lag-time fix because one issue reports both
(GitHub issue 723, reported by kawasakin):  a profile with missing
concentrations stopped pk.nca() for every subject.

NEWS conflict:  both entries are kept under "Bug fixes", and each now
credits the reporter and names the issue and the pull request.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The usage on main showed pk.calc.mrt.iv() and pk.calc.mrt.md.iv() with
`dose` and `time.dose` required, while both arguments default to NULL in
R/.  This is the page roxygen2 writes for the code on main; no function
changes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…s cases as tests

A missing cmax with a zero or negative cmin gave Inf; either input missing
now gives NA_real_.  The tests pin the three cases of GitHub issue 723
(reported by kawasakin) through pk.nca() at conc.na "drop" and 0:  the
profile with missing concentrations gets NA for tlag or swing, and the
other subject's result is calculated.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nce in its exclude text

combine_exclude_reasons() splits each input's text on "; " before joining,
so a reason that reaches a parameter twice (cmax and cmin of an
all-missing interval, both feeding swing) is listed once.  Values are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ta says why

The simple calculation functions (pk.calc.simple.R and pk.calc.dn())
return their NA with an exclude attribute naming the reason, which
pk.nca() puts in the exclude column and carries into everything
calculated from it.  No value changes.  Tests pin the reasons per
function, through pk.nca() per condition, and iterate every registered
parameter calculated by a simple function on all-missing, all-zero,
missing-end, and missing-dose profiles so that a new NA without a reason
fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… clearer end texts

An "exclude" attribute describes the whole vector, so na_with_reason()
now sets it only on a result of length one; pk.nca() always calls with
single values, and a vector from a direct call keeps no attribute that
would also sit beside its valid values.

pk.calc.dn(0, 0) stays NaN and now says "Dose is zero".  pk.calc.ctrough()
with an infinite interval end says "The interval has no finite end", and
pk.calc.ceoi() says the dose duration may be missing because there is no
dose in the interval.  The @returns documentation names the reason
attribute for every function that sets one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nca()

The enumerating test runs every registered parameter on missing, zero and
single-sample fixtures with and without a usable dose, and pins the set
of parameters that still give NA without a reason (the auciv family).
Vectors get no attribute, 0/0 dose normalisation says why, and ctrough
with an infinite end gives its own text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…worded texts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The missing-value fix on this branch tested the inputs with `||`, so a
vector of cmax with one cmin, which gave one swing per cmax in the released
version (`pk.calc.swing(cmax = c(10, 20), cmin = 1)` is 900 and 1900),
stopped with "'length = 2' in coercion to 'logical(1)'".  The function is now
vectorized, as its sibling pk.calc.ptr() is: every value the released
version returned is returned again, except that a vector of cmax with a cmin
of zero or below gives one Inf for each cmax where it gave a single Inf.
A missing cmin or cmax is still NA.

Found by the review of the input-check branch; Bill's ruling of 2026-11-20:
calls that returned a value keep working, and vectorized functions are
preferred where the values need no consolidation.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Brings PR 730 (fix/impute-start-na-conc) and the urine rates fix, which
merge before this branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	NEWS.md
Brings the negative-dose and dose-rate fixes, which merge before this
branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With PR 730's numeric conc.na, the start row's concentration is missing, so
cstart is NA with "No non-missing concentration at the start of the
interval", and aucabove.predose.all, which is above cstart, carries that
root cause before its own reason, as this branch does for every result
calculated from another (the aucabove.trough.all example in NEWS).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@billdenney
billdenney merged commit a9ccad1 into main Nov 25, 2026
8 of 9 checks passed
@billdenney
billdenney deleted the fix/tlag-missing-conc branch November 25, 2026 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant