R‎ > ‎R‎ > ‎

Running OpenBUGS on Mac Using Wine

WinBUGS is no longer under development, and has been superseded by OpenBUGS (www.openbugs.info). 

Up until now, to use OpenBUGS on a Mac, you needed to run R under Wine and then call OpenBUGS via the BRugs package. Now, the R2WinBUGS package has been updated to work with OpenBUGS and allows R, running on a Mac, to call OpenBUGS using Wine.

So far, OpenBUGS seems to run much faster and more efficiently than WinBUGS.

Here's how to get it running.

1. If you don't have Wine up and running, follows steps 1-6 on this page
2. Download OpenBUGS321setup.exe (windows executable) and place it in your default directory for Wine programs (mine is "~/.wine/drive_c")
3. Run the OpenBugs installer by:
a. Opening XQuartz and a Terminal Window
b. cd to the directory where you place the OpenBUGS executable you downloaded in step 2. 
c. type: wine OpenBUGS321setup.exe
d. wait for a while and then follow the prompts to install - remember the directory you created to install it
(mine is "~/[username]/.wine/drive_c/Program Files/OpenBUGS321")
e. there may be an error at the end, this is OK. Close down the Terminal Window
4. Test OpenBUGS by opening a new XQuartz Terminal window typing: wine C:\\Program\ Files\\OpenBUGS321\\OpenBUGS.exe
5. Get the R2OpenBUGS source package from here.
6. Install the package. 
a. From the R GUI choose the Packages and Data menu, and select install packages. 
b. Select "Local Source Package" from the drop down. 
c. Click Install. 
d. Locate the R2OpenBUGS.tar.gz file you downloaded in step 5 and click open
7. Test OpenBugs by running this R script:

#Load the OpenBUGS Package - make sure XQuartz is running
library(R2OpenBUGS)

#schools data in the R2OpenBUGS library
data(schools)

#define the model
nummodel <- function(){
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)
}

# write the model code out to a file
write.model(nummodel, "nummodel.txt")
model.file1 = paste(getwd(),"nummodel.txt", sep="/")
## and let's take a look:
file.show("nummodel.txt")

#prepare the data for input into OpenBUGS
J <- nrow(schools)
y <- schools$estimate
sigma.y <- schools$sd
data <- list ("J", "y", "sigma.y")

#initialization of variables
inits <- function(){
  list(theta = rnorm(J, 0, 100), mu.theta = rnorm(1, 0, 100), sigma.theta = runif(1, 0, 100))}

#set the WINE working directory and the directory to OpenBUGS - change the OpenBUGS.exe location as necessary
WINE="/opt/local/bin/wine"
WINEPATH="/opt/local/bin/winepath"
OpenBUGS.pgm="/Users/[username]/.wine/drive_c/Program Files/OpenBUGS321/OpenBUGS.exe"

#these are the parameters to save
parameters = c("theta", "mu.theta", "sigma.theta")

#run the model
schools.sim <- bugs(data, inits, model.file = model.file1,parameters=parameters,n.chains = 3, n.iter = 1000, OpenBUGS.pgm=OpenBUGS.pgm, WINE=WINE, WINEPATH=WINEPATH,useWINE=T)

#R will pause. When model is complete a prompt will reappear
print(schools.sim)

7. Output from step 6 should be:
Inference for Bugs model at "/Users/bigbird/Documents/Duke/2paper/rcode/nummodel.txt", 
Current: 3 chains, each with 1000 iterations (first 500 discarded)
Cumulative: n.sims = 1500 iterations saved
            mean  sd 2.5%  25%  50%  75% 97.5% Rhat n.eff
theta[1]    11.7 7.5 -1.4  7.4 11.5 15.0  29.3  1.1   350
theta[2]     9.0 6.1 -4.2  5.2 10.0 12.4  19.2  1.1    21
theta[3]     7.7 7.2 -9.4  3.2  9.1 12.4  18.8  1.2    18
theta[4]     8.8 6.3 -4.7  4.6 10.0 12.4  20.1  1.1    22
theta[5]     6.8 6.6 -8.8  2.6  8.0 12.0  16.7  1.3    11
theta[6]     7.6 6.6 -7.4  3.1  8.9 12.3  17.4  1.2    13
theta[7]    11.2 6.1 -0.8  7.7 11.5 14.4  24.0  1.1   160
theta[8]     9.7 7.1 -5.0  5.6 10.7 13.1  24.5  1.1    54
mu.theta     8.9 5.1 -1.9  5.9  9.8 12.4  17.2  1.2    15
sigma.theta  5.2 5.3  0.0  0.9  3.6  8.1  17.9  1.7     6
deviance    60.5 2.0 57.1 59.3 60.4 61.4  64.9  1.0   200

For each parameter, n.eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor (at convergence, Rhat=1).

DIC info (using the rule, pD = Dbar-Dhat)
pD = 63.0 and DIC = 2.5
DIC is an estimate of expected predictive error (lower deviance is better).

Comments