*** This work has been supported by a grant from the Spencer Foundation (#201400002). The views expressed are those of the author and do not necessarily reflect those of the Spencer Foundation. ***
It seems that the heir to WinBUGS is Stan. With Stan, reasonably complex Bayesian models can be expressed in a compact way and easily estimated. It is good software, and it is under active development to further improve it.
I have a small quibble about RStan, the R interface to Stan. RStan would be much improved if its default behavior was to run one MCMC chain per core. For software that prides itself on speed – Stan goes to the trouble translating the Stan modeling specification into a stand-alone C++ program for execution – it seems a little odd that the extra cores present on nearly all modern machines would not be put to use by default.
Currently, running chains in parallel is possible, but only with platform dependent boilerplate code. For example, the RStan Quick Start Guide gives an
mclapply example for Mac and Linux users and a
parLapply example for Windows users. The boilerplate nature of the code makes it cumbersome to fit models several times, and the platform dependent nature of the examples makes it difficult to share code between platforms.
To address this issue, I have implemented the boilerplate code from the Quick Start Guide in a cross-platform R package:
rstanmulticore. The syntax is easy. Simply replace a call to
with a call to
pstan version will compile the model, distribute the compiled models to separate cores for a parallel run, and then recombine the results as if the code had execute serially. Since I used
parLapply to distribute the work to multiple cores,
pstan will run on Windows, Linux, and Mac.
At least as far as I have pushed
pstan, it works well for me. Your needs may differ. I would appreciate feedback and suggestions on how to improve it. You can access it via GitHub here. Installation instructions and a brief usage example are below.
Installation from GitHub
Step 0.A : If you do not already have
rstan installed, install it using the instructions here.
Step 0.B: If you do not already have
devtools installed, install it using the instructions here.
Step 1: Install
rstanmulticore directly from my GitHub repository using
> library(devtools) > install_github('nathanvan/rstanmulticore') Downloading github repo nathanvan/[email protected] Installing rstanmulticore "C:/PROGRA~1/R/R-31~1.3/bin/x64/R" --vanilla CMD INSTALL "C:/Users/vanhoudnos-nathan/AppData/Local/Temp/RtmpQBcRKa/devtools924351029d0/nathanvan-rstanmulticore-c7f9d4e" --library="C:/Users/vanhoudnos-nathan/Documents/R/win-library/3.1" --install-tests * installing *source* package 'rstanmulticore' ... ** R ** tests ** preparing package for lazy loading ** help *** installing help indices ** building package indices ** testing if installed package can be loaded *** arch - i386 *** arch - x64 * DONE (rstanmulticore)
A usage example
We begin with the default “Eight Schools” example from the Quick Start Guide using the default
library(stan) ## Loading required package: Rcpp ## Loading required package: inline ## ## Attaching package: 'inline' ## ## The following object is masked from 'package:Rcpp': ## ## registerPlugin ## ## rstan (Version 2.6.0, packaged: 2015-02-06 21:02:34 UTC, GitRev: 198082f07a60) ## The data to analyze (Yes, it is very little!) schools_dat
stan is pretty verbose.
I chose to make
pstan less verbose. By default,
pstan reports sparse progress information to the R console and the more detailed information is redirected to a file,
stan-debug-*, that is created in the current working directory. (If you wish to see the detailed info in real time, use Use
tail -f in your shell.)
Usage is as follows:
library(rstanmulticore) ## Loading required package: parallel fit.parallel
If, in the unlikely case, you want no console output and no file redirection, you can pass
pdebug = FALSE to
help(pstan) for details.
Note that, as promised, the output — the actually samples drawn from the posterior — of
pstan is identical to that of
all.equal( [email protected]$samples, [email protected]$samples ) ##  TRUE
A request for help
rstanmulticore works well for my needs, but it may not work for you. If it does not work for you, please let me know and I’ll do my best to accommodate you. Pull requests and additional test cases are most welcome!