runjags (version 0.9.0-4)

run.jagsfile: Read a User Specified Model in a WinBUGS Type Textfile or Character Variable, and Run the Simulation in JAGS from Within R

Description

Runs a user specified JAGS (similar to WinBUGS) model in a WinBUGS type textfile or character variable from within R, returning a list of the MCMC chain(s) along with optional convergence diagnostics, autocorrelation diagnostics and monitored variable summaries. JAGS is called using the lower level function run.jags.

Usage

run.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, 
   autorun=FALSE, ...)

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 run.jags) if FALSE.
autorun
option to call autorun.jags rather than run.jags for the simulation, which allows automatic calculation of the necessary run length and convergence diagnostics. If TRUE, burnin, sample and check.conv are ignored. See also
...
other options to be passed directly to run.jags (see run.jags) or autorun.jags (see autorun.jags).

Value

  • The output of run.jags (or autorun.jags if autorun==TRUE). See the help file run.jags or autorun.jags for more information.

See Also

autorun.jagsfile run.jags autorun.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 <- run.jagsfile(model, n.chains=2, initlist=initfunction)

# Analyse the results
results$summary

Run the code above in your browser using DataCamp Workspace