Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: pharmr.extra
Title: Extension of pharmr (Pharmpy) functionality
Version: 0.0.0.9032
Version: 0.0.0.9033
Authors@R: c(
person("Ron", "Keizer", email = "ron@insight-rx.com", role = c("cre", "aut")),
person("Michael", "McCarthy", email = "michael.mccarthy@insight-rx.com", role = "ctb"),
Expand Down
2 changes: 2 additions & 0 deletions R/get_initial_estimates_from_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ get_initial_estimates_from_data <- function(
suppressWarnings(
data <- data |>
dplyr::mutate(
AMT = as.numeric(as.character(.data$AMT)),
DV = as.numeric(.data$DV),
TIME = as.numeric(.data$TIME)
) |>
Expand Down Expand Up @@ -134,6 +135,7 @@ get_initial_estimates_from_individual_data <- function(
est$V <- dose / max(obs$DV, na.rm=TRUE)
est$CL <- KEL * est$V
} else { # more crude estimation

Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is trailing whitespace on this blank line, which shows up as a changed line in the diff. Please remove the stray spaces to avoid noisy diffs and potential lint/style check failures.

Suggested change

Copilot uses AI. Check for mistakes.
if(length(tmp$DV) > 0) {
est$V <- dose / (max(tmp$DV, na.rm=TRUE) * 5)
est$CL <- est$V / 10
Expand Down
61 changes: 61 additions & 0 deletions tests/testthat/test-get_initial_estimates_from_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,64 @@ test_that("get_initial_estimates_from_data errors on non-existent CSV file", {
"`data` file does not exist"
)
})

test_that("get_initial_estimates_from_data works when AMT is character", {
test_data <- data.frame(
ID = c(1, 1, 1, 1),
TIME = c(0, 1, 4, 8),
DV = c(0, 100, 50, 25),
EVID = c(1, 0, 0, 0),
MDV = c(1, 0, 0, 0),
AMT = c("1000", "0", "0", "0"),
stringsAsFactors = FALSE
)

result <- get_initial_estimates_from_data(test_data, n_cmt = 1)

expect_type(result, "list")
expect_named(result, c("V", "CL"))
expect_true(all(result > 0))
expect_equal(result$V, 10, tolerance = 0.1)
})

test_that("get_initial_estimates_from_data works when AMT is factor", {
test_data <- data.frame(
ID = c(1, 1, 1, 1),
TIME = c(0, 1, 4, 8),
DV = c(0, 100, 50, 25),
EVID = c(1, 0, 0, 0),
MDV = c(1, 0, 0, 0),
AMT = factor(c("1000", "0", "0", "0"))
)

result <- get_initial_estimates_from_data(test_data, n_cmt = 1)

expect_type(result, "list")
expect_named(result, c("V", "CL"))
expect_true(all(result > 0))
expect_equal(result$V, 10, tolerance = 0.1)
})

test_that("get_initial_estimates_from_data gives same result regardless of AMT column type", {
base_data <- data.frame(
ID = c(1, 1, 1, 1),
TIME = c(0, 1, 4, 8),
DV = c(0, 100, 50, 25),
EVID = c(1, 0, 0, 0),
MDV = c(1, 0, 0, 0),
AMT = c(1000, 0, 0, 0)
)

result_numeric <- get_initial_estimates_from_data(base_data, n_cmt = 1)
result_character <- get_initial_estimates_from_data(
transform(base_data, AMT = as.character(AMT)), n_cmt = 1
)
result_factor <- get_initial_estimates_from_data(
transform(base_data, AMT = as.factor(AMT)), n_cmt = 1
)

expect_equal(result_numeric$V, result_character$V, tolerance = 1e-6)
expect_equal(result_numeric$CL, result_character$CL, tolerance = 1e-6)
expect_equal(result_numeric$V, result_factor$V, tolerance = 1e-6)
expect_equal(result_numeric$CL, result_factor$CL, tolerance = 1e-6)
})
Loading