Setting up OpenBugs and R

Since a few things have changed since my previous post on setting up OpenBugs and R, notably that the BRugs package is back in CRAN, here’s an update on that topic.

Installing OpenBugs

Important: first, install the open bugs software. 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. (an alternative for Mac users is to use Jags instead, which offers similar, but not identical functionalities).

Installing the R / Bugs interface

Make sure that you have a recent and supported R version installed. 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 you get by


Also, you will need the coda and the lattice packages, so if you don’t have them already installed load them by


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

## Prepare the example files in a temporary directory
exfiles <- dir(options()$OpenBUGSExamples, pattern="^Rats.*txt$", full.names=TRUE)
ok <- file.copy(exfiles, tempdir())

## Now setting the working directory to the temporary one:
oldwd <- setwd(tempdir())

## 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: Setting up OpenBugs and 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