Back to NIMBLE Vignettes

 

library(nimble)

Define a model with one node.

code <- nimbleCode({
    a ~ dnorm(0, 1)
})
constants <- list()
data <- list()
inits <- list(a = 0)

Rmodel <- nimbleModel(code, constants, data, inits)

We’ll add three samplers to node a.

The first RW sampler is not adaptive, so it should be fast.

The second RW sampler is adaptive with adaptInterval = 10, very frequent, so it should be slower.

The third is a slice sampler, which should be the slowest.

conf <- configureMCMC(Rmodel, nodes = NULL)

conf$addSampler(target='a', type='RW', control = list(adaptive=FALSE), print=FALSE)
conf$addSampler(target='a', type='RW', control = list(adaptive=TRUE, adaptInterval=10), print=FALSE)
conf$addSampler(target='a', type='slice', print=FALSE)

conf$printSamplers()
## [1] RW sampler: a,  adaptive: FALSE
## [2] RW sampler: a,  adaptInterval: 10
## [3] slice sampler: a

Build, compile, and run MCMC.

When you run the MCMC using mcmc$run(...), add the extra argument time = TRUE, which will make it record sampler timings.

Rmcmc <- buildMCMC(conf)
Cmodel <- compileNimble(Rmodel)
Cmcmc <- compileNimble(Rmcmc, project = Rmodel)
system.time(Cmcmc$run(1000000, time = TRUE))
##    user  system elapsed 
##   1.232   1.613   2.845

Now we’ll extract the times. It’s a vector, with length the number of samplers (in this case, three). Each number corresponds to the number of seconds (total) spent in each sampler.

times <- Cmcmc$getTimes()
times
## [1] 0.482218 0.498343 0.638112