APPLIED BAYESIAN MODELING
ICPSR SUMMER PROGRAM 2019



Welcome to the website for my workshop on Applied Bayesian Modeling at the 2019 ICPSR Summer Program. This page contains a variety of information on how to fit and present Bayesian models using JAGS and R and some information specifically for Mac users on how to install JAGS and related programs to use Bayesian techniques in R.

The video below provides some information about the content and learning goals of the workshop.



Workshop materials

Please email me if you have any questions about the material on this page or if you discover any errors. Additional materials for participants will be distributed during the workshop via MBox.

How to set up JAGS and R on a Mac

  1. Install the most recent version of R from the CRAN website.
  2. Download and install RStudio™.
  3. Install Clang (clang-8.0.0.pkg) and GNU Fortran (gfortran-6.1.pkg.dmg) from the CRAN tools directory.
  4. Now install JAGS version 4.3.0 (JAGS-4.3.0.dmg) from Martyn Plummer's repository. Detailed instructions quoted from the JAGS readme file:
    1. Download the disk image from the JAGS website.
    2. Double click on the disk image to mount (this may not be required).
    3. Double click on the 'JAGS-4.3.0.mpkg' file within the mounted disk image.
    4. Follow the instructions in the installer. If you receive a warning that this software cannot be installed because it comes from an unidentified developer, you need to go to System Preferences > Security & Privacy, and authorize the installation there before proceeding.
    5. Authenticate as the administrative user. The first user you create when setting up Mac OS X has administrator privileges by default.
  5. Start the Terminal and type jags to see if you receive the message: Welcome to JAGS 4.3.0.
  6. Open R and install the packages R2jags, coda, R2WinBUGS, lattice, and rjags, by typing
    install.packages(c("R2jags", "coda", "R2WinBUGS", "lattice", "rjags"))
    in the R command line.
  7. Before using R2jags the first time, you might need to set a random number generator seed. To do this, type
    set.seed(123)
    directly in R. (You can choose any number here instead of 123.) To replicate Bayesian estimates, make sure you include such seeds for random number generation in your script. We discuss replication of Bayesian estimates in more detail throughout the workshop.
  8. Use a scientific text editor for writing code. I recommend Sublime Text or Atom. RStudio™ is ideal for using R, but a separate text editor can be useful for manipulating JAGS model code and looking at occasional files.

How to set up WinBUGS/OpenBUGS and R on a Mac

WinBUGS and OpenBUGS can be used on the Mac, but need emulation software to run. This requires a few other programs to be installed, some time, and a good internet connection. The instructions below are a modified version of a tutorial by Jarrett Byrnes and seem to work well for most users. For most applications in this workshop however, using JAGS via R will be faster and usually more convenient. Note: make sure you have a good internet connection, at least 10GB of free hard drive space, and several hours of available time to complete the steps below.

  1. Update your Mac operating system to the newest version.
  2. Install Xcode through the App Store
  3. Check if you have X11 installed: hit Command-Space, type "X11", and see if the program shows up. If not, install it from here.
  4. Download a the stable pre-compiled version of Wine. These instructions previously suggested installing Wine via Macports. Note:This has not been tested after macOS 10.14.4.
  5. Download WinBUGS 1.4.3. Also download the immortality key and the patch. Make sure that all three are saved in your "Downloads" folder.
  6. Open your terminal and type
    cd ~/Downloads
    wine WinBUGS14.exe
  7. Follow the instructions to install WinBUGS into c:Program Files.
  8. Run WinBUGS via the terminal by typing:
    wine "/Users/yourusername/.wine/drive_c/Program Files/WinBUGS14/WinBUGS14"
    Note that you need to replace "yourusername" with your Mac's user name.
  9. After first running WinBUGS, install the immortality key. Close WinBUGS. Open it again as above and install the patch.
  10. Install the most recent version of R and RStudio. Then, install the R2WinBUGS package.
  11. Test your WinBUGS installation by executing this example script after adjusting the respective file paths in the bugs() call to your setup. Your bugs() call needs to contain this argument:
    bugs.directory = "/Users/yourusername/.wine/drive_c/Program Files/WinBUGS14"
    Note that you need to replace "yourusername" with your Mac's user name.
  12. To install OpenBUGS and R2OpenBUGS as an alternative to WinBUGS, repeat steps 6-12, but replace WinBUGS14 with OpenBUGS where applicable. Download OpenBUGS from here and use this script to test your installation.

Installing Stan

To install Stan, a powerful and fast-growing tool for Bayesian estimation, follow the instructions at stan-dev. If you run into installation problems, check the Stan forums.


Lab tutorials

The following tutorials accompany the labs offered during the workshop:
  1. First steps in R
  2. Reproducible data analysis in R (by Herrissa Lamothe)
  3. Using JAGS and BUGS via R
  4. Model presentation using MCMC output
  5. Multilevel data management
  6. Using Stan via R

BayesPostEst

To facilitate using the methods from this workshop, I provide an R package (jointly authored with Shana Scogin, Andy Beger, and Rob Williams) to generate and plot postestimation quantities after estimating Bayesian regression models using MCMC. Quantities of interest include predicted probabilities and changes in probabilities in generalized linear models and analyses of model fit using ROC curves and precision-recall curves. The package also contains two functions to create publication-ready tables summarizing model results with an assessment of substantively meaningful effect sizes.

Questions? E-mail me.