Package 'basicMCMCplots'

Title: Trace Plots, Density Plots and Chain Comparisons for MCMC Samples
Description: Provides methods for examining posterior MCMC samples from a single chain using trace plots and density plots, and from multiple chains by comparing posterior medians and credible intervals from each chain. These plotting functions have a variety of options, such as figure sizes, legends, parameters to plot, and saving plots to file. Functions interface with the NIMBLE software package, see de Valpine, Turek, Paciorek, Anderson-Bergman, Temple Lang and Bodik (2017) <doi:10.1080/10618600.2016.1172487>.
Authors: Daniel Turek [aut, cre]
Maintainer: Daniel Turek <[email protected]>
License: GPL-3
Version: 0.2.7
Built: 2024-11-21 05:04:54 UTC
Source: https://github.com/cran/basicMCMCplots

Help Index


Compare trace plots from multiple MCMC chains

Description

Overlays trace plots from each MCMC chain, for each parameter

Usage

chainsPlot(
  samplesList,
  var = NULL,
  ind = NULL,
  burnin = NULL,
  scale = FALSE,
  line = NULL,
  ncols = NULL,
  width = 7,
  height = NULL,
  legend = !is.null(names(samplesList)),
  legend.location = "topright",
  cex = 1,
  traceplot = TRUE,
  densityplot = TRUE,
  file = NULL
)

Arguments

samplesList

List of arrays of MCMC samples from different chains

var

Parameter names to plot

ind

Indices of MCMC samples to plot

burnin

Number of initial samples to discard from each MCMC chain (default: 0)

scale

Logical, whether to normalize each posterior chain (default: FALSE)

line

Numeric vector of true parameter values for adding lines to plots

ncols

Number of columns in grid of parameter traceplots or densityplots

width

Width of the plot

height

Height of the plot

legend

Logical, whether to include a legend of chain names

legend.location

Legend location

cex

Expansion coefficient for text (default: 1)

traceplot

Logical, whether to generate posterior trace plots (default: TRUE)

densityplot

Logical, whether to generate posterior density plots (default: TRUE)

file

Filename for saving figure to a file

Examples

samples1 <- cbind(rnorm(1000, 1), rgamma(1000, 1), rpois(1000, 1))
colnames(samples1) <- c('alpha', 'beta', 'gamma')
samples2 <- cbind(rnorm(1000, 2), rgamma(1000, 2), rpois(1000, 2))
colnames(samples2) <- c('alpha', 'beta', 'gamma')
samplesList <- list(chain1 = samples1, chain2 = samples2)

chainsPlot(samplesList)

chainsPlot(samplesList, densityplot = FALSE, burnin = 500)

chainsPlot(samplesList, traceplot = FALSE, legend.location = 'topleft', cex = 0.7)

Compare summary statistics from multiple MCMC chains

Description

Plots median and 95

Usage

chainsSummary(
  samplesList,
  var = NULL,
  nrows = NULL,
  scale = FALSE,
  width = 7,
  height = NULL,
  legend = !is.null(names(samplesList)),
  legend.location = "topright",
  jitter,
  buffer = NULL,
  buffer.right = NULL,
  buffer.left = NULL,
  cex = 1,
  file = NULL
)

Arguments

samplesList

List of arrays of MCMC samples from different chains

var

Parameter names to plot

nrows

Number of rows in the resulting plot

scale

Logical, whether to normalize each posterior chain

width

Width of figure

height

Height of figure

legend

Logical, whether to include a legend of chain names

legend.location

Legend location

jitter

Scale factor for spreading out lines from each chain

buffer

Buffer margin on both sides. Overrides buffer.right and buffer.left

buffer.right

Additional buffer on left side of plot

buffer.left

Additional buffer on right side of plot

cex

Expansion coefficient for text

file

Filename for saving figure to a file

Examples

samples1 <- cbind(rnorm(1000, 1), rgamma(1000, 1), rpois(1000, 1))
colnames(samples1) <- c('alpha', 'beta', 'gamma')
samples2 <- cbind(rnorm(1000, 2), rgamma(1000, 2), rpois(1000, 2))
colnames(samples2) <- c('alpha', 'beta', 'gamma')
samplesList <- list(chain1 = samples1, chain2 = samples2)
chainsSummary(samplesList, nrow = 1, jitter = .3, buffer.left = .5, buffer.right = .5)

Plot MCMC traceplots and density plots

Description

Plot MCMC traceplots and density plots

Usage

samplesPlot(
  samples,
  var = colnames(samples),
  ind = NULL,
  burnin = NULL,
  scale = FALSE,
  line = NULL,
  width = 7,
  height = 4,
  legend = TRUE,
  legend.location = "topright",
  traceplot = TRUE,
  densityplot = TRUE,
  file = NULL
)

Arguments

samples

Array of MCMC samples, or a list of samples from multiple chains in which case the first chain is used

var

Parameter names to plot

ind

Indices of MCMC samples to plot

burnin

Number of initial MCMC samples to discard (default: 0)

scale

Logical, whether to normalize each posterior chain

line

Numeric vector of true parameter values for adding lines to plots

width

Width of the plot

height

Height of the plot

legend

Logical, whether to include a legend of parameter names

legend.location

Location of legend

traceplot

Logical, whether to include traceplots (default: TRUE)

densityplot

Logaical, whether to include density plots (default: TRUE)

file

Optional filename to save figure as a file

Examples

samples <- cbind(rnorm(1000), rgamma(1000, 1))
colnames(samples) <- c('alpha', 'beta')
samplesPlot(samples)