Learn R Programming

nimble (version 0.6-6)

buildMCMC: Create an MCMC function, from an MCMCconf object

Description

Accepts a single required argument, which may be of class MCMCconf, or inherit from class modelBaseClass (a NIMBLE model object). Returns an MCMC function; see details section.

Usage

buildMCMC(conf, ...)

Arguments

conf

An object of class MCMCconf that specifies the model, samplers, monitors, and thinning intervals for the resulting MCMC function. See configureMCMC for details of creating MCMCconf objects. Alternatively, MCMCconf may a NIMBLE model object, in which case an MCMC function corresponding to the default MCMC configuration for this model is returned.

...

Additional arguments to be passed to configureMCMC if conf is a NIMBLE model object

Calculating WAIC

After the MCMC has been run, calling the calculateWAIC() method of the MCMC object will return the WAIC for the model, calculated using the posterior samples from the MCMC run.

calculateWAIC() has a single arugment:

burnIn: The number of iterations to subtract from the beginning of the posterior samples of the MCMC object for WAIC calculation. Defaults to 0.

The calculateWAIC method calculates the WAIC of the model that the MCMC was performed on. The WAIC (Watanabe, 2010) is calculated from Equations 5, 12, and 13 in Gelman (2014). Note that the set of all parameters monitored by the mcmc object will be treated as \(theta\) for the purposes of e.g. Equation 5 from Gelman (2014). All parameters downstream of the monitored parameters that are necessary to calculate \(p(y|theta)\) will be simulated from the posterior samples of \(theta\).

Details

Calling buildMCMC(conf) will produce an uncompiled (R) R mcmc function object, say 'Rmcmc'.

The uncompiled MCMC function will have arguments:

niter: The number of iterations to run the MCMC.

reset: Boolean specifying whether to reset the internal MCMC sampling algorithms to their initial state (in terms of self-adapting tuning parameters), and begin recording posterior sample chains anew. Specifying reset=FALSE allows the MCMC algorithm to continue running from where it left off, appending additional posterior samples to the already existing sample chains. Generally, reset=FALSE should only be used when the MCMC has already been run (default = TRUE).

simulateAll: Boolean specifying whether to simulate into all stochastic nodes. This will overwrite the current values in all stochastic nodes (default = FALSE).

time: Boolean specifying whether to record runtimes of the individual internal MCMC samplers. When time=TRUE, a vector of runtimes (measured in seconds) can be extracted from the MCMC using the method mcmc$getTimes() (default = FALSE).

progressBar: Boolean specifying whether to display a progress bar during MCMC execution (default = TRUE). The progress bar can be permanently disabled by setting the system option nimbleOptions(MCMCprogressBar = FALSE).

Samples corresponding to the monitors and monitors2 from the MCMCconf are stored into the interval variables mvSamples and mvSamples2, respectively. These may be accessed and converted into R matrix objects via: as.matrix(mcmc$mvSamples) as.matrix(mcmc$mvSamples2)

The uncompiled (R) MCMC function may be compiled to a compiled MCMC object, taking care to compile in the same project as the R model object, using: Cmcmc <- compileNimble(Rmcmc, project=Rmodel)

The compiled function will function identically to the uncompiled object, except acting on the compiled model object.

Examples

Run this code
# NOT RUN {
code <- nimbleCode({
    mu ~ dnorm(0, 1)
    x ~ dnorm(mu, 1)
})
Rmodel <- nimbleModel(code)
conf <- configureMCMC(Rmodel)
Rmcmc <- buildMCMC(conf)
Cmodel <- compileNimble(Rmodel)
Cmcmc <- compileNimble(Rmcmc, project=Rmodel)
Cmcmc$run(10000)
samples <- as.matrix(Cmcmc$mvSamples)
head(samples)
WAIC <- Cmcmc$calculateWAIC(burnIn = 1000)
# }

Run the code above in your browser using DataLab