-
Notifications
You must be signed in to change notification settings - Fork 15
Add compartments cmt to regimens in nm_to_regimen #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
1cb5abf
e414a40
a917277
a598a79
bdf78e0
9d1b8cb
37f154f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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!") | ||
|
|
@@ -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)){ | ||
| # map cmt to dose type | ||
| reg[[i]] <- new_regimen( | ||
| amt = tmp$amt, | ||
| times = tmp$time, | ||
| cmt = tmp$cmt, | ||
|
||
| 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){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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.