Back to NIMBLE Vignettes

 

Load NIMBLE, set the option buildInterfacesForCompiledNestedNimbleFunctions.

library(nimble)
nimbleOptions(buildInterfacesForCompiledNestedNimbleFunctions = TRUE)
set.seed(0)

Example model. Any model will do.

code <- nimbleCode({
     for(i in 1:10) {
         x[i] ~ dnorm(0, sd=10)
         y[i] ~ dnorm(exp(x[i]), sd=10000)
     }
})

constants <- list()
data <- list(y = rnorm(10, 10, 1))
inits <- list(x = rep(0,10))

Rmodel <- nimbleModel(code, constants, data, inits)
## defining model...
## building model...
## setting data and initial values...
## running calculate on model (any error reports that follow may simply
## reflect missing values in model variables) ...
## 
## checking model sizes and dimensions...
## 
## model building finished.

Make an MCMC, with one of each type of sampler, run the MCMC.

conf <- configureMCMC(Rmodel, nodes=NULL)

conf$addSampler('x[1]', 'slice')
conf$addSampler('x[1:3]', 'RW_block')
conf$addSampler('x[4:6]', 'AF_slice', control=list(sliceWidths=rep(1,3)))
conf$addSampler('x[8:10]', 'RW_rotated_block')

conf$printSamplers()
## [1] slice sampler: x[1]
## [2] RW_block sampler: x[1:3]
## [3] AF_slice sampler: x[4:6],  sliceWidths: 1, 1, 1
## [4] RW_rotated_block sampler: x[8:10]
Rmcmc <- buildMCMC(conf)

Cmodel <- compileNimble(Rmodel)
## compiling... this may take a minute. Use nimbleOptions(showCompilerOutput = TRUE) to see C++ compiler details.
## compilation finished.
Cmcmc <- compileNimble(Rmcmc, project = Rmodel)
## compiling... this may take a minute. Use nimbleOptions(showCompilerOutput = TRUE) to see C++ compiler details.
## compilation finished.
Cmcmc$run(10000)
## |-------------|-------------|-------------|-------------|
## |-------------------------------------------------------|
## NULL

Here’s how you can extract the adaptive tuning parameters for each type of sampler:

## slice sampler:
## width (scalar)
Cmcmc$samplerFunctions$contentsList[[1]]$width
## [1] 15.96654
## RW_block sampler:
## scale (scalar)
## propCov (dxd matrix)
Cmcmc$samplerFunctions$contentsList[[2]]$scale
## [1] 1.36786
Cmcmc$samplerFunctions$contentsList[[2]]$propCov
##            [,1]       [,2]       [,3]
## [1,] 61.9738017 -0.5495009 -1.4369477
## [2,] -0.5495009 57.3981261 -0.2372433
## [3,] -1.4369477 -0.2372433 58.8741899
## AF_slice sampler:
## width (length-d vector)
## gammaMat (dxd matrix)
Cmcmc$samplerFunctions$contentsList[[3]]$width
## [1] 7.678307 7.783784 7.702128
Cmcmc$samplerFunctions$contentsList[[3]]$gammaMat
##             [,1]       [,2]        [,3]
## [1,] -0.96491372 -0.2523408 -0.07256472
## [2,]  0.09220917 -0.5844273  0.80618994
## [3,]  0.24584341 -0.7712126 -0.58719005
## RW_rotated_block sampler:
## scaleVector (length-d vector)
## factorMat (dxd matrix)
Cmcmc$samplerFunctions$contentsList[[4]]$scaleVector
## [1] 1 1 1
Cmcmc$samplerFunctions$contentsList[[4]]$factorMat
##           [,1]       [,2]        [,3]
## [1,] 0.8000470  0.4782436  0.36222612
## [2,] 0.5040177 -0.8632847  0.02656506
## [3,] 0.3254088  0.1613151 -0.93171162

You can also set the values of any of these adaptive parameters:

## slice sampler: 
Cmcmc$samplerFunctions$contentsList[[1]]$width
## [1] 15.96654
Cmcmc$samplerFunctions$contentsList[[1]]$width <- 4 
Cmcmc$samplerFunctions$contentsList[[1]]$width
## [1] 4
## RW_block sampler: 
Cmcmc$samplerFunctions$contentsList[[2]]$scale
## [1] 1.36786
Cmcmc$samplerFunctions$contentsList[[2]]$scale <- 3
Cmcmc$samplerFunctions$contentsList[[2]]$scale
## [1] 3
## AF_slice sampler:
Cmcmc$samplerFunctions$contentsList[[3]]$width
## [1] 7.678307 7.783784 7.702128
Cmcmc$samplerFunctions$contentsList[[3]]$width <- 1:3
Cmcmc$samplerFunctions$contentsList[[3]]$width
## [1] 1 2 3
## RW_rotated_block sampler:
Cmcmc$samplerFunctions$contentsList[[4]]$factorMat
##           [,1]       [,2]        [,3]
## [1,] 0.8000470  0.4782436  0.36222612
## [2,] 0.5040177 -0.8632847  0.02656506
## [3,] 0.3254088  0.1613151 -0.93171162
Cmcmc$samplerFunctions$contentsList[[4]]$factorMat <- diag(3)
Cmcmc$samplerFunctions$contentsList[[4]]$factorMat
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1

Is this what you need to do?