Creates posterior histograms for parameters from a JAGS MCMC model. Parameters can be selected individually or by their base parameter name. The resulting ggplot2 object can be further customized using standard ggplot2 functionality.

urc_hist(
  model,
  parameters = NULL,
  exclude = NULL,
  deviance = FALSE,
  labels = NULL,
  bins = 30,
  binwidth = NULL,
  fill = NA,
  color = "#333333",
  alpha = 1,
  linewidth = 0.4,
  scales = "free",
  ncol = NULL,
  nrow = NULL,
  x_label = "Posterior",
  y_label = "Density",
  title = NULL,
  subtitle = NULL,
  caption = NULL,
  theme = ggplot2::theme_minimal(),
  theme_args = list(),
  histogram_args = list(),
  ...
)

Arguments

model

A fitted JAGS model object containing posterior samples.

parameters

Optional character vector specifying the parameters to plot. If NULL, all available parameters are plotted except loglik and, by default, deviance. Parameters can be specified by their exact name (e.g., "lambda[1]") or by their base name (e.g., "lambda"), which selects all indexed parameters.

exclude

Optional character vector specifying parameters to exclude from the plot. Exact parameter names or base parameter names can be used.

deviance

Logical; should posterior deviance be included? Defaults to FALSE.

labels

Optional named character vector used to rename parameter labels in the facets. Names can correspond to exact parameter names or base parameter names.

bins

Number of histogram bins. Defaults to 30.

binwidth

Optional numeric value specifying the width of the histogram bins. If NULL, ggplot2 determines the bin width automatically.

fill

Character string specifying the histogram fill color. Defaults to NA, producing an unfilled histogram.

color

Character string specifying the histogram outline color. Defaults to "#333333".

alpha

Numeric value between 0 and 1 specifying histogram transparency. Defaults to 1.

linewidth

Numeric value specifying the histogram outline width. Defaults to 0.4.

scales

Character string specifying the scales used for the facets. Must be one of "fixed", "free", "free_x", or "free_y". Defaults to "free".

ncol

Number of columns in the facet layout.

nrow

Number of rows in the facet layout.

x_label

Character string specifying the x-axis label. Defaults to "Posterior".

y_label

Character string specifying the y-axis label. Defaults to "Density".

title

Optional plot title.

subtitle

Optional plot subtitle.

caption

Optional plot caption.

theme

A ggplot2 theme object. Defaults to ggplot2::theme_minimal().

theme_args

Named list of arguments passed to ggplot2::theme().

histogram_args

Named list of additional arguments passed to ggplot2::geom_histogram().

...

Additional ggplot2 layers or components added to the plot.

Value

A ggplot object showing posterior histograms.

Examples

if (FALSE) { # \dontrun{
output <- urc_mcmc(x = mydata)

# Plot all parameters
urc_hist(output$models$poisson)

# Plot selected parameters
urc_hist(
  output$models$zip,
  parameters = c("lambda", "pi")
)

# Plot a specific indexed parameter
urc_hist(
  output$models$zip,
  parameters = "lambda[1]"
)

# Plot all lambda parameters
urc_hist(
  output$models$zip,
  parameters = "lambda"
)

# Exclude a parameter
urc_hist(
  output$models$zip,
  exclude = "pi"
)

# Rename parameters
urc_hist(
  output$models$zip,
  labels = c(
    lambda = "Mean Count",
    pi = "Zero-Inflation Probability"
  )
)

# Add color
urc_hist(
  output$models$poisson,
  fill = "#0072B2",
  color = "#0072B2",
  alpha = 0.3
)

# Publication-style plot
urc_hist(
  output$models$poisson,
  theme = ggplot2::theme_classic(),
  bins = 25
)

# Add additional ggplot components
urc_hist(output$models$poisson) +
  ggplot2::geom_vline(
    xintercept = 0,
    linetype = "dashed"
  )
} # }