Setting up OpenBugs and R

NOTE: these instructions are outdated, an update has been posted here.

WinBugs and its open source counterpart OpenBugs are the most commonly used software packages for evaluating the posterior distribution of a Bayesian model by means of a Markov Chain Monte Carlo algorithm (see e.g. my previous post for more details on MCMC algorithms).

WinBugs and OpenBugs can be used stand-alone, but many users find it more convenient to call them from R. In the following, I explain how to set up both the Bugs software and the R interface on Windows or Linux computers.

Installing OpenBugs

Installing the open bugs software should be easy (provided you are not using a Mac). Source, binaries and installation instructions for Windows and Linux can be found at OpenBugs homepage. Installing Bugs on a Mac will at the moment require to install Wine. Using the search engine of your choice will provide installation instructions. Mac users might want to consider to use Jags instead.

Installing the R / Bugs interface

There are a few options for calling Bugs from R, most noteworthy the R2WinBugs/R2OpenBUGS packages and the BRugs package. My recommendation is to start with the BRugs package, which is unfortunately not in the CRAN at the moment due to some licence incompatibilities I believe. Thus, you have to download the package “by hand”.

From the UserContributedCode at the OpenBugs page, download the latest version of the package. It may be possible that you need to rename the file to the standard R-package format which is Make sure your downloaded version fits to your R version, currently the latest RBrugs version is available as binaries for R 2.11 and R 2.12, binaries seemed to work with R 2.13.1, too, but I would recommend sticking to the “officially” supported versions. If you have to upgrade/downgrade R see my comments below. Also, there seem to be some problems with the 64 bit R versions, so choose a 32 bit R version even when you are on a 64 bit system.

Then, in the R-gui, go to Packages > Install packages from local .zip files and install the donwloaded zip file. Also, you will need the coda and the lattice packages, they are in CRAN, so if you don’t have them already installed simply type


Once that is done, try some example, e.g. the following code that is displayed when typing ?BRugs

###    Step by step example:    ###
library(BRugs) # loading BRugs

## Now setting the working directory to the examples' one:
oldwd <- getwd()

## some usual steps (like clicking in WinBUGS):
modelCheck("Ratsmodel.txt")          # check model file
modelData("Ratsdata.txt")            # read data file
modelCompile(numChains=2)            # compile model with 2 chains
modelInits(rep("Ratsinits.txt", 2))  # read init data file
modelUpdate(1000)                    # burn in
samplesSet(c("alpha0", "alpha"))     # alpha0 and alpha should be monitored
modelUpdate(1000)                    # 1000 more iterations ....

samplesStats("*")                    # the summarized results

## some plots
samplesHistory("*", mfrow = c(4, 2)) # plot the chain,
samplesDensity("alpha")              # plot the densities,
samplesBgr("alpha[1:6]")             # plot the bgr statistics, and
samplesAutoC("alpha[1:6]", 1)        # plot autocorrelations of 1st chain

## switch back to the previous working directory:

## Not run:
# Getting more (online-)help:
if (is.R())

## End(Not run)

Upgrading R

A hint: if you have to upgrade/downgrade R, the following code which I copied from this post explains an easy way to reinstall all your packages (I find that easier than moving or sharing a package folder). First, save all your package names in your current (old) R version to a file:

#--run in the old version of R
setwd("C:/Temp/") # or any other existing temp directory
packages <- installed.packages()[,"Package"]
save(packages, file="Rpackages")

and then load the names in the new version and install the packages

#--run in the new version
setwd("C:/Temp/") # or any other existing temp directory
for (p in setdiff(packages, installed.packages()[,"Package"]))

One thought on “Setting up OpenBugs and R

  1. Pingback: A simple Metropolis-Hastings MCMC in R « theoretical ecology

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s