Running Bugs from R
Running WinBugs and OpenBugs from R
The software on this page gets updated. Refresh to get latest versions. Last update: 24 Jan 2007.
To run WinBugs from R
- Write a Bugs model in a file with a .bug extension
(in your R working directory; for example, c:/bayes/)).
- Prepare the inputs to the "bugs" function and run it (see example below).
- A WinBugs14 window will pop up and R will freeze up. The model
will now run in WinBugs. It might take awhile. You will see
things happening in the Log window within WinBugs14. When WinBugs
is done, its window will close and R will work again.
- If an error message appears, re-run with the debug=TRUE option.
Then, when the model stops running, the Bugs window will stay open
and you can play with the model directly in WinBugs. When you are
ready, you can then close the WinBugs14 window and R will resume.
- To see the inferences, use the "print" and "plot" functions (see example below).
- To work with the simulations, use the "attach.all" function (see example below).
- For more information, type "help(bugs)" when in R.
To run OpenBugs from R
- Exactly the same steps as above, except that you add program="openbugs" to the call to bugs() from R (see end of the example below)
- Also the debug=TRUE option is not yet set up when you call OpenBUGS.
Example
- 8 schools analysis from Section 5.5 of "Bayesian Data Analysis"
- Put the following in the file "schools.bug" in your R working directory (it is convenient to edit this and other .bug and .R files in XEmacs):
model {
for (j in 1:J){
y[j] ~ dnorm (theta[j], tau.y[j])
theta[j] ~ dnorm (mu.theta, tau.theta)
tau.y[j] <- pow(sigma.y[j], -2)
}
mu.theta ~ dnorm (0.0, 1.0E-6)
tau.theta <- pow(sigma.theta, -2)
sigma.theta ~ dunif (0, 1000)
}
- Save this file as "schools.dat" in your R working directory.
- Do the following commands in R:
schools <- read.table ("schools.dat", header=TRUE)
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))}
parameters <- c("theta", "mu.theta", "sigma.theta")
schools.sim <- bugs (data, inits, parameters, "schools.bug", n.chains=3, n.iter=1000)
- The WinBugs14 window will open, run the script (this will take a
few seconds, and you'll see stuff happening in the Log window in
WinBugs14), close itself, and return to R.
- Do the following commands in R:
print (schools.sim)
plot (schools.sim)
- You will see this on the R console and this in the graphics window. (For an example of a model with more parameters, see
here and here.)
- The printed output for this example is schools.sim$summary:
summaries of the parameter inferences and convergence monitoring.
- Do the following command in R:
attach.bugs (schools.sim)
- The simulated parameter values are now saved in R as:
"theta": a 1500 x 8 matrix, representing 1500 simulations of the vector theta
"mu.theta": a vector of length 1500 of simulations of mu.theta
"sigma.theta": a vector of length 1500 of simulations of sigma.theta.
- For more information, type "help(bugs)" when in R.
- To fit using OpenBUGS:
library ("BRugs")
schools.sim <- bugs (data, inits, parameters, "schools.bug", n.chains=3, n.iter=1000, program="openbugs")
Back to Andrew Gelman's homepage.