runjags (version 0.9.0-4)

autorun.jagsfile: Read a User Specified Model in a WinBUGS Type Textfile or Character Variable, and Run the Simulation in JAGS with Automatically Calculated Run Length and Convergence Diagnostics

Description

Runs a user specified JAGS (similar to WinBUGS) model in a WinBUGS type Textfile from within R, returning a list of the MCMC chain(s) along with convergence diagnostics, autocorrelation diagnostics and monitored variable summaries. This function is a wrapper for run.jagsfile with autorun==TRUE.

Usage

autorun.jagsfile(path=stop("No path or model string supplied"), 
   datalist=NA, initlist=NA, n.chains=NA, data=NA, model=NA, 
   inits=NA, monitor=NA, call.jags=TRUE, ...)

Arguments

path
either a relative or absolute path to a textfile (including the file extension) containing a model in the JAGS language and possibly monitored variable names, data and/or initial values, or a character string of the same. No default. The model must be s
datalist
an optional named list containing variables used as data, or alternatively a function (with no arguments) that returns a named list. If any variables are specified in the model block using '#data# variable', the value for the corresponding named variable
initlist
an optional named list containing variables used as initial values, or alternatively a function (with a single argument representing the chain number) that returns a named list. If any variables are specified in the model block using '#inits# variable',
n.chains
the number of chains to use with the simulation. More chains will improve the sensitivity of the convergence diagnostic, but will cause the simulation to run more slowly. If NA, the number of chains will be taken from the number of inits blocks in the m
data
OPTIONAL character vector in the R dump format (or named list) containing the data. If supplied (!=NA), all data in the model file is ignored. Default NA.
model
OPTIONAL model in JAGS syntax. If supplied (!=NA), the model in the model file is ignored. Default NA.
inits
OPTIONAL character vector(s) in the R dump format containing the initial value(s). If supplied (!=NA), all inits in the model file are ignored. Default NA.
monitor
OPTIONAL character vector containing the monitored variables. If supplied (!=NA), all monitor statements in the model block are ignored. Default NA.
call.jags
option results in either simulation being called if TRUE, or returns a named list of the data, model, initial values, monitored variables and number of chains (which can be supplied to autorun.jags) if FALSE.
...
other options to be passed directly to autorun.jags (see autorun.jags).

Value

  • The output of autorun.jags. See the help file autorun.jags for more information.

See Also

run.jagsfile autorun.jags run.jags read.winbugs

Examples

Run this code
# run a model to calculate the intercept and slope of the expression y = m x + c, assuming normal observation errors for y:

# Model in the JAGS format
model <- "model {
for(i in 1 : N){ #data# N
Y[i] ~ dnorm(true.y[i], precision); #data# Y
true.y[i] <- (m * X[i]) + c; #data# X
}
m ~ dunif(-1000,1000); #inits# m
c ~ dunif(-1000,1000);
precision ~ dexp(1);
#monitor# m, c, precision
}"

# Simulate the data
X <- 1:100
Y <- rnorm(length(X), 2*X + 10, 1)
N <- length(X)

initfunction <- function(chain) return(switch(chain, "1"=list(m=-10), "2"=list(m=10)))

results <- autorun.jagsfile(model, n.chains=2, initlist=initfunction)

# Analyse the results
results$summary

Run the code above in your browser using DataLab