Our lab conference table is currently hosting a Bayesian data analysis / programming in R learning group. We’re planning to work through some key chapters of a textbook called Doing Bayesian Data Analysis: A Tutorial with R and BUGS by John K. Kruschke. The book uses R and BUGS. BUGS runs on Windows machines fairly simply, but needs WINE to be run on a Mac. JAGS is a BUGS alternative that runs on a Mac with no extra steps, so some us have decided to give JAGS a go. Caitlyn and I (under Bill’s guidance) have thrown together a quick tutorial on setting up JAGS in R.
0. Make sure you have xcode and x11.
This might already be on your computer. Xcode is probably on your Mac OS install disc. If you have matlab you’ve got X11. Ask Bill or Kim for more information if you’re stuck at this step.
1. Download and install R.
R is available for download here: http://www.r-project.org/
2. Download and install JAGS.
JAGS is available for download here: http://sourceforge.net/projects/mcmc-jags/files/ – grab the link at the top (“Looking for the latest version?”)
3. Use R’s Package Installer to install the rjags package.
Run R. Go to the menu Packages & Data -> select Package Installer.
If you haven’t ever used Package Installer in R before, you’ll need to select a mirror for grabbing packages. Click on “get list” and a box will pop up with mirror locations. Select the one most appropriate for you. R will ask you “Do you want me to remember the mirror you selected for future sessions?” – You might as well say Yes.
Once you have a mirror, you’ll be able to search packages no problem. Type rjags in the search bar on the top right, and click the “Get List” button. Highlight the version you want, and click the “Install Selected” button.
If everything has gone well, you’ll see something kind of like this in your R Console:
Content type 'application/x-gzip' length 328998 bytes (321 Kb) opened URL ================================================== downloaded 321 Kb The downloaded packages are in /var/folders/WA/WAhiByrrFbGuXVxkFSVipk+++TI/-Tmp-//Rtmp4tCNNB/downloaded_packages
4. Use R’s Package Installer to install the coda package if you don’t have it.
If you already have the coda package, chances are you aren’t reading this tutorial. However, to find out if you need to install the coda package, type
If you don’t have it, you’ll see:
> library('rjags') Error: package 'coda' required by 'rjags' could not be found
Note that in your R-using career you may get messages like this, where some packages you have require other packages you don’t have. Not a problem! Just go back to Packages & Data -> Package Installer and look for coda in the same way you searched for rjags. Search for coda, click on the coda package, and click on Install Selected. R Console will tell you something like this:
trying URL 'http://cran.stat.sfu.ca/bin/macosx/leopard/contrib/2.13/coda_0.14-4.tgz' Content type 'application/x-gzip' length 199166 bytes (194 Kb) opened URL ================================================== downloaded 194 Kb The downloaded packages are in /var/folders/WA/WAhiByrrFbGuXVxkFSVipk+++TI/-Tmp-//Rtmp4tCNNB/downloaded_packages
Now, you have coda! Run rjags again by typing library(‘rjags’) or by pressing the up arrow key to access previous commands (just like in Matlab):
> library('rjags') Loading required package: coda Loading required package: lattice module basemod loaded module bugs loaded >
You have JAGS! Way to go, you! We’ll be using this blog space to post tutorials for fellow lab-mates when simple things like this arise, so watch this space if you’re interested.
(An aside: I haven’t read the entire thing, but so far I think this is a great book. Kruschke has a wonderful way of guiding students through complicated topics by anticipating the kinds of questions they might ask. He’s not condescending to those who might need a bit of extra explanation, but his explanations are never boring for those who know what he’s talking about. Also, there are puppies on the cover. And the book is full of quatrains.)