Creates posterior density plots from posterior samples obtained from a JAGS MCMC model. Parameters can be selected individually or by their base parameter name, and the resulting ggplot2 object can be extensively customized.

urc_density(
  model,
  parameters = NULL,
  exclude = NULL,
  deviance = FALSE,
  labels = NULL,
  fill = "#333333",
  color = NA,
  alpha = 0.25,
  linewidth = 0.6,
  adjust = 1,
  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(),
  density_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.

fill

Character string specifying the fill color of the density. Defaults to "#333333".

color

Character string specifying the outline color of the density. Defaults to NA, meaning no outline is drawn.

alpha

Numeric value between 0 and 1 specifying density transparency. Defaults to 0.25.

linewidth

Numeric value specifying the width of the density outline. Defaults to 0.6.

adjust

Numeric value used to adjust the bandwidth of the density estimate. Values greater than 1 produce a smoother density, while values less than 1 produce a more detailed density. Defaults to 1.

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().

density_args

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

...

Additional ggplot2 layers or components added to the plot.

Value

A ggplot object showing posterior densities.

Examples

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

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

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

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

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

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

# Publication-style plot
urc_density(
  output$models$poisson,
  fill = "#333333",
  alpha = 0.25,
  color = "#333333",
  linewidth = 0.6,
  theme = ggplot2::theme_classic()
)

# Customize density estimation
urc_density(
  output$models$poisson,
  adjust = 1.5
)

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