I have a bit experience on Bayesian analysis and am a JAGS user. Recently I have to run a more complicated model that contains integration (I use adaptIntegrate from cubature package in the original codes) for a part of the calculation under Bayesian framework. From what I understand, in JAGS/BUGS we cannot use user-defined function or function from other R packages in the model. In the earlier post , some people suggested using LaplacesDemon and nimble as they allow more flexibility on customizing functions. But from the user manuals, LaplacesDemon and nimble still seem to have limit ability to include integration in iterations.

I was wondering what package do people usually use when they have to include integration in Bayesian models? Hope my question sounds clear.


1 Answer 1


SAS allows direct access to an adaptive quadrature integration routine from within PROC MCMC via CALL QUAD (if I recall correctly from SAS/STAT version 14.1 or 14.2 onwards) and there are, I believe, presentations online that show an example. Alternatively, you can hand-code some quadrature algorithm (it is really not so bad, if the software allows you to do it). One option is one with a fixed number of intervals, e.g. a composite Simpson's rule, if you can analytically derive some limits on the quality of the approximation (you may have to find a maximum of the fourth derivative over the integration range). Another option is an adaptive quadrature one, which should be fine with most MCMC samplers. An exception may be that this may not work with MCMC samplers that rely on automatically generating derivatives of the posterior (e.g. Stan's NUTS - perhaps I am wrong about that though), while with most other samplers should be fine. Some of these algorithms may require you to remove any singularities from your integral before you apply them, while some automatic integration routines may even do that for you.

If a package has an ordinary differential equation solver, then you can try to use that to get solutions to definite integrals. E.g. Stan has two different ODE solvers and I have used them to do numerical integration. I assume some other MCMC software may have an ODE solver, too.

  • $\begingroup$ Thanks very much, @Björn ! I haven't tried Stan and thought it's similar to JAGS in the function-wise but just more efficient when computing. It seems that Stan should be the next package for me to try. $\endgroup$
    – CYH
    Commented Mar 1, 2017 at 15:30
  • $\begingroup$ I can certainly recommend Stan. It certainly allows extreme flexibility (plus interfaces with e.g. Python or R via the rstan package) in terms of user defined functions, likelihoods, priors (without any restriction to pre-defined or conjugate/conditionally conjugate distributions), and so on. Additionally, people have developed packages to do standard analyses that are driven by Stan in the background (e.g. the brms, RStanArm or prophet R packages) that give you the computational efficiency of Stan without having to learn the Stan language (probably not so helpful in your particular case). $\endgroup$
    – Björn
    Commented Mar 1, 2017 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.