diff --git a/R/half.life.R b/R/half.life.R index 0dab3f1e..0ab7aab6 100644 --- a/R/half.life.R +++ b/R/half.life.R @@ -180,6 +180,11 @@ pk.calc.half.life <- function(conc, time, tmax, tlast, # as.numeric is for units handling dfK <- data[as.numeric(data$time) > as.numeric(ret$tmax), ] } + # When all concentrations are the same (non-zero) value cannot compute half-life (#503) + if (sd(data$log_conc, na.rm = TRUE) == 0) { + attr(ret, "exclude") <- "No point variability in concentrations for half-life calculation" + return(ret) + } if (manually.selected.points) { if (nrow(data) > 0) { fit <- fit_half_life(data=data, tlast=ret$tlast, conc_units=conc_units) @@ -266,6 +271,7 @@ pk.calc.half.life <- function(conc, time, tmax, tlast, class = "pknca_halflife_too_few_points" ) } + # Drop the inputs of tmax and tlast, if given. if (!missing(tmax)) ret$tmax <- NULL diff --git a/tests/testthat/test-half.life.R b/tests/testthat/test-half.life.R index 202fa421..afd97d8b 100644 --- a/tests/testthat/test-half.life.R +++ b/tests/testthat/test-half.life.R @@ -506,3 +506,20 @@ test_that("get_halflife_points method (1: PKNCAdata, 2: PKNCAresults)", { info = "get_halflife_points uses lambda.z.time.last, not tlast" ) }) + +test_that("half-life has a exclude message when it cannot be calculated for flat data (#503)", { + result <- suppressMessages( + pk.calc.half.life( + conc = c(1, 1, 1, 1), + time = c(0, 1, 2, 3), + min.hl.points = 3, + allow.tmax.in.half.life = TRUE, + adj.r.squared.factor = 0.0001, + check = FALSE + ) + ) + expect_equal( + attr(result, "exclude"), + "No point variability in concentrations for half-life calculation" + ) +})