Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial attempt (updates for neutropenia model) #90

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 13 additions & 5 deletions R/get_var_y.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#' @param in_parallel run simulations in parallel?
#' @param n_cores if run in parallel, on how many cores?
#' @param return_all return object with all relevant information?
#' @param exclude_parameters Vector of parameters to exclude from variability
#' calculation, implemented for "delta" method only.
#' @param ... passed on to `sim_ode()`
#'
#' @export
Expand All @@ -47,6 +49,7 @@ get_var_y <- function(
in_parallel = FALSE,
n_cores = 3,
return_all = FALSE,
exclude_parameters = NULL,
...) {
if(method == "sim" && is.null(n_ind)) {
stop("Please specify number of individuals when using simulation method!")
Expand Down Expand Up @@ -93,6 +96,11 @@ get_var_y <- function(
}
if(auc) y <- diff(y)
nams <- names(parameters_est)
if (!is.null(exclude_parameters)) {
ind <- which(!nams %in% exclude_parameters)
nams <- setdiff(nams, exclude_parameters)
omega_full <- omega_full[ind,ind]
}
jac <- c()
if(!(method %in% c("delta", "sim"))) {
stop("Requested method not recognized!")
Expand All @@ -101,10 +109,10 @@ get_var_y <- function(
v <- list()
qnt <- list()
if(method == "delta") {
sim_func <- function(i, ...) {
sim_func <- function(par, ...) {
par_tmp <- parameters
dP <- rel_delta * par_tmp[[nams[i]]]
par_tmp[[nams[i]]] <- par_tmp[[nams[i]]] + dP
dP <- rel_delta * par_tmp[[par]]
par_tmp[[par]] <- par_tmp[[par]] + dP
res_dP <- PKPDsim::sim_ode(
ode = model,
regimen = regimen,
Expand Down Expand Up @@ -132,9 +140,9 @@ get_var_y <- function(
}
# running in parallel is actually not faster for most simple models. Only for models with larger number of parameters.
if(in_parallel) {
jac <- matrix(unlist(parallel::mclapply(seq(along = nams), sim_func, mc.cores = n_cores, ...)), nrow = ifelse(auc, 1, length(t_obs)))
jac <- matrix(unlist(parallel::mclapply(nams, sim_func, mc.cores = n_cores, ...)), nrow = ifelse(auc, 1, length(t_obs)))
} else {
jac <- matrix(unlist(lapply(seq(along = nams), sim_func, ...)), nrow = ifelse(auc, 1, length(t_obs)))
jac <- matrix(unlist(lapply(nams, sim_func, ...)), nrow = ifelse(auc, 1, length(t_obs)))
}
for(i in seq(types)) {
type <- types[i]
Expand Down
4 changes: 4 additions & 0 deletions man/get_var_y.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions tests/testthat/test_get_var_y.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,28 @@ test_that("One compartment with MM kinetics", {
expect_equal(round(v1$regular, 3), c(0.120, 4.206))

})

test_that("delta method, exclude a parameter", {
skip_on_cran()
v_delta_with <- get_var_y(
model = mod_1cmt_iv,
parameters = par,
t_obs = t_obs,
regimen = reg,
omega = omega,
method = "delta"
)
v_delta_without <- get_var_y(
model = mod_1cmt_iv,
parameters = par,
t_obs = t_obs,
regimen = reg,
omega = omega,
method = "delta",
exclude_parameters = "V"
)

expect_true(all(v_delta_with$regular > v_delta_without$regular))
expect_true(all(v_delta_with$log > v_delta_without$log))
expect_equal(round(v_delta_without$regular, 3), c(0.055, 0.240))
})
Loading