<?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>Julius&#39;s Blog - R Posts</title>
<link>https://jbogomolovas2.github.io/Julius-s-Blog/r-posts.html</link>
<atom:link href="https://jbogomolovas2.github.io/Julius-s-Blog/r-posts.xml" rel="self" type="application/rss+xml"/>
<description>Data Science, Swimming Analytics, and R Programming</description>
<generator>quarto-1.7.32</generator>
<lastBuildDate>Sun, 24 May 2026 07:00:00 GMT</lastBuildDate>
<item>
  <title>Conway–Maxwell–Binomial regression: two-directional dispersion for bounded counts</title>
  <dc:creator>Julius Bogomolovas</dc:creator>
  <link>https://jbogomolovas2.github.io/Julius-s-Blog/posts/CMB_regression/</link>
  <description><![CDATA[ 





<section id="how-i-got-here" class="level2">
<h2 class="anchored" data-anchor-id="how-i-got-here">How I got here</h2>
<p>For a long time I was fitting cell proliferation counts with Conway-Maxwell-Poisson and a <code>log(n)</code> offset. It worked fine. Counts behaved like rates, the dispersion parameter ν gave me a handle on whether the data were over or underdispersed relative to Poisson, and the offset turned an unbounded count distribution into something that read like a proportion model. Standard applied stats move, nothing exotic.</p>
<p>Then I tried to build a simulator. In the <a href="https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/">previous post</a> I wrote a CMP-with-offset data generator so I could check coverage and power before running real experiments. And the simulator started producing counts above the biological ceiling. Cells dividing more times than there are cells. The unbounded count distribution does not know my data has an upper bound, and the offset trick does not enforce one.</p>
<p>In my real data this had not bitten me because proliferation rarely hits the ceiling. So the bound was a silent assumption I had been getting away with for years. The simulator surfaced it.</p>
<p>That sent me reading. The right answer for bounded counts with flexible dispersion is the Conway-Maxwell-Binomial distribution, the bounded analogue of CMP. It spans both dispersion directions through the same ν parameter, and it respects the bound by construction.</p>
<p>The puzzle is why CMB is not a standard tool. Underdispersion in proportion data is a known headache and the workarounds in common use are all unsatisfying. The oldest is the arcsine square-root transformation: apply a variance-stabilizing transform to the observed proportions and fit a Gaussian regression on the transformed values. This throws away the bounded discrete structure of the data, produces coefficients on a scale that does not map cleanly back to rates, and offers no way to model dispersion separately from the mean. The coral fertilization paper we work through later in this post takes exactly this route, and we will see why it leaves a real biological signal on the table. A second workaround is quasi-binomial, which allows the dispersion parameter to fall below 1 but does so without a proper likelihood behind it, so AIC comparisons, likelihood ratio tests, and random effects are all off the table. A third option is to fit a binomial GLM and ignore the underdispersion, accepting standard errors that are wrong in a direction that is hard to predict from the output alone. None of these is what someone would reach for if a clean alternative were sitting in their toolkit.</p>
<p>R support for CMB is thin. The <a href="https://rdrr.io/github/andrewraim/COMMultReg/">COMMultReg package</a> by Andrew Raim <span class="citation" data-cites="COMMultReg">(Raim, n.d.)</span> provides distribution-level utilities for CMB (density, sampling, normalizing constant, mean, variance) and a full regression interface for the <em>multinomial</em> generalization. The package name is honest about its scope: Conway-Maxwell-<em>Multinomial</em> Regression. The binomial case sits in the distribution module only, in the canonical <code>(m, p, ν)</code> parameterization <span class="citation" data-cites="kadane2016cmb">(Kadane 2016)</span>, with no regression interface, no formula syntax, no dispersion submodel, and no random effects.</p>
<p>So that is what I built. CMB now lives on two public forks: the <a href="https://github.com/jbogomolovas2/adcomp/tree/cmb-family">TMB-side family definition</a> with the auto-differentiable likelihood, and the <a href="https://github.com/jbogomolovas2/glmmTMB/tree/cmb-family">glmmTMB-side integration</a> that wires it into the formula interface so you can write <code>family = compbinomial, dispformula = ~ ...</code> like any other family. It is not on CRAN. Maybe one day. For now if you want to follow along you install from those forks.</p>
<p>This post is two things. First, a short explanation of why CMB is the right tool when your data are bounded and your dispersion is heterogeneous or two-sided. Second, two worked examples: a simulated three-group dataset where you can see the machinery work in controlled conditions, and a real coral fertilization study where CMB sharpens the biological finding the original analysis could not formally test.</p>
</section>
<section id="the-dispersion-landscape" class="level2">
<h2 class="anchored" data-anchor-id="the-dispersion-landscape">The dispersion landscape</h2>
<p>A short conceptual section before code.</p>
<p>You have a bounded count: <img src="https://latex.codecogs.com/png.latex?y"> successes out of <img src="https://latex.codecogs.com/png.latex?n"> trials. The default model is binomial, which fixes the variance at <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(y)%20=%20np(1-p)"> once you condition on the mean. Real data are often more variable than this, sometimes less. The deviation from binomial variance, in either direction, is what people call dispersion.</p>
<p>The standard fix for <em>more</em> variance than binomial is the beta-binomial. It writes the variance as</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(y)%20=%20np(1-p)%5C,%5Cbigl%5B1%20+%20(n-1)%5Crho%5Cbigr%5D,%20%5Cquad%20%5Crho%20%5Cin%20%5B0,%201)."></p>
<p>The dispersion parameter <img src="https://latex.codecogs.com/png.latex?%5Crho"> inflates the binomial variance. When <img src="https://latex.codecogs.com/png.latex?%5Crho%20=%200"> you recover binomial. When <img src="https://latex.codecogs.com/png.latex?%5Crho%20%5Cto%201"> the variance approaches the maximum possible for a bounded count. So beta-binomial covers everything from binomial up to the upper bound.</p>
<p>What it does not cover is anything below the binomial line. The constraint <img src="https://latex.codecogs.com/png.latex?%5Crho%20%5Cgeq%200"> is not a numerical artifact, it is a property of the family. If your data are <em>less</em> variable than binomial, the BB likelihood pushes <img src="https://latex.codecogs.com/png.latex?%5Crho"> against zero and either pegs there (silently behaving as binomial) or fails to converge with a non-positive-definite Hessian. There is no way to wiggle through it.</p>
<p>The Conway-Maxwell-Binomial family does cover both sides. Its variance, approximately, behaves like <img src="https://latex.codecogs.com/png.latex?np(1-p)/%5Cnu">, with the dispersion parameter <img src="https://latex.codecogs.com/png.latex?%5Cnu"> on a multiplicative scale: <img src="https://latex.codecogs.com/png.latex?%5Cnu%20=%201"> is binomial, <img src="https://latex.codecogs.com/png.latex?%5Cnu%20%3C%201"> is overdispersed (more variance), <img src="https://latex.codecogs.com/png.latex?%5Cnu%20%3E%201"> is underdispersed (less variance). One parameter, two directions, the bound preserved.</p>
<p>The other natural alternative for rate-like data, CMP with a <code>log(n)</code> offset, fails for a different reason. It is an unbounded count distribution, so the bound is not part of the model. In situations where counts rarely approach <img src="https://latex.codecogs.com/png.latex?n"> the offset trick can be a decent approximation, but it is not a principled choice for bounded data and it breaks at the ceiling.</p>
<p>A picture of what these three families look like for the same mean and different ν values:</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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">n  <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;">20</span></span>
<span id="cb1-2">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;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># COMMultReg uses canonical (m, p, nu) parameterization. At p = 0.5 the</span></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CMB pmf is symmetric about n/2 for any nu, so the mean equals n/2 = 10</span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># in all three regimes without inverting the mean-p relation.</span></span>
<span id="cb1-7">p_over  <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;">sapply</span>(k, <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;">d_cmb</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nu =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>))</span>
<span id="cb1-8">p_bin   <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;">dbinom</span>(k, n, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb1-9">p_under <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;">sapply</span>(k, <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;">d_cmb</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nu =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>))</span>
<span id="cb1-10"></span>
<span id="cb1-11">df_pmf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> 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;">bind_rows</span>(</span>
<span id="cb1-12">  tibble<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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> k, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pmf =</span> p_over,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">regime =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"overdispersed (ν &lt; 1)"</span>),</span>
<span id="cb1-13">  tibble<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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> k, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pmf =</span> p_bin,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">regime =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binomial (ν = 1)"</span>),</span>
<span id="cb1-14">  tibble<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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> k, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pmf =</span> p_under, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">regime =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"underdispersed (ν &gt; 1)"</span>)</span>
<span id="cb1-15">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-16">  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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">regime =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(regime, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb1-17">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"overdispersed (ν &lt; 1)"</span>,</span>
<span id="cb1-18">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binomial (ν = 1)"</span>,</span>
<span id="cb1-19">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"underdispersed (ν &gt; 1)"</span></span>
<span id="cb1-20">  )))</span>
<span id="cb1-21"></span>
<span id="cb1-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df_pmf, <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> k, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> pmf, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> regime)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-24">  <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;">group =</span> regime), <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.6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-25">  <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> <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;">"#d7191c"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey30"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2c7bb6"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"y (successes out of n = 20)"</span>,</span>
<span id="cb1-27">       <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;">"probability mass"</span>,</span>
<span id="cb1-28">       <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;">"Same mean rate, three dispersion regimes"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/CMB_regression/index_files/figure-html/dispersion-densities-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>CMB pmf for n = 20 at mean rate 0.5, under three dispersion regimes. The binomial baseline (ν = 1) is shown in grey. Overdispersion (ν = 0.5) flattens the mass relative to binomial. Underdispersion (ν = 2) concentrates it around the mean. Densities computed with <code>COMMultReg::d_cmb</code>.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="the-implementation" class="level2">
<h2 class="anchored" data-anchor-id="the-implementation">The implementation</h2>
<p>Two things needed to happen to get CMB working as a regression family in R.</p>
<section id="mean-parameterization" class="level3">
<h3 class="anchored" data-anchor-id="mean-parameterization">Mean parameterization</h3>
<p>The standard CMB pmf <span class="citation" data-cites="kadane2016cmb">(Kadane 2016)</span> is</p>
<p><img src="https://latex.codecogs.com/png.latex?P(X%20=%20k%20%5Cmid%20n,%20p,%20%5Cnu)%20=%20%5Cfrac%7B1%7D%7BZ(n,%20p,%20%5Cnu)%7D%5Cbinom%7Bn%7D%7Bk%7D%5E%5Cnu%20p%5Ek%20(1-p)%5E%7Bn-k%7D"></p>
<p>with normalizing constant <img src="https://latex.codecogs.com/png.latex?Z(n,%20p,%20%5Cnu)%20=%20%5Csum_%7Bj=0%7D%5E%7Bn%7D%20%5Cbinom%7Bn%7D%7Bj%7D%5E%5Cnu%20p%5Ej%20(1-p)%5E%7Bn-j%7D">. This is a finite sum, which matters: unlike the CMP normalizing constant, the CMB version does not need truncation or special-function approximations.</p>
<p>The catch is the role of <img src="https://latex.codecogs.com/png.latex?p">. When <img src="https://latex.codecogs.com/png.latex?%5Cnu%20=%201"> the family reduces to binomial and <img src="https://latex.codecogs.com/png.latex?p"> equals the mean rate. When <img src="https://latex.codecogs.com/png.latex?%5Cnu%20%5Cneq%201">, <img src="https://latex.codecogs.com/png.latex?p"> is just a canonical parameter and the mean <img src="https://latex.codecogs.com/png.latex?%5Cmu%20=%20E%5BX%5D/n"> depends on both <img src="https://latex.codecogs.com/png.latex?p"> and <img src="https://latex.codecogs.com/png.latex?%5Cnu"> together. There is no closed-form expression. This makes the canonical parameterization unusable for regression. If you wrote <code>logit(p) ~ X</code>, your coefficients would be shifts on a parameter that mixes mean and dispersion in a way the reader cannot interpret.</p>
<p>What the implementation does, following the pattern glmmTMB uses for negative binomial and beta-binomial and the mean parameterization Huang <span class="citation" data-cites="huang2017meanCMP">(Huang 2017)</span> introduced for CMP, is the <em>mean-parametrized</em> version. Given a target mean <img src="https://latex.codecogs.com/png.latex?%5Cmu"> and a dispersion <img src="https://latex.codecogs.com/png.latex?%5Cnu">, solve numerically for the <img src="https://latex.codecogs.com/png.latex?p"> that satisfies <img src="https://latex.codecogs.com/png.latex?E%5BX%20%5Cmid%20n,%20p,%20%5Cnu%5D%20=%20n%5Cmu">, then use that <img src="https://latex.codecogs.com/png.latex?p"> inside the likelihood. The user-facing model is then:</p>
<pre><code>logit(μ) = Xβ        # mean rate, modeled like any binomial-family GLM
log(ν)   = Zγ        # dispersion, modeled separately</code></pre>
<p>The Newton iteration to solve for <img src="https://latex.codecogs.com/png.latex?p"> runs at every likelihood evaluation. This is fine because TMB handles the derivatives by automatic differentiation, so the gradient of the marginal likelihood propagates through the inner solve without any hand-coded chain rule. This is the main reason I built on TMB rather than coding the family in plain R.</p>
</section>
<section id="family-registration-on-the-tmb-side" class="level3">
<h3 class="anchored" data-anchor-id="family-registration-on-the-tmb-side">Family registration on the TMB side</h3>
<p>The <code>compbinomial</code> family is added to TMB’s family list with its own log-density function, then exposed to glmmTMB through its dispatch. Once registered it inherits everything: random effects, dispformula, zero-inflation if you want it, the standard <code>fixef()</code> / <code>ranef()</code> / <code>vcov()</code> accessors. The model code looks identical to any other glmmTMB fit:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install from forks (one-time)</span></span>
<span id="cb3-2">remotes<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;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jbogomolovas2/adcomp"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ref =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cmb-family"</span>,</span>
<span id="cb3-3">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subdir =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TMB"</span>)</span>
<span id="cb3-4">remotes<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;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jbogomolovas2/glmmTMB"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ref =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cmb-family"</span>,</span>
<span id="cb3-5">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subdir =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"glmmTMB"</span>)</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Then use it like any glmmTMB family</span></span>
<span id="cb3-8">fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(y, n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> id),</span>
<span id="cb3-9">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dispformula =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group,</span>
<span id="cb3-10">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family      =</span> compbinomial,</span>
<span id="cb3-11">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data        =</span> my_data)</span></code></pre></div>
</div>
<p>That’s the whole interface. From the user’s side it is just another family. The work was making it behave that way.</p>
</section>
</section>
<section id="a-simulated-three-group-example" class="level2">
<h2 class="anchored" data-anchor-id="a-simulated-three-group-example">A simulated three-group example</h2>
<p>The cleanest way to see the families fail and succeed is to put them on data where we control the truth. We simulate three groups of observations from CMB itself, with the same trial size and the same mean rate in each group, but with three different dispersion parameters: ν = 0.5 (overdispersed), ν = 1 (binomial), ν = 2 (underdispersed). Each group has sixty observations of <code>y</code> out of <code>n = 30</code> trials. We set the canonical parameter <code>p = 0.5</code> so the CMB distribution is symmetric about <code>n/2</code> for any ν, which puts the true mean at exactly <code>15</code> in all three groups without needing to invert the mean-p relation. We then fit four models on the pooled data and compare them: binomial (no dispersion submodel), beta-binomial with <code>dispformula = ~ group</code>, CMP-with-offset with <code>dispformula = ~ group</code>, and CMB with <code>dispformula = ~ group</code>.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(COMMultReg)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(glmmTMB)</span>
<span id="cb4-3"></span>
<span id="cb4-4"><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;">42</span>)</span>
<span id="cb4-5"></span>
<span id="cb4-6">n_trials  <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;">30</span></span>
<span id="cb4-7">n_per_grp <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;">60</span></span>
<span id="cb4-8">p_true    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb4-9">nu_true   <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;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">over =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binomial =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">under =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>)</span>
<span id="cb4-10"></span>
<span id="cb4-11">sim_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> 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;">bind_rows</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(nu_true), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(g) {</span>
<span id="cb4-12">  tibble<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;">tibble</span>(</span>
<span id="cb4-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> g,</span>
<span id="cb4-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n     =</span> n_trials,</span>
<span id="cb4-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">r_cmb</span>(n_per_grp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m =</span> n_trials, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> p_true, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nu =</span> nu_true[[g]])</span>
<span id="cb4-16">  )</span>
<span id="cb4-17">})) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-18">  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="cb4-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"over"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binomial"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"under"</span>)),</span>
<span id="cb4-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">miss  =</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y</span>
<span id="cb4-21">  )</span>
<span id="cb4-22"></span>
<span id="cb4-23">sim_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-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;">group_by</span>(group) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-25">  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;">summarise</span>(</span>
<span id="cb4-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_obs            =</span> 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="cb4-27">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_rate        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n),</span>
<span id="cb4-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">obs_var          =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">var</span>(y),</span>
<span id="cb4-29">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binomial_var     =</span> n_trials <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>(y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<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;">mean</span>(y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>n)),</span>
<span id="cb4-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var_ratio        =</span> obs_var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> binomial_var,</span>
<span id="cb4-31">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups          =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span></span>
<span id="cb4-32">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-33">  knitr<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;">kable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">group</th>
<th style="text-align: right;">n_obs</th>
<th style="text-align: right;">mean_rate</th>
<th style="text-align: right;">obs_var</th>
<th style="text-align: right;">binomial_var</th>
<th style="text-align: right;">var_ratio</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">over</td>
<td style="text-align: right;">60</td>
<td style="text-align: right;">0.528</td>
<td style="text-align: right;">17.68898</td>
<td style="text-align: right;">7.476</td>
<td style="text-align: right;">2.3661290</td>
</tr>
<tr class="even">
<td style="text-align: left;">binomial</td>
<td style="text-align: right;">60</td>
<td style="text-align: right;">0.487</td>
<td style="text-align: right;">10.44379</td>
<td style="text-align: right;">7.495</td>
<td style="text-align: right;">1.3934147</td>
</tr>
<tr class="odd">
<td style="text-align: left;">under</td>
<td style="text-align: right;">60</td>
<td style="text-align: right;">0.504</td>
<td style="text-align: right;">2.79548</td>
<td style="text-align: right;">7.499</td>
<td style="text-align: right;">0.3727601</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The last column is the empirical analogue of 1/ν. We expect about 2.0 for the overdispersed group, 1.0 for binomial, and 0.5 for the underdispersed group. The observed ratios (2.37, 1.39, 0.37) match the expected pattern, with the binomial group running a little high by chance at this sample size.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(sim_data, <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> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_jitter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <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;">"grey20"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<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;">percent_format</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</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="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_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="at" style="color: #657422;
background-color: null;
font-style: inherit;">over =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#d7191c"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binomial =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey30"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">under =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2c7bb6"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"observed rate"</span>, <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;">"Simulated rates by group"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-6">  <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;">"none"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/CMB_regression/index_files/figure-html/sim-quick-look-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>Simulated y / n by group. The overdispersed group has visibly more spread than the binomial baseline; the underdispersed group has visibly less.</figcaption>
</figure>
</div>
</div>
</div>
<section id="fitting-the-four-candidates" class="level3">
<h3 class="anchored" data-anchor-id="fitting-the-four-candidates">Fitting the four candidates</h3>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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">safe_fit <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>(expr) {</span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tryCatch</span>(</span>
<span id="cb6-3">    <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;">fit =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eval.parent</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substitute</span>(expr)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_character_</span>),</span>
<span id="cb6-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <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;">fit =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">conditionMessage</span>(e))</span>
<span id="cb6-5">  )</span>
<span id="cb6-6">}</span>
<span id="cb6-7"></span>
<span id="cb6-8">form_bound <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;">cbind</span>(y, miss) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group</span>
<span id="cb6-9">form_cmp   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group <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;">offset</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(n))</span>
<span id="cb6-10"></span>
<span id="cb6-11">fits <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="cb6-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binomial =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">safe_fit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(form_bound, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> binomial, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sim_data)),</span>
<span id="cb6-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bb       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">safe_fit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(form_bound, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> betabinomial,</span>
<span id="cb6-14">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dispformula =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sim_data)),</span>
<span id="cb6-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cmp      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">safe_fit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(form_cmp,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> compois,</span>
<span id="cb6-16">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dispformula =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sim_data)),</span>
<span id="cb6-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cmb      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">safe_fit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(form_bound, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> compbinomial,</span>
<span id="cb6-18">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dispformula =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sim_data))</span>
<span id="cb6-19">)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>Warning in finalizeTMB(TMBStruc, obj, fit, h, data.tmb.old): Model convergence
problem; non-positive-definite Hessian matrix. See vignette('troubleshooting')</code></pre>
</div>
<details class="code-fold">
<summary>Show code</summary>
<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">compare_tbl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> 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;">bind_rows</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(fits), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(nm) {</span>
<span id="cb8-2">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fits[[nm]]</span>
<span id="cb8-3">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.null</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit)) {</span>
<span id="cb8-4">    <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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> nm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AIC =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb8-5">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb8-6">  }</span>
<span id="cb8-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb8-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family    =</span> nm,</span>
<span id="cb8-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attr</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logLik</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"df"</span>),</span>
<span id="cb8-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AIC       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">AIC</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit),</span>
<span id="cb8-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">isTRUE</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sdr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pdHess),</span>
<span id="cb8-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb8-13">  )</span>
<span id="cb8-14">})) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-15">  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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dAIC =</span> AIC <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;">min</span>(AIC, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</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;">|&gt;</span></span>
<span id="cb8-16">  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;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(AIC), AIC)</span>
<span id="cb8-17"></span>
<span id="cb8-18">knitr<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;">kable</span>(compare_tbl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">family</th>
<th style="text-align: right;">df</th>
<th style="text-align: right;">AIC</th>
<th style="text-align: left;">converged</th>
<th style="text-align: right;">dAIC</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">cmb</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">894.63</td>
<td style="text-align: left;">TRUE</td>
<td style="text-align: right;">0.00</td>
</tr>
<tr class="even">
<td style="text-align: left;">cmp</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">901.93</td>
<td style="text-align: left;">TRUE</td>
<td style="text-align: right;">7.30</td>
</tr>
<tr class="odd">
<td style="text-align: left;">binomial</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">945.45</td>
<td style="text-align: left;">TRUE</td>
<td style="text-align: right;">50.81</td>
</tr>
<tr class="even">
<td style="text-align: left;">bb</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">NA</td>
<td style="text-align: left;">FALSE</td>
<td style="text-align: right;">NA</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>CMB wins. CMP-offset comes in a respectable second, a little behind. Plain binomial sits at +51 AIC, which is the expected penalty for a model with no dispersion structure at all. And beta-binomial does not finish: the row shows <code>converged = FALSE</code> and <code>AIC = NA</code>. The optimizer did not trip on a numerical artifact, it ran into the rho ≥ 0 wall and could not find any set of parameter values that simultaneously fit all three groups. BB is the delicate flower here, it wilts from the slightest whiff of underdispersion dragging the whole fit along.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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">cmb_nu_by_group <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, groups) {</span>
<span id="cb9-2">  grid <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;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(groups, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> groups))</span>
<span id="cb9-3">  X    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model.matrix</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> grid)</span>
<span id="cb9-4">  beta <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;">fixef</span>(fit)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>disp</span>
<span id="cb9-5">  V    <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;">vcov</span>(fit)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>disp</span>
<span id="cb9-6">  eta  <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.vector</span>(X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> beta)</span>
<span id="cb9-7">  se   <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;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> V <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;">t</span>(X)))</span>
<span id="cb9-8">  grid <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-9">    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="cb9-10">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nu_hat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(eta),</span>
<span id="cb9-11">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(eta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> se),</span>
<span id="cb9-12">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(eta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> se)</span>
<span id="cb9-13">    )</span>
<span id="cb9-14">}</span>
<span id="cb9-15"></span>
<span id="cb9-16">bb_rho_by_group <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, groups) {</span>
<span id="cb9-17">  grid <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;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(groups, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> groups))</span>
<span id="cb9-18">  X    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model.matrix</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> grid)</span>
<span id="cb9-19">  beta <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;">fixef</span>(fit)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>disp</span>
<span id="cb9-20">  V    <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;">vcov</span>(fit)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>disp</span>
<span id="cb9-21">  eta  <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.vector</span>(X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> beta)</span>
<span id="cb9-22">  phi  <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;">exp</span>(eta)</span>
<span id="cb9-23">  grid <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-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;">mutate</span>(</span>
<span id="cb9-25">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">phi          =</span> phi,</span>
<span id="cb9-26">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho_overdisp =</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;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> phi),</span>
<span id="cb9-27">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">at_boundary  =</span> phi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb9-28">    )</span>
<span id="cb9-29">}</span>
<span id="cb9-30"></span>
<span id="cb9-31">nu_tbl  <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;">cmb_nu_by_group</span>(fits<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cmb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit, <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;">"over"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binomial"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"under"</span>))</span>
<span id="cb9-32">nu_tbl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nu_true <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> nu_true[<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;">"over"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binomial"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"under"</span>)]</span>
<span id="cb9-33"></span>
<span id="cb9-34">rho_tbl <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;">bb_rho_by_group</span>(fits<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit, <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;">"over"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binomial"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"under"</span>))</span>
<span id="cb9-35"></span>
<span id="cb9-36">knitr<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;">kable</span>(nu_tbl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb9-37">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMB ν estimates against simulated truth"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>CMB ν estimates against simulated truth</caption>
<thead>
<tr class="header">
<th style="text-align: left;">group</th>
<th style="text-align: right;">nu_hat</th>
<th style="text-align: right;">lower</th>
<th style="text-align: right;">upper</th>
<th style="text-align: right;">nu_true</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">over</td>
<td style="text-align: right;">0.410</td>
<td style="text-align: right;">0.281</td>
<td style="text-align: right;">0.598</td>
<td style="text-align: right;">0.5</td>
</tr>
<tr class="even">
<td style="text-align: left;">binomial</td>
<td style="text-align: right;">0.714</td>
<td style="text-align: right;">0.494</td>
<td style="text-align: right;">1.030</td>
<td style="text-align: right;">1.0</td>
</tr>
<tr class="odd">
<td style="text-align: left;">under</td>
<td style="text-align: right;">2.791</td>
<td style="text-align: right;">1.947</td>
<td style="text-align: right;">4.000</td>
<td style="text-align: right;">2.0</td>
</tr>
</tbody>
</table>
</div>
<details class="code-fold">
<summary>Show code</summary>
<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">knitr<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;">kable</span>(rho_tbl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb10-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Beta-binomial ρ by group, with boundary flag"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Beta-binomial ρ by group, with boundary flag</caption>
<thead>
<tr class="header">
<th style="text-align: left;">group</th>
<th style="text-align: right;">phi</th>
<th style="text-align: right;">rho_overdisp</th>
<th style="text-align: left;">at_boundary</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">over</td>
<td style="text-align: right;">20.982</td>
<td style="text-align: right;">0.045</td>
<td style="text-align: left;">FALSE</td>
</tr>
<tr class="even">
<td style="text-align: left;">binomial</td>
<td style="text-align: right;">76.442</td>
<td style="text-align: right;">0.013</td>
<td style="text-align: left;">TRUE</td>
</tr>
<tr class="odd">
<td style="text-align: left;">under</td>
<td style="text-align: right;">62271613.949</td>
<td style="text-align: right;">0.000</td>
<td style="text-align: left;">TRUE</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>CMB recovery is decent: every 95% interval covers its simulated truth, and the point estimates land in the right neighborhood. At sixty observations per group with moderate effect sizes, that is about what we should expect.</p>
<p>The beta-binomial table tells two stories at once. On the overdispersed group, BB does its job: rho is 0.045 and the boundary flag is <code>FALSE</code>. On the binomial group, BB pushes rho down to 0.013 and trips <code>at_boundary = TRUE</code>, which is correct behavior. There is no overdispersion to estimate, so the family collapses toward the binomial limit, and that is what we want. The underdispersed group is where things break. Phi explodes to about 6 x 10^7 and rho goes to zero, not because BB has discovered “very little overdispersion” but because the optimizer is running off to infinity in search of a parameter value that does not exist in the family. Combined with the <code>converged = FALSE</code> we saw in the AIC table, this is what hitting the rho &gt;= 0 wall looks like in practice. The two boundary hits look identical in the table and mean opposite things: one is the family doing the right thing, the other is the family unable to do anything at all.</p>
<p>A picture of the recovery:</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(nu_tbl, <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> nu_hat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</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;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <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;">"grey45"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_pointrange</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;">xmin =</span> lower, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</span> upper), <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.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-4">  <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> nu_true), <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;">"#d7191c"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb11-5">             <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;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stroke =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"CMB ν estimate (log scale, with 95% CI)"</span>,</span>
<span id="cb11-8">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb11-9">       <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;">"Simulated truth (red crosses) vs CMB estimate (black)"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/CMB_regression/index_files/figure-html/sim-nu-plot-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>CMB ν estimates with 95% confidence intervals, against the simulated values (red crosses). The dashed vertical line is the binomial baseline. CMB hits all three regimes.</figcaption>
</figure>
</div>
</div>
</div>
<p>And finally, the practical payoff: what does the confidence interval on the group mean look like under each model?</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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">ci_widths <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> 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;">bind_rows</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</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;">"binomial"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bb"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cmb"</span>), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(m) {</span>
<span id="cb12-2">  f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fits[[m]]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit</span>
<span id="cb12-3">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.null</span>(f)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb12-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(emmeans<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;">emmeans</span>(f, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-5">    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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> m, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ci_width =</span> asymp.UCL <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> asymp.LCL)</span>
<span id="cb12-6">}))</span>
<span id="cb12-7"></span>
<span id="cb12-8">ci_widths <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-9">  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;">select</span>(group, model, ci_width) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-10">  tidyr<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;">pivot_wider</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> ci_width,</span>
<span id="cb12-11">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_prefix =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ci_"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-12">  knitr<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;">kable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb12-13">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"95% CI widths on the per-group mean rate"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>95% CI widths on the per-group mean rate</caption>
<thead>
<tr class="header">
<th style="text-align: left;">group</th>
<th style="text-align: right;">ci_binomial</th>
<th style="text-align: right;">ci_bb</th>
<th style="text-align: right;">ci_cmb</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">over</td>
<td style="text-align: right;">0.0461</td>
<td style="text-align: right;">0.0701</td>
<td style="text-align: right;">0.0701</td>
</tr>
<tr class="even">
<td style="text-align: left;">binomial</td>
<td style="text-align: right;">0.0461</td>
<td style="text-align: right;">0.0541</td>
<td style="text-align: right;">0.0542</td>
</tr>
<tr class="odd">
<td style="text-align: left;">under</td>
<td style="text-align: right;">0.0462</td>
<td style="text-align: right;">0.0462</td>
<td style="text-align: right;">0.0279</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Same data, three stories. If the group is overdispersed, BB and CMB both recognize it and widen the CI. If the group is underdispersed, only CMB recognizes it; BB cannot, because its dispersion floor is rho = 0. That is the asymmetry. Beta-binomial sees half the dispersion landscape. CMB sees both halves.</p>
</section>
</section>
<section id="the-coral-fertilization-study" class="level2">
<h2 class="anchored" data-anchor-id="the-coral-fertilization-study">The coral fertilization study</h2>
<p>The simulated example showed CMB doing its job on data we built. The next test is real data, where there is no simulated truth, no guarantee any family fits well, and the biology might or might not be visible once the statistical work is done.</p>
<p>The dataset comes from Puisay et al. <span class="citation" data-cites="puisay2024coral">(Puisay et al. 2024)</span>, <a href="https://www.sciencedirect.com/science/article/abs/pii/S0022098123000527">How thermal priming of coral gametes shapes fertilization success</a>, with the raw spreadsheet publicly archived on <a href="https://zenodo.org/records/8032395">Zenodo</a> <span class="citation" data-cites="puisay2024coral_data">(Puisay et al. 2023)</span>. The original study asked whether pre-exposing the gametes of a broadcast-spawning coral (<em>Acropora cytherea</em>) to elevated temperature, before fertilization, can rescue fertilization success when fertilization itself occurs at warmer ocean temperatures.</p>
<section id="the-question" class="level3">
<h3 class="anchored" data-anchor-id="the-question">The question</h3>
<p>The experiment is a clean 2 × 2 × 2 × 4 factorial. Each replicate vial holds a known number of eggs (range 33 to 457, mean about 160) and a controlled batch of sperm, under these conditions:</p>
<ul>
<li>Sperm pre-exposed at 27°C or 30°C</li>
<li>Oocytes pre-exposed at 27°C or 30°C</li>
<li>Pre-exposure lasting 0.5, 1, 1.5, or 2 hours</li>
<li>Fertilization itself carried out at either 27°C (control) or 30°C (warm, the climate-change scenario)</li>
</ul>
<p>The outcome is the count of embryos that developed normally (2-cell, 4-cell, or more-cell stage) out of the total eggs scored. Six replicates per cell, 192 vials in total, and just over 30,000 eggs scored. This is a textbook bounded count: <code>y</code> successes out of <code>n</code> trials, with <code>n</code> known per vial.</p>
</section>
<section id="how-they-analyzed-it" class="level3">
<h3 class="anchored" data-anchor-id="how-they-analyzed-it">How they analyzed it</h3>
<p>The authors fit a binomial-style GLM, but with a twist that matters here. They applied an arcsine square-root transformation to the proportions, then ran <code>glm()</code> on the transformed values. This is one of the three workarounds for proportion data that section 2 listed: throw away the bounded discrete structure, get coefficients on a scale that does not map cleanly back to rates, and accept that you cannot model dispersion separately from the mean. It is what people reach for when proportion data misbehave and there is no proper bounded-count family with dispersion structure in the toolkit they are using.</p>
<p>The authors noticed that dispersion in their data was doing something interesting. From their discussion section:</p>
<blockquote class="blockquote">
<p>“Interestingly, all things being equal, the fertilization success was more variable at 30°C, which may indicate opportunity for selection during the fertilization success. Furthermore, when considering the particular treatment where the thermal priming of gametes increased the fertilization success at 30°C, we observed a reduction of the variation of the fertilization success […] This experimental reduction in the variation of the fertilization success may imply that selection occurred on a pool of diverse gametes.”</p>
</blockquote>
<p>This is exactly the kind of observation a CMB model is built to formalize. Warm fertilization produces more variable outcomes than control. Successful priming both raises the mean and reduces the variability. Their analysis let them comment on this qualitatively in the discussion but did not let them attach a number to it, give it a confidence interval, or test it formally against the mean structure. They could see the dispersion pattern but could not model it.</p>
</section>
<section id="how-we-analyze-it" class="level3">
<h3 class="anchored" data-anchor-id="how-we-analyze-it">How we analyze it</h3>
<p>Same mean structure as theirs (a three-way interaction of fertilization temperature, pre-exposure time, and the cross of sperm × oocyte priming), but with three changes. First, we go back to the proper binomial scale via <code>cbind(y, n - y)</code>, no transformation. Second, we use <code>family = compbinomial</code> and let <code>dispformula = ~ fert_temp * treatment</code> model the dispersion across the eight (fert_temp × treatment) cells. Third, we add <code>(1 | site)</code> to capture shared variation across the two fertilization-temperature samples drawn from the same pre-exposure batch. We then compare the same set of candidate families introduced in the simulated example.</p>
</section>
<section id="loading-the-data" class="level3">
<h3 class="anchored" data-anchor-id="loading-the-data">Loading the data</h3>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readxl)</span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(glmmTMB)</span>
<span id="cb13-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(emmeans)</span>
<span id="cb13-4"></span>
<span id="cb13-5">raw <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;">read_excel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/Users/jb/Downloads/DATA (1).xlsx"</span>,</span>
<span id="cb13-6">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sheet =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GLM models data 2014"</span>,</span>
<span id="cb13-7">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skip  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb13-8"></span>
<span id="cb13-9">mod_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> raw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-10">  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;">transmute</span>(</span>
<span id="cb13-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">site      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Site),</span>
<span id="cb13-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fert_temp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Temp,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">27</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)),</span>
<span id="cb13-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">temp_s    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</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;">Temp S</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;">levels =</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;">27</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)),</span>
<span id="cb13-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">temp_o    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</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;">Temp O</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;">levels =</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;">27</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)),</span>
<span id="cb13-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(time),</span>
<span id="cb13-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(total),</span>
<span id="cb13-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</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;">2 Cells</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</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;">4 Cells</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>
<span id="cb13-18">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</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;">More Cells</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span>
<span id="cb13-19">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-20">  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="cb13-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">miss      =</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y,</span>
<span id="cb13-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">treatment =</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;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"O"</span>, temp_o, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"S"</span>, temp_s),</span>
<span id="cb13-23">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"O27S27"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"O27S30"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"O30S27"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"O30S30"</span>))</span>
<span id="cb13-24">  )</span>
<span id="cb13-25"></span>
<span id="cb13-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rows =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(mod_data),</span>
<span id="cb13-27">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total_eggs =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(mod_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n),</span>
<span id="cb13-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total_fertilized =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(mod_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y),</span>
<span id="cb13-29">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">overall_rate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(mod_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y) <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;">sum</span>(mod_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>            rows       total_eggs total_fertilized     overall_rate 
         192.000        30465.000        25458.000            0.836 </code></pre>
</div>
</div>
</section>
<section id="a-model-free-look-at-dispersion" class="level3">
<h3 class="anchored" data-anchor-id="a-model-free-look-at-dispersion">A model-free look at dispersion</h3>
<p>Before fitting anything, we check whether the data really do have heterogeneous dispersion across treatments. For each of the 32 treatment cells (2 fertilization temps × 4 priming treatments × 4 pre-exposure times, 6 replicates each), we compute a model-free binomial-index-of-dispersion: the Pearson chi-square against a binomial null with the cell’s own mean rate, divided by degrees of freedom. Values above 1 indicate overdispersion, below 1 indicate underdispersion.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">bid_screen <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mod_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-2">  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;">group_by</span>(fert_temp, treatment, time) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-3">  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;">summarise</span>(</span>
<span id="cb15-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p_bar =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(y) <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;">sum</span>(n),</span>
<span id="cb15-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">BID   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>((y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p_bar)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p_bar <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p_bar), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span></span>
<span id="cb15-6">              (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 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="cb15-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span></span>
<span id="cb15-8">  )</span>
<span id="cb15-9"></span>
<span id="cb15-10">bid_screen <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-11">  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;">group_by</span>(fert_temp) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-12">  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;">summarise</span>(</span>
<span id="cb15-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cells       =</span> 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="cb15-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min_BID     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(BID), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb15-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">median_BID  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(BID), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb15-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_BID     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(BID), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb15-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_under_1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(BID <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb15-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span></span>
<span id="cb15-19">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-20">  knitr<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;">kable</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">fert_temp</th>
<th style="text-align: right;">cells</th>
<th style="text-align: right;">min_BID</th>
<th style="text-align: right;">median_BID</th>
<th style="text-align: right;">max_BID</th>
<th style="text-align: right;">pct_under_1</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">27</td>
<td style="text-align: right;">16</td>
<td style="text-align: right;">0.18</td>
<td style="text-align: right;">0.76</td>
<td style="text-align: right;">7.05</td>
<td style="text-align: right;">0.69</td>
</tr>
<tr class="even">
<td style="text-align: left;">30</td>
<td style="text-align: right;">16</td>
<td style="text-align: right;">0.97</td>
<td style="text-align: right;">4.63</td>
<td style="text-align: right;">15.16</td>
<td style="text-align: right;">0.12</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>At control fertilization (27°C), the median BID is below 1 and 70% of cells sit on the underdispersed side, some quite strongly (minimum BID around 0.18). At warm fertilization (30°C), the median jumps to about 4.6, with the most extreme cells running up around 15. The data have heterogeneous, two-directional dispersion structure, exactly the regime CMB is designed for and exactly the regime BB cannot represent.</p>
<p>A picture of all 32 cells:</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(bid_screen,</span>
<span id="cb16-2">       <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(time), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> BID, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> fert_temp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</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;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <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;">"grey45"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<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;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-5">  <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> treatment, <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;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_log10</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-7">  <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> <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;">"27"</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;">"#2c7bb6"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"30"</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;">"#d7191c"</span>),</span>
<span id="cb16-8">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fertilization °C"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Pre-exposure time (h)"</span>,</span>
<span id="cb16-10">       <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;">"BID (log scale)"</span>,</span>
<span id="cb16-11">       <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;">"Model-free dispersion screen by treatment"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/CMB_regression/index_files/figure-html/coral-bid-plot-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>Model-free dispersion screen by treatment. Cells above the dashed line are overdispersed, below are underdispersed. Warm fertilization (red) sits almost entirely above; control (blue) sits mostly below.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="fitting-six-candidates" class="level3">
<h3 class="anchored" data-anchor-id="fitting-six-candidates">Fitting six candidates</h3>
<p>Six families this time. The four from the simulated section, plus the two constant-dispersion variants (beta-binomial and CMB with <code>dispformula = ~ 1</code>) to confirm that the by-treatment dispersion submodel earns its degrees of freedom on real data. Random intercept on site captures shared variation across the two fertilization-temperature samples drawn from each pre-exposure batch.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">safe_fit <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>(expr) {</span>
<span id="cb17-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tryCatch</span>(</span>
<span id="cb17-3">    <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;">fit =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eval.parent</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substitute</span>(expr)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_character_</span>),</span>
<span id="cb17-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <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;">fit =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">conditionMessage</span>(e))</span>
<span id="cb17-5">  )</span>
<span id="cb17-6">}</span>
<span id="cb17-7"></span>
<span id="cb17-8">form_bound <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;">cbind</span>(y, miss) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> fert_temp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> treatment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> site)</span>
<span id="cb17-9">form_cmp   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> fert_temp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> treatment <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;">offset</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> site)</span>
<span id="cb17-10"></span>
<span id="cb17-11">fits <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="cb17-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binomial      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">safe_fit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(form_bound, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> binomial, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> mod_data)),</span>
<span id="cb17-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bb_constant   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">safe_fit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(form_bound, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> betabinomial,</span>
<span id="cb17-14">                                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dispformula =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> mod_data)),</span>
<span id="cb17-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bb_by_treat   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">safe_fit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(form_bound, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> betabinomial,</span>
<span id="cb17-16">                                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dispformula =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> fert_temp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> treatment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> mod_data)),</span>
<span id="cb17-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cmp_offset    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">safe_fit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(form_cmp,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> compois,</span>
<span id="cb17-18">                                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dispformula =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> fert_temp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> treatment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> mod_data)),</span>
<span id="cb17-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cmb_constant  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">safe_fit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(form_bound, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> compbinomial,</span>
<span id="cb17-20">                                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dispformula =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> mod_data)),</span>
<span id="cb17-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cmb_by_treat  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">safe_fit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(form_bound, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> compbinomial,</span>
<span id="cb17-22">                                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dispformula =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> fert_temp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> treatment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> mod_data))</span>
<span id="cb17-23">)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>Warning in finalizeTMB(TMBStruc, obj, fit, h, data.tmb.old): Model convergence
problem; non-positive-definite Hessian matrix. See vignette('troubleshooting')</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning in finalizeTMB(TMBStruc, obj, fit, h, data.tmb.old): Model convergence
problem; false convergence (8). See vignette('troubleshooting'),
help('diagnose')</code></pre>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">compare_tbl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> 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;">bind_rows</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(fits), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(nm) {</span>
<span id="cb20-2">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fits[[nm]]</span>
<span id="cb20-3">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.null</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit)) {</span>
<span id="cb20-4">    <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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> nm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AIC =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb20-5">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb20-6">  }</span>
<span id="cb20-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb20-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family    =</span> nm,</span>
<span id="cb20-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attr</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logLik</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"df"</span>),</span>
<span id="cb20-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AIC       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">AIC</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit),</span>
<span id="cb20-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">isTRUE</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sdr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pdHess),</span>
<span id="cb20-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb20-13">  )</span>
<span id="cb20-14">})) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb20-15">  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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dAIC =</span> AIC <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;">min</span>(AIC, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</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;">|&gt;</span></span>
<span id="cb20-16">  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;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(AIC), AIC)</span>
<span id="cb20-17"></span>
<span id="cb20-18">knitr<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;">kable</span>(compare_tbl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">family</th>
<th style="text-align: right;">df</th>
<th style="text-align: right;">AIC</th>
<th style="text-align: left;">converged</th>
<th style="text-align: right;">dAIC</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">cmb_by_treat</td>
<td style="text-align: right;">25</td>
<td style="text-align: right;">1308.82</td>
<td style="text-align: left;">TRUE</td>
<td style="text-align: right;">0.00</td>
</tr>
<tr class="even">
<td style="text-align: left;">cmp_offset</td>
<td style="text-align: right;">25</td>
<td style="text-align: right;">1327.89</td>
<td style="text-align: left;">TRUE</td>
<td style="text-align: right;">19.07</td>
</tr>
<tr class="odd">
<td style="text-align: left;">cmb_constant</td>
<td style="text-align: right;">18</td>
<td style="text-align: right;">1356.92</td>
<td style="text-align: left;">TRUE</td>
<td style="text-align: right;">48.10</td>
</tr>
<tr class="even">
<td style="text-align: left;">bb_constant</td>
<td style="text-align: right;">18</td>
<td style="text-align: right;">1357.98</td>
<td style="text-align: left;">TRUE</td>
<td style="text-align: right;">49.16</td>
</tr>
<tr class="odd">
<td style="text-align: left;">binomial</td>
<td style="text-align: right;">17</td>
<td style="text-align: right;">1594.79</td>
<td style="text-align: left;">TRUE</td>
<td style="text-align: right;">285.97</td>
</tr>
<tr class="even">
<td style="text-align: left;">bb_by_treat</td>
<td style="text-align: right;">25</td>
<td style="text-align: right;">NA</td>
<td style="text-align: left;">FALSE</td>
<td style="text-align: right;">NA</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Binomial sits at +286 AIC, which is the price of ignoring the dispersion structure entirely. Among the families that try to model dispersion, CMB-by-treatment wins by 19 AIC over CMP-offset and by 48 over the constant-dispersion variants. CMP-offset finishes second, which is honest: at the rates and trial sizes here (rates 0.46 to 0.97, trial sizes 33 to 457), the upper bound is not strongly binding for most cells, so the unbounded-count approximation is not catastrophic. It still loses to CMB by a clear margin. Beta-binomial with by-treatment dispersion did not converge, as the simulated example predicted.</p>
</section>
<section id="what-cmb-found-about-dispersion" class="level3">
<h3 class="anchored" data-anchor-id="what-cmb-found-about-dispersion">What CMB found about dispersion</h3>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">disp_grid <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;">expand.grid</span>(</span>
<span id="cb21-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fert_temp =</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;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">27</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)),</span>
<span id="cb21-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">treatment =</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"O27S27"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"O27S30"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"O30S27"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"O30S30"</span>),</span>
<span id="cb21-4">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"O27S27"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"O27S30"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"O30S27"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"O30S30"</span>))</span>
<span id="cb21-5">)</span>
<span id="cb21-6"></span>
<span id="cb21-7">cmb_nu_by_cell <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, grid) {</span>
<span id="cb21-8">  X    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model.matrix</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> fert_temp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> treatment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> grid)</span>
<span id="cb21-9">  beta <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;">fixef</span>(fit)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>disp</span>
<span id="cb21-10">  V    <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;">vcov</span>(fit)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>disp</span>
<span id="cb21-11">  eta  <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.vector</span>(X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> beta)</span>
<span id="cb21-12">  se   <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;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> V <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;">t</span>(X)))</span>
<span id="cb21-13">  grid <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb21-14">    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="cb21-15">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nu_hat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(eta),</span>
<span id="cb21-16">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(eta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> se),</span>
<span id="cb21-17">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(eta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> se)</span>
<span id="cb21-18">    )</span>
<span id="cb21-19">}</span>
<span id="cb21-20"></span>
<span id="cb21-21">knitr<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;">kable</span>(</span>
<span id="cb21-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cmb_nu_by_cell</span>(fits<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cmb_by_treat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit, disp_grid),</span>
<span id="cb21-23">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb21-24">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMB ν by treatment cell"</span></span>
<span id="cb21-25">)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>CMB ν by treatment cell</caption>
<thead>
<tr class="header">
<th style="text-align: left;">fert_temp</th>
<th style="text-align: left;">treatment</th>
<th style="text-align: right;">nu_hat</th>
<th style="text-align: right;">lower</th>
<th style="text-align: right;">upper</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">27</td>
<td style="text-align: left;">O27S27</td>
<td style="text-align: right;">0.921</td>
<td style="text-align: right;">0.508</td>
<td style="text-align: right;">1.671</td>
</tr>
<tr class="even">
<td style="text-align: left;">30</td>
<td style="text-align: left;">O27S27</td>
<td style="text-align: right;">0.143</td>
<td style="text-align: right;">0.076</td>
<td style="text-align: right;">0.271</td>
</tr>
<tr class="odd">
<td style="text-align: left;">27</td>
<td style="text-align: left;">O27S30</td>
<td style="text-align: right;">0.947</td>
<td style="text-align: right;">0.527</td>
<td style="text-align: right;">1.700</td>
</tr>
<tr class="even">
<td style="text-align: left;">30</td>
<td style="text-align: left;">O27S30</td>
<td style="text-align: right;">0.129</td>
<td style="text-align: right;">0.069</td>
<td style="text-align: right;">0.241</td>
</tr>
<tr class="odd">
<td style="text-align: left;">27</td>
<td style="text-align: left;">O30S27</td>
<td style="text-align: right;">0.359</td>
<td style="text-align: right;">0.190</td>
<td style="text-align: right;">0.676</td>
</tr>
<tr class="even">
<td style="text-align: left;">30</td>
<td style="text-align: left;">O30S27</td>
<td style="text-align: right;">0.136</td>
<td style="text-align: right;">0.074</td>
<td style="text-align: right;">0.249</td>
</tr>
<tr class="odd">
<td style="text-align: left;">27</td>
<td style="text-align: left;">O30S30</td>
<td style="text-align: right;">1.663</td>
<td style="text-align: right;">0.936</td>
<td style="text-align: right;">2.955</td>
</tr>
<tr class="even">
<td style="text-align: left;">30</td>
<td style="text-align: left;">O30S30</td>
<td style="text-align: right;">0.303</td>
<td style="text-align: right;">0.168</td>
<td style="text-align: right;">0.548</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>CMB sees a clean two-temperature story. Every warm-fertilization cell is strongly overdispersed, with ν between 0.13 and 0.30 and CIs nowhere near 1. The control cells are quieter: two are statistically binomial, one (O30S27) is clearly overdispersed, and one (O30S30, the double-primed condition) has a point estimate of ν ≈ 1.66 with a CI that just brushes 1. That last cell is exactly the underdispersion the paper noticed in the discussion. CMB does not formally call it significant at the 95% level, but it sees the direction the authors described, and it puts numbers on it.</p>
<p>A picture of the CMB ν surface:</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">cmb_nu_tbl <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;">cmb_nu_by_cell</span>(fits<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cmb_by_treat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit, disp_grid)</span>
<span id="cb22-2"></span>
<span id="cb22-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(cmb_nu_tbl,</span>
<span id="cb22-4">       <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> nu_hat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">interaction</span>(treatment, fert_temp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | "</span>),</span>
<span id="cb22-5">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> fert_temp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</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;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <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;">"grey45"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_pointrange</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;">xmin =</span> lower, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</span> upper), <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.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-9">  <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> <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;">"27"</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;">"#2c7bb6"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"30"</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;">"#d7191c"</span>),</span>
<span id="cb22-10">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fertilization °C"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"CMB ν estimate (log scale, with 95% CI)"</span>,</span>
<span id="cb22-12">       <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;">"Treatment | Fert °C"</span>,</span>
<span id="cb22-13">       <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;">"CMB dispersion structure on the coral data"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/CMB_regression/index_files/figure-html/coral-nu-plot-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>CMB ν by treatment, with 95% confidence intervals. All warm-fertilization cells (red) are clearly overdispersed. Control-fertilization cells (blue) straddle binomial, with O30S30 hinting at underdispersion.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="confidence-intervals-on-what-we-actually-care-about" class="level3">
<h3 class="anchored" data-anchor-id="confidence-intervals-on-what-we-actually-care-about">Confidence intervals on what we actually care about</h3>
<p>The dispersion table is the methodological finding. The biological question is “by how much does each priming strategy raise fertilization success at warm temperature, and how confident are we in that number?” That is the marginal-mean question, and the answer depends on the dispersion model.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">em_tables <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="cb23-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (m <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
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;">"binomial"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cmb_by_treat"</span>)) {</span>
<span id="cb23-3">  f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fits[[m]]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit</span>
<span id="cb23-4">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.null</span>(f)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">next</span></span>
<span id="cb23-5">  em <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;">emmeans</span>(f, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> fert_temp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> treatment,</span>
<span id="cb23-6">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">at =</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;">time =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.25</span>),</span>
<span id="cb23-7">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>)</span>
<span id="cb23-8">  em_tables[[m]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(em) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb23-9">    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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> m, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ci_width =</span> asymp.UCL <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> asymp.LCL)</span>
<span id="cb23-10">}</span></code></pre></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>NOTE: Results may be misleading due to involvement in interactions
NOTE: Results may be misleading due to involvement in interactions</code></pre>
</div>
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">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;">bind_rows</span>(em_tables) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb25-2">  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;">select</span>(model, fert_temp, treatment, ci_width) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb25-3">  tidyr<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;">pivot_wider</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> ci_width,</span>
<span id="cb25-4">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_prefix =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ci_"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb25-5">  knitr<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;">kable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb25-6">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"95% CI widths on the marginal predicted rate per cell"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>95% CI widths on the marginal predicted rate per cell</caption>
<thead>
<tr class="header">
<th style="text-align: left;">fert_temp</th>
<th style="text-align: left;">treatment</th>
<th style="text-align: right;">ci_binomial</th>
<th style="text-align: right;">ci_cmb_by_treat</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">27</td>
<td style="text-align: left;">O27S27</td>
<td style="text-align: right;">0.0224</td>
<td style="text-align: right;">0.0168</td>
</tr>
<tr class="even">
<td style="text-align: left;">30</td>
<td style="text-align: left;">O27S27</td>
<td style="text-align: right;">0.0485</td>
<td style="text-align: right;">0.0690</td>
</tr>
<tr class="odd">
<td style="text-align: left;">27</td>
<td style="text-align: left;">O27S30</td>
<td style="text-align: right;">0.0220</td>
<td style="text-align: right;">0.0162</td>
</tr>
<tr class="even">
<td style="text-align: left;">30</td>
<td style="text-align: left;">O27S30</td>
<td style="text-align: right;">0.0594</td>
<td style="text-align: right;">0.0887</td>
</tr>
<tr class="odd">
<td style="text-align: left;">27</td>
<td style="text-align: left;">O30S27</td>
<td style="text-align: right;">0.0313</td>
<td style="text-align: right;">0.0352</td>
</tr>
<tr class="even">
<td style="text-align: left;">30</td>
<td style="text-align: left;">O30S27</td>
<td style="text-align: right;">0.0531</td>
<td style="text-align: right;">0.0689</td>
</tr>
<tr class="odd">
<td style="text-align: left;">27</td>
<td style="text-align: left;">O30S30</td>
<td style="text-align: right;">0.0296</td>
<td style="text-align: right;">0.0163</td>
</tr>
<tr class="even">
<td style="text-align: left;">30</td>
<td style="text-align: left;">O30S30</td>
<td style="text-align: right;">0.0410</td>
<td style="text-align: right;">0.0409</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The CMB CIs adjust the binomial baseline up where the data are overdispersed and down where they are not. Warm-fertilization cells widen by 30 to 50 percent relative to binomial, with one exception: O30S30 at warm has the mildest overdispersion of the four warm cells (ν ≈ 0.30, closest to binomial) and ends up at essentially the binomial width. Control cells either widen slightly (O30S27, the one clearly overdispersed control cell) or tighten below binomial. The O30S30 cell at control is the cleanest example: binomial 0.030, CMB 0.016, almost half.</p>
<p>That asymmetric correction is the practical payoff. Dispersion is not just a parameter we estimate, it shifts how confident we are in every cell-level rate, in the right direction for each cell.</p>
</section>
<section id="sharpening-the-biological-finding" class="level3">
<h3 class="anchored" data-anchor-id="sharpening-the-biological-finding">Sharpening the biological finding</h3>
<p>With dispersion correctly modeled, we can ask the question the original paper asked, but with calibrated standard errors. At warm fertilization (30°C), which priming strategy works?</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">em_30 <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;">emmeans</span>(fits<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cmb_by_treat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> treatment,</span>
<span id="cb26-2">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">at =</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;">fert_temp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">27</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)),</span>
<span id="cb26-3">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time      =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.25</span>))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>NOTE: Results may be misleading due to involvement in interactions</code></pre>
</div>
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pairs</span>(em_30, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adjust =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tukey"</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code> contrast        estimate    SE  df z.ratio p.value
 O27S27 - O27S30   0.3861 0.152 Inf   2.541  0.0538
 O27S27 - O30S27   0.3062 0.137 Inf   2.235  0.1139
 O27S27 - O30S30  -0.2651 0.127 Inf  -2.087  0.1573
 O27S30 - O30S27  -0.0799 0.142 Inf  -0.563  0.9430
 O27S30 - O30S30  -0.6513 0.132 Inf  -4.922  &lt;.0001
 O30S27 - O30S30  -0.5714 0.115 Inf  -4.974  &lt;.0001

Results are given on the log odds ratio (not the response) scale. 
P value adjustment: tukey method for comparing a family of 4 estimates </code></pre>
</div>
</div>
<p>Three things sit in that table. First, double-priming (O30S30) significantly outperforms each single-priming strategy, both contrasts at p &lt; 0.0001 with effect sizes near 0.6 on the log-odds scale. Second, double-priming does not significantly beat no priming (p = 0.16). Third, single-priming does not improve on no priming either; the O27S27 vs O27S30 contrast is even marginally significant in the wrong direction (p = 0.054), meaning no priming was slightly better than priming sperm alone.</p>
<p>This sharpens the paper’s conclusion. The original analysis said priming helps and double-priming helps most. The CMB-calibrated version says something more pointed: you have to prime <em>both</em> gametes. Single-priming does not significantly outperform no priming, and may even be marginally worse. The only thing that lets us make that claim cleanly is calibrated uncertainty on each contrast, which required the right dispersion model.</p>
<p>The variability hint in the paper’s discussion now has both halves: priming both gametes restored the mean and the consistency of fertilization, and we can put numbers on each.</p>
</section>
</section>
<section id="when-to-use-what" class="level2">
<h2 class="anchored" data-anchor-id="when-to-use-what">When to use what</h2>
<p>CMB is not the answer to every proportion-regression problem.</p>
<p>If your data are clearly overdispersed and the dispersion looks roughly constant across groups, use beta-binomial. It is faster, well-supported, and what your reviewers will expect.</p>
<p>If your counts are nominally bounded but rarely come near the bound, and the dispersion goes in only one direction, CMP with an offset is a defensible workaround. This is the situation I originally had, and it works fine until you ask it to simulate.</p>
<p>If your counts can hit the bound, the dispersion is heterogeneous across groups, or you suspect any sub-binomial variance in any subgroup, CMB is the right tool. The standard alternatives fail in this regime in ways that are hard to detect unless you specifically look for them.</p>
<p>A few warning signs:</p>
<ul>
<li>Your binomial residuals show clear structure (cells systematically over or under predicted relative to binomial expectation).</li>
<li>Variance ratios (observed variance over binomial expectation) differ across subgroups, or cross 1.</li>
<li>A Pearson chi-square comes in well below the residual degrees of freedom.</li>
<li>A beta-binomial with per-group dispersion fails to converge, or pegs rho near zero in some groups but clearly above zero in others.</li>
</ul>
<p>Any one of these is a hint that the family you are using cannot describe what the data are doing.</p>
</section>
<section id="bottom-line" class="level2">
<h2 class="anchored" data-anchor-id="bottom-line">Bottom line</h2>
<p>For bounded count data, the binomial baseline is correct only when the data are well-behaved. Real data often are not. The standard upgrade path is beta-binomial, but BB can only ever inflate variance above binomial. When the data want sub-binomial variance, BB hits its <code>rho &gt;= 0</code> wall, sometimes silently, sometimes by refusing to converge.</p>
<p>CMB fixes this. It is the binomial analogue of CMP, it covers both dispersion directions through a single ν parameter, and it respects the upper bound by construction. With the implementation linked below it slots into the standard <code>glmmTMB</code> workflow.</p>
<p>The coral example showed what this buys you on a real study: a sharper biological claim than the original analysis could make, with properly calibrated intervals on each contrast. The dispersion structure the authors had only described qualitatively is now an estimable, testable feature of the fit.</p>
</section>
<section id="getting-cmb-in-your-r-session" class="level2">
<h2 class="anchored" data-anchor-id="getting-cmb-in-your-r-session">Getting CMB in your R session</h2>
<p>The CMB family is not on CRAN. For now it lives on two public forks:</p>
<ul>
<li>TMB side, the auto-differentiable likelihood: <a href="https://github.com/jbogomolovas2/adcomp/tree/cmb-family">github.com/jbogomolovas2/adcomp@cmb-family</a></li>
<li>glmmTMB side, the formula interface: <a href="https://github.com/jbogomolovas2/glmmTMB/tree/cmb-family">github.com/jbogomolovas2/glmmTMB@cmb-family</a></li>
</ul>
<p>Install both with the snippet above, write <code>family = compbinomial</code> in your <code>glmmTMB()</code> call, and the rest is the standard workflow you already know. Issues and pull requests are welcome on either fork. If <code>compbinomial</code> ends up useful for your work I would be glad to hear about it.</p>
</section>
<section id="acknowledgements" class="level2">
<h2 class="anchored" data-anchor-id="acknowledgements">Acknowledgements</h2>
<p>This post stands on a lot of other people’s code. The CMB distribution work by <span class="citation" data-cites="kadane2016cmb">(Kadane 2016)</span> and the COMMultReg package by Andrew Raim <span class="citation" data-cites="COMMultReg">(Raim, n.d.)</span> provided the distribution-level reference implementation that I checked the TMB-side density against. The mean parameterization follows the pattern Huang <span class="citation" data-cites="huang2017meanCMP">(Huang 2017)</span> introduced for CMP. TMB <span class="citation" data-cites="kristensen2016TMB">(Kristensen et al. 2016)</span> handled the automatic differentiation that made the inner Newton solve tractable, and glmmTMB <span class="citation" data-cites="brooks2017glmmTMB">(Brooks et al. 2017)</span> provided the family-dispatch and dispformula machinery that the CMB family slots into. The coral data is from Puisay et al. <span class="citation" data-cites="puisay2024coral">(Puisay et al. 2024)</span>, archived openly on Zenodo <span class="citation" data-cites="puisay2024coral_data">(Puisay et al. 2023)</span>, which is what made the real-data demonstration possible. R package citations were generated with <code>grateful</code> <span class="citation" data-cites="grateful">(Rodrı́guez-Sánchez, n.d.)</span>.</p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-brooks2017glmmTMB" class="csl-entry">
Brooks, Mollie E., Kasper Kristensen, Koen J. van Benthem, Arni Magnusson, Casper W. Berg, Anders Nielsen, Hans J. Skaug, Martin Mächler, and Benjamin M. Bolker. 2017. <span>“<span class="nocase">glmmTMB</span> Balances Speed and Flexibility Among Packages for Zero-Inflated Generalized Linear Mixed Models.”</span> <em>The R Journal</em> 9 (2): 378–400. <a href="https://doi.org/10.32614/RJ-2017-066">https://doi.org/10.32614/RJ-2017-066</a>.
</div>
<div id="ref-huang2017meanCMP" class="csl-entry">
Huang, Alan. 2017. <span>“Mean-Parametrized <span>Conway–Maxwell–Poisson</span> Regression Models for Dispersed Counts.”</span> <em>Statistical Modelling</em> 17 (6): 359–80. <a href="https://doi.org/10.1177/1471082X17697749">https://doi.org/10.1177/1471082X17697749</a>.
</div>
<div id="ref-kadane2016cmb" class="csl-entry">
Kadane, Joseph B. 2016. <span>“Sums of Possibly Associated <span>B</span>ernoulli Variables: The <span class="nocase">Conway-Maxwell-binomial</span> Distribution.”</span> <em>Bayesian Analysis</em> 11 (2): 403–20. <a href="https://doi.org/10.1214/15-BA955">https://doi.org/10.1214/15-BA955</a>.
</div>
<div id="ref-kristensen2016TMB" class="csl-entry">
Kristensen, Kasper, Anders Nielsen, Casper W. Berg, Hans Skaug, and Bradley M. Bell. 2016. <span>“<span>TMB</span>: Automatic Differentiation and <span>Laplace</span> Approximation.”</span> <em>Journal of Statistical Software</em> 70 (5): 1–21. <a href="https://doi.org/10.18637/jss.v070.i05">https://doi.org/10.18637/jss.v070.i05</a>.
</div>
<div id="ref-puisay2024coral_data" class="csl-entry">
Puisay, Antoine, Laetitia Hédouin, Rosanne Pilon, Claire Goiran, and Benoit Pujol. 2023. <span>“Data for: How Thermal Priming of Coral Gametes Shapes Fertilization Success.”</span> Zenodo. <a href="https://doi.org/10.5281/zenodo.8032394">https://doi.org/10.5281/zenodo.8032394</a>.
</div>
<div id="ref-puisay2024coral" class="csl-entry">
———. 2024. <span>“How Thermal Priming of Coral Gametes Shapes Fertilization Success.”</span> <em>Journal of Experimental Marine Biology and Ecology</em>. <a href="https://doi.org/10.1016/j.jembe.2023.151953">https://doi.org/10.1016/j.jembe.2023.151953</a>.
</div>
<div id="ref-COMMultReg" class="csl-entry">
Raim, Andrew M. n.d. <em><span>COMMultReg</span>: <span>Conway–Maxwell</span> Multinomial Regression</em>. <a href="https://github.com/andrewraim/COMMultReg">https://github.com/andrewraim/COMMultReg</a>.
</div>
<div id="ref-grateful" class="csl-entry">
Rodrı́guez-Sánchez, Francisco. n.d. <em><span class="nocase">grateful</span>: Facilitate Citation of <span>R</span> Packages</em>. <a href="https://pakillo.github.io/grateful/">https://pakillo.github.io/grateful/</a>.
</div>
</div>


</section>

 ]]></description>
  <category>R</category>
  <category>GLM</category>
  <category>GLMM</category>
  <category>Count Regression</category>
  <category>CMB</category>
  <category>Biometrics</category>
  <guid>https://jbogomolovas2.github.io/Julius-s-Blog/posts/CMB_regression/</guid>
  <pubDate>Sun, 24 May 2026 07:00:00 GMT</pubDate>
  <media:content url="https://jbogomolovas2.github.io/Julius-s-Blog/posts/CMB_regression/corals.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>Conway–Maxwell–Poisson vs Welch’s t-test for proliferation count data</title>
  <dc:creator>Julius Bogomolovas</dc:creator>
  <link>https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/</link>
  <description><![CDATA[ 





<p>There is a Chinese saying, 物尽其用, which approximately translates as “make the best use of everything.” I first came across it as the title of a Song Dong installation at MoMA. So in this post I am testing whether a 物尽其用 approach to quantifying cell proliferation actually does better than the standard one.</p>
<p>In my work I count proliferating cardiomyocytes and compare proliferation rates between wild-type and knockout hearts. The data comes from tissue sections stained for a cardiomyocyte marker and a proliferation marker, which gives me, per microscopy field, the total number of CMs and the number of proliferating CMs.</p>
<p>The standard analysis is a t-test on per-animal proliferation ratios. The moment you take that path, three things get thrown away: the within-animal variation sitting right there across the multiple fields per heart; the statistical weight that should come with larger denominators (1/10 and 100/1000 are not equally trustworthy estimates of 0.1); and the awkward fact that ratios live in [0,1], which is not where a t-test wants its data. None of these are catastrophic on their own. Together they motivate an obvious question: what if we kept the information instead?</p>
<p>Two of the three losses, the statistical weight and the bounded distribution, are about how you model the count itself, and count regression fixes both. Instead of collapsing into a ratio, you model the raw counts directly, with total cells counted entering as an offset. The offset is what carries the credibility of each estimate into the analysis, and the model respects the discrete, bounded-below, often-overdispersed nature of count data. The third loss, within-animal variation, is about how you treat the nesting. It is fixed by adding a random intercept per animal, so the model separates between-animal from within-animal variance instead of averaging it away.</p>
<p>These are two orthogonal decisions. Crossed, they give a <strong>2×2 of methods</strong>, all of which I’ll evaluate side by side:</p>
<ul>
<li><strong>Animal-level (no random effects).</strong> Aggregate fields to one number per animal, then test. Welch’s t-test (on ratios) vs Conway–Maxwell–Poisson (CMP) GLM (on counts). This is the standard biologist workflow.</li>
<li><strong>Field-level (with random effects).</strong> Keep the field structure, add a random intercept per animal. Linear mixed model (LMM) on ratios vs CMP GLMM on counts.</li>
</ul>
<p>I argued the count-regression half of this case in <a href="https://pubmed.ncbi.nlm.nih.gov/36563012/">my previous publication</a> (Bogomolovas et al., 2022), where I walked from raw microscopy images through AI-based cell counts to count-regression statistics. But I kept saying “I should formally test this.” Here it is. I believe the 物尽其用 approach is the better one, but is it always better than the default? Where does it shine? Are there regimes where the simpler approach holds up just fine?</p>
<p>I build the comparison from scratch: a data generator, four fitters in one place, a Latin-hypercube sweep of the realistic experimental design space, and Gaussian process emulators that let me ask questions about method behaviour anywhere in that space, not just at the handful of points I simulated. The post is organised by metric — Type-I error, statistical power, CI precision — comparing all four methods on each in turn, then a practical recommendation at the end.</p>
<section id="setup" class="level2">
<h2 class="anchored" data-anchor-id="setup">Setup</h2>
<div class="cell">
<details open="" class="code-fold">
<summary>Show code</summary>
<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;">suppressPackageStartupMessages</span>({</span>
<span id="cb1-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr); <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr); <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2); <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span>
<span id="cb1-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(scales); <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(knitr)</span>
<span id="cb1-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(COMMultReg)             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># r_cmb, e_cmb: the simulator's count distribution</span></span>
<span id="cb1-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(glmmTMB); <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(emmeans)</span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lme4);   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lmerTest)</span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(hetGP)                  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># GP emulators</span></span>
<span id="cb1-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lhs)                    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Latin hypercube sampling (used in LHS demo figure)</span></span>
<span id="cb1-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(GGally)                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ggpairs (used in LHS demo figure)</span></span>
<span id="cb1-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(grateful)               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># auto-generated package citations</span></span>
<span id="cb1-11">})</span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shared visual conventions</span></span>
<span id="cb1-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>))</span>
<span id="cb1-15"></span>
<span id="cb1-16">method_colors_no_re <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Welch's t-test"</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;">"#4A7FB8"</span>,</span>
<span id="cb1-17">                         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLM"</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;">"#C0504D"</span>)</span>
<span id="cb1-18">method_colors_re    <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LMM"</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;">"#4A7FB8"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLMM"</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;">"#C0504D"</span>)</span>
<span id="cb1-19"></span>
<span id="cb1-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tiny utilities used everywhere</span></span>
<span id="cb1-21">loguni <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>(u, lo, hi) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(lo) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> u <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>(hi) <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>(lo)))</span>
<span id="cb1-22"></span>
<span id="cb1-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Smoothed logit so 0% and 100% rejection rates don't blow up the GP</span></span>
<span id="cb1-24">logit_p <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>(p, n) {</span>
<span id="cb1-25">  p_adj <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(p_adj <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p_adj))</span>
<span id="cb1-27">}</span>
<span id="cb1-28"></span>
<span id="cb1-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># p-value formatting for plot annotations</span></span>
<span id="cb1-30">fmt_p <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>(p) {</span>
<span id="cb1-31">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(p)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p = NA"</span>)</span>
<span id="cb1-32">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p = %.3f"</span>, p))</span>
<span id="cb1-33">  e <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;">floor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(p)); m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p <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;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>e</span>
<span id="cb1-34">  <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;">"p = %.1f \u00d7 10^%d"</span>, m, e)</span>
<span id="cb1-35">}</span></code></pre></div>
</details>
</div>
</section>
<section id="the-data-generator" class="level2">
<h2 class="anchored" data-anchor-id="the-data-generator">The data generator</h2>
<p>A proliferation experiment has a three-level hierarchy: <strong>group → animal → field</strong>. The generator below produces one row per field, with these knobs:</p>
<ul>
<li><code>p0</code>: baseline proliferation rate (control group mean)</li>
<li><code>change</code>: multiplicative effect size; <code>1.0</code> is the null</li>
<li><code>CV</code>: between-animal coefficient of variation (biological scatter)</li>
<li><code>nu</code>: within-field dispersion of the count distribution (measurement noise; <img src="https://latex.codecogs.com/png.latex?%5Cnu%20=%201"> is the binomial baseline, <img src="https://latex.codecogs.com/png.latex?%5Cnu%20%3C%201"> is overdispersed, <img src="https://latex.codecogs.com/png.latex?%5Cnu%20%3E%201"> is underdispersed)</li>
<li><code>n_animals</code>, <code>n_replicates</code>, <code>m</code>: animals per group, fields per animal, cells per field</li>
</ul>
<p>A quick note on how I landed on Beta + Conway–Maxwell–Binomial (CMB) rather than the more obvious alternatives. I first tried a Conway–Maxwell–Poisson (CMP) draw with total cells as a fixed offset. That ran into trouble: CMP is upper-unbounded, so draws sometimes overflowed the offset and produced more proliferating cells than were counted in total. That cannot happen biologically. The CMB is bounded by <img src="https://latex.codecogs.com/png.latex?m"> by construction, which removes the overflow problem entirely. The same logic applies one level up at the animal rates: drawing them as <img src="https://latex.codecogs.com/png.latex?p_0"> + Normal noise occasionally produced rates &gt; 1, so I switched to drawing directly from a Beta with prescribed mean and CV. The Beta lives on <img src="https://latex.codecogs.com/png.latex?(0,1)"> by construction.</p>
<p>A CMB regression would be the natural choice here since the generator itself is CMB, but no CMB regression package exists yet. So I stuck with CMP for the fitters, which works fine in practice as you will see below. I am thinking of putting together a CMB regression myself at some point; most of the pieces are already out there.</p>
<p>Three implementation details worth surfacing. The CMB’s mean is only <img src="https://latex.codecogs.com/png.latex?m%20%5Ccdot%20p"> when <img src="https://latex.codecogs.com/png.latex?%5Cnu%20=%201">. For any other <img src="https://latex.codecogs.com/png.latex?%5Cnu"> I numerically invert the expected-value function so that a target rate produces draws that realize that rate on average. That is what <code>p_from_rate</code> is doing in the code below. Secondly, animal IDs are group-prefixed (<code>ctrl1</code> and <code>cko1</code> are <em>different</em> animals) so a random-effect term can’t collapse across groups by accident. Finally, the multi-field structure is preserved even when the analysis aggregates it away. That way the same generator feeds both pipelines, with and without random animal effect.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Show code</summary>
<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;"># Invert e_cmb so the caller specifies a target rate and gets back the p-parameter</span></span>
<span id="cb2-2">p_from_rate <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>(target_rate, m, nu) {</span>
<span id="cb2-3">  target_rate <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;">pmax</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(target_rate, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.999</span>), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>)</span>
<span id="cb2-4">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(nu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-10</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(target_rate)</span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tryCatch</span>(</span>
<span id="cb2-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">uniroot</span>(<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">e_cmb</span>(m, p, nu) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> m <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> target_rate,</span>
<span id="cb2-7">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper =</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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tol =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>root,</span>
<span id="cb2-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) target_rate</span>
<span id="cb2-9">  )</span>
<span id="cb2-10">}</span>
<span id="cb2-11"></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Beta with prescribed (mean = mu, SD = mu * CV). Support is (0,1) by construction.</span></span>
<span id="cb2-13">draw_animal_rates <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>(mu, CV, n) {</span>
<span id="cb2-14">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (CV <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(mu, n))</span>
<span id="cb2-15">  sigma2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> CV)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb2-16">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (sigma2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu)) {</span>
<span id="cb2-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</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;">"Beta infeasible: mu=%.3g with CV=%.3g requires CV &lt; %.3g"</span>,</span>
<span id="cb2-18">                 mu, CV, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> mu) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> mu)))</span>
<span id="cb2-19">  }</span>
<span id="cb2-20">  conc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sigma2 <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="cb2-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbeta</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape1 =</span> mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> conc, <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> conc)</span>
<span id="cb2-22">}</span>
<span id="cb2-23"></span>
<span id="cb2-24">simulate_experiment <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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p0 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">change =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CV =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nu =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>,</span>
<span id="cb2-25">                                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_animals =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_replicates =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>) {</span>
<span id="cb2-26">  wt_rates <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;">draw_animal_rates</span>(p0,          CV, n_animals)</span>
<span id="cb2-27">  ko_rates <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;">draw_animal_rates</span>(p0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> change, CV, n_animals)</span>
<span id="cb2-28"></span>
<span id="cb2-29">  wt_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;">vapply</span>(wt_rates, p_from_rate, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</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;">m =</span> m, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nu =</span> nu)</span>
<span id="cb2-30">  ko_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;">vapply</span>(ko_rates, p_from_rate, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</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;">m =</span> m, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nu =</span> nu)</span>
<span id="cb2-31"></span>
<span id="cb2-32">  wt_counts <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;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(wt_p, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replicate</span>(n_replicates, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">r_cmb</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, m, p, nu))))</span>
<span id="cb2-33">  ko_counts <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;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(ko_p, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replicate</span>(n_replicates, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">r_cmb</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, m, p, nu))))</span>
<span id="cb2-34"></span>
<span id="cb2-35">  EmbryoID <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;">c</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;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ctrl"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n_animals)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> n_replicates),</span>
<span id="cb2-36">                <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;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cko"</span>,  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n_animals)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> n_replicates))</span>
<span id="cb2-37"></span>
<span id="cb2-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb2-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(wt_counts, ko_counts)),</span>
<span id="cb2-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N        =</span> m,</span>
<span id="cb2-41">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Group    =</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;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ctrl"</span>, n_animals <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n_replicates),</span>
<span id="cb2-42">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cko"</span>,  n_animals <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n_replicates)),</span>
<span id="cb2-43">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"ctrl"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cko"</span>)),</span>
<span id="cb2-44">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">EmbryoID =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(EmbryoID)</span>
<span id="cb2-45">  )</span>
<span id="cb2-46">}</span></code></pre></div>
</details>
</div>
<section id="what-the-generator-produces" class="level3">
<h3 class="anchored" data-anchor-id="what-the-generator-produces">What the generator produces</h3>
<p>Let’s simulate an oversized experiment: 200 animals per group, 20 fields each, true effect 1.5×. This makes the three levels of variation easy to see, and gives us a concrete dataset for the sanity check that follows.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-2">demo <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;">simulate_experiment</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p0 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">change =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CV =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nu =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>,</span>
<span id="cb3-3">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_animals =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_replicates =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>)</span>
<span id="cb3-4"></span>
<span id="cb3-5">per_animal_demo <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> demo <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(EmbryoID, Group) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">animal_rate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span>
<span id="cb3-8"></span>
<span id="cb3-9">truth_demo <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="cb3-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Group =</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ctrl"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cko"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"ctrl"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cko"</span>)),</span>
<span id="cb3-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>)</span>
<span id="cb3-12">)</span>
<span id="cb3-13"></span>
<span id="cb3-14">p1 <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;">ggplot</span>(per_animal_demo, <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> animal_rate, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</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.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> truth_demo, <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> rate, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Group),</span>
<span id="cb3-17">             <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.8</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;">"dashed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_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="at" style="color: #657422;
background-color: null;
font-style: inherit;">ctrl =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#4A7FB8"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cko =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#C0504D"</span>), <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-19">  <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ctrl =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2C4E73"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cko =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7A3431"</span>), <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Per-animal rates (Beta)"</span>,</span>
<span id="cb3-21">       <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;">"observed rate"</span>, <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;">"animals"</span>)</span>
<span id="cb3-22"></span>
<span id="cb3-23">p2 <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;">ggplot</span>(demo, <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> Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</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.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_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="at" style="color: #657422;
background-color: null;
font-style: inherit;">ctrl =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#4A7FB8"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cko =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#C0504D"</span>), <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Per-field counts (CMB)"</span>,</span>
<span id="cb3-27">       <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;">"count per field (m = 500)"</span>, <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;">"fields"</span>)</span>
<span id="cb3-28"></span>
<span id="cb3-29">p3 <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;">ggplot</span>(per_animal_demo, <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> Group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> animal_rate, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_violin</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.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_jitter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</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.4</span>, <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="cb3-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> truth_demo, <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> Group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> rate),</span>
<span id="cb3-33">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <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;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stroke =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, <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;">"black"</span>,</span>
<span id="cb3-34">             <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_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="at" style="color: #657422;
background-color: null;
font-style: inherit;">ctrl =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#4A7FB8"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cko =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#C0504D"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Per-animal rates by group"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"animal rate"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-37">  <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;">"none"</span>)</span>
<span id="cb3-38"></span>
<span id="cb3-39">(p1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> p2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p3</span></code></pre></div>
</details>
<div class="cell-output-display">
<div id="fig-generator" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-generator-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/index_files/figure-html/fig-generator-1.png" class="img-fluid figure-img" width="960">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-generator-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Three levels of variation in the simulator, run at oversized scale (200 animals per group, 20 fields each, true effect 1.5×). Top left: per-animal rates cluster around the true group means (dashed) with Beta scatter at the animal level. Bottom left: per-field counts add within-animal CMB noise on top. Right: per-animal rates by group, X marking the truth that the simulator recovers on average.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="four-methods-on-one-well-resourced-experiment" class="level2">
<h2 class="anchored" data-anchor-id="four-methods-on-one-well-resourced-experiment">Four methods on one well-resourced experiment</h2>
<p>Now to the analysis side. The 2×2 introduced at the start of this post gives four fitters, two per pipeline. All four see exactly the same simulated data; what differs is how they reduce it and what likelihood they assume.</p>
<p><strong>Animal-level (no random effects)</strong> — aggregate fields to one totals row per animal first, then fit:</p>
<ul>
<li><strong>Welch’s t-test</strong> on per-animal proliferation ratios <code>sum(Y) / sum(N)</code>. Unequal variances, with Welch–Satterthwaite degrees of freedom (R’s <code>t.test()</code> returns these by default). The df correction matters at small n: a naive z would be anti-conservative.</li>
<li><strong>CMP GLM</strong> on per-animal totals: <code>glmmTMB(Y_tot ~ Group + offset(log(N_tot)), family = compois())</code>. The <code>offset(log(N_tot))</code> turns count regression into rate regression, so the model fits log-rates with each animal’s denominator entering as statistical weight. The Conway–Maxwell–Poisson dispersion parameter <img src="https://latex.codecogs.com/png.latex?%5Cnu"> is estimated from the data, so the model adapts to overdispersion or underdispersion automatically rather than assuming Poisson variance.</li>
</ul>
<p><strong>Field-level (with random effects)</strong> — keep the field × animal hierarchy and add a random intercept per animal:</p>
<ul>
<li><strong>LMM</strong> on per-field ratios: <code>lmer(Y/N ~ Group + (1 | EmbryoID))</code>. Satterthwaite df via lmerTest, exposed through emmeans.</li>
<li><strong>CMP GLMM</strong> on field-level counts: <code>glmmTMB(Y ~ Group + offset(log(N)) + (1 | EmbryoID), family = compois())</code>. Same count likelihood as the GLM version, but the random intercept lets the model separate between-animal from within-animal variation rather than averaging it away.</li>
</ul>
<p>By aggregating to per-animal totals, Pipeline A sidesteps pseudoreplication at the field level — at the cost of losing within-animal variation. Pipeline B keeps it.</p>
<section id="shared-inference-machinery" class="level3">
<h3 class="anchored" data-anchor-id="shared-inference-machinery">Shared inference machinery</h3>
<p>The same three-step inference setup runs across all four fitters, so that the only methodological difference between methods within a pipeline is the likelihood:</p>
<ol type="1">
<li><strong>emmeans for clean contrasts.</strong> The raw output of <code>summary(glmmTMB_fit)</code> (or <code>lmer</code>) is a coefficient table that’s cumbersome to read as a “cko vs ctrl group contrast on the appropriate scale”. I pull the contrast through <code>emmeans</code>: <code>contrast(emmeans(fit, ~ Group, offset = 0), "revpairwise")</code> returns a clean cko − ctrl difference on the right scale for each method.</li>
<li><strong>Satterthwaite df for finite-n correction.</strong> Each <em>pipeline</em> shares a single df: the Gaussian fitter computes it natively (Welch’s t-test in Pipeline A, lmerTest in Pipeline B), and the count-likelihood fitter inherits it via <code>emmeans(..., df = ...)</code>. So Welch and CMP GLM use the same df; LMM and CMP GLMM use the same df. The only remaining difference between methods within a pipeline is the likelihood.</li>
<li><strong>Delta-transform for common-scale presentation.</strong> Welch’s t-test and LMM return rate differences natively; CMP returns log-rate-ratios. I delta-transform the Gaussian outputs to log-rate-ratio so all four methods report on the same scale, with the same Satterthwaite df propagating to the CI half-widths.</li>
</ol>
<p>The two fitter functions implementing all of the above:</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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">fit_no_re <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>(data, change, p0) {</span>
<span id="cb4-2">  truth_ttest <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (change <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="cb4-3">  truth_logRR <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;">log</span>(change)</span>
<span id="cb4-4"></span>
<span id="cb4-5">  per_animal <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(EmbryoID, Group) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y_tot =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(Y), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N_tot =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(N), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ratio =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(Y) <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;">sum</span>(N),</span>
<span id="cb4-8">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span>
<span id="cb4-9"></span>
<span id="cb4-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Welch's t-test, native rate-difference scale</span></span>
<span id="cb4-11">  tt <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;">tryCatch</span>({</span>
<span id="cb4-12">    t <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;">t.test</span>(ratio <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> per_animal, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var.equal =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb4-13">    <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;">pvalue =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>parameter),</span>
<span id="cb4-14">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</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;">diff</span>(t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate)),</span>
<span id="cb4-15">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>conf.int[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>conf.int[<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;">converged =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb4-16">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <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;">pvalue =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb4-17">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb4-18">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb4-19">  df_use <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;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>df)) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>df</span>
<span id="cb4-20"></span>
<span id="cb4-21">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># t-test on log-RR via delta method</span></span>
<span id="cb4-22">  tt_logRR <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;">tryCatch</span>({</span>
<span id="cb4-23">    grp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> per_animal <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;">group_by</span>(Group) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-24">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(ratio), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(ratio) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span>
<span id="cb4-25">    mc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>m[grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"ctrl"</span>]; mk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>m[grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"cko"</span>]</span>
<span id="cb4-26">    sc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>s[grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"ctrl"</span>]; sk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>s[grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"cko"</span>]</span>
<span id="cb4-27">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (mc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</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> mk <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"non-positive group mean"</span>)</span>
<span id="cb4-28">    est <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;">log</span>(mk <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> mc); se <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;">sqrt</span>((sk<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>mk)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (sc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>mc)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb4-29">    tcrit <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;">qt</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.975</span>, df_use)</span>
<span id="cb4-30">    <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;">estimate =</span> est, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> est <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> tcrit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>se, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> est <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> tcrit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>se)</span>
<span id="cb4-31">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <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;">estimate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>))</span>
<span id="cb4-32"></span>
<span id="cb4-33">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CMP GLM on per-animal totals</span></span>
<span id="cb4-34">  cmp <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;">tryCatch</span>({</span>
<span id="cb4-35">    fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressWarnings</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(Y_tot <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group <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;">offset</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(N_tot)),</span>
<span id="cb4-36">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compois</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> per_animal))</span>
<span id="cb4-37">    ok  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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.null</span>(fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>convergence) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>convergence <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;">&amp;&amp;</span></span>
<span id="cb4-38">           <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.null</span>(fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sdr) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">isTRUE</span>(fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sdr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pdHess)</span>
<span id="cb4-39">    con <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;">contrast</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(fit, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">offset =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"revpairwise"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> df_use)</span>
<span id="cb4-40">    cs  <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(con)); ci <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">confint</span>(con))</span>
<span id="cb4-41">    <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;">pvalue =</span> cs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value[<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;">estimate =</span> cs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb4-42">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> ci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower.CL[<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;">CI_hi =</span> ci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper.CL[<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;">converged =</span> ok)</span>
<span id="cb4-43">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <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;">pvalue =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb4-44">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb4-45"></span>
<span id="cb4-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb4-47">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method      =</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;">"t_test"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cmp_glm"</span>),</span>
<span id="cb4-48">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pvalue      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pvalue, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pvalue),</span>
<span id="cb4-49">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate),</span>
<span id="cb4-50">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo),</span>
<span id="cb4-51">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi),</span>
<span id="cb4-52">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate_logRR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt_logRR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate),</span>
<span id="cb4-53">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt_logRR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo,    cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo),</span>
<span id="cb4-54">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt_logRR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi,    cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi),</span>
<span id="cb4-55">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt_logRR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> tt_logRR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo,</span>
<span id="cb4-56">                       cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo),</span>
<span id="cb4-57">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(truth_logRR, truth_logRR),</span>
<span id="cb4-58">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>converged, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>converged)</span>
<span id="cb4-59">  )</span>
<span id="cb4-60">}</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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">fit_re <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>(data, change, p0) {</span>
<span id="cb5-2">  truth_logRR <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;">log</span>(change)</span>
<span id="cb5-3">  data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rate <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y <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;">$</span>N</span>
<span id="cb5-4"></span>
<span id="cb5-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># LMM on per-field ratios (native = rate diff)</span></span>
<span id="cb5-6">  lmm_n <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;">tryCatch</span>({</span>
<span id="cb5-7">    fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressMessages</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmer</span>(rate <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> EmbryoID), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data))</span>
<span id="cb5-8">    con <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;">contrast</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(fit, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"revpairwise"</span>)</span>
<span id="cb5-9">    cs  <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(con)); ci <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">confint</span>(con))</span>
<span id="cb5-10">    <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;">fit =</span> fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pvalue =</span> cs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value[<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;">df =</span> cs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>df[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb5-11">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> cs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate[<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;">CI_lo =</span> ci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower.CL[<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;">CI_hi =</span> ci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper.CL[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb5-12">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb5-13">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <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;">fit =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pvalue =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb5-14">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb5-15">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb5-16"></span>
<span id="cb5-17">  df_use <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;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(lmm_n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>df)) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> lmm_n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>df</span>
<span id="cb5-18"></span>
<span id="cb5-19">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># LMM log-RR via delta with per-group emmeans SEs</span></span>
<span id="cb5-20">  lmm_l <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;">tryCatch</span>({</span>
<span id="cb5-21">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.null</span>(lmm_n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"no LMM"</span>)</span>
<span id="cb5-22">    es <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(lmm_n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group)))</span>
<span id="cb5-23">    mc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>emmean[es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"ctrl"</span>]; mk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>emmean[es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"cko"</span>]</span>
<span id="cb5-24">    sc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>SE[es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"ctrl"</span>];     sk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>SE[es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"cko"</span>]</span>
<span id="cb5-25">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (mc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</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> mk <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"non-positive group mean"</span>)</span>
<span id="cb5-26">    est <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;">log</span>(mk<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>mc); se <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;">sqrt</span>((sk<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>mk)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (sc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>mc)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb5-27">    tcrit <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;">qt</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.975</span>, df_use)</span>
<span id="cb5-28">    <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;">estimate =</span> est, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> est <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> tcrit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>se, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> est <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> tcrit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>se)</span>
<span id="cb5-29">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <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;">estimate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>))</span>
<span id="cb5-30"></span>
<span id="cb5-31">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CMP GLMM on field-level counts (native = log-RR)</span></span>
<span id="cb5-32">  cmp <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;">tryCatch</span>({</span>
<span id="cb5-33">    fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressWarnings</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group <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;">offset</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(N)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> EmbryoID),</span>
<span id="cb5-34">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compois</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data))</span>
<span id="cb5-35">    ok  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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.null</span>(fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>convergence) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>convergence <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;">&amp;&amp;</span></span>
<span id="cb5-36">           <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.null</span>(fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sdr) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">isTRUE</span>(fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sdr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pdHess)</span>
<span id="cb5-37">    con <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;">contrast</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(fit, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">offset =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"revpairwise"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> df_use)</span>
<span id="cb5-38">    cs <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(con)); ci <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">confint</span>(con))</span>
<span id="cb5-39">    <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;">pvalue =</span> cs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value[<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;">estimate =</span> cs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb5-40">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> ci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower.CL[<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;">CI_hi =</span> ci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper.CL[<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;">converged =</span> ok)</span>
<span id="cb5-41">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <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;">pvalue =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb5-42">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb5-43"></span>
<span id="cb5-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb5-45">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method      =</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;">"lmm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cmp_glmm"</span>),</span>
<span id="cb5-46">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pvalue      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lmm_n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pvalue, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pvalue),</span>
<span id="cb5-47">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df_used     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lmm_n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>df, df_use),</span>
<span id="cb5-48">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate_logRR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lmm_l<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate),</span>
<span id="cb5-49">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lmm_l<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo,    cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo),</span>
<span id="cb5-50">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lmm_l<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi,    cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi),</span>
<span id="cb5-51">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lmm_l<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lmm_l<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo),</span>
<span id="cb5-52">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(truth_logRR, truth_logRR),</span>
<span id="cb5-53">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lmm_n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>converged, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>converged)</span>
<span id="cb5-54">  )</span>
<span id="cb5-55">}</span></code></pre></div>
</details>
</div>
</section>
<section id="sanity-check-at-scale" class="level3">
<h3 class="anchored" data-anchor-id="sanity-check-at-scale">Sanity check at scale</h3>
<p>With all the machinery in place, let me check that all four methods recover the truth when the experiment is generously sized: 200 animals × 20 fields × 500 cells, true effect 1.5×, statistical paradise.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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">res_ideal <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;">bind_rows</span>(</span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_no_re</span>(demo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">change =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p0 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>) <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;">pipeline =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Animal-level"</span>),</span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_re</span>(demo,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">change =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p0 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>) <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;">pipeline =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Field-level"</span>)</span>
<span id="cb6-4">)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>Note: D.f. calculations have been disabled because the number of observations exceeds 3000.
To enable adjustments, add the argument 'pbkrtest.limit = 8000' (or larger)
[or, globally, 'set emm_options(pbkrtest.limit = 8000)' or larger];
but be warned that this may result in large computation time and memory use.</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Note: D.f. calculations have been disabled because the number of observations exceeds 3000.
To enable adjustments, add the argument 'lmerTest.limit = 8000' (or larger)
[or, globally, 'set emm_options(lmerTest.limit = 8000)' or larger];
but be warned that this may result in large computation time and memory use.</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Note: D.f. calculations have been disabled because the number of observations exceeds 3000.
To enable adjustments, add the argument 'pbkrtest.limit = 8000' (or larger)
[or, globally, 'set emm_options(pbkrtest.limit = 8000)' or larger];
but be warned that this may result in large computation time and memory use.</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Note: D.f. calculations have been disabled because the number of observations exceeds 3000.
To enable adjustments, add the argument 'lmerTest.limit = 8000' (or larger)
[or, globally, 'set emm_options(lmerTest.limit = 8000)' or larger];
but be warned that this may result in large computation time and memory use.</code></pre>
</div>
<details class="code-fold">
<summary>Show code</summary>
<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">method_label <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;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t_test  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Welch's t-test (\u0394)"</span>,</span>
<span id="cb11-2">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cmp_glm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLM"</span>,</span>
<span id="cb11-3">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lmm     =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LMM (\u0394)"</span>,</span>
<span id="cb11-4">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cmp_glmm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLMM"</span>)</span>
<span id="cb11-5"></span>
<span id="cb11-6">res_ideal <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transmute</span>(</span>
<span id="cb11-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Pipeline   =</span> pipeline,</span>
<span id="cb11-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Method     =</span> method_label[method],</span>
<span id="cb11-10">    <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;">log RR</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>   <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;">round</span>(estimate_logRR, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb11-11">    <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;">95% CI</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>   <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;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[%.4f, %.4f]"</span>, CI_lo_logRR, CI_hi_logRR),</span>
<span id="cb11-12">    <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;">% change</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <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;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%+.1f%%"</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;">exp</span>(estimate_logRR) <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="cb11-13">    <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;">% CI</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>     <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;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[%+.1f%%, %+.1f%%]"</span>,</span>
<span id="cb11-14">                         <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;">exp</span>(CI_lo_logRR) <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="cb11-15">                         <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;">exp</span>(CI_hi_logRR) <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="cb11-16">    <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;">p-value</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>  <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;">vapply</span>(pvalue, fmt_p, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">character</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)),</span>
<span id="cb11-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Truth      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb11-18">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>()</span></code></pre></div>
</details>
<div id="tbl-ideal" class="cell quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-ideal-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: Common-scale results on the ideal demo dataset (200 animals × 20 fields × 500 cells, true effect 1.5×). All four methods detect the effect emphatically and recover the truth.
</figcaption>
<div aria-describedby="tbl-ideal-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="do-not-create-environment cell caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 12%">
<col style="width: 17%">
<col style="width: 6%">
<col style="width: 16%">
<col style="width: 8%">
<col style="width: 16%">
<col style="width: 16%">
<col style="width: 6%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Pipeline</th>
<th style="text-align: left;">Method</th>
<th style="text-align: right;">log RR</th>
<th style="text-align: left;">95% CI</th>
<th style="text-align: left;">% change</th>
<th style="text-align: left;">% CI</th>
<th style="text-align: left;">p-value</th>
<th style="text-align: right;">Truth</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Animal-level</td>
<td style="text-align: left;">Welch’s t-test (Δ)</td>
<td style="text-align: right;">0.4165</td>
<td style="text-align: left;">[0.3688, 0.4643]</td>
<td style="text-align: left;">+51.7%</td>
<td style="text-align: left;">[+44.6%, +59.1%]</td>
<td style="text-align: left;">p = 3.6 × 10^-46</td>
<td style="text-align: right;">0.4055</td>
</tr>
<tr class="even">
<td style="text-align: left;">Animal-level</td>
<td style="text-align: left;">CMP GLM</td>
<td style="text-align: right;">0.4165</td>
<td style="text-align: left;">[0.3682, 0.4649]</td>
<td style="text-align: left;">+51.7%</td>
<td style="text-align: left;">[+44.5%, +59.2%]</td>
<td style="text-align: left;">p = 2.1 × 10^-47</td>
<td style="text-align: right;">0.4055</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Field-level</td>
<td style="text-align: left;">LMM (Δ)</td>
<td style="text-align: right;">0.4165</td>
<td style="text-align: left;">[0.3650, 0.4681]</td>
<td style="text-align: left;">+51.7%</td>
<td style="text-align: left;">[+44.1%, +59.7%]</td>
<td style="text-align: left;">p = 3.5 × 10^-62</td>
<td style="text-align: right;">0.4055</td>
</tr>
<tr class="even">
<td style="text-align: left;">Field-level</td>
<td style="text-align: left;">CMP GLMM</td>
<td style="text-align: right;">0.4170</td>
<td style="text-align: left;">[0.3650, 0.4681]</td>
<td style="text-align: left;">+51.7%</td>
<td style="text-align: left;">[+44.1%, +59.7%]</td>
<td style="text-align: left;">p = 1.5 × 10^-62</td>
<td style="text-align: right;">0.4055</td>
</tr>
</tbody>
</table>
</div>
</div>
</figure>
</div>
</div>
<p>All four methods agree to four decimal places: each recovers the <em>sample</em> log-RR of this particular dataset (≈ 0.4165), which sits about 0.4 standard errors above the <em>true</em> log-RR of <img src="https://latex.codecogs.com/png.latex?%5Clog(1.5)%20%5Capprox%200.4055">. With <img src="https://latex.codecogs.com/png.latex?n%20=%20200"> animals per group, the sampling SE of the log-RR estimate is ~0.024 — so a deviation of ±0.01 from truth in any one realization is exactly what we expect. Truth is inside every 95% CI, every p-value is astronomical, and the four methods give numerically indistinguishable answers. At this sample size, the choice of method doesn’t matter; only the data does.</p>
<p>But no real experiment has 200 animals. The interesting question is what happens in the messier universe biologists actually work in.</p>
</section>
</section>
<section id="a-universe-of-experiments" class="level2">
<h2 class="anchored" data-anchor-id="a-universe-of-experiments">A universe of experiments</h2>
<p>So the ideal case is settled: all four methods work fine when the experiment is large enough. The interesting question is the real-world case, where it usually isn’t.</p>
<p>Real proliferation studies come from biological material that’s expensive and ethically constrained. Six animals per group is rich. Four is common, three is typical. Field counts vary with tissue and stain, between-animal variability changes with the system, and every lab’s pipeline has its own quirks.</p>
<p>The honest question is not “do these methods work?” With enough data they all do. The honest question is: across the messy, resource-limited, variable experiments biologists actually run, <strong>how do they behave?</strong></p>
<p>I want a <em>surface</em>, not a point estimate: each method’s Type-I, power, and CI precision as continuous functions of the six knobs that define a real experiment. The approach: define the design space, sample it with <strong>Latin hypercube sampling</strong>, run hundreds of simulations at each sampled point with all four fitters, and smooth the noisy per-point estimates with <strong>Gaussian process</strong> emulators. The GP is what turns 300 noisy point estimates into a continuous surface I can query anywhere in the design space, with calibrated uncertainty in regions where designs are sparse. Once the emulators are in place, Sobol decomposition lets me ask which knobs each method is actually responding to.</p>
<p><strong>Two parallel campaigns.</strong> The <strong>null campaign</strong> fixes <code>change = 1</code> and varies the other five knobs; from it I get Type-I error per design point, with 500 simulations per design (Type-I is the small probability we most need to pin down precisely). The <strong>main campaign</strong> varies all six knobs and uses 200 simulations per design; from it I get raw power and CI width. Splitting them this way also keeps the Type-I estimate from contaminating the power estimate. Each campaign runs for both pipelines, so four campaigns total.</p>
<section id="designing-the-lhs-sample" class="level3">
<h3 class="anchored" data-anchor-id="designing-the-lhs-sample">Designing the LHS sample</h3>
<p>Each knob is sampled the way its physical meaning suggests. Knobs with multiplicative biology — effect size <code>change</code>, baseline rate <code>p0</code>, cells per field <code>m</code>, dispersion <code>ν</code> — are sampled <strong>log-uniformly</strong> because a change from 0.03 to 0.06 is the same biological story as one from 0.20 to 0.40 (both are doublings, not “+0.03” or “+0.20”). Sampling these uniformly on the linear scale would over-represent the large end of each range, where small absolute differences become biologically meaningless. The between-animal CV is sampled <strong>uniformly on the linear scale</strong>, since it’s already a ratio that doesn’t need a further log. <code>n_animals</code> is sampled <strong>uniformly on a discrete set</strong> {3, 4, 5, 6, 7, 8, 10, 12} that covers the realistic range with denser coverage at small n where method differences matter most.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressPackageStartupMessages</span>({</span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lhs)</span>
<span id="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(GGally)</span>
<span id="cb12-4">})</span>
<span id="cb12-5"></span>
<span id="cb12-6"><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;">42</span>)</span>
<span id="cb12-7">N_DEMO <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;">300</span></span>
<span id="cb12-8">u_demo <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;">randomLHS</span>(N_DEMO, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span>
<span id="cb12-9"></span>
<span id="cb12-10">design_demo <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="cb12-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">change    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loguni</span>(u_demo[, <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;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>),</span>
<span id="cb12-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p0        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loguni</span>(u_demo[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.40</span>),</span>
<span id="cb12-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CV        =</span> u_demo[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span>,</span>
<span id="cb12-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_animals =</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;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</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;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(u_demo[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</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="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="cb12-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loguni</span>(u_demo[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>)),</span>
<span id="cb12-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nu        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loguni</span>(u_demo[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>], <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>)</span>
<span id="cb12-17">)</span>
<span id="cb12-18"></span>
<span id="cb12-19">log_vars <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"change"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"m"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nu"</span>)</span>
<span id="cb12-20">varnames <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;">names</span>(design_demo)</span>
<span id="cb12-21"></span>
<span id="cb12-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Lower triangle: scatter with jitter only on the n_animals axis (so the</span></span>
<span id="cb12-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># eight discrete levels don't collapse into hard vertical stripes).</span></span>
<span id="cb12-24">lower_panel <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>(data, mapping, ...) {</span>
<span id="cb12-25">  x_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rlang<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;">as_label</span>(mapping<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x)</span>
<span id="cb12-26">  y_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rlang<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;">as_label</span>(mapping<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y)</span>
<span id="cb12-27">  jx <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;">if</span> (x_var <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;">"n_animals"</span>) <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb12-28">  jy <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;">if</span> (y_var <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;">"n_animals"</span>) <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb12-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, mapping) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-30">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_jitter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> jx, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> jy, <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;">1</span>),</span>
<span id="cb12-31">               <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.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <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;">"#4A7FB8"</span>)</span>
<span id="cb12-32">}</span>
<span id="cb12-33"></span>
<span id="cb12-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Diagonal: histogram with bins matched to each variable's sampling scheme.</span></span>
<span id="cb12-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Log-spaced bins for log-sampled vars → uniform sample shows flat. Integer</span></span>
<span id="cb12-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># bins for n_animals so each discrete level is its own bar.</span></span>
<span id="cb12-37">diag_panel <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>(data, mapping, ...) {</span>
<span id="cb12-38">  x_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rlang<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;">as_label</span>(mapping<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x)</span>
<span id="cb12-39">  vals  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data[[x_var]]</span>
<span id="cb12-40">  brks  <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;">if</span> (x_var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> log_vars) {</span>
<span id="cb12-41">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</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;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(vals)), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(vals)), <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;">22</span>)</span>
<span id="cb12-42">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (x_var <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;">"n_animals"</span>) {</span>
<span id="cb12-43">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12.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="cb12-44">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb12-45">    <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>(vals), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(vals), <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;">22</span>)</span>
<span id="cb12-46">  }</span>
<span id="cb12-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, mapping) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-48">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> brks, <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;">"#4A7FB8"</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.85</span>,</span>
<span id="cb12-49">                   <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;">"white"</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.2</span>)</span>
<span id="cb12-50">}</span>
<span id="cb12-51"></span>
<span id="cb12-52">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;">ggpairs</span>(</span>
<span id="cb12-53">  design_demo,</span>
<span id="cb12-54">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</span> varnames,</span>
<span id="cb12-55">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower   =</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;">continuous =</span> lower_panel),</span>
<span id="cb12-56">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diag    =</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;">continuous =</span> diag_panel),</span>
<span id="cb12-57">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper   =</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;">continuous =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blank"</span>),</span>
<span id="cb12-58">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axisLabels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"show"</span></span>
<span id="cb12-59">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-60">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-61">  <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;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb12-62">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<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;">"grey95"</span>),</span>
<span id="cb12-63">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<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;">9</span>))</span>
<span id="cb12-64"></span>
<span id="cb12-65"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Apply log scales to panels whose underlying variable is log-sampled.</span></span>
<span id="cb12-66"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(varnames)) {</span>
<span id="cb12-67">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(varnames)) {</span>
<span id="cb12-68">    sub <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p[i, j]</span>
<span id="cb12-69">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (varnames[j] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> log_vars) sub <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sub <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_x_log10</span>()</span>
<span id="cb12-70">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> j <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> varnames[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> log_vars) sub <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sub <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_y_log10</span>()</span>
<span id="cb12-71">    p[i, j] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sub</span>
<span id="cb12-72">  }</span>
<span id="cb12-73">}</span>
<span id="cb12-74"></span>
<span id="cb12-75">p</span></code></pre></div>
</details>
<div class="cell-output-display">
<div id="fig-lhs-pairs" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-lhs-pairs-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/index_files/figure-html/fig-lhs-pairs-1.png" class="img-fluid figure-img" width="816">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-lhs-pairs-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: 300-point Latin hypercube design across all six knobs. Diagonal: marginal distribution per knob, with log-spaced bins for the log-sampled axes and one bin per integer for n_animals. Lower triangle: every two-way scatter, with the x- and y-axes set to log scale on the log-sampled variables. Uniform marginal coverage shows as flat histograms on the diagonal; the space-filling property of LHS shows as evenly-spread point clouds in every scatter. I added jitter to <code>n_animals</code> for visualization purposes
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="reproducing-the-pipeline" class="level3">
<h3 class="anchored" data-anchor-id="reproducing-the-pipeline">Reproducing the pipeline</h3>
<p>The whole pipeline — four LHS campaigns, 12 GP emulators, Sobol indices on six of them — takes several hours of compute on a laptop, so I don’t re-run it during render. <strong>Two options for the reader:</strong> download the precomputed <code>.rds</code> bundles (skip to the loading chunk below), or run the three scripts below to rebuild everything from scratch. The three chunks are self-contained — concatenate them into a single <code>.R</code> file in order and source it. They produce nine <code>.rds</code> bundles in the working directory; the rest of the post loads from those.</p>
<div class="cell">
<details class="code-fold">
<summary>Script 1 — build the four LHS campaigns (several hours)</summary>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ============================================================================</span></span>
<span id="cb13-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Script 1: build LHS campaigns</span></span>
<span id="cb13-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Produces: lhs_null_campaign.rds, lhs_campaign.rds,</span></span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#           lhs_null_campaign_re.rds, lhs_campaign_re.rds</span></span>
<span id="cb13-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ============================================================================</span></span>
<span id="cb13-6"></span>
<span id="cb13-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressPackageStartupMessages</span>({</span>
<span id="cb13-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr); <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(parallel); <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lhs)</span>
<span id="cb13-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(COMMultReg)                    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># r_cmb, e_cmb</span></span>
<span id="cb13-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(glmmTMB); <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(emmeans)</span>
<span id="cb13-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lme4); <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lmerTest)</span>
<span id="cb13-12">})</span>
<span id="cb13-13"></span>
<span id="cb13-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---- Data generator --------------------------------------------------------</span></span>
<span id="cb13-15"></span>
<span id="cb13-16">p_from_rate <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>(target_rate, m, nu) {</span>
<span id="cb13-17">  target_rate <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;">pmax</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(target_rate, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.999</span>), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>)</span>
<span id="cb13-18">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(nu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-10</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(target_rate)</span>
<span id="cb13-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tryCatch</span>(</span>
<span id="cb13-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">uniroot</span>(<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">e_cmb</span>(m, p, nu) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> m <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> target_rate,</span>
<span id="cb13-21">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper =</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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tol =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>root,</span>
<span id="cb13-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) target_rate</span>
<span id="cb13-23">  )</span>
<span id="cb13-24">}</span>
<span id="cb13-25"></span>
<span id="cb13-26">draw_animal_rates <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>(mu, CV, n) {</span>
<span id="cb13-27">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (CV <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(mu, n))</span>
<span id="cb13-28">  sigma2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> CV)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb13-29">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (sigma2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu)) {</span>
<span id="cb13-30">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</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;">"Beta infeasible: mu=%.3g with CV=%.3g requires CV &lt; %.3g"</span>,</span>
<span id="cb13-31">                 mu, CV, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> mu) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> mu)))</span>
<span id="cb13-32">  }</span>
<span id="cb13-33">  conc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sigma2 <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="cb13-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbeta</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape1 =</span> mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> conc, <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> conc)</span>
<span id="cb13-35">}</span>
<span id="cb13-36"></span>
<span id="cb13-37">simulate_experiment <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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p0 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">change =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CV =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nu =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>,</span>
<span id="cb13-38">                                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_animals =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_replicates =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>) {</span>
<span id="cb13-39">  wt_rates <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;">draw_animal_rates</span>(p0,          CV, n_animals)</span>
<span id="cb13-40">  ko_rates <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;">draw_animal_rates</span>(p0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> change, CV, n_animals)</span>
<span id="cb13-41">  wt_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;">vapply</span>(wt_rates, p_from_rate, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</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;">m =</span> m, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nu =</span> nu)</span>
<span id="cb13-42">  ko_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;">vapply</span>(ko_rates, p_from_rate, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</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;">m =</span> m, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nu =</span> nu)</span>
<span id="cb13-43">  wt_counts <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;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(wt_p, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p)</span>
<span id="cb13-44">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replicate</span>(n_replicates, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">r_cmb</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, m, p, nu))))</span>
<span id="cb13-45">  ko_counts <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;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(ko_p, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p)</span>
<span id="cb13-46">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replicate</span>(n_replicates, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">r_cmb</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, m, p, nu))))</span>
<span id="cb13-47">  EmbryoID <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;">c</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;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ctrl"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n_animals)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> n_replicates),</span>
<span id="cb13-48">                <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;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cko"</span>,  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n_animals)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> n_replicates))</span>
<span id="cb13-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb13-50">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(wt_counts, ko_counts)),</span>
<span id="cb13-51">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N        =</span> m,</span>
<span id="cb13-52">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Group    =</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;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ctrl"</span>, n_animals <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n_replicates),</span>
<span id="cb13-53">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cko"</span>,  n_animals <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n_replicates)),</span>
<span id="cb13-54">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"ctrl"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cko"</span>)),</span>
<span id="cb13-55">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">EmbryoID =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(EmbryoID)</span>
<span id="cb13-56">  )</span>
<span id="cb13-57">}</span>
<span id="cb13-58"></span>
<span id="cb13-59"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---- Fitters ---------------------------------------------------------------</span></span>
<span id="cb13-60"></span>
<span id="cb13-61">fit_no_re <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>(data, change, p0) {</span>
<span id="cb13-62">  truth_logRR <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;">log</span>(change)</span>
<span id="cb13-63">  per_animal <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-64">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(EmbryoID, Group) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-65">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y_tot =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(Y), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N_tot =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(N), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ratio =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(Y) <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;">sum</span>(N),</span>
<span id="cb13-66">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span>
<span id="cb13-67"></span>
<span id="cb13-68">  tt <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;">tryCatch</span>({</span>
<span id="cb13-69">    t <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;">t.test</span>(ratio <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> per_animal, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var.equal =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb13-70">    <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;">pvalue =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>parameter),</span>
<span id="cb13-71">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>conf.int[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>conf.int[<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;">converged =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb13-72">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <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;">pvalue =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb13-73">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb13-74">  df_use <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;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>df)) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>df</span>
<span id="cb13-75"></span>
<span id="cb13-76">  tt_logRR <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;">tryCatch</span>({</span>
<span id="cb13-77">    grp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> per_animal <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;">group_by</span>(Group) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-78">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(ratio), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(ratio) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span>
<span id="cb13-79">    mc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>m[grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"ctrl"</span>]; mk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>m[grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"cko"</span>]</span>
<span id="cb13-80">    sc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>s[grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"ctrl"</span>]; sk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>s[grp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"cko"</span>]</span>
<span id="cb13-81">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (mc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</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> mk <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"non-positive group mean"</span>)</span>
<span id="cb13-82">    est <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;">log</span>(mk <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> mc); se <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;">sqrt</span>((sk<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>mk)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (sc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>mc)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb13-83">    tcrit <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;">qt</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.975</span>, df_use)</span>
<span id="cb13-84">    <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;">estimate =</span> est, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> est <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> tcrit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>se, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> est <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> tcrit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>se)</span>
<span id="cb13-85">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <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;">estimate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>))</span>
<span id="cb13-86"></span>
<span id="cb13-87">  cmp <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;">tryCatch</span>({</span>
<span id="cb13-88">    fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressWarnings</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(Y_tot <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group <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;">offset</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(N_tot)),</span>
<span id="cb13-89">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compois</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> per_animal))</span>
<span id="cb13-90">    ok <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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.null</span>(fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>convergence) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>convergence <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;">&amp;&amp;</span></span>
<span id="cb13-91">          <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.null</span>(fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sdr) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">isTRUE</span>(fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sdr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pdHess)</span>
<span id="cb13-92">    con <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;">contrast</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(fit, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">offset =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"revpairwise"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> df_use)</span>
<span id="cb13-93">    cs <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(con)); ci <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">confint</span>(con))</span>
<span id="cb13-94">    <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;">pvalue =</span> cs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value[<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;">estimate =</span> cs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb13-95">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> ci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower.CL[<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;">CI_hi =</span> ci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper.CL[<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;">converged =</span> ok)</span>
<span id="cb13-96">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <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;">pvalue =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb13-97">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb13-98"></span>
<span id="cb13-99">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb13-100">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method         =</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;">"t_test"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cmp_glm"</span>),</span>
<span id="cb13-101">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pvalue         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pvalue, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pvalue),</span>
<span id="cb13-102">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate_logRR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt_logRR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate),</span>
<span id="cb13-103">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt_logRR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo),</span>
<span id="cb13-104">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt_logRR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi),</span>
<span id="cb13-105">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt_logRR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> tt_logRR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo,</span>
<span id="cb13-106">                       cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo),</span>
<span id="cb13-107">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(truth_logRR, truth_logRR),</span>
<span id="cb13-108">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>converged, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>converged)</span>
<span id="cb13-109">  )</span>
<span id="cb13-110">}</span>
<span id="cb13-111"></span>
<span id="cb13-112">fit_re <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>(data, change, p0) {</span>
<span id="cb13-113">  truth_logRR <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;">log</span>(change)</span>
<span id="cb13-114">  data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rate <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y <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;">$</span>N</span>
<span id="cb13-115"></span>
<span id="cb13-116">  lmm_n <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;">tryCatch</span>({</span>
<span id="cb13-117">    fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressMessages</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmer</span>(rate <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> EmbryoID), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data))</span>
<span id="cb13-118">    con <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;">contrast</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(fit, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"revpairwise"</span>)</span>
<span id="cb13-119">    cs <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(con)); ci <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">confint</span>(con))</span>
<span id="cb13-120">    <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;">fit =</span> fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pvalue =</span> cs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value[<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;">df =</span> cs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>df[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb13-121">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> cs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate[<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;">CI_lo =</span> ci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower.CL[<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;">CI_hi =</span> ci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper.CL[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb13-122">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb13-123">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <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;">fit =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pvalue =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb13-124">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb13-125">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb13-126">  df_use <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;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(lmm_n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>df)) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> lmm_n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>df</span>
<span id="cb13-127"></span>
<span id="cb13-128">  lmm_l <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;">tryCatch</span>({</span>
<span id="cb13-129">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.null</span>(lmm_n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"no LMM"</span>)</span>
<span id="cb13-130">    es <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(lmm_n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group)))</span>
<span id="cb13-131">    mc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>emmean[es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"ctrl"</span>]; mk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>emmean[es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"cko"</span>]</span>
<span id="cb13-132">    sc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>SE[es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"ctrl"</span>];     sk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>SE[es<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Group <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;">"cko"</span>]</span>
<span id="cb13-133">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (mc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</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> mk <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"non-positive group mean"</span>)</span>
<span id="cb13-134">    est <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;">log</span>(mk <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> mc); se <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;">sqrt</span>((sk<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>mk)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (sc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>mc)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb13-135">    tcrit <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;">qt</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.975</span>, df_use)</span>
<span id="cb13-136">    <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;">estimate =</span> est, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> est <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> tcrit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>se, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> est <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> tcrit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>se)</span>
<span id="cb13-137">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <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;">estimate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>))</span>
<span id="cb13-138"></span>
<span id="cb13-139">  cmp <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;">tryCatch</span>({</span>
<span id="cb13-140">    fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressWarnings</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmmTMB</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group <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;">offset</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(N)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> EmbryoID),</span>
<span id="cb13-141">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compois</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data))</span>
<span id="cb13-142">    ok <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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.null</span>(fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>convergence) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>convergence <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;">&amp;&amp;</span></span>
<span id="cb13-143">          <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.null</span>(fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sdr) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">isTRUE</span>(fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sdr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pdHess)</span>
<span id="cb13-144">    con <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;">contrast</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">emmeans</span>(fit, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">offset =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"revpairwise"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> df_use)</span>
<span id="cb13-145">    cs <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(con)); ci <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">confint</span>(con))</span>
<span id="cb13-146">    <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;">pvalue =</span> cs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value[<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;">estimate =</span> cs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb13-147">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> ci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower.CL[<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;">CI_hi =</span> ci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper.CL[<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;">converged =</span> ok)</span>
<span id="cb13-148">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <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;">pvalue =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb13-149">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb13-150"></span>
<span id="cb13-151">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb13-152">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method         =</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;">"lmm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cmp_glmm"</span>),</span>
<span id="cb13-153">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pvalue         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lmm_n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pvalue, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pvalue),</span>
<span id="cb13-154">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df_used        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lmm_n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>df, df_use),</span>
<span id="cb13-155">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate_logRR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lmm_l<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate),</span>
<span id="cb13-156">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_lo_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lmm_l<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo),</span>
<span id="cb13-157">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CI_hi_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lmm_l<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi),</span>
<span id="cb13-158">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lmm_l<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lmm_l<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_hi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CI_lo),</span>
<span id="cb13-159">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth_logRR    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(truth_logRR, truth_logRR),</span>
<span id="cb13-160">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">converged      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lmm_n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>converged, cmp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>converged)</span>
<span id="cb13-161">  )</span>
<span id="cb13-162">}</span>
<span id="cb13-163"></span>
<span id="cb13-164"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---- LHS design ------------------------------------------------------------</span></span>
<span id="cb13-165"></span>
<span id="cb13-166">loguni <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>(u, lo, hi) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(lo) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> u <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>(hi) <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>(lo)))</span>
<span id="cb13-167"></span>
<span id="cb13-168"><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;">42</span>)</span>
<span id="cb13-169">N_DESIGNS <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;">300</span></span>
<span id="cb13-170">u <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;">randomLHS</span>(N_DESIGNS, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span>
<span id="cb13-171"></span>
<span id="cb13-172">design_main <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-173">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point_id     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(N_DESIGNS),</span>
<span id="cb13-174">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">change       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loguni</span>(u[, <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;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>),</span>
<span id="cb13-175">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p0           =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loguni</span>(u[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.40</span>),</span>
<span id="cb13-176">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CV           =</span> u[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span>,</span>
<span id="cb13-177">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_animals    =</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;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</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;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(u[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</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="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="cb13-178">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m            =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loguni</span>(u[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>)),</span>
<span id="cb13-179">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nu           =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loguni</span>(u[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>], <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>),</span>
<span id="cb13-180">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_replicates =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb13-181">)</span>
<span id="cb13-182">design_null <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;">transform</span>(design_main, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">change =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb13-183"></span>
<span id="cb13-184"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---- Per-point runner (parallel via mclapply) ------------------------------</span></span>
<span id="cb13-185"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Windows users: replace mclapply with parLapply or set mc.cores = 1.</span></span>
<span id="cb13-186"></span>
<span id="cb13-187">run_one_point <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>(pars, n_sim, fitter, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_cores =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) {</span>
<span id="cb13-188">  sim_pars <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.list</span>(pars[, <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;">"p0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"change"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CV"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nu"</span>,</span>
<span id="cb13-189">                               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n_animals"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n_replicates"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"m"</span>)])</span>
<span id="cb13-190">  one_sim <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>(i) {</span>
<span id="cb13-191">    d <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;">do.call</span>(simulate_experiment, sim_pars)</span>
<span id="cb13-192">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fitter</span>(d, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">change =</span> pars<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>change, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p0 =</span> pars<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p0)</span>
<span id="cb13-193">  }</span>
<span id="cb13-194">  raw <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;">mclapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n_sim), one_sim,</span>
<span id="cb13-195">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mc.cores =</span> n_cores, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mc.preschedule =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb13-196">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, raw)</span>
<span id="cb13-197">}</span>
<span id="cb13-198"></span>
<span id="cb13-199">summarise_point <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>(fits, design_row) {</span>
<span id="cb13-200">  per_method <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fits <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-201">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(converged) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-202">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(method) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-203">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb13-204">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_sim        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(),</span>
<span id="cb13-205">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type1        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(pvalue <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>),    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># at the null</span></span>
<span id="cb13-206">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(pvalue <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>),    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># at the alternative (same calc)</span></span>
<span id="cb13-207">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">median_width =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(width_logRR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb13-208">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups      =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span></span>
<span id="cb13-209">    )</span>
<span id="cb13-210">  per_method<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>point_id <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> design_row<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>point_id</span>
<span id="cb13-211">  design_cols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> design_row[, <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;">"change"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CV"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n_animals"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"m"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nu"</span>)]</span>
<span id="cb13-212">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(per_method, design_cols, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb13-213">}</span>
<span id="cb13-214"></span>
<span id="cb13-215">run_campaign <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>(design, n_sim, fitter, label, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_cores =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) {</span>
<span id="cb13-216">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">message</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;">"[%s] %d designs x %d sims (n_cores = %d)"</span>,</span>
<span id="cb13-217">                  label, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(design), n_sim, n_cores))</span>
<span id="cb13-218">  rows <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;">lapply</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>(design)), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i) {</span>
<span id="cb13-219">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (i <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;">25</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;">0</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">message</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;">"  point %d / %d"</span>, i, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(design)))</span>
<span id="cb13-220">    fits <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;">run_one_point</span>(design[i, ], n_sim, fitter, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_cores =</span> n_cores)</span>
<span id="cb13-221">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise_point</span>(fits, design[i, ])</span>
<span id="cb13-222">  })</span>
<span id="cb13-223">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(rows)</span>
<span id="cb13-224">}</span>
<span id="cb13-225"></span>
<span id="cb13-226"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---- Run the four campaigns ------------------------------------------------</span></span>
<span id="cb13-227"></span>
<span id="cb13-228">camp_null_no_re <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;">run_campaign</span>(design_null, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_sim =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, fit_no_re, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"null no-RE"</span>)</span>
<span id="cb13-229">camp_main_no_re <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;">run_campaign</span>(design_main, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_sim =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, fit_no_re, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"main no-RE"</span>)</span>
<span id="cb13-230">camp_null_re    <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;">run_campaign</span>(design_null, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_sim =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, fit_re,    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"null RE"</span>)</span>
<span id="cb13-231">camp_main_re    <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;">run_campaign</span>(design_main, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_sim =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, fit_re,    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"main RE"</span>)</span>
<span id="cb13-232"></span>
<span id="cb13-233"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---- Save ------------------------------------------------------------------</span></span>
<span id="cb13-234"></span>
<span id="cb13-235"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</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;">summary =</span> camp_null_no_re, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">design =</span> design_null), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lhs_null_campaign.rds"</span>)</span>
<span id="cb13-236"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</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;">summary =</span> camp_main_no_re, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">design =</span> design_main), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lhs_campaign.rds"</span>)</span>
<span id="cb13-237"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</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;">summary =</span> camp_null_re,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">design =</span> design_null), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lhs_null_campaign_re.rds"</span>)</span>
<span id="cb13-238"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</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;">summary =</span> camp_main_re,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">design =</span> design_main), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lhs_campaign_re.rds"</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Script 2 — fit 12 GP emulators with LOOCV (tens of minutes)</summary>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ============================================================================</span></span>
<span id="cb14-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Script 2: fit 12 GP emulators with leave-one-out cross-validation</span></span>
<span id="cb14-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reads:  lhs_*_campaign*.rds</span></span>
<span id="cb14-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Writes: gp_null_emulators.rds, gp_emulators.rds, gp_emulators_re.rds</span></span>
<span id="cb14-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ============================================================================</span></span>
<span id="cb14-6"></span>
<span id="cb14-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressPackageStartupMessages</span>({</span>
<span id="cb14-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr); <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(hetGP)</span>
<span id="cb14-9">})</span>
<span id="cb14-10"></span>
<span id="cb14-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---- Helpers ---------------------------------------------------------------</span></span>
<span id="cb14-12"></span>
<span id="cb14-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Smoothed logit so rejection rates of 0 or 1 don't blow up the GP fit.</span></span>
<span id="cb14-14">logit_p <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>(p, n) {</span>
<span id="cb14-15">  p_adj <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb14-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(p_adj <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p_adj))</span>
<span id="cb14-17">}</span>
<span id="cb14-18"></span>
<span id="cb14-19">transform_inputs_null <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>(df) {</span>
<span id="cb14-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;">lnu =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nu), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CV =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CV,</span>
<span id="cb14-21">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ln_animals =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_animals), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>m), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lp0 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p0))</span>
<span id="cb14-22">}</span>
<span id="cb14-23">transform_inputs_main <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>(df) {</span>
<span id="cb14-24">  <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;">lchange =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>change), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lnu =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nu), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CV =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CV,</span>
<span id="cb14-25">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ln_animals =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_animals), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>m), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lp0 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p0))</span>
<span id="cb14-26">}</span>
<span id="cb14-27"></span>
<span id="cb14-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fit one homoscedastic GP with Matern 5/2 + ARD, then leave-one-out CV.</span></span>
<span id="cb14-29">fit_emulator <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>(df, target, transform_fn, ard_names, label) {</span>
<span id="cb14-30">  X_raw <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;">transform_fn</span>(df)</span>
<span id="cb14-31">  x_min <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;">sapply</span>(X_raw, min); x_max <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;">sapply</span>(X_raw, max)</span>
<span id="cb14-32">  X <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mapply</span>(<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(c, lo, hi) (c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lo) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (hi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lo),</span>
<span id="cb14-33">                        X_raw, x_min, x_max, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">SIMPLIFY =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb14-34">  fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mleHomGP</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;">Z =</span> target, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">covtype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Matern5_2"</span>)</span>
<span id="cb14-35"></span>
<span id="cb14-36">  n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(X); loo_mu <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;">numeric</span>(n); loo_sd <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;">numeric</span>(n)</span>
<span id="cb14-37">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n)) {</span>
<span id="cb14-38">    fit_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mleHomGP</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>i, , <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">drop =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Z =</span> target[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>i],</span>
<span id="cb14-39">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">covtype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Matern5_2"</span>)</span>
<span id="cb14-40">    p_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(fit_i, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> X[i, , <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">drop =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>])</span>
<span id="cb14-41">    loo_mu[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p_i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean</span>
<span id="cb14-42">    loo_sd[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(p_i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sd2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nugs)</span>
<span id="cb14-43">  }</span>
<span id="cb14-44">  resid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> target <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> loo_mu</span>
<span id="cb14-45"></span>
<span id="cb14-46">  <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;">label =</span> label, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fit =</span> fit,</span>
<span id="cb14-47">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ard   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setNames</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>theta), ard_names),</span>
<span id="cb14-48">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_min =</span> x_min, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_max =</span> x_max,</span>
<span id="cb14-49">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">loo   =</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_obs =</span> target, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_pred =</span> loo_mu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_sd =</span> loo_sd),</span>
<span id="cb14-50">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metrics =</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;">rmse  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(resid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)),</span>
<span id="cb14-51">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov95 =</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;">abs</span>(resid) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> loo_sd)))</span>
<span id="cb14-52">}</span>
<span id="cb14-53"></span>
<span id="cb14-54">ard_null <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lnu"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CV"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ln_animals"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lp0"</span>)</span>
<span id="cb14-55">ard_main <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lchange"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lnu"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CV"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ln_animals"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lp0"</span>)</span>
<span id="cb14-56"></span>
<span id="cb14-57"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---- Load campaigns --------------------------------------------------------</span></span>
<span id="cb14-58"></span>
<span id="cb14-59">camp_null_no_re <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lhs_null_campaign.rds"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary</span>
<span id="cb14-60">camp_main_no_re <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lhs_campaign.rds"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary</span>
<span id="cb14-61">camp_null_re    <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lhs_null_campaign_re.rds"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary</span>
<span id="cb14-62">camp_main_re    <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lhs_campaign_re.rds"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary</span>
<span id="cb14-63"></span>
<span id="cb14-64"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---- Drop low-convergence Pipeline B designs -------------------------------</span></span>
<span id="cb14-65"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CMP GLMM occasionally fails to converge at extreme corners of the design</span></span>
<span id="cb14-66"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># space (very small n with very rare events). Drop designs where fewer than</span></span>
<span id="cb14-67"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># half the simulations converged for at least one method.</span></span>
<span id="cb14-68"></span>
<span id="cb14-69">exclude_lowconv <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>(camp, min_n) {</span>
<span id="cb14-70">  camp <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;">group_by</span>(point_id) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-71">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min_keep =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(n_sim), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-72">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(min_keep <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> min_n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-73">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(point_id)</span>
<span id="cb14-74">}</span>
<span id="cb14-75">excluded <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="cb14-76">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">null =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exclude_lowconv</span>(camp_null_re, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min_n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span>),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 50% of 500</span></span>
<span id="cb14-77">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exclude_lowconv</span>(camp_main_re, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min_n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 50% of 200</span></span>
<span id="cb14-78">)</span>
<span id="cb14-79"></span>
<span id="cb14-80"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---- Pipeline A: Type-I ----------------------------------------------------</span></span>
<span id="cb14-81"></span>
<span id="cb14-82">tt_null  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> camp_null_no_re <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;">filter</span>(method <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;">"t_test"</span>)</span>
<span id="cb14-83">cmp_null <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> camp_null_no_re <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;">filter</span>(method <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;">"cmp_glm"</span>)</span>
<span id="cb14-84"></span>
<span id="cb14-85">em_null_no_re <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="cb14-86">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">T1_tt  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_emulator</span>(tt_null,  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logit_p</span>(tt_null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>type1,  tt_null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_sim),</span>
<span id="cb14-87">                        transform_inputs_null, ard_null, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Welch's t-test"</span>),</span>
<span id="cb14-88">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">T1_cmp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_emulator</span>(cmp_null, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logit_p</span>(cmp_null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>type1, cmp_null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_sim),</span>
<span id="cb14-89">                        transform_inputs_null, ard_null, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLM"</span>)</span>
<span id="cb14-90">)</span>
<span id="cb14-91"></span>
<span id="cb14-92"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---- Pipeline A: Power + Width ---------------------------------------------</span></span>
<span id="cb14-93"></span>
<span id="cb14-94">tt_main  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> camp_main_no_re <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;">filter</span>(method <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;">"t_test"</span>)</span>
<span id="cb14-95">cmp_main <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> camp_main_no_re <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;">filter</span>(method <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;">"cmp_glm"</span>)</span>
<span id="cb14-96"></span>
<span id="cb14-97">em_main_no_re <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="cb14-98">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power_tt  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_emulator</span>(tt_main,  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logit_p</span>(tt_main<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power,  tt_main<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_sim),</span>
<span id="cb14-99">                           transform_inputs_main, ard_main, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Welch's t-test"</span>),</span>
<span id="cb14-100">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power_cmp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_emulator</span>(cmp_main, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logit_p</span>(cmp_main<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power, cmp_main<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_sim),</span>
<span id="cb14-101">                           transform_inputs_main, ard_main, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLM"</span>),</span>
<span id="cb14-102">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width_tt  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_emulator</span>(tt_main,  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(tt_main<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>median_width),</span>
<span id="cb14-103">                           transform_inputs_main, ard_main, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Welch's t-test"</span>),</span>
<span id="cb14-104">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width_cmp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_emulator</span>(cmp_main, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(cmp_main<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>median_width),</span>
<span id="cb14-105">                           transform_inputs_main, ard_main, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLM"</span>)</span>
<span id="cb14-106">)</span>
<span id="cb14-107"></span>
<span id="cb14-108"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---- Pipeline B: Type-I ----------------------------------------------------</span></span>
<span id="cb14-109"></span>
<span id="cb14-110">lmm_null_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> camp_null_re <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;">filter</span>(method <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;">"lmm"</span>,      <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>point_id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> excluded<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>null)</span>
<span id="cb14-111">cmp_null_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> camp_null_re <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;">filter</span>(method <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;">"cmp_glmm"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>point_id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> excluded<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>null)</span>
<span id="cb14-112"></span>
<span id="cb14-113">em_null_re <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="cb14-114">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type1_lmm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_emulator</span>(lmm_null_re, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logit_p</span>(lmm_null_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>type1, lmm_null_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_sim),</span>
<span id="cb14-115">                           transform_inputs_null, ard_null, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LMM"</span>),</span>
<span id="cb14-116">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type1_cmp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_emulator</span>(cmp_null_re, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logit_p</span>(cmp_null_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>type1, cmp_null_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_sim),</span>
<span id="cb14-117">                           transform_inputs_null, ard_null, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLMM"</span>)</span>
<span id="cb14-118">)</span>
<span id="cb14-119"></span>
<span id="cb14-120"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---- Pipeline B: Power + Width ---------------------------------------------</span></span>
<span id="cb14-121"></span>
<span id="cb14-122">lmm_main_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> camp_main_re <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;">filter</span>(method <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;">"lmm"</span>,      <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>point_id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> excluded<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>main)</span>
<span id="cb14-123">cmp_main_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> camp_main_re <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;">filter</span>(method <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;">"cmp_glmm"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>point_id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> excluded<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>main)</span>
<span id="cb14-124"></span>
<span id="cb14-125">em_power_re <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="cb14-126">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power_lmm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_emulator</span>(lmm_main_re, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logit_p</span>(lmm_main_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power, lmm_main_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_sim),</span>
<span id="cb14-127">                           transform_inputs_main, ard_main, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LMM"</span>),</span>
<span id="cb14-128">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power_cmp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_emulator</span>(cmp_main_re, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logit_p</span>(cmp_main_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power, cmp_main_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_sim),</span>
<span id="cb14-129">                           transform_inputs_main, ard_main, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLMM"</span>)</span>
<span id="cb14-130">)</span>
<span id="cb14-131">em_width_re <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="cb14-132">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width_lmm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_emulator</span>(lmm_main_re, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(lmm_main_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>median_width),</span>
<span id="cb14-133">                           transform_inputs_main, ard_main, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LMM"</span>),</span>
<span id="cb14-134">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width_cmp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_emulator</span>(cmp_main_re, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(cmp_main_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>median_width),</span>
<span id="cb14-135">                           transform_inputs_main, ard_main, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLMM"</span>)</span>
<span id="cb14-136">)</span>
<span id="cb14-137"></span>
<span id="cb14-138"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---- Save ------------------------------------------------------------------</span></span>
<span id="cb14-139"></span>
<span id="cb14-140"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</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;">emulators =</span> em_null_no_re), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gp_null_emulators.rds"</span>)</span>
<span id="cb14-141"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</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;">emulators =</span> em_main_no_re), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gp_emulators.rds"</span>)</span>
<span id="cb14-142"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</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;">em_null_re  =</span> em_null_re,</span>
<span id="cb14-143">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">em_power_re =</span> em_power_re,</span>
<span id="cb14-144">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">em_width_re =</span> em_width_re,</span>
<span id="cb14-145">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">excluded    =</span> excluded),    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gp_emulators_re.rds"</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Script 3 — Sobol first- and total-order indices (seconds)</summary>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ============================================================================</span></span>
<span id="cb15-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Script 3: Sobol indices for power + width emulators (not Type-I)</span></span>
<span id="cb15-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reads:  gp_emulators.rds, gp_emulators_re.rds</span></span>
<span id="cb15-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Writes: sobol_no_re_indices.rds, sobol_re_indices.rds</span></span>
<span id="cb15-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ============================================================================</span></span>
<span id="cb15-6"></span>
<span id="cb15-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressPackageStartupMessages</span>({</span>
<span id="cb15-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr); <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(hetGP); <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sensitivity)</span>
<span id="cb15-9">})</span>
<span id="cb15-10"></span>
<span id="cb15-11">em_main_no_re <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gp_emulators.rds"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>emulators</span>
<span id="cb15-12">em_re_bundle  <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gp_emulators_re.rds"</span>)</span>
<span id="cb15-13">em_power_re   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> em_re_bundle<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>em_power_re</span>
<span id="cb15-14">em_width_re   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> em_re_bundle<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>em_width_re</span>
<span id="cb15-15"></span>
<span id="cb15-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Input ranges on the transformed (log) scale that the GPs were fit on.</span></span>
<span id="cb15-17">input_lo <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;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lchange =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>),  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lnu =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CV =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb15-18">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ln_animals =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lm  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>),   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lp0 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span>))</span>
<span id="cb15-19">input_hi <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;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lchange =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>),  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lnu =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>),  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CV =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span>,</span>
<span id="cb15-20">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ln_animals =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>),  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lp0 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.40</span>))</span>
<span id="cb15-21"></span>
<span id="cb15-22">input_label_map <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;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lchange =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"change"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lnu =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\u03bd"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CV =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CV"</span>,</span>
<span id="cb15-23">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ln_animals =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n_animals"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"m"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lp0 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p0"</span>)</span>
<span id="cb15-24">input_levels    <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;">unname</span>(input_label_map)</span>
<span id="cb15-25">target_levels   <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Type-I"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Width"</span>)</span>
<span id="cb15-26"></span>
<span id="cb15-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Saltelli sample on the 6-D box.</span></span>
<span id="cb15-28">N_SOBOL <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;">1024</span></span>
<span id="cb15-29"><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;">20260517</span>)</span>
<span id="cb15-30">make_sample <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>(N)</span>
<span id="cb15-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mapply</span>(<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(l, h) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(N, l, h),</span>
<span id="cb15-32">                       input_lo, input_hi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">SIMPLIFY =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb15-33">X1 <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;">make_sample</span>(N_SOBOL)</span>
<span id="cb15-34">X2 <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;">make_sample</span>(N_SOBOL)</span>
<span id="cb15-35"></span>
<span id="cb15-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Wrap a fitted emulator as a function R^6 -&gt; R that sobolSalt can call.</span></span>
<span id="cb15-37">make_predictor <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>(em) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(X) {</span>
<span id="cb15-38">  Xs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mapply</span>(<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(c, l, h) (c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> l) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (h <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> l),</span>
<span id="cb15-39">                         <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(X), em<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x_min, em<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x_max, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">SIMPLIFY =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb15-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(em<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Xs)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean</span>
<span id="cb15-41">}</span>
<span id="cb15-42"></span>
<span id="cb15-43">extract_sobol <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>(sob, target_lab, method_lab) {</span>
<span id="cb15-44">  s <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.data.frame</span>(sob<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>S); t <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.data.frame</span>(sob<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>T)</span>
<span id="cb15-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb15-46">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">input  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(input_label_map[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(input_lo)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> input_levels),</span>
<span id="cb15-47">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S      =</span> s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>original, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S_lo =</span> s<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;">min. c.i.</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;">S_hi =</span> s<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;">max. c.i.</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,</span>
<span id="cb15-48">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">T      =</span> t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>original, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">T_lo =</span> t<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;">min. c.i.</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;">T_hi =</span> t<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;">max. c.i.</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,</span>
<span id="cb15-49">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">target =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(target_lab, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> target_levels),</span>
<span id="cb15-50">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> method_lab</span>
<span id="cb15-51">  )</span>
<span id="cb15-52">}</span>
<span id="cb15-53"></span>
<span id="cb15-54">run_sobol <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>(em, target_lab, method_lab) {</span>
<span id="cb15-55">  sob <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;">sobolSalt</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_predictor</span>(em), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X1 =</span> X1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X2 =</span> X2,</span>
<span id="cb15-56">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scheme =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nboot =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>)</span>
<span id="cb15-57">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_sobol</span>(sob, target_lab, method_lab)</span>
<span id="cb15-58">}</span>
<span id="cb15-59"></span>
<span id="cb15-60">sobol_no_re <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;">bind_rows</span>(</span>
<span id="cb15-61">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run_sobol</span>(em_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power_tt,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Welch's t-test"</span>),</span>
<span id="cb15-62">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run_sobol</span>(em_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power_cmp, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLM"</span>),</span>
<span id="cb15-63">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run_sobol</span>(em_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>width_tt,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Width"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Welch's t-test"</span>),</span>
<span id="cb15-64">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run_sobol</span>(em_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>width_cmp, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Width"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLM"</span>)</span>
<span id="cb15-65">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-66">  <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;">method =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(method, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"Welch's t-test"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLM"</span>)))</span>
<span id="cb15-67"></span>
<span id="cb15-68">sobol_re <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;">bind_rows</span>(</span>
<span id="cb15-69">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run_sobol</span>(em_power_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power_lmm, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LMM"</span>),</span>
<span id="cb15-70">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run_sobol</span>(em_power_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power_cmp, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLMM"</span>),</span>
<span id="cb15-71">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run_sobol</span>(em_width_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>width_lmm, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Width"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LMM"</span>),</span>
<span id="cb15-72">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run_sobol</span>(em_width_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>width_cmp, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Width"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLMM"</span>)</span>
<span id="cb15-73">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-74">  <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;">method =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(method, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"LMM"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLMM"</span>)))</span>
<span id="cb15-75"></span>
<span id="cb15-76"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</span>(sobol_no_re, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sobol_no_re_indices.rds"</span>)</span>
<span id="cb15-77"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</span>(sobol_re,    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sobol_re_indices.rds"</span>)</span></code></pre></div>
</details>
</div>
</section>
<section id="loading-the-precomputed-bundles" class="level3">
<h3 class="anchored" data-anchor-id="loading-the-precomputed-bundles">Loading the precomputed bundles</h3>
<p>This is what actually runs during render. If you ran the three scripts above, the <code>.rds</code> bundles are in your working directory. If not, the precomputed bundles are published alongside this post and can be downloaded directly: <a href="lhs_null_campaign.rds"><code>lhs_null_campaign.rds</code></a>, <a href="lhs_campaign.rds"><code>lhs_campaign.rds</code></a>, <a href="lhs_null_campaign_re.rds"><code>lhs_null_campaign_re.rds</code></a>, <a href="lhs_campaign_re.rds"><code>lhs_campaign_re.rds</code></a>, <a href="gp_null_emulators.rds"><code>gp_null_emulators.rds</code></a>, <a href="gp_emulators.rds"><code>gp_emulators.rds</code></a>, <a href="gp_emulators_re.rds"><code>gp_emulators_re.rds</code></a>, <a href="sobol_no_re_indices.rds"><code>sobol_no_re_indices.rds</code></a>, <a href="sobol_re_indices.rds"><code>sobol_re_indices.rds</code></a>. Save them next to the .qmd. The chunk below loads all nine into the variables the rest of the post uses.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Raw LHS simulation campaigns (used for paired-difference and Type-I plots)</span></span>
<span id="cb16-2">camp_null_no_re <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lhs_null_campaign.rds"</span>)</span>
<span id="cb16-3">camp_main_no_re <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lhs_campaign.rds"</span>)</span>
<span id="cb16-4">null_summary_no_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> camp_null_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary</span>
<span id="cb16-5">main_summary_no_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> camp_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary</span>
<span id="cb16-6"></span>
<span id="cb16-7">camp_null_re <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lhs_null_campaign_re.rds"</span>)</span>
<span id="cb16-8">camp_main_re <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lhs_campaign_re.rds"</span>)</span>
<span id="cb16-9">null_summary_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> camp_null_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary</span>
<span id="cb16-10">main_summary_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> camp_main_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary</span>
<span id="cb16-11"></span>
<span id="cb16-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># GP emulator bundles</span></span>
<span id="cb16-13">em_null_no_re <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gp_null_emulators.rds"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>emulators</span>
<span id="cb16-14">em_main_no_re <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gp_emulators.rds"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>emulators</span>
<span id="cb16-15">em_power_no_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power_tt  =</span> em_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power_tt,</span>
<span id="cb16-16">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power_cmp =</span> em_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power_cmp)</span>
<span id="cb16-17">em_width_no_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width_tt  =</span> em_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>width_tt,</span>
<span id="cb16-18">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width_cmp =</span> em_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>width_cmp)</span>
<span id="cb16-19"></span>
<span id="cb16-20">em_re_bundle <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gp_emulators_re.rds"</span>)</span>
<span id="cb16-21">em_null_re   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> em_re_bundle<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>em_null_re</span>
<span id="cb16-22">em_power_re  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> em_re_bundle<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>em_power_re</span>
<span id="cb16-23">em_width_re  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> em_re_bundle<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>em_width_re</span>
<span id="cb16-24">excluded_re  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> em_re_bundle<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>excluded</span>
<span id="cb16-25"></span>
<span id="cb16-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sobol indices</span></span>
<span id="cb16-27">sobol_no_re <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sobol_no_re_indices.rds"</span>)</span>
<span id="cb16-28">sobol_re    <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sobol_re_indices.rds"</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Input transforms: log on log-uniformly sampled axes, linear on CV.</span></span>
<span id="cb17-2">transform_inputs_null <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>(df) {</span>
<span id="cb17-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;">lnu =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nu), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CV =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CV,</span>
<span id="cb17-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ln_animals =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_animals), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>m), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lp0 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p0))</span>
<span id="cb17-5">}</span>
<span id="cb17-6">transform_inputs_main <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>(df) {</span>
<span id="cb17-7">  <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;">lchange =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>change), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lnu =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nu), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CV =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CV,</span>
<span id="cb17-8">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ln_animals =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_animals), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>m), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lp0 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p0))</span>
<span id="cb17-9">}</span>
<span id="cb17-10"></span>
<span id="cb17-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict from a fitted emulator at new design points.</span></span>
<span id="cb17-12">predict_gp <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>(emulator, newdata_raw, transform_fn, <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="cb17-13">  X_raw <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;">transform_fn</span>(newdata_raw)</span>
<span id="cb17-14">  X <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mapply</span>(</span>
<span id="cb17-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(col, lo, hi) (col <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lo) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (hi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lo),</span>
<span id="cb17-16">    X_raw, emulator<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x_min, emulator<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x_max, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">SIMPLIFY =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)))</span>
<span id="cb17-17">  mu <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>(emulator<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit, <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>mean</span>
<span id="cb17-18">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (link <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;">"logit"</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;">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;">exp</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>mu))    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># back to probability</span></span>
<span id="cb17-19">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (link <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;">"log"</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(mu)               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># back to width</span></span>
<span id="cb17-20">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>                    mu                    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># raw GP scale</span></span>
<span id="cb17-21">}</span>
<span id="cb17-22"></span>
<span id="cb17-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Chunked predict to keep memory bounded on large MC grids.</span></span>
<span id="cb17-24">chunk_predict <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>(emulator, df, transform_fn, <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="cb17-25">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">chunk_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50000</span>) {</span>
<span id="cb17-26">  n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(df); out <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;">numeric</span>(n)</span>
<span id="cb17-27">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (s <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> chunk_size)) {</span>
<span id="cb17-28">    e <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;">min</span>(s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> chunk_size <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>, n)</span>
<span id="cb17-29">    out[s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>e] <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_gp</span>(emulator, df[s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>e, , <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">drop =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>], transform_fn, link)</span>
<span id="cb17-30">  }</span>
<span id="cb17-31">  out</span>
<span id="cb17-32">}</span>
<span id="cb17-33"></span>
<span id="cb17-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ----- Plotting helpers shared across pipelines -----</span></span>
<span id="cb17-35"></span>
<span id="cb17-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># LOOCV residual diagnostic for any pair of emulators with $loo populated.</span></span>
<span id="cb17-37">plot_loo_residuals <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>(emulators, method_colors, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_label =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) {</span>
<span id="cb17-38">  method_levels <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;">vapply</span>(emulators, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">character</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb17-39"></span>
<span id="cb17-40">  loo_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;">bind_rows</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(emulators, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) {</span>
<span id="cb17-41">    <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;">Method  =</span> e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label,</span>
<span id="cb17-42">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_obs   =</span> e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y_obs,</span>
<span id="cb17-43">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_pred  =</span> e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y_pred,</span>
<span id="cb17-44">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_sd    =</span> e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y_sd,</span>
<span id="cb17-45">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resid   =</span> e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y_pred,</span>
<span id="cb17-46">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">covered =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y_pred) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y_sd)</span>
<span id="cb17-47">  })) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-48">    <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;">Method =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Method, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> method_levels)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-49">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(Method) <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;">arrange</span>(y_obs) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-50">    <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;">idx =</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;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb17-51"></span>
<span id="cb17-52">  cov_stats <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> loo_df <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;">group_by</span>(Method) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-53">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov95 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(covered),</span>
<span id="cb17-54">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rmse  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(resid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span>
<span id="cb17-55">  cov_label <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;">paste</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;">"%s: 95%% cov = %.0f%%, RMSE = %.2f"</span>,</span>
<span id="cb17-56">                             cov_stats<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Method, <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> cov_stats<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cov95, cov_stats<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rmse),</span>
<span id="cb17-57">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"  |  "</span>)</span>
<span id="cb17-58"></span>
<span id="cb17-59">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(loo_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> idx, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> resid, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Method)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-60">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <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;">"grey40"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-61">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_errorbar</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;">ymin =</span> resid <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y_sd, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> resid <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y_sd),</span>
<span id="cb17-62">                  <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.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</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="cb17-63">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</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.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-64">    <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> Method, <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;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_x"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-65">    <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> method_colors, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-66">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"LOOCV residuals"</span>,</span>
<span id="cb17-67">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> cov_label,</span>
<span id="cb17-68">         <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;">"design point (sorted by observed value)"</span>,</span>
<span id="cb17-69">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y        =</span> y_label <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 \u2212 predicted"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-70">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb17-71">}</span>
<span id="cb17-72"></span>
<span id="cb17-73"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sobol first- and total-order indices for one or more targets.</span></span>
<span id="cb17-74"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># `sobol_df` must have columns: input, S, S_lo, S_hi, T, T_lo, T_hi, target, method.</span></span>
<span id="cb17-75">plot_sobol <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>(sobol_df, method_colors, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) {</span>
<span id="cb17-76">  sobol_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sobol_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-77">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">droplevels</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-78">    <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;">method =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(method, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(method_colors)))</span>
<span id="cb17-79"></span>
<span id="cb17-80">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(sobol_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> input, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> method)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-81">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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;">y =</span> T, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> method),</span>
<span id="cb17-82">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_dodge</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-83">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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;">y =</span> S, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> method),</span>
<span id="cb17-84">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_dodge</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-85">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_errorbar</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;">ymin =</span> T_lo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> T_hi),</span>
<span id="cb17-86">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_dodge</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>,</span>
<span id="cb17-87">                  <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;">"grey30"</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.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-88">    <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> target, <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;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-89">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> method_colors, <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-90">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-91">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title    =</span> title,</span>
<span id="cb17-92">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Solid bars: first-order (main effect). Light bars: total-order (main + interactions). Error bars: 95% bootstrap CIs on total-order."</span>,</span>
<span id="cb17-93">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Sobol index"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-94">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-95">    <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="cb17-96">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb17-97">}</span>
<span id="cb17-98"></span>
<span id="cb17-99"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Coalesce operator used by plot_loo_residuals.</span></span>
<span id="cb17-100"><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;">%||%</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <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>(a, b) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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.null</span>(a)) a <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> b</span></code></pre></div>
</details>
</div>
</section>
</section>
<section id="results" class="level1">
<h1>Results</h1>
<p>The 300-design universe is built; the 12 emulators are fit. Each metric (Type-I error, statistical power, CI precision) gets its own section below, comparing all four methods side by side. Diagnostics first (does the emulator we’re trusting actually fit the data?), then the substantive result, then a brief discussion of what drives method behaviour.</p>
<section id="type-i-error" class="level2">
<h2 class="anchored" data-anchor-id="type-i-error">Type-I error</h2>
<p>A method that rejects too often at the null is not more powerful — it’s crying wolf more often. We need to know each method is honest before comparing them on anything else.</p>
<p>The null campaign fixes <code>change = 1</code> and varies the other five knobs. 300 LHS points × 500 simulations × 4 fitters → 600,000 fits, summarised to a Type-I rate per (design point, method). Each rate feeds a GP emulator, giving four Type-I surfaces I can interrogate anywhere in the design space.</p>
<section id="how-good-are-the-emulators" class="level3">
<h3 class="anchored" data-anchor-id="how-good-are-the-emulators">How good are the emulators?</h3>
<p>Before reading anything off the Type-I surfaces, the standard check: leave-one-out cross-validation. For each design point I refit the GP without it, predict at the held-out location, and compare prediction to observation. Residuals should cluster around zero with the ±1.96 SD predictive intervals crossing zero ~95% of the time. RMSE on the logit scale and 95% coverage are reported in the subtitle of each panel.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">pA <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;">plot_loo_residuals</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(em_null_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>T1_tt, em_null_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>T1_cmp),</span>
<span id="cb18-2">                         method_colors_no_re,</span>
<span id="cb18-3">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"observed \u2212 predicted logit(Type-I)"</span>)</span>
<span id="cb18-4">pB <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;">plot_loo_residuals</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(em_null_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>type1_lmm, em_null_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>type1_cmp),</span>
<span id="cb18-5">                         method_colors_re,</span>
<span id="cb18-6">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"observed \u2212 predicted logit(Type-I)"</span>)</span>
<span id="cb18-7">pA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> pB</span></code></pre></div>
</details>
<div class="cell-output-display">
<div id="fig-loo-null-all" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-loo-null-all-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/index_files/figure-html/fig-loo-null-all-1.png" class="img-fluid figure-img" width="960">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-loo-null-all-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: LOOCV residuals for the four Type-I emulators on the logit scale. Top row: animal-level methods (Welch’s t-test, CMP GLM). Bottom row: field-level methods (LMM, CMP GLMM). Coverage near nominal 95% with residuals centred on zero is what we want.
</figcaption>
</figure>
</div>
</div>
</div>
<p>All four emulators are well-calibrated — coverage close to 95%, residuals centred. The Type-I surfaces are trustworthy.</p>
</section>
<section id="does-each-method-hit-the-nominal-rate" class="level3">
<h3 class="anchored" data-anchor-id="does-each-method-hit-the-nominal-rate">Does each method hit the nominal rate?</h3>
<p>To judge calibration I use <strong>Bradley’s [2.5%, 7.5%] band of liberal robustness for a nominal-5% test</strong> (Bradley, 1978). The idea: a Type-I rate of exactly 5% is a fantasy at finite sample sizes; what we want is a method whose empirical Type-I across realistic designs falls inside Bradley’s band. Anything outside is either conservative (wasting power, the smaller problem) or anti-conservative (false positives, the larger one).</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">bradley_panel <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>(summary_df, method_filter, method_recode, colors) {</span>
<span id="cb19-2">  d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> summary_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb19-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(method <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> method_filter) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb19-4">    <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;">Method =</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;">recode</span>(method, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!!!</span>method_recode),</span>
<span id="cb19-5">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(method_recode)))</span>
<span id="cb19-6">  meds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d <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;">group_by</span>(Method) <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;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">med =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(type1), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span>
<span id="cb19-7"></span>
<span id="cb19-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(d, <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> type1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Method)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</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.85</span>, <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;">"white"</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.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</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;">"dashed"</span>, <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;">"grey30"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.025</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.075</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;">"dotted"</span>, <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;">"grey50"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> meds, <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> med, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Method),</span>
<span id="cb19-13">               <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.9</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="cb19-14">    <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> Method, <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;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> colors, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-16">    <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> colors, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">percent_format</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</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;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.11</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Type-I error rate"</span>, <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;">"design points"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb19-20">}</span>
<span id="cb19-21"></span>
<span id="cb19-22">pA <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;">bradley_panel</span>(null_summary_no_re,</span>
<span id="cb19-23">                    <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;">"t_test"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cmp_glm"</span>),</span>
<span id="cb19-24">                    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t_test =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Welch's t-test"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cmp_glm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLM"</span>),</span>
<span id="cb19-25">                    method_colors_no_re) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Animal-level (no random effects)"</span>)</span>
<span id="cb19-27"></span>
<span id="cb19-28">pB <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;">bradley_panel</span>(null_summary_re <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;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>point_id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> excluded_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>null),</span>
<span id="cb19-29">                    <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;">"lmm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cmp_glmm"</span>),</span>
<span id="cb19-30">                    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lmm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LMM"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cmp_glmm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLMM"</span>),</span>
<span id="cb19-31">                    method_colors_re) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Field-level (with random effects)"</span>,</span>
<span id="cb19-33">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dashed: nominal 5%. Dotted: Bradley's [2.5%, 7.5%] band. Coloured: median."</span>)</span>
<span id="cb19-34"></span>
<span id="cb19-35">pA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> pB</span></code></pre></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_bar()`).</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 1 row containing non-finite outside the scale range
(`stat_bin()`).</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_bar()`).</code></pre>
</div>
<div class="cell-output-display">
<div id="fig-type1-all" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-type1-all-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/index_files/figure-html/fig-type1-all-1.png" class="img-fluid figure-img" width="960">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-type1-all-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Empirical Type-I across 300 null designs, all four methods. Dashed line: nominal 5%. Dotted lines: Bradley’s [2.5%, 7.5%] band. Coloured line: median across the 300 designs.
</figcaption>
</figure>
</div>
</div>
</div>
<p>A few things to read off this. All four methods sit mostly inside Bradley’s band — none is structurally broken. But they’re not all in the same place inside it. <strong>Welch’s t-test</strong> is mildly conservative (median ~4.4%); <strong>CMP GLM</strong> is mildly anti-conservative (median ~6%); the field-level pair sits in between. The medians are within ~1–2 pp of each other but consistently on different sides of nominal 5%.</p>
</section>
<section id="why-this-matters-for-power-size-adjustment" class="level3">
<h3 class="anchored" data-anchor-id="why-this-matters-for-power-size-adjustment">Why this matters for power: size adjustment</h3>
<p>Here’s the catch. If one method rejects 6% of the time at the null and another rejects 4%, then at the alternative, the first method’s “power” includes some apparent advantage that’s really just inherited Type-I inflation. To compare power fairly, I size-adjust each method’s raw power using the first-order Lloyd correction (Lloyd, 2005):</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Ctext%7Badjusted%20power%7D%20%5Capprox%20%5Ctext%7Braw%20power%7D%20-%20(%5Ctext%7BType-I%20rate%7D%20-%200.05)"></p>
<p>This resets every method’s starting line to 5% and asks: at a <em>matched</em> nominal level, who detects the effect more often? It’s not a perfect adjustment (it ignores higher-order distributional differences), but at the small Type-I gaps we see here it’s enough to make the comparison honest. All power numbers from here on are size-adjusted.</p>
</section>
</section>
<section id="statistical-power" class="level2">
<h2 class="anchored" data-anchor-id="statistical-power">Statistical power</h2>
<p>The main campaign varies all six knobs including effect size. 300 LHS points × 200 simulations × 4 fitters → 240,000 fits, summarised to a raw rejection rate per (design point, method). Subtracting the matched Type-I excess turns these into size-adjusted power surfaces.</p>
<section id="power-emulator-diagnostics" class="level3">
<h3 class="anchored" data-anchor-id="power-emulator-diagnostics">Power emulator diagnostics</h3>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">pA <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;">plot_loo_residuals</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(em_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power_tt, em_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power_cmp),</span>
<span id="cb23-2">                         method_colors_no_re,</span>
<span id="cb23-3">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"observed \u2212 predicted logit(power)"</span>)</span>
<span id="cb23-4">pB <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;">plot_loo_residuals</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(em_power_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power_lmm, em_power_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power_cmp),</span>
<span id="cb23-5">                         method_colors_re,</span>
<span id="cb23-6">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"observed \u2212 predicted logit(power)"</span>)</span>
<span id="cb23-7">pA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> pB</span></code></pre></div>
</details>
<div class="cell-output-display">
<div id="fig-loo-power-all" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-loo-power-all-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/index_files/figure-html/fig-loo-power-all-1.png" class="img-fluid figure-img" width="960">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-loo-power-all-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: LOOCV residuals for the four power emulators on the logit scale. Top row: animal-level. Bottom row: field-level. Coverage near 95% confirms the GP surfaces are reliable.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="what-drives-power-sobol-decomposition" class="level3">
<h3 class="anchored" data-anchor-id="what-drives-power-sobol-decomposition">What drives power: Sobol decomposition</h3>
<p>A fitted GP is more than a smoother; it’s a deterministic function I can probe. <strong>Sobol first- and total-order indices</strong> decompose the GP’s output variance into the share attributable to each input. The first-order <img src="https://latex.codecogs.com/png.latex?S_i"> is input <img src="https://latex.codecogs.com/png.latex?i">’s contribution on its own; the total-order <img src="https://latex.codecogs.com/png.latex?S_%7BT_i%7D"> adds in interactions with every other input. The gap between them is how much of an input’s influence flows through interactions.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">pA <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;">plot_sobol</span>(sobol_no_re <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;">filter</span>(target <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;">"Power"</span>), method_colors_no_re,</span>
<span id="cb24-2">                 <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;">"Animal-level (no random effects)"</span>)</span>
<span id="cb24-3">pB <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;">plot_sobol</span>(sobol_re    <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;">filter</span>(target <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;">"Power"</span>), method_colors_re,</span>
<span id="cb24-4">                 <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;">"Field-level (with random effects)"</span>)</span>
<span id="cb24-5">pA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> pB</span></code></pre></div>
</details>
<div class="cell-output-display">
<div id="fig-sobol-power-all" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-sobol-power-all-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/index_files/figure-html/fig-sobol-power-all-1.png" class="img-fluid figure-img" width="960">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-sobol-power-all-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;6: Sobol first- and total-order indices for the four power emulators. Solid bars: first-order (main effect). Light bars: total-order (main + interactions). Error bars are 95% bootstrap CIs on the total-order indices.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Across all four methods the picture is nearly identical. Power is dominated by <code>change</code> (~55–60% of variance), with <code>CV</code> second (~20–25%) and <code>n_animals</code> a clear but smaller third (~10–13%). The remaining knobs — <code>ν</code>, <code>m</code>, <code>p0</code> — contribute under 5% each. The light bars sit modestly above the solid ones on <code>change</code> and <code>CV</code>, indicating some interaction structure, but no single pair dominates.</p>
<p>All four methods agree on what <em>drives</em> power; differences between them will live in how strongly each responds at a given design. And the three top drivers play very different roles: <code>change</code> is the effect we’re trying to <em>detect</em>, <code>CV</code> is a by-product of the biology/methodology, and <code>n_animals</code> is the one knob the experimenter actually sets.</p>
</section>
<section id="where-does-the-count-likelihood-actually-win" class="level3">
<h3 class="anchored" data-anchor-id="where-does-the-count-likelihood-actually-win">Where does the count likelihood actually win?</h3>
<p>GP emulation gives us a clean way to query power across the entire experimental universe. The fitted GPs aren’t just smoothers; they’re cheap surrogate models of each method’s behaviour at every point in the 6-dimensional design space. Once we have them, asking “what is method X’s power at this design?” becomes a function call rather than a fresh simulation campaign.</p>
<p>Apart from <code>change</code>, the Sobol analysis identified <code>n_animals</code> and <code>CV</code> as the two next most important drivers of power. So a natural question to ask the emulators is: at a fixed, medium-sized effect (say a 30% increase in proliferation, <code>change = 1.3</code>), how does each method’s power vary as a function of <code>n_animals</code> and <code>CV</code>?</p>
<p>To answer it cleanly we need to do something about the remaining three knobs: <code>ν</code>, <code>m</code>, and <code>p0</code>. We could just fix them at one arbitrary combination, but the answer would depend on the choice. Better is to <em>marginalize</em> over them: for each cell on the <code>(n_animals, CV)</code> grid, draw a Monte Carlo sample of <code>(ν, m, p0)</code> from their realistic distributions, predict size-adjusted power at every sampled point with the emulator, and average. Each heatmap cell is then <em>expected</em> power across realistic variation in the nuisance dimensions, not a slice at one arbitrary combination. Tens of thousands of GP predictions cost seconds; running a fresh simulation campaign of the same density would cost days.</p>
<p>The result, one heatmap per pipeline, on the same axes: animals per group on the x-axis, between-animal CV on the y-axis. Fill colour is the size-adjusted power difference (CMP minus Gaussian); the two contour lines mark each method’s 80% power boundary.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-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="cb25-2">M       <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;">1500</span></span>
<span id="cb25-3">n_grid  <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb25-4">cv_grid <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</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;">36</span>)</span>
<span id="cb25-5">change_fixed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.3</span></span>
<span id="cb25-6"></span>
<span id="cb25-7">mc_grid <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;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_animals =</span> n_grid, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CV =</span> cv_grid, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mc =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(M),</span>
<span id="cb25-8">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">KEEP.OUT.ATTRS =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb25-9">mc_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>change <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> change_fixed</span>
<span id="cb25-10">mc_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nu     <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;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(mc_grid), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>)))</span>
<span id="cb25-11">mc_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>m      <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(mc_grid), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>))))</span>
<span id="cb25-12">mc_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p0     <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;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(mc_grid), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.40</span>)))</span>
<span id="cb25-13"></span>
<span id="cb25-14">mc_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_tt_raw  <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;">chunk_predict</span>(em_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power_tt,  mc_grid, transform_inputs_main, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logit"</span>)</span>
<span id="cb25-15">mc_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_cmp_raw <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;">chunk_predict</span>(em_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power_cmp, mc_grid, transform_inputs_main, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logit"</span>)</span>
<span id="cb25-16">mc_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t1_tt     <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;">chunk_predict</span>(em_null_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>T1_tt,     mc_grid, transform_inputs_null, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logit"</span>)</span>
<span id="cb25-17">mc_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t1_cmp    <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;">chunk_predict</span>(em_null_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>T1_cmp,    mc_grid, transform_inputs_null, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logit"</span>)</span>
<span id="cb25-18"></span>
<span id="cb25-19">mc_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_tt_adj  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mc_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_tt_raw  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (mc_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t1_tt  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)</span>
<span id="cb25-20">mc_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_cmp_adj <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mc_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_cmp_raw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (mc_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t1_cmp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)</span>
<span id="cb25-21"></span>
<span id="cb25-22">agg_power_no_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mc_grid <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb25-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(n_animals, CV) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb25-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power_tt  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(p_tt_adj),</span>
<span id="cb25-25">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power_cmp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(p_cmp_adj),</span>
<span id="cb25-26">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delta_adj =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(p_cmp_adj <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p_tt_adj),</span>
<span id="cb25-27">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span>
<span id="cb25-28"></span>
<span id="cb25-29">dmax_no <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(agg_power_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>delta_adj))</span>
<span id="cb25-30"></span>
<span id="cb25-31"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(agg_power_no_re, <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> n_animals, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> CV)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_raster</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;">fill =</span> delta_adj), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interpolate =</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="cb25-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour</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;">z =</span> power_tt,  <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;">"Welch's t-test"</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;">1.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.80</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour</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;">z =</span> power_cmp, <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;">"CMP GLM"</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;">1.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.80</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradient2</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">low =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#C0504D"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mid =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey95"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">high =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2C4E73"</span>,</span>
<span id="cb25-36">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">midpoint =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dmax_no, dmax_no),</span>
<span id="cb25-37">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">percent_format</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb25-38">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\u0394 power</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(CMP \u2212 t-test)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-39">  <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> <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;">"Welch's t-test"</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;">"#F4A261"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLM"</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;">"#1B1B1B"</span>),</span>
<span id="cb25-40">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"80% power contour"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</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;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-42">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<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;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Animal-level: CMP GLM vs Welch's t-test"</span>,</span>
<span id="cb25-44">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</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;">"change = %.2f. Blue: CMP wins. Above each line: 80%% power reached."</span>, change_fixed),</span>
<span id="cb25-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;">"animals per group"</span>, <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;">"between-animal CV"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-47">  <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;">panel.grid =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</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;">"right"</span>, <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></code></pre></div>
</details>
<div class="cell-output-display">
<div id="fig-money-power-no-re" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-money-power-no-re-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/index_files/figure-html/fig-money-power-no-re-1.png" class="img-fluid figure-img" width="960">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-money-power-no-re-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;7: Animal-level (no random effects). Size-adjusted power difference (CMP GLM − Welch’s t-test) at change = 1.3, marginalized over (ν, m, p0). Lines: 80% power contour for each method individually.
</figcaption>
</figure>
</div>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="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;">2</span>)</span>
<span id="cb26-2">mc_grid_re <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;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_animals =</span> n_grid, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CV =</span> cv_grid, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mc =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(M),</span>
<span id="cb26-3">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">KEEP.OUT.ATTRS =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb26-4">mc_grid_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>change <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> change_fixed</span>
<span id="cb26-5">mc_grid_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nu     <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;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(mc_grid_re), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>)))</span>
<span id="cb26-6">mc_grid_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>m      <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(mc_grid_re), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>))))</span>
<span id="cb26-7">mc_grid_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p0     <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;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(mc_grid_re), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.40</span>)))</span>
<span id="cb26-8"></span>
<span id="cb26-9">mc_grid_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_lmm_raw <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;">chunk_predict</span>(em_power_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power_lmm, mc_grid_re, transform_inputs_main, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logit"</span>)</span>
<span id="cb26-10">mc_grid_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_cmp_raw <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;">chunk_predict</span>(em_power_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power_cmp, mc_grid_re, transform_inputs_main, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logit"</span>)</span>
<span id="cb26-11">mc_grid_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t1_lmm    <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;">chunk_predict</span>(em_null_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>type1_lmm,  mc_grid_re, transform_inputs_null, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logit"</span>)</span>
<span id="cb26-12">mc_grid_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t1_cmp    <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;">chunk_predict</span>(em_null_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>type1_cmp,  mc_grid_re, transform_inputs_null, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logit"</span>)</span>
<span id="cb26-13"></span>
<span id="cb26-14">mc_grid_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_lmm_adj <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mc_grid_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_lmm_raw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (mc_grid_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t1_lmm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)</span>
<span id="cb26-15">mc_grid_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_cmp_adj <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mc_grid_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p_cmp_raw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (mc_grid_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t1_cmp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)</span>
<span id="cb26-16"></span>
<span id="cb26-17">agg_power_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mc_grid_re <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(n_animals, CV) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power_lmm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(p_lmm_adj),</span>
<span id="cb26-20">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power_cmp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(p_cmp_adj),</span>
<span id="cb26-21">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delta_adj =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(p_cmp_adj <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p_lmm_adj),</span>
<span id="cb26-22">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span>
<span id="cb26-23"></span>
<span id="cb26-24">dmax_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(agg_power_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>delta_adj))</span>
<span id="cb26-25"></span>
<span id="cb26-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(agg_power_re, <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> n_animals, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> CV)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_raster</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;">fill =</span> delta_adj), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interpolate =</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="cb26-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour</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;">z =</span> power_lmm, <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;">"LMM"</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;">1.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.80</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_contour</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;">z =</span> power_cmp, <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;">"CMP GLMM"</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;">1.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.80</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradient2</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">low =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#C0504D"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mid =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey95"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">high =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2C4E73"</span>,</span>
<span id="cb26-31">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">midpoint =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dmax_re, dmax_re),</span>
<span id="cb26-32">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">percent_format</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb26-33">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\u0394 power</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(CMP \u2212 LMM)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-34">  <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> <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;">"LMM"</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;">"#F4A261"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CMP GLMM"</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;">"#1B1B1B"</span>),</span>
<span id="cb26-35">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"80% power contour"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</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;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<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;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Field-level: CMP GLMM vs LMM"</span>,</span>
<span id="cb26-39">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</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;">"change = %.2f. Blue: CMP GLMM wins. Above each line: 80%% power reached."</span>, change_fixed),</span>
<span id="cb26-40">       <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;">"animals per group"</span>, <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;">"between-animal CV"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-42">  <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;">panel.grid =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</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;">"right"</span>, <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></code></pre></div>
</details>
<div class="cell-output-display">
<div id="fig-money-power-re" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-money-power-re-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/index_files/figure-html/fig-money-power-re-1.png" class="img-fluid figure-img" width="960">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-money-power-re-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;8: Field-level (with random effects). Size-adjusted power difference (CMP GLMM − LMM) at change = 1.3, on the same axes for direct comparison with the animal-level figure above.
</figcaption>
</figure>
</div>
</div>
</div>
<p>These plots reinforce what we already saw on the ideal dataset: when there is plenty of good data, all four methods agree. The heatmaps confirm it: the 80% power contour lands at essentially the same spot for both methods in each pipeline.</p>
<p>Yet, two things become visible from this “real world” analysis. First, <strong>adding random effects shifts the 80% contour to the left.</strong> Pipeline B reaches 80% power with noticeably fewer animals than Pipeline A across the realistic CV range. Modeling the hierarchy with <code>(1 | EmbryoID)</code> lets you save animals — a non-trivial 3R win for the same scientific question.</p>
<p>Second, and this is the punchline: <strong>in the underpowered regime below the 80% line — where most real experiments actually live — the count likelihood outshines the Gaussian one.</strong> Across most of that space CMP wins by 3 to 8 percentage points, in both pipelines. In practical terms, when you run a typical small-animal study with a real 30% effect, the count-regression test is more likely to give you the satisfying p &lt; 0.05 than the t-test on the same data. Neither method magically rescues a badly-designed study, but in the space where biology usually lives, count regression gives you the better shot at seeing the effect that is actually there.</p>
</section>
</section>
<section id="ci-precision" class="level2">
<h2 class="anchored" data-anchor-id="ci-precision">CI precision</h2>
<p>So far we’ve established that count regression is more likely to deliver the satisfying <code>p &lt; 0.05</code> in the small-n regime. But a <code>p &lt; 0.05</code> on its own isn’t the whole story. Two reasons to care about how <em>precisely</em> each method measures the effect, not just whether it can detect one.</p>
<p>First, magnitude matters. A 95% CI of [-0.1, 0.5] and a 95% CI of [0.45, 0.55] both contain a real effect, but they’re telling different scientific stories. The first is “we can’t rule out no effect”; the second is “the effect is right around 0.5”. For any biology question that needs an answer beyond yes/no, interval width is the real currency.</p>
<p>Second, and more practically, post-hoc contrasts in multi-group or multi-factor designs live or die by precision. If your group-level estimates are noisy, the pairwise comparisons that come out of them, the tests that often carry the real biological story, will be underpowered even when the overall effect is detectable. You’ll get a significant main effect and then watch every post-hoc comparison fail to clear <code>p &lt; 0.05</code>. Tight intervals at the group level are what keep that from happening.</p>
<p>The main campaign records the median 95% CI width per (design, method), which feeds four more GP emulators. The structure of this section mirrors the power section: emulator diagnostics, Sobol decomposition of what drives precision, then the headline comparison.</p>
<section id="width-emulator-diagnostics" class="level3">
<h3 class="anchored" data-anchor-id="width-emulator-diagnostics">Width emulator diagnostics</h3>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">pA <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;">plot_loo_residuals</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(em_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>width_tt, em_main_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>width_cmp),</span>
<span id="cb27-2">                         method_colors_no_re,</span>
<span id="cb27-3">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"observed \u2212 predicted log(width)"</span>)</span>
<span id="cb27-4">pB <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;">plot_loo_residuals</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(em_width_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>width_lmm, em_width_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>width_cmp),</span>
<span id="cb27-5">                         method_colors_re,</span>
<span id="cb27-6">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"observed \u2212 predicted log(width)"</span>)</span>
<span id="cb27-7">pA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> pB</span></code></pre></div>
</details>
<div class="cell-output-display">
<div id="fig-loo-width-all" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-loo-width-all-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/index_files/figure-html/fig-loo-width-all-1.png" class="img-fluid figure-img" width="960">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-loo-width-all-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;9: LOOCV residuals for the four CI-width emulators on the log scale. Top row: animal-level. Bottom row: field-level.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="what-drives-width-sobol-decomposition" class="level3">
<h3 class="anchored" data-anchor-id="what-drives-width-sobol-decomposition">What drives width: Sobol decomposition</h3>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">pA <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;">plot_sobol</span>(sobol_no_re <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;">filter</span>(target <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;">"Width"</span>), method_colors_no_re,</span>
<span id="cb28-2">                 <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;">"Animal-level (no random effects)"</span>)</span>
<span id="cb28-3">pB <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;">plot_sobol</span>(sobol_re    <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;">filter</span>(target <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;">"Width"</span>), method_colors_re,</span>
<span id="cb28-4">                 <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;">"Field-level (with random effects)"</span>)</span>
<span id="cb28-5">pA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> pB</span></code></pre></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_col()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_col()`).</code></pre>
</div>
<div class="cell-output-display">
<div id="fig-sobol-width-all" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-sobol-width-all-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/index_files/figure-html/fig-sobol-width-all-1.png" class="img-fluid figure-img" width="960">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-sobol-width-all-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;10: Sobol indices for the four CI-width emulators. Solid bars: first-order (main effect). Light bars: total-order (main + interactions). Error bars: 95% bootstrap CIs on total-order.
</figcaption>
</figure>
</div>
</div>
</div>
<p>In contrast to power, <code>change</code> is essentially zero, and it should be: once you have data with a real effect, the magnitude of that effect doesn’t determine how precisely you measure it. Width is governed by between-animal noise and sample size.</p>
<p>So the hierarchy reorders. <code>CV</code> is the dominant driver of width (~50% of variance) and <code>n_animals</code> is second (~15-20%). All four methods agree on this.</p>
<p>Two other things pop out. The remaining knobs <code>ν</code>, <code>m</code>, and <code>p0</code> have small first-order effects but visibly larger total-order indices: their light bars sit roughly 2-5× higher than their solid bars. Most of their influence flows through interactions with the other knobs rather than directly. And the four methods look strikingly similar on this decomposition. The precision advantage we are about to see does not come from one method responding to a knob the others ignore. It comes from something subtler: how efficiently each method extracts information from the same drivers.</p>
</section>
<section id="per-design-ci-width-comparison" class="level3">
<h3 class="anchored" data-anchor-id="per-design-ci-width-comparison">Per-design CI-width comparison</h3>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1">pair_no_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> main_summary_no_re <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb30-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(point_id, method, median_width) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb30-3">  <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> method, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> median_width) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb30-4">  <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;">pct_narrower =</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> cmp_glm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> t_test) <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;">100</span>)</span>
<span id="cb30-5">med_no <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;">median</span>(pair_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_narrower, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb30-6">win_no <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;">mean</span>(pair_no_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_narrower <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb30-7"></span>
<span id="cb30-8">pair_re <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> main_summary_re <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb30-9">  <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>point_id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> excluded_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>main) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb30-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(point_id, method, median_width) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb30-11">  <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> method, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> median_width) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb30-12">  <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;">pct_narrower =</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> cmp_glmm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> lmm) <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;">100</span>)</span>
<span id="cb30-13">med_re <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;">median</span>(pair_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_narrower, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb30-14">win_re <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;">mean</span>(pair_re<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_narrower <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb30-15"></span>
<span id="cb30-16">pA <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;">ggplot</span>(pair_no_re, <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> pct_narrower)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</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;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2C4E73"</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.85</span>,</span>
<span id="cb30-18">                 <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;">"white"</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.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <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;">"grey30"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> med_no,  <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;">"#7A3431"</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.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</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;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%+d%%"</span>, x)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> med_no, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>,</span>
<span id="cb30-23">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</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;">"median = %+.1f%%"</span>, med_no),</span>
<span id="cb30-24">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.3</span>, <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;">"#7A3431"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</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;">"Animal-level: CMP GLM vs Welch's t-test (%.0f%% of designs CMP is tighter)"</span>,</span>
<span id="cb30-26">                       <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> win_no),</span>
<span id="cb30-27">       <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;">"CMP's CI narrower than Welch's by"</span>, <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;">"design points"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb30-29"></span>
<span id="cb30-30">pB <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;">ggplot</span>(pair_re, <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> pct_narrower)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</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;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2C4E73"</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.85</span>,</span>
<span id="cb30-32">                 <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;">"white"</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.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <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;">"grey30"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> med_re, <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;">"#7A3431"</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.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</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;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%+d%%"</span>, x)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> med_re, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>,</span>
<span id="cb30-37">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</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;">"median = %+.1f%%"</span>, med_re),</span>
<span id="cb30-38">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.3</span>, <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;">"#7A3431"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</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;">"Field-level: CMP GLMM vs LMM (%.0f%% of designs CMP GLMM is tighter)"</span>,</span>
<span id="cb30-40">                       <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> win_re),</span>
<span id="cb30-41">       <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;">"CMP GLMM's CI narrower than LMM's by"</span>, <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;">"design points"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-42">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb30-43"></span>
<span id="cb30-44">pA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> pB</span></code></pre></div>
</details>
<div class="cell-output-display">
<div id="fig-width-summary-all" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-width-summary-all-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/index_files/figure-html/fig-width-summary-all-1.png" class="img-fluid figure-img" width="960">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-width-summary-all-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;11: Paired per-design CI-width gap across the 300 main-campaign LHS points, both pipelines. Negative: Gaussian method is tighter. Positive: count-likelihood method is tighter. Dashed line: parity. Coloured line: median.
</figcaption>
</figure>
</div>
</div>
</div>
<p>At the animal level, <strong>CMP’s CI is narrower at ~97% of the 300 LHS designs, with a median ~7% improvement</strong>. At the field level the effect is stronger: <strong>CMP GLMM is narrower at essentially every design, with a median ~12% improvement</strong>. The asymmetry makes sense: the field-level fit uses all the data instead of pre-aggregating, which gives the count likelihood more room to exploit the dispersion parameter.</p>
</section>
</section>
</section>
<section id="bottom-line" class="level1">
<h1>Bottom line</h1>
<p>Across 300 realistic designs and three metrics, the picture is consistent.</p>
<p><strong>When the experiment is well-powered, all four methods agree.</strong> At and above 80% power, the choice between Welch, LMM, CMP GLM, and CMP GLMM doesn’t materially change the answer. If you have plenty of animals and a large effect, use whichever you are comfortable with.</p>
<p><strong>When the experiment is underpowered, the count likelihood gives you the better shot at seeing the effect.</strong> Across most of the realistic small-n / moderate-CV regime where biology usually lives, CMP wins by 3 to 8 percentage points of size-adjusted power over the Gaussian alternative. No method magically rescues a badly-designed study, but in the regime where the answer is hard to read, count regression is the better tool.</p>
<p><strong>Modeling the field-level hierarchy further shifts the 80% power contour to the left.</strong> Field-level fits with <code>(1 | EmbryoID)</code> reach 80% power with noticeably fewer animals than animal-level aggregation does. A direct 3R win for the same scientific question.</p>
<p><strong>On precision, the count likelihood almost always wins.</strong> CMP GLM produces narrower 95% CIs at ~97% of the 300 LHS designs (median ~7% narrower than Welch). CMP GLMM does it at essentially every design (median ~12% narrower than LMM). Precision matters wherever the work continues past a single yes/no test: post-hoc contrasts in multi-factor designs, replication studies, meta-analyses, or simply translating “X% increase” into a defensible biological claim.</p>
<section id="practical-recommendation" class="level3">
<h3 class="anchored" data-anchor-id="practical-recommendation">Practical recommendation</h3>
<ul>
<li><strong>Default</strong>: CMP GLMM with <code>glmmTMB(Y ~ Group + offset(log(N)) + (1 | EmbryoID), family = compois())</code>. At least as good as any other choice in every regime we tested, and meaningfully better in the regime where most experiments live.</li>
<li><strong>Acceptable fallback</strong>: animal-level CMP GLM if the random-effect fit doesn’t converge (sometimes happens at very small n with very rare events).</li>
<li><strong>Reasonable but suboptimal</strong>: Welch’s t-test on per-animal ratios, if you have a well-resourced experiment and are not chasing subtle effects.</li>
</ul>
<p>There is no real-world downside to the count-likelihood approach. Its mild Type-I anti-conservatism at the animal level is small enough that size adjustment handles it, and you gain power and precision essentially for free.</p>
</section>
</section>
<section id="references" class="level1">
<h1>References</h1>
<p><strong>Methods.</strong> The Type-I calibration band comes from Bradley (1978). The size-adjusted power formula is the first-order Lloyd correction (Lloyd, 2005). The biology, imaging pipeline, and count-regression case that this post builds on were developed in Bogomolovas et al.&nbsp;(2022).</p>
<ul>
<li>Bogomolovas, J., et al.&nbsp;(2022). <em>[TODO: fill in title — PMID 36563012]</em>. <a href="https://pubmed.ncbi.nlm.nih.gov/36563012/" class="uri">https://pubmed.ncbi.nlm.nih.gov/36563012/</a></li>
<li>Bradley, J. V. (1978). Robustness? <em>British Journal of Mathematical and Statistical Psychology</em>, 31, 144–152.</li>
<li>Lloyd, C. J. (2005). Estimating test power adjusted for size. <em>Journal of Statistical Computation and Simulation</em>, 75(11), 921–933.</li>
</ul>
<p><strong>R packages.</strong> Citations below are generated from the rendering session via <code>grateful::cite_packages()</code>. Full citations follow.</p>
<p>We used R v. 4.5.2 <span class="citation" data-cites="base">(R Core Team 2025)</span> and the following R packages: COMMultReg v. 0.1.1 <span class="citation" data-cites="COMMultReg">(Raim and Morris 2026)</span>, emmeans v. 1.11.2.8 <span class="citation" data-cites="emmeans">(Lenth 2025)</span>, GGally v. 2.4.0 <span class="citation" data-cites="GGally">(Schloerke et al. 2025)</span>, glmmTMB v. 1.1.14 <span class="citation" data-cites="glmmTMB2017 glmmTMB2025">(Brooks et al. 2017; McGillycuddy et al. 2025)</span>, hetGP v. 1.1.8 <span class="citation" data-cites="hetGP">(Binois and Gramacy 2025)</span>, knitr v. 1.50 <span class="citation" data-cites="knitr2014 knitr2015 knitr2025">(Xie 2014, 2015, 2025)</span>, lhs v. 1.2.1 <span class="citation" data-cites="lhs">(Carnell 2026)</span>, lme4 v. 1.1.37 <span class="citation" data-cites="lme4">(Bates et al. 2015)</span>, lmerTest v. 3.1.3 <span class="citation" data-cites="lmerTest">(Kuznetsova, Brockhoff, and Christensen 2017)</span>, patchwork v. 1.3.2 <span class="citation" data-cites="patchwork">(Pedersen 2025)</span>, rmarkdown v. 2.30 <span class="citation" data-cites="rmarkdown2018 rmarkdown2020 rmarkdown2025">(Xie, Allaire, and Grolemund 2018; Xie, Dervieux, and Riederer 2020; Allaire et al. 2025)</span>, scales v. 1.4.0 <span class="citation" data-cites="scales">(Wickham, Pedersen, and Seidel 2025)</span>, tidyverse v. 2.0.0 <span class="citation" data-cites="tidyverse">(Wickham et al. 2019)</span>.</p>
<div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-rmarkdown2025" class="csl-entry">
Allaire, JJ, Yihui Xie, Christophe Dervieux, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, et al. 2025. <em><span class="nocase">rmarkdown</span>: Dynamic Documents for r</em>. <a href="https://github.com/rstudio/rmarkdown">https://github.com/rstudio/rmarkdown</a>.
</div>
<div id="ref-lme4" class="csl-entry">
Bates, Douglas, Martin Mächler, Ben Bolker, and Steve Walker. 2015. <span>“Fitting Linear Mixed-Effects Models Using <span class="nocase">lme4</span>.”</span> <em>Journal of Statistical Software</em> 67 (1): 1–48. <a href="https://doi.org/10.18637/jss.v067.i01">https://doi.org/10.18637/jss.v067.i01</a>.
</div>
<div id="ref-hetGP" class="csl-entry">
Binois, Mickael, and Robert B. Gramacy. 2025. <em><span class="nocase">hetGP</span>: Heteroskedastic Gaussian Process Modeling and Design Under Replication</em>. <a href="https://doi.org/10.32614/CRAN.package.hetGP">https://doi.org/10.32614/CRAN.package.hetGP</a>.
</div>
<div id="ref-glmmTMB2017" class="csl-entry">
Brooks, Mollie E., Kasper Kristensen, Koen J. van Benthem, Arni Magnusson, Casper W. Berg, Anders Nielsen, Hans J. Skaug, Martin Maechler, and Benjamin M. Bolker. 2017. <span>“<span class="nocase">glmmTMB</span> Balances Speed and Flexibility Among Packages for Zero-Inflated Generalized Linear Mixed Modeling.”</span> <em>The R Journal</em> 9 (2): 378–400. <a href="https://doi.org/10.32614/RJ-2017-066">https://doi.org/10.32614/RJ-2017-066</a>.
</div>
<div id="ref-lhs" class="csl-entry">
Carnell, Rob. 2026. <em><span class="nocase">lhs</span>: Latin Hypercube Samples</em>. <a href="https://doi.org/10.32614/CRAN.package.lhs">https://doi.org/10.32614/CRAN.package.lhs</a>.
</div>
<div id="ref-lmerTest" class="csl-entry">
Kuznetsova, Alexandra, Per B. Brockhoff, and Rune H. B. Christensen. 2017. <span>“<span class="nocase">lmerTest</span> Package: Tests in Linear Mixed Effects Models.”</span> <em>Journal of Statistical Software</em> 82 (13): 1–26. <a href="https://doi.org/10.18637/jss.v082.i13">https://doi.org/10.18637/jss.v082.i13</a>.
</div>
<div id="ref-emmeans" class="csl-entry">
Lenth, Russell V. 2025. <em><span class="nocase">emmeans</span>: Estimated Marginal Means, Aka Least-Squares Means</em>. <a href="https://doi.org/10.32614/CRAN.package.emmeans">https://doi.org/10.32614/CRAN.package.emmeans</a>.
</div>
<div id="ref-glmmTMB2025" class="csl-entry">
McGillycuddy, Maeve, David I. Warton, Gordana Popovic, and Benjamin M. Bolker. 2025. <span>“Parsimoniously Fitting Large Multivariate Random Effects in <span class="nocase">glmmTMB</span>.”</span> <em>Journal of Statistical Software</em> 112 (1): 1–19. <a href="https://doi.org/10.18637/jss.v112.i01">https://doi.org/10.18637/jss.v112.i01</a>.
</div>
<div id="ref-patchwork" class="csl-entry">
Pedersen, Thomas Lin. 2025. <em><span class="nocase">patchwork</span>: The Composer of Plots</em>. <a href="https://doi.org/10.32614/CRAN.package.patchwork">https://doi.org/10.32614/CRAN.package.patchwork</a>.
</div>
<div id="ref-base" class="csl-entry">
R Core Team. 2025. <em><span>R</span>: A Language and Environment for Statistical Computing</em>. Vienna, Austria: R Foundation for Statistical Computing. <a href="https://www.R-project.org/">https://www.R-project.org/</a>.
</div>
<div id="ref-COMMultReg" class="csl-entry">
Raim, Andrew M., and Darcy Steeg Morris. 2026. <em><span>COMMultReg</span>: Conway-Maxwell Multinomial Regression</em>.
</div>
<div id="ref-GGally" class="csl-entry">
Schloerke, Barret, Di Cook, Joseph Larmarange, Francois Briatte, Moritz Marbach, Edwin Thoen, Amos Elberg, and Jason Crowley. 2025. <em><span>GGally</span>: Extension to <span>“<span class="nocase">ggplot2</span>”</span></em>. <a href="https://doi.org/10.32614/CRAN.package.GGally">https://doi.org/10.32614/CRAN.package.GGally</a>.
</div>
<div id="ref-tidyverse" class="csl-entry">
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. <span>“Welcome to the <span class="nocase">tidyverse</span>.”</span> <em>Journal of Open Source Software</em> 4 (43): 1686. <a href="https://doi.org/10.21105/joss.01686">https://doi.org/10.21105/joss.01686</a>.
</div>
<div id="ref-scales" class="csl-entry">
Wickham, Hadley, Thomas Lin Pedersen, and Dana Seidel. 2025. <em><span class="nocase">scales</span>: Scale Functions for Visualization</em>. <a href="https://doi.org/10.32614/CRAN.package.scales">https://doi.org/10.32614/CRAN.package.scales</a>.
</div>
<div id="ref-knitr2014" class="csl-entry">
Xie, Yihui. 2014. <span>“<span class="nocase">knitr</span>: A Comprehensive Tool for Reproducible Research in <span>R</span>.”</span> In <em>Implementing Reproducible Computational Research</em>, edited by Victoria Stodden, Friedrich Leisch, and Roger D. Peng. Chapman; Hall/CRC.
</div>
<div id="ref-knitr2015" class="csl-entry">
———. 2015. <em>Dynamic Documents with <span>R</span> and Knitr</em>. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. <a href="https://yihui.org/knitr/">https://yihui.org/knitr/</a>.
</div>
<div id="ref-knitr2025" class="csl-entry">
———. 2025. <em><span class="nocase">knitr</span>: A General-Purpose Package for Dynamic Report Generation in <span>R</span></em>. <a href="https://yihui.org/knitr/">https://yihui.org/knitr/</a>.
</div>
<div id="ref-rmarkdown2018" class="csl-entry">
Xie, Yihui, J. J. Allaire, and Garrett Grolemund. 2018. <em>R Markdown: The Definitive Guide</em>. Boca Raton, Florida: Chapman; Hall/CRC. <a href="https://bookdown.org/yihui/rmarkdown">https://bookdown.org/yihui/rmarkdown</a>.
</div>
<div id="ref-rmarkdown2020" class="csl-entry">
Xie, Yihui, Christophe Dervieux, and Emily Riederer. 2020. <em>R Markdown Cookbook</em>. Boca Raton, Florida: Chapman; Hall/CRC. <a href="https://bookdown.org/yihui/rmarkdown-cookbook">https://bookdown.org/yihui/rmarkdown-cookbook</a>.
</div>
</div>
<hr>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Reproducibility
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>The expensive parts of this analysis (LHS simulation campaigns, GP fitting with LOOCV, Sobol index computation) are all done offline. The .qmd just loads precomputed <code>.rds</code> bundles.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 37%">
<col style="width: 62%">
</colgroup>
<thead>
<tr class="header">
<th>file</th>
<th>contents</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>lhs_null_campaign.rds</code></td>
<td>300-point null LHS, animal-level fits</td>
</tr>
<tr class="even">
<td><code>lhs_campaign.rds</code></td>
<td>300-point main LHS, animal-level fits</td>
</tr>
<tr class="odd">
<td><code>lhs_null_campaign_re.rds</code></td>
<td>300-point null LHS, field-level fits</td>
</tr>
<tr class="even">
<td><code>lhs_campaign_re.rds</code></td>
<td>300-point main LHS, field-level fits</td>
</tr>
<tr class="odd">
<td><code>gp_null_emulators.rds</code></td>
<td>Pipeline A Type-I GP emulators (with LOOCV)</td>
</tr>
<tr class="even">
<td><code>gp_emulators.rds</code></td>
<td>Pipeline A power + width GP emulators (with LOOCV)</td>
</tr>
<tr class="odd">
<td><code>gp_emulators_re.rds</code></td>
<td>Pipeline B GP emulators: Type-I, power, width (with LOOCV)</td>
</tr>
<tr class="even">
<td><code>sobol_no_re_indices.rds</code></td>
<td>Pipeline A Sobol indices for power + width</td>
</tr>
<tr class="odd">
<td><code>sobol_re_indices.rds</code></td>
<td>Pipeline B Sobol indices for power + width</td>
</tr>
</tbody>
</table>
<p>All <code>gp_*</code> and <code>sobol_*</code> bundles are rebuilt by <code>refit_emulators.R</code> (run once from the <code>.qmd</code> directory). The script’s core fitting logic is reproduced earlier in this document as the <code>refit-doc</code> chunk.</p>
</div>
</div>
</div>


</section>

 ]]></description>
  <category>R</category>
  <category>GLM</category>
  <category>GLMM</category>
  <category>Count Regression</category>
  <category>CMP</category>
  <category>Power Analysis</category>
  <category>Gaussian Process</category>
  <category>LHS</category>
  <category>Sobol</category>
  <guid>https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/</guid>
  <pubDate>Tue, 19 May 2026 07:00:00 GMT</pubDate>
  <media:content url="https://jbogomolovas2.github.io/Julius-s-Blog/posts/comparison/heart.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>“And I’m looking for the funnies”: sentiment in Lithuanian news headlines</title>
  <dc:creator>Julius Bogomolovas</dc:creator>
  <link>https://jbogomolovas2.github.io/Julius-s-Blog/posts/sentiment_analysis/</link>
  <description><![CDATA[ 





<p>There’s a moment in Suzanne Vega’s <em>Tom’s Diner</em> where the narrator picks up a newspaper, skips the stories, and goes straight to the funnies. It’s a small, accurate observation about reading the news: most of it doesn’t feel good, so you protect yourself.</p>
<p>I wanted to know how true that is, in numbers. Are Lithuanian news headlines as relentlessly negative as they feel? Are some outlets more negative than others?</p>
<p>To find out, I built a small system that hourly pulls RSS feeds from 7 Lithuanian outlets, deduplicates by URL, and sends each new headline to Claude Sonnet 4.6. The model returns three things: a sentiment score in [-1, +1], an IPTC Media Topic label (one of 17 international press categories like <em>politics</em>, <em>sport</em>, <em>crime, law and justice</em>), and up to three named entities.</p>
<p>Two things made this approach attractive. First, Lithuanian is heavily inflected and has limited classical NLP tooling, so a frontier multilingual model handles it cleanly without needing to train anything. Second, using IPTC’s controlled vocabulary instead of letting the model invent topic labels makes per-topic comparisons coherent across outlets. Cost is about $0.001 per headline.</p>
<p>After a week, I had 4,832 scored headlines spanning 9 days. Here’s the distribution of sentiment scores:</p>
<p><em>Examples below appear in the original Lithuanian with English translations.</em></p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/sentiment_analysis/index_files/figure-html/plot1_distribution-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>To make the score concrete, here are real processed headlines from the dataset at three different sentiment levels and topics. Claude does a pretty good job.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Example headlines at three sentiment levels</caption>
<colgroup>
<col style="width: 2%">
<col style="width: 17%">
<col style="width: 36%">
<col style="width: 43%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Score</th>
<th style="text-align: left;">Topic</th>
<th style="text-align: left;">Lithuanian</th>
<th style="text-align: left;">English</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">-0.7</td>
<td style="text-align: left;">disaster, accident and emergency incident</td>
<td style="text-align: left;">Vilkaviškyje dingo iš ligoninės pasišalinęs nepilnametis</td>
<td style="text-align: left;">A minor went missing after leaving a hospital in Vilkaviškis</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.0</td>
<td style="text-align: left;">economy, business and finance</td>
<td style="text-align: left;">Klausiate, atsakome. Ar antstoliai nuskaitys atgautas antrosios pensijų pakopos lėšas?</td>
<td style="text-align: left;">Q&amp;A on whether bailiffs will deduct recovered second-pillar pension funds</td>
</tr>
<tr class="odd">
<td style="text-align: left;">+0.7</td>
<td style="text-align: left;">education</td>
<td style="text-align: left;">Klaipėdos Vydūno gimnazijos pirmokai Lietuvai atstovaus robotikos čempionate Honkonge</td>
<td style="text-align: left;">Klaipėda’s Vydūnas Gymnasium first-years will represent Lithuania at a robotics championship in Hong Kong</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The distribution is clearly bimodal, with two humps separated by a trough at zero. Headlines exist to provoke; pure neutrality is rare. Most lean one way or the other, and few are ambivalent.</p>
<p>So I went looking for differences between outlets. The chart said <code>15min.lt</code> was the most negative outlet in Lithuania. The chart was wrong. Not because the number was wrong, but because the comparison wasn’t fair. Here’s what went wrong, and what the data actually shows.</p>
<section id="the-first-wrong-finding" class="level1 unnumbered">
<h1 class="unnumbered">The first (wrong) finding</h1>
<p>I plotted mean sentiment per outlet, ranked them, and got this:</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/sentiment_analysis/index_files/figure-html/plot2_naive-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>By this measure, 15min sits noticeably below everyone else. If I had published this as the result, it would have looked clean and confident, and it would have been misleading.</p>
</section>
<section id="what-i-missed" class="level1 unnumbered">
<h1 class="unnumbered">What I missed</h1>
<p>Outlets cover different mixes of topics, and topics carry their own sentiment baseline. Crime headlines are negative everywhere; sport headlines are positive everywhere. So a fair comparison has to account for what each outlet writes about, not just how their headlines score on average.</p>
</section>
<section id="what-the-model-says" class="level1 unnumbered">
<h1 class="unnumbered">What the model says</h1>
<p>The right tool is <strong>partial pooling with a hierarchical Bayesian model</strong>. The intuition: every headline’s sentiment is a sum of effects, namely what topic it’s about, which outlet wrote it, and whether this particular outlet treats this particular topic differently from average. The model estimates each piece simultaneously and shrinks small-sample estimates toward the population mean. So an outlet with three crime headlines doesn’t get to claim a wildly different “crime tone” from one with three hundred. Its estimate gets pulled toward what we know about crime coverage in general.</p>
<p>Three modelling choices need a brief defence:</p>
<p><strong>Why ordinal, not Gaussian?</strong> A linear regression on sentiment treats the score as if it were continuous and normally distributed. It isn’t. The scores live on 21 discrete rungs from -1.0 to +1.0 in steps of 0.1, and the histogram you saw earlier is bimodal, not bell-shaped. Fitting a Gaussian model gives sensible <em>rankings</em> but unreliable uncertainty estimates and ugly posterior predictive checks. Cumulative ordinal regression handles the discrete, bimodal structure natively.</p>
<p><strong>Why Bayesian, not frequentist?</strong> I fit several frequentist models first as sanity checks (<code>lm</code>, <code>polr</code>, <code>clmm</code>), and they all agreed on the substantive answer. The Bayesian fit adds three things the frequentist alternatives can’t: honest credible intervals on variance components (especially important when the source-level variance is genuinely small and a frequentist point estimate near zero is misleading), proper partial pooling that’s robust to sparse cells in the source by topic interaction, and posterior distributions that visualise uncertainty as shape rather than as a confidence-interval bar.</p>
<p><strong>Why the source by topic interaction?</strong> Without it, “outlets are similar on average” would be the only thing the model could say. The interaction term is what lets us ask the more interesting question: does any outlet treat any specific topic unusually, even if it doesn’t differ on average? That’s where editorial fingerprints actually live, and we’ll come back to it.</p>
<p>I fit the model in <code>brms</code> with weakly informative priors and 8 chains by 2000 iterations. All R-hat values came in below 1.01. Diagnostics are in the appendix.</p>
<p>The first thing the model tells us is the variance budget, namely how much of headline-level sentiment variation lives at each level:</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/sentiment_analysis/index_files/figure-html/variance_components-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>Topic-level variation is roughly 12 times outlet-level variation. The story is overwhelmingly: <em>what</em> a headline is about determines <em>how</em> it reads, far more than <em>who</em> wrote it.</p>
</section>
<section id="topic-effects-the-dominant-signal" class="level1 unnumbered">
<h1 class="unnumbered">Topic effects, the dominant signal</h1>
<p>Here’s how each topic shifts sentiment, regardless of outlet. Crime and disaster anchor the negative end across the board; sport and culture anchor the positive end:</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/sentiment_analysis/index_files/figure-html/plot3_topics-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>The pattern matches intuition: arts, lifestyle, sport, and science cluster at the top. These are stories of achievement, performance, and discovery. Crime and disaster anchor the bottom, with conflict close behind. Politics sits in mildly negative territory, while weather, economy, and health hover near zero, where the framing depends almost entirely on the specific event.</p>
<p>The range is what matters most. Topic effects span more than 5 log-odds top to bottom, a huge range, meaning a sport headline is many times more likely to land in the upper sentiment categories than a crime headline, regardless of who wrote it. Outlet effects, as we’ll see next, span barely a tenth of that.</p>
</section>
<section id="outlet-effects-whats-left-after-topic" class="level1 unnumbered">
<h1 class="unnumbered">Outlet effects, what’s left after topic</h1>
<p>Once the model accounts for topic mix, the gap between outlets nearly disappears:</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/sentiment_analysis/index_files/figure-html/plot4_outlets-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>These are the same outlets that looked dramatically different in the naive chart. After the model accounts for what each one covers, they’re nearly indistinguishable. Lrytas and 15min lean slightly negative; LRT and Verslo žinios lean slightly positive; the credible intervals overlap heavily and most include zero.</p>
<p>Notice also that the order has shifted from the naive chart. Vakaru ekspresas was at the top of the raw ranking on the strength of just 133 headlines; partial pooling now pulls that small-sample estimate toward the population mean, and Verslo žinios takes the top spot. The differences remain small either way.</p>
<p>The naive ranking didn’t lie about the numbers. It lied about what those numbers meant.</p>
</section>
<section id="where-outlets-actually-differ" class="level1 unnumbered">
<h1 class="unnumbered">Where outlets actually differ</h1>
<p>But “outlets are similar on average” doesn’t mean “outlets are interchangeable.” The interaction term, <em>does this outlet cover this specific topic differently from how that topic is treated on average?</em>, is where editorial fingerprints actually live.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/sentiment_analysis/index_files/figure-html/plot5_interactions-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>These are the cells that survive the strictest filter, namely combinations where we are 95% confident the effect is nonzero. They’re not the only differences between outlets, just the ones we can be confident about given the data we have.</p>
<p>On the positive side: LRT covers human interest and sport more positively than other outlets do, and 15min covers health more positively than the cross-outlet average for health. On the negative side: Lrytas covers disaster more negatively, LRT covers conflict more negatively, and Delfi covers sport more negatively than the cross-outlet baseline. The Delfi sport finding is notable because sport runs positive everywhere else.</p>
<p>The story isn’t “this outlet is negative.” The story is “this outlet covers <em>this thing</em> unusually.”</p>
</section>
<section id="what-i-can-and-cant-say" class="level1 unnumbered">
<h1 class="unnumbered">What I can and can’t say</h1>
<p>This analysis supports:</p>
<ul>
<li>Lithuanian news headlines on average sit slightly below sentiment-neutral.</li>
<li>Topic is by far the strongest determinant of headline sentiment, about an order of magnitude larger than the outlet effect.</li>
<li>After controlling for topic mix, outlets are far more similar in tone than the raw chart suggests.</li>
<li>Editorial differences between outlets exist, but live mostly in <em>which topics they cover unusually</em>, not in average tone.</li>
<li>Crime, conflict, and disaster anchor the negative end across all outlets; sport and culture anchor the positive end.</li>
</ul>
<p>This analysis does <strong>not</strong> support:</p>
<ul>
<li>Causal claims about why outlets cover what they do.</li>
<li>Claims about <em>framing</em> in any deep sense. Sentiment of a headline is not the same as bias of a story.</li>
<li>Generalisation beyond the specific RSS feeds I sampled.</li>
<li>Anything about audience response. Sentiment of headlines is not sentiment of readers.</li>
</ul>
</section>
<section id="methodology" class="level1 unnumbered">
<h1 class="unnumbered">Methodology</h1>
<p>4,832 headlines from 7 Lithuanian outlets (15min, Bernardinai, Delfi, LRT, Lrytas, Vakaru ekspresas, Verslo zinios) over 9 days. Sentiment scores live on 21 discrete rungs from -1.0 to +1.0 in steps of 0.1, assigned by Claude Sonnet 4.6. Topic labels come from the IPTC Media Topics taxonomy (17 top-level categories). The statistical model is a cumulative ordinal Bayesian regression with random intercepts for source, topic, and source by topic, fit with <code>brms</code> using 8 chains by 2000 iterations (1000 warmup). Priors: <code>normal(0, 5)</code> for thresholds, <code>normal(0, 1)</code> for random-effect SDs. All R-hat values were below 1.01 and effective sample sizes above 1500 across parameters of interest. Pipeline and code are open source.</p>
</section>
<section id="software-and-packages" class="level1 unnumbered">
<h1 class="unnumbered">Software and packages</h1>
<div class="cell" data-layout-align="center">
<p>We used R v. 4.5.2 <span class="citation" data-cites="base">(R Core Team 2025)</span> and the following R packages: brms v. 2.23.0 <span class="citation" data-cites="brms2017 brms2018 brms2021">(Bürkner 2017, 2018, 2021)</span>, grateful v. 0.3.0 <span class="citation" data-cites="grateful">(Rodriguez-Sanchez and Jackson 2025)</span>, patchwork v. 1.3.2 <span class="citation" data-cites="patchwork">(Pedersen 2025)</span>, scales v. 1.4.0 <span class="citation" data-cites="scales">(Wickham, Pedersen, and Seidel 2025)</span>, tidybayes v. 3.0.7 <span class="citation" data-cites="tidybayes">(Kay 2024)</span>, tidyverse v. 2.0.0 <span class="citation" data-cites="tidyverse">(Wickham et al. 2019)</span>.</p>
</div>
</section>
<section id="appendix-model-diagnostics" class="level1 unnumbered">
<h1 class="unnumbered">Appendix: model diagnostics</h1>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/sentiment_analysis/index_files/figure-html/diagnostics_pp-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>If the simulated intervals cover the observed bars, the model is generating data that looks like the data we have.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Variance components: posterior summaries</caption>
<colgroup>
<col style="width: 37%">
<col style="width: 12%">
<col style="width: 13%">
<col style="width: 18%">
<col style="width: 18%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Parameter</th>
<th style="text-align: right;">Estimate</th>
<th style="text-align: right;">Est.Error</th>
<th style="text-align: right;">95% CI lower</th>
<th style="text-align: right;">95% CI upper</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">sd_source__Intercept</td>
<td style="text-align: right;">0.120</td>
<td style="text-align: right;">0.098</td>
<td style="text-align: right;">0.005</td>
<td style="text-align: right;">0.362</td>
</tr>
<tr class="even">
<td style="text-align: left;">sd_topic__Intercept</td>
<td style="text-align: right;">1.412</td>
<td style="text-align: right;">0.245</td>
<td style="text-align: right;">1.019</td>
<td style="text-align: right;">1.969</td>
</tr>
<tr class="odd">
<td style="text-align: left;">sd_source:topic__Intercept</td>
<td style="text-align: right;">0.423</td>
<td style="text-align: right;">0.061</td>
<td style="text-align: right;">0.314</td>
<td style="text-align: right;">0.554</td>
</tr>
</tbody>
</table>
</div>
</div>
<hr>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-brms2017" class="csl-entry">
Bürkner, Paul-Christian. 2017. <span>“<span class="nocase">brms</span>: An <span>R</span> Package for <span>Bayesian</span> Multilevel Models Using <span>Stan</span>.”</span> <em>Journal of Statistical Software</em> 80 (1): 1–28. <a href="https://doi.org/10.18637/jss.v080.i01">https://doi.org/10.18637/jss.v080.i01</a>.
</div>
<div id="ref-brms2018" class="csl-entry">
———. 2018. <span>“Advanced <span>Bayesian</span> Multilevel Modeling with the <span>R</span> Package <span class="nocase">brms</span>.”</span> <em>The R Journal</em> 10 (1): 395–411. <a href="https://doi.org/10.32614/RJ-2018-017">https://doi.org/10.32614/RJ-2018-017</a>.
</div>
<div id="ref-brms2021" class="csl-entry">
———. 2021. <span>“Bayesian Item Response Modeling in <span>R</span> with <span class="nocase">brms</span> and <span>Stan</span>.”</span> <em>Journal of Statistical Software</em> 100 (5): 1–54. <a href="https://doi.org/10.18637/jss.v100.i05">https://doi.org/10.18637/jss.v100.i05</a>.
</div>
<div id="ref-tidybayes" class="csl-entry">
Kay, Matthew. 2024. <em><span class="nocase">tidybayes</span>: Tidy Data and Geoms for <span>Bayesian</span> Models</em>. <a href="https://doi.org/10.5281/zenodo.1308151">https://doi.org/10.5281/zenodo.1308151</a>.
</div>
<div id="ref-patchwork" class="csl-entry">
Pedersen, Thomas Lin. 2025. <em><span class="nocase">patchwork</span>: The Composer of Plots</em>. <a href="https://doi.org/10.32614/CRAN.package.patchwork">https://doi.org/10.32614/CRAN.package.patchwork</a>.
</div>
<div id="ref-base" class="csl-entry">
R Core Team. 2025. <em><span>R</span>: A Language and Environment for Statistical Computing</em>. Vienna, Austria: R Foundation for Statistical Computing. <a href="https://www.R-project.org/">https://www.R-project.org/</a>.
</div>
<div id="ref-grateful" class="csl-entry">
Rodriguez-Sanchez, Francisco, and Connor P. Jackson. 2025. <em><span class="nocase">grateful</span>: Facilitate Citation of <span>R</span> Packages</em>. <a href="https://pakillo.github.io/grateful/">https://pakillo.github.io/grateful/</a>.
</div>
<div id="ref-tidyverse" class="csl-entry">
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. <span>“Welcome to the <span class="nocase">tidyverse</span>.”</span> <em>Journal of Open Source Software</em> 4 (43): 1686. <a href="https://doi.org/10.21105/joss.01686">https://doi.org/10.21105/joss.01686</a>.
</div>
<div id="ref-scales" class="csl-entry">
Wickham, Hadley, Thomas Lin Pedersen, and Dana Seidel. 2025. <em><span class="nocase">scales</span>: Scale Functions for Visualization</em>. <a href="https://doi.org/10.32614/CRAN.package.scales">https://doi.org/10.32614/CRAN.package.scales</a>.
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>Bayesian</category>
  <category>brms</category>
  <category>Ordinal Regression</category>
  <category>NLP</category>
  <category>LLM</category>
  <category>Lithuanian</category>
  <category>Media Analysis</category>
  <guid>https://jbogomolovas2.github.io/Julius-s-Blog/posts/sentiment_analysis/</guid>
  <pubDate>Sat, 02 May 2026 07:00:00 GMT</pubDate>
  <media:content url="https://jbogomolovas2.github.io/Julius-s-Blog/posts/sentiment_analysis/thermo.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>Six Years of Daily Steps: When Did Things Actually Change?</title>
  <dc:creator>Julius Bogomolovas</dc:creator>
  <link>https://jbogomolovas2.github.io/Julius-s-Blog/posts/steps/</link>
  <description><![CDATA[ 





<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lubridate)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mgcv)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gratia)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(scales)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(imputeTS)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(changepoint)</span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(strucchange)</span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(Rbeast)</span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>))</span>
<span id="cb1-13"></span>
<span id="cb1-14">COL_RAW    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#adb5bd"</span></span>
<span id="cb1-15">COL_FIT    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2196F3"</span></span>
<span id="cb1-16">COL_BEAST  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#6c3483"</span></span>
<span id="cb1-17">COL_MISS1  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#e74c3c"</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># missing from JSON</span></span>
<span id="cb1-18">COL_MISS2  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#e67e22"</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># zero-coded (off-wrist)</span></span>
<span id="cb1-19">COL_VG     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FF9800"</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># variogram</span></span></code></pre></div>
</details>
</div>
<section id="the-data" class="level2">
<h2 class="anchored" data-anchor-id="the-data">The data</h2>
<p>So I continue to explore the biometric data I’ve accumulated through the years with my Garmin. This time I turn to daily step counts — six years of them. Three questions drive this post: how does my step count change over time, is it affected by yearly and weekly rhythms the way my weight was, and — most importantly — can I detect abrupt changes in the series, and do those changes make any sense when I line them up with what was actually happening in my life? So let’s dig in.</p>
<p>The raw Garmin Connect export is a sprawl of JSON files (one <code>UDSFile_*.json</code> per day, nested in folders), which I parse separately into a tidy <code>daily.csv</code> — one row per day, step count and a few companion metrics I might want later. The parsing script isn’t part of this post; what matters here is the clean daily series.</p>
<p>One note before we touch the data. There are two kinds of problematic days: days missing from the JSON entirely (I wasn’t wearing the watch and Garmin has nothing to say), and days with zero steps recorded (the watch exists in the dataset but registered nothing — effectively 24 hours on a charger). I treat both as missing. A zero-step day isn’t a behavioral signal; it’s a sensor-absence signal, and lumping it in with real low-activity days would drag everything downward and confuse the changepoint detectors later.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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;"># Load pre-processed daily step series — generated locally from the raw</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Garmin JSON dump (not committed to repo)</span></span>
<span id="cb2-3">steps_raw <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;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"daily.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_col_types =</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;">|&gt;</span></span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb2-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weekday_num =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wday</span>(date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">week_start =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)),</span>
<span id="cb2-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">doy         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">yday</span>(date),</span>
<span id="cb2-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t           =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(date <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;">min</span>(date))</span>
<span id="cb2-8">  )</span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Full daily grid — lets us see (and classify) what's missing</span></span>
<span id="cb2-11">audit <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(steps_raw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date),</span>
<span id="cb2-12">                           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(steps_raw<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;">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="cb2-13">  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;">left_join</span>(steps_raw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> 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;">select</span>(date, totalSteps), <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb2-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">missing_type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb2-16">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(totalSteps) <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;">"missing JSON"</span>,</span>
<span id="cb2-17">      totalSteps <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zero-coded"</span>,</span>
<span id="cb2-18">      <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;">"observed"</span></span>
<span id="cb2-19">    ),</span>
<span id="cb2-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Promote zero-step days to NA for all downstream analysis</span></span>
<span id="cb2-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">totalSteps =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(totalSteps <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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, totalSteps)</span>
<span id="cb2-22">  )</span>
<span id="cb2-23"></span>
<span id="cb2-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Modeling-ready observed set</span></span>
<span id="cb2-25">steps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> audit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-26">  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;">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>(totalSteps)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-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;">t =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(date <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;">min</span>(date)))</span>
<span id="cb2-28"></span>
<span id="cb2-29">n_days    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(audit)</span>
<span id="cb2-30">n_obs     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(steps)</span>
<span id="cb2-31">n_missing <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n_days <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> n_obs</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_rug</span>(<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;">filter</span>(audit, missing_type <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="cb3-3">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> missing_type),</span>
<span id="cb3-4">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sides =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b"</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.7</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.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-5">  <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;">"missing JSON"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> COL_MISS1,</span>
<span id="cb3-6">                                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zero-coded"</span>   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> COL_MISS2),</span>
<span id="cb3-7">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Missing type"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> steps, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(date, totalSteps),</span>
<span id="cb3-9">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_RAW, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.55</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.40</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25000</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> comma) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_breaks =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1 year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_labels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb3-14">    <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;">"Six years of daily steps"</span>,</span>
<span id="cb3-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(</span>
<span id="cb3-16">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%s to %s · %d observed days · %d missing (%.1f%%) — missingness rug at bottom"</span>,</span>
<span id="cb3-17">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%b %Y"</span>),</span>
<span id="cb3-18">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%b %Y"</span>),</span>
<span id="cb3-19">      n_obs, n_missing, <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> n_missing <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n_days</span>
<span id="cb3-20">    ),</span>
<span id="cb3-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Daily steps"</span></span>
<span id="cb3-22">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-23">  <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;">"top"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/steps/index_files/figure-html/plot-raw-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>A few things jump out. There’s obvious daily noise — expected with behavioural data. There are stretches where activity visibly sits higher or lower for months at a time, which is the whole reason this post exists. And the missingness isn’t uniform: some years are clean, others have clear gaps where I abandoned the watch for a bit. Real life, basically. However, only 4.4% days dont have step data. not too bad I would say.</p>
</section>
<section id="a-closer-look-marginal-and-memory" class="level2">
<h2 class="anchored" data-anchor-id="a-closer-look-marginal-and-memory">A closer look — marginal and memory</h2>
<p>Before proceeding with further analysis lets have a closer look at the data.First: what does the marginal distribution of daily steps actually look like? Second: is there memory or hidden periods in the series?</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Marginal moments (hand-rolled so we don't pull in an extra package)</span></span>
<span id="cb4-2">mu    <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;">mean</span>(audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>totalSteps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb4-3">sig   <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;">sd</span>(audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>totalSteps,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb4-4">skew  <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;">mean</span>((audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>totalSteps <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu)<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;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</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> sig<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;">3</span></span>
<span id="cb4-5">ekurt <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;">mean</span>((audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>totalSteps <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu)<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;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</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> sig<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;">4</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;">3</span></span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Empirical variogram on the full calendar-aligned series</span></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># γ(h) = 0.5 × mean squared difference at lag h</span></span>
<span id="cb4-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Uses audit (not steps) so "lag" means calendar days, not observation index.</span></span>
<span id="cb4-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NA pairs are simply skipped — no imputation, no gap-filling.</span></span>
<span id="cb4-11">compute_variogram <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_lag =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>) {</span>
<span id="cb4-12">  n <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;">length</span>(x)</span>
<span id="cb4-13">  gamma   <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;">numeric</span>(max_lag)</span>
<span id="cb4-14">  n_pairs <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;">integer</span>(max_lag)</span>
<span id="cb4-15">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (h <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>max_lag) {</span>
<span id="cb4-16">    d  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> h)] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x[(<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> h)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n]</span>
<span id="cb4-17">    ok <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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>(d)</span>
<span id="cb4-18">    n_pairs[h] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(ok)</span>
<span id="cb4-19">    gamma[h]   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</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>(d[ok]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb4-20">  }</span>
<span id="cb4-21">  <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;">lag =</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>max_lag, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gamma =</span> gamma, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_pairs =</span> n_pairs)</span>
<span id="cb4-22">}</span>
<span id="cb4-23"></span>
<span id="cb4-24">vg          <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;">compute_variogram</span>(audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>totalSteps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_lag =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>)</span>
<span id="cb4-25">sample_var  <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;">var</span>(audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>totalSteps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb4-26">vg          <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> vg <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;">gamma_norm =</span> gamma <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sample_var)</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Marginal density vs. matched Gaussian</span></span>
<span id="cb5-2">p_density <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;">ggplot</span>(audit, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(totalSteps)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> COL_RAW, <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.5</span>, <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;">NA</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_function</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> dnorm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">args =</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;">mean =</span> mu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> sig),</span>
<span id="cb5-5">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_FIT, <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.9</span>, <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;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> mu,</span>
<span id="cb5-7">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_FIT, <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 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.4</span>) <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;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30000</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;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> comma) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Marginal distribution"</span>,</span>
<span id="cb5-11">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</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;">"Empirical (grey) vs. matched Gaussian (blue dashed) · skew = %.2f · excess kurtosis = %.2f"</span>,</span>
<span id="cb5-12">                          skew, ekurt),</span>
<span id="cb5-13">       <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;">"Daily steps"</span>, <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;">"Density"</span>)</span>
<span id="cb5-14"></span>
<span id="cb5-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Normal QQ plot — same story from the tail-extremes angle</span></span>
<span id="cb5-16">p_qq <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;">ggplot</span>(audit, <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;">sample =</span> totalSteps)) <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;">stat_qq</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_RAW, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</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.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_qq_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_FIT, <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 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;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> comma) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Normal QQ plot"</span>,</span>
<span id="cb5-21">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Perfect Gaussian = straight blue line"</span>,</span>
<span id="cb5-22">       <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;">"Theoretical quantiles"</span>, <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;">"Sample quantiles"</span>)</span>
<span id="cb5-23"></span>
<span id="cb5-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variogram: how fast does γ(h) climb, does it plateau, where are the dips?</span></span>
<span id="cb5-25">p_vg <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;">ggplot</span>(vg, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(lag, gamma_norm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</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;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span>),</span>
<span id="cb5-27">             <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 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;">"grey60"</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.4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>,</span>
<span id="cb5-29">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</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;">"7d"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"14d"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"21d"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"28d"</span>),</span>
<span id="cb5-30">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.8</span>, <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;">"grey40"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</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;">linetype =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey40"</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.4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">89</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"σ² (decorrelated)"</span>,</span>
<span id="cb5-33">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</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;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.8</span>, <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;">"grey40"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-34">  <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;">colour =</span> COL_VG, <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_VG, <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="cb5-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Empirical variogram γ(h) / σ²"</span>,</span>
<span id="cb5-37">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Normalized: 0 = identical, 1 = as different as random pairs · dips at 7/14/21/28 = weekly cycle"</span>,</span>
<span id="cb5-38">       <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;">"Lag (days)"</span>, <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;">"γ(h) / σ²"</span>)</span>
<span id="cb5-39"></span>
<span id="cb5-40">(p_density <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p_qq) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> p_vg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">heights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/steps/index_files/figure-html/diagnostics-plot-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p><strong>On average I do hit 10k steps a day, however it is not truely Gaussian.</strong> The distribution is approximately Gaussian, but not quite. Skew is essentially zero, so there is no big asymmetry. What is off is the shape. The empirical density is leptokurtic: narrower and peakier than a matched Gaussian in the core, with slightly fatter tails at both ends. Mass has moved from the shoulders into the peak and into the extremes. The peakiness partly reflects self-regulation, since step goals and daily routines pull counts toward a typical value, and the floor at zero forces the left side into a stricter shape than a Gaussian allows. The fatter tails are the other side of the coin: very low days (sick, travel, off-wrist) and very high days (hikes, races) show up more often than a Gaussian would predict.</p>
<p><strong>The series is mostly noise plus a clean weekly cycle.</strong> γ(h)/σ² sits around 0.75 already at lag 1, meaning only about 25% of total variance is shared between consecutive days. Most of what you see on any given day is effectively independent of the day before. What structure there is lives mostly in the weekly rhythm: clean dips in γ(h) at h = 7, 14, 21, 28, 35, where same-weekday pairs are systematically more similar than mixed ones. The dips keep going all the way out to 84 days, so the weekly cycle is phase-locked across the entire six-year record. Beyond the week, the variogram plateaus near σ², so there is little slow memory to speak of.</p>
</section>
<section id="smooth-structure-trend-weekday-and-season" class="level2">
<h2 class="anchored" data-anchor-id="smooth-structure-trend-weekday-and-season">Smooth structure: trend, weekday, and season</h2>
<p>So what did the initial inspection tell us? The marginal is leptokurtic with mildly fatter tails than a Gaussian, which means a Student-t likelihood would probably fit better than the default Gaussian one. Now yes, we know that modelling is about the <em>conditional</em> distribution of the response given the predictors, not the marginal, and those can look very different. But we go a bit crazy here and assume the marginal features will seep into the conditional, so we use <code>family = scat()</code> from <strong>mgcv</strong> <span class="citation" data-cites="mgcv2017">(Wood 2017)</span> — a scaled Student-t likelihood that handles heavy tails gracefully and downweights the outlier days (hikes, sick days) without us having to hand-tune anything.</p>
<p>On the memory side, there is a strong weekly cycle. And — wow, again, just like my weight post — we might also expect a yearly rhythm: more walking in summer, less in winter, or the opposite depending on where you live. The variogram did not show it directly because we only went out 90 days, but six years of data is enough to estimate a seasonal smooth over day-of-year. So we fit three smooths: a long-term trend <code>s(t)</code>, a cyclic weekly smooth <code>s(weekday)</code>, and a cyclic seasonal smooth <code>s(doy)</code>.</p>
<p>The cyclic basis (<code>bs = "cc"</code>) makes sure Sunday wraps back to Monday, and December 31 wraps back to January 1. Without it, the endpoints of each smooth would be artificially pulled toward zero and the cycle would look broken at the boundary.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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">gam_steps <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;">gam</span>(</span>
<span id="cb6-2">  totalSteps <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;">s</span>(t, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-3">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(weekday_num, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cc"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-4">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(doy,         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cc"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>),</span>
<span id="cb6-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data   =</span> audit <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;">mutate</span>(</span>
<span id="cb6-7">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t           =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(date <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;">min</span>(date)),</span>
<span id="cb6-8">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weekday_num =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wday</span>(date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">week_start =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)),</span>
<span id="cb6-9">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">doy         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">yday</span>(date)</span>
<span id="cb6-10">             ),</span>
<span id="cb6-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scat</span>(),</span>
<span id="cb6-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"REML"</span>,</span>
<span id="cb6-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">knots  =</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;">weekday_num =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.5</span>),</span>
<span id="cb6-14">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">doy         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">366.5</span>))</span>
<span id="cb6-15">)</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(gam_steps)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>
Family: Scaled t(5.244,3268.657) 
Link function: identity 

Formula:
totalSteps ~ s(t, k = 15) + s(weekday_num, bs = "cc", k = 7) + 
    s(doy, bs = "cc", k = 12)

Parametric coefficients:
            Estimate Std. Error z value Pr(&gt;|z|)    
(Intercept) 11432.60      77.96   146.6   &lt;2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Approximate significance of smooth terms:
                 edf Ref.df Chi.sq  p-value    
s(t)           9.522  11.03 219.64  &lt; 2e-16 ***
s(weekday_num) 4.689   5.00 292.76  &lt; 2e-16 ***
s(doy)         2.950  10.00  11.33 0.000984 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

R-sq.(adj) =  0.139   Deviance explained = 14.1%
-REML =  22553  Scale est. = 1         n = 2321</code></pre>
</div>
<details class="code-fold">
<summary>Show code</summary>
<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">gratia<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;">appraise</span>(gam_steps)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/steps/index_files/figure-html/gam-summary-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>The model explains about 14% of variance. That might sound low, but it is right in line with the budget the variogram set: about 75% of variance is day-to-day noise that no smooth model can touch. What the smooths can do is capture the structure that is there, and all three terms are clearly pulling their weight. The weekday smooth is the strongest signal by a wide margin, which matches what the variogram dips already told us. The trend smooth is almost fully using its flexibility, meaning activity really does wiggle across the six years. The seasonal smooth is real but simple — a single gentle curve over the year, no multi-peak drama. The scaled Student-t likelihood seems to be dealing well with the heavy tails: the QQ plot tracks the reference line cleanly across the bulk of the range, and <code>scat()</code> ended up estimating about 5 degrees of freedom — markedly heavier-tailed than a Gaussian, which is consistent with what the marginal distribution hinted at. Residuals vs linear predictor look roughly flat with stable variance, histogram is symmetric around zero — nothing alarming, nothing to fix.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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">date_origin <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;">min</span>(audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date)</span>
<span id="cb10-2">intercept   <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;">coef</span>(gam_steps)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Long-term trend, back on the original scale</span></span>
<span id="cb10-5">sm_t <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;">smooth_estimates</span>(gam_steps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">select =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s(t)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_confint</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-7">  <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;">date  =</span> date_origin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> t,</span>
<span id="cb10-8">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fit   =</span> .estimate <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> intercept,</span>
<span id="cb10-9">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</span> .lower_ci <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> intercept,</span>
<span id="cb10-10">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper =</span> .upper_ci <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> intercept)</span>
<span id="cb10-11"></span>
<span id="cb10-12">sm_wd  <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;">smooth_estimates</span>(gam_steps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">select =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s(weekday_num)"</span>) <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;">add_confint</span>()</span>
<span id="cb10-13">sm_doy <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;">smooth_estimates</span>(gam_steps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">select =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s(doy)"</span>)         <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;">add_confint</span>()</span>
<span id="cb10-14"></span>
<span id="cb10-15">p_trend <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;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> audit, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(date, totalSteps),</span>
<span id="cb10-17">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_RAW, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</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.25</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_ribbon</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sm_t, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> lower, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> upper),</span>
<span id="cb10-19">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> COL_FIT, <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.25</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-20">  <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;">data =</span> sm_t, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(date, fit),</span>
<span id="cb10-21">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_FIT, <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.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25000</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> comma) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_breaks =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1 year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_labels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Long-term trend s(t)"</span>,</span>
<span id="cb10-26">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weekday and seasonal effects removed"</span>,</span>
<span id="cb10-27">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Daily steps"</span>)</span>
<span id="cb10-28"></span>
<span id="cb10-29">p_wd <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;">ggplot</span>(sm_wd, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(weekday_num, .estimate)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_ribbon</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;">ymin =</span> .lower_ci, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> .upper_ci),</span>
<span id="cb10-31">              <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;">"#27ae60"</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.30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-32">  <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;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#196f3d"</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.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey50"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</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;">7</span>,</span>
<span id="cb10-35">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</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;">"Mon"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tue"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wed"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Thu"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fri"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sat"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sun"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Weekday effect s(weekday)"</span>,</span>
<span id="cb10-37">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Partial effect (steps)"</span>)</span>
<span id="cb10-38"></span>
<span id="cb10-39">p_doy <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;">ggplot</span>(sm_doy, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(doy, .estimate)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_ribbon</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;">ymin =</span> .lower_ci, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> .upper_ci),</span>
<span id="cb10-41">              <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;">"#f39c12"</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.30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-42">  <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;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#9a6a0b"</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.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey50"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">91</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">121</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">152</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">182</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">213</span>,</span>
<span id="cb10-45">                                <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">244</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">274</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">305</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">335</span>),</span>
<span id="cb10-46">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> month.abb) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Seasonal effect s(doy)"</span>,</span>
<span id="cb10-48">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Partial effect (steps)"</span>)</span>
<span id="cb10-49"></span>
<span id="cb10-50">p_trend <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (p_wd <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p_doy)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/steps/index_files/figure-html/gam-smooths-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>Three things to pick out.</p>
<p><strong>The long-term trend</strong> opens high in late 2019 at around 15 to 16k steps, slides down through 2020, and settles into a flatter regime around 11k steps for the rest of the series. The GAM paints this as a smooth slide, but a slide is just one possibility. The actual drop could have been far more abrupt than the spline is willing to show.</p>
<p><strong>The weekday effect</strong> runs the opposite way from the stereotype: weekdays (Mon to Fri) are <em>higher</em>, weekends are the trough, with Saturday and Sunday sitting roughly 2,000 to 3,000 steps below midweek. This is the structure the variogram’s h=7 dips were pointing at.</p>
<p><strong>The seasonal effect</strong> is small but real. A gentle summer peak, winter dip, consistent with living somewhere like San Diego where the weather does not dictate outdoor activity as strongly as further north. A few hundred steps of amplitude over the year.</p>
<p>So the smooth model has done its job. The weekly rhythm is cleanly isolated, the seasonal cycle is captured, and the trend shows activity settling from a high 2019 baseline to a lower plateau. But GAMs are all about the smooth, which is great, until it is not. Look at that dip at the beginning of 2020. The trend smooth glided right over it, leaving a gentle curve where there might have been a cliff. Could something be done?</p>
</section>
<section id="detecting-abrupt-change" class="level2">
<h2 class="anchored" data-anchor-id="detecting-abrupt-change">Detecting abrupt change</h2>
<p>Changepoint detection is designed for exactly this. Instead of a continuous curve, it asks: at which moments did the mean of the series jump to a new level? The output is a set of dates, not a smooth function, and that is the point. If the early-2020 dip was a cliff rather than a slide, a changepoint method should tell us where the cliff is.</p>
<p>Before running anything, two practical problems. The 107 missing days need to be handled, because none of the methods below take NAs in stride. We use Kalman imputation via <code>imputeTS::na_kalman</code> <span class="citation" data-cites="imputeTS">(Steffen Moritz and Thomas Bartz-Beielstein 2017)</span>. The bigger problem is the weekly cycle, which the GAM just showed us is by far the largest periodic component in the data. None of the changepoint methods below model periodicity natively, which means a clean weekly dip looks to them like a structural break every seven days. The standard fix is <strong>weekly aggregation</strong>: take the mean of each calendar week and the weekly cycle disappears by construction. Every week contains exactly one of each weekday, so high midweek days and low weekend days balance in the mean. The 2,428-day series collapses to around 315 weekly points, which is a manageable size for all three methods and keeps far more temporal resolution than going all the way to monthly would.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Kalman imputation for NA-handling</span></span>
<span id="cb11-2">steps_imp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> audit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb11-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">totalSteps_imp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(</span>
<span id="cb11-5">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na_kalman</span>(totalSteps, <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;">"StructTS"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">smooth =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb11-6">    )</span>
<span id="cb11-7">  )</span>
<span id="cb11-8"></span>
<span id="cb11-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Weekly aggregation. ISO week (Monday start) so each bucket covers</span></span>
<span id="cb11-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># exactly one of each weekday and the weekly cycle is cancelled.</span></span>
<span id="cb11-11">weekly <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> steps_imp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-12">  <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;">week =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor_date</span>(date, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"week"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">week_start =</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="cb11-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(week) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_steps =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(totalSteps_imp),</span>
<span id="cb11-15">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_days     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(),</span>
<span id="cb11-16">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(n_days <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;">7</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># drop partial head/tail weeks</span></span></code></pre></div>
</details>
</div>
<section id="attempt-1-pelt" class="level3">
<h3 class="anchored" data-anchor-id="attempt-1-pelt">Attempt 1: PELT</h3>
<p>PELT, short for Pruned Exact Linear Time <span class="citation" data-cites="changepoint2014">(Killick and Eckley 2014)</span>, is the workhorse of frequentist changepoint detection. It minimises squared deviations from segment means plus a BIC penalty on the number of changepoints. Fast, exact under its assumptions, easy to call.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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">cp_pelt    <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;">cpt.mean</span>(weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean_steps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PELT"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">penalty =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BIC"</span>)</span>
<span id="cb12-2">n_pelt     <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;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cpts</span>(cp_pelt))</span>
<span id="cb12-3">dates_pelt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cpts</span>(cp_pelt)]</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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">pelt_seg <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>
<span id="cb13-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week), dates_pelt <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;">7</span>),</span>
<span id="cb13-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dates_pelt, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week))</span>
<span id="cb13-4">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowwise</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-6">  <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;">mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean_steps[</span>
<span id="cb13-7">    weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> start <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> end])) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb13-9"></span>
<span id="cb13-10"><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="cb13-11">  <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;">data =</span> weekly, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(week, mean_steps),</span>
<span id="cb13-12">            <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;">"grey40"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(dates_pelt),</span>
<span id="cb13-14">             <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;">"#e74c3c"</span>, <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;">2</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.4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> pelt_seg,</span>
<span id="cb13-16">               <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> start, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> end, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mean, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> mean),</span>
<span id="cb13-17">               <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;">"#e74c3c"</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;">1.1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25000</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> comma) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_breaks =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1 year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_labels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title    =</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;">"PELT on weekly means: %d changepoints"</span>, n_pelt),</span>
<span id="cb13-22">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Red lines are detected changepoints"</span>,</span>
<span id="cb13-23">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Weekly mean steps"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/steps/index_files/figure-html/pelt-weekly-plot-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>PELT found 344 changepoints in 315 weekly observations. Essentially one changepoint per week. This is still the same failure mode as before, even with the weekly cycle neutralised: week-to-week noise in the aggregated series is large enough that BIC keeps finding it cheaper to open a new segment than to tolerate the variance as residual. PELT with default settings simply is not robust for this kind of noisy behavioural signal. A more restrictive penalty would help, but at that point the method’s usefulness becomes “whatever you tune it to,” and we can do better.</p>
</section>
<section id="attempt-2-strucchange" class="level3">
<h3 class="anchored" data-anchor-id="attempt-2-strucchange">Attempt 2: strucchange</h3>
<p><code>breakpoints()</code> from <code>strucchange</code> <span class="citation" data-cites="strucchange2002 strucchange2003">(Zeileis et al. 2002, 2003)</span> uses Bai-Perron dynamic programming to find the optimal breakpoint locations given a target number of breaks, then selects the number of breakpoints by BIC. Crucially, it also returns 95% confidence intervals for each changepoint location, the first method in this sequence that admits it is uncertain about where a break actually is.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">bp    <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;">breakpoints</span>(mean_steps <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> weekly, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb14-2">n_bp  <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;">length</span>(bp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>breakpoints)</span>
<span id="cb14-3"></span>
<span id="cb14-4">ci    <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;">confint</span>(bp)</span>
<span id="cb14-5">ci_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;">as.data.frame</span>(ci<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>confint) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setNames</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;">"lower_idx"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"point_idx"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"upper_idx"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb14-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower_idx =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(lower_idx, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb14-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper_idx =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(upper_idx, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(weekly)),</span>
<span id="cb14-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</span> weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week[lower_idx],</span>
<span id="cb14-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">point =</span> weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week[point_idx],</span>
<span id="cb14-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper =</span> weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week[upper_idx]</span>
<span id="cb14-13">  )</span>
<span id="cb14-14"></span>
<span id="cb14-15">seg_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;">tibble</span>(</span>
<span id="cb14-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week), weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week[bp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>breakpoints] <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;">7</span>),</span>
<span id="cb14-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week[bp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>breakpoints], <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week))</span>
<span id="cb14-18">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowwise</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-20">  <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;">mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean_steps[</span>
<span id="cb14-21">    weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> start <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> end])) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><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="cb15-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> audit, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(date, totalSteps),</span>
<span id="cb15-3">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_RAW, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</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.25</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-4">  <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;">data =</span> weekly, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(week, mean_steps),</span>
<span id="cb15-5">            <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;">"grey40"</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ci_df,</span>
<span id="cb15-7">            <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;">xmin =</span> lower, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</span> upper, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>),</span>
<span id="cb15-8">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> COL_FIT, <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.18</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> seg_df,</span>
<span id="cb15-10">               <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> start, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> end, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mean, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> mean),</span>
<span id="cb15-11">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_FIT, <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;">1.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ci_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;">xintercept =</span> point),</span>
<span id="cb15-13">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_FIT, <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;">2</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.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25000</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> comma) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_breaks =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1 year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_labels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title    =</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;">"strucchange on weekly means: %d changepoints"</span>, n_bp),</span>
<span id="cb15-18">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Blue bands are 95% CIs for changepoint location"</span>,</span>
<span id="cb15-19">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Daily steps"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/steps/index_files/figure-html/strucchange-weekly-plot-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>strucchange returned 3 changepoints: one in early 2020, one around the start of 2022, and one in late 2022. Four regimes: a high-activity baseline of about 13k daily steps through late 2019 and into early 2020, a drop to around 11k through 2021, a brief bounce back to ~13k in early-to-mid 2022, and then a settled lower regime of around 10.5k from late 2022 onward.</p>
<p>The confidence bands matter as much as the point estimates. The early-2020 break has a wide band stretching roughly from February to June — strucchange is confident that <em>something</em> shifted in that window, but cannot pin it to a specific week. The early-2022 break is similarly wide. The late-2022 break, by contrast, is sharp: a narrow band centred on a single point, which means the drop happened abruptly enough that the data strongly prefers one specific week for it.</p>
<p>But there is still a commitment baked in. strucchange picks one number of breakpoints, the one BIC likes best, and hands you that configuration. It does not tell you whether a model with one more or one fewer break was nearly as good, or which of its three breaks is best supported by the data. For that, we need something Bayesian.</p>
</section>
<section id="attempt-3-beast" class="level3">
<h3 class="anchored" data-anchor-id="attempt-3-beast">Attempt 3: BEAST</h3>
<p>BEAST, the Bayesian Estimator of Abrupt change, Seasonality, and Trend <span class="citation" data-cites="Rbeast2019">(Zhao et al. 2019)</span>, takes the Bayesian route. Instead of picking a single best number of changepoints, it samples across configurations and returns the <strong>posterior probability of a changepoint at each time point</strong>. The number of changepoints itself is a random variable with a distribution.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">out_wk <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;">beast</span>(</span>
<span id="cb16-2">  weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean_steps,</span>
<span id="cb16-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">season     =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb16-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tcp.minmax =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>),</span>
<span id="cb16-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tseg.min   =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>,          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># minimum segment ≈ 12 weeks (≈ 3 months)</span></span>
<span id="cb16-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">decimal_date</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(weekly<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>week)),</span>
<span id="cb16-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deltat     =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">365.25</span>,</span>
<span id="cb16-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">print.progress =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb16-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">print.options  =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb16-10">)</span>
<span id="cb16-11"></span>
<span id="cb16-12">n_beast <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> out_wk<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trend<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ncp_mode</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(out_wk)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/steps/index_files/figure-html/beast-weekly-plot-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>The native BEAST plot shows, from top to bottom: the observed weekly means with the posterior mean trend overlaid in green and trend changepoints marked as dashed lines, the posterior probability of a trend changepoint at each week <code>Pr(tcp)</code>, the trend order (how many segments BEAST is averaging over), the slope sign of the trend (positive in red, negative in blue), and the residuals. The posterior mode for the number of trend changepoints is 10.</p>
<p>The real payoff is the <code>Pr(tcp)</code> panel. Instead of a yes/no answer at each date, you see how much the data supports a changepoint there. The tallest spike sits around June 2020 with probability close to 0.85, with another strong one in March 2020 around 0.7. A third confident break shows up in late 2022. These three are the ones BEAST insists on. Smaller spikes, around late 2019, mid-2021, early 2022, and another faint one at the very end of the series, show places where the data hints at a shift but does not strongly commit. The long flat stretch from 2023 through most of 2025 is informative in its own right: BEAST finds essentially no evidence for regime change across those three years.</p>
<p>The trend panel itself tells a richer story than strucchange’s three flat segments. Activity opens high through late 2019, then takes a sharp dip in early 2020 down to around 8k, followed by a bounce back up to ~14k in mid-2020, before settling into a lower plateau around 11k that holds through 2022 with only minor wobbles. Late 2022 brings another step down to ~10k, which then remains essentially flat for the rest of the series. The early-2020 trough-and-bounce is the kind of feature a piecewise-constant method like strucchange cannot easily represent and tends to collapse into a single step, which is exactly what we saw in the previous panel where strucchange placed one break somewhere in that window and drew a flat segment across it.</p>
<p>A few parameter notes. <code>season = "none"</code> because weekly aggregation already stripped the weekly cycle. <code>tcp.minmax = c(0, 10)</code> caps the prior at at most 10 trend changepoints. <code>tseg.min = 12</code> is the main regularizer: it tells BEAST that regimes must last at least 12 weeks (roughly three months), which suppresses tiny spurious breaks while leaving room for any genuine regime shift. <code>deltat = 7 / 365.25</code> sets the time axis to weeks-expressed-as-years for a clean calendar x-axis. One last note: BEAST is fed the same Kalman-imputed series the other two methods used, purely for consistency. Unlike PELT and strucchange, BEAST can actually handle NAs natively by integrating over them in the MCMC, so the imputation was a courtesy rather than a requirement. On a series with this little missingness (~4%) the distinction does not matter, but for heavier gaps it would.</p>
</section>
</section>
<section id="what-did-the-methods-actually-find" class="level2">
<h2 class="anchored" data-anchor-id="what-did-the-methods-actually-find">What did the methods actually find?</h2>
<p>We ran three changepoint methods and fit a GAM, all without ever telling any of them about the wider world. The data knew that something shifted in early 2020, mid-2020, and late 2022. Now we can put the external context back in and see whether the data-driven breaks correspond to anything real.</p>
<p>The external context for 2020 is easy to pin down. San Diego County issued its first Stay-at-Home order on March 19, 2020, lifted June 12, 2020. A second order came into effect on December 6, 2020, and lifted on January 25, 2021. The state-wide full reopening followed on June 15, 2021. None of these dates were fed to BEAST or the GAM.</p>
<p>The summary figure has three panels. The top shows BEAST’s regime decomposition directly on the raw daily data. The middle shows the same data as a 2D density with the GAM trend drawn through it, and the San Diego lockdown periods marked as a reference. The bottom shows the two cyclic smooths from the GAM, one for weekday and one for day-of-year.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Extract BEAST's top changepoints as Date objects ----------------------</span></span>
<span id="cb18-2">decyear_to_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>(dy) {</span>
<span id="cb18-3">  yr <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;">floor</span>(dy); frac <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> yr</span>
<span id="cb18-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(yr, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-01-01"</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;">round</span>(frac <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">365.25</span>)</span>
<span id="cb18-5">}</span>
<span id="cb18-6"></span>
<span id="cb18-7">beast_cp_dates <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;">sort</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">decyear_to_date</span>(</span>
<span id="cb18-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(out_wk<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trend<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cp[<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>(out_wk<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trend<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cp)], n_beast)</span>
<span id="cb18-9">))</span>
<span id="cb18-10"></span>
<span id="cb18-11">beast_seg <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>
<span id="cb18-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date), beast_cp_dates),</span>
<span id="cb18-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(beast_cp_dates, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date))</span>
<span id="cb18-14">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowwise</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-16">  <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;">mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>totalSteps[</span>
<span id="cb18-17">    audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> start <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> end], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</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;">|&gt;</span></span>
<span id="cb18-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb18-19"></span>
<span id="cb18-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- External reference: SAH and reopening --------------------------------</span></span>
<span id="cb18-21">sah_periods <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>
<span id="cb18-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmin  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</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;">"2020-03-19"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-12-06"</span>)),</span>
<span id="cb18-23">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</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;">"2020-06-12"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2021-01-25"</span>)),</span>
<span id="cb18-24">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</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;">"SAH-1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SAH-2"</span>)</span>
<span id="cb18-25">) <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;">xmid =</span> xmin <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (xmax <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> xmin) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb18-26"></span>
<span id="cb18-27">reopen_date  <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;">"2021-06-15"</span>)</span>
<span id="cb18-28">month_breaks <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;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">91</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">121</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">152</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">182</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">213</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">244</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">274</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">305</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">335</span>)</span>
<span id="cb18-29"></span>
<span id="cb18-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- GAM smooths on the response scale ------------------------------------</span></span>
<span id="cb18-31">intercept <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;">coef</span>(gam_steps)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb18-32">sm_t  <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;">smooth_estimates</span>(gam_steps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">select =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s(t)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_confint</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-34">  <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;">date  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> t,</span>
<span id="cb18-35">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fit   =</span> .estimate <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> intercept,</span>
<span id="cb18-36">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</span> .lower_ci <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> intercept,</span>
<span id="cb18-37">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper =</span> .upper_ci <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> intercept)</span>
<span id="cb18-38"></span>
<span id="cb18-39">sm_wd  <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;">smooth_estimates</span>(gam_steps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">select =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s(weekday_num)"</span>) <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;">add_confint</span>()</span>
<span id="cb18-40">sm_doy <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;">smooth_estimates</span>(gam_steps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">select =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s(doy)"</span>)         <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;">add_confint</span>()</span>
<span id="cb18-41"></span>
<span id="cb18-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Panel A: BEAST regimes on raw data -----------------------------------</span></span>
<span id="cb18-43">p_A <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;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> audit, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(date, totalSteps),</span>
<span id="cb18-45">             <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.22</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.55</span>, <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;">"grey50"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> beast_cp_dates,</span>
<span id="cb18-47">             <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;">2</span>, <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;">"#6c3483"</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.45</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-48">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> beast_seg, <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="cb18-49">               <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> start, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> end, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mean, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> mean),</span>
<span id="cb18-50">               <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;">"#6c3483"</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;">1.4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-51">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</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;">2000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25000</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-52">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> comma, <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;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-53">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_breaks =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1 year"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_labels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y"</span>,</span>
<span id="cb18-54">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-55">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Steps"</span>,</span>
<span id="cb18-56">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</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;">"A. Daily steps with Bayesian regime detection (BEAST)"</span>),</span>
<span id="cb18-57">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</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;">"Purple = segment means between %d detected changepoints · no external information used"</span>,</span>
<span id="cb18-58">                          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(beast_cp_dates))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-59">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb18-60"></span>
<span id="cb18-61"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Panel B: 2D KDE + GAM trend + SAH overlay ----------------------------</span></span>
<span id="cb18-62">sm_t_num  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sm_t <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;">x_num =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(date))</span>
<span id="cb18-63">sah_num   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sah_periods <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-64">  <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;">x_min =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(xmin),</span>
<span id="cb18-65">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_max =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(xmax),</span>
<span id="cb18-66">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_mid =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(xmid))</span>
<span id="cb18-67">reopen_nx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(reopen_date)</span>
<span id="cb18-68"></span>
<span id="cb18-69">p_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;">ggplot</span>(audit <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;">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>(totalSteps)),</span>
<span id="cb18-70">              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(date), totalSteps)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-71">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_density_2d</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;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(density)),</span>
<span id="cb18-72">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"raster"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb18-73">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</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;">120</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-74">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_viridis_c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"magma"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sqrt"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-75">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sah_num, <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="cb18-76">            <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;">xmin =</span> x_min, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</span> x_max, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>),</span>
<span id="cb18-77">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <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;">"white"</span>, <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 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.4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-78">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sah_num, <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="cb18-79">            <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_mid, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19500</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> label),</span>
<span id="cb18-80">            <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;">"white"</span>, <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;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-81">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> reopen_nx,</span>
<span id="cb18-82">             <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;">2</span>, <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;">"white"</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;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-83">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> reopen_nx <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;">90</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19500</span>,</span>
<span id="cb18-84">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Reopen"</span>, <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;">"white"</span>, <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;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-85">  <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;">data =</span> sm_t_num, <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="cb18-86">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x_num, fit), <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;">"white"</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;">1.6</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.95</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-87">  <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;">data =</span> sm_t_num, <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="cb18-88">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x_num, fit), <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;">"#1b4f72"</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.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-89">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(</span>
<span id="cb18-90">    <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;">as.numeric</span>(<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;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-01-01"</span>),</span>
<span id="cb18-91">                            <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-01-01"</span>), <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 year"</span>)),</span>
<span id="cb18-92">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</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;">2026</span>),</span>
<span id="cb18-93">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span>)</span>
<span id="cb18-94">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-95">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</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;">5000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20000</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-96">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> comma, <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;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2500</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-97">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Steps"</span>,</span>
<span id="cb18-98">       <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. 2D density with GAM trend and SAH overlay"</span>,</span>
<span id="cb18-99">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Magma = kernel density of daily observations · blue = GAM s(t) · white dotted = SAH periods"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-100">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-101">  <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;">panel.grid =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>())</span>
<span id="cb18-102"></span>
<span id="cb18-103"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Panel C: cyclic smooths ----------------------------------------------</span></span>
<span id="cb18-104">p_C1 <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;">ggplot</span>(sm_wd, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(weekday_num, .estimate)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-105">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_ribbon</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;">ymin =</span> .lower_ci, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> .upper_ci),</span>
<span id="cb18-106">              <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;">"#27ae60"</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.30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-107">  <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;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#196f3d"</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.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-108">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey50"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-109">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</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;">7</span>,</span>
<span id="cb18-110">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</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;">"Mon"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tue"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wed"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Thu"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fri"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sat"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sun"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-111">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Partial effect (steps)"</span>,</span>
<span id="cb18-112">       <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;">"C. s(weekday)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-113">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb18-114"></span>
<span id="cb18-115">p_C2 <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;">ggplot</span>(sm_doy, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(doy, .estimate)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-116">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_ribbon</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;">ymin =</span> .lower_ci, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> .upper_ci),</span>
<span id="cb18-117">              <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;">"#f39c12"</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.30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-118">  <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;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#9a6a0b"</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.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-119">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey50"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-120">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> month_breaks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> month.abb) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-121">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Partial effect (steps)"</span>,</span>
<span id="cb18-122">       <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;">"s(doy)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-123">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb18-124"></span>
<span id="cb18-125"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Assemble -------------------------------------------------------------</span></span>
<span id="cb18-126">(p_A) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (p_B) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (p_C1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p_C2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-127">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">heights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</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.9</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-128">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_annotation</span>(</span>
<span id="cb18-129">    <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 Garmin steps: what changed, when, and how"</span>,</span>
<span id="cb18-130">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</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;">"%s to %s · %d days · %.1f%% missing"</span>,</span>
<span id="cb18-131">                       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%b %Y"</span>),</span>
<span id="cb18-132">                       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%b %Y"</span>),</span>
<span id="cb18-133">                       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(audit),</span>
<span id="cb18-134">                       <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;">is.na</span>(audit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>totalSteps))),</span>
<span id="cb18-135">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theme =</span> <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;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <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;">14</span>))</span>
<span id="cb18-136">  )</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/steps/index_files/figure-html/final-figure-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p><strong>Body keeps the score.</strong> BEAST and the GAM do a great job of reading it.</p>
<p><strong>At the beginning of the series, the GAM captures a clear reduction in daily steps, and BEAST isolates it into three segments.</strong> The lowest of them, sitting around 8k steps in Panel A, overlaps almost perfectly with the SAH-1 window drawn on Panel B. No one told the method about lockdowns. The data did.</p>
<p><strong>After SAH-1 lifts, BEAST catches the bounce: a segment back up to around 14k for a couple of months, which the GAM smooths right over.</strong> Life briefly opened up, walking resumed with interest, then settled again. BEAST shows it as a sharp peak. The GAM turns the whole episode into a gentle sag.</p>
<p><strong>Neither method flags anything for SAH-2 in winter 2020.</strong> The second order is drawn on Panel B, but the data around it looks like the surrounding weeks. Maybe the shorter order and the colder season meant less change in daily behaviour, or maybe I had already adjusted. Either way, it is not a break the data insists on.</p>
<p><strong>Then, after the full June 2021 reopening, the GAM trend rises again.</strong> Panel B shows the blue curve ticking upward through late 2021 and peaking in early 2022. BEAST agrees, placing a segment that jumps back up to around 13k for the 2022 period. Restrictions gone, activity up.</p>
<p><strong>A step down follows in late 2022.</strong> Both methods agree on this one. The GAM trend slides toward a new plateau around 10k, and BEAST places a confident changepoint right at the transition. No public anchor I can point to, but the data is certain something changed. From there, three years of quiet: flat GAM trend, no BEAST changepoints, a stable regime all the way through 2025.</p>
<p><strong>Then, at the very end of the series in 2026, activity starts to climb again.</strong> The GAM trend lifts, BEAST places one last tentative changepoint, and Panel A shows the final segment back around 11 to 12k. I walk more again.</p>
<p><strong>The weekly and seasonal smooths in Panel C.</strong> Midweek days sit about a thousand steps above the Monday baseline, and weekends drop three thousand below, consistent with weekday-dominant activity patterns. The seasonal smooth shows a gentle summer bump of a few hundred steps, with the confidence band staying wide. Small compared to the regime shifts, but real.</p>
<p>This is the point of the whole exercise. Given no knowledge of public health timelines, personal events, or my calendar, a Bayesian changepoint detector recovered the rough shape of real historical disruption, and a GAM independently traced the smooth version of the same story. The match is not perfect. BEAST places breaks in weeks, while SAH orders have specific dates. But the alignment is there, and it is there because the signal is genuinely in the data. The body kept the score.</p>
</section>
<section id="acknowledgments" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="acknowledgments">Acknowledgments</h2>
<p>Analysis performed in R with <strong>tidyverse</strong> <span class="citation" data-cites="tidyverse">(Wickham et al. 2019)</span> for data wrangling, <strong>mgcv</strong> <span class="citation" data-cites="mgcv2017">(Wood 2017)</span> for GAMs, <strong>gratia</strong> <span class="citation" data-cites="gratia2024">(Simpson 2024)</span> for smooth extraction and diagnostics, <strong>imputeTS</strong> <span class="citation" data-cites="imputeTS">(Steffen Moritz and Thomas Bartz-Beielstein 2017)</span> for Kalman imputation, <strong>changepoint</strong> <span class="citation" data-cites="changepoint2014">(Killick and Eckley 2014)</span> for PELT, <strong>strucchange</strong> <span class="citation" data-cites="strucchange2002">(Zeileis et al. 2002)</span> for Bai-Perron breakpoints, <strong>Rbeast</strong> <span class="citation" data-cites="Rbeast2019">(Zhao et al. 2019)</span> for Bayesian changepoint detection, and <strong>patchwork</strong> <span class="citation" data-cites="patchwork">(Pedersen 2025)</span> and <strong>scales</strong> <span class="citation" data-cites="scales">(Wickham, Pedersen, and Seidel 2025)</span> for figure composition. Package citations auto-generated with <strong>grateful</strong>.</p>
</section>
<section id="references" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="references">References</h2>
<div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-changepoint2014" class="csl-entry">
Killick, Rebecca, and Idris A. Eckley. 2014. <span>“<span class="nocase">changepoint</span>: An <span>R</span> Package for Changepoint Analysis.”</span> <em>Journal of Statistical Software</em> 58 (3): 1–19. <a href="https://www.jstatsoft.org/article/view/v058i03">https://www.jstatsoft.org/article/view/v058i03</a>.
</div>
<div id="ref-patchwork" class="csl-entry">
Pedersen, Thomas Lin. 2025. <em><span class="nocase">patchwork</span>: The Composer of Plots</em>. <a href="https://doi.org/10.32614/CRAN.package.patchwork">https://doi.org/10.32614/CRAN.package.patchwork</a>.
</div>
<div id="ref-gratia2024" class="csl-entry">
Simpson, Gavin L. 2024. <span>“<span class="nocase">gratia</span>: An <span>R</span> Package for Exploring Generalized Additive Models.”</span> <em>Journal of <span>O</span>pen <span>S</span>ource <span>S</span>oftware</em> 9 (104): 6962. <a href="https://doi.org/10.21105/joss.06962">https://doi.org/10.21105/joss.06962</a>.
</div>
<div id="ref-imputeTS" class="csl-entry">
Steffen Moritz, and Thomas Bartz-Beielstein. 2017. <span>“<span class="nocase">imputeTS: Time Series Missing Value Imputation in R</span>.”</span> <em><span>The R Journal</span></em> 9 (1): 207–18. <a href="https://doi.org/10.32614/RJ-2017-009">https://doi.org/10.32614/RJ-2017-009</a>.
</div>
<div id="ref-tidyverse" class="csl-entry">
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. <span>“Welcome to the <span class="nocase">tidyverse</span>.”</span> <em>Journal of Open Source Software</em> 4 (43): 1686. <a href="https://doi.org/10.21105/joss.01686">https://doi.org/10.21105/joss.01686</a>.
</div>
<div id="ref-scales" class="csl-entry">
Wickham, Hadley, Thomas Lin Pedersen, and Dana Seidel. 2025. <em><span class="nocase">scales</span>: Scale Functions for Visualization</em>. <a href="https://doi.org/10.32614/CRAN.package.scales">https://doi.org/10.32614/CRAN.package.scales</a>.
</div>
<div id="ref-mgcv2017" class="csl-entry">
Wood, S. N. 2017. <em>Generalized <span>A</span>dditive <span>M</span>odels: An Introduction with <span>R</span></em>. 2nd ed. Chapman; Hall/CRC.
</div>
<div id="ref-strucchange2003" class="csl-entry">
Zeileis, Achim, Christian Kleiber, Walter Krämer, and Kurt Hornik. 2003. <span>“Testing and Dating of Structural Changes in Practice.”</span> <em>Computational Statistics &amp; Data Analysis</em> 44 (1–2): 109–23. <a href="https://doi.org/10.1016/S0167-9473(03)00030-6">https://doi.org/10.1016/S0167-9473(03)00030-6</a>.
</div>
<div id="ref-strucchange2002" class="csl-entry">
Zeileis, Achim, Friedrich Leisch, Kurt Hornik, and Christian Kleiber. 2002. <span>“<span class="nocase">strucchange</span>: An r Package for Testing for Structural Change in Linear Regression Models.”</span> <em>Journal of Statistical Software</em> 7 (2): 1–38. <a href="https://doi.org/10.18637/jss.v007.i02">https://doi.org/10.18637/jss.v007.i02</a>.
</div>
<div id="ref-Rbeast2019" class="csl-entry">
Zhao, Kaiguang, Michael Wulder, Tongxi Hu, Ryan Bright, Qiusheng Wu, Haiming Qin, Yang Li, et al. 2019. <span>“Detecting Change-Point, Trend, and Seasonality in Satellite Time Series Data to Track Abrupt Changes and Nonlinear Dynamics: A Bayesian Ensemble Algorithm.”</span> <em>Remote Sensing of Environment</em> 232: 111181. <a href="https://doi.org/10.1016/j.rse.2019.04.034">https://doi.org/10.1016/j.rse.2019.04.034</a>.
</div>
</div>


</section>

 ]]></description>
  <category>R</category>
  <category>Changepoint</category>
  <category>BEAST</category>
  <category>GAM</category>
  <category>Biometrics</category>
  <category>Time Series</category>
  <guid>https://jbogomolovas2.github.io/Julius-s-Blog/posts/steps/</guid>
  <pubDate>Sun, 19 Apr 2026 07:00:00 GMT</pubDate>
  <media:content url="https://jbogomolovas2.github.io/Julius-s-Blog/posts/steps/steps.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>Three Years of Daily Weight: What the Patterns Actually Are</title>
  <dc:creator>Julius Bogomolovas</dc:creator>
  <link>https://jbogomolovas2.github.io/Julius-s-Blog/posts/weight_fluctations/</link>
  <description><![CDATA[ 





<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lubridate)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mgcv)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gratia)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lomb)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(performance)</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>))</span>
<span id="cb1-10"></span>
<span id="cb1-11">COL_RAW   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#adb5bd"</span></span>
<span id="cb1-12">COL_FIT   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2196F3"</span></span>
<span id="cb1-13">COL_RESID <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E91E63"</span></span>
<span id="cb1-14">COL_LOMB  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FF9800"</span></span></code></pre></div>
</details>
</div>
<section id="the-data" class="level2">
<h2 class="anchored" data-anchor-id="the-data">The data</h2>
<p>I’ve been weighing myself almost every morning with a Garmin scale since January 2022. Three years of data. The raw series looks noisy — but is it really just noise, or are there patterns at different timescales hiding inside it? So because of that “almost” I am dealing either irregularily spaced time series or time series with missing values, depending on how you look at it. This is a big no-no for standard time series analyses, that assume evenly spaced observations. Everything that follows uses methods that work directly on the irregular grid — no imputation, no gap-filling.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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;"># Load pre-processed data — generate weight_data.csv locally from</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the raw Garmin JSON using run_once.R (not committed to repo)</span></span>
<span id="cb2-3">weights <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;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"weight_data.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_col_types =</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;">|&gt;</span></span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb2-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(date <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;">min</span>(date)),</span>
<span id="cb2-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dow_num   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wday</span>(date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">week_start =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)),</span>
<span id="cb2-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">month_num =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">month</span>(date))</span>
<span id="cb2-8">  )</span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Full daily grid — only used for reference, not for modelling</span></span>
<span id="cb2-11">df_full <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(weights<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;">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="cb2-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(weights, <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb2-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(date <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;">min</span>(date)),</span>
<span id="cb2-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dow_num   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wday</span>(date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">week_start =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)),</span>
<span id="cb2-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">month_num =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">month</span>(date))</span>
<span id="cb2-17">  )</span>
<span id="cb2-18"></span>
<span id="cb2-19">n_obs     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(weights)</span>
<span id="cb2-20">n_missing <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(df_full<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>weight_kg))</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(weights, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(date, weight_kg)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_RAW, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</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.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"loess"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">span =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.12</span>,</span>
<span id="cb3-4">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_FIT, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</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.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb3-6">    <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;">"Three years of daily weight"</span>,</span>
<span id="cb3-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(</span>
<span id="cb3-8">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%d measurements, %d days missing (%.0f%%) — irregular time series"</span>,</span>
<span id="cb3-9">      n_obs, n_missing, <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> n_missing <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n_missing)</span>
<span id="cb3-10">    ),</span>
<span id="cb3-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Weight (kg)"</span></span>
<span id="cb3-12">  )</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/weight_fluctations/index_files/figure-html/plot-raw-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>There’s clearly a lot going on: a long-term rise and fall, noticeable short-term wiggling and things in between. The question is how to decompose these cleanly.</p>
</section>
<section id="hunting-for-periods-lomb-scargle" class="level2">
<h2 class="anchored" data-anchor-id="hunting-for-periods-lomb-scargle">Hunting for periods: Lomb-Scargle</h2>
<p>Before fitting any model, it helps to know which timescales contain signal. The standard tool for this on an irregular grid is the <strong>Lomb-Scargle periodogram</strong> (<a href="https://doi.org/10.1076/brhm.30.2.178.1422">Ruf, 1999</a>). Unlike the FFT, it doesn’t require evenly spaced data — it fits sinusoids directly to whatever observations exist.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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">ls_raw <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;">lsp</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>weight_kg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t,</span>
<span id="cb4-2">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"period"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ofac =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb4-3"></span>
<span id="cb4-4"><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;">period =</span> ls_raw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>scanned, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power =</span> ls_raw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(period, power)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-6">  <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;">colour =</span> COL_LOMB, <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.6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</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;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">113</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">182</span>),</span>
<span id="cb4-8">             <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;">"dotted"</span>, <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;">"grey50"</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.4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">113</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">182</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>,</span>
<span id="cb4-10">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</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;">"7d"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"70d"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"113d"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"182d"</span>),</span>
<span id="cb4-11">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <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;">3</span>, <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;">"grey40"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Lomb-Scargle periodogram — raw weight"</span>,</span>
<span id="cb4-14">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No imputation needed — works directly on irregular observations"</span>,</span>
<span id="cb4-15">       <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;">"Period (days, log scale)"</span>, <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;">"L-S Power"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/weight_fluctations/index_files/figure-html/lomb-raw-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>Four things stand out: a sharp spike at <strong>7 days</strong>, a broad messy humps between <strong>70–182 days</strong>, and the spectrum rising again at long periods reflecting the overall trend. These are candidates, not conclusions — the periodogram tells us where energy is, not what it means.</p>
</section>
<section id="three-progressive-gams" class="level2">
<h2 class="anchored" data-anchor-id="three-progressive-gams">Three progressive GAMs</h2>
<p>GAMs handle missing data gracefully — they fit on observed rows only, no interpolation needed (<a href="https://doi.org/10.1201/9781315370279">Wood, 2017</a>; <a href="https://doi.org/10.1111/j.1467-9868.2010.00749.x">Wood, 2011</a>). The strategy is to build the model up one layer at a time and watch what each layer explains.</p>
<p>The three models are:</p>
<ul>
<li><strong>M1</strong>: long-term trend only — <code>weight ~ s(t)</code></li>
<li><strong>M2</strong>: trend + weekday rhythm — <code>weight ~ s(t) + s(weekday)</code><br>
</li>
<li><strong>M3</strong>: trend + weekday + seasonal — <code>weight ~ s(t) + s(weekday) + s(month)</code></li>
</ul>
<p><code>s(weekday)</code> and <code>s(month)</code> use cyclic cubic splines (<code>bs="cc"</code>) so that Sunday wraps back to Monday, and December wraps back to January. The <code>knots</code> argument sets the period correctly — boundary knots at 0.5/7.5 and 0.5/12.5 rather than at the data edges, which would collapse the endpoints.</p>
<p>Model comparison uses the <code>performance</code> package (<a href="https://doi.org/10.21105/joss.03139">Lüdecke et al., 2021</a>), and smooth visualisation uses <code>gratia</code> (<a href="https://doi.org/10.21105/joss.06962">Simpson, 2024</a>).</p>
<p>The trend smooth uses <code>k=10</code> — enough flexibility to track genuine multi-year drift, but stiff enough not to absorb the within-year seasonal pattern that <code>s(month)</code> should own.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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">knots_dow   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dow_num   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.5</span>))</span>
<span id="cb5-2">knots_month <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">month_num =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12.5</span>))</span>
<span id="cb5-3"></span>
<span id="cb5-4">m1 <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;">gam</span>(weight_kg <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;">s</span>(t, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>),</span>
<span id="cb5-5">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> weights, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"REML"</span>)</span>
<span id="cb5-6"></span>
<span id="cb5-7">m2 <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;">gam</span>(weight_kg <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;">s</span>(t, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <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;">s</span>(dow_num, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cc"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>),</span>
<span id="cb5-9">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> weights, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">knots =</span> knots_dow, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"REML"</span>)</span>
<span id="cb5-10"></span>
<span id="cb5-11">m3 <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;">gam</span>(weight_kg <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;">s</span>(t, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-12">                       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(dow_num,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cc"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-13">                       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(month_num, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cc"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>),</span>
<span id="cb5-14">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> weights, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">knots =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(knots_dow, knots_month), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"REML"</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compare_performance</span>(m1, m2, m3) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Name, AIC, AICc, BIC, R2, RMSE) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-4">  knitr<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;">kable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb6-5">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Model comparison — each added layer earns its place"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Model comparison — each added layer earns its place</caption>
<thead>
<tr class="header">
<th style="text-align: left;">Name</th>
<th style="text-align: right;">AIC</th>
<th style="text-align: right;">AICc</th>
<th style="text-align: right;">BIC</th>
<th style="text-align: right;">R2</th>
<th style="text-align: right;">RMSE</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">m1</td>
<td style="text-align: right;">3081.0</td>
<td style="text-align: right;">3081.2</td>
<td style="text-align: right;">3135.4</td>
<td style="text-align: right;">0.683</td>
<td style="text-align: right;">1.017</td>
</tr>
<tr class="even">
<td style="text-align: left;">m2</td>
<td style="text-align: right;">2999.5</td>
<td style="text-align: right;">2999.9</td>
<td style="text-align: right;">3070.9</td>
<td style="text-align: right;">0.707</td>
<td style="text-align: right;">0.976</td>
</tr>
<tr class="odd">
<td style="text-align: left;">m3</td>
<td style="text-align: right;">2745.0</td>
<td style="text-align: right;">2746.0</td>
<td style="text-align: right;">2857.5</td>
<td style="text-align: right;">0.771</td>
<td style="text-align: right;">0.859</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Each layer earns its place. AIC drops ~90 points adding weekday, then another ~100 adding seasonal. BIC agrees — these terms aren’t overfitting, they’re explaining real structure. The final RMSE of ~0.85 kg is the irreducible day-to-day scatter: meal timing, hydration, measurement noise.</p>
<p>One modelling decision is worth unpacking. The trend smooth <code>s(t)</code> has a tuning parameter <code>k</code> that controls how flexible it is. Crank it up and the trend can wiggle at multi-month timescales — at which point it starts absorbing the seasonal pattern that <code>s(month)</code> is supposed to own. The two smooths compete for the same variance, and the flexible trend wins. Drop <code>k</code> back down and the seasonal smooth recovers, but you’ve lost some fit.</p>
<p>This tension is familiar in classical time series decomposition, but it usually resolves cleanly. In the famous textbook example airline passenger dataset seasons are sharp, regular, and large relative to the trend. There’s no ambiguity about what belongs where. Weight data is messier. The “seasonal” pattern here is real but modest (~3 kg peak-to-trough), and the trend itself drifts and curves in ways that overlap with multi-month timescales. In ecology you face the same problem: a population that crashes every winter looks periodic, but a population recovering from a disturbance looks like trend, and if both happen simultaneously the decomposition is a judgement call, not a fact.</p>
<p>The choice I made — <code>k=10</code>, roughly one degree of freedom per four months — keeps the trend smooth enough that <code>s(month)</code> gets to explain the repeating annual structure, while still tracking genuine multi-year drift. It’s a modelling choice, not a discovery. The data can’t tell you where the trend ends and the cycle begins.</p>
</section>
<section id="what-the-model-found" class="level2">
<h2 class="anchored" data-anchor-id="what-the-model-found">What the model found</h2>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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;"># Trend with CI</span></span>
<span id="cb7-2">sm_trend <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;">smooth_estimates</span>(</span>
<span id="cb7-3">  m3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">select =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s(t)"</span>,</span>
<span id="cb7-4">  <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t), <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;">500</span>),</span>
<span id="cb7-5">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dow_num   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dow_num),</span>
<span id="cb7-6">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">month_num =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>month_num))</span>
<span id="cb7-7">) <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;">add_confint</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb7-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.estimate =</span> .estimate <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;">coef</span>(m3)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb7-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.lower_ci =</span> .lower_ci <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;">coef</span>(m3)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb7-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.upper_ci =</span> .upper_ci <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;">coef</span>(m3)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb7-12">    <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;">min</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> t</span>
<span id="cb7-13">  )</span>
<span id="cb7-14"></span>
<span id="cb7-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Weekday smooth</span></span>
<span id="cb7-16">sm_dow <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;">smooth_estimates</span>(</span>
<span id="cb7-17">  m3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">select =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s(dow_num)"</span>,</span>
<span id="cb7-18">  <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dow_num   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>),</span>
<span id="cb7-19">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">month_num =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>month_num),</span>
<span id="cb7-20">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t))</span>
<span id="cb7-21">) <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;">add_confint</span>()</span>
<span id="cb7-22"></span>
<span id="cb7-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Seasonal smooth</span></span>
<span id="cb7-24">sm_month <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;">smooth_estimates</span>(</span>
<span id="cb7-25">  m3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">select =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s(month_num)"</span>,</span>
<span id="cb7-26">  <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">month_num =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>),</span>
<span id="cb7-27">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dow_num   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dow_num),</span>
<span id="cb7-28">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t))</span>
<span id="cb7-29">) <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;">add_confint</span>()</span>
<span id="cb7-30"></span>
<span id="cb7-31">p_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> weights, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(date, weight_kg),</span>
<span id="cb7-33">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_RAW, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</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.4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_ribbon</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sm_trend, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> .lower_ci, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> .upper_ci),</span>
<span id="cb7-35">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> COL_FIT, <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.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-36">  <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;">data =</span> sm_trend, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(date, .estimate),</span>
<span id="cb7-37">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> COL_FIT, <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.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Long-term trend"</span>,</span>
<span id="cb7-39">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s(t) only — weekday and seasonal effects removed"</span>,</span>
<span id="cb7-40">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Weight (kg)"</span>)</span>
<span id="cb7-41"></span>
<span id="cb7-42">p_dow <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;">ggplot</span>(sm_dow, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(dow_num, .estimate)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_ribbon</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;">ymin =</span> .lower_ci, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> .upper_ci), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> COL_FIT, <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.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-44">  <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;">colour =</span> COL_FIT, <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="cb7-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</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.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</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;">7</span>,</span>
<span id="cb7-47">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</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;">"Mon"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tue"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wed"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Thu"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fri"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sat"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sun"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-48">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Weekday effect"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Effect (kg)"</span>)</span>
<span id="cb7-49"></span>
<span id="cb7-50">p_month <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;">ggplot</span>(sm_month, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(month_num, .estimate)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-51">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_ribbon</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;">ymin =</span> .lower_ci, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> .upper_ci), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> COL_RESID, <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.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-52">  <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;">colour =</span> COL_RESID, <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="cb7-53">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</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.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-54">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</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;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> month.abb) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-55">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Seasonal effect"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <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;">"Effect (kg)"</span>)</span>
<span id="cb7-56"></span>
<span id="cb7-57">p_fit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (p_dow <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p_month)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/weight_fluctations/index_files/figure-html/plot-components-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>Three things jump out:</p>
<p><strong>The long-term trend</strong> rose about 5 kg from early 2022 to mid-2024, then came back down. Not a linear drift — more like a slow wave.</p>
<p><strong>The weekly pattern</strong> is ±0.35 kg and the shape is clean: heaviest on Monday, declining through the week, lightest around Thursday, with a small rebound heading into the weekend. It maps almost perfectly onto the Lomb-Scargle 7-day spike — which turns out to be nothing more exotic than weekday eating habits. This pattern isn’t unique to me: <a href="https://doi.org/10.1159/000356147">Orsama et al.&nbsp;(2014)</a> found the same Monday peak and Friday trough in 80 adults across four studies, and <a href="https://doi.org/10.1371/journal.pone.0232152">Turicchi et al.&nbsp;(2020)</a> replicated it in over 1,400 participants across three European countries. The weekly rhythm in body weight appears to be a near-universal feature of modern life, not a biological clock.</p>
<p><strong>The seasonal pattern</strong> is a ~3 kg swing from January (heaviest) to August–September (lightest), recovering towards December. <a href="https://doi.org/10.1371/journal.pone.0232152">Turicchi et al.&nbsp;(2020)</a> observed similar seasonal fluctuations in a large European cohort, with patterns largely driven by holiday-period weight gain and summer reduction.</p>
</section>
<section id="what-the-model-didnt-explain-the-lomb-scargle-cascade" class="level2">
<h2 class="anchored" data-anchor-id="what-the-model-didnt-explain-the-lomb-scargle-cascade">What the model didn’t explain: the Lomb-Scargle cascade</h2>
<p>Now for the most interesting part. If the GAM captured the real periodic structure, its residuals should be white noise. Let’s check by running Lomb-Scargle on the residuals of each model — the same tool we started with, now used to audit what’s left over.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<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">weights <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> weights <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb8-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resid_m1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>(m1),</span>
<span id="cb8-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resid_m2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>(m2),</span>
<span id="cb8-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resid_m3 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>(m3)</span>
<span id="cb8-6">  )</span>
<span id="cb8-7"></span>
<span id="cb8-8">ls_df <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, t, label) {</span>
<span id="cb8-9">  ls <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;">lsp</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"period"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ofac =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb8-10">  <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;">period =</span> ls<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>scanned, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power =</span> ls<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> label)</span>
<span id="cb8-11">}</span>
<span id="cb8-12"></span>
<span id="cb8-13">ls_all <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;">bind_rows</span>(</span>
<span id="cb8-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls_df</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>weight_kg, weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Raw data"</span>),</span>
<span id="cb8-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls_df</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>resid_m1,  weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M1: trend removed"</span>),</span>
<span id="cb8-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls_df</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>resid_m2,  weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M2: + weekday"</span>),</span>
<span id="cb8-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls_df</span>(weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>resid_m3,  weights<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M3: + weekday + seasonal"</span>)</span>
<span id="cb8-18">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-19">  <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;">model =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"Raw data"</span>,</span>
<span id="cb8-20">                                          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M1: trend removed"</span>,</span>
<span id="cb8-21">                                          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M2: + weekday"</span>,</span>
<span id="cb8-22">                                          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M3: + weekday + seasonal"</span>)))</span>
<span id="cb8-23"></span>
<span id="cb8-24">model_colours <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;">c</span>(</span>
<span id="cb8-25">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Raw data"</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;">"#2c3e50"</span>,</span>
<span id="cb8-26">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M1: trend removed"</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;">"#e67e22"</span>,</span>
<span id="cb8-27">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M2: + weekday"</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;">"#e74c3c"</span>,</span>
<span id="cb8-28">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M3: + weekday + seasonal"</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;">"#9b59b6"</span></span>
<span id="cb8-29">)</span>
<span id="cb8-30"></span>
<span id="cb8-31">ref_lines <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>
<span id="cb8-32">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">period =</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;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">113</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">182</span>),</span>
<span id="cb8-33">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label  =</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;">"7d"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"70d"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"113d"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"182d"</span>)</span>
<span id="cb8-34">)</span>
<span id="cb8-35"></span>
<span id="cb8-36"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(ls_all, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(period, power, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> model)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-37">  <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.6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ref_lines, <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> period),</span>
<span id="cb8-39">             <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;">"dotted"</span>, <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;">"grey60"</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.4</span>,</span>
<span id="cb8-40">             <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ref_lines, <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> period, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> label),</span>
<span id="cb8-42">            <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>,</span>
<span id="cb8-43">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.8</span>, <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;">"grey40"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-45">  <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> model_colours) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-46">  <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>model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</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;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fixed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Lomb-Scargle cascade — what each model layer removes"</span>,</span>
<span id="cb8-48">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Shared Y axis: 7d spike gone after M2, but 70–182d band persists throughout"</span>,</span>
<span id="cb8-49">       <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;">"Period (days, log scale)"</span>, <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;">"L-S Power"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-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;">"none"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/weight_fluctations/index_files/figure-html/lomb-cascade-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>So few things pop up after comparing original periodogram to our model reisduals. Firstly our weekly smooth removes 7 day peak. Bazinga! Now the yearly smooth affects low frequency region. So definintely there is something there and yearly smooth at least partly explains it. But with 3.2 years of data I have very few long-period cycles, and the periodogram becomes unreliable at these frequencies. Whats stands out are 70 and 113 day peaks that remain largely “unseen” by mmodels. Wiht ~16 periods in dataset thats real signal. It will be interesting to pin them down!</p>
<p>A few directions from here:</p>
<ul>
<li><strong>Find the phase anchor for the 70 and 113d peaks</strong> The period is known; the question is what external variable marks the start of each cycle.</li>
<li><strong>Low-frequency band (&gt;150d): treat with caution.</strong> Peaks here are broad, the data is short, and the seasonal smooth interacts with this region in ways that are hard to disentangle. Wavelet analysis would show whether the energy is episodic or continuous — and whether the apparent period drifts over time.</li>
<li><strong>Time-varying seasonal amplitude.</strong> <code>s(month)</code> assumes the annual pattern has the same shape every year. If the winter weight gain was larger in one year than another, that variation ends up in the residuals. A time-varying extension would let the seasonal amplitude float.</li>
</ul>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Component</th>
<th>Amplitude</th>
<th>Character</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Weekly rhythm</td>
<td>±0.35 kg</td>
<td>Explained — weekday eating</td>
</tr>
<tr class="even">
<td>Seasonal drift</td>
<td>~3 kg Jan→Sep</td>
<td>Explained — average annual cycle</td>
</tr>
<tr class="odd">
<td>Long-term trend</td>
<td>~5 kg over 3 yrs</td>
<td>Explained — slow drift</td>
</tr>
<tr class="even">
<td>70 and 113d peaks</td>
<td>~0.3–0.5 kg</td>
<td>Unexplained — period known, phase missing</td>
</tr>
<tr class="odd">
<td>&gt;150d band</td>
<td>unclear</td>
<td>Unreliable at this data length</td>
</tr>
</tbody>
</table>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<p><strong>Data papers</strong></p>
<ul>
<li>Orsama et al.&nbsp;(2014). Weight rhythms: Weight increases during weekends and decreases during weekdays. <em>Obesity Facts</em>, 7, 36–47. <a href="https://doi.org/10.1159/000356147" class="uri">https://doi.org/10.1159/000356147</a></li>
<li>Turicchi et al.&nbsp;(2020). Weekly, seasonal and holiday body weight fluctuation patterns. <em>PLOS ONE</em>, 15(4), e0232152. <a href="https://doi.org/10.1371/journal.pone.0232152" class="uri">https://doi.org/10.1371/journal.pone.0232152</a></li>
</ul>
<p><strong>R packages</strong></p>
<ul>
<li>Wood, S.N. (2017). <em>Generalized Additive Models: An Introduction with R</em> (2nd ed.). Chapman and Hall/CRC.</li>
<li>Wood, S.N. (2011). Fast stable REML and ML estimation of semiparametric GLMs. <em>JRSS-B</em>, 73(1), 3–36.</li>
<li>Simpson, G.L. (2024). gratia: An R package for exploring GAMs. <em>JOSS</em>, 9(104), 6962. <a href="https://doi.org/10.21105/joss.06962" class="uri">https://doi.org/10.21105/joss.06962</a></li>
<li>Ruf, T. (1999). The Lomb-Scargle periodogram in biological rhythm research. <em>Biological Rhythm Research</em>, 30, 178–201.</li>
<li>Wickham et al.&nbsp;(2019). Welcome to the tidyverse. <em>JOSS</em>, 4(43), 1686. <a href="https://doi.org/10.21105/joss.01686" class="uri">https://doi.org/10.21105/joss.01686</a></li>
<li>Lüdecke et al.&nbsp;(2021). performance: An R package for assessment of statistical models. <em>JOSS</em>, 6(60), 3139. <a href="https://doi.org/10.21105/joss.03139" class="uri">https://doi.org/10.21105/joss.03139</a></li>
<li>Pedersen, T.L. (2025). patchwork: The composer of plots. <a href="https://doi.org/10.32614/CRAN.package.patchwork" class="uri">https://doi.org/10.32614/CRAN.package.patchwork</a></li>
</ul>


</section>

 ]]></description>
  <category>R</category>
  <category>GAM</category>
  <category>Biometrics</category>
  <category>Time Series</category>
  <guid>https://jbogomolovas2.github.io/Julius-s-Blog/posts/weight_fluctations/</guid>
  <pubDate>Sun, 12 Apr 2026 07:00:00 GMT</pubDate>
  <media:content url="https://jbogomolovas2.github.io/Julius-s-Blog/posts/weight_fluctations/food.png" medium="image" type="image/png" height="96" width="144"/>
</item>
<item>
  <title>What’s Your Base? Estimating Swimming Fitness from Routine Training Data</title>
  <dc:creator>Julius Bogomolovas</dc:creator>
  <link>https://jbogomolovas2.github.io/Julius-s-Blog/posts/swim-css-pipeline/</link>
  <description><![CDATA[ 





<section id="the-question-every-swimmer-gets-asked" class="level2">
<h2 class="anchored" data-anchor-id="the-question-every-swimmer-gets-asked">The Question Every Swimmer Gets Asked</h2>
<p>I swim a lot, and like most swimmers, I am a little obsessive about tracking progress.</p>
<p>Garmin is great in many ways, but when it comes to swimming, the readouts are a bit underwhelming. You get average pace, effort score, SWOLF, and a few other summary numbers, but none of them really answer the question swimmers actually care about: how fast am I, really, right now?</p>
<p>There are formal ways to get at that. You can do time trials, swim a CSS test, or race. But those are just occasional snapshots. What I wanted was something more automatic: some kind of session-by-session measure that tells me how fast, or how fit, I was swimming in an ordinary workout.</p>
<p>In swimming, that idea is usually captured by base pace. If you join a masters team, one of the first questions people ask is: what’s your base? Roughly speaking, it is the pace you can hold steadily for repeat 100s on short rest. Not easy, not all-out, just your solid training pace. It is close to CSS, usually a bit slower, and it is one of the most practical ways swimmers think about fitness.</p>
<p>So I started wondering: could I estimate something like that automatically from routine workout data?</p>
<p>That is harder than it sounds, because swim workouts are messy on purpose. Within a single session you swim at different efforts, different distances, with different rest, and at different stages of fatigue. Sometimes you are sprinting, sometimes cruising, sometimes just surviving the set. So the average pace Garmin gives you is not really a fitness metric. It is heavily shaped by the structure of the workout itself.</p>
<p>That was really the whole motivation for this project.</p>
<p>In a <a href="../post-with-code/index.html">previous post</a>, I showed that GAMs can do a pretty good job modeling swim speed from workout structure and pulling out broad trends over time. That was a useful first step, but it still had a big limitation: it could not really separate swimming slow because I was tired from swimming slow because I was intentionally taking it easy. Effort and fitness were still tangled together.</p>
<p>This post is my attempt to go one step further. The idea is to use GAMs to tease apart lap speed by accounting for the main things that distort it — repeat length, position within a rep, accumulated fatigue, rest, stroke rate, heart rate, and so on — and then smooth the cleaned-up signal over time to get an estimate of base pace.</p>
<p>And because any homemade metric needs some kind of reality check, I have a couple of built-in positive controls. During this period I was training consistently toward the La Jolla Rough Water Swim relay on September 27, 2025, so if this metric is capturing something real, I should be getting faster into that race. I also had a stretch in spring 2025 when I was training with a swim buddy, which reliably made me swim harder and better than usual. So, at the very least, I would hope this thing can detect those signals and not just produce a fancy-looking curve.</p>
<p>In other words, the goal here is pretty simple: take messy routine swim data and turn it into a number swimmers already understand intuitively — a proxy for how fast, or how fit, I really was in a given session.</p>
</section>
<section id="the-pipeline-at-a-glance" class="level2">
<h2 class="anchored" data-anchor-id="the-pipeline-at-a-glance">The Pipeline at a Glance</h2>
<p>The pipeline has two stages, each built around a generalized additive model:</p>
<ol type="1">
<li><strong>Structural GAM</strong> — reduces the influence of within-session mechanics (pacing, fatigue, rest, pool length) and effort (stroke rate, heart rate) on raw speed</li>
<li><strong>Density surface GAM</strong> — smooths the adjusted pace distribution over calendar time, with peak extraction via parabolic interpolation</li>
</ol>
<p>The output is a continuous fitness curve expressed in pace per 100 yards — the unit swimmers think in.</p>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<section id="fit-file-extraction" class="level3">
<h3 class="anchored" data-anchor-id="fit-file-extraction">FIT File Extraction</h3>
<p>All data comes from Garmin FIT files recorded during pool swimming sessions, read via the <code>FITfileR</code> package <span class="citation" data-cites="FITfileR">(Smith 2025)</span>. The extraction pulls three message types per file: session-level metadata (date, pool length), per-length timing and stroke data, and per-lap heart rate. The full extraction runs in parallel across 7 cores and takes a few minutes.</p>
<div class="cell">
<details class="code-fold">
<summary>Block 1: FIT file extraction (not evaluated — requires local FIT files)</summary>
<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>(FITfileR)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(future.apply)</span>
<span id="cb1-5"></span>
<span id="cb1-6">fit_dir    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"path/to/your/FIT/files"</span></span>
<span id="cb1-7">cache_file <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"swim_data.rds"</span></span>
<span id="cb1-8">n_workers  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> parallelly<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;">availableCores</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">pool_fits <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.files</span>(fit_dir, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pattern =</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;">.fit$"</span>,</span>
<span id="cb1-11">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">recursive =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-12"></span>
<span id="cb1-13">extract_fit <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>(f) {</span>
<span id="cb1-14">  fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tryCatch</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readFitFile</span>(f), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb1-15">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.null</span>(fit)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb1-16">  </span>
<span id="cb1-17">  out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">session =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lengths =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lap_info =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb1-18">  </span>
<span id="cb1-19">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Session metadata</span></span>
<span id="cb1-20">  sess <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;">tryCatch</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getMessagesByType</span>(fit, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"session"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb1-21">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.null</span>(sess) <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;">nrow</span>(sess) <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pool_length"</span> <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;">names</span>(sess)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb1-22">  </span>
<span id="cb1-23">  pl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sess<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pool_length[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb1-24">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(pl) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> pl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb1-25">  </span>
<span id="cb1-26">  out<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>session <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-27">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file         =</span> f,</span>
<span id="cb1-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">session_date =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(sess<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>start_time[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]),</span>
<span id="cb1-29">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pool_length  =</span> pl,</span>
<span id="cb1-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_hr_session =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"avg_heart_rate"</span> <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;">names</span>(sess)) sess<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>avg_heart_rate[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_integer_</span>,</span>
<span id="cb1-31">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_hr_session =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max_heart_rate"</span> <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;">names</span>(sess)) sess<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>max_heart_rate[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_integer_</span>,</span>
<span id="cb1-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb1-33">  )</span>
<span id="cb1-34">  </span>
<span id="cb1-35">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Per-length data</span></span>
<span id="cb1-36">  lens <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;">tryCatch</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getMessagesByType</span>(fit, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"length"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb1-37">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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.null</span>(lens) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(lens) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb1-38">    lens<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>file <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> f</span>
<span id="cb1-39">    lens<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>length_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;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(lens)) <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>
<span id="cb1-40">    out<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lengths <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lens</span>
<span id="cb1-41">  }</span>
<span id="cb1-42">  </span>
<span id="cb1-43">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Lap-level HR (not available per-length in Garmin)</span></span>
<span id="cb1-44">  laps <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;">tryCatch</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getMessagesByType</span>(fit, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lap"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb1-45">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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.null</span>(laps) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(laps) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</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;">&amp;&amp;</span></span>
<span id="cb1-46">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first_length_index"</span> <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;">names</span>(laps) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span></span>
<span id="cb1-47">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"num_lengths"</span> <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;">names</span>(laps)) {</span>
<span id="cb1-48">    </span>
<span id="cb1-49">    real_laps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> laps <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-50">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(first_length_index <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65535</span>, num_lengths <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb1-51">    </span>
<span id="cb1-52">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(real_laps) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb1-53">      rows <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;">vector</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"list"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(real_laps))</span>
<span id="cb1-54">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="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>(real_laps))) {</span>
<span id="cb1-55">        rl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> real_laps[i, ]</span>
<span id="cb1-56">        fi <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>(rl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>first_length_index)</span>
<span id="cb1-57">        nl <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>(rl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>num_lengths)</span>
<span id="cb1-58">        rows[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-59">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file         =</span> f,</span>
<span id="cb1-60">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length_index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(fi, fi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> nl <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>
<span id="cb1-61">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_hr =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"avg_heart_rate"</span> <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;">names</span>(rl)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(rl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>avg_heart_rate) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb1-62">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_hr =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max_heart_rate"</span> <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;">names</span>(rl)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(rl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>max_heart_rate) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>,</span>
<span id="cb1-63">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb1-64">        )</span>
<span id="cb1-65">      }</span>
<span id="cb1-66">      out<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lap_info <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;">bind_rows</span>(rows)</span>
<span id="cb1-67">    }</span>
<span id="cb1-68">  }</span>
<span id="cb1-69">  </span>
<span id="cb1-70">  out</span>
<span id="cb1-71">}</span>
<span id="cb1-72"></span>
<span id="cb1-73"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plan</span>(multisession, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">workers =</span> n_workers)</span>
<span id="cb1-74">results <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;">future_lapply</span>(pool_fits, extract_fit,</span>
<span id="cb1-75">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">future.seed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb1-76">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">future.packages =</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;">"FITfileR"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dplyr"</span>))</span>
<span id="cb1-77"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plan</span>(sequential)</span>
<span id="cb1-78"></span>
<span id="cb1-79">sessions_raw <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;">bind_rows</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(results, <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;">[[</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;">"session"</span>))</span>
<span id="cb1-80">lengths_raw  <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;">bind_rows</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(results, <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;">[[</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;">"lengths"</span>))</span>
<span id="cb1-81">lap_info     <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;">bind_rows</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(results, <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;">[[</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;">"lap_info"</span>))</span>
<span id="cb1-82"></span>
<span id="cb1-83"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</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;">sessions_raw =</span> sessions_raw,</span>
<span id="cb1-84">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lengths_raw  =</span> lengths_raw,</span>
<span id="cb1-85">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lap_info     =</span> lap_info), cache_file)</span></code></pre></div>
</details>
</div>
<p>The cached extraction (<code>swim_data.rds</code>, 381 KB) is available in this post’s directory for reproducibility. Everything below runs from this file.</p>
<div class="cell">
<details class="code-fold">
<summary>Load cached data and build predictor table</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mgcv)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gratia)</span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>))</span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load cached extraction</span></span>
<span id="cb2-11">cached <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"swim_data.rds"</span>)</span>
<span id="cb2-12">sessions_raw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cached<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sessions_raw</span>
<span id="cb2-13">lengths_raw  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cached<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lengths_raw</span>
<span id="cb2-14">lap_info     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cached<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lap_info</span>
<span id="cb2-15"></span>
<span id="cb2-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Join lengths with lap-level HR</span></span>
<span id="cb2-17">lengths_joined <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lengths_raw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(lap_info, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</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;">"file"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"length_index"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(sessions_raw, <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;">"file"</span>)</span></code></pre></div>
</details>
</div>
</section>
<section id="building-the-predictor-table" class="level3">
<h3 class="anchored" data-anchor-id="building-the-predictor-table">Building the Predictor Table</h3>
<p>Each active, non-drill length becomes one row with the following predictors:</p>
<div class="cell">
<details class="code-fold">
<summary>Block 3: Compute predictors — one row per active length</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rep detection: consecutive active lengths between idle rows form one rep</span></span>
<span id="cb3-2">laps_all <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lengths_joined <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(file) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(file, start_time) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb3-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row_id =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row_number</span>(),</span>
<span id="cb3-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rep_id =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lag</span>(length_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">default =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"active"</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;">"idle"</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="cb3-8">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb3-10"></span>
<span id="cb3-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rest durations from idle rows</span></span>
<span id="cb3-12">rest_durations <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> laps_all <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(length_type <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;">"idle"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(file) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-15">  <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;">next_rep_id =</span> rep_id <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="cb3-16">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rest_seconds =</span> total_elapsed_time) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(file, next_rep_id, rest_seconds)</span>
<span id="cb3-19"></span>
<span id="cb3-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Build the full predictor table</span></span>
<span id="cb3-21">laps_all <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> laps_all <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(length_type <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;">"active"</span>,</span>
<span id="cb3-23">         <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>(swim_stroke), swim_stroke <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>
<span id="cb3-24">         swim_stroke <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;">"drill"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(file, rep_id) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-26">  <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;">lap_in_rep =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row_number</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rep_size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(rest_durations, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</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;">"file"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rep_id"</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;">"next_rep_id"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(file) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(file, row_id) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb3-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">speed          =</span> pool_length <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> total_elapsed_time,</span>
<span id="cb3-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cum_distance   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(pool_length),</span>
<span id="cb3-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pos_mid        =</span> (lap_in_rep <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> rep_size,</span>
<span id="cb3-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rep_length     =</span> rep_size <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pool_length,</span>
<span id="cb3-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log_rep_length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(rep_length),</span>
<span id="cb3-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rest_before    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(lap_in_rep <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&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>(rest_seconds),</span>
<span id="cb3-38">                             rest_seconds, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb3-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rest_before    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(lap_in_rep <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> rep_id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, rest_before),</span>
<span id="cb3-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log1p_rest     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1p</span>(rest_before),</span>
<span id="cb3-41">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stroke_rate    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(total_strokes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</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;">&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>(total_strokes),</span>
<span id="cb3-42">                             total_strokes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> total_elapsed_time, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>),</span>
<span id="cb3-43">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dps            =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</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>(total_strokes) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> total_strokes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb3-44">                             pool_length <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> total_strokes, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>)</span>
<span id="cb3-45">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb3-48">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">session_id   =</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;">paste</span>(session_date, file, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"|"</span>)),</span>
<span id="cb3-49">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">session_date =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(session_date),</span>
<span id="cb3-50">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">swim_stroke  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(swim_stroke)</span>
<span id="cb3-51">  )</span>
<span id="cb3-52"></span>
<span id="cb3-53"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Quality filter</span></span>
<span id="cb3-54">laps_clean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> laps_all <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;">filter</span>(speed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, speed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>Dataset: 14933 lengths | 203 sessions | 2024-08-16 to 2026-03-26 </code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Lengths per stroke:</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 2
  swim_stroke      n
  &lt;fct&gt;        &lt;int&gt;
1 freestyle    11407
2 breaststroke  1398
3 backstroke    1242
4 butterfly      886</code></pre>
</div>
</div>
</section>
<section id="predictor-definitions" class="level3">
<h3 class="anchored" data-anchor-id="predictor-definitions">Predictor Definitions</h3>
<p>The response variable is <strong>speed</strong> (m/s) = pool length / elapsed time, which is pool-length-independent by construction.</p>
<p>The key predictors fall into two groups:</p>
<p><strong>Structural predictors</strong> describe where each length sits within the workout:</p>
<ul>
<li><strong><code>pos_mid</code></strong> — normalized position within the interval, <img src="https://latex.codecogs.com/png.latex?(i%20-%200.5)%20/%20n">. A value near 0 is the first length, near 1 is the last.</li>
<li><strong><code>log_rep_length</code></strong> — log of interval distance in meters. Log scale because the difference between a 50 m and 100 m is larger than between 250 m and 300 m.</li>
<li><strong><code>cum_distance</code></strong> — running total of meters swum in the session so far.</li>
<li><strong><code>log1p_rest</code></strong> — log(1 + rest seconds) before each interval. First lengths after a wall start get the preceding idle duration; mid-interval lengths get 0 (lengths that continue through the wall without stopping).</li>
<li><strong><code>pool_length</code></strong> — pool length in meters (22.86 m for a 25-yard pool, 50 m for long course, etc.).</li>
<li><strong><code>set_type</code></strong> — freestyle fraction of the interval, grouped into three levels: pure freestyle, mixed, and IM set. A freestyle length inside an IM set may behave differently from one in a pure freestyle set.</li>
</ul>
<p><strong>Effort predictors</strong> describe how hard the swimmer is working:</p>
<ul>
<li><strong><code>stroke_rate</code></strong> — strokes per second. In my data, distance per stroke is nearly flat overall (<img src="https://latex.codecogs.com/png.latex?r%20=%200.01">), so most of the speed variation appears to be driven by stroke rate.</li>
<li><strong><code>avg_hr</code></strong> — average heart rate for the interval. The watch tracks HR continuously, but pool swimming FIT files only report it aggregated per interval. For a continuous 100 (4 lengths), all four lengths share the same HR value. This means HR distinguishes effort between intervals, while stroke rate captures effort variation within them. Correlation between the two is only 0.24, confirming independent information. Available for 81% of lengths; sessions without HR data are excluded.</li>
</ul>
</section>
</section>
<section id="filtering-for-freestyle-with-hr" class="level2">
<h2 class="anchored" data-anchor-id="filtering-for-freestyle-with-hr">Filtering for Freestyle with HR</h2>
<div class="cell">
<details class="code-fold">
<summary>Block 4: Filter funnel — freestyle with HR</summary>
<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">n0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(laps_clean)</span>
<span id="cb7-2">s0 <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;">n_distinct</span>(laps_clean<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>session_id)</span>
<span id="cb7-3"></span>
<span id="cb7-4">d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> laps_clean <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;">filter</span>(swim_stroke <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;">"freestyle"</span>)</span>
<span id="cb7-5">n1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(d); s1 <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;">n_distinct</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>session_id)</span>
<span id="cb7-6"></span>
<span id="cb7-7">d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d <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;">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>(stroke_rate))</span>
<span id="cb7-8">n2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(d); s2 <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;">n_distinct</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>session_id)</span>
<span id="cb7-9"></span>
<span id="cb7-10">d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d <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;">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>(avg_hr), avg_hr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">220</span>)</span>
<span id="cb7-11">n3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(d); s3 <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;">n_distinct</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>session_id)</span>
<span id="cb7-12"></span>
<span id="cb7-13">d_free_hr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d <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;">session_id =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(session_id))</span>
<span id="cb7-14"></span>
<span id="cb7-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute freestyle fraction per interval and classify</span></span>
<span id="cb7-16">rep_composition <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> laps_clean <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(file, rep_id) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb7-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rep_n_total  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(),</span>
<span id="cb7-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rep_n_free   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(swim_stroke <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;">"freestyle"</span>),</span>
<span id="cb7-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rep_pct_free =</span> rep_n_free <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> rep_n_total,</span>
<span id="cb7-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span></span>
<span id="cb7-23">  )</span>
<span id="cb7-24"></span>
<span id="cb7-25">d_free_hr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d_free_hr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(rep_composition, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</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;">"file"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rep_id"</span>))</span>
<span id="cb7-27"></span>
<span id="cb7-28">d_free_hr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>set_type <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;">cut</span>(</span>
<span id="cb7-29">  d_free_hr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rep_pct_free,</span>
<span id="cb7-30">  <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>),</span>
<span id="cb7-31">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</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;">"IM_set"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mixed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pure_free"</span>),</span>
<span id="cb7-32">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include.lowest =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb7-33">)</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>Filter funnel:</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>  All strokes:        14933 laps | 203 sessions</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>  Freestyle only:     11407 laps | 202 sessions</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>  + stroke_rate:      11407 laps | 202 sessions</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>  + avg_hr (&lt; 220):   11396 laps | 202 sessions</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>  Retention:          76.3 % of all laps</code></pre>
</div>
</div>
</section>
<section id="stage-1-the-structural-gam" class="level2">
<h2 class="anchored" data-anchor-id="stage-1-the-structural-gam">Stage 1: The Structural GAM</h2>
<section id="the-model" class="level3">
<h3 class="anchored" data-anchor-id="the-model">The Model</h3>
<p>The goal is to predict speed from workout structure and effort, so that what remains reflects fitness more clearly. The formula:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Bspeed%7D%20%5Csim%20%5Ctext%7Bte%7D(%5Ctext%7Bpos%5C_mid%7D,%20%5Clog(%5Ctext%7Brep%5C_length%7D))%20+%20s(%5Ctext%7Bcum%5C_distance%7D)%20+%20s(%5Clog(1%20+%20%5Ctext%7Brest%7D))%20+%20%5Ctext%7Bpool%5C_length%7D%20+%20s(%5Ctext%7Bstroke%5C_rate%7D)%20+%20s(%5Ctext%7Bavg%5C_hr%7D)%20+%20%5Ctext%7Bset%5C_type%7D%0A"></p>
<p>A few modeling choices are worth explaining:</p>
<ul>
<li><strong>Tensor product</strong> <code>te(pos_mid, log_rep_length)</code> rather than separate smooths, because pacing shape genuinely varies with rep distance — the step function of a 50 looks nothing like the gradual ramp of a 300.</li>
<li><strong><code>scat()</code> family</strong> — a scaled <img src="https://latex.codecogs.com/png.latex?t">-distribution rather than Gaussian. Watch glitches, bad stroke detection, and occasional anomalous sprints or coasts create heavy-tailed residuals. The estimated degrees of freedom (<img src="https://latex.codecogs.com/png.latex?%5Capprox%204">) confirm this; <code>scat()</code> automatically down-weights these without aggressive deletion <span class="citation" data-cites="wood2017">(Wood 2017)</span>.</li>
<li><strong><code>bam()</code> with <code>discrete = TRUE</code></strong> for computational efficiency on roughly 10K rows <span class="citation" data-cites="wood2011">(Wood 2011)</span>.</li>
<li><strong>No temporal smooth.</strong> One might consider adding <code>s(date)</code> to capture the fitness trend directly in Stage 1. I deliberately omit it: a temporal smooth here would compete with the density surface in Stage 2, creating identifiability issues — if Stage 1 already absorbs the fitness trend, the density surface has nothing left to track. Practically, I tested including <code>s(date_num)</code> and found it introduced instability in the smooth estimates without improving the adjusted pace quality. The structural GAM stays purely cross-sectional (modeling within-session mechanics), and all temporal evolution is delegated to Stage 2.</li>
</ul>
<div class="cell">
<details class="code-fold">
<summary>Block 5: Fit structural GAM</summary>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">fit_structural <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;">bam</span>(</span>
<span id="cb14-2">  speed <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;">te</span>(pos_mid, log_rep_length, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(cum_distance, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(log1p_rest, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-5">    pool_length <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(stroke_rate, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(avg_hr) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-8">    set_type,</span>
<span id="cb14-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data   =</span> d_free_hr,</span>
<span id="cb14-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scat</span>(),</span>
<span id="cb14-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fREML"</span>,</span>
<span id="cb14-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">discrete =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb14-13">)</span>
<span id="cb14-14"></span>
<span id="cb14-15">sm <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;">summary</span>(fit_structural)</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>R² = 0.531 </code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>scat df = 3.99 </code></pre>
</div>
</div>
</section>
<section id="what-each-term-captures" class="level3">
<h3 class="anchored" data-anchor-id="what-each-term-captures">What Each Term Captures</h3>
<div class="cell">
<details class="code-fold">
<summary>Partial effect plots</summary>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw</span>(fit_structural, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">residuals =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/swim-css-pipeline/index_files/figure-html/block5-draw-1.png" class="img-fluid figure-img" width="1500"></p>
<figcaption>Partial effects of each smooth term. Stroke rate dominates (~1.7 m/s range), confirming it is the primary effort lever. The pacing surface, cumulative distance, rest state, and heart rate each capture distinct structural effects.</figcaption>
</figure>
</div>
</div>
</div>
<p>The partial effects line up pretty well with what you would expect as a swimmer:</p>
<ul>
<li><strong>Tensor (pacing × rep distance)</strong> — shorter sets tend to be faster, and speed is higher at the start and end of intervals compared to the middle.</li>
<li><strong>Cumulative distance</strong> — you can see the slower warmup in the first few hundred meters, followed by a plateau, and more variable speed toward the end of the workout.</li>
<li><strong>Rest state</strong> — shows a U-shape. At log1p_rest = 0, which mostly corresponds to lengths that continue through the wall without stopping, speed is relatively high because the swimmer carries momentum through the turn. As rest increases from zero (short wall starts), speed drops — the swimmer has stopped but hasn’t recovered yet. With longer rest, speed recovers back toward that turn-continuation baseline.</li>
<li><strong>Stroke rate</strong> — dominates everything, spanning roughly 1.7 m/s across the full speed range. This confirms stroke rate is the primary effort lever. Note that unusually fast speeds at low stroke rates likely correspond to sets with fins or paddles, which the model cannot distinguish.</li>
<li><strong>Heart rate</strong> — adds a smaller but real contribution. Everything above roughly 135 bpm is associated with higher speed.</li>
</ul>
<div class="cell">
<details class="code-fold">
<summary>Model diagnostics</summary>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">appraise</span>(fit_structural)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/swim-css-pipeline/index_files/figure-html/block5-diagnostics-1.png" class="img-fluid figure-img" width="1500"></p>
<figcaption>Model diagnostics via gratia::appraise(). The QQ plot confirms the scat() family is appropriate — a Gaussian fit would show heavy tails.</figcaption>
</figure>
</div>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Basis adequacy checks</summary>
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">k.check</span>(fit_structural)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>                           k'       edf   k-index p-value
te(log_rep_length,pos_mid) 19 18.517552 0.9573048   0.000
s(cum_distance)             9  6.353819 0.9763632   0.045
s(log1p_rest)               5  4.655131 0.9927923   0.325
s(stroke_rate)              9  8.856147 0.7298421   0.000
s(avg_hr)                   9  4.785629 1.0090911   0.740</code></pre>
</div>
</div>
<p>Most terms pass the basis adequacy check comfortably. The stroke rate smooth has a low <img src="https://latex.codecogs.com/png.latex?k">-index, but this is not surprising — the relationship between stroke rate and speed is strong and nearly linear, so the smooth is doing most of its work with very few effective degrees of freedom. The low <img src="https://latex.codecogs.com/png.latex?k">-index reflects the fact that there is real speed variation at any given stroke rate (from unmeasured factors like equipment, kick intensity, etc.) rather than a problem with the model.</p>
</section>
</section>
<section id="computing-adjusted-pace" class="level2">
<h2 class="anchored" data-anchor-id="computing-adjusted-pace">Computing Adjusted Pace</h2>
<p>The structural GAM explains about half the variance in raw speed. It doesn’t perfectly remove structural effects — it <em>reduces</em> them substantially, narrowing the pace distribution so the fitness signal becomes more visible. The adjusted pace is computed as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Badj%5C_speed%7D_i%20=%20%5Ctext%7Bspeed%7D_i%20-%20%5Chat%7Bf%7D(%5Cmathbf%7Bx%7D_i)%20+%20%5Chat%7Bf%7D(%5Cmathbf%7Bx%7D_%7B%5Ctext%7Bref%7D%7D)%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bf%7D"> is the structural model prediction and <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bx%7D_%7B%5Ctext%7Bref%7D%7D"> is a fixed reference condition: mid-rep of a typical repeat, mid-workout, off a flip turn, at median stroke rate and heart rate, in a 25-yard pool, in a pure freestyle set. The reference point gives the adjusted pace an interpretable anchor — it represents the predicted speed under standardized conditions.</p>
<div class="cell">
<details class="code-fold">
<summary>Block 6: Compute adjusted pace</summary>
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define working laps for reference SR</span></span>
<span id="cb21-2">working_laps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d_free_hr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(cum_distance <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>,</span>
<span id="cb21-4">         cum_distance <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;">quantile</span>(cum_distance, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>))</span>
<span id="cb21-5"></span>
<span id="cb21-6">base_sr <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;">median</span>(working_laps<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>stroke_rate, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb21-7">most_common_pool <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d_free_hr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(pool_length, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sort =</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;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(pool_length)</span>
<span id="cb21-9"></span>
<span id="cb21-10">structural_pred <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_structural, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>)</span>
<span id="cb21-11"></span>
<span id="cb21-12">ref <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="cb21-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pos_mid        =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb21-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log_rep_length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(d_free_hr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>log_rep_length),</span>
<span id="cb21-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cum_distance   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(d_free_hr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cum_distance),</span>
<span id="cb21-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log1p_rest     =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb21-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pool_length    =</span> most_common_pool,</span>
<span id="cb21-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stroke_rate    =</span> base_sr,</span>
<span id="cb21-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_hr         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(d_free_hr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>avg_hr),</span>
<span id="cb21-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">set_type       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pure_free"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(d_free_hr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>set_type))</span>
<span id="cb21-21">)</span>
<span id="cb21-22"></span>
<span id="cb21-23">ref_baseline <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(fit_structural, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> ref, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>))</span>
<span id="cb21-24"></span>
<span id="cb21-25">d_free_hr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>adj_speed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d_free_hr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>speed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> structural_pred <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ref_baseline</span>
<span id="cb21-26">d_free_hr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>adj_pace  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">91.44</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> d_free_hr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>adj_speed</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>Reference baseline: 1.073 m/s = 1:25.2 /100y</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>  base SR: 0.483 str/s | pool: 22.86 m | median HR: 148 bpm</code></pre>
</div>
</div>
<section id="the-adjustment-tightens-the-distribution" class="level3">
<h3 class="anchored" data-anchor-id="the-adjustment-tightens-the-distribution">The Adjustment Tightens the Distribution</h3>
<p>The whole point of Stage 1 is to reduce noise so that the fitness signal becomes visible. Here’s the effect — raw pace versus adjusted pace:</p>
<div class="cell">
<details class="code-fold">
<summary>Variance reduction visualization</summary>
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">d_free_hr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>raw_pace <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">91.44</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> d_free_hr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>speed</span>
<span id="cb24-2"></span>
<span id="cb24-3">density_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;">bind_rows</span>(</span>
<span id="cb24-4">  d_free_hr <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;">transmute</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pace =</span> raw_pace, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Raw pace"</span>),</span>
<span id="cb24-5">  d_free_hr <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;">transmute</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pace =</span> adj_pace, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adjusted pace"</span>)</span>
<span id="cb24-6">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-7">  <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;">type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"Raw pace"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adjusted pace"</span>)))</span>
<span id="cb24-8"></span>
<span id="cb24-9">raw_sd <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(d_free_hr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>raw_pace, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb24-10">adj_sd <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(d_free_hr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>adj_pace, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb24-11"></span>
<span id="cb24-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(density_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> pace, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> type)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</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 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.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(</span>
<span id="cb24-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pace per 100y (sec)"</span>,</span>
<span id="cb24-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</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;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>),</span>
<span id="cb24-17">    <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;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</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;">5</span>),</span>
<span id="cb24-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</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;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%d:%02d"</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;">60</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;">60</span>)</span>
<span id="cb24-19">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_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;">"grey60"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-21">  <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> <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;">"grey40"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue4"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb24-23">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Raw SD = "</span>, raw_sd, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" sec</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Adj SD = "</span>, adj_sd, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" sec"</span>),</span>
<span id="cb24-24">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Structural adjustment narrows the pace distribution"</span>,</span>
<span id="cb24-26">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Reducing effort, pacing, fatigue, and rest effects"</span>,</span>
<span id="cb24-27">       <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;">"Density"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-28">  <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/swim-css-pipeline/index_files/figure-html/block6-variance-reduction-1.png" class="img-fluid figure-img" width="1200"></p>
<figcaption>The structural adjustment narrows the pace distribution substantially, reducing workout design noise that would otherwise mask the fitness signal.</figcaption>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="stage-2-the-density-surface" class="level2">
<h2 class="anchored" data-anchor-id="stage-2-the-density-surface">Stage 2: The Density Surface</h2>
<p>With adjusted paces in hand, the question becomes: how does the <em>distribution</em> of adjusted pace evolve over time? Rather than tracking a single session mean, I model the full pace distribution as a smooth surface over (date × pace).</p>
<section id="binning-into-a-2d-histogram" class="level3">
<h3 class="anchored" data-anchor-id="binning-into-a-2d-histogram">Binning into a 2D Histogram</h3>
<p>Each session’s adjusted paces are binned into 1-second pace bins, normalized to proportions. The resulting grid is completed with zeros so every session × pace combination has a value.</p>
<div class="cell">
<details class="code-fold">
<summary>Block 7: Build 2D histogram</summary>
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">d_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d_free_hr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-2">  <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;">date_num =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(session_date <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;">min</span>(session_date)))</span>
<span id="cb25-3"></span>
<span id="cb25-4">pace_grid <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(d_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>adj_pace, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)),</span>
<span id="cb25-5">                 <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ceiling</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(d_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>adj_pace, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</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="cb25-6"></span>
<span id="cb25-7">d_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pace_bin <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;">cut</span>(d_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>adj_pace, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> pace_grid,</span>
<span id="cb25-8">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include.lowest =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb25-9">d_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pace_mid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pace_grid[d_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pace_bin] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb25-10"></span>
<span id="cb25-11">hist2d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d_plot <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-12">  <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>(pace_bin)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(session_id, session_date, date_num, pace_mid) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(session_id) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-16">  <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;">prop =</span> count <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;">sum</span>(count)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb25-18"></span>
<span id="cb25-19">all_sessions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d_plot <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;">distinct</span>(session_id, session_date, date_num)</span>
<span id="cb25-20">all_pace <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;">pace_mid =</span> pace_grid[<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;">length</span>(pace_grid)] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb25-21"></span>
<span id="cb25-22">full_grid <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;">crossing</span>(all_sessions, all_pace) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(hist2d <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>(session_id, pace_mid, prop),</span>
<span id="cb25-24">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</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;">"session_id"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pace_mid"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-25">  <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;">prop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace_na</span>(prop, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb25-26"></span>
<span id="cb25-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Trim extremes</span></span>
<span id="cb25-28">pace_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;">quantile</span>(d_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>adj_pace, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.995</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb25-29">full_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> full_grid <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(pace_mid <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;">floor</span>(pace_range[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]),</span>
<span id="cb25-31">         pace_mid <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;">ceiling</span>(pace_range[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]))</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>Histogram grid: 8282 cells | 202 sessions | 41 pace bins</code></pre>
</div>
</div>
</section>
<section id="fitting-the-density-surface" class="level3">
<h3 class="anchored" data-anchor-id="fitting-the-density-surface">Fitting the Density Surface</h3>
<p>A tensor product GAM smooths the noisy session-level histograms into a continuous density surface. The model uses a quasibinomial family (logit link respects <img src="https://latex.codecogs.com/png.latex?%5B0,%201%5D"> bounds) and lets the penalty — not the basis dimension — control complexity <span class="citation" data-cites="wood2017">(Wood 2017)</span>.</p>
<div class="cell">
<details class="code-fold">
<summary>Block 8: Fit density surface GAM</summary>
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">fit_density <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;">bam</span>(</span>
<span id="cb27-2">  prop <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;">te</span>(date_num, pace_mid, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</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;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs =</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;">"cr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cr"</span>)),</span>
<span id="cb27-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data    =</span> full_grid,</span>
<span id="cb27-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quasibinomial</span>(),</span>
<span id="cb27-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fREML"</span>,</span>
<span id="cb27-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">discrete =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb27-7">)</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>Density surface R² = 0.493 </code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Effective degrees of freedom: 77 </code></pre>
</div>
</div>
<p>The surface has a temporal resolution of approximately 6 weeks — each density estimate borrows information from sessions within roughly ±3 weeks. In this retrospective analysis the tensor product smooths bidirectionally, borrowing from both past and future sessions. In a rolling real-time use case, the same idea could be applied with a trailing window so the estimate only borrows from the past.</p>
<div class="cell">
<details class="code-fold">
<summary>Predict on fine grid</summary>
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1">pred_grid <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;">crossing</span>(</span>
<span id="cb30-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_num =</span> <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>(full_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date_num), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(full_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date_num),</span>
<span id="cb30-3">                 <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;">200</span>),</span>
<span id="cb30-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pace_mid =</span> <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>(full_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pace_mid), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(full_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pace_mid), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb30-5">)</span>
<span id="cb30-6">pred_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pred <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_density, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_grid, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>)</span>
<span id="cb30-7">pred_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date <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;">min</span>(d_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>session_date) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pred_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date_num</span></code></pre></div>
</details>
</div>
</section>
</section>
<section id="base-pace-extraction" class="level2">
<h2 class="anchored" data-anchor-id="base-pace-extraction">Base Pace Extraction</h2>
<p>The fitness metric is the <strong>mode</strong> of the adjusted pace distribution at each time point — the pace where the density surface peaks. This is the “base” in the sense swimmers use the word: the pace you naturally settle into across a typical training session, after accounting for effort and workout structure.</p>
<p>Raw grid evaluation produces step-function artifacts, so I refine the peak location with parabolic interpolation through 5 grid points centered on the argmax:</p>
<div class="cell">
<details class="code-fold">
<summary>Block 9: Extract base pace via parabolic interpolation</summary>
<div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1">refine_peak <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, y) {</span>
<span id="cb31-2">  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;">which.max</span>(y)</span>
<span id="cb31-3">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (idx <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</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> idx <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;">length</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;">1</span>) {</span>
<span id="cb31-4">    <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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">peak_pace =</span> x[idx], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">peak_density =</span> y[idx]))</span>
<span id="cb31-5">  }</span>
<span id="cb31-6">  win <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, idx <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(y), idx <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb31-7">  xw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x[win]; yw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y[win]</span>
<span id="cb31-8">  qfit <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;">lm</span>(yw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> xw <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;">I</span>(xw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb31-9">  co <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;">coef</span>(qfit)</span>
<span id="cb31-10">  peak_x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>co[<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 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> co[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>])</span>
<span id="cb31-11">  peak_y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> co[<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> co[<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> peak_x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> co[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> peak_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;">2</span></span>
<span id="cb31-12">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (peak_x <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;">min</span>(xw) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> peak_x <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;">max</span>(xw)) {</span>
<span id="cb31-13">    <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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">peak_pace =</span> x[idx], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">peak_density =</span> y[idx]))</span>
<span id="cb31-14">  }</span>
<span id="cb31-15">  <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;">peak_pace =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(peak_x),</span>
<span id="cb31-16">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">peak_density =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(peak_y))</span>
<span id="cb31-17">}</span>
<span id="cb31-18"></span>
<span id="cb31-19">gam_peak <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pred_grid <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb31-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(date_num, date) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb31-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(pace_mid) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb31-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_modify</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;">refine_peak</span>(.x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pace_mid, .x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pred)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb31-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span></code></pre></div>
</details>
</div>
</section>
<section id="results" class="level2">
<h2 class="anchored" data-anchor-id="results">Results</h2>
<section id="the-density-surface" class="level3">
<h3 class="anchored" data-anchor-id="the-density-surface">The Density Surface</h3>
<div class="cell">
<details class="code-fold">
<summary>Density surface heatmap with base pace overlay</summary>
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Formatting helpers</span></span>
<span id="cb32-2">format_pace <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>(secs) {</span>
<span id="cb32-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(secs <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;">60</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;">"%04.1f"</span>, secs <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;">60</span>))</span>
<span id="cb32-4">}</span>
<span id="cb32-5"></span>
<span id="cb32-6">race_date   <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;">"2025-09-27"</span>)</span>
<span id="cb32-7">buddy_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;">"2025-04-14"</span>)</span>
<span id="cb32-8">buddy_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;">"2025-05-31"</span>)</span>
<span id="cb32-9"></span>
<span id="cb32-10">pace_ticks  <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">105</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;">5</span>)</span>
<span id="cb32-11">pace_labels <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;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%d:%02d"</span>, pace_ticks <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;">60</span>, pace_ticks <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;">60</span>)</span>
<span id="cb32-12"></span>
<span id="cb32-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pred_grid, <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> pace_mid, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> pred)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb32-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interpolate =</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="cb32-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_viridis_c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inferno"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb32-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_reverse</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adjusted pace / 100y"</span>,</span>
<span id="cb32-17">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> pace_ticks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> pace_labels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb32-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Swim buddy period</span></span>
<span id="cb32-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rect"</span>,</span>
<span id="cb32-20">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmin =</span> buddy_start, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</span> buddy_end,</span>
<span id="cb32-21">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>,</span>
<span id="cb32-22">           <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;">"black"</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.15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb32-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> buddy_start <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (buddy_end <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> buddy_start) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb32-24">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(pace_ticks) <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="cb32-25">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Swim buddy"</span>, <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;">"black"</span>, <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;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb32-26">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Race</span></span>
<span id="cb32-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> race_date, <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;">2</span>, <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;">"black"</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.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb32-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> race_date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(pace_ticks),</span>
<span id="cb32-29">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"La Jolla relay"</span>, <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;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <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;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb32-30">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Base pace trace</span></span>
<span id="cb32-31">  <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;">data =</span> gam_peak, <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> peak_pace, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>),</span>
<span id="cb32-32">            <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;">"cyan"</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="cb32-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Adjusted pace density surface"</span>,</span>
<span id="cb32-34">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cyan line = density peak (base pace) | Parabolic interpolation"</span>,</span>
<span id="cb32-35">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb32-36">  <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;">"right"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/swim-css-pipeline/index_files/figure-html/block10-surface-1.png" class="img-fluid figure-img" width="1500"></p>
<figcaption>Smoothed pace density surface over 19 months. Darker regions indicate more laps at that pace. The cyan line traces the density peak (base pace). The vertical dashed line marks the La Jolla Rough Water Swim relay; the shaded region marks a period of training with a swim buddy.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="the-base-pace-trend" class="level3">
<h3 class="anchored" data-anchor-id="the-base-pace-trend">The Base Pace Trend</h3>
<div class="cell">
<details class="code-fold">
<summary>Base pace time series</summary>
<div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(gam_peak, <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> peak_pace)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Swim buddy shading</span></span>
<span id="cb33-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rect"</span>,</span>
<span id="cb33-4">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmin =</span> buddy_start, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</span> buddy_end,</span>
<span id="cb33-5">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>,</span>
<span id="cb33-6">           <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;">"black"</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.1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> buddy_start <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (buddy_end <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> buddy_start) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb33-8">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(gam_peak<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>peak_pace) <span class="sc" style="color: #5E5E5E;
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="cb33-9">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Swim buddy"</span>, <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;">"grey30"</span>, <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;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</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.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_reverse</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Base pace / 100y"</span>,</span>
<span id="cb33-12">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> pace_ticks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> pace_labels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_breaks =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 months"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_labels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%b '%y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> race_date, <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;">2</span>, <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;">"grey40"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> race_date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(gam_peak<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>peak_pace) <span class="sc" style="color: #5E5E5E;
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="cb33-16">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"La Jolla relay"</span>, <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;">"grey40"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <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;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Estimated base pace over time"</span>,</span>
<span id="cb33-18">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mode of adjusted pace distribution | Faster = up"</span>,</span>
<span id="cb33-19">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/swim-css-pipeline/index_files/figure-html/block10-peak-1.png" class="img-fluid figure-img" width="1500"></p>
<figcaption>Estimated base pace over time. Y-axis is reversed so faster is up. The metric captures mesocycle oscillations and shows the expected peak leading into the September 2025 race.</figcaption>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>Base pace range: 1:23.5 to 1:26.4 | span: 3 sec</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Current (latest): 1:25.0 /100y</code></pre>
</div>
</div>
<p>A few things stand out immediately. First, the base estimate is not flat: it moves on the scale of weeks to months in a way that looks like actual training rather than random session noise. Second, it trends faster heading into the September 2025 relay, which is exactly what I would hope to see if the metric is capturing real fitness. Third, the swim-buddy block lines up with a visibly faster patch as well. That is reassuring, because those are two periods where I independently expected to be swimming better.</p>
</section>
<section id="raw-data-vs-smoothed-surface" class="level3">
<h3 class="anchored" data-anchor-id="raw-data-vs-smoothed-surface">Raw Data vs Smoothed Surface</h3>
<div class="cell">
<details class="code-fold">
<summary>Raw 2D histogram for comparison</summary>
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1">x_breaks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(</span>
<span id="cb36-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</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;">"2024-10-01"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-01-01"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-04-01"</span>,</span>
<span id="cb36-3">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-07-01"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-10-01"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-01-01"</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;">min</span>(d_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>session_date)</span>
<span id="cb36-4">)</span>
<span id="cb36-5">x_labels <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Oct '24"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jan '25"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Apr '25"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jul '25"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Oct '25"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jan '26"</span>)</span>
<span id="cb36-6"></span>
<span id="cb36-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(d_plot, <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_num, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> adj_pace)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb36-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bin2d</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</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;">14</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="cb36-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_viridis_c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inferno"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Laps"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb36-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_reverse</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adjusted pace / 100y"</span>,</span>
<span id="cb36-11">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> pace_ticks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> pace_labels,</span>
<span id="cb36-12">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(d_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>adj_pace, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.995</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb36-13">                             <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(d_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>adj_pace, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</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="cb36-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> x_breaks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> x_labels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb36-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Raw 2D histogram — 2-week × 1-sec bins"</span>,</span>
<span id="cb36-16">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Before GAM smoothing"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/swim-css-pipeline/index_files/figure-html/block10-raw-1.png" class="img-fluid figure-img" width="1500"></p>
<figcaption>Raw 2D histogram before GAM smoothing. The density surface captures the dominant structure while suppressing session-to-session noise.</figcaption>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="limitations-and-future-directions" class="level2">
<h2 class="anchored" data-anchor-id="limitations-and-future-directions">Limitations and Future Directions</h2>
<p><strong>Equipment confound.</strong> Pull sets with paddles and buoy are systematically faster but equipment use is not recorded in FIT files. You can see this in the stroke rate partial effect — the cluster of fast speeds at low stroke rates likely corresponds to equipment-assisted sets. The model cannot distinguish these from genuinely efficient swimming, so they contribute noise to the adjusted paces.</p>
<p><strong>Retrospective smoothing.</strong> In the analysis above, the density surface borrows from both earlier and later sessions, so the plotted curve is best understood as a retrospective fitness trace. But that does not mean the framework is limited to retrospective use. In ongoing monitoring, the right edge naturally has no “future” to borrow from anyway, so the newest workouts are already estimated from past data only. Practically, this could be made more efficient by fitting on rolling windows rather than the full history: for example, Stage 1 could be refit on a recent block of sessions to adapt to gradual changes in workout structure, while Stage 2 could be run on only the last ~100 sessions, since real pace changes are usually slow and the density surface mainly needs local history rather than the entire archive.</p>
<p><strong>Future work.</strong> The same pipeline could be applied to other strokes — backstroke, breaststroke, butterfly — each with its own structural model.</p>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>Two GAMs, one purpose. The first reduces within-session structural variation (pacing, fatigue, rest, effort) to produce adjusted pace. The second smooths the adjusted pace distribution over calendar time and extracts its peak as a base pace estimate.</p>
<p>The resulting base pace metric:</p>
<ul>
<li>Produces a running estimate that can be refreshed with each new session</li>
<li>Is robust to effort variation (easy days don’t look like fitness drops)</li>
<li>Captures mesocycle oscillations invisible to raw averages</li>
<li>Showed the expected fitness peak heading into the La Jolla relay</li>
</ul>
<p>In the full retrospective version shown here, the surface borrows a little from the future as well as the past. For ongoing monitoring, though, the newest workout naturally sits at the right edge and has no future data available, so in practice the estimate there is already driven only by past sessions. A practical real-time implementation would therefore not need to rerun everything on the full archive: Stage 1 could be updated on a rolling window to stay calibrated to recent workout structure, and Stage 2 could use only the last ~100 sessions or so, since changes in true pace are slow and mostly local in time.</p>
<p>The full extraction and analysis code is available on <a href="https://github.com/jbogomolovas2/swim-css-pipeline">GitHub</a>.</p>
<hr>
<p><em>Visualization and diagnostics built with <code>gratia</code><span class="citation" data-cites="gratia">(Simpson 2024)</span>. Model fitting via <code>mgcv</code><span class="citation" data-cites="wood2011 wood2017">(Wood 2011, 2017)</span>.</em></p>
</section>
<section id="references" class="level2">




</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-gratia" class="csl-entry">
Simpson, Gavin L. 2024. <em><span class="nocase">gratia</span>: Graceful Ggplot-Based Graphics and Other Functions for <span>GAMs</span> Fitted Using <span class="nocase">mgcv</span></em>. <a href="https://gavinsimpson.github.io/gratia/">https://gavinsimpson.github.io/gratia/</a>.
</div>
<div id="ref-FITfileR" class="csl-entry">
Smith, Mike. 2025. <em><span>FITfileR</span>: Read <span>FIT</span> Files Using Only Native <span>R</span> Code</em>. <a href="https://github.com/grimbough/FITfileR">https://github.com/grimbough/FITfileR</a>.
</div>
<div id="ref-wood2011" class="csl-entry">
Wood, Simon N. 2011. <span>“Fast Stable Restricted Maximum Likelihood and Marginal Likelihood Estimation of Semiparametric Generalized Linear Models.”</span> <em>Journal of the Royal Statistical Society: Series B</em> 73 (1): 3–36. <a href="https://doi.org/10.1111/j.1467-9868.2010.00749.x">https://doi.org/10.1111/j.1467-9868.2010.00749.x</a>.
</div>
<div id="ref-wood2017" class="csl-entry">
———. 2017. <em>Generalized Additive Models: An Introduction with <span>R</span></em>. 2nd ed. Chapman; Hall/CRC. <a href="https://doi.org/10.1201/9781315370279">https://doi.org/10.1201/9781315370279</a>.
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>GAM</category>
  <category>Sports Analytics</category>
  <category>Swimming</category>
  <category>Time Series</category>
  <guid>https://jbogomolovas2.github.io/Julius-s-Blog/posts/swim-css-pipeline/</guid>
  <pubDate>Sat, 04 Apr 2026 07:00:00 GMT</pubDate>
  <media:content url="https://jbogomolovas2.github.io/Julius-s-Blog/posts/swim-css-pipeline/IMG_9057.JPG" medium="image"/>
</item>
<item>
  <title>Cherry Bloom Prediction</title>
  <dc:creator>Julijus Bogomolovas</dc:creator>
  <link>https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/</link>
  <description><![CDATA[ 





<section id="cherry-blossom-day-prediction" class="level2">
<h2 class="anchored" data-anchor-id="cherry-blossom-day-prediction">Cherry Blossom day prediction</h2>
<p>So earlier this year I participated in <a href="https://competition.statistics.gmu.edu/"><strong>International Cherry Blossom Prediction Competition</strong></a><strong>,</strong> which invites you to predict this years bloom date of cherry trees in 5 different locations based on provided historical bloom dates provided and any data you dig out. Now that cherry trees bloomed long time ago, I am sharing my entry. As good ideas come after, I enhanced last modelling step by introducing State-Space framework into GAM model, allowing to deal with observation poor sites. Lets load all required packages.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load all required packages</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(nasapower)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(grateful)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lubridate)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(missForest)</span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(spls)</span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stringr)</span>
<span id="cb1-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mvgam)</span>
<span id="cb1-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tibble)</span></code></pre></div>
</div>
<section id="cherry-blossom-data-engineering" class="level3">
<h3 class="anchored" data-anchor-id="cherry-blossom-data-engineering">Cherry Blossom Data Engineering</h3>
<p>The historical cherry blossom data was provided by the competition organizers at <a href="https://competition.statistics.gmu.edu/" class="uri">https://competition.statistics.gmu.edu/</a>. For New York City, which had only one historical record in the competition dataset, I augmented it with observations from the USA National Phenology Network.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Import cherry blossom data
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import cherry blossom data and filter for records from 1981 onward</span></span>
<span id="cb2-2">Vancouver <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;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/vancouver.csv"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_col_types =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-3">Washington <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;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/washingtondc.csv"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_col_types =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-4">Kyoto <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;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/kyoto.csv"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_col_types =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-5">Swiss <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;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/liestal.csv"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_col_types =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-6">Nyc <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;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/nyc.csv"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_col_types =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-7"></span>
<span id="cb2-8">combined_blossom_dates <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>(Vancouver, Washington, Kyoto, Swiss, Nyc)</span>
<span id="cb2-9">combined_blossom_dates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> combined_blossom_dates[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(combined_blossom_dates<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location, combined_blossom_dates<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year), ]</span>
<span id="cb2-10">combined_blossom_dates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> combined_blossom_dates[combined_blossom_dates<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1981</span>, ]</span></code></pre></div>
</div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Augment New York data with USA-NPN observations
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read USA-NPN individual phenometrics data</span></span>
<span id="cb3-2">USA_NPN_status_intensity <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;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data/USA-NPN_status_intensity_observations_data.csv'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_col_types =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert to data frame and prepare Intensity_Value as factor</span></span>
<span id="cb3-5">USA_status <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.data.frame</span>(USA_NPN_status_intensity)</span>
<span id="cb3-6">USA_status<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Intensity_Value <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.factor</span>(USA_status<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Intensity_Value)</span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Filter out records with intensity values that do not match our criteria</span></span>
<span id="cb3-9">USA_status_filtered <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> USA_status[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(USA_status<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Intensity_Value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> </span>
<span id="cb3-10">  <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;">"-9999"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Little"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"25-49%"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5-24%"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Less than 5%"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"More than 10"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"50-74%"</span>)), ]</span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert dates and extract Year and Day of Year</span></span>
<span id="cb3-13">USA_status_filtered<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Date <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;">mdy</span>(USA_status_filtered<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Observation_Date)</span>
<span id="cb3-14">USA_status_filtered<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Year <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;">year</span>(USA_status_filtered<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Date)</span>
<span id="cb3-15">USA_status_filtered<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Day_of_Year <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;">yday</span>(USA_status_filtered<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Date)</span>
<span id="cb3-16"></span>
<span id="cb3-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a scatter plot to visualize Day of Year vs. Year, colored by Intensity_Value</span></span>
<span id="cb3-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(USA_status_filtered, <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> Year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Day_of_Year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Intensity_Value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<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;">3</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.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</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;">"Set1"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Day of Year vs Year for USA-NPN Data"</span>,</span>
<span id="cb3-22">       <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;">"Year"</span>,</span>
<span id="cb3-23">       <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;">"Day of Year"</span>,</span>
<span id="cb3-24">       <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;">"Intensity Value"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Summarize the filtered data by year</span></span>
<span id="cb4-2">year_summary_NY_individual <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> USA_status_filtered <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(Year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb4-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Min =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(Day_of_Year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb4-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(Day_of_Year, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb4-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Median =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(Day_of_Year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb4-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(Day_of_Year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb4-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q3 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(Day_of_Year, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb4-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Max =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(Day_of_Year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb4-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()</span>
<span id="cb4-12">  )</span>
<span id="cb4-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(year_summary_NY_individual)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 13 × 8
    Year   Min    Q1 Median  Mean    Q3   Max Count
   &lt;int&gt; &lt;int&gt; &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt;
 1  2012    89  89      89    89    89     89     2
 2  2013   104 110     142   137.  150    170     9
 3  2014    98 108.    141   132.  156    160    11
 4  2015   101 138     148   141.  153    156    24
 5  2016   110 128.    135   134   143.   153     6
 6  2017    87 110     138   128.  143    146     9
 7  2018    79  99     101   103.  107    140    66
 8  2019    96 101     106.  115.  124.   151    24
 9  2020    78  90.5   100   107.  117.   243    32
10  2021    86  99     106   111.  116    148    41
11  2022    97 110.    122.  122.  136.   154    10
12  2023    92 104     130.  130.  139.   255    18
13  2024    88  94     106   110.  124    147    11</code></pre>
</div>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create new NY data frame from the yearly summary (excluding 2024)</span></span>
<span id="cb6-2">ny_data <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="cb6-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">location =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"newyorkcity"</span>,</span>
<span id="cb6-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40.73040</span>,</span>
<span id="cb6-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">long =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">73.99809</span>,</span>
<span id="cb6-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alt =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.5</span>,</span>
<span id="cb6-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> year_summary_NY_individual<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Year[year_summary_NY_individual<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Year <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;">2024</span>],</span>
<span id="cb6-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bloom_date =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,</span>
<span id="cb6-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bloom_doy =</span> year_summary_NY_individual<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Min[year_summary_NY_individual<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Year <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;">2024</span>]</span>
<span id="cb6-10">)</span>
<span id="cb6-11"></span>
<span id="cb6-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert day-of-year to actual dates</span></span>
<span id="cb6-13">ny_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bloom_date <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>(ny_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bloom_doy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">origin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(ny_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-01-01"</span>))</span>
<span id="cb6-14"></span>
<span id="cb6-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Merge with existing combined blossom dates</span></span>
<span id="cb6-16">combined_blossom_dates <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>(combined_blossom_dates, ny_data)</span>
<span id="cb6-17">combined_blossom_dates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> combined_blossom_dates[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(combined_blossom_dates<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location, combined_blossom_dates<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>year), ]</span></code></pre></div>
</div>
</div>
</div>
</div>
<p>Let’s examine the distribution of bloom dates across locations to determine an appropriate modeling timeframe:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Summarize blossom dates by location</span></span>
<span id="cb7-2">blossom_summary <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> combined_blossom_dates <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(location) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb7-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Min =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(bloom_doy),</span>
<span id="cb7-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(bloom_doy, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>),</span>
<span id="cb7-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Median =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(bloom_doy),</span>
<span id="cb7-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q3 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(bloom_doy, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>),</span>
<span id="cb7-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Max =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(bloom_doy),</span>
<span id="cb7-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Range =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(bloom_doy) <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;">min</span>(bloom_doy)</span>
<span id="cb7-11">  )</span>
<span id="cb7-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(blossom_summary)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 7
  location       Min    Q1 Median    Q3   Max Range
  &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1 kyoto           84  93       95    99   109    25
2 liestal         75  87       93   101   121    46
3 newyorkcity     78  87       92    98   110    32
4 vancouver       83  84.5     86    91    96    13
5 washingtondc    74  86       91    95   101    27</code></pre>
</div>
</div>
<p>The maximum third quartile (Q3) at day 100 represents when 75% of historical blooms have occurred in late–bloomer locations effectively marking the end of the bloom season across all locations. While I could theoretically calculate predictors from one recorded bloom date to the next, this creates a circular dependency: to predict next year’s bloom date, we’d need to know it already to determine when to stop accumulating our weather predictors. By using day 100 as our universal cutoff, I establish a “cherry year” that runs from April 10 to April 9, nicely matching historical bloom cycles. ### 2. Weather Feature Engineering</p>
<p>With our cherry year defined (April 10 to April 9), we now need to engineer weather features that align with this timeframe. The challenge is creating predictors that capture cumulative weather effects leading up to bloom, without knowing when the bloom will occur.</p>
<p>I used NASA POWER for continuous weather data from 1981-2025, calculated daily anomalies to normalize across locations, created 30-day rolling sums to capture cumulative effects, and transformed everything to cherry year coordinates. Missing values (~2.4%) were imputed using random forest.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Download weather data from NASA POWER
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Retrieve weather data using nasapower for each location</span></span>
<span id="cb9-2">Kyoto_temp <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;">get_power</span>(</span>
<span id="cb9-3">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">community =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ag"</span>,</span>
<span id="cb9-4">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lonlat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">135.6761</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">35.0120</span>),</span>
<span id="cb9-5">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pars =</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;">"T2M"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T2M_MAX"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T2M_MIN"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PRECTOTCORR"</span>),</span>
<span id="cb9-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dates =</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;">"1981-01-01"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-02-15"</span>),</span>
<span id="cb9-7">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">temporal_api =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"daily"</span></span>
<span id="cb9-8">)</span>
<span id="cb9-9"></span>
<span id="cb9-10">Swiss_temp <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;">get_power</span>(</span>
<span id="cb9-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">community =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ag"</span>,</span>
<span id="cb9-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lonlat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.730519</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">47.4814</span>),</span>
<span id="cb9-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pars =</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;">"T2M"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T2M_MAX"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T2M_MIN"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PRECTOTCORR"</span>),</span>
<span id="cb9-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dates =</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;">"1981-01-01"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-02-15"</span>),</span>
<span id="cb9-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">temporal_api =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"daily"</span></span>
<span id="cb9-16">)</span>
<span id="cb9-17"></span>
<span id="cb9-18">Washington_temp <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;">get_power</span>(</span>
<span id="cb9-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">community =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ag"</span>,</span>
<span id="cb9-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lonlat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">77.0386</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">38.8853</span>),</span>
<span id="cb9-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pars =</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;">"T2M"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T2M_MAX"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T2M_MIN"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PRECTOTCORR"</span>),</span>
<span id="cb9-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dates =</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;">"1981-01-01"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-02-15"</span>),</span>
<span id="cb9-23">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">temporal_api =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"daily"</span></span>
<span id="cb9-24">)</span>
<span id="cb9-25"></span>
<span id="cb9-26">Vancouver_temp <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;">get_power</span>(</span>
<span id="cb9-27">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">community =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ag"</span>,</span>
<span id="cb9-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lonlat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">123.1636</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">49.2237</span>),</span>
<span id="cb9-29">   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pars =</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;">"T2M"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T2M_MAX"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T2M_MIN"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PRECTOTCORR"</span>),</span>
<span id="cb9-30">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dates =</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;">"1981-01-01"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-02-15"</span>),</span>
<span id="cb9-31">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">temporal_api =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"daily"</span></span>
<span id="cb9-32">)</span>
<span id="cb9-33"></span>
<span id="cb9-34">NY_temp <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;">get_power</span>(</span>
<span id="cb9-35">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">community =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ag"</span>,</span>
<span id="cb9-36">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lonlat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">73.99809</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40.73040</span>),</span>
<span id="cb9-37">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pars =</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;">"T2M"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T2M_MAX"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T2M_MIN"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PRECTOTCORR"</span>),</span>
<span id="cb9-38">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dates =</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;">"1981-01-01"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-02-15"</span>),</span>
<span id="cb9-39">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">temporal_api =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"daily"</span></span>
<span id="cb9-40">)</span></code></pre></div>
</div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Calculate daily weather anomalies
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-4" class="callout-4-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Function to calculate daily climate anomalies</span></span>
<span id="cb10-2">calculate_climate_anomalies <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>(data, </span>
<span id="cb10-3">                                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">baseline_start =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1981</span>, </span>
<span id="cb10-4">                                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">baseline_end =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2024</span>, </span>
<span id="cb10-5">                                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vars =</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;">"T2M"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T2M_MAX"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T2M_MIN"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PRECTOTCORR"</span>)) {</span>
<span id="cb10-6">  </span>
<span id="cb10-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Validate input variables</span></span>
<span id="cb10-8">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;">all</span>(vars <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;">names</span>(data))) {</span>
<span id="cb10-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Not all specified variables are present in the dataset."</span>)</span>
<span id="cb10-10">  }</span>
<span id="cb10-11">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;">all</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;">"YEAR"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DOY"</span>) <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;">names</span>(data))) {</span>
<span id="cb10-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dataset must include 'YEAR' and 'DOY' columns."</span>)</span>
<span id="cb10-13">  }</span>
<span id="cb10-14">  </span>
<span id="cb10-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create baseline subset</span></span>
<span id="cb10-16">  baseline_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>YEAR <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> baseline_start <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>YEAR <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> baseline_end, ]</span>
<span id="cb10-17">  </span>
<span id="cb10-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate daily climatology (mean for each day-of-year)</span></span>
<span id="cb10-19">  climatology <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;">aggregate</span>(baseline_data[vars], </span>
<span id="cb10-20">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</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;">DOY =</span> baseline_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>DOY), </span>
<span id="cb10-21">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> mean, </span>
<span id="cb10-22">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb10-23">  </span>
<span id="cb10-24">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Merge climatology with original data and compute anomalies</span></span>
<span id="cb10-25">  result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(data, climatology, <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;">"DOY"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">suffixes =</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;">""</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_mean"</span>))</span>
<span id="cb10-26">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (var <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> vars) {</span>
<span id="cb10-27">    mean_col <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>(var, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_mean"</span>)</span>
<span id="cb10-28">    anom_col <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>(var, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_anomaly"</span>)</span>
<span id="cb10-29">    result[[anom_col]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> result[[var]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> result[[mean_col]]</span>
<span id="cb10-30">  }</span>
<span id="cb10-31">  </span>
<span id="cb10-32">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Attach attributes and sort by date</span></span>
<span id="cb10-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attr</span>(result, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"baseline_period"</span>) <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;">paste</span>(baseline_start, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, baseline_end)</span>
<span id="cb10-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attr</span>(result, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"variables"</span>) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> vars</span>
<span id="cb10-35">  result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> result[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>YYYYMMDD), ]</span>
<span id="cb10-36">  </span>
<span id="cb10-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(result)</span>
<span id="cb10-38">}</span>
<span id="cb10-39"></span>
<span id="cb10-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate anomalies for each location</span></span>
<span id="cb10-41">NY_temp_anomalies <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;">calculate_climate_anomalies</span>(NY_temp)</span>
<span id="cb10-42">Vancouver_temp_anomalies <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;">calculate_climate_anomalies</span>(Vancouver_temp)</span>
<span id="cb10-43">Swiss_temp_anomalies <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;">calculate_climate_anomalies</span>(Swiss_temp)</span>
<span id="cb10-44">Washington_temp_anomalies <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;">calculate_climate_anomalies</span>(Washington_temp)</span>
<span id="cb10-45">Kyoto_temp_anomalies <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;">calculate_climate_anomalies</span>(Kyoto_temp)</span>
<span id="cb10-46"></span>
<span id="cb10-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Append location identifiers and combine</span></span>
<span id="cb10-48">NY_temp_anomalies<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"New York, USA"</span></span>
<span id="cb10-49">Vancouver_temp_anomalies<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vancouver, Canada"</span></span>
<span id="cb10-50">Swiss_temp_anomalies<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Liestal-Weideli, Switzerland"</span></span>
<span id="cb10-51">Washington_temp_anomalies<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Washington DC, USA"</span></span>
<span id="cb10-52">Kyoto_temp_anomalies<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kyoto, Japan"</span></span>
<span id="cb10-53"></span>
<span id="cb10-54">combined_anomalies <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="cb10-55">  NY_temp_anomalies,</span>
<span id="cb10-56">  Vancouver_temp_anomalies,</span>
<span id="cb10-57">  Swiss_temp_anomalies,</span>
<span id="cb10-58">  Washington_temp_anomalies,</span>
<span id="cb10-59">  Kyoto_temp_anomalies</span>
<span id="cb10-60">)</span>
<span id="cb10-61">combined_anomalies <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> combined_anomalies[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(combined_anomalies<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location, combined_anomalies<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>YYYYMMDD), ]</span></code></pre></div>
</div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-5-contents" aria-controls="callout-5" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Create rolling sums and transform to cherry years
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-5" class="callout-5-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert to data.table and compute 30-day rolling sums for each anomaly type</span></span>
<span id="cb11-2">combined_anomalies <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.data.table</span>(combined_anomalies)</span>
<span id="cb11-3">combined_anomalies[, temp_ave_pos_rollsum <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</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;">frollsum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(T2M_anomaly <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, T2M_anomaly, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> location]</span>
<span id="cb11-4">combined_anomalies[, temp_ave_neg_rollsum <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</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;">frollsum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(T2M_anomaly <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, T2M_anomaly, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> location]</span>
<span id="cb11-5">combined_anomalies[, temp_max_pos_rollsum <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</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;">frollsum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(T2M_MAX_anomaly <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, T2M_MAX_anomaly, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> location]</span>
<span id="cb11-6">combined_anomalies[, temp_max_neg_rollsum <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</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;">frollsum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(T2M_MAX_anomaly <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, T2M_MAX_anomaly, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> location]</span>
<span id="cb11-7">combined_anomalies[, temp_min_pos_rollsum <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</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;">frollsum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(T2M_MIN_anomaly <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, T2M_MIN_anomaly, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> location]</span>
<span id="cb11-8">combined_anomalies[, temp_min_neg_rollsum <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</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;">frollsum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(T2M_MIN_anomaly <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, T2M_MIN_anomaly, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> location]</span>
<span id="cb11-9">combined_anomalies[, prcp_pos_rollsum <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</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;">frollsum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(PRECTOTCORR_anomaly <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, PRECTOTCORR_anomaly, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> location]</span>
<span id="cb11-10">combined_anomalies[, prcp_neg_rollsum <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</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;">frollsum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(PRECTOTCORR_anomaly <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, PRECTOTCORR_anomaly, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> location]</span>
<span id="cb11-11"></span>
<span id="cb11-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a numeric day-of-year and adjust to form a 'cherry_year'</span></span>
<span id="cb11-13">combined_anomalies[, doy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</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;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>(YYYYMMDD, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%j"</span>))]</span>
<span id="cb11-14">combined_anomalies[, cherry_year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</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;">ifelse</span>(doy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">year</span>(YYYYMMDD) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">year</span>(YYYYMMDD))]</span>
<span id="cb11-15">combined_anomalies[, day_number <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</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;">ifelse</span>(doy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, doy <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;">99</span>, doy <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;">366</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;">99</span>))]</span>
<span id="cb11-16"></span>
<span id="cb11-17">small_anomaly_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> combined_anomalies <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-18">  <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><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(DOY, LON, LAT, YEAR, MM, DD, T2M, T2M_MAX, T2M_MIN, PRECTOTCORR,</span>
<span id="cb11-19">            T2M_mean, T2M_MAX_mean, T2M_MIN_mean, PRECTOTCORR_mean,</span>
<span id="cb11-20">            T2M_anomaly, T2M_MAX_anomaly, T2M_MIN_anomaly, PRECTOTCORR_anomaly))</span></code></pre></div>
</div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-6-contents" aria-controls="callout-6" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Reshape to wide format and impute missing values
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-6" class="callout-6-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="cell">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Example for positive temperature average anomalies - repeated for all 8 variables</span></span>
<span id="cb12-2">wide_temp_ave_pos <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;">dcast</span>(small_anomaly_df[<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>(temp_ave_pos_rollsum)], </span>
<span id="cb12-3">                           cherry_year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> location <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> day_number, </span>
<span id="cb12-4">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value.var =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp_ave_pos_rollsum"</span>)</span>
<span id="cb12-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setnames</span>(wide_temp_ave_pos, </span>
<span id="cb12-6">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">old =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setdiff</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(wide_temp_ave_pos), <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;">"cherry_year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"location"</span>)), </span>
<span id="cb12-7">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp_ave_pos_rollsum_"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setdiff</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(wide_temp_ave_pos), <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;">"cherry_year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"location"</span>)))))</span>
<span id="cb12-8"></span>
<span id="cb12-9">numeric_cols <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;">setdiff</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(wide_temp_ave_pos), <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;">"cherry_year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"location"</span>))</span>
<span id="cb12-10">numeric_matrix <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(wide_temp_ave_pos[, ..numeric_cols])</span>
<span id="cb12-11">imputed_result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">missForest</span>(numeric_matrix, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">maxiter =</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;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb12-12">imputed_matrix <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> imputed_result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ximp</span>
<span id="cb12-13">wide_temp_ave_pos_imputed <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;">cbind</span>(wide_temp_ave_pos[, .(cherry_year, location)], imputed_matrix)</span>
<span id="cb12-14"></span>
<span id="cb12-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Repeat for all 8 rolling sum variables...</span></span>
<span id="cb12-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># [Similar code blocks for temp_ave_neg, temp_max_pos, temp_max_neg, </span></span>
<span id="cb12-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  temp_min_pos, temp_min_neg, prcp_pos, prcp_neg]</span></span>
<span id="cb12-18"></span>
<span id="cb12-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Merge all wide-format datasets by cherry_year and location</span></span>
<span id="cb12-20">final_wide <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;">Reduce</span>(<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, y) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</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;">"cherry_year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"location"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb12-21">                     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(wide_temp_ave_pos_imputed, wide_temp_ave_neg_imputed, </span>
<span id="cb12-22">                          wide_temp_max_pos_imputed, wide_temp_max_neg_imputed,</span>
<span id="cb12-23">                          wide_temp_min_pos_imputed, wide_temp_min_neg_imputed,</span>
<span id="cb12-24">                          wide_prcp_pos_imputed, wide_prcp_neg_imputed))</span></code></pre></div>
</div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-7-contents" aria-controls="callout-7" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Merge with bloom dates
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-7" class="callout-7-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="cell">
<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">final_wide <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_tibble</span>(final_wide)</span>
<span id="cb13-2"></span>
<span id="cb13-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Map location names to match blossom data format</span></span>
<span id="cb13-4">location_mapping <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;">c</span>(</span>
<span id="cb13-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kyoto, Japan"</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;">"kyoto"</span>,</span>
<span id="cb13-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Liestal-Weideli, Switzerland"</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;">"liestal"</span>,</span>
<span id="cb13-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"New York, USA"</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;">"newyorkcity"</span>,</span>
<span id="cb13-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vancouver, Canada"</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;">"vancouver"</span>,</span>
<span id="cb13-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Washington DC, USA"</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;">"washingtondc"</span></span>
<span id="cb13-10">)</span>
<span id="cb13-11">final_wide<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location_mapped <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> location_mapping[final_wide<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location]</span>
<span id="cb13-12"></span>
<span id="cb13-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Merge with bloom dates</span></span>
<span id="cb13-14">final_wide <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;">merge</span>(</span>
<span id="cb13-15">  final_wide,</span>
<span id="cb13-16">  combined_blossom_dates[, <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;">"location"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bloom_doy"</span>)],</span>
<span id="cb13-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by.x =</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;">"location_mapped"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cherry_year"</span>),</span>
<span id="cb13-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by.y =</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;">"location"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>),</span>
<span id="cb13-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all.x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb13-20">)</span>
<span id="cb13-21"></span>
<span id="cb13-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reorder columns to put key variables first</span></span>
<span id="cb13-23">keep_vars <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cherry_year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"location"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bloom_doy"</span>)</span>
<span id="cb13-24">existing_keep_vars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> keep_vars[keep_vars <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;">colnames</span>(final_wide)]</span>
<span id="cb13-25">other_vars <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;">setdiff</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(final_wide), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(existing_keep_vars, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"location_mapped"</span>))</span>
<span id="cb13-26">final_wide <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> final_wide[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(existing_keep_vars, other_vars)]</span>
<span id="cb13-27"></span>
<span id="cb13-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save the final dataset</span></span>
<span id="cb13-29"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write.csv</span>(final_wide, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/final_wide_with_bloom.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</div>
</div>
</div>
</div>
<p>The final dataset contains 2,928 weather predictors (8 rolling sums × 366 days) aligned to our cherry year timeframe, plus the actual bloom dates. You can download the pre-processed data from <a href="data/final_wide_with_bloom.csv">final_wide_with_bloom.csv</a> or recreate it using the code above.</p>
<p>To use the pre-processed data in your analysis:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the pre-processed data</span></span>
<span id="cb14-2">final_wide <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;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/final_wide_with_bloom.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</div>
</section>
<section id="selecting-and-engineering-features-for-blossom-date-prediction-using-sparse-partial-least-squares" class="level3">
<h3 class="anchored" data-anchor-id="selecting-and-engineering-features-for-blossom-date-prediction-using-sparse-partial-least-squares">Selecting and Engineering features for blossom date prediction using sparse Partial Least Squares</h3>
<p>In this section, I use Sparse Partial Least Squares (SPLS) to identify key predictors and extract latent factors that summarize the high-dimensional weather data. SPLS is particularly well-suited for our case: we have nearly 3,000 highly correlated rolling-sum features and just a few hundred samples. This method not only reduces dimensionality but also performs variable selection by introducing a sparsity penalty—so we can focus on the most relevant time windows and weather patterns that drive blooming. I first convert our wide-format dataset into a matrix form and define predictors (X) and the response (Y, i.e., bloom day). Then, I run a grid search with 10-fold cross-validation to find the best combination of latent factor count (K) and sparsity level (eta). Once the optimal parameters are selected, I fit the SPLS model to the training data. This gives me both a sparse projection matrix and a subset of predictors contributing to each latent factor. Finally, I use the trained model to compute latent scores (<code>latent1</code>, <code>latent2</code>, etc.) for all observations—these represent condensed weather signatures that track with bloom timing and will be used for downstream modeling. For now, I’ll treat all locations as pooled, ignoring site-specific effects (which I’ll handle later with GAMs).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prepare data for SPLS</span></span>
<span id="cb15-2">my_data <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.data.frame</span>(final_wide)</span>
<span id="cb15-3">id_cols <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cherry_year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"location"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bloom_doy"</span>)</span>
<span id="cb15-4"></span>
<span id="cb15-5">predictor_cols <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;">setdiff</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(my_data), id_cols)</span>
<span id="cb15-6">train_data <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;">subset</span>(my_data, <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>(bloom_doy))</span>
<span id="cb15-7"></span>
<span id="cb15-8">X_train <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(train_data[, predictor_cols])</span>
<span id="cb15-9">Y_train <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> train_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bloom_doy</span>
<span id="cb15-10"></span>
<span id="cb15-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cross-validation for optimal parameters</span></span>
<span id="cb15-12"><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;">123</span>)</span>
<span id="cb15-13">cv.out <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;">cv.spls</span>(</span>
<span id="cb15-14">  X_train, </span>
<span id="cb15-15">  Y_train,</span>
<span id="cb15-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K   =</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;">10</span>,</span>
<span id="cb15-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>),</span>
<span id="cb15-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fold =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb15-19">)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>eta = 0.1 
eta = 0.2 
eta = 0.3 
eta = 0.4 
eta = 0.5 
eta = 0.6 
eta = 0.7 
eta = 0.8 
eta = 0.9 

Optimal parameters: eta = 0.3, K = 3</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">optimal_K   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cv.out<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>K.opt</span>
<span id="cb17-2">optimal_eta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cv.out<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>eta.opt</span>
<span id="cb17-3"></span>
<span id="cb17-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fit final SPLS model and extract latent factors</span></span>
<span id="cb17-5">final_model <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;">spls</span>(X_train, Y_train, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> optimal_K, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta =</span> optimal_eta)</span>
<span id="cb17-6">X_all <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(my_data[, predictor_cols])</span>
<span id="cb17-7">X_all_std <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;">sweep</span>(X_all, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, final_model<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>meanx, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>)</span>
<span id="cb17-8">X_all_std <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;">sweep</span>(X_all_std, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, final_model<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>normx, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>)</span>
<span id="cb17-9">X_all_sub <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> X_all_std[, final_model<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>A, 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="cb17-10">latent_all <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> X_all_sub <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> final_model<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>projection</span>
<span id="cb17-11"></span>
<span id="cb17-12">num_latent <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>(latent_all)</span>
<span id="cb17-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(latent_all) <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 class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"latent"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(num_latent))</span>
<span id="cb17-14"></span>
<span id="cb17-15">latent_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="cb17-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cherry_year =</span> my_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cherry_year,</span>
<span id="cb17-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">location    =</span> my_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location,</span>
<span id="cb17-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bloom_doy   =</span> my_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bloom_doy,</span>
<span id="cb17-19">  latent_all</span>
<span id="cb17-20">)</span></code></pre></div>
</div>
<p>So our optimization left us with 3 latent factors. Not bad. Lets as LLMs like to say delve deeper. ### Performance of our Sparse PLS model I first assess the performance of our Sparse PLS model using training data. I compute key statistics—such as R², RMSE, and MAE—to gauge how well the model explains the variance in the bloom day (with a higher R² indicating a better fit).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict on training data</span></span>
<span id="cb18-2">y_pred_train <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>(final_model, X_train)</span>
<span id="cb18-3">SST <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>((Y_train <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>(Y_train))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb18-4">SSE <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>((Y_train <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y_pred_train)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb18-5">R2_train <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;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> SSE<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>SST</span>
<span id="cb18-6"></span>
<span id="cb18-7">MAE_train <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;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(Y_train <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y_pred_train))</span>
<span id="cb18-8">RMSE_train <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;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((Y_train <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y_pred_train)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb18-9">n_selected_vars <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;">length</span>(final_model<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>A)</span>
<span id="cb18-10">percent_vars_selected <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (n_selected_vars <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;">length</span>(predictor_cols)) <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;">100</span></span>
<span id="cb18-11"></span>
<span id="cb18-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Diagnostic plot with unified styling</span></span>
<span id="cb18-13">stats_text <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="cb18-14">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R² = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(R2_train, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</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;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb18-15">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RMSE = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(RMSE_train, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" days</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb18-16">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MAE = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(MAE_train, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" days</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb18-17">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variables: "</span>, n_selected_vars, <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;">length</span>(predictor_cols), </span>
<span id="cb18-18">  <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;">round</span>(percent_vars_selected, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%)"</span></span>
<span id="cb18-19">)</span>
<span id="cb18-20"></span>
<span id="cb18-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">Actual =</span> Y_train, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Predicted =</span> y_pred_train), <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> Actual, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Predicted)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<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;">"#1f77b4"</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.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_abline</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;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slope =</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;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <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;">"#d62728"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(Y_train) <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;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(y_pred_train) <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;">10</span>, </span>
<span id="cb18-25">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> stats_text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</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;">vjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.2</span>, <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;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb18-27">    <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;">"Predicted vs Actual Bloom Day"</span>,</span>
<span id="cb18-28">    <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;">"Actual Bloom DOY"</span>,</span>
<span id="cb18-29">    <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;">"Predicted Bloom DOY"</span></span>
<span id="cb18-30">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb18-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb18-34">    <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;">"right"</span></span>
<span id="cb18-35">  )</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Not so shabby! Now lets examine the latent factors:</p>
</section>
<section id="latent-factor-interpretation" class="level3">
<h3 class="anchored" data-anchor-id="latent-factor-interpretation">Latent Factor Interpretation</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Parse the projection matrix from the SPLS model</span></span>
<span id="cb19-2">proj_mat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> final_model<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>projection</span>
<span id="cb19-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Each row name is in the form "temp_ave_pos_rollsum_123"</span></span>
<span id="cb19-4">parsed <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;">str_match</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(proj_mat), <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;">d+)$"</span>)</span>
<span id="cb19-5">group_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> parsed[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># e.g., "temp_ave_pos_rollsum"</span></span>
<span id="cb19-6">day_indices <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(parsed[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>])  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># day number</span></span>
<span id="cb19-7"></span>
<span id="cb19-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a data frame of loadings with associated group and day</span></span>
<span id="cb19-9">df_proj <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.data.frame</span>(proj_mat)</span>
<span id="cb19-10">df_proj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> group_names</span>
<span id="cb19-11">df_proj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>day   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> day_indices</span>
<span id="cb19-12"></span>
<span id="cb19-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rename factor columns for clarity</span></span>
<span id="cb19-14">n_fac <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>(proj_mat)</span>
<span id="cb19-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(df_proj)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_fac] <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 class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Factor"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n_fac))</span>
<span id="cb19-16"></span>
<span id="cb19-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pivot the data to long format: each row becomes (group, day, factor, loading)</span></span>
<span id="cb19-18">df_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;">pivot_longer</span>(df_proj, </span>
<span id="cb19-19">                        <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;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Factor"</span>),</span>
<span id="cb19-20">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factor"</span>,</span>
<span id="cb19-21">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"loading"</span>)</span>
<span id="cb19-22"></span>
<span id="cb19-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Build a complete grid for all groups, days (1:366), and factors</span></span>
<span id="cb19-24">all_groups <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp_ave_pos_rollsum"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp_ave_neg_rollsum"</span>, </span>
<span id="cb19-25">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp_max_pos_rollsum"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp_max_neg_rollsum"</span>, </span>
<span id="cb19-26">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp_min_pos_rollsum"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp_min_neg_rollsum"</span>, </span>
<span id="cb19-27">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prcp_pos_rollsum"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prcp_neg_rollsum"</span>)</span>
<span id="cb19-28">all_factors <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;">unique</span>(df_long<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>factor)</span>
<span id="cb19-29">grid_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;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> all_groups, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">day =</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;">366</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">factor =</span> all_factors, </span>
<span id="cb19-30">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb19-31"></span>
<span id="cb19-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Merge grid with loadings to include missing combinations as NA</span></span>
<span id="cb19-33">df_plot <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;">left_join</span>(grid_df, df_long, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</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;">"group"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"day"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factor"</span>))</span>
<span id="cb19-34"></span>
<span id="cb19-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert day index to a date for plotting; day 1 corresponds to April 10</span></span>
<span id="cb19-36">base_date <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;">"2020-04-10"</span>)</span>
<span id="cb19-37">df_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> base_date <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (df_plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>day <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="cb19-38"></span>
<span id="cb19-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract variable type and anomaly direction from group name</span></span>
<span id="cb19-40">df_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_plot <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb19-42">    <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;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_pos_rollsum.*|_neg_rollsum.*"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, group),</span>
<span id="cb19-43">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">anomaly =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_pos_"</span>, group), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pos"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"neg"</span>)</span>
<span id="cb19-44">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-45">  <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;">type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"temp_min"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp_ave"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp_max"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prcp"</span>)),</span>
<span id="cb19-46">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">anomaly =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(anomaly, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"pos"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"neg"</span>)))</span>
<span id="cb19-47"></span>
<span id="cb19-48"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Heatmap of latent factor loadings</span></span>
<span id="cb19-49"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df_plot, <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> anomaly, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> loading)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-51">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(type <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> factor) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-52">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_breaks =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2 months"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_labels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%b"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-53">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradient2</span>(</span>
<span id="cb19-54">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">low =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#313695"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mid =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">high =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#a50026"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">midpoint =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb19-55">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey90"</span>,</span>
<span id="cb19-56">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<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;">label_number</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0001</span>)</span>
<span id="cb19-57">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-58">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb19-59">    <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;">"Heatmap of SPLS Loadings by Factor and Variable Type"</span>,</span>
<span id="cb19-60">    <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 (within cherry year)"</span>,</span>
<span id="cb19-61">    <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;">"Anomaly Direction"</span>,</span>
<span id="cb19-62">    <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;">"Loading"</span></span>
<span id="cb19-63">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-64">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-65">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb19-66">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb19-67">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb19-68">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>)</span>
<span id="cb19-69">  )</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>These factors summarize the original rolling sum predictors, showing clear patterns. Negative temperature anomalies accumulated before the median bloom date load negatively; since these sums are negative, a larger accumulation of cold days actually delays blooming. In contrast, higher rolling sums of positive temperature anomalies—indicating warmer days—load negatively, which means that more warm days speed up the bloom. Additionally, the model captures a dormancy effect: a buildup of cold days in October and November tends to lead to an earlier bloom the following year. Similarly, lower-than-average precipitation starting in December is associated with earlier blooming. This interpretation confirms that the latent factors meaningfully reflect the influence of weather on cherry blossom timing.</p>
<p>Now let’s examine how these latent factors cluster by location.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cleaned and pivoted as before</span></span>
<span id="cb20-2">latent_clean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> latent_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"latent"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factor"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"score"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-4">  <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>(bloom_doy), <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>(score), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.finite</span>(bloom_doy), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.finite</span>(score))</span>
<span id="cb20-5"></span>
<span id="cb20-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(latent_clean, <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> bloom_doy, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> score, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> factor)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</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.6</span>, <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;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</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="cb20-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</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;">"dashed"</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.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</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="cb20-9">  </span>
<span id="cb20-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># wrap long facet labels at ~12 characters</span></span>
<span id="cb20-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(</span>
<span id="cb20-12">    location <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> factor,</span>
<span id="cb20-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>,</span>
<span id="cb20-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">switch  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>,</span>
<span id="cb20-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labeller =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labeller</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">location =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label_wrap_gen</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>))</span>
<span id="cb20-16">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-17">  </span>
<span id="cb20-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</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;">"Set1"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb20-20">    <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;">"Latent Factor Scores vs Bloom Day by Location"</span>,</span>
<span id="cb20-21">    <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;">"Bloom Day of Year"</span>,</span>
<span id="cb20-22">    <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;">"Latent Score"</span>,</span>
<span id="cb20-23">    <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;">"Latent Factor"</span></span>
<span id="cb20-24">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb20-27">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># move the y-strips to the left side</span></span>
<span id="cb20-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.placement        =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"outside"</span>,</span>
<span id="cb20-29">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># make left strip text horizontal and a bit smaller</span></span>
<span id="cb20-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text.y.left      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)),</span>
<span id="cb20-31">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># standard strip text for top</span></span>
<span id="cb20-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text             =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb20-33">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># tighten the panels</span></span>
<span id="cb20-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.spacing          =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>),</span>
<span id="cb20-35">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># legend at bottom</span></span>
<span id="cb20-36">    <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="cb20-37">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># bold title</span></span>
<span id="cb20-38">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title             =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb20-39">  )</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/index_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>Even though each latent factor shows an almost linear relationship with bloom day, you can see that the fitted lines for different sites sit at slightly different levels (and in some cases have different slopes). Those location-specific shifts tell us that bloom–weather relationships aren’t identical everywhere—so in the next step we’ll fit a GAM with separate smooths for each site to capture those nuances.</p>
</section>
<section id="dynamic-gam-modeling-for-forecasting" class="level3">
<h3 class="anchored" data-anchor-id="dynamic-gam-modeling-for-forecasting">Dynamic GAM Modeling for Forecasting</h3>
<p><strong>Dynamic GAM Modeling for Location-Specific Latent Factor Adaptation</strong></p>
<p>I now have three powerful latent factors from our SPLS analysis that capture the main weather patterns driving cherry blossom variability. However, these factors were derived as linear predictors from data pooled across all five locations, assuming each site responds identically to the same weather patterns.</p>
<p><strong>The problem:</strong>&nbsp;Real cherry trees don’t follow universal rules. Each location likely has its own unique sensitivity to weather - Kyoto’s cherry blossoms might respond differently to warming than Vancouver’s, and some locations have frustratingly sparse observations that make traditional modeling challenging.</p>
<p><strong>The approach:</strong>&nbsp;So I decided to go with <code>mvgam</code> <span class="citation" data-cites="mvgam">(Clark and Wells 2023)</span>. <code>mvgam</code> stands for MultiVariate (Dynamic) Generalized Additive Models and is specifically designed for time series analysis like ours:</p>
<ol type="1">
<li><p><strong>Time series focus</strong>: Specifically designed for time series prediction with built-in capabilities to model autocorrelation if needed.</p></li>
<li><p><strong>GAM flexibility</strong>: At its core, it’s still a GAM, making it ideal for bending our otherwise linear latent factors to each location individually.</p></li>
<li><p><strong>State-space robustness</strong>: The state-space modeling framework is perfect for time series with missing observations. It models the underlying bloom process separately from the observations, so our sparse New York and Vancouver data doesn’t break the model—missing data areas handled by modeling the latent bloom process as it evolves through time.</p></li>
<li><p><strong>Bayesian uncertainty</strong>: The Bayesian framework provides natural uncertainty quantification for both parameters and predictions.</p></li>
</ol>
<p><strong>Data preparation and model setup</strong></p>
<p>First, I split my data into training and testing sets at 2024 to ensure at least one bloom date for each location in our forecast evaluation. For state-space modeling, I need both&nbsp;<code>series</code>&nbsp;(to identify different time series) and&nbsp;<code>trend</code>&nbsp;(for the process formula) variables.</p>
<p>My model structure uses shrinkage smooths (<code>bs="sz"</code>) to handle the location-specific responses. For example,&nbsp;<code>s(latent1, k=3)</code>&nbsp;creates the main smooth effect of latent1, while&nbsp;<code>s(trend, latent1, bs="sz", k=3)</code>&nbsp;adds location-specific deviations. The “sz” basis applies shrinkage - locations with sparse data have their specific effects shrunk toward zero, essentially borrowing the response pattern from data-rich locations. Meanwhile, Kyoto and others with complete records can maintain their unique response curves.</p>
<p>Let’s prepare the data and fit the model:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> latent_df</span>
<span id="cb22-2">df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location <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.factor</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location)</span>
<span id="cb22-3">df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>series <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Keep series for identifying time series</span></span>
<span id="cb22-4">df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trend <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add trend for trend_formula</span></span>
<span id="cb22-5">df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cherry_year <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;">1981</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#For numerical stability</span></span>
<span id="cb22-6"></span>
<span id="cb22-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Split data into training (&lt;2024) and testing (&gt;=2024)</span></span>
<span id="cb22-8">train_mvgam <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df[df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cherry_year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2024</span>, ]</span>
<span id="cb22-9">test_mvgam  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df[df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cherry_year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2024</span>, ]</span>
<span id="cb22-10"></span>
<span id="cb22-11"></span>
<span id="cb22-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Now fit with tweaked priors</span></span>
<span id="cb22-13">model_gaussian <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;">mvgam</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> bloom_doy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> series,</span>
<span id="cb22-14">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trend_formula =</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;">s</span>(latent1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</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;">s</span>(trend, latent1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sz"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-15">                           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(latent2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</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;">s</span>(trend, latent2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sz"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-16">                           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(latent3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</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;">s</span>(trend, latent3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sz"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb22-17">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> train_mvgam,</span>
<span id="cb22-18">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> test_mvgam,</span>
<span id="cb22-19">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">noncentred =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb22-20">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gaussian</span>(),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">silent =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
</div>
<p>Let’s examine the model diagnostics:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(model_gaussian, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include_betas =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>GAM observation formula:
bloom_doy ~ series

GAM process formula:
~s(latent1, k = 3) + s(trend, latent1, bs = "sz", k = 3) + s(latent2, 
    k = 3) + s(trend, latent2, bs = "sz", k = 3) + s(latent3, 
    k = 3) + s(trend, latent3, bs = "sz", k = 3)

Family:
gaussian

Link function:
identity

Trend model:
None

N process models:
5 

N series:
5 

N timepoints:
45 

Status:
Fitted using Stan 
4 chains, each with iter = 1000; warmup = 500; thin = 1 
Total post-warmup draws = 2000


Observation error parameter estimates:
             2.5%  50% 97.5% Rhat n_eff
sigma_obs[1] 0.98 2.30   3.1 1.08    50
sigma_obs[2] 2.50 3.50   4.6 1.02   209
sigma_obs[3] 5.70 8.30  14.0 1.00   996
sigma_obs[4] 0.16 0.95   5.2 1.00  1024
sigma_obs[5] 1.10 3.40   4.3 1.08    37

GAM observation model coefficient (beta) estimates:
                                   2.5%   50% 97.5% Rhat n_eff
(Intercept)                        89.0 94.00  99.0 1.00  1271
seriesLiestal-Weideli, Switzerland -5.0 -1.80   1.1 1.00  1047
seriesNew York, USA                -2.6  0.82   4.7 1.00  1492
seriesVancouver, Canada            -4.3 -1.30   1.4 1.00  1147
seriesWashington DC, USA           -5.6 -1.70   2.1 1.01   593

Process error parameter estimates:
           2.5%  50% 97.5% Rhat n_eff
sigma[1] 0.0069 0.48   2.1 1.07    54
sigma[2] 0.0170 0.41   2.3 1.02   156
sigma[3] 0.0150 0.34   1.9 1.00  1205
sigma[4] 0.0110 0.50   2.0 1.00   959
sigma[5] 0.0190 0.44   3.2 1.09    35

GAM process model coefficient (beta) estimates:
                  2.5%   50% 97.5% Rhat n_eff
(Intercept)_trend -5.1 0.016   4.8    1  1350

Approximate significance of GAM process smooths:
                   edf Ref.df  Chi.sq p-value    
s(latent1)        1.03      2 4723.06 &lt; 2e-16 ***
s(series,latent1) 7.18     12  132.36    0.37    
s(latent2)        1.02      2  640.80 &lt; 2e-16 ***
s(series,latent2) 3.75     12    2.03    1.00    
s(latent3)        0.99      2  326.97 7.1e-06 ***
s(series,latent3) 1.22     12    2.26    1.00    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Stan MCMC diagnostics:
✔ No issues with effective samples per iteration
✖ Rhats above 1.05 found for some parameters
    Use pairs() and mcmc_plot() to investigate
✖ 132 of 2000 iterations ended with a divergence (6.6%)
    Try a larger adapt_delta to remove divergences
✔ No issues with maximum tree depth

Samples were drawn using sampling(hmc). For each parameter, n_eff is a
  crude measure of effective sample size, and Rhat is the potential scale
  reduction factor on split MCMC chains (at convergence, Rhat = 1)

Use how_to_cite() to get started describing this model</code></pre>
</div>
</div>
<p>The model shows severe convergence issues. Several variance parameters (sigmas) from both the observation and process models are poorly identified, with terrible effective sample sizes and Rhat values. Let me unpack what’s happening here. In our state-space formulation:</p>
<p>The baseline: Without weather effects, each location would bloom on roughly the same day every year (captured by the location intercepts) The process model: Our SPLS-derived latent factors predict deviations from this baseline. The process error represents additional year-to-year variation not captured by weather - perhaps soil conditions, tree age, or other unmeasured factors The observation model: This is where bloom dates actually get recorded. The observation error represents how much our weather-based predictions miss the true bloom dates</p>
<p>The identifiability crisis occurs because the model can explain the same bloom variability in multiple ways. High process error + low observation error? Low process error + high observation error? Without constraints, the model can’t decide. Here’s the key insight: Our SPLS model achieved good R² and MAE when fitted on pooled data from all locations. This means it captures the “average” weather-bloom relationship well, but likely misses location-specific nuances by roughly the same amount everywhere. The observation error is essentially quantifying “how wrong is our one-size-fits-all weather model?” By setting share_obs_params = TRUE, I enforce this logic: all locations share the same observation variance because they all use the same pooled SPLS model. This constraint breaks the identifiability problem while still allowing location-specific process errors to capture unique temporal patterns. Additionally, with 19% of iterations ending in divergences, I’ll increase adapt_delta to 0.99, max_treedepth to 12, and number of iterations for more careful exploration of the posterior:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">model_shared_sz <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;">mvgam</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> bloom_doy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> series,</span>
<span id="cb25-2">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trend_formula =</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;">s</span>(latent1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</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;">s</span>(trend, latent1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sz"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-3">                           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(latent2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</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;">s</span>(trend, latent2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sz"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-4">                           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(latent3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</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;">s</span>(trend, latent3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sz"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb25-5">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> train_mvgam,</span>
<span id="cb25-6">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> test_mvgam,</span>
<span id="cb25-7">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">share_obs_params =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb25-8">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">noncentred =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb25-9">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control =</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;">adapt_delta =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_treedepth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>),</span>
<span id="cb25-10">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">burnin =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb25-11">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">samples =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>,</span>
<span id="cb25-12">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gaussian</span>(),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">silent =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb25-13"></span>
<span id="cb25-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(model_shared_sz, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include_betas =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>GAM observation formula:
bloom_doy ~ series

GAM process formula:
~s(latent1, k = 3) + s(trend, latent1, bs = "sz", k = 3) + s(latent2, 
    k = 3) + s(trend, latent2, bs = "sz", k = 3) + s(latent3, 
    k = 3) + s(trend, latent3, bs = "sz", k = 3)

Family:
gaussian

Link function:
identity

Trend model:
None

N process models:
5 

N series:
5 

N timepoints:
45 

Status:
Fitted using Stan 
4 chains, each with iter = 2500; warmup = 500; thin = 1 
Total post-warmup draws = 8000


Observation error parameter estimates:
          2.5% 50% 97.5% Rhat n_eff
sigma_obs  2.7 3.2   3.7    1  2401

GAM observation model coefficient (beta) estimates:
                                   2.5%   50% 97.5% Rhat n_eff
(Intercept)                        89.0 94.00  99.0    1  3503
seriesLiestal-Weideli, Switzerland -5.1 -1.60   1.6    1  4643
seriesNew York, USA                -2.4  0.81   4.5    1  4682
seriesVancouver, Canada            -4.8 -0.85   2.6    1  9138
seriesWashington DC, USA           -5.7 -1.30   2.5    1  4568

Process error parameter estimates:
          2.5%  50% 97.5% Rhat n_eff
sigma[1] 0.012 0.26   1.1    1  7048
sigma[2] 0.016 0.47   2.2    1  2022
sigma[3] 2.100 4.50   6.8    1  2254
sigma[4] 0.013 0.34   1.8    1 11527
sigma[5] 0.016 0.44   2.0    1  2386

GAM process model coefficient (beta) estimates:
                  2.5%    50% 97.5% Rhat n_eff
(Intercept)_trend -5.1 -0.036   5.1    1  3722

Approximate significance of GAM process smooths:
                   edf Ref.df  Chi.sq p-value    
s(latent1)        1.01      2 4085.94 &lt; 2e-16 ***
s(series,latent1) 7.64     12  239.55    0.18    
s(latent2)        1.09      2  655.87 &lt; 2e-16 ***
s(series,latent2) 1.97     12    2.81    1.00    
s(latent3)        1.01      2  305.76 1.7e-05 ***
s(series,latent3) 2.12     12    1.27    1.00    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Stan MCMC diagnostics:
✔ No issues with effective samples per iteration
✔ Rhat looks good for all parameters
✔ No issues with divergences
✔ No issues with maximum tree depth

Samples were drawn using sampling(hmc). For each parameter, n_eff is a
  crude measure of effective sample size, and Rhat is the potential scale
  reduction factor on split MCMC chains (at convergence, Rhat = 1)

Use how_to_cite() to get started describing this model</code></pre>
</div>
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">gratia<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;">draw</span>(model_shared_sz, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trend_effects=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>The enhanced sampling and shared observation parameters completely fixed our convergence issues. All parameters now have excellent effective sample sizes and Rhat values of 1.00. The shared observation error settled at about 3.2 days (pretty much as MAE of joint SPLS model) - this represents how much our weather-based predictions typically miss the actual bloom dates. Looking at the smooth effects, the main patterns are beautifully linear, exactly as expected from our SPLS-derived factors. Each latent factor shows a strong linear relationship with bloom timing (all p &lt; 0.001). The location-specific deviations (s(series,latent) terms) aren’t statistically significant, which isn’t surprising given our limited data. However, they still capture subtle location-specific responses that contribute to more accurate predictions. Lets look at series themselves:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(model_shared_sz, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"forecast"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trend_effects =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">series=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/index_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Out of sample CRPS:
0.817677993584371</code></pre>
</div>
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(model_shared_sz, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"forecast"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trend_effects =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">series=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/index_files/figure-html/unnamed-chunk-18-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Out of sample CRPS:
3.94968940550937</code></pre>
</div>
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(model_shared_sz, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"forecast"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trend_effects =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">series=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/index_files/figure-html/unnamed-chunk-18-3.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Out of sample CRPS:
1.3673925550625</code></pre>
</div>
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(model_shared_sz, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"forecast"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trend_effects =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">series=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/index_files/figure-html/unnamed-chunk-18-4.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Out of sample CRPS:
5.40008450334061</code></pre>
</div>
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(model_shared_sz, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"forecast"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trend_effects =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">series=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/index_files/figure-html/unnamed-chunk-18-5.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Out of sample CRPS:
2.59631972186406</code></pre>
</div>
</div>
<p>Pretty groowy. So lets now run predictions for 2025 and compare with real data.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Forecast</span></span>
<span id="cb38-2">fc <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;">forecast</span>(model_shared_sz, test_mvgam)</span>
<span id="cb38-3"></span>
<span id="cb38-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Map full location names to short names for predictions</span></span>
<span id="cb38-5">location_mapping <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;">c</span>(</span>
<span id="cb38-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kyoto, Japan"</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;">"kyoto"</span>,</span>
<span id="cb38-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Liestal-Weideli, Switzerland"</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;">"liestal"</span>,</span>
<span id="cb38-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"New York, USA"</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;">"newyorkcity"</span>,</span>
<span id="cb38-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vancouver, Canada"</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;">"vancouver"</span>,</span>
<span id="cb38-10">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Washington DC, USA"</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;">"washingtondc"</span></span>
<span id="cb38-11">)</span>
<span id="cb38-12"></span>
<span id="cb38-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create dataframe with actual 2025 bloom dates</span></span>
<span id="cb38-14">actual_2025 <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="cb38-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">location =</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;">"liestal"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vancouver"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kyoto"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"washingtondc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"newyorkcity"</span>),</span>
<span id="cb38-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">actual_date =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</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;">"2025-03-27"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-04-03"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-04-04"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-03-28"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-04-04"</span>)),</span>
<span id="cb38-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">actual_doy =</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;">86</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">93</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">94</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">87</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">94</span>),</span>
<span id="cb38-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb38-19">)</span>
<span id="cb38-20"></span>
<span id="cb38-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract posterior draws for all locations</span></span>
<span id="cb38-22">posterior_draws <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="cb38-23"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(fc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>series_names)) {</span>
<span id="cb38-24">  full_name <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.character</span>(fc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>series_names[i])</span>
<span id="cb38-25">  short_name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> location_mapping[full_name]</span>
<span id="cb38-26">  posterior_draws[[short_name]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>forecasts[[i]][, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2025 predictions</span></span>
<span id="cb38-27">}</span>
<span id="cb38-28"></span>
<span id="cb38-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert to long format for plotting</span></span>
<span id="cb38-30">draws_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="cb38-31">  <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;">unlist</span>(posterior_draws),</span>
<span id="cb38-32">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">location =</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;">names</span>(posterior_draws), <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;">length</span>(posterior_draws[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]))</span>
<span id="cb38-33">)</span>
<span id="cb38-34"></span>
<span id="cb38-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add actual values</span></span>
<span id="cb38-36">draws_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;">merge</span>(draws_df, actual_2025[, <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;">"location"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"actual_doy"</span>)], <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;">"location"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all.x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb38-37"></span>
<span id="cb38-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add proper location names</span></span>
<span id="cb38-39">draws_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location_full <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;">factor</span>(draws_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>location, </span>
<span id="cb38-40">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"kyoto"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"liestal"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"newyorkcity"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vancouver"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"washingtondc"</span>),</span>
<span id="cb38-41">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</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;">"Kyoto, Japan"</span>, </span>
<span id="cb38-42">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Liestal, Switzerland"</span>, </span>
<span id="cb38-43">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"New York City, USA"</span>, </span>
<span id="cb38-44">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vancouver, Canada"</span>, </span>
<span id="cb38-45">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Washington DC, USA"</span>))</span>
<span id="cb38-46"></span>
<span id="cb38-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate summary statistics with rounded differences</span></span>
<span id="cb38-48">summary_stats <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> draws_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb38-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(location_full, actual_doy) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb38-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb38-51">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">median_pred =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(value),</span>
<span id="cb38-52">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_pred =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(value),</span>
<span id="cb38-53">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'drop'</span></span>
<span id="cb38-54">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb38-55">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb38-56">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diff =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(median_pred <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> actual_doy, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb38-57">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diff_text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb38-58">      diff <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</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;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+"</span>, diff, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" days"</span>),</span>
<span id="cb38-59">      diff <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</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;">paste0</span>(diff, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" days"</span>),</span>
<span id="cb38-60">      <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;">"Perfect!"</span></span>
<span id="cb38-61">    )</span>
<span id="cb38-62">  )</span>
<span id="cb38-63"></span>
<span id="cb38-64"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create the plot</span></span>
<span id="cb38-65"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define a unified fill palette (you can swap in your custom hexes if you prefer)</span></span>
<span id="cb38-66">palette5 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> RColorBrewer<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;">brewer.pal</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set1"</span>)</span>
<span id="cb38-67"></span>
<span id="cb38-68"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clean data if needed (optional defensive step)</span></span>
<span id="cb38-69">draws_df_clean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> draws_df <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;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.finite</span>(value))</span>
<span id="cb38-70">summary_stats_clean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> summary_stats <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;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.finite</span>(actual_doy))</span>
<span id="cb38-71"></span>
<span id="cb38-72">p_presentation <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;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-73">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Density curves</span></span>
<span id="cb38-74">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(</span>
<span id="cb38-75">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data    =</span> draws_df_clean,</span>
<span id="cb38-76">    <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> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> location_full),</span>
<span id="cb38-77">    <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.8</span>,</span>
<span id="cb38-78">    <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;">"white"</span>,</span>
<span id="cb38-79">    <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="cb38-80">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm   =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb38-81">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-82">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Vertical line at actual bloom date</span></span>
<span id="cb38-83">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(</span>
<span id="cb38-84">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> summary_stats_clean,</span>
<span id="cb38-85">    <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> actual_doy, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> actual_doy, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>),</span>
<span id="cb38-86">    <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;">"black"</span>,</span>
<span id="cb38-87">    <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;">1.2</span></span>
<span id="cb38-88">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-89">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Label with error text</span></span>
<span id="cb38-90">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>(</span>
<span id="cb38-91">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> summary_stats_clean,</span>
<span id="cb38-92">    <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> actual_doy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(diff <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Spot on!"</span>, diff_text)),</span>
<span id="cb38-93">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust         =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>,</span>
<span id="cb38-94">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size          =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>,</span>
<span id="cb38-95">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface      =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>,</span>
<span id="cb38-96">    <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;">"white"</span>,</span>
<span id="cb38-97">    <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.9</span>,</span>
<span id="cb38-98">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.padding =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>),</span>
<span id="cb38-99">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.size    =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>,</span>
<span id="cb38-100">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm         =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb38-101">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-102">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Facet by location</span></span>
<span id="cb38-103">  <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> location_full, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>, <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;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-104">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unified fill palette</span></span>
<span id="cb38-105">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> palette5) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-106">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># X axis limits and breaks</span></span>
<span id="cb38-107">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">112</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-108">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<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;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">110</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;">10</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-109">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Labels</span></span>
<span id="cb38-110">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb38-111">    <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;">"2025 Cherry Blossom Predictions vs Reality"</span>,</span>
<span id="cb38-112">    <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;">"Day of Year</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(vertical line = actual bloom date)"</span>,</span>
<span id="cb38-113">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y     =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<span id="cb38-114">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-115">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unified theme</span></span>
<span id="cb38-116">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-117">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb38-118">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <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;">16</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb38-119">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <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;">11</span>),</span>
<span id="cb38-120">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<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;">9</span>),</span>
<span id="cb38-121">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb38-122">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks.y     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb38-123">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_line</span>(<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;">"grey85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>),</span>
<span id="cb38-124">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb38-125">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.spacing    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>),</span>
<span id="cb38-126">    <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;">"none"</span>,</span>
<span id="cb38-127">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.margin      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb38-128">  )</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument instead.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(p_presentation)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/index_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>Two perfect predictions (Kyoto and Vancouver), and the rest within a week - not bad for a model predicting a complex biological phenomenon months in advance. What started as a challenge to predict cherry blossoms using weather anomalies has demonstrated the power of combining dimensional reduction (SPLS) with flexible modeling (mvgam). The approach successfully handled the twin challenges of high-dimensional weather data and sparse observations, delivering predictions with well-calibrated uncertainty. Most satisfying is that the locations with the least data (New York and Vancouver) performed just as well as data-rich sites - proof that our shrinkage approach effectively borrowed strength across locations. Cherry trees may not follow universal rules, but with the right modeling framework, I can still capture their unique responses to weather patterns</p>
<div class="cell">
<div class="cell-output-display">
<p>We used R version 4.5.1 <span class="citation" data-cites="base">(R Core Team 2025)</span> and the following R packages: data.table v. 1.17.8 <span class="citation" data-cites="datatable">(Barrett et al. 2025)</span>, gratia v. 0.10.0 <span class="citation" data-cites="gratia">(Simpson 2024)</span>, missForest v. 1.5 <span class="citation" data-cites="missForest2012 missForest2022">(Stekhoven and Buehlmann 2012; Stekhoven 2022)</span>, mvgam v. 1.1.51 <span class="citation" data-cites="mvgam">(Clark and Wells 2023)</span>, nasapower v. 4.2.5 <span class="citation" data-cites="nasapower">(Sparks 2018)</span>, RColorBrewer v. 1.1.3 <span class="citation" data-cites="RColorBrewer">(Neuwirth 2022)</span>, rmarkdown v. 2.29 <span class="citation" data-cites="rmarkdown2018 rmarkdown2020 rmarkdown2024">(Xie, Allaire, and Grolemund 2018; Xie, Dervieux, and Riederer 2020; Allaire et al. 2024)</span>, scales v. 1.4.0 <span class="citation" data-cites="scales">(Wickham, Pedersen, and Seidel 2025)</span>, spls v. 2.3.2 <span class="citation" data-cites="spls">(Chung, Chun, and Keles 2025)</span>, tidyverse v. 2.0.0 <span class="citation" data-cites="tidyverse">(Wickham et al. 2019)</span>.</p>
</div>
</div>



</section>
</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-rmarkdown2024" class="csl-entry">
Allaire, JJ, Yihui Xie, Christophe Dervieux, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, et al. 2024. <em><span class="nocase">rmarkdown</span>: Dynamic Documents for r</em>. <a href="https://github.com/rstudio/rmarkdown">https://github.com/rstudio/rmarkdown</a>.
</div>
<div id="ref-datatable" class="csl-entry">
Barrett, Tyson, Matt Dowle, Arun Srinivasan, Jan Gorecki, Michael Chirico, Toby Hocking, Benjamin Schwendinger, and Ivan Krylov. 2025. <em><span class="nocase">data.table</span>: Extension of <span>“<span class="nocase">data.frame</span>”</span></em>. <a href="https://doi.org/10.32614/CRAN.package.data.table">https://doi.org/10.32614/CRAN.package.data.table</a>.
</div>
<div id="ref-spls" class="csl-entry">
Chung, Dongjun, Hyonho Chun, and Sunduz Keles. 2025. <em><span class="nocase">spls</span>: Sparse Partial Least Squares (SPLS) Regression and Classification</em>. <a href="https://doi.org/10.32614/CRAN.package.spls">https://doi.org/10.32614/CRAN.package.spls</a>.
</div>
<div id="ref-mvgam" class="csl-entry">
Clark, Nicholas J, and Konstans Wells. 2023. <span>“Dynamic Generalized Additive Models (DGAMs) for Forecasting Discrete Ecological Time Series.”</span> <em>Methods in Ecology and Evolution</em> 14: 771–84. <a href="https://doi.org/10.18637/jss.v100.i05">https://doi.org/10.18637/jss.v100.i05</a>.
</div>
<div id="ref-RColorBrewer" class="csl-entry">
Neuwirth, Erich. 2022. <em><span>RColorBrewer</span>: ColorBrewer Palettes</em>. <a href="https://doi.org/10.32614/CRAN.package.RColorBrewer">https://doi.org/10.32614/CRAN.package.RColorBrewer</a>.
</div>
<div id="ref-base" class="csl-entry">
R Core Team. 2025. <em><span>R</span>: A Language and Environment for Statistical Computing</em>. Vienna, Austria: R Foundation for Statistical Computing. <a href="https://www.R-project.org/">https://www.R-project.org/</a>.
</div>
<div id="ref-gratia" class="csl-entry">
Simpson, Gavin L. 2024. <em><span class="nocase">gratia</span>: Graceful <span class="nocase">ggplot</span>-Based Graphics and Other Functions for <span>GAM</span>s Fitted Using <span class="nocase">mgcv</span></em>. <a href="https://gavinsimpson.github.io/gratia/">https://gavinsimpson.github.io/gratia/</a>.
</div>
<div id="ref-nasapower" class="csl-entry">
Sparks, Adam H. 2018. <span>“<span class="nocase">nasapower</span>: A NASA POWER Global Meteorology, Surface Solar Energy and Climatology Data Client for r.”</span> <em>The Journal of Open Source Software</em> 3 (30): 1035. <a href="https://doi.org/10.21105/joss.01035">https://doi.org/10.21105/joss.01035</a>.
</div>
<div id="ref-missForest2022" class="csl-entry">
Stekhoven, Daniel J. 2022. <em><span class="nocase">missForest</span>: Nonparametric Missing Value Imputation Using Random Forest</em>.
</div>
<div id="ref-missForest2012" class="csl-entry">
Stekhoven, Daniel J., and Peter Buehlmann. 2012. <span>“MissForest - Non-Parametric Missing Value Imputation for Mixed-Type Data.”</span> <em>Bioinformatics</em> 28 (1): 112–18.
</div>
<div id="ref-tidyverse" class="csl-entry">
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. <span>“Welcome to the <span class="nocase">tidyverse</span>.”</span> <em>Journal of Open Source Software</em> 4 (43): 1686. <a href="https://doi.org/10.21105/joss.01686">https://doi.org/10.21105/joss.01686</a>.
</div>
<div id="ref-scales" class="csl-entry">
Wickham, Hadley, Thomas Lin Pedersen, and Dana Seidel. 2025. <em><span class="nocase">scales</span>: Scale Functions for Visualization</em>. <a href="https://doi.org/10.32614/CRAN.package.scales">https://doi.org/10.32614/CRAN.package.scales</a>.
</div>
<div id="ref-rmarkdown2018" class="csl-entry">
Xie, Yihui, J. J. Allaire, and Garrett Grolemund. 2018. <em>R Markdown: The Definitive Guide</em>. Boca Raton, Florida: Chapman; Hall/CRC. <a href="https://bookdown.org/yihui/rmarkdown">https://bookdown.org/yihui/rmarkdown</a>.
</div>
<div id="ref-rmarkdown2020" class="csl-entry">
Xie, Yihui, Christophe Dervieux, and Emily Riederer. 2020. <em>R Markdown Cookbook</em>. Boca Raton, Florida: Chapman; Hall/CRC. <a href="https://bookdown.org/yihui/rmarkdown-cookbook">https://bookdown.org/yihui/rmarkdown-cookbook</a>.
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>Phenology</category>
  <category>Time Series Analysis</category>
  <category>Machine Learning</category>
  <guid>https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/</guid>
  <pubDate>Mon, 07 Jul 2025 07:00:00 GMT</pubDate>
  <media:content url="https://jbogomolovas2.github.io/Julius-s-Blog/posts/cherry_post/blossom.png" medium="image" type="image/png" height="96" width="144"/>
</item>
<item>
  <title>Swimming speed Analysis With Generalized Additive Models</title>
  <dc:creator>Julius Bogomolovas</dc:creator>
  <link>https://jbogomolovas2.github.io/Julius-s-Blog/posts/post-with-code/</link>
  <description><![CDATA[ 





<p>As a swimmer seeking to enhance performance, tracking workout data can offer valuable insights into progress over time. Analysis options for swimming compared to running in Garmin are instead week. I enjoy swimming and data analysis. In this analysis, I’ll explore several years of swimming data extracted from Garmin FIT files to figure out if I am getting faster.&nbsp;</p>
<section id="data-extraction-from-fit-files" class="level2">
<h2 class="anchored" data-anchor-id="data-extraction-from-fit-files">Data Extraction from FIT Files</h2>
<p>First, I’ll extract swimming data from all available FIT files. The <code>FITfileR</code> <span class="citation" data-cites="FITfileR">(Smith 2025)</span> package allows us to read Garmin workout files and identify pool swimming sessions.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(FITfileR)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(utils)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(hexbin)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(viridis)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gratia)</span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mgcv)</span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb1-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(performance)</span>
<span id="cb1-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lubridate)</span></code></pre></div>
</div>
<section id="identifying-pool-swimming-sessions" class="level3">
<h3 class="anchored" data-anchor-id="identifying-pool-swimming-sessions">Identifying Pool Swimming Sessions</h3>
<p>So, one can ask Garmin to download all the files. Swim session or activity files are stored in .fit format and located in the DI_CONNECT folder, specifically in the subfolder DI-Connect-Uploaded-Files. File names are rather cryptical, so I first look for files that have “lap_swimming” attribute within.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scan directory for all .fit files</span></span>
<span id="cb2-2">all_files <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.files</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/Users/jb/Documents/BW_analysis/Swim"</span>,</span>
<span id="cb2-3">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pattern    =</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;">.fit$"</span>,</span>
<span id="cb2-4">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check each file to identify pool swimming sessions</span></span>
<span id="cb2-7">n  <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;">length</span>(all_files)</span>
<span id="cb2-8">ok <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;">logical</span>(n)</span>
<span id="cb2-9">pb <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;">txtProgressBar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(all_files)) {</span>
<span id="cb2-12">  path <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> all_files[i]</span>
<span id="cb2-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Safely check if file contains pool swimming data</span></span>
<span id="cb2-14">  ok[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">possibly</span>(<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p) {</span>
<span id="cb2-15">    sp <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;">getMessagesByType</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readFitFile</span>(p), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sport"</span>)</span>
<span id="cb2-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">isTRUE</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">any</span>(sp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sport <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;">"swimming"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> sp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sub_sport <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;">"lap_swimming"</span>,</span>
<span id="cb2-17">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb2-18">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">otherwise =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)(path)</span>
<span id="cb2-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setTxtProgressBar</span>(pb, i)</span>
<span id="cb2-20">}</span>
<span id="cb2-21"></span>
<span id="cb2-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">close</span>(pb)</span>
<span id="cb2-23"></span>
<span id="cb2-24">pool_lap_fits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> all_files[ok]</span>
<span id="cb2-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</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;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Found"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(pool_lap_fits), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pool-lap swim files</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
</div>
</section>
<section id="extracting-session-information" class="level3">
<h3 class="anchored" data-anchor-id="extracting-session-information">Extracting Session Information</h3>
<p>Now, by examining several <code>lap_swimming</code> attributes containing files, I found that pool length in meters and activity data are stored in <code>session</code>, and <code>length</code> contains the workout information.&nbsp;</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">sessions <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;">map_dfr</span>(pool_lap_fits, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(f) {</span>
<span id="cb3-2">  sess <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;">tryCatch</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getMessagesByType</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readFitFile</span>(f), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"session"</span>),</span>
<span id="cb3-3">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb3-4">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.null</span>(sess)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb3-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file          =</span> f,</span>
<span id="cb3-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">session_date  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(sess<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>start_time),</span>
<span id="cb3-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pool_length   =</span> sess<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pool_length</span>
<span id="cb3-9">  )</span>
<span id="cb3-10">}) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(session_date) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-12">  <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;">session_number =</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;">%&gt;%</span></span>
<span id="cb3-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb3-14"></span>
<span id="cb3-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract individual length (lap) data</span></span>
<span id="cb3-16">raw_laps <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;">map_dfr</span>(pool_lap_fits, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(f) {</span>
<span id="cb3-17">  lengths <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;">tryCatch</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getMessagesByType</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readFitFile</span>(f), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"length"</span>),</span>
<span id="cb3-18">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb3-19">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.null</span>(lengths)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb3-20">  lengths <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;">file =</span> f)</span>
<span id="cb3-21">})</span></code></pre></div>
</div>
</section>
</section>
<section id="data-processing-and-set-detection" class="level2">
<h2 class="anchored" data-anchor-id="data-processing-and-set-detection">Data Processing and Set Detection</h2>
<p>Swimming workouts typically consist of multiple sets with rest periods between each set. I’ll identify these sets and calculate the relative positions of each lap within the workout and set itself:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Identify sets based on idle periods</span></span>
<span id="cb4-2">laps_with_sets <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> raw_laps <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(file) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb4-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lap_index          =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row_number</span>(),</span>
<span id="cb4-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total_laps         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(),</span>
<span id="cb4-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pos_within_workout =</span> lap_index <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> total_laps,</span>
<span id="cb4-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New set starts after each idle period</span></span>
<span id="cb4-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">set_id             =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(length_type <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;">"idle"</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="cb4-10">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb4-12"></span>
<span id="cb4-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Process active laps only and calculate set-specific metrics</span></span>
<span id="cb4-14">final_tbl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> laps_with_sets <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(length_type <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;">"active"</span>, swim_stroke <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;">"drill"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(file, set_id) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb4-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lap_in_set     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row_number</span>(),</span>
<span id="cb4-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">set_size       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(),</span>
<span id="cb4-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pos_within_set =</span> lap_in_set <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> set_size</span>
<span id="cb4-21">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(sessions, <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;">"file"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(</span>
<span id="cb4-25">    session_date,</span>
<span id="cb4-26">    session_number,</span>
<span id="cb4-27">    pool_length,</span>
<span id="cb4-28">    total_elapsed_time,</span>
<span id="cb4-29">    set_id,</span>
<span id="cb4-30">    lap_index,</span>
<span id="cb4-31">    lap_in_set,</span>
<span id="cb4-32">    pos_within_workout,</span>
<span id="cb4-33">    pos_within_set,</span>
<span id="cb4-34">    swim_stroke</span>
<span id="cb4-35">  )</span>
<span id="cb4-36"></span>
<span id="cb4-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save processed data</span></span>
<span id="cb4-38"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write.csv</span>(final_tbl, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"all_swim_laps.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
</div>
<p>You can load it directly in <code>R</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">final_tbl <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;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://jbogomolovas2.github.io/julius-blog/posts/post-with-code/all_swim_laps.csv"</span>)</span></code></pre></div>
</div>
<p>I’ll identify gaps in training and analyze data from July 2020 onward to have a nice, recent, and consistent training segment to analyze.</p>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Identify training gaps</span></span>
<span id="cb6-2">sessions_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> final_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(session_date) <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;">arrange</span>(session_date)</span>
<span id="cb6-5"></span>
<span id="cb6-6">gaps_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sessions_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1) make sure session_date is a Date</span></span>
<span id="cb6-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;">session_date =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(session_date)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2) grab the “previous” date</span></span>
<span id="cb6-10">  <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;">prev_date =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lag</span>(session_date)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3) compute gap in days</span></span>
<span id="cb6-12">  <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;">gap_days =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">difftime</span>(session_date, prev_date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">units =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"days"</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4) drop the first row (where lag was NA)</span></span>
<span id="cb6-14">  <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>(gap_days))</span>
<span id="cb6-15"></span>
<span id="cb6-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Show top 5 largest training gaps</span></span>
<span id="cb6-17">top5_gaps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gaps_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(gap_days)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</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;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(</span>
<span id="cb6-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end_date   =</span> session_date,</span>
<span id="cb6-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_date =</span> prev_date</span>
<span id="cb6-23">  )</span>
<span id="cb6-24"></span>
<span id="cb6-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(top5_gaps)</span></code></pre></div>
</div>
<p>Let’s perform some data cleaning: select a segment, calculate swim speeds, remove super-fast laps (I wish they were real!), and create a simple time variable.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">final_tbl_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> final_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1) ensure session_date is Date</span></span>
<span id="cb7-3">  <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;">session_date =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(session_date)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-4">  </span>
<span id="cb7-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2) filter from July 7, 2020 onward</span></span>
<span id="cb7-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(session_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;">ymd</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-07-07"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-7">  </span>
<span id="cb7-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3) compute speed and factor columns</span></span>
<span id="cb7-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb7-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">speed       =</span> pool_length <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> total_elapsed_time,</span>
<span id="cb7-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">swim_stroke =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(swim_stroke),</span>
<span id="cb7-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">session_id  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(session_date)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># or combine with session_number if you need uniqueness</span></span>
<span id="cb7-13">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-14">  </span>
<span id="cb7-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4) remove unrealistically fast laps</span></span>
<span id="cb7-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(speed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-17">  </span>
<span id="cb7-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5) sort and compute days since first session</span></span>
<span id="cb7-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(session_date) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb7-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">days_since_start =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(</span>
<span id="cb7-22">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">difftime</span>(session_date,</span>
<span id="cb7-23">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(session_date),</span>
<span id="cb7-24">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">units =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"days"</span>)</span>
<span id="cb7-25">    )</span>
<span id="cb7-26">  )</span></code></pre></div>
</div>
</section>
<section id="visualizing-speed-distributions" class="level2">
<h2 class="anchored" data-anchor-id="visualizing-speed-distributions">Visualizing Speed Distributions</h2>
<p>Let’s examine how swimming speed varies over time for different strokes. As I have a bunch of laps, let’s plot them as densities.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">p_contour <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;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> final_tbl_2020, </span>
<span id="cb8-3">             <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> session_date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> speed),</span>
<span id="cb8-4">             <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.05</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <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;">"grey50"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density_2d</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> final_tbl_2020, </span>
<span id="cb8-6">                  <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> session_date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> speed),</span>
<span id="cb8-7">                  <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;">"darkblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</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.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-8">  <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> swim_stroke, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</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="cb8-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Session Date"</span>,</span>
<span id="cb8-10">       <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;">"Speed (m/s)"</span>,</span>
<span id="cb8-11">       <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;">"Swimming Speed Over Time by Stroke Type"</span>,</span>
<span id="cb8-12">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density contours showing speed distribution"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-13">  <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="cb8-14">  <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;">"none"</span>,</span>
<span id="cb8-15">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb8-16">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<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;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>))</span>
<span id="cb8-17"></span>
<span id="cb8-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(p_contour)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/post-with-code/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="statistical-modeling-with-gams" class="level2">
<h2 class="anchored" data-anchor-id="statistical-modeling-with-gams">Statistical Modeling with GAMs</h2>
<p>I have something that is more or less monomodal. Therefore, my average swim speed would be a good indicator of my progress. However, in workouts, I do different sets, such as sprints, cooldowns, etc. Which vary in intensity, length, and position within the workout. I hypothesize that these factors significantly affect “average” swim speed and need to be accounted for. I don’t expect them to be linearly related, so I enter the magical world of Generalized Additive Models (GAMs). I will be using <code>mgcv</code> <span class="citation" data-cites="mgcv">(Wood 2011)</span>&nbsp;for fitting, <code>gratia</code> <span class="citation" data-cites="gratia">(Simpson 2024)</span>&nbsp;for visualization, and <code>performance</code>&nbsp;<span class="citation" data-cites="performance">(Lüdecke et al. 2021)</span>&nbsp;for model assessment. I will start with a simple&nbsp;speed ~ time&nbsp;model and try to add covariates to get a better fit, closer to “average” swim speed.&nbsp;As I am working with a rather large dataset, I will use&nbsp;the <code>bam</code>&nbsp;function and a few corresponding tweaks:&nbsp;<code>discrete = TRUE</code>&nbsp;and&nbsp;<code>method = "fREML"</code>&nbsp;which enables faster computation for extensive datasets. By examining the swimming speed distribution, I clearly see some tails, so I will use the scaled t-distribution&nbsp;<code>family = scat()</code>.&nbsp;</p>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model 1: Simple time trend by stroke</span></span>
<span id="cb9-2">simple <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;">bam</span>(</span>
<span id="cb9-3">    speed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> swim_stroke <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pool_length <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb9-4">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(days_since_start, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> swim_stroke, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gp"</span>),</span>
<span id="cb9-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data     =</span> final_tbl_2020,</span>
<span id="cb9-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">discrete =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb9-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scat</span>(),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scaled t-distribution for robustness</span></span>
<span id="cb9-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method   =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fREML"</span></span>
<span id="cb9-9">)</span>
<span id="cb9-10"></span>
<span id="cb9-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model 2: Add position effects (fatigue within workout/set)</span></span>
<span id="cb9-12">set_location <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;">bam</span>(</span>
<span id="cb9-13">    speed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> swim_stroke <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pool_length <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb9-14">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(days_since_start, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> swim_stroke, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gp"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb9-15">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(pos_within_workout, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> swim_stroke) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-16">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(pos_within_set, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> swim_stroke),</span>
<span id="cb9-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data     =</span> final_tbl_2020,</span>
<span id="cb9-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">discrete =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb9-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scat</span>(),</span>
<span id="cb9-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method   =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fREML"</span></span>
<span id="cb9-21">)</span>
<span id="cb9-22"></span>
<span id="cb9-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model 3: Add session random effects</span></span>
<span id="cb9-24">set_location_re <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;">bam</span>(</span>
<span id="cb9-25">    speed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> swim_stroke <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pool_length <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb9-26">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(days_since_start, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> swim_stroke, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gp"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-27">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(pos_within_workout, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> swim_stroke) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-28">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(pos_within_set, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> swim_stroke) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-29">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">s</span>(session_id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"re"</span>),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Random effect for sessions</span></span>
<span id="cb9-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data     =</span> final_tbl_2020,</span>
<span id="cb9-31">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">discrete =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb9-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scat</span>(),</span>
<span id="cb9-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method   =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fREML"</span></span>
<span id="cb9-34">)</span>
<span id="cb9-35"></span>
<span id="cb9-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compare model performance</span></span>
<span id="cb9-37">model_comparison <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;">compare_performance</span>(simple, set_location, set_location_re)</span>
<span id="cb9-38"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(model_comparison)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># Comparison of Model Performance Indices

Name            | Model |    AIC (weights) |   AICc (weights)
-------------------------------------------------------------
simple          |   bam | -60982.5 (&lt;.001) | -60982.4 (&lt;.001)
set_location    |   bam | -78903.1 (&lt;.001) | -78902.5 (&lt;.001)
set_location_re |   bam | -81777.9 (&gt;.999) | -81765.5 (&gt;.999)

Name            |    BIC (weights) |    R2 |  RMSE | Sigma
----------------------------------------------------------
simple          | -60631.1 (&lt;.001) | 0.204 | 0.123 | 1.000
set_location    | -78017.1 (&gt;.999) | 0.449 | 0.101 | 1.000
set_location_re | -77560.3 (&lt;.001) | 0.475 | 0.099 | 1.000</code></pre>
</div>
<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">model_summary<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;">summary</span>(set_location_re)</span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(model_summary)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Family: Scaled t(3.729,0.064) 
Link function: identity 

Formula:
speed ~ swim_stroke + pool_length + s(days_since_start, by = swim_stroke, 
    bs = "gp") + s(pos_within_workout, by = swim_stroke) + s(pos_within_set, 
    by = swim_stroke) + s(session_id, bs = "re")

Parametric coefficients:
                          Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept)              0.8914292  0.0049522  180.01   &lt;2e-16 ***
swim_strokebreaststroke  0.0613416  0.0052787   11.62   &lt;2e-16 ***
swim_strokebutterfly     0.2525302  0.0047424   53.25   &lt;2e-16 ***
swim_strokefreestyle     0.2141422  0.0031635   67.69   &lt;2e-16 ***
pool_length             -0.0017971  0.0001487  -12.09   &lt;2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Approximate significance of smooth terms:
                                                  edf  Ref.df        F  p-value
s(days_since_start):swim_strokebackstroke       6.088   6.963    7.448  &lt; 2e-16
s(days_since_start):swim_strokebreaststroke     5.747   6.583    4.031 0.000274
s(days_since_start):swim_strokebutterfly        7.962   8.641    7.520  &lt; 2e-16
s(days_since_start):swim_strokefreestyle        8.047   8.390    7.933  &lt; 2e-16
s(pos_within_workout):swim_strokebackstroke     2.495   3.100    4.059 0.006375
s(pos_within_workout):swim_strokebreaststroke   7.311   8.292   20.984  &lt; 2e-16
s(pos_within_workout):swim_strokebutterfly      6.725   7.816   12.175  &lt; 2e-16
s(pos_within_workout):swim_strokefreestyle      8.375   8.888  312.136  &lt; 2e-16
s(pos_within_set):swim_strokebackstroke         8.232   8.778  163.531  &lt; 2e-16
s(pos_within_set):swim_strokebreaststroke       8.347   8.804  514.321  &lt; 2e-16
s(pos_within_set):swim_strokebutterfly          8.474   8.888  500.872  &lt; 2e-16
s(pos_within_set):swim_strokefreestyle          8.838   8.990 1051.382  &lt; 2e-16
s(session_id)                                 390.899 449.000    8.486  &lt; 2e-16
                                                 
s(days_since_start):swim_strokebackstroke     ***
s(days_since_start):swim_strokebreaststroke   ***
s(days_since_start):swim_strokebutterfly      ***
s(days_since_start):swim_strokefreestyle      ***
s(pos_within_workout):swim_strokebackstroke   ** 
s(pos_within_workout):swim_strokebreaststroke ***
s(pos_within_workout):swim_strokebutterfly    ***
s(pos_within_workout):swim_strokefreestyle    ***
s(pos_within_set):swim_strokebackstroke       ***
s(pos_within_set):swim_strokebreaststroke     ***
s(pos_within_set):swim_strokebutterfly        ***
s(pos_within_set):swim_strokefreestyle        ***
s(session_id)                                 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

R-sq.(adj) =  0.475   Deviance explained = 42.7%
fREML =  65732  Scale est. = 1         n = 39572</code></pre>
</div>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">appraise</span>(set_location_re)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/post-with-code/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Based on model comparison, the most complex one seems to be doing the best.&nbsp; Some sanity check: indeed, backstroke is my slowest stroke, whereas butterfly is the fastest, and swimming in a 50 m pool vs.&nbsp;a 25-yard pool slows me a bit. In terms of fixed effects, the model effectively captured them.&nbsp; Smooth terms revealed significant nonlinear trends over time for all strokes, even when accounted for lap temporal position and session as a random factor. So let’s have a look.&nbsp;</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">gratia<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;">draw</span>(set_location_re,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">select=</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;">4</span> )</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/post-with-code/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>I am getting faster with my strokes, but I am plateauing with my freestyle. Let’s create predictions from our best model and overlay them on the actual data:&nbsp;</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create prediction dataset</span></span>
<span id="cb15-2">pred_data <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;">expand.grid</span>(</span>
<span id="cb15-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">days_since_start =</span> <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>(final_tbl_2020<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>days_since_start), </span>
<span id="cb15-4">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(final_tbl_2020<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>days_since_start), </span>
<span id="cb15-5">                        <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;">200</span>),</span>
<span id="cb15-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">swim_stroke =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(final_tbl_2020<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>swim_stroke),</span>
<span id="cb15-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb15-8">)</span>
<span id="cb15-9"></span>
<span id="cb15-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set other variables to median/reference values</span></span>
<span id="cb15-11">pred_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pool_length <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;">min</span>(final_tbl_2020<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pool_length) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#most of my workouts are in 25 yards</span></span>
<span id="cb15-12">pred_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pos_within_workout <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;">median</span>(final_tbl_2020<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pos_within_workout)</span>
<span id="cb15-13">pred_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pos_within_set <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;">median</span>(final_tbl_2020<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pos_within_set)</span>
<span id="cb15-14">pred_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>session_id <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;">levels</span>(final_tbl_2020<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>session_id)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb15-15"></span>
<span id="cb15-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate predictions (excluding random effects)</span></span>
<span id="cb15-17">predictions <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>(set_location_re, </span>
<span id="cb15-18">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> pred_data, </span>
<span id="cb15-19">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">exclude =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s(session_id)"</span>,</span>
<span id="cb15-20">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se.fit =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb15-21"></span>
<span id="cb15-22">pred_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> predictions<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit</span>
<span id="cb15-23">pred_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>se <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> predictions<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>se.fit</span>
<span id="cb15-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Validation plot</span></span>
<span id="cb15-25">p_validation <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;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-26">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scatter with very low alpha for context</span></span>
<span id="cb15-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> final_tbl_2020, </span>
<span id="cb15-28">             <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> days_since_start, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> speed),</span>
<span id="cb15-29">             <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.05</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <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;">"grey50"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-30">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2D density contour lines</span></span>
<span id="cb15-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density_2d</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> final_tbl_2020, </span>
<span id="cb15-32">                  <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> days_since_start, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> speed),</span>
<span id="cb15-33">                  <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;">"darkblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-34">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model smooth lines only - no CI</span></span>
<span id="cb15-35">  <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;">data =</span> pred_data,</span>
<span id="cb15-36">            <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> days_since_start, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> fit),</span>
<span id="cb15-37">            <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;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-38">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Facet by stroke type</span></span>
<span id="cb15-39">  <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> swim_stroke, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</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="cb15-40">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Labels and theme</span></span>
<span id="cb15-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<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;">"Days Since Start"</span>,</span>
<span id="cb15-42">       <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;">"Speed (m/s)"</span>,</span>
<span id="cb15-43">       <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;">"Swimming Speed Over Time by Stroke Type"</span>,</span>
<span id="cb15-44">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density contours with GAM smooth curves"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-45">  <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="cb15-46">  <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;">"none"</span>,</span>
<span id="cb15-47">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb15-48">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<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;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>))</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(p_validation)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://jbogomolovas2.github.io/Julius-s-Blog/posts/post-with-code/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Although structured test sets would be ideal for tracking progress, even this messy lap-level data can already provide some insights. Overall, the model indicates that my backstroke, breaststroke, and butterfly are improving (which I have noticed myself), while freestyle still lags behind. Extending the analysis to quartiles or deciles of lap speeds should provide a more comprehensive picture of training progress and pacing strategies.</p>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-performance" class="csl-entry">
Lüdecke, Daniel, Mattan S. Ben-Shachar, Indrajeet Patil, Philip Waggoner, and Dominique Makowski. 2021. <span>“<span></span>Performance<span></span>: An <span></span>r<span></span> Package for Assessment, Comparison and Testing of Statistical Models”</span> 6: 3139. <a href="https://doi.org/10.21105/joss.03139">https://doi.org/10.21105/joss.03139</a>.
</div>
<div id="ref-gratia" class="csl-entry">
Simpson, Gavin L. 2024. <span>“<span></span>Gratia<span></span>: Graceful <span></span>Ggplot<span></span>-Based Graphics and Other Functions for <span></span>GAM<span></span>s Fitted Using <span></span>Mgcv<span></span>.”</span> <a href="https://gavinsimpson.github.io/gratia/">https://gavinsimpson.github.io/gratia/</a>.
</div>
<div id="ref-FITfileR" class="csl-entry">
Smith, Mike. 2025. <span>“FITfileR: Read FIT Files Using Only Native r Code.”</span> <a href="https://github.com/grimbough/FITfileR.git">https://github.com/grimbough/FITfileR.git</a>.
</div>
<div id="ref-mgcv" class="csl-entry">
Wood, S. N. 2011. <span>“Fast Stable Restricted Maximum Likelihood and Marginal Likelihood Estimation of Semiparametric Generalized Linear Models”</span> 73. <a href="https://doi.org/10.1111/j.1467-9868.2010.00749.x">https://doi.org/10.1111/j.1467-9868.2010.00749.x</a>.
</div>
</div></section></div> ]]></description>
  <category>R</category>
  <category>GAM</category>
  <category>Sports Analytics</category>
  <category>Data Visualization</category>
  <guid>https://jbogomolovas2.github.io/Julius-s-Blog/posts/post-with-code/</guid>
  <pubDate>Mon, 23 Jun 2025 07:00:00 GMT</pubDate>
  <media:content url="https://jbogomolovas2.github.io/Julius-s-Blog/posts/post-with-code/image.jpg" medium="image" type="image/jpeg"/>
</item>
</channel>
</rss>
