# Tricks for working with JAGS

**Posted:**May 8, 2015

**Filed under:**Post |

**Tags:**BUGS, JAGS, R Leave a comment

- The Gamma distribution is frequently used as a prior for the precision in a model because of its conjugacy with the Normal. Given JAGS parameterization of the Gamma distribution with a rate and shape parameter, it’s not easy to select sensible hyper-parameters. It’s easier to think of hyper-parameters that correspond to the mean and the variance of a prior for the precision. To use mean and variance hyper-parameters (mu and sigma) with JAGS’s
`dgamma`

, use:`dgamma( ((mu^2)/sigma), (mu/sigma))`

. - The
`glm`

module provides a series of more efficient samplers that JAGS automatically selects whenever possible (e.g. Albert/Chib (1993)). This usually leads to much better mixing. I tend to load the module right away with the actual`rjags`

package, e.i.`library(rjags); load.module("glm")`

. - To check which samplers are selected by JAGS, use the
`list.samplers()`

function on the JAGS model object. This is useful, because sometimes a slight change in the coding helps JAGS to select better samplers. For example, when using a logit model in JAGS and you want the Holmes-Held sampler from the glm module to work, do not use`dmnorm()`

but`dnorm()`

for the prior of the coefficient, e.i. do not use blocking. - Avoid deterministic nodes as much as possible to increase speed. In particular, do all pre- and post processing in R including drawing from the predictive distribution and compute deterministic functions inside probabilistic statements, e.g.
`y ~ dnorm( (x*beta), 1)`

instead of`mu .`

- For comprehensible / readable code, let the running index be the small letter corresponding to the capital letter of the upper limit, e.i.
`for(n in 1:N)`

instead of`for(i in 1:N)`

. This becomes especially useful when one has many nested hierarchies.

Advertisements
(function(g,$){if("undefined"!=typeof g.__ATA){
g.__ATA.initAd({collapseEmpty:'after', sectionId:26942, width:300, height:250});
g.__ATA.initAd({collapseEmpty:'after', sectionId:114160, width:300, height:250});
}})(window,jQuery);
var o = document.getElementById('crt-563266450');
if ("undefined"!=typeof Criteo) {
var p = o.parentNode;
p.style.setProperty('display', 'inline-block', 'important');
o.style.setProperty('display', 'block', 'important');
Criteo.DisplayAcceptableAdIfAdblocked({zoneid:388248,containerid:"crt-563266450",collapseContainerIfNotAdblocked:true,"callifnotadblocked": function () {var o = document.getElementById('crt-563266450'); o.style.setProperty('display','none','important');o.style.setProperty('visbility','hidden','important'); }
});
} else {
o.style.setProperty('display', 'none', 'important');
o.style.setProperty('visibility', 'hidden', 'important');
}