Hi Eduardo,
Good question. Yes, you can access it, but it takes a little explanation.
Compiled nimbleFunction objects automatically provide access from R to member variables and methods in the underlying C++ object. This access is provided in the reference class objects constructed to interface with the corresponding C++ objects. In other words, in R,
compiled_object$x
will access the "x" in the C++ object that compiled_object serves as an interface to. "compiled_object" itself is an R reference class object.
In this case, you will want to access a member variable called "numDivergences" in the NUTS (HMC) sampler.
However, to reduce the building of components that are only rarely used, there is a default setting that this kind of access will not be nested. In other words, compiled_object$my_object$x will not work by default, because my_object is nested within compiled_object. To make this work, you need to set a nimbleOption:
nimbleOptions(buildInterfacesForCompiledNestedNimbleFunctions = TRUE)
Now, where to find the value you want? In the compiled MCMC object, there is an object "samplerFunctions" that is a list of the samplers used. If you are using HMC as the sole sampler, then it is probably the only one on the list. There could be other samplers (possibly if you have posterior predictive nodes, for example). When you configure the samplers, you should see a list confirming that. If you are not sure, you can do MCMCconfig$printSamplers(), or you can look at the uncompiled samplerFunctions. For example, you would be doing:
model <- nimbleModel( < your inputs > )
MCMCconfig <- configureHMC(model)
MCMCconfig$printSamplers() # Look to determine where the NUTS sampler is
MCMC <- buildMCMC(MCMCconfig)
compiled <- compileNimble(model, MCMC)
runMCMC(compiled$MCMC, <your inputs > )
OR
model <- nimbleModel( < your inputs > )
MCMC <- buildHMC(model) # does configureHMC and then buildMCMC
MCMC$samplerFunctions # look at this list
<same as above>
Once you know which sampler in the samplerFunctions is the HMC sampler (say it is the 1st), and if you have set the above nimbleOption, you should be able to do
compiled$MCMC$samplerFunctions[[1]]$numDivergences
You can inspect the source code for nimbleHMC, looking at the sampler_NUTS nimbleFunction, and see in the setup code various other quantities you can access in this way.
An alternative would be to clone the nimbleHMC package and modify it. In the "after_chain" function (called for every sampler after the chain has been run), you could insert a call to a function that you could define with nimbleRcall. This provides a way to pass any variables you want (e.g., numDivergences) back to R and then manage how to store them in any way you want. For example, you would insert into after_chain:
save_divergences(numDivergences)
where by nimbleRcall you have made "save_divergences" call an R function like "save_divergences_R", which you define normally in R like
save_divergences_R <- function(numDivergences) { < save numDivergences however you want > }
HTH! I did not test the code I gave you above so it is possible I made a mistake.
Perry