Skip to content

Commit 38b982b

Browse files
authored
Merge pull request #506 from utkarshpawade/test/ppc-distribution-data-coverage
Add test coverage for ppc_data and ppd_data
2 parents 471afa8 + 0129894 commit 38b982b

2 files changed

Lines changed: 94 additions & 3 deletions

File tree

NEWS.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* Default to `quantiles=100` for all dot plots by @behramulukir (#402)
2121
* Use `"neff_ratio"` consistently in diagnostic color scale helpers to avoid relying on partial matching of `"neff"`.
2222
* Replace `expand = c(mult, add)` with `ggplot2::expansion()` helper in scale functions for consistency with ggplot2 >= 3.3.0 style.
23-
* Replace uses of `geom_bar(stat = "identity")` with the more idiomatic ggplot2 form `geom_col()`
23+
* Replace uses of `geom_bar(stat = "identity")` with the more idiomatic ggplot2 form `geom_col()`
2424
* New function `ppc_rootogram_grouped` for grouped rootogram plots by @behramulukir and @jgabry (#419)
2525

2626
# bayesplot 1.15.0
@@ -37,7 +37,7 @@
3737

3838
# bayesplot 1.14.0
3939

40-
* PPC "avg" functions (`ppc_scatter_avg()`, `ppc_error_scatter_avg()`, etc.) gain a `stat` argument
40+
* PPC "avg" functions (`ppc_scatter_avg()`, `ppc_error_scatter_avg()`, etc.) gain a `stat` argument
4141
to set the averaging function. (Suggestion of #348, @kruschke).
4242
* `ppc_error_scatter_avg_vs_x(x = some_expression)` labels the x axis with `some_expression`.
4343
* New quantile dot plot functions `ppc_dots()` and `ppd_dots()` by @behramulukir (#357)
@@ -59,7 +59,7 @@
5959

6060
* Expand checking workflows to more platforms by @andrjohns (#324)
6161
* Skip tests depending on Suggested dependency rstantools if not installed by @MichaelChirico (#325)
62-
* Skip tests depending on Suggested dependency gridExtra if not installed by @MichaelChirico (#326)
62+
* Skip tests depending on Suggested dependency gridExtra if not installed by @MichaelChirico (#326)
6363
* Fix missing legends for unobserved levels in rhat and neff plots (#328)
6464
* Document problems with `ppc_stat` with `stat="mean"` (#329)
6565
* Ensure rank overlay plot starts at 0 even if not all bins present, thanks @sims1253 (#332)

tests/testthat/test-ppc-distributions.R

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,97 @@ test_that("ppc_violin_grouped returns a ggplot object", {
129129
expect_gg(ppc_violin_grouped(y, yrep, group, y_draw = "both", y_jitter = 0.3))
130130
})
131131

132+
# ppc_data / ppd_data tests -----------------------------------------------
133+
134+
test_that("ppc_data returns the correct structure", {
135+
y_small <- c(10, 20)
136+
yrep_small <- rbind(c(11, 21), c(12, 22))
137+
138+
d <- ppc_data(y_small, yrep_small)
139+
140+
expect_s3_class(d, "data.frame")
141+
expect_named(d, c("y_id", "y_name", "rep_id", "rep_label",
142+
"is_y", "is_y_label", "value"))
143+
expect_equal(nrow(d), length(y_small) * (nrow(yrep_small) + 1))
144+
expect_equal(d$y_id, c(1L, 1L, 2L, 2L, 1L, 2L))
145+
expect_equal(as.character(d$y_name), c("1", "1", "2", "2", "1", "2"))
146+
expect_equal(d$rep_id, c(1L, 2L, 1L, 2L, NA, NA))
147+
expect_equal(d$is_y, c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE))
148+
expect_equal(d$value, c(11, 12, 21, 22, 10, 20))
149+
expect_equal(d$value[d$is_y], y_small)
150+
151+
first_level <- levels(d$rep_label)[1]
152+
expect_true(all(as.character(d$rep_label[d$is_y]) == first_level))
153+
expect_true(all(as.character(d$rep_label[!d$is_y]) != first_level))
154+
})
155+
156+
test_that("ppc_data carries group through correctly", {
157+
y_small <- c(10, 20)
158+
yrep_small <- rbind(c(11, 21), c(12, 22))
159+
group_small <- factor(c("a", "b"))
160+
161+
d <- ppc_data(y_small, yrep_small, group = group_small)
162+
163+
expect_named(d, c("group", "y_id", "y_name", "rep_id", "rep_label",
164+
"is_y", "is_y_label", "value"))
165+
expect_equal(as.character(d$group), c("a", "a", "b", "b", "a", "b"))
166+
expect_equal(as.character(d$group[d$is_y]), as.character(group_small))
167+
})
168+
169+
test_that("ppc_data handles a single replicate matrix", {
170+
y_small <- c(10, 20)
171+
yrep_small <- matrix(c(11, 21), nrow = 1)
172+
173+
d <- ppc_data(y_small, yrep_small)
174+
175+
expect_equal(sum(!d$is_y), length(y_small))
176+
expect_equal(d$rep_id[!d$is_y], c(1L, 1L))
177+
expect_equal(d$value[!d$is_y], c(11, 21))
178+
})
179+
180+
test_that("ppd_data returns the correct structure", {
181+
yrep_small <- rbind(c(11, 21), c(12, 22))
182+
183+
d <- ppd_data(yrep_small)
184+
185+
expect_s3_class(d, "data.frame")
186+
expect_named(d, c("y_id", "y_name", "rep_id", "rep_label", "value"))
187+
expect_equal(nrow(d), nrow(yrep_small) * ncol(yrep_small))
188+
expect_equal(d$y_id, c(1L, 1L, 2L, 2L))
189+
expect_equal(as.character(d$y_name), c("1", "1", "2", "2"))
190+
expect_equal(d$rep_id, c(1L, 2L, 1L, 2L))
191+
expect_equal(d$value, c(11, 12, 21, 22))
192+
expect_true(all(grepl("pred", levels(d$rep_label), fixed = TRUE)))
193+
})
194+
195+
test_that("ppd_data carries group through correctly", {
196+
yrep_small <- rbind(c(11, 21), c(12, 22))
197+
group_small <- factor(c("a", "b"))
198+
199+
d <- ppd_data(yrep_small, group = group_small)
200+
201+
expect_named(d, c("group", "y_id", "y_name", "rep_id", "rep_label", "value"))
202+
expect_equal(as.character(d$group), c("a", "a", "b", "b"))
203+
})
204+
205+
test_that("ppd_data carries observation names through to y_name", {
206+
yrep_named <- rbind(c(11, 21), c(12, 22))
207+
colnames(yrep_named) <- c("obs_a", "obs_b")
208+
209+
d <- ppd_data(yrep_named)
210+
211+
expect_equal(as.character(d$y_name), c("obs_a", "obs_a", "obs_b", "obs_b"))
212+
})
213+
214+
test_that("ppd_data handles a single replicate matrix", {
215+
yrep_small <- matrix(c(11, 21), nrow = 1)
216+
217+
d <- ppd_data(yrep_small)
218+
219+
expect_equal(nrow(d), ncol(yrep_small))
220+
expect_equal(d$rep_id, c(1L, 1L))
221+
expect_equal(d$value, c(11, 21))
222+
})
132223

133224

134225
# Visual tests -----------------------------------------------------------------

0 commit comments

Comments
 (0)