Creates autocorrelation function (ACF) plots for posterior samples from a JAGS MCMC model. Autocorrelation is calculated separately for each MCMC chain and parameter.

urc_acf(
  model,
  parameters = NULL,
  exclude = NULL,
  deviance = FALSE,
  chains = NULL,
  max_lag = 50,
  labels = NULL,
  x_label = "Lag",
  y_label = "Autocorrelation",
  title = NULL,
  subtitle = NULL,
  caption = NULL,
  scales = "fixed",
  line_args = list(),
  theme = ggplot2::theme_minimal(),
  theme_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.

chains

Optional numeric vector specifying the chains to plot. If NULL, all available chains are plotted.

max_lag

Maximum lag to display. Defaults to 50.

labels

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

x_label

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

y_label

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

title

Optional plot title.

subtitle

Optional plot subtitle.

caption

Optional plot caption.

scales

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

line_args

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

theme

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

theme_args

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

...

Additional ggplot2 layers or components added to the plot.

Value

A ggplot object showing posterior autocorrelation functions for each parameter and MCMC chain.

Examples

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

# Plot ACF for all parameters and all chains
urc_acf(output$models$poisson)

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

# Plot selected chains
urc_acf(
  output$models$poisson,
  chains = 1
)

# Change maximum lag
urc_acf(
  output$models$poisson,
  max_lag = 100
)

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

# Customize the plot
urc_acf(
  output$models$poisson,
  title = "Posterior Autocorrelation",
  line_args = list(linewidth = 1),
  theme = ggplot2::theme_classic()
)

# Add additional ggplot components
urc_acf(output$models$poisson) +
  ggplot2::geom_hline(
    yintercept = 0,
    linetype = "dashed"
  )
} # }