Skip to content

Commit 2e7d651

Browse files
committed
merge origin/main
Merge commit '4127c214a4857777a1e3a6554c0c6f9215ee16c5' #Conflicts: # R/calculate_stats.R # R/run_eval_core.R
2 parents e50ba4b + 4127c21 commit 2e7d651

9 files changed

Lines changed: 60 additions & 29 deletions

R/calculate_bayesian_impact.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#'
1212
#' @export
1313
calculate_bayesian_impact <- function(
14-
res
14+
.res
1515
) {
16-
out <- res$stats_summ |>
16+
out <- .res$stats_summ |>
1717
dplyr::filter(!.data$apriori) |>
1818
dplyr::select("type", "apriori", "rmse", "mape") |>
1919
tidyr::pivot_wider(names_from = "type", values_from = c("rmse", "mape")) |>

R/calculate_shrinkage.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
#' @returns tibble
2323
#'
2424
#' @export
25-
calculate_shrinkage <- function(res) {
26-
om <- get_omega_for_parameters(res$mod_obj)
27-
out <- res$results |>
25+
calculate_shrinkage <- function(.res) {
26+
om <- get_omega_for_parameters(.res$mod_obj)
27+
out <- .res$results |>
2828
dplyr::mutate(dplyr::across(
2929
.cols = names(om),
30-
.fns = ~ calc_eta(.x, dplyr::cur_column(), res$mod_obj$parameters),
30+
.fns = ~ calc_eta(.x, dplyr::cur_column(), .res$mod_obj$parameters),
3131
.names = "eta_{.col}"
3232
)) |>
3333
dplyr::group_by(.data$`_iteration`) |>

R/calculate_stats.R

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Calculate basic statistics, like RMSE, MPE, MAPE for forecasted data
22
#'
3-
#' @param res output object (`mipdeval_results`) from `run_eval()`, or
3+
#' @param .res output object (`mipdeval_results`) from `run_eval()`, or
44
#' `data.frame` with raw results.
55
#' @param rounding number of decimals to round to.
66
#' @param acc_error_abs,acc_error_rel For calculating [accuracy()]: Positive number
@@ -12,21 +12,25 @@
1212
#'
1313
#' @export
1414
calculate_stats <- function(
15-
res,
15+
.res,
1616
rounding = 3,
1717
acc_error_abs = NULL,
1818
acc_error_rel = NULL
1919
) {
20-
if(inherits(res, "mipdeval_results")) {
21-
res <- res$results
20+
if(inherits(.res, "mipdeval_results")) {
21+
.res <- .res$results
2222
}
2323
## Check for errors during fits / predictions
24-
errors <- res |>
25-
dplyr::filter(is.na(pred) | (is.na(map_ipred) & !apriori) | is.na(iter_ipred))
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+
)
2630
if(nrow(errors) > 0) {
27-
cli::cli_warn("Errors were encountered in {nrow(errors)} out of {nrow(res)} evaluated predictions. The problems occurred in patient(s) {unique(errors$id)}.")
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)}.")
2832
}
29-
out <- res |>
33+
out <- .res |>
3034
tidyr::pivot_longer(
3135
cols = c("pred", "map_ipred", "iter_ipred"), names_to = "type"
3236
) |>

R/plot.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ plot.mipdeval_results <- function(x, type = "vpc", ...) {
2020
plot_fun(x, ...)
2121
}
2222

23-
plot_vpc <- function(res, ...) {
23+
plot_vpc <- function(.res, ...) {
2424
rlang::check_installed("vpc", reason = "for VPC plotting.")
2525
rlang::check_dots_used()
2626
vpc::vpc(
27-
sim = res$sim,
28-
obs = dplyr::filter(res$data, .data$EVID == 0),
27+
sim = .res$sim,
28+
obs = dplyr::filter(.res$data, .data$EVID == 0),
2929
...
3030
)
3131
}

R/run_eval_core.R

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ run_eval_core <- function(
7474
.fit_options
7575
)
7676
)
77+
78+
## Data frame with predictive data
79+
pred_data <- tibble::tibble(
80+
id = obs_data$id,
81+
t = obs_data$t,
82+
dv = fit$dv,
83+
ipred = fit$ipred,
84+
ires = fit$ires,
85+
iwres = fit$iwres,
86+
pred = fit$pred,
87+
res = fit$res,
88+
wres = fit$wres,
89+
cwres = fit$cwres,
90+
ofv = fit$fit$value,
91+
ss_w = ss(fit$dv, fit$ipred, weights),
92+
`_iteration` = iterations[i],
93+
`_grouper` = obs_data$`_grouper`
94+
)
7795
if(inherits(fit, "error")) {
7896
## create NA records for this fit
7997
pred_data <- tibble::tibble(
@@ -97,7 +115,12 @@ run_eval_core <- function(
97115
t = obs_data$t,
98116
dv = fit$dv,
99117
ipred = fit$ipred,
118+
ires = fit$ires,
119+
iwres = fit$iwres,
100120
pred = fit$pred,
121+
res = fit$res,
122+
wres = fit$wres,
123+
cwres = fit$cwres,
101124
ofv = fit$fit$value,
102125
ss_w = ss(fit$dv, fit$ipred, weights),
103126
`_iteration` = iterations[i],
@@ -136,7 +159,12 @@ run_eval_core <- function(
136159
t = obs_data$t,
137160
dv = fit_map$dv,
138161
ipred = fit_map$ipred,
162+
ires = fit_map$ires,
163+
iwres = fit_map$iwres,
139164
pred = fit_map$pred,
165+
res = fit_map$res,
166+
wres = fit_map$wres,
167+
cwres = fit_map$cwres,
140168
ofv = fit_map$fit$value,
141169
ss_w = ss(fit_map$dv, fit_map$ipred, w = NULL),
142170
`_iteration` = iterations[i],
@@ -173,9 +201,8 @@ run_eval_core <- function(
173201
apriori = (.data$`_iteration` == 0)
174202
) |>
175203
dplyr::select(
176-
"id", "_iteration", "_grouper", "t", "dv", "pred", "map_ipred",
177-
"ofv", "ss_w",
178-
"iter_ipred", "apriori",
204+
"id", "_iteration", "_grouper", "t", "dv", "pred", "res", "wres", "cwres",
205+
"map_ipred", "ofv", "ss_w", "iter_ipred", "ires", "iwres", "apriori",
179206
!!names(mod_obj$parameters)
180207
)
181208

man/calculate_bayesian_impact.Rd

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

man/calculate_shrinkage.Rd

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

man/calculate_stats.Rd

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

tests/testthat/test-run_eval.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ test_that("Basic run with vanco data + model works", {
2222
expect_s3_class(res$stats_summ, c("tbl_df", "tbl", "data.frame"))
2323
expect_equal(
2424
names(res$results),
25-
c("id", "_iteration", "_grouper", "t", "dv", "pred", "map_ipred",
26-
"ofv", "ss_w", "iter_ipred", "apriori", "CL", "V", "TH_CRCL",
27-
"Q", "V2")
25+
c("id", "_iteration", "_grouper", "t", "dv", "pred", "res", "wres", "cwres",
26+
"map_ipred", "ofv", "ss_w", "iter_ipred", "ires", "iwres", "apriori", "CL",
27+
"V", "TH_CRCL", "Q", "V2")
2828
)
2929
expect_equal(
3030
round(res$results$CL[1:5], 3),

0 commit comments

Comments
 (0)