Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extending burn-in period after JAGS model has been run in runjags for R

The runjags package for R is fantastic. The parallel capabilities and the ability to use the extend.jags function make my life so much better. However, sometimes, after I run a model, I realize the burn-in phase should be have been longer. How can I trim extra samples out of my run.jags output, so I can re-estimate my parameter distributions and check for convergence?

jags.object <- run.jags(model, n.chains=3, data=data, monitor =c('a','b'), sample=10000)
like image 887
colin Avatar asked Jun 23 '16 17:06

colin


2 Answers

There is currently no way to do this within runjags unfortunately, so you will have to work with the underlying mcmc.list object - something like:

library('coda')
mcmc.object <- as.mcmc.list(jags.object)
niter(mcmc.object)
windowed.object <- window(mcmc.object, start=10001)
summary(windowed.object)

Note that the start (and end) arguments of window.mcmc include the burn in phase, so if you have 5000 burn in + 10000 samples then this code gives you iterations 10001:15000

However, a window method for the runjags class would be a good idea, and hopefully something that will appear soon!

[It may also be worth noting that you can use the combine=FALSE argument with extend.jags to drop the entire first lot of iterations, but this obviously requires re-sampling new iterations so not exactly what you want.]

Also - thanks for the kind words about the package - feedback and feature suggestions are always welcome at https://sourceforge.net/p/runjags/forum/general/ :)

like image 141
Matt Denwood Avatar answered Oct 21 '22 00:10

Matt Denwood


If you want to trim off extra samples from the posterior distribution you can subset the mcmc.list with the lapply function.

The code below would trim off the first 50 samples from each chain. You would just need to change 1:50 to something else if you wanted to trim more.

trimmed.posterior <- lapply(jags.object, function(df, vec){df[-vec,,drop=TRUE]}, 1:50)
like image 1
mfidino Avatar answered Oct 21 '22 01:10

mfidino