PMean: Using BUGS within the R programming environment

Steve Simon


I am giving a talk today for the Kansas City R Users group about BUGS (Bayes Using Gibbs Sampler). I have already written extensively about BUGS and the interface to BUGS from within the R programming environment


Here’s a simple practical example. You are running a randomized clinical trial that needs to recruit 350 patients. You’re not sure how long it will take to recruit this many patients. You’re thinking it will take three years to recruit that many patients. This implies that you will be able to find a new patient every 3.1 days on average. There is substantial uncertainty about this estimate


You combine your prior beliefs with the existing data to get a posterior distribution of the waiting time

This use of prior distributions is extremely controversial

Notice that this represents a simple linear extrapolation of the waiting times. These graphs come from a webpage I wrote a while back.

The mathematics of Bayesian data analysis is quite complex. Here’s a description on the Wikipedia page on posterior distributions.

The denominator

Back in the early 1990s statisticians discovered a new computer simulation approach that avoids the complexities of the normalizing constant. It was actually a rediscovery of methods developed much earlier

The simulation approach has two issues that you must account for at all times. First


Plot of twochains{width=“405” height=“437”}

This example from a webpage discussing segmented linear regression models shows that it is naive to expect convergence after a thousand simulations and that perhaps as many as 3,000 simulations should be run before convergence.

The software to run these simulations was developed by a group headed by David Spiegelhalter. Their program

The acronym BUGS stands for Bayes Using Gibbs Sampler and the acronym JAGS stands for Just Another Gibbs Sampler.

If you want to run WinBUGS

I have recently been using JAGS and the R interface to JAGS (rjags). It seems to have fewer compatibility issues and the error messages are a bit less cryptic. Here’s an example of how to run JAGS from within R.

Why would you want to learn Bayesian data analysis software? Bayesian data analysis is moderately complex

Bayesian methods are especially useful for multi-level models (also known as mixed models)