From 11769f6e12b00dd0ed0631eb6606205ee18796a8 Mon Sep 17 00:00:00 2001 From: "Watal M. Iwasaki" Date: Mon, 13 Jun 2022 12:11:21 +0900 Subject: [PATCH] Add option 'bins' to histogram functions --- R/mcmc-diagnostics-nuts.R | 18 +++++++++++++----- R/ppc-distributions.R | 3 +++ R/ppc-errors.R | 1 + R/ppc-test-statistics.R | 5 ++++- R/ppd-distributions.R | 3 +++ R/ppd-test-statistics.R | 6 +++++- man-roxygen/args-hist.R | 4 +++- man/MCMC-diagnostics.Rd | 2 +- man/MCMC-distributions.Rd | 2 +- man/MCMC-nuts.Rd | 14 ++++++++++++-- man/MCMC-recover.Rd | 2 +- man/PPC-distributions.Rd | 14 ++++++++++++-- man/PPC-errors.Rd | 1 + man/PPC-test-statistics.Rd | 4 +++- man/PPD-distributions.Rd | 13 +++++++++++-- man/PPD-test-statistics.Rd | 5 ++++- 16 files changed, 78 insertions(+), 19 deletions(-) diff --git a/R/mcmc-diagnostics-nuts.R b/R/mcmc-diagnostics-nuts.R index 1cfe69e6..b3fcb9ff 100644 --- a/R/mcmc-diagnostics-nuts.R +++ b/R/mcmc-diagnostics-nuts.R @@ -129,13 +129,16 @@ NULL #' @export #' @param binwidth An optional value passed to [ggplot2::geom_histogram()] to #' override the default binwidth. +#' @param bins An optional value passed to [ggplot2::geom_histogram()] to +#' override the default bins. Overridden by `binwidth`. #' mcmc_nuts_acceptance <- function(x, lp, chain = NULL, ..., - binwidth = NULL) { + binwidth = NULL, + bins = NULL) { suggested_package("gridExtra") check_ignored_arguments(...) @@ -160,7 +163,8 @@ mcmc_nuts_acceptance <- color = get_color("lh"), linewidth = 0.25, na.rm = TRUE, - binwidth = binwidth + binwidth = binwidth, + bins = bins ) + bayesplot_theme_get() @@ -209,7 +213,8 @@ mcmc_nuts_acceptance <- color = NA, alpha = 0.5, na.rm = TRUE, - binwidth = binwidth + binwidth = binwidth, + bins = bins ) chain_scatter_data <- data.frame( @@ -417,6 +422,7 @@ mcmc_nuts_energy <- function(x, ..., binwidth = NULL, + bins = NULL, alpha = 0.5, merge_chains = FALSE) { check_ignored_arguments(...) @@ -446,7 +452,8 @@ mcmc_nuts_energy <- ), linewidth = 0.25, na.rm = TRUE, - binwidth = binwidth + binwidth = binwidth, + bins = bins ) + geom_histogram( aes( @@ -457,7 +464,8 @@ mcmc_nuts_energy <- linewidth = 0.25, na.rm = TRUE, alpha = alpha, - binwidth = binwidth + binwidth = binwidth, + bins = bins ) + scale_fill_manual("", values = fills, labels = aes_labs) + scale_color_manual("", values = clrs, labels = aes_labs) + diff --git a/R/ppc-distributions.R b/R/ppc-distributions.R index a03986e2..7db526f3 100644 --- a/R/ppc-distributions.R +++ b/R/ppc-distributions.R @@ -402,6 +402,7 @@ ppc_freqpoly <- yrep, ..., binwidth = NULL, + bins = NULL, freq = TRUE, size = 0.5, alpha = 1) { @@ -421,6 +422,7 @@ ppc_freqpoly <- stat = "bin", binwidth = binwidth, linewidth = size, + bins = bins, alpha = alpha ) + scale_fill_ppc() + @@ -446,6 +448,7 @@ ppc_freqpoly_grouped <- group, ..., binwidth = NULL, + bins = NULL, freq = TRUE, size = 0.5, alpha = 1) { diff --git a/R/ppc-errors.R b/R/ppc-errors.R index 100a9cb1..c81a8111 100644 --- a/R/ppc-errors.R +++ b/R/ppc-errors.R @@ -164,6 +164,7 @@ ppc_error_hist_grouped <- ..., facet_args = list(), binwidth = NULL, + bins = bins, breaks = NULL, freq = TRUE) { diff --git a/R/ppc-test-statistics.R b/R/ppc-test-statistics.R index baa55e1b..0af31508 100644 --- a/R/ppc-test-statistics.R +++ b/R/ppc-test-statistics.R @@ -165,6 +165,7 @@ ppc_stat_grouped <- ..., facet_args = list(), binwidth = NULL, + bins = NULL, breaks = NULL, freq = TRUE) { check_ignored_arguments(...) @@ -208,7 +209,8 @@ ppc_stat_freqpoly <- aes(color = "yrep"), linewidth = 0.5, na.rm = TRUE, - binwidth = binwidth + binwidth = binwidth, + bins = bins ) + geom_vline( data = dplyr::filter(data, .data$variable == "y"), @@ -240,6 +242,7 @@ ppc_stat_freqpoly_grouped <- ..., facet_args = list(), binwidth = NULL, + bins = NULL, freq = TRUE) { check_ignored_arguments(...) call <- match.call(expand.dots = FALSE) diff --git a/R/ppd-distributions.R b/R/ppd-distributions.R index 9cbc3757..bbf67218 100644 --- a/R/ppd-distributions.R +++ b/R/ppd-distributions.R @@ -194,6 +194,7 @@ ppd_freqpoly <- function(ypred, ..., binwidth = NULL, + bins = NULL, freq = TRUE, size = 0.5, alpha = 1) { @@ -213,6 +214,7 @@ ppd_freqpoly <- geom_area( stat = "bin", binwidth = binwidth, + bins = bins, size = size, alpha = alpha ) + @@ -238,6 +240,7 @@ ppd_freqpoly_grouped <- group, ..., binwidth = NULL, + bins = NULL, freq = TRUE, size = 0.5, alpha = 1) { diff --git a/R/ppd-test-statistics.R b/R/ppd-test-statistics.R index fc7354c4..fc4acd38 100644 --- a/R/ppd-test-statistics.R +++ b/R/ppd-test-statistics.R @@ -85,6 +85,7 @@ ppd_stat_grouped <- ..., facet_args = list(), binwidth = NULL, + bins = NULL, breaks = NULL, freq = TRUE) { check_ignored_arguments(...) @@ -104,6 +105,7 @@ ppd_stat_freqpoly <- ..., facet_args = list(), binwidth = NULL, + bins = NULL, freq = TRUE) { stopifnot(length(stat) == 1) dots <- list(...) @@ -122,7 +124,8 @@ ppd_stat_freqpoly <- aes(color = "ypred"), linewidth = 0.5, na.rm = TRUE, - binwidth = binwidth + binwidth = binwidth, + bins = bins ) + scale_color_ppd( name = stat_legend_title(stat, deparse(substitute(stat))), @@ -146,6 +149,7 @@ ppd_stat_freqpoly_grouped <- ..., facet_args = list(), binwidth = NULL, + bins = NULL, freq = TRUE) { check_ignored_arguments(...) call <- match.call(expand.dots = FALSE) diff --git a/man-roxygen/args-hist.R b/man-roxygen/args-hist.R index 657d20ec..338e12ac 100644 --- a/man-roxygen/args-hist.R +++ b/man-roxygen/args-hist.R @@ -1,7 +1,9 @@ #' @param binwidth Passed to [ggplot2::geom_histogram()] to override #' the default binwidth. +#' #' @param bins Passed to [ggplot2::geom_histogram()] to override -#' the default binwidth. +#' the default bins. Overridden by `binwidth`. +#' #' @param breaks Passed to [ggplot2::geom_histogram()] as an #' alternative to `binwidth`. #' diff --git a/man/MCMC-diagnostics.Rd b/man/MCMC-diagnostics.Rd index 1ca949bb..8880eb0e 100644 --- a/man/MCMC-diagnostics.Rd +++ b/man/MCMC-diagnostics.Rd @@ -56,7 +56,7 @@ default size (for \code{mcmc_rhat()}, \code{mcmc_neff()}) or the default binwidth.} \item{bins}{Passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} to override -the default binwidth.} +the default bins. Overridden by \code{binwidth}.} \item{breaks}{Passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} as an alternative to \code{binwidth}.} diff --git a/man/MCMC-distributions.Rd b/man/MCMC-distributions.Rd index da157805..7ee7b88c 100644 --- a/man/MCMC-distributions.Rd +++ b/man/MCMC-distributions.Rd @@ -162,7 +162,7 @@ on the plot) instead of the \strong{ggplot2} default of \code{scales="fixed"}.} the default binwidth.} \item{bins}{Passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} to override -the default binwidth.} +the default bins. Overridden by \code{binwidth}.} \item{breaks}{Passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} as an alternative to \code{binwidth}.} diff --git a/man/MCMC-nuts.Rd b/man/MCMC-nuts.Rd index 72feacbc..9b497a7d 100644 --- a/man/MCMC-nuts.Rd +++ b/man/MCMC-nuts.Rd @@ -10,7 +10,7 @@ \alias{mcmc_nuts_energy} \title{Diagnostic plots for the No-U-Turn-Sampler (NUTS)} \usage{ -mcmc_nuts_acceptance(x, lp, chain = NULL, ..., binwidth = NULL) +mcmc_nuts_acceptance(x, lp, chain = NULL, ..., binwidth = NULL, bins = NULL) mcmc_nuts_divergence(x, lp, chain = NULL, ...) @@ -18,7 +18,14 @@ mcmc_nuts_stepsize(x, lp, chain = NULL, ...) mcmc_nuts_treedepth(x, lp, chain = NULL, ...) -mcmc_nuts_energy(x, ..., binwidth = NULL, alpha = 0.5, merge_chains = FALSE) +mcmc_nuts_energy( + x, + ..., + binwidth = NULL, + bins = NULL, + alpha = 0.5, + merge_chains = FALSE +) } \arguments{ \item{x}{A molten data frame of NUTS sampler parameters, either created by @@ -42,6 +49,9 @@ is not used by \code{mcmc_nuts_energy()}.} \item{binwidth}{An optional value passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} to override the default binwidth.} +\item{bins}{An optional value passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} to +override the default bins. Overridden by \code{binwidth}.} + \item{alpha}{For \code{mcmc_nuts_energy()} only, the transparency (alpha) level in \verb{[0,1]} used for the overlaid histogram.} diff --git a/man/MCMC-recover.Rd b/man/MCMC-recover.Rd index 03700829..6754b976 100644 --- a/man/MCMC-recover.Rd +++ b/man/MCMC-recover.Rd @@ -91,7 +91,7 @@ appearance of plotted points.} the default binwidth.} \item{bins}{Passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} to override -the default binwidth.} +the default bins. Overridden by \code{binwidth}.} \item{breaks}{Passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} as an alternative to \code{binwidth}.} diff --git a/man/PPC-distributions.Rd b/man/PPC-distributions.Rd index cc6392d8..c8bdbd0f 100644 --- a/man/PPC-distributions.Rd +++ b/man/PPC-distributions.Rd @@ -79,7 +79,16 @@ ppc_hist( freq = TRUE ) -ppc_freqpoly(y, yrep, ..., binwidth = NULL, freq = TRUE, size = 0.5, alpha = 1) +ppc_freqpoly( + y, + yrep, + ..., + binwidth = NULL, + bins = NULL, + freq = TRUE, + size = 0.5, + alpha = 1 +) ppc_freqpoly_grouped( y, @@ -87,6 +96,7 @@ ppc_freqpoly_grouped( group, ..., binwidth = NULL, + bins = NULL, freq = TRUE, size = 0.5, alpha = 1 @@ -169,7 +179,7 @@ passed to \code{\link[ggplot2:stat_ecdf]{ggplot2::stat_ecdf()}}. If \code{discre the default binwidth.} \item{bins}{Passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} to override -the default binwidth.} +the default bins. Overridden by \code{binwidth}.} \item{breaks}{Passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} as an alternative to \code{binwidth}.} diff --git a/man/PPC-errors.Rd b/man/PPC-errors.Rd index fd37b943..7af29eb0 100644 --- a/man/PPC-errors.Rd +++ b/man/PPC-errors.Rd @@ -30,6 +30,7 @@ ppc_error_hist_grouped( ..., facet_args = list(), binwidth = NULL, + bins = bins, breaks = NULL, freq = TRUE ) diff --git a/man/PPC-test-statistics.Rd b/man/PPC-test-statistics.Rd index 0ebb3b68..604d7991 100644 --- a/man/PPC-test-statistics.Rd +++ b/man/PPC-test-statistics.Rd @@ -30,6 +30,7 @@ ppc_stat_grouped( ..., facet_args = list(), binwidth = NULL, + bins = NULL, breaks = NULL, freq = TRUE ) @@ -52,6 +53,7 @@ ppc_stat_freqpoly_grouped( ..., facet_args = list(), binwidth = NULL, + bins = NULL, freq = TRUE ) @@ -83,7 +85,7 @@ then generic naming is used in the legend.} the default binwidth.} \item{bins}{Passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} to override -the default binwidth.} +the default bins. Overridden by \code{binwidth}.} \item{breaks}{Passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} as an alternative to \code{binwidth}.} diff --git a/man/PPD-distributions.Rd b/man/PPD-distributions.Rd index 0b11fae8..4a078b34 100644 --- a/man/PPD-distributions.Rd +++ b/man/PPD-distributions.Rd @@ -39,13 +39,22 @@ ppd_dens(ypred, ..., trim = FALSE, size = 0.5, alpha = 1) ppd_hist(ypred, ..., binwidth = NULL, bins = NULL, breaks = NULL, freq = TRUE) -ppd_freqpoly(ypred, ..., binwidth = NULL, freq = TRUE, size = 0.5, alpha = 1) +ppd_freqpoly( + ypred, + ..., + binwidth = NULL, + bins = NULL, + freq = TRUE, + size = 0.5, + alpha = 1 +) ppd_freqpoly_grouped( ypred, group, ..., binwidth = NULL, + bins = NULL, freq = TRUE, size = 0.5, alpha = 1 @@ -86,7 +95,7 @@ passed to \code{\link[ggplot2:stat_ecdf]{ggplot2::stat_ecdf()}}. If \code{discre the default binwidth.} \item{bins}{Passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} to override -the default binwidth.} +the default bins. Overridden by \code{binwidth}.} \item{breaks}{Passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} as an alternative to \code{binwidth}.} diff --git a/man/PPD-test-statistics.Rd b/man/PPD-test-statistics.Rd index b2e4a69a..e4ce54d8 100644 --- a/man/PPD-test-statistics.Rd +++ b/man/PPD-test-statistics.Rd @@ -28,6 +28,7 @@ ppd_stat_grouped( ..., facet_args = list(), binwidth = NULL, + bins = NULL, breaks = NULL, freq = TRUE ) @@ -38,6 +39,7 @@ ppd_stat_freqpoly( ..., facet_args = list(), binwidth = NULL, + bins = NULL, freq = TRUE ) @@ -48,6 +50,7 @@ ppd_stat_freqpoly_grouped( ..., facet_args = list(), binwidth = NULL, + bins = NULL, freq = TRUE ) @@ -74,7 +77,7 @@ then generic naming is used in the legend.} the default binwidth.} \item{bins}{Passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} to override -the default binwidth.} +the default bins. Overridden by \code{binwidth}.} \item{breaks}{Passed to \code{\link[ggplot2:geom_histogram]{ggplot2::geom_histogram()}} as an alternative to \code{binwidth}.}