rjagsrunjags

Adding/removing summaries/plots requires re-compilation of runjags object


Since runjags object with all plots is too big, I tried to run.jags with plot=FALSE, save the resultant runjags object into file, restore it in new R session (as out) and then generate the plots by

out.with_summaries <- extend.jags(out, sample = 0, adapt = 0)

(for this trick see discussion here: https://stackoverflow.com/a/21859618/684229)

However, for unknown reason this re-compiles and adapts the model again! Even when I set the sample = 0, adapt = 0!

require(runjags)

t1 <- proc.time()
out.sum <- extend.jags(out, sample = 0, adapt = 0)
# Re-compiling rjags model and adapting...
# Calculating the Gelman-Rubin statistic for 4 variables....
# Convergence may have failed for this run for 4 parameters after 500
# iterations (multi-variate psrf = 214.873)
# Finished running the simulation
t2 <- proc.time()
print(t2 - t1)
#   user  system elapsed 
# 345.67    0.08  352.30 

It takes a pretty long time just for plotting the graphs, which is pretty annoying. The same happens when I compute the runjags object with plots and then try to get rid of them to store the runjags object small:

t1 <- proc.time()
out.no_sum <- extend.jags(out.sum, sample = 0, adapt = 0, summarise=FALSE, plot=FALSE)
# Loading required package: rjags
# Loading required package: coda
# Loading required package: lattice
# Linked to JAGS 3.3.0
# Loaded modules: basemod,bugs
# Re-compiling rjags model and adapting...
# Finished running the simulation
t2 <- proc.time()
print(t2 - t1)
#    user  system elapsed 
#  327.53    0.05  329.73

Any tips on how to fix this issue (apart for writing my own plotting functions)?

Warning: The second run the extend.jags function on the same runjags object is already fast. But if you save the runjags object and load it again in a new session, extend.jags is slow again. It seems that runjags or JAGS are caching something (but not within the original runjags object).


Solution

  • This extend.jags function call is slow because the model is recompiling (in your case it is not actually adapting, despite the somewhat misleading message). This is because you're using the rjags method from a saved object - this means the model has to be re-loaded into memory and made ready to sample from (even though you don't actually want to sample from it). This doesn't happen the second time you call extend.jags because it is already compiled.

    Using extend.jags in this way is really a bit of a hack - the next version of runjags will provide a cleaner way of doing this. In the meantime, if you specify adapt=0, sample=0, method='simple' this should prevent the JAGS object being recompiled.

    EDIT: As stated in the help file for runjags, it is more efficient to recreate plots using lattice::traceplot or densityplot (or both). To extract an MCMC object use as.mcmc.list(runjags_object) - this also allows you to extract specific variables if necessary, see ?as.mcmc.list.runjags