From 61fd1fb46d03368d213e705c333b70c4d5e8fc16 Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Mon, 21 Dec 2015 22:17:15 -0500 Subject: [PATCH] Fixes #8, when providing time to calculations within an interval, subtract away the time for th beginning of the interval. For example, Tmax on day 14 should be between 0 and 24 not 2*7*24+c(0, 24) --- R/pk.calc.all.R | 4 +- tests/testthat/test-pk.calc.all.R | 72 +++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/R/pk.calc.all.R b/R/pk.calc.all.R index 4efb78da..3cc212b5 100644 --- a/R/pk.calc.all.R +++ b/R/pk.calc.all.R @@ -146,7 +146,9 @@ pk.nca.interval <- function(conc, time, interval, options=list()) { if (arg == "conc") { call.args[[arg]] <- conc } else if (arg == "time") { - call.args[[arg]] <- time + ## Realign the time to be relative to the start of the + ## interval + call.args[[arg]] <- time - interval$start[1] } else if (arg == "options") { call.args[[arg]] <- options } else if (any(mask.arg <- ret$PPTESTCD %in% arg)) { diff --git a/tests/testthat/test-pk.calc.all.R b/tests/testthat/test-pk.calc.all.R index d174439f..186a3ad7 100644 --- a/tests/testthat/test-pk.calc.all.R +++ b/tests/testthat/test-pk.calc.all.R @@ -1,2 +1,74 @@ context("All NCA calculations") +source("generate.data.R") + +test_that("pk.nca", { + ## Note that generate.conc sets the random seed, so it doesn't have + ## to happen here. + tmpconc <- generate.conc(2, 1, 0:24) + tmpdose <- generate.dose(tmpconc) + myconc <- PKNCAconc(tmpconc, conc~time|treatment+ID) + mydose <- PKNCAdose(tmpdose, dose~time|treatment+ID) + mydata <- PKNCAdata(myconc, mydose) + myresult <- pk.nca(mydata) + + expect_equal(names(myresult), + c("result", "data", "provenance"), + info="Make sure that the result has the expected names (and only the expected names) in it.") + + ## Test each of the pieces for myresult for accuracy + + expect_equal(myresult$data, { + tmp <- mydata + ## The options should be the default options after the + ## calculations are done. + tmp$options <- PKNCA.options() + tmp + }, info="The data is just a copy of the input data plus an instantiation of the PKNCA.options") + + ## The specific order of the levels isn't important-- the fact that + ## they are factors and that the set doesn't change is important. + test.code.levels <- levels(myresult$result$PPTESTCD) + verify.result <- + data.frame( + start=0, + end=c(24, rep(Inf, 12), + 24, rep(Inf, 12)), + treatment="Trt 1", + ID=rep(c(1, 2), each=13), + PPTESTCD=factor(rep(c("auclast", "cmax", "tmax", "tlast", + "lambda.z", "r.squared", "adj.r.squared", + "lambda.z.time.first", "lambda.z.n.points", + "clast.pred", "half.life", "span.ratio", + "aucinf"), + times=2), + levels=test.code.levels), + PPORRES=c(13.54, 0.9998, 4.000, 24.00, + 0.04297, 0.9072, 0.9021, 5.000, + 20.00, 0.3356, 16.13, 1.178, + 21.55, 14.03, 0.9410, 2.000, + 24.00, 0.05689, 0.9000, 0.8944, + 5.000, 20.00, 0.3011, 12.18, + 1.560, 19.56), + stringsAsFactors=FALSE) + expect_equal(myresult$result, verify.result, + tol=0.001) + + ## Dosing not at time 0 + tmpconc.multi <- generate.conc(2, 1, 0:24) + tmpdose.multi <- generate.dose(tmpconc) + tmpconc.multi$time <- tmpconc.multi$time + 2 + tmpdose.multi$time <- tmpdose.multi$time + 2 + myconc.multi <- PKNCAconc(tmpconc.multi, conc~time|treatment+ID) + mydose.multi <- PKNCAdose(tmpdose.multi, dose~time|treatment+ID) + mydata.multi <- PKNCAdata(myconc.multi, mydose.multi) + myresult.multi <- pk.nca(mydata.multi) + + verify.result.multi <- verify.result + verify.result.multi$start <- verify.result.multi$start + 2 + verify.result.multi$end <- verify.result.multi$end + 2 + expect_equal(myresult.multi$result, verify.result.multi, + tol=0.001, + info="Shifted dosing works the same as un-shifted where time parameters like tmax and tlast are reported relative to the start of the interval") + +})