nimble (version 0.7.0)

MCMCsuite: Executes multiple MCMC algorithms and organizes results.

Description

Creates, runs, and organizes output from a suite of MCMC algorithms, all applied to the same model, data, and initial values. This can include WinBUGS, OpenBUGS, JAGS and Stan MCMCs, as well as NIMBLE MCMC algorithms. Trace plots and density plots for the MCMC samples may also be generated and saved.

Usage

MCMCsuite(code, constants = list(), data = list(), inits = list(),
  monitors = character(), niter = 10000, burnin = 2000, thin = 1,
  summaryStats = c("mean", "median", "sd", "CI95_low", "CI95_upp"),
  calculateEfficiency = FALSE, MCMCs = "nimble", MCMCdefs = list(),
  winbugs_directory = "C:/WinBUGS14", winbugs_program = "WinBUGS",
  openbugs_directory = "C:/OpenBUGS323", openbugs_program = "OpenBUGS",
  stan_model = "", stan_inits = NULL, stan_data = NULL,
  stanNameMaps = list(), makePlot = TRUE, savePlot = TRUE,
  plotName = "MCMCsuite", setSeed = TRUE,
  check = getNimbleOption("checkModel"), debug = FALSE)

Arguments

code

The quoted code expression representing the model, such as the return value from a call to nimbleCode). No default value, this is a required argument.

constants

A named list giving values of constants for the model. This is the same as the constants argument which would be passed to nimbleModel. Default value is list().

data

A named list giving the data values for the model. This is the same as the data argument which would be passed to nimbleModel or model$setData. Default value is list().

inits

A named list giving the initial values for the model. This is the same as the inits argument which would be passed to nimbleModel or model$setInits. Default value is list().

monitors

A character vector giving the node names or variable names to monitor. The samples corresponding to these nodes will be stored in the output samples, will have summary statistics calculated, and density and trace plots generated. Default value is all top-level stochastic nodes of the model.

niter

Number of MCMC iterations to run. This applies to all MCMC algorithms in the suite. Default value is 10,000.

burnin

Number of initial, post-thinning, MCMC iterations to discard. Default value is 2,000.

thin

Thinning interval for the MCMC samples. This applies to all MCMC algorithms in the suite. The thinning occurs prior to the burnin samples being discarded. Default value is 1.

summaryStats

A character vector, specifying the summary statistics to calculate on the MCMC samples. Each element may be the character name of an exisiting R function (possibly user-defined) which acts on a numeric vector and returns a scalar (e.g., mean or sd, or a character string which when parsed and evaluted will define such a function (e.g., function(x) mean(sqrt(x))). Default value is c('mean', 'median', 'sd', 'CI95_low', 'CI95_upp'), where the final two elements are functions which calculate the limits of a 95 percent Bayesian credible interval.

calculateEfficiency

A logical, specifying whether to calculate the efficiency for each MCMC algorithm. Efficiency is defined as the effective sample size (ESS) of each model parameter divided by the algorithm runtime (in seconds). Default is FALSE.

MCMCs

A character vector specifying the MCMC algorithms to run. 'winbugs' specifies WinBUGS; 'openbugs' specifies OpenBUGS; 'jags' specifies JAGS; 'stan' specifies Stan; in this case, must also provide the 'stan_model' argument; 'nimble' specifies NIMBLE's default MCMC algorithm; 'nimble_noConj' specifies NIMBLE's default MCMC algorithm without the use of any conjugate Gibbs sampling; 'nimble_RW' specifies NIMBLE MCMC algorithm using only random walk Metropolis-Hastings ('RW') samplers; 'nimble_slice' specifies NIMBLE MCMC algorithm using only slice ('slice') samplers; 'autoBlock' specifies NIMBLE MCMC algorithm with block sampling of dynamically determined parameter groups attempting to maximize sampling efficiency; Anything else will be interpreted as NIMBLE MCMC algorithms, and must have associated entries in the MCMCdefs argument. Default value is 'nimble', which specifies NIMBLE's default MCMC algorithm.

MCMCdefs

A named list of MCMC definitions. The names of list elements should corespond to any custom MCMC algorithms specified in the MCMCs argument. The list elements should be quoted expressions, enclosed in braces. When executed, the internal code must return an MCMC configuration object, specifying the corresponding MCMC algorithm; in particular, setting the appropriate samplers. The code may assume existance of the R model object Rmodel, and must *return* the MCMC configuration object. Therefore, the final line of such a code block would frequently be a standalone MCMCconf, to return this object.

winbugs_directory

A character string giving the directory of the executable WinBUGS program for the WinBUGS MCMC. This argument will be passed directly to the bugs(...) call, from the R2WinBUGS library. Default value is 'C:/WinBUGS14'.

winbugs_program

A character string giving the name of the WinBUGS program, for the WinBUGS MCMC. This argument will be passed directly to the bugs(...) call, from the R2WinBUGS library. Default value is 'WinBUGS'.

openbugs_directory

A character string giving the directory of the executable OpenBUGS program for the OpenBUGS MCMC. This argument will be passed directly to the bugs(...) call, from the R2WinBUGS library. Default value is 'C:/OpenBUGS323'.

openbugs_program

A character string giving the name of the OpenBUGS program, for the OpenBUGS MCMC. This argument will be passed directly to the bugs(...) call, from the R2WinBUGS library. Default value is 'OpenBUGS'.

stan_model

A character string specifying the location and name of the model file ('modelName.stan') for use with the Stan MCMC program. This argument must include the '.stan' extension, and must be provided whenever the MCMCs argument includes 'stan'.

stan_inits

A character string specifying the location and name of the inits file ('modelName.init.R') for use with the Stan MCMC program. This argument must include the '.init.R' extension, and must be provided whenever the MCMCs argument includes 'stan'. If omitted, it will attempt to locate an inits file in the same directory as the Stan model file.

stan_data

A character string specifying the location and name of the data file (in the form 'modelName.data.R') for use with the Stan MCMC program. This argument must include the '.data.R' extension, and must be provided whenever the MCMCs argument includes 'stan'. If omitted, it will attempt to locate a data file in the same directory as the Stan model file.

stanNameMaps

A list specifying name mappings between Stan and WinBUGS/OpenBUGS. The syntax for list elements is list(BUGS_PARAM_NAME = list(StanSourceName = 'STAN_PARAM_NAME', transform = function(x) TRANSFORMATION_FUNCTION(x))). The transformation is optional.

makePlot

Logical argument, specifying whether to generate the trace plots and posterior density plots, for each monitored node. Default value is TRUE.

savePlot

Logical argument, specifying whether to save the trace plots and density plots. Plots will be saved into the current working directory. Only used when makePlot == TRUE. Default value is TRUE.

plotName

Character string, giving the file name for saving the trace plots and density plots. Only used when makePlot == TRUE and savePlot == TRUE. Default value is 'MCMCsuite'.

setSeed

Logical argument, specifying whether to set.seed(0) prior to MCMC sampling. Default value is TRUE.

check

Logical argument, specifying whether to check the model object for missing or invalid values. Default is given by the NIMBLE option 'checkModel', see help on nimbleOptions for details.

debug

Logical argument, specifying whether to enter a browser() at the onset of executing each MCMC algrithm. For use in debugging individual MCMC algorithms, if necessary. Default value is FALSE.

...

For internal use only

Value

Returns a named list containing elements: samples: A 3-dimensional array containing samples from each MCMC algorithm. summary: A 3-dimensional array containing summary statistics for each variable and algorithm. timing: A numeric vector containing timing information. efficiency: Minimum and mean sampling efficiencies for each algorithm (only provided if option calculateEfficiency = TRUE). See the NIMBLE User Manual for more information about the organization of the return object.

Details

Creates and runs an MCMC Suite. By default, this will execute the specified MCMCs, record all samples, generate summary statistics, and create and save trace plots and posterior density plots. This default behavior can ben altered via a variety of arguments. Following execution of the MCMC algorithms, returns a named list containing samples, summary, and timing elements. See the NIMBLE User Manual for more information about the organization of the return object.

Examples

Run this code
# NOT RUN {
code <- nimbleCode({
    mu ~ dnorm(0, 1)
    x ~ dnorm(mu, 1)
})
output <- MCMCsuite(code,
                    data = list(x=3),
                    inits = list(mu=0),
                    niter = 10000,
                    monitors = 'mu',
                    MCMCs = c('nimble', 'nimble_RW'),
                    summaryStats = c('mean', 'sd', 'max', 'function(x) max(abs(x))'),
                    makePlot = FALSE)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab