Skip to content

Commit 918e0de

Browse files
committed
allow negative integer input to ppc_bars, ppc_bars_grouped
closes #172
1 parent db2e4fc commit 918e0de

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

R/helpers-ppc.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,12 @@ is_whole_number <- function(x, tol = .Machine$double.eps) {
243243
}
244244
}
245245

246-
# check if all values in x are counts (non-negative whole numbers)
246+
# check if all values in x are whole numbers or counts (non-negative whole numbers)
247+
all_whole_number <- function(x, ...) {
248+
all(is_whole_number(x, ...))
249+
}
247250
all_counts <- function(x, ...) {
248-
all(is_whole_number(x, ...)) && min(x) >= 0
251+
all_whole_number(x, ...) && min(x) >= 0
249252
}
250253

251254
# labels ----------------------------------------------------------------

R/ppc-discrete.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ ppc_bars <-
103103
check_ignored_arguments(...)
104104
y <- validate_y(y)
105105
yrep <- validate_yrep(yrep, y)
106-
if (!all_counts(y))
107-
stop("ppc_bars expects only non-negative integers in 'y'.")
108-
if (!all_counts(yrep))
109-
stop("ppc_bars expects only non-negative integers in 'yrep'.")
106+
if (!all_whole_number(y))
107+
stop("ppc_bars expects 'y' to be discrete.")
108+
if (!all_whole_number(yrep))
109+
stop("ppc_bars expects 'yrep' to be discrete.")
110110

111111
alpha <- (1 - prob) / 2
112112
probs <- sort(c(alpha, 0.5, 1 - alpha))
@@ -145,10 +145,10 @@ ppc_bars_grouped <-
145145
y <- validate_y(y)
146146
yrep <- validate_yrep(yrep, y)
147147
group <- validate_group(group, y)
148-
if (!all_counts(y))
149-
stop("ppc_bars expects only non-negative integers in 'y'.")
150-
if (!all_counts(yrep))
151-
stop("ppc_bars expects only non-negative integers in 'yrep'.")
148+
if (!all_whole_number(y))
149+
stop("ppc_bars_grouped expects 'y' to be discrete.")
150+
if (!all_whole_number(yrep))
151+
stop("ppc_bars_grouped expects 'yrep' to be discrete.")
152152

153153
alpha <- (1 - prob) / 2
154154
probs <- sort(c(alpha, 0.5, 1 - alpha))

tests/testthat/test-extractors.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ test_that("neff_ratio.stanreg returns correct structure", {
7979
test_that("rhat.stanfit returns correct structure", {
8080
r <- rhat(fit$stanfit)
8181
expect_named(r)
82+
r <- r[!names(r) %in% "mean_PPD"]
8283
expect_equal(r, summary(fit)[, "Rhat"])
8384

8485
r2 <- rhat(fit$stanfit, pars = c("wt", "sigma"))
@@ -91,6 +92,7 @@ test_that("neff_ratio.stanreg returns correct structure", {
9192

9293
ratio <- neff_ratio(fit$stanfit)
9394
expect_named(ratio)
95+
ratio <- ratio[!names(ratio) %in% "mean_PPD"]
9496
ans <- summary(fit)[, "n_eff"] / denom
9597
expect_equal(ratio, ans, tol = 0.001)
9698

tests/testthat/test-ppc-discrete.R

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,21 @@ test_that("freq argument to ppc_bars works", {
3030
expect_true(all(y_prop < 1) && all(y_prop > 0))
3131
})
3232

33-
test_that("ppc_bars errors if y/yrep not natural numbers", {
33+
test_that("ppc_bars works with negative integers", {
34+
y <- round(rnorm(100, -10, 1))
35+
yrep <- round(matrix(rnorm(100 * 500, -10, 1), 500, 100))
36+
expect_gg(ppc_bars(y, yrep))
37+
})
38+
39+
test_that("ppc_bars(_grouped) errors if y/yrep not discrete", {
3440
expect_error(ppc_bars(y + 0.5, yrep),
35-
"ppc_bars expects only non-negative integers in 'y'")
41+
"ppc_bars expects 'y' to be discrete")
3642
expect_error(ppc_bars(y, yrep + 0.5),
37-
"ppc_bars expects only non-negative integers in 'yrep'")
43+
"ppc_bars expects 'yrep' to be discrete")
44+
expect_error(ppc_bars_grouped(y + 0.5, yrep, group = esoph$agegp),
45+
"ppc_bars_grouped expects 'y' to be discrete")
46+
expect_error(ppc_bars_grouped(y, yrep + 0.5, group = esoph$agegp),
47+
"ppc_bars_grouped expects 'yrep' to be discrete")
3848
})
3949

4050

0 commit comments

Comments
 (0)