diff --git a/NEWS.md b/NEWS.md index 18247a33..fa674efe 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,14 @@ the dosing including dose amount and route. ## Breaking changes +* A negative numeric dose amount is an error (`pknca_error_dose_negative`, + naming the first rows and their count), in `PKNCAdose()` and again in + `pk.nca()` for a dose made negative later, for example by `mutate()` + (@billdenney). Code that passed a negative dose before now stops. A zero + or missing dose amount is allowed as before, a dose row excluded with + `exclude` is not checked, and a dose column that is not numeric is not + checked here. + * The excretion rates `ertlst`, `ermax`, `ertmax`, and `erlst` are now `NA` when a collection with a volume has no concentration, as `ae`, `fe`, and `erint` already were (@billdenney, #729). They were calculated @@ -18,6 +26,27 @@ the dosing including dose amount and route. missing both its concentration and its volume, or a known concentration with a missing volume, leaves the rates as they were. +* A calculation function that returns `NA` because of missing or unusable data + now returns it with an `exclude` attribute that gives the reason, as the AUC + functions already did, so that `pk.nca()` reports why (@billdenney, #727). + The value is unchanged. The functions are `pk.calc.cmax()`, `pk.calc.cmin()`, + `pk.calc.tmax()`, `pk.calc.tmin()`, `pk.calc.tlast()`, `pk.calc.tfirst()`, + `pk.calc.clast.obs()`, `pk.calc.aucpext()`, `pk.calc.cl()`, `pk.calc.f()`, + `pk.calc.mrt()`, `pk.calc.mrt.iv()`, `pk.calc.mrt.md()`, + `pk.calc.mrt.md.iv()`, `pk.calc.cav()`, `pk.calc.ctrough()`, + `pk.calc.cstart()`, `pk.calc.ptr()`, `pk.calc.tlag()`, `pk.calc.deg.fluc()`, + `pk.calc.ceoi()`, `pk.calc.totdose()`, and `pk.calc.dn()`; the vectorised + ones give the attribute only for a result of length one. For a result `x` + such as `pk.calc.cmax(c(NA, NA))`: + * these now fail: `identical(x, NA)`, `isTRUE(all.equal(x, NA))`, + `expect_equal(x, NA)`, `expect_identical(x, NA)`, and + `expect_equal(x, NA_real_)`; + * the attribute is carried by `x * 2` and by `data.frame(a = x)$a`; + * `is.na()`, `ifelse()`, `c()`, `sapply()`, `sum()`, `max()`, and `paste()` + are unaffected; + * `as.numeric(x)` drops the attribute (giving `NA_real_`, where `cmax`, + `cmin`, `tmax`, `tmin`, `tlast`, and `tfirst` gave a logical `NA`). + * Parameter names and descriptions follow one style, so some column headers of `summary()` and some descriptions change (@billdenney): * Pretty names write lambda.z as math (`$\lambda_z$`), including "Number of @@ -1222,6 +1251,82 @@ the generated intervals: ## Bug fixes +* `PKNCAdose()` with a `rate` column now works with more than one dose row + (@billdenney, #728). It stopped with "'length = 2' in coercion + to 'logical(1)'", because the dose amounts were passed where the name of the + dose column was expected; one row worked by accident. Each dose's duration + is its amount divided by its rate. A `rate` with no dose amount (a formula + without a left side) is now the error `pknca_error_rate_without_dose`; it + was `pknca_error_dose_invalid_duration` with one row and an error without a + class with more. A direct `setDuration(object, rate = 5, dose = NA)` + returned a duration of 0 and is now that error too. + +* `pk.calc.tlag()` skips missing concentrations, as `pk.calc.cmax()` and the + other simple parameters do, and gains a `check` argument like theirs + (reported by @kawasakin, #723; @billdenney, #727). Two results change: a + missing value before or between the measured ones gave `NA` (for example + `pk.calc.tlag(c(NA, 40, 80, 10), c(0, 2, 4, 8))`) and now gives the lag time + of the measured values (2); and a missing value with no increase after it, + or every value missing, stopped the calculation (in `pk.nca()`, "Please + report a bug ... missing value where TRUE/FALSE needed") and now gives `NA`. + +* `pk.calc.swing()` gives `NA` for a missing `cmin` or `cmax` instead of + stopping with "missing value where TRUE/FALSE needed" (reported by + @kawasakin, #723; @billdenney, #727). In `pk.nca()`, an interval whose + concentrations are all missing stopped with "Please report a bug" when + `swing` was requested, which stopped the calculation for every subject, and + now gives `NA` for that interval. `pk.calc.swing()` gives one value for + each pair of `cmax` and `cmin`; a vector of `cmax` with one `cmin` gives the + values it gave before, except that with a `cmin` of zero or below the result + is one `Inf` for each `cmax` (before, a single `Inf`). + +* A simple parameter that is `NA` because of missing or unusable data now says + why in the `exclude` column of `pk.nca()` results, where the column was + empty (@billdenney, #727). No value changes; only the `exclude` text of + results that were already `NA`. The reasons, which the calculation functions + return as an `exclude` attribute (see "Breaking changes"): + - "All concentrations are missing": `cmax`, `cmin`, `tmax`, `tmin`, `tlast`, + `tfirst`, `clast.obs`, `tlag`. + - "No concentration is above zero": `tmax`, `tlast`, `tfirst`. + - "Concentrations never increase": `tlag`. + - "No non-missing concentration at the start of the interval": `cstart`; + "No non-missing concentration at the end of the interval" and "The + interval has no finite end": `ctrough`; "No non-missing concentration at + the end of the infusion" and "Dose duration is missing (or there is no + dose in the interval)": `ceoi`. + - "AUC is zero or negative": `cl.*`, `mrt.*` (all forms), and the reference + AUC of `f.*`; "AUCinf is zero": `aucpext.*`; "Cav is zero": `deg.fluc`; + "Ctrough is zero": `ptr`. + - "Dose is missing": `cl.*`, `f.*`, `totdose`, and the dose-normalized + parameters (`*.dn`); "Dose is zero": `*.dn` when the parameter is also + zero (the value stays `NaN`); "Dose is zero or negative": `f.*`; "No dose + in the interval": `cl.*`, `totdose`, `mrt.iv.*`, and `mrt.ivmd.*`. + - "A dose time or duration is missing", or "A dose amount is missing, or the + doses sum to zero" (when several doses are weighted): `mrt.iv.*` and + `mrt.ivmd.*`; "The dosing interval (tau) is not known": `mrt.md.*` and + `mrt.ivmd.*`. + - "The interval start or end is missing", "The interval has zero duration": + `cav.*`. + + Everything calculated from one of these carries its reason, as for any + other reason (for example `swing`, `vss.*`, `vz.*`, `kel.*`, `thalf.eff.*`). + What changes for code reading the results: `summary()` gives the same + numbers (it ignores an `NA` either way); `as.data.frame(filter_excluded = + TRUE)` now leaves out these `NA` rows, as it leaves out any row with a + reason; and `exclude()` adds its reason after the one already there. + +* The `exclude` text of a result that already had a reason now starts with the + root cause and lists each reason once (@billdenney, #727). For example, + the half-life results of an interval with every concentration missing read + "All concentrations are missing; Too few points for half-life calculation + (min.hl.points=3 with only 0 points)" instead of starting at "Too few + points"; `aucabove.trough.all` with no concentration at the end of the + interval reads "No non-missing concentration at the end of the interval; + Missing concentration to be above"; and a reason that reached a result from + two inputs ("X; X") is given once ("X"). Code that matches a reason by its + exact text must be updated. The values, and which results have a reason, + are unchanged. + * `mrt.iv.*`, `mrt.ivint.*`, `mrt.ivmd.*`, and everything calculated from them (`vss.iv.*`, `vss.ivint.*`, `vss.ivmd.*`, `kel.iv.*`, `thalf.eff.iv.*`) are now correct, and one value per interval, when an interval has more than one diff --git a/R/class-PKNCAdose.R b/R/class-PKNCAdose.R index 4e5427e0..35913828 100644 --- a/R/class-PKNCAdose.R +++ b/R/class-PKNCAdose.R @@ -16,9 +16,12 @@ #' column from the `data`, and if given as a number, it is the value for all #' doses. Only one may be given, and if neither is given, then the dose is #' assumed to be a bolus (`duration=0`). If `rate` is given, then the dose -#' amount must be given (the left hand side of the `formula`). A numeric -#' duration is in the time unit of the analysis; a difftime duration is -#' converted to that unit in [PKNCAdata()]. +#' amount must be given (the left hand side of the `formula`; +#' `pknca_error_rate_without_dose` otherwise), and the duration of each dose +#' is its amount divided by its rate. A missing dose amount or rate, or a +#' rate of zero, gives no valid duration (`pknca_error_dose_invalid_duration`). +#' A numeric duration is in the time unit of the analysis; a difftime +#' duration is converted to that unit in [PKNCAdata()]. #' @param time.nominal (optional) The name of the nominal time column (if the #' main time variable is actual time. The `time.nominal` is not used during #' calculations; it is available to assist with data summary and checking. @@ -43,6 +46,12 @@ #' same dose is given in every interval. When given as a two-sided #' formula, both the dose amount and time are used directly from the #' data. +#' +#' A negative numeric dose amount is an error (`pknca_error_dose_negative`, +#' naming the rows), here and again in [pk.nca()] for a dose made negative +#' later (for example by `mutate()`). A zero or missing amount is allowed, +#' and a row excluded with `exclude` is not checked. A dose column that is +#' not numeric is not checked here. #' @family PKNCA objects #' @export PKNCAdose <- function(data, ...) @@ -152,6 +161,7 @@ PKNCAdose.data.frame <- function(data, formula, route, rate, duration, class = "pknca_error_partial_missing_indepvar" ) } + assert_dose_not_negative(ret) if (length(parsed_form$time) == 1 && inherits(data[[parsed_form$time]], "Date")) { pknca_warn_date_time(time_col = parsed_form$time, data_type = "dose") } @@ -160,8 +170,8 @@ PKNCAdose.data.frame <- function(data, formula, route, rate, duration, } else { ret <- setRoute(ret, route) } - ret <- setDuration.PKNCAdose(ret, duration=duration, - rate=rate, dose=getDepVar.PKNCAdose(ret)) + # The dose is the column name, which setDuration.PKNCAdose() uses by default + ret <- setDuration.PKNCAdose(ret, duration=duration, rate=rate) if (!missing(time.nominal)) { ret <- setAttributeColumn(object=ret, @@ -224,7 +234,9 @@ setRoute.PKNCAdose <- function(object, route, ...) { #' @param duration The value to set for the duration or the name of the column #' in the data to use for the duration. #' @param rate (for PKNCAdose objects only) The rate of infusion -#' @param dose (for PKNCAdose objects only) The dose amount +#' @param dose (for PKNCAdose objects only) The name of the dose column (the +#' default, from the formula), or the dose amounts (a value, or one per +#' row); used with `rate` #' @param ... Arguments passed to another setDuration function #' @returns The object with duration set #' @export @@ -245,7 +257,14 @@ setDuration.PKNCAdose <- function(object, duration, rate, dose, ...) { # requiring near-equal checks for floating point error. } else if (!missing(duration)) { object <- setAttributeColumn(object=object, attr_name="duration", col_or_value=duration) - } else if (!missing(rate) && !missing(dose) && !is.na(dose)) { + } else if (!missing(rate)) { + # `dose` is a column name (the default) or the dose amounts + if (length(dose) == 0 || all(is.na(dose))) { + rlang::abort( + "`rate` needs the dose amount (the left side of the formula) to calculate the duration; without a dose amount, give `duration` instead", + class = "pknca_error_rate_without_dose" + ) + } tmprate <- getColumnValueOrNot(object$data, rate, "rate") tmpdose <- getColumnValueOrNot(object$data, dose, "dose") duration <- tmpdose$data[[tmpdose$name]]/tmprate$data[[tmprate$name]] @@ -293,6 +312,42 @@ getDepVar.PKNCAdose <- function(x, ...) { } } +#' Require that no dose amount is negative +#' +#' A negative dose amount is invalid input. A zero amount and a missing +#' amount are allowed, and an excluded row is not checked, as for the dose +#' times. A dose column that is not numeric (character, factor, difftime) is +#' not checked: it fails later, where it is used as a number. +#' +#' @param object A PKNCAdose object +#' @returns `object`, invisibly, or the error `pknca_error_dose_negative` +#' naming the first rows of the dose data with a negative amount and their +#' count +#' @keywords Internal +#' @noRd +assert_dose_not_negative <- function(object) { + dose <- getDepVar.PKNCAdose(object) + if (!is.numeric(dose)) { + return(invisible(object)) + } + # The plain numbers, so that a units column needs no method + values <- as.numeric(dose) + rows <- which(!is.na(values) & values < 0 & is.na(normalize_exclude(object))) + if (length(rows) > 0) { + rlang::abort( + sprintf( + "Dose amounts (column '%s') must not be negative; %d row(s) of the dose data are: %s%s", + object$columns$dose, + length(rows), + paste(utils::head(rows, 5), collapse = ", "), + if (length(rows) > 5) ", ..." else "" + ), + class = "pknca_error_dose_negative" + ) + } + invisible(object) +} + #' @export getIndepVar.PKNCAdose <- function(x, ...) { time_col <- x$columns$time diff --git a/R/pk.calc.all.R b/R/pk.calc.all.R index 6c484cc4..75549591 100644 --- a/R/pk.calc.all.R +++ b/R/pk.calc.all.R @@ -29,6 +29,11 @@ #' @export pk.nca <- function(data, verbose=FALSE) { assert_PKNCAdata(data) + # PKNCAdose() refused a negative dose amount; a mutate() or join on the + # dose data afterwards may have made one + if (inherits(data$dose, "PKNCAdose")) { + assert_dose_not_negative(data$dose) + } # Date-time times and intervals and difftime durations become numbers here, # so that intervals can change after PKNCAdata(). The results keep the # converted data, which is the data the calculations used. @@ -975,10 +980,13 @@ bind_interval_result <- function(interval_cols, calculated) { # Combine exclusion reasons from a calculation's inputs with the exclusion the # calculation itself set (the "exclude" attribute). "DO NOT EXCLUDE" on the -# result wins and clears everything. Documented in -# vignettes/v80-writing-parameter-functions.Rmd. +# result wins and clears everything. Each input's exclusion may already be +# several reasons joined with "; ", so they are split before a reason given by +# more than one input (cmax and cmin of an all-missing profile) is listed once. +# Documented in vignettes/v80-writing-parameter-functions.Rmd. combine_exclude_reasons <- function(from_inputs, from_result) { - reasons <- stats::na.omit(c(from_inputs, from_result)) + reasons <- stats::na.omit(as.character(c(from_inputs, from_result))) + reasons <- unique(unlist(strsplit(reasons, split = "; ", fixed = TRUE))) if (identical(from_result, "DO NOT EXCLUDE")) { NA_character_ } else if (length(reasons) > 0) { diff --git a/R/pk.calc.simple.R b/R/pk.calc.simple.R index da8f5731..b8a00c81 100644 --- a/R/pk.calc.simple.R +++ b/R/pk.calc.simple.R @@ -16,12 +16,28 @@ adj.r.squared <- function(r.sq, n) { } } +# Set the values of `ret` selected by `mask` to `NA` and, for a single value, +# add `reason` to its "exclude" attribute, which pk.nca() puts in the exclude +# column (see combine_exclude_reasons()). An attribute describes the whole +# vector, so a longer result gets none rather than one that would also sit +# beside its valid values; pk.nca() calls with single values. `ret` is +# unchanged when `mask` selects nothing. +na_with_reason <- function(ret, mask, reason) { + if (any(mask)) { + ret[mask] <- NA_real_ + if (length(ret) == 1) { + attr(ret, "exclude") <- paste(c(attr(ret, "exclude"), reason), collapse = "; ") + } + } + ret +} + #' Determine maximum observed PK concentration #' #' @inheritParams assert_conc_time #' @param check Run [assert_conc()]? #' @return a number for the maximum concentration or NA if all concentrations -#' are missing +#' are missing, with the reason in its "exclude" attribute #' @family NCA parameters for concentrations during the intervals #' @export pk.calc.cmax <- function(conc, check=TRUE) { @@ -29,7 +45,7 @@ pk.calc.cmax <- function(conc, check=TRUE) { assert_conc(conc = conc) } if (length(conc) == 0 || all(is.na(conc))) { - NA + structure(NA, exclude = "All concentrations are missing") } else { max(conc, na.rm=TRUE) } @@ -61,7 +77,7 @@ pk.calc.cmin <- function(conc, check=TRUE) { assert_conc(conc=conc) } if (length(conc) == 0 || all(is.na(conc))) { - NA + structure(NA, exclude = "All concentrations are missing") } else { min(conc, na.rm=TRUE) } @@ -99,7 +115,8 @@ add.interval.col("cmin", #' @param first.tmax If there is more than one time point with the maximum value (Cmax or ERmax), #' which time should be selected for Tmax/ERTmax? If 'TRUE', the first will be selected. If not, then the #' last is considered Tmax/ERTmax. -#' @returns The time of the maximum concentration +#' @returns The time of the maximum concentration, or `NA` with the reason in +#' its "exclude" attribute when no concentration is above zero #' @family NCA time parameters #' @examples #' conc_data <- Theoph[Theoph$Subject == 1,] @@ -113,8 +130,10 @@ pk.calc.tmax <- function(conc, time, if (check) { assert_conc_time(conc = conc, time = time) } - if (length(conc) == 0 || all(conc %in% c(NA, 0))) { - NA + if (length(conc) == 0 || all(is.na(conc))) { + structure(NA, exclude = "All concentrations are missing") + } else if (all(conc %in% c(NA, 0))) { + structure(NA, exclude = "No concentration is above zero") } else { ret <- time[conc %in% pk.calc.cmax(conc, check=FALSE)] if (first.tmax) { @@ -158,7 +177,8 @@ add.interval.col("tmax", #' @param first.tmin If there is more than one time point with the minimum value (Cmin), #' which time should be selected for Tmin? If 'TRUE', the first will be selected. If not, #' then the last is considered Tmin. -#' @returns The time of the minimum concentration +#' @returns The time of the minimum concentration, or `NA` with the reason in +#' its "exclude" attribute when all concentrations are missing #' @family NCA time parameters #' @examples #' conc_data <- Theoph[Theoph$Subject == 1,] @@ -173,7 +193,7 @@ pk.calc.tmin <- function(conc, time, assert_conc_time(conc = conc, time = time) } if (length(conc) == 0 || all(is.na(conc))) { - NA + structure(NA, exclude = "All concentrations are missing") } else { ret <- time[conc %in% pk.calc.cmin(conc, check=FALSE)] if (first.tmin) { @@ -204,15 +224,19 @@ add.interval.col("tmin", #' #' @inheritParams assert_conc_time #' @inheritParams clean.conc.blq -#' @returns The time of the last observed concentration measurement +#' @returns The time of the last (`pk.calc.tlast()`) or first +#' (`pk.calc.tfirst()`) observed concentration measurement, or `NA` with the +#' reason in its "exclude" attribute when no concentration is above zero #' @family NCA time parameters #' @export pk.calc.tlast <- function(conc, time, check=TRUE) { if (check) { assert_conc_time(conc = conc, time = time) } - if (all(conc %in% c(NA, 0))) { - NA + if (all(is.na(conc))) { + structure(NA, exclude = "All concentrations are missing") + } else if (all(conc %in% c(NA, 0))) { + structure(NA, exclude = "No concentration is above zero") } else { max(time[!(conc %in% c(NA, 0))]) } @@ -238,8 +262,10 @@ pk.calc.tfirst <- function(conc, time, check=TRUE) { if (check) { assert_conc_time(conc, time) } - if (all(conc %in% c(NA, 0))) { - NA + if (all(is.na(conc))) { + structure(NA, exclude = "All concentrations are missing") + } else if (all(conc %in% c(NA, 0))) { + structure(NA, exclude = "No concentration is above zero") } else { min(time[!(conc %in% c(NA, 0))]) } @@ -268,7 +294,8 @@ add.interval.col("tfirst", #' #' @inheritParams assert_conc_time #' @inheritParams clean.conc.blq -#' @returns The last observed concentration above the LOQ +#' @returns The last observed concentration above the LOQ, or `NA` with the +#' reason in its "exclude" attribute when all concentrations are missing #' @family NCA parameters for concentrations during the intervals #' @export pk.calc.clast.obs <- function(conc, time, check=TRUE) { @@ -276,7 +303,7 @@ pk.calc.clast.obs <- function(conc, time, check=TRUE) { assert_conc_time(conc = conc, time = time) } if (all(is.na(conc))) { - NA_real_ + structure(NA_real_, exclude = "All concentrations are missing") } else if (all(conc %in% c(0, NA))) { 0 } else { @@ -516,9 +543,10 @@ add.interval.col("thalf.eff.iv.all", #' @param auclast the area under the curve from time 0 to the last measurement #' above the limit of quantification #' @param aucinf the area under the curve from time 0 to infinity -#' @returns The numeric value of the AUC percent extrapolated or `NA_real_` if -#' any of the following are true `is.na(aucinf)`, `is.na(auclast)`, -#' `aucinf <= 0`, or `auclast <= 0`. +#' @returns The numeric value of the AUC percent extrapolated, or `NA_real_` +#' if `aucinf` or `auclast` is `NA` or `aucinf` is zero (a negative value is +#' calculated, with a warning). A result of length one that is `NA` because of missing or unusable data +#' has the reason in its "exclude" attribute, which [pk.nca()] reports. #' @family Half-life and elimination #' @export pk.calc.aucpext <- function(auclast, aucinf) { @@ -557,7 +585,7 @@ pk.calc.aucpext <- function(auclast, aucinf) { ) ret[mask_calc] <- 100*(1-auclast[mask_calc]/aucinf[mask_calc]) - ret + na_with_reason(ret, mask = !mask_na & aucinf %in% 0, reason = "AUCinf is zero") } pknca_concept(pk.calc.aucpext) <- "auc_extrapolation" @@ -808,7 +836,8 @@ add.interval.col("kel.sparse.last", #' @param dose the dose administered #' @param auc The area under the concentration-time curve. #' @returns the numeric value of the total (CL) or observed oral clearance -#' (CL/F) +#' (CL/F). A result of length one that is `NA` because of missing or unusable data +#' has the reason in its "exclude" attribute, which [pk.nca()] reports. #' @details If `dose` is the same length as the other inputs, then the output #' will be the same length as all of the inputs; the function assumes that you #' are calculating for multiple intervals simultaneously. If the inputs other @@ -823,17 +852,14 @@ add.interval.col("kel.sparse.last", pk.calc.cl <- function(dose, auc) { # sum() of nothing is 0, which would be a wrong answer, not a missing one if (length(dose) == 0) { - return(NA_real_) + return(structure(NA_real_, exclude = "No dose in the interval")) } if (length(auc) == 1) { dose <- sum(dose) } ret <- dose/auc - mask_zero <- !is.na(auc) & (auc <= 0) - if (any(mask_zero)) { - ret[mask_zero] <- NA_real_ - } - ret + ret <- na_with_reason(ret, mask = !is.na(auc) & is.na(dose), reason = "Dose is missing") + na_with_reason(ret, mask = !is.na(auc) & (auc <= 0), reason = "AUC is zero or negative") } pknca_concept(pk.calc.cl) <- "clearance" @@ -1061,17 +1087,16 @@ add.interval.col("cl.sparse.last", #' method 1 #' @param auc2 The AUC from 0 to infinity or 0 to tau administered in route or #' method 2 +#' @returns The bioavailability. A result of length one that is `NA` because of missing or unusable data +#' has the reason in its "exclude" attribute, which [pk.nca()] reports. #' @export pk.calc.f <- function(dose1, auc1, dose2, auc2) { ret <- (auc2/dose2)/(auc1/dose1) - mask_zero <- - is.na(auc1) | (auc1 <= 0) | - is.na(dose2) | (dose2 <= 0) | - is.na(dose1) | (dose1 <= 0) - if (any(mask_zero)) { - ret[mask_zero] <- NA_real_ - } - ret + # A missing AUC carries its own reason from its calculation + ret[is.na(auc1)] <- NA_real_ + ret <- na_with_reason(ret, mask = is.na(dose1) | is.na(dose2), reason = "Dose is missing") + ret <- na_with_reason(ret, mask = (!is.na(dose1) & dose1 <= 0) | (!is.na(dose2) & dose2 <= 0), reason = "Dose is zero or negative") + na_with_reason(ret, mask = !is.na(auc1) & auc1 <= 0, reason = "AUC is zero or negative") } pknca_concept(pk.calc.f) <- "bioavailability" @@ -1234,7 +1259,8 @@ add.interval.col("f.int.inf.pred", #' @param time.dose The time(s) of the dose(s) relative to the start of the #' interval (the same origin as `aumc`). When missing, the dose is assumed to #' be at the interval start (time 0). -#' @returns the numeric value of the mean residence time +#' @returns the numeric value of the mean residence time. A result of length one that is `NA` because of missing or unusable data +#' has the reason in its "exclude" attribute, which [pk.nca()] reports. #' @family Mean residence time #' @export pk.calc.mrt <- function(auc, aumc) { @@ -1354,11 +1380,11 @@ add.interval.col("mrt.sparse.last", #' @export pk.calc.mrt.iv <- function(auc, aumc, duration.dose, dose = NULL, time.dose = NULL) { mit <- pk_mean_input_time(dose, time.dose, duration.dose) - ret <- aumc/auc - mit - mask_zero <- is.na(auc) | auc <= 0 - if (any(mask_zero)) { - ret[mask_zero] <- NA_real_ - } + ret <- aumc/auc - as.numeric(mit) + # A missing AUC carries its own reason from its calculation + ret[is.na(auc)] <- NA_real_ + ret <- na_with_reason(ret, mask = !is.na(auc) & is.na(mit), reason = attr(mit, "exclude")) + ret <- na_with_reason(ret, mask = !is.na(auc) & auc <= 0, reason = "AUC is zero or negative") if (length(ret) != 1) { rlang::abort("MRT must be a single value per interval (auc and aumc must each be one value)", class = "pknca_error_mrt_not_scalar") } @@ -1380,12 +1406,13 @@ pk.calc.mrt.iv <- function(auc, aumc, duration.dose, dose = NULL, time.dose = NU # dose(s) are at the interval start. # @param duration.dose The dose durations # @returns A scalar mean input time, `NA_real_` when it cannot be determined +# (with the reason in its "exclude" attribute) # @noRd pk_mean_input_time <- function(dose = NULL, time.dose = NULL, duration.dose) { checkmate::assert_numeric(duration.dose, lower = 0, finite = TRUE) n_dose <- length(duration.dose) if (n_dose == 0) { - return(NA_real_) + return(structure(NA_real_, exclude = "No dose in the interval")) } if (is.null(time.dose)) { time.dose <- rep(0, n_dose) @@ -1395,13 +1422,15 @@ pk_mean_input_time <- function(dose = NULL, time.dose = NULL, duration.dose) { checkmate::assert_numeric(dose, len = n_dose, lower = 0, finite = TRUE, null.ok = n_dose == 1) midpoint <- time.dose + duration.dose/2 + if (anyNA(midpoint)) { + return(structure(NA_real_, exclude = "A dose time or duration is missing")) + } if (n_dose == 1) { # The weight does not matter for a single dose return(as.numeric(midpoint)) } - if (anyNA(dose) || anyNA(midpoint) || sum(dose) <= 0) { - # The weights or the input times are unknown - return(NA_real_) + if (anyNA(dose) || sum(dose) <= 0) { + return(structure(NA_real_, exclude = "A dose amount is missing, or the doses sum to zero")) } as.numeric(sum(dose*midpoint)/sum(dose)) } @@ -1531,6 +1560,8 @@ add.interval.col("mrt.ivint.inf.obs", #' @param aucinf the AUC from time 0 to infinity (typically using single-dose #' data) #' @inheritParams assert_dosetau +#' @returns The mean residence time. A result of length one that is `NA` because of missing or unusable data +#' has the reason in its "exclude" attribute, which [pk.nca()] reports. #' @export pk.calc.mrt.md <- function(auctau, aumctau, aucinf, tau) { pk.calc.mrt.md.iv( @@ -1578,11 +1609,12 @@ add.interval.col("mrt.md.inf.pred", #' @export pk.calc.mrt.md.iv <- function(auctau, aumctau, aucinf, tau, duration.dose, dose = NULL, time.dose = NULL) { mit <- pk_mean_input_time(dose, time.dose, duration.dose) - ret <- aumctau/auctau + tau*(aucinf-auctau)/auctau - mit - mask_zero <- is.na(auctau) | auctau <= 0 - if (any(mask_zero)) { - ret[mask_zero] <- NA_real_ - } + ret <- aumctau/auctau + tau*(aucinf-auctau)/auctau - as.numeric(mit) + # A missing AUC carries its own reason from its calculation + ret[is.na(auctau)] <- NA_real_ + ret <- na_with_reason(ret, mask = !is.na(auctau) & is.na(tau), reason = "The dosing interval (tau) is not known") + ret <- na_with_reason(ret, mask = !is.na(auctau) & is.na(mit), reason = attr(mit, "exclude")) + ret <- na_with_reason(ret, mask = !is.na(auctau) & auctau <= 0, reason = "AUC is zero or negative") if (length(ret) != 1) { rlang::abort("MRT must be a single value per interval (auctau, aumctau, and aucinf must each be one value)", class = "pknca_error_mrt.md_not_scalar") } @@ -2110,16 +2142,14 @@ add.interval.col("vss.sparse.last", #' #' @param auc The area under the curve during the interval #' @inheritParams assert_intervaltime_single -#' @returns The Cav (average concentration during the interval) +#' @returns The Cav (average concentration during the interval). A result of length one that is `NA` because of missing or unusable data +#' has the reason in its "exclude" attribute, which [pk.nca()] reports. #' @family NCA parameters for concentrations during the intervals #' @export pk.calc.cav <- function(auc, start, end) { ret <- auc/(end-start) - mask_zero <- is.na(end) | is.na(start) | end == start - if (any(mask_zero)) { - ret[mask_zero] <- NA_real_ - } - ret + ret <- na_with_reason(ret, mask = is.na(end) | is.na(start), reason = "The interval start or end is missing") + na_with_reason(ret, mask = !is.na(end) & !is.na(start) & end == start, reason = "The interval has zero duration") } pknca_concept(pk.calc.cav) <- "average_conc" @@ -2287,16 +2317,24 @@ add.interval.col( #' #' @inheritParams assert_conc_time #' @inheritParams assert_intervaltime_single -#' @returns The concentration when `time == end`. If none match, then `NA` +#' @returns The concentration when `time == end` (`pk.calc.ctrough()`) or +#' `time == start` (`pk.calc.cstart()`). If none match, or that +#' concentration is missing, then `NA` with the reason in its "exclude" +#' attribute #' @family NCA parameters for concentrations during the intervals #' @export pk.calc.ctrough <- function(conc, time, end) { assert_conc_time(conc = conc, time = time) mask_end <- time %in% end - if (sum(mask_end) == 1) { + if (sum(mask_end) == 1 && !is.na(conc[mask_end])) { conc[mask_end] - } else if (sum(mask_end) == 0) { - NA_real_ + } else if (sum(mask_end) == 0 && length(end) == 1 && is.infinite(end)) { + structure(NA_real_, exclude = "The interval has no finite end") + } else if (sum(mask_end) <= 1) { + structure( + if (sum(mask_end) == 1) conc[mask_end] else NA_real_, + exclude = "No non-missing concentration at the end of the interval" + ) } else { # This should be impossible as assert_conc_time should catch # duplicates. @@ -2327,10 +2365,13 @@ add.interval.col("ctrough", pk.calc.cstart <- function(conc, time, start) { assert_conc_time(conc = conc, time = time) mask_start <- time %in% start - if (sum(mask_start) == 1) { + if (sum(mask_start) == 1 && !is.na(conc[mask_start])) { conc[mask_start] - } else if (sum(mask_start) == 0) { - NA_real_ + } else if (sum(mask_start) <= 1) { + structure( + if (sum(mask_start) == 1) conc[mask_start] else NA_real_, + exclude = "No non-missing concentration at the start of the interval" + ) } else { # This should be impossible as assert_conc_time should catch # duplicates. @@ -2358,13 +2399,13 @@ add.interval.col("cstart", #' #' @param cmax The maximum observed concentration #' @param ctrough The last concentration in an interval -#' @return The ratio of cmax to ctrough (if ctrough == 0, NA) +#' @return The ratio of cmax to ctrough (if ctrough == 0, NA). A result of length one that is `NA` because of missing or unusable data +#' has the reason in its "exclude" attribute, which [pk.nca()] reports. #' @family Multiple-dose PK parameters #' @export pk.calc.ptr <- function(cmax, ctrough) { ret <- cmax/ctrough - ret[ctrough %in% 0] <- NA_real_ - ret + na_with_reason(ret, mask = ctrough %in% 0, reason = "Ctrough is zero") } pknca_concept(pk.calc.ptr) <- "fluctuation" @@ -2384,17 +2425,35 @@ add.interval.col("ptr", #' concentration above the limit of quantification or above the first #' concentration in the interval) #' +#' Missing concentrations are skipped, as in [pk.calc.cmax()] and the other +#' simple parameters: the increase is looked for among the measured +#' concentrations. `NA` is returned when no measured concentration increases +#' (including when every concentration is missing). Because the lag is found +#' among the 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, the time of the last measured zero. +#' #' @inheritParams assert_conc_time -#' @returns The time associated with the first increasing concentration +#' @param check Run [assert_conc_time()]? +#' @returns The time associated with the first increasing concentration, or +#' `NA` with the reason in its "exclude" attribute when concentrations never +#' increase #' @family NCA time parameters #' @export -pk.calc.tlag <- function(conc, time) { - assert_conc_time(conc = conc, time = time) +pk.calc.tlag <- function(conc, time, check = TRUE) { + if (check) { + assert_conc_time(conc = conc, time = time) + } + mask_measured <- !is.na(conc) + conc <- conc[mask_measured] + time <- time[mask_measured] mask.increase <- c(conc[-1] > conc[-length(conc)], FALSE) if (any(mask.increase)) { time[mask.increase][1] + } else if (length(conc) == 0) { + structure(NA_real_, exclude = "All concentrations are missing") } else { - NA_real_ + structure(NA_real_, exclude = "Concentrations never increase") } } @@ -2421,15 +2480,15 @@ add.interval.col("tlag", #' @param cmin The minimum observed concentration #' @param cav The average concentration in the interval #' @returns The degree of fluctuation around the average concentration. +#' A result of length one that is `NA` because of missing or unusable data +#' has the reason in its "exclude" attribute, which [pk.nca()] reports. #' @family Multiple-dose PK parameters #' @export pk.calc.deg.fluc <- function(cmax, cmin, cav) { ret <- 100*(cmax - cmin)/cav - mask_zero <- is.na(cav) | cav == 0 - if (any(mask_zero)) { - ret[mask_zero] <- NA_real_ - } - ret + # A missing cav carries its own reason from its calculation + ret[is.na(cav)] <- NA_real_ + na_with_reason(ret, mask = cav %in% 0, reason = "Cav is zero") } pknca_concept(pk.calc.deg.fluc) <- "fluctuation" @@ -2450,15 +2509,16 @@ add.interval.col("deg.fluc", #' #' @details swing is `100*(cmax - cmin)/cmin`. #' -#' @returns The swing above the minimum concentration. If `cmin` is zero, then -#' the result is infinity. +#' @returns The swing above the minimum concentration, one value for each pair +#' of `cmax` and `cmin`. If `cmin` is zero, then the result is infinity; if +#' `cmin` or `cmax` is missing, it is `NA`. #' @export pk.calc.swing <- function(cmax, cmin) { - if (cmin > 0) { - 100*(cmax - cmin)/cmin - } else { - Inf - } + ret <- 100*(cmax - cmin)/cmin + # A cmin at or below zero gives infinity, also where the division does not + # (a cmax of zero, or a negative cmin) + ret[!is.na(cmax) & !is.na(cmin) & cmin <= 0] <- Inf + ret } pknca_concept(pk.calc.swing) <- "fluctuation" @@ -2481,7 +2541,8 @@ add.interval.col("swing", #' @param duration.dose The duration for the dosing administration (typically #' from IV infusion) #' @returns The concentration at the end of the infusion, `NA` if -#' `duration.dose` is `NA`, or `NA` if all `time != duration.dose` +#' `duration.dose` is `NA`, or `NA` if all `time != duration.dose` or that +#' concentration is missing; an `NA` has the reason in its "exclude" attribute #' @family NCA parameters for concentrations during the intervals #' @export pk.calc.ceoi <- function(conc, time, duration.dose=NA, check=TRUE) { @@ -2489,9 +2550,9 @@ pk.calc.ceoi <- function(conc, time, duration.dose=NA, check=TRUE) { assert_conc_time(conc = conc, time = time) } if (is.na(duration.dose)) { - NA_real_ - } else if (all(time != duration.dose)) { - NA_real_ + structure(NA_real_, exclude = "Dose duration is missing (or there is no dose in the interval)") + } else if (all(time != duration.dose) || is.na(conc[time == duration.dose][1])) { + structure(NA_real_, exclude = "No non-missing concentration at the end of the infusion") } else { conc[time == duration.dose][1] } @@ -2647,13 +2708,14 @@ add.interval.col( #' Extract the dose used for calculations #' #' @inheritParams pk.calc.cl -#' @returns The total dose for an interval +#' @returns The total dose for an interval, or `NA` with the reason in its +#' "exclude" attribute when there is no dose or a dose is missing #' @export pk.calc.totdose <- function(dose) { if (length(dose) == 0) { - return(NA_real_) + return(structure(NA_real_, exclude = "No dose in the interval")) } - sum(dose) + na_with_reason(sum(dose), mask = anyNA(dose), reason = "Dose is missing") } pknca_concept(pk.calc.totdose) <- "total_dose" diff --git a/R/zzz-pk.calc.dn.R b/R/zzz-pk.calc.dn.R index 1dcc62b7..d44cb910 100644 --- a/R/zzz-pk.calc.dn.R +++ b/R/zzz-pk.calc.dn.R @@ -2,12 +2,20 @@ #' #' @param parameter Parameter to dose normalize #' @param dose Dose in units compatible with the area under the curve -#' @returns a number for dose normalized AUC +#' @returns a number for dose normalized AUC. A result of length one has the +#' reason in its "exclude" attribute when it is `NA` because the dose is +#' missing, or `NaN` because the parameter and the dose are both zero. #' @examples #' pk.calc.dn(90, 10) #' @export pk.calc.dn <- function(parameter, dose) { - parameter/dose + # A missing parameter carries its own reason from its calculation + ret <- na_with_reason(parameter/dose, mask = !is.na(parameter) & is.na(dose), reason = "Dose is missing") + # 0/0 stays NaN, as it was; its reason says why + if (length(ret) == 1 && is.nan(ret) && isTRUE(dose == 0)) { + attr(ret, "exclude") <- "Dose is zero" + } + ret } local({ diff --git a/man/PKNCAdose.Rd b/man/PKNCAdose.Rd index 453c2641..cf7ce40d 100644 --- a/man/PKNCAdose.Rd +++ b/man/PKNCAdose.Rd @@ -48,9 +48,12 @@ duration of dosing. If given as a character string, it is the name of a column from the \code{data}, and if given as a number, it is the value for all doses. Only one may be given, and if neither is given, then the dose is assumed to be a bolus (\code{duration=0}). If \code{rate} is given, then the dose -amount must be given (the left hand side of the \code{formula}). A numeric -duration is in the time unit of the analysis; a difftime duration is -converted to that unit in \code{\link[=PKNCAdata]{PKNCAdata()}}.} +amount must be given (the left hand side of the \code{formula}; +\code{pknca_error_rate_without_dose} otherwise), and the duration of each dose +is its amount divided by its rate. A missing dose amount or rate, or a +rate of zero, gives no valid duration (\code{pknca_error_dose_invalid_duration}). +A numeric duration is in the time unit of the analysis; a difftime +duration is converted to that unit in \code{\link[=PKNCAdata]{PKNCAdata()}}.} \item{time.nominal}{(optional) The name of the nominal time column (if the main time variable is actual time. The \code{time.nominal} is not used during @@ -88,6 +91,12 @@ per group. When the right side is missing, PKNCA assumes that the same dose is given in every interval. When given as a two-sided formula, both the dose amount and time are used directly from the data. + +A negative numeric dose amount is an error (\code{pknca_error_dose_negative}, +naming the rows), here and again in \code{\link[=pk.nca]{pk.nca()}} for a dose made negative +later (for example by \code{mutate()}). A zero or missing amount is allowed, +and a row excluded with \code{exclude} is not checked. A dose column that is +not numeric is not checked here. } \seealso{ Other PKNCA objects: diff --git a/man/pk.calc.aucpext.Rd b/man/pk.calc.aucpext.Rd index 247b83b0..b86a58d5 100644 --- a/man/pk.calc.aucpext.Rd +++ b/man/pk.calc.aucpext.Rd @@ -13,9 +13,10 @@ above the limit of quantification} \item{aucinf}{the area under the curve from time 0 to infinity} } \value{ -The numeric value of the AUC percent extrapolated or \code{NA_real_} if -any of the following are true \code{is.na(aucinf)}, \code{is.na(auclast)}, -\code{aucinf <= 0}, or \code{auclast <= 0}. +The numeric value of the AUC percent extrapolated, or \code{NA_real_} +if \code{aucinf} or \code{auclast} is \code{NA} or \code{aucinf} is zero (a negative value is +calculated, with a warning). A result of length one that is \code{NA} because of missing or unusable data +has the reason in its "exclude" attribute, which \code{\link[=pk.nca]{pk.nca()}} reports. } \description{ Calculate the AUC percent extrapolated diff --git a/man/pk.calc.cav.Rd b/man/pk.calc.cav.Rd index f617435d..322bd937 100644 --- a/man/pk.calc.cav.Rd +++ b/man/pk.calc.cav.Rd @@ -14,7 +14,8 @@ pk.calc.cav(auc, start, end) \item{end}{The end time of the interval} } \value{ -The Cav (average concentration during the interval) +The Cav (average concentration during the interval). A result of length one that is \code{NA} because of missing or unusable data +has the reason in its "exclude" attribute, which \code{\link[=pk.nca]{pk.nca()}} reports. } \description{ Calculate the average concentration during an interval. diff --git a/man/pk.calc.ceoi.Rd b/man/pk.calc.ceoi.Rd index 80c0a224..dc6e831d 100644 --- a/man/pk.calc.ceoi.Rd +++ b/man/pk.calc.ceoi.Rd @@ -18,7 +18,8 @@ from IV infusion)} } \value{ The concentration at the end of the infusion, \code{NA} if -\code{duration.dose} is \code{NA}, or \code{NA} if all \code{time != duration.dose} +\code{duration.dose} is \code{NA}, or \code{NA} if all \code{time != duration.dose} or that +concentration is missing; an \code{NA} has the reason in its "exclude" attribute } \description{ Determine the concentration at the end of infusion diff --git a/man/pk.calc.cl.Rd b/man/pk.calc.cl.Rd index d0a970f9..c3ffff44 100644 --- a/man/pk.calc.cl.Rd +++ b/man/pk.calc.cl.Rd @@ -13,7 +13,8 @@ pk.calc.cl(dose, auc) } \value{ the numeric value of the total (CL) or observed oral clearance -(CL/F) +(CL/F). A result of length one that is \code{NA} because of missing or unusable data +has the reason in its "exclude" attribute, which \code{\link[=pk.nca]{pk.nca()}} reports. } \description{ Calculate the (observed oral) clearance diff --git a/man/pk.calc.clast.obs.Rd b/man/pk.calc.clast.obs.Rd index ca54a0a5..ead72509 100644 --- a/man/pk.calc.clast.obs.Rd +++ b/man/pk.calc.clast.obs.Rd @@ -15,7 +15,8 @@ pk.calc.clast.obs(conc, time, check = TRUE) \item{check}{Run \code{\link[=assert_conc_time]{assert_conc_time()}}?} } \value{ -The last observed concentration above the LOQ +The last observed concentration above the LOQ, or \code{NA} with the +reason in its "exclude" attribute when all concentrations are missing } \description{ If all concentrations are missing, \code{NA_real_} is returned. If all diff --git a/man/pk.calc.cmax.Rd b/man/pk.calc.cmax.Rd index b23da96c..53e4823a 100644 --- a/man/pk.calc.cmax.Rd +++ b/man/pk.calc.cmax.Rd @@ -16,7 +16,7 @@ pk.calc.cmin(conc, check = TRUE) } \value{ a number for the maximum concentration or NA if all concentrations -are missing +are missing, with the reason in its "exclude" attribute } \description{ Determine maximum observed PK concentration diff --git a/man/pk.calc.ctrough.Rd b/man/pk.calc.ctrough.Rd index 4e910d6f..046ed730 100644 --- a/man/pk.calc.ctrough.Rd +++ b/man/pk.calc.ctrough.Rd @@ -19,7 +19,10 @@ pk.calc.cstart(conc, time, start) \item{start}{The start time of the interval} } \value{ -The concentration when \code{time == end}. If none match, then \code{NA} +The concentration when \code{time == end} (\code{pk.calc.ctrough()}) or +\code{time == start} (\code{pk.calc.cstart()}). If none match, or that +concentration is missing, then \code{NA} with the reason in its "exclude" +attribute } \description{ Determine the trough (end of interval) concentration diff --git a/man/pk.calc.deg.fluc.Rd b/man/pk.calc.deg.fluc.Rd index f240be7c..1f8bbb6a 100644 --- a/man/pk.calc.deg.fluc.Rd +++ b/man/pk.calc.deg.fluc.Rd @@ -18,9 +18,12 @@ pk.calc.swing(cmax, cmin) } \value{ The degree of fluctuation around the average concentration. +A result of length one that is \code{NA} because of missing or unusable data +has the reason in its "exclude" attribute, which \code{\link[=pk.nca]{pk.nca()}} reports. -The swing above the minimum concentration. If \code{cmin} is zero, then -the result is infinity. +The swing above the minimum concentration, one value for each pair +of \code{cmax} and \code{cmin}. If \code{cmin} is zero, then the result is infinity; if +\code{cmin} or \code{cmax} is missing, it is \code{NA}. } \description{ Determine the degree of fluctuation diff --git a/man/pk.calc.dn.Rd b/man/pk.calc.dn.Rd index a991a286..cd5d0e4f 100644 --- a/man/pk.calc.dn.Rd +++ b/man/pk.calc.dn.Rd @@ -12,7 +12,9 @@ pk.calc.dn(parameter, dose) \item{dose}{Dose in units compatible with the area under the curve} } \value{ -a number for dose normalized AUC +a number for dose normalized AUC. A result of length one has the +reason in its "exclude" attribute when it is \code{NA} because the dose is +missing, or \code{NaN} because the parameter and the dose are both zero. } \description{ Determine dose normalized NCA parameter diff --git a/man/pk.calc.f.Rd b/man/pk.calc.f.Rd index ae587594..03a9ae38 100644 --- a/man/pk.calc.f.Rd +++ b/man/pk.calc.f.Rd @@ -17,6 +17,10 @@ method 1} \item{auc2}{The AUC from 0 to infinity or 0 to tau administered in route or method 2} } +\value{ +The bioavailability. A result of length one that is \code{NA} because of missing or unusable data +has the reason in its "exclude" attribute, which \code{\link[=pk.nca]{pk.nca()}} reports. +} \description{ Calculate the absolute (or relative) bioavailability } diff --git a/man/pk.calc.mrt.Rd b/man/pk.calc.mrt.Rd index b347d8ec..cac95cda 100644 --- a/man/pk.calc.mrt.Rd +++ b/man/pk.calc.mrt.Rd @@ -10,11 +10,19 @@ multiple-dose data.} \usage{ pk.calc.mrt(auc, aumc) -pk.calc.mrt.iv(auc, aumc, duration.dose, dose, time.dose) +pk.calc.mrt.iv(auc, aumc, duration.dose, dose = NULL, time.dose = NULL) pk.calc.mrt.md(auctau, aumctau, aucinf, tau) -pk.calc.mrt.md.iv(auctau, aumctau, aucinf, tau, duration.dose, dose, time.dose) +pk.calc.mrt.md.iv( + auctau, + aumctau, + aucinf, + tau, + duration.dose, + dose = NULL, + time.dose = NULL +) } \arguments{ \item{auc}{the AUC from 0 to infinity or 0 to tau} @@ -44,7 +52,11 @@ data)} \item{tau}{The dosing interval} } \value{ -the numeric value of the mean residence time +the numeric value of the mean residence time. A result of length one that is \code{NA} because of missing or unusable data +has the reason in its "exclude" attribute, which \code{\link[=pk.nca]{pk.nca()}} reports. + +The mean residence time. A result of length one that is \code{NA} because of missing or unusable data +has the reason in its "exclude" attribute, which \code{\link[=pk.nca]{pk.nca()}} reports. } \description{ Calculate the mean residence time (MRT) for single-dose data or linear diff --git a/man/pk.calc.ptr.Rd b/man/pk.calc.ptr.Rd index 7fa4906b..0784021e 100644 --- a/man/pk.calc.ptr.Rd +++ b/man/pk.calc.ptr.Rd @@ -12,7 +12,8 @@ pk.calc.ptr(cmax, ctrough) \item{ctrough}{The last concentration in an interval} } \value{ -The ratio of cmax to ctrough (if ctrough == 0, NA) +The ratio of cmax to ctrough (if ctrough == 0, NA). A result of length one that is \code{NA} because of missing or unusable data +has the reason in its "exclude" attribute, which \code{\link[=pk.nca]{pk.nca()}} reports. } \description{ Determine the peak-to-trough ratio diff --git a/man/pk.calc.tlag.Rd b/man/pk.calc.tlag.Rd index d41e9773..548555f4 100644 --- a/man/pk.calc.tlag.Rd +++ b/man/pk.calc.tlag.Rd @@ -6,20 +6,28 @@ concentration above the limit of quantification or above the first concentration in the interval)} \usage{ -pk.calc.tlag(conc, time) +pk.calc.tlag(conc, time, check = TRUE) } \arguments{ \item{conc}{Measured concentrations} \item{time}{Time of the measurement of the concentrations} + +\item{check}{Run \code{\link[=assert_conc_time]{assert_conc_time()}}?} } \value{ -The time associated with the first increasing concentration +The time associated with the first increasing concentration, or +\code{NA} with the reason in its "exclude" attribute when concentrations never +increase } \description{ -Determine the observed lag time (time before the first -concentration above the limit of quantification or above the first -concentration in the interval) +Missing concentrations are skipped, as in \code{\link[=pk.calc.cmax]{pk.calc.cmax()}} and the other +simple parameters: the increase is looked for among the measured +concentrations. \code{NA} is returned when no measured concentration increases +(including when every concentration is missing). Because the lag is found +among the measured samples only, a missing sample between the last zero and +the first increase can move it: \code{pk.calc.tlag(c(0, NA, 5, 8), c(0, 1, 2, 4))} +is 0, the time of the last measured zero. } \seealso{ Other NCA time parameters: diff --git a/man/pk.calc.tlast.Rd b/man/pk.calc.tlast.Rd index 0c27dcdb..f37f1bc8 100644 --- a/man/pk.calc.tlast.Rd +++ b/man/pk.calc.tlast.Rd @@ -18,7 +18,9 @@ pk.calc.tfirst(conc, time, check = TRUE) \item{check}{Run \code{\link[=assert_conc_time]{assert_conc_time()}}?} } \value{ -The time of the last observed concentration measurement +The time of the last (\code{pk.calc.tlast()}) or first +(\code{pk.calc.tfirst()}) observed concentration measurement, or \code{NA} with the +reason in its "exclude" attribute when no concentration is above zero } \description{ \code{NA} will be returned if all \code{conc} are \code{NA} or 0. diff --git a/man/pk.calc.tmax.Rd b/man/pk.calc.tmax.Rd index ef9bf083..2af4e0df 100644 --- a/man/pk.calc.tmax.Rd +++ b/man/pk.calc.tmax.Rd @@ -21,7 +21,8 @@ last is considered Tmax/ERTmax.} \item{check}{Run \code{\link[=assert_conc_time]{assert_conc_time()}}?} } \value{ -The time of the maximum concentration +The time of the maximum concentration, or \code{NA} with the reason in +its "exclude" attribute when no concentration is above zero } \description{ Input restrictions are: diff --git a/man/pk.calc.tmin.Rd b/man/pk.calc.tmin.Rd index 47e325f4..11a2dfed 100644 --- a/man/pk.calc.tmin.Rd +++ b/man/pk.calc.tmin.Rd @@ -21,7 +21,8 @@ then the last is considered Tmin.} \item{check}{Run \code{\link[=assert_conc_time]{assert_conc_time()}}?} } \value{ -The time of the minimum concentration +The time of the minimum concentration, or \code{NA} with the reason in +its "exclude" attribute when all concentrations are missing } \description{ Input restrictions are: diff --git a/man/pk.calc.totdose.Rd b/man/pk.calc.totdose.Rd index 76ec4195..25cde15b 100644 --- a/man/pk.calc.totdose.Rd +++ b/man/pk.calc.totdose.Rd @@ -10,7 +10,8 @@ pk.calc.totdose(dose) \item{dose}{the dose administered} } \value{ -The total dose for an interval +The total dose for an interval, or \code{NA} with the reason in its +"exclude" attribute when there is no dose or a dose is missing } \description{ Extract the dose used for calculations diff --git a/man/setDuration.Rd b/man/setDuration.Rd index bfab66b1..3971fab6 100644 --- a/man/setDuration.Rd +++ b/man/setDuration.Rd @@ -22,7 +22,9 @@ in the data to use for the duration.} \item{rate}{(for PKNCAdose objects only) The rate of infusion} -\item{dose}{(for PKNCAdose objects only) The dose amount} +\item{dose}{(for PKNCAdose objects only) The name of the dose column (the +default, from the formula), or the dose amounts (a value, or one per +row); used with \code{rate}} } \value{ The object with duration set diff --git a/tests/testthat/test-class-PKNCAdose.R b/tests/testthat/test-class-PKNCAdose.R index b960942b..7fa4d929 100644 --- a/tests/testthat/test-class-PKNCAdose.R +++ b/tests/testthat/test-class-PKNCAdose.R @@ -584,3 +584,128 @@ test_that("PKNCAdose does not error for excluded, invalid times (#310)", { tmp.dose <- data.frame(time = c(1, 2), dose = c(1, 2)) expect_no_error(PKNCAdose(tmp.dose, formula = dose~time, exclude = NULL)) }) + +test_that("a negative dose amount is an error naming the rows", { + d_dose <- data.frame(id = 1:8, time = 0, dose = c(1, -1, 2, -2, -3, -4, -5, -6)) + expect_error( + PKNCAdose(d_dose, dose ~ time | id), + regexp = "Dose amounts (column 'dose') must not be negative; 6 row(s) of the dose data are: 2, 4, 5, 6, 7, ...", + fixed = TRUE, + class = "pknca_error_dose_negative" + ) + # Exactly five rows are all named, with no "..." + cnd <- + expect_error( + PKNCAdose(data.frame(id = 1:6, time = 0, dose = c(1, -1, -2, -3, -4, -5)), dose ~ time | id), + class = "pknca_error_dose_negative" + ) + expect_equal( + conditionMessage(cnd), + "Dose amounts (column 'dose') must not be negative; 5 row(s) of the dose data are: 2, 3, 4, 5, 6" + ) + # One dose per group for every interval (`dose ~ .`) + expect_error( + PKNCAdose(data.frame(id = 1:2, dose = c(5, -5)), dose ~ . | id), + regexp = "Dose amounts (column 'dose') must not be negative; 1 row(s) of the dose data are: 2", + fixed = TRUE, + class = "pknca_error_dose_negative" + ) + # A units column + expect_error( + PKNCAdose(data.frame(id = 1, time = 0, dose = units::set_units(-5, "mg")), dose ~ time | id), + class = "pknca_error_dose_negative" + ) + # Through PKNCAdata() with a data.frame and formula.dose + expect_error( + PKNCAdata( + data.frame(id = 1, time = 0:2, conc = c(0, 2, 1)), data.frame(id = 1, time = 0, dose = -1), + formula.conc = conc ~ time | id, formula.dose = dose ~ time | id + ), + class = "pknca_error_dose_negative" + ) +}) + +test_that("an excluded negative dose, a zero dose, and a missing dose are allowed", { + d_dose <- data.frame(id = 1:2, time = 0, dose = c(1, -1), excl = c(NA, "bad dose")) + expect_no_error(PKNCAdose(d_dose, dose ~ time | id, exclude = "excl")) + # No dose amount at all + expect_no_error(PKNCAdose(data.frame(id = 1, time = 0), ~ time | id)) + # A dose column that is not numeric is not checked here + expect_no_error(PKNCAdose(data.frame(id = 1:2, time = 0, dose = c("1", "-2")), dose ~ time | id)) + # Zero and missing amounts give what they gave before negative amounts were + # refused: a zero dose divides to Inf, and a missing one gives NA + d_conc <- data.frame(id = 1, time = c(0, 1, 2, 4, 8), conc = c(0, 10, 8, 4, 2)) + intervals <- data.frame(start = 0, end = Inf, cl.last = TRUE, auclast.dn = TRUE, cmax.dn = TRUE, totdose = TRUE) + for (current in list(list(dose = 0, expected = c(cl.last = 0, auclast.dn = Inf, cmax.dn = Inf, totdose = 0)), + list(dose = NA_real_, expected = c(cl.last = NA_real_, auclast.dn = NA_real_, cmax.dn = NA_real_, totdose = NA_real_)))) { + o_data <- + PKNCAdata( + PKNCAconc(d_conc, conc ~ time | id), + PKNCAdose(data.frame(id = 1, time = 0, dose = current$dose), dose ~ time | id), + intervals = intervals + ) + res <- as.data.frame(suppressMessages(pk.nca(o_data))) + value <- stats::setNames(res$PPORRES, res$PPTESTCD) + expect_equal(value[names(current$expected)], current$expected, info = as.character(current$dose)) + } +}) + +test_that("pk.nca refuses a dose made negative after PKNCAdose", { + o_dose <- PKNCAdose(data.frame(id = 1, time = 0, dose = 1), dose ~ time | id) + o_data <- + PKNCAdata( + PKNCAconc(data.frame(id = 1, time = 0:2, conc = c(0, 2, 1)), conc ~ time | id), + dplyr::mutate(o_dose, dose = -dose), + intervals = data.frame(start = 0, end = Inf, cmax.dn = TRUE) + ) + expect_error( + pk.nca(o_data), + regexp = "Dose amounts (column 'dose') must not be negative; 1 row(s) of the dose data are: 1", + fixed = TRUE, + class = "pknca_error_dose_negative" + ) +}) + +# The durations of the doses, from PKNCAdose(rate = ) on `d_dose` +rate_durations <- function(d_dose, formula = dose ~ time | id, rate = "rate") { + o_dose <- PKNCAdose(d_dose, formula, route = "intravascular", rate = rate) + as.data.frame(o_dose)[[o_dose$columns$duration]] +} + +test_that("a rate column gives each dose its own duration, with any number of rows", { + expect_equal(rate_durations(data.frame(id = 1, time = 0, dose = 10, rate = 5)), 2) + expect_equal(rate_durations(data.frame(id = 1:2, time = 0, dose = c(10, 20), rate = c(5, 4))), c(2, 5)) + expect_equal(rate_durations(data.frame(id = 1:3, time = 0, dose = c(10, 20, 30), rate = c(5, 4, 3))), c(2, 5, 10)) + # One rate for every dose + expect_equal(rate_durations(data.frame(id = 1:2, time = 0, dose = c(10, 20)), rate = 2), c(5, 10)) + # A missing rate, a rate of zero, or a missing dose amount gives no valid + # duration + for (d_dose in list( + data.frame(id = 1:2, time = 0, dose = c(10, 20), rate = c(5, NA)), + data.frame(id = 1:2, time = 0, dose = c(10, 20), rate = c(5, 0)), + data.frame(id = 1:2, time = 0, dose = c(10, NA), rate = c(5, 4)) + )) { + expect_error(rate_durations(d_dose), class = "pknca_error_dose_invalid_duration") + } + # Without a dose amount, a rate cannot give a duration + for (n in 1:2) { + expect_error( + rate_durations(data.frame(id = seq_len(n), time = 0, rate = 5), formula = ~ time | id), + regexp = "`rate` needs the dose amount", + class = "pknca_error_rate_without_dose" + ) + } +}) + +# The durations of the doses of a PKNCAdose object +dose_test_duration <- function(object) { + as.data.frame(object)[[object$columns$duration]] +} + +test_that("setDuration() with a rate takes the dose column by default, or the dose amounts", { + o_dose <- PKNCAdose(data.frame(id = 1:2, time = 0, dose = c(10, 20)), dose ~ time | id, route = "intravascular") + expect_equal(dose_test_duration(setDuration(o_dose, rate = 5)), c(2, 4)) + expect_equal(dose_test_duration(setDuration(o_dose, rate = 5, dose = c(20, 40))), c(4, 8)) + # A missing dose amount gives no duration + expect_error(setDuration(o_dose, rate = 5, dose = NA), class = "pknca_error_rate_without_dose") +}) diff --git a/tests/testthat/test-class-PKNCAresults.R b/tests/testthat/test-class-PKNCAresults.R index 74b78803..7a42c5e6 100644 --- a/tests/testthat/test-class-PKNCAresults.R +++ b/tests/testthat/test-class-PKNCAresults.R @@ -165,22 +165,29 @@ test_that("PKNCAresults has exclude, when applicable", { ) ] ), - "Too few points for half-life calculation (min.hl.points=3 with only 0 points)", + # half.life is calculated from tmax and tlast, which give their own reason + "No concentration is above zero; Too few points for half-life calculation (min.hl.points=3 with only 0 points)", info="exclusions are propagated to results" ) expect_equal( - unique( - o_result_df$exclude[ - !(o_result_df$ID == 2 & - o_result_df$PPTESTCD %in% - c("lambda.z", "r.squared", "adj.r.squared", "lambda.z.corrxy", "lambda.z.time.first", - "lambda.z.time.last", "lambda.z.n.points", "clast.pred", "half.life", "span.ratio") - ) - ] - ), + unique(o_result_df$exclude[o_result_df$ID == 1]), NA_character_, info="exclusions are propagated to results only when applicable" ) + # The other parameters of the all-zero profile say why they are NA + id2_other <- + o_result_df[ + o_result_df$ID == 2 & + o_result_df$PPTESTCD %in% c("tmax", "tlast", "tlag", "aucpext.inf.obs", "cl.inf.obs"), + ] + expect_equal( + id2_other$exclude[match(c("tmax", "tlast", "tlag", "aucpext.inf.obs", "cl.inf.obs"), id2_other$PPTESTCD)], + c( + "No concentration is above zero", "No concentration is above zero", + "Concentrations never increase", "AUCinf is zero", "AUC is zero or negative" + ) + ) + expect_true(all(is.na(id2_other$PPORRES))) }) test_that("ptr works as a parameter", { diff --git a/tests/testthat/test-impute.R b/tests/testthat/test-impute.R index 167bf00d..9ab15e68 100644 --- a/tests/testthat/test-impute.R +++ b/tests/testthat/test-impute.R @@ -1601,7 +1601,13 @@ test_that("a numeric conc.na is the value of a missing start concentration, so t expect_equal(unname(res$value[c("count.conc", "cstart", "aucabove.predose.all")]), c(8, 0, 1836.33993274182), info = info) } else { expect_equal(unname(res$value[c("count.conc", "cstart", "aucabove.predose.all")]), c(7, NA, NA), info = info) - expect_equal(unname(res$exclude["aucabove.predose.all"]), "Missing concentration to be above", info = info) + # The predose concentration it is above is cstart, which is missing; + # its reason comes first, as for any result calculated from it + expect_equal( + unname(res$exclude["aucabove.predose.all"]), + "No non-missing concentration at the start of the interval; Missing concentration to be above", + info = info + ) } } } diff --git a/tests/testthat/test-pk.calc.all.R b/tests/testthat/test-pk.calc.all.R index fe8e0897..c13c10ac 100644 --- a/tests/testthat/test-pk.calc.all.R +++ b/tests/testthat/test-pk.calc.all.R @@ -353,7 +353,8 @@ test_that("Calculations when no dose info is given", { "AUC: lin up/log down", rep("", 2) ), - exclude=NA_character_ + # cl.last cannot be calculated without a dose, and says so + exclude=rep(c(NA_character_, NA_character_, "Dose is missing"), 2) ) ) }) diff --git a/tests/testthat/test-pk.calc.dn.R b/tests/testthat/test-pk.calc.dn.R index 4d2175fb..cc9bd72c 100644 --- a/tests/testthat/test-pk.calc.dn.R +++ b/tests/testthat/test-pk.calc.dn.R @@ -2,7 +2,7 @@ test_that("pk.calc.dn", { # Ensure correct calculation expect_equal(pk.calc.dn(1, 5), 0.2) expect_equal(pk.calc.dn(NA, 5), NA_real_) - expect_equal(pk.calc.dn(1, NA), NA_real_) + expect_equal(pk.calc.dn(1, NA), structure(NA_real_, exclude = "Dose is missing")) }) test_that("pk.calc.cmax", { diff --git a/tests/testthat/test-pk.calc.simple.R b/tests/testthat/test-pk.calc.simple.R index fcd045e3..684c871b 100644 --- a/tests/testthat/test-pk.calc.simple.R +++ b/tests/testthat/test-pk.calc.simple.R @@ -16,9 +16,9 @@ test_that("adj.r.squared", { test_that("pk.calc.cmax", { # Confirm that all NAs give NA as an output expect_warning(v1 <- pk.calc.cmax(NA)) - expect_equal(v1, NA) + expect_equal(v1, structure(NA, exclude = "All concentrations are missing")) expect_warning(v2 <- pk.calc.cmax(c(NA, NA))) - expect_equal(v2, NA) + expect_equal(v2, structure(NA, exclude = "All concentrations are missing")) # Confirm that no NAs give the max value expect_equal(pk.calc.cmax(c(1, 2)), 2) @@ -32,15 +32,15 @@ test_that("pk.calc.cmax", { # Confirm that no data gives NA with a warning expect_warning(v3 <- pk.calc.cmax(numeric())) - expect_equal(v3, NA) + expect_equal(v3, structure(NA, exclude = "All concentrations are missing")) }) test_that("pk.calc.cmin", { # Confirm that all NAs give NA as an output expect_warning(v1 <- pk.calc.cmin(NA)) - expect_equal(v1, NA) + expect_equal(v1, structure(NA, exclude = "All concentrations are missing")) expect_warning(v2 <- pk.calc.cmin(c(NA, NA))) - expect_equal(v2, NA) + expect_equal(v2, structure(NA, exclude = "All concentrations are missing")) # Confirm that no NAs give the min value expect_equal(pk.calc.cmin(c(1, 2)), 1) @@ -54,7 +54,7 @@ test_that("pk.calc.cmin", { # Confirm that no data gives NA with a warning expect_warning(v3 <- pk.calc.cmin(numeric())) - expect_equal(v3, NA) + expect_equal(v3, structure(NA, exclude = "All concentrations are missing")) }) test_that("pk.calc.tmax", { @@ -64,7 +64,7 @@ test_that("pk.calc.tmax", { class = "pknca_warning_no_concentration"), class = "pknca_warning_no_time" ) - expect_equal(v1, NA) + expect_equal(v1, structure(NA, exclude = "All concentrations are missing")) # Either concentration or time is missing, give an error expect_error( @@ -94,7 +94,7 @@ test_that("pk.calc.tmin", { class = "pknca_warning_no_concentration"), class = "pknca_warning_no_time" ) - expect_equal(v1, NA) + expect_equal(v1, structure(NA, exclude = "All concentrations are missing")) # Either concentration or time is missing, give an error expect_error( @@ -108,7 +108,7 @@ test_that("pk.calc.tmin", { # All NA concentrations give NA expect_warning( - expect_equal(pk.calc.tmin(c(NA, NA), c(0, 1), first.tmin=TRUE), NA), + expect_equal(pk.calc.tmin(c(NA, NA), c(0, 1), first.tmin=TRUE), structure(NA, exclude = "All concentrations are missing")), class = "pknca_warning_all_concentration_missing" ) @@ -138,7 +138,7 @@ test_that("pk.calc.tlast", { expect_equal(pk.calc.tlast(c(1, 2), c(0, 1)), 1) expect_equal(pk.calc.tlast(c(0, 0), c(0, 1)), - NA) + structure(NA, exclude = "No concentration is above zero")) expect_equal(pk.calc.tlast(c(0, 1), c(0, 1)), 1) expect_equal(pk.calc.tlast(c(1, 0), c(0, 1)), @@ -188,7 +188,7 @@ test_that("pk.calc.clast.obs", { v1 <- pk.calc.clast.obs(c1, t1), class = "pknca_warning_all_concentration_missing" ) - expect_equal(v1, NA_real_) + expect_equal(v1, structure(NA_real_, exclude = "All concentrations are missing")) c1 <- rep(0, 4) t1 <- c(0, 1, 2, 3) @@ -260,19 +260,19 @@ test_that("pk.calc.cl", { test_that("pk.calc.f", { expect_equal(pk.calc.f(1, 1, 1, 2), 2, info="Standard bioavailability calculation") - expect_equal(pk.calc.f(NA, 1, 1, 1), NA_real_, + expect_equal(pk.calc.f(NA, 1, 1, 1), structure(NA_real_, exclude = "Dose is missing"), info="NA handling per parameter in bioavailability (1)") expect_equal(pk.calc.f(1, NA, 1, 1), NA_real_, info="NA handling per parameter in bioavailability (2)") - expect_equal(pk.calc.f(1, 1, NA, 1), NA_real_, + expect_equal(pk.calc.f(1, 1, NA, 1), structure(NA_real_, exclude = "Dose is missing"), info="NA handling per parameter in bioavailability (3)") expect_equal(pk.calc.f(1, 1, 1, NA), NA_real_, info="NA handling per parameter in bioavailability (4)") - expect_equal(pk.calc.f(0, 1, 1, 1), NA_real_, + expect_equal(pk.calc.f(0, 1, 1, 1), structure(NA_real_, exclude = "Dose is zero or negative"), info="Zero handling per parameter in bioavailability (1)") - expect_equal(pk.calc.f(1, 0, 1, 1), NA_real_, + expect_equal(pk.calc.f(1, 0, 1, 1), structure(NA_real_, exclude = "AUC is zero or negative"), info="Zero handling per parameter in bioavailability (2)") - expect_equal(pk.calc.f(1, 1, 0, 1), NA_real_, + expect_equal(pk.calc.f(1, 1, 0, 1), structure(NA_real_, exclude = "Dose is zero or negative"), info="Zero handling per parameter in bioavailability (3)") expect_equal(pk.calc.f(1, 1, 1, 0), 0, info="Zero handling per parameter in bioavailability (4)") @@ -289,7 +289,7 @@ test_that("pk.calc.aucpext", { class = "pknca_warning_aucpext_aucinf_le_auclast"), class = "pknca_warning_aucpext_aucinf_auclast_positive" ) - expect_equal(v2, NA_real_, + expect_equal(v2, structure(NA_real_, exclude = "AUCinf is zero"), info="aucinf<=0 gives NA_real_ (not infinity)") expect_equal(pk.calc.aucpext(NA, NA), NA_real_, @@ -328,10 +328,10 @@ test_that("pk.calc.mrt.iv", { 2, info="MRT.iv is calculated correctly when duration is 0") expect_equal(pk.calc.mrt.iv(auc=1, aumc=2, duration.dose=NA), - NA_real_, + structure(NA_real_, exclude = "A dose time or duration is missing"), info="MRT.iv is calculated correctly when duration is missing") expect_equal(pk.calc.mrt.iv(auc=0, aumc=2, duration.dose=NA), - NA_real_, + structure(NA_real_, exclude = "A dose time or duration is missing; AUC is zero or negative"), info="MRT.iv is calculated correctly when auc is zero") }) @@ -378,24 +378,24 @@ test_that("pk.calc.mrt.iv uses the dose-weighted mean input time for several dos test_that("pk.calc.mrt.iv returns NA when the mean input time is unknown for several doses", { expect_equal( pk.calc.mrt.iv(auc=1, aumc=6, duration.dose=c(1, NA), dose=c(10, 10), time.dose=c(0, 1)), - NA_real_ + structure(NA_real_, exclude = "A dose time or duration is missing") ) expect_equal( pk.calc.mrt.iv(auc=1, aumc=6, duration.dose=c(1, 2), dose=c(10, NA), time.dose=c(0, 1)), - NA_real_ + structure(NA_real_, exclude = "A dose amount is missing, or the doses sum to zero") ) expect_equal( pk.calc.mrt.iv(auc=1, aumc=6, duration.dose=c(1, 2), dose=c(0, 0), time.dose=c(0, 1)), - NA_real_ + structure(NA_real_, exclude = "A dose amount is missing, or the doses sum to zero") ) expect_equal( pk.calc.mrt.iv(auc=1, aumc=6, duration.dose=c(1, 2), dose=c(10, 10), time.dose=c(0, NA)), - NA_real_, + structure(NA_real_, exclude = "A dose time or duration is missing"), info="a missing dose time with several doses gives NA" ) expect_equal( pk.calc.mrt.iv(auc=0, aumc=6, duration.dose=c(1, 2), dose=c(10, 10), time.dose=c(0, 1)), - NA_real_ + structure(NA_real_, exclude = "AUC is zero or negative") ) # The weights are not guessed: with one dose missing, the result is NA rather than # the equal-weight value @@ -410,27 +410,27 @@ test_that("pk.calc.mrt.iv returns NA when the mean input time is unknown for sev 6 - (3 + 2/2) ) # A single dose with a missing time or duration is NA - expect_equal(pk.calc.mrt.iv(auc=1, aumc=6, duration.dose=2, dose=10, time.dose=NA_real_), NA_real_) - expect_equal(pk.calc.mrt.iv(auc=1, aumc=6, duration.dose=NA_real_, dose=10, time.dose=0), NA_real_) + expect_equal(pk.calc.mrt.iv(auc=1, aumc=6, duration.dose=2, dose=10, time.dose=NA_real_), structure(NA_real_, exclude = "A dose time or duration is missing")) + expect_equal(pk.calc.mrt.iv(auc=1, aumc=6, duration.dose=NA_real_, dose=10, time.dose=0), structure(NA_real_, exclude = "A dose time or duration is missing")) }) test_that("pk.calc.mrt.md.iv returns NA when the mean input time is unknown for several doses", { args <- list(auctau=10, aumctau=40, aucinf=12, tau=12) expect_equal( do.call(pk.calc.mrt.md.iv, c(args, list(duration.dose=c(1, NA), dose=c(10, 10), time.dose=c(0, 1)))), - NA_real_ + structure(NA_real_, exclude = "A dose time or duration is missing") ) expect_equal( do.call(pk.calc.mrt.md.iv, c(args, list(duration.dose=c(1, 2), dose=c(10, NA), time.dose=c(0, 1)))), - NA_real_ + structure(NA_real_, exclude = "A dose amount is missing, or the doses sum to zero") ) expect_equal( do.call(pk.calc.mrt.md.iv, c(args, list(duration.dose=c(1, 2), dose=c(0, 0), time.dose=c(0, 1)))), - NA_real_ + structure(NA_real_, exclude = "A dose amount is missing, or the doses sum to zero") ) expect_equal( do.call(pk.calc.mrt.md.iv, c(args, list(duration.dose=c(1, 2), dose=c(10, 10), time.dose=c(0, NA)))), - NA_real_ + structure(NA_real_, exclude = "A dose time or duration is missing") ) # A single dose row does not use dose expect_equal( @@ -494,7 +494,7 @@ test_that("pk.calc.mrt.iv requires one result and consistent dose inputs (#694)" test_that("pk.calc.mrt.md", { expect_equal(pk.calc.mrt.md(1, 2, 1.5, 24), 2 + 24*0.5) - expect_equal(pk.calc.mrt.md(0, 2, 1.5, 24), NA_real_, + expect_equal(pk.calc.mrt.md(0, 2, 1.5, 24), structure(NA_real_, exclude = "AUC is zero or negative"), info="auctau <= 0 becomes NA (not Inf)") }) @@ -503,9 +503,9 @@ test_that("pk.calc.mrt.md.iv", { expect_equal(pk.calc.mrt.md.iv(1, 2, 1.5, 24, duration.dose=0), pk.calc.mrt.md(1, 2, 1.5, 24), info="a zero infusion duration is the same as the non-IV form") - expect_equal(pk.calc.mrt.md.iv(0, 2, 1.5, 24, duration.dose=4), NA_real_, + expect_equal(pk.calc.mrt.md.iv(0, 2, 1.5, 24, duration.dose=4), structure(NA_real_, exclude = "AUC is zero or negative"), info="auctau <= 0 becomes NA (not Inf)") - expect_equal(pk.calc.mrt.md.iv(1, 2, 1.5, 24, duration.dose=NA), NA_real_, + expect_equal(pk.calc.mrt.md.iv(1, 2, 1.5, 24, duration.dose=NA), structure(NA_real_, exclude = "A dose time or duration is missing"), info="a missing infusion duration becomes NA") }) @@ -597,7 +597,7 @@ test_that("pk.calc.vss and its wrappers", { test_that("pk.calc.cav", { expect_equal(pk.calc.cav(2, 0, 1), 2) expect_equal(pk.calc.cav(NA, 0, 1), NA_real_) - expect_equal(pk.calc.cav(2, 1, 1), NA_real_, + expect_equal(pk.calc.cav(2, 1, 1), structure(NA_real_, exclude = "The interval has zero duration"), info="If end == start, return NA_real_") }) @@ -677,7 +677,7 @@ test_that("pk.calc.ctrough", { info="Found and it's the first time") expect_equal(pk.calc.ctrough(1:5, 0:4, 1), 2, info="Found and it's not the first time") - expect_equal(pk.calc.ctrough(1:5, 0:4, 1.5), NA_real_, + expect_equal(pk.calc.ctrough(1:5, 0:4, 1.5), structure(NA_real_, exclude = "No non-missing concentration at the end of the interval"), info="Not found") expect_error( pk.calc.ctrough(1:5, c(0, 0:3), 0), @@ -688,7 +688,7 @@ test_that("pk.calc.ctrough", { test_that("pk.calc.ptr", { expect_equal(pk.calc.ptr(cmax=2, ctrough=1), 2, info="Confirm that the ratio goes the right way") - expect_equal(pk.calc.ptr(2, 0), NA_real_, + expect_equal(pk.calc.ptr(2, 0), structure(NA_real_, exclude = "Ctrough is zero"), info="Division by zero returns NA") }) @@ -697,26 +697,94 @@ test_that("pk.calc.tlag", { info="find the first point") expect_equal(pk.calc.tlag(c(0, 0, 0, 0, 1), 0:4), 3, info="find the next to last point") - expect_equal(pk.calc.tlag(c(0, 0, 0, 0, 0), 0:4), NA_real_, + expect_equal(pk.calc.tlag(c(0, 0, 0, 0, 0), 0:4), structure(NA_real_, exclude = "Concentrations never increase"), info="No increase gives NA") - expect_equal(pk.calc.tlag(5:1, 0:4), NA_real_, + expect_equal(pk.calc.tlag(5:1, 0:4), structure(NA_real_, exclude = "Concentrations never increase"), info="No increase gives NA") }) +test_that("pk.calc.tlag skips missing concentrations, as the other simple parameters do", { + # At the start: the first increase among the measured values is 40 to 80 + expect_equal(pk.calc.tlag(c(NA, 40, 80, 10), c(0, 2, 4, 8)), 2) + # At the start with no increase among the measured values + expect_equal(pk.calc.tlag(c(NA, 40, 20, 10), c(0, 2, 4, 8)), structure(NA_real_, exclude = "Concentrations never increase")) + # In the middle: 10 and 5 are not an increase, 5 to 8 is + expect_equal(pk.calc.tlag(c(10, NA, 5, 8), 0:3), 2) + # At the end + expect_equal(pk.calc.tlag(c(0, 5, 10, NA), 0:3), 0) + # Between the last zero and the first increase: the lag is the time of the + # last measured zero + expect_equal(pk.calc.tlag(c(0, NA, 5, 8), c(0, 1, 2, 4)), 0) + # Everywhere, with a warning from the check, and without it + expect_warning( + value_all_missing <- pk.calc.tlag(c(NA_real_, NA_real_, NA_real_), 0:2), + class = "pknca_warning_all_concentration_missing" + ) + expect_equal(value_all_missing, structure(NA_real_, exclude = "All concentrations are missing")) + expect_equal(pk.calc.tlag(c(NA_real_, NA_real_, NA_real_), 0:2, check = FALSE), structure(NA_real_, exclude = "All concentrations are missing")) + # check = FALSE skips the assertions, not the missing values + expect_equal(pk.calc.tlag(c(10, NA, 5, 8), 0:3, check = FALSE), 2) +}) + +# tlag from 0 to 8 of one profile with the conc.na option `conc_na` +tlag_test_nca <- function(conc, conc_na) { + o_data <- + PKNCAdata( + PKNCAconc(data.frame(id = 1, time = c(0, 2, 4, 8), conc = conc), conc ~ time | id), + PKNCAdose(data.frame(id = 1, time = 0, dose = 1), dose ~ time | id), + intervals = data.frame(start = 0, end = 8, tlag = TRUE), + options = list(conc.na = conc_na) + ) + res <- as.data.frame(suppressWarnings(suppressMessages(pk.nca(o_data)))) + res$PPORRES[res$PPTESTCD == "tlag"] +} + +test_that("tlag in pk.nca with missing concentrations", { + # The missing value is skipped at either conc.na, as for cmax + for (conc_na in list("drop", 0)) { + expect_equal(tlag_test_nca(c(NA, 40, 80, 10), conc_na), 2, info = as.character(conc_na)) + # No increase among the measured values: NA, where the interval stopped + # with "Please report a bug" + expect_equal(tlag_test_nca(c(NA, 40, 20, 10), conc_na), NA_real_, info = as.character(conc_na)) + } + # Every concentration missing: NA, where the interval stopped with "Please + # report a bug" (the data checks warn that every concentration is missing) + expect_warning( + value_all_missing <- tlag_test_nca(rep(NA_real_, 4), "drop"), + class = "pknca_warning_all_concentration_missing" + ) + expect_equal(value_all_missing, NA_real_) +}) + test_that("pk.calc.deg.fluc", { expect_equal(pk.calc.deg.fluc(cmax=100, cmin=10, cav=45), 200, info="Degree of fluctuation math works") - expect_equal(pk.calc.deg.fluc(cmax=100, cmin=10, cav=0), NA_real_, + expect_equal(pk.calc.deg.fluc(cmax=100, cmin=10, cav=0), structure(NA_real_, exclude = "Cav is zero"), info="Degree of fluctuation returns NA when cav=0") }) test_that("pk.calc.swing", { + # A missing cmin or cmax gives NA_real_, not an error, and not the infinity + # of a zero cmin + expect_identical(pk.calc.swing(10, NA), NA_real_) + expect_identical(pk.calc.swing(NA, 2), NA_real_) + expect_identical(pk.calc.swing(NA, 0), NA_real_) + expect_identical(pk.calc.swing(NA, NA), NA_real_) + expect_identical(pk.calc.swing(10, NA_integer_), NA_real_) expect_equal(pk.calc.swing(100, 10), 900, info="Swing math works") expect_equal(pk.calc.swing(100, 0), Inf, info="Swing handle Ctrough=0") expect_equal(pk.calc.swing(100, -1), Inf, info="Swing handle Ctrough<0") + expect_identical(pk.calc.swing(0, 0), Inf) + # One value for each pair, as the released version gave for a vector of cmax + expect_identical(pk.calc.swing(cmax = c(10, 20), cmin = 1), c(900, 1900)) + expect_identical(pk.calc.swing(cmax = numeric(0), cmin = 1), numeric(0)) + expect_identical( + pk.calc.swing(cmax = c(10, 20, NA, 5, 5, 0), cmin = c(1, 4, 1, NA, 0, 0)), + c(900, 400, NA, NA, Inf, Inf) + ) }) test_that("pk.calc.ceoi", { @@ -724,10 +792,10 @@ test_that("pk.calc.ceoi", { 1, info="Ceoi returns the concentration at the end of the dosing duration") expect_equal(pk.calc.ceoi(conc=0:5, time=0:5, duration.dose=1.5), - NA_real_, + structure(NA_real_, exclude = "No non-missing concentration at the end of the infusion"), info="Ceoi returns NA if there is no measurement at the end of the dosing duration") expect_equal(pk.calc.ceoi(conc=0:5, time=0:5, duration.dose=NA), - NA_real_, + structure(NA_real_, exclude = "Dose duration is missing (or there is no dose in the interval)"), info="Ceoi returns NA if there is no dosing duration") }) @@ -793,7 +861,7 @@ test_that("pk.calc.totdose", { test_that("pk.calc.cstart", { expect_equal(pk.calc.cstart(1:5, 0:4, 0), 1) expect_equal(pk.calc.cstart(1:5, 0:4, 1), 2) - expect_equal(pk.calc.cstart(1:5, 0:4, 1.5), NA_real_) + expect_equal(pk.calc.cstart(1:5, 0:4, 1.5), structure(NA_real_, exclude = "No non-missing concentration at the start of the interval")) expect_error( pk.calc.cstart(1:5, c(0, 0:3), 0), regexp = "Assertion on 'time' failed: Contains duplicated values, position 2." @@ -816,10 +884,11 @@ test_that("pk.calc.aucabove rejects non-finite conc_above", { }) test_that("zero-length input gives NA rather than zero (issue 601)", { - expect_equal(pk.calc.cl(dose = numeric(0), auc = 10), NA_real_) - expect_equal(pk.calc.cl(dose = NULL, auc = 10), NA_real_) - expect_equal(pk.calc.totdose(dose = numeric(0)), NA_real_) - expect_equal(pk.calc.totdose(dose = NULL), NA_real_) + no_dose <- structure(NA_real_, exclude = "No dose in the interval") + expect_equal(pk.calc.cl(dose = numeric(0), auc = 10), no_dose) + expect_equal(pk.calc.cl(dose = NULL, auc = 10), no_dose) + expect_equal(pk.calc.totdose(dose = numeric(0)), no_dose) + expect_equal(pk.calc.totdose(dose = NULL), no_dose) # Unchanged for real input, including multiple doses in one interval expect_equal(pk.calc.cl(dose = 100, auc = 10), 10) expect_equal(pk.calc.cl(dose = c(50, 50), auc = 10), 10) @@ -864,3 +933,256 @@ test_that("count.conc and count.conc.measured count imputed concentrations by va # by both even though it was not measured at that time expect_equal(counts("start_cmin"), c(conc = 7, measured = 7)) }) + +test_that("swing in pk.nca() with every concentration missing is NA", { + # The data checks warn that every concentration is missing + res <- + suppressWarnings( + as.data.frame(pk.nca(PKNCAdata( + PKNCAconc(data.frame(id = 1, time = c(0, 2, 4), conc = NA_real_), conc ~ time | id), + PKNCAdose(data.frame(id = 1, time = 0, dose = 1), dose ~ time | id), + intervals = data.frame(start = 0, end = 4, swing = TRUE) + ))), + classes = "pknca_warning_all_concentration_missing" + ) + expect_equal(res$PPORRES[res$PPTESTCD == "swing"], NA_real_) +}) + +# The tlag or swing results of pk.nca() for concentration data with the columns +# id, time, and conc, on one interval from 0 to `end`, in the order of id +tlag_swing_results <- function(d_conc, parameter, conc.na, end) { + d_interval <- data.frame(start = 0, end = end) + d_interval[[parameter]] <- TRUE + o_data <- + PKNCAdata( + PKNCAconc(d_conc, conc ~ time | id), + intervals = d_interval, + options = list(conc.na = conc.na) + ) + res <- + suppressWarnings( + as.data.frame(pk.nca(o_data)), + classes = "pknca_warning_all_concentration_missing" + ) + res <- res[res$PPTESTCD == parameter, ] + res$PPORRES[order(res$id)] +} + +test_that("a profile with missing concentrations does not stop tlag or swing for the other profiles (#723)", { + # The profile with missing concentrations gets NA, and the other subject's + # result is calculated, at any conc.na. + + # tlag: a falling profile, with one sample missing for the second subject + d_falling <- + data.frame( + id = rep(1:2, each = 5), + time = rep(c(0.25, 1, 2, 4, 8), 2), + conc = c(10, 8, 6, 4, 2, 10, 8, NA, 4, 2) + ) + expect_identical(tlag_swing_results(d_falling, "tlag", conc.na = "drop", end = Inf), c(NA_real_, NA_real_)) + expect_identical(tlag_swing_results(d_falling, "tlag", conc.na = 0, end = Inf), c(NA_real_, NA_real_)) + expect_identical(pk.calc.tlag(conc = c(10, 8, NA, 4, 2), time = c(0.25, 1, 2, 4, 8)), structure(NA_real_, exclude = "Concentrations never increase")) + + # tlag: no measurable concentration and one missing sample for the second + # subject + d_none_measurable <- + data.frame( + id = rep(1:2, each = 4), + time = rep(c(0, 1, 2, 4), 2), + conc = c(0, 5, 3, 1, 0, 0, NA, 0) + ) + expect_identical(tlag_swing_results(d_none_measurable, "tlag", conc.na = "drop", end = Inf), c(0, NA_real_)) + + # swing: every sample of the second subject's interval is missing + d_all_missing <- + data.frame( + id = rep(1:2, each = 4), + time = rep(c(0, 1, 2, 4), 2), + conc = c(1, 5, 3, 1, NA, NA, NA, NA) + ) + expect_identical(tlag_swing_results(d_all_missing, "swing", conc.na = "drop", end = 4), c(400, NA_real_)) + expect_identical(tlag_swing_results(d_all_missing, "swing", conc.na = 0, end = 4), c(400, NA_real_)) +}) + +# Results of `parameters` from pk.nca() for one subject, dosed at time 0, over +# the interval 0 to 24. +simple_reason_results <- function(conc, parameters, dose = 100, duration = 0, + route = "extravascular", conc.na = "drop", + time = c(0, 1, 2, 4, 8, 24)) { + d_interval <- data.frame(start = 0, end = 24) + for (current_parameter in parameters) { + d_interval[[current_parameter]] <- TRUE + } + # The profiles are degenerate on purpose, so the warnings they give are + # expected + res <- + suppressWarnings( + { + o_data <- + PKNCAdata( + PKNCAconc(data.frame(id = 1, time = time, conc = conc), conc ~ time | id), + PKNCAdose( + data.frame(id = 1, time = 0, dose = dose, duration = duration), + dose ~ time | id, route = route, duration = "duration" + ), + intervals = d_interval, + options = list(conc.na = conc.na) + ) + as.data.frame(pk.nca(o_data)) + }, + classes = c( + "pknca_warning_all_concentration_missing", + "pknca_warning_deprecated_parameter", + "pknca_warning_interval_inconsistent_parameters", + "pknca_warning_auc_before_first", + "pknca_warning_tau_undetermined", + "pknca_warning_halflife_too_few_points", + "pknca_warning_aucpext_aucinf_le_auclast", + "pknca_warning_aucpext_aucinf_auclast_positive" + ) + ) + as.data.frame(res[res$PPTESTCD %in% parameters, c("PPTESTCD", "PPORRES", "exclude")]) +} + +test_that("a simple parameter that is NA because of missing or unusable data says why in pk.nca()", { + all_missing <- + simple_reason_results( + conc = rep(NA_real_, 6), + parameters = c("cmax", "cmin", "tmax", "tmin", "tlast", "tfirst", "clast.obs", "tlag", "swing", "cmax.dn") + ) + expect_true(all(is.na(all_missing$PPORRES))) + expect_equal(unique(all_missing$exclude), "All concentrations are missing") + + all_zero <- + simple_reason_results( + conc = rep(0, 6), + parameters = c("tmax", "tlast", "tfirst", "tlag", "cl.last", "mrt.last", "ptr", "deg.fluc", "aucpext.inf.obs", "mrt.md.inf.obs") + ) + expect_true(all(is.na(all_zero$PPORRES))) + expect_equal( + all_zero$exclude[match(c("tmax", "tlast", "tfirst", "tlag", "cl.last", "mrt.last", "ptr", "deg.fluc", "aucpext.inf.obs", "mrt.md.inf.obs"), all_zero$PPTESTCD)], + c( + rep("No concentration is above zero", 3), + "Concentrations never increase", + "AUC is zero or negative", "AUC is zero or negative", + "Ctrough is zero", + "Cav is zero", + "AUCinf is zero", + "The dosing interval (tau) is not known; AUC is zero or negative" + ) + ) + + missing_ends <- + simple_reason_results(conc = c(NA, 5, 8, 6, 3, NA), parameters = c("cstart", "ctrough", "ptr")) + expect_true(all(is.na(missing_ends$PPORRES))) + expect_equal( + missing_ends$exclude[match(c("cstart", "ctrough", "ptr"), missing_ends$PPTESTCD)], + c( + "No non-missing concentration at the start of the interval", + "No non-missing concentration at the end of the interval", + "No non-missing concentration at the end of the interval" + ) + ) + + dose_missing <- + simple_reason_results(conc = c(0, 5, 8, 6, 3, 1), parameters = c("cl.last", "cmax.dn", "totdose", "vss.last"), dose = NA) + expect_true(all(is.na(dose_missing$PPORRES))) + expect_equal(unique(dose_missing$exclude), "Dose is missing") + + infusion <- + simple_reason_results( + conc = c(0, 5, 8, 6, 3, 1), parameters = "ceoi", duration = 1.5, route = "intravascular" + ) + expect_equal(infusion$PPORRES, NA_real_) + expect_equal(infusion$exclude, "No non-missing concentration at the end of the infusion") +}) + +test_that("every registered parameter gives a reason with each NA in pk.nca()", { + all_intervals <- get.interval.cols() + parameters <- + names(all_intervals)[vapply(all_intervals, function(x) !is.null(x$FUN) && !is.na(x$FUN), logical(1))] + # Parameters these fixtures cannot calculate: they need a urine collection + # (volume), a reference interval, or a concentration threshold + not_exercised <- c( + "ae", "fe", "volpk", "ertlst", "ermax", "ertmax", "erint", "erlst", + grep("^clr[.]", parameters, value = TRUE), + grep("^(f|ratio)[.]", parameters, value = TRUE), + "time.above", "time_above" + ) + expect_true(all(not_exercised %in% parameters)) + parameters <- setdiff(parameters, not_exercised) + # Known to give NA without a reason in these fixtures, from the IV + # back-extrapolation functions (and cl.iv.* calculated from them). When they + # gain reasons, this test fails until they are removed from the list. + known_without_reason <- c( + "aucivinf.obs", "aucivinf.pred", "aumcivinf.obs", "aumcivinf.pred", + "cl.iv.inf.obs", "cl.iv.inf.pred", "cl.iv.obs", "cl.iv.pred", + "aucivpbext.last", "aucivpbext.all", "aucivpbext.inf.obs", "aucivpbext.inf.pred", + "aucivpbext.int.last", "aucivpbext.int.all", + "aucivpbextlast", "aucivpbextall", "aucivpbextinf.obs", "aucivpbextinf.pred", + "aucivpbextint.last", "aucivpbextint.all" + ) + fixtures <- list( + all_missing = list(conc = rep(NA_real_, 6), dose = 100), + all_zero = list(conc = rep(0, 6), dose = 100), + missing_ends = list(conc = c(NA, 5, 8, 6, 3, NA), dose = 100), + one_measured = list(conc = c(0, 0, 5, 0, 0, 0), dose = 100), + dose_zero = list(conc = c(0, 5, 8, 6, 3, 1), dose = 0), + dose_missing = list(conc = c(0, 5, 8, 6, 3, 1), dose = NA) + ) + found_without_reason <- character(0) + for (current_route in c("extravascular", "intravascular")) { + for (current_fixture in names(fixtures)) { + for (current_conc_na in list("drop", 0)) { + current_info <- paste(current_route, current_fixture, current_conc_na) + res <- + simple_reason_results( + conc = fixtures[[current_fixture]]$conc, parameters = parameters, + dose = fixtures[[current_fixture]]$dose, + duration = if (current_route == "intravascular") 1.5 else 0, + route = current_route, conc.na = current_conc_na + ) + # The fixture calculates and has NA results to check + expect_gt(nrow(res), 0) + expect_true(any(is.na(res$PPORRES)), info = current_info) + no_reason <- unique(res$PPTESTCD[is.na(res$PPORRES) & is.na(res$exclude)]) + expect_equal(setdiff(no_reason, known_without_reason), character(0), info = current_info) + found_without_reason <- union(found_without_reason, no_reason) + } + } + } + expect_setequal(found_without_reason, known_without_reason) +}) + +test_that("a reason is given with a single value only, never beside a valid value", { + # One value: the reason is given + # A zero aucinf warns as well + aucpext_classes <- c("pknca_warning_aucpext_aucinf_le_auclast", "pknca_warning_aucpext_aucinf_auclast_positive") + expect_equal( + suppressWarnings(pk.calc.aucpext(1, 0), classes = aucpext_classes), + structure(NA_real_, exclude = "AUCinf is zero") + ) + expect_equal(pk.calc.cav(10, 5, 5), structure(NA_real_, exclude = "The interval has zero duration")) + expect_equal(pk.calc.dn(1, NA), structure(NA_real_, exclude = "Dose is missing")) + # Several values with one NA among them: no attribute + expect_equal( + suppressWarnings(pk.calc.aucpext(c(1, 1), c(2, 0)), classes = aucpext_classes), + c(50, NA_real_) + ) + expect_equal(pk.calc.cav(10, c(0, 5), 5), c(2, NA_real_)) + expect_equal(pk.calc.dn(c(1, 2), c(1, NA)), c(1, NA_real_)) + expect_equal(pk.calc.ptr(c(2, 2), c(1, 0)), c(2, NA_real_)) + expect_equal(pk.calc.deg.fluc(cmax = c(100, 100), cmin = 10, cav = c(45, 0)), c(200, NA_real_)) + expect_equal(pk.calc.f(c(1, 0), 1, 1, 1), c(1, NA_real_)) + expect_equal(pk.calc.totdose(c(1, 2)), 3) +}) + +test_that("a dose-normalized zero over a zero dose stays NaN and says why", { + expect_equal(pk.calc.dn(0, 0), structure(NaN, exclude = "Dose is zero")) + # A nonzero value over a zero dose is infinite, as before, with no reason + expect_identical(pk.calc.dn(1, 0), Inf) +}) + +test_that("ctrough for an interval with no finite end says so", { + expect_equal(pk.calc.ctrough(1:5, 0:4, Inf), structure(NA_real_, exclude = "The interval has no finite end")) +}) diff --git a/tests/testthat/test-secondary-parameters.R b/tests/testthat/test-secondary-parameters.R index f74d7234..479a3c41 100644 --- a/tests/testthat/test-secondary-parameters.R +++ b/tests/testthat/test-secondary-parameters.R @@ -2046,6 +2046,9 @@ test_that("combine_exclude_reasons() combines and clears exclusion reasons", { expect_equal(combine_exclude_reasons(c("a", NA, "b"), NULL), "a; b") expect_equal(combine_exclude_reasons(c("a", "b"), "DO NOT EXCLUDE"), NA_character_) expect_equal(combine_exclude_reasons(NULL, "DO NOT EXCLUDE"), NA_character_) + # A reason is listed once, also when it is one of several already joined + expect_equal(combine_exclude_reasons(c("a", "a"), "a"), "a") + expect_equal(combine_exclude_reasons(c("a; b", "b; c"), "a"), "a; b; c") }) # The automatic reference finder, group_ref, and ephemeral references --------- diff --git a/vignettes/v80-writing-parameter-functions.Rmd b/vignettes/v80-writing-parameter-functions.Rmd index 3c194e42..574ef025 100644 --- a/vignettes/v80-writing-parameter-functions.Rmd +++ b/vignettes/v80-writing-parameter-functions.Rmd @@ -40,7 +40,7 @@ The interval's own times given to the function (`time` and `time.dose`) are rela The function should return either a scalar which is the value for the parameter (usually the case) or a data.frame with parameters named for each parameter calculated. For an example of returning a data.frame, see the `half.life` function. -The return value may have an attribute of `exclude` (set by `attr(return_value, "exclude") <- "reason"`). If the `exclude` attribute is set to a character string, then that string will be included in the `exclude` column for results. If any of the input parameters have an exclude attribute set, then those are also added to the `exclude` column. The exception to the setting of the `exclude` column is if the `exclude` attribute is `"DO NOT EXCLUDE"`, then the `exclude` column is set to `NA_character_`. +The return value may have an attribute of `exclude` (set by `attr(return_value, "exclude") <- "reason"`). If the `exclude` attribute is set to a character string, then that string will be included in the `exclude` column for results. If any of the input parameters have an exclude attribute set, then those are also added to the `exclude` column, each distinct reason once. When the function returns `NA` because of missing or unusable data, give the reason this way so that the `NA` is explained (for example, `structure(NA_real_, exclude = "Dose is missing")`). The exception to the setting of the `exclude` column is if the `exclude` attribute is `"DO NOT EXCLUDE"`, then the `exclude` column is set to `NA_character_`. ## Best Practices