# APPLIED BAYESIAN MODELING

ICPSR SUMMER PROGRAM 2017

Welcome to the website for the 2017 ICPSR Summer Program's course on Applied Bayesian Modeling. 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.

## Course materials

- Workshop syllabus
- Tutorial 1: Intro to R
- Tutorial 2: Basic workflow for Bayesian modeling
- Links to model code examples

**This page will be updated with current material in June/July 2017.**

## How to set up JAGS and R on a Mac

- Install the most recent version of R from the CRAN website.
- Download and install RStudio™.
- Install the Tcl/Tk libraries (
`tcltk-8.5.5-x11.dmg`

) and GNU Fortran (`gfortran-4.2.3.dmg`

) from the CRAN tools directory. - 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:- Download the disk image from the JAGS website.
- Ensure that the 'Allow apps downloaded from anywhere' box is selected in the Security and Privacy (General) pane of System Preferences.
- Double click on the disk image to mount (this may not be required).
- Double click on the 'JAGS-4.2.0.mpkg' file within the mounted disk image.
- Follow the instructions in the installer.
- Authenticate as the administrative user. The first user you create when setting up Mac OS X has administrator privileges by default.

- Start the Terminal and type
`jags`

to see if you receive the message:`Welcome to JAGS 4.2.0`

. - Open R and install the packages R2jags, coda, R2WinBUGS, lattice, and rjags, by typing

in the R command line.`install.packages(c("R2jags", "coda", "R2WinBUGS", "lattice", "rjags"))`

- 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 course.`set.seed(123)`

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

- Update your Mac operating system to the newest version.
- Install Xcode through the App Store
- Check if you have X11 installed: hit Command-Space, type "X11", and see if the program shows up. If not, install it from here.
- 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

You will be asked for your password. Type it and then press enter when you've finished typing your password.`echo export PATH=/opt/local/bin:/opt/local/sbin:$PATH$'n'export MANPATH=/opt/local/man:$MANPATH | sudo tee -a /etc/profile`

- Open your terminal and type

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.`sudo port install wine-devel`

- Download WinBUGS 1.4.3. Also download the immortality key and the patch. Make sure that all three are saved in your "Downloads" folder.
- Open your terminal and type
`cd ~/Downloads`

`wine WinBUGS14.exe`

- Follow the instructions to install WinBUGS into c:Program Files.
- Run WinBUGS via the terminal by typing:

Note that you need to replace "yourusername" with your Mac's user name.`wine "/Users/yourusername/.wine/drive_c/Program Files/WinBUGS14/WinBUGS14"`

- After first running WinBUGS, install the immortality key. Close WinBUGS. Open it again as above and install the patch.
- Install the most recent version of R and RStudio. Then, install the R2WinBUGS package.
- 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:

Note that you need to replace "yourusername" with your Mac's user name.`bugs.directory = "/Users/yourusername/.wine/drive_c/Program Files/WinBUGS14"`

- 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 2 and 3 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

- 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

- 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.
- Posterior-Plots.R: a function written by Kevin Reuning (participant in the 2015 Applied Bayes workshop at ICPSR) to create a coefficient dot plot with added posterior density.
- 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.