From dde74ea087a8dca842724a2b53aa2a60616d715e Mon Sep 17 00:00:00 2001 From: Jordan Brooks Date: Mon, 3 Jun 2024 23:13:54 +0000 Subject: [PATCH 1/8] add t_inf to sc doses in new_regimen --- R/new_regimen.R | 11 +++++++++++ tests/testthat/test_new_regimen.R | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/R/new_regimen.R b/R/new_regimen.R index 06c86c2d..12b91edd 100644 --- a/R/new_regimen.R +++ b/R/new_regimen.R @@ -73,6 +73,11 @@ new_regimen <- function( } else if (any(is.na(t_inf))) { t_inf[is.na(t_inf)] <- 1 } + if(any(type == "sc") && (is.null(t_inf) || length(t_inf) == 0)) { + reg$t_inf = 1/60 + } else if (any(is.na(t_inf))) { + t_inf[is.na(t_inf)] <- 1/60 + } } if(ss) { if(is.null(amt) || is.null(interval)) { @@ -135,6 +140,12 @@ 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)) diff --git a/tests/testthat/test_new_regimen.R b/tests/testthat/test_new_regimen.R index 0b15c050..edde21f4 100644 --- a/tests/testthat/test_new_regimen.R +++ b/tests/testthat/test_new_regimen.R @@ -67,3 +67,13 @@ test_that("new_regimen can take arbitrary values for `type`", { test_that("do not creat regimens of `type` 'covariate'", { expect_error(new_regimen(100, times = 0, type = "covariate")) }) + +test_that("sc doses accept an infusion length argument'", { + reg1 <- new_regimen( + amt = 100, + times = c(0, 12, 24, 36, 48), + type = "sc", + t_inf = 30/60 + ) + expect_equal(reg1$t_inf, rep(0.5,5)) +}) From bc6056382cfcd52c5722d476305eff61cceb623b Mon Sep 17 00:00:00 2001 From: jasmineirx Date: Tue, 4 Jun 2024 12:31:03 -0400 Subject: [PATCH 2/8] add a couple of edge cases to test --- tests/testthat/test_new_regimen.R | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/testthat/test_new_regimen.R b/tests/testthat/test_new_regimen.R index edde21f4..7bf08a3c 100644 --- a/tests/testthat/test_new_regimen.R +++ b/tests/testthat/test_new_regimen.R @@ -77,3 +77,19 @@ test_that("sc doses accept an infusion length argument'", { ) expect_equal(reg1$t_inf, rep(0.5,5)) }) + +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") + ) + 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) + ) + 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)) +}) From 880e4164b037fe9c70ac34cf3efd641e5b4a6d69 Mon Sep 17 00:00:00 2001 From: Jordan Brooks Date: Thu, 6 Jun 2024 19:29:06 +0000 Subject: [PATCH 3/8] updated logic to cover mixed regimen type automatic t_inf assignment --- R/new_regimen.R | 37 ++++++++++++++++++++----------- tests/testthat/test_new_regimen.R | 15 +++++++------ 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/R/new_regimen.R b/R/new_regimen.R index 12b91edd..729fce3b 100644 --- a/R/new_regimen.R +++ b/R/new_regimen.R @@ -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) { @@ -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")) { @@ -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)) diff --git a/tests/testthat/test_new_regimen.R b/tests/testthat/test_new_regimen.R index 7bf08a3c..ba7867b8 100644 --- a/tests/testthat/test_new_regimen.R +++ b/tests/testthat/test_new_regimen.R @@ -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)) }) + From 1693710a14c19bad04d2aaea80638713d7826495 Mon Sep 17 00:00:00 2001 From: Jordan Brooks Date: Thu, 6 Jun 2024 19:35:18 +0000 Subject: [PATCH 4/8] altered logic when t_inf==0 to update to 1/60 for infusion, sc, and im --- R/new_regimen.R | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/R/new_regimen.R b/R/new_regimen.R index 729fce3b..134f9e66 100644 --- a/R/new_regimen.R +++ b/R/new_regimen.R @@ -143,10 +143,8 @@ new_regimen <- function( } if(any(reg$type == "infusion" | reg$type == "sc" | reg$type == "im")) { if(any(reg$t_inf == 0)) { - 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 + reg$t_inf[reg$t_inf == 0] <- 1/60 + reg$rate[reg$t_inf == 0] <- 60 } } if(any(reg$type == "bolus")) { From 92313aeae7fea7e7eb2858f56b8eecd6e7ebe984 Mon Sep 17 00:00:00 2001 From: JordanBrooks33 <129786148+JordanBrooks33@users.noreply.github.com> Date: Thu, 6 Jun 2024 13:47:14 -0700 Subject: [PATCH 5/8] Update R/new_regimen.R Co-authored-by: Jasmine Hughes <43552465+jasmineirx@users.noreply.github.com> --- R/new_regimen.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/new_regimen.R b/R/new_regimen.R index 134f9e66..cde336a3 100644 --- a/R/new_regimen.R +++ b/R/new_regimen.R @@ -141,7 +141,7 @@ 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" | reg$type == "sc" | reg$type == "im")) { + if(any(reg$type %in% c("infusion", "sc", "im"))) { if(any(reg$t_inf == 0)) { reg$t_inf[reg$t_inf == 0] <- 1/60 reg$rate[reg$t_inf == 0] <- 60 From 5bbf857d08e9f58f420459b3f74bf256911504f7 Mon Sep 17 00:00:00 2001 From: JordanBrooks33 <129786148+JordanBrooks33@users.noreply.github.com> Date: Thu, 6 Jun 2024 13:48:17 -0700 Subject: [PATCH 6/8] Update R/new_regimen.R Co-authored-by: Jasmine Hughes <43552465+jasmineirx@users.noreply.github.com> --- R/new_regimen.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/new_regimen.R b/R/new_regimen.R index cde336a3..ed4d4838 100644 --- a/R/new_regimen.R +++ b/R/new_regimen.R @@ -69,7 +69,7 @@ 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[reg$type=="infusion"] = 1 + reg$t_inf[reg$type == "infusion"] <- 1 } else if (any(is.na(t_inf))) { reg$t_inf[(reg$type=="infusion" & is.na(t_inf))] <- 1 } From 9aceaf807c3ff1bdf25dfb1855456e16705b2b29 Mon Sep 17 00:00:00 2001 From: Jordan Brooks Date: Thu, 6 Jun 2024 20:51:39 +0000 Subject: [PATCH 7/8] cleaned up repetitive formatting --- R/new_regimen.R | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/R/new_regimen.R b/R/new_regimen.R index ed4d4838..79c5ab65 100644 --- a/R/new_regimen.R +++ b/R/new_regimen.R @@ -68,30 +68,16 @@ new_regimen <- function( if (any(reg$type == "covariate")) { 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[reg$type == "infusion"] <- 1 - } else if (any(is.na(t_inf))) { - 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[reg$type=="sc"] = 1/60 - } else if (any(is.na(t_inf))) { - 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 (is.null(t_inf) || length(t_inf) == 0) { + reg$t_inf <- rep(NA_real_, length(reg$type)) + } + if (any(is.na(reg$t_inf))) { + # impute using standard values for each dose type + impute_t_inf <- c(infusion = 1, sc = 1/60, im = 1/60, oral = 0, bolus = 0) + idx <- which(is.na(reg$t_inf)) + reg$t_inf[idx] <- impute_t_inf[reg$type[idx]] + # set unrecognized types to 1 + reg$t_inf[is.na(reg$t_inf)] <- 1 } } if(ss) { From 8c43c2d4e732bf41397ee253f5837c8ae52d6241 Mon Sep 17 00:00:00 2001 From: jasmineirx Date: Fri, 7 Jun 2024 12:07:30 -0400 Subject: [PATCH 8/8] add contributor; tests for edge cases --- DESCRIPTION | 1 + tests/testthat/test_new_regimen.R | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index b903c19d..f45788a7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -8,6 +8,7 @@ Authors@R: c( person("Jasmine", "Hughes", email = "jasmine@insight-rx.com", role = "aut"), person("Dominic", "Tong", email = "dominic@insight-rx.com", role = "aut"), person("Kara", "Woo", email = "kara@insight-rx.com", role = c("aut")), + person("Jordan", "Brooks", email = "jordan.brooks@insight-rx.com", role = "aut"), person("InsightRX", role = c("cph", "fnd"))) Depends: R (>= 3.0.2) Imports: Rcpp (>= 0.12.9), BH, data.table, stringr, MASS, diff --git a/tests/testthat/test_new_regimen.R b/tests/testthat/test_new_regimen.R index ba7867b8..f5559b87 100644 --- a/tests/testthat/test_new_regimen.R +++ b/tests/testthat/test_new_regimen.R @@ -90,7 +90,28 @@ test_that("t_inf imputed correctly", { type = c("sc", "infusion", "im", "sc", "infusion", "im","bolus","oral"), t_inf = c(2/60, 2.5, 3/60, NA, NA, NA, NA, NA) ) + reg3 <- new_regimen( + amt = 100, + times = c(0, 12, 24, 36, 48, 60, 72, 84), + type = c("sc", "infusion", "im", "sc", "infusion", "im","bolus","oral"), + t_inf = numeric(0) + ) + reg4 <- new_regimen( + amt = 100, + times = c(0, 12, 24, 36, 48, 60, 72, 84), + type = c("sc", "infusion", "im", "sc", "infusion", "im","bolus","oral"), + t_inf = NULL + ) + reg5 <- new_regimen( + amt = 100, + times = c(0, 12, 24, 36), + type = c("sc", "infusion", "im", "unknown_drug_type"), + t_inf = c(2/60, 2.5, 3/60, NA) + ) 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)) + expect_equal(reg3$t_inf, c(1/60, 1, 1/60, 1/60, 1, 1/60, 0, 0)) + expect_equal(reg4$t_inf, c(1/60, 1, 1/60, 1/60, 1, 1/60, 0, 0)) + expect_equal(reg5$t_inf, c(2/60, 2.5, 3/60, 1)) })