# Thinning chains in BUGS/JAGS

Hi I have a quick question about the details of running a model in JAGS and BUGS.

Say I run a model with n.burnin=5000, n.iter=5000 and thin=2. Does this mean that the program will

• run 5,000 iterations, and discard results,

and then

• run another 10,000 iterations, only keeping every second result?

If I save these simulations as a CODA object, are all 10,000 saved, or only the thinned 5,000? I'm just trying to understand which set of iterations are used to make the ACF plot?

Thank you

• Note that even though it is rather common to do thinning to reduce the autocorrelation, it is more often better not to thin. If the chain is long enough, you get a good approximation of the posterior even with autocorrelation (and usually better than from the thinned chain without autocorrelation). See for example here doingbayesiandataanalysis.blogspot.de/2011/11/…. However, thinning can make sense if you want to make further computional intensive calculation on your MCMC sample or if you run into memory samples. – Erik Feb 4 '13 at 13:34

Short answer: The number of iterations incorporates the burn in and does not incorporate thinning.

Less short answer: If you were to run a BUGS model through R2WinBUGS or R2OpenBUGS (or view a summary of WinBUGS output) with the arguments you stated:

 n.iter=5000, n.burnin=5000, n.thin=2


you would get an error message/no output. n.iter refers to the total number of iterations including the burn in, hence all your iterations are burn in and are thrown away (or not included in the CODA output and any ACF plot in WinBUGS).

Thinning is treated differently (in relation to n.iter). For example if you set your MCMC up with any of the following arguments:

 n.iter=6000, n.burnin=5000, n.thin=1
n.iter=6000, n.burnin=5000, n.thin=5
n.iter=6000, n.burnin=5000, n.thin=10


only 1000 iterations will be saved, i.e. all non-thinned simulations are discarded (in CODA output or any ACF plot in WinBUGS).

Not sure if this is the same for jags?

Other answered is correct about BUGS but his answer does not apply to JAGS (at least, not to rjags, R2jags might be different). I haven't used JAGS directly, but the writer of rjags is the creator of JAGS so I would guess they use the same convention.

In rjags, the jags.model object keeps track of the number of iterations that the chain(s) have been run.

Here is a small model in a file "tmpJags":

model {
X ~ dnorm(Y, 1)
Y ~ dt(0, 1, 1)
}


Then I run

X <- 1
jm <- jags.model(file = "tmpJags", data = list(X = X),
n.chains = 4, n.adapt = 1000)


jm consists of 4 chains, each of which has been run 1000 total iterations. Then I do

samps <- coda.samples(jm, "Y", n.iter = 1000, thin = 10)


Now all 4 of my chains have been run for a total of 2000 iterations each, and I have collected 100 samples from each chain, so that 400 samples are saved in total.

To me, it makes sense to do it this way because for the purposes of monitoring chain convergence you would rather think in terms of total iterations than iterations after thinning.