jagsUI: Call JAGS from R

Description Usage Arguments Value Examples

Description

The jagsUI function is a basic user interface for running JAGS analyses via package rjags inspired by similar packages like R2WinBUGS, R2OpenBUGS, and R2jags. The user provides a model file, data, initial values (optional), and parameters to save. The function compiles the information and sends it to JAGS, then consolidates and summarizes the MCMC output in an object of class jagsUI.

Usage

1
2
3
jagsUI(model_file, data, inits = NULL, params, n_cores = 1, n_chains,
  n_adapt = 100, n_iter, n_warmup = 0, n_thin = 1,
  modules = c("glm", "dic"), factories = NULL, quiet = FALSE)

Arguments

model_file

Path to file containing the model written in BUGS code.

data

A named list of the data objects required by the model.

inits

A list with n.chains elements; each element of the list is itself a list of starting values for the BUGS model, or a function creating (possibly random) initial values. If inits is NULL, JAGS will generate initial values for parameters.

params

Character vector of the names of the parameters in the model which should be monitored.

n_cores

If > 1, the number of parallel CPU cores to use. MCMC chains are split between cores.

n_chains

Number of Markov chains to run.

n_adapt

Number of iterations to run in the adaptive phase.

n_iter

Total number of iterations per chain (*including* warm-up).

n_warmup

Number of iterations at the beginning of each chain to discard. Does not include the adaptive iterations.

n_thin

Thinning rate. Must be a positive integer.

modules

List of JAGS modules to load before analysis.

factories

Optional character vector of factories to enable or disable, in the format <factory> <type> <setting>. For example, to turn TemperedMix on you would provide 'mix::TemperedMix sampler TRUE' (note spaces between parts). Make sure you have the corresponding modules loaded as well.

quiet

If TRUE, suppress console output.

Value

An object of class jagsUI. Notable elements in the output object include:

samples

The original output object from the rjags package, as class mcmc.list.

summary

A summary of various statistics calculated based on model output, in matrix form.

model

The rjags model object; this will contain multiple elements if n_cores > 1.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 #Analyze Longley economic data in JAGS 
 #Package data
 data(longley)
 data <- list(gnp=longley$GNP, employed=longley$Employed, 
              n=length(longley$Employed))

 #Write model file
 model_file <- tempfile()
 writeLines("
 model{
 #Likelihood
 for (i in 1:n){ 
   employed[i] ~ dnorm(mu[i], tau)     
   mu[i] <- alpha + beta*gnp[i]
 }
 #Priors
 alpha ~ dnorm(0, 0.00001)
 beta ~ dnorm(0, 0.00001)
 sigma ~ dunif(0,1000)
 tau <- pow(sigma,-2)
 }
 ", con=model_file)

#Inits function
 inits <- function(){  
   list(alpha=rnorm(1,0,1),beta=rnorm(1,0,1),sigma=runif(1,0,3))  
 }

#Parameters to save posteriors for
params <- c('alpha','beta','sigma')

#Run analysis
out <- jagsUI(model_file, data, inits, params, 
            n_chains=3, n_iter=1000, n_warmup=500)

#Take another 1000 posterior samples
out <- update(out, n_iter=1000)

kenkellner/jagsUI2 documentation built on July 5, 2019, 9:38 a.m.