Вы находитесь на странице: 1из 22

Applied Bayesian Modeling

A brief JAGS and R2jags tutorial


Johannes Karreth
University at Albany, SUNY
jkarreth@albany.edu
Supplementary R and JAGS code:
http://www.jkarreth.net/bayes2014.html
ICPSR Summer Program 2014

All code for this tutorial can also be found on my github page at https://github.com/jkarreth/Bayes.
Updated code will be posted there.
If you find any errors in this document or have suggestions for improvement, I would appreciate an email
notice.

Contents
1

Bayesian modeling for Mac users

Using R as frontend on Windows and Linux

What are JAGS, R2jags, ...?

Installing JAGS and R2jags on Mac OS X 10.6 and above

Fitting Bayesian models in JAGS


3
5.1 Using R2jags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5.2 Using JAGS via Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Using the boa and coda packages in R

Differences between JAGS and WinBUGS


19
7.1 Logit models and the p.bound workaround: . . . . . . . . . . . . . . . . . . . . . . . . . . 19
7.2 Ordered logit models in JAGS: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Following this course using JAGS

17

21

Applied Bayesian Modeling

JAGS Tutorial

2 of 22

Bayesian modeling for Mac users

As a Mac user, you have the following (and more) options to work through the materials presented in this
workshop:
1. Use WinBUGS (Spiegelhalter et al., 2003) on Windows on the computers in the Helen Newberry
building.
2. Install a virtual machine or emulator on your Mac and use WinBUGS on the Mac.
3. Use JAGSa complete BUGS engine for Unixon your Mac (with very minor code adjustments).
This tutorial focuses on the third option, since it gives you the most mileage if you want to keep working
with Bayesian models. If you are interested in the second option, but do not plan on purchasing or using a
full virtual machine, it is also possible to use WinBUGS via Wine. Instructions for this option can be found
at several sources. Two I have found to be very useful (on OS X Mavericks) are
http://archives.aidanfindlater.com/blog/2010/07/22/installing-openbugs-in-mac-os-x-using http://www.imachordata.com/running-r2winbugs-on-a-mac-running-osx/.
Another program you might want to check out is the R package MCMCpack (Martin, Quinn, and Park,
2011), which we will introduce in the fourth week of the course: see http://mcmcpack.wustl.edu/.

Using R as frontend on Windows and Linux

JAGS also works on Windows and Linux. You can use almost all of the commands in this tutorial on those
systems as well.
A convenient way to fit Bayesian models using WinBUGS or OpenBUGS is to use R packages that function
as frontends for WinBUGS or OpenBUGS, such as R2WinBUGS (Sturtz, Ligges, and Gelman, 2005) or
BRugs (Thomas et al., 2006). R2WinBUGS works very similar to R2jags, so you can adapt the code in this
tutorial easily. We will also offer a separate lab on R and WinBUGS.

What are JAGS, R2jags, ...?

JAGS (Plummer, 2011) is Just Another Gibbs Sampler that was mainly written by Martyn Plummer in
order to provide a BUGS engine for Unix. More information can be found in the excellent JAGS manual at
http://sourceforge.net/projects/mcmc-jags/.
R2jags (Su and Yajima, 2012) is an R package that allows fitting JAGS models from within R. It was written by Andrew Gelman et al. with the purpose of providing identical functionality to the R2WinBUGS
package on Windows machines. Almost all examples in Gelman and Hills Data Analysis Using Regression
and Multilevel/Hierarchical Models (2007) can thus be worked through equivalently in JAGS, using R2jags.
rjags (Plummer, 2013) is another R package that allows fitting JAGS models from within R. R2jags depends
on it. Simon Jackmans Bayesian Analysis for the Social Sciences (2009 )provides many examples using
rjags, and so does John Kruschkes Doing Bayesian Data Analysis (2011).
In this tutorial, I focus on the use of R2jags, as well as using JAGS directly from the Terminal.

Applied Bayesian Modeling

JAGS Tutorial

3 of 22

Installing JAGS and R2jags on Mac OS X 10.6 and above

1. Install the most recent R version from the CRAN website: http://cran.r-project.org/bin/
macosx. If you are already using a different version of R, you can (but need not) uninstall it by typing
$ rm -rf /Library/Frameworks/R.framework /Applications/R.app
in the Terminal.
2. NOTE: As of June 2014, rjags and R2jags are not yet available for the Mavericks build of R
3.1.0. If you are using the Mavericks build of R 3.1.0, you need to re-install the Snow Leopard
build of R 3.1.0 in order to use rjags and/or R2jags. This issue will likely be corrected soon, but
in the meantime, please resort to the Snow Leopard build if you receive an error when trying to
install.packages("rjags"). More information is available here: http://bit.ly/1ypBrxL.
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: http://cran.r-project.org/bin/macosx/tools.
4. Install JAGS version 3.3.0 (JAGSdist-3.3.0.dmg) from Martyn Plummers repository: http://
sourceforge.net/projects/mcmc-jags/files/JAGS/3.x/Mac%20OS%20X/. Start the Terminal and type
$ jags
to see if JAGS 3.3.0 is installed.
5. Install the packages R2jags, coda, R2WinBUGS, lattice, and (lastly) rjags from within R, via the
Package Installer or by using
> # install.packages("R2jags", dependencies = TRUE, repos = "http://cran.us.r-project.org")

6. Download a scientific text editor for writing R and JAGS code. I recommend TextWrangler (http://
www.barebones.com/products/textwrangler/) or Sublime Text (http://www.sublimetext.
com). RStudio is a very neat integrated environment for using R on a Mac (and other platforms), and
I recommend using it: http://www.rstudio.org.
7. Note for users of Mac OS X 10.5 (Leopard): Due to a particular behavior of the JAGS installer
on Leopard, the JAGS files that rjags requires to run are not located where rjags is looking for
them. See http://martynplummer.wordpress.com/2011/11/04/rjags-3-for-mac-os-x/
#comments If you would like to use R2jags or rjags on Mac OS X 10.5, you need to manually
relocate these files from /usr to /usr/local. See the TAs if you would like help with this.

Fitting Bayesian models in JAGS

5
5.1

Using R2jags

Just like R2WinBUGS1 , the purpose of R2jags is to allow fitting JAGS models from within R, and to analyze
convergence and perform other diagnostics right within R. A typical sequence of using R2jags could look
like this:

1 See

Appendix C in Gelman and Hill (2007) or their online appendix http://www.stat.columbia.edu/~gelman/bugsR/


runningbugs.html for more info on how to use R2WinBUGS and R.

JAGS Tutorial

Applied Bayesian Modeling

4 of 22

> library(R2jags)
calls:
>
>
>
>

library(coda)
library(lattice)
library(R2WinBUGS)
library(rjags)

Preparing the data and model


Read the data in from the car package:
> library(car)
> data(Angell)
> angell.1 <- Angell[, -4] ## remove the 4th column (the order is (row, column))

Save the model as "angell.model.jags" in your working directory. (Do not paste this model
straight into R.) You can set your working directory in the R preferences, or via:
> setwd("/Users/johanneskarreth/R/Bayes/angell")

The model looks just like the BUGS models shown in class:
model {
for(i in 1:N){
moral[i] ~ dnorm(mu[i], tau)
mu[i] <- alpha + beta1 * hetero[i] + beta2 * mobility[i]
}
alpha
beta1
beta2
tau ~
}

~ dnorm(0, .01)
~ dunif(-100, 100)
~ dunif(-100, 100)
dgamma(.01, .01)

Instead of saving the model in your WD, you can also enter it in your R script:
> angell.mod <- function()

+
+
+
+

for(i in 1:N){
moral[i] ~ dnorm(mu[i], tau)
mu[i] <- alpha + beta1 * hetero[i] + beta2 * mobility[i]
}

+
+
+
+

alpha
beta1
beta2
tau ~

~ dnorm(0, .01)
~ dunif(-100, 100)
~ dunif(-100, 100)
dgamma(.01, .01)

+ }

Now define the vectors of the data matrix for JAGS:

Applied Bayesian Modeling

>
>
>
>

JAGS Tutorial

5 of 22

moral <- angell.1$moral


hetero <- angell.1$hetero
mobility <- angell.1$mobility
N <- length(angell.1$moral)

Read in the Angell data for JAGS


> angell.data

<- list("moral", "hetero", "mobility", "N")

(You could also do this more conveniently using the as.list command on your data frame:)
> angell.data

<- as.list(angell.1)

Note, though, that you will also need to specify any other variables not in the data, like in this case N.
So here, you would need to add:
> angell.data$N

<- nrow(angell.1)

Define the parameters youre interested in:


> angell.params <- c("alpha", "beta1", "beta2")

Define the starting values for JAGS. Per Gelman and Hill (2007, 370), you can use a function to
do this. This function creates a list that contains one element for each parameter. Each parameter
then gets assigned a random draw from a normal distributio as a starting value. This random draw
is created using the rnorm function. The first argument of this function is the number of draws. If
your parameters are not indexed in the model code, this argument will be 1. If your jags command
below then specifies more than one chain, each chain will start at a different random value for each
parameter.
> angell.inits <- function(){
+
list("alpha" = rnorm(1), "beta1" = rnorm(1), "beta2" = rnorm(1))
+
}

Alternatively, if you want to have control over which starting values are chosen, you can provide
specific separate starting values for each chain:
> inits1 <- list("alpha"=0, "beta1"=0, "beta2"=0)
> inits2 <- list("alpha"=1, "beta1"=1, "beta2"=1)
> angell.inits <- list(inits1, inits2)

Before using R2jags the first time, you might need to set a seed. To do this, type
> set.seed(123)

directly in R. You can choose any not too big number here. Setting a random seed before fitting a
model is also good practice for making your estimates replicable. We will discuss replication in more
detail in week 4.

Applied Bayesian Modeling

JAGS Tutorial

6 of 22

Fitting the model


Fit the model in JAGS:
> angell.fit <- jags(data = angell.data, inits = angell.inits,
+
parameters.to.save = angell.params, n.chains = 2, n.iter = 9000,
+
n.burnin = 1000, model.file = "~/R/Bayes/angell/angell.model.jags")
## Compiling model graph
##
Resolving undeclared variables
##
Allocating nodes
##
Graph Size: 266
##
## Initializing model

Note: If you use as your model file the function you gave directly to R above, then remove the
quotation marks:
> angell.fit <- jags(data = angell.data, inits = angell.inits,
+
parameters.to.save = angell.params, n.chains = 2, n.iter = 9000,
+
n.burnin = 1000, model.file = angell.mod)
## Compiling model graph
##
Resolving undeclared variables
##
Allocating nodes
##
Graph Size: 266
##
## Initializing model

Update your model if necessary - e.g. if there is no/little convergence:


> angell.fit.upd <- update(angell.fit, n.iter=1000)
> angell.fit.upd <- autojags(angell.fit)

This function will auto-update until convergence.


Diagnostics
One of the neat things about running JAGS or BUGS from R is that this offers seamless, and therefore quick,
access to convergence diagnostics after fitting a model. See for yourself:
> print(angell.fit)
##
##
##
##
##
##
##
##
##
##

Inference for Bugs model at "/var/folders/gc/70thbq_d7wxdwsybcfclksqc0000gn/T//RtmpDbvfNB/model4a119e06981


2 chains, each with 9000 iterations (first 1000 discarded), n.thin = 8
n.sims = 2000 iterations saved
mu.vect sd.vect
2.5%
25%
50%
75%
alpha
19.743
1.214 17.329 18.910 19.730 20.570
beta1
-0.107
0.018 -0.142 -0.118 -0.106 -0.095
beta2
-0.189
0.035 -0.261 -0.212 -0.189 -0.166
deviance 192.664
3.027 188.934 190.411 191.924 194.181
97.5% Rhat n.eff
alpha
22.093 1.003 2000

JAGS Tutorial

Applied Bayesian Modeling

##
##
##
##
##
##
##
##
##
##

beta1
-0.072 1.003
beta2
-0.123 1.002
deviance 200.046 1.004

7 of 22

1500
2000
970

For each parameter, n.eff is a crude measure of effective sample size,


and Rhat is the potential scale reduction factor (at convergence, Rhat=1).
DIC info (using the rule, pD = var(deviance)/2)
pD = 4.6 and DIC = 197.2
DIC is an estimate of expected predictive error (lower deviance is better).

> plot(angell.fit)
> traceplot(angell.fit)

If you want to print and save the plot, you can use the following set of commands:
> pdf("~/R/Bayes/angell/angell_trace.pdf")

... defines that the plot will be saved as a PDF file with the name "angell trace.pdf" in your
working directory. 2
> traceplot(angell.fit)

creates the plot in the background (you will not see it).
> dev.off()
## pdf
##
3

finishes the printing process and creates the PDF file of the plot. If successful, R will display the
message "null device 1".
More diagnostics are available when you convert your model output into an MCMC object. You can generate
an MCMC object for analysis with this command:
> angell.fit.mcmc <- as.mcmc(angell.fit)
> summary(angell.fit.mcmc)
##
##
##
##
##
##
##
##
##
##
2 LAT

Iterations = 1001:8993
Thinning interval = 8
Number of chains = 2
Sample size per chain = 1000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean

SD Naive SE Time-series SE

EX cannot process file names with periods, so if you use LATEX and try to include the graphics file angell.trace, LATEX
will not compile your document.

JAGS Tutorial

Applied Bayesian Modeling

##
##
##
##
##
##
##
##
##
##
##
##

alpha
19.743
beta1
-0.107
beta2
-0.189
deviance 192.664

1.2136
0.0176
0.0352
3.0271

0.027136
0.000392
0.000788
0.067687

8 of 22

0.046210
0.000449
0.001360
0.075447

2. Quantiles for each variable:


2.5%
25%
50%
75%
97.5%
alpha
17.329 18.910 19.730 20.5704 22.093
beta1
-0.142 -0.118 -0.106 -0.0949 -0.072
beta2
-0.261 -0.212 -0.189 -0.1656 -0.123
deviance 188.934 190.411 191.924 194.1808 200.046

With an MCMC object, you can use a variety of commands for diagnostics and presentation. First, using
CODA:
Plot:
> xyplot(angell.fit.mcmc)

You can customize the plot layout (you can use other Lattice options here as well):
> xyplot(angell.fit.mcmc, layout=c(2,6), aspect="fill")

Density plot:
> densityplot(angell.fit.mcmc)
> densityplot(angell.fit.mcmc, layout=c(2,6), aspect="fill")

Trace- and density in one plot, print directly to your working directory:
> pdf("~/R/Bayes/angell/angell_fit_mcmc_plot.pdf")
> plot(angell.fit.mcmc)
> dev.off()
## pdf
##
2

Autocorrelation plot, print directly to your working directory:


> pdf("~/R/Bayes/angell/angell_fit_mcmc_autocorr.pdf")
> autocorr.plot(angell.fit.mcmc)
> dev.off()
## pdf
##
2

Other diagnostics using CODA:

JAGS Tutorial

Applied Bayesian Modeling

> gelman.plot(angell.fit.mcmc)
> geweke.diag(angell.fit.mcmc)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

[[1]]
Fraction in 1st window = 0.1
Fraction in 2nd window = 0.5
alpha
-0.9992

beta1
1.2970

beta2 deviance
0.4110
0.3609

[[2]]
Fraction in 1st window = 0.1
Fraction in 2nd window = 0.5
alpha
0.5719

beta1
-1.2212

beta2 deviance
-0.1129 -1.2715

> geweke.plot(angell.fit.mcmc)
> raftery.diag(angell.fit.mcmc)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

[[1]]
Quantile (q) = 0.025
Accuracy (r) = +/- 0.005
Probability (s) = 0.95
You need a sample size of at least 3746 with these values of q, r and s
[[2]]
Quantile (q) = 0.025
Accuracy (r) = +/- 0.005
Probability (s) = 0.95
You need a sample size of at least 3746 with these values of q, r and s

> heidel.diag(angell.fit.mcmc)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

[[1]]
Stationarity
test
alpha
passed
beta1
passed
beta2
passed
deviance passed

start
iteration
1
1
1
1

Halfwidth Mean
test
alpha
passed
19.725
beta1
passed
-0.106
beta2
passed
-0.189
deviance passed
192.762

p-value
0.688
0.611
0.906
0.980

Halfwidth
0.13864
0.00135
0.00395
0.21937

9 of 22

JAGS Tutorial

Applied Bayesian Modeling

##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

10 of 22

[[2]]
Stationarity
test
alpha
passed
beta1
passed
beta2
passed
deviance passed

start
iteration
1
1
1
1

Halfwidth Mean
test
alpha
passed
19.761
beta1
passed
-0.107
beta2
passed
-0.190
deviance passed
192.566

p-value
0.0916
0.4597
0.1428
0.5263

Halfwidth
0.11659
0.00113
0.00358
0.19836

A very convenient function to analyze numerical representations of diagnostics in one sweep is the superdiag
package (Tsai, Gill, and Rapkin, 2012).
First, install the package:
> # install.packages("superdiag", dependencies = TRUE, repos = "http://cran.us.r-project.org")
> library(superdiag)

Next, all you need to do is:


> superdiag(angell.fit.mcmc, burnin = 100)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Number of chains = 2
Number of iterations = 1000 per chain before discarding the burn-in period
The burn-in period = 100 per chain
Sample size in total = 1800
********** The Geweke diagnostic: **********
Z-scores:
chain1 chain 2
alpha
-0.4168 2.1301
beta1
0.6216 -0.1807
beta2
0.2418 -2.5667
deviance
-0.5376 0.4049
Window From Start 0.1000 0.5721
Window From Stop
0.5000 0.2187
********** The Gelman-Rubin diagnostic: **********
Potential scale reduction factors:

alpha
beta1
beta2
deviance

Point est. Upper C.I.


1.00
1.00
1.01
1.01
1.00
1.01
1.00
1.01

Multivariate psrf

JAGS Tutorial

Applied Bayesian Modeling

##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

11 of 22

1
********** The Heidelberger-Welch diagnostic: **********
Chain 1, epsilon=0.1,
Stationarity
test
alpha
passed
beta1
passed
beta2
passed
deviance passed

alpha=0.05
start
p-value
iteration
1
0.811
1
0.614
1
0.932
1
0.908

Halfwidth Mean
test
alpha
passed
19.750
beta1
passed
-0.106
beta2
passed
-0.189
deviance passed
192.745

Halfwidth
0.13693
0.00143
0.00420
0.24001

Chain 2, epsilon=0.151, alpha=0.05


Stationarity start
p-value
test
iteration
alpha
passed
1
0.0514
beta1
passed
1
0.7007
beta2
passed
91
0.1503
deviance passed
1
0.8048
Halfwidth Mean
test
alpha
passed
19.756
beta1
passed
-0.107
beta2
passed
-0.189
deviance passed
192.607

Halfwidth
0.12125
0.00113
0.00358
0.20836

********** The Raftery-Lewis diagnostic: **********


Chain 1, converge.eps = 0.001
Quantile (q) = 0.025
Accuracy (r) = +/- 0.005
Probability (s) = 0.95
You need a sample size of at least 3746 with these values of q, r and s
Chain 2, converge.eps = 0.0025
Quantile (q) = 0.01
Accuracy (r) = +/- 0.005
Probability (s) = 0.9
You need a sample size of at least 1072 with these values of q, r and s

Note that the R2jags object by default retains 1000 iterations (through thinning), hence the burn-in
period you provide for superdiag must be less than 1000.
A convenient way to obtain graphical diagnostics and results is using the mcmcplots package (Curtis, 2012):
First, install the package:

JAGS Tutorial

Applied Bayesian Modeling

12 of 22

> # install.packages("mcmcplots", dependencies = TRUE, repos = "http://cran.us.r-project.org")


> library(mcmcplots)

Some commands for plots:


> denplot(angell.fit.mcmc)
> denplot(angell.fit.mcmc, parms = c("alpha", "beta1", "beta2"))
> traplot(angell.fit.mcmc, parms = c("alpha", "beta1", "beta2"))

As always, check the help files for options to customize these plots.
Or, for quick diagnostics, you can produce html files with trace, density, and autocorrelation plots all
on one page. The files will be displayed in your default internet browser.
> mcmcplot(angell.fit.mcmc)
##
Preparing plots for alpha.
##

25% complete.

Preparing plots for beta1.


##

50% complete.

Preparing plots for beta2.


##

75% complete.

Preparing plots for deviance.

100% complete.

If you want to produce a coefficient dot plot with credible intervals, use caterplot:
> caterplot(angell.fit.mcmc)
> caterplot(angell.fit.mcmc, parms = c("alpha", "beta1", "beta2"),
+
labels = c("Intercept", "Heterogeneity", "Mobility"))

Yet another option for plotting output is the ggmcmc package (Fernandez i Marn, 2013):
>
>
>
>
>

# install.packages("ggmcmc", dependencies = TRUE, repos = "http://cran.us.r-project.org")


library(ggmcmc)
angell.fit.gg <- ggs(angell.fit.mcmc)
ggs_density(angell.fit.gg)
ggmcmc(angell.fit.gg)

##
##
##
##
##
##
##
##
##
##
##
##

Plotting
Plotting
Plotting
Plotting
Plotting
Plotting
Plotting
Plotting
Plotting
Plotting
pdf
2

histograms
density plots
traceplots
running means
comparison of partial and full chain
autocorrelation plots
crosscorrelation plot
Potential Scale Reduction Factors
Geweke Diagnostic
caterpillar plot

Applied Bayesian Modeling

JAGS Tutorial

13 of 22

If you want to write out the output from your model, you could use the function jags2. This function is
slightly more demanding when it comes to the format of starting values and the data, so follow the model
below:
>
>
>
>
>
>
>
+
+
>
+
+

setwd("~/R/Bayes/angell")
angell.params <- c("alpha", "beta1", "beta2")
angell.data <- as.list(angell.1)
angell.data$N <- nrow(angell.1)
dump(list = "angell.data", file = "angell.dump.txt")
bugs2jags(infile = "angell.dump.txt", outfile = "angell.datjags.txt")
angell.inits <- function(){
list("alpha"= rnorm(1), "beta1" = rnorm(1), "beta2" = rnorm(1))
}
angell.fit <- jags2(data = "angell.datjags.txt", inits = angell.inits,
parameters.to.save = angell.params, n.chains = 2, n.iter = 5000, n.burnin = 1000,
model.file="angell.model.jags", clearWD = FALSE)

This will create the following files in your working directory (if clearWD is set to FALSE):
/Users/johanneskarreth/R/Bayes/angell/CODAchain1.txt
/Users/johanneskarreth/R/Bayes/angell/CODAchain2.txt
/Users/johanneskarreth/R/Bayes/angell/CODAindex.txt
/Users/johanneskarreth/R/Bayes/angell/jagsdata.txt
/Users/johanneskarreth/R/Bayes/angell/jagsinits1.txt
/Users/johanneskarreth/R/Bayes/angell/jagsinits2.txt
/Users/johanneskarreth/R/Bayes/angell/jagsscript.txt
... which can then be analyzed using BOA
> library(boa)
> boa.menu()
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Bayesian Output Analysis Program (BOA)


Version 1.1.7 for x86_64, darwin10.8.0
Copyright (c) 2007 Brian J. Smith <brian-j-smith@uiowa.edu>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License or any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
For a copy of the GNU General Public License write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA, or visit their web site at
http://www.gnu.org/copyleft/gpl.html
NOTE: if the event of a menu system crash, type
"boa.menu(recover = TRUE)" to restart and recover your work.

## Error:

menu() cannot be used non-interactively

Applied Bayesian Modeling

JAGS Tutorial

14 of 22

or CODA.
> library(coda)
> codamenu()
## Error:

menu() cannot be used non-interactively

In both cases (using boa or coda), remember to rename the index files extension to .ind, and the chain
files to .out. See section 6 for code on how to read these files into R via the command line.

5.2

Using JAGS via Terminal

JAGS can also be operated straight from the command line - on Windows and Unix systems alike. The
most feasible way to do this is to write a script file with the following parts, and save it, for instance as
angell.jags:
model clear
data clear
load dic
model in "angell.mod"
data in "angell.dat"
compile, nchains(2)
inits in "angell1.inits", chain(1)
inits in "angell2.inits", chain(2)
initialize
update 2500, by(100)
monitor alpha, thin(2)
monitor beta1, thin(2)
monitor beta2, thin(2)
monitor deviance, thin(2)
update 2500, by(100)
coda *
You can run this script file by opening a Terminal window, changing to the working directory in which all
the above files are located $ cd "/Users/johanneskarreth/R/Bayes/angell"
and then simply telling JAGS to run the script:
$ jags angell.jags
In your Terminal, you will see something like this:

Applied Bayesian Modeling

JAGS Tutorial

15 of 22

In more detail, this is what each line of the scrip does:


model clear
data clear
load dic
Remove previous data and models (if applicable), load the dic module so you can monitor the model
deviance later.
model in "angell.mod"
Use the model angell.mod, which is saved in your WD, and looks like a regular BUGS model. Make
sure you use the exact and full name of the model file as it is in your working directory, otherwise
JAGS will not find it. Look out for hidden file name extensions. 3
model {
for(i in 1:N){
moral[i] ~ dnorm(mu[i], tau)
mu[i] <- alpha + beta1 * hetero[i] + beta2 * mobility[i]
}
alpha
beta1
beta2
tau ~
}

~ dnorm(0, .01)
~ dunif(-100, 100)
~ dunif(-100, 100)
dgamma(.01, .01)

data in "angell.dat"
Use the data angell.dat. These data can be saved as vectors in one file that you name angell.dat.
This file will look something like this:
"moral" <- c( 19, 17, ...)
"hetero" <- c( 20.6, 15.6, ...)
"mobility" <- c( 15, 20.2, ...)
"N" <- 43
3 On

the Mac, you can set the Finder to display all file extensions by going to Finder > Preferences > check Show all filename
extensions.

Applied Bayesian Modeling

JAGS Tutorial

16 of 22

You can create this data file by hand (inconvenient), or you can work with some R commands to do
this automatically. If angell.1 is a data frame object in R, you may do this:
>
>
>
>

angell.data <- as.list(angell.1)


angell.data$N <- nrow(angell.1)
dump("angell.data", file = "angell.dump")
bugs2jags("angell.dump", "angell.dat")

The first command converts the data frame into a list; the second command writes out the data in
BUGS format as a file to your working directory; the third command (part of the coda package)
translates it into JAGS format. Both angell.dump and angell.dat are written to your working directory,
so be sure that you have specified that in the beginning. Also be sure to visually inspect your data file
was created correctly.
compile, nchains(2)
Compile the models and run two Markov chains.
inits in "angell1.inits", chain(1)
inits in "angell2.inits", chain(2)
Use the starting values you provide in angell1.inits and angell2.inits, each of which could look like
this:
"alpha" <- c(0)
"beta1" <- c(0)
"beta2" <- c(0)
This way, you can specify different starting values for each chain.
initialize
Initialize and run the model.
update 2500, by(100)
Specify 2500 updates before you start monitoring your parameters of interest.
monitor
monitor
monitor
monitor

alpha, thin(2)
beta1, thin(2)
beta2, thin(2)
deviance, thin(2)

Monitor the values for these parameters, in this case the three regression coefficients and the model
deviance. thin(2) specifies that only each second draw from the chains will be used for your model
output (compare this to the thinning command in WinBUGS).
update 2500, by(100)
coda *, stem(angell_out)
Tell JAGS to produce coda files that all begin with the stem angell out and are put in your WD.
These can then be analyzed with the tools described in this tutorial.

Applied Bayesian Modeling

JAGS Tutorial

17 of 22

Using the boa and coda packages in R

You can use either of the two packages boa or coda in R to analyze your BUGS/JAGS output, regardless of
the BUGS software you used to fit your model (i.e. WinBUGS/OpenBUGS/JAGS). These are the key steps
to get your data into R to use boa or coda:
Fit your model in WinBUGS/OpenBUGS/JAGS, and identify where your software saved the chains
and index files most likely in the working directory where the other components of your model
(data, model, inits) are.
Give these files a proper name, for instance angell out chain1.txt and
angell out chain2.txt.
In R, load the boa or coda package, whichever you prefer:
> library(boa)
> library(coda)

Now, read your chain and index files into R, via the commands below.
If you prefer the usual R command-line behavior to the boa/coda menu option, both packages also can
be used via the command line.
In coda, it should work like this:
Read in your BUGS/JAGS output. This requires that the chains and index files (see above) are
in your working directory.
> chain1 <- read.coda(output.file = "~/R/Bayes/angell/angell_out_chain1.txt",
+
index.file = "~/R/Bayes/angell/angell_out_index.txt")
##
##
##
##

Abstracting
Abstracting
Abstracting
Abstracting

alpha ... 1250 valid values


beta1 ... 1250 valid values
beta2 ... 1250 valid values
deviance ... 1250 valid values

> chain2 <- read.coda(output.file = "~/R/Bayes/angell/angell_out_chain2.txt",


+
index.file = "~/R/Bayes/angell/angell_out_index.txt")
##
##
##
##

Abstracting
Abstracting
Abstracting
Abstracting

alpha ... 1250 valid values


beta1 ... 1250 valid values
beta2 ... 1250 valid values
deviance ... 1250 valid values

> angell.chains <- as.mcmc.list(list(chain1, chain2))

Now you can analyze using some of the commands listed in


> help(package = "coda")

for instance:
> summary(angell.chains)

JAGS Tutorial

Applied Bayesian Modeling

##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

18 of 22

Iterations = 2501:4999
Thinning interval = 2
Number of chains = 2
Sample size per chain = 1250
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean
alpha
19.501
beta1
-0.107
beta2
-0.181
deviance 192.694

SD
1.2179
0.0175
0.0362
3.1360

Naive SE Time-series SE
0.024358
0.084804
0.000351
0.000711
0.000723
0.002433
0.062720
0.123467

2. Quantiles for each variable:


2.5%
25%
50%
75%
97.5%
alpha
16.915 18.756 19.512 20.3100 21.7640
beta1
-0.139 -0.119 -0.107 -0.0946 -0.0718
beta2
-0.249 -0.205 -0.182 -0.1602 -0.1008
deviance 188.886 190.489 191.912 194.0282 200.8739

> traceplot(angell.chains)

You should be able to play around with graphical parameters and different printing devices this
way.
In boa, try the following steps:
Start the boa session:
> boa.init()
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Bayesian Output Analysis Program (BOA)


Version 1.1.7 for x86_64, darwin10.8.0
Copyright (c) 2007 Brian J. Smith <brian-j-smith@uiowa.edu>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License or any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
For a copy of the GNU General Public License write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA, or visit their web site at
http://www.gnu.org/copyleft/gpl.html

> my.model.chain1 <- boa.importBUGS("angell_out", "~/R/Bayes/angell")


where angell out is the stem of your chains and index files; and "~/R/Bayes/angell/" is
the folder where your chains and index files are saved.

JAGS Tutorial

Applied Bayesian Modeling

19 of 22

Now you can analyze using some of the commands listed in


> help(package=boa)

for instance:
> summary(my.model.chain1)
##
##
##
##
##
##
##
##
##
##
##
##
##
##

alpha
Min.
:15.2
1st Qu.:18.8
Median :19.5
Mean
:19.5
3rd Qu.:20.3
Max.
:23.2
deviance
Min.
:188
1st Qu.:190
Median :192
Mean
:193
3rd Qu.:194
Max.
:211

beta1
Min.
:-0.1605
1st Qu.:-0.1186
Median :-0.1065
Mean
:-0.1064
3rd Qu.:-0.0950
Max.
:-0.0476

beta2
Min.
:-0.3016
1st Qu.:-0.2057
Median :-0.1822
Mean
:-0.1830
3rd Qu.:-0.1617
Max.
:-0.0568

Convenient: the superdiag, mcmcplots, and ggmcmc packages also work like described above once
you have declared your JAGS output an MCMC object.

Differences between JAGS and WinBUGS

There are just a few minor differences between JAGS and WinBUGS that require small adjustments in the
code. Two that are relevant for the assignments in this course are described below. A few others that you
might encounter in your work are listed in the JAGS manual, section 7.

7.1

Logit models and the p.bound workaround:

Andrew Gelmans blog entry4 on specifying p.bound:


Aurelien Madouasse writes:
I am currently fitting a multilevel logistic model using WinBUGS. I have adapted the
example you provide in Data analysis using Regression and Multilevel/Hierarchical
Models p. 381-2:
y[i] ~ dbin(p.bound[i], 1)
p.bound[i] <- max(0, min(1, p[i]))
logit(p[i]) <- Xbeta[i] ...

I was wondering what is the aim of the p.bound variable. When I use p[i] instead
WinBUGS crashes. I thought that the inverse logit was bounded between 0 and 1 so
I dont see the point of constraining it to be in this interval. What do I miss?
My reply: Yes, inverse logit is bounded, but I think Bugs sometimes messes up and gets it
outside of the bound. The other thing is that when you change the specification (in this case,
using p.bound) it changes the sampler that Bugs uses. Maybe its switching from the buggy
adaptive rejection sampler to the foolproof Metropolis or slice sampler.
4 http://www.stat.columbia.edu/

~cook/movabletype/archives/2009/04/pbound_in_multi.html

Applied Bayesian Modeling

7.2

JAGS Tutorial

20 of 22

Ordered logit models in JAGS:

From the JAGS manual (currently p. 34):


Prior ordering of top-level parameters in the model can be achieved using the sort function,
which sorts a vector in ascending order. Symmetric truncation relations like this
alpha[1] ~ dnorm(0, 1.0E-3) I(,alpha[2])
alpha[2] ~ dnorm(0, 1.0E-3) I(alpha[1],alpha[3])
alpha[3] ~ dnorm(0, 1.0E-3) I(alpha[2],)

should be replaced by this


for (i in 1:3) {
alpha0[i] ~ dnorm(0, 1.0E-3)
}
alpha[1:3] <- sort(alpha0)

Accordingly, we need to adjust the code for a simple ordered logit model like displayed below.
BUGS:
model{
for(i in 1:N){
for(j in 1:2){
logit(gamma[i,j]) <- theta[j] - mu[i]
}
quality[i] ~ dcat(p[i,1:3])
p[i,1]<- gamma[i,1]
p[i,2] <- gamma[i,2] - gamma[i,1]
p[i,3] <- 1-gamma[i,2]
mu[i] <- b[1]*price[i] + b[2]*sodium[i] + b[3]*alcohol[i]+b[4]*calories[i]
}
for(m in 1:4){
b[m] ~ dnorm(0, .0001)
}
theta[1] ~ dnorm(0,.1)I(0, theta[2])
theta[2] ~ dnorm(0,.1)I(theta[1], )
}

JAGS:
model{
for (i in 1:N){
for (j in 1:2){
logit(gamma[i,j]) <- theta1[j] - mu[i]
}
quality[i] ~ dcat(p[i,1:3])
p[i,1]<- gamma[i,1]
p[i,2] <- gamma[i,2] - gamma[i,1]

Applied Bayesian Modeling

JAGS Tutorial

21 of 22

p[i,3] <- 1-gamma[i,2]


mu[i] <- b[1]*price[i] + b[2]*sodium[i] + b[3]*alcohol[i]+b[4]*calories[i]
}
for(m in 1:4){
b[m] ~ dnorm(0, .0001)
}
for (i in 1:2) {
theta[i] ~ dnorm(0, 1.0E-3)
}
theta1[1:2] <- sort(theta)
}

Following this course using JAGS

You can find modified (if necessary) JAGS code for all models presented in this course on my website at
http://www.jkarreth.net/bayes2014.html.
The most recent version of this file is posted at http://www.jkarreth.net/files/jags.tutorial.
pdf.
Aside from the web in general and http://stackoverflow.com/questions/tagged/jags, one good
resource for JAGS is reading the discussion board at http://sourceforge.net/projects/mcmc-jags/
forums/forum/610037.

Applied Bayesian Modeling

JAGS Tutorial

22 of 22

References
Curtis, S. McKay. 2012. mcmcplots: Create Plots from MCMC Output. R package version 0.4.1.
URL: http://CRAN.R-project.org/package=mcmcplots
Fernandez i Marn, Xavier. 2013. ggmcmc: Graphical tools for analyzing Markov Chain Monte Carlo
simulations from Bayesian inference. R package version 0.5.1.
URL: http://xavier-fim.net/packages/ggmcmc
Gelman, Andrew, and Jennifer Hill. 2007. Data Analysis Using Regression and Multilevel/Hierarchical
Models. New York, NY: Cambridge University Press.
Jackman, Simon. 2009. Bayesian Analysis for the Social Sciences. Wiley Series in Probability and Statistics
Wiley.
Kruschke, John. 2011. Doing Bayesian Data Analysis: A Tutorial Introduction with R. Oxford: Academic
Press / Elsevier.
Martin, Andrew D., Kevin M. Quinn, and Jong Hee Park. 2011. MCMCpack: Markov Chain Monte Carlo
in R. Journal of Statistical Software 42 (9): 22.
Plummer, Martyn. 2011. JAGS Version 3.1.0 User Manual..
Plummer, Martyn. 2013. rjags: Bayesian graphical models using MCMC. R package version 3-10.
URL: http://CRAN.R-project.org/package=rjags
Spiegelhalter, David J., Andrew Thomas, Nicky G. Best, and Dave Lunn. 2003. WinBUGS Version 1.4
User Manual..
Sturtz, Sibylle, Uwe Ligges, and Andrew Gelman. 2005. R2WinBUGS: A Package for Running WinBUGS
from R. Journal of Statistical Software 12 (3): 116.
Su, Yu-Sung, and Masanao Yajima. 2012. R2jags: A Package for Running jags from R. R package version
0.03-08.
URL: http://CRAN.R-project.org/package=R2jags
Thomas, Andrew, Bob OHara, Uwe Ligges, and Sibylle Sturtz. 2006. Making BUGS Open. R News 6
(1): 1217.
Tsai, Tsung-han, Jeff Gill, and Jonathan Rapkin. 2012. superdiag: R Code for Testing Markov Chain
Nonconvergence. R package version 1.1.
URL: http://CRAN.R-project.org/package=superdiag

Вам также может понравиться