Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ URL: https://github.com/InsightRX/PKPDsim,
Language: en-US
LazyData: TRUE
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
RoxygenNote: 7.3.3
Config/testthat/edition: 3
Config/Needs/website: tidyverse, nlmixr2
Encoding: UTF-8
Expand Down
43 changes: 31 additions & 12 deletions R/nm_to_regimen.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
#' @param data NONMEM-type dataset
#' @param reset_time start time for each simulated patient at 0, irrespective of design in dataset
#' @param first_only use only design from first individual in dataset
#' @param dose_cmts map from compartment number to dose type, defaults to compartment 1 being an infusion dose
#' @export
#' @return Regimen object
nm_to_regimen <- function(data,
reset_time = TRUE,
first_only = FALSE) {
nm_to_regimen <- function(
data,
reset_time = TRUE,
first_only = FALSE,
dose_cmts = NULL
) {
colnames(data) <- tolower(colnames(data))
if(!"evid" %in% colnames(data)) {
stop("EVID column is required in source dataset!")
Expand All @@ -19,29 +23,44 @@ nm_to_regimen <- function(data,
if(! "time" %in% colnames(data)) {
stop("TIME column is required in source dataset!")
}
m <- match(c("id", "mdv", "evid", "amt", "time", "rate"), colnames(data), 0)
m <- match(c("id", "mdv", "evid", "amt", "time", "rate", "cmt"), colnames(data), 0)
m <- m[m>0]
data <- data[,m]
doses <- data[data$evid == 1,]
dum <- data[data$evid == 2,]
ids <- unique(doses$id)
if(first_only) {
ids <- ids[1]
}
type <- "bolus"
if("rate" %in% colnames(doses) &! 0 %in% doses$rate) {
type <- "infusion"
}
reg <- list()
for(i in 1:length(ids)) {
tmp <- doses[doses$id == ids[i],]
if(reset_time) {
tmp$time <- tmp$time - min(tmp$time)
}
if(type == "infusion") {
reg[[i]] <- new_regimen(amt = tmp$amt, times = tmp$time, type = "infusion", t_inf = tmp$amt / tmp$rate)
if (!is.null(dose_cmts)){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might also want to add a check for !is.null(tmp$cmt) since the code below uses it.

# map cmt to dose type
reg[[i]] <- new_regimen(
amt = tmp$amt,
times = tmp$time,
cmt = tmp$cmt,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could skip this argument. if it is null, PKPDsim::sim will use the compartment definitions of the model file, which is probably safer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see this leading to a situation where you specify CMT = 1 but PKPDsim keeps giving you CMT = 2 and you have no idea where it is coming from. Explicit is better than implicit, and this function is basically not used anywhere except in PKPDposterior (which is essentially unmaintained) and in mipdeval, where the original fix would have already worked.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you help me understand this case with an example? It is possible that NONMEM-style data and PKPDsim model definitions use different compartments for one dose type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/InsightRX/mipdeval/pull/25/files

This is the only relevant place this function is currently being used, where we want to specify which compartments are from which doses using the NONMEM data set. Leaving it to be assumed by the model downstream, especially when CMT should be specified in the data set anyway, is not optimal imo.

type = unname(unlist(dose_cmts[as.character(tmp$cmt)])),
t_inf = ifelse(is.na(tmp$rate), NA, tmp$amt / tmp$rate)
)
} else if ("rate" %in% colnames(doses) &! 0 %in% doses$rate){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we have two doses: on a first dose RATE=0 (bolus loading) and on second dose RATE=1? Then all doses will be treated as bolus, I think. Would add that as a test case to check, and potentially update behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two blocks below are for backwards compatibility. If you have CMT defined (which you really should), then the previous if statement works fine. If I were writing this function from scratch, compartment would be a required column.

# if rate exists and is non-zero, assume infusion
reg[[i]] <- new_regimen(
amt = tmp$amt,
times = tmp$time,
type = "infusion",
t_inf = tmp$amt / tmp$rate
)
} else {
reg[[i]] <- new_regimen(amt = tmp$amt, times = tmp$time, type = "bolus")
# assume bolus
reg[[i]] <- new_regimen(
amt = tmp$amt,
times = tmp$time,
type = "bolus"
)
}
}
if(length(ids) == 1) {
Expand Down
4 changes: 3 additions & 1 deletion man/nm_to_regimen.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 41 additions & 2 deletions tests/testthat/test_nm_to_regimen.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test_that("Required column checks occur", {
)
})

test_that("NM-style dataframe for 1 ID converted to regimen", {
test_that("Assume infusion if no compartment but RATE given", {
pt1 <- data.frame(
ID = 1,
EVID = c(1, 1, 0, 1, 1, 0),
Expand All @@ -38,15 +38,31 @@ test_that("NM-style dataframe for 1 ID converted to regimen", {
expect_equal(reg1$type, rep("infusion", 4))
})

test_that("Assume bolus if no compartment and no RATE given", {
pt1 <- data.frame(
ID = 1,
EVID = c(1, 1, 0, 1, 1, 0),
AMT = c(100, 100, 0, 200, 200, 0),
TIME = c(0, 12, 23, 25, 36, 47),
DV = c(0, 0, 5, 0, 0, 12)
)
reg1 <- nm_to_regimen(pt1)
expect_true(inherits(reg1, "regimen"))
expect_equal(reg1$dose_amts, c(100, 100, 200, 200))
expect_equal(reg1$dose_times, c(0, 12, 25, 36))
expect_equal(reg1$type, rep("bolus", 4))
})

test_that("Bolus/oral doses handled", {
pt2 <- data.frame(
ID = 1,
EVID = c(1, 1, 0, 1, 1, 0),
CMT = 1,
AMT = c(100, 100, 0, 200, 200, 0),
TIME = c(0, 12, 23, 25, 36, 47),
DV = c(0, 0, 5, 0, 0, 12)
)
reg2 <- nm_to_regimen(pt2)
reg2 <- nm_to_regimen(pt2, dose_cmts = c("1" = "bolus"))
expect_true(inherits(reg2, "regimen"))
expect_equal(reg2$dose_amts, c(100, 100, 200, 200))
expect_equal(reg2$dose_times, c(0, 12, 25, 36))
Expand All @@ -58,6 +74,7 @@ test_that("Multiple regimens from NONMEM-style dataset", {
nm <- data.frame(
ID = c(1, 1, 1, 2, 2, 2),
EVID = c(1, 1, 0, 1, 1, 0),
CMT = 1,
AMT = c(100, 100, 0, 200, 200, 0),
TIME = c(0, 12, 23, 0, 24, 47),
DV = c(0, 0, 5, 0, 0, 12)
Expand All @@ -71,3 +88,25 @@ test_that("Multiple regimens from NONMEM-style dataset", {
expect_equal(multi_regs[[1]]$dose_amts, c(100, 100))
expect_equal(multi_regs[[2]]$dose_amts, c(200, 200))
})

test_that("Doses in different compartments handled", {
pt2 <- data.frame(
ID = 1,
EVID = c(1, 1, 0, 1, 1, 0, 1, 1, 0),
CMT = c(2, 2, 2, 2, 2, 2, 1, 1, 2), # last two doses are oral
AMT = c(100, 100, 0, 200, 200, 0, 150, 150, 0),
TIME = c(0, 12, 23, 25, 36, 47, 48, 60, 71),
RATE = c(100, 100, 0, 200, 400, 0, 0, 0, 0),
DV = c(0, 0, 5, 0, 0, 12, 0, 0, 14)
)
cmt_mapping <- c(
"1" = "oral",
"2" = "infusion"
)
reg2 <- nm_to_regimen(pt2, dose_cmts = cmt_mapping)
expect_true(inherits(reg2, "regimen"))
expect_equal(reg2$dose_amts, c(100, 100, 200, 200, 150, 150))
expect_equal(reg2$dose_times, c(0, 12, 25, 36, 48, 60))
expect_equal(reg2$t_inf, c(1, 1, 1, 0.5, 0, 0))
expect_equal(reg2$type, c(rep("infusion", 4), rep("oral", 2)))
})