Skip to content

Commit 31940cb

Browse files
committed
remove unused code and improve test coverage
1 parent e8e3d49 commit 31940cb

10 files changed

+53
-31
lines changed

NAMESPACE

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,3 @@ importFrom(dplyr,top_n)
206206
importFrom(dplyr,ungroup)
207207
importFrom(dplyr,vars)
208208
importFrom(ggplot2,"%+replace%")
209-
importFrom(ggridges,geom_density_ridges)
210-
importFrom(ggridges,geom_density_ridges2)

R/helpers-gg.R

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,6 @@ geom_ignore <- function(...) {
2121
show.legend = FALSE, inherit.aes = FALSE)
2222
}
2323

24-
#' Wrappers for ggridges
25-
#'
26-
#' The "area ridges" are for use in `mcmc_areas()`. The scale of 1 and the
27-
#' identity statistic prevent the ridges from overlapping.
28-
#' `geom_density_ridges2()` draws closed polygons.
29-
#'
30-
#' @importFrom ggridges geom_density_ridges geom_density_ridges2
31-
#' @noRd
32-
geom_area_ridges <- function(...) {
33-
ggridges::geom_density_ridges(..., stat = "identity", scale = .95)
34-
}
35-
36-
geom_area_ridges2 <- function(...) {
37-
ggridges::geom_density_ridges2(..., stat = "identity", scale = .95)
38-
}
39-
4024

4125
#' Add new aesthetic mappings to a list of aesthetic mappings
4226
#'

R/helpers-ppc.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ validate_predictions <- function(predictions, n_obs = NULL) {
107107
#' @return Either throws an error or returns a numeric vector.
108108
#' @noRd
109109
validate_pit <- function(pit) {
110+
if (anyNA(pit)) {
111+
abort("NAs not allowed in 'pit'.")
112+
}
113+
110114
stopifnot(is.numeric(pit))
111115

112116
if (!is_vector_or_1Darray(pit)) {
@@ -117,10 +121,6 @@ validate_pit <- function(pit) {
117121
abort("'pit' must only contain values between 0 and 1.")
118122
}
119123

120-
if (anyNA(pit)) {
121-
abort("NAs not allowed in 'pit'.")
122-
}
123-
124124
unname(pit)
125125
}
126126

@@ -508,7 +508,7 @@ get_interpolation_values <- function(N, K, L, prob) {
508508
".\n",
509509
"Try either setting a value of 'K' >= ",
510510
min(vals[vals$N <= N, ]$K),
511-
"or 'interpolate_adj' = FALSE.",
511+
" or 'interpolate_adj' = FALSE.",
512512
sep = ""
513513
))
514514
}

R/ppc-loo.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ ppc_loo_pit_overlay <- function(y,
165165
grid_len = grid_len
166166
)
167167

168-
if (all(data$value[data$is_y] %in% 0:1)) {
168+
if (!missing(y) && all(y %in% 0:1)) {
169169
warning(
170170
"This plot is not recommended for binary data. ",
171171
"For plots that are more suitable see ",
@@ -667,7 +667,7 @@ ppc_loo_ribbon <-
667667
}
668668

669669
if (grid_len < 100){
670-
grid_len = 100
670+
grid_len <- 100
671671
}
672672

673673
# Get relative frequency boundaries and counts for input vector

tests/testthat/test-aesthetics.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ test_that("theme_default creates ggplot theme", {
157157
expect_equal(thm2[["text"]][["size"]], 13)
158158
})
159159

160+
test_that("bayesplot_theme_set warns of missing theme elements", {
161+
dark2 <- ggplot2::theme_dark()
162+
dark2$line <- NULL
163+
expect_warning(
164+
bayesplot_theme_set(dark2),
165+
"New theme missing the following elements: line"
166+
)
167+
bayesplot_theme_set()
168+
})
169+
160170
test_that("bayesplot_theme_set/get work", {
161171
bayesplot_theme_set()
162172
expect_identical(bayesplot_theme_get(), default)

tests/testthat/test-helpers-ppc.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ test_that("get_interpolation_values catches impossible values", {
105105
get_interpolation_values(0, 1000, 4, .95),
106106
"No precomputed values to interpolate from for sample length of 0."
107107
)
108+
expect_error(
109+
get_interpolation_values(1e5, 10, 4, .95),
110+
"No precomputed values to interpolate from for sample length of 1e+05",
111+
fixed = TRUE
112+
)
113+
expect_error(
114+
get_interpolation_values(100, 300, 4, .95),
115+
"No precomputed values available for interpolation for 'K' = 300"
116+
)
108117
})
109118

110119
# ecdf_intervals ---------------------------------------------------------

tests/testthat/test-mcmc-diagnostics.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ test_that("rhat and neff plots return a ggplot object", {
2121
# named ok
2222
rhat <- setNames(runif(5, 1, 1.5), paste0("alpha[", 1:5, "]"))
2323
expect_gg(mcmc_rhat(rhat))
24+
25+
# doesn't error with ratios > 1 (not common but can happen)
26+
expect_gg(mcmc_neff(ratio = c(0.5, 1, 1.25)))
27+
expect_gg(mcmc_neff(ratio = c(0.5, 1, 2)))
2428
})
2529

2630
test_that("rhat and neff plot functions throw correct errors & warnings", {

tests/testthat/test-ppc-distributions.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ test_that("ppc_dens,pp_hist,ppc_freqpoly,ppc_boxplot return ggplot objects", {
6161
test_that("ppc_pit_ecdf, ppc_pit_ecdf_grouped returns a ggplot object", {
6262
expect_gg(ppc_pit_ecdf(y, yrep, interpolate_adj = FALSE))
6363
expect_gg(ppc_pit_ecdf_grouped(y, yrep, group = group, interpolate_adj = FALSE))
64+
expect_message(ppc_pit_ecdf(pit = runif(100)), "'pit' specified")
65+
expect_message(
66+
ppc_pit_ecdf_grouped(pit = runif(length(group)), group = group, interpolate_adj = FALSE),
67+
"'pit' specified"
68+
)
6469
})
6570

6671
test_that("ppc_freqpoly_grouped returns a ggplot object", {

tests/testthat/test-ppc-input-validation.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ context("PPC: input validation")
33

44
source(test_path("data-for-ppc-tests.R"))
55

6-
# validating y ------------------------------------------------------------
76
test_that("validate_y works", {
87
expect_identical(validate_y(y), y)
98
expect_identical(validate_y(as.array(y)), y)
@@ -19,7 +18,6 @@ test_that("validate_y throws errors", {
1918
expect_error(validate_y(c(y, NA)), "NAs not allowed")
2019
})
2120

22-
# validating yrep ----------------------------------------------------------
2321
test_that("validate_predictions works", {
2422
expect_identical(validate_predictions(yrep, length(y)), yrep)
2523
expect_equal(validate_predictions(yrep2, length(y2)), yrep2)
@@ -35,7 +33,6 @@ test_that("validate_predictions throws errors", {
3533
expect_error(validate_predictions(yrep, length(y2)), "must be equal to ")
3634
})
3735

38-
# validating group --------------------------------------------------------
3936
test_that("validate_group works", {
4037
expect_identical(validate_group(1:3, n_obs = 3), as.factor(1:3))
4138
expect_identical(validate_group(as.numeric(1:3), n_obs = 3), as.factor(1:3))
@@ -49,8 +46,6 @@ test_that("validate_group throws errors", {
4946
"must be equal to the number of observations")
5047
})
5148

52-
53-
# validating x --------------------------------------------------------
5449
test_that("validate_x works", {
5550
x <- rnorm(3)
5651
expect_identical(validate_x(x, y = 1:3), x)
@@ -65,3 +60,11 @@ test_that("validate_x throws errors", {
6560
expect_error(validate_x(c(1,2,NA), y = 1:3), "NAs not allowed")
6661
expect_error(validate_x(1:4, y = 1:3), "must be equal to")
6762
})
63+
64+
test_that("validate_pit works", {
65+
expect_error(validate_pit("pit"), "is.numeric")
66+
expect_error(validate_pit(cbind(1, 2)), "vector")
67+
expect_error(validate_pit(-1), "between 0 and 1")
68+
expect_error(validate_pit(NA), "NAs not allowed")
69+
expect_identical(validate_pit(c(name = 0.5)), 0.5)
70+
})

tests/testthat/test-ppc-loo.R

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ test_that("ppc_loo_pit gives deprecation warning but still works", {
3434
test_that("ppc_loo_pit_overlay returns ggplot object", {
3535
skip_if_not_installed("rstanarm")
3636
skip_if_not_installed("loo")
37-
expect_gg(p1 <- ppc_loo_pit_overlay(y, yrep, lw, samples = 25))
37+
expect_gg(ppc_loo_pit_overlay(y, yrep, lw, samples = 25))
38+
})
39+
40+
test_that("ppc_loo_pit_overlay warns about binary data", {
41+
skip_if_not_installed("rstanarm")
42+
skip_if_not_installed("loo")
43+
expect_warning(
44+
ppc_loo_pit_overlay(rep(1, length(y)), yrep, lw),
45+
"not recommended for binary data"
46+
)
3847
})
3948

4049
test_that("ppc_loo_pit_overlay works with boundary_correction=TRUE", {

0 commit comments

Comments
 (0)