Skip to content

Commit

Permalink
updated logic to cover mixed regimen type automatic t_inf assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanBrooks33 committed Jun 6, 2024
1 parent bc60563 commit 880e416
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
37 changes: 24 additions & 13 deletions R/new_regimen.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,29 @@ new_regimen <- function(
stop("'covariate' is a protected type and cannot be used for doses.")
}
if(any(type == "infusion") && (is.null(t_inf) || length(t_inf) == 0)) {
reg$t_inf = 1
reg$t_inf[reg$type=="infusion"] = 1
} else if (any(is.na(t_inf))) {
t_inf[is.na(t_inf)] <- 1
reg$t_inf[(reg$type=="infusion" & is.na(t_inf))] <- 1
}
if(any(type == "sc") && (is.null(t_inf) || length(t_inf) == 0)) {
reg$t_inf = 1/60
reg$t_inf[reg$type=="sc"] = 1/60
} else if (any(is.na(t_inf))) {
t_inf[is.na(t_inf)] <- 1/60
reg$t_inf[(reg$type=="sc" & is.na(reg$t_inf))] <- 1/60
}
if(any(type == "im") & (is.null(t_inf) || length(t_inf) == 0)) {
reg$t_inf[reg$type=="im"] = 1/60
} else if (any(is.na(t_inf))) {
reg$t_inf[(reg$type=="im" & is.na(reg$t_inf))] <- 1/60
}
if(any(type == "bolus") && (is.null(t_inf) || length(t_inf) == 0)) {
reg$t_inf[reg$type=="bolus"] = 0
} else if (any(is.na(t_inf))) {
reg$t_inf[(reg$type=="bolus" & is.na(reg$t_inf))] <- 0
}
if(any(type == "oral") && (is.null(t_inf) || length(t_inf) == 0)) {
reg$t_inf[reg$type=="oral"] = 0
} else if (any(is.na(t_inf))) {
reg$t_inf[(reg$type=="oral" & is.na(reg$t_inf))] <- 0
}
}
if(ss) {
Expand Down Expand Up @@ -126,10 +141,12 @@ new_regimen <- function(
if(length(reg$type) != length(reg$dose_times)) {
reg$type <- rep(reg$type[1], length(reg$dose_times))
}
if(any(reg$type == "infusion")) {
if(any(reg$type == "infusion" | reg$type == "sc" | reg$type == "im")) {
if(any(reg$t_inf == 0)) {
reg$t_inf[reg$t_inf == 0] <- 1/60
reg$rate[reg$t_inf == 0] <- 60
reg$t_inf[reg$t_inf == 0 & (type =="sc" | type =="im")] <- 1/60
reg$rate[reg$t_inf == 0 & (type =="sc" | type =="im")] <- 60
reg$t_inf[reg$t_inf == 0 & type == "infusion"] <- 1/60
reg$rate[reg$t_inf == 0 & type == "infusion"] <- 60
}
}
if(any(reg$type == "bolus")) {
Expand All @@ -140,12 +157,6 @@ new_regimen <- function(
reg$t_inf[reg$type == "oral"] <- 0
reg$rate[reg$type == "oral"] <- 0
}
if(any(reg$type == "sc")) {
if(any(reg$t_inf == 0)) {
reg$t_inf[reg$t_inf == 0] <- 1/60
reg$rate[reg$t_inf == 0] <- 60
}
}
if(!is.null(cmt)) {
if(length(cmt) != length(reg$dose_times)) {
cmt <- rep(cmt[1], length(reg$dose_times))
Expand Down
15 changes: 8 additions & 7 deletions tests/testthat/test_new_regimen.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,16 @@ test_that("sc doses accept an infusion length argument'", {
test_that("t_inf imputed correctly", {
reg1 <- new_regimen(
amt = 100,
times = c(0, 12, 24, 36, 48),
type = c("sc", "infusion", "sc", "infusion", "sc")
times = c(0, 12, 24, 36, 48, 60, 72, 84),
type = c("sc", "infusion", "im", "sc", "infusion", "im","bolus","oral")
)
reg2 <- new_regimen(
amt = 100,
times = c(0, 12, 24, 36, 48),
type = c("sc", "infusion", "sc", "infusion", "sc"),
t_inf = c(2/60, 2.5, NA, NA, 0)
times = c(0, 12, 24, 36, 48, 60, 72, 84),
type = c("sc", "infusion", "im", "sc", "infusion", "im","bolus","oral"),
t_inf = c(2/60, 2.5, 3/60, NA, NA, NA, NA, NA)
)
expect_equal(reg1$t_inf, c(1/60, 1, 1/60, 1, 1/60))
expect_equal(reg2$t_inf, c(2/60, 2.5, 1/60, 1, 1/60))
expect_equal(reg1$t_inf, c(1/60, 1, 1/60, 1/60, 1, 1/60, 0, 0))
expect_equal(reg2$t_inf, c(2/60, 2.5, 3/60, 1/60, 1, 1/60, 0, 0))
})

0 comments on commit 880e416

Please sign in to comment.