bugs | R Documentation |
The bugs
function takes data and starting values as
input. It automatically writes a WinBUGS script, calls the model, and
saves the simulations for easy access in R or S-PLUS.
bugs(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.chains * (n.iter - n.burnin) / n.sims)),
n.sims = 1000, bin=(n.iter - n.burnin) / n.thin,
debug=FALSE, DIC=TRUE, digits=5, codaPkg=FALSE,
bugs.directory="c:/Program Files/WinBUGS14/",
program=c("WinBUGS", "OpenBUGS", "winbugs", "openbugs"),
working.directory=NULL, clearWD=FALSE,
useWINE=.Platform$OS.type != "windows", WINE=NULL,
newWINE=TRUE, WINEPATH=NULL, bugs.seed=NULL, summary.only=FALSE,
save.history=!summary.only, over.relax = FALSE)
data |
either a named list (names corresponding to variable names
in the |
inits |
a list with |
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 WinBUGS code.
The extension can be either ‘.bug’ or ‘.txt’.
If the extension is ‘.bug’ and |
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.thin |
thinning rate. Must be a positive integer. Set
|
n.sims |
The approximate number of simulations to keep after thinning. |
bin |
number of iterations between saving of results
(i.e. the coda files are saved after each |
debug |
if |
DIC |
logical; if |
digits |
number of significant digits used for WinBUGS input, see
|
codaPkg |
logical; if |
bugs.directory |
directory that contains the WinBUGS executable.
If the global option |
program |
the program to use, either
|
working.directory |
sets working directory during execution of
this function; WinBUGS' in- and output will be stored in this
directory; if |
clearWD |
logical; indicating whether the files ‘data.txt’,
‘inits[1:n.chains].txt’, ‘log.odc’, ‘codaIndex.txt’,
and ‘coda[1:nchains].txt’ should be removed after WinBUGS has
finished. If set to |
useWINE |
logical; attempt to use the Wine emulator to run
WinBUGS, defaults to |
WINE |
character, path to ‘wine’ binary file, it is
tried hard (by a guess and the utilities |
newWINE |
Use new versions of Wine that have ‘winepath’ utility |
WINEPATH |
character, path to ‘winepath’ binary file, it is
tried hard (by a guess and the utilities |
bugs.seed |
random seed for WinBUGS (default is no seed) |
summary.only |
If |
save.history |
If |
over.relax |
If |
To run:
Write a BUGS model in an ASCII file (hint: use
write.model
).
Go into R / S-PLUS.
Prepare the inputs for the bugs
function and run it (see
Example section).
A WinBUGS window will pop up and R / S-PLUS will freeze up. The model will now run in WinBUGS. It might take awhile. You will see things happening in the Log window within WinBUGS. When WinBUGS is done, its window will close and R / S-PLUS will work again.
If an error message appears, re-run with debug=TRUE
.
BUGS version support:
default
via argument program="OpenBUGS"
Operation system support:
no problem
possible with Wine emulation via useWINE=TRUE
, but
only for WinBUGS 1.4.*
If useWINE=TRUE
is used, all paths (such as
working.directory
and model.file
, must be given in
native (Unix) style, but bugs.directory
can be given in
Windows path style (e.g. “c:/Program Files/WinBUGS14/”) or
native (Unix) style
(e.g. “/path/to/wine/folder/dosdevices/c:/Program
Files/WinBUGS14”). This is done to achieve greatest portability with
default argument value for bugs.directory
.
If codaPkg=TRUE
the returned values are the names
of coda output files written by WinBUGS containing
the Markov Chain Monte Carlo output in the CODA format.
This is useful for direct access with read.bugs
.
If codaPkg=FALSE
, the following values are returned:
n.chains |
see Section ‘Arguments’ |
n.iter |
see Section ‘Arguments’ |
n.burnin |
see Section ‘Arguments’ |
n.thin |
see Section ‘Arguments’ |
n.keep |
number of iterations kept per chain (equal to
|
n.sims |
number of posterior simulations (equal to
|
sims.array |
3-way array of simulation output, with dimensions n.keep, n.chains, and length of combined parameter vector |
sims.list |
list of simulated parameters:
for each scalar parameter, a vector of length n.sims
for each vector parameter, a 2-way array of simulations,
for each matrix parameter, a 3-way array of simulations, etc.
(for convenience, the |
sims.matrix |
matrix of simulation output, with
|
summary |
summary statistics and convergence information for each saved parameter. |
mean |
a list of the estimated parameter means |
sd |
a list of the estimated parameter standard deviations |
median |
a list of the estimated parameter medians |
root.short |
names of argument |
long.short |
indexes; programming stuff |
dimension.short |
dimension of |
indexes.short |
indexes of |
last.values |
list of simulations from the most recent iteration; they can be used as starting points if you wish to run WinBUGS for further iterations |
pD |
an estimate of the effective number of parameters, for calculations see the section “Arguments”. |
DIC |
|
Andrew Gelman, gelman@stat.columbia.edu; modifications and packaged by Sibylle Sturtz, sturtz@statistik.tu-dortmund.de, and Uwe Ligges.
Gelman, A., Carlin, J.B., Stern, H.S., Rubin, D.B. (2003): Bayesian Data Analysis, 2nd edition, CRC Press.
Sturtz, S., Ligges, U., Gelman, A. (2005): R2WinBUGS: A Package for Running WinBUGS from R. Journal of Statistical Software 12(3), 1-16.
print.bugs
, plot.bugs
, as well as
coda and BRugs packages
# An example model file is given in:
model.file <- system.file(package="R2WinBUGS", "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=J, y=y, sigma.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")
## Not run:
## You may need to edit "bugs.directory",
## also you need write access in the working directory:
schools.sim <- bugs(data, inits, parameters, model.file,
n.chains=3, n.iter=5000,
bugs.directory="c:/Program Files/WinBUGS14/")
print(schools.sim)
plot(schools.sim)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.