Skip to content

Commit c683a1e

Browse files
authored
Merge pull request #638 from stan-dev/r_eff-false-by-default
Default to not computing r_eff for loo
2 parents 1250d1e + 8f140f5 commit c683a1e

1 file changed

Lines changed: 35 additions & 15 deletions

File tree

R/loo.R

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@
5959
#' @param k_threshold Threshold for flagging estimates of the Pareto shape
6060
#' parameters \eqn{k} estimated by \code{loo}. See the \emph{How to proceed
6161
#' when \code{loo} gives warnings} section, below, for details.
62-
#'
62+
#' @param r_eff \code{TRUE} or \code{FALSE} indicating whether to compute the
63+
#' \code{r_eff} argument to pass to the \pkg{loo} package. If \code{TRUE},
64+
#' \pkg{rstanarm} will call \code{\link[loo]{relative_eff}} to compute the
65+
#' \code{r_eff} argument to pass to the \pkg{loo} package. If \code{FALSE}
66+
#' (the default), we avoid computing \code{r_eff}, which can be very slow.
67+
#' \code{r_eff} measures the amount of autocorrelation in MCMC draws, and is
68+
#' used to compute more accurate ESS and MCSE estimates for pointwise and
69+
#' total ELPDs. When \code{r_eff=FALSE}, the reported ESS and MCSE estimates
70+
#' may be over-optimistic if the posterior draws are far from independent.
6371
#' @return The structure of the objects returned by \code{loo} and \code{waic}
6472
#' methods are documented in detail in the \strong{Value} section in
6573
#' \code{\link[loo]{loo}} and \code{\link[loo]{waic}} (from the \pkg{loo}
@@ -76,10 +84,10 @@
7684
#' distributional assumption and integrate over uncertainty in the parameters.
7785
#' This only assumes that any one observation can be omitted without having a
7886
#' major effect on the posterior distribution, which can be judged using the
79-
#' diagnostic plot provided by the \code{\link[loo:pareto-k-diagnostic]{plot.loo}} method and the
80-
#' warnings provided by the \code{\link[loo]{print.loo}} method (see the
81-
#' \emph{How to Use the rstanarm Package} vignette for an example of this
82-
#' process).
87+
#' diagnostic plot provided by the
88+
#' \code{\link[loo:pareto-k-diagnostic]{plot.loo}} method and the warnings
89+
#' provided by the \code{\link[loo]{print.loo}} method (see the \emph{How to
90+
#' Use the rstanarm Package} vignette for an example of this process).
8391
#'
8492
#' \subsection{How to proceed when \code{loo} gives warnings (k_threshold)}{
8593
#' The \code{k_threshold} argument to the \code{loo} method for \pkg{rstanarm}
@@ -111,7 +119,7 @@
111119
#'
112120
#' @seealso
113121
#' \itemize{
114-
#' \item The new \href{https://mc-stan.org/loo/articles/}{\pkg{loo} package vignettes}
122+
#' \item The \href{https://mc-stan.org/loo/articles/}{\pkg{loo} package vignettes}
115123
#' and various \href{https://mc-stan.org/rstanarm/articles/}{\pkg{rstanarm} vignettes}
116124
#' for more examples using \code{loo} and related functions with \pkg{rstanarm} models.
117125
#' \item \code{\link[loo]{pareto-k-diagnostic}} in the \pkg{loo} package for
@@ -184,9 +192,15 @@ loo.stanreg <-
184192
...,
185193
cores = getOption("mc.cores", 1),
186194
save_psis = FALSE,
187-
k_threshold = NULL) {
188-
if (model_has_weights(x))
195+
k_threshold = NULL,
196+
r_eff = FALSE) {
197+
if (model_has_weights(x)) {
189198
recommend_exact_loo(reason = "model has weights")
199+
}
200+
201+
if (!r_eff) {
202+
r_eff <- NULL
203+
}
190204

191205
user_threshold <- !is.null(k_threshold)
192206
if (user_threshold) {
@@ -196,9 +210,9 @@ loo.stanreg <-
196210
}
197211

198212

199-
if (used.sampling(x)) # chain_id to pass to loo::relative_eff
213+
if (used.sampling(x)) {# chain_id to pass to loo::relative_eff
200214
chain_id <- chain_id_for_loo(x)
201-
else { # ir_idx to pass to ...
215+
} else { # ir_idx to pass to ...
202216
if (exists("ir_idx",x)) {
203217
ir_idx <- x$ir_idx
204218
} else if ("diagnostics" %in% names(x$stanfit@sim) &
@@ -212,7 +226,9 @@ loo.stanreg <-
212226

213227
if (is.stanjm(x)) {
214228
ll <- log_lik(x)
215-
r_eff <- loo::relative_eff(exp(ll), chain_id = chain_id, cores = cores)
229+
if (!is.null(r_eff)) {
230+
r_eff <- loo::relative_eff(exp(ll), chain_id = chain_id, cores = cores)
231+
}
216232
loo_x <-
217233
suppressWarnings(loo.matrix(
218234
ll,
@@ -223,7 +239,9 @@ loo.stanreg <-
223239
} else if (is.stanmvreg(x)) {
224240
M <- get_M(x)
225241
ll <- do.call("cbind", lapply(1:M, function(m) log_lik(x, m = m)))
226-
r_eff <- loo::relative_eff(exp(ll), chain_id = chain_id, cores = cores)
242+
if (!is.null(r_eff)) {
243+
r_eff <- loo::relative_eff(exp(ll), chain_id = chain_id, cores = cores)
244+
}
227245
loo_x <-
228246
suppressWarnings(loo.matrix(
229247
ll,
@@ -242,7 +260,9 @@ loo.stanreg <-
242260
)
243261
ll <- ll[,!cons, drop = FALSE]
244262
}
245-
r_eff <- loo::relative_eff(exp(ll), chain_id = chain_id, cores = cores)
263+
if (!is.null(r_eff)) {
264+
r_eff <- loo::relative_eff(exp(ll), chain_id = chain_id, cores = cores)
265+
}
246266
loo_x <-
247267
suppressWarnings(loo.matrix(
248268
ll,
@@ -256,7 +276,7 @@ loo.stanreg <-
256276
likfun <- function(data_i, draws) {
257277
exp(llfun(data_i, draws))
258278
}
259-
if (used.sampling(x)) {
279+
if (used.sampling(x) && !is.null(r_eff)) {
260280
r_eff <- loo::relative_eff(
261281
# using function method
262282
x = likfun,
@@ -266,7 +286,7 @@ loo.stanreg <-
266286
cores = cores,
267287
...
268288
)
269-
} else {
289+
} else if (!used.sampling(x) && !is.null(r_eff)) {
270290
w_ir <- as.numeric(table(ir_idx))/length(ir_idx)
271291
ir_uidx <- which(!duplicated(ir_idx))
272292
draws <- args$draws

0 commit comments

Comments
 (0)