Skip to content

Commit ea8242e

Browse files
authored
Merge pull request #34 from InsightRX/RXR-2888
RXR-2888: skip in case of fit error and add options to reduce precision
2 parents 4127c21 + 428fffd commit ea8242e

7 files changed

Lines changed: 126 additions & 18 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export(calculate_shrinkage)
1515
export(calculate_stats)
1616
export(compare_psn_execute_results)
1717
export(compare_psn_proseval_results)
18+
export(fit_options)
1819
export(group_by_dose)
1920
export(group_by_time)
2021
export(install_default_literature_model)

R/calculate_stats.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ calculate_stats <- function(
2020
if(inherits(.res, "mipdeval_results")) {
2121
.res <- .res$results
2222
}
23+
## Check for errors during fits / predictions
24+
errors <- dplyr::filter(
25+
.res,
26+
is.na(.data$pred) |
27+
(is.na(.data$map_ipred) & !.data$apriori) |
28+
is.na(.data$iter_ipred)
29+
)
30+
if(nrow(errors) > 0) {
31+
cli::cli_warn("Errors were encountered in {nrow(errors)} out of {nrow(.res)} evaluated predictions. The problems occurred in patient(s) {unique(errors$id)}.")
32+
}
2333
out <- .res |>
2434
tidyr::pivot_longer(
2535
cols = c("pred", "map_ipred", "iter_ipred"), names_to = "type"

R/run_eval.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#' result from a call to [stats_summ_options()].
3333
#' @param .vpc_options Options for VPC simulations. This must be the result from
3434
#' a call to [vpc_options()].
35+
#' @param .fit_options Options for controlling MAP Bayesian fit. This must be
36+
#' the result from a call to [fit_options()].
3537
#' @param threads number of threads to divide computations on. Default is 1,
3638
#' i.e. no parallel execution
3739
#' @param ruv residual error variability magnitude, specified as list.
@@ -63,6 +65,7 @@ run_eval <- function(
6365
incremental = FALSE,
6466
.stats_summ_options = stats_summ_options(),
6567
.vpc_options = vpc_options(),
68+
.fit_options = fit_options(),
6669
threads = 1,
6770
progress = TRUE,
6871
verbose = TRUE
@@ -127,6 +130,7 @@ run_eval <- function(
127130
weight_prior = weight_prior,
128131
incremental = incremental,
129132
progress_function = p,
133+
.fit_options = .fit_options,
130134
.threads = threads,
131135
.skip = .vpc_options$vpc_only
132136
)
@@ -189,3 +193,26 @@ run_eval <- function(
189193
## 5. Return results
190194
out
191195
}
196+
197+
#' Options for controlling MAP Bayesian fit
198+
#'
199+
#' @param ... These dots are reserved for future extensibility and must be empty.
200+
#' @param reltol Relative convergence tolerance. `reltol = 1e-04` will perform a
201+
#' slightly coarser but more stable fit, which can be useful in some case.
202+
#'
203+
#' @returns A list.
204+
#' @export
205+
fit_options <- function(
206+
...,
207+
reltol = 1e-05
208+
) {
209+
rlang::check_dots_empty()
210+
out <- list(
211+
control = list(
212+
reltol = vctrs::vec_assert(
213+
reltol, ptype = numeric(), size = 1L, arg = "reltol"
214+
)
215+
)
216+
)
217+
structure(out, class = "mipdeval_fit_options")
218+
}

R/run_eval_core.R

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ run_eval_core <- function(
1414
weight_prior = 1,
1515
censor_covariates = TRUE,
1616
incremental = FALSE,
17-
progress_function = function() {}
17+
progress_function = function() {},
18+
.fit_options = NULL
1819
) {
1920

2021
progress_function()
@@ -52,20 +53,26 @@ run_eval_core <- function(
5253
mod_upd$parameters <- fit$parameters # take params from previous fit
5354
mod_upd$omega <- fit$vcov
5455
}
55-
fit <- PKPDmap::get_map_estimates(
56-
model = mod_obj$model,
57-
parameters = mod_upd$parameters,
58-
omega = mod_upd$omega,
59-
error = mod_obj$ruv,
60-
fixed = mod_obj$fixed,
61-
as_eta = mod_obj$kappa,
62-
data = data$observations,
63-
covariates = cov_data,
64-
regimen = data$regimen,
65-
weight_prior = weight_prior,
66-
weights = weights,
67-
iov_bins = mod_obj$bins,
68-
verbose = FALSE
56+
fit <- do.call(
57+
PKPDmap::get_map_estimates,
58+
c(
59+
list(
60+
model = mod_obj$model,
61+
parameters = mod_upd$parameters,
62+
omega = mod_upd$omega,
63+
error = mod_obj$ruv,
64+
fixed = mod_obj$fixed,
65+
as_eta = mod_obj$kappa,
66+
data = data$observations,
67+
covariates = cov_data,
68+
regimen = data$regimen,
69+
weight_prior = weight_prior,
70+
weights = weights,
71+
iov_bins = mod_obj$bins,
72+
verbose = FALSE
73+
),
74+
.fit_options
75+
)
6976
)
7077

7178
## Data frame with predictive data
@@ -85,9 +92,44 @@ run_eval_core <- function(
8592
`_iteration` = iterations[i],
8693
`_grouper` = obs_data$`_grouper`
8794
)
95+
if(inherits(fit, "error")) {
96+
## create NA records for this fit
97+
pred_data <- tibble::tibble(
98+
id = obs_data$id,
99+
t = obs_data$t,
100+
dv = NA,
101+
ipred = NA,
102+
pred = NA,
103+
ofv = NA,
104+
ss_w = NA,
105+
`_iteration` = iterations[i],
106+
`_grouper` = obs_data$`_grouper`
107+
)
108+
par_dummy <- as.data.frame(mod_upd$parameters)
109+
par_dummy[, 1:ncol(par_dummy)] <- NA
110+
fit_pars <- dplyr::mutate(as.data.frame(par_dummy), id = obs_data$id[1])
111+
} else {
112+
## Data frame with predictive data
113+
pred_data <- tibble::tibble(
114+
id = obs_data$id,
115+
t = obs_data$t,
116+
dv = fit$dv,
117+
ipred = fit$ipred,
118+
ires = fit$ires,
119+
iwres = fit$iwres,
120+
pred = fit$pred,
121+
res = fit$res,
122+
wres = fit$wres,
123+
cwres = fit$cwres,
124+
ofv = fit$fit$value,
125+
ss_w = ss(fit$dv, fit$ipred, weights),
126+
`_iteration` = iterations[i],
127+
`_grouper` = obs_data$`_grouper`
128+
)
129+
## Add parameter estimates
130+
fit_pars <- dplyr::mutate(as.data.frame(fit$parameters), id = obs_data$id[1])
131+
}
88132

89-
## Add parameter estimates
90-
fit_pars <- dplyr::mutate(as.data.frame(fit$parameters), id = obs_data$id[1])
91133
comb <- dplyr::bind_rows(
92134
comb, dplyr::left_join(pred_data, fit_pars, by = "id")
93135
)

man/fit_options.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/run_eval.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/run_eval_core.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)