From a9ba38b005c52ae7b27fb601bfdec358154c7230 Mon Sep 17 00:00:00 2001 From: Frank Weber <55132727+fweber144@users.noreply.github.com> Date: Mon, 24 Jan 2022 15:31:46 +0100 Subject: [PATCH] Fix issue #212 (#267) * Minor cleaning (the use of `[,]` makes things just more complicated as dimensions could be dropped accidentally). * Fix issue #212. --- R/cv_varsel.R | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/R/cv_varsel.R b/R/cv_varsel.R index 95359b15d..c21a2ecd1 100644 --- a/R/cv_varsel.R +++ b/R/cv_varsel.R @@ -404,7 +404,12 @@ loo_varsel <- function(refmodel, method, nterms_max, ndraws, r_eff = rep(1, ncol(log_lik_sub))) ) lw_sub <- suppressWarnings(loo::weights.importance_sampling(sub_psisloo)) - loo_sub[[k]][inds] <- apply(log_lik_sub[,] + lw_sub[,], 2, log_sum_exp) + # Take into account that clustered draws usually have different weights: + lw_sub <- lw_sub + log(p_pred$weights) + # This re-weighting requires a re-normalization (as.array() is applied to + # have stricter consistency checks, see `?sweep`): + lw_sub <- sweep(lw_sub, 2, as.array(apply(lw_sub, 2, log_sum_exp))) + loo_sub[[k]][inds] <- apply(log_lik_sub + lw_sub, 2, log_sum_exp) for (i in seq_along(inds)) { mu_sub[[k]][inds[i]] <- mu_k[i, ] %*% exp(lw_sub[, i]) }