Skip to content

Commit c056b03

Browse files
authored
Merge pull request #510 from utkarshpawade/test/ppc-error-loo-data-coverage
Add test coverage for ppc_error_data and ppc_loo_pit_data
2 parents bd8b804 + 13caa93 commit c056b03

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ release-prep.R
1616

1717
# vscode/positron/etc settings
1818
.vscode/*
19+
Rplots.pdf

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# bayesplot (development version)
22

3+
* Added unit tests for `ppc_error_data()` and `ppc_loo_pit_data()` covering output structure, argument handling, and edge cases.
34
* Added vignette sections demonstrating `*_data()` companion functions for building custom ggplot2 visualizations (#435)
45
* Extract `drop_singleton_values()` helper in `mcmc_nuts_treedepth()` to remove duplicated filtering logic.
56
* Eliminate redundant data processing in `mcmc_areas_data()` by reusing the prepared MCMC array for both interval and density computation.

tests/testthat/test-ppc-errors.R

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
skip_if_not_installed("rstantools")
12
source(test_path("data-for-ppc-tests.R"))
23

34
test_that("ppc_error_hist and ppc_error_scatter return ggplot object", {
4-
skip_if_not_installed("rstantools")
55
expect_gg(ppc_error_hist(y, yrep[1:5, ], binwidth = 0.1))
66
expect_gg(ppc_error_scatter(y, yrep[1:5, ]))
77

@@ -13,14 +13,12 @@ test_that("ppc_error_hist and ppc_error_scatter return ggplot object", {
1313
})
1414

1515
test_that("ppc_error_hist_grouped returns ggplot object", {
16-
skip_if_not_installed("rstantools")
1716
expect_gg(ppc_error_hist_grouped(y, yrep[1:5, ], group, binwidth = 0.1))
1817
expect_gg(ppc_error_hist_grouped(y, yrep[1,, drop = FALSE], group,
1918
freq = FALSE, binwidth = 1))
2019
})
2120

2221
test_that("ppc_error_scatter_avg returns ggplot2 object", {
23-
skip_if_not_installed("rstantools")
2422
expect_gg(ppc_error_scatter_avg(y, yrep))
2523
expect_gg(ppc_error_scatter_avg(y, yrep[1:5, ]))
2624

@@ -30,7 +28,6 @@ test_that("ppc_error_scatter_avg returns ggplot2 object", {
3028
})
3129

3230
test_that("ppc_error_scatter_avg same as ppc_error_scatter if nrow(yrep) = 1", {
33-
skip_if_not_installed("rstantools")
3431
p1 <- ppc_error_scatter_avg(y2, yrep2)
3532
p2 <- ppc_error_scatter(y2, yrep2)
3633
d1 <- p1$data
@@ -42,8 +39,6 @@ test_that("ppc_error_scatter_avg same as ppc_error_scatter if nrow(yrep) = 1", {
4239
})
4340

4441
test_that("ppc_error_scatter_avg_vs_x returns ggplot2 object", {
45-
skip_if_not_installed("rstantools")
46-
4742
# expect warning
4843
expect_warning(expect_gg(ppc_error_scatter_avg_vs_x(y, yrep, x = rnorm(length(y)))),
4944
"'ppc_error_scatter_avg_vs_x' is deprecated.")
@@ -52,7 +47,6 @@ test_that("ppc_error_scatter_avg_vs_x returns ggplot2 object", {
5247
})
5348

5449
test_that("ppc_error_binned returns ggplot object", {
55-
skip_if_not_installed("rstantools")
5650
load(test_path("data-for-binomial.rda"))
5751
expect_gg(ppc_error_binned(y, Ey))
5852
expect_gg(ppc_error_binned(y[1:5], Ey[, 1:5]))
@@ -73,6 +67,24 @@ test_that("bin_errors works for edge cases", {
7367
expect_equal(ans, val)
7468
})
7569

70+
# ppc_error_data tests -----------------------------------------------------
71+
72+
test_that("ppc_error_data returns exact structure and computed errors", {
73+
d <- ppc_error_data(y, yrep)
74+
expect_named(d, c("y_id", "y_name", "y_obs", "rep_id", "rep_label", "value"))
75+
third_rep <- d[d$rep_id == 3, ]
76+
expected_errors <- y - yrep[3, ]
77+
expect_equal(third_rep$value, expected_errors)
78+
expect_equal(third_rep$y_obs, y)
79+
})
80+
81+
test_that("ppc_error_data with group returns exact structure", {
82+
d <- ppc_error_data(y, yrep, group = group)
83+
expect_named(d, c("group", "y_id", "y_name", "y_obs", "rep_id", "rep_label", "value"))
84+
expect_identical(levels(d$group), levels(group))
85+
expect_equal(d$group[d$rep_id == 1], group)
86+
})
87+
7688

7789
# Visual tests -----------------------------------------------------------------
7890

tests/testthat/test-ppc-loo.R

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,50 @@ test_that("ppc_loo_pit_ecdf renders correctly", {
352352
)
353353
vdiffr::expect_doppelganger("ppc_loo_pit_ecdf (ecdf difference)", p_custom)
354354
})
355+
356+
357+
# ppc_loo_pit_data tests ---------------------------------------------------
358+
359+
test_that("ppc_loo_pit_data returns the expected structure for both boundary modes", {
360+
set.seed(123)
361+
pit_vals <- runif(50)
362+
n_samples <- 10
363+
expect_message(
364+
d_raw <- ppc_loo_pit_data(
365+
pit = pit_vals,
366+
boundary_correction = FALSE,
367+
samples = n_samples
368+
),
369+
"pit"
370+
)
371+
expect_s3_class(d_raw, "data.frame")
372+
expect_named(
373+
d_raw,
374+
c("y_id", "y_name", "rep_id", "rep_label", "is_y", "is_y_label", "value")
375+
)
376+
y_rows <- d_raw[d_raw$is_y, ]
377+
yrep_rows <- d_raw[!d_raw$is_y, ]
378+
expect_equal(nrow(y_rows), length(pit_vals))
379+
expect_equal(nrow(yrep_rows), length(pit_vals) * n_samples)
380+
expect_equal(y_rows$value, pit_vals)
381+
382+
grid_len <- 128
383+
expect_message(
384+
d_bc <- ppc_loo_pit_data(
385+
pit = pit_vals,
386+
boundary_correction = TRUE,
387+
samples = n_samples,
388+
grid_len = grid_len
389+
),
390+
"pit"
391+
)
392+
expect_named(
393+
d_bc,
394+
c("y_id", "y_name", "rep_id", "rep_label", "is_y", "is_y_label", "value", "x")
395+
)
396+
y_rows <- d_bc[d_bc$is_y, ]
397+
yrep_rows <- d_bc[!d_bc$is_y, ]
398+
expect_equal(nrow(y_rows), grid_len)
399+
expect_equal(nrow(yrep_rows), grid_len * n_samples)
400+
expect_false(anyNA(d_bc$x))
401+
})

0 commit comments

Comments
 (0)