What’s new in JAGS 4.0.0 part 3/4: R-style features

The BUGS language is syntactically very similar to R and there are several packages that allow R users to interface with the JAGS library. When you are using both R and JAGS simultaneously, small differences between the languages can be frustrating. Consequently there is some pressure to narrow the gap between the two languages.  There are some things we cannot change, such as the parameterization of the distributions, but there is a tendency for JAGS to become more R-like with each release. JAGS 4.0.0 introduces a few more R-like features.

The major change is vector indexing. In R, if you want to create a vector y consisting of the 5th, 4th, 2nd and 8th, elements of the vector x, then you do this very simply:

ind <- c(5,4,2,8)
y <- x[ind]

The equivalent calculation in JAGS 3.4.0 requires a for loop

for (i in 1:length(ind)) {
   y[i] <- x[ind[i]] 

JAGS 4.0.0 introduces vector indexing so that you can create the vector y in a single line. It also includes the c() function from R that combines its arguments into a single vector. Both of these changes should improve the expressive power of the BUGS language.

A side effect of this change is that for loops have become much more general. Previously for loops had to take the form

for (i in a:b) {


A for loop in JAGS 4.0.0 taks the form

for (i in v) {

where “v” is any expression that evaluates to a fixed integer vector. The integer sequence operator “:” is now a standalone function that can be used anywhere in JAGS code. The arguments to “:” must be fixed integer expressions. This is because the length of a vector in JAGS is determined by the compiler and cannot change from one iteration to another. An important difference between JAGS and R remains: if b < a then a:b generates a zero-length vector in JAGS 4.0.0, whereas R generates the reverse sequence. Some people consider that this is a mis-feature of R.

A minor change that will help save some typing is that compiler now accepts an equals sign for the definition of a non-stochastic node, e.g.

a = 1
b = sum(x[1:N])

This parallels the use of the equals sign for assignment in R.

Finally, the sum() and prod() functions can take a varying number of arguments and return respectively the sum and product over all arguments, like their R equivalents. The new behaviour is backward-compatible with code written for previous versions of JAGS.


4 thoughts on “What’s new in JAGS 4.0.0 part 3/4: R-style features

  1. I’m just adding a note here that the change to for loops may break some old JAGS model descriptions. Previously when the syntax was “for(i in a:b)” then expressions “a” and “b” were evaluated first. So, for example, “for(i in 1:N-1)” generated a sequence from 1 to N-1.

    In JAGS 4.0.0 the “:” operator has a lower precedence than the arithmetic operators so this expression will instead generate a sequence from 0 to N-1. Use brackets to enforce precedence (Most people seem to do this anyway).

  2. Thanks for all the recent JAGS updates Martyn. Look forward to trying 4.0.

    Quick question: in your example of 3.4.0 versus 4.0, you mention that where in 3.4.0 you had to do this:

    for (i in 1:length(ind)) {
    y[i] <- x[ind[i]]

    Whereas in 4.0 you can do this:

    ind <- c(5,4,2,8)
    y <- x[ind]

    Is there any difference in speed between these two alternatives?

  3. I made these changes in order to improve readability of R code. There might be a speed implication but I haven’t tested them. You should bear in mind that although BUGS has a similar syntax to R, it does not mean that vectorized calculations are necessarily faster.

  4. Pingback: 4 for 4.0 - The Latest JAGS - Statistical Modeling, Causal Inference, and Social Science

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s