runMCMC | R Documentation |
Takes as input an MCMC algorithm (ideally a compiled one for speed) and runs the MCMC with one or more chains, any returns any combination of posterior samples, posterior summary statistics, and a WAIC value.
runMCMC(
mcmc,
niter = 10000,
nburnin = 0,
thin,
thin2,
nchains = 1,
inits,
setSeed = FALSE,
progressBar = getNimbleOption("MCMCprogressBar"),
samples = TRUE,
samplesAsCodaMCMC = FALSE,
summary = FALSE,
WAIC = FALSE,
perChainWAIC = FALSE
)
mcmc |
A NIMBLE MCMC algorithm. See details. |
niter |
Number of iterations to run each MCMC chain. Default value is 10000. |
nburnin |
Number of initial, pre-thinning, MCMC iterations to discard. Default value is 0. |
thin |
Thinning interval for collecting MCMC samples, corresponding to |
thin2 |
Thinning interval for collecting MCMC samples, corresponding to the second, optional set of |
nchains |
Number of MCMC chains to run. Default value is 1. |
inits |
Optional argument to specify initial values for each chain. See details. |
setSeed |
Logical or numeric argument. If a single numeric value is provided, R's random number seed will be set to this value at the onset of each MCMC chain. If a numeric vector of length |
progressBar |
Logical argument. If |
samples |
Logical argument. If |
samplesAsCodaMCMC |
Logical argument. If |
summary |
Logical argument. When |
WAIC |
Logical argument. When |
perChainWAIC |
Logical argument. When |
At least one of samples
, summary
or WAIC
must be TRUE
, since otherwise, nothing will be returned. Any combination of these may be TRUE
, including possibly all three, in which case posterior samples and summary statistics are returned for each MCMC chain, and an overall WAIC value is calculated and returned.
When samples = TRUE
, the form of the posterior samples is determined by the samplesAsCodaMCMC
argument, as either matrices of posterior samples, or coda
mcmc
and mcmc.list
objects.
Posterior summary statistics are returned individually for each chain, and also as calculated from all chains combined (when nchains > 1
).
If provided, the inits
argument can be one of three things:
(1) a function to generate initial values, which will be executed to generate initial values at the beginning of each MCMC chain, or
(2) a single named list of initial values which, will be used for each chain, or
(3) a list of length nchains
, each element being a named list of initial values which be used for one MCMC chain.
The inits
argument may also be omitted, in which case the current values in the model
object will be used as the initial values of the first chain, and subsequent chains will begin using starting values where the previous chain ended.
Other aspects of the MCMC algorithm, such as the specific sampler assignments, must be specified in advance using the MCMC configuration object (created using configureMCMC
), which is then used to build an MCMC algorithm (using buildMCMC
) argument.
The niter
argument specifies the number of pre-thinning MCMC iterations, and the nburnin
argument specifies the number of pre-thinning MCMC samples to discard. After discarding these burn-in samples, thinning of the remaining samples will take place. The total number of posterior samples returned will be floor((niter-nburnin)/thin).
The MCMC option mcmc$run(..., reset = FALSE)
, used to continue execution of an MCMC chain, is not available through runMCMC()
.
A list is returned with named elements depending on the arguments passed to nimbleMCMC
, unless this list contains only a single element, in which case only that element is returned. These elements may include samples
, summary
, and WAIC
, and when the MCMC is monitoring a second set of nodes using monitors2
, also samples2
. When nchains = 1
, posterior samples are returned as a single matrix, and summary statistics as a single matrix. When nchains > 1
, posterior samples are returned as a list of matrices, one matrix for each chain, and summary statistics are returned as a list containing nchains+1
matrices: one matrix corresponding to each chain, and the final element providing a summary of all chains, combined. If samplesAsCodaMCMC
is TRUE
, then posterior samples are provided as coda
mcmc
and mcmc.list
objects. When WAIC
is TRUE
, a WAIC summary object is returned.
Daniel Turek
configureMCMC
buildMCMC
nimbleMCMC
## Not run:
code <- nimbleCode({
mu ~ dnorm(0, sd = 1000)
sigma ~ dunif(0, 1000)
for(i in 1:10) {
x[i] ~ dnorm(mu, sd = sigma)
}
})
Rmodel <- nimbleModel(code)
Rmodel$setData(list(x = c(2, 5, 3, 4, 1, 0, 1, 3, 5, 3)))
Rmcmc <- buildMCMC(Rmodel)
Cmodel <- compileNimble(Rmodel)
Cmcmc <- compileNimble(Rmcmc, project = Rmodel)
inits <- function() list(mu = rnorm(1,0,1), sigma = runif(1,0,10))
samplesList <- runMCMC(Cmcmc, niter = 10000, nchains = 3, inits = inits)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.