Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e5b7b2a
fix: interval_add_secondary() links per-subject intervals to their ow…
billdenney Nov 9, 2026
a97dcda
fix: match results to a group-stratified units table by value
billdenney Nov 9, 2026
c71413d
fix: preferred units when some unit sets need no conversion
billdenney Nov 9, 2026
ec83512
fix: calculate dose-normalized renal clearance
billdenney Nov 9, 2026
6c1c576
Merge branch 'fix/clr-dn-formalsmap' into fix/interval-add-secondary-…
billdenney Nov 9, 2026
87f192b
Merge branch 'fix/concu-pref-per-group-units' into fix/clr-dn-formalsmap
billdenney Nov 9, 2026
e2a21ca
Merge branch 'fix/update-factor-groups' into fix/units-join-group-types
billdenney Nov 9, 2026
49021aa
Merge branch 'fix/units-join-group-types' into fix/concu-pref-per-gro…
billdenney Nov 9, 2026
6abd600
docs: separate the news bullets
billdenney Nov 9, 2026
867a9cf
Merge branch 'fix/concu-pref-per-group-units' into fix/clr-dn-formalsmap
billdenney Nov 9, 2026
4d250a5
Merge branch 'fix/update-factor-groups' into fix/units-join-group-types
billdenney Nov 9, 2026
14ef220
Merge branch 'fix/units-join-group-types' into fix/concu-pref-per-gro…
billdenney Nov 9, 2026
f59dbad
Merge branch 'fix/clr-dn-formalsmap' into fix/interval-add-secondary-…
billdenney Nov 9, 2026
df40d66
docs: code-format the parameter name in the news
billdenney Nov 9, 2026
93f7134
Merge branch 'fix/clr-dn-formalsmap' into fix/interval-add-secondary-…
billdenney Nov 9, 2026
0f412e5
Merge branch 'fix/update-factor-groups' into fix/units-join-group-types
billdenney Nov 10, 2026
b28541d
Merge branch 'fix/concu-pref-per-group-units' into fix/clr-dn-formalsmap
billdenney Nov 10, 2026
153a158
Merge branch 'fix/clr-dn-formalsmap' into fix/interval-add-secondary-…
billdenney Nov 10, 2026
08939ea
Merge branch 'fix/units-join-group-types' into fix/concu-pref-per-gro…
billdenney Nov 10, 2026
6dd5dde
test: include the renamed and deprecated renal clearance dose-normali…
billdenney Nov 10, 2026
8e47a6a
Merge branch 'fix/clr-dn-formalsmap' into fix/interval-add-secondary-…
billdenney Nov 10, 2026
949576c
docs: use the renamed renal clearance names in the news and the test
billdenney Nov 10, 2026
09f3a67
Merge branch 'fix/concu-pref-per-group-units' into fix/clr-dn-formalsmap
billdenney Nov 10, 2026
0637e20
Merge branch 'fix/clr-dn-formalsmap' into fix/interval-add-secondary-…
billdenney Nov 10, 2026
4197281
Merge branch 'fix/update-factor-groups' into fix/units-join-group-types
billdenney Nov 10, 2026
ef3c28b
Merge branch 'fix/units-join-group-types' into fix/concu-pref-per-gro…
billdenney Nov 10, 2026
2ac4b8a
revert: drop the calculation of dose-normalized renal clearance
billdenney Nov 10, 2026
b71476f
Merge branch 'fix/update-factor-groups' into fix/units-join-group-types
billdenney Nov 10, 2026
861ef93
Merge branch 'fix/units-join-group-types' into fix/concu-pref-per-gro…
billdenney Nov 10, 2026
e0d3bae
Merge branch 'fix/concu-pref-per-group-units' into fix/interval-add-s…
billdenney Nov 10, 2026
5a413c1
Merge branch 'fix/update-factor-groups' into fix/units-join-group-types
billdenney Nov 10, 2026
393b88f
Merge branch 'fix/units-join-group-types' into fix/concu-pref-per-gro…
billdenney Nov 10, 2026
c3d242b
Merge branch 'fix/concu-pref-per-group-units' into fix/interval-add-s…
billdenney Nov 10, 2026
758cd72
Merge branch 'fix/update-factor-groups' into fix/units-join-group-types
billdenney Nov 10, 2026
7cc627d
Merge branch 'fix/units-join-group-types' into fix/concu-pref-per-gro…
billdenney Nov 10, 2026
8ab19cc
Merge branch 'fix/concu-pref-per-group-units' into fix/interval-add-s…
billdenney Nov 10, 2026
a6e0573
Merge branch 'fix/update-factor-groups' into fix/units-join-group-types
billdenney Nov 10, 2026
e6db766
Merge branch 'fix/units-join-group-types' into fix/concu-pref-per-gro…
billdenney Nov 10, 2026
3da01b4
Merge branch 'fix/concu-pref-per-group-units' into fix/interval-add-s…
billdenney Nov 10, 2026
9460e94
Merge remote-tracking branch 'origin/main' into fix/interval-add-seco…
billdenney Nov 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,15 @@ the generated intervals:
function name has a dot in it, so loading the package failed with `could not
find function "getS3method"`. `getS3method` is now imported.

* `interval_add_secondary()` works on automatically generated intervals, which
have one row per subject. A `reference` such as
`data.frame(start = 0, end = 12)` matched one row per subject, and the helper
stopped with `pknca_error_secondary_ref_ambiguous_spec` because it chose
among those rows by their times alone; each interval now takes the reference
row of its own group (its own subject), ignoring the group columns named in
`reference` or `ref_vary`. The helper also no longer warns about the
unknown `interval_id` column of tibble intervals. (@billdenney)

* `PKNCAdata(impute = NA)` and `PKNCAdata(impute = NULL)` now mean the same as
the default: the `"impute"` column of the intervals when there is one, else
no imputation. Before, `impute = NA` stored no imputation setting and
Expand Down
64 changes: 49 additions & 15 deletions R/secondary-parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ interval_next_ref_names <- function(used, n, prefix = "ref") {
# for a factor. An absent or unfilled (all-NA logical) column takes character
# identifiers, and is created here so the caller can assign into it.
interval_new_ids <- function(intervals, n, prefix = "ref") {
existing <- intervals$interval_id
existing <- intervals[["interval_id"]]
if (is.null(existing) || (is.logical(existing) && all(is.na(existing)))) {
intervals$interval_id <- NA_character_
existing <- character(0)
Expand Down Expand Up @@ -1484,7 +1484,7 @@ interval_assign_ref_ids <- function(intervals, ref_groups, ref_id, prefix = "ref
need_new <- rep(FALSE, length(ref_groups))
for (i in seq_along(ref_groups)) {
current <-
if (is.null(intervals$interval_id)) {
if (is.null(intervals[["interval_id"]])) {
NULL
} else {
stats::na.omit(intervals$interval_id[ref_groups[[i]]])
Expand All @@ -1511,8 +1511,8 @@ interval_assign_ref_ids <- function(intervals, ref_groups, ref_id, prefix = "ref
}
}
if (!is.null(ref_id) &&
(is.null(intervals$interval_id) ||
(is.logical(intervals$interval_id) && all(is.na(intervals$interval_id))))) {
(is.null(intervals[["interval_id"]]) ||
(is.logical(intervals[["interval_id"]]) && all(is.na(intervals[["interval_id"]]))))) {
intervals$interval_id <- ref_id[rep(NA_integer_, nrow(intervals))]
}
if (any(need_new)) {
Expand Down Expand Up @@ -1543,34 +1543,54 @@ interval_reference_groups <- function(intervals, ref_rows) {
}

# Which of the reference intervals a test row uses. One reference serves every
# test row; several are told apart by matching the test row's own times, or by
# a single reference covering them (a created reference can be wider than its
# test interval when it spans whole collections).
interval_pick_reference <- function(intervals, row, ref_groups, param) {
# test row. Several are first narrowed to those sharing the test row's values
# in `match_cols` (the group columns that `reference` does not name and that may
# not vary), so per-subject intervals each take their own subject's reference;
# the engine reads the group values of the reference row it is pointed at.
# When none shares them, all are candidates. The candidates are then told
# apart by matching the test row's own times, or by a single reference covering
# them (a created reference can be wider than its test interval when it spans
# whole collections).
interval_pick_reference <- function(intervals, row, ref_groups, param,
match_cols = character(0)) {
if (length(ref_groups) == 1) {
return(1L)
}
candidates <- seq_along(ref_groups)
if (length(match_cols) > 0) {
first_rows <- vapply(X = ref_groups, FUN = `[`, 1L, FUN.VALUE = 1L)
same_group <-
which(interval_match_groups(
intervals[first_rows, match_cols, drop = FALSE],
intervals[row, match_cols, drop = FALSE]
))
if (length(same_group) == 1) {
return(same_group)
} else if (length(same_group) > 1) {
candidates <- same_group
}
}
same_time <-
which(vapply(
X = ref_groups,
candidates[vapply(
X = ref_groups[candidates],
FUN = function(rows) {
(intervals$start[rows[1]] %in% intervals$start[row]) &&
(intervals$end[rows[1]] %in% intervals$end[row])
},
FUN.VALUE = TRUE
))
)]
if (length(same_time) == 1) {
return(same_time)
}
covering <-
which(vapply(
X = ref_groups,
candidates[vapply(
X = ref_groups[candidates],
FUN = function(rows) {
(intervals$start[rows[1]] <= intervals$start[row]) &&
(intervals$end[rows[1]] >= intervals$end[row])
},
FUN.VALUE = TRUE
))
)]
if (length(covering) == 1) {
return(covering)
}
Expand Down Expand Up @@ -1866,7 +1886,21 @@ interval_edit_secondary <- function(intervals, param, reference, target_groups,
ref_col <- paste0(param, "_ref")
used_groups <- integer(0)
for (row in test_rows) {
current_group <- interval_pick_reference(intervals, row, ref_groups, param)
row_ref_vary <-
if (is.null(ref_vary)) {
interval_ref_vary(intervals, row, param, default = ref_vary_default)
} else {
ref_vary
}
current_group <-
interval_pick_reference(
intervals, row, ref_groups, param,
match_cols =
setdiff(
interval_describe_cols(intervals),
c("start", "end", names(reference), row_ref_vary)
)
)
current_id <- assigned$ids[[current_group]]
current_pointer <- intervals[[ref_col]][row]
if (!is.na(current_pointer) &&
Expand Down
80 changes: 80 additions & 0 deletions tests/testthat/test-secondary-parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,86 @@ test_that("interval_add_secondary() creates one reference per test interval", {
expect_equal(intervals_linked$clr.last_ref, c("ref1", "ref2", NA, NA))
})

# Automatically generated intervals have one row per subject and dosing
# interval, so `reference = data.frame(start = 0, end = 12)` matches one row per
# subject; each subject's later interval must take its own subject's reference,
# since the calculation reads the group values of the row it points at.
# linear auclast, subject 1: 0-12 = (0+10)/2*2 + (10+2)/2*10 = 70
# 12-24 = (2+12)/2*2 + (12+4)/2*10 = 94
# subject 2: 0-12 = (0+20)/2*2 + (20+2)/2*10 = 130
test_that("interval_add_secondary() links generated per-subject intervals to their own subject", {
d_conc_md <-
data.frame(
subject = rep(1:2, each = 5),
time = rep(c(0, 2, 12, 14, 24), 2),
conc = c(0, 10, 2, 12, 4, 0, 20, 2, 12, 4)
)
d_dose_md <- data.frame(subject = rep(1:2, each = 2), time = rep(c(0, 12), 2), dose = 1)
o_data_md <-
PKNCAdata(
PKNCAconc(d_conc_md, conc~time|subject), PKNCAdose(d_dose_md, dose~time|subject),
options = list(auc.method = "linear")
)
# The generated intervals are a tibble without an `interval_id` column
expect_s3_class(o_data_md$intervals, "tbl_df")
expect_false("interval_id" %in% names(o_data_md$intervals))
expect_no_warning(
o_data_linked <-
interval_add_secondary(
o_data_md, param = "ratio.auclast", reference = data.frame(start = 0, end = 12)
)
)
expect_equal(o_data_linked$intervals$subject, c(1L, 1L, 2L, 2L))
expect_equal(o_data_linked$intervals$start, c(0, 12, 0, 12))
expect_equal(o_data_linked$intervals$interval_id, c("ref1", NA, "ref2", NA))
expect_equal(o_data_linked$intervals$ratio.auclast_ref, c(NA, "ref1", NA, "ref2"))
expect_equal(o_data_linked$intervals$ratio.auclast, c(FALSE, TRUE, FALSE, TRUE))
d_res <- suppressWarnings(as.data.frame(pk.nca(o_data_linked)))
d_ratio <- d_res[d_res$PPTESTCD %in% "ratio.auclast", ]
expect_equal(d_ratio$subject, 1:2)
expect_equal(d_ratio$PPORRES, c(94/70, 94/130))
})

# A group column named in `ref_vary` may differ between the interval and its
# reference, so it does not take part in choosing among the reference rows: in
# this crossover subject 1 received the reference in period 1 and subject 2 in
# period 2, and each test profile takes its own subject's reference.
test_that("interval_add_secondary() picks the subject's own reference across varying groups", {
d_conc_xo <-
data.frame(
subject = rep(1:2, each = 6),
period = rep(rep(1:2, each = 3), 2),
treatment = c(rep(c("ref", "test"), each = 3), rep(c("test", "ref"), each = 3)),
time = 0:2,
conc = c(0, 10, 5, 0, 6, 3, 0, 9, 4, 0, 20, 8)
)
d_dose_xo <- unique(d_conc_xo[, c("subject", "period", "treatment")])
d_dose_xo$time <- 0
d_dose_xo$dose <- 1
o_data_xo <-
PKNCAdata(
PKNCAconc(d_conc_xo, conc~time|treatment+period+subject),
PKNCAdose(d_dose_xo, dose~time|treatment+period+subject),
intervals =
data.frame(
subject = c(1, 1, 2, 2), period = c(1, 2, 1, 2),
treatment = c("ref", "test", "test", "ref"),
start = 0, end = Inf, cmax = TRUE
)
)
o_data_linked <-
interval_add_secondary(
o_data_xo, param = "ratio.cmax", reference = data.frame(treatment = "ref"),
ref_vary = "period"
)
expect_equal(o_data_linked$intervals$interval_id, c("ref1", NA, NA, "ref2"))
expect_equal(o_data_linked$intervals$ratio.cmax_ref, c(NA, "ref1", "ref2", NA))
d_res <- as.data.frame(pk.nca(o_data_linked))
d_ratio <- d_res[d_res$PPTESTCD %in% "ratio.cmax", ]
expect_equal(d_ratio$subject, c(1, 2))
expect_equal(d_ratio$PPORRES, c(6/10, 9/20))
})

# A created reference row is a new interval: it carries the columns of the
# intervals it was built from but none of their bookkeeping
test_that("a created reference interval takes no identifier or imputation of its own", {
Expand Down
Loading