diff --git a/DESCRIPTION b/DESCRIPTION index 4aa8216..fd74b3c 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/R/new_regimen.R b/R/new_regimen.R index 06c86c2..79c5ab6 100644 --- a/R/new_regimen.R +++ b/R/new_regimen.R @@ -68,10 +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 = 1 - } else if (any(is.na(t_inf))) { - t_inf[is.na(t_inf)] <- 1 + 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) { @@ -121,7 +127,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")) { + 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 diff --git a/tests/testthat/test_new_regimen.R b/tests/testthat/test_new_regimen.R index 0b15c05..f5559b8 100644 --- a/tests/testthat/test_new_regimen.R +++ b/tests/testthat/test_new_regimen.R @@ -67,3 +67,51 @@ 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)) +}) + +test_that("t_inf imputed correctly", { + reg1 <- new_regimen( + amt = 100, + 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, 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) + ) + 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)) +}) +