jagshelper-vignette"

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width=7,
  fig.height=7
)
library(jagshelper)

jagshelper

The goal of jagshelper is to streamline Bayesian analysis in JAGS using the jagsUI package.\ Functions are provided for extracting output in a simpler form, assessing model convergence, and plotting model output. Also included is a function giving a template model in JAGS syntax with the associated jagsUI code.

Some of the jagshelper functionality is illustrated below, with steps approximately corresponding to those of a typical Bayesian analysis using JAGS.

Model template

When starting a Bayesian analysis in JAGS from scratch, I can never remember the exact structure for doing so, and sometimes need reminding of basic JAGS model syntax.

The skeleton() function prints a JAGS model template to the screen, along with code to simulate a corresponding dataset.

library(jagshelper)
skeleton("EXAMPLE")

Output summary & Assessing convergence

A simple model

Having run a model for the first time, it can be useful to see how many parameter nodes have been saved in total, or how many nodes exist for each named parameter. This can aid in deciding the appropriate strategy for assessing convergence: for example, whether trace plots can be feasibly assessed for all parameter nodes in the model, or just a subset.

In the example below, there are relatively few parameters saved and all trace plots can be examined.

nparam(asdf_jags_out)  # how many parameters in total
nbyname(asdf_jags_out)  # how many parameters (or dimensions) per parameter name
tracedens_jags(asdf_jags_out, parmfrow=c(3,3))  # trace plots for all parameters
check_Rhat(asdf_jags_out)  # proportion of Rhats below a threshold of 1.1
asdf_jags_out$Rhat  #  Rhat values

A more complex model

In the example below, there are many more parameters saved, and it is perhaps more illustrative to examine the trace plots associated with the least- converged parameter nodes, as measured by Rhat value (Gelman & Rubin 1992).

nparam(SS_out)  # how many parameters in total
nbyname(SS_out)  # how many parameters (or dimensions) per parameter name
traceworstRhat(SS_out, parmfrow=c(3,2))  # trace plots for least-converged nodes
check_Rhat(SS_out)  # proportion of Rhats below a threshold of 1.1
plotRhats(SS_out)  # plotting Rhat values

Additional functionality

JAGS is run internally k times (or alternately, the size of the dataset), withholding each of k "folds" of the input data and drawing posterior predictive samples corresponding to the withheld data, which can then be compared to the input data to assess model predictive power.

Global measures of predictive power are provided in output: Root Mean Square (Prediction) Error and Mean Absolute (Prediction) Error. However, it is likely that these measures will not be meaningful by themselves; rather, as a metric for scoring a set of candidate models.

pairstrace_jags(asdf_jags_out, p=c("a","sig_a"), points=TRUE, parmfrow=c(3,2))
plotcor_jags(SS_out, p=c("trend","rate","sig"))

Extracting output as data.frame

The jags_df() function extracts all posterior samples from an output object returned by jagsUI::jags() as a data.frame, which may be preferable for some users.

out_df <- jags_df(asdf_jags_out)
str(out_df)

Additional functionality

Plotting a matrix associated with a vector of parameter nodes

A single matrix

The caterpillar() and envelope() functions plot output associated with a vector of parameter nodes. This is often expressed as a two-dimensional matrix or data.frame, with a column for each parameter node and a row for each MCMC iteration. Both caterpillar() and envelope() were originally written to accept such data.frames as inputs, but now also accept a jagsUI output object and parameter name.

It is anticipated that caterpillar() could be used for effect sizes associated with a categorical variable, in which plotting order may or may not matter.

By contrast, envelope() is intended for a matrix associated with a sequence of parameter nodes, such as in a time series.

For a simpler case, plotdens() produces a kernel density plot of a single parameter node, or overlays multiple parameter nodes from a list. Alternately (shown below) it overlays kernel density plots of a vector of parameter nodes.

old_parmfrow <- par("mfrow")  # storing old graphics state
par(mfrow=c(3,1))
caterpillar(asdf_jags_out, "a")
envelope(SS_out, "trend", x=SS_data$x)
plotdens(asdf_jags_out, "a")
par(mfrow=old_parmfrow)  # resetting graphics state

Multiple matrices or multiple models

It may be appropriate to make by-element comparisons of multiple such matrices, perhaps between multiple candidate models.

Function comparecat() produces interleaved caterpillar plots for a list of jagsUI output objects and an optional list of parameters, plotting parameters common to a set of models adjacent to one another. The example below uses the same output object three times, but will show functionality.

Function comparedens() behaves similarly, but produces left- and right-facing kernel density plots for TWO jagsUI output objects and an optional list of parameters. The example below uses the same output object twice, but will show functionality.

old_parmfrow <- par("mfrow")  # storing old graphics state
par(mfrow=c(2,1))
comparecat(x=list(asdf_jags_out, asdf_jags_out, asdf_jags_out),
           p=c("a","b","sig"))
comparedens(x1=asdf_jags_out, x2=asdf_jags_out, p=c("a","b","sig"))
par(mfrow=old_parmfrow)  # resetting graphics state

Function overlayenvelope() will automatically overlay multiple envelope() plots, and may be used with a variety of input structures:

par(mfrow=c(2,2))

## usage with list of input data.frames
overlayenvelope(df=list(SS_out$sims.list$cycle_s[,,1],
                            SS_out$sims.list$cycle_s[,,2]))

## usage with a 3-d input array
overlayenvelope(df=SS_out$sims.list$cycle_s)

## usage with a jagsUI output object and parameter name (2-d parameter)
overlayenvelope(df=SS_out, p="cycle_s")

## usage with a single jagsUI output object and multiple parameters
overlayenvelope(df=SS_out, p=c("trend","rate"))

Function crossplot() plots corresponding pairs of parameter densities on the X- and Y-axes. Three plotting methods are provided, that may be overlayed if desired:

This function may be used with vectors or matrices of MCMC samples, or with a jagsUI object and a vector of parameter names.

## Usage with single vectors (or data.frames or 2d matrices) 
xx <- SS_out$sims.list$trend[,41]
yy <- SS_out$sims.list$cycle[,41]

## Showing possible geometries
par(mfrow = c(2, 2))
plot(xx, yy, col=adjustcolor(1, alpha.f=.1), pch=16, main="Cross Geometry")
crossplot(xx, yy, add=TRUE, col=1)
plot(xx, yy, col=adjustcolor(1, alpha.f=.1), pch=16, main="G Geometry")
crossplot(xx, yy, add=TRUE, col=1,
          drawcross=FALSE, drawx=TRUE)
plot(xx, yy, col=adjustcolor(1, alpha.f=.1), pch=16, main="Blob Geometry")
crossplot(xx, yy, add=TRUE, col=1,
          drawcross=FALSE, drawblob=TRUE)
plot(xx, yy, col=adjustcolor(1, alpha.f=.1), pch=16, main="Blob Outlines")
crossplot(xx, yy, add=TRUE, col=1,
          drawcross=FALSE, drawblob=TRUE, outline=TRUE)

## Usage with jagsUI object and parameter names, plus addl functionality
par(mfrow = c(1, 1))
crossplot(SS_out, p=c("trend","cycle"),
          labels=SS_data$x, labelpos=1, link=TRUE, drawblob=TRUE,
          col="random")

Comparison between priors and posteriors

Function comparepriors() is a wrapper for comparedens(), and plots side-by-side kernel densities of all parameters with names ending in "_prior", along with the respective posterior densities. It should be noted that additional parameters must be included in the JAGS model to provide samples of the prior distributions, as is shown in the example below.

...
sig ~ dunif(0, 10)   # this is the parameter that is used elsewhere in the model
sig_prior ~ dunif(0, 10)  # this is only used to give samples of the prior
...
comparepriors(asdf_prior_jags_out, parmfrow=c(2,3))


Try the jagshelper package in your browser

Any scripts or data that you put into this service are public.

jagshelper documentation built on Oct. 22, 2024, 1:06 a.m.