APPLIED BAYESIAN MODELING
ICPSR SUMMER PROGRAM, MAY 17-28, 2021



Welcome to the website for my two-week workshop Applied Bayesian Models for Social Scientists: From Theory to Estimation and Inference in the 2021 ICPSR Summer Program. This page contains course materials, a variety of information on how to fit and present Bayesian models using Stan, JAGS, and R, and some information specifically for Mac users on how to install JAGS and related programs to use Bayesian techniques in R.

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 Canvas. Instructions on how to prepare and what to install will be sent to all participants before the first day of the workshop.

Workshop materials

Installing R, RStudio, and Stan

  1. Install the most recent version of R (4.0.0 or higher) from the CRAN website.
  2. Download and install RStudio™ (version 1.4 or higher).
  3. To install Stan, a powerful and fast-growing tool for Bayesian estimation, follow the instructions at stan-dev. We've set time aside to go over installation during the workshop, since it will likely require a few individual adjustments depending on your computer's operating system. But if you like, you can already install Stan before the workshop. You might run into some issues; if so, I've collected a few helpful links below.
    • Check out these brief instructions and these points on the Stan forum for how to deal with a few changes due to R 4.0.
    • If you run into installation problems, we will follow up on them once the workshop has started.

Lab tutorials

The following tutorials (available on Canvas) accompany the labs offered during the workshop:
  1. Introduction to R and Reproducible data analysis in R (by Herrissa Lamothe)
  2. Introduction to Stan and rstanarm
  3. Assessing convergence
  4. Working with MCMC output
  5. Managing multilevel data in R
  6. Writing customized models in Stan
  7. Communicating results from Bayesian analysis

R package: 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.

Optional: 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.

Questions? E-mail me.