Skip to content

Commit 73da416

Browse files
authored
Merge pull request #128 from InsightRX/RXR-2888
Add compartments cmt to regimens in nm_to_regimen
2 parents 6753b53 + 37f154f commit 73da416

3 files changed

Lines changed: 107 additions & 24 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ URL: https://github.com/InsightRX/PKPDsim,
3030
Language: en-US
3131
LazyData: TRUE
3232
Roxygen: list(markdown = TRUE)
33-
RoxygenNote: 7.3.2
33+
RoxygenNote: 7.3.3
3434
Config/testthat/edition: 3
3535
Config/Needs/website: tidyverse, nlmixr2
3636
Encoding: UTF-8

R/nm_to_regimen.R

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#' @param first_only use only design from first individual in dataset
77
#' @export
88
#' @return Regimen object
9-
nm_to_regimen <- function(data,
10-
reset_time = TRUE,
11-
first_only = FALSE) {
9+
nm_to_regimen <- function(
10+
data,
11+
reset_time = TRUE,
12+
first_only = FALSE
13+
) {
1214
colnames(data) <- tolower(colnames(data))
1315
if(!"evid" %in% colnames(data)) {
1416
stop("EVID column is required in source dataset!")
@@ -19,29 +21,60 @@ nm_to_regimen <- function(data,
1921
if(! "time" %in% colnames(data)) {
2022
stop("TIME column is required in source dataset!")
2123
}
22-
m <- match(c("id", "mdv", "evid", "amt", "time", "rate"), colnames(data), 0)
24+
if (!"cmt" %in% colnames(data)) {
25+
warning("CMT column is missing from source dataset!")
26+
}
27+
m <- match(c("id", "mdv", "evid", "amt", "time", "rate", "cmt"), colnames(data), 0)
2328
m <- m[m>0]
2429
data <- data[,m]
2530
doses <- data[data$evid == 1,]
26-
dum <- data[data$evid == 2,]
2731
ids <- unique(doses$id)
2832
if(first_only) {
2933
ids <- ids[1]
3034
}
31-
type <- "bolus"
32-
if("rate" %in% colnames(doses) &! 0 %in% doses$rate) {
33-
type <- "infusion"
34-
}
3535
reg <- list()
3636
for(i in 1:length(ids)) {
3737
tmp <- doses[doses$id == ids[i],]
3838
if(reset_time) {
3939
tmp$time <- tmp$time - min(tmp$time)
4040
}
41-
if(type == "infusion") {
42-
reg[[i]] <- new_regimen(amt = tmp$amt, times = tmp$time, type = "infusion", t_inf = tmp$amt / tmp$rate)
41+
# use CMT if it exists in input data
42+
if (!is.null(tmp$cmt)){
43+
# if RATE is given, use to calculate infusion time t_inf
44+
if (!is.null(tmp$rate)){
45+
suppressWarnings({
46+
reg[[i]] <- new_regimen(
47+
amt = tmp$amt,
48+
times = tmp$time,
49+
cmt = tmp$cmt,
50+
t_inf = ifelse(tmp$rate == 0, 0, tmp$amt/tmp$rate)
51+
)
52+
})
53+
} else {
54+
# if no RATE is given, then assume bolus
55+
suppressWarnings({
56+
reg[[i]] <- new_regimen(
57+
amt = tmp$amt,
58+
times = tmp$time,
59+
cmt = tmp$cmt
60+
)
61+
})
62+
}
63+
} else if ("rate" %in% colnames(doses) &! 0 %in% doses$rate){
64+
# if rate exists and is non-zero, assume infusion
65+
reg[[i]] <- new_regimen(
66+
amt = tmp$amt,
67+
times = tmp$time,
68+
type = "infusion",
69+
t_inf = tmp$amt / tmp$rate
70+
)
4371
} else {
44-
reg[[i]] <- new_regimen(amt = tmp$amt, times = tmp$time, type = "bolus")
72+
# assume bolus
73+
reg[[i]] <- new_regimen(
74+
amt = tmp$amt,
75+
times = tmp$time,
76+
type = "bolus"
77+
)
4578
}
4679
}
4780
if(length(ids) == 1) {

tests/testthat/test_nm_to_regimen.R

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
test_that("Required column checks occur", {
22
expect_error(
3-
nm_to_regimen(
4-
data.frame(EVID = 0, AMT = 100)
5-
),
3+
nm_to_regimen(data.frame(EVID = 0, AMT = 100)),
64
"TIME column is required"
75
)
86

97
expect_error(
10-
nm_to_regimen(
11-
data.frame(EVID = 0, TIME = 0)
12-
),
8+
nm_to_regimen(data.frame(EVID = 0, TIME = 0)),
139
"AMT column is required"
1410
)
1511

1612
expect_error(
17-
nm_to_regimen(
18-
data.frame(AMT = 100, TIME = 0)
19-
),
13+
nm_to_regimen(data.frame(AMT = 100, TIME = 0)),
2014
"EVID column is required"
2115
)
2216
})
2317

24-
test_that("NM-style dataframe for 1 ID converted to regimen", {
18+
test_that("Assume infusion if no compartment but RATE given", {
2519
pt1 <- data.frame(
2620
ID = 1,
2721
EVID = c(1, 1, 0, 1, 1, 0),
@@ -38,10 +32,26 @@ test_that("NM-style dataframe for 1 ID converted to regimen", {
3832
expect_equal(reg1$type, rep("infusion", 4))
3933
})
4034

41-
test_that("Bolus/oral doses handled", {
35+
test_that("Assume bolus if no compartment and no RATE given", {
36+
pt1 <- data.frame(
37+
ID = 1,
38+
EVID = c(1, 1, 0, 1, 1, 0),
39+
AMT = c(100, 100, 0, 200, 200, 0),
40+
TIME = c(0, 12, 23, 25, 36, 47),
41+
DV = c(0, 0, 5, 0, 0, 12)
42+
)
43+
reg1 <- nm_to_regimen(pt1)
44+
expect_true(inherits(reg1, "regimen"))
45+
expect_equal(reg1$dose_amts, c(100, 100, 200, 200))
46+
expect_equal(reg1$dose_times, c(0, 12, 25, 36))
47+
expect_equal(reg1$type, rep("bolus", 4))
48+
})
49+
50+
test_that("Assume bolus and no t_inf if RATE not given", {
4251
pt2 <- data.frame(
4352
ID = 1,
4453
EVID = c(1, 1, 0, 1, 1, 0),
54+
CMT = 1,
4555
AMT = c(100, 100, 0, 200, 200, 0),
4656
TIME = c(0, 12, 23, 25, 36, 47),
4757
DV = c(0, 0, 5, 0, 0, 12)
@@ -52,12 +62,14 @@ test_that("Bolus/oral doses handled", {
5262
expect_equal(reg2$dose_times, c(0, 12, 25, 36))
5363
expect_equal(reg2$t_inf, rep(0, 4))
5464
expect_equal(reg2$type, rep("bolus", 4))
65+
expect_equal(reg2$cmt, rep(1, 4))
5566
})
5667

5768
test_that("Multiple regimens from NONMEM-style dataset", {
5869
nm <- data.frame(
5970
ID = c(1, 1, 1, 2, 2, 2),
6071
EVID = c(1, 1, 0, 1, 1, 0),
72+
CMT = c(1, 1, 1, 1, 2, 1),
6173
AMT = c(100, 100, 0, 200, 200, 0),
6274
TIME = c(0, 12, 23, 0, 24, 47),
6375
DV = c(0, 0, 5, 0, 0, 12)
@@ -70,4 +82,42 @@ test_that("Multiple regimens from NONMEM-style dataset", {
7082
expect_equal(multi_regs[[2]]$dose_times, c(0, 24))
7183
expect_equal(multi_regs[[1]]$dose_amts, c(100, 100))
7284
expect_equal(multi_regs[[2]]$dose_amts, c(200, 200))
85+
expect_equal(multi_regs[[1]]$cmt, c(1, 1))
86+
expect_equal(multi_regs[[2]]$cmt, c(1, 2))
87+
})
88+
89+
test_that("Doses in different compartments handled", {
90+
pt2 <- data.frame(
91+
ID = 1,
92+
EVID = c(1, 1, 0, 1, 1, 0, 1, 1, 0),
93+
CMT = c(2, 2, 2, 2, 2, 2, 1, 1, 2), # last two doses are oral
94+
AMT = c(100, 100, 0, 200, 200, 0, 150, 150, 0),
95+
TIME = c(0, 12, 23, 25, 36, 47, 48, 60, 71),
96+
RATE = c(100, 100, 0, 200, 400, 0, 0, 0, 0),
97+
DV = c(0, 0, 5, 0, 0, 12, 0, 0, 14)
98+
)
99+
reg2 <- nm_to_regimen(pt2)
100+
expect_true(inherits(reg2, "regimen"))
101+
expect_equal(reg2$dose_amts, c(100, 100, 200, 200, 150, 150))
102+
expect_equal(reg2$dose_times, c(0, 12, 25, 36, 48, 60))
103+
expect_equal(reg2$t_inf, c(1, 1, 1, 0.5, 0, 0))
104+
expect_equal(reg2$cmt, c(2, 2, 2, 2, 1, 1))
105+
})
106+
107+
test_that("Different rates handled correctly", {
108+
pt2 <- data.frame(
109+
ID = 1,
110+
EVID = c(1, 1, 0),
111+
CMT = 2,
112+
AMT = c(100, 200, 0),
113+
TIME = c(0, 6, 23),
114+
RATE = c(0, 100, 0),
115+
DV = c(0, 0, 5)
116+
)
117+
reg2 <- nm_to_regimen(pt2)
118+
expect_true(inherits(reg2, "regimen"))
119+
expect_equal(reg2$dose_amts, c(100, 200))
120+
expect_equal(reg2$dose_times, c(0, 6))
121+
expect_equal(reg2$t_inf, c(0, 2))
122+
expect_equal(reg2$cmt, c(2, 2))
73123
})

0 commit comments

Comments
 (0)