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.
Packages => Install package(s) from local zip files...
.
Error in install.packages: type == "both" cannot be used with 'repos=NULL'
,
then run the install.packages()
shown in your R console,
but add , type="source"
after repos=NULL
.
install.packages("c:/rube_0.3-11.tar.gz",type="source",repos=NULL)
.
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.
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.
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".
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: alphaTwo 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/
library(help=rube)
can be used to
show what version of rube
you are running.
nSamples
, which defaults to 30, determines what "large"
means. For parameters with greater than nSamples
elements a
random sample of nSamples
elements is shown, and clicking the
button for that parameter shows a new sample.
priorExplore()
.
rube()
now always returns a "rube" object. This can be examined using
the print() or summary() methods (which are currently equivalent). When the
bugs() run is successful, these methods show the MCMC results. This improves
on the default view of a bugs() object in several ways. There is a column
for the MCMC error, the number of digits does not default to a very low
number (as does print.bugs), and there is a "limit" parameter (with a default of 10)
which limits the number of parameters shown for vector parameters. When
bugs() fails (or if you specify check=TRUE), examining the rube object
gives the "bugsCheck" results summarizing the model, data, and initializations.
rube()
sees that bugs() has failed, it shows you the bugs() error
message, so you should rarely need to use debug=TRUE.
rube()
allowed WinBUGS to see scientific notation forms
that it does not understand was fixed.
compare()
function that takes a list of two or more
rube()
results as its first argument. The posteriors are compared
graphically.
y[i]~rnorm(log(2),pow(30,-2))
,
are now allowed as parameters to distributions. (The numeric value
will be passed to bugs(), which does not allow this construction.)
rube()
will cull variables from parameters.to.save if they are pure
data (which would otherwise cause bugs() to fail with a misleading
message).
rube()
now checks the environmental variable "BUGSDIR" to set
the bugs.directory. This is particularly useful for Vista where
you will need to install WinBUGS in a directory other than "program files".
You can do this two ways:
Sys.setenv(BUGSDIR="C:\\WINBUGS14")
.
Even better, use .First=function()Sys.setenv(BUGSDIR="C:\\WINBUGS14")
to make this run automatically each time R starts (in this workspace).
rube()
is now "always". In a large simulation study
you may want to change this to save a little time.
rube()
objects and graphically compares them.
result = rube(model, data, inits, parameters)you don't need to examine the result to know that it failed.
rube()
gives more detailed information about failures, including showing
you the WinBUGS error message when possible.
rube()
allows a data.frame as its 'data' argument by making
vectors from each column. An 'N' data value is automatically added.
rube()
doesn't bother to try to run WinBUGS if n.thinby<1.
rube()
problem with use of predefined variables such as "c" in bugs models.
rube()
problem with some forms of line continuation syntax.
rube()
problems with funny spacing in code.
rube()
now returns a rube object nearly always. For early failures,
there is an R warning and the rube
problem list indicates "model checking
as unsuccessful".
rube()
function now sets a different random number seed for WinBUGS each time you
run it. The seed is stored in the $bugs.seed component of the result, so you can use
bugs.seed=myPriorModel$bugs.seed
if you really want to re-generate identical
results. This reverses the bugs()
default of bugs.seed=NULL
which
uses the same random number sequences for every WinBUGS run.
compare()
function is improved to better handle comparing results collected
under differing conditions.
rube()
now gives an error for an LC() formula with a intercept but no prefix or with neither
prefix nor suffix.
rube()
allows you to skip specification of a varList, and defaults to an intercept only.
priPost()
now has a "sdFromGammaPrecision" distribution, which shows the distribution
of a posterior standard deviation compared to its prior when specified as a gamma distribution
on the precision scale.
priPost()
handles HyperGamma better, commented FOR()
statements are no longer expanded, unbalanced parenthesis are always an explicit error,
the compare()
function handles a wider variety of params= values, and the
mechanism to handle expressions for substitution variables is more robust.
model()
which can be applied to
a model text, or a rube result to view the model. The
'indent' parameter can be set to tab("\t"), or any other fixed set
of characters. The code lines are numbered unless 'lineNumbers' is
set to FALSE.
rube()
's 'parameters.to.save' argument to
take the value "*" which causes all parameters to be saved.
If a vector starting with "*" is used, the rest of the vector
is elements to be dropped from the parameters.to.save.
rube()
now
first checks the environmental variable "BUGSWD". This solves
a problem where getwd() is in a form that bugs() does not understand
causing WinBUGS to hang. Just specify a "normal" working directory
in this environmental variable, e.g., using Sys.setenv() to make
a setting for your whole session.
rube
objects, rather than as a list.
augment(myRubeRun,
add=list(sdRI="sqrt(varRI)",
pHat="1/(1+exp(-LO))"))
creates a new variable called 'sdRI' (assuming that 'varRI' is a scalar
parameter that has been saved in the rube output called 'myRubeRun').
And it creates a set of 'pHat' values assuming that 'LO' is a
vector variable that has been saved in the output.The sims.array, sims.matrix, sims.list, summary, mean, sd, median, and MCMCerr components of the object are all updated, but the time consuming construction of all except the first can be suppressed by using 'minimal=TRUE'. The MCMCerr and n.eff components of the output are computed by the 'coda' package, and disagree with the R2WinBUGS results (and sometimes are NA).
All three functions now use two separate functions (makeStats and makeMatList) to recompute results after constructing the new $sims.array. All three have a 'minimal=FALSE' argument which can be set to TRUE to suppress the lengthier calculations if you only want to plot the new results. For all three the MCMCerr and n.eff components of the output are computed by the 'coda' package, and disagree with the R2WinBUGS results (and sometimes are NA).
The shrink() function has a new parameter, 'dropChains', which can be used to discard some chains of a multi-chain run.
The stitch() functions gives appropriate errors and warnings if it can detect an inappropriate stich, e.g., different numbers of chains, different thinning, or burn-in for the second run. The print.summary.rube() function now shows both start and run times for stitched objects.
attr(myDtf, NS=55)
to specify the number of
subjects in a hierarchical model) are also automatically added.
rube()
now defaults to "auto", which
determines the MCMC engine, with "jags" as the other new option. With "auto",
if packlage "R2jags" is loaded, jags will be run instead of WinBUGS.
An "engine" element has been added to the rube
object.
rube()
call now includes "progress.bar=" (defaults to "gui" to
specify the jags (non-parallel) method reporting MCMC progress.
rube()
call now includes "RNGname=" (defaults to "Wichmann-Hill") to
specify the jags random number generator.
rube()
call now allows "parallel=TRUE" to invoke the parallel
MCMC routine in jags. This requires that "inits=" be a function with no arguments
(which cannot access non-local variables). It also requires that rube
attach the data,
so a warning is given if rube()
needs to temporarily
move any global variable(s) with the same name(s) as the data variables.
rube
cannot protect you from specifying
non-stochastic variables in your initialization function, so it stops
with an annotated error, if you try that.
prefix0suffix
for FOR() or LC() model constructs.
load.module("msm")
before using a model with
dmstate(), and load.module("mix")
before using a model with dnormmix().
length(y[])
instead of length(y)
.
Last updated 3/26/2014.
Please send comments to