Tagged: rjags

Additional installation instructions for Mac users

I have a Mac, so I figured I would post some notes on software installation specific to Mac users, from my own experience, to supplement Jacob’s instructions here. Regardless of which operating system you’re using, I encourage you to run the test models in step 4 to check that everything has installed correctly. If possible, please do this before Tuesday, so we have a chance to troubleshoot with you before class if you run into trouble.

1. Install R

Mac users click here (for PC and Linux, see the links here).

This downloads the installation package file. Click on it in your downloads folder to open the file. This opens the install wizard thingy.

Read the stuff and click “Continue” a bunch.

Check that it worked. Either click on the R logo in your Applications folder to launch, or open a terminal and type “r”, then hit enter. You should see something like this:

R version 3.0.2 (2013-09-25) -- "Frisbee Sailing" Copyright (C) 2013 The R Foundation for Statistical Computing Platform: x86_64-apple-darwin10.8.0 (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. 

2. Install JAGS

Mac users click here

This downloads the installation package. Click on it in your downloads folder to open, and then click on the package installer that appears in Finder (“JAGS 3.3.0.pkg”). This opens the install wizard thingy.

Read the stuff and click “Continue” a bunch.

Check that it worked. You will NOT see an application called JAGS in your applications folder. JAGS just runs in the background and talks to R. To check that it installed correctly, open the terminal and type “jags”, then hit enter. You should see something like this:

Welcome to JAGS 3.3.0 on Sat Jan 11 01:29:42 2014 JAGS is free software and comes with ABSOLUTELY NO WARRANTY Loading module: basemod: ok Loading module: bugs: ok 

If you’re not comfortable using the terminal, you can skip this and just check that all three programs are working together at the end (step 4).

3. Install rjags (and R2jags, if you like)

You install rjags (and R2jags) from within R. Open R, and then type

install.packages("rjags", dependencies=TRUE) 
install.packages("R2jags", dependencies=TRUE) 

and hit enter after each. If it asks you to select a CRAN mirror, pick “USA (OR)”. This will run installation scripts.
Check that it worked. In R, type

library(rjags) 

You should see something like this:

Loading required package: coda Loading required package: lattice Linked to JAGS 3.3.0 Loaded modules: basemod,bugs 

4. Check that everything is working.

Run a test script for one or two simple models.

Test 1: Download the program files from Kruschke’s book from here. You will need to set the working directory for R to that folder using setwd().

Open the file “BESTexample.R”. Select all of the text, and run it. It may take a little while. At the end, you should see several pretty graphs pop up.

Test 2: Another test, using R2jags instead of rjags (Note that for this script to work, you have to run “install.packages(“R2jags”, dependencies=TRUE)” first, if you haven’t already):

Download the R script (“example jags.r”) and JAGS model file (“Rate 1.txt”) in this folder, taken from the Lee & Wagenmakers text. You will need to edit the R script to set your working directory to the folder where the JAGS script is saved.
Open the R script, select all, and run. If it runs correctly, you should see a histogram of the parameter “theta” pop up.

If you’re getting an error about x11, you may need to install xquartz (http://xquartz.macosforge.org/landing/). Do that, and then try running it again.

If you can’t get at least one of these models to run, please email Rose or Jacob right away. Sometimes it’s tricky to get these programs to work. For this reason, it is a really good idea NOT to wait until 10 minutes before we meet to start installing software.

Getting Set Up with Software

Throughout the term, we will be discussing code samples from two complimentary software packages / languages: R and JAGS (“Just Another Gibbs Sampler”). It would be helpful to have both of these up and running on your personal computers before we dive in (i.e., by Week 2, if possible). For the most part, setup of both is straightforward; most of you likely have R set up already. Having said that, I’ve run into enough trouble installing JAGS in the past (albeit in Linux) that I’m writing to point out some resources.

John Kruschke, from whom we will be reading for our week 2 meeting, has the best guide that I’ve found for installing the necessary packages. That guide is here. I recommend following it. In short, you’ll need to install three components:

  1. R (and, if you like, an IDE such as RStudio)
  2. JAGS, from the project’s SourceForge page
    1. For Linux, JAGS is also available from major distros’ repositories.
    2. JAGS has released three versions. If given the choice, I recommend installing JAGS3, since it’s most up-to-date.
  3. rjags, an R package that lets JAGS and R speak to each other. To install this, launch R, then run install.packages('rjags').

Notes on the installation process:

  • Make sure that R and JAGS are both working before attempting to install rjags. To do this, open a terminal, run R (i.e., type R, and press enter), and make sure that R loads (to exit, type quit()). Then try running jags and make sure that it launches, as well (to exit, you can use Ctrl+C).
  • Potential problems that may be unique to Linux (and, within Linux, may even be unique to openSUSE): If you try to install rjags and are getting error messages about files not being found, one of a few things is most likely wrong:
    • You don’t have a C++ compiler installed.
    • If you get an error message about a file called “console.h” not being available, you may need to install the jags-devel package in addition to the jags package.
    • You are not installing JAGS with superuser privileges (sudo R).
    • You may want to look at the post here, if all else has failed.

Feel free to comment on this post if you have questions or other tips to add. Good luck!

UPDATE: Rose has written up helpful additional Mac-specific instructions (as well as some additional instructions that should work cross-platform) here. Take a look at those, as well.