Skip to content

Commit 4af00d0

Browse files
committed
Extend the changes also to the ppc_loo functions.
1 parent 652eca8 commit 4af00d0

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

NEWS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ gains an argument `discrete`, which is `FALSE` by default, but can be used to ma
1919
Geom more appropriate for discrete data. (#145)
2020

2121
* [PPC intervals plots](http://mc-stan.org/bayesplot/reference/PPC-intervals.html)
22+
and [LOO predictive checks](http://mc-stan.org/bayesplot/reference/PPC-loo.html)
2223
now draw both an outer and an inner probability interval, which can be
2324
controlled through the new argument `prob_outer` and the already existing
24-
`prob`. The default value of the outer interval has been changed from 1.0 to
25-
0.9, consistently with what is produced by `mcmc_intervals()` and similar.
25+
`prob`, consistently with what is produced by `mcmc_intervals()` and similar.
2626
(#154, @mcol)
2727

2828
# bayesplot 1.5.0

R/ppc-loo.R

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,18 @@ ppc_loo_pit <-
269269
#' object returned by the \code{psis} function (or by the \code{loo} function
270270
#' with argument \code{save_psis} set to \code{TRUE}).
271271
#' @param prob A value between 0 and 1 indicating the desired probability mass
272-
#' to include in the intervals. The default is 0.9.
272+
#' to include in the inner interval. The default is 0.5.
273+
#' @param prob_outer The probability mass to include in the outer interval.
274+
#' The default is 0.9.
273275
#' @param intervals For \code{ppc_loo_intervals} and \code{ppc_loo_ribbon},
274-
#' optionally a matrix of precomputed LOO predictive intervals intervals with
276+
#' optionally a matrix of precomputed LOO predictive intervals
275277
#' that can be specified instead of \code{yrep} and \code{lw} (these are both
276278
#' ignored if \code{intervals} is specified). If not specified the intervals
277279
#' are computed internally before plotting. If specified, \code{intervals}
278280
#' must be a matrix with number of rows equal to the number of data points and
279-
#' three columns in the following order: the first for the lower bound of the
280-
#' interval, the second for median (50\%), and the third for the interval
281-
#' upper bound (column names are ignored).
281+
#' five columns in the following order: lower outer interval, lower inner
282+
#' interval, median (50\%), upper inner interval and upper outer interval
283+
#' (column names are ignored).
282284
#' @param order For \code{ppc_loo_intervals}, a string indicating how to arrange
283285
#' the plotted intervals. The default (\code{"index"}) is to plot them in the
284286
#' order of the observations. The alternative (\code{"median"}) arranges them
@@ -300,7 +302,8 @@ ppc_loo_intervals <-
300302
subset = NULL,
301303
intervals = NULL,
302304
...,
303-
prob = 0.9,
305+
prob = 0.5,
306+
prob_outer = 0.9,
304307
size = 1,
305308
fatten = 3,
306309
order = c("index", "median")) {
@@ -309,11 +312,14 @@ ppc_loo_intervals <-
309312
y <- validate_y(y)
310313
order_by_median <- match.arg(order) == "median"
311314
if (!is.null(intervals)) {
312-
stopifnot(is.matrix(intervals), ncol(intervals) == 3)
315+
stopifnot(is.matrix(intervals), ncol(intervals) %in% c(3, 5))
313316
message(
314317
"'intervals' specified so ignoring ",
315318
"'yrep', 'psis_object', 'subset', if specified."
316319
)
320+
if (ncol(intervals) == 3) {
321+
intervals <- cbind(intervals[, 1], intervals, intervals[, 3])
322+
}
317323
} else {
318324
suggested_package("loo", min_version = "2.0.0")
319325
yrep <- validate_yrep(yrep, y)
@@ -323,7 +329,8 @@ ppc_loo_intervals <-
323329
yrep <- yrep[, subset, drop=FALSE]
324330
psis_object <- .psis_subset(psis_object, subset)
325331
}
326-
a <- (1 - prob) / 2
332+
probs <- sort(c(prob, prob_outer))
333+
a <- (1 - probs) / 2
327334
stopifnot(identical(dim(psis_object), dim(yrep)))
328335
intervals <- suppressWarnings(t(loo::E_loo(
329336
x = yrep,
@@ -367,17 +374,21 @@ ppc_loo_ribbon <-
367374
subset = NULL,
368375
intervals = NULL,
369376
...,
370-
prob = 0.9,
377+
prob = 0.5,
378+
prob_outer = 0.9,
371379
alpha = 0.33,
372380
size = 0.25) {
373381
check_ignored_arguments(...)
374382
y <- validate_y(y)
375383
if (!is.null(intervals)) {
376-
stopifnot(is.matrix(intervals), ncol(intervals) == 3)
384+
stopifnot(is.matrix(intervals), ncol(intervals) %in% c(3, 5))
377385
message(
378386
"'intervals' specified so ignoring ",
379387
"'yrep', 'psis_object', 'subset', if specified."
380388
)
389+
if (ncol(intervals) == 3) {
390+
intervals <- cbind(intervals[, 1], intervals, intervals[, 3])
391+
}
381392
} else {
382393
suggested_package("loo", min_version = "2.0.0")
383394
yrep <- validate_yrep(yrep, y)
@@ -387,7 +398,8 @@ ppc_loo_ribbon <-
387398
yrep <- yrep[, subset, drop=FALSE]
388399
psis_object <- .psis_subset(psis_object, subset)
389400
}
390-
a <- (1 - prob) / 2
401+
probs <- sort(c(prob, prob_outer))
402+
a <- (1 - probs) / 2
391403
stopifnot(identical(dim(psis_object), dim(yrep)))
392404
intervals <- suppressWarnings(t(loo::E_loo(
393405
x = yrep,
@@ -416,9 +428,11 @@ ppc_loo_ribbon <-
416428
y_id = seq_along(y),
417429
y_obs = y,
418430
x = x,
419-
lo = intervals[, 1],
420-
mid = intervals[, 2],
421-
hi = intervals[, 3])
431+
ll = intervals[, 1],
432+
l = intervals[, 2],
433+
m = intervals[, 3],
434+
h = intervals[, 4],
435+
hh = intervals[, 5])
422436
}
423437

424438
# subset a psis_object without breaking it

man/PPC-loo.Rd

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

tests/testthat/test-ppc-loo.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ test_that("ppc_loo_ribbon returns ggplot object", {
7272
})
7373

7474
test_that("ppc_loo_intervals/ribbon work when 'intervals' specified", {
75-
intervals <- t(apply(yrep, 2, quantile, probs = c(0.1, 0.5, 0.9)))
75+
intervals <- t(apply(yrep, 2, quantile, probs = c(0.1, 0.25, 0.5, 0.75, 0.9)))
7676
expect_gg(ppc_loo_intervals(y, intervals = intervals))
7777
expect_gg(ppc_loo_ribbon(y, intervals = intervals))
7878
expect_message(ppc_loo_ribbon(y, intervals = intervals),
@@ -81,6 +81,12 @@ test_that("ppc_loo_intervals/ribbon work when 'intervals' specified", {
8181
"'intervals' specified so ignoring 'yrep', 'psis_object', 'subset', if specified")
8282
})
8383

84+
test_that("ppc_loo_intervals/ribbon work when 'intervals' has 3 columns", {
85+
intervals <- t(apply(yrep, 2, quantile, probs = c(0.1, 0.5, 0.9)))
86+
expect_gg(ppc_loo_intervals(y, intervals = intervals))
87+
expect_gg(ppc_loo_ribbon(y, intervals = intervals))
88+
})
89+
8490
test_that("errors if dimensions of yrep and lw don't match", {
8591
expect_error(
8692
ppc_loo_pit_overlay(y, yrep, lw[, 1:5]),

0 commit comments

Comments
 (0)