Please note: This page is archived from the last time I taught this course.
For materials for current courses, please go to my teaching page.

Welcome to the website for my workshop on Applied Bayesian Modeling at the 2017 ICPSR Summer Program. This page contains a variety of information on how to fit Bayesian models using (mostly) JAGS and R, some general code for the intuitive presentation of Bayesian model results, 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.

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 the Tcl/Tk libraries (tcltk-8.5.5-x11.dmg) and GNU Fortran (gfortran-4.2.3.dmg) from the CRAN tools directory.
  4. Now install JAGS version 4.2.0 (JAGS-4.2.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.2.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.2.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
    directly in R. (You can choose any not too big number here.) 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: = "/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, repeat steps 6-12, but replace WinBUGS14 with OpenBUGS where applicable. Download OpenBUGS from here and use this script to test your installation.

Tutorial: Intro to R

This tutorial [.pdf] describes how to install R and how to perform basic operations in R. This file accompanies Lab 1 of the workshop.

Tutorial: Basic workflow for Bayesian Modeling

This tutorial [.pdf] describes the installation process, principal workflow to fit JAGS models via R2jags or the Terminal, how to use R2WinBUGS and R2OpenBUGS, and how to obtain diagnostics using different packages. This tutorial accompanies Labs 3 and 4 of the workshop.

Modeling examples

You can find example model codes for linear regression, generalized linear models, factor models, IRT models, and multilevel models on my Bayes repository on Github.


  • 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.

Model presentation

Questions? E-mail me.