jags {R2jags}R Documentation

Run jags from R

Description

The jags function takes data and starting values as input. It automatically writes a jags script, calls the model, and saves the simulations for easy access in R.

Usage

jags(data, inits, parameters.to.save, model.file="model.bug",
  n.chains=3, n.iter=2000, n.burnin=floor(n.iter/2),
  n.thin=max(1, floor((n.iter - n.burnin) / n.sims)),
  n.sims = 1000, n.adapt = 1000, DIC=FALSE, working.directory=NULL,
  refresh = n.iter/50, progress.bar = "text")

jags2(data, inits, parameters.to.save, model.file="model.bug",
  n.chains=3, n.iter=2000, n.burnin=floor(n.iter/2),
  n.thin=max(1, floor((n.iter - n.burnin) / n.sims)),
  n.sims = 1000, DIC=TRUE, jags.path="", 
  working.directory=NULL, clearWD=TRUE,
  refresh = n.iter/50)

Arguments

data a vector or list of the names of the data objects used 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.
parameters.to.save character vector of the names of the parameters to save which should be monitored.
model.file file containing the model written in BUGS code.
n.chains number of Markov chains (default: 3)
n.iter number of total iterations per chain (including burn in; default: 2000)
n.burnin length of burn in, i.e. number of iterations to discard at the beginning. Default is n.iter/2, that is, discarding the first half of the simulations.
n.thin thinning rate. Must be a positive integer. Set n.thin > 1 to save memory and computation time if n.iter is large. Default is max(1, floor(n.chains * (n.iter-n.burnin) / 1000)) which will only thin if there are at least 2000 simulations.
n.sims The approximate number of simulations to keep after thinning.
n.adapt the number of iterations for adaptation. When a model is first created, some of the samplers may have an adaptive mode. In this case, the samplers are run for n.adapt iterations and then the adaptive mode is switched off.
DIC logical; if TRUE (default), compute deviance, pD, and DIC. The rule pD=var(deviance) / 2 is used.
working.directory sets working directory during execution of this function; This should be the directory where model file is.
jags.path directory that contains the jags executable. The default is “”.
clearWD indicating whether the files ‘data.txt’, ‘inits[1:n.chains].txt’, ‘codaIndex.txt’, ‘jagsscript.txt’, and ‘CODAchain[1:nchains].txt’ should be removed after jags has finished, default=TRUE.
refresh refresh frequency for progress bar, default is n.iter/50
progress.bar type of progress bar. Possible values are “text”, “gui”, and “none”. Type “text” is displayed on the R console. Type “gui” is a graphical progress bar in a new window. The progress bar is suppressed if progress.bar is “none”

Details

To run:

  1. Write a BUGS model in an ASCII file.
  2. Go into R.
  3. Prepare the inputs for the jags function and run it (see Example section).
  4. The model will now run in JAGS. It might take awhile. You will see things happening in the R console.

BUGS version support:

Author(s)

Yu-Sung Su ys463@columbia.edu, Masanao Yajima yajima@stat.columbia.edu

References

Plummer, Martyn (2003) “JAGS: A program for analysis of Bayesian graphical models using Gibbs sampling.” http://citeseer.ist.psu.edu/plummer03jags.html.

Gelman, A., Carlin, J. B., Stern, H.S., Rubin, D.B. (2003) Bayesian Data Analysis, 2nd edition, CRC Press.

Sibylle Sturtz and Uwe Ligges and Andrew Gelman. (2005). “R2WinBUGS: A Package for Running WinBUGS from R.” Journal of Statistical Software 3 (12): 1–6.

Examples

  # An example model file is given in:
  model.file <- system.file(package="R2jags", "model", "schools.txt")
  # Let's take a look:
  file.show(model.file)

  # data
  J <- 8.0
  y <- c(28.4,7.9,-2.8,6.8,-0.6,0.6,18.0,12.2)
  sd <- c(14.9,10.2,16.3,11.0,9.4,11.4,10.4,17.6)

  jags.data <- list("y","sd","J")
  jags.params <- c("mu","sigma","theta")
  jags.inits <- function(){
    list("mu"=rnorm(1),"sigma"=runif(1),"theta"=rnorm(J))
  }
  
#=============#
# using jags  #
#=============#
  jagsfit <- jags(data=jags.data, inits=jags.inits, jags.params, 
    n.iter=5000, model.file=model.file)
    
  # display the output
  print(jagsfit)
  plot(jagsfit)
  
  # traceplot
  traceplot(jagsfit)
  
  # if the model does not converge, update it!
  jagsfit.upd <- update(jagsfit, n.iter=1000)
  print(jagsfit.upd)
  plot(jagsfit.upd)
  
  # or auto update it until it converges! see ?autojags for details
  jagsfit.upd <- autojags(jagsfit)
    
  # to get DIC or specify DIC=TRUE in jags()
  dic.samples(jagsfit.upd$model, n.iter=1000, type="pD")

  # to pick up the last save session
  # for example, load("RWorkspace.Rdata")
  recompile(jagsfit)
  jagsfit.upd <- update(jagsfit)

#=============#
# using jags2 #
#=============#
  ## jags cannot be updated, but produces coda files
  ## You may need to edit "jags.path",
  ## also you need write access in the working directory:
  ## currently works only under Windows OS
  ## e.g. setwd("c:/")
  
  #jagsfit <- jags2(data=jags.data, inits=jags.inits, jags.params, 
  #  n.iter=5000, model.file=model.file)
  #print(jagsfit)
  #plot(jagsfit)

[Package R2jags version 0.01-21 Index]