Skip to content

Commit

Permalink
prep for release
Browse files Browse the repository at this point in the history
* fix r cmd check issues from #282
* fix ggplot test issues (closes #289)
* add @TeemuSailynoja as contributor
  • Loading branch information
jgabry committed Nov 10, 2022
1 parent 718b585 commit d85d397
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 61 deletions.
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
Package: bayesplot
Type: Package
Title: Plotting for Bayesian Models
Version: 1.9.0.9000
Date: 2022-03-09
Version: 1.10.0
Date: 2022-11-10
Authors@R: c(person("Jonah", "Gabry", role = c("aut", "cre"), email = "[email protected]"),
person("Tristan", "Mahr", role = "aut"),
person("Paul-Christian", "Bürkner", role = "ctb"),
person("Martin", "Modrák", role = "ctb"),
person("Malcolm", "Barrett", role = "ctb"),
person("Frank", "Weber", role = "ctb"),
person("Eduardo", "Coronado Sroka", role = "ctb"),
person("Teemu", "Sailynoja", role = "ctb"),
person("Aki", "Vehtari", role = "ctb"))
Maintainer: Jonah Gabry <[email protected]>
Description: Plotting functions for posterior analysis, MCMC diagnostics,
Expand All @@ -25,7 +26,7 @@ URL: https://mc-stan.org/bayesplot/
BugReports: https://github.com/stan-dev/bayesplot/issues/
SystemRequirements: pandoc (>= 1.12.3), pandoc-citeproc
Depends:
R (>= 3.1.0)
R (>= 3.5.0)
Imports:
dplyr (>= 0.8.0),
ggplot2 (>= 3.0.0),
Expand Down
12 changes: 9 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<!-- See http://style.tidyverse.org/news.html for advice on writing news -->

# bayesplot 1.9.0.9000

Items for next release go here
# bayesplot 1.10.0

* New function `mcmc_rank_ecdf()` for rank ecdf plots with confidence bands for
assessing if two or more chains sample the same distribution (#282,
@TeemuSailynoja)
* New functions `ppc_pit_ecdf()`, `ppc_pit_ecdf_grouped()`, PIT ecdf plots with
confidence bands to assess if `y` and `yrep` contain samples from the same
distribution. (#282, @TeemuSailynoja)
* Fix failing tests due to latest ggplot2 release (#289)

# bayesplot 1.9.0

Expand Down
2 changes: 1 addition & 1 deletion R/bayesplot-colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ plot_scheme <- function(scheme = NULL) {
value = rep(1, length(x))
)

ggplot(color_data, aes_(x = ~ name, y = ~ value, fill = ~ group)) +
ggplot(color_data, aes(x = .data$name, y = .data$value, fill = .data$group)) +
geom_bar(
width = .5,
stat = "identity",
Expand Down
2 changes: 1 addition & 1 deletion R/helpers-gg.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ set_hist_aes <- function(freq = TRUE, ...) {
if (freq) {
aes_(x = ~ value, ...)
} else {
aes_(x = ~ value, y = ~ stat(density), ...)
aes_(x = ~ value, y = ~ after_stat(density), ...)
}
}

Expand Down
8 changes: 3 additions & 5 deletions R/helpers-ppc.R
Original file line number Diff line number Diff line change
Expand Up @@ -448,21 +448,19 @@ interpolate_gamma <- function(N, K, prob, L) {
#' @noRd
get_interpolation_values <- function(N, K, L, prob) {
for (dim in c("L", "prob")) {
if (all(get(dim) != bayesplot:::gamma_adj[, dim])) {
if (all(get(dim) != gamma_adj[, dim])) {
stop(paste(
"No precomputed values to interpolate from for '", dim, "' = ",
get(dim),
".\n",
"Values of '", dim, "' available for interpolation: ",
paste(unique(bayesplot:::gamma_adj[, dim]), collapse = ", "),
paste(unique(gamma_adj[, dim]), collapse = ", "),
".",
sep = ""
))
}
}
vals <- bayesplot:::gamma_adj[
bayesplot:::gamma_adj$L == L & bayesplot:::gamma_adj$prob == prob,
]
vals <- gamma_adj[gamma_adj$L == L & gamma_adj$prob == prob, ]
if (N > max(vals$N)) {
stop(paste(
"No precomputed values to interpolate from for sample length of ",
Expand Down
6 changes: 3 additions & 3 deletions R/mcmc-diagnostics-nuts.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ mcmc_nuts_acceptance <-
Mean = mean(.data$Value),
Median = median(.data$Value))

hists <- ggplot(data, aes_(x = ~ Value, y = ~ ..density..)) +
hists <- ggplot(data, aes_(x = ~ Value, y = ~ after_stat(density))) +
geom_histogram(
fill = get_color("l"),
color = get_color("lh"),
Expand Down Expand Up @@ -354,7 +354,7 @@ mcmc_nuts_treedepth <- function(x, lp, chain = NULL, ...) {
treedepth <- dplyr::filter(x, .data$Parameter == "treedepth__")
accept_stat <- dplyr::filter(x, .data$Parameter == "accept_stat__")

hist_td <- ggplot(treedepth, aes_(x = ~ Value, y = ~ ..density..)) +
hist_td <- ggplot(treedepth, aes_(x = ~ Value, y = ~ after_stat(density))) +
geom_histogram(
fill = get_color("l"),
color = get_color("lh"),
Expand Down Expand Up @@ -450,7 +450,7 @@ mcmc_nuts_energy <-
clrs <- set_names(get_color(c("lh", "mh")), c("E_fill", "Ediff_fill"))
aes_labs <- c(expression(pi[E]), expression(pi[paste(Delta, E)]))

graph <- ggplot(data, aes_(y = ~ ..density..)) +
graph <- ggplot(data, aes_(y = ~ after_stat(density))) +
geom_histogram(
aes_(
x = ~ Ediff_centered,
Expand Down
2 changes: 1 addition & 1 deletion R/mcmc-traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ mcmc_rank_ecdf <-
x = x
)
data <- data %>%
group_by(parameter, chain) %>%
group_by(.data$parameter, .data$chain) %>%
dplyr::group_map(~ data.frame(
parameter = .y[1],
chain = .y[2],
Expand Down
10 changes: 6 additions & 4 deletions R/ppc-distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@
#' \donttest{
#' # ppc_ecdf_overlay with continuous data (set discrete=TRUE if discrete data)
#' ppc_ecdf_overlay(y, yrep[sample(nrow(yrep), 25), ])
#' }
#'
#' # ECDF and ECDF difference plot of the PIT values of ´y´ compared to ´yrep
#' # with 99% simultaneous confidence bands.
#' ppc_pit_ecdf(y, yrep, prob = 0.99, plot_diff = FALSE, interpolate_adj = FALSE)
#' ppc_pit_ecdf(y, yrep, prob = 0.99, interpolate_adj = FALSE)
#'
#' }
#'
#' # for ppc_hist,dens,freqpoly,boxplot definitely use a subset yrep rows so
#' # only a few (instead of nrow(yrep)) histograms are plotted
Expand Down Expand Up @@ -104,9 +104,11 @@
#'
#' ppc_ecdf_overlay_grouped(y, yrep[1:25, ], group = group)
#'
#' \donttest{
#' # ECDF difference plots of the PIT values by group
#' # with 99% simultaneous confidence bands.
#' ppc_pit_ecdf_grouped(y, yrep, group=group, prob=0.99, interpolate_adj=FALSE)
#' }
#'
#' # don't need to only use small number of rows for ppc_violin_grouped
#' # (as it pools yrep draws within groups)
Expand Down Expand Up @@ -596,7 +598,7 @@ ppc_pit_ecdf <- function(y,

if (is.null(pit)) {
pit <- ppc_data(y, yrep) %>%
group_by(y_id) %>%
group_by(.data$y_id) %>%
dplyr::group_map(~ mean(.x$value[.x$is_y] >= .x$value[!.x$is_y])) %>%
unlist()
if (is.null(K)) {
Expand Down Expand Up @@ -661,7 +663,7 @@ ppc_pit_ecdf_grouped <-

if (is.null(pit)) {
pit <- ppc_data(y, yrep, group) %>%
group_by(y_id) %>%
group_by(.data$y_id) %>%
dplyr::group_map(~ mean(.x$value[.x$is_y] >= .x$value[!.x$is_y])) %>%
unlist()
if (is.null(K)) {
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-bayesplot_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ test_that("bayesplot_grid throws correct errors", {
test_that("bayesplot_grid works", {
expect_message(
a <- bayesplot_grid(p1, p2, xlim = c(-200, 200), ylim = c(0, 200)),
"Adding another scale for 'y'"
"is already present",
fixed = TRUE
)
expect_silent(
b <- bayesplot_grid(plots = list(p1, p2),
Expand Down
70 changes: 33 additions & 37 deletions tests/testthat/test-convenience-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,38 @@ context("Convenience functions (for ggplot objects)")

# abline_01, vline_ and hline_ ------------------------------------------
test_that("abline_01 returns the correct object", {
expect_equal(
abline_01(color = "green", linetype = 2),
geom_abline(intercept = 0, slope = 1, color = "green", linetype = 2, na.rm = TRUE),
check.environment = FALSE
)
a <- abline_01(color = "green", linetype = 2)
b <- geom_abline(intercept = 0, slope = 1, color = "green", linetype = 2, na.rm = TRUE)
a$constructor <- b$constructor <- NULL
expect_equal(a, b, check.environment = FALSE)
})
test_that("vline_* and hline_* return correct objects", {
expect_equal(
vline_0(color = "red"),
geom_vline(xintercept = 0, color = "red", na.rm = TRUE),
check.environment = FALSE
)
expect_equal(
hline_0(size = 2, linetype = 3),
geom_hline(yintercept = 0, size = 2, linetype = 3, na.rm = TRUE),
check.environment = FALSE
)
expect_equal(
vline_at(c(3,4), na.rm = FALSE),
geom_vline(xintercept = c(3,4)),
check.environment = FALSE
)
expect_equal(
hline_at(c(3,4), na.rm = FALSE),
geom_hline(yintercept = c(3,4)),
check.environment = FALSE
)
a <- vline_0(color = "red")
b <- geom_vline(xintercept = 0, color = "red", na.rm = TRUE)
a$constructor <- b$constructor <- NULL
expect_equal(a, b, check.environment = FALSE)

a <- hline_0(linewidth = 2, linetype = 3)
b <- geom_hline(yintercept = 0, linewidth = 2, linetype = 3, na.rm = TRUE)
a$constructor <- b$constructor <- NULL
expect_equal(a, b, check.environment = FALSE)

a <- vline_at(c(3,4), na.rm = FALSE)
b <- geom_vline(xintercept = c(3,4))
a$constructor <- b$constructor <- NULL
expect_equal(a, b, check.environment = FALSE)

a <- hline_at(c(3,4), na.rm = FALSE)
b <- geom_hline(yintercept = c(3,4))
a$constructor <- b$constructor <- NULL
expect_equal(a, b, check.environment = FALSE)
})
test_that("vline_at with 'fun' works", {
x <- example_mcmc_draws(chains = 1)
expect_equal(
vline_at(x, colMeans),
geom_vline(xintercept = colMeans(x), na.rm = TRUE),
check.environment = FALSE
)
a <- vline_at(x, colMeans)
b <- geom_vline(xintercept = colMeans(x), na.rm = TRUE)
a$constructor <- b$constructor <- NULL
expect_equal(a, b, check.environment = FALSE)
})
test_that("calc_v (internal function) works", {
a <- 1:4
Expand Down Expand Up @@ -70,8 +67,8 @@ test_that("lbub works", {
# plot and facet backgrounds ----------------------------------------------
test_that("grid_lines returns correct theme object", {
thm <- theme_default() + grid_lines(size = 1.5, color = "purple")
expect_equal(thm$panel.grid.major, element_line(size = 1.5, color = "purple"))
expect_equal(thm$panel.grid.minor, element_line(size = 0.75, color = "purple"))
expect_equal(thm$panel.grid.major, element_line(linewidth = 1.5, color = "purple"))
expect_equal(thm$panel.grid.minor, element_line(linewidth = 0.75, color = "purple"))
})
test_that("panel_bg returns correct theme object", {
bg1 <- panel_bg()
Expand Down Expand Up @@ -182,9 +179,8 @@ test_that("yaxis_ticks returns correct theme object", {
# overlay functions -------------------------------------------------------
test_that("overlay_function returns the correct object", {
expect_error(overlay_function(), 'argument "fun" is missing')
expect_equal(
overlay_function(fun = "dnorm"),
stat_function(fun = "dnorm", inherit.aes = FALSE),
check.environment = FALSE
)
a <- overlay_function(fun = "dnorm")
b <- stat_function(fun = "dnorm", inherit.aes = FALSE)
a$constructor <- b$constructor <- NULL
expect_equal(a, b, check.environment = FALSE)
})
3 changes: 1 addition & 2 deletions tests/testthat/test-extractors.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ test_that("cmdstanr methods work", {

fit <- cmdstanr::cmdstanr_example("logistic", iter_sampling = 500, chains = 2)
np <- nuts_params(fit)
np_names <- paste0(c("treedepth", "divergent", "accept_stat", "stepsize",
"n_leapfrog", "energy"), "__")
np_names <- paste0(c("treedepth", "divergent", "energy", "accept_stat", "stepsize", "n_leapfrog"), "__")
expect_identical(levels(np$Parameter), np_names)
expect_equal(range(np$Iteration), c(1, 500))
expect_equal(range(np$Chain), c(1, 2))
Expand Down

0 comments on commit d85d397

Please sign in to comment.