R‎ > ‎R‎ > ‎

Running WinBUGS on MacOSX via Wine

posted Nov 12, 2010, 1:25 AM by David Eagle   [ updated Oct 17, 2013, 7:50 AM ]
WinBUGS is a program that allows for relatively efficient methods of estimating Bayesian models using Gibbs and other related sampling techniques. However, it does not run natively on MacOSX. OpenBUGS, the open source alternative, does not have all of the same feature set as WinBUGS. 

What is a Mac Owner to do? Run WinBUGS under Wine (an open-source Windows emulator). While WinBUGS can be run as a stand-alone package, it is most efficient to call from R. Here's how:

A. INSTALL XQuartz (the open source X-Windows System needed to run Windows Programs)
1. Go to the X-Quartz website and download and install their latest version of XQuartz http://xquartz.macosforge.org/.
2. Log in/Log out of your computer to activate the new X11 environment.
3. Go to step B

B. Install MacPorts, Freetype, and Wine
1. Go to www.macports.org and install the macports software. This enables you to install open source linux programs easily, without self-compiling. THIS REQUIRES XCODE to work, so get it from apple.com if you haven't installed Xcode.
2. Once MacPorts is installed, open an X window terminal and type the following. This will install freetype, a good font renderer for Xserver.

sudo port install freetype

3. Symbolically link the freetype directory to /usr/lib to allow X windows to find it (the period in the last line is needed).

cd /usr/lib
sudo ln -s /opt/local/lib/libfree* .

4. Install the latest version of Wine by issuing the command. This will create a <USERNAME>/.wine directory, which will become the default directory in which Wine is installed. Detailed Wine installation instructions here.

sudo port install wine-devel

You may also want to type this command to save disk space:

sudo port clean all

6. Make sure wine is installed. Open an XQuartz terminal window and type:

wine --version
6. Download WinBUGS14.exe from here. Copy the entire directory to ~/.wine/drive_c/Program Files.
7. Verify that WinBUGS is running by opening a XQuartz terminal window and typing:

wine "C:\Program Files\WinBUGS14\WinBUGS14.exe"

1. Download the current patch from: http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/contents.shtml. These should be .txt files.
2. Open WinBUGS, click File Open, choose ".txt" for type of file and find the patch key and open it.
3. Go to Tools > Decode, then click Decode All.
4. Quit WinBUGS, reopen WinBUGS.
5. Download the immortality key from the link in 1. Again, just like in steps 2-4, decode this patch and restart WinBUGS

1. First, if you don't have a ~/.Renviron file create it by:
2. Open a text editor and enter the following into it:
BUGS.DIR="/Users/<USERNAME>/.wine/drive_c/Program Files/WinBUGS14/"
3. Save this file as ~/.Renviron
4. Before running a WinBUGS program through R, start XQuartz.app 
5. Run a sample file to test if it works:

# An example model file is given in:
model.file <- system.file(package = "R2WinBUGS", "model", "schools.txt")
# Let's take a look:
# Some example data (see ?schools for details):
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")
## Not run: 
## both write access in the working directory and package BRugs required:
schools.sim <- bugs(data, inits, parameters, model.file, n.chains=3, n.iter=1000,n.thin=10,n.burnin=10,bin=1,DIC=F,debug=F)