From 05c4684d9ceec1be9c3b53ef311d8a02539b7e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teemu=20S=C3=A4ilynoja?= Date: Wed, 9 Apr 2025 17:25:49 +0300 Subject: [PATCH 1/2] Fix missing counts in ppc_bars_data() --- DESCRIPTION | 1 + R/ppc-discrete.R | 1 + .../_snaps/ppc-discrete/ppc-bars-default.svg | 9 +++++--- .../ppc-discrete/ppc-bars-grouped-default.svg | 22 +++++++++++++------ .../ppc-bars-grouped-facet-args-prob-size.svg | 20 ++++++++++++----- .../ppc-bars-prob-0-33-width-size-fatten.svg | 9 +++++--- .../ppc-bars-width-size-fatten.svg | 9 +++++--- tests/testthat/test-ppc-discrete.R | 8 +++---- 8 files changed, 53 insertions(+), 26 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 677d81d1..8003c73d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -37,6 +37,7 @@ Imports: rlang (>= 0.3.0), stats, tibble (>= 2.0.0), + tidyr, tidyselect, utils Suggests: diff --git a/R/ppc-discrete.R b/R/ppc-discrete.R index 89f6ee00..199c394b 100644 --- a/R/ppc-discrete.R +++ b/R/ppc-discrete.R @@ -395,6 +395,7 @@ ppc_bars_data <- data <- reshape2::melt(tmp_data, id.vars = "group") %>% count(.data$group, .data$value, .data$variable) %>% + tidyr::complete(.data$group, .data$value, .data$variable, fill = list(n = 0)) %>% group_by(.data$variable, .data$group) %>% mutate(proportion = .data$n / sum(.data$n)) %>% ungroup() %>% diff --git a/tests/testthat/_snaps/ppc-discrete/ppc-bars-default.svg b/tests/testthat/_snaps/ppc-discrete/ppc-bars-default.svg index 4884ed5f..9e825ec3 100644 --- a/tests/testthat/_snaps/ppc-discrete/ppc-bars-default.svg +++ b/tests/testthat/_snaps/ppc-discrete/ppc-bars-default.svg @@ -29,17 +29,20 @@ + - - + + + - + + diff --git a/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-default.svg b/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-default.svg index 79b7916f..d9158435 100644 --- a/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-default.svg +++ b/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-default.svg @@ -29,17 +29,20 @@ + - - + + + - - + + + @@ -54,16 +57,21 @@ + + + - - + + + - + + diff --git a/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-facet-args-prob-size.svg b/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-facet-args-prob-size.svg index fc093aee..a9372d7a 100644 --- a/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-facet-args-prob-size.svg +++ b/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-facet-args-prob-size.svg @@ -29,17 +29,20 @@ + - - + + + - - + + + @@ -54,16 +57,21 @@ + + + - + + - + + diff --git a/tests/testthat/_snaps/ppc-discrete/ppc-bars-prob-0-33-width-size-fatten.svg b/tests/testthat/_snaps/ppc-discrete/ppc-bars-prob-0-33-width-size-fatten.svg index 800dd449..3fd563df 100644 --- a/tests/testthat/_snaps/ppc-discrete/ppc-bars-prob-0-33-width-size-fatten.svg +++ b/tests/testthat/_snaps/ppc-discrete/ppc-bars-prob-0-33-width-size-fatten.svg @@ -29,17 +29,20 @@ + - - + + + - + + diff --git a/tests/testthat/_snaps/ppc-discrete/ppc-bars-width-size-fatten.svg b/tests/testthat/_snaps/ppc-discrete/ppc-bars-width-size-fatten.svg index 9da25a91..c8dbda46 100644 --- a/tests/testthat/_snaps/ppc-discrete/ppc-bars-width-size-fatten.svg +++ b/tests/testthat/_snaps/ppc-discrete/ppc-bars-width-size-fatten.svg @@ -29,17 +29,20 @@ + - - + + + - + + diff --git a/tests/testthat/test-ppc-discrete.R b/tests/testthat/test-ppc-discrete.R index 2a1d5285..a5ddb622 100644 --- a/tests/testthat/test-ppc-discrete.R +++ b/tests/testthat/test-ppc-discrete.R @@ -68,15 +68,15 @@ test_that("ppc_bars_data includes all levels", { # yrep has more unique values than y d2 <- ppc_bars_data(y_ord2, yrep_ord) expect_equal(d2$x, 1:4) - expect_equal(d2$y_obs, c(NA, sum(tab[1:2]), tab[3:4])) + expect_equal(d2$y_obs, c(0, sum(tab[1:2]), tab[3:4])) # y has more unique values than yrep d3 <- ppc_bars_data(y_ord, yrep_ord2) expect_equal(d3$x, 1:4) expect_equal(d3$y_obs, tab) - expect_equivalent(d3$l[2], NA_real_) - expect_equivalent(d3$m[2], NA_real_) - expect_equivalent(d3$h[2], NA_real_) + expect_equivalent(d3$l[2], 0) + expect_equivalent(d3$m[2], 0) + expect_equivalent(d3$h[2], 0) }) From 8fe188829fcf5af0f86a5243b5d6d0f63ecb049e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teemu=20S=C3=A4ilynoja?= Date: Wed, 9 Apr 2025 17:30:48 +0300 Subject: [PATCH 2/2] Add #342 to news. --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 3a211788..bbed0ed9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,6 +9,7 @@ * Skip tests depending on Suggested dependency gridExtra if not installed by @MichaelChirico (#326) * Skip tests depending on Suggested dependency rstantools if not installed by @MichaelChirico (#325) * Update GitHub actions workflows (#338) +* Fix missing counts in `ppc_bars_data()` by @TeemuSailynoja, thanks @famuvie (#342) # bayesplot 1.11.1