You are on page 1of 4

Getting Started with JAGS, rjags, and Bayesian Modelling | R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Home About RSS add your blog! Learn R R jobs Contact us

WELCOME! Getting Started with JAGS, SEARCH R-BLOGGERS

rjags, and Bayesian Search.. Go

Modelling
Here you will find daily
news and tutorials about
R, contributed by hundreds April 10, 2012 MOST VISITED ARTICLES OF THE WEEK
of bloggers.
By Jeromy Anglim
There are many ways to
follow us - 1. How to write the first for loop in R
By e-mail: 2. 5 Ways to Subset a Data Frame in R
3. R – Sorting a data frame by the contents of a
Your e-mail here [This article was first published on Jeromy Anglim's Blog: Psychology and Statistics, and
column
Subscribe
kindly contributed to R-bloggers]. (You can report issue about the content on this page here) 4. Loading packages efficiently
5. Using apply, sapply, lapply in R
On Facebook: Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. 6. In-depth introduction to machine learning in
15 hours of expert videos
7. R Tutorial Series: Simple Linear Regression
 Share  Tweet 8. Date Formats in R
9. Installing R packages

This post provides links to various resources on getting started with Bayesian
modelling using JAGS and R. It discusses: (1) what is JAGS; (2) why you might
want to perform Bayesian modelling using JAGS; (3) how to install JAGS; (4) SPONSORS
where to find further information on JAGS; (5) where to find examples of JAGS
scripts in action; (6) where to ask questions; and (7) some interesting
psychological applications of Bayesian modelling.

JOBS FOR R USERS


What is JAGS?
JAGS stands for Just Another Gibbs Sampler. To quote the program author, (Junior) Data Analyst – Berlin
If you are an R blogger Associate Researcher (Data Analytics)
yourself you are invited to Martyn Plummer, “It is a program for analysis of Bayesian hierarchical models
Lead Data Scientist @ Stabio, Ticino, Switzerland
add your own R content using Markov Chain Monte Carlo (MCMC) simulation…” It uses a dialect of the
Senior Scientist, Translational Informatics
feed to this site (Non- BUGS language, similar but a little different to OpenBUGS and WinBUGS.
Senior Scientist, Translational Informatics @
English R bloggers should
Vancouver, BC, Canada
add themselves- here) Why JAGS? Data Scientist @ New York, United States
Empirical Research Librarian
The question of why you might want to use JAGS can be approached in several
JOBS FOR R-USERS different ways:
Full list of contributing R-bloggers
(Junior) Data Analyst – Why Bayesian rather than Null Hypothesis Significance Testing
Berlin (NHST) approaches?
Associate Researcher (Data
Analytics) To quote John D. Cook quoting Anthony O’Hagan, the benefits of
Lead Data Scientist @ “the bayesian approach are that it is 1. fundamentally sound, 2.
Stabio, Ticino, Switzerland very flexible, 3. produces clear and direct inferences, and 4. makes
Senior Scientist, use of all available information.” (see John’s blog post for
Translational Informatics elaboration)
Senior Scientist, John K. Kruschke made a similar argument in an Open Letter
Translational Informatics
@ Vancouver, BC, Canada extolling the benefits of the bayesian approach summarised as:
“(1) Scientific disciplines from astronomy to zoology are moving
to Bayesian data analysis. We should be leaders of the move, not
followers. (2) Modern Bayesian methods provide richer
RECENT POSTS information, with greater flexibility and broader applicability than
20th century methods. Bayesian methods are intellectually
Easy introduction to
Offensive Programming coherent and intuitive. Bayesian analyses are readily computed
Le Monde puzzle [#1114] with modern software and hardware. (3) Null-hypothesis
upcoming AI-related significance testing (NHST), with its reliance on p values, has
courses many problems. There is little reason to persist with NHST now
Free R/datascience Extract: that Bayesian methods are accessible to everyone.”
Evaluating a Classification
Model with a Spam Filter Why JAGS/BUGS rather than coding in a low-level language?
Parsing Sda Pages
Super Solutions for Shiny It’s simpler; for models that BUGS can handle, BUGS can shield
Apps #4 of 5: Using R6 you from some of the thorny details related to numeric integration.
Classes
There are simple interfaces with R.
Simulating data with
Bayesian networks
Why JAGS rather than WinBUGS or OpenBUGS?
JAMA retraction after
miscoding – new Finalfit
I’m using JAGS because it works well on Ubuntu. WinBUGS is

https://www.r-bloggers.com/getting-started-with-jags-rjags-and-bayesian-modelling/[05/01/2020 18:32:49]
Getting Started with JAGS, rjags, and Bayesian Modelling | R-bloggers

function to check recoding


broadly Windows specific, although I’ve read that it may work
Finding free science books
from Springer with the emulation software Wine.
Merge MLP And CNN in JAGS interfaces well with R. I’m comfortable writing scripts.
Keras Thus, I don’t personally see the benefits of using a dedicated GUI
Shiny 1.4.0 like WinBUGS. I can leverage what I know about R.
Strange Attractors: an R However, ultimately converting code between different flavours of
experiment about maths, BUGS is not that difficult.
recursivity and creative
For further discussion of the issue, see this r-help discussion and
coding
What are Your Use Cases this discussion on CrossValidated.
for rOpenSci Tools and
Resources? http://stats.stackexchange.com/questions/9202/openbugs-vs-jags
Selection bias, death, and
dying More than anything I found that JAGS provided a useful entry point into the
Automatic data types world of Bayesian modelling. This in turn appealed to me for several reasons:
checking in predictive
models 1. Even when I perform analyses using an NHST approach I often
intuitively think of empirical research questions in terms of probability
densities on a parameter of interest that changes as empirical and
theoretical evidence is accumulated. See for example Thompson’s (2002)
OTHER SITES
concept of meta-analytic thinking. Bayesian analysis provides tools for
Jobs for R-users formalising this orientation.
SAS blogs 2. More broadly, I appreciate the explicitness that a Bayesian approach
requires and encourages. E.g., specifying the distribution of the error
term, specifying a prior, specifying the distribution of parameters in a
mixed effects model, and so on.
3. There are several modelling challenges that I’m currently working
through where a Bayesian approach offers substantial flexibility and
applicability. In particular, I’m interested in modelling individual
differences in the effect of practice on strategy use and task performance
and then relating these individual differences to factors like intelligence,
prior experience, and personality.

JAGS Installation
JAGS runs on Linux, Mac, and Windows. I run JAGS on Ubuntu through an
interface with R called rjags.

The following sets out a basic installation process:

1. If necessary Download and install R and potentially a user interface to R


like R Studio (see here for tips on getting started with R).
2. Download and install JAGS as per operating system requriements.
3. Install additional R packages: e.g., in R
install.packages("rjags") . In particular, I use the packages
rjags to interface with JAGS and coda to process MCMC output.

Information on JAGS
The manual for different versions of JAGS is located here. e.g., the pdf of
the manual for 3.1.0. Several particularly relevant sections include:

the list of supported distributions and how they are parameterised.


This is often important given that the code looks similar to R but
often uses different parameterisation (e.g., precision is used
instead of standard deviation for a normal distribution).
It summarises differences between WinBUGS and JAGS.
It sets out available functions and operators.

The rjags help pdf for information about how to interface with JAGS
from R.

Martin Plummer has a blog called JAGS NEWS


The Bayesian Task View on CRAN lists and briefly describes the many R
packages related to Bayesian statistics.
Lunn and colleagues have a 2009 article called The BUGS project:
Evolution, critique and future directions. It provides a useful historical
perspective on the broader BUGS project, although it does not mention
much about JAGS specifically.

http://cran.r-project.org/web/views/Bayesian.html
http://www.stat.columbia.edu/~gelman/bayescomputation/lunnbugswithcommen
ts.pdf

Examples JAGS Scripts


I find it easier to pick up a new language by playing with examples. The
following provides links to example JAGS code, often with accompanying
explanations:

https://www.r-bloggers.com/getting-started-with-jags-rjags-and-bayesian-modelling/[05/01/2020 18:32:49]
Getting Started with JAGS, rjags, and Bayesian Modelling | R-bloggers

Justin Esarey
An entire course on Bayesian Statistics with examples in R and
JAGS. It includes 10 lectures and each lecture lasts around 2
hours. The content is designed for a social science audience and it
includes a syllabus linking with Simon Jackman’s text. The videos
are linked from above or available direclty on YouTube

John Myles White

A course on statistical models that is under development with


JAGS scripts on github
A model of Cannabalt scores using a gamma distribution
Simple introductory examples of fitting a normal distribution,
linear regression, and logistic regression
A follow-up post demonstrating the use of the coda package with
rjags to perform MCMC diagnostics.

John K. Kruschke

John Krushke wrote a book called Doing Bayesian Data Analysis:


A Tutorial with R and BUGS. It’s an excellent entry point into the
world of Bayesian statistics for the social and behavioural scientist
who has reasonable quantiative training, but is not necessarily
ready to absorb the kinds of books that are used in graduate-level
statistics courses.
The book has a website that provides all the examples used in the
book all the examples used in the book. See this blog post for a
link to the zip file containing the JAGS code.

BUGS Project

BUGS is well known for the large set of examples that accompany
the project.
The PDF providing documentation for Volume 1 and 2 of the
examples is available here.
You can see the JAGS code used to run these examples here.

Patrick J Mineault

An example from Gelman et al examining the effect of training


programs on SAT scores

Miguel Lobo

A short tutorial

Simon Jackman

Simon Jackman wrote the book Bayesian Analysis for the Social
Sciences that has accompanying JAGS code.
The book’s website has several useful resources including
example papers using Bayesian methods.
An associated course that uses the book as a text book has slides
and many examples of using and R and JAGS.

Johannes Karreth

A course on applied bayesian modelling with examples of data,


and code using the R2jags interface.

Myself

I also plan to post a few examples in upcoming blog posts. I


typically will share the code for these on my github account:
jeromyanglim. If you are reading this through syndication you
may wish to subscribe to the RSS feed of the source blog
jeromyanglim.blogspot.com.

More broadly, examples and tutorials designed for WinBUGS can generally be
adapted to be useful for JAGS. So for example, you can explore these WinBUGS
examples:

Michael Lee and Eric-Jan Wagemakers have a free online book called A
Course in Bayesian Graphical Modeling for Cognitive Science: see PDF
and website.
The website for the book Markov Chain Monte Carlo has several
WinBUGS examples.
There is an extensive list of BUGS resources on the BUGS project
website.

Asking questions

https://www.r-bloggers.com/getting-started-with-jags-rjags-and-bayesian-modelling/[05/01/2020 18:32:49]
Getting Started with JAGS, rjags, and Bayesian Modelling | R-bloggers

There are several places to ask questions about JAGS, R, and Bayesian statistics.

JAGS, BUGS, and bayesian questions on stats.stackexchange.com (aka


CrossValidated).
JAGS discussion forum
There’s also a BUGS discussion list

In general, I prefer the Stack Exchange model for asking and answering
questions on the internet, although the most important issue is typically where
the experts are located.

Interesting Psychological Applications of Bayesian


Modelling
If you want to see some examples of Bayesian modelling applied to
psychological data, I found the following articles quite interesting. PDFs are
available online.

Shiffrin, Lee, Kim, and Wagenmakers (2008, PDF) present a tutorial on


hierarchical bayesian methods in the context of cognitive science.
Michael Lee (2011, PDF) in Journal of Mathematical Psychology
discusses the benefits of hiearchical Bayesian methods to modelling
psychological data and provides several example applications.
Lee Averell and Andrew Heathcote (2010, PDF) in Journal of
Mathematical Psychology analyse individual differences in the forgetting
curve using a hierarchical Bayesian approach.

If you know of any other interesting JAGS resources or have any comments
about my choice of software for Bayesian data analysis, feel free to post a
comment.

 Share  Tweet

To leave a comment for the author, please follow the link and comment on their blog:
Jeromy Anglim's Blog: Psychology and Statistics.

R-bloggers.com offers daily e-mail updates about R news and tutorials about
learning R and many other topics. Click here if you're looking to post or find an
R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you
don't.

If you got this far, why not subscribe for updates from the site?
Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

R-bloggers was founded by Tal Galili, with gratitude to the R community.


Is powered by WordPress using a bavotasan.com design.
Copyright © 2019 R-bloggers. All Rights Reserved. Terms and Conditions for this website

https://www.r-bloggers.com/getting-started-with-jags-rjags-and-bayesian-modelling/[05/01/2020 18:32:49]

You might also like