<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>NIMBLE</title>
<link>https://r-nimble.org/blog/</link>
<atom:link href="https://r-nimble.org/blog/index.xml" rel="self" type="application/rss+xml"/>
<description>An R package for programming with BUGS models and compiling parts of R</description>
<generator>quarto-1.7.31</generator>
<lastBuildDate>Wed, 18 Feb 2026 08:00:00 GMT</lastBuildDate>
<item>
  <title>Version 1.4.1 of NIMBLE released</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/version-1-4-1-of-nimble-released.html</link>
  <description><![CDATA[ 




<p>We’ve released the newest version of NIMBLE on CRAN and on our <a href="https://r-nimble.org/">website</a>. NIMBLE is a system for building and sharing analysis methods for statistical models, especially for hierarchical models and computationally-intensive methods (such as MCMC, Laplace approximation, and SMC).</p>
<p>Version 1.4.1 is a micro release that primarily addresses some minor issues, but it also includes:</p>
<ul>
<li>An MCMC sampler for sampling elements of partially observed multivariate normal random variables, e.g., if one has some elements of a data vector that are missing.</li>
<li>The ability to change the MCMC thinning interval when continuing an MCMC.</li>
</ul>
<p>Please see the <a href="https://r-nimble.org/release-notes.html">release notes</a> on our website for more details.</p>



 ]]></description>
  <category>release</category>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/version-1-4-1-of-nimble-released.html</guid>
  <pubDate>Wed, 18 Feb 2026 08:00:00 GMT</pubDate>
</item>
<item>
  <title>Using nimbleQuad for maximum likelihood estimation and deterministic posterior approximation</title>
  <dc:creator>Paul van Dam-Bates and Christopher Paciorek</dc:creator>
  <link>https://r-nimble.org/blog/nimbleQuad.html</link>
  <description><![CDATA[ 




<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>The new <code>nimbleQuad</code> package provides quadrature-based inference methods that use Laplace and Adaptive Gauss-Hermite Quadrature (AGHQ) approximation. It has two main components:</p>
<ol type="1">
<li>Laplace and AGHQ approximations for marginalizing over latent nodes (e.g., random effects) to perform approximate maximum likelihood estimation.</li>
<li>Deterministic nested quadrature methods for posterior approximation using Laplace approximation for the inner marginalization of the latent nodes and general quadrature methods for outer marginalization of the parameter nodes. These methods borrow ideas from the popular INLA approach <span class="citation" data-cites="rue2009approximate">(Rue, Martino, and Chopin 2009)</span> and from the extended Gaussian latent model approach provided in the R <code>aghq</code> package <span class="citation" data-cites="stringer2023fast">(Stringer, Brown, and Stafford 2023)</span>.</li>
</ol>
<p>The first component was present in the core <code>nimble</code> package until version 1.4.0. The second component is a new feature.</p>
<p>By providing these methods in the NIMBLE system, via <code>nimbleQuad</code>, users can use the methods on models constructed using the NIMBLE model language (which extends the BUGS model language) and can also customize and extend the algorithms.</p>
<p>In this document we will introduce some of the workflow and show the flexibility available when using <code>buildNestedApprox</code> for posterior approximations.</p>
<p>Additional details about nimbleQuad can be found in <a href="https://r-nimble.org/manual/cha-laplace.html">the NIMBLE User Manual</a> and <a href="https://r-nimble.org/vignettes/nimbleQuad.html">the nimbleQuad vignette</a>.</p>
<section id="what-is-nimble" class="level3">
<h3 class="anchored" data-anchor-id="what-is-nimble">What is NIMBLE?</h3>
<p><a href="https://r-nimble.org">NIMBLE</a> is a system for writing hierarchical statistical models and algorithms. It is distributed as the R package, <a href="https://CRAN.R-project.org/package=nimble">nimble</a>. NIMBLE includes:</p>
<ol type="1">
<li><p>A dialect of the BUGS model language that is extensible. NIMBLE uses almost the same model code as WinBUGS, OpenBUGS, and JAGS. Being “extensible” means that it is possible to write new functions and distributions and use them in your models.</p></li>
<li><p>An algorithm library including Markov chain Monte Carlo (MCMC) and other methods.</p></li>
<li><p>A compiler that generates C++ for each model and algorithm, compiles the C++, and lets you use it from R. You don’t need to know anything about C++ to use nimble.</p></li>
</ol>
</section>
<section id="illustrative-example" class="level3">
<h3 class="anchored" data-anchor-id="illustrative-example">Illustrative Example</h3>
<p>For this vignette we will use the salamander example from the R package <code>glmmTMB</code>. Example usage of this model can be seen by running <code>?glmmTMB</code>. The data are repeated counts, <img src="https://latex.codecogs.com/png.latex?y">, of salamanders at multiple sites, which are zero-inflated. Some of the sites are mined, and some are not (<img src="https://latex.codecogs.com/png.latex?x">). Based on <code>glmmTMB</code>, we can analyze these data as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%20%20y_i%7Cz_i%20%5Csim%20%5Ctext%7BPoisson%7D(z_i%5Clambda_i)%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%20%20z_i%20%5Csim%20%5Ctext%7BBernoulli%7D(1-p_i)%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%20%20%5Ctext%7Blog%7D(%5Clambda_i)%20=%20%5Cbeta%20x_i%20+%20u_%7Bsite_i%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Blogit%7D(p_i)%20=%20%5Calpha_0%20x_i%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0Au_%7Bj%7D%20%5Csim%20%5Ctext%7BNormal%7D(0,%20%5Csigma%5E2)%0A"></p>
<p>The maximum likelihood estimator can be found with <code>glmmTMB</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(glmmTMB)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(Salamanders)</span>
<span id="cb1-3">fit_tmb <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(count <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> spp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> mined <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>site), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zi=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family=</span>poisson, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>Salamanders)</span></code></pre></div>
</div>
<p>We can also fit this model in <code>INLA</code> to get the approximate posterior using the “zeroinflatedpoisson1” model family.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(INLA)</span>
<span id="cb2-2">fit_inla <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inla</span>( count <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> spp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> mined <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(site, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iid"</span>), </span>
<span id="cb2-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quantiles =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">025</span>,.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>,.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>,.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">975</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zeroinflatedpoisson1"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>Salamanders,</span>
<span id="cb2-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control.inla =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">int.strategy =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ccd"</span>))</span></code></pre></div>
</div>
</section>
</section>
<section id="building-a-glmm-in-nimble" class="level2">
<h2 class="anchored" data-anchor-id="building-a-glmm-in-nimble">Building a GLMM in NIMBLE</h2>
<p>To write this model in NIMBLE we will first write a zero-inflated Poisson distribution that marginalizes over the indirectly observed discrete value <img src="https://latex.codecogs.com/png.latex?z">. When <img src="https://latex.codecogs.com/png.latex?y=0">, <img src="https://latex.codecogs.com/png.latex?z"> is a discrete latent variable. In order to use the methods in <code>nimbleQuad</code>, all the latent variables in this model must be continuous.</p>
<p><code>nimbleQuad</code> relies on automatic differentiation (AD) to take derivatives. To include AD in a <code>nimbleFunction</code> we must set <code>buildDerivs = 'run'</code>.</p>
<p>A nimbleFunction, <code>rZIP</code>, to simulate from the distribution is also provided for convenience, although this is not strictly needed for this model to fit.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(nimbleQuad)</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">dZIP <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nimbleFunction</span>(</span>
<span id="cb4-2"> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">run =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">double</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">double</span>(),</span>
<span id="cb4-3">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zeroProb =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">double</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logical</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">default =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) {</span>
<span id="cb4-4">   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">returnType</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">double</span>())</span>
<span id="cb4-5">   <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## For use with AD, we cannot use an `if` statement to handle the mixture.</span></span>
<span id="cb4-6">   prob <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> zeroProb <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbinom</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> zeroProb) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dpois</span>(x, lambda)</span>
<span id="cb4-7">   <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (log) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(prob))</span>
<span id="cb4-8">     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(prob)</span>
<span id="cb4-9">   },</span>
<span id="cb4-10">   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">buildDerivs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'run'</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Needed when used with AD-based algorithms.</span></span>
<span id="cb4-11">)</span>
<span id="cb4-12"></span>
<span id="cb4-13">rZIP <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nimbleFunction</span>(</span>
<span id="cb4-14"> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">run =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integer</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">double</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zeroProb =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">double</span>()) {</span>
<span id="cb4-15">   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">returnType</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">double</span>())</span>
<span id="cb4-16">   isStructuralZero <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbinom</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> zeroProb, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-17">   <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (isStructuralZero) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-18">   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rpois</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, lambda))</span>
<span id="cb4-19">})</span></code></pre></div>
</div>
<p>We will use prior distributions to match the default priors in the <code>INLA</code> model that was fit above to allow direct comparison of results. See <code>inla.priors.used(fit_inla)</code> for a description of the default priors. INLA places a logit-normal prior on <img src="https://latex.codecogs.com/png.latex?p">. For convenience, we will write our own logit-normal prior. In this case, we explicitly register the distribution to tell NIMBLE the range of possible values, needed for use in NIMBLE’s parameter transformation system.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">dlogitnormal <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nimbleFunction</span>(</span>
<span id="cb5-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">run =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">double</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">double</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">double</span>(), </span>
<span id="cb5-3">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logical</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">default =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)){</span>
<span id="cb5-4">    ans <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dnorm</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logit</span>(x), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> mean, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> sd, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>x)</span>
<span id="cb5-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">returnType</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">double</span>())</span>
<span id="cb5-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(log) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(ans)</span>
<span id="cb5-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(ans))</span>
<span id="cb5-8">  },</span>
<span id="cb5-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">buildDerivs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'run'</span></span>
<span id="cb5-10">)</span>
<span id="cb5-11"></span>
<span id="cb5-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">registerDistributions</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb5-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dlogitnormal =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">BUGSdist =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dlogitnormal(mean, sd)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">range =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb5-14">))</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>  [Warning] Random generation function for dlogitnormal is not available. NIMBLE is generating a placeholder function, rlogitnormal, that will invoke an error if an algorithm needs to simulate from this distribution. Some algorithms (such as random-walk Metropolis MCMC sampling) will work without the ability to simulate from the distribution.  If simulation is needed, provide a nimbleFunction (with no setup code) to do it.</code></pre>
</div>
</div>
<p>If not trying to use the same model as INLA, one might choose different priors, and one would not necessarily need to define the logit-normal distribution.</p>
<p>Following INLA, we use a completely flat prior on the intercept <code>dflat</code>, a gamma prior on precision <code>tau_re</code> and a logit-normal on <code>p</code>. We can now write the model code, and then provide constants and data to build the model.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">code <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nimbleCode</span>({</span>
<span id="cb7-2">  beta[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dflat</span>()</span>
<span id="cb7-3">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>( i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>np ) </span>
<span id="cb7-4">    beta[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>) </span>
<span id="cb7-5">  tau_re <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dgamma</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5e-5</span>)</span>
<span id="cb7-6">  p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dlogitnormal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>))</span>
<span id="cb7-7">  </span>
<span id="cb7-8">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>nsites) </span>
<span id="cb7-9">    re[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> tau_re)</span>
<span id="cb7-10"></span>
<span id="cb7-11">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>( i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>nobs ){</span>
<span id="cb7-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(lam[i]) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(beta[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>np]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>X[i,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>np]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> re[site[i]]</span>
<span id="cb7-13">    count[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dZIP</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> lam[i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zeroProb =</span> p)</span>
<span id="cb7-14">  }</span>
<span id="cb7-15">})</span>
<span id="cb7-16"></span>
<span id="cb7-17">nimconst <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>()</span>
<span id="cb7-18">nimconst<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model.matrix</span>( <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> spp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> mined, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> Salamanders ))</span>
<span id="cb7-19">nimconst<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>np <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(nimconst<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X)</span>
<span id="cb7-20">nimconst<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nobs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(nimconst<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X)</span>
<span id="cb7-21">nimconst<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>site <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Salamanders<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>site))</span>
<span id="cb7-22">nimconst<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nsites <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(nimconst<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>site)</span>
<span id="cb7-23"></span>
<span id="cb7-24">nimdata <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>()</span>
<span id="cb7-25">nimdata<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>count <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Salamanders<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>count</span>
<span id="cb7-26"></span>
<span id="cb7-27">inits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">29</span>,</span>
<span id="cb7-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau_re =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb7-29">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">re =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(nimconst<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nsites,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>),</span>
<span id="cb7-30">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(nimconst<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>np))</span>
<span id="cb7-31"></span>
<span id="cb7-32">m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nimbleModel</span>(code, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> nimdata, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">constants =</span> nimconst, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inits =</span> inits, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">buildDerivs =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Defining model</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>  [Note] Registering 'dZIP' as a distribution based on its use in BUGS code. If you make changes to the nimbleFunctions for the distribution, you must call 'deregisterDistributions' before using the distribution in BUGS code for those changes to take effect.</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Building model</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Setting data and initial values</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Running calculate on model
  [Note] Any error reports that follow may simply reflect missing values in model variables.</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Checking model sizes and dimensions</code></pre>
</div>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">cm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compileNimble</span>(m)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Compiling
  [Note] This may take a minute.
  [Note] Use 'showCompilerOutput = TRUE' to see C++ compilation details.</code></pre>
</div>
</div>
<p>Note that since nimbleQuad methods use NIMBLE’s automatic differentiation (AD) system, we set <code>buildDerivs = TRUE</code> when building the nimble model.</p>
</section>
<section id="using-the-laplace-approximation" class="level2">
<h2 class="anchored" data-anchor-id="using-the-laplace-approximation">Using the Laplace Approximation</h2>
<p>We can find the maximum likelihood estimate by using <code>buildLaplace</code> (or <code>buildAGHQ</code> if we want more quadrature nodes for marginalizing over the random effects, as discussed below).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">laplace <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">buildLaplace</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> m)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Building 23 individual Laplace approximations (one dot for each): .......................</code></pre>
</div>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">claplace <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compileNimble</span>(laplace)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Compiling
  [Note] This may take a minute.
  [Note] Use 'showCompilerOutput = TRUE' to see C++ compilation details.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">nimble_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runLaplace</span>(claplace)</span></code></pre></div>
</div>
<p>NIMBLE reports that it is able to use 23 separate (one-dimensional) Laplace approximations (because the random effects, plus their data dependencies, are conditionally independent).</p>
<p>Let’s compare the results with <code>glmmTMB</code>. The results are very similar.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">nimble_params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> nimble_fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>params</span>
<span id="cb21-2">nimble_fixef <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> nimble_params[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"beta"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(nimble_params)),]</span>
<span id="cb21-3"></span>
<span id="cb21-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(nimble_fixef[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"estimate"</span>], <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fixef</span>(fit_tmb)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cond, </span>
<span id="cb21-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NIMBLE Estimate"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"glmmTMB Estimate"</span>)</span>
<span id="cb21-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://r-nimble.org/blog/nimbleQuad_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># logit(p):</span></span>
<span id="cb22-2">fit_tmb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>par[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"betazi"</span>]     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># TMB</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   betazi 
-0.933805 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logit</span>(nimble_params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p"</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NIMBLE</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -0.9338518</code></pre>
</div>
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># tau_re</span></span>
<span id="cb26-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>fit_tmb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>par[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theta"</span>])  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># TMB</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   theta 
3.646564 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">nimble_params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tau_re"</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NIMBLE</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3.646879</code></pre>
</div>
</div>
<p>It can be helpful to check the Laplace approximation at the MLE by increasing the number of quadrature nodes using AGHQ. If the likelihood is very different when increasing the number of quadrature nodes, that indicates that Laplace was not a good approximation to the marginal likelihood. In this particular case, since we have have conditionally independent random effects (and therefore a set of 1-dimensional AGHQ approximations), increasing the number of quadrature nodes will be relatively quick, but in other cases it can be very slow.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1">claplace<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calcLogLik</span>(nimble_fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>params[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'estimate'</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -881.9147</code></pre>
</div>
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1">claplace<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">updateSettings</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nQuad =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb32-2">claplace<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calcLogLik</span>(nimble_fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>params[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'estimate'</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -881.8752</code></pre>
</div>
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1">claplace<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">updateSettings</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nQuad =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb34-2">claplace<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calcLogLik</span>(nimble_fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>params[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'estimate'</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -881.8749</code></pre>
</div>
</div>
</section>
<section id="posterior-approximation" class="level2">
<h2 class="anchored" data-anchor-id="posterior-approximation">Posterior Approximation</h2>
<p>Posterior approximation is done by <code>buildNestedApprox</code> (to build the approximation) and <code>runNestedApprox</code> (to calculate the approximation). Note that the setup for <code>buildNestedApprox</code>is similar to <code>buildLaplace</code> except that we refer to the combination of random effects and fixed effects as <code>latentNodes</code> and the hyperparameters as <code>paramNodes</code>, which we will refer to more generally as the “parameters”. Including fixed effects with the random effects reduces the dimension of the outer quadrature, which reduces computation time. However, in some cases one may want to include the fixed effects in the <code>paramNodes</code>, as the choice of where to include the fixed effects can affect statistical performance. If the user does not specify <code>latentNodes</code> and <code>paramNodes</code>, NIMBLE will automatically determine them based on the graphical structure of the model, and the user can check that a reasonable determination has been made.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1">approx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">buildNestedApprox</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> m, </span>
<span id="cb36-2">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">paramNodes =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tau_re'</span>), </span>
<span id="cb36-3">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">latentNodes =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'beta'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'re'</span>))</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Building nested posterior approximation for the following node sets:
  - parameter nodes: p, tau_re
  - latent nodes: beta (14 elements), re (23 elements)
  with AGHQ grid for the parameters and Laplace approximation for the latent nodes.</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Building Laplace approximation.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1">capprox <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compileNimble</span>(approx, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">project =</span> m)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Compiling
  [Note] This may take a minute.
  [Note] Use 'showCompilerOutput = TRUE' to see C++ compilation details.</code></pre>
</div>
</div>
<p>We then use <code>runNestedApprox</code> to run the basic approximation and apply various methods to the output object to carry out further inference. The steps include:</p>
<ol type="1">
<li>Find posterior mode of the parameters using Laplace (or AGHQ) to marginalize over the latent nodes.</li>
<li>Use an <em>integration-free</em> method analogous to that of INLA to quickly approximate the marginal posterior distribution of each parameter <span class="citation" data-cites="martins2013bayesian">(Martins et al. 2013)</span>.</li>
<li>Calculate the posterior density of the parameters across the parameter quadrature grid (the default grid uses the CCD (central composite design) approach when the number of parameters is greater than two). In addition, a quadrature-based marginal log likelihood is produced based on this computation.</li>
<li>Sample from the joint posterior of the <code>latentNodes</code> using a mixture of multivariate normal distributions with mean, covariance, and weights obtained at each grid point in the parameter quadrature grid.</li>
</ol>
<p>Using the integration-free method is the fastest way to produce marginal posterior density estimates of the parameters. Following the methodology of INLA, this is done by using an asymmetric multivariate normal distribution to approximate the posterior density of the parameters.</p>
<p>If more accuracy is required, quadrature can be used such as AGHQ to marginalize over the other parameters (similar to the R package <code>aghq</code>). Here the user can choose between AGHQ, CCD, sparse AGHQ, or a user-provided quadrature rule, as discussed further below). Marginal density values are computed at a fixed set of points and then a spline is used for computations with the univariate density.</p>
<p>For inference on the latent nodes, at each point in the parameter grid, Laplace is used to marginalize the latent nodes to calculate the posterior density for the point, which requires an inner optimization. This can be computationally expensive (particularly with a large number of latent nodes), scaling with the number of grid points. CCD is the default (as is done in INLA) because it scales well to higher dimensions. Alternatively, an AGHQ grid can be used. This will be computationally more expensive because there are more grid points but potentially more accurate. The user can also provide their own grid. By default, we do not do compute the parameter grid when calling <code>runNestedApprox</code> unless requested, because the integration-free estimates do not require it. However, it is necessary for inference on the latent nodes and is done automatically when the latent nodes are sampled (via <code>sampleLatents</code>).</p>
<p>Here is the initial (integration-free) inference for the parameters. We compare results to samples from a long HMC (MCMC) run.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1">result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runNestedApprox</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">approx =</span> capprox)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Finding posterior mode for parameter(s).</code></pre>
</div>
<div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1">result</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Model (hyper)parameters: 
            mean         sd      2.5%       25%       50%      75%     97.5%
p      0.2829532 0.03213737 0.2223163 0.2607501 0.2822413 0.304266 0.3480409
tau_re 4.2832265 2.21980867 1.4690074 2.7377845 3.7993353 5.280151 9.9046828

Marginal log-likelihood (asymmetric Gaussian approximation): -957.392(*)
  (*) Invalid for improper priors and may not be useful for non-informative priors.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb45-2"></span>
<span id="cb45-3">result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plotMarginal</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tau_re"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb45-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(hmc_tau_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inla.dmarginal</span>(hmc_tau_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, fit_inla<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>marginals.hyperpar[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb45-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(hmc_tau_re, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'green'</span>)</span>
<span id="cb45-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NIMBLE</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">nested approx"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INLA"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HMC"</span>),</span>
<span id="cb45-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'black'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'green'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bty =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'n'</span>)</span>
<span id="cb45-8"></span>
<span id="cb45-9">result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plotMarginal</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb45-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(hmc_p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inla.dmarginal</span>(hmc_p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, fit_inla<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>marginals.hyperpar[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb45-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(hmc_p, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'green'</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://r-nimble.org/blog/nimbleQuad_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<p>The nested approximation and INLA results match MCMC well, though for some reason the densities plotted by INLA don’t quite integrate to one and therefore lies above the other two lines.</p>
<p>Here is the sampling-based inference for the latent nodes. The density estimates match HMC reasonably well, but not quite as well as INLA for the fixed effects.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove these lines later</span></span>
<span id="cb46-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># hmc_beta1 &lt;- density(samples_hmc[ , 'beta[1]'])</span></span>
<span id="cb46-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># hmc_beta2 &lt;- density(samples_hmc[ , 'beta[2]'])</span></span>
<span id="cb46-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># hmc_re1 &lt;- density(samples_hmc[ , 're[1]'])</span></span>
<span id="cb46-5"></span>
<span id="cb46-6">result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sampleLatents</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Calculating inner AGHQ/Laplace approximation at 9 parameter (outer)
  grid points (one dot per point): .........</code></pre>
</div>
<div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb48-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">density</span>(result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>samples[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"beta[1]"</span>]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"beta[1]"</span>)</span>
<span id="cb48-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(hmc_beta1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inla.dmarginal</span>(hmc_beta1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, fit_inla<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>marginals.fixed[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb48-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(hmc_beta1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'green'</span>)</span>
<span id="cb48-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">density</span>(result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>samples[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"beta[2]"</span>]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"beta[2]"</span>)</span>
<span id="cb48-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(hmc_beta2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inla.dmarginal</span>(hmc_beta2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, fit_inla<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>marginals.fixed[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb48-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(hmc_beta2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'green'</span>)</span>
<span id="cb48-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">density</span>(result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>samples[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"re[1]"</span>]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"re[1]"</span>)</span>
<span id="cb48-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(hmc_re1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inla.dmarginal</span>(hmc_re1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, fit_inla<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>marginals.random[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb48-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(hmc_re1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'green'</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://r-nimble.org/blog/nimbleQuad_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<section id="improving-the-parameter-marginals" class="level3">
<h3 class="anchored" data-anchor-id="improving-the-parameter-marginals">Improving the Parameter Marginals</h3>
<p>Although the integration-free method is fast and performs reasonably well, it may be that the user wishes to get a more exact marginal. This is done individually for each parameter of interest, marginalizing over the remaining parameters using an outer quadrature rule (AGHQ, CCD, AGHQSPARSE, USER), with AGHQ as the default. We choose the number of quadrature points for marginalization (<code>nQuad</code>) as well as the number of points along a grid to evaluate the marginal (<code>nMarginalGrid</code>). Increasing either can improve performance but at the expense of additional computation and generally with diminishing improvement.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1">result  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Original (integration-free) parameter estimates</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Model (hyper)parameters: 
            mean         sd      2.5%       25%       50%      75%     97.5%
p      0.2829532 0.03213737 0.2223163 0.2607501 0.2822413 0.304266 0.3480409
tau_re 4.2832265 2.21980867 1.4690074 2.7377845 3.7993353 5.280151 9.9046828

Marginal log-likelihood (asymmetric Gaussian approximation): -957.392(*)
Marginal log-likelihood (grid-based): -957.3845(*)
  (*) Invalid for improper priors and may not be useful for non-informative priors.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb51" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb51-1">result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">improveParamMarginals</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nodes =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tau_re'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p'</span>),</span>
<span id="cb51-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nMarginalGrid =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nQuad =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quadRule =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AGHQ"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Approximating 2 individual parameter marginal densities via AGHQ:</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>  - calculating inner AGHQ/Laplace approximation at (9) marginal points
    with 3 quadrature grid points (one dot per grid point): (1)...(2)...(3)...(4)...(5)...(6)...(7)...(8)...(9)...
  - calculating inner AGHQ/Laplace approximation at (9) marginal points
    with 3 quadrature grid points (one dot per grid point): (1)...(2)...(3)...(4)...(5)...(6)...(7)...(8)...(9)...</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Model (hyper)parameters: 
            mean         sd     2.5%       25%       50%       75%     97.5%
p      0.2829161 0.03233042 0.220414 0.2608876 0.2826332 0.3046184  0.347118
tau_re 4.3240215 2.34440791 1.450420 2.7365618 3.8040399 5.3028558 10.249151

Marginal log-likelihood (asymmetric Gaussian approximation): -957.392(*)
Marginal log-likelihood (grid-based): -957.3845(*)
  (*) Invalid for improper priors and may not be useful for non-informative priors.</code></pre>
</div>
</div>
<p>Comparing to the quantiles from the integration-free approach, the differences are generally minor, but there is a noticeable difference in the right tail for <code>tau_re</code>.</p>
</section>
<section id="customizing-the-quadrature-grid" class="level3">
<h3 class="anchored" data-anchor-id="customizing-the-quadrature-grid">Customizing the Quadrature Grid</h3>
<p>For the salamanders example, we have two parameters, for which <code>nimbleQuad</code> defaults to using AGHQ as the parameter grid. The grid points are skewed based on the skew from the asymmetric multivariate normal approximation, and then scaled using spectral decomposition (or Cholesky decomposition).</p>
<p>For more than two parameters, <code>nimbleQuad</code> defaults to using a CCD grid, also used by INLA.</p>
<p>The user may also provide their own parameter grid. This must be written as a <code>nimbleFunction</code> with some specific components and is described in <a href="https://r-nimble.org/vignettes/nimbleQuad.html#customizing-the-quadrature-grid">the nimbleQuad vignette</a>.</p>
</section>
<section id="sampling-from-the-posterior-over-parameters" class="level3">
<h3 class="anchored" data-anchor-id="sampling-from-the-posterior-over-parameters">Sampling from the posterior over parameters</h3>
<p>The user may also choose to sample from the asymmetric multivariate normal approximation to make inference about the parameters. This can be particularly useful when making inference on parameters with dimension changes (e.g., proportions with a Dirichlet prior or elements of a covariance matrix, such as one with a Wishart or LKJ prior) or generally making inference about functionals of multiple parameters.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb55" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1">samples_params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sampleParams</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>)</span>
<span id="cb55-2">result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plotMarginal</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tau_re"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb55-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">density</span>(samples_params[,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'black'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb55-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(hmc_tau_re, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'green'</span>)</span>
<span id="cb55-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nested approx"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nested approx, samples"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HMC"</span>),</span>
<span id="cb55-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'black'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'black'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'green'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bty =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'n'</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://r-nimble.org/blog/nimbleQuad_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="model-selection-and-the-marginal-likelihood" class="level3">
<h3 class="anchored" data-anchor-id="model-selection-and-the-marginal-likelihood">Model selection and the marginal likelihood</h3>
<p>With proper priors, one can calculate <img src="https://latex.codecogs.com/png.latex?p(y)=%5Cint%20p(y%7C%5Ctheta)p(%5Ctheta)d%5Ctheta">, the marginal likelihood, which is useful for model selection. This is reported with the nested approximation results, initially using an asymmetric multivariate normal approximation to the joint posterior of the parameters. An an improved estimate based on quadrature over the parameter grid (by default using AGHQ) can be obtained using the <code>calcMarginalLogLikImproved</code> method of the nested approximation object.</p>
<p>However, much caution is warranted. The quantity is ill-defined when the prior is improper. Furthermore, even with proper priors, if the prior is diffuse, the integral will generally average over values of the parameters that give very poor fits to the data, which can produce misleading results for model selection.</p>



</section>
</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-martins2013bayesian" class="csl-entry">
Martins, Thiago G, Daniel Simpson, Finn Lindgren, and Håvard Rue. 2013. <span>“Bayesian Computing with INLA: New Features.”</span> <em>Computational Statistics &amp; Data Analysis</em> 67: 68–83.
</div>
<div id="ref-rue2009approximate" class="csl-entry">
Rue, Håvard, Sara Martino, and Nicolas Chopin. 2009. <span>“Approximate Bayesian Inference for Latent Gaussian Models by Using Integrated Nested Laplace Approximations.”</span> <em>Journal of the Royal Statistical Society Series B: Statistical Methodology</em> 71 (2): 319–92.
</div>
<div id="ref-stringer2023fast" class="csl-entry">
Stringer, Alex, Patrick Brown, and Jamie Stafford. 2023. <span>“Fast, Scalable Approximations to Posterior Distributions in Extended Latent Gaussian Models.”</span> <em>Journal of Computational and Graphical Statistics</em> 32 (1): 84–98.
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>tutorial</category>
  <guid>https://r-nimble.org/blog/nimbleQuad.html</guid>
  <pubDate>Thu, 29 Jan 2026 08:00:00 GMT</pubDate>
</item>
<item>
  <title>Using derived quantities in NIMBLE’s MCMC system</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/derived-quantities.html</link>
  <description><![CDATA[ 




<p>NIMBLE is a system for building and sharing analysis methods for statistical models, especially for hierarchical models and computationally-intensive methods (such as MCMC, Laplace approximation, and sequential Monte Carlo).</p>
<p>As of version 1.4.0, NIMBLE allows one to specify “derived quantities” to calculate or record additional quantities of interest during an MCMC. Potential derived quantities include:</p>
<ul>
<li>posterior means,</li>
<li>posterior variances,</li>
<li>log-probabilities (log-density values) for single or (summed) groups of nodes,</li>
<li>posterior predictive samples for predictive nodes, and</li>
<li>arbitrary calculations by providing a user-defined nimbleFunction that follows the required specification.</li>
</ul>
<p>By using derived quantities, users can avoid adding additional nodes to the model itself and can potentially reduce how much is stored during an MCMC run and saved as output from the run, thereby reducing memory and disk use.</p>
<p>We’ll give a brief example of saving log-probability (log-posterior-density) values summed over a set of nodes in a simple Poisson random effects model.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(nimble)</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">pumpCode <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nimbleCode</span>({ </span>
<span id="cb2-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N) {</span>
<span id="cb2-3">        theta[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dgamma</span>(alpha, beta)</span>
<span id="cb2-4">        lambda[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> theta[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>t[i]</span>
<span id="cb2-5">        x[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dpois</span>(lambda[i])</span>
<span id="cb2-6">    }</span>
<span id="cb2-7">    alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dexp</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>)</span>
<span id="cb2-8">    beta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dgamma</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>)</span>
<span id="cb2-9">})</span>
<span id="cb2-10"></span>
<span id="cb2-11">pumpConsts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb2-12">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">94.3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15.7</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">62.9</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">126</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.24</span>,</span>
<span id="cb2-13">                         <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.5</span>))</span>
<span id="cb2-14">pumpData <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>))</span>
<span id="cb2-15">pumpInits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb2-16">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, pumpConsts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>N))</span>
<span id="cb2-17">pumpModel <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nimbleModel</span>(pumpCode, pumpConsts, pumpData, pumpInits)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Defining model</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Building model</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Setting data and initial values</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Running calculate on model
  [Note] Any error reports that follow may simply reflect missing values in model variables.</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Checking model sizes and dimensions</code></pre>
</div>
</div>
<p>Now we add the <code>logProb</code> derived quantity, specified directly as an argument to <code>configureMCMC</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">conf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">configureMCMC</span>(pumpModel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">logProb =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'alpha'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'beta'</span>))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>===== Monitors =====
thin = 1: alpha, beta
===== Samplers =====
RW sampler (1)
  - alpha
conjugate sampler (11)
  - beta
  - theta[]  (10 elements)
===== DerivedQ =====
- logProb (1)</code></pre>
</div>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">conf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">printDerivedQuantities</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] derived quantity: logProb,  execution interval: thin,  nodes: c(alpha, beta)</code></pre>
</div>
</div>
<p>When we run the MCMC, we see the derived quantities are saved as an element of the output list, in addition to the usual posterior samples of the parameters.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">mcmc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">buildMCMC</span>(conf)</span>
<span id="cb12-2"></span>
<span id="cb12-3">cPumpModel <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compileNimble</span>(pumpModel)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Compiling
  [Note] This may take a minute.
  [Note] Use 'showCompilerOutput = TRUE' to see C++ compilation details.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">cmcmc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compileNimble</span>(mcmc)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Compiling
  [Note] This may take a minute.
  [Note] Use 'showCompilerOutput = TRUE' to see C++ compilation details.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runMCMC</span>(cmcmc, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">niter =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">thin =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>running chain 1...</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>|-------------|-------------|-------------|-------------|
|-------------------------------------------------------|</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>$samples
         alpha      beta
[1,] 0.7434957 0.8437510
[2,] 0.5046057 0.8152505
[3,] 0.5201513 1.3765454
[4,] 1.1023278 2.2564913
[5,] 1.4316249 2.1598157

$derived
$derived$logProb
          alpha      beta
[1,] -0.7434957 -2.943556
[2,] -0.5046057 -2.884129
[3,] -0.5201513 -3.916877
[4,] -1.1023278 -5.241634
[5,] -1.4316249 -5.105549</code></pre>
</div>
</div>
<p>We could also have added the <code>logProb</code> derived quantity to an existing MCMC configuration argument (and also illustrating that one can modify the interval at which recording is done):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">conf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">configureMCMC</span>(pumpModel)</span>
<span id="cb20-2">conf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">addDerivedQuantity</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'logProb'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nodes =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'alpha'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'beta'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interval =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
</div>
<p>The operation of every derived quantity function is governed by an <code>interval</code> parameter. Each derived quantity function is executed at the end of every <code>interval</code> MCMC iterations (and only for iterations after the burnin period). The default value for <code>interval</code> is given by the <code>thin</code> interval of the MCMC. For example, if <code>interval = 1</code> (regardless of the <code>thin</code> value), then that derived quantity function will execute at the end of every MCMC sampling iteration.</p>
<p>Note that user-defined derived quantity nimbleFunctions provide a general way for a user to intervene at the end of an MCMC iteration and do something. This would often be recording a value (to be returned at the end of the MCMC) or updating some summary statistic, but the derived quantities system doesn’t require this, giving users flexibility.</p>
<p>More details can be found in the <a href="https://r-nimble.org/manual/cha-mcmc.html#sec:derived-quantities">NIMBLE User Manual</a> and via <code>help(derived)</code>.</p>



 ]]></description>
  <category>announcement</category>
  <category>R</category>
  <category>tutorial</category>
  <guid>https://r-nimble.org/blog/derived-quantities.html</guid>
  <pubDate>Tue, 27 Jan 2026 08:00:00 GMT</pubDate>
</item>
<item>
  <title>Version 1.4.0 of NIMBLE released, plus new quadrature-based functionality in nimbleQuad package</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/version-1-4-0-of-nimble-released.html</link>
  <description><![CDATA[ 




<p>We’ve released the newest version of NIMBLE on CRAN and on our <a href="https://r-nimble.org/">website</a>. NIMBLE is a system for building and sharing analysis methods for statistical models, especially for hierarchical models and computationally-intensive methods (such as MCMC, Laplace approximation, and SMC).</p>
<p>Version 1.4.0 provides important new and improved functionality, plus some bug fixes and improved error trapping.</p>
<p>The new and improved functionality includes:</p>
<ul>
<li>A new INLA-like deterministic nested posterior approximation whose methodology borrows from both INLA and the extended latent Gaussian models approach of the <code>aghq</code> package in R. This new approximation and NIMBLE’s existing Laplace and AGHQ approximation now live in a new package, <a href="https://cran.r-project.org/web/packages/nimbleQuad/index.html"><code>nimbleQuad</code></a>, rather than in the core <code>nimble</code> package.</li>
<li>A new system for computing and storing “derived quantities” during MCMC execution, allowing users to record additional quantities of interest at every saved MCMC iteration (i.e., following the thinning interval, or some other user-chosen interval). Derived quantities provided by NIMBLE include means, variances, model log-densities, and predictive nodes. Users can also define their own derived quantities.</li>
<li>Matrix exponential functionality via <code>expm</code> and <code>expAv</code>.</li>
<li>The ability to provide multiple code chunks to <code>nimbleCode</code> for greater flexibility in composing models.</li>
<li>Greatly improved efficiency and memory use of AD system and making efficiency improvements to Laplace/AGHQ approximation.</li>
</ul>
<p>In addition to the new and improved functionality above, other bug fixes, improved error trapping, and enhancements include:</p>
<ul>
<li>Removing some documentation references to “BUGS” when referring to models.</li>
<li>Allowing users to turn off <code>model$checkBasics</code> via a new option.</li>
<li>Better handling inconsistencies between <code>inits</code> and <code>dimensions</code>.</li>
<li>Making minor improvements to the Pólya-gamma sampler.</li>
<li>Generalizing the system of dynamically generating conjugate MCMC samplers, to allow for multivariate parameters of dependent distributions to have distinct sizes from the dependent node itself .</li>
<li>Making MCEM append new samples when increasing sample size using the ascent-based method, rather than starting a new sample.</li>
</ul>
<p>Please see the <a href="https://r-nimble.org/release-notes.html">release notes</a> on our website for more details.</p>



 ]]></description>
  <category>release</category>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/version-1-4-0-of-nimble-released.html</guid>
  <pubDate>Wed, 14 Jan 2026 08:00:00 GMT</pubDate>
</item>
<item>
  <title>announcing the nimbleMacros package and the use of macros in NIMBLE models</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/announcing-the-nimblemacros-package-and-the-use-of-macros-in-nimble-models.html</link>
  <description><![CDATA[ 




<p>Recent versions of NIMBLE now include the ability to use macros in models. NIMBLE is a system for building and sharing analysis methods for statistical models, especially for hierarchical models and computationally-intensive methods (such as MCMC, Laplace approximation, and SMC).</p>
<p>A NIMBLE macro is a succinct syntax that expands to create the NIMBLE model code for part or all of a model.</p>
<p>We recently released the first version of the <a href="https://cran.r-project.org/web/packages/nimbleMacros/index.html"><code>nimbleMacros</code> package on CRAN</a>, which provides an initial set of macros available to users and developers. As an example, one could set up the code for a linear mixed effects model by using the <code>LM</code> (“linear model”) macro like this:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(nimbleMacros)</span>
<span id="cb1-2">code <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nimbleCode</span>({</span>
<span id="cb1-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">LM</span>(weight[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>Chick))</span>
<span id="cb1-4">})</span></code></pre></div>
</div>
<p>with the formula syntax mimicking that of the lme4 package. After building the model based on the <code>code</code> object, you can see the model code produced after the macro is expanded with <code>model$getCode()</code>. The nimbleMacros package also includes macros for creating linear predictors and for loops, and we plan to add additional macros in the future.</p>
<p>Developers can use the tools in <code>nimble</code> itself to create their own macros. See <a href="https://r-nimble.org/html_manual/cha-user-defined.html#sec:user-macros">Section 12.4 of the NIMBLE user manual</a>, <a href="https://cran.r-project.org/web/packages/nimbleMacros/vignettes/nimbleMacros.html">the <code>nimbleMacros</code> vignette</a>, or <code>help(buildMacro)</code> for more information.</p>



 ]]></description>
  <category>announcement</category>
  <category>release</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/announcing-the-nimblemacros-package-and-the-use-of-macros-in-nimble-models.html</guid>
  <pubDate>Wed, 19 Mar 2025 07:00:00 GMT</pubDate>
</item>
<item>
  <title>Version 1.3.0 of NIMBLE released</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/version-1-3-0-of-nimble-released.html</link>
  <description><![CDATA[ 




<p>We’ve released the newest version of NIMBLE on CRAN and on our <a href="https://r-nimble.org/">website</a>. NIMBLE is a system for building and sharing analysis methods for statistical models, especially for hierarchical models and computationally-intensive methods (such as MCMC, Laplace approximation, and SMC).</p>
<p>Version 1.3.0 provides some new and improved functionality, plus some bug fixes and improved error trapping.</p>
<p>The new and improved functionality includes:</p>
<ul>
<li>A new multivariate sampler, the Barker proposal sampler (<code>sampler_barker</code>). We encourage users to try this sampler in place of the block Metropolis <code>RW_block</code> sampler and let us know how well it works. The Barker sampler uses gradient information and may improve adaptation behavior, including<br>
better mixing when parameters are on different scales or the initialproposal scale is too large.</li>
<li>An improved Laplace/AGHQ implementation that includes use of the <code>nlminb</code> optimizer for both inner and outer optimization (for better optimization performance), improved messaging and output naming, returning the log-likelihood and degrees of freedom for model selection calculations, and unified control of optimization method and other controls at either the build stage or through the <code>updateSettings</code> method.</li>
<li>The addition of the BOBYQA optimization method through <code>nimOptim</code>, registered via <code>nimOptimMethod</code>.</li>
</ul>
<p>In addition to the new and improved functionality above, other bug fixes, improved error trapping, and enhancements include:</p>
<ul>
<li>Preventing the use of nimbleFunction method names and nimbleFunction names that conflict with names in the nimble language (DSL).</li>
<li>More carefully checking for and warning of cases of NaN and non-finite log probability values in various samplers that in some cases may indicate invalid MCMC sampling.</li>
<li>More carefully handling of NaN and non-finite log probability values in the CRP sampler.</li>
<li>Error trapping cases of dynamic indices producing a non-scalar result in AD-enabled models and provide a suggested work-around.</li>
<li>Error trapping use of a non-existent nimbleList.</li>
<li>Preventing use of a single seed when running multiple chains via <code>runMCMC</code>.</li>
<li>Improving messaging related to lack of derivative support for functions.</li>
<li>Adding information about model macros to the manual.</li>
<li>Fixing bug in caching values in the CRP sampler when maximum number of clusters is exceeded, which would have caused incorrect sampling (albeit with the user having been warned that they should increase the maximum number of clusters).</li>
<li>Fixing an issue preventing use of nimbleList elements in <code>nimCat</code>.</li>
<li>Preventing an adaptation interval of one for various block samplers for which an interval of one leads to an error.</li>
<li>Allowing <code>runLaplace</code> to use an uncompiled Laplace object.</li>
</ul>
<p>Please see the <a href="https://r-nimble.org/more/news">release notes</a> on our website for more details.</p>



 ]]></description>
  <category>release</category>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/version-1-3-0-of-nimble-released.html</guid>
  <pubDate>Sat, 21 Dec 2024 08:00:00 GMT</pubDate>
</item>
<item>
  <title>Version 1.2.1 of NIMBLE released</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/version-1-2-1-of-nimble-released.html</link>
  <description><![CDATA[ 




<p>We’ve released the newest version of NIMBLE on CRAN and on our <a href="https://r-nimble.org/">website</a>. NIMBLE is a system for building and sharing analysis methods for statistical models, especially for hierarchical models and computationally-intensive methods (such as MCMC, Laplace approximation, and SMC).</p>
<p>This is a micro release that primarily addresses some packaging changes requested by CRAN. In addition, this release includes:</p>
<ul>
<li>A multinomial MCMC sampler, <code>sampler_RW_multinomial</code>, for random variables following a multinomial distribution.</li>
<li>Some enhancements to error trapping and warning messages.</li>
<li>A variety of minor bug fixes.</li>
</ul>



 ]]></description>
  <category>release</category>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/version-1-2-1-of-nimble-released.html</guid>
  <pubDate>Wed, 31 Jul 2024 07:00:00 GMT</pubDate>
</item>
<item>
  <title>Version 1.2.0 of NIMBLE released</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/version-1-2-0-of-nimble-released.html</link>
  <description><![CDATA[ 




<p>We’ve released the newest version of NIMBLE on CRAN and on our <a href="https://r-nimble.org/">website</a>. NIMBLE is a system for building and sharing analysis methods for statistical models, especially for hierarchical models and computationally-intensive methods (such as MCMC, Laplace approximation, and SMC).</p>
<p>This release provides provides extensive new functionality, including:</p>
<ul>
<li>A Pólya-gamma sampler, <code>sampler_polyagamma</code>, for conjugate sampling of linear predictor parameters in logistic regression model specifications, including handling zero inflation and stochastic design matrices. This sampler must be added to an MCMC configuration manually.</li>
<li>A new sampler, <code>sampler_noncentered</code>, which samples the mean or standard deviation of a set of random effect values in a transformed space such that the random effects are deterministically shifted or scaled given new values of their hyperparameters. For random effects written in a centered parameterization, sampling is performed as if they had been written in a noncentered parameterization, thereby enabling a variant on the <a href="https://doi.org/10.1198/jcgs.2011.203main">Yu and Meng (2011)</a> interweaving sampling strategy of sampling in both parameterizations.This sampler must be added to an MCMC configuration manually.</li>
<li>Adaptive Gauss-Hermite quadrature (AGHQ) for integrating over continuous latent effects, as an extension of NIMBLE’s Laplace approximation functionality. We also add user-friendly R functions, <code>runLaplace</code> and <code>runAGHQ</code>, for using Laplace and AGHQ approximation for maximum likelihood estimation.</li>
<li>A more flexible optimization system via <code>nimOptim</code>, with support for <code>nlminb</code> built in as well as the capability for users to provide potentially arbitrary optimization functions in R.</li>
<li>Allowing the use of nimbleFunctions with setup code in models, either for user-defined functions via <code>&lt;-</code> or for user-defined distributions via <code>~</code>. This supports holding large objects outside of model nodes for use in models.</li>
<li>A completely revamped MCEM algorithm, using automatic derivatives in the maximization when possible, fixing a bug so that any parts of the model not connected to the latent states are included in MLE calculations, giving greater control and adding minor extensions to the ascent-based MCEM approach, and converting <code>buildMCEM</code> to be a nimbleFunction rather than an R function.</li>
</ul>
<p>In addition to the new functionality above, other enhancements and bug fixes include:</p>
<ul>
<li>Improving the speed of MCMC and MCMC building in certain cases.</li>
<li>Adding an argument to buildMCMC controlling whether to initialize values in the model.</li>
<li>Providing the ability to control the number of digits printed in C++ output.</li>
<li>Allowing use of a categorical MCMC sampler with user-specified dcat-like distributions.</li>
<li>Warning of use of backward indexing in models.</li>
<li>Improve documentation of the LKJ distribution and of advanced aspects of writing code for derivative tracking using the AD system.</li>
<li>Fixing an insufficient check for conjugacy in stick-breaking specifications of Bayesian nonparametric distributions.</li>
<li>Fixing compilation failures occurring on Red Hat Linux.</li>
<li>Reenabling functionality for user-provided Eigen library and related updates to the autoconf configuration used in package building.</li>
<li>Enhancing functionality to support model macros, which will be fully released and documented in the future.</li>
<li>Removing deprecated <code>is.na.vec</code> and <code>is.nan.vec</code> functions.</li>
<li>Improving some warnings and error messages.</li>
</ul>
<p>Please see the <a href="https://r-nimble.org/more/news">release notes</a> on our website for more details.</p>



 ]]></description>
  <category>release</category>
  <category>announcement</category>
  <guid>https://r-nimble.org/blog/version-1-2-0-of-nimble-released.html</guid>
  <pubDate>Fri, 14 Jun 2024 07:00:00 GMT</pubDate>
</item>
<item>
  <title>Version 1.1.0 of NIMBLE released</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/version-1-1-0-of-nimble-released.html</link>
  <description><![CDATA[ 




<p>We’ve released the newest version of NIMBLE on CRAN and on our <a href="https://r-nimble.org/">website</a>. NIMBLE is a system for building and sharing analysis methods for statistical models, especially for hierarchical models and computationally-intensive methods (such as MCMC,Laplace approximation, and SMC).</p>
<p>This release provides new functionality as well as various bug fixes and improved error trapping, including:</p>
<ul>
<li>Improving our automatic differentiation (AD) system so it can be used in a wider range of models, including models with stochastic indexing, discrete latent states, and CAR distributions. Support for AD for these models means that HMC sampling and Laplace approximation can be used.</li>
<li>Allowing distributions and functions (whether user-defined or built-in) that lack AD support (such as <code>dinterval</code>, <code>dconstraint</code>, and truncated distributions) to be used and compiled in AD-enabled models. The added flexibility increases the range of models in which one can use AD methods (HMC or Laplace) on some parts of a model and other samplers or methods on other parts.</li>
<li>Adding <code>nimIntegrate</code> to the NIMBLE language, providing one-dimensional numerical integration via adaptive quadrature, equivalent to R’s <code>integrate</code>. This can, for example, be used in a user-defined function or distribution for use in model code, such as to implement certain point process or survival models that involve a one-dimensional integral.</li>
<li>Adding a “prior samples” MCMC sampler, which uses an existing set of numerical samples to define the prior distribution of model node(s).</li>
<li>Better support of the dCRP distribution in non-standard model structures.</li>
<li>Adding error trapping to prevent accidental use of C++ keywords as model variable names.</li>
<li>Removing the <code>RW_multinomial</code> MCMC sampler, which was found to generate incorrect posterior results (in cases when a latent state followed a multinomial distribution)</li>
<li>Fixing a bug in conjugacy checking in a case of subsets of multivariate nodes.</li>
<li>Fixing <code>is.na</code> and <code>is.nan</code> to operate in the expected vectorized fashion.</li>
<li>Improving documentation of AD, nimbleHMC, and nimbleSMC in the manual.</li>
<li>Updating Eigen (the C++ linear algebra library used by nimble) to version 3.4.0.</li>
</ul>
<p>Please see the <a href="https://r-nimble.org/more/news">release notes</a> on our website for more details.</p>



 ]]></description>
  <category>release</category>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/version-1-1-0-of-nimble-released.html</guid>
  <pubDate>Sun, 04 Feb 2024 08:00:00 GMT</pubDate>
</item>
<item>
  <title>nimbleHMC version 0.2.0 released, providing improved HMC performance</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/nimblehmc-version-0-2-0-released-providing-improved-hmc-performance.html</link>
  <description><![CDATA[ 




<p>nimbleHMC provides Hamiltonian Monte Carlo samplers for use with NIMBLE, in particular NUTS samplers. NIMBLE’s HMC samplers can be flexibly assigned to a subset of model parameters, allowing users to consider various sampling configurations.</p>
<p>We’ve released version 0.2.0 of nimbleHMC, which includes a new default NUTS sampler inspired by Stan’s implementation of NUTS. It also provides an updated version of our previous NUTS sampler (which is based on the original Hoffman and Gelman paper, and is now called the ‘NUTS_classic’ sampler in NIMBLE) that fixes performance issues in version 0.1.1.</p>



 ]]></description>
  <category>release</category>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/nimblehmc-version-0-2-0-released-providing-improved-hmc-performance.html</guid>
  <pubDate>Wed, 20 Sep 2023 07:00:00 GMT</pubDate>
</item>
<item>
  <title>Version 1.0.1 of NIMBLE released, fixing a bug in version 1.0.0 affecting certain models</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/version-1-0-1-of-nimble-released-fixing-a-bug-in-version-1-0-0-affecting-certain-models.html</link>
  <description><![CDATA[ 




<p>We’ve released the newest version of NIMBLE on CRAN and on our website. NIMBLE is a system for building and sharing analysis methods for statistical models, especially for hierarchical models and computationally-intensive methods (such as MCMC and SMC).</p>
<p>Version 1.0.1 follows shortly after 1.0.0 and fixes an issue and a bug introduced in version 1.0.0 causing data to be set incorrectly in certain models.</p>
<p>Both cases occur only when a variable (e.g., <code>x</code>) contains both stochastic nodes (e.g.&nbsp;<code>x[2] ~ &lt;some distribution&gt;</code>) and <em>either</em> deterministic nodes (e.g.&nbsp;<code>x[3] &lt;- &lt;some calculation&gt;</code>) or right-hand-side-only nodes (e.g.&nbsp;<code>x[4]</code> appears only on the right-hand-side, like an explanatory value).</p>
<p>The issue involves a change of behavior (relative to previous nimble versions) when both setting data values for some nodes and initial values for other nodes within the same variable (that satisfies the previous condition). Data values for right-hand-side-only nodes were replaced by initial values (inits) if both were provided. Version 1.0.1 reverts to previous behavior that data values are not replaced by initial values in that situation.</p>
<p>The bug involves models where (for a variable satisfying the previous condition) not every scalar element within the variable is used as a node and some of the nodes in the variable are data. In that situation, data values may be set incorrectly. This could typically occur in models with autoregressive structure directly on some data nodes (such as may be the case for capture-recapture models involving many individual capture histories within the same variable, indexed by individual and time, with some individuals not present for the entire time series, resulting in unused scalar elements of the variable).</p>
<p>Please see the <a href="https://r-nimble.org/more/news">release notes</a> on our website for more details.</p>



 ]]></description>
  <category>release</category>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/version-1-0-1-of-nimble-released-fixing-a-bug-in-version-1-0-0-affecting-certain-models.html</guid>
  <pubDate>Wed, 21 Jun 2023 07:00:00 GMT</pubDate>
</item>
<item>
  <title>Version 1.0.0 of NIMBLE released, providing automatic differentiation, Laplace approximation, and HMC sampling</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/version-1-0-0-of-nimble-released-providing-automatic-differentiation-laplace-approximation-and-hmc-sampling.html</link>
  <description><![CDATA[ 




<p>We’ve released the newest version of NIMBLE on CRAN and on our <a href="https://r-nimble.org/">website</a>. NIMBLE is a system for building and sharing analysis methods for statistical models, especially for hierarchical models and computationally-intensive methods (such as MCMC and SMC).</p>
<p>Version 1.0.0 provides substantial new functionality. This includes:</p>
<ul>
<li>A Laplace approximation algorithm that allows one to find the MLE for model parameters based on approximating the marginal likelihood in models with continuous random effects/latent process values.</li>
<li>A Hamiltonian Monte Carlo (HMC) MCMC sampler implementing the NUTS algorithm (available in the newly-released <a href="https://cran.r-project.org/web/packages/nimbleHMC/index.html">nimbleHMC</a> package).</li>
<li>Support in NIMBLE’s algorithm programming system to obtain derivatives of functions and arbitrary calculations within models.</li>
<li>A parameter transformation system allowing algorithms to work in unconstrained parameter spaces when model parameters have constrained domains.</li>
</ul>
<p>These are documented via the R help system and <a href="https://r-nimble.org/html_manual/cha-AD.html">a new section at the end of our User Manual</a>. We’re excited for users to try out the new features and let us know of their experiences. In particular, given these major additions to the NIMBLE system, we anticipate the possibility of minor glitches. The best place to reach out for support is still the <a href="https://r-nimble.org/more/issues-and-groups">nimble-users list</a>.</p>
<p>In addition to the new functionality above, other enhancements and bug fixes include:</p>
<ul>
<li>Fixing a bug (previously reported in a nimble-users message) giving incorrect results in NIMBLE’s cross-validation function (<code>runCrossValidate</code>) for all but the ‘predictive’ loss function for NIMBLE versions 0.10.0 – 0.13.2.</li>
<li>Fixing a bug in conjugacy checking causing incorrect identification of conjugate relationships in models with unusual uses of subsets, supersets, and slices of multivariate normal nodes.</li>
<li>Improving control of the <code>addSampler</code> method for MCMC.</li>
<li>Improving the WAIC system in a few small ways.</li>
<li>Enhancing error trapping and warning messages.</li>
</ul>
<p>Please see the <a href="https://github.com/nimble-dev/nimble/blob/devel/packages/nimble/inst/NEWS.md">NEWS file</a> in the package source for more details.</p>



 ]]></description>
  <category>release</category>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/version-1-0-0-of-nimble-released-providing-automatic-differentiation-laplace-approximation-and-hmc-sampling.html</guid>
  <pubDate>Wed, 31 May 2023 07:00:00 GMT</pubDate>
</item>
<item>
  <title>Version 0.13.1 of NIMBLE released</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/version-0-13-1-of-nimble-released.html</link>
  <description><![CDATA[ 




<p>We’ve released the newest version of NIMBLE on CRAN and on our website. This version is purely a bug fix release that fixes <a href="https://r-nimble.org/bug-in-newly-released-version-0-13-0-affecting-mcmc-for-models-with-predictive-nodes">a bug introduced in our new handling of predictive nodes in version 0.13.0</a> (released in November). If you installed version 0.13.0, please upgrade to 0.13.1.</p>



 ]]></description>
  <category>release</category>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/version-0-13-1-of-nimble-released.html</guid>
  <pubDate>Mon, 19 Dec 2022 08:00:00 GMT</pubDate>
</item>
<item>
  <title>Bug in newly-released version 0.13.0 affecting MCMC for models with predictive nodes</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/bug-in-newly-released-version-0-13-0-affecting-mcmc-for-models-with-predictive-nodes.html</link>
  <description><![CDATA[ 




<p>We recently released version 0.13.0, which has some improvements in how we handle predictive nodes in NIMBLE’s MCMC engine.</p>
<p>Unfortunately, we realized (thanks to a user post from a couple days ago) that there is a bug in this new approach to predictive nodes.</p>
<p>If you haven’t upgraded to version 0.13.0, simply wait to upgrade until we release a bug fix in 0.13.1 in the next couple weeks.</p>
<p>If you have upgraded to version 0.13.0 and if you have run an MCMC on a model that both (1) has predictive nodes and (2) has multivariate nodes, then the bug might affect your results. Please set:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nimbleOptions</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">MCMCusePredictiveDependenciesInCalculations =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</div>
<p>and then reconfigure/rebuild and rerun your MCMC. The option above will ensure that the MCMC behaves as it would in previous versions of NIMBLE.</p>



 ]]></description>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/bug-in-newly-released-version-0-13-0-affecting-mcmc-for-models-with-predictive-nodes.html</guid>
  <pubDate>Thu, 08 Dec 2022 08:00:00 GMT</pubDate>
</item>
<item>
  <title>Version 0.13.0 of NIMBLE released</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/version-0-13-0-of-nimble-released.html</link>
  <description><![CDATA[ 




<p>We’ve released the newest version of NIMBLE on CRAN and on our <a href="https://r-nimble.org/">website</a>. NIMBLE is a system for building and sharing analysis methods for statistical models, especially for hierarchical models and computationally-intensive methods (such as MCMC and SMC).</p>
<p>Version 0.13.0 provides new functionality (in particular improved handling of predictive nodes in MCMC) and minor bug fixes, including:</p>
<ul>
<li>Thoroughly revamping handling of posterior predictive nodes in the MCMC system, in particular that MCMC samplers, by default, will now exclude predictive dependencies from internal sampler calculations. This should improve MCMC mixing for models with predictive nodes. Posterior predictive nodes are now sampled conditional on all other model nodes at the end of each MCMC iteration.</li>
<li>Adding functionality to the MCMC configuration system, including a new <code>replaceSamplers</code> method and updates to the arguments for the <code>addSamplers</code> method.</li>
<li>Adding an option to the WAIC system to allow additional burnin (in addition to standard MCMC burnin) before calculating online WAIC, thereby allowing inspection of initial samples without forcing them to be used for WAIC.</li>
<li>Warning users of unused constants during model building.</li>
<li>Fixing bugs that prevented use of variables starting with “logProb” or named “i” in model code.</li>
<li>Fixing a bug to prevent infinite recursion in particular cases in conjugacy checking.</li>
<li>Fixing a bug in simulating from <code>dcar_normal</code> nodes when multiple nodes passed to simulate.</li>
</ul>
<p>Please see the <a href="https://r-nimble.org/more/news">release notes</a> on our website for more details.</p>



 ]]></description>
  <category>release</category>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/version-0-13-0-of-nimble-released.html</guid>
  <pubDate>Tue, 29 Nov 2022 08:00:00 GMT</pubDate>
</item>
<item>
  <title>We’re looking for a programmer</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/were-looking-for-a-programmer.html</link>
  <description><![CDATA[ 




<p>The NIMBLE project anticipates having some funding for a part-time programmer to implement statistical algorithms and make improvements in nimble’s core code. Examples may include building adaptive Gaussian quadrature in nimble’s programming system and expanding nimble’s hierarchical model system. Remote work is possible. This is not a formal job solicitation, but please send a CV/resume to nimble.stats@gmail.com if you are interested so we can have you on our list. Important skills will be experience with hierarchical statistical modeling algorithms, R programming, and nimble itself. Experience with C++ will be helpful but not required.</p>



 ]]></description>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/were-looking-for-a-programmer.html</guid>
  <pubDate>Fri, 07 Oct 2022 07:00:00 GMT</pubDate>
</item>
<item>
  <title>NIMBLE virtual short course, January 4-6, 2023</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/nimble-virtual-short-course-january-4-6-2023.html</link>
  <description><![CDATA[ 




<p>We’ll be holding a virtual training workshop on <a href="http://r-nimble.org/">NIMBLE</a>, January 4-6, 2023 from 8 am to 1 pm US Pacific (California) time each day. NIMBLE is a system for building and sharing analysis methods for statistical models, especially for hierarchical models and computationally-intensive methods (such as MCMC and SMC).</p>
<p>Recently we added support for automatic differentiation (AD) to NIMBLE in a <a href="https://r-nimble.org/beta-version-of-nimble-with-automatic-differentiation-including-hmc-sampling-and-laplace-approximation">beta release</a>, and the workshop will cover NIMBLE’s AD capabilities in detail.</p>
<p>The workshop will cover the following material:</p>
<ul>
<li>the basic concepts and workflows for using NIMBLE and converting BUGS or JAGS models to work in NIMBLE.</li>
<li>overview of different MCMC sampling strategies and how to use them in NIMBLE, including Hamiltonian Monte Carlo (HMC).</li>
<li>writing new distributions and functions for more flexible modeling and more efficient computation.</li>
<li>tips and tricks for improving computational efficiency.</li>
<li>using advanced model components, including Bayesian non-parametric distributions (based on Dirichlet process priors), conditional auto-regressive (CAR) models for spatially correlated random fields, Laplace approximation, and reversible jump samplers for variable selection.</li>
<li>an introduction to programming new algorithms in NIMBLE.</li>
<li>use of automatic differentiation (AD) in algorithms.</li>
<li>calling R and compiled C++ code from compiled NIMBLE models or functions.</li>
</ul>
<p>If you are interested in attending, please <a href="https://forms.gle/8NuUcDJtMUGxCHv78">pre-register</a>. Registration fees will be $125 (regular) or $50 (student). We are also offering a process (see the pre-registration form) for students to request a fee waiver.</p>
<p>The workshop will assume attendees have a basic understanding of hierarchical/Bayesian models and MCMC, the BUGS (or JAGS) model language, and some familiarity with R.</p>



 ]]></description>
  <category>education</category>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/nimble-virtual-short-course-january-4-6-2023.html</guid>
  <pubDate>Thu, 08 Sep 2022 07:00:00 GMT</pubDate>
</item>
<item>
  <title>Beta version of NIMBLE with automatic differentiation, including HMC sampling and Laplace approximation</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/beta-version-of-nimble-with-automatic-differentiation-including-hmc-sampling-and-laplace-approximation.html</link>
  <description><![CDATA[ 




<p>We’re excited to announce that NIMBLE now supports automatic differentiation (AD), also known as algorithmic differentiation, in a <a href="https://r-nimble.org/ad-beta">beta version available on our website</a>. In this beta version, NIMBLE now provides:</p>
<ul>
<li>Hamiltonian Monte Carlo (HMC) sampling for an entire parameter vector or arbitrary subsets of the parameter vector (i.e., combined with other samplers for the remaining parameters).</li>
<li>Laplace approximation for approximate integration over latent states in a model, allowing maximum likelihood estimation and MCMC based on the marginal likelihood (via the RW_llFunction samplers).</li>
<li>The ability for users and algorithm developers to write nimbleFunctions that calculate derivatives of functions, including many but not all mathematical operations that are supported in the NIMBLE language.</li>
</ul>
<p>We’re making this beta release available to allow our users to test and evaluate the AD functionality and the new algorithms, but it is not recommended for production use at this stage. So please give it a try, and let us know of any problems or suggestions you have, either via the <a href="https://groups.google.com/g/nimble-users">nimble-users list</a>, <a href="https://github.com/nimble-dev/nimble/issues">bug reports to our GitHub repository</a>, or email to <a href="mailto:nimble.stats@gmail.com">nimble.stats@gmail.com</a>.</p>
<p>You can download the <a href="https://r-nimble.org/ad-beta">beta version</a> and view an [extensive draft manual]https://r-nimble.org/ADuserManual_draft/chapter_AD.html) for the AD functionality.</p>
<p>We plan to release this functionality in the next NIMBLE release on CRAN in the coming months.</p>



 ]]></description>
  <category>announcement</category>
  <category>release</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/beta-version-of-nimble-with-automatic-differentiation-including-hmc-sampling-and-laplace-approximation.html</guid>
  <pubDate>Fri, 15 Jul 2022 07:00:00 GMT</pubDate>
</item>
<item>
  <title>Version 0.12.2 of NIMBLE released, including an important bug fix for some models using Bayesian nonparametrics with the dCRP distribution</title>
  <dc:creator>NIMBLE Development Team</dc:creator>
  <link>https://r-nimble.org/blog/version-0-12-2-of-nimble-released-including-an-important-bug-fix-for-some-models-using-bayesian-nonparametrics-with-the-dcrp-distribution.html</link>
  <description><![CDATA[ 




<p>We’ve released the newest version of NIMBLE on CRAN and on our website. NIMBLE is a system for building and sharing analysis methods for statistical models, especially for hierarchical models and computationally-intensive methods (such as MCMC and SMC).</p>
<p>Version 0.12.2 is a bug fix release. In particular, this release fixes a bug in our Bayesian nonparametric distribution (BNP) functionality that gives incorrect MCMC results for some models, specifically when using the dCRP distribution when the parameters of the mixture components (i.e., the clusters) have hyperparameters (i.e., the base measure parameters) that are unknown and sampled during the MCMC. Here is an example basic model structure that is affected by the bug:</p>
<p><code>k[1:n] ~ dCRP(alpha, n) for(i in 1:n) {     y[i] ~ dnorm(mu[k[i]], 1)     mu[i] ~ dnorm(mu0, 1) ## mixture component parameters with hyperparameter } mu0 ~ dnorm(0, 1) ## unknown cluster hyperparameter</code></p>
<p>(There is no problem without the hyperparameter layer - i.e., if <code>mu0</code> is a fixed value – which is the situation in many models.)</p>
<p>We strongly encourage users using models with this type of structure to rerun their analyses, and we apologize for this issue.</p>
<p>Other changes in this release include:</p>
<ul>
<li>Fixing an issue with reversible jump variable selection under a similar situation to the BNP issue discussed above (in particular where there are unknown hyperparameters of the regression coefficients being considered, which would likely be an unusual use case).</li>
<li>Fixing a bug preventing setup of conjugate samplers for dwishart or dinvwishart nodes when using dynamic indexing.</li>
<li>Fixing a bug preventing use of truncation bounds specified via <code>data</code> or <code>constants</code>.</li>
<li>Fixing a bug preventing MCMC sampling with the LKJ prior for 2×2 matrices.</li>
<li>Fixing a bug in <code>runCrossValidate</code> affecting extraction of multivariate nodes.</li>
<li>Fixing a bug producing incorrect subset assignment into logical vectors in nimbleFunction code.</li>
<li>Fixing a bug preventing use of <code>nimbleExternalCall</code> with a constant expression.</li>
<li>Fixing a bug preventing use of recursion in nimbleFunctions without setup code.</li>
</ul>
<p>Please see the <a href="https://r-nimble.org/more/news">release notes</a> on our website for more details.</p>



 ]]></description>
  <category>release</category>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/version-0-12-2-of-nimble-released-including-an-important-bug-fix-for-some-models-using-bayesian-nonparametrics-with-the-dcrp-distribution.html</guid>
  <pubDate>Fri, 04 Mar 2022 08:00:00 GMT</pubDate>
</item>
<item>
  <title>NIMBLE in-person short course, June 1-3, Lisbon, Portugal</title>
  <dc:creator>Chris Paciorek</dc:creator>
  <link>https://r-nimble.org/blog/nimble-in-person-short-course-june-1-3-lisbon-portugal.html</link>
  <description><![CDATA[ 




<p>We’ll be holding a in-person training workshop on NIMBLE, June 1-3, 2022, in Lisbon, Portugal, sponsored by the Centro de Estatística e Aplicações at the Universidade Lisboa (CEAUL).</p>
<p>NIMBLE is a system for building and sharing analysis methods for statistical models, especially for hierarchical models and computationally-intensive methods (such as MCMC and SMC).</p>
<p>More details and registration are available at the <a href="https://nimbleworkshop.weebly.com/">workshop website</a>. No previous NIMBLE experience is required, but the workshop will assume some familiarity with hierarchical models, Markov chain Monte Carlo (MCMC), and R.</p>



 ]]></description>
  <category>education</category>
  <category>announcement</category>
  <category>R</category>
  <guid>https://r-nimble.org/blog/nimble-in-person-short-course-june-1-3-lisbon-portugal.html</guid>
  <pubDate>Wed, 02 Mar 2022 08:00:00 GMT</pubDate>
</item>
</channel>
</rss>
