jags {R2jags} | R Documentation |
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.
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) / 1000)), DIC=TRUE, 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) / 1000)), DIC=TRUE, jags.path="", working.directory=NULL, clearWD=TRUE, refresh = n.iter/50)
data |
a vector or list of the names of the data objects used by the model, or a list of the data objects themselves. |
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. If n.burnin is 0,
jags() will run 100 iterations for adaption. |
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. |
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” |
To run:
jags
function and run it (see
Example section).
BUGS version support:
Yu-Sung Su ys463@columbia.edu, Masanao Yajima yajima@stat.columbia.edu
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.
# 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) # or to use some plots in coda # use as.mcmmc to convert rjags object into mcmc.list jagsfit.mcmc <- as.mcmc(jagsfit) ## now we can use the plotting methods from coda xyplot(jagsfit.mcmc) densityplot(jagsfit.mcmc) # 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() or do the following dic.samples(jagsfit.upd$model, n.iter=1000, type="pD") # attach jags object into search path see "attach.bugs" for details attach.jags(jagsfit.upd) # this will show a 3-way array of the bugs.sim object, for example: mu # detach jags object into search path see "attach.bugs" for details detach.jags() # 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:/") ## NOT RUN HERE #jagsfit <- jags2(data=jags.data, inits=jags.inits, jags.params, # n.iter=5000, model.file=model.file) #print(jagsfit) #plot(jagsfit)