From 80e3142de4244c7fecee3cfff0c3a129f8e1942c Mon Sep 17 00:00:00 2001 From: Jake Thompson Date: Mon, 29 Jan 2024 10:37:41 -0600 Subject: [PATCH] make prediction summary more efficient --- R/utils-methods.R | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/R/utils-methods.R b/R/utils-methods.R index b968dd3..fff74b1 100644 --- a/R/utils-methods.R +++ b/R/utils-methods.R @@ -103,27 +103,29 @@ summarize_probs <- function(x, probs, id, optim) { sum_frame <- x %>% dplyr::mutate(dplyr::across(dplyr::where(posterior::is_rvar), - ~lapply(.x, summarize_rvar, probs = probs))) %>% + ~lapply(.x, summarize_rvar, probs = probs, + optim = optim))) %>% tidyr::pivot_longer(cols = dplyr::all_of(summary_names), names_to = type, values_to = "summary") %>% tidyr::unnest("summary") - if (optim) { - sum_frame <- sum_frame %>% - dplyr::select(!!id, !!type, "probability") - } - return(sum_frame) } -summarize_rvar <- function(rv, probs) { - tibble::tibble(probability = E(rv), - bounds = tibble::as_tibble_row( - stats::quantile(rv, probs = probs, names = TRUE), - .name_repair = ~paste0(probs * 100, "%") - )) %>% - tidyr::unnest("bounds") +summarize_rvar <- function(rv, probs, optim) { + ret_frame <- if (optim) { + tibble::tibble(probability = E(rv)) + } else { + tibble::tibble(probability = E(rv), + bounds = tibble::as_tibble_row( + stats::quantile(rv, probs = probs, names = TRUE), + .name_repair = ~paste0(probs * 100, "%") + )) %>% + tidyr::unnest("bounds") + } + + return(ret_frame) } calculate_probs_no_summary <- function(ret_list, method) {