View source: R/mcmcInternals.R
mcmcNIMBLERun | R Documentation |
mcmcNIMBLERun
provides a function to encapsulate an entire
run of NIMBLE (including compilation steps) using a set of user-specified
parameters
mcmcNIMBLERun(
...,
mcCores = 1,
runTimeGlobal = list(),
initCode = list(),
exitCode = list()
)
... |
Named arguments to be passed to the constituent NIMBLE functions
|
mcCores |
An integer scalar giving the number of cores to distribute the
chains between. A value of |
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 |
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
Joseph D. Chipperfield, joechip90@googlemail.com
configureMCMC
, runMCMC
,
nimbleModel
, compileNimble
,
buildMCMC
, nimbleCode
## 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.