We expect folks to explore the code: run it, look at the different objects with $\tt str$, $\tt summary$ and $\tt head$, and to try adapting for their own analyses.
We assume you are familiar with R, in particular lists, data frames, arrays, factors and
for loops. There are plenty of resources on the web to get up to speed with R, and we do not include that here.
You should be able to copy/paste R code from browser directly into an R script (works on Firefox and Chrome, others?), but we also provide a ZIP file with the code and data for each page here.
You will need recent versions of R and JAGS. These are free and work on all platforms. Details of the latest versions and links to download software are here.
On Windows, we recommend using Notepad++ with NppToR – see here for an overview. We prefer this to RStudio as it does not mess with the internals of R; the only change it makes is to set itself as the default editor with
options(editor="C:\Program Files (x86)\NppToR\NppEditR.exe"). It enforces Single Document Interface (SDI) mode – ie, the Console and graphics windows are separate top-level windows – and you can add other options1You will find a list of options here to the R CMD call – I use
--no-save --no-restore. Sadly, that is only available for Windows.
We recommend using a custom folder for R temporary files on Windows and a custom library folder too. See here for details.
Running the JAGS code
JAGS does not have its own graphical user interface (GUI) and is designed to be run from R. Mostly we use the
jags function in Ken Kellner’s
jagsUI package to do this.
We run MCMC chains in parallel for all except the simplest models. In our code we specify 3 chains, since all recent computers have at least 4 cores. To check, run
parallel::detectCores(). (This returns the number of logical cores, which is generally twice the number of physical cores thanks to hyperthreading. References to “cores” in the tutorial mean logical cores.) If your computer has more cores – and most laptops on sale now have 8 – you may want to increase the number of chains and reduce the number of iterations per chain.
We try to use core R functions as far as possible and minimise the number of third-party packages used. We don’t use pipes (the
%>% operator from the
magrittr package) as we think it’s important to inspect and check the calculation at each step.
We started off using the Google style guide for R, but that’s no longer on the Google web site and its offspring are more complex. So we did our own “essential style guide“.
When R output is included in the scripts, it’s preceded by a “
#” so that it doesn’t cause errors when the script is run. Large blocks of output have been edited. In particular, the output from
jagsUI::jags has columns for
f, which are rarely relevant to our analyses and exceed the width of the tutorial page, so I have just deleted them.
Our JAGS code follows similar conventions to R. See our JAGS coding preferences for more information.
Questions and suggestions
Please use the Comments area for questions about the material on the page or issues in running the code. Please make sure you have the latest versions of everything before reporting an issue: we don’t want to know if old packages don’t work!