From 55518e0830f27969262964a4a43c2fafab3f767d Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 25 Feb 2026 01:02:37 +0000 Subject: [PATCH] Optimize marg_pabs in R/utils.R Replaced inefficient loop-based dataframe construction and `bind_rows` in `marg_pabs` with vectorized subsetting. This significantly improves performance and removes the dependency on `dplyr` for this function. The logic for `est_samples` (posterior draws) remains consistent with the original implementation: `est_samples` is flattened column-wise (draws for observation 1, then observation 2, etc.), which corresponds to the vectorized expansion of `newdata` (row 1 repeated n_sim times, then row 2 repeated n_sim times). This change improves performance by avoiding repeated memory allocations and dataframe binding operations. It also makes the function more robust by relying on base R instead of the suggested `dplyr` package. Co-authored-by: lcgodoy <24809942+lcgodoy@users.noreply.github.com> --- R/utils.R | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/R/utils.R b/R/utils.R index 3bd6dfc..9207237 100644 --- a/R/utils.R +++ b/R/utils.R @@ -222,18 +222,15 @@ marg_pabs <- function(drm, newdata) { drm$stanfit$draws(variables = c("beta_t"), format = "draws_matrix") n_sim <- NROW(est_samples) - output <- vector(mode = "list", length = NROW(newdata)) est_samples <- tcrossprod(est_samples, new_x) est_samples <- stats::plogis(c(est_samples)) - rownames(newdata) <- NULL - for (i in seq_along(output)) { - output[[i]] <- - cbind.data.frame(newdata[i, , drop = FALSE], iter = seq_len(n_sim)) |> - suppressWarnings() - } - output <- dplyr::bind_rows(output) - output <- dplyr::mutate(output, prob_abs = est_samples) + n_obs <- nrow(newdata) + idx <- rep(seq_len(n_obs), each = n_sim) + output <- newdata[idx, , drop = FALSE] + output$iter <- rep(seq_len(n_sim), times = n_obs) + output$prob_abs <- est_samples + rownames(output) <- NULL return(output) }