APPLIED BAYESIAN MODELING
ICPSR SUMMER PROGRAM 2018



Welcome to the website for my workshop on Applied Bayesian Modeling at the 2018 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 do not hesitate to 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-6.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. Ensure that the 'Allow apps downloaded from anywhere' box is selected in the Security and Privacy (General) pane of System Preferences.
    3. Double click on the disk image to mount (this may not be required).
    4. Double click on the 'JAGS-4.3.0.mpkg' file within the mounted disk image.
    5. Follow the instructions in the installer.
    6. 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 TextWrangler or Sublime Text. 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. Install Macports. Follow these instructions, but complete only step 2.2.1. Download Macports (for Yosemite) and install it. Afterward, open the terminal and type
    echo export PATH=/opt/local/bin:/opt/local/sbin:$PATH$'n'export MANPATH=/opt/local/man:$MANPATH | sudo tee -a /etc/profile
    You will be asked for your password. Type it and then press enter when you've finished typing your password.
  5. Open your terminal and type
    sudo port install wine-devel
    Again, enter your password when prompted and press enter afterward. This will install wine (the emulation software needed to run WinBUGS/OpenBUGS). Be patient, as this step may take several hours.
  6. Download WinBUGS 1.4.3. Also download the immortality key and the patch. Make sure that all three are saved in your "Downloads" folder.
  7. Open your terminal and type
    cd ~/Downloads
    wine WinBUGS14.exe
  8. Follow the instructions to install WinBUGS into c:Program Files.
  9. 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.
  10. After first running WinBUGS, install the immortality key. Close WinBUGS. Open it again as above and install the patch.
  11. Install the most recent version of R and RStudio. Then, install the R2WinBUGS package.
  12. 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.
  13. 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.

Code snippets

  • diagnostics.R: This script shows a variety of ways to obtain diagnostics (traceplots, density plots, BGR, etc.) of JAGS/MCMC objects in R using the coda, superdiag, ggmcmc, and mcmcplots packages.
  • regression.table.R: example code to easily export JAGS/BUGS results to LaTeX or HTML. Based on my mcmctab function.
  • regression.dotplot.R: example code to easily make regression coefficient plots from JAGS/BUGS results.
  • interaction.instructions.R: code to plot marginal effects from a Bayesian linear model with an interaction term across the range of a moderating variable.
  • MCMC_probability_demo.Rmd: code to plot predicted probabilities for average cases and observed values, based on my MCMC_simcase_probs and MCMC_observed_probs functions.
  • ologit.pp.plot.instructions.R: step-by-step example code to plot predicted probabilities over the simulated range of explanatory variables after a Bayesian ordered (or multinomial) logit model. Partially based on code by Dave Armstrong (UW-Milwaukee).
  • factor.dotplot.R: code to make a dot plot (with credible intervals) of a Bayesian factor score.

Questions? E-mail me.