MCMC, Particle Filtering, and Programmable Hierarchical Modeling

A system for writing hierarchical statistical models largely compatible with 'BUGS' and 'JAGS', writing nimbleFunctions to operate models and do basic R-style math, and compiling both models and nimbleFunctions via custom-generated C++. 'NIMBLE' includes default methods for MCMC, particle filtering, Monte Carlo Expectation Maximization, and some other tools. The nimbleFunction system makes it easy to do things like implement new MCMC samplers from R, customize the assignment of samplers to different parts of a model from R, and compile the new samplers automatically via C++ alongside the samplers 'NIMBLE' provides. 'NIMBLE' extends the 'BUGS'/'JAGS' language by making it extensible: New distributions and functions can be added, including as calls to external compiled code. Although most people think of MCMC as the main goal of the 'BUGS'/'JAGS' language for writing models, one can use 'NIMBLE' for writing arbitrary other kinds of model-generic algorithms as well. A full User Manual is available at <>.

One can specify the path to where the Eigen header files are located and also whether to link common C++ files into every DSO/DLL we create or whether to treat the files as an extra library with:

R CMD INSTALL --configure-args='--with-eigen=/home/duncan/local --enable-lib' nimble

or, within R

install.packages("nimble", configure.args = c("--with-eigen=/home/duncan/local", "--enable-lib=true"), repos = NULL)


Typically, you need the R developer tools (i.e., compiler, make, etc.) to use nimble. Accordingly, it is quite straightforward to install the package from source as you will have the necessary tools already installed. These are available from the Rtools page on CRAN.

To install the package from source, from within R,

install.packages("nimble", type = "source", INSTALL_opts = "--merge-multiarch")

or from a local copy of the source package,

install.packages("nimble_0.6-2.tar.gz", repos = NULL, INSTALL_opts = "--merge-multiarch")

Alternatively, use the shell command (in the DOS Command prompt)

R CMD INSTALL --merge-multiarch nimble_0.6-2.tar.gz

Of course, you can also compile directly from a clone of the git repository:

R CMD INSTALL --merge-multiarch nimble

The --merge-multiarch is necessary when using a version of R that supports both 32 and 64 bit. This option to installation will ensure that create both 32 and 64 bit installations.

R CMD build nimble
R CMD INSTALL --build --merge-multiarch nimble_0.6-2.tar.gz

We need to create the .tar.gz file first, hence the first command.


                       CHANGES IN VERSION 0.6-3 (December 2016)


-- Minor bug fixes: sd() and var() were not working in BUGS code, fix to MCEM code


-- Removed use of compileNimble in rankSample to avoid Solaris issues

                       CHANGES IN VERSION 0.6-2 (November 2016)


-- Added ability to add conjugate (Gibbs) samplers to particular nodes in an MCMC configuration object using: conf$addSampler(nodeName, 'conjugate').

-- dmulti(), rmulti(), dcat() and rcat() now allow 'probs' argument with arbitrary non-negative values and internally normalize these so they sum to one, to be consistent with R's multinom behavior.

-- Added optional (turned on by default) checking of nimbleFunction run code to warn users if they are calling functions (in particular, R functions) that are not part of the NIMBLE DSL.

-- Added getBound() functionality that provides dynamic access to lower and upper bounds on a node (from R or in the DSL), based on the underlying distribution and any user-defined truncation. Functionality is analogous to getParam(). Now used in the reflection sampler and the MCEM algorithm.

-- cleaned up and added functionality to get information about a distribution based on its name or to get distributional information about model nodes or variables. This functionality is expected to be useful in querying nodes in nimbleFunction setup code when writing algorithms. See help on getDistributionInfo for getting information based on distribution name and help on modelBaseClass for getting distribution information about nodes and variables.

-- user-defined distributions can be used in BUGS code without registration, which will be done automatically behind the scenes. Parameters and dimensions are determined automatically from the nimbleFunction density function. However, in cases where users want to provide alternative parameterizations, the range of the distribution, or indication the distribution is discrete, registerDistributions should be used in its full functionality.


-- Various bug fixes including: cases where compileNimble would not return a correct interface for a nimbleFunction used in multiple particular ways; access to one nimbleFunction's member data from another.

                       CHANGES IN VERSION 0.6-1 (October 2016)


-- Changes to to address Solaris installation issues and removal of some lingering std::cout calls.

-- Rearranged some parts of package and compileNimble compilation. no longer includes redundant compilation involving classes used only at the compileNimble stage. A sessions-specific DLL is created the first time compileNimble is used. It contains R function registration and a finalizer management system and links to CppCode/libnimble.[a|dylib|dll].

-- Added a layer to manage finalizers registered with R for nimble C++ objects. This allows nimble to manually finalize objects and then safely handle R's calls to finalizers.

-- Added an internal clearCompiled system to manually finalize C++ objects and safely manage any external pointers to them in R. This is not yet safe on Windows (can cause crash later upon exiting R) and is intended for developer use at this stage.

-- Fixed bug in making an internal function accessible to Nimble generated code.

-- Fixed bug in use of nimSwitch in getParam.

           CHANGES IN VERSION 0.6-1


-- Added getBound() functionality that provides dynamic access to lower and upper bounds on a node (from R or in the DSL), based on the underlying distribution and any user-defined truncation. Functionality is analogous to getParam(). Now used in the reflection sampler.

                       CHANGES IN VERSION 0.6 (August 2016)


-- Building models is faster and uses less memory for the building steps themselves.

-- The new model implementation uses less memory and seems to have faster compiled execution in many cases.

-- Compiling nimbleFunctions is generally faster.

-- compileNimble can handle model operation functions that use a vector of node names with a scalar index, e.g., model$calculate(nodes[i]). Index ranges are not supported.

-- The computation time spent in each sampler of an MCMC can be obtained by mcmc$run(niter, time = TRUE) and then mcmc$getTimes().

-- New RW_multinomial sampler incorporated into MCMC engine, for sampling nodes following a dmulti() distribution.

-- Progress bar added to MCMC.

-- New function runMCMC() available for easily running multiple MCMC chains.

-- model checking modified so that only size/dimension and NA checking is done by default. Extended checking of ability to calculate all model nodes is now invoked via model$check() only at user request.

-- MCMC and modelValues configuration syntax now uses 'conf' rather than 'spec' in its naming throughout NIMBLE.


-- New C++ nimbleGraph system for some steps of processing graphs.

-- New design of models, specifically nodeFunctions. This is a major implementation change.

-- New C++ memory allocation system using new double[] instead of vector. This is notably more efficient since it can avoid unnecessary initialization upon allocation of local variables in otherwise fast, heavily used nimbleFunctions.

-- New installation system for Windows and for binary builds on OS X. For non-Linux we now by default build a static library, libnimble.a, that is linked into compiled code for models or nimbleFunctions.

-- We now export many fewer functions, though some non-user facing functions are still exported.

-- Test suite now works in Windows.

-- Reduced Windows compiler warnings.

-- Various modifications to packaging to align with CRAN rules.

                       CHANGES IN VERSION 0.5-1 (May 2016)


-- Sequential Monte Carlo algorithms added: Bootstrap filter, Auxiliary particle filter, Ensemble Kalman filter, and Liu and West filter can be accessed by calls to buildBootstrapFilter(), buildAuxiliaryFilter, buildEnsembleKF(), and buildLiuWestFilter() respectively. buildPF() is no longer available, but that name will be re-used in a future version as a wrapper to some of the filter functions listed above.

-- Particle MCMC (Particle Marginal Metropolis Hastings) samplers can be used as samplers within an MCMC. 'RW_PF' can be used to sample scalar parameters, and 'RW_PF_block' can be used for multivariate parameters or multiple parameters.

-- An ascent-based version of the MCEM algorithm has been added to buildMCEM(), providing automated convergence assessment and stopping.

-- A block sampler that uses user-specified likelihoods has been implemented and can be used as a sampler within an MCMC, by using 'RW_llFunction_block'.

-- Added functions numeric(), integer(), matrix(), and array() to the NIMBLE DSL, for creating non-scalar variables in nimbleFunctions. These are intended to replace usage of declare(), with declare() being deprecated but still currently functional. setSize() is still available to change the size of an existing object prior to assigning into specific elements, but another call to one of the new functions can also be used for that purpose.

-- Added multivariate-t distribution for use in BUGS code and the DSL.

-- Added binary sampler for Gibbs sampling of discrete 0/1 nodes. This is the default sampler used for Bernoulli and Binomial(p, size=1) nodes.

-- New options for Metropolis-Hastings RW sampler: log=TRUE to sample on a log scale, and reflective=TRUE to reflect normal proposal distribution to stay within the range of the target distribution.

-- Improved printing in printSamplers() and getSamplers() methods of MCMC configuration objects.

-- Some steps of building and compiling models and algorithms are generally faster.

-- forwardsolve(), backsolve() and solve() now work more flexibly than before and can be used in BUGS code.

-- A variety of small bug fixes.


-- Testing has been added for SMC algorithms.

                       CHANGES IN VERSION 0.5 (March 2016)


-- Added support for solve(), forwardsolve(), and backsolve() to the NIMBLE DSL, with the same functionality as in R.

-- Improved performance of conjugate multivariate-normal - multivariate-normal sampling.

-- API changes for MCMCspec objects: new method printSamplers() prints the current samplers (replacing the old getSamplers() method); new functionality for getSamplers(), which now returns a list of samplerSpec objects; new overloaded functionality of setSamplers(), which can accept a list of samplerSpec objects, replacing the current set of samplers; new method getSamplerDefinition(), which returns the nimbleFunction definition of a sampler.

-- API created for modifying samplerSpec objects: setName(), setSamplerFunction(), setTarget(), setControl().

-- MCMC conjugate sampling efficiency improved by ~33%.

-- new system for automating the comparison of different MCMCs and generating html results pages: the main function is compareMCMCs.

-- enhanced checking of model to check for size and dimension mismatch in BUGS code.

-- some steps of building models and determining dependencies (e.g. getDependencies method for model classes) are faster and use less memory.

-- models have a new method, getDependenciesList, that provides a list of neighbor relationships in the model graph.

-- There is a new function, getParam(model, node, parameter), that returns the value of a parameter of a stochastic node. It works in R and in the NIMBLE DSL (compilable run code).

-- Models now have member functions for calculate, simulate, getLogProb, calculateDiff, and getParam. model$calculate(nodes) is equivalent to calculate(model, nodes), etc.

-- The NIMBLE DSL now allows drop=FALSE as the last argument for indices. e.g. x[1,,drop=FALSE]. This mimics R.

-- added ability to use any of the methods for R's optim function in MCEM optimization step.

-- There are more flexible ways to provide arguments to declare and setSize.

-- The random walk block sampler is a bit more efficient.

-- There is better error trapping during some compilation steps.


-- various modifications to conform to CRAN rules


-- Compilation of BUGS function inprod is fixed

-- some cases of handling multivariate variables of length one (e.g. a 1x1 matrix) have been fixed.

-- added warning when 'size' in multinomial is not sum of values

-- added missing terms (involving only the data values) in 'ddirchmulti' in test-user.R and in manual

                    CHANGES IN VERSION 0.4-1 (Oct. 3, 2015)


-- added support for OpenBUGS to MCMCsuite.

-- added additional option for MCMCsuite: 'calculateEfficiency' to calculate ESS and ESS/time.

-- added elliptical slice sampler 'ess' to MCMC engine.

-- added new MCMC option for MCMCsuite that omits conjugate samplers: 'nimble_noConj'


-- fixed bug preventing use of nimbleFunctions in packages depending on NIMBLE.

-- fixed bug preventing use of nimStop in R version of nimbleFunctions.

-- reduced generation of C++ compiler warnings on Windows during compileNimble.

                    CHANGES IN VERSION 0.4 (Aug. 2, 2015)


-- almost everything is faster in almost all cases (building models, configuring MCMCs, compiling models and nimbleFunctions), sometimes very much faster, especially during R processing.

-- added DSL functions stop("Error message") and checkInterrupt() to check for a user interrupt (via R_checkUserInterrupt() in C++).

-- added support for scalar and vector character strings in the compiler. The ony meaningful use currently is as argument(s) to print or stop.

-- added calculateDiff as a fourth fundamental method (after calculate, simulate, getLogProb) in model node functions.

-- nearly all calls to DSL functions or other nimbleFunctions handle R-style named or ordered arguments.

-- dots (".") are allowed in nimbleFunction argument names.

-- most Windows compiler warnings about comparing unsigned and signed ints should be gone. Harmless warnings remain and some objects possibly being used before initialization.

-- more informative error messages added in many cases.

-- nimble version of some common R functions is now consistently prefixed with "nim" instead of "nimble". E.g. nimRound, nimStop, nimPrint, nimCopy. These can still used without the prefix in the DSL, e.g. round, stop, print, copy.

-- added nimbleOptions useMultiInterfaceForNestedNimbleFunctions (default TRUE) and (experimental) clearNimbleFunctionsAfterCompiling (default FALSE) to reduce memory use. See User Manual section 9.6.

-- setSize now consistently works in both compiled and uncompiled uses as setSize(X, size1, size2, etc.), not setSize(X, c(size1, size2, etc.)).

-- added support for user-defined functions in BUGS code.

-- added support for use of user-defined distributions in BUGS code, with the distribution density, simulate, distribution and quantile functions coded as nimbleFunctions and registered via registerDistributions().

-- added support for truncation (T(,) and I(,) syntax in BUGS) plus dinterval distribution for censoring, both following the JAGS functionality.

-- added ability to impose constraints via the dconstraint syntax; these constraints act like data and therefore only take effect a posteriori and are not imposed when simulating from the model.

-- added alternative distribution names (aliases) usable in BUGS code (via an extensible system for adding additional aliases as desired): dbinom for dbin, dmultinom for dmulti, dnbinom for dnegbin, dchisqr for dchisq, dweibull for dweib, ddirich for ddirch, dwishart for dwish.

-- added additional parameterizations for dlnorm and dt.

-- improved handling of distribution functions in nimbleFunction run code (i.e., the DSL), allowing argument matching by name and use of default values. Distribution functions can also be used as deterministic functions (e.g., pnorm to do probit calculations) in BUGS code.

-- added an optional check when building model that alerts user to presence of nodes without values and log probability calculations that return NA.

-- model$checkConjugacy(nodes) now accepts a character vector argument of node/variable names, and returns a named list identifying conjugate relationships.

-- incorporated automated blocking into MCMC engine: configureMCMC(model, autoBLock=TRUE) returns an MCMCspec defined by automated blocking procedure, and buildMCMC(model, autoBlock=TRUE) returns the corresponding MCMC algorithm.

-- new syntax for MCMCspec$addSampler(type, target, control). mandatory 'type' argument specifies sampler type, which may be a character string or nimbleFunction object. mandatory 'target' argument specifices a character vector of target model nodes. optional 'control' list argument overrides default sampler control parameters.

-- default control parameters for MCMC sampling algorithms are now a NIMBLE system level option: MCMCcontrolDefaultList.

-- MCMCspec$getSamplers(ind), MCMCspec$setSamplers(ind), and MCMCspec$removeSamplers(ind) can optionally also accept a character vector argument, and will act on all samplers which sample the nodes specified.

-- cleaned up formatting of output when querying and setting MCMC samplers.

-- NIMBLE will no longer "hang" when used in RStudio.

-- reordered argument names to nimbleModel to put in more logical order of importance. first four arguments are: code, constants, data, inits.

-- added documentation of NIMBLE's built-in MCMC samplers via help(samplers).

-- added more informative error messages for common errors encountered when calling nimbleModel() and compileNimble().


-- re-wrote model definition processing and R nodeFunction instantiation to do more work at the level of a BUGS declaration rather than for each individual node.

-- re-wrote checkConjugacy to use the new model definition content.

-- re-wrote C++ nimCopy system and corresponding R partial evaluation to do more processing in R and set up C++ for faster processing.

-- added test-copy for nimCopy, values() and values()<- .

-- added nimbleProjectClass method for adding groups of nimbleFunctions from same generator at once.

-- modified core samplers like RW to use the new calculateDiff nodeFunction method.

-- added CmultiNimbleFunctionInterface to interface compiled nimbleFunctions contained within other nimbleFunctions.

-- removed some old code deemed to be fully defunct.

-- added 'range' variable to all distributions; when truncation is specified this modifies the range and also sets the 'truncated' flag to TRUE; range is not at the moment used but could be used by developers in algorithms.

-- added a variety of error checking for various distribution functions and now generally use doubles in distribution functions in C++.

-- modified API for nimble options to mimic R's options(), including nimbleOptions() and getOptions() and renaming of the underlying nimble options object to be .nimbleOptions.

-- added an environment, nimbleUserNamespace, in the package namespace, that allows user-supplied information to control the behavior of NIMBLE. At the moment, only used for user-supplied distributions list.

-- testing of user-defined distributions and functions in BUGS code (tests/test-user.R).

-- full suite of testing for truncation/censoring/constraints added (tests/test-trunc.R).

-- names of custom MCMC sampler nimbleFunctions need not begin with "sampler_".

-- added initializeModel() nimbleFunction for use in algorithms; it performs sensible model initialization at the onset of an algorithm.


-- setSize fixed.

-- generation of "1/(scale)" type bits in keyword processing for exp and gamma fixed.

-- fixed bug where quitting R after dyn.unload()ing in OS X / Linux caused a segfault. Now we rely on a finalizer that uses the base class virtual destructor, which is in the package dll, not the generated one(s). We still have segfaults on Windows after dyn.unload()ing.

-- made row subset assignment work in the compiler, e.g. x[i,] <- foo(a).

-- updated package to use new igraph 1.x.x API and now explicitly link in LAPACK_LIBS and BLAS_LIBS when building as dpotrf not being found when using igraph 1.x.x.

-- fixed bug where dexp was passing the wrong parameter (rate instead of scale) to C++; we now use our own dexp_nimble, which calls Rmath's dexp correctly.

-- set default for deparse to width.cutoff=500L to avoid splitting of lines when going from expressions to string names.

                    CHANGES IN VERSION 0.3-1 (Mar. 7, 2015)


-- fully allow data to be provided as part of constants argument in nimbleModel for compatibility with JAGS and BUGS, which mix data and constants

-- added Dirichlet-multinomial conjugacy handling

-- Added 'oldSpec' argument to configureMCMC. Allows rebuilding of MCMCs without the need to recompile code if there are no new types of samplers.

-- Now allow for up to 4D arrays

-- Added handling of raising a vector of values to a scalar power in nimble functions (i.e., the NIMBLE DSL)


-- fixed naming issue in checking posterior summaries against known values in test_mcmc


-- fixed bug that allowed inits to overwrite data values in variables that are mixtures of data and parameters. Also now detect elements of initial values that are not variables in the model.

-- fixed bug in naming of nodes in as.matrix() when there is a variable with a single indexed node (e.g. x[1], but no other nodes in the variable 'x')

-- fixed bug regarding logProbs with gaps (e.g., if logProb_x[1] and logProb_x[3] are defined but not logProb_x[2])

-- fixed bug in handling of unary minus for non-scalars

        CHANGES IN VERSION 0.3 (Dec. 31, 2014)


-- IMPORTANT SYNTAX CHANGE: nimbleFunctions are run via myNimbleFunction$run() instead of myNimbleFunction(). This means code written in v0.2 and earlier will not run without adding $run.

-- IMPORTANT SYNTAX CHANGES: --- writing code for nimble models is now done with nimbleCode (previously modelCode) --- to customize MCMC use myMCMCspec <- configureMCMC(myModel) (previously MCMCspec) --- to build an MCMC algorithm, either myGenericMCMC <- buildMCMC(myModel) for a generic build or myCustomizedMCMC <- buildMCMC(myMCMCspec) for a customized MCMC algorithm can be used (previously one always had to build an MCMCspec, even for generic build)

-- Variables and methods of a nimbleFunction can be accessed by myNimbleFunction$myVariable or myNimbleFunction$myMethod() rather than nfVar(myNimbleFunction, ‘myVariable’) or nfMethod(myNimbleFunction, ‘myMethod’)(). Similarly, names of objects/methods of a nimble function can be queried by ls(myNimbleFunction)

-- faster compiling, most notably when compiling MCMC algorithms


-- Addition of graphIDs for logProbs for models (separate set than graphIDs for nodes) and graphIDs for modelValue variables

-- Addition of $expandNodeNames() for modelValues

-- $expandNodeNames() now evals nodeNames text array to quickly retrieve graphIDs. expandNodeNames can also accept graphIDs and return names. Also can return multivariate nodes as either ‘x[1:10]’ or ‘x[1]’,’x[2]’, etc.

-- Addition of numberedObjects (both at R and C++ level) currently used for fast construction of C++ nodeFunctionVectors, modelVariableAccessors and modelValuesAccessors

-- Reduction of nimbleFunctions required for initialization functions of MCMCs

-- nimCopy now only requires that the number of nodes are equal, not that the sets of consecutive nodes be equal (i.e. previous nimCopy(…, nodes = c(‘x[1]’, ‘x[2]’), nodesTo = c(‘y[1,1]’, ‘y[2,2]’)) was not allowed).

-- Addition of keywordProcessing system for more organized compiling of nimbleFunctions

Bug fixes

-- Fixed bug regarding calculating log probabilities for multinomial distribution with probabilities equal to 0

                 CHANGES IN VERSION 0.2 (Oct. 12, 2014)

-- Internal changes to decrease time to build models and nimbleFunctions.

-- Multivariate conjugate updaters are now included in the default MCMC implementation, as well as block updating on scalar and/or multivariate nodes.

-- Vector treatment of and is.nan, so, nodes)) will work, equivalently to any( in R.

-- More extensive suite of tests.

-- Extension of node maps, which can be accessed via graphIDs, to increase speed of determining node types, checking if nodes in model, etc.

-- model$getNodeNames, expandNodeNames and getDependencies all return the full node function name, i.e. if 'x[1:2]' is multivariate, will return 'x[1:2]' rather than 'x[1]', 'x[2]', unless returnScalarComponents = TRUE

-- Added class of nodeVectors, which save the graphIDs of a set of nodes. Use of these is minimal at the moment.


-- Fixed issue with specifying multivariate nodes as rows/columns of matrices

-- Fixed bug in the conjugacy system

-- Fixed bug in compiled version of adaptive block updater

-- Fixed issue with building the NIMBLE package for Windows, as well as cleaning up causes of compiler warnings.

-- Fixed issue that values(model, nodes) sorted the outcome. Remaining bug to be fixed: nimCopy(nodesFrom, nodesTo) will NOT work if nodesFrom and nodesTo have different number/different lengths of ordered contingent blocks of memory, i.e. nimCopy(nodesFrom = c('x[1]', 'x[2]'), nodesTo = c('y[2]', 'y[1]')) will fail because nodesFrom is one block but nodesTo is two blocks

-- Fixed issue that chol(A+B) was being translated to chol(A) + B

                 CHANGES IN VERSION 0.1-1 (Aug. 28, 2014)


-- handling of spaces in installed directory name on Windows

-- ensured that various Makevars and and files are provided in the package for proper installation on all platforms

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.6-6 by Christopher Paciorek, 2 months ago

Browse source code at

Authors: Perry de Valpine, Christopher Paciorek, Daniel Turek, Cliff Anderson- Bergman, Nick Michaud, Fritz Obermeyer, Duncan Temple Lang

Documentation:   PDF Manual  

Task views: Bayesian Inference

BSD_3_clause + file LICENSE license

Imports methods, igraph, coda

Suggests testthat, R2WinBUGS, rjags, rstan, xtable, abind, ggplot2

System requirements: GNU make

See at CRAN