R Package: rube

Overview     Manual     Vista/7 notes/Macintosh     Examples     Version history     Notes for the latest version


Overview

The rube package is a Really Useful WinBUGS and JAGS Enhancer, which greatly facilitates the efficient and organized writing and running of Bayesian analyses using WinBUGS or JAGS from R.

The current rube package requires R version 3.4.1 or above (the older versions work with 2.15.0 or above), so go to www.r-project.org to load it if you are using an old version. The current version of rube is rube_0.3-11.

These procedures will install other needed packages, including stringr and R2WinBUGS. In either case, you must also use library(rube) to load it in each session.

In case you are interested, the full code is at code directory.

Manual

The rube manual is in rubeManual.pdf. Please let me know of bugs, confusing input or output, deficiencies in documentation, and/or whatever you have on your wish-list.

Windows Vista/7/Macintosh notes

As mentioned in the WinBUGS documentation, Windows Vista and Windows 7 do not allow writing to the "Program Files" folder, but WinBUGS tries to do this. Therefore on these operating systems, you should install WinBUGS into a folder such as "c:\\WinBUGS14". If you do this you will need to explicitly tell rube() and getBugsExamples() where you installed WinBUGS. Alternatively, you may be able to get everything to work if you start R with "Run as Administrator". See the pdf documentation for details.

Rube runs well on a Macintosh, but you must use jags, not WinBUGS. As of the beginning of July 2014, the creators of jags have not released a Mavericks version, so you must run the Snow Leopard version of R on your Mavericks machine.

Note for Macintosh Yosemite:
If you get an error message like:
jags failed: call: dyn.load(file, DLLpath = DLLpath, ...) error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.1/Resources/library/tcltk/libs/tcltk.so': dlopen(/Library/Frameworks/R.framework/Versions/3.1/Resources/library/tcltk/libs/tcltk.so, 10): Library not loaded: /opt/X11/lib/libX11.6.dylib Referenced from: /Library/Frameworks/R.framework/Versions/3.1/Resources/library/tcltk/libs/tcltk.so Reason: image not found
then you have run in to the fact the Yosemite changes where X11 and Quartz are installed relative to earlier OS10 versions. The quick fix is to add

, progress.bar="none"
to all of your rube() calls.

Instructions for running rube on a Macintosh using a Windows emulator or CMU's Virtual Andrew are included in the main rube documentation file.

Rube runs fine from within R-Studio on either a PC or a Mac. The R-Studio limitation is that interactive graphics lik p3() cannot be "zoomed".

Examples

A good way to learn/play is to use the rube function getBugsExample() to run the built-in WinBUGS examples. This function reads an example (by default from the standard location) from its .odc file, and returns a list object with the model, data, and initializations all in a form ready for rube to use. If multiples are present they are all loaded. If you want to put the model in a text file you can use code like this:
write(ali$model1, file="aliModel.txt")
Critically, the getBugsExample() function automatically corrects for the differences in array ordering between the examples and how R works.

Here are examples of how to use rube based on the "Aligators" example that comes with WinBUGS:

### Find any example as a ".odc" file in the WinBUGS example directory.
### Load the model, data, and initializations from that file:
library(rube)
ali <- getBugsExample("Aligators")
names(ali)  #  this example has only one model and one data set and one initialization.

### Show a summary of the model:
with(ali, summary(rube(model=model1, data=data1, inits=inits1)))
# No problems were found, and a nice summary of the model is produced:
## Rube Summary:
## For variables: i, j, k 
## 
## Assignments (logicals):
## alpha[], beta[,], gamma[,], mu[,,], b[,], g[,] 
## 
## Constants:
##   Size Min Max Mean SD NAs
## I    1            4      0
## J    1            2      0
## K    1            5      0
## 
## Data:
##   Distr  Size NAs Parameters (mean,sd) Initial Value(s) [Range] Flags
## X dpois 4x2x5   0            mu[i,j,k]  5.475 +/- 5.588 [0, 23]      
## 
## Stochastics:
##        Distr Size NAs  Parameters (mean,sd) Initial Value(s) [Range] Flags
## alpha  dnorm    5   1 0, 1e-05 (0, 316.228)           0 +/- 0 [0, 0]      
## beta   dnorm  4x5   8 0, 1e-05 (0, 316.228)           0 +/- 0 [0, 0]      
## gamma  dnorm  2x5   6 0, 1e-05 (0, 316.228)           0 +/- 0 [0, 0]      
## lambda dnorm  4x2   0 0, 1e-05 (0, 316.228)           0 +/- 0 [0, 0]      
## 
## No problems detected!


### Run the model through bugs by specifying parameters-to-save.
result = with(ali, rube(model1, data1, inits1, 
                        parameters.to.save=c("alpha","beta"), n.chains=1))
summary(result)
## Rube Summary:
## Run at 2010-08-10 08:04 and taking 2.98 secs 
## Based on 1000 kept iterations (n.thin=1) after burn-in of 1000 iterations
##             mean    sd MCMCerr   2.5%   25%   50%    75%    97.5%
## alpha[2]  -1.847 0.509  0.0690 -2.885 -2.14 -1.82 -1.496 -0.94746
## alpha[3]  -2.650 0.811  0.1017 -4.779 -3.01 -2.56 -2.135 -1.36475
## alpha[4]  -2.104 0.554  0.0341 -3.281 -2.47 -2.09 -1.708 -1.05980
## alpha[5]  -0.772 0.377  0.0327 -1.507 -1.01 -0.78 -0.528 -0.00548
## beta[2,2]  2.754 0.605  0.0776  1.688  2.33  2.68  3.139  3.98512
## beta[3,2]  3.012 0.618  0.0795  1.972  2.54  2.94  3.427  4.27110
## beta[4,2]  1.759 0.596  0.0732  0.679  1.34  1.72  2.126  3.02617
## 
## DIC = 191.816

p3(result) # produces graphical summaries


### bugs() wants a function for the initializations if you are 
### running multiple chains:
ali$inits
# $alpha
# [1] NA  0  0  0  0
# ...
simpleInit = function() {
  init = ali$inits
  init$alpha[2:5] = rnorm(4, 0, 1)
  return(init)
}
result3 = with(ali, rube(model1, data1, simpleInit, 
                         c("alpha","beta","gamma"), 
                         n.burn=1000, n.iter=9000))
p3(result3)

### Note that rube helps you document your models:
result3$startTime
result3$runTime
result3$model

### Demonstrate problem detection:
write(ali$model1, "aliModel.txt")
### Now, screw something(s) up in aliModel.txt, e.g.
### change one alpha[k] to alpha[k,m] and change a ")" to "("
### and misspell a function or density.
summary(rube("aliModel.txt", ali$data1, ali$inits1))
## You'll get something like this:
## Rube Summary:
## 
## No assignments.
## 
## No constants.
## 
## No data.
## 
## Stochastics:
## None
## 
## Problems:
## At line 12 in $model: Distribution must be ~dfun(...) or ~dfun(...)I(...). 
## beta[i, k] ~ dnorm(0, 0.00001( 
## At line 28 in $model: rpois is not on the list of valid distributions 
## X[i, j, k] ~ rpois(mu[i, j, k])

### Fixing errors may take multiple passes.  For my version of the above
### example, fixing the two errors then re-running rube() gives:
## Problems:
## Variable(s) with inconsistent dimensions: alpha
Two more examples are in
basicExample.R and Rasch.R.

A more complex example on a 4-parameter-logistic curve fit with random effects and covariates is at http://www.stat.cmu.edu/~hseltman/TrajBUGS/

Version history

Note that the R command library(help=rube) can be used to show what version of rube you are running.
  • Version 0.2-17, 27 November 2012
  • Version 0.2-17, 28 November 2012
  • Version 0.2-18, 13 February 2013
  • Version 0.3-1, 5 September, 2013
  • Version 0.3-2, 20 September, 2013
  • Version 0.3-3, 8 October, 2013
  • Version 0.3-4, 14 October, 2013
  • Version 0.3-5, 26 March, 2014
  • Version 0.3-6, 24 April, 2014
  • Version 0.3-7, 19 June, 2014
  • Version 0.3-8, 7 November, 2014
  • Version 0.3-9, 14 July, 2015 + 28 September 2017
  • Version 0.3-10, 29 November, 2017
  • Version 0.3-11, 23 March, 2018