Sunday, January 1, 2012

Now in JAGS! Now in JAGS!

I have created JAGS versions of all the BUGS programs in Doing Bayesian Data Analysis. Unlike BUGS, JAGS runs on MacOS, Linux, and Windows. JAGS has other features that make it more robust and user-friendly than BUGS. I recommend that you use the JAGS versions of the programs. Please let me know if you encounter any errors or inaccuracies in the programs.

JAGS is an MCMC sampler much like BUGS. To communicate with JAGS from R, a library called rjags is loaded into R. The diagram below illustrates the analogous roles of the sampling programs and the interface libraries:

JAGS, OpenBUGS, and WinBUGS are MCMC samplers. Each has a corresponding package for communicating with R. The book used OpenBUGS with BRugs. But the new programs feature JAGS with rjags.
Even if you use Windows, JAGS is nicer than BUGS for many reasons. One nicety is that JAGS has a dynamic progress bar that tells you how much of the requested chain has been sampled. JAGS also seems more robust and actually works in many situations where BUGS mysteriously fails; e.g., the equals(,) function works in JAGS where it would not work in BUGS.

It is easy to install JAGS and rjags. First, go to the JAGS web site and follow the instructions for downloading and installing JAGS for your operating system. Then, at the command line in R, type install.packages("rjags"). Done!

It is easy to get the JAGS versions of the programs for Doing Bayesian Data Analysis. JAGS versions of the programs use the same name as the BUGS versions, but with the string "Bugs" or "BRugs" replaced with "Jags". (All of the original programs are still available.) A zip file with all the programs and data files is available here. A list of individual programs is available here; click on the column header "last modified" twice to get the most recent files to appear at the top of the list. [Update January 12, 2012: A few programs (BernBeta...) were inadvertently missing from the upload of January 1. I've now posted them with the other programs. Thanks to a reader for pointing out the missing files.]

As explained in other blog posts, there are other revisions and additions to the programs.
* There is a new program for split-plot designs, called SplitPlotJags.R. See this blog post.
* The new versions of the programs do no thinning but use longer chains (defaulting to chain lengths of 50000, but you can use even longer chains for real research reports). See this blog post.
* The ANOVA-like programs consider only the sum-to-zero (STZ) versions of the parameters, because autocorrelation in the pre-STZ parameters is irrelevant. Use the versions of the programs with filenames that end with "STZ". See this blog post.
* Instead of saving plots using dev.copy2eps, the programs use savePlot. It is easy to change the savePlot command to save in whatever format you prefer, such as jpg/jpeg. Each program also begins with a check of the user's operating system to redefine the windows() command if necessary. Both of these changes were recommended by comments from readers.

[Added 28 January: Complete installation instructions are listed here.]

15 comments:

  1. Thank you for this excellent New Year's present. I will test it out on my class (many of us are using MacOS), and let you know how it goes.

    ReplyDelete
  2. This is great. I bought the book last year, but as Mac user, had put off trying out the code. Now I will.

    ReplyDelete
  3. Thanks for doing this. I prefer JAGS since it is cross-platform.

    ReplyDelete
  4. Thank you!
    I had been struggling managing an environment in my multiple machines, work, home, mobile.
    I can continue studying now!!

    ReplyDelete
  5. FYI: I had trouble with rjags on Mac OSX10.7, until I discovered that the current mac version of rjags is built to talk to JAGS 2.X and not JAGS 3.X. Installing JAGS 2.X made things run smoothly.

    ReplyDelete
  6. Thanks for posting. JAGS and rjags are looking to be simple and easy to use. I just installed very easily without getting any kind of difficulty. I am happy to read that it is easy to get the JAGS versions of the programs for Doing Bayesian Data Analysis. Let me try it....:)

    ReplyDelete
  7. plotChain.R does not seem to work with JAGS since the samplesStats function is not available. Suggestions?

    ReplyDelete
  8. Regarding plotChain.R:

    All the JAGS versions of the programs have (or should have) this code at the beginning of the EXAMINE THE RESULTS section:


    checkConvergence = F
    if ( checkConvergence ) {
    show( summary( codaSamples ) )
    windows()
    plot( codaSamples , ask=F )
    windows()
    autocorr.plot( codaSamples , ask=F )
    }

    All you have to do is change checkConvergence=F
    to
    checkConvergence=TRUE

    ReplyDelete
  9. As you noted on your other page about installing JAGS: mac and linux users probably need to install JAGS 2.*** rather than JAGS 3.****.

    ReplyDelete
  10. Warning in install.packages :
    package ‘rjags’ is not available

    ReplyDelete
  11. Dear Anonymous March 29:

    Without more information, here's my guess: When installing rjags (from R), you need to be linked to the internet so that the package can be retrieved. If that's not the problem (i.e., if you are getting through the internet to a file repository), then try some other geographical repository (the one you selected might be temporarily down or out of date).

    ReplyDelete
  12. I just started working through the book, already had R and RStudio installed on my Mac, and came across the BUGS/WINE requirement... after a quick search of your blog I'm very happy to learn that I won't need to run a VM on my Mac to do the course exercises. Thanks!!!

    ReplyDelete
  13. Thank you SO much for updating the examples to work with JAGS! This makes Bayesian data analysis immensely more comfortable and efficient, and even fun!

    ReplyDelete
  14. Ihave installed jags and execute the library(rjags) command successfully, but when I execute
    modelCheck("Model.txt") within R I
    get the following error message:

    Error: could nto find function
    "modelCheck"
    I'm sure it's obvoius, but I don't
    see what I am doing wrong. Could
    you please tell me?
    Thanks
    Neal

    ReplyDelete
  15. Dear Neal (nlen):

    I need some more context. What program are you trying to run? What is the very first error message you encounter and exactly where in the code?

    ReplyDelete