Skip to content

Commit

Permalink
plot.brmsfit: show posterior histograms instead of densities by default
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-buerkner committed Dec 19, 2023
1 parent 5837313 commit ccfef37
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 34 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ if potentially results-changing arguments are provided to the criterion method.
* No longer automatically canonicalize the Stan code if cmdstanr is used
as backend. (#1544)
* Improve parameter class names in the `summary` output.
* Show histograms rather than densities in the `plot` method by default.
* Deprecate argument `N` in the `plot` method in favor of argument `nvariables`.

### Bug Fixes

Expand Down
11 changes: 5 additions & 6 deletions R/brm.R
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,16 @@
#' # Poisson regression for the number of seizures in epileptic patients
#' # using normal priors for population-level effects
#' # and half-cauchy priors for standard deviations of group-level effects
#' prior1 <- prior(normal(0,10), class = b) +
#' prior(cauchy(0,2), class = sd)
#'
#' fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
#' data = epilepsy, family = poisson(), prior = prior1)
#' prior1 <- prior(normal(0, 10), class = b) +
#' prior(cauchy(0, 2), class = sd)
#' fit1 <- brm(count ~ zBase * Trt + (1|patient), data = epilepsy,
#' family = poisson(), prior = prior1)
#'
#' # generate a summary of the results
#' summary(fit1)
#'
#' # plot the MCMC chains as well as the posterior distributions
#' plot(fit1, ask = FALSE)
#' plot(fit1)
#'
#' # predict responses based on the fitted model
#' head(predict(fit1))
Expand Down
15 changes: 8 additions & 7 deletions R/hypothesis.R
Original file line number Diff line number Diff line change
Expand Up @@ -520,11 +520,11 @@ print.brmshypothesis <- function(x, digits = 2, chars = 20, ...) {
#' @rdname brmshypothesis
#' @method plot brmshypothesis
#' @export
plot.brmshypothesis <- function(x, N = 5, ignore_prior = FALSE,
chars = 40, colors = NULL,
theme = NULL, ask = TRUE,
plot = TRUE, ...) {
plot.brmshypothesis <- function(x, nvariables = 5, N = NULL,
ignore_prior = FALSE, chars = 40, colors = NULL,
theme = NULL, ask = TRUE, plot = TRUE, ...) {
dots <- list(...)
nvariables <- use_alias(nvariables, N)
if (!is.data.frame(x$samples)) {
stop2("No posterior draws found.")
}
Expand Down Expand Up @@ -572,12 +572,13 @@ plot.brmshypothesis <- function(x, N = 5, ignore_prior = FALSE,
hyps <- paste0(x$hypothesis$Group, ": ", hyps)
}
names(samples)[seq_along(hyps)] <- hyps
nplots <- ceiling(length(hyps) / N)
nplots <- ceiling(length(hyps) / nvariables)
plots <- vector(mode = "list", length = nplots)
for (i in seq_len(nplots)) {
rel_hyps <- hyps[((i - 1) * N + 1):min(i * N, length(hyps))]
sub <- ((i - 1) * nvariables + 1):min(i * nvariables, length(hyps))
sub_hyps <- hyps[sub]
sub_samples <- cbind(
utils::stack(samples[, rel_hyps, drop = FALSE]),
utils::stack(samples[, sub_hyps, drop = FALSE]),
samples[, "Type", drop = FALSE]
)
# make sure that parameters appear in the original order
Expand Down
25 changes: 15 additions & 10 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#' graphic and should be the name of one of the available
#' \code{\link[bayesplot:MCMC-overview]{MCMC}} functions
#' (omitting the \code{mcmc_} prefix).
#' @param N The number of parameters plotted per page.
#' @param nvariables The number of variables (parameters) plotted per page.
#' @param N Deprecated alias of \code{nvariables}.
#' @param theme A \code{\link[ggplot2:theme]{theme}} object
#' modifying the appearance of the plots.
#' For some basic themes see \code{\link[ggplot2:ggtheme]{ggtheme}}
Expand All @@ -23,6 +24,7 @@
#' should be matched exactly (\code{TRUE}) or treated as
#' regular expressions (\code{FALSE}). Default is \code{FALSE}
#' and only works with argument \code{pars}.
#' @param bins Number of bins used for posterior histograms (defaults to 30).
#' @param plot Logical; indicates if plots should be
#' plotted directly in the active graphic device.
#' Defaults to \code{TRUE}.
Expand Down Expand Up @@ -53,13 +55,14 @@
#' @importFrom graphics plot
#' @importFrom grDevices devAskNewPage
#' @export
plot.brmsfit <- function(x, pars = NA, combo = c("dens", "trace"),
N = 5, variable = NULL, regex = FALSE, fixed = FALSE,
theme = NULL, plot = TRUE, ask = TRUE,
newpage = TRUE, ...) {
plot.brmsfit <- function(x, pars = NA, combo = c("hist", "trace"),
nvariables = 5, N = NULL, variable = NULL, regex = FALSE,
fixed = FALSE, bins = 30, theme = NULL, plot = TRUE,
ask = TRUE, newpage = TRUE, ...) {
contains_draws(x)
if (!is_wholenumber(N) || N < 1) {
stop2("Argument 'N' must be a positive integer.")
nvariables <- use_alias(nvariables, N)
if (!is_wholenumber(nvariables) || nvariables < 1) {
stop2("Argument 'nvariables' must be a positive integer.")
}
variable <- use_variable_alias(variable, x, pars, fixed = fixed)
if (is.null(variable)) {
Expand All @@ -77,13 +80,15 @@ plot.brmsfit <- function(x, pars = NA, combo = c("dens", "trace"),
on.exit(devAskNewPage(default_ask))
devAskNewPage(ask = FALSE)
}
n_plots <- ceiling(length(variables) / N)
n_plots <- ceiling(length(variables) / nvariables)
plots <- vector(mode = "list", length = n_plots)
for (i in seq_len(n_plots)) {
sub_vars <- variables[((i - 1) * N + 1):min(i * N, length(variables))]
sub <- ((i - 1) * nvariables + 1):min(i * nvariables, length(variables))
sub_vars <- variables[sub]
sub_draws <- draws[, , sub_vars, drop = FALSE]
plots[[i]] <- bayesplot::mcmc_combo(
sub_draws, combo = combo, gg_theme = theme, ...
sub_draws, combo = combo, bins = bins,
gg_theme = theme, ...
)
if (plot) {
plot(plots[[i]], newpage = newpage || i > 1)
Expand Down
11 changes: 5 additions & 6 deletions man/brm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions man/brmshypothesis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions man/plot.brmsfit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ccfef37

Please sign in to comment.