<?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>Olivier Supplisson</title>
<link>https://olivier-supplisson.fr/posts.html</link>
<atom:link href="https://olivier-supplisson.fr/posts.xml" rel="self" type="application/rss+xml"/>
<description>Notes on R-INLA, inlabru, and Bayesian modeling workflows</description>
<generator>quarto-1.9.37</generator>
<lastBuildDate>Sun, 12 Apr 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Daily joint prediction of CAC40 opening and closing levels</title>
  <dc:creator>Olivier Supplisson</dc:creator>
  <link>https://olivier-supplisson.fr/posts/applications/daily-joint-prediction-of-cac40-opening-and-closing-levels/</link>
  <description><![CDATA[ 





<p>This application studies a joint forecasting problem for the CAC 40 opening and closing levels. Rather than treating the intraday move as a single response, the specification uses two linked observation equations: one for the opening level and one for the closing level. The link is simple: the opening equation is anchored by the previous close, and the closing equation is anchored by the same-day open.</p>
<p>The aim is to build a model able to maintain a probabilistic view of both levels during the day, quantify uncertainty, and update the implied distribution of the close once the opening level is known.</p>
<section id="data" class="level1">
<h1>Data</h1>
<p>The code below downloads daily CAC 40 opening and closing data from Yahoo Finance under the ticker <code>^FCHI</code> and starts the analysis on January 1, 2000.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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>(tidyverse)</span>
<span id="cb1-2">date_start <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.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2000-01-01"</span>)</span>
<span id="cb1-3">date_end <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.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-04-14"</span>)</span>
<span id="cb1-4">forecast_start <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.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-03-01"</span>)</span>
<span id="cb1-5">forecast_end <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.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-04-14"</span>)</span>
<span id="cb1-6"></span>
<span id="cb1-7">time_index <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> date_start, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> date_end, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1 day"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time_id =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row_number</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>
<span id="cb1-9"></span>
<span id="cb1-10">cac40_xts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> quantmod<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;">getSymbols</span>(</span>
<span id="cb1-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Symbols =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^FCHI"</span>,</span>
<span id="cb1-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">src =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"yahoo"</span>,</span>
<span id="cb1-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1999-01-01"</span>),</span>
<span id="cb1-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> date_end,</span>
<span id="cb1-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">auto.assign =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb1-16">)</span>
<span id="cb1-17"></span>
<span id="cb1-18">cac40 <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;">data.frame</span>(</span>
<span id="cb1-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> zoo<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;">index</span>(cac40_xts),</span>
<span id="cb1-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">open =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(quantmod<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;">Op</span>(cac40_xts)),</span>
<span id="cb1-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">close =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(quantmod<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;">Cl</span>(cac40_xts))</span>
<span id="cb1-22">)</span>
<span id="cb1-23"></span>
<span id="cb1-24"></span>
<span id="cb1-25">cac40 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cac40 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</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;">is.na</span>(open) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</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;">is.na</span>(close))  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">intercept =</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;">|&gt;</span> </span>
<span id="cb1-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">close_lag =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lag</span>(close)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(date <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> date_start) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(time_index, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"date"</span>)</span></code></pre></div></div>
</div>
<p>The observed series contains 6715 trading days, from 2000-01-03 to 2026-04-13.</p>
<p>The forecasting exercise below uses the trading days from 2026-03-01 to 2026-04-14 as a rolling one-step-ahead evaluation window. For each trading day in that range, the model is refit using only the data available up to the previous trading day.</p>
<p>The opening and closing levels are shown below.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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">plot_df <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;">rbind</span>(</span>
<span id="cb2-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> cac40<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">series =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Open"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> cac40<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>open),</span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> cac40<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">series =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> cac40<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>close)</span>
<span id="cb2-4">)</span>
<span id="cb2-5"></span>
<span id="cb2-6">ggplot2<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;">ggplot</span>(plot_df, ggplot2<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;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> series)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-7">  ggplot2<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;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.55</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-8">  ggplot2<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;">scale_colour_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</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;">"Open"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#6baed6"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#08519c"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-9">  ggplot2<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;">labs</span>(</span>
<span id="cb2-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>,</span>
<span id="cb2-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CAC 40 level"</span>,</span>
<span id="cb2-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Daily CAC 40 opening and closing levels"</span>,</span>
<span id="cb2-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<span id="cb2-14">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-15">  ggplot2<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;">theme_minimal</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://olivier-supplisson.fr/posts/applications/daily-joint-prediction-of-cac40-opening-and-closing-levels/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="1344"></p>
</figure>
</div>
</div>
</div>
<p>The two series move together over the long run, but the gap between them still changes enough from day to day to justify modeling the opening and closing levels separately.</p>
</section>
<section id="model" class="level1">
<h1>Model</h1>
<p>Let <img src="https://latex.codecogs.com/png.latex?O_t"> and <img src="https://latex.codecogs.com/png.latex?C_t"> denote the CAC 40 opening and closing levels on trading day <img src="https://latex.codecogs.com/png.latex?t">. Since both quantities are strictly positive, the observational layer uses the <a href="https://inla.r-inla-download.org/r-inla.org/doc/likelihood/lognormal.pdf">lognormal likelihood entry</a> from the R-INLA documentation.</p>
<p>Conditional on latent predictors <img src="https://latex.codecogs.com/png.latex?%5Ceta_t%5E%7B(O)%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Ceta_t%5E%7B(C)%7D">, the observation model is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Clog%20O_t%20%5Cmid%20%5Ceta_t%5E%7B(O)%7D,%20%5Ctau_O%20%5Csim%20%5Cmathcal%7BN%7D(%5Ceta_t%5E%7B(O)%7D,%20%5Ctau_O%5E%7B-1%7D),%0A%5Cqquad%0A%5Clog%20C_t%20%5Cmid%20%5Ceta_t%5E%7B(C)%7D,%20%5Ctau_C%20%5Csim%20%5Cmathcal%7BN%7D(%5Ceta_t%5E%7B(C)%7D,%20%5Ctau_C%5E%7B-1%7D).%0A"></p>
<p>The corresponding linear predictors are</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ceta_t%5E%7B(O)%7D%20=%20%5Clog%20C_%7Bt-1%7D%20+%20%5Calpha_O%20+%20h_t%5E%7B(O)%7D,%0A%5Cqquad%0A%5Ceta_t%5E%7B(C)%7D%20=%20%5Clog%20O_t%20+%20%5Calpha_C%20+%20h_t%5E%7B(C)%7D,%0A"></p>
<p>where the notation mirrors the code through <code>CloseLag</code>, <code>Open</code>, <code>InterceptOpen</code>, <code>InterceptClose</code>, <code>betaOpen</code>, and <code>betaClose</code>. The previous close enters the opening equation as an offset, while the same-day open enters the closing equation as an offset. The latent terms <img src="https://latex.codecogs.com/png.latex?h_t%5E%7B(O)%7D"> and <img src="https://latex.codecogs.com/png.latex?h_t%5E%7B(C)%7D"> are modeled separately, so the dependence between the two equations comes from this sequential conditioning structure rather than from a shared latent field.</p>
<p>Here, I am using a PRW2 latent effect but other approaches could be used with likely little to no change, as highlighted in <a href="../../../posts/methods/p-splines-in-r-inla/index.html">this methodological post on P-splines in R-INLA</a>. I’ll fit the corresponding model <code>inlabru</code>.</p>
<p>The code below defines the latent components:</p>
</section>
<section id="fit" class="level1">
<h1>Fit</h1>
<p>The remaining piece is the observation model. For the lognormal likelihood, the INLA implementation uses an identity link on the log scale together with a precision hyperparameter. In the current code, the opening and closing equations are written as two separate <code>bru_obs()</code> objects. Both equations receive the same PC prior on the observation precision.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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">prior_prec_likelihood <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="cb3-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pc.prec"</span>,</span>
<span id="cb3-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">param =</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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb3-4">)</span>
<span id="cb3-5"></span>
<span id="cb3-6"></span>
<span id="cb3-7">likopen <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;">bru_obs</span>(</span>
<span id="cb3-8">  open <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>(CloseLag)  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> InterceptOpen <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> betaOpen,</span>
<span id="cb3-9">  <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;">"lognormal"</span>,</span>
<span id="cb3-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> cac40,</span>
<span id="cb3-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control.family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb3-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">link =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>,</span>
<span id="cb3-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hyper =</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;">prec =</span> prior_prec_likelihood)</span>
<span id="cb3-14">  )</span>
<span id="cb3-15">)</span>
<span id="cb3-16"></span>
<span id="cb3-17">likclose <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;">bru_obs</span>(</span>
<span id="cb3-18">  close <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>(Open) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> InterceptClose <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> betaClose,</span>
<span id="cb3-19">  <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;">"lognormal"</span>,</span>
<span id="cb3-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> cac40,</span>
<span id="cb3-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control.family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb3-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">link =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>,</span>
<span id="cb3-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hyper =</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;">prec =</span> prior_prec_likelihood)</span>
<span id="cb3-24">  )</span>
<span id="cb3-25">)</span></code></pre></div></div>
</div>
<p>This is where the two-equation formulation is assembled: <code>likopen</code> and <code>likclose</code> are fitted together in a single <code>bru()</code> call against the same component object <code>cmp</code>.</p>
<p>The fit below keeps the configuration output needed later for posterior simulation, predictive checks, and rolling forecasts.</p>
</section>
<section id="in-sample-posterior-predictive-check" class="level1">
<h1>In-sample posterior predictive check</h1>
<p>As a first sanity check, a simple posterior predictive assessment can be carried out. The approach use a shenanigan hidden in <a href="https://groups.google.com/g/r-inla-discussion-group/c/QvQVySJdFlQ/m/WVcRtngKAAAJ">this post</a> on the R-INLA discussion forum.</p>
<p>The first step of this magical trick is to draw samples from the joint posterior distribution of the latent state.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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">ndraws <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb4-2">state <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;">evaluate_state</span>(</span>
<span id="cb4-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">result =</span> fit,</span>
<span id="cb4-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">property =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sample"</span>,</span>
<span id="cb4-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> ndraws,</span>
<span id="cb4-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>L</span>
<span id="cb4-7">)</span></code></pre></div></div>
</div>
<p>Once these draws are available, they can be combined to rebuild any predictor of interest. Here, the posterior predictive check is performed on the same data set that was used for estimation.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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">predictor_observed <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;">evaluate_model</span>(</span>
<span id="cb5-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bru_info<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model,</span>
<span id="cb5-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">result =</span> fit,</span>
<span id="cb5-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state =</span> state,</span>
<span id="cb5-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> cac40,</span>
<span id="cb5-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">predictor =</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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">predictor_close =</span> InterceptClose <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> betaClose,</span>
<span id="cb5-7">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">predictor_open =</span> InterceptOpen <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> betaOpen,</span>
<span id="cb5-8">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Precision_for_open =</span> Precision_for_the_lognormal_observations,</span>
<span id="cb5-9">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Precision_for_close =</span> Precision_for_the_lognormal_observations_2_)</span>
<span id="cb5-10">)</span>
<span id="cb5-11"></span>
<span id="cb5-12">predictor_observed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> purrr<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;">map2_dfr</span>(</span>
<span id="cb5-13">  predictor_observed,</span>
<span id="cb5-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(predictor_observed),</span>
<span id="cb5-15">  <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;">cbind</span>(.x, cac40) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">draw_id =</span> .y)</span>
<span id="cb5-16">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-17">  dplyr<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;">mutate</span>(</span>
<span id="cb5-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">predicted_open =</span> stats<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;">rlnorm</span>(</span>
<span id="cb5-19">      dplyr<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;">n</span>(),</span>
<span id="cb5-20">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">meanlog =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(close_lag) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> predictor_open,</span>
<span id="cb5-21">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sdlog =</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>(Precision_for_open)</span>
<span id="cb5-22">    ),</span>
<span id="cb5-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">predicted_close =</span> stats<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;">rlnorm</span>(</span>
<span id="cb5-24">      dplyr<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;">n</span>(),</span>
<span id="cb5-25">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">meanlog =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(open) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> predictor_close,</span>
<span id="cb5-26">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sdlog =</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>(Precision_for_close)</span>
<span id="cb5-27">    )</span>
<span id="cb5-28">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(date) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb5-31">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_predicted_open =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(predicted_open),</span>
<span id="cb5-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_predicted_close =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(predicted_close),</span>
<span id="cb5-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q0.025_predicted_open =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(predicted_open, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.025</span>),</span>
<span id="cb5-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q0.025_predicted_close =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(predicted_close, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.025</span>),</span>
<span id="cb5-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q0.975_predicted_open =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(predicted_open, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.975</span>),</span>
<span id="cb5-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q0.975_predicted_close =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(predicted_close, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.975</span>)</span>
<span id="cb5-37">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(cac40, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"date"</span>)</span></code></pre></div></div>
</div>
<p>The figure below compares the observed and replicated series over the 2026 segment.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">fun_to_plot_date <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(str_date_start,</span>
<span id="cb6-2">                             str_date_end){</span>
<span id="cb6-3">  plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> predictor_observed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(date <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(str_date_start),</span>
<span id="cb6-5">           date <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(str_date_end)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(open,</span>
<span id="cb6-7">                          close,</span>
<span id="cb6-8">                          mean_predicted_open,</span>
<span id="cb6-9">                          mean_predicted_close,</span>
<span id="cb6-10">                          q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.025</span>_predicted_open,</span>
<span id="cb6-11">                          q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.025</span>_predicted_close,</span>
<span id="cb6-12">                          q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.975</span>_predicted_open,</span>
<span id="cb6-13">                          q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.975</span>_predicted_close)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">facet =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb6-15">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"open"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Open"</span>,</span>
<span id="cb6-16">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"close"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span></span>
<span id="cb6-17">    ),</span>
<span id="cb6-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb6-19">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</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;">"open"</span>, </span>
<span id="cb6-20">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"close"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observed"</span>,</span>
<span id="cb6-21">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</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;">"q0.025_predicted_open"</span>,</span>
<span id="cb6-22">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q0.025_predicted_close"</span>,</span>
<span id="cb6-23">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q0.975_predicted_open"</span>,</span>
<span id="cb6-24">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q0.975_predicted_close"</span>,</span>
<span id="cb6-25">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean_predicted_open"</span>, </span>
<span id="cb6-26">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean_predicted_close"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simulated"</span></span>
<span id="cb6-27">    ),</span>
<span id="cb6-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label_legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb6-29">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"open"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Open"</span>,</span>
<span id="cb6-30">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"close"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span>,</span>
<span id="cb6-31">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q0.025_predicted_open"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LB ETI95%"</span>,</span>
<span id="cb6-32">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q0.025_predicted_close"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LB ETI95%"</span>,</span>
<span id="cb6-33">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q0.975_predicted_open"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UB ETI95%"</span>,</span>
<span id="cb6-34">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q0.975_predicted_close"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UB ETI95%"</span>,</span>
<span id="cb6-35">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean_predicted_open"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Average"</span>, </span>
<span id="cb6-36">     name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean_predicted_close"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Average"</span></span>
<span id="cb6-37">    )) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-38">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb6-39">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, </span>
<span id="cb6-40">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value, </span>
<span id="cb6-41">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> label_legend, </span>
<span id="cb6-42">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> type)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb6-43">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>facet, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</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></span>
<span id="cb6-44">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb6-45">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>,</span>
<span id="cb6-46">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Index"</span>,</span>
<span id="cb6-47">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Series: "</span>,</span>
<span id="cb6-48">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Type of series: "</span></span>
<span id="cb6-49">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-50">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-51">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>)</span>
<span id="cb6-52">  </span>
<span id="cb6-53">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Never said this blog was for cood coding practice ;-)</span></span>
<span id="cb6-54">  d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>type, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>label_legend, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>facet) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> value)</span>
<span id="cb6-55">  pct_open <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;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</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;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(d, open <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.025</span>_predicted_open <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> open <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.975</span>_predicted_open)), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb6-56">  pct_close <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;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</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;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(d, close <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.025</span>_predicted_close <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> close <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.975</span>_predicted_close)), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb6-57">  <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;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> plot,</span>
<span id="cb6-58">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pct_open"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> pct_open,</span>
<span id="cb6-59">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pct_close"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> pct_close))</span>
<span id="cb6-60"></span>
<span id="cb6-61">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#From "2026-01-01" to "2026-04-14"</span></span>
<span id="cb7-2">p <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;">fun_to_plot_date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-01-01"</span>, date_end)</span>
<span id="cb7-3">p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot"</span>]</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>$plot</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://olivier-supplisson.fr/posts/applications/daily-joint-prediction-of-cac40-opening-and-closing-levels/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="1344"></p>
</figure>
</div>
</div>
</div>
<p>The plot shows that the posterior means can miss individual days, but the interval bands are the more relevant object here because the application is about sequential uncertainty quantification rather than about a single determin</p>
<p>istic path. The observed value is almost always within this uncertainty band. More specifically, only the proportion of values observed outside the ETI95% were equal to `1.43`% and `0`%, for the open and close serie, respectively. Across the full period these % were 1.77% and 2.49%.</p>
<p>Not bad! However, an in-sample check only tells us whether the fitted model can reproduce the training data. The more relevant exercise is a one-step-ahead forecasting problem.</p>
</section>
<section id="out-of-sample-day-ahead-forecast" class="level1">
<h1>Out-of-sample day-ahead forecast</h1>
<p>In this section, I keep the same state-based workflow as above, but instead of simulating one long path over the whole hold-out period, I refit the model day by day. For each trading day in the evaluation window, both <code>open</code> and <code>close</code> are hidden, the model is rerun using only the earlier observations, and I simulate the same-day opening and closing levels. The opening draw uses the observed lagged close from the previous trading day, while the closing draw uses the simulated opening level from the same posterior draw.</p>
<p>This mirrors the situation where a trader would perform its prediction at end of time <img src="https://latex.codecogs.com/png.latex?t"> for planning its action in time <img src="https://latex.codecogs.com/png.latex?t+1"> based on the predictive distribution of CAC40 index opening and closing level.</p>
<p>This produces one predictive distribution per day in the hold-out window, which can then be compared with the realised opening and closing levels over the evaluation period.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">periodic_forecast <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(open,</span>
<span id="cb9-3">                        close,</span>
<span id="cb9-4">                        mean_predicted_open,</span>
<span id="cb9-5">                        mean_predicted_close,</span>
<span id="cb9-6">                        q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.025</span>_predicted_open,</span>
<span id="cb9-7">                        q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.025</span>_predicted_close,</span>
<span id="cb9-8">                        q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.975</span>_predicted_open,</span>
<span id="cb9-9">                        q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.975</span>_predicted_close)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb9-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">facet =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb9-12">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"open"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Open"</span>,</span>
<span id="cb9-13">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"close"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span></span>
<span id="cb9-14">    ),</span>
<span id="cb9-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb9-16">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</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;">"open"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"close"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observed"</span>,</span>
<span id="cb9-17">      <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simulated"</span></span>
<span id="cb9-18">    ),</span>
<span id="cb9-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label_legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb9-20">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"open"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Open"</span>,</span>
<span id="cb9-21">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"close"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Close"</span>,</span>
<span id="cb9-22">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q0.025_predicted_open"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LB ETI95%"</span>,</span>
<span id="cb9-23">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q0.025_predicted_close"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LB ETI95%"</span>,</span>
<span id="cb9-24">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q0.975_predicted_open"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UB ETI95%"</span>,</span>
<span id="cb9-25">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q0.975_predicted_close"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UB ETI95%"</span>,</span>
<span id="cb9-26">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean_predicted_open"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Average"</span>,</span>
<span id="cb9-27">      name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean_predicted_close"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Average"</span></span>
<span id="cb9-28">    )</span>
<span id="cb9-29">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(</span>
<span id="cb9-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date,</span>
<span id="cb9-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value,</span>
<span id="cb9-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> label_legend,</span>
<span id="cb9-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> type</span>
<span id="cb9-36">  ))  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(</span>
<span id="cb9-38">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date,</span>
<span id="cb9-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value,</span>
<span id="cb9-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> label_legend</span>
<span id="cb9-41">  ), <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-42">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>facet, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</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></span>
<span id="cb9-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb9-44">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>,</span>
<span id="cb9-45">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Index"</span>,</span>
<span id="cb9-46">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Series: "</span>,</span>
<span id="cb9-47">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Type of series: "</span></span>
<span id="cb9-48">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://olivier-supplisson.fr/posts/applications/daily-joint-prediction-of-cac40-opening-and-closing-levels/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="1344"></p>
</figure>
</div>
</div>
</div>
<p>As expected, more observed values are falling outside of the uncertainty interval. More specifically, 24.14% of the observed opening values and 6.9% of the observed closing values fall outside the ETI95% over the hold-out window. Considering a larger uncertainty band, such as the ETI99%, would help decreasing these percentages.</p>


</section>

 ]]></description>
  <category>Applications</category>
  <guid>https://olivier-supplisson.fr/posts/applications/daily-joint-prediction-of-cac40-opening-and-closing-levels/</guid>
  <pubDate>Sun, 12 Apr 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Censored binomial likelihood in R-INLA</title>
  <dc:creator>Olivier Supplisson</dc:creator>
  <link>https://olivier-supplisson.fr/posts/methods/censored-binomial/</link>
  <description><![CDATA[ 





<p><a href="../../../downloads/cloglike-censored-binomial.c">Download <code>cloglike-censored-binomial.c</code></a></p>
<p>R-INLA provides a wide range of built-in likelihoods, documented on the <a href="https://inla.r-inla-download.org/r-inla.org/doc/likelihood/">project likelihood page</a>. Since <a href="https://github.com/hrue/r-inla/discussions/112">INLA 25.08.21</a>, an experimental interface also allows users to implement custom likelihoods. In this post, I use that feature to define a censored binomial likelihood. Notice that censored likelihood are already available for the <a href="chrome-extension://efaidnbmnnnibpcajpcglclefindmkaj/https://inla.r-inla-download.org/r-inla.org/doc/likelihood/cenpoisson2.pdf">Poisson</a> and <a href="chrome-extension://efaidnbmnnnibpcajpcglclefindmkaj/https://inla.r-inla-download.org/r-inla.org/doc/likelihood/cennbinomial2.pdf">Negative Binomial</a> distribution.</p>
<p>This likelihood is useful when counts are deliberately masked for disclosure control. A common situation is left censoring at a fixed threshold: if the true count is small, the released dataset does not report the exact value and only indicates that it lies below the threshold. For example, some public spatially-indexed health datasets in France suppress counts at or below 10.</p>
<section id="binomial-and-censored-binomial-likelihood" class="level1">
<h1>Binomial and Censored binomial likelihood</h1>
<p>Let <img src="https://latex.codecogs.com/png.latex?Y"> denote the number of successes in a sample of size <img src="https://latex.codecogs.com/png.latex?N">. A natural model is the binomial distribution</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AY%20%5Cmid%20p,N%20%5Csim%20%5Cmathcal%7BB%7D(N,%20p),%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?p"> is the success probability. The corresponding probability mass function is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CPr(Y%20=%20j%20%5Cmid%20p,%20N)%20=%20%5Cbinom%7BN%7D%7Bj%7D%20p%5Ej%20(1%20-%20p)%5E%7BN%20-%20j%7D,%0A%5Cqquad%20j%20=%200,%201,%20%5Cdots,%20N.%0A"></p>
<p>In regression settings, it is standard to connect <img src="https://latex.codecogs.com/png.latex?p"> to a linear predictor <img src="https://latex.codecogs.com/png.latex?%5Ceta"> through the logit link</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ap%20=%20%5Coperatorname%7Blogit%7D%5E%7B-1%7D(%5Ceta)%20=%20%5Cfrac%7B%5Cexp(%5Ceta)%7D%7B1%20+%20%5Cexp(%5Ceta)%7D.%0A"></p>
<p>Now suppose that counts are left censored at a threshold <img src="https://latex.codecogs.com/png.latex?c">. We no longer observe <img src="https://latex.codecogs.com/png.latex?Y"> exactly for small values. Instead, we observe</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AZ%20=%0A%5Cbegin%7Bcases%7D%0AY,%20&amp;%20%5Ctext%7Bif%20%7D%20Y%20%3E%20c,%20%5C%5C%0A%5Ctext%7Bcensored%7D,%20&amp;%20%5Ctext%7Bif%20%7D%20Y%20%5Cle%20c.%0A%5Cend%7Bcases%7D%0A"></p>
<p>The observed-data likelihood therefore has two contributions.</p>
<p>For an uncensored observation, with reported count <img src="https://latex.codecogs.com/png.latex?z%20%3E%20c">,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CPr(Z%20=%20z%20%5Cmid%20p,%20N,%20c)%20=%20%5CPr(Y%20=%20z%20%5Cmid%20p,%20N)%0A=%20%5Cbinom%7BN%7D%7Bz%7D%20p%5Ez%20(1%20-%20p)%5E%7BN%20-%20z%7D.%0A"></p>
<p>For a censored observation,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CPr(Z%20=%20%5Ctext%7Bcensored%7D%20%5Cmid%20p,%20N,%20c)%0A=%20%5CPr(Y%20%5Cle%20c%20%5Cmid%20p,%20N)%0A=%20%5Csum_%7Bk=0%7D%5E%7Bc%7D%20%5Cbinom%7BN%7D%7Bk%7D%20p%5Ek%20(1%20-%20p)%5E%7BN-k%7D.%0A"></p>
<p>So the censored binomial likelihood is just the usual binomial likelihood for uncensored observations, and the binomial cumulative distribution function for censored observations.</p>
<p>For implementation in INLA it is convenient to write the contribution on the log scale, as this is required by the cloglike interface.</p>
<p>For an uncensored observation with reported count <img src="https://latex.codecogs.com/png.latex?z%20%3E%20c">,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Clog%20%5CPr(Z%20=%20z%20%5Cmid%20%5Ceta,%20N,%20c)%0A=%20%5Clog%20%5Cbinom%7BN%7D%7Bz%7D%0A+%20z%20%5Ceta%0A-%20N%20%5Clog(1%20+%20%5Cexp(%5Ceta)).%0A"></p>
<p>For a censored observation,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Clog%20%5CPr(Z%20=%20%5Ctext%7Bcensored%7D%20%5Cmid%20%5Ceta,%20N,%20c)%0A=%20%5Clog%20%5Cleft%5B%0A%5Csum_%7Bk=0%7D%5E%7Bc%7D%0A%5Cbinom%7BN%7D%7Bk%7D%0A%5Coperatorname%7Blogit%7D%5E%7B-1%7D(%5Ceta)%5Ek%0A%5Cleft(1%20-%20%5Coperatorname%7Blogit%7D%5E%7B-1%7D(%5Ceta)%5Cright)%5E%7BN-k%7D%0A%5Cright%5D.%0A"></p>
<p>In practice, the censored term should be evaluated through the binomial CDF rather than by summing probabilities manually. In <code>R</code>, this corresponds to <code>pbinom(c, size = N, prob = plogis(eta), log.p = TRUE)</code>, while the uncensored term is <code>dbinom(z, size = N, prob = plogis(eta), log = TRUE)</code>.</p>
</section>
<section id="simulation-study-why-do-we-have-to-account-for-this-censoring" class="level1">
<h1>Simulation study: why do we have to account for this censoring?</h1>
<p>The code above fit a binomial model for the specific case where <img src="https://latex.codecogs.com/png.latex?(y_i)_%7Bi%5Cin%5C%7B1,%5Ccdots,10000%5C%7D%7D%5Cvert%20N_i,%20p%5Csim%20B(N_i,p)">, <img src="https://latex.codecogs.com/png.latex?N_i%5Cvert%5Clambda=20%5Csim%5Cmathcal%7BP%7D(%5Clambda)">, and <img src="https://latex.codecogs.com/png.latex?p=0.3">. The censoring threshold is set to 5. For the fit, a uniform prior on <img src="https://latex.codecogs.com/png.latex?(0,1)"> is set.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Packages</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(INLA)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(inlabru)</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Simulation setup</span></span>
<span id="cb1-7">prob <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span></span>
<span id="cb1-8">nsample <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb1-9">N <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;">rpois</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> nsample, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb1-10">censoring_threshold <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb1-11">Y <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;">rbinom</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> nsample, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> prob)</span>
<span id="cb1-12">censored <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.integer</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> censoring_threshold)</span>
<span id="cb1-13">Z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> Y</span>
<span id="cb1-14">Z[censored <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>L] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> censoring_threshold</span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Bru components</span></span>
<span id="cb1-17">comp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(</span>
<span id="cb1-18">  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb1-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean.linear =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb1-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prec.linear =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb1-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">marginal =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bm_marginal</span>(qbeta, pbeta, dbeta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape1 =</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;">shape2 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-22">)</span>
<span id="cb1-23"></span>
<span id="cb1-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Uncensored fit</span></span>
<span id="cb1-25">likelihood_uncensored <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;">bru_obs</span>(</span>
<span id="cb1-26">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y <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;">0</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;">qlogis</span>(p),</span>
<span id="cb1-27">  <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;">"binomial"</span>,</span>
<span id="cb1-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(Y,N),</span>
<span id="cb1-29">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Ntrials =</span> N,</span>
<span id="cb1-30">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control.family =</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;">link =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logit"</span>)</span>
<span id="cb1-31">)</span>
<span id="cb1-32"></span>
<span id="cb1-33">fit_uncensored <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;">bru</span>(comp, </span>
<span id="cb1-34">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">likelihood=</span>likelihood_uncensored, </span>
<span id="cb1-35">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">options =</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;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb1-36">                                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bru_verbose =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb1-37">                                     )</span>
<span id="cb1-38">                      )</span>
<span id="cb1-39"></span>
<span id="cb1-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Censored fit</span></span>
<span id="cb1-41">likelihood_censored <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;">bru_obs</span>(</span>
<span id="cb1-42">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Z <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;">0</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;">qlogis</span>(p),</span>
<span id="cb1-43">  <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;">"binomial"</span>,</span>
<span id="cb1-44">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(Z,N),</span>
<span id="cb1-45">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Ntrials =</span> N,</span>
<span id="cb1-46">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control.family =</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;">link =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logit"</span>)</span>
<span id="cb1-47">)</span>
<span id="cb1-48"></span>
<span id="cb1-49">fit_censored <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;">bru</span>(comp, </span>
<span id="cb1-50">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">likelihood=</span>likelihood_censored, </span>
<span id="cb1-51">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">options =</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;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb1-52">                                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bru_verbose =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb1-53">                                     )</span>
<span id="cb1-54">                      )</span>
<span id="cb1-55"></span>
<span id="cb1-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Compute the posterior mean and ETI95% of p</span></span>
<span id="cb1-57">draws_uncensored <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;">predict</span>(fit_uncensored, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p=</span>p))</span>
<span id="cb1-58">draws_censored <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;">predict</span>(fit_censored, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p=</span>p))</span>
<span id="cb1-59"></span>
<span id="cb1-60">fmt2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.4f"</span>, x))</span>
<span id="cb1-61"></span>
<span id="cb1-62">result_uncensored <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;">paste0</span>(</span>
<span id="cb1-63">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt2</span>(draws_uncensored<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" ["</span>,</span>
<span id="cb1-64">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt2</span>(draws_uncensored<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.025</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>,</span>
<span id="cb1-65">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt2</span>(draws_uncensored<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.975</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]"</span></span>
<span id="cb1-66">)</span>
<span id="cb1-67"></span>
<span id="cb1-68">result_censored <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;">paste0</span>(</span>
<span id="cb1-69">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt2</span>(draws_censored<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" ["</span>,</span>
<span id="cb1-70">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt2</span>(draws_censored<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.025</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>,</span>
<span id="cb1-71">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt2</span>(draws_censored<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.975</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]"</span></span>
<span id="cb1-72">)</span></code></pre></div></div>
</div>
<p>The mean [ETI95%] found is 0,3007 [0,2985, 0,3024] for the uncensored fit and 0,3263 [0,3243, 0,3282] for the censored one, showing that not accounting for the censoring will induce an upward inflation in the inferred parameter.</p>
</section>
<section id="censored-binomial-implementation" class="level1">
<h1>Censored binomial implementation</h1>
<p>The <code>cloglike-censored-binomial.c</code> file, downloadable at the start of this post, implements the censored binomial likelihood. Let us give it a try with the previous setpup.</p>
<p>The <code>cloglike</code> fit needs a matrix-valued response passed through <code>data=</code>, with a response name that matches the left-hand side of the formula, as examplified below.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Look at the rgeneric vignette for the process</span></span>
<span id="cb2-2">cloglike <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.cloglike.define</span>(<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;">"inla_cloglike_censored_binomial"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shlib =</span> shlib)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#INLA response</span></span>
<span id="cb2-5">Y_cens <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> INLA<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;">inla.mdata</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(Z, N, censored))</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Bru likelihood</span></span>
<span id="cb2-8">likelihood_censored_new <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;">bru_obs</span>(</span>
<span id="cb2-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y_cens <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;">0</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;">qlogis</span>(p),</span>
<span id="cb2-10">  <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;">"cloglike"</span>,</span>
<span id="cb2-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb2-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y_cens =</span> Y_cens</span>
<span id="cb2-13">  ),</span>
<span id="cb2-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control.family =</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;">cloglike =</span> cloglike),</span>
<span id="cb2-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">is_rowwise =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb2-16">)</span>
<span id="cb2-17"></span>
<span id="cb2-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Fit</span></span>
<span id="cb2-19">fit_censored_new <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;">bru</span>(</span>
<span id="cb2-20">  comp,</span>
<span id="cb2-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">likelihood =</span> likelihood_censored_new,</span>
<span id="cb2-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">options =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb2-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> F,</span>
<span id="cb2-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bru_verbose =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb2-25">  )</span>
<span id="cb2-26">)</span>
<span id="cb2-27"></span>
<span id="cb2-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Posterior draws</span></span>
<span id="cb2-29">draws_censored_new <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;">predict</span>(fit_censored_new, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p=</span>p))</span>
<span id="cb2-30"></span>
<span id="cb2-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Summary</span></span>
<span id="cb2-32">result_censored_new <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;">paste0</span>(</span>
<span id="cb2-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt2</span>(draws_censored_new<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" ["</span>,</span>
<span id="cb2-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt2</span>(draws_censored_new<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.025</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>,</span>
<span id="cb2-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt2</span>(draws_censored_new<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>q0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.975</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]"</span></span>
<span id="cb2-36">)</span></code></pre></div></div>
</div>
<p>With this likelihood implementation, the correct parameter is recovered, as the posterior mean [ETI95%] is 0,3006 [0,2990, 0,3026].</p>


</section>

 ]]></description>
  <category>Methods</category>
  <guid>https://olivier-supplisson.fr/posts/methods/censored-binomial/</guid>
  <pubDate>Sun, 12 Apr 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Univariate P-splines in R-INLA and inlabru</title>
  <dc:creator>Olivier Supplisson</dc:creator>
  <link>https://olivier-supplisson.fr/posts/methods/p-splines-in-r-inla/</link>
  <description><![CDATA[ 





<p>P-splines combine a flexible B-spline basis with a roughness penalty on the coefficients. Their main practical objective is to make model specification more robust to the number and locations of the knots used to define the basis. In a Bayesian setting, that penalty is naturally represented by an n-th-order random walk prior on the spline weights. This short blog post shows how to fit a smoothing P-spline in R-INLA.</p>
<p>Useful academic work on P-splines include:</p>
<ul>
<li><p>Paul Eilers and Brian Marx’s original article introducing P-splines, <em>Flexible Smoothing with B-splines and Penalties,</em> <a href="https://projecteuclid.org/journals/statistical-science/volume-11/issue-2/Flexible-smoothing-with-B-splines-and-penalties/10.1214/ss/1038425655.pdf" class="uri">https://projecteuclid.org/journals/statistical-science/volume-11/issue-2/Flexible-smoothing-with-B-splines-and-penalties/10.1214/ss/1038425655.pdf</a>.</p></li>
<li><p>Paul Eilers and Brian Marx’s masterpiece <em>Practical Smoothing: The Joys of P-splines</em>, <a href="https://psplines.bitbucket.io/" class="uri">https://psplines.bitbucket.io/</a>.</p></li>
<li><p>Stefan Lang and Andreas Brezger’s <em>Bayesian P-splines</em> paper, <a href="https://www.tandfonline.com/doi/abs/10.1198/1061860043010" class="uri">https://www.tandfonline.com/doi/abs/10.1198/1061860043010</a>.</p></li>
<li><p>Aritz Adin Urtasun’s Ph.D.&nbsp;thesis <em>Hierarchical and spline-based models in space-time disease mapping.</em> I could not find the original link, so I uploaded the file to my personal Google Drive <a href="https://drive.google.com/file/d/10DnjlI09w3TvHrQAZk-NbqWnkSZzHsxY/view?usp=sharing">here</a>.</p></li>
<li><p>Dae-Jin Lee’s Ph.D.&nbsp;thesis <em>Smoothing mixed models for spatial and spatio-temporal data,</em> <a href="https://e-archivo.uc3m.es/entities/publication/72bffb09-ff83-48eb-acba-8aaed8de45f4" class="uri">https://e-archivo.uc3m.es/entities/publication/72bffb09-ff83-48eb-acba-8aaed8de45f4</a>.</p></li>
</ul>
<p>Related blog posts:</p>
<ul>
<li><p><a href="https://tjmahr.github.io/">Tristan Mahr</a>’s blog post <em>Random effects and penalized splines are the same thing</em>, <a href="https://www.tjmahr.com/random-effects-penalized-splines-same-thing/" class="uri">https://www.tjmahr.com/random-effects-penalized-splines-same-thing/</a>.</p></li>
<li><p>Gavin Simpson’s blog post <em>Extrapolating with B splines and GAMs</em>, <a href="https://fromthebottomoftheheap.net/2020/06/03/extrapolating-with-gams/" class="uri">https://fromthebottomoftheheap.net/2020/06/03/extrapolating-with-gams/</a>.</p></li>
</ul>
<section id="b-spline-basis" class="level1">
<h1>B-spline basis</h1>
<p>For a scalar covariate <img src="https://latex.codecogs.com/png.latex?x">, a P-spline smooth approximates the unknown function <img src="https://latex.codecogs.com/png.latex?f"> through a finite-dimensional decomposition:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af(x)=%20%5Cbeta_0%20+%20%5Csum_%7Bj=1%7D%5E%7BK%7D%20B_j(x)%5Calpha_j,%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?B_j(x)"> are spline basis functions defined over the full domain of <img src="https://latex.codecogs.com/png.latex?x">. The basis itself is determined by two ingredients: a polynomial degree and a knot sequence. The knots split the covariate range into intervals, and on each interval every <img src="https://latex.codecogs.com/png.latex?B_j"> is an ordinary polynomial. In practice one often uses cubic B-splines, that is, splines of degree 3, because they are flexible while remaining smooth enough for most regression problems.</p>
<p>A central property of B-splines is local support. Each <img src="https://latex.codecogs.com/png.latex?B_j(x)"> is zero outside a limited portion of the covariate range, so at a fixed location <img src="https://latex.codecogs.com/png.latex?x"> only a few basis functions are active. For splines of degree <img src="https://latex.codecogs.com/png.latex?d">, at most <img src="https://latex.codecogs.com/png.latex?d%20+%201"> basis functions are nonzero at the same <img src="https://latex.codecogs.com/png.latex?x">. This is what makes the basis local: changing one coefficient mainly perturbs the fitted curve near the associated knot region instead of changing the function everywhere.</p>
<p>Another useful property is that the basis functions form a partition of unity, meaning</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Csum_%7Bj=1%7D%5E%7BK%7D%20B_j(x)%20=%201.%0A"></p>
<p>This makes the spline behave like a local weighted average of the coefficients. It also clarifies why one has to think about identifiability: if the basis can already represent constant functions, then the intercept and the spline part are not automatically separated unless one imposes a constraint or centers the smooth.</p>
<p>The coefficients <img src="https://latex.codecogs.com/png.latex?(%5Calpha_1,%5Cldots,%5Calpha_K)"> control the contribution of each basis function. For observations <img src="https://latex.codecogs.com/png.latex?x_1,%5Cldots,x_n">, the basis enters the model through the design matrix</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7BB%7D%20=%0A%5Cbegin%7Bpmatrix%7D%0AB_1(x_1)%20&amp;%20%5Ccdots%20&amp;%20B_K(x_1)%20%5C%5C%0A%5Cvdots%20&amp;%20%5Cddots%20&amp;%20%5Cvdots%20%5C%5C%0AB_1(x_n)%20&amp;%20%5Ccdots%20&amp;%20B_K(x_n)%0A%5Cend%7Bpmatrix%7D.%0A"></p>
<p>Row <img src="https://latex.codecogs.com/png.latex?i"> shows how the basis combines to represent the smooth at <img src="https://latex.codecogs.com/png.latex?x_i">, and column <img src="https://latex.codecogs.com/png.latex?j"> shows where coefficient <img src="https://latex.codecogs.com/png.latex?%5Calpha_j"> contributes. Because of local support, this matrix is typically sparse or at least strongly banded in structure, which is one of the reasons B-splines are computationally attractive.</p>
<p>However, the fit quality of B-splines has been shown to be highly sensitive to the number and locations of the knots used to define the basis, leading to poor representation when too few knots are used and to overfitting when too many are used.</p>
<p>P-splines provide a fallback solution to this issue: instead of leaving the parameters <img src="https://latex.codecogs.com/png.latex?%5Calpha"> unconstrained, they penalise their finite differences so that neighbouring coefficients are encouraged to vary smoothly. With the standard second-order penalty, the second differences satisfy</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Calpha_j%20-%202%5Calpha_%7Bj-1%7D%20+%20%5Calpha_%7Bj-2%7D%20%5Csim%20N(0,%20%5Ctau%5E%7B-1%7D),%0A%5Cqquad%20j%20=%203,%20%5Cldots,%20K.%0A"></p>
<p>Large values of <img src="https://latex.codecogs.com/png.latex?%5Ctau"> shrink the second differences more strongly and therefore produce smoother functions, while smaller values allow more local curvature. The role of the P-spline is therefore split into two parts: the B-spline basis provides a rich approximation space, and the RW2 penalty regularises that space so that the fitted curve remains smooth.</p>
<p>Although it is possible to construct P-splines in several ways, I keep the rest of this post close to plain R-INLA so the basis construction and the RW2 penalty remain explicit.</p>
</section>
<section id="data-import" class="level1">
<h1>Data import</h1>
<p>To keep the discussion concrete, I will work with daily closing values of the CAC 40 index. On Yahoo Finance, the CAC 40 index is available under the ticker <code>^FCHI</code>.</p>
<p>For the purpose of this blog post, I will use</p>
</section>
<section id="basis-construction" class="level1">
<h1>Basis construction</h1>
<p>For illustrative purposes, I will model the closing series <code>y</code> as a smooth function of the trading-day index <code>x</code>. With <img src="https://latex.codecogs.com/png.latex?x%20=%201,%20%5Cldots,%20n"> (n = 1608), the B-spline basis is built as follows. To reduce boundary effects, I extend the covariate range slightly and construct the basis on the augmented grid from <code>-5</code> to <code>n + 5</code>, then evaluate it back on the observed index.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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">k <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>L</span>
<span id="cb1-2">B_aug <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;">seq</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;">5</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(x) <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;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-3">B_basis <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;">bs</span>(</span>
<span id="cb1-4">  x,</span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> k,</span>
<span id="cb1-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">degree =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb1-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">intercept =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb1-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Boundary.knots =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>(B_aug)</span>
<span id="cb1-9">)</span>
<span id="cb1-10">B <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;">unclass</span>(B_basis)</span>
<span id="cb1-11">A_ps <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;">Matrix</span>(B, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sparse =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
</div>
<p>Here <code>B</code> is the B-spline design matrix evaluated at the observed covariate values. The argument <code>k</code> controls the basis dimension and therefore the richness of the spline representation. The extended boundary knots help stabilise the fitted spline near the endpoints. With the chosen degree, this induces the following internal knot locations:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attr</span>(B_basis, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"knots"</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1]   21.60256   42.20513   62.80769   83.41026  104.01282  124.61538
 [7]  145.21795  165.82051  186.42308  207.02564  227.62821  248.23077
[13]  268.83333  289.43590  310.03846  330.64103  351.24359  371.84615
[19]  392.44872  413.05128  433.65385  454.25641  474.85897  495.46154
[25]  516.06410  536.66667  557.26923  577.87179  598.47436  619.07692
[31]  639.67949  660.28205  680.88462  701.48718  722.08974  742.69231
[37]  763.29487  783.89744  804.50000  825.10256  845.70513  866.30769
[43]  886.91026  907.51282  928.11538  948.71795  969.32051  989.92308
[49] 1010.52564 1031.12821 1051.73077 1072.33333 1092.93590 1113.53846
[55] 1134.14103 1154.74359 1175.34615 1195.94872 1216.55128 1237.15385
[61] 1257.75641 1278.35897 1298.96154 1319.56410 1340.16667 1360.76923
[67] 1381.37179 1401.97436 1422.57692 1443.17949 1463.78205 1484.38462
[73] 1504.98718 1525.58974 1546.19231 1566.79487 1587.39744</code></pre>
</div>
</div>
<p>The resulting basis functions along the covariate index are shown below.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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">basis_df <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;">data.frame</span>(</span>
<span id="cb4-2">  <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;">rep</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(B)),</span>
<span id="cb4-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">basis =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(B)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(B))),</span>
<span id="cb4-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(B)</span>
<span id="cb4-5">)</span>
<span id="cb4-6"></span>
<span id="cb4-7">knot_df <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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">knot =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attr</span>(B_basis, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"knots"</span>)))</span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(basis_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> basis, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> basis)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(</span>
<span id="cb4-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> knot_df,</span>
<span id="cb4-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> knot),</span>
<span id="cb4-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb4-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey45"</span>,</span>
<span id="cb4-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb4-17">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> grDevices<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;">hcl.colors</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(B), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dynamic"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb4-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Trading-day index"</span>,</span>
<span id="cb4-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Basis value"</span>,</span>
<span id="cb4-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B-spline basis along x"</span></span>
<span id="cb4-23">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://olivier-supplisson.fr/posts/methods/p-splines-in-r-inla/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="1344"></p>
</figure>
</div>
</div>
</div>
<p>The same object can also be viewed directly as a matrix. This makes the local support of the basis especially visible: each column is active only over a limited range of rows, which gives the design matrix its banded structure.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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">B_long <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;">data.frame</span>(</span>
<span id="cb5-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(B)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(B)),</span>
<span id="cb5-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">basis =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(B)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(B)),</span>
<span id="cb5-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(B)</span>
<span id="cb5-5">)</span>
<span id="cb5-6"></span>
<span id="cb5-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(B_long, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> row, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> basis, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_raster</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(</span>
<span id="cb5-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> knot_df,</span>
<span id="cb5-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> knot),</span>
<span id="cb5-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb5-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey45"</span>,</span>
<span id="cb5-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb5-15">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradientn</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colors =</span> grDevices<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;">hcl.colors</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"YlOrRd"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rev =</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>
<span id="cb5-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb5-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Trading-day index"</span>,</span>
<span id="cb5-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Basis index"</span>,</span>
<span id="cb5-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Value"</span>,</span>
<span id="cb5-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B-spline design matrix B"</span></span>
<span id="cb5-22">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://olivier-supplisson.fr/posts/methods/p-splines-in-r-inla/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="1344"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="rw-precision-structure" class="level1">
<h1>RW precision structure</h1>
<p>Usual choices for P-splines are RW(1) and RW(2) precision structures. They enforce smoothness while keeping the precision matrix sparse, which is one of the main computational advantages of these priors. If <img src="https://latex.codecogs.com/png.latex?%5Cpsi%20=%20(%5Calpha_1,%5Cldots,%5Calpha_K)%5E%5Ctop"> denotes the spline coefficient vector, the first- and second-order difference operators are</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AD_%7BRW1%7D%5Cpsi%20=%0A%5Cbegin%7Bpmatrix%7D%0A%5Calpha_2%20-%20%5Calpha_1%20%5C%5C%0A%5Calpha_3%20-%20%5Calpha_2%20%5C%5C%0A%5Cvdots%20%5C%5C%0A%5Calpha_K%20-%20%5Calpha_%7BK-1%7D%0A%5Cend%7Bpmatrix%7D,%0A%5Cqquad%0AD_%7BRW2%7D%5Cpsi%20=%0A%5Cbegin%7Bpmatrix%7D%0A%5Calpha_3%20-%202%5Calpha_2%20+%20%5Calpha_1%20%5C%5C%0A%5Calpha_4%20-%202%5Calpha_3%20+%20%5Calpha_2%20%5C%5C%0A%5Cvdots%20%5C%5C%0A%5Calpha_K%20-%202%5Calpha_%7BK-1%7D%20+%20%5Calpha_%7BK-2%7D%0A%5Cend%7Bpmatrix%7D.%0A"></p>
<p>The associated intrinsic precision matrices are then</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AQ_%7BRW1%7D%20=%20D_%7BRW1%7D%5E%5Ctop%20D_%7BRW1%7D,%0A%5Cqquad%0AQ_%7BRW2%7D%20=%20D_%7BRW2%7D%5E%5Ctop%20D_%7BRW2%7D.%0A"></p>
<p>In <code>R</code>, these objects can be derived directly from sparse finite-difference matrices:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">n_basis <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>(B)</span>
<span id="cb6-2">I_k <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;">Diagonal</span>(n_basis)</span>
<span id="cb6-3"></span>
<span id="cb6-4">D_rw1 <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;">diff</span>(I_k, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">differences =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb6-5">D_rw2 <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;">diff</span>(I_k, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">differences =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb6-6"></span>
<span id="cb6-7">Q_rw1 <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;">crossprod</span>(D_rw1)</span>
<span id="cb6-8">Q_rw2 <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;">crossprod</span>(D_rw2)</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(Q_rw1[<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>]))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1   -1    0    0    0    0    0    0
[2,]   -1    2   -1    0    0    0    0    0
[3,]    0   -1    2   -1    0    0    0    0
[4,]    0    0   -1    2   -1    0    0    0
[5,]    0    0    0   -1    2   -1    0    0
[6,]    0    0    0    0   -1    2   -1    0
[7,]    0    0    0    0    0   -1    2   -1
[8,]    0    0    0    0    0    0   -1    2</code></pre>
</div>
<div class="code-copy-outer-scaffold"><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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(Q_rw2[<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>]))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1   -2    1    0    0    0    0    0
[2,]   -2    5   -4    1    0    0    0    0
[3,]    1   -4    6   -4    1    0    0    0
[4,]    0    1   -4    6   -4    1    0    0
[5,]    0    0    1   -4    6   -4    1    0
[6,]    0    0    0    1   -4    6   -4    1
[7,]    0    0    0    0    1   -4    6   -4
[8,]    0    0    0    0    0    1   -4    6</code></pre>
</div>
</div>
<p>The RW1 precision penalizes successive differences in the coefficients, while the RW2 precision penalizes local curvature through second differences. Both matrices are sparse. RW1 has constant vectors in its null space, whereas RW2 has both constant and linear vectors in its null space, which is why additional constraints or an explicit intercept are usually required for identifiability.</p>
</section>
<section id="r-inla-model-component" class="level1">
<h1>R-INLA model component</h1>
<p>In matrix form, the spline contribution can be written as <img src="https://latex.codecogs.com/png.latex?B%5Cpsi">. The previous construction makes explicit the precision matrix associated with the prior on <img src="https://latex.codecogs.com/png.latex?%5Cpsi">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cpsi%5Cvert%20%5Ctau%20%5Csim%20%5Cmathcal%7BN%7D%5Cleft(0,%5B%5Ctau%20Q_%7BRW(k)%7D%5D%5E-%5Cright)%0A"></p>
<p>Here <img src="https://latex.codecogs.com/png.latex?%5B%5Ctau%20Q_%7BRW(k)%7D%5D%5E-"> denotes the generalised inverse of <img src="https://latex.codecogs.com/png.latex?%5Ctau%20Q_%7BRW(k)%7D">, with <img src="https://latex.codecogs.com/png.latex?%5Ctau"> the precision hyperparameter, equal to <img src="https://latex.codecogs.com/png.latex?1/%5Csigma%5E2">, where <img src="https://latex.codecogs.com/png.latex?%5Csigma"> is the standard deviation. Such custom latent components can be specified in R-INLA using the <code>Generic0</code> latent component, described <a href="https://inla.r-inla-download.org/r-inla.org/doc/latent/generic0.pdf">here</a>.</p>
</section>
<section id="fit-with-inlabru" class="level1">
<h1>Fit with inlabru</h1>
<p>The <code>generic0</code> latent model takes a structure matrix <code>Cmatrix</code> and internally uses the precision form <img src="https://latex.codecogs.com/png.latex?Q%20=%20%5Ctau%20C">. Here we simply supply <code>Q_rw2</code> as the structure matrix. On the <code>inlabru</code> side, <code>bm_matrix(n_basis)</code> tells the component system to interpret the matrix-valued input as a direct matrix multiplication between the supplied basis matrix and the latent state vector. Since CAC 40 closing values are strictly positive, I use a Gamma likelihood instead of a Gaussian one. INLA’s Gamma likelihood uses a default log-link for the mean, so the spline acts on the log-mean scale. For the precision hyperparameter, I use a PC prior of the form <img src="https://latex.codecogs.com/png.latex?%5CPr(%5Csigma%20%3E%20u)%20=%20%5Calpha">, implemented in INLA through <code>prior = "pc.prec"</code>.</p>
<p>To learn more about PC-priors, see:</p>
<ul>
<li>Daniel Simpson, Håvard Rue, Andrea Riebler, Thiago G. Martins, Sigrunn H. Sørbye, <em>Penalising Model Component Complexity: A Principled, Practical Approach to Constructing Priors</em>, DOI: 10.1214/16-STS576</li>
<li>Daniel Simpson’s blog post: <a href="https://dansblog.netlify.app/#category=PC%20priors" class="uri">https://dansblog.netlify.app/#category=PC%20priors</a></li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><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">pc_prec <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="cb10-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pc.prec"</span>,</span>
<span id="cb10-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">param =</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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb10-4">)</span>
<span id="cb10-5"></span>
<span id="cb10-6">cmp_ps_generic0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">~</span></span>
<span id="cb10-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Intercept</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb10-8">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prec.linear =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb10-9">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean.linear =</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>
<span id="cb10-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pspline</span>(</span>
<span id="cb10-11">    B,</span>
<span id="cb10-12">    <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;">"generic0"</span>,</span>
<span id="cb10-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapper =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bm_matrix</span>(n_basis),</span>
<span id="cb10-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Cmatrix =</span> Q_rw2,</span>
<span id="cb10-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rankdef =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb10-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">constr =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb10-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diagonal =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-06</span>,</span>
<span id="cb10-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hyper =</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;">prec =</span> pc_prec)</span>
<span id="cb10-19">  )</span>
<span id="cb10-20"></span>
<span id="cb10-21">lik_ps_generic0 <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;">bru_obs</span>(</span>
<span id="cb10-22">  y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pspline,</span>
<span id="cb10-23">  <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;">"gamma"</span>,</span>
<span id="cb10-24">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y)</span>
<span id="cb10-25">)</span>
<span id="cb10-26"></span>
<span id="cb10-27">fit_ps_generic0_bru <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;">bru</span>(</span>
<span id="cb10-28">  cmp_ps_generic0,</span>
<span id="cb10-29">  lik_ps_generic0</span>
<span id="cb10-30">)</span></code></pre></div></div>
</div>
<p>The arguments <code>rankdef = 2</code> and <code>constr = TRUE</code> reflect the intrinsic rank deficiency of the RW2 precision matrix. The small diagonal inflation is a standard numerical stabilisation for intrinsic models represented through <code>generic0</code>. The PC prior above is interpreted as <img src="https://latex.codecogs.com/png.latex?%5CPr(%5Csigma_%7B%5Ctext%7Bpspline%7D%7D%20%3E%205)%20=%200.01">.</p>
</section>
<section id="posterior-predictive-check" class="level1">
<h1>Posterior predictive check</h1>
<p>Let us perform a simple posterior predictive check by comparing the observed series with the posterior fitted band on the response scale.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">obs_idx <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;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(y))</span>
<span id="cb11-2">fitted_obs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fit_ps_generic0_bru<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary.fitted.values[obs_idx, , drop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>]</span>
<span id="cb11-3"></span>
<span id="cb11-4">ppc_df <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;">data.frame</span>(</span>
<span id="cb11-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x,</span>
<span id="cb11-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y,</span>
<span id="cb11-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fitted_mean =</span> fitted_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean,</span>
<span id="cb11-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fitted_q025 =</span> fitted_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</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;">0.025quant</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,</span>
<span id="cb11-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fitted_q975 =</span> fitted_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</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;">0.975quant</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb11-10">)</span>
<span id="cb11-11"></span>
<span id="cb11-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(ppc_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_ribbon</span>(</span>
<span id="cb11-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(</span>
<span id="cb11-15">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> fitted_q025,</span>
<span id="cb11-16">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> fitted_q975,</span>
<span id="cb11-17">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"95% credible band"</span></span>
<span id="cb11-18">    ),</span>
<span id="cb11-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span></span>
<span id="cb11-20">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(</span>
<span id="cb11-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> fitted_mean, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Posterior mean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Posterior mean"</span>),</span>
<span id="cb11-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span></span>
<span id="cb11-24">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(</span>
<span id="cb11-26">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> fitted_q025, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lower 95% bound"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lower 95% bound"</span>),</span>
<span id="cb11-27">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb11-28">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(</span>
<span id="cb11-30">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> fitted_q975, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Upper 95% bound"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Upper 95% bound"</span>),</span>
<span id="cb11-31">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb11-32">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(</span>
<span id="cb11-34">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observed"</span>),</span>
<span id="cb11-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span>,</span>
<span id="cb11-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span></span>
<span id="cb11-37">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_manual</span>(</span>
<span id="cb11-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb11-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb11-41">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observed"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>,</span>
<span id="cb11-42">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Posterior mean"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#08519c"</span>,</span>
<span id="cb11-43">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lower 95% bound"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#3182bd"</span>,</span>
<span id="cb11-44">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Upper 95% bound"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#3182bd"</span></span>
<span id="cb11-45">    ),</span>
<span id="cb11-46">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</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;">"Observed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Posterior mean"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lower 95% bound"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Upper 95% bound"</span>)</span>
<span id="cb11-47">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-48">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_linetype_manual</span>(</span>
<span id="cb11-49">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb11-50">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb11-51">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observed"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>,</span>
<span id="cb11-52">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Posterior mean"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>,</span>
<span id="cb11-53">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lower 95% bound"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>,</span>
<span id="cb11-54">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Upper 95% bound"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dotdash"</span></span>
<span id="cb11-55">    ),</span>
<span id="cb11-56">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</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;">"Observed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Posterior mean"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lower 95% bound"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Upper 95% bound"</span>)</span>
<span id="cb11-57">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-58">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(</span>
<span id="cb11-59">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb11-60">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</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;">"95% credible band"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#9ecae1"</span>)</span>
<span id="cb11-61">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-62">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</span>(</span>
<span id="cb11-63">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guide_legend</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb11-64">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guide_legend</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb11-65">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guide_legend</span>(</span>
<span id="cb11-66">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb11-67">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">override.aes =</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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span>)</span>
<span id="cb11-68">    )</span>
<span id="cb11-69">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-70">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb11-71">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Trading-day index"</span>,</span>
<span id="cb11-72">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CAC 40 close"</span>,</span>
<span id="cb11-73">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observed values and posterior fitted band"</span></span>
<span id="cb11-74">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-75">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-76">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb11-77">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>,</span>
<span id="cb11-78">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.box =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vertical"</span></span>
<span id="cb11-79">  )</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://olivier-supplisson.fr/posts/methods/p-splines-in-r-inla/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="1344"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="other-competing-possibilities-within-r-inla-rw2-prw2-and-spde" class="level1">
<h1>Other competing possibilities within R-INLA: RW2, PRW2, and SPDE</h1>
<p>R-INLA offers a wide range of latent effects related to the P-spline construction presented above for temporal modelling. Below I briefly highlight three possibilities:</p>
<ul>
<li>RW2 latent effect, which differs from the previous P-spline construction in that there is no spline basis (see <a href="https://inla.r-inla-download.org/r-inla.org/doc/latent/rw2.pdf" class="uri">https://inla.r-inla-download.org/r-inla.org/doc/latent/rw2.pdf</a>);</li>
<li>PRW2, which is the non-intrinsic version of the RW2 latent effect (see <a href="https://inla.r-inla-download.org/r-inla.org/doc/latent/prw2.pdf" class="uri">https://inla.r-inla-download.org/r-inla.org/doc/latent/prw2.pdf</a>);</li>
<li>Gaussian process implemented using the finite element representation presented in Finn Lindgren,&nbsp;Håvard Rue,&nbsp;Johan Lindström 2011 article <em>An explicit link between Gaussian fields and Gaussian Markov random fields: the stochastic partial differential equation approach</em>, https://doi.org/10.1111/j.1467-9868.2011.00777.x</li>
</ul>
<p>Other latent effects, such as AR(1), AR(p), or Fractional Gaussian Noise are also available, see <a href="https://inla.r-inla-download.org/r-inla.org/doc/latent/" class="uri">https://inla.r-inla-download.org/r-inla.org/doc/latent/</a>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Case 1: direct RW2 latent effect</span></span>
<span id="cb12-2">cmp_ps_rw2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">~</span></span>
<span id="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Intercept</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb12-4">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prec.linear =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb12-5">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean.linear =</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>
<span id="cb12-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rw2</span>(</span>
<span id="cb12-7">    x,</span>
<span id="cb12-8">    <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;">"rw2"</span>,</span>
<span id="cb12-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rankdef =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb12-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">constr =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb12-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diagonal =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-06</span>,</span>
<span id="cb12-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hyper =</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;">prec =</span> pc_prec)</span>
<span id="cb12-13">  )</span>
<span id="cb12-14">lik_rw2 <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;">bru_obs</span>(</span>
<span id="cb12-15">  y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rw2,</span>
<span id="cb12-16">  <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;">"gamma"</span>,</span>
<span id="cb12-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x)</span>
<span id="cb12-18">)</span>
<span id="cb12-19">fit_ps_rw2_bru <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;">bru</span>(</span>
<span id="cb12-20">  cmp_ps_rw2,</span>
<span id="cb12-21">  lik_rw2</span>
<span id="cb12-22">)</span>
<span id="cb12-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Case 2: PRW2 latent effect</span></span>
<span id="cb12-24">pc_prw2_range <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="cb12-25">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pc.prw2.range"</span>,</span>
<span id="cb12-26">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">param =</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;">25</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb12-27">)</span>
<span id="cb12-28">cmp_ps_prw2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">~</span></span>
<span id="cb12-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Intercept</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb12-30">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prec.linear =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb12-31">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean.linear =</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>
<span id="cb12-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prw2</span>(</span>
<span id="cb12-33">    x,</span>
<span id="cb12-34">    <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;">"prw2"</span>,</span>
<span id="cb12-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diagonal =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-06</span>,</span>
<span id="cb12-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hyper =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb12-37">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prec =</span> pc_prec,</span>
<span id="cb12-38">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">range =</span> pc_prw2_range</span>
<span id="cb12-39">    )</span>
<span id="cb12-40">  )</span>
<span id="cb12-41">lik_prw2 <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;">bru_obs</span>(</span>
<span id="cb12-42">  y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> prw2,</span>
<span id="cb12-43">  <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;">"gamma"</span>,</span>
<span id="cb12-44">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x)</span>
<span id="cb12-45">)</span>
<span id="cb12-46">fit_ps_prw2_bru <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;">bru</span>(</span>
<span id="cb12-47">  cmp_ps_prw2,</span>
<span id="cb12-48">  lik_prw2</span>
<span id="cb12-49">)</span>
<span id="cb12-50"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Case 3: Gaussian process</span></span>
<span id="cb12-51">mesh1d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fmesher<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;">fm_mesh_1d</span>(</span>
<span id="cb12-52">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(x) <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;">5</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(x) <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;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>),</span>
<span id="cb12-53">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">boundary =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free"</span></span>
<span id="cb12-54">)</span>
<span id="cb12-55">matern_1d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> INLA<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;">inla.spde2.pcmatern</span>(</span>
<span id="cb12-56">  mesh1d,</span>
<span id="cb12-57">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior.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;">25</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>),</span>
<span id="cb12-58">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior.sigma =</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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>),</span>
<span id="cb12-59">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">constr =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb12-60">)</span>
<span id="cb12-61">cmp_ps_gp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">~</span></span>
<span id="cb12-62">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Intercept</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb12-63">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prec.linear =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb12-64">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean.linear =</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>
<span id="cb12-65">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gp</span>(</span>
<span id="cb12-66">    x,</span>
<span id="cb12-67">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> matern_1d</span>
<span id="cb12-68">  )</span>
<span id="cb12-69">lik_gp <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;">bru_obs</span>(</span>
<span id="cb12-70">  y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> gp,</span>
<span id="cb12-71">  <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;">"gamma"</span>,</span>
<span id="cb12-72">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x),</span>
<span id="cb12-73">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">domain =</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> mesh1d)</span>
<span id="cb12-74">)</span>
<span id="cb12-75">fit_ps_gp_bru <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;">bru</span>(</span>
<span id="cb12-76">  cmp_ps_gp,</span>
<span id="cb12-77">  lik_gp</span>
<span id="cb12-78">)</span></code></pre></div></div>
</div>
<p>The posterior mean fitted values from the four approaches can be compared directly on the response scale.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">obs_idx <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;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(y))</span>
<span id="cb13-2"></span>
<span id="cb13-3">extract_obs_mean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(fit) {</span>
<span id="cb13-4">  fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary.fitted.values[obs_idx, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>]</span>
<span id="cb13-5">}</span>
<span id="cb13-6"></span>
<span id="cb13-7">fit_compare_df <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;">data.frame</span>(</span>
<span id="cb13-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x,</span>
<span id="cb13-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Observed =</span> y,</span>
<span id="cb13-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Generic0 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_obs_mean</span>(fit_ps_generic0_bru),</span>
<span id="cb13-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">RW2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_obs_mean</span>(fit_ps_rw2_bru),</span>
<span id="cb13-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">PRW2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_obs_mean</span>(fit_ps_prw2_bru),</span>
<span id="cb13-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GP =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_obs_mean</span>(fit_ps_gp_bru)</span>
<span id="cb13-14">)</span>
<span id="cb13-15"></span>
<span id="cb13-16">fit_compare_long <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;">rbind</span>(</span>
<span id="cb13-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">series =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> fit_compare_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Observed),</span>
<span id="cb13-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">series =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Generic0"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> fit_compare_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Generic0),</span>
<span id="cb13-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">series =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RW2"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> fit_compare_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>RW2),</span>
<span id="cb13-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">series =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PRW2"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> fit_compare_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>PRW2),</span>
<span id="cb13-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">series =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gaussian process"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> fit_compare_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>GP)</span>
<span id="cb13-22">)</span>
<span id="cb13-23"></span>
<span id="cb13-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(fit_compare_long, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> series, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> series, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> series)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</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>
<span id="cb13-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_manual</span>(</span>
<span id="cb13-27">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb13-28">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observed"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>,</span>
<span id="cb13-29">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Generic0"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#08519c"</span>,</span>
<span id="cb13-30">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RW2"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2ca25f"</span>,</span>
<span id="cb13-31">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PRW2"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#cb181d"</span>,</span>
<span id="cb13-32">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gaussian process"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#756bb1"</span></span>
<span id="cb13-33">    )</span>
<span id="cb13-34">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_linetype_manual</span>(</span>
<span id="cb13-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb13-37">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observed"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>,</span>
<span id="cb13-38">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Generic0"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>,</span>
<span id="cb13-39">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RW2"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"longdash"</span>,</span>
<span id="cb13-40">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PRW2"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dotdash"</span>,</span>
<span id="cb13-41">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gaussian process"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"twodash"</span></span>
<span id="cb13-42">    )</span>
<span id="cb13-43">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_alpha_manual</span>(</span>
<span id="cb13-45">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb13-46">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observed"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>,</span>
<span id="cb13-47">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Generic0"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb13-48">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RW2"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb13-49">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PRW2"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb13-50">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gaussian process"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb13-51">    )</span>
<span id="cb13-52">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-53">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb13-54">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Trading-day index"</span>,</span>
<span id="cb13-55">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CAC 40 close"</span>,</span>
<span id="cb13-56">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb13-57">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb13-58">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Posterior mean comparison across four latent-effect choices"</span></span>
<span id="cb13-59">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-60">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-61">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb13-62">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>,</span>
<span id="cb13-63">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.box =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vertical"</span></span>
<span id="cb13-64">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-65">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://olivier-supplisson.fr/posts/methods/p-splines-in-r-inla/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="1344"></p>
</figure>
</div>
</div>
</div>
<p>The negligible differences can be explained primarily by how each effect approximates the continuous target function. The usual P-spline uses 80 basis functions to define the approximation, much like the SPDE approximation of the Gaussian process, which relies on a mesh with a comparable resolution. By contrast, the RW2 and PRW2 effects use one latent value for each trading-day index.</p>


</section>

 ]]></description>
  <category>Methods</category>
  <guid>https://olivier-supplisson.fr/posts/methods/p-splines-in-r-inla/</guid>
  <pubDate>Sun, 12 Apr 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Welcome!</title>
  <dc:creator>Olivier Supplisson</dc:creator>
  <link>https://olivier-supplisson.fr/posts/news/welcome/</link>
  <description><![CDATA[ 





<p>Welcome to my brand-new blog. If you would like to know more about my academic and professional background, you can visit my <a href="https://www.linkedin.com/in/oliviersupplisson/">LinkedIn</a> or read my <a href="../../../cv.html">CV</a>. I hope you will find the posts useful.</p>
<p>You can reach out directly on LinkedIn or by email at <code>osupplis@gmail.com</code>.</p>



 ]]></description>
  <category>News</category>
  <guid>https://olivier-supplisson.fr/posts/news/welcome/</guid>
  <pubDate>Sun, 12 Apr 2026 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
