bugs function takes data and starting values as
input. It automatically writes a bugs(data, inits, parameters.to.save, n.iter, model.file="model.txt",
n.chains=3, n.burnin=floor(n.iter / 2), n.thin=1,
saveExec=FALSE,restart=FALSE,
debug=FALSE, DIC=TRUE, digits=5, codaPkg=FALSE,
OpenBUGS.pgm=NULL,
working.directory=NULL,
clearWD=FALSE, useWINE=FALSE, WINE=NULL,
newWINE=TRUE, WINEPATH=NULL, bugs.seed=1, summary.only=FALSE,
save.history=(.Platform$OS.type == "windows" | useWINE==TRUE),
over.relax = FALSE)model.file) of the data for the data n.chains elements; each element of the
list is itself a list of starting values for the inits=NULLworking.directory.
The old convention allowing model.file to be named n.iter/2, that is,
discarding the first half of the simulations.n.thin = 1. The thinning is implemented in the OpenBUGS update phase, so
thinned samples are never stored, and they are not counted in n.burnin or
n.basename (model.file) and extension .bug in the working
directory, which must be specified. The .bug files can be large, so
users should monitor them carefully an.burnin=0,additional iterations are performed and all iterations since
the previous burnin are used (iFALSE (default), TRUE (default), compute deviance, pD,
and DIC. The results are extracted directly
from the pD =
Dbar - Dhat. If extraction fails or
if there are lessformatCFALSE (default) a bugs object
is returned, if TRUE file names of NULL, a temporary working directory via
tempdirFALSE. If WINE is used, the arguments OpenBUGS.pgm and working.directory must be given in form of Linux paths
rather than Winwhich and locate)
to get the information automatically if not given.which and locate)
to get the information automatically if not given.TRUE, only a parameter summary for very quick analyses is given,
temporary created files are not removed in that case.TRUE (the default), trace plots are generated at the end.TRUE, over-relaxed form of MCMC is used if available from OpenBUGS.codaPkg=TRUE the returned values are the names
of coda output files written by read.bugs.
If codaPkg=FALSE, the following values are returned:(n.iter-n.burnin) / n.thin)n.chains * n.keep)n.keep*n.chains simulations in
sims.matrix and sims.list (but NOT sims.array) have been randomly
permuted)n.chains*n.keep rows and one column for each element of
each saved parameter (for convenience, the n.keep*n.chains
simulations in sims.matrix and sims.list (but NOT sims.array) have
been randomly permuted)parameters.to.save and
indexes.shortroot.shortmean(deviance) + pDOpenBUGS.pgmwrite.model).bugsfunction and run it (see
Example section).debug=TRUE.print.bugs, plot.bugs, as well as
# An example model file is given in:
model.file <- system.file(package="R2OpenBUGS", "model", "schools.txt")
# Let's take a look:
file.show(model.file)
# Some example data (see ?schools for details):
data(schools)
schools
J <- nrow(schools)
y <- schools$estimate
sigma.y <- schools$sd
data <- list ("J", "y", "sigma.y")
inits <- function(){
list(theta=rnorm(J, 0, 100), mu.theta=rnorm(1, 0, 100),
sigma.theta=runif(1, 0, 100))
}
## or alternatively something like:
# inits <- list(
# list(theta=rnorm(J, 0, 90), mu.theta=rnorm(1, 0, 90),
# sigma.theta=runif(1, 0, 90)),
# list(theta=rnorm(J, 0, 100), mu.theta=rnorm(1, 0, 100),
# sigma.theta=runif(1, 0, 100))
# list(theta=rnorm(J, 0, 110), mu.theta=rnorm(1, 0, 110),
# sigma.theta=runif(1, 0, 110)))
parameters <- c("theta", "mu.theta", "sigma.theta")
## You may need to specify "OpenBUGS.pgm"
## also you need write access in the working directory:
schools.sim <- bugs(data, inits, parameters, model.file,
n.chains=3, n.iter=5000)
print(schools.sim)
plot(schools.sim)Run the code above in your browser using DataLab