# Issue with thinning in R2OpenBUGS vs R2jags

**Gianluca Baio's blog**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

While preparing the practicals for our course at the University of Alberta, I’ve discovered something kind of interesting. I’m sure this is nothing new and actually people who normally use both OpenBUGS and JAGS have already figured this out.

But since I normally just use JAGS, it took me a bit to see this, so I thought I should post about it…

So: the issue is that when running a Bayesian model based on MCMC (eg Gibbs sampling), often it helps to improve convergence if the chains are “thinned” $-$ basically, instead of saving all the successive iterations of the process, only 1 on $s$ are stored and this generally reduces autocorrelation.

R2jags (which is the R library to interface R and JAGS) lets you select the total number of iterations you want to run, the number of “burn-in” iterations (which will be discarded) and the thinning factor. So a command

m <- jags(..., n.iter=20000, n.burnin=9500, n.thin=21, n.chains=2)

will generate two chains, each with 20000 iterations, discard the first 9500, which of course leaves 10500 iterations, and then save only one every 21 of these $-$ a total of 500 iterations per chain.

Conversely, R2OpenBUGS thinks in slightly different terms: as the help (which to be fair I’ve never bothered reading…) says “*The thinning is implemented in the OpenBUGS update phase, so thinned samples are never stored, and they are not counted in n.burnin or n.iter. Setting n.thin=2, doubles the number of iterations OpenBUGS performs, but does not change n.iter or n.burnin*“.

So the command

m <- bugs(..., n.iter=20000, n.burnin=9500, n.thin=21, n.chains=2)

will actually save 21000 iterations altogether (10500 per chain).

I realised this because the same relatively simple model was taking for ever when run using OpenBUGS and was really quick in JAGS $-$ no kidding!…

**leave a comment**for the author, please follow the link and comment on their blog:

**Gianluca Baio's blog**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.