mcmcNIMBLERun: Run NIMBLE using the specified arguments

View source: R/mcmcInternals.R

mcmcNIMBLERunR Documentation

Run NIMBLE using the specified arguments

Description

mcmcNIMBLERun provides a function to encapsulate an entire run of NIMBLE (including compilation steps) using a set of user-specified parameters

Usage

mcmcNIMBLERun(
  ...,
  mcCores = 1,
  runTimeGlobal = list(),
  initCode = list(),
  exitCode = list()
)

Arguments

...

Named arguments to be passed to the constituent NIMBLE functions configureMCMC, runMCMC, nimbleModel, and compileNimble. The argument names will be matched to any relevant arguments in the constituent functions. If you wish to specify that an argument is to passed to a specific constitutent function then you can the name the argument with the form function.argument, where 'function' is replaced with the name of the relevant constituent function and 'argument' is replaced with the name of the argument

mcCores

An integer scalar giving the number of cores to distribute the chains between. A value of NA sets the number of cores to be equal to the number available on the system

runTimeGlobal

A named list of objects to pass to each of the instances of NIMBLE at run time

initCode

Either a language object or a list of language objects containing code to run at the beginning of each NIMBLE instance

exitCode

Either a language object of a list of language objects containing code to run on completion of each NIMBLE instance

Value

A list containing the following named elements:

modelObject

An uncompiled model object as returned by the function nimbleModel using the parameters supplied

mcmcObject

An uncompiled MCMC object as returned by the function buildMCMC using the parameters supplied

...

The output of the completed MCMC as returned by the function runMCMC using the parameters supplied

Author(s)

Joseph D. Chipperfield, joechip90@googlemail.com

See Also

configureMCMC, runMCMC, nimbleModel, compileNimble, buildMCMC, nimbleCode

Examples

## Not run: 
# Set some parameters for a linear regression
slopeParam <- 0.6
interceptParam <- 4.0
varParam <- 2.0
# A set of arguments to control the MCMC
thin <- 1
thin2 <- 2
niter <- 500
nburnin <- 100
nchains <- 2
# Initialise some fake data
xtest <- 1:100
ytest <- rnorm(length(xtest), interceptParam + slopeParam * xtest, sqrt(varParam))
# NIMBLE code to run the linear regression
testCode <- nimble::nimbleCode({
 slopeParam ~ dnorm(0.0, 0.001)
 interceptParam ~ dnorm(0.0, 0.001)
 varParam ~ dgamma(0.001, 0.001)
 precParam <- 1.0 / varParam
 for(iter in 1:length(xtest)) {
   meanVals[iter] <- interceptParam + slopeParam * xtest[iter]
   ytest[iter] ~ dnorm(meanVals[iter], precParam)
 }
})
# Run the model using the parameters, data, and code defined above
outputMCMC <- mcmcNIMBLERun(code = testCode, data = list(ytest = ytest),
  constants = list(xtest = xtest), WAIC = TRUE, summary = TRUE,
  inits = list(slopeParam = 0.0, interceptParam = 0.0, varParam = 10.0),
  monitors = c("slopeParam", "interceptParam"), monitors2 = "varParam",
  niter = niter, thin = thin, thin2 = thin2, nburnin = nburnin,
  nchains = nchains)$mcmcOutput
# Plot the results of the linear regression
plot(xtest, ytest)
abline(
  a = outputMCMC$summary$all.chains["interceptParam", "Mean"],
  b = outputMCMC$summary$all.chains["slopeParam", "Mean"]
)

## End(Not run) # TODO: Fix this example


joechip90/PaGAn documentation built on April 17, 2025, 4:05 p.m.