<?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>Emily Miller&#39;s Blog</title>
<link>https://rellimylime.github.io/blog.html</link>
<atom:link href="https://rellimylime.github.io/blog.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.8.23</generator>
<lastBuildDate>Sat, 28 Mar 2026 07:00:00 GMT</lastBuildDate>
<item>
  <title>Hyperspectral Hyperawareness</title>
  <dc:creator>Emily Miller</dc:creator>
  <link>https://rellimylime.github.io/posts/hyperspectral-imaging/</link>
  <description><![CDATA[ 





<section id="the-question" class="level2">
<h2 class="anchored" data-anchor-id="the-question">The question</h2>
<p>This post came out of a spring-break learning project. I had the very specific feeling that I knew enough remote sensing to recognize what hyperspectral imagery was <em>for</em>, but not enough to open a cube from scratch and trust my own interpretation. So I did what I usually do when something seems learnable: opened too many tabs, downloaded a benchmark scene, and worked from the data upward.</p>
<p>What follows is the tutorial I wish I had at the start. It is not a full hyperspectral textbook, and it is not trying to be. The goal is narrower and more honest: get from “I understand satellite imagery and false color composites” to “I can load a hyperspectral cube, inspect spectra, and run a few standard decomposition methods without treating the output like magic.”</p>
<p>If you work with remote sensing imagery, you already know the usual move: choose a few bands, stretch them well, and interpret the resulting composite. Even a false-color image, though, still compresses the scene down to a small number of broad spectral channels.</p>
<p>Hyperspectral imagery changes that. Instead of 3 broad RGB bands, or even a modest multispectral stack, it records hundreds of narrow, contiguous wavelength channels. In the HYDICE Urban scene used here, each pixel carries a 210-value spectrum spanning the visible through the shortwave infrared.</p>
<p>That full spectral curve is what makes hyperspectral analysis different from standard image interpretation. Vegetation, asphalt, bare ground, rooftops, and metal surfaces can overlap in brightness or color in a broad-band composite while still differing strongly in spectral shape. The question stops being only “what does this object look like in a display image?” and becomes “what spectral signature is present in this pixel, and what does that imply about materials?”</p>
<p>So the real question in this post is: <strong>given a full spectrum at each pixel, can we separate the scene into meaningful material patterns without labels?</strong></p>
<p>Three approaches, each with a different way of framing that problem:</p>
<ol type="1">
<li><strong>Principal component analysis (PCA)</strong>: What are the dominant directions of spectral variance in the scene? PCA is the compression view. It asks how much of the 210-band signal can be summarized with a small number of orthogonal patterns.</li>
<li><strong>Linear spectral unmixing</strong>: If we already know what a few pure materials look like, what fraction of each material is present in a given pixel? This is the fraction view, closest to the classic subpixel remote-sensing problem.</li>
<li><strong>Non-negative matrix factorization (NMF)</strong>: Can we learn additive, material-like spectral patterns directly from the data, without supplying endmembers ahead of time? This is the discovery view.</li>
</ol>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>Remote-sensing intuition.</strong> A false-color composite asks: which three bands should I display? Hyperspectral analysis asks a different question: what can I learn if I use the <em>entire</em> spectral curve at each pixel instead of just a few display bands?</p>
</div>
</div>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">PCA: Variance View</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Unmixing: Fraction View</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">NMF: Parts View</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<p>Use PCA when you want the strongest spectral contrasts in the scene, whether or not they map cleanly onto physical materials. It is especially useful for compression, QA, dimensionality reduction, and anomaly screening.</p>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p>Use linear unmixing when you already have plausible endmembers and want outputs that read like material fractions. This is the most direct fit to the remote-sensing question of mixed pixels and subpixel composition.</p>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<p>Use NMF when you want interpretable additive components but do not yet trust a fixed endmember library. It often sits between purely mathematical compression and fully supervised unmixing.</p>
</div>
</div>
</div>
</section>
<section id="the-data" class="level2">
<h2 class="anchored" data-anchor-id="the-data">The data</h2>
<p>We’ll use <strong>HYDICE Urban</strong> <span class="citation" data-cites="erdc_hypercube">(U.S. Army Engineer Research and Development Center 2004)</span>, a classic hyperspectral benchmark from the U.S. Army Engineer Research and Development Center. It is a 307 x 307 pixel urban scene with 210 bands spanning roughly 400-2500 nm, at about 2 m ground sampling distance.</p>
<p>It is a useful teaching scene because it mixes vegetation, roads, rooftops, bare ground, and shadow in a compact area. If you already have intuition for urban false-color imagery, this dataset is a natural next step: the same scene elements are there, but now we can analyze their full spectral behavior rather than only a few displayed bands.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>Getting the data.</strong> Download the URBAN sample ZIP from the <a href="https://www.erdc.usace.army.mil/Media/Fact-Sheets/Fact-Sheet-Article-View/Article/610433/hypercube/">ERDC HyperCube page</a>. Leave it zipped and place it at <code>data/HYDICE_Urban/Urban.zip</code> relative to this post, the loading code extracts it to a temporary directory automatically.</p>
<p>To run the code cells yourself, change <code>eval: false</code> to <code>eval: true</code> in the document YAML. All figures in this post were pre-generated and committed.</p>
</div>
</div>
<p>If you want a more widely used imaging-spectroscopy benchmark, the same ideas also apply to AVIRIS Moffett Field <span class="citation" data-cites="aviris_moffett">(Jet Propulsion Laboratory 1992)</span>, a 224-band scene over water, vegetation, and urban land near Mountain View, California. The workflow is almost the same; you mainly change the file path and, if needed, mask a few strong water-absorption bands.</p>
<section id="loading-the-cube" class="level3">
<h3 class="anchored" data-anchor-id="loading-the-cube">Loading the cube</h3>
<p>This is a sensor-native style product: a binary cube plus a few small metadata files. The main data file holds the radiance values, while the wavelength file tells us which wavelength corresponds to each band. If you have worked with ENVI-style remote-sensing products before, the layout will feel familiar.</p>
<p>The <code>.wvl</code> file is plain text, so we read it directly from the ZIP archive with Python’s <code>zipfile</code> module:</p>
<div id="load-data" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">ZIP_PATH <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> DATA_DIR <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Urban.zip"</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Parse wavelengths from the .wvl file (tab-separated, \r line endings, col 1 = nm)</span></span>
<span id="cb1-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> zipfile.ZipFile(ZIP_PATH) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> zf:</span>
<span id="cb1-5">    wvl_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">next</span>(n <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> zf.namelist() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> n.endswith(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".wvl"</span>))</span>
<span id="cb1-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> zf.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(wvl_name) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb1-7">        lines <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f.read().decode(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ascii"</span>).replace(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\r</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>).splitlines()</span>
<span id="cb1-8">        wavelengths <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(l.split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\t</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)[<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;">for</span> l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> lines <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> l.strip()])</span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The .hdr is a custom HyperCube format, not standard ENVI, so we read the binary directly.</span></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Header specifies: big-endian int16, BIL interleave, 307 samples x 307 lines x 210 bands.</span></span>
<span id="cb1-12"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> zipfile.ZipFile(ZIP_PATH) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> zf:</span>
<span id="cb1-13">    data_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">next</span>(n <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> zf.namelist() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> n.endswith((<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;">".hdr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".wvl"</span>)))</span>
<span id="cb1-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> zf.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(data_name) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb1-15">        raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.frombuffer(f.read(), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>np.dtype(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&gt;i2"</span>))</span>
<span id="cb1-16"></span>
<span id="cb1-17">H, W, B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">307</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">307</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">210</span></span>
<span id="cb1-18">cube <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> raw.reshape(H, B, W).transpose(<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;">0</span>, <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;"># BIL: lines x bands x samples -&gt; bands x rows x cols</span></span>
<span id="cb1-19"></span>
<span id="cb1-20"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Cube shape:       </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cube<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb1-21"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Wavelength range: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>wavelengths[<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;">:.0f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> - </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>wavelengths[<span class="op" 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;">:.0f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> nm"</span>)</span>
<span id="cb1-22"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Dtype: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cube<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>dtype<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, Min: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cube<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, Max: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cube<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</div>
<p>The cube shape <code>(B, H, W)</code> means:</p>
<ul>
<li>first index = wavelength band</li>
<li>second index = row</li>
<li>third index = column</li>
</ul>
<p>That layout is convenient because it makes both views of the data easy: one band at a time as an image, or one pixel at a time as a spectrum.</p>
</section>
<section id="a-note-on-radiance-vs.-reflectance" class="level3">
<h3 class="anchored" data-anchor-id="a-note-on-radiance-vs.-reflectance">A note on radiance vs.&nbsp;reflectance</h3>
<p>This dataset stores <strong>radiance</strong>, which is the light measured by the sensor. It does <strong>not</strong> store surface reflectance.</p>
<p>That distinction matters. Reflectance is usually easier to interpret as a surface property, because it is closer to “what fraction of incoming light did the target reflect?” Radiance still contains the material signal, but it also includes illumination geometry and atmospheric effects.</p>
<p>The relationship between the two is approximately:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Crho%20%5Capprox%20%5Cfrac%7B%5Cpi%20%5C,%20L%20%5C,%20d%5E2%7D%7BE_0%20%5Ccos(%5Ctheta_%7BSZ%7D)%7D"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?L"> is at-sensor radiance, <img src="https://latex.codecogs.com/png.latex?E_0"> is top-of-atmosphere solar irradiance for that band, <img src="https://latex.codecogs.com/png.latex?d"> is the Earth-Sun distance in AU, and <img src="https://latex.codecogs.com/png.latex?%5Ctheta_%7BSZ%7D"> is the solar zenith angle <span class="citation" data-cites="nasa_seadas">(NASA Ocean Biology Processing Group 2023)</span>. Atmospheric-correction workflows go further than this simple relation and try to remove path radiance so the result is closer to true surface reflectance.</p>
<p>For this demo, radiance is fine. The decomposition math is the same either way. The main difference is interpretation: if you want to compare across dates, sensors, or spectral libraries, reflectance is the more physically stable quantity.</p>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Important
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>Interpretation boundary.</strong> The outputs below are still useful on radiance, but they should not be read as scene-invariant material signatures. For library matching, cross-scene comparison, or retrieval tasks, surface reflectance is the safer input.</p>
</div>
</div>
</section>
<section id="rgb-composite" class="level3">
<h3 class="anchored" data-anchor-id="rgb-composite">RGB composite</h3>
<p>To make a normal-looking color image, we pick one band near red, one near green, and one near blue. Choosing them by wavelength instead of by hard-coded band number makes the code easier to reuse on other datasets:</p>
<div id="rgb-composite" class="cell" data-fig-show="hide" data-execution_count="3">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> nearest_band(wavelengths: np.ndarray, target_nm: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>:</span>
<span id="cb2-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Return the band index closest to target_nm."""</span></span>
<span id="cb2-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>(np.argmin(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(wavelengths <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> target_nm)))</span>
<span id="cb2-4"></span>
<span id="cb2-5">R     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nearest_band(wavelengths, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">650</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># red</span></span>
<span id="cb2-6">G     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nearest_band(wavelengths, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">550</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># green</span></span>
<span id="cb2-7">B_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nearest_band(wavelengths, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">470</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># blue</span></span>
<span id="cb2-8"></span>
<span id="cb2-9">rgb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cube[[R, G, B_idx], :, :].astype(np.float32).transpose(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb2-10">lo, hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(rgb, [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">98</span>])</span>
<span id="cb2-11">rgb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.clip((rgb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lo) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lo), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-12"></span>
<span id="cb2-13">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</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;">6</span>))</span>
<span id="cb2-14">ax.imshow(rgb)</span>
<span id="cb2-15">ax.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HYDICE Urban: RGB composite (650 / 550 / 470 nm)"</span>, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb2-16">ax.axis(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>)</span>
<span id="cb2-17">plt.tight_layout()</span>
<span id="cb2-18">plt.savefig(FIG_DIR <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rgb_composite.png"</span>, bbox_inches<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tight"</span>)</span>
<span id="cb2-19">plt.show()</span></code></pre></div></div>
</details>
</div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/hyperspectral-imaging/figures/rgb_composite.png" alt="RGB composite of the HYDICE Urban scene." style="display:block; margin:0 auto; width:70%;" class="figure-img"></p>
<figcaption>
RGB composite of the HYDICE Urban scene.
</figcaption>
</figure>
</section>
<section id="pixel-spectra" class="level3">
<h3 class="anchored" data-anchor-id="pixel-spectra">Pixel spectra</h3>
<p>A pixel spectrum is the basic object in hyperspectral analysis: one pixel, plotted across wavelength. If two surfaces are made of different materials, their spectral shapes often differ even when they look similar in a composite image.</p>
<p>Here are four hand-picked examples from recognizable surfaces in the RGB image:</p>
<div id="pixel-spectra" class="cell" data-fig-show="hide" data-execution_count="4">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">pixels <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb3-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Asphalt road"</span>:  (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>),</span>
<span id="cb3-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Grass lawn"</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;">200</span>),</span>
<span id="cb3-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Metal rooftop"</span>: (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>,  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>),</span>
<span id="cb3-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Treed area"</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;">150</span>),</span>
<span id="cb3-6">}</span>
<span id="cb3-7"></span>
<span id="cb3-8">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb3-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> label, (row, col) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> pixels.items():</span>
<span id="cb3-10">    spectrum <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cube[:, row, col].astype(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)</span>
<span id="cb3-11">    ax.plot(wavelengths, spectrum, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>label, linewidth<span class="op" 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">ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wavelength (nm)"</span>)</span>
<span id="cb3-14">ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Radiance (DN)"</span>)</span>
<span id="cb3-15">ax.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Example pixel spectra - HYDICE Urban"</span>)</span>
<span id="cb3-16">ax.legend(frameon<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span>
<span id="cb3-17">plt.tight_layout()</span>
<span id="cb3-18">plt.savefig(FIG_DIR <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pixel_spectra.png"</span>, bbox_inches<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tight"</span>)</span>
<span id="cb3-19">plt.show()</span></code></pre></div></div>
</details>
</div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/hyperspectral-imaging/figures/pixel_spectra.png" alt="Spectra from four hand-picked pixels. Even in raw radiance, each material type has a recognizably distinct shape across wavelength." style="display:block; margin:0 auto; width:85%;" class="figure-img"></p>
<figcaption>
Spectra from four hand-picked pixels. Even in raw radiance, each material type has a recognizably distinct shape across wavelength.
</figcaption>
</figure>
<p>Even without atmospheric correction, these curves separate cleanly. Grass shows the familiar vegetation red edge, low values in the red and a strong rise into the near-infrared. Asphalt is flatter. Rooftops are brighter and less vegetation-like. Those differences are exactly the signal the decomposition methods below try to isolate.</p>
</section>
</section>
<section id="from-cube-to-matrix" class="level2">
<h2 class="anchored" data-anchor-id="from-cube-to-matrix">From cube to matrix</h2>
<p>All three methods below want the data in matrix form rather than cube form, so we flatten the image:</p>
<p><img src="https://latex.codecogs.com/png.latex?X%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BN%20%5Ctimes%20B%7D,%20%5Cquad%20N%20=%20H%20%5Ccdot%20W"></p>
<p>Each row is one pixel spectrum. Each column is one wavelength band. We have not changed the data itself, only its shape.</p>
<div id="reshape" class="cell" data-execution_count="5">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cube.astype(np.float64).reshape(B, H <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> W).T  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># shape: (N, B)</span></span>
<span id="cb4-2">N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Pixel matrix X: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>X<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>shape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (94249, 210)</span></span></code></pre></div></div>
</div>
<p>This is more than bookkeeping. Once the data are in this form, each pixel becomes one point in a 210-dimensional spectral space, and the methods below look for structure in that cloud of points.</p>
</section>
<section id="pca-svd" class="level2">
<h2 class="anchored" data-anchor-id="pca-svd">PCA / SVD</h2>
<section id="derivation" class="level3">
<h3 class="anchored" data-anchor-id="derivation">Derivation</h3>
<p>PCA looks for a small number of directions that explain most of the variation in the data. For a remote-sensing reader, one good way to think about it is this: if you had to summarize each 210-band spectrum with just a few spectral contrasts, which contrasts would matter most?</p>
<p>Mathematically, PCA is SVD applied to the mean-centered data. “Mean-centered” just means we subtract the average spectrum first:</p>
<p><img src="https://latex.codecogs.com/png.latex?X_c%20=%20X%20-%20%5Cmathbf%7B1%7D%5Cmu%5ET,%20%5Cquad%20%5Cmu%20=%20%5Cfrac%7B1%7D%7BN%7D%5Csum_%7Bn=1%7D%5E%7BN%7D%20x_n"></p>
<p>Then apply the economy SVD:</p>
<p><img src="https://latex.codecogs.com/png.latex?X_c%20=%20U%20%5CSigma%20V%5ET"></p>
<p>The three factors have a useful remote-sensing interpretation:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?V%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BB%20%5Ctimes%20K%7D">: spectral loading vectors, or wavelength patterns that capture strong variance</li>
<li><img src="https://latex.codecogs.com/png.latex?%5CSigma%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BK%20%5Ctimes%20K%7D">: the strength of those patterns</li>
<li><img src="https://latex.codecogs.com/png.latex?U%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BN%20%5Ctimes%20K%7D">: the per-pixel scores telling us how strongly each pixel expresses each pattern</li>
</ul>
<p>If you reshape one score column of <img src="https://latex.codecogs.com/png.latex?U"> back into image form, you get an <strong>eigenimage</strong>: a map showing where that spectral pattern is strong or weak. In scikit-learn, <code>components_</code> stores the spectral loadings and <code>transform</code> returns the pixel scores <span class="citation" data-cites="sklearn_pca">(scikit-learn developers 2024b)</span>.</p>
</section>
<section id="code" class="level3">
<h3 class="anchored" data-anchor-id="code">Code</h3>
<div id="pca" class="cell" data-execution_count="6">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">K <span class="op" 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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of components to retain</span></span>
<span id="cb5-2"></span>
<span id="cb5-3">pca <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PCA(n_components<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>K, svd_solver<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"randomized"</span>, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb5-4">Z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pca.fit_transform(X)         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pixel scores, shape (N, K)</span></span>
<span id="cb5-5">Vt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pca.components_              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># spectral patterns, shape (K, B)</span></span>
<span id="cb5-6">explained <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pca.explained_variance_ratio_</span>
<span id="cb5-7"></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Eigenimages: reshape each score column back to spatial dimensions</span></span>
<span id="cb5-9">eigenimages <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Z.T.reshape(K, H, W)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># shape: (K, H, W)</span></span>
<span id="cb5-10"></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rank-K reconstruction and per-pixel L2 error</span></span>
<span id="cb5-12">X_hat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pca.inverse_transform(Z)</span>
<span id="cb5-13">error_map <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linalg.norm(X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> X_hat, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>).reshape(H, W)</span></code></pre></div></div>
</details>
</div>
</section>
<section id="scree-plot-and-eigenimages" class="level3">
<h3 class="anchored" data-anchor-id="scree-plot-and-eigenimages">Scree plot and eigenimages</h3>
<div id="scree-eigenimages" class="cell" data-fig-show="hide" data-execution_count="7">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<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;">3.5</span>))</span>
<span id="cb6-2">ax.bar(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), explained <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#4878CF"</span>, width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>)</span>
<span id="cb6-3">ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Component"</span>)</span>
<span id="cb6-4">ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variance explained (%)"</span>)</span>
<span id="cb6-5">ax.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Scree plot: HYDICE Urban PCA"</span>)</span>
<span id="cb6-6">plt.tight_layout()</span>
<span id="cb6-7">plt.savefig(FIG_DIR <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"scree.png"</span>, bbox_inches<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tight"</span>)</span>
<span id="cb6-8">plt.show()</span>
<span id="cb6-9"></span>
<span id="cb6-10">fig2, axes2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, figsize<span class="op" 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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>))</span>
<span id="cb6-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, ax <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(axes2.flat):</span>
<span id="cb6-12">    img <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eigenimages[i]</span>
<span id="cb6-13">    vabs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(img), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span>)</span>
<span id="cb6-14">    ax.imshow(img, cmap<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RdBu_r"</span>, vmin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=-</span>vabs, vmax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vabs)</span>
<span id="cb6-15">    ax.set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"PC </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="op" 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="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  (</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>explained[i]<span class="op" 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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">%)"</span>, fontsize<span class="op" 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="cb6-16">    ax.axis(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>)</span>
<span id="cb6-17">plt.suptitle(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"First 6 eigenimages"</span>, fontsize<span class="op" 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>
<span id="cb6-18">plt.tight_layout()</span>
<span id="cb6-19">plt.savefig(FIG_DIR <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"eigenimages.png"</span>, bbox_inches<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tight"</span>)</span>
<span id="cb6-20">plt.show()</span></code></pre></div></div>
</details>
</div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/hyperspectral-imaging/figures/eigenimages.png" alt="Top 6 eigenimages, rendered with a diverging colormap. PC1 captures overall scene brightness. Subsequent components capture spectral contrast between material classes." style="display:block; margin:0 auto; width:100%;" class="figure-img"></p>
<figcaption>
Top 6 eigenimages, rendered with a diverging colormap. PC1 captures overall scene brightness. Subsequent components capture spectral contrast between material classes.
</figcaption>
</figure>
<p>The scree plot tells us how much each component matters. The eigenimages show where each component is active in space.</p>
<p>Usually, the first component captures broad scene brightness. The next few components often pick up real material contrasts, especially vegetation versus built surfaces or bright rooftops versus darker pavement. Later components tend to capture finer structure and, eventually, noise.</p>
<p>One important point: PCA values can be positive or negative. That is fine mathematically, but awkward physically. A negative PCA score does <strong>not</strong> mean “negative asphalt” or “negative grass.” It only means the pixel sits on one side of a mathematical axis instead of the other. That is why the next methods can be easier to interpret.</p>
</section>
<section id="reconstruction-error-map" class="level3">
<h3 class="anchored" data-anchor-id="reconstruction-error-map">Reconstruction error map</h3>
<div id="error-map" class="cell" data-fig-show="hide" data-execution_count="8">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>))</span>
<span id="cb7-2">im <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ax.imshow(error_map, cmap<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inferno"</span>)</span>
<span id="cb7-3">plt.colorbar(im, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L2 reconstruction error (DN)"</span>)</span>
<span id="cb7-4">ax.set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"PCA reconstruction error (K = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>K<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span>, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb7-5">ax.axis(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>)</span>
<span id="cb7-6">plt.tight_layout()</span>
<span id="cb7-7">plt.savefig(FIG_DIR <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pca_error.png"</span>, bbox_inches<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tight"</span>)</span>
<span id="cb7-8">plt.show()</span></code></pre></div></div>
</details>
</div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/hyperspectral-imaging/figures/pca_error.png" alt="Per-pixel reconstruction error for K=12. Bright pixels are where a 12-dimensional subspace fits poorly, typically rare materials, mixed pixels at material boundaries, or sensor noise." style="display:block; margin:0 auto; width:65%;" class="figure-img"></p>
<figcaption>
Per-pixel reconstruction error for K=12. Bright pixels are where a 12-dimensional subspace fits poorly, typically rare materials, mixed pixels at material boundaries, or sensor noise.
</figcaption>
</figure>
<p>This map shows where a 12-component PCA model fits poorly. Bright areas are pixels whose spectra are unusual or hard to summarize with just 12 components.</p>
<p>In practice, those bright spots often correspond to rare materials, shadows, standing water, vehicles, or strongly mixed pixels. That makes the error map a simple but useful anomaly detector.</p>
</section>
</section>
<section id="linear-spectral-unmixing" class="level2">
<h2 class="anchored" data-anchor-id="linear-spectral-unmixing">Linear spectral unmixing</h2>
<section id="the-linear-mixing-model" class="level3">
<h3 class="anchored" data-anchor-id="the-linear-mixing-model">The linear mixing model</h3>
<p>Now switch from compression to mixing.</p>
<p>The linear mixing model says that one pixel can be approximated as a weighted sum of a few pure material spectra, called <strong>endmembers</strong> <span class="citation" data-cites="plaza_2012">(Plaza et al. 2009)</span>. For an urban scene at 2 m resolution, that is a reasonable first model: many pixels are mixtures of vegetation, pavement, roof, and shadow rather than perfectly pure targets.</p>
<p><img src="https://latex.codecogs.com/png.latex?y_n%20%5Capprox%20%5Csum_%7Bp=1%7D%5E%7BP%7D%20s_%7Bnp%7D%20%5C,%20m_p%20=%20M%20s_n"></p>
<p>In matrix form for all pixels: <img src="https://latex.codecogs.com/png.latex?Y%20%5Capprox%20S%20M%5ET">, where <img src="https://latex.codecogs.com/png.latex?M%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BB%20%5Ctimes%20P%7D"> is the endmember matrix and <img src="https://latex.codecogs.com/png.latex?S%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BN%20%5Ctimes%20P%7D"> contains per-pixel abundance vectors.</p>
<p>This model is only an approximation, but it is a useful one. At 2 m resolution, many pixels cover more than one surface type, so “unmixing” means estimating the fractions of those hidden ingredients.</p>
<p>Two physical constraints on the abundance vector <img src="https://latex.codecogs.com/png.latex?s_n">:</p>
<ul>
<li><strong>Abundance nonnegativity (ANC):</strong> <img src="https://latex.codecogs.com/png.latex?s_n%20%5Cgeq%200">, you can’t have a negative fraction of a material</li>
<li><strong>Abundance sum-to-one (ASC):</strong> <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7B1%7D%5ET%20s_n%20=%201">, the fractions must add up</li>
</ul>
<p>Together, those constraints make the weights behave like fractions. The optimization problem per pixel is:</p>
<p><img src="https://latex.codecogs.com/png.latex?s_n%5E*%20=%20%5Carg%5Cmin_%7Bs%20%5C,%5Cin%5C,%20%5CDelta%5E%7BP-1%7D%7D%20%5C%7Cy_n%20-%20Ms%5C%7C_2%5E2"></p>
<p>Once <img src="https://latex.codecogs.com/png.latex?M"> is fixed, this is a well-behaved optimization problem with one best solution per pixel <span class="citation" data-cites="heinz_chang_2001">(Heinz and Chang 2001)</span>.</p>
</section>
<section id="endmember-selection" class="level3">
<h3 class="anchored" data-anchor-id="endmember-selection">Endmember selection</h3>
<p>Before we can solve for fractions, we need example spectra for the pure materials.</p>
<p>In a full remote-sensing workflow, you might estimate those endmembers automatically with algorithms such as N-FINDR or vertex component analysis. For a tutorial, hand-picking a few obvious pixels is easier to follow: choose one pixel that looks like road, one that looks like grass, one rooftop, and one tree patch.</p>
<div id="endmember-selection" class="cell" data-fig-show="hide" data-execution_count="9">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hand-picked pixel coordinates (row, col)</span></span>
<span id="cb8-2">endmember_pixels <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb8-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Asphalt"</span>: (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>),</span>
<span id="cb8-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Grass"</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;">200</span>),</span>
<span id="cb8-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rooftop"</span>: (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>,  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>),</span>
<span id="cb8-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Trees"</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;">150</span>),</span>
<span id="cb8-7">}</span>
<span id="cb8-8"></span>
<span id="cb8-9">P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(endmember_pixels)</span>
<span id="cb8-10">M <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros((B, P))</span>
<span id="cb8-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, (name, (r, c)) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(endmember_pixels.items()):</span>
<span id="cb8-12">    M[:, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cube[:, r, c].astype(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)</span>
<span id="cb8-13"></span>
<span id="cb8-14">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb8-15"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(endmember_pixels):</span>
<span id="cb8-16">    ax.plot(wavelengths, M[:, i], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>name, linewidth<span class="op" 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="cb8-17">ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wavelength (nm)"</span>)</span>
<span id="cb8-18">ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Radiance (DN)"</span>)</span>
<span id="cb8-19">ax.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Endmember spectra"</span>)</span>
<span id="cb8-20">ax.legend(frameon<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span>
<span id="cb8-21">plt.tight_layout()</span>
<span id="cb8-22">plt.savefig(FIG_DIR <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"endmembers.png"</span>, bbox_inches<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tight"</span>)</span>
<span id="cb8-23">plt.show()</span></code></pre></div></div>
</div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/hyperspectral-imaging/figures/endmembers.png" alt="Selected endmember spectra. Each represents a pure material type: the vegetation signatures (Grass, Trees) show the characteristic red-edge feature near 700 nm." style="display:block; margin:0 auto; width:85%;" class="figure-img"></p>
<figcaption>
Selected endmember spectra. Each represents a pure material type: the vegetation signatures (Grass, Trees) show the characteristic red-edge feature near 700 nm.
</figcaption>
</figure>
</section>
<section id="solving-for-abundances-fcls" class="level3">
<h3 class="anchored" data-anchor-id="solving-for-abundances-fcls">Solving for abundances: FCLS</h3>
<p>Now we solve for the fractions in each pixel.</p>
<p>We use fully constrained least squares (FCLS). The goal is to estimate the nonnegative abundance vector that best reconstructs the observed spectrum while also forcing the abundances to sum to one. The implementation below uses a common NNLS-based trick from <span class="citation" data-cites="heinz_chang_2001">Heinz and Chang (2001)</span>.</p>
<div id="fcls" class="cell" data-execution_count="10">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> fcls(M: np.ndarray, y: np.ndarray, delta: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> np.ndarray:</span>
<span id="cb9-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Fully constrained least squares (ANC + ASC).</span></span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Embeds the sum-to-one constraint by augmenting [M; delta*1^T] @ s = [y; delta],</span></span>
<span id="cb9-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    solves via NNLS (enforcing ANC), then normalizes to ensure sum(s) = 1.</span></span>
<span id="cb9-6"></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Parameters</span></span>
<span id="cb9-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ----------</span></span>
<span id="cb9-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    M : (B, P) endmember matrix</span></span>
<span id="cb9-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    y : (B,) pixel spectrum</span></span>
<span id="cb9-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    delta : scaling factor for the ASC constraint row (larger = stricter enforcement)</span></span>
<span id="cb9-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb9-13">    M_aug <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.vstack([M, delta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.ones((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, M.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))])</span>
<span id="cb9-14">    y_aug <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.append(y, delta)</span>
<span id="cb9-15">    s, _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nnls(M_aug, y_aug)</span>
<span id="cb9-16">    total <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>()</span>
<span id="cb9-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> total <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-10</span>:</span>
<span id="cb9-18">        s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/=</span> total</span>
<span id="cb9-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> s</span>
<span id="cb9-20"></span>
<span id="cb9-21"></span>
<span id="cb9-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Solve per pixel</span></span>
<span id="cb9-23">S <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros((N, P))</span>
<span id="cb9-24"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(N):</span>
<span id="cb9-25">    S[n] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fcls(M, X[n])</span>
<span id="cb9-26"></span>
<span id="cb9-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reshape abundance maps to (P, H, W)</span></span>
<span id="cb9-28">abundance_maps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> S.T.reshape(P, H, W)</span>
<span id="cb9-29"></span>
<span id="cb9-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Per-pixel residual: ||y_n - M s_n||</span></span>
<span id="cb9-31">residual <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linalg.norm(X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> S <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> M.T, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>).reshape(H, W)</span></code></pre></div></div>
</details>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The loop above is easy to read, but not especially fast. For HYDICE at 307 x 307 pixels it is still manageable. For much larger scenes, you would usually parallelize it or move to a more specialized solver.</p>
</div>
</div>
<div id="abundance-maps" class="cell" data-fig-show="hide" data-execution_count="11">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1">names <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(endmember_pixels.keys())</span>
<span id="cb10-2">cmaps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Reds"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Greens"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Oranges"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"YlGn"</span>]</span>
<span id="cb10-3"></span>
<span id="cb10-4">fig, axes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb10-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, (name, cmap) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(names, cmaps)):</span>
<span id="cb10-6">    axes[i].imshow(abundance_maps[i], cmap<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>cmap, vmin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, vmax<span class="op" 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="cb10-7">    axes[i].set_title(name, fontsize<span class="op" 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="cb10-8">    axes[i].axis(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>)</span>
<span id="cb10-9"></span>
<span id="cb10-10">im <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> axes[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].imshow(residual, cmap<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inferno"</span>)</span>
<span id="cb10-11">plt.colorbar(im, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axes[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Residual (DN)"</span>)</span>
<span id="cb10-12">axes[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Residual"</span>, fontsize<span class="op" 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="cb10-13">axes[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].axis(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>)</span>
<span id="cb10-14"></span>
<span id="cb10-15">plt.suptitle(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FCLS abundance maps + unmixing residual"</span>, fontsize<span class="op" 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>
<span id="cb10-16">plt.tight_layout()</span>
<span id="cb10-17">plt.savefig(FIG_DIR <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"abundance_maps.png"</span>, bbox_inches<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tight"</span>)</span>
<span id="cb10-18">plt.show()</span></code></pre></div></div>
</details>
</div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/hyperspectral-imaging/figures/abundance_maps.png" alt="Abundance maps for each endmember plus the per-pixel unmixing residual. Warmer colors indicate higher fractional abundance of that material. High residual highlights pixels the four-endmember model can't explain." style="display:block; margin:0 auto; width:100%;" class="figure-img"></p>
<figcaption>
Abundance maps for each endmember plus the per-pixel unmixing residual. Warmer colors indicate higher fractional abundance of that material. High residual highlights pixels the four-endmember model can’t explain.
</figcaption>
</figure>
<p>The residual map tells you where the chosen endmembers are not enough. Bright residuals often mean “there is something here that the current material library cannot explain well,” such as shadow, water, or bare soil.</p>
<p>Compared with PCA, these abundance maps are easier to explain. Each one answers a direct question like: “Where in the scene does this material seem common?” The trade-off is that the answer depends heavily on the endmembers you chose.</p>
</section>
</section>
<section id="nmf" class="level2">
<h2 class="anchored" data-anchor-id="nmf">NMF</h2>
<p>NMF asks a similar question to unmixing, but with less supervision: can we factor <img src="https://latex.codecogs.com/png.latex?X"> into two nonnegative matrices without telling the model in advance what the materials are?</p>
<p><img src="https://latex.codecogs.com/png.latex?X%20%5Capprox%20WH,%20%5Cquad%20W%20%5Cin%20%5Cmathbb%7BR%7D_%7B%5Cgeq%200%7D%5E%7BN%20%5Ctimes%20K%7D,%20%5C;%20H%20%5Cin%20%5Cmathbb%7BR%7D_%7B%5Cgeq%200%7D%5E%7BK%20%5Ctimes%20B%7D"></p>
<p>The objective (squared Frobenius norm) is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cmin_%7BW,%5C,%20H%5C,%20%5Cgeq%5C,%200%7D%20%5C;%5Cfrac%7B1%7D%7B2%7D%5C%7CX%20-%20WH%5C%7C_F%5E2"></p>
<p>Rows of <img src="https://latex.codecogs.com/png.latex?H"> are learned spectral patterns. Columns of <img src="https://latex.codecogs.com/png.latex?W"> tell us how much of each pattern appears in each pixel. Unlike PCA, NMF does not allow negative values, and unlike unmixing, it does not require the weights to sum to one.</p>
<p>The main idea from <span class="citation" data-cites="lee_seung_1999">Lee and Seung (1999)</span> is simple: nonnegative parts can only add, not cancel each other out. That often makes the learned components easier to read. PCA can produce positive-negative contrast patterns. NMF tends to produce parts that look more like material pieces of the scene.</p>
<p>The downside is that NMF is not unique. Different initial guesses can lead to somewhat different answers. Using <code>nndsvda</code> gives the algorithm a stable starting point and usually leads to cleaner results <span class="citation" data-cites="sklearn_nmf">(scikit-learn developers 2024a)</span>.</p>
<div id="nmf" class="cell" data-fig-show="hide" data-execution_count="12">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">K_nmf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb11-2"></span>
<span id="cb11-3">nmf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> NMF(</span>
<span id="cb11-4">    n_components<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>K_nmf,</span>
<span id="cb11-5">    init<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nndsvda"</span>,           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># deterministic SVD-based initialization</span></span>
<span id="cb11-6">    solver<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mu"</span>,              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># multiplicative updates (Lee &amp; Seung)</span></span>
<span id="cb11-7">    beta_loss<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frobenius"</span>,</span>
<span id="cb11-8">    random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>,</span>
<span id="cb11-9">    max_iter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb11-10">)</span>
<span id="cb11-11"></span>
<span id="cb11-12">W_nmf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nmf.fit_transform(X)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pixel weights, shape (N, K_nmf)</span></span>
<span id="cb11-13">H_nmf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nmf.components_          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># component spectra, shape (K_nmf, B)</span></span>
<span id="cb11-14">nmf_maps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> W_nmf.T.reshape(K_nmf, H, W)</span>
<span id="cb11-15"></span>
<span id="cb11-16">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb11-17"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(K_nmf):</span>
<span id="cb11-18">    ax.plot(wavelengths, H_nmf[k], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Component </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>k<span class="op" 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="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, linewidth<span class="op" 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="cb11-19">ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wavelength (nm)"</span>)</span>
<span id="cb11-20">ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Learned basis value"</span>)</span>
<span id="cb11-21">ax.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NMF component spectra"</span>)</span>
<span id="cb11-22">ax.legend(frameon<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, ncol<span class="op" 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="cb11-23">plt.tight_layout()</span>
<span id="cb11-24">plt.savefig(FIG_DIR <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nmf_spectra.png"</span>, bbox_inches<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tight"</span>)</span>
<span id="cb11-25">plt.show()</span>
<span id="cb11-26"></span>
<span id="cb11-27">fig2, axes2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, figsize<span class="op" 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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>))</span>
<span id="cb11-28"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k, ax <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(axes2.flat):</span>
<span id="cb11-29">    ax.imshow(nmf_maps[k], cmap<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"viridis"</span>)</span>
<span id="cb11-30">    ax.set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"NMF component </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>k<span class="op" 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="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, fontsize<span class="op" 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="cb11-31">    ax.axis(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>)</span>
<span id="cb11-32">plt.suptitle(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NMF spatial weight maps"</span>, fontsize<span class="op" 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>
<span id="cb11-33">plt.tight_layout()</span>
<span id="cb11-34">plt.savefig(FIG_DIR <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nmf_maps.png"</span>, bbox_inches<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tight"</span>)</span>
<span id="cb11-35">plt.show()</span></code></pre></div></div>
</details>
</div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/hyperspectral-imaging/figures/nmf_maps.png" alt="NMF component spectra (top) and spatial weight maps (bottom). Unlike PCA eigenvectors, NMF spectra are strictly nonnegative, they can be visually compared to known material spectra in a spectral library." style="display:block; margin:0 auto; width:100%;" class="figure-img"></p>
<figcaption>
NMF component spectra (top) and spatial weight maps (bottom). Unlike PCA eigenvectors, NMF spectra are strictly nonnegative, they can be visually compared to known material spectra in a spectral library.
</figcaption>
</figure>
<p>Where PCA gives contrast axes, NMF gives positive-only parts. Those parts are not guaranteed to be true materials, but they often look material-like enough to be useful. In practice, NMF is good for exploration when you do not yet know the right endmembers, while linear unmixing is better when you do know them and want physically interpretable fractions.</p>
</section>
<section id="where-deep-learning-fits-now" class="level2">
<h2 class="anchored" data-anchor-id="where-deep-learning-fits-now">Where deep learning fits now</h2>
<p>If I were learning hyperspectral imaging from scratch in 2026, I would not skip straight from “here is a cube” to “here is a transformer.” Deep learning is absolutely part of the field now, especially for classification, detection, denoising, and operational pipelines. But the older decomposition methods are still the fastest way to build intuition about what is actually in the data.</p>
<p>Recent reviews make that split pretty clear. Ghasemi et al.&nbsp;survey deep-learning methods for onboard spaceborne hyperspectral processing and emphasize lightweight CNNs, 1D-CNNs, GAN-based augmentation and denoising, and hardware-aware deployment under tight compute and bandwidth limits <span class="citation" data-cites="ghasemi_2025">(Ghasemi et al. 2025)</span>. Bourriz et al.&nbsp;review AI for hyperspectral crop mapping and show how strongly the field has moved toward spectral-spatial deep models, while also pointing out the same bottlenecks people keep running into: limited labeled data, uneven use of spaceborne HSI, and the need for models that scale without becoming impossible to interpret <span class="citation" data-cites="bourriz_2025">(Bourriz et al. 2025)</span>.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-2-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-1" aria-controls="tabset-2-1" aria-selected="true" href="">What deep learning changes</a></li><li class="nav-item"><a class="nav-link" id="tabset-2-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-2" aria-controls="tabset-2-2" aria-selected="false" href="">What it does not replace</a></li><li class="nav-item"><a class="nav-link" id="tabset-2-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-3" aria-controls="tabset-2-3" aria-selected="false" href="">A realistic workflow</a></li></ul>
<div class="tab-content">
<div id="tabset-2-1" class="tab-pane active" aria-labelledby="tabset-2-1-tab">
<p>Once you have labels or a clearly defined prediction task, deep models can learn nonlinear spectral-spatial structure that PCA or FCLS cannot. That matters for crop typing, semantic segmentation, target detection, denoising, onboard triage, and other tasks where the goal is accurate prediction rather than simple exploratory interpretation <span class="citation" data-cites="ghasemi_2025 bourriz_2025">(Ghasemi et al. 2025; Bourriz et al. 2025)</span>.</p>
</div>
<div id="tabset-2-2" class="tab-pane" aria-labelledby="tabset-2-2-tab">
<p>Deep learning does not remove the need to check radiometry, inspect spectra, think about mixed pixels, or ask whether the result is physically plausible. PCA, unmixing, and NMF are still useful for QA, dimensionality reduction, anomaly screening, endmember discovery, and interpretable baselines. They help you understand the signal before you ask a larger model to generalize it.</p>
</div>
<div id="tabset-2-3" class="tab-pane" aria-labelledby="tabset-2-3-tab">
<p>In practice, the stack is often hybrid: calibrate and correct the cube, inspect spectra, use PCA or another low-rank method to understand variance structure, use unmixing or NMF to get material intuition, and then move into supervised machine learning or deep learning if the task actually calls for it. In that sense, the classical methods are not pre-deep-learning leftovers. They are still part of how you figure out what problem you are solving.</p>
</div>
</div>
</div>
<p>If I keep pushing this project, the next natural step would be a follow-on post showing that handoff more explicitly: start with decomposition for intuition, then train a small spectral-spatial model on top of the same scene.</p>
</section>
<section id="exporting-products" class="level2">
<h2 class="anchored" data-anchor-id="exporting-products">Exporting products</h2>
<p>All three methods produce spatial outputs worth saving. HYDICE Urban does not include geospatial metadata such as a CRS or geotransform, so <code>.npy</code> files are a simple and honest export format here. For georeferenced datasets, you would usually write GeoTIFFs instead:</p>
<div id="export" class="cell" data-execution_count="13">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1">derived <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Path(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/derived"</span>)</span>
<span id="cb12-2">derived.mkdir(exist_ok<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb12-3"></span>
<span id="cb12-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save as .npy, HYDICE Urban has no CRS or geotransform, so plain arrays are more honest</span></span>
<span id="cb12-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># than an empty GeoTIFF. Each array is (layers, H, W), float32.</span></span>
<span id="cb12-6">np.save(derived <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"eigenimages.npy"</span>, eigenimages.astype(np.float32))</span>
<span id="cb12-7">np.save(derived <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"abundances.npy"</span>, abundance_maps.astype(np.float32))</span>
<span id="cb12-8">np.save(derived <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nmf_maps.npy"</span>, nmf_maps.astype(np.float32))</span>
<span id="cb12-9">np.save(derived <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"wavelengths.npy"</span>, wavelengths)</span>
<span id="cb12-10"></span>
<span id="cb12-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Saved:"</span>, [<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(p) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>(derived.glob(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"*.npy"</span>))])</span></code></pre></div></div>
</details>
</div>
</section>
<section id="where-this-fits-in-a-real-pipeline" class="level2">
<h2 class="anchored" data-anchor-id="where-this-fits-in-a-real-pipeline">Where this fits in a real pipeline</h2>
<p>The analysis above sits in the middle of a larger hyperspectral workflow. One thing I had to keep reminding myself while learning this is that decomposition is not the whole pipeline. In a production setting, you would usually do several preprocessing steps before running PCA, unmixing, or NMF:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Demo step</th>
<th>Production analogue</th>
<th>Why it matters</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Load ENVI cube, parse wavelengths</td>
<td>Data ingest, metadata validation</td>
<td>Wrong wavelength labels corrupt every downstream result</td>
</tr>
<tr class="even">
<td>Radiance discussion</td>
<td>Radiometric calibration, unit consistency</td>
<td>Decompositions are only cross-scene comparable when inputs share a radiometric convention</td>
</tr>
<tr class="odd">
<td>Spatial subset / windowed read</td>
<td>Tiling, chunked processing, distributed compute</td>
<td>Windowed I/O and chunk sizing are core scaling tactics for cubes that exceed RAM</td>
</tr>
<tr class="even">
<td>PCA eigenimages + error map</td>
<td>QA, compression, anomaly flagging</td>
<td>Error maps surface model mismatch and flag rare materials</td>
</tr>
<tr class="odd">
<td>FCLS abundance maps</td>
<td>Subpixel material estimation</td>
<td>ANC + ASC encode physical meaning; residuals guide endmember library iteration</td>
</tr>
<tr class="even">
<td>NMF component maps</td>
<td>Blind source separation, interpretable bases</td>
<td>Nonnegativity improves spectral factor interpretability without manual endmember selection</td>
</tr>
<tr class="odd">
<td>Export derived products</td>
<td><code>.npy</code> here (no geospatial metadata); GeoTIFF/COG for georeferenced datasets</td>
<td>Product generation, GIS integration</td>
</tr>
</tbody>
</table>
<p>Real pipelines usually include radiometric calibration, geometric correction, and atmospheric correction before this stage <span class="citation" data-cites="plaza_2012">(Plaza et al. 2009)</span>. This post skips those steps because the goal is to teach the decomposition ideas clearly, not to build a full operational workflow.</p>
</section>
<section id="reproducibility" class="level2">
<h2 class="anchored" data-anchor-id="reproducibility">Reproducibility</h2>
<ul>
<li>Random seeds: <code>random_state=42</code> for PCA and NMF ensures deterministic results across runs.</li>
<li>All figures use the full 307 x 307 scene with no extra spatial cropping, so re-renders should match exactly.</li>
<li>Freeze your environment: <code>conda env export &gt; environment.yml</code> or <code>uv pip freeze &gt; requirements.txt</code>.</li>
<li>The dataset is freely available from ERDC <span class="citation" data-cites="erdc_hypercube">(U.S. Army Engineer Research and Development Center 2004)</span>; record the access date and use the provided handle as a citation.</li>
</ul>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-bourriz_2025" class="csl-entry">
Bourriz, Mohamed, Hicham Hajji, Ahmed Laamrani, Nadir Elbouanani, Hamd Ait Abdelali, Francois Bourzeix, Ali El-Battay, Abdelhakim Amazirh, and Abdelghani Chehbouni. 2025. <span>“Integration of Hyperspectral Imaging and <span>AI</span> Techniques for Crop Type Mapping: Present Status, Trends, and Challenges.”</span> <em>Remote Sensing</em> 17 (9): 1574. <a href="https://doi.org/10.3390/rs17091574">https://doi.org/10.3390/rs17091574</a>.
</div>
<div id="ref-ghasemi_2025" class="csl-entry">
Ghasemi, Nafiseh, Jon Alvarez Justo, Marco Celesti, Laurent Despoisse, and Jens Nieke. 2025. <span>“Onboard Processing of Hyperspectral Imagery: Deep Learning Advancements, Methodologies, Challenges, and Emerging Trends.”</span> <em>IEEE Journal of Selected Topics in Applied Earth Observations and Remote Sensing</em> 18: 4780–90. <a href="https://doi.org/10.1109/JSTARS.2025.3527898">https://doi.org/10.1109/JSTARS.2025.3527898</a>.
</div>
<div id="ref-heinz_chang_2001" class="csl-entry">
Heinz, Daniel C., and Chein-I Chang. 2001. <span>“Fully Constrained Least Squares Linear Spectral Mixture Analysis Method for Material Quantification in Hyperspectral Imagery.”</span> <em>IEEE Transactions on Geoscience and Remote Sensing</em> 39 (3): 529–45. <a href="https://doi.org/10.1109/36.911111">https://doi.org/10.1109/36.911111</a>.
</div>
<div id="ref-aviris_moffett" class="csl-entry">
Jet Propulsion Laboratory. 1992. <span>“<span>AVIRIS</span> Free Standard Data Products — Moffett Field Radiance.”</span> NASA Jet Propulsion Laboratory. <a href="https://aviris.jpl.nasa.gov/data/free_data.html">https://aviris.jpl.nasa.gov/data/free_data.html</a>.
</div>
<div id="ref-lee_seung_1999" class="csl-entry">
Lee, Daniel D., and H. Sebastian Seung. 1999. <span>“Learning the Parts of Objects by Non-Negative Matrix Factorization.”</span> <em>Nature</em> 401: 788–91. <a href="https://doi.org/10.1038/44565">https://doi.org/10.1038/44565</a>.
</div>
<div id="ref-nasa_seadas" class="csl-entry">
NASA Ocean Biology Processing Group. 2023. <span>“<span>SeaDAS</span> — Radiance to Reflectance Conversion Documentation.”</span> NASA Goddard Space Flight Center. <a href="https://seadas.gsfc.nasa.gov/">https://seadas.gsfc.nasa.gov/</a>.
</div>
<div id="ref-plaza_2012" class="csl-entry">
Plaza, Antonio, Jon Atli Benediktsson, Joseph W. Boardman, Jason Brazile, Lorenzo Bruzzone, Gustavo Camps-Valls, Jocelyn Chanussot, et al. 2009. <span>“Recent Advances in Techniques for Hyperspectral Image Processing.”</span> <em>Remote Sensing of Environment</em> 113: S110–22. <a href="https://doi.org/10.1016/j.rse.2009.06.009">https://doi.org/10.1016/j.rse.2009.06.009</a>.
</div>
<div id="ref-sklearn_nmf" class="csl-entry">
scikit-learn developers. 2024a. <span>“Sklearn.decomposition.<span>NMF</span> — Scikit-Learn Documentation.”</span> <a href="https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.NMF.html" class="uri">https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.NMF.html</a>.
</div>
<div id="ref-sklearn_pca" class="csl-entry">
———. 2024b. <span>“Sklearn.decomposition.<span>PCA</span> — Scikit-Learn Documentation.”</span> <a href="https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html" class="uri">https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html</a>.
</div>
<div id="ref-erdc_hypercube" class="csl-entry">
U.S. Army Engineer Research and Development Center. 2004. <span>“<span>HyperCube</span> Hyperspectral Image Analysis Software — <span>URBAN</span> Sample Dataset.”</span> U.S. Army ERDC. <a href="https://www.erdc.usace.army.mil/Media/Fact-Sheets/Fact-Sheet-Article-View/Article/610433/hypercube/">https://www.erdc.usace.army.mil/Media/Fact-Sheets/Fact-Sheet-Article-View/Article/610433/hypercube/</a>.
</div>
</div>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{miller2026,
  author = {Miller, Emily},
  title = {Hyperspectral {Hyperawareness}},
  date = {2026-03-28},
  url = {https://rellimylime.github.io/posts/hyperspectral-imaging},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-miller2026" class="csl-entry quarto-appendix-citeas">
Miller, Emily. 2026. <span>“Hyperspectral Hyperawareness.”</span> March
28, 2026. <a href="https://rellimylime.github.io/posts/hyperspectral-imaging">https://rellimylime.github.io/posts/hyperspectral-imaging</a>.
</div></div></section></div> ]]></description>
  <category>Python</category>
  <category>Remote Sensing</category>
  <category>Linear Algebra</category>
  <category>Data Science</category>
  <guid>https://rellimylime.github.io/posts/hyperspectral-imaging/</guid>
  <pubDate>Sat, 28 Mar 2026 07:00:00 GMT</pubDate>
  <media:content url="https://rellimylime.github.io/posts/hyperspectral-imaging/figures/rgb_composite_thumbnail.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Getting Data That Doesn’t Come in a CSV</title>
  <dc:creator>Emily Miller</dc:creator>
  <link>https://rellimylime.github.io/posts/federal-grant-watch/</link>
  <description><![CDATA[ 





<p>A lot of interesting public data doesn’t come as a downloadable spreadsheet. It lives on a website, behind an API, or scattered across dozens of pages. If you want to analyze it, you have to collect it yourself — and that process is called <strong>web scraping</strong> (or, when the data comes from a structured interface, <strong>API data collection</strong>).</p>
<p>This post is a walkthrough of three techniques I used for the first time building this project: fetching paginated API data, enriching that data using a second API, and scraping HTML from a news site. I’ll use a real and timely example throughout: the DOGE federal grant termination list.</p>
<p>Since January 20, 2025, DOGE has published a public list of terminated federal grants. By the time I pulled it, it had over 15,000 entries. That’s too many to copy by hand and too important to ignore. Here’s how I got it, enriched it, and what it showed.</p>
<hr>
<section id="the-problem-data-that-lives-on-a-website" class="level2">
<h2 class="anchored" data-anchor-id="the-problem-data-that-lives-on-a-website">The Problem: Data That Lives on a Website</h2>
<p>Go to <a href="https://doge.gov">doge.gov</a> and you’ll see a table of terminated grants — agency, recipient, claimed savings. You can scroll through it, but you can’t download it as a file. And if you want to filter, join, or visualize it, you need it in a format your analysis tool can read.</p>
<p>This situation comes up constantly in data work. Government databases, financial filings, research registries, news archives — they publish the data, just not in a form you can directly use.</p>
<p>The three approaches in this project are a good starting point for learning to handle it:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Technique</th>
<th>When to use it</th>
<th>Tools</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>REST API (paginated)</td>
<td>Structured data with a public endpoint</td>
<td><code>httr2</code></td>
</tr>
<tr class="even">
<td>API enrichment</td>
<td>You have IDs from one source; another API has more info</td>
<td><code>httr2</code> + <code>purrr</code></td>
</tr>
<tr class="odd">
<td>HTML scraping</td>
<td>No API exists; data is in the page itself</td>
<td><code>rvest</code></td>
</tr>
</tbody>
</table>
<hr>
</section>
<section id="technique-1-fetching-a-paginated-rest-api" class="level2">
<h2 class="anchored" data-anchor-id="technique-1-fetching-a-paginated-rest-api">Technique 1: Fetching a Paginated REST API</h2>
<section id="what-is-an-api" class="level3">
<h3 class="anchored" data-anchor-id="what-is-an-api">What is an API?</h3>
<p>An <strong>API</strong> (Application Programming Interface) is a structured way for programs to talk to each other. A <strong>REST API</strong> lets you request data over the web by constructing a URL, the same way your browser requests a page — except instead of HTML, you get back data, usually in <strong>JSON</strong> format.</p>
<p>The DOGE grant list is available at <code>https://api.doge.gov/savings/grants</code>. If you paste that into a browser, you’ll see something like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode json code-with-copy"><code class="sourceCode json"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"meta"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"pages"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">127</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">},</span></span>
<span id="cb1-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"result"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-4">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"grants"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">[</span></span>
<span id="cb1-5">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"agency"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Department of Education"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"recipient"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"..."</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"savings"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3200000</span><span class="fu" style="color: #4758AB;
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;">}</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-6">      <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">...</span></span>
<span id="cb1-7">    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb1-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>That’s the structure we’ll be working with in R.</p>
</section>
<section id="the-challenge-pagination" class="level3">
<h3 class="anchored" data-anchor-id="the-challenge-pagination">The challenge: pagination</h3>
<p>APIs rarely return everything in one shot. The DOGE API returns 100 records per request (“page”), and there are 127 pages. To get all 12,000+ grants, you need to loop through each page and stack the results.</p>
<p>Here’s the function I wrote to do that:</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">fetch_doge_grants <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;">per_page =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_pages =</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;">pause_seconds =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) {</span>
<span id="cb2-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(httr2)</span>
<span id="cb2-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lubridate)</span>
<span id="cb2-5"></span>
<span id="cb2-6">    base_url <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;">"https://api.doge.gov/savings/grants"</span></span>
<span id="cb2-7"></span>
<span id="cb2-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: Fetch page 1 to learn how many pages exist</span></span>
<span id="cb2-9">    first_body <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">request</span>(base_url) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-10">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req_url_query</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">page =</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;">per_page =</span> per_page) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-11">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req_perform</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;">resp_body_json</span>()</span>
<span id="cb2-13"></span>
<span id="cb2-14">    total_pages <span class="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>(first_body<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>meta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pages)</span>
<span id="cb2-15">    pages_to_fetch <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.null</span>(max_pages)) total_pages <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(max_pages, total_pages)</span>
<span id="cb2-16"></span>
<span id="cb2-17">    all_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;">vector</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"list"</span>, pages_to_fetch)</span>
<span id="cb2-18">    all_results[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</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;">bind_rows</span>(first_body<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grants)</span>
<span id="cb2-19"></span>
<span id="cb2-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 2: Loop through the remaining pages</span></span>
<span id="cb2-21">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (pages_to_fetch <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;">1</span>) {</span>
<span id="cb2-22">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (page <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>pages_to_fetch) {</span>
<span id="cb2-23">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">message</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fetching page "</span>, page, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" of "</span>, pages_to_fetch, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"..."</span>)</span>
<span id="cb2-24">            body <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">request</span>(base_url) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-25">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req_url_query</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">page =</span> page, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">per_page =</span> per_page) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-26">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req_perform</span>() <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;">resp_body_json</span>()</span>
<span id="cb2-28"></span>
<span id="cb2-29">            grants <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> body<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grants</span>
<span id="cb2-30">            <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;">length</span>(grants) <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>L) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb2-31"></span>
<span id="cb2-32">            all_results[[page]] <span class="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>(grants)</span>
<span id="cb2-33">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(pause_seconds)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># be polite: don't hammer the server</span></span>
<span id="cb2-34">        }</span>
<span id="cb2-35">    }</span>
<span id="cb2-36"></span>
<span id="cb2-37">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(all_results) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-38">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb2-39">            <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;">mdy</span>(date),</span>
<span id="cb2-40">            <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;">suppressWarnings</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(value)),</span>
<span id="cb2-41">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">savings =</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;">as.numeric</span>(savings))</span>
<span id="cb2-42">        )</span>
<span id="cb2-43">}</span></code></pre></div></div>
</details>
</div>
<p>A few things worth noting for beginners:</p>
<p><strong><code>request() |&gt; req_url_query() |&gt; req_perform() |&gt; resp_body_json()</code></strong> — this is the <code>httr2</code> pipeline for making an API request. Think of <code>request()</code> as addressing an envelope, <code>req_url_query()</code> as adding query parameters (like <code>?page=1&amp;per_page=100</code>), <code>req_perform()</code> as mailing it, and <code>resp_body_json()</code> as reading the reply.</p>
<p><strong>Fetch page 1 first, then loop.</strong> The first request tells you how many total pages exist — so you don’t have to guess.</p>
<p><strong><code>Sys.sleep(pause_seconds)</code></strong> — this is important. If you loop 127 times without pausing, you’re making 127 requests in a few seconds. That can look like a denial-of-service attack to the server. A half-second pause is polite and usually enough.</p>
<hr>
</section>
</section>
<section id="technique-2-enriching-data-using-a-second-api" class="level2">
<h2 class="anchored" data-anchor-id="technique-2-enriching-data-using-a-second-api">Technique 2: Enriching Data Using a Second API</h2>
<section id="the-problem-with-self-reported-numbers" class="level3">
<h3 class="anchored" data-anchor-id="the-problem-with-self-reported-numbers">The problem with self-reported numbers</h3>
<p>The DOGE grant list includes a “savings” column — the claimed value of each termination. But this is a self-reported figure from DOGE. Is there an independent way to check it?</p>
<p>It turns out, yes. Many entries in the DOGE list include a link to <strong>USAspending.gov</strong>, the federal government’s official grant tracking database. That link contains an award ID, and USAspending has its own API where you can look up each award and get the government’s own numbers: total obligation, total outlay, and recipient name.</p>
</section>
<section id="extracting-ids-and-looking-them-up" class="level3">
<h3 class="anchored" data-anchor-id="extracting-ids-and-looking-them-up">Extracting IDs and looking them up</h3>
<p>Here’s what the process looks like:</p>
<ol type="1">
<li>Parse the USAspending award ID out of the DOGE <code>link</code> field using a regex</li>
<li>For each ID, hit the USAspending API and pull back the award details</li>
<li>Join the results back to the original DOGE data</li>
</ol>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">fetch_usaspending_awards <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>(doge_grants, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_ids =</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;">pause_seconds =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>) {</span>
<span id="cb3-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb3-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stringr)</span>
<span id="cb3-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(httr2)</span>
<span id="cb3-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)</span>
<span id="cb3-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tibble)</span>
<span id="cb3-7"></span>
<span id="cb3-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: Extract award IDs from the DOGE link field</span></span>
<span id="cb3-9">    ids <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> doge_grants <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-10">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb3-11">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">usaspending_award_id =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_match</span>(link, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"usaspending</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;">.gov/award/([^/?#]+)"</span>)[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb3-12">        ) <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>(<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>(usaspending_award_id)) <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;">distinct</span>(usaspending_award_id) <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;">slice_head</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> max_ids) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-16">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(usaspending_award_id)</span>
<span id="cb3-17"></span>
<span id="cb3-18">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 2: Look up each ID in the USAspending API</span></span>
<span id="cb3-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(ids, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(id) {</span>
<span id="cb3-20">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(pause_seconds)</span>
<span id="cb3-21">        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;">tryCatch</span>(</span>
<span id="cb3-22">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">request</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;">"https://api.usaspending.gov/api/v2/awards/"</span>, id, <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;">|&gt;</span></span>
<span id="cb3-23">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req_perform</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-24">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">resp_body_json</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">simplifyVector =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb3-25">            <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># if a lookup fails, return nothing</span></span>
<span id="cb3-26">        )</span>
<span id="cb3-27"></span>
<span id="cb3-28">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb3-29">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">usaspending_award_id        =</span> id,</span>
<span id="cb3-30">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">usaspending_total_obligation =</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.null</span>(b)) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>total_obligation,</span>
<span id="cb3-31">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">usaspending_total_outlay     =</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.null</span>(b)) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>total_outlay,</span>
<span id="cb3-32">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">usaspending_recipient        =</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.null</span>(b)) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_character_</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>recipient<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>recipient_name</span>
<span id="cb3-33">        )</span>
<span id="cb3-34">    })</span>
<span id="cb3-35">}</span></code></pre></div></div>
</details>
</div>
<p><strong><code>str_match()</code> with a regex</strong> — the DOGE <code>link</code> field contains URLs like <code>https://www.usaspending.gov/award/GRANT-12345/</code>. The regex <code>usaspending\.gov/award/([^/?#]+)</code> captures everything between <code>/award/</code> and the next <code>/</code>, <code>?</code>, or <code>#</code>. That’s the ID we want.</p>
<p><strong><code>map_dfr()</code></strong> — this applies a function to every element of a vector and stacks the results into a single dataframe. It’s the <code>purrr</code> equivalent of a for loop that collects results.</p>
<p><strong><code>tryCatch()</code></strong> — some award IDs in the DOGE list are invalid, retired, or return errors. Wrapping the API call in <code>tryCatch()</code> means a single failed lookup won’t crash the whole loop. If an ID fails, we return a row of <code>NA</code> values and move on.</p>
<hr>
</section>
</section>
<section id="technique-3-searching-a-government-api-by-keyword" class="level2">
<h2 class="anchored" data-anchor-id="technique-3-searching-a-government-api-by-keyword">Technique 3: Searching a Government API by Keyword</h2>
<section id="the-federal-register" class="level3">
<h3 class="anchored" data-anchor-id="the-federal-register">The Federal Register</h3>
<p>Not every useful API gives you a list to paginate through. Some let you <em>search</em> — you send a query and get back matching documents. The <a href="https://www.federalregister.gov/developers/documentation/api/v1">Federal Register API</a> is a good example: it’s the official public record of executive orders, agency rules, and federal notices, and it supports full keyword search with date and document-type filters.</p>
<p>For this project, we can use it to find presidential documents and agency notices related to DOGE and grant policy — giving us a reproducible, auto-updating policy event source instead of a manually maintained list.</p>
<p>The key difference from the DOGE API: instead of paginating through a fixed list, we run multiple searches (one per search term) and deduplicate the results.</p>
<div class="cell">
<details class="code-fold">
<summary>Show code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">fetch_federal_register_events <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>
<span id="cb4-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_date    =</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-01-20"</span>),</span>
<span id="cb4-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">search_terms  =</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;">"DOGE"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Department of Government Efficiency"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grant freeze"</span>),</span>
<span id="cb4-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">doc_types     =</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;">"PRESDOCU"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NOTICE"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RULE"</span>),</span>
<span id="cb4-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">per_page      =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>,</span>
<span id="cb4-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_pages     =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb4-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pause_seconds =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span></span>
<span id="cb4-8">) {</span>
<span id="cb4-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(httr2)</span>
<span id="cb4-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb4-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)</span>
<span id="cb4-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lubridate)</span>
<span id="cb4-13"></span>
<span id="cb4-14">    base_url <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;">"https://www.federalregister.gov/api/v1/documents.json"</span></span>
<span id="cb4-15"></span>
<span id="cb4-16">    fetch_one_term <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>(term) {</span>
<span id="cb4-17">        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;">list</span>()</span>
<span id="cb4-18">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (page <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>(max_pages)) {</span>
<span id="cb4-19">            req <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">request</span>(base_url) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-20">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req_url_query</span>(</span>
<span id="cb4-21">                    <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;">conditions[term]</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> term,</span>
<span id="cb4-22">                    <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;">conditions[publication_date][gte]</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;">format</span>(start_date, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-%d"</span>),</span>
<span id="cb4-23">                    <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;">conditions[type][]</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> doc_types,</span>
<span id="cb4-24">                    <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;">fields[]</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"title"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"publication_date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"document_number"</span>,</span>
<span id="cb4-25">                                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"abstract"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"html_url"</span>),</span>
<span id="cb4-26">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">per_page =</span> per_page,</span>
<span id="cb4-27">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">page     =</span> page,</span>
<span id="cb4-28">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"oldest"</span>,</span>
<span id="cb4-29">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.multi   =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"explode"</span></span>
<span id="cb4-30">                )</span>
<span id="cb4-31">            body <span class="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-32">                req <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;">req_perform</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;">resp_body_json</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">simplifyVector =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>),</span>
<span id="cb4-33">                <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="cb4-34">            )</span>
<span id="cb4-35">            <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>(body) <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>(body<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results) <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>L) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb4-36"></span>
<span id="cb4-37">            results[[page]] <span class="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>(body<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>results, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(r) {</span>
<span id="cb4-38">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb4-39">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">publication_date =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>publication_date),</span>
<span id="cb4-40">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title            =</span> r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>title <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;">NA_character_</span>,</span>
<span id="cb4-41">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">document_number  =</span> r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>document_number <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;">NA_character_</span>,</span>
<span id="cb4-42">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type             =</span> r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>type <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%||%</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_character_</span>,</span>
<span id="cb4-43">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">abstract         =</span> r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>abstract <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;">NA_character_</span>,</span>
<span id="cb4-44">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">html_url         =</span> r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>html_url <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;">NA_character_</span></span>
<span id="cb4-45">                )</span>
<span id="cb4-46">            })</span>
<span id="cb4-47">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(pause_seconds)</span>
<span id="cb4-48">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (page <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> body<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>total_pages) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb4-49">        }</span>
<span id="cb4-50">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(results)</span>
<span id="cb4-51">    }</span>
<span id="cb4-52"></span>
<span id="cb4-53">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run one search per term, then deduplicate by document number</span></span>
<span id="cb4-54">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(search_terms, fetch_one_term) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-55">        <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>(document_number)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-56">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(document_number, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep_all =</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="cb4-57">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(publication_date <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> start_date) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-58">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(publication_date)</span>
<span id="cb4-59">}</span></code></pre></div></div>
</details>
</div>
<p>A few things worth noting:</p>
<p><strong>Multiple search terms, one pass.</strong> Searching <code>"DOGE"</code> returns presidential documents; <code>"grant freeze"</code> catches OMB-adjacent notices. Running both and deduplicating on <code>document_number</code> gets more complete coverage than any single query.</p>
<p><strong><code>conditions[type][]</code> as a vector.</strong> To filter by multiple document types, the Federal Register API expects the same parameter repeated (e.g., <code>?conditions[type][]=PRESDOCU&amp;conditions[type][]=NOTICE</code>). Passing a character vector with <code>.multi = "explode"</code> does this automatically — one instance of the parameter per value.</p>
<p><strong>Early exit.</strong> <code>if (page &gt;= body$total_pages) break</code> stops pagination once we’ve consumed all available pages for a given search term, even if <code>max_pages</code> is higher.</p>
<p>Here’s a sample of what the Federal Register target returns when the pipeline runs:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>publication_date</th>
<th>type</th>
<th>title</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>2025-01-20</td>
<td>Presidential Document</td>
<td>Establishing and Implementing the President’s “Department of Government Efficiency”</td>
</tr>
<tr class="even">
<td>2025-01-20</td>
<td>Presidential Document</td>
<td>Reevaluating and Realigning United States Foreign Aid</td>
</tr>
<tr class="odd">
<td>2025-01-30</td>
<td>Presidential Document</td>
<td>Expanding Educational Freedom and Opportunity for Families</td>
</tr>
<tr class="even">
<td>2025-02-19</td>
<td>Presidential Document</td>
<td>Implementing the President’s “Department of Government Efficiency” Workforce Optimization Initiative</td>
</tr>
<tr class="odd">
<td>2025-02-26</td>
<td>Presidential Document</td>
<td>Implementing the President’s “Department of Government Efficiency” Cost Efficiency Initiative</td>
</tr>
<tr class="even">
<td>2025-03-14</td>
<td>Notice</td>
<td>Termination of Federal Assistance Awards</td>
</tr>
<tr class="odd">
<td>2025-04-02</td>
<td>Notice</td>
<td>National Endowment for the Humanities: Termination of Grants</td>
</tr>
</tbody>
</table>
<p>This is the same data available to any researcher who runs <code>tar_make()</code> — no manual curation required, and it stays current as new documents are published.</p>
<hr>
</section>
</section>
<section id="technique-4-scraping-html-when-theres-no-api" class="level2">
<h2 class="anchored" data-anchor-id="technique-4-scraping-html-when-theres-no-api">Technique 4: Scraping HTML When There’s No API</h2>
<section id="when-you-cant-just-ask-for-the-data" class="level3">
<h3 class="anchored" data-anchor-id="when-you-cant-just-ask-for-the-data">When you can’t just ask for the data</h3>
<p>For the first three techniques, we were talking to APIs that returned structured JSON. But sometimes data only exists as HTML on a web page — readable in a browser, but not downloadable as a file.</p>
<p>For this project I wanted a news timeline: which days had major DOGE announcements, and what happened? <a href="https://www.govexec.com">GovExec</a> covers federal government closely and timestamps every article. There’s no API for it. So I scraped it.</p>
<p>Every web page is built from HTML — a tree of nested elements. The steps below walk through how to read it with <code>rvest</code>, using the GovExec article list as an example.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">page <span class="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_html</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.govexec.com/topic/doge/more/?page=1"</span>)</span></code></pre></div></div>
<p><strong>Step 1: Find the article titles with a CSS selector</strong></p>
<p><strong>CSS selectors</strong> are patterns that describe which elements you want. You find them by right-clicking any element in your browser → “Inspect” → reading the class names in the Elements panel. Here’s what a single article looks like in GovExec’s HTML:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode html code-with-copy"><code class="sourceCode html"><span id="cb6-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">article</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"river-item"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-2"></span>
<span id="cb6-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">a</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"river-item-hed-link"</span></span>
<span id="cb6-4"><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">     href</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/management/2025/02/doge-cuts-350m"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-5">    DOGE Cuts $350M in Education Grants</span>
<span id="cb6-6">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">a</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-7"></span>
<span id="cb6-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ul</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"story-meta"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-9">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">li</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"story-meta-date"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-10">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">time</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> datetime</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-02-13T12:00:00Z"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-11">        February 13, 2025</span>
<span id="cb6-12">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">time</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-13">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">li</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-14">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ul</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-15"></span>
<span id="cb6-16"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">article</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span></code></pre></div></div>
<p>The article headlines are inside <code>&lt;a&gt;</code> tags with the class <code>river-item-hed-link</code>. The selector <code>a.river-item-hed-link</code> means: <em>all link elements with that class</em>. <code>html_text2()</code> extracts the visible text — the headline you’d read on screen, with whitespace cleaned up.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">titles <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> page <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html_elements</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a.river-item-hed-link"</span>) <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;">html_text2</span>() <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;">str_squish</span>()</span></code></pre></div></div>
<p><strong>Step 2: Get the dates from a hidden attribute</strong></p>
<p>The human-readable date (“February 13, 2025”) is unreliable to parse. But HTML <code>&lt;time&gt;</code> tags carry a <code>datetime</code> attribute — a machine-readable ISO timestamp that’s much easier to work with. The selector <code>li.story-meta-date time</code> navigates down: <em>find <code>&lt;li&gt;</code> tags with class <code>story-meta-date</code>, then get the <code>&lt;time&gt;</code> inside</em>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">datetimes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> page <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;">html_elements</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"li.story-meta-date time"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html_attr</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"datetime"</span>)</span></code></pre></div></div>
<p><code>html_attr("datetime")</code> extracts the <em>attribute value</em>, not the visible text. This is the difference between <code>html_text2()</code> and <code>html_attr()</code> — and knowing which to use requires reading the HTML.</p>
<p><strong>Step 3: Put it together in a reusable function</strong></p>
<p>The function below wraps all three steps — building the URL, applying the selectors, and assembling the result. It takes a <code>page_num</code> argument so it can be looped across multiple pages. The safety checks (lines 9–10) guard against empty or mismatched results when the page structure changes.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">scrape_one_govexec_page <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;">page_num =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb9-2">    url  <span class="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;">"https://www.govexec.com/topic/doge/more/?page="</span>, page_num)</span>
<span id="cb9-3">    page <span class="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_html</span>(url)</span>
<span id="cb9-4"></span>
<span id="cb9-5">    titles    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> page <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;">html_elements</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a.river-item-hed-link"</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;">html_text2</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;">str_squish</span>()</span>
<span id="cb9-6">    datetimes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> page <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;">html_elements</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"li.story-meta-date time"</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;">html_attr</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"datetime"</span>)</span>
<span id="cb9-7">    links     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> page <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;">html_elements</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a.river-item-hed-link"</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;">html_attr</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"href"</span>)</span>
<span id="cb9-8"></span>
<span id="cb9-9">    <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;">length</span>(titles) <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>L <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>(datetimes) <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>L) <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;">tibble</span>())</span>
<span id="cb9-10">    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;">min</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(titles), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(datetimes), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(links))</span>
<span id="cb9-11"></span>
<span id="cb9-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb9-13">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> titles[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n)],</span>
<span id="cb9-14">        <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;">ymd_hms</span>(datetimes[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n)]),</span>
<span id="cb9-15">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(</span>
<span id="cb9-16">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_starts</span>(links[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n)], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http"</span>),</span>
<span id="cb9-17">            links[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n)],</span>
<span id="cb9-18">            <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;">"https://www.govexec.com"</span>, links[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n)])</span>
<span id="cb9-19">        )</span>
<span id="cb9-20">    )</span>
<span id="cb9-21">}</span></code></pre></div></div>
<p><strong>One important caveat:</strong> scrapers break when websites redesign. The class names <code>river-item-hed-link</code> and <code>story-meta-date</code> are specific to GovExec’s current template. If they change, this function returns nothing — checking your scrapers periodically is part of maintaining a data pipeline.</p>
<hr>
</section>
</section>
<section id="putting-it-together" class="level2">
<h2 class="anchored" data-anchor-id="putting-it-together">Putting It Together</h2>
<p>Once we have data from all three sources, we combine them into a single, enriched dataset:</p>
<ul>
<li>DOGE records provide the termination date, agency, recipient, and claimed savings</li>
<li>USAspending adds verified obligation and outlay figures where a match is found</li>
<li>The news scrape provides the policy event dates used to annotate the charts</li>
</ul>
<p>The <code>{targets}</code> package manages the whole pipeline. Each step is a “target” — a named computation. <code>{targets}</code> checks which steps are outdated and re-runs only what’s necessary, which matters a lot when one step (fetching 12,000 USAspending records) takes 20 minutes.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;"># _targets.R (abbreviated)</span></span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb10-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tar_target</span>(doge_grants,        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fetch_doge_grants</span>()),</span>
<span id="cb10-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tar_target</span>(usaspending_awards, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fetch_usaspending_awards</span>(doge_grants)),</span>
<span id="cb10-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tar_target</span>(news_articles,      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scrape_grant_news</span>()),</span>
<span id="cb10-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tar_target</span>(combined,           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">combine_sources</span>(doge_grants, news_articles, usaspending_awards)),</span>
<span id="cb10-7">    ...</span>
<span id="cb10-8">)</span></code></pre></div></div>
<p>Running <code>tar_make()</code> executes every step in the right order. Running it again only re-fetches if the upstream code changed.</p>
<hr>
</section>
<section id="what-the-data-shows" class="level2">
<h2 class="anchored" data-anchor-id="what-the-data-shows">What the Data Shows</h2>
<p>With the combined dataset in hand, we can ask questions that weren’t possible from any single source.</p>
<p>The most useful cross-reference: where DOGE’s claimed savings figure diverges from USAspending’s record of what was actually obligated. That gap is what motivates the first chart.</p>
<section id="the-cumulative-picture" class="level3">
<h3 class="anchored" data-anchor-id="the-cumulative-picture">The Cumulative Picture</h3>
<div class="cell">
<div class="cell-output-display">
<div class="plotly html-widget html-fill-item" id="htmlwidget-13158bcd88e89f279394" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="htmlwidget-13158bcd88e89f279394">{"x":{"data":[{"x":[20089,20120,20148,20179,20209,20240,20270,20332,20362,20362,20362,20332,20270,20240,20209,20179,20148,20120,20089,20089],"y":[0,0,0,0,0,0,0,0,0,0,17931694088.810001,16674724230.57,16674480181.210001,16674113189.210001,16671185747.610001,15817424959.860001,14235265483.720001,980241553.27999997,4272209.3599999994,0],"text":"","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(78,121,167,0.2)","hoveron":"points","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20179,null,20120,null,20089,null,20270,null,20240,null,20148,null,20209,null,20362,null,20332],"y":[15817424959.860001,null,980241553.27999997,null,4272209.3599999994,null,16674480181.210001,null,16674113189.210001,null,14235265483.720001,null,16671185747.610001,null,17931694088.810001,null,16674724230.57],"text":["<b>Apr 2025<\/b><br>DOGE claimed (cumulative): $15.82B",null,"<b>Feb 2025<\/b><br>DOGE claimed (cumulative): $0.98B",null,"<b>Jan 2025<\/b><br>DOGE claimed (cumulative): $0.00B",null,"<b>Jul 2025<\/b><br>DOGE claimed (cumulative): $16.67B",null,"<b>Jun 2025<\/b><br>DOGE claimed (cumulative): $16.67B",null,"<b>Mar 2025<\/b><br>DOGE claimed (cumulative): $14.24B",null,"<b>May 2025<\/b><br>DOGE claimed (cumulative): $16.67B",null,"<b>Oct 2025<\/b><br>DOGE claimed (cumulative): $17.93B",null,"<b>Sep 2025<\/b><br>DOGE claimed (cumulative): $16.67B"],"type":"scatter","mode":"lines","line":{"width":4.5354330708661417,"color":"rgba(78,121,167,1)","dash":"solid"},"hoveron":"points","name":"DOGE claimed savings","legendgroup":"DOGE claimed savings","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20179,null,20120,null,20089,null,20270,null,20240,null,20148,null,20209,null,20362,null,20332],"y":[1036106505.54,null,223196433.24000001,null,0,null,1491249166.3299999,null,1491249166.3299999,null,768270407.64999998,null,1489525561.28,null,1491249166.3299999,null,1491249166.3299999],"text":["<b>Apr 2025<\/b><br>Verified remaining (cumulative): $1.04B",null,"<b>Feb 2025<\/b><br>Verified remaining (cumulative): $0.22B",null,"<b>Jan 2025<\/b><br>Verified remaining (cumulative): $0.00B",null,"<b>Jul 2025<\/b><br>Verified remaining (cumulative): $1.49B",null,"<b>Jun 2025<\/b><br>Verified remaining (cumulative): $1.49B",null,"<b>Mar 2025<\/b><br>Verified remaining (cumulative): $0.77B",null,"<b>May 2025<\/b><br>Verified remaining (cumulative): $1.49B",null,"<b>Oct 2025<\/b><br>Verified remaining (cumulative): $1.49B",null,"<b>Sep 2025<\/b><br>Verified remaining (cumulative): $1.49B"],"type":"scatter","mode":"lines","line":{"width":3.7795275590551185,"color":"rgba(225,87,89,1)","dash":"dash"},"hoveron":"points","name":"Verified remaining (USAspending proxy)","legendgroup":"Verified remaining (USAspending proxy)","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20108,20108,null,20115,20115,null,20117,20117,null,20132,20132,null,20136,20136,null,20138,20138,null,20145,20145,null,20153,20153,null,20157,20157,null,20180,20180],"y":[-896584704.44050014,18828278793.2505,null,-896584704.44050014,18828278793.2505,null,-896584704.44050014,18828278793.2505,null,-896584704.44050014,18828278793.2505,null,-896584704.44050014,18828278793.2505,null,-896584704.44050014,18828278793.2505,null,-896584704.44050014,18828278793.2505,null,-896584704.44050014,18828278793.2505,null,-896584704.44050014,18828278793.2505,null,-896584704.44050014,18828278793.2505],"text":"","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"rgba(153,153,153,0.7)","dash":"dash"},"hoveron":"points","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20176,20176,20184,20184,20176],"y":[0,17931694088.810001,17931694088.810001,0,0],"text":"<b>April 02, 2025<\/b><br>NEH/IMLS termination wave reported","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20128,20128,20136,20136,20128],"y":[0,17931694088.810001,17931694088.810001,0,0],"text":"<b>February 13, 2025<\/b><br>ED: $350M+ cancellations","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20132,20132,20140,20140,20132],"y":[0,17931694088.810001,17931694088.810001,0,0],"text":"<b>February 17, 2025<\/b><br>ED: $600M+ teacher-training cuts","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20134,20134,20142,20142,20134],"y":[0,17931694088.810001,17931694088.810001,0,0],"text":"<b>February 19, 2025<\/b><br>ED: $226M comprehensive center cuts","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20141,20141,20149,20149,20141],"y":[0,17931694088.810001,17931694088.810001,0,0],"text":"<b>February 26, 2025<\/b><br>DOGE cost-efficiency EO (30-day review window)","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20104,20104,20112,20112,20104],"y":[0,17931694088.810001,17931694088.810001,0,0],"text":"<b>January 20, 2025<\/b><br>DOGE established; foreign aid review EO","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20111,20111,20119,20119,20111],"y":[0,17931694088.810001,17931694088.810001,0,0],"text":"<b>January 27, 2025<\/b><br>OMB M-25-13 pause memo","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20113,20113,20121,20121,20113],"y":[0,17931694088.810001,17931694088.810001,0,0],"text":"<b>January 29, 2025<\/b><br>OMB M-25-13 rescinded (M-25-14)","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20149,20149,20157,20157,20149],"y":[0,17931694088.810001,17931694088.810001,0,0],"text":"<b>March 06, 2025<\/b><br>Court extends block on broad freeze","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20153,20153,20161,20161,20153],"y":[0,17931694088.810001,17931694088.810001,0,0],"text":"<b>March 10, 2025<\/b><br>State: ~83% of USAID programs cut","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null}],"layout":{"margin":{"t":61.695309256953102,"r":39.850560398505614,"b":43.304275633042771,"l":71.996679119966814},"paper_bgcolor":"rgba(255,255,255,1)","font":{"color":"rgba(0,0,0,1)","family":"","size":15.940224159402243},"title":{"text":"Cumulative Academic Grant Funding Loss Under DOGE","font":{"color":"rgba(0,0,0,1)","family":"","size":19.128268991282692},"x":0,"xref":"paper"},"xaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[20075.349999999999,20375.650000000001],"tickmode":"array","ticktext":["Jan","Apr","Jul","Oct"],"tickvals":[20089,20179,20270,20362],"categoryorder":"array","categoryarray":["Jan","Apr","Jul","Oct"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":3.9850560398505608,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":12.7521793275218},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(235,235,235,1)","gridwidth":0,"zeroline":false,"anchor":"y","title":{"text":"","font":{"color":"rgba(0,0,0,1)","family":"","size":15.940224159402243}},"hoverformat":".2f"},"yaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[-896584704.44050014,18828278793.2505],"tickmode":"array","ticktext":["$0B","$5B","$10B","$15B"],"tickvals":[-1.1920928955078125e-07,5000000000,10000000000,15000000000],"categoryorder":"array","categoryarray":["$0B","$5B","$10B","$15B"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":3.9850560398505608,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":12.7521793275218},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(235,235,235,1)","gridwidth":0,"zeroline":false,"anchor":"x","title":{"text":"Cumulative dollars","font":{"color":"rgba(0,0,0,1)","family":"","size":15.940224159402243}},"hoverformat":".2f"},"shapes":[{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","layer":"below","x0":0,"x1":1,"y0":0,"y1":1}],"showlegend":true,"legend":{"bgcolor":null,"bordercolor":null,"borderwidth":0,"font":{"color":"rgba(0,0,0,1)","family":"","size":12.7521793275218},"title":{"text":"","font":{"color":"rgba(0,0,0,1)","family":"","size":15.940224159402243}},"orientation":"h","x":0,"y":1.0800000000000001},"hovermode":"closest","barmode":"relative"},"config":{"doubleClick":"reset","modeBarButtonsToAdd":["hoverclosest","hovercompare"],"showSendToCloud":false},"source":"A","attrs":{"7ac346522c2b":{"x":{},"y":{},"type":"scatter"},"7ac343931706d":{"x":{},"y":{},"colour":{},"text":{}},"7ac3461c612c4":{"x":{},"y":{},"colour":{},"text":{}},"7ac34521c38ca":{"xintercept":{}},"7ac3438e200b":{"xmin":{},"xmax":{},"ymin":{},"ymax":{},"text":{}}},"cur_data":"7ac346522c2b","visdat":{"7ac346522c2b":["function (y) ","x"],"7ac343931706d":["function (y) ","x"],"7ac3461c612c4":["function (y) ","x"],"7ac34521c38ca":["function (y) ","x"],"7ac3438e200b":["function (y) ","x"]},"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.20000000000000001,"selected":{"opacity":1},"debounce":0},"shinyEvents":["plotly_hover","plotly_click","plotly_selected","plotly_relayout","plotly_brushed","plotly_brushing","plotly_clickannotation","plotly_doubleclick","plotly_deselect","plotly_afterplot","plotly_sunburstclick"],"base_url":"https://plot.ly"},"evals":[],"jsHooks":[]}</script>
<p>Cumulative claimed savings from terminated academic grants (DOGE) vs.&nbsp;verified remaining obligation (USAspending). Hover over the dashed vertical lines for policy event descriptions.</p>
</div>
</div>
<p>The blue line is DOGE’s figure. The red dashed line is the USAspending-derived estimate — total obligation minus total outlay, for grants where we could find a match. The gap is large and persistent. DOGE reports termination values; USAspending records actual obligations. For multi-year grants, DOGE may count future-year funding that was never guaranteed. That independent cross-reference is only possible because we pulled both sources.</p>
</section>
<section id="what-fields-are-being-cut" class="level3">
<h3 class="anchored" data-anchor-id="what-fields-are-being-cut">What Fields Are Being Cut?</h3>
<div class="cell">
<div class="cell-output-display">
<div class="plotly html-widget html-fill-item" id="htmlwidget-91271f15daa9bd939d75" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="htmlwidget-91271f15daa9bd939d75">{"x":{"data":[{"orientation":"v","width":[25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709],"base":[969892636.91999996,13163995648.23,1560940094.1700001,850988158.43000007],"x":[20120,20148,20179,20209],"y":[6076707,91028282.210000992,21219381.970000029,2772629.3199999332],"text":["<b>Agriculture & Food Science<\/b><br>Feb 2025<br>Claimed: $6.10M","<b>Agriculture & Food Science<\/b><br>Mar 2025<br>Claimed: $91.00M","<b>Agriculture & Food Science<\/b><br>Apr 2025<br>Claimed: $21.20M","<b>Agriculture & Food Science<\/b><br>May 2025<br>Claimed: $2.80M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(166,206,227,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Agriculture & Food Science","legendgroup":"Agriculture & Food Science","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":[25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709],"base":[4203709.3600000003,966798687.38999999,13156782864.33,1277888389.8800001,850887440.49000001,1255055186],"x":[20089,20120,20148,20179,20209,20362],"y":[68500,3093949.5299999714,7212783.8999996185,283051704.28999996,100717.94000005722,1914672.2400000095],"text":["<b>Arts, Humanities & Culture<\/b><br>Jan 2025<br>Claimed: $0.10M","<b>Arts, Humanities & Culture<\/b><br>Feb 2025<br>Claimed: $3.10M","<b>Arts, Humanities & Culture<\/b><br>Mar 2025<br>Claimed: $7.20M","<b>Arts, Humanities & Culture<\/b><br>Apr 2025<br>Claimed: $283.10M","<b>Arts, Humanities & Culture<\/b><br>May 2025<br>Claimed: $0.10M","<b>Arts, Humanities & Culture<\/b><br>Oct 2025<br>Claimed: $1.90M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(79,150,196,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Arts, Humanities & Culture","legendgroup":"Arts, Humanities & Culture","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":[25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709],"base":[966788287.38999999,1276853211.77,839214011.63999999,2927441.6000000001,1251006401.5599999],"x":[20120,20179,20209,20240,20362],"y":[10400,1035178.1100001335,11673428.850000024,0,4048784.4400000572],"text":["<b>Astrophysics, Space & Nuclear<\/b><br>Feb 2025<br>Claimed: $0.00M","<b>Astrophysics, Space & Nuclear<\/b><br>Apr 2025<br>Claimed: $1.00M","<b>Astrophysics, Space & Nuclear<\/b><br>May 2025<br>Claimed: $11.70M","<b>Astrophysics, Space & Nuclear<\/b><br>Jun 2025<br>Claimed: $0.00M","<b>Astrophysics, Space & Nuclear<\/b><br>Oct 2025<br>Claimed: $4.00M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(73,149,168,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Astrophysics, Space & Nuclear","legendgroup":"Astrophysics, Space & Nuclear","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":[25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709],"base":[966360822.10000002,3211880808.5999999,1253374724.6000001,836041763.94000006,1580307.55],"x":[20120,20148,20179,20209,20240],"y":[427465.28999996185,9944902055.7299995,23478487.169999838,3172247.6999999285,1347134.05],"text":["<b>Biological & Health Sciences<\/b><br>Feb 2025<br>Claimed: $0.40M","<b>Biological & Health Sciences<\/b><br>Mar 2025<br>Claimed: $9,944.90M","<b>Biological & Health Sciences<\/b><br>Apr 2025<br>Claimed: $23.50M","<b>Biological & Health Sciences<\/b><br>May 2025<br>Claimed: $3.20M","<b>Biological & Health Sciences<\/b><br>Jun 2025<br>Claimed: $1.30M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(167,215,141,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Biological & Health Sciences","legendgroup":"Biological & Health Sciences","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":[25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709],"base":[966202822.10000002,3154796074.8400002,1241422914.5799999,728618320.37,783434414.29000008],"x":[20120,20148,20179,20209,20362],"y":[158000,57084733.759999752,11951810.020000219,107423443.57000005,467571987.26999986],"text":["<b>Business & Economics<\/b><br>Feb 2025<br>Claimed: $0.20M","<b>Business & Economics<\/b><br>Mar 2025<br>Claimed: $57.10M","<b>Business & Economics<\/b><br>Apr 2025<br>Claimed: $12.00M","<b>Business & Economics<\/b><br>May 2025<br>Claimed: $107.40M","<b>Business & Economics<\/b><br>Oct 2025<br>Claimed: $467.60M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(105,187,84,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Business & Economics","legendgroup":"Business & Economics","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":[25.19999999999709,25.19999999999709],"base":[2930918240.8299999,1219062960.98],"x":[20148,20179],"y":[223877834.01000023,22359953.599999905],"text":["<b>Clinical & Community Health<\/b><br>Mar 2025<br>Claimed: $223.90M","<b>Clinical & Community Health<\/b><br>Apr 2025<br>Claimed: $22.40M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(93,158,67,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Clinical & Community Health","legendgroup":"Clinical & Community Health","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":[25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709],"base":[963087376.31999993,2872489807.48,1168877469.8500001,718516194.48000002,701176.55000000005,754339570.25999999],"x":[20120,20148,20179,20209,20240,20362],"y":[3115445.7800000906,58428433.349999905,50185491.129999876,10102125.889999986,879131,29094844.030000091],"text":["<b>Conservation & Sustainability<\/b><br>Feb 2025<br>Claimed: $3.10M","<b>Conservation & Sustainability<\/b><br>Mar 2025<br>Claimed: $58.40M","<b>Conservation & Sustainability<\/b><br>Apr 2025<br>Claimed: $50.20M","<b>Conservation & Sustainability<\/b><br>May 2025<br>Claimed: $10.10M","<b>Conservation & Sustainability<\/b><br>Jun 2025<br>Claimed: $0.90M","<b>Conservation & Sustainability<\/b><br>Oct 2025<br>Claimed: $29.10M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(222,154,137,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Conservation & Sustainability","legendgroup":"Conservation & Sustainability","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":[25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709],"base":[4203709.3600000003,564955488.8499999,2871485146.7200003,1159437835.8700001,714004386.60000002,727149263.63999999],"x":[20089,20120,20148,20179,20209,20362],"y":[0,398131887.47000003,1004660.759999752,9439633.9800000191,4511807.8799999952,27190306.620000005],"text":["<b>DEI & Social Equity<\/b><br>Jan 2025<br>Claimed: $0.00M","<b>DEI & Social Equity<\/b><br>Feb 2025<br>Claimed: $398.10M","<b>DEI & Social Equity<\/b><br>Mar 2025<br>Claimed: $1.00M","<b>DEI & Social Equity<\/b><br>Apr 2025<br>Claimed: $9.40M","<b>DEI & Social Equity<\/b><br>May 2025<br>Claimed: $4.50M","<b>DEI & Social Equity<\/b><br>Oct 2025<br>Claimed: $27.20M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(239,89,90,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"DEI & Social Equity","legendgroup":"DEI & Social Equity","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":[25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709],"base":[4193709.3599999999,564400006.77999997,2855366887.7200003,1112968470.74,690538892.33000004,598629322.83000004],"x":[20089,20120,20148,20179,20209,20362],"y":[10000.000000000466,555482.06999993324,16118259,46469365.130000114,23465494.269999981,128519940.80999994],"text":["<b>Engineering & Physical Sciences<\/b><br>Jan 2025<br>Claimed: $0.00M","<b>Engineering & Physical Sciences<\/b><br>Feb 2025<br>Claimed: $0.60M","<b>Engineering & Physical Sciences<\/b><br>Mar 2025<br>Claimed: $16.10M","<b>Engineering & Physical Sciences<\/b><br>Apr 2025<br>Claimed: $46.50M","<b>Engineering & Physical Sciences<\/b><br>May 2025<br>Claimed: $23.50M","<b>Engineering & Physical Sciences<\/b><br>Oct 2025<br>Claimed: $128.50M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(230,49,39,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Engineering & Physical Sciences","legendgroup":"Engineering & Physical Sciences","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":[25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709],"base":[4066406.6499999999,541869832.54999995,2687058634.5799999,560409187.52999997,516062944.86000001,96888.550000000003,577528079],"x":[20089,20120,20148,20179,20209,20240,20362],"y":[127302.70999999996,22530174.230000019,168308253.14000034,552559283.21000004,174475947.47000003,604288,21101243.830000043],"text":["<b>Environmental Science<\/b><br>Jan 2025<br>Claimed: $0.10M","<b>Environmental Science<\/b><br>Feb 2025<br>Claimed: $22.50M","<b>Environmental Science<\/b><br>Mar 2025<br>Claimed: $168.30M","<b>Environmental Science<\/b><br>Apr 2025<br>Claimed: $552.60M","<b>Environmental Science<\/b><br>May 2025<br>Claimed: $174.50M","<b>Environmental Science<\/b><br>Jun 2025<br>Claimed: $0.60M","<b>Environmental Science<\/b><br>Oct 2025<br>Claimed: $21.10M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(247,155,93,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Environmental Science","legendgroup":"Environmental Science","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":[25.19999999999709,25.19999999999709,25.19999999999709],"base":[540347608.33999991,2569664659.5799999,560375178.37],"x":[20120,20148,20179],"y":[1522224.2100000381,117393975,34009.159999966621],"text":["<b>International & Global Programs<\/b><br>Feb 2025<br>Claimed: $1.50M","<b>International & Global Programs<\/b><br>Mar 2025<br>Claimed: $117.40M","<b>International & Global Programs<\/b><br>Apr 2025<br>Claimed: $0.00M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(253,163,63,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"International & Global Programs","legendgroup":"International & Global Programs","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":[25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709],"base":[538671268.18999994,2565971388.5799999,554141433.35000002,573625235.22000003],"x":[20120,20148,20179,20362],"y":[1676340.1499999762,3693271,6233745.0199999809,3902843.7799999714],"text":["<b>Law, Policy & Governance<\/b><br>Feb 2025<br>Claimed: $1.70M","<b>Law, Policy & Governance<\/b><br>Mar 2025<br>Claimed: $3.70M","<b>Law, Policy & Governance<\/b><br>Apr 2025<br>Claimed: $6.20M","<b>Law, Policy & Governance<\/b><br>Oct 2025<br>Claimed: $3.90M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(251,130,15,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Law, Policy & Governance","legendgroup":"Law, Policy & Governance","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":[25.19999999999709,25.19999999999709,25.19999999999709],"base":[2552475211.6500001,524486900.08999997,501259430.12],"x":[20148,20179,20209],"y":[13496176.929999828,29654533.26000005,14803514.74000001],"text":["<b>Mathematics, Statistics & Data Science<\/b><br>Mar 2025<br>Claimed: $13.50M","<b>Mathematics, Statistics & Data Science<\/b><br>Apr 2025<br>Claimed: $29.70M","<b>Mathematics, Statistics & Data Science<\/b><br>May 2025<br>Claimed: $14.80M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(217,163,152,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Mathematics, Statistics & Data Science","legendgroup":"Mathematics, Statistics & Data Science","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":[25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709],"base":[0,129940.77,599938,1922110.7,1232504.7000000002,0,0,0,0],"x":[20089,20120,20148,20179,20209,20240,20270,20332,20362],"y":[4066406.6499999999,538541327.41999996,2551875273.6500001,522564789.38999999,500026925.42000002,96888.550000000003,366992,244049.35999999999,573625235.22000003],"text":["<b>Other Academic<\/b><br>Jan 2025<br>Claimed: $4.10M","<b>Other Academic<\/b><br>Feb 2025<br>Claimed: $538.50M","<b>Other Academic<\/b><br>Mar 2025<br>Claimed: $2,551.90M","<b>Other Academic<\/b><br>Apr 2025<br>Claimed: $522.60M","<b>Other Academic<\/b><br>May 2025<br>Claimed: $500.00M","<b>Other Academic<\/b><br>Jun 2025<br>Claimed: $0.10M","<b>Other Academic<\/b><br>Jul 2025<br>Claimed: $0.40M","<b>Other Academic<\/b><br>Sep 2025<br>Claimed: $0.20M","<b>Other Academic<\/b><br>Oct 2025<br>Claimed: $573.60M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(167,136,192,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Other Academic","legendgroup":"Other Academic","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"v","width":[25.19999999999709,25.19999999999709,25.19999999999709,25.19999999999709],"base":[0,0,0,0],"x":[20120,20148,20179,20209],"y":[129940.77,599938,1922110.7,1232504.7000000002],"text":["<b>Social Sciences & Ed Research<\/b><br>Feb 2025<br>Claimed: $0.10M","<b>Social Sciences & Ed Research<\/b><br>Mar 2025<br>Claimed: $0.60M","<b>Social Sciences & Ed Research<\/b><br>Apr 2025<br>Claimed: $1.90M","<b>Social Sciences & Ed Research<\/b><br>May 2025<br>Claimed: $1.20M"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(106,61,154,0.85)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Social Sciences & Ed Research","legendgroup":"Social Sciences & Ed Research","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20089,20120,20148,20179,20209,20240,20270,20332,20362],"y":[4272209.3600000003,975969343.91999996,13255023930.440001,1582159476.1400001,853760787.75,2927441.6000000001,366992,244049.35999999999,1256969858.24],"text":["<b>Jan 2025<\/b><br>Total claimed: $4.30M","<b>Feb 2025<\/b><br>Total claimed: $976.00M","<b>Mar 2025<\/b><br>Total claimed: $13,255.00M","<b>Apr 2025<\/b><br>Total claimed: $1,582.20M","<b>May 2025<\/b><br>Total claimed: $853.80M","<b>Jun 2025<\/b><br>Total claimed: $2.90M","<b>Jul 2025<\/b><br>Total claimed: $0.40M","<b>Sep 2025<\/b><br>Total claimed: $0.20M","<b>Oct 2025<\/b><br>Total claimed: $1,257.00M"],"type":"scatter","mode":"lines","line":{"width":3.7795275590551185,"color":"rgba(0,0,0,1)","dash":"solid"},"hoveron":"points","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20089,20120,20148,20179,20209,20240,20270,20332,20362],"y":[4272209.3600000003,975969343.91999996,13255023930.440001,1582159476.1400001,853760787.75,2927441.6000000001,366992,244049.35999999999,1256969858.24],"text":"","type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgba(0,0,0,1)","opacity":1,"size":6.8031496062992138,"symbol":"circle","line":{"width":1.8897637795275593,"color":"rgba(0,0,0,1)"}},"hoveron":"points","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20108,20108,null,20115,20115,null,20117,20117,null,20132,20132,null,20136,20136,null,20138,20138,null,20145,20145,null,20153,20153,null,20157,20157,null,20180,20180],"y":[-662751196.52200007,13917775126.962,null,-662751196.52200007,13917775126.962,null,-662751196.52200007,13917775126.962,null,-662751196.52200007,13917775126.962,null,-662751196.52200007,13917775126.962,null,-662751196.52200007,13917775126.962,null,-662751196.52200007,13917775126.962,null,-662751196.52200007,13917775126.962,null,-662751196.52200007,13917775126.962,null,-662751196.52200007,13917775126.962],"text":"","type":"scatter","mode":"lines","line":{"width":1.5118110236220474,"color":"rgba(102,102,102,0.7)","dash":"dash"},"hoveron":"points","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20176,20176,20184,20184,20176],"y":[0,13255023930.440001,13255023930.440001,0,0],"text":"<b>April 02, 2025<\/b><br>NEH/IMLS termination wave reported","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20128,20128,20136,20136,20128],"y":[0,13255023930.440001,13255023930.440001,0,0],"text":"<b>February 13, 2025<\/b><br>ED: $350M+ cancellations","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20132,20132,20140,20140,20132],"y":[0,13255023930.440001,13255023930.440001,0,0],"text":"<b>February 17, 2025<\/b><br>ED: $600M+ teacher-training cuts","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20134,20134,20142,20142,20134],"y":[0,13255023930.440001,13255023930.440001,0,0],"text":"<b>February 19, 2025<\/b><br>ED: $226M comprehensive center cuts","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20141,20141,20149,20149,20141],"y":[0,13255023930.440001,13255023930.440001,0,0],"text":"<b>February 26, 2025<\/b><br>DOGE cost-efficiency EO (30-day review window)","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20104,20104,20112,20112,20104],"y":[0,13255023930.440001,13255023930.440001,0,0],"text":"<b>January 20, 2025<\/b><br>DOGE established; foreign aid review EO","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20111,20111,20119,20119,20111],"y":[0,13255023930.440001,13255023930.440001,0,0],"text":"<b>January 27, 2025<\/b><br>OMB M-25-13 pause memo","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20113,20113,20121,20121,20113],"y":[0,13255023930.440001,13255023930.440001,0,0],"text":"<b>January 29, 2025<\/b><br>OMB M-25-13 rescinded (M-25-14)","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20149,20149,20157,20157,20149],"y":[0,13255023930.440001,13255023930.440001,0,0],"text":"<b>March 06, 2025<\/b><br>Court extends block on broad freeze","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[20153,20153,20161,20161,20153],"y":[0,13255023930.440001,13255023930.440001,0,0],"text":"<b>March 10, 2025<\/b><br>State: ~83% of USAID programs cut","type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"transparent","dash":"solid"},"fill":"toself","fillcolor":"rgba(190,190,190,0.01)","hoveron":"fills","showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null}],"layout":{"margin":{"t":61.695309256953102,"r":26.567040265670411,"b":56.587795765877978,"l":71.996679119966814},"paper_bgcolor":"rgba(255,255,255,1)","font":{"color":"rgba(0,0,0,1)","family":"","size":15.940224159402243},"title":{"text":"Monthly Academic Grant Cuts by Research Domain","font":{"color":"rgba(0,0,0,1)","family":"","size":19.128268991282692},"x":0,"xref":"paper"},"xaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[20061.490000000002,20389.509999999998],"tickmode":"array","ticktext":["Jan","Apr","Jul","Oct"],"tickvals":[20089,20179,20270,20362],"categoryorder":"array","categoryarray":["Jan","Apr","Jul","Oct"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":3.9850560398505608,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":12.7521793275218},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(235,235,235,1)","gridwidth":0,"zeroline":false,"anchor":"y","title":{"text":"","font":{"color":"rgba(0,0,0,1)","family":"","size":15.940224159402243}},"hoverformat":".2f"},"yaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[-662751196.52200007,13917775126.962],"tickmode":"array","ticktext":["$0B","$5B","$10B"],"tickvals":[0,5000000000,10000000000],"categoryorder":"array","categoryarray":["$0B","$5B","$10B"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":3.9850560398505608,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":12.7521793275218},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(235,235,235,1)","gridwidth":0,"zeroline":false,"anchor":"x","title":{"text":"DOGE claimed savings","font":{"color":"rgba(0,0,0,1)","family":"","size":15.940224159402243}},"hoverformat":".2f"},"shapes":[{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","layer":"below","x0":0,"x1":1,"y0":0,"y1":1}],"showlegend":true,"legend":{"bgcolor":null,"bordercolor":null,"borderwidth":0,"font":{"color":"rgba(0,0,0,1)","family":"","size":12.7521793275218},"title":{"text":"Domain","font":{"color":"rgba(0,0,0,1)","family":"","size":15.940224159402243}},"orientation":"v","x":1.01,"y":1},"hovermode":"closest","barmode":"relative"},"config":{"doubleClick":"reset","modeBarButtonsToAdd":["hoverclosest","hovercompare"],"showSendToCloud":false},"source":"A","attrs":{"7ac3464083632":{"x":{},"y":{},"fill":{},"text":{},"type":"bar"},"7ac3463142680":{"x":{},"y":{},"text":{}},"7ac342174369d":{"x":{},"y":{}},"7ac3424095840":{"xintercept":{}},"7ac347c996940":{"xmin":{},"xmax":{},"ymin":{},"ymax":{},"text":{}}},"cur_data":"7ac3464083632","visdat":{"7ac3464083632":["function (y) ","x"],"7ac3463142680":["function (y) ","x"],"7ac342174369d":["function (y) ","x"],"7ac3424095840":["function (y) ","x"],"7ac347c996940":["function (y) ","x"]},"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.20000000000000001,"selected":{"opacity":1},"debounce":0},"shinyEvents":["plotly_hover","plotly_click","plotly_selected","plotly_relayout","plotly_brushed","plotly_brushing","plotly_clickannotation","plotly_doubleclick","plotly_deselect","plotly_afterplot","plotly_sunburstclick"],"base_url":"https://plot.ly"},"evals":[],"jsHooks":[]}</script>
<p>Monthly DOGE claimed savings from academic grants, by research domain. Hover the vertical event lines for descriptions.</p>
</div>
</div>
<div class="cell">
<div class="cell-output-display">
<div class="plotly html-widget html-fill-item" id="htmlwidget-382c6cc12cfbeffedaef" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="htmlwidget-382c6cc12cfbeffedaef">{"x":{"data":[{"orientation":"h","width":[0.89999999999999858,0.89999999999999858,0.89999999999999858,0.89999999999999858,0.89999999999999858,0.89999999999999947,0.90000000000000036,0.90000000000000036,0.90000000000000036,0.90000000000000036,0.90000000000000013,0.89999999999999991],"base":[0,0,0,0,0,0,0,0,0,0,0,0],"x":[9973327389.9400005,4691407887.6599998,644189974.62,295442327.89999998,246237787.61000001,215138541.28,121097000.5,118950208.37,57954224.93,16767791.4,15506199.949999999,3884494.1699999999],"y":[15,14,12,10,9,8,6,5,4,3,2,1],"text":["<b>Biological & Health Sciences<\/b><br>Claimed savings: $9.97B<br>Grants terminated: 458","<b>Other Academic<\/b><br>Claimed savings: $4.69B<br>Grants terminated: 2,926","<b>Business & Economics<\/b><br>Claimed savings: $0.64B<br>Grants terminated: 75","<b>Arts, Humanities & Culture<\/b><br>Claimed savings: $0.30B<br>Grants terminated: 2,614","<b>Clinical & Community Health<\/b><br>Claimed savings: $0.25B<br>Grants terminated: 59","<b>Engineering & Physical Sciences<\/b><br>Claimed savings: $0.22B<br>Grants terminated: 214","<b>Agriculture & Food Science<\/b><br>Claimed savings: $0.12B<br>Grants terminated: 78","<b>International & Global Programs<\/b><br>Claimed savings: $0.12B<br>Grants terminated: 32","<b>Mathematics, Statistics & Data Science<\/b><br>Claimed savings: $0.06B<br>Grants terminated: 96","<b>Astrophysics, Space & Nuclear<\/b><br>Claimed savings: $0.02B<br>Grants terminated: 14","<b>Law, Policy & Governance<\/b><br>Claimed savings: $0.02B<br>Grants terminated: 27","<b>Social Sciences & Ed Research<\/b><br>Claimed savings: $0.00B<br>Grants terminated: 26"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(186,176,172,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Other academic domains","legendgroup":"Other academic domains","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"orientation":"h","width":[0.89999999999999858,0.89999999999999858,0.90000000000000036],"base":[0,0,0],"x":[939706492.59000003,440278296.70999998,151805471.18000001],"y":[13,11,7],"text":["<b>Environmental Science<\/b><br>Claimed savings: $0.94B<br>Grants terminated: 324","<b>DEI & Social Equity<\/b><br>Claimed savings: $0.44B<br>Grants terminated: 202","<b>Conservation & Sustainability<\/b><br>Claimed savings: $0.15B<br>Grants terminated: 211"],"type":"bar","textposition":"none","marker":{"autocolorscale":false,"color":"rgba(225,87,89,1)","line":{"width":1.8897637795275593,"color":"transparent"}},"name":"Politically scrutinized","legendgroup":"Politically scrutinized","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null}],"layout":{"margin":{"t":61.695309256953102,"r":39.850560398505614,"b":59.244499792445012,"l":272.84350352843518},"paper_bgcolor":"rgba(255,255,255,1)","font":{"color":"rgba(0,0,0,1)","family":"","size":15.940224159402243},"title":{"text":"DOGE Grant Cuts by Academic Research Domain","font":{"color":"rgba(0,0,0,1)","family":"","size":19.128268991282692},"x":0,"xref":"paper"},"xaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[-498666369.49700004,10471993759.437],"tickmode":"array","ticktext":["$0B","$3B","$5B","$8B","$10B"],"tickvals":[0,2500000000,5000000000,7500000000,10000000000],"categoryorder":"array","categoryarray":["$0B","$3B","$5B","$8B","$10B"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":3.9850560398505608,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":12.7521793275218},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(235,235,235,1)","gridwidth":0,"zeroline":false,"anchor":"y","title":{"text":"DOGE claimed savings (billions)","font":{"color":"rgba(0,0,0,1)","family":"","size":15.940224159402243}},"hoverformat":".2f"},"yaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[0.40000000000000002,15.6],"tickmode":"array","ticktext":["Social Sciences & Ed Research","Law, Policy & Governance","Astrophysics, Space & Nuclear","Mathematics, Statistics & Data Science","International & Global Programs","Agriculture & Food Science","Conservation & Sustainability","Engineering & Physical Sciences","Clinical & Community Health","Arts, Humanities & Culture","DEI & Social Equity","Business & Economics","Environmental Science","Other Academic","Biological & Health Sciences"],"tickvals":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],"categoryorder":"array","categoryarray":["Social Sciences & Ed Research","Law, Policy & Governance","Astrophysics, Space & Nuclear","Mathematics, Statistics & Data Science","International & Global Programs","Agriculture & Food Science","Conservation & Sustainability","Engineering & Physical Sciences","Clinical & Community Health","Arts, Humanities & Culture","DEI & Social Equity","Business & Economics","Environmental Science","Other Academic","Biological & Health Sciences"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":3.9850560398505608,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":12.7521793275218},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(235,235,235,1)","gridwidth":0,"zeroline":false,"anchor":"x","title":{"text":"","font":{"color":"rgba(0,0,0,1)","family":"","size":15.940224159402243}},"hoverformat":".2f"},"shapes":[{"type":"rect","fillcolor":null,"line":{"color":null,"width":0,"linetype":[]},"yref":"paper","xref":"paper","layer":"below","x0":0,"x1":1,"y0":0,"y1":1}],"showlegend":true,"legend":{"bgcolor":null,"bordercolor":null,"borderwidth":0,"font":{"color":"rgba(0,0,0,1)","family":"","size":12.7521793275218},"title":{"text":"","font":{"color":"rgba(0,0,0,1)","family":"","size":15.940224159402243}},"orientation":"h","x":0,"y":-0.14999999999999999},"hovermode":"closest","barmode":"relative"},"config":{"doubleClick":"reset","modeBarButtonsToAdd":["hoverclosest","hovercompare"],"showSendToCloud":false},"source":"A","attrs":{"7ac344ee5214":{"x":{},"y":{},"fill":{},"text":{},"type":"bar"}},"cur_data":"7ac344ee5214","visdat":{"7ac344ee5214":["function (y) ","x"]},"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.20000000000000001,"selected":{"opacity":1},"debounce":0},"shinyEvents":["plotly_hover","plotly_click","plotly_selected","plotly_relayout","plotly_brushed","plotly_brushing","plotly_clickannotation","plotly_doubleclick","plotly_deselect","plotly_afterplot","plotly_sunburstclick"],"base_url":"https://plot.ly"},"evals":[],"jsHooks":[]}</script>
<p>Total claimed savings by academic research domain. Domain is assigned by keyword matching on grant descriptions.</p>
</div>
</div>
<p>Domain classification here is keyword-based — a <code>case_when()</code> applied to lowercased grant descriptions. It’s imperfect, and the “Other Academic” category is large because many grant descriptions use generic language. But the directional signal is consistent: DEI, environmental science, and conservation grants appear concentrated in the waves that follow specific policy events, not distributed evenly across the year.</p>
<hr>
</section>
</section>
<section id="caveats-and-limitations" class="level2">
<h2 class="anchored" data-anchor-id="caveats-and-limitations">Caveats and Limitations</h2>
<p><strong>DOGE’s data has quality issues.</strong> Grants have been added, removed, and modified on the DOGE website without announcement. This analysis uses a snapshot pulled on March 10, 2026.</p>
<p><strong>USAspending match coverage is partial.</strong> Only grants with a traceable award ID in the DOGE link field could be matched. Match rates vary by agency.</p>
<p><strong>“Savings” is not “money returned.”</strong> When a multi-year grant is terminated, DOGE records the full remaining authorized value. If future-year funding was never obligated, it was never going to be spent regardless.</p>
<p><strong>Domain classification is approximate.</strong> A grant about “biodiversity in coastal ecosystems” might land in Environmental Science or Biological Sciences depending on which keywords hit first. Treat the domain column as directionally informative, not precise.</p>
<hr>
</section>
<section id="getting-started-yourself" class="level2">
<h2 class="anchored" data-anchor-id="getting-started-yourself">Getting Started Yourself</h2>
<p>If this is your first time scraping or using APIs in R, here’s the minimum you need:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">install.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;">"httr2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rvest"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"purrr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dplyr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"targets"</span>))</span></code></pre></div></div>
<p>The full code for this project — <code>_targets.R</code> and all the <code>R/</code> functions — is in the <a href="https://github.com/rellimylime/federal-grant-watch">federal-grant-watch repository</a>.</p>
<p>The best way to learn this stuff is to pick a dataset you actually care about and try to get it. Every scraping project teaches you something different.</p>
<hr>
</section>
<section id="data-methods" class="level2">
<h2 class="anchored" data-anchor-id="data-methods">Data &amp; Methods</h2>
<p>Data pulled from the <a href="https://api.doge.gov/savings/grants">DOGE public grants API</a> (snapshot: March 10, 2026) and enriched with award-level data from <a href="https://api.usaspending.gov/">USAspending.gov</a>. Policy event news scraped from <a href="https://www.govexec.com/topic/doge/">GovExec</a>.</p>
<p>Academic scope defined using recipient name matching (university, college, research institute) plus keyword signals in agency and description fields. Domain classification used a keyword-priority <code>case_when()</code> on lowercased grant text, with DEI keywords checked first to prevent false positives from terms like “biodiversity.”</p>
<p>Analysis in R. Key packages: <code>targets</code>, <code>httr2</code>, <code>rvest</code>, <code>purrr</code>, <code>dplyr</code>, <code>ggplot2</code>, <code>plotly</code>.</p>


</section>

 ]]></description>
  <category>Personal</category>
  <category>R</category>
  <category>Data Science</category>
  <category>Web Scraping</category>
  <category>Tutorial</category>
  <guid>https://rellimylime.github.io/posts/federal-grant-watch/</guid>
  <pubDate>Thu, 12 Mar 2026 07:00:00 GMT</pubDate>
  <media:content url="https://rellimylime.github.io/images/posts/federal-grant-watch/scraping-cover.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Driving Under the Influence of Statistics</title>
  <dc:creator>Emily Miller</dc:creator>
  <link>https://rellimylime.github.io/posts/eds240-final/</link>
  <description><![CDATA[ 





<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>Here’s a statistic: sober drivers cause 88% of all crashes in California. Alarming, right? You might want to reconsider that designated driver situation.</p>
<p>That statistic is real. It comes from the California Highway Patrol’s SWITRS database: tens of thousands of reported crashes from 2024, cleaned, filtered to at-fault drivers, summarized honestly and completely misleading.</p>
<p><em>Crash Course in Bad Statistics</em> is a deadpan parody of the kind of scary public health PSA you’ve probably scrolled past, with dark backgrounds, bold shocking claims, authoritative(-looking) charts. Every number in it is <em>technically</em> accurate. Every conclusion it nudges you toward is wrong. The goal isn’t to convince you that drunk driving is fine (it isn’t). The goal is to show how easy it is to weaponize real data through selective framing, and to make you a little more suspicious the next time a statistic sounds exactly as alarming as someone needs it to.</p>
<hr>
</section>
<section id="the-infographic" class="level2">
<h2 class="anchored" data-anchor-id="the-infographic">The Infographic</h2>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rellimylime.github.io/images/posts/eds240-final/draft2.png" class="img-fluid figure-img" style="width:85.0%"></p>
<figcaption>A dark PSA-style infographic set inside a car interior illustration. The rearview mirror shows raw crash counts by sobriety (sober drivers: 88%). The dashboard gauges compare crash timing by hour; all crashes peak at rush hour while alcohol-impaired crashes peak at 2am. A roadside billboard shows estimated deaths broken down by gender and sobriety.</figcaption>
</figure>
</div>
<hr>
</section>
<section id="design-process" class="level2">
<h2 class="anchored" data-anchor-id="design-process">Design Process</h2>
<section id="graphic-form" class="level3">
<h3 class="anchored" data-anchor-id="graphic-form">1. Graphic Form</h3>
<p>Each of the three panels uses a conventional chart type: horizontal bar, polar clock (radial bar graph), column bar, chosen specifically because they look trustworthy. The horizontal bar for Myth 1 is the starkest way to display the 7:1 raw count ratio; there’s no visual complexity to distract from the number. The polar clocks for Myth 2 lean into the car dashboard aesthetic of the overall infographic design while also being a genuinely useful format for 24-hour temporal data. The key manipulation there is subtle: the right clock’s y-axis is expanded to 2.2x, which compresses the alcohol bars to fill only about 45% of the clock face, making the late-night peak look minor rather than concentrated. The column bar for Myth 3 is intentionally conventional; the absurdity of the “sober men are deadlier” conclusion lands harder when the chart looks completely normal. All three chart types read as authoritative. That’s the point.</p>
<hr>
</section>
<section id="text-titles-labels-annotations" class="level3">
<h3 class="anchored" data-anchor-id="text-titles-labels-annotations">2. Text (Titles, Labels, Annotations)</h3>
<p>All titles are all-caps, which registers as alarm or warning, the visual register of a PSA. “SOBER DRIVERS CAUSE MORE CRASHES” is designed to read like a headline before the viewer processes that it’s a chart title. The Myth 3 title uses <code>ggtext::element_markdown()</code> to render “DEADLIER” in red inline, making an absurd claim feel like breaking news. Subtitles carry the technically-true framing without fabricating anything.</p>
<p>The Myth 2 patchwork annotation, “IF DRUNK DRIVING CAUSED THE DANGER, THE PEAKS WOULD ALIGN,” does the most rhetorical work of any text in the infographic. It states the misleading logical argument explicitly so the viewer doesn’t have to make the inferential leap themselves. Presenting the conclusion we want the viewer to draw up front also keeps them from doing too much of their own interpreting and thinking about how the data is represented and what it could mean. Similarly, the Myth 3 dashed reference line is annotated with the cumulative drunk-driver death count so the viewer can immediately make the sober-men-vs-all-drunk comparison without doing math.</p>
<hr>
</section>
<section id="themes" class="level3">
<h3 class="anchored" data-anchor-id="themes">3. Themes</h3>
<p>All three charts share a <code>dashboard_theme</code> built on <code>theme_void()</code>, which strips all default chart chrome and leaves only the data and explicitly added elements. Near-black backgrounds (<code>#07091E</code> for charts, <code>#0D0F28</code> for the patchwork panel) establish the night-driving, danger aesthetic immediately. Grid lines exist (<code>#1A2550</code>) but are barely visible, structural scaffolding rather than decoration, keeping the data-ink ratio high. Individual charts override the shared theme as needed: the polar clocks get larger axis text since the labels sit on a curved face; the bar charts suppress x-axis text since the values are labeled directly on the bars.</p>
<hr>
</section>
<section id="colors" class="level3">
<h3 class="anchored" data-anchor-id="colors">4. Colors</h3>
<p>The palette uses two data accents: <code>#FF3333</code> (red) for alcohol-impaired and <code>#4DB8FF</code> (blue) for sober. This maps directly onto the danger/safe cultural association; red means stop, blue means calm, which is itself a design manipulation. Blue sober bars that dominate the chart read as the dangerous category, which creates a subtle cognitive dissonance that reinforces the parody’s argument. All typography uses cool whites and blues (<code>#FFFFFF</code>, <code>#A8BCD8</code>, <code>#7A8EBB</code>) against near-black backgrounds for high contrast ratios. On colorblindness: red-blue is readable for deuteranopia (the most common form) but can fail for protanopia. Given that the distinction between the two groups is also encoded in bar position and direct labeling, this is an acceptable tradeoff, though not ideal.</p>
<hr>
</section>
<section id="typography" class="level3">
<h3 class="anchored" data-anchor-id="typography">5. Typography</h3>
<p>Rajdhani (loaded via <code>sysfonts</code> and <code>showtext</code>) is geometric, condensed, and slightly techno-adjacent; it reads like an instrument panel readout, which fits the dashboard framing. The weight hierarchy is straightforward: bold 35pt titles, 20pt subtitles, 14–25pt axis labels depending on the chart. The larger axis sizes on the polar clocks (25pt) are necessary because the labels orbit the chart face rather than sitting on a flat axis, so they need more visual weight to be legible. <code>showtext_auto()</code> ensures consistent rendering across both PDF and HTML outputs, which matters since the charts were exported to PDF for Affinity assembly before being embedded here. In Affinity, Shatterboxx font was used for “CRASH” and Skyline for “COURSE”. I used blacked out letters from Skyline to continue the pattern across the horizon.</p>
<hr>
</section>
<section id="general-design" class="level3">
<h3 class="anchored" data-anchor-id="general-design">6. General Design</h3>
<p>The three R-generated charts were exported as PDFs and assembled in Affinity Designer inside a car interior illustration: rearview mirror, dashboard gauges, and a roadside billboard. That spatial anchoring does the visual hierarchy work: the eye enters through the mirror (Myth 1, the hook), moves to the dashboard gauges (Myth 2, the temporal argument), then lands on the billboard (Myth 3, the punchline). Each panel handles exactly one misleading argument, which keeps information density low and makes each claim feel self-contained. The “CRASH Course / BAD STATISTICS” text painted on the road serves as the satirical frame anchor; it’s the one element that breaks the deadpan and signals that the infographic knows exactly what it’s doing.</p>
<hr>
</section>
<section id="contextualizing-the-data" class="level3">
<h3 class="anchored" data-anchor-id="contextualizing-the-data">7. Contextualizing the Data</h3>
<p>Each panel hides the same thing: the denominator. Myth 1 shows raw crash counts without accounting for the fact that sober drivers vastly outnumber drunk drivers on the road, so more exposure means more crashes, regardless of risk per mile. Myth 2 shows crash timing patterns without mentioning that alcohol’s fatality rate is higher at every hour of the day, not just after midnight. Myth 3 uses estimated deaths (crashes × fatality rate) to make the absolute count comparison, but buries the fact that drunk drivers have roughly double the fatality rate of sober drivers in every gender group; when you rank by rate rather than total, the story inverts completely.</p>
<p>This blog post is the contextualization layer. The infographic is designed to be encountered first, to let the misleading framing land; the critique comes second. That sequence is intentional: it’s easier to understand how data manipulation works when you’ve already been manipulated by it.</p>
<hr>
</section>
<section id="centering-the-primary-message" class="level3">
<h3 class="anchored" data-anchor-id="centering-the-primary-message">8. Centering the Primary Message</h3>
<p>The infographic’s surface argument is “driving is just dangerous, and you might as well have a good time.” The real message is that framing choices (counts vs.&nbsp;rates, selective time windows, variable substitution) can make the same dataset tell opposite stories, and that this is worth being skeptical about.</p>
<p>The biggest design challenge was tone calibration. Playing it completely straight risks the infographic reading as genuine advocacy for drunk driving, which is the wrong takeaway entirely. The “CRASH Course / BAD STATISTICS” road text was added late in the process specifically to solve this: it’s the one moment where the infographic winks at the viewer. Whether that’s enough of a signal, or whether it needs to be more prominent, was the main feedback question going into peer review.</p>
<hr>
</section>
<section id="accessibility" class="level3">
<h3 class="anchored" data-anchor-id="accessibility">9. Accessibility</h3>
<p>The red-blue palette was checked for deuteranopia readability; the two hues remain distinguishable. Protanopia is a known limitation; red and blue can appear similar, though the distinction is also encoded positionally and through direct bar labeling, which provides a redundant channel. All text is rendered against near-black backgrounds with high contrast ratios, and Rajdhani at the sizes used (14pt minimum) is legible on dark backgrounds. Alt text is provided for the infographic image. Code chunks in the blog body use <code>echo: false</code> so only the output renders; the full code is available in the collapsed chunk at the end for anyone who wants to dig in.</p>
<hr>
</section>
<section id="dei-lens" class="level3">
<h3 class="anchored" data-anchor-id="dei-lens">10. DEI Lens</h3>
<p>The Myth 3 panel uses a binary gender breakdown (M/F) because SWITRS only records two categories; non-binary drivers are not represented in this data, which is a real limitation worth naming. The analysis reflects who was recorded in crash reports, not the full population of California drivers.</p>
<p>There’s also a broader framing consideration. The “sober men are the real danger” argument the infographic makes is a parody, but it mirrors a genuine rhetorical pattern: using statistics to redirect accountability away from a risky behavior and onto a demographic variable instead. That’s not just a data literacy problem, it’s how policy debates get muddied. The blog post making that connection explicit is the responsible satire move. The infographic alone, without this context, would just be irresponsible.</p>
<hr>
</section>
</section>
<section id="reflections" class="level2">
<h2 class="anchored" data-anchor-id="reflections">Reflections</h2>
<p>The car interior framing worked better than expected; spatially anchoring each chart to a physical part of the vehicle gave the layout a natural reading order without needing explicit numbering or arrows. The polar clocks in particular felt like the right chart type the moment they were placed inside dashboard gauges.</p>
<p>The harder part was finding the right level of misleading. Early drafts were too obviously wrong; later ones were convincing enough that peer reviewers weren’t sure if the satire was intentional. The road text was a late addition specifically to thread that needle. Whether it lands depends on the reader, which is probably the point, data manipulation works best when it doesn’t feel like manipulation.</p>
<p>What this project revealed most clearly: the gap between “technically accurate” and “honestly communicated” is enormous, and closing it requires deliberate choices at every step of the design process.</p>
<hr>
</section>
<section id="explore-the-code" class="level2">
<h2 class="anchored" data-anchor-id="explore-the-code">Explore the Code</h2>
<p>The three charts in this infographic were generated in R using <code>{ggplot2}</code> and assembled in <a href="https://affinity.serif.com/en-us/designer/">Affinity Designer</a>. The full code is in the expandable chunk below.</p>
<div class="cell">
<details class="code-fold">
<summary>Show full code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Setup ─────────────────────────────────────────────────────────────────────</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(here)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(scales)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sysfonts)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(showtext)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggtext)</span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Raw files: CA Highway Patrol SWITRS database, crashes + parties CSVs (2024).</span></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Download portal: https://iswitrs.chp.ca.gov</span></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cleaning, filtering to at-fault drivers, and joining the two files is</span></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># documented in exploration.qmd. The processed file was saved there with:</span></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   write_csv(crash_clean, here("data/processed/crash_clean.csv"))</span></span>
<span id="cb1-15">crash_clean <span class="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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/processed/crash_clean.csv"</span>),</span>
<span id="cb1-16">                        <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="cb1-17"></span>
<span id="cb1-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "rajdhani" is the family name used in all family = "rajdhani" calls below</span></span>
<span id="cb1-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">font_add_google</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rajdhani"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</span>)</span>
<span id="cb1-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">showtext_auto</span>()</span>
<span id="cb1-21"></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Color palette ─────────────────────────────────────────────────────────────</span></span>
<span id="cb1-23">PAL <span class="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="cb1-24">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot_bg      =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#07091E"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># chart background - deep indigo</span></span>
<span id="cb1-25">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bg           =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0D0F28"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># page background, used in myth 2 patchwork panel</span></span>
<span id="cb1-26">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alcohol      =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FF3333"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># red - alcohol-impaired / danger</span></span>
<span id="cb1-27">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sober        =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#4DB8FF"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># blue - sober / safe</span></span>
<span id="cb1-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text_primary =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FFFFFF"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># titles</span></span>
<span id="cb1-29">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text_muted   =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7A8EBB"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># subtitles</span></span>
<span id="cb1-30">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text_axis    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#A8BCD8"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># axis labels</span></span>
<span id="cb1-31">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grid_line    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1A2550"</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># grid lines</span></span>
<span id="cb1-32">)</span>
<span id="cb1-33"></span>
<span id="cb1-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Shared theme ──────────────────────────────────────────────────────────────</span></span>
<span id="cb1-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Applied to all three charts; individual charts add overrides on top.</span></span>
<span id="cb1-36">dashboard_theme <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb1-38">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.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> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>plot_bg, <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>
<span id="cb1-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.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> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>plot_bg, <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>
<span id="cb1-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major.y =</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> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grid_line),</span>
<span id="cb1-41">    <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>
<span id="cb1-42">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text_axis, <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;">25</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># large enough to read on polar clock face</span></span>
<span id="cb1-43">      <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;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</span></span>
<span id="cb1-44">    ),</span>
<span id="cb1-45">    <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>
<span id="cb1-46">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text_primary, <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># centered</span></span>
<span id="cb1-47">      <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;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</span>,</span>
<span id="cb1-48">      <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;">35</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;">b =</span> <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;"># tight gap below title</span></span>
<span id="cb1-49">    ),</span>
<span id="cb1-50">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(</span>
<span id="cb1-51">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text_muted, <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># centered</span></span>
<span id="cb1-52">      <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;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</span>,</span>
<span id="cb1-53">      <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;">t =</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;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># small top, more breathing room below</span></span>
<span id="cb1-54">    ),</span>
<span id="cb1-55">    <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;">10</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;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>)        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># top right bottom left</span></span>
<span id="cb1-56">  )</span>
<span id="cb1-57"></span>
<span id="cb1-58"></span>
<span id="cb1-59"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Visualization 1: Myth 1 ───────────────────────────────────────────────────</span></span>
<span id="cb1-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Sober Drivers Don't Cause Most Crashes"</span></span>
<span id="cb1-61"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shows raw crash counts by sobriety. Sober drivers account for ~88% of crashes.</span></span>
<span id="cb1-62"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># By reporting counts instead of rates, the chart makes sober driving look like</span></span>
<span id="cb1-63"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the bigger danger - technically true, deliberately misleading.</span></span>
<span id="cb1-64"></span>
<span id="cb1-65">fmt_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;">label_number</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale_cut =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cut_short_scale</span>())</span>
<span id="cb1-66"></span>
<span id="cb1-67">myth1_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> crash_clean <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-68">  <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>(sobriety)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-69">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(collision_id, sobriety) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-70">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(sobriety) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-71">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb1-72">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(n <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 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="cb1-73">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">driver =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(sobriety <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;">"No alcohol"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOBER</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;">DRIVERS"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DRUNK</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;">DRIVERS"</span>),</span>
<span id="cb1-74">    <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt_n</span>(n), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"  \u00b7  "</span>, pct, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%  "</span>)</span>
<span id="cb1-75">  )</span>
<span id="cb1-76"></span>
<span id="cb1-77"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(myth1_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> n, <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;">reorder</span>(driver, n), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> sobriety)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-78">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># narrower than default; breathing room between bars</span></span>
<span id="cb1-79">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(</span>
<span id="cb1-80">    <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;">label =</span> label),</span>
<span id="cb1-81">    <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;">1.05</span>,                      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># flush against right edge of bar, inside</span></span>
<span id="cb1-82">    <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;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb1-83">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-84">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(</span>
<span id="cb1-85">    <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;">"No alcohol"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sober, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alcohol impaired"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>alcohol),</span>
<span id="cb1-86">    <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>
<span id="cb1-87">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-88">  <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;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expansion</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mult =</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.02</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># left padding</span></span>
<span id="cb1-89">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb1-90">    <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;">"SOBER DRIVERS CAUSE MORE CRASHES"</span>,</span>
<span id="cb1-91">    <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;">"Raw crash counts \u00b7 CA SWITRS 2024"</span>,</span>
<span id="cb1-92">    <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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<span id="cb1-93">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-94">  dashboard_theme <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-95">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb1-96">    <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_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text_axis, <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;">18</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</span>),</span>
<span id="cb1-97">    <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_blank</span>(),</span>
<span id="cb1-98">    <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="cb1-99">  )</span>
<span id="cb1-100"></span>
<span id="cb1-101"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"myth1.pdf"</span>)</span>
<span id="cb1-102"></span>
<span id="cb1-103"></span>
<span id="cb1-104"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Visualization 2: Myth 2 ───────────────────────────────────────────────────</span></span>
<span id="cb1-105"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Drunk Driving Makes the Roads Dangerous for Everyone"</span></span>
<span id="cb1-106"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Juxtaposes crash timing for all crashes (left, morning rush peak) against</span></span>
<span id="cb1-107"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># alcohol-impaired crashes (right, 2am peak - shown at 2.2x expanded y-scale</span></span>
<span id="cb1-108"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># to make the pattern look minor). The mismatch implies drunk driving can't be</span></span>
<span id="cb1-109"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># responsible for the most dangerous conditions.</span></span>
<span id="cb1-110"></span>
<span id="cb1-111">all_crashes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> crash_clean <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-112">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(collision_id, hour) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-113">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(hour)</span>
<span id="cb1-114"></span>
<span id="cb1-115">alc_crashes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> crash_clean <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-116">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(sobriety <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;">"Alcohol impaired"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-117">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(collision_id, hour) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-118">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(hour)</span>
<span id="cb1-119"></span>
<span id="cb1-120"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Peak-hour indicator ring positions (top 4 hours in each panel)</span></span>
<span id="cb1-121">all_peaks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> all_crashes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-122">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_max</span>(n, <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;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">with_ties =</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="cb1-123">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(hour)</span>
<span id="cb1-124"></span>
<span id="cb1-125">alc_peaks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> alc_crashes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-126">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_max</span>(n, <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;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">with_ties =</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="cb1-127">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(hour)</span>
<span id="cb1-128"></span>
<span id="cb1-129">all_limit <span class="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>(all_crashes<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;">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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.22</span></span>
<span id="cb1-130">alc_limit <span class="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>(alc_crashes<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;">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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span></span>
<span id="cb1-131"></span>
<span id="cb1-132">all_ring <span class="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;">hour =</span> all_peaks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ring_y =</span> all_limit <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.93</span>)</span>
<span id="cb1-133">alc_ring <span class="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;">hour =</span> alc_peaks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ring_y =</span> alc_limit <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.93</span>)</span>
<span id="cb1-134"></span>
<span id="cb1-135">all_ring_h <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> all_limit <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.06</span></span>
<span id="cb1-136">alc_ring_h <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> alc_limit <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.06</span></span>
<span id="cb1-137"></span>
<span id="cb1-138"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extra margin + clip off so polar axis labels don't get chopped at panel edges</span></span>
<span id="cb1-139">myth2_clock_theme <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb1-140">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major.y =</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> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grid_line, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>),</span>
<span id="cb1-141">  <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;">color =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text_primary, <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 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;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</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;">18</span>),</span>
<span id="cb1-142">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</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;">color =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text_muted,   <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</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>
<span id="cb1-143">  <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>
<span id="cb1-144">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text_axis, <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;">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;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</span>,</span>
<span id="cb1-145">    <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;">t =</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;">r =</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;">b =</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;">l =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb1-146">  ),</span>
<span id="cb1-147">  <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;">28</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span>)</span>
<span id="cb1-148">)</span>
<span id="cb1-149"></span>
<span id="cb1-150">p_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;">ggplot</span>(all_crashes, <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>(hour, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-151">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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.72</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>alcohol) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># slight gap between clock segments</span></span>
<span id="cb1-152">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>(</span>
<span id="cb1-153">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> all_ring,</span>
<span id="cb1-154">    <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="cb1-155">    <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>(hour, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> ring_y),</span>
<span id="cb1-156">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>alcohol, <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="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.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> all_ring_h</span>
<span id="cb1-157">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-158">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_polar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clip =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-159">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_discrete</span>(</span>
<span id="cb1-160">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"6"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"12"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"18"</span>),   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># label quarter-hours only</span></span>
<span id="cb1-161">    <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;">"12am"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"6am"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"12pm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"6pm"</span>)</span>
<span id="cb1-162">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-163">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(</span>
<span id="cb1-164">    <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>, all_limit),</span>
<span id="cb1-165">    <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;">expansion</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mult =</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>
<span id="cb1-166">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-167">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb1-168">    <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;">"ALL CRASHES BY HOUR"</span>,</span>
<span id="cb1-169">    <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;">"Morning commute is the deadliest window"</span></span>
<span id="cb1-170">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-171">  dashboard_theme <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-172">  myth2_clock_theme</span>
<span id="cb1-173"></span>
<span id="cb1-174"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expanded y-axis so alcohol bars only reach ~45% of clock face,</span></span>
<span id="cb1-175"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># making the pattern look minor and clearly misaligned with the left chart</span></span>
<span id="cb1-176">p_alc <span class="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>(alc_crashes, <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>(hour, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-177">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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.72</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>alcohol) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-178">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>(</span>
<span id="cb1-179">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> alc_ring,</span>
<span id="cb1-180">    <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="cb1-181">    <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>(hour, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> ring_y),</span>
<span id="cb1-182">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>alcohol, <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="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.78</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> alc_ring_h</span>
<span id="cb1-183">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-184">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_polar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clip =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-185">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_discrete</span>(</span>
<span id="cb1-186">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"6"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"12"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"18"</span>),</span>
<span id="cb1-187">    <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;">"12am"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"6am"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"12pm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"6pm"</span>)</span>
<span id="cb1-188">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-189">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(</span>
<span id="cb1-190">    <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>, alc_limit),           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2.2x makes bars fill ~45% of face</span></span>
<span id="cb1-191">    <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;">expansion</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mult =</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>
<span id="cb1-192">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-193">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb1-194">    <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;">"ALCOHOL-IMPAIRED CRASHES BY HOUR"</span>,</span>
<span id="cb1-195">    <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;">"Fewer crashes -- and the peak doesn't match"</span></span>
<span id="cb1-196">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-197">  dashboard_theme <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-198">  myth2_clock_theme</span>
<span id="cb1-199"></span>
<span id="cb1-200">p_all <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_alc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-201">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_annotation</span>(</span>
<span id="cb1-202">    <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;">"IF DRUNK DRIVING CAUSED THE DANGER, THE PEAKS WOULD ALIGN"</span>,</span>
<span id="cb1-203">    <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>
<span id="cb1-204">      <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>
<span id="cb1-205">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text_primary, <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># centered over both panels</span></span>
<span id="cb1-206">        <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;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</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="cb1-207">      ),</span>
<span id="cb1-208">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.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> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bg, <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>
<span id="cb1-209">      <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;">4</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;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb1-210">    )</span>
<span id="cb1-211">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb1-212">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb1-213">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.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> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>plot_bg, <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>
<span id="cb1-214">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.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> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>plot_bg, <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>
<span id="cb1-215">  )</span>
<span id="cb1-216"></span>
<span id="cb1-217"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"myth2.pdf"</span>)</span>
<span id="cb1-218"></span>
<span id="cb1-219"></span>
<span id="cb1-220"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ── Visualization 3: Myth 3 ───────────────────────────────────────────────────</span></span>
<span id="cb1-221"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Drunk Driving Is What's Killing Californians"</span></span>
<span id="cb1-222"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Breaks down estimated deaths (crashes x fatality rate) by gender and sobriety.</span></span>
<span id="cb1-223"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sober men account for more estimated deaths than all drunk drivers combined.</span></span>
<span id="cb1-224"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The dashed reference line marks the sum of estimated deaths across both drunk</span></span>
<span id="cb1-225"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># driver groups.</span></span>
<span id="cb1-226"></span>
<span id="cb1-227">myth3_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> crash_clean <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-228">  <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>(sobriety), gender_code <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-229">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(collision_id, gender_code, sobriety, fatal) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-230">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(gender_code, sobriety) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-231">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb1-232">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">est_deaths =</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;">n</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>(fatal, <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="cb1-233">    <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="cb1-234">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-235">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb1-236">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group_label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb1-237">      gender_code <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;">"M"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> sobriety <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;">"No alcohol"</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;">"SOBER MEN"</span>,</span>
<span id="cb1-238">      gender_code <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;">"M"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> sobriety <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;">"Alcohol impaired"</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;">"DRUNK MEN"</span>,</span>
<span id="cb1-239">      gender_code <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;">"F"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> sobriety <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;">"No alcohol"</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;">"SOBER WOMEN"</span>,</span>
<span id="cb1-240">      gender_code <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;">"F"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> sobriety <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;">"Alcohol impaired"</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;">"DRUNK WOMEN"</span></span>
<span id="cb1-241">    ),</span>
<span id="cb1-242">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group_label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(</span>
<span id="cb1-243">      group_label,</span>
<span id="cb1-244">      <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;">"SOBER MEN"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DRUNK MEN"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOBER WOMEN"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DRUNK WOMEN"</span>)</span>
<span id="cb1-245">    )</span>
<span id="cb1-246">  )</span>
<span id="cb1-247"></span>
<span id="cb1-248"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sum of estimated deaths for drunk men + drunk women combined</span></span>
<span id="cb1-249">all_drunk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> myth3_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-250">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(sobriety <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;">"Alcohol impaired"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-251">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(est_deaths) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-252">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>()</span>
<span id="cb1-253"></span>
<span id="cb1-254"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(myth3_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_label, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> est_deaths, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> sobriety)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-255">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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.6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># narrower columns; more whitespace between groups</span></span>
<span id="cb1-256">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(</span>
<span id="cb1-257">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> all_drunk,</span>
<span id="cb1-258">    <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> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>alcohol, <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;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span></span>
<span id="cb1-259">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-260">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(</span>
<span id="cb1-261">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>,</span>
<span id="cb1-262">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</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;">y =</span> all_drunk <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;">18</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># left of first bar, above reference line</span></span>
<span id="cb1-263">    <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;">"Est. Number of Deaths from Drunk Drivers - Cumulative: "</span>, all_drunk),</span>
<span id="cb1-264">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>alcohol, <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>,</span>
<span id="cb1-265">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</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>
<span id="cb1-266">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-267">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(</span>
<span id="cb1-268">    <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;">label =</span> est_deaths),</span>
<span id="cb1-269">    <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.4</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># just above bar top</span></span>
<span id="cb1-270">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</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="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.5</span>,</span>
<span id="cb1-271">    <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="cb1-272">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-273">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_limits</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;">max</span>(myth3_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>est_deaths) <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.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 20% headroom for value labels</span></span>
<span id="cb1-274">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(</span>
<span id="cb1-275">    <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;">"Alcohol impaired"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>alcohol, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No alcohol"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sober),</span>
<span id="cb1-276">    <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>
<span id="cb1-277">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-278">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb1-279">    <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;">"SOBER MEN ARE &lt;span style='color:#FF3333'&gt;DEADLIER&lt;/span&gt;&lt;br&gt;THAN ALL DRUNK DRIVERS COMBINED"</span>,</span>
<span id="cb1-280">    <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;">"Estimated deaths = crashes \u00d7 fatality rate, by gender and sobriety"</span>,</span>
<span id="cb1-281">    <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>
<span id="cb1-282">    <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;">"Estimated deaths"</span></span>
<span id="cb1-283">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-284">  dashboard_theme <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-285">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb1-286">    <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;">color =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text_axis, <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</span>),</span>
<span id="cb1-287">    <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_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text_axis, <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</span>),</span>
<span id="cb1-288">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major.y =</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> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>grid_line),</span>
<span id="cb1-289">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-290">    <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_markdown</span>(   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># allows HTML color span in title</span></span>
<span id="cb1-291">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> PAL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text_primary, <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="cb1-292">      <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;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rajdhani"</span>,</span>
<span id="cb1-293">      <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;">35</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;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-294">    )</span>
<span id="cb1-295">  )</span>
<span id="cb1-296"></span>
<span id="cb1-297"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"myth3.pdf"</span>)</span></code></pre></div></div>
</details>
</div>


</section>

 ]]></description>
  <category>MEDS</category>
  <category>R</category>
  <category>Data Visualization</category>
  <guid>https://rellimylime.github.io/posts/eds240-final/</guid>
  <pubDate>Mon, 09 Mar 2026 07:00:00 GMT</pubDate>
  <media:content url="https://rellimylime.github.io/images/posts/eds240-final/thumbnail.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Un-Daming the Benthos</title>
  <dc:creator>Emily Miller</dc:creator>
  <dc:creator>Lucian Scher &amp; Marie Tolteca</dc:creator>
  <link>https://rellimylime.github.io/posts/eds241-final/</link>
  <description><![CDATA[ 





<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readxl)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lme4)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lmerTest)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(broom.mixed)</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>(knitr)</span></code></pre></div></div>
</details>
</div>
<section id="study-overview" class="level2">
<h2 class="anchored" data-anchor-id="study-overview">Study Overview</h2>
<p>“Slow drawdown, fast recovery: Stream macroinvertebrate communities improve quickly after large dam decommissioning” by Atristain et al.&nbsp;(2024) asked whether decommissioning the Enobieta Dam (42 m, northern Spain) restored downstream macroinvertebrate communities. They used a <strong>multiple Before-After/Control-Impact (mBACI) design</strong>, the ecological equivalent of <strong>difference-in-differences (DiD)</strong>.</p>
<ul>
<li><strong>Treatment:</strong> Reservoir drawdown (Dec 2018 - Nov 2019)</li>
<li><strong>Outcome:</strong> Macroinvertebrate community metrics (taxa richness, diversity, density, biomonitoring index)</li>
<li><strong>Design:</strong> Compare the change over time at impact sites (downstream of dam) vs.&nbsp;control sites (undammed tributaries)</li>
</ul>
<p>In this post, we walk through a full replication of Atristain et al.&nbsp;(2024)’s mBACI analysis in R. We cover data loading and wrangling, exploratory visualization of the DiD structure, a linear mixed-effects model with annotated output and coefficient interpretation, an overall interaction test, model diagnostics, a robustness check across all four community metrics, and a critical evaluation of the causal assumptions and study limitations.</p>
<p>The figure below, reproduced from the paper’s supporting materials, illustrates how dam removal via slow drawdown transforms a degraded, low-diversity system into a fully recovered, biodiverse stream ecosystem. It conceptualizes the three stages (Before, Drawdown, and After) that form the basis of the statistical analysis.</p>
<p><img src="https://rellimylime.github.io/images/posts/eds241-final/drawdown-diagram.jpg" class="img-fluid"></p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Data Access
</div>
</div>
<div class="callout-body-container callout-body">
<p>Data are from the <a href="https://doi.org/10.5061/dryad.w3r2280ws">Dryad Digital Repository</a>. Download <code>Atristainetal_JAE_Data.xlsx</code> and place it in a <code>data/</code> subfolder.</p>
</div>
</div>
<hr>
</section>
<section id="data-loading-description" class="level2">
<h2 class="anchored" data-anchor-id="data-loading-description">Data Loading &amp; Description</h2>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">invert_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;">"data/Atristainetal_JAE_Data.xlsx"</span>,</span>
<span id="cb2-2">                         <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;">"Invertebrates"</span>)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># two columns named "Site", rename for clarity</span></span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(invert_raw)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <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;">"site_name"</span></span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(invert_raw)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>] <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;">"site_code"</span></span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">invert <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> invert_raw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(site_name, Replicate, Period, Reach, site_code, S, <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;">H´</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, TD, IASPT) <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;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">richness =</span> S, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shannon =</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;">H´</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;">density =</span> TD) <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;">mutate</span>(</span>
<span id="cb3-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reach_type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb3-6">      Reach <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;">"C"</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;">"C"</span>,</span>
<span id="cb3-7">      Reach <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;">"R"</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;">"R"</span>,</span>
<span id="cb3-8">      <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;">"I"</span></span>
<span id="cb3-9">    ),</span>
<span id="cb3-10">    <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;">factor</span>(Period, <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;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"D"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>),</span>
<span id="cb3-11">                    <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;">"Before"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Drawdown"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"After"</span>)),</span>
<span id="cb3-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reach_type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(reach_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;">"C"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"I"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span>))</span>
<span id="cb3-13">  )</span>
<span id="cb3-14"></span>
<span id="cb3-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># filter to control + impact only (reservoir sites only sampled post-drawdown)</span></span>
<span id="cb3-16">invert_baci <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> invert <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>(reach_type <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"I"</span>))</span>
<span id="cb3-17"></span>
<span id="cb3-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(invert_baci)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 120
Columns: 11
$ site_name  &lt;chr&gt; "Enobieta Up", "Enobieta Up", "Enobieta Up", "Enobieta Up",…
$ Replicate  &lt;dbl&gt; 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,…
$ Period     &lt;chr&gt; "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",…
$ Reach      &lt;chr&gt; "C", "C", "C", "C", "C", "I1", "I1", "I1", "I1", "I1", "C",…
$ site_code  &lt;chr&gt; "C1", "C1", "C1", "C1", "C1", "I1", "I1", "I1", "I1", "I1",…
$ richness   &lt;dbl&gt; 21, 23, 30, 30, 28, 6, 2, 4, 3, 3, 24, 20, 19, 11, 8, 10, 1…
$ shannon    &lt;dbl&gt; 2.7536694, 2.6475343, 2.6377758, 2.5200668, 2.6637126, 0.93…
$ density    &lt;dbl&gt; 2355.55556, 4277.77778, 2655.55556, 3277.77778, 1700.00000,…
$ IASPT      &lt;dbl&gt; 6.500000, 6.434783, 6.607143, 6.777778, 6.571429, 3.833333,…
$ reach_type &lt;fct&gt; C, C, C, C, C, I, I, I, I, I, C, C, C, C, C, I, I, I, I, I,…
$ period     &lt;fct&gt; Before, Before, Before, Before, Before, Before, Before, Bef…</code></pre>
</div>
</div>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>Category</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Unit of observation</strong></td>
<td>Five random samples collected with a Surber net (a standardized benthic invertebrate sampler covering 0.09 m²) from one site on one sampling occasion</td>
</tr>
<tr class="even">
<td><strong>Outcome variables</strong></td>
<td><code>richness</code> (S, taxa richness); <code>shannon</code> (H’, Shannon-Wiener diversity); <code>density</code> (TD, total invertebrate density); <code>IASPT</code> (Iberian Average Score Per Taxon, a pollution tolerance index)</td>
</tr>
<tr class="odd">
<td><strong>Treatment</strong></td>
<td><code>reach_type</code>: Impact (I1-I4, downstream of dam) vs.&nbsp;Control (C1-C4, undammed tributaries + upstream)</td>
</tr>
<tr class="even">
<td><strong>Time</strong></td>
<td><code>period</code>: Before (Nov 2017), Drawdown (Oct 2019), After (Nov 2020)</td>
</tr>
<tr class="odd">
<td><strong>Cleaning decisions</strong></td>
<td>Reservoir sites (R) excluded, only sampled in the After period, so not usable for the before-after comparison</td>
</tr>
<tr class="even">
<td><strong>Sample size</strong></td>
<td>120 samples total, 5 Surber net replicates x 4 sites per group x 3 periods; Artikutza protected catchment, northern Spain (2017-2020)</td>
</tr>
</tbody>
</table>
<p>The four community metrics capture different but complementary dimensions of invertebrate recovery. Taxa richness (S) counts the number of distinct taxa present and is the most direct measure of biodiversity. Shannon diversity (H’) weights taxa by their relative abundance, so it captures evenness as well as richness and is less dominated by common taxa. Total density (TD) measures the number of individuals per unit area, reflecting overall habitat productivity and suitability. IASPT (Iberian Average Score Per Taxon) is a biomonitoring index that scores communities based on the pollution tolerance of their constituent families: higher values indicate communities dominated by pollution-sensitive taxa, which are associated with good ecological condition. Together, these four metrics provide a robust picture of whether recovery was genuine and broad-based rather than an artifact of any single measure.</p>
<hr>
</section>
<section id="exploratory-analysis" class="level2">
<h2 class="anchored" data-anchor-id="exploratory-analysis">Exploratory Analysis</h2>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">invert_baci <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-2">  <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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> site_code, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> richness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> reach_type)) <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_boxplot</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;">outlier.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-4">  <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>period, <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;">3</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;">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;">"C"</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;">"#4393C3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"I"</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;">"#D6604D"</span>),</span>
<span id="cb5-6">                    <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;">"Control"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Impact"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-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;">"Site"</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;">"Taxa richness (S)"</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;">"Reach type"</span>,</span>
<span id="cb5-8">       <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;">"Taxa Richness Across Sites and Periods (Before, Drawdown, After)"</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;">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="cb5-10">  <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;">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;">hjust =</span> <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;">panel.border =</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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey70"</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;">NA</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="cb5-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>()</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-richness" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-richness-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://rellimylime.github.io/posts/eds241-final/index_files/figure-html/fig-richness-1.png" class="img-fluid figure-img" width="864">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-richness-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Taxa richness by site and period. Impact sites (red) had notably lower richness before drawdown; they approach control levels (blue) by the After period.
</figcaption>
</figure>
</div>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">did_means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> invert_baci <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;">group_by</span>(period, reach_type) <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;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_richness =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(richness),</span>
<span id="cb6-4">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(richness) <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>
<span id="cb6-5">            <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="cb6-6"></span>
<span id="cb6-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(did_means, <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> mean_richness,</span>
<span id="cb6-8">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> reach_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> reach_type)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-9">  <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="cb6-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="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="cb6-11">  <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> mean_richness <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="cb6-12">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> mean_richness <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 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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-13">  <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;">"C"</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;">"#4393C3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"I"</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;">"#D6604D"</span>),</span>
<span id="cb6-14">                     <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;">"Control"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Impact"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-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;">x =</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;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mean taxa richness (S)"</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;">"Reach type"</span>,</span>
<span id="cb6-16">       <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;">"Mean Taxa Richness by Period and Reach Type"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-17">  <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></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-did" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-did-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://rellimylime.github.io/posts/eds241-final/index_files/figure-html/fig-did-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-did-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Mean taxa richness by period and reach type. This plot visualizes the Difference-in-Differences (DiD) design by showing how taxa richness changed over time in impact vs.&nbsp;control sites. The key signature of recovery is convergence: impact sites starting below controls and closing the gap by the After period.
</figcaption>
</figure>
</div>
</div>
</div>
<p><strong>Annotation:</strong></p>
<p><strong>What this does:</strong> These two plots visualize the raw data and group means across periods to build intuition for the DiD structure before any formal modeling. The boxplot shows site-level variation within each period; the means plot highlights the group-level trajectories that the model will estimate.</p>
<p><strong>Identification approach:</strong> The DiD logic requires that we observe a <em>differential change over time</em>: impact sites should change more than control sites across the Before-to-After period, not merely be different at any single point. The convergence of the two lines in the means plot is the visual signature we are looking for. Control sites remain relatively stable over time, while impact sites show a substantial increase in richness from Before to Drawdown to After, closing the pre-existing gap that the dam had created.</p>
<p><strong>Key assumption (parallel trends):</strong> For the DiD estimator to be valid, control and impact sites must have been on the same trajectory before the drawdown. Because only one pre-treatment time point (November 2017) is available, this assumption cannot be formally tested. However, the paper builds indirect confidence in three ways: the four control sites span separate, undammed tributaries, making a coincidental shared trend with impact sites unlikely; the recovery signal is consistent across all four community metrics; and the effect is spatially graded, with the strongest recovery observed at I1 and I2, the sites nearest the dam, which is exactly the spatial pattern expected from a causal dam effect rather than a background trend.</p>
<p><strong>Evidence:</strong> Impact sites gained approximately 5 taxa on average from Before to After, while control sites declined slightly over the same period. This differential change is consistent with a causal recovery signal. The pattern is most pronounced at I1 and I2, the impact sites immediately downstream of the former reservoir, consistent with a distance-decay effect of the dam’s historical influence and further supporting the causal interpretation.</p>
<hr>
</section>
<section id="mbaci-model" class="level2">
<h2 class="anchored" data-anchor-id="mbaci-model">mBACI Model</h2>
<p>The mBACI design maps onto DiD as follows:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>DiD Term</th>
<th>mBACI Equivalent</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Treatment group</td>
<td>Impact sites (I1-I4)</td>
</tr>
<tr class="even">
<td>Control group</td>
<td>Control sites (C1-C4)</td>
</tr>
<tr class="odd">
<td>Pre-treatment</td>
<td>Before period (B)</td>
</tr>
<tr class="even">
<td>Post-treatment</td>
<td>Drawdown (D) &amp; After (A)</td>
</tr>
<tr class="odd">
<td>Treatment effect</td>
<td><code>period x reach_type</code> interaction</td>
</tr>
</tbody>
</table>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">mod_richness <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmer</span>(</span>
<span id="cb7-2">  richness <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> period <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> reach_type <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_code),</span>
<span id="cb7-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> invert_baci,</span>
<span id="cb7-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">REML =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb7-5">)</span>
<span id="cb7-6"></span>
<span id="cb7-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(mod_richness)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: richness ~ period * reach_type + (1 | site_code)
   Data: invert_baci

REML criterion at convergence: 706.7

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.5983 -0.7530 -0.1060  0.7423  2.4335 

Random effects:
 Groups    Name        Variance Std.Dev.
 site_code (Intercept)  8.905   2.984   
 Residual              22.225   4.714   
Number of obs: 120, groups:  site_code, 8

Fixed effects:
                           Estimate Std. Error      df t value Pr(&gt;|t|)    
(Intercept)                  21.950      1.827   9.868  12.015 3.27e-07 ***
periodDrawdown               -1.150      1.491 108.000  -0.771 0.442160    
periodAfter                  -1.950      1.491 108.000  -1.308 0.193647    
reach_typeI                  -8.750      2.584   9.868  -3.387 0.007054 ** 
periodDrawdown:reach_typeI    6.000      2.108 108.000   2.846 0.005301 ** 
periodAfter:reach_typeI       7.150      2.108 108.000   3.391 0.000973 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation of Fixed Effects:
            (Intr) prdDrw prdAft rch_tI prD:_I
peridDrwdwn -0.408                            
periodAfter -0.408  0.500                     
reach_typeI -0.707  0.289  0.289              
prdDrwdw:_I  0.289 -0.707 -0.354 -0.408       
prdAftr:r_I  0.289 -0.354 -0.707 -0.408  0.500</code></pre>
</div>
</div>
<p><strong>Annotation:</strong></p>
<p><strong>What this does:</strong> This fits a linear mixed-effects model (LME) with fixed effects for period, reach type, and their interaction, plus a random intercept for site.</p>
<p><strong>Why fixed effects?</strong> Fixed effects for <code>period</code> and <code>reach_type</code> are included to control for confounding sources of variation. The <code>reach_type</code> term accounts for pre-existing baseline differences between impact and control sites: the dam had already suppressed impact communities before the study began, so the two groups start at different levels. The <code>period</code> term accounts for temporal changes that affect all sites equally, such as seasonal or interannual variation in catchment conditions. Including both as fixed effects ensures that the <code>period:reach_type</code> interaction term isolates only the differential change over time, which is the DiD estimator of interest.</p>
<p><strong>Why a random intercept for site?</strong> Each of the five Surber net replicates collected at a given site on a given sampling occasion shares the same physical environment: the same hydrology, substrate composition, water chemistry, and local species pool. Treating these replicates as fully independent observations would inflate the effective sample size and understate uncertainty about site-level differences. The random intercept <code>(1 | site_code)</code> absorbs between-site variation in baseline richness, so that inference on the fixed effects is based on the variation in <em>change over time</em> across sites rather than within-site replicate noise. This directly addresses the pseudoreplication structure of the Surber net sampling design used in Atristain et al.&nbsp;(2024).</p>
<p><strong>Identification approach:</strong> The <code>period:reach_type</code> interaction is the DiD estimator. It measures whether the <em>change over time</em> differed between control and impact sites. A positive and significant <code>periodAfter:reach_typeI</code> coefficient indicates that impact sites increased more than control sites from Before to After, the expected signature of dam-removal-driven recovery.</p>
<p><strong>Assumptions required for causal validity:</strong></p>
<ol type="1">
<li><strong>Parallel trends:</strong> Without treatment, impact and control sites would have changed at the same rate over time</li>
<li><strong>No spillover (SUTVA):</strong> Drawdown at impact sites did not affect control site outcomes</li>
<li><strong>No time-varying confounders:</strong> Nothing else changed between periods that affected impact vs.&nbsp;control sites differentially</li>
<li><strong>Model specification:</strong> Random intercept structure captures the data’s dependence structure; residuals approximately normal with constant variance</li>
</ol>
<hr>
</section>
<section id="coefficient-interpretation" class="level2">
<h2 class="anchored" data-anchor-id="coefficient-interpretation">Coefficient Interpretation</h2>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy</span>(mod_richness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">effects =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fixed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf.int =</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="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(term, estimate, std.error, statistic, conf.low, conf.high) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-3">  <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>
<span id="cb9-4">        <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;">"Fixed-effect estimates from the mBACI linear mixed model for taxa richness. CI Lower and CI Upper are 95% confidence intervals."</span>,</span>
<span id="cb9-5">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.names =</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;">"Term"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Estimate"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SE"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Statistic"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CI Lower"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CI Upper"</span>))</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Fixed-effect estimates from the mBACI linear mixed model for taxa richness. CI Lower and CI Upper are 95% confidence intervals.</caption>
<colgroup>
<col style="width: 38%">
<col style="width: 12%">
<col style="width: 8%">
<col style="width: 14%">
<col style="width: 12%">
<col style="width: 12%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Term</th>
<th style="text-align: right;">Estimate</th>
<th style="text-align: right;">SE</th>
<th style="text-align: right;">Statistic</th>
<th style="text-align: right;">CI Lower</th>
<th style="text-align: right;">CI Upper</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">(Intercept)</td>
<td style="text-align: right;">21.95</td>
<td style="text-align: right;">1.827</td>
<td style="text-align: right;">12.015</td>
<td style="text-align: right;">17.872</td>
<td style="text-align: right;">26.028</td>
</tr>
<tr class="even">
<td style="text-align: left;">periodDrawdown</td>
<td style="text-align: right;">-1.15</td>
<td style="text-align: right;">1.491</td>
<td style="text-align: right;">-0.771</td>
<td style="text-align: right;">-4.105</td>
<td style="text-align: right;">1.805</td>
</tr>
<tr class="odd">
<td style="text-align: left;">periodAfter</td>
<td style="text-align: right;">-1.95</td>
<td style="text-align: right;">1.491</td>
<td style="text-align: right;">-1.308</td>
<td style="text-align: right;">-4.905</td>
<td style="text-align: right;">1.005</td>
</tr>
<tr class="even">
<td style="text-align: left;">reach_typeI</td>
<td style="text-align: right;">-8.75</td>
<td style="text-align: right;">2.584</td>
<td style="text-align: right;">-3.387</td>
<td style="text-align: right;">-14.517</td>
<td style="text-align: right;">-2.983</td>
</tr>
<tr class="odd">
<td style="text-align: left;">periodDrawdown:reach_typeI</td>
<td style="text-align: right;">6.00</td>
<td style="text-align: right;">2.108</td>
<td style="text-align: right;">2.846</td>
<td style="text-align: right;">1.821</td>
<td style="text-align: right;">10.179</td>
</tr>
<tr class="even">
<td style="text-align: left;">periodAfter:reach_typeI</td>
<td style="text-align: right;">7.15</td>
<td style="text-align: right;">2.108</td>
<td style="text-align: right;">3.391</td>
<td style="text-align: right;">2.971</td>
<td style="text-align: right;">11.329</td>
</tr>
</tbody>
</table>
</div>
</div>
<p><strong>Annotation:</strong></p>
<p><strong>What this does:</strong> This table extracts the fixed-effect estimates with 95% confidence intervals. Each row is one DiD coefficient, and together they tell the story of whether and when impact sites recovered relative to controls.</p>
<p><strong>Key terms and their ecological interpretation:</strong></p>
<ul>
<li><p><code>(Intercept)</code>: The estimated mean taxa richness for control sites in the Before period, approximately 22 taxa. This is the ecological baseline for an undammed, reference-quality tributary in the Artikutza catchment before any intervention.</p></li>
<li><p><code>reach_typeI</code>: The impact-vs-control gap in the Before period, approximately -8.75 taxa. Impact sites already had about 9 fewer taxa than control sites before drawdown began, reflecting the long-term suppressive effect of the Enobieta Dam on downstream communities. This pre-existing difference does not threaten the DiD design; the design only requires that the <em>trend</em> would have been parallel, not that the groups start at the same level.</p></li>
<li><p><code>periodDrawdown:reach_typeI</code>: The DiD estimate for the Drawdown period. An estimate of approximately +6.0 means that impact sites increased by 6 more taxa than controls from Before to Drawdown, indicating that recovery began rapidly even while the reservoir was still being emptied, consistent with Atristain et al.&nbsp;(2024)’s finding of fast community reassembly.</p></li>
<li><p><code>periodAfter:reach_typeI</code>: The main recovery signal. An estimate of approximately +7.15 (95% CI: 3.0-11.3) means that impact sites gained about 7 more taxa than controls from Before to After. Because the confidence interval excludes zero, this is a statistically significant and ecologically meaningful recovery: impact communities, which started roughly 9 taxa below controls, had closed most of that gap within one year of full drawdown completion.</p></li>
</ul>
<hr>
</section>
<section id="overall-interaction-test-baci" class="level2">
<h2 class="anchored" data-anchor-id="overall-interaction-test-baci">Overall Interaction Test (BACI)</h2>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(mod_richness)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Type III Analysis of Variance Table with Satterthwaite's method
                   Sum Sq Mean Sq NumDF DenDF F value   Pr(&gt;F)   
period             81.517  40.758     2   108  1.8339 0.164735   
reach_type         81.605  81.605     1     6  3.6717 0.103816   
period:reach_type 294.817 147.408     2   108  6.6325 0.001919 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p><strong>Annotation:</strong></p>
<p><strong>What this does:</strong> This is a Type I (sequential) F-test of the <code>period:reach_type</code> interaction term, testing whether the trajectories over time differed <em>at all</em> between impact and control sites across both post-treatment periods jointly. Because <code>period:reach_type</code> is the last term added in the model formula, the Type I result for this term is equivalent to what a Type III test would give: it tests the interaction after all main effects are already in the model. Note that <code>lme4</code> alone does not provide p-values for mixed models; loading <code>lmerTest</code> applies the Satterthwaite approximation to compute denominator degrees of freedom and p-values for each term.</p>
<p><strong>Result:</strong> The interaction term is statistically significant (p &lt; 0.05), indicating that the trajectories of taxa richness over time differed between impact and control sites. This replicates the core finding in Table 2 of Atristain et al.&nbsp;(2024) and confirms that the pattern seen in the means plot is not attributable to chance alone.</p>
<p><strong>Interpretation:</strong> A significant interaction means that being downstream of the dam changed how richness evolved over time. This is the causal claim the mBACI design is built to test. This joint F-test is more conservative than looking at individual period coefficients because it asks whether <em>any</em> period shows a differential change, not just the After period specifically. Passing this test with both post-treatment periods simultaneously strengthens the result.</p>
<hr>
</section>
<section id="model-diagnostics" class="level2">
<h2 class="anchored" data-anchor-id="model-diagnostics">Model Diagnostics</h2>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">diag_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fitted =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fitted</span>(mod_richness),</span>
<span id="cb12-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resid  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>(mod_richness))</span>
<span id="cb12-3"></span>
<span id="cb12-4">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>(diag_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> fitted, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> resid)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-5">  <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.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-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;">"Fitted values"</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;">"Residuals"</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;">"Residuals vs. Fitted"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-8">  <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="cb12-9"></span>
<span id="cb12-10">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>(diag_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;">sample =</span> resid)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-11">  <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;">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 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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-12">  <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;">"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="cb12-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;">"Normal QQ Plot"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-14">  <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="cb12-15"></span>
<span id="cb12-16">p1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p2</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-diagnostics" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-diagnostics-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://rellimylime.github.io/posts/eds241-final/index_files/figure-html/fig-diagnostics-1.png" class="img-fluid figure-img" width="864">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-diagnostics-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Residual diagnostics. Left: residuals vs.&nbsp;fitted (checks homoscedasticity). Right: QQ plot (checks normality).
</figcaption>
</figure>
</div>
</div>
</div>
<p><strong>Annotation:</strong></p>
<p><strong>What this does:</strong> These two plots check the two core LME assumptions: constant residual variance (homoscedasticity) and approximate normality of residuals.</p>
<p><strong>Residuals vs.&nbsp;fitted:</strong> Residuals should scatter randomly around zero with no systematic fanning or curvature. A fan shape would indicate heteroscedasticity; a curve would suggest the linear specification is incorrect.</p>
<p><strong>QQ plot:</strong> Points should fall along the red line. Departures at the tails indicate non-normality.</p>
<p><strong>Evidence:</strong> The residuals scatter roughly randomly around zero with no obvious fan shape, suggesting homoscedasticity is approximately satisfied. The QQ plot shows mild heavy tails, which is common in ecological count data. The LME is moderately robust to mild non-normality, particularly with n = 120 observations, so inference on the fixed effects is unlikely to be substantially affected. If heavier non-normality were present, alternatives such as log-transforming richness or fitting a Poisson/negative binomial GLMM could be considered.</p>
<hr>
</section>
<section id="robustness-multiple-outcomes" class="level2">
<h2 class="anchored" data-anchor-id="robustness-multiple-outcomes">Robustness: Multiple Outcomes</h2>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">mod_shannon <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmer</span>(shannon <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> period <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> reach_type <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_code),</span>
<span id="cb13-2">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> invert_baci, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">REML =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb13-3"></span>
<span id="cb13-4">mod_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;">lmer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(density) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> period <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> reach_type <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_code),</span>
<span id="cb13-5">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> invert_baci, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">REML =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb13-6"></span>
<span id="cb13-7">mod_iaspt   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmer</span>(IASPT <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> period <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> reach_type <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_code),</span>
<span id="cb13-8">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> invert_baci, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">REML =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">extract_interaction <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>(model, label) {</span>
<span id="cb14-2">  aov <span class="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;">anova</span>(model))</span>
<span id="cb14-3">  row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> aov[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"period:reach_type"</span>, ]</span>
<span id="cb14-4"></span>
<span id="cb14-5">  coefs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy</span>(model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">effects =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fixed"</span>)</span>
<span id="cb14-6">  drw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> coefs[coefs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>term <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;">"periodDrawdown:reach_typeI"</span>, ]</span>
<span id="cb14-7">  aft <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> coefs[coefs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>term <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;">"periodAfter:reach_typeI"</span>, ]</span>
<span id="cb14-8"></span>
<span id="cb14-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb14-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Response       =</span> label,</span>
<span id="cb14-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;">DiD Drawdown</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="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>(drw) <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(drw<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;">2</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_real_</span>,</span>
<span id="cb14-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;">DiD After</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="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>(aft) <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(aft<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;">2</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_real_</span>,</span>
<span id="cb14-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;">F 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> row[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F value"</span>]],</span>
<span id="cb14-14">    <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> row[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pr(&gt;F)"</span>]]</span>
<span id="cb14-15">  )</span>
<span id="cb14-16">}</span>
<span id="cb14-17"></span>
<span id="cb14-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(</span>
<span id="cb14-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_interaction</span>(mod_richness, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Taxa richness (S)"</span>),</span>
<span id="cb14-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_interaction</span>(mod_shannon,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Shannon diversity (H')"</span>),</span>
<span id="cb14-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_interaction</span>(mod_density,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log(Total density)"</span>),</span>
<span id="cb14-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_interaction</span>(mod_iaspt,    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"IASPT (pollution tolerance index)"</span>)</span>
<span id="cb14-23">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-24">  <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;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb14-25">        <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;">"Interaction coefficients and overall F-tests for the period x reach_type DiD term across all four outcome models. DiD Drawdown and DiD After are the estimated interaction coefficients (periodDrawdown:reach_typeI and periodAfter:reach_typeI), reflecting the differential change in impact vs. control sites during and after reservoir drawdown."</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Interaction coefficients and overall F-tests for the period x reach_type DiD term across all four outcome models. DiD Drawdown and DiD After are the estimated interaction coefficients (periodDrawdown:reach_typeI and periodAfter:reach_typeI), reflecting the differential change in impact vs.&nbsp;control sites during and after reservoir drawdown.</caption>
<colgroup>
<col style="width: 46%">
<col style="width: 17%">
<col style="width: 13%">
<col style="width: 10%">
<col style="width: 10%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Response</th>
<th style="text-align: right;">DiD Drawdown</th>
<th style="text-align: right;">DiD After</th>
<th style="text-align: right;">F value</th>
<th style="text-align: right;">p value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Taxa richness (S)</td>
<td style="text-align: right;">6.00</td>
<td style="text-align: right;">7.15</td>
<td style="text-align: right;">6.63</td>
<td style="text-align: right;">0.0019</td>
</tr>
<tr class="even">
<td style="text-align: left;">Shannon diversity (H’)</td>
<td style="text-align: right;">0.37</td>
<td style="text-align: right;">0.46</td>
<td style="text-align: right;">4.72</td>
<td style="text-align: right;">0.0109</td>
</tr>
<tr class="odd">
<td style="text-align: left;">log(Total density)</td>
<td style="text-align: right;">0.86</td>
<td style="text-align: right;">0.99</td>
<td style="text-align: right;">5.48</td>
<td style="text-align: right;">0.0054</td>
</tr>
<tr class="even">
<td style="text-align: left;">IASPT (pollution tolerance index)</td>
<td style="text-align: right;">0.47</td>
<td style="text-align: right;">0.89</td>
<td style="text-align: right;">8.23</td>
<td style="text-align: right;">0.0005</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">invert_baci <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(period, reach_type) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</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;">Taxa richness</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;">mean</span>(richness),</span>
<span id="cb15-4">            <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;">Shannon diversity</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;">mean</span>(shannon),</span>
<span id="cb15-5">            <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(Density)</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;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(density)),</span>
<span id="cb15-6">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">IASPT              =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(IASPT),</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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<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;">Taxa richness</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Shannon diversity</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,</span>
<span id="cb15-9">                        <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(Density)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, IASPT),</span>
<span id="cb15-10">               <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;">"metric"</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;">"mean_value"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-11">  <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>(<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> mean_value,</span>
<span id="cb15-12">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> reach_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> reach_type)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-13">  <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="cb15-14">  <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.8</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;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>metric, <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-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> <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;">"C"</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;">"#4393C3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"I"</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;">"#D6604D"</span>),</span>
<span id="cb15-17">                     <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;">"Control"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Impact"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-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;">"Period"</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;">"Mean value"</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;">"Reach type"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-19">  <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-20">  <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.border =</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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey70"</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;">NA</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></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-robustness" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-robustness-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://rellimylime.github.io/posts/eds241-final/index_files/figure-html/fig-robustness-1.png" class="img-fluid figure-img" width="960">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-robustness-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: DiD pattern across all four community metrics. Impact sites (red) converge toward control values (blue) over time for every metric, consistent with broad-based community recovery following dam removal.
</figcaption>
</figure>
</div>
</div>
</div>
<p><strong>Annotation:</strong></p>
<p><strong>What this does:</strong> This section fits the same mBACI/DiD model on three additional community metrics: Shannon diversity, log-transformed total density, and IASPT (Iberian Average Score Per Taxon, a pollution tolerance index). The table reports both the interaction coefficients (DiD estimates for the Drawdown and After periods) and the overall interaction F-test p-value for each outcome. The figure shows whether the visual DiD convergence pattern holds across all metrics simultaneously.</p>
<p><strong>Identification approach:</strong> If the drawdown caused recovery, the <code>period:reach_type</code> interaction should be positive and significant across all metrics, not just taxa richness. A result that held for only one metric could be dismissed as a spurious finding, but concordance across four independent measures of community condition is much harder to explain without a genuine treatment effect.</p>
<p><strong>Results:</strong> All four interaction tests are statistically significant (p &lt; 0.05), and in every case the DiD After coefficient is positive, meaning impact sites improved more than control sites from Before to After. The IASPT result is particularly informative: rising IASPT scores indicate that the recovering communities were not just accumulating any taxa, but specifically gaining pollution-sensitive taxa that are the hallmark of good ecological condition. This is consistent with the paper’s conclusion that dam removal produced rapid and ecologically meaningful community reassembly, not merely a numerical increase in richness counts.</p>
<hr>
</section>
<section id="critical-evaluation" class="level2">
<h2 class="anchored" data-anchor-id="critical-evaluation">Critical Evaluation</h2>
<section id="causal-identification" class="level3">
<h3 class="anchored" data-anchor-id="causal-identification">Causal Identification</h3>
<p>The mBACI/DiD design is credible given the study setting. The Artikutza catchment is a protected watershed with no competing land-use changes, water withdrawals, or other interventions during the 2017-2020 study period, which reduces the risk of time-varying confounders affecting impact and control sites differently. For estimates to be causal, the <strong>counterfactual must hold</strong>: control sites must represent what impact sites would have looked like in the absence of dam removal. The use of four independent control sites spread across separate, undammed tributaries of the same headwater catchment strengthens this claim considerably relative to a single control. If one control site behaved anomalously due to local disturbance, the other three would not, and the model would still estimate the treatment effect correctly.</p>
<p>The main credibility concern is the parallel trends assumption. There is only <strong>one pre-treatment time point (November 2017)</strong>, which means the assumption that impact and control sites were on the same trajectory before drawdown cannot be formally tested. However, Atristain et al.&nbsp;(2024) build indirect confidence in several ways: the control sites are ecologically comparable to the impact sites within the same protected catchment; the recovery signal is consistent across all four community metrics; and the effect is strongest at I1 and I2, the sites nearest the dam, which is the spatial gradient expected from a causal dam-proximity effect rather than a coincidental background trend unrelated to the intervention.</p>
</section>
<section id="statistical-assumptions" class="level3">
<h3 class="anchored" data-anchor-id="statistical-assumptions">Statistical Assumptions</h3>
<p>The <strong>parallel trends</strong> assumption is the most vulnerable. With only one pre-treatment observation per site, we cannot distinguish between “impact sites recovered because of the drawdown” and “impact sites were already on an upward trajectory before 2018 for other reasons.” The indirect evidence described above mitigates but does not eliminate this concern.</p>
<p><strong>Normality and homoscedasticity</strong> appear approximately satisfied based on the residual diagnostics in Figure 3. Mild heavy tails in the QQ plot are common in ecological count data and are unlikely to substantially affect inference at this sample size.</p>
<p>The <strong>random effects structure</strong> is limited by the number of sites: with only 4 sites per group, the random intercept variance is estimated from few clusters. This can lead to underestimation of between-site variance and potentially anticonservative fixed-effect inference. A sensitivity analysis using site-level means rather than replicate-level data would be informative.</p>
<p><strong>SUTVA (no spillover)</strong> is well-supported. Control sites C2-C4 are in separate tributaries physically isolated from the drawdown reach by ridge topography, and C1 is located upstream of the dam, making downstream sediment or chemical effects on control communities implausible.</p>
</section>
<section id="sampling-external-validity" class="level3">
<h3 class="anchored" data-anchor-id="sampling-external-validity">Sampling &amp; External Validity</h3>
<p>The study sampled 8 stream sites in a single protected headwater catchment in Basque Country, Spain, with 5 random Surber net samples per site per period. Results apply most directly to sites with similar characteristics: temperate, forested, protected headwater streams with comparable dam height and reservoir volume. Dam removal in urban, agricultural, or arid-region streams may produce different recovery dynamics due to differences in sediment loads, non-native species pressure, and altered flow regimes.</p>
<p>The time scale is also a constraint. Recovery was assessed after only approximately one year post-drawdown (November 2020 sampling). The paper’s conclusion of “rapid recovery” is well-supported for the metrics and species present in the data, but pollution-sensitive taxa, rare species, and organisms with longer generation times may require additional years to recolonize and establish stable populations. Monitoring through 2022 or 2023 would clarify whether the community trajectory continued upward or plateaued after the initial reassembly phase.</p>
</section>
<section id="measurement" class="level3">
<h3 class="anchored" data-anchor-id="measurement">Measurement</h3>
<p>Taxa richness is a coarse biodiversity measure: it counts taxa present but treats a single individual of a sensitive species identically to 1,000 individuals of a tolerant one. Shannon diversity (H’) partially corrects for this by incorporating relative abundance, which is why including both provides a more complete picture. Surber nets sample a fixed benthic area (0.09 m²) and may undersample mobile or large-bodied invertebrates; because the method is applied consistently across all sites and periods, systematic bias is present but stable and does not confound the DiD estimate. IASPT depends on accurate taxonomic identification to family level; any misidentification error would tend to attenuate the signal rather than inflate it, making significant IASPT results conservative.</p>
<p>Treatment timing introduces one additional measurement concern. The “Drawdown” sampling (October 2019) occurred while the reservoir was still being emptied and sediment mobilization was ongoing, so it does not represent a stable post-treatment state. The positive DiD coefficient at Drawdown therefore likely underestimates the full recovery signal relative to what would be observed after the system fully stabilized, which is why the After coefficient is the primary effect estimate of interest.</p>
</section>
<section id="other-limitations" class="level3">
<h3 class="anchored" data-anchor-id="other-limitations">Other Limitations</h3>
<p>Five Surber net replicates collected within a single site on a single date are not truly independent: they share the same hydrological conditions, substrate patch, and local invertebrate source pool. The random intercept for site partially addresses this by absorbing between-site baseline differences, but within-site spatial autocorrelation among the five replicates is not fully accounted for and likely leads to slightly anticonservative inference.</p>
<p>The paper documents invertebrate recovery but does not directly measure the physical mechanisms driving it. Changes in bed substrate composition, water turbidity, and flow velocity are the most plausible mediating variables, but without direct measurements of these, we know the outcome changed without knowing precisely why. This limits the mechanistic understanding needed to predict recovery dynamics at other dam sites with different physical characteristics.</p>
<p>Finally, no power analysis is reported. With 4 sites per group and 3 time points, the study has limited power to detect moderate treatment effects for metrics with high within-site variability. The statistically significant results for all four outcomes are reassuring, but weaker or null results for any individual metric might represent false negatives rather than genuine absence of recovery.</p>
</section>
<section id="recommendations-for-future-research" class="level3">
<h3 class="anchored" data-anchor-id="recommendations-for-future-research">Recommendations for Future Research</h3>
<p>Future studies of dam removal ecology could address several of these limitations directly. Extended post-removal monitoring spanning 3-5 years would allow assessment of whether rapid recovery is sustained and whether slower-responding taxa eventually recolonize. Pairing biological sampling with concurrent physical habitat measurements (grain size distribution, turbidity, water temperature, fine sediment cover) would help identify the mechanisms linking reservoir drawdown to community recovery, allowing researchers to model the pathway from physical change to biological response rather than treating recovery as a black box. Replication across additional dam removals spanning different stream types, land-use contexts, and climate zones would improve the generalizability of results beyond a single protected headwater catchment in northern Spain. Finally, beginning monitoring with multiple pre-treatment sampling years before dam removal would allow a formal test of the parallel trends assumption, which is the central threat to causal identification in any DiD-based design.</p>
<hr>
</section>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<p>Atristain, M., Solagaistua, L., Larrañaga, A., von Schiller, D., &amp; Elosegi, A. (2024). Slow drawdown, fast recovery: Stream macroinvertebrate communities improve quickly after large dam decommissioning. <em>Journal of Applied Ecology</em>, 61, 1481-1491. <a href="https://doi.org/10.1111/1365-2664.14656" class="uri">https://doi.org/10.1111/1365-2664.14656</a></p>
<p>Bates, D., Mächler, M., Bolker, B., &amp; Walker, S. (2015). Fitting linear mixed-effects models using lme4. <em>Journal of Statistical Software</em>, 67, 1-48.</p>
<p>Kuznetsova, A., Brockhoff, P. B., &amp; Christensen, R. H. B. (2017). lmerTest package: Tests in linear mixed effects models. <em>Journal of Statistical Software</em>, 82(13), 1-26.</p>


</section>

 ]]></description>
  <category>R</category>
  <category>Statistics</category>
  <category>MEDS</category>
  <guid>https://rellimylime.github.io/posts/eds241-final/</guid>
  <pubDate>Fri, 06 Mar 2026 08:00:00 GMT</pubDate>
  <media:content url="https://rellimylime.github.io/images/posts/eds241-final/reservoir-1030292_1280.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Lost in the Woods</title>
  <dc:creator>Emily Miller</dc:creator>
  <link>https://rellimylime.github.io/posts/forest-data-compilation/</link>
  <description><![CDATA[ 





<section id="the-data-everyone-wants-to-use" class="level2">
<h2 class="anchored" data-anchor-id="the-data-everyone-wants-to-use">The Data Everyone Wants to Use</h2>
<p>The USDA Forest Service’s Forest Inventory and Analysis program has been running permanent forest plots across all 50 states since the early 2000s. Every five years or so, field crews visit each ~1-acre plot and record every tree: species, diameter, crown class, condition, disturbance history, regeneration. The national dataset currently covers around 6,900 permanent sites with measurements spanning 25 years. It’s the most comprehensive longitudinal record of US forest health that exists.</p>
<p>Researchers who work on forest disturbance, thermophilization, fire ecology, carbon accounting, and biodiversity all eventually find their way to FIA data. And then they spend the next week trying to figure out how it’s structured.</p>
</section>
<section id="why-fia-is-hard-to-work-with" class="level2">
<h2 class="anchored" data-anchor-id="why-fia-is-hard-to-work-with">Why FIA Is Hard to Work With</h2>
<p>The national FIA database (FIADB v9.4) is distributed as either a 70 GB SQLite file or per-state CSV downloads. Either way, before you can analyze anything, you need to understand the structure - and the user guide is 600+ pages.</p>
<p>The complexity is real, not just documentation overhead. A few things that catch new users:</p>
<p><strong>FIA samples all US land</strong>, not just forests. About 59% of plot-year rows in the raw data have <code>pct_forested == 0</code>. They’re fields, parking lots, lawns. The first filter for any forest analysis is just: <em>is this actually a forest plot?</em> This isn’t documented anywhere obvious - you discover it when your species diversity numbers look completely wrong.</p>
<p><strong>Code fields are everywhere.</strong> STATUSCD, CCLCD, AGENTCD, COND_STATUS_CD, DSTRBCD, TRTCD - each has its own set of integer codes with specific meanings buried in appendix tables. The wrong code filter doesn’t error. It silently returns the wrong answer.</p>
<p><strong>Table relationships aren’t always intuitive.</strong> TREE_GRM_COMPONENT, which holds between-measurement mortality data, links back to TREE via <code>TRE_CN</code> - not to PLOT directly. That means you need a TREE join to get <code>INVYR</code> and <code>SPCD</code> even when you’re only analyzing mortality. The first time you encounter this, it’s genuinely confusing.</p>
<p><strong>50+ tables</strong>, and some names don’t telegraph their content. Which table has seedling counts? (SEEDLING.) Okay, but how does it relate to SUBPLOT? What about SUBP_COND? None of this is unfixable, but it slows down the first few weeks considerably.</p>
</section>
<section id="the-dashboard" class="level2">
<h2 class="anchored" data-anchor-id="the-dashboard">The Dashboard</h2>
<p>The FIA Database Schema Navigator is a Streamlit app for exploring FIADB v9.4 structure without loading the database into memory. The core design constraint: the full SQLite file is 70 GB, so all schema navigation runs off SQLite’s <code>PRAGMA table_info()</code>. Instant, even on the full database.</p>
<p>The app runs in <strong>metadata-only mode</strong> when no database is connected - the relationship diagram, field codebook, table browser, and SQL templates are all hardcoded from the User Guide and work without the SQLite file. You can explore it live here:</p>
<iframe src="https://fiadb-dashboard.streamlit.app?embed=true" width="100%" height="700px" frameborder="0" style="border-radius:8px;"></iframe>
<p><a href="https://fiadb-dashboard.streamlit.app/">Open the FIA Database Schema Navigator</a></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> sqlite3</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> streamlit <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> st</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@st.cache_data</span></span>
<span id="cb1-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_table_schema(db_path: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, table_name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>]:</span>
<span id="cb1-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Schema introspection with no row loading."""</span></span>
<span id="cb1-7">    conn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sqlite3.<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">connect</span>(db_path)</span>
<span id="cb1-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb1-9">        cursor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> conn.execute(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"PRAGMA table_info(</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>table_name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span>)</span>
<span id="cb1-10">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [</span>
<span id="cb1-11">            {</span>
<span id="cb1-12">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: col[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb1-13">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>: col[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb1-14">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"not_null"</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span>(col[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]),</span>
<span id="cb1-15">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"primary_key"</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span>(col[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]),</span>
<span id="cb1-16">            }</span>
<span id="cb1-17">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> col <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cursor.fetchall()</span>
<span id="cb1-18">        ]</span>
<span id="cb1-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">finally</span>:</span>
<span id="cb1-20">        conn.close()</span></code></pre></div></div>
<p>Row previews are available but opt-in - you request them explicitly. The app doesn’t load anything by default.</p>
<section id="table-browser" class="level3">
<h3 class="anchored" data-anchor-id="table-browser">Table Browser</h3>
<p>The 50+ FIADB tables are organized into categories pulled from the user guide - Location Level, Tree Level, Invasive/Understory Vegetation, Down Woody Material, Population, Reference - rather than alphabetically. Alphabetical is how a database shows you tables. Categorical is how a researcher looks for them.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">TABLE_CATEGORIES: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>]] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb2-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Location Level"</span>: [</span>
<span id="cb2-3">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SURVEY"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PROJECT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"COUNTY"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PLOT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"COND"</span>,</span>
<span id="cb2-4">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SUBPLOT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SUBP_COND"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BOUNDARY"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SUBP_COND_CHNG_MTRX"</span>,</span>
<span id="cb2-5">    ],</span>
<span id="cb2-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tree Level"</span>: [</span>
<span id="cb2-7">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TREE"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TREE_WOODLAND_STEMS"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TREE_GRM_COMPONENT"</span>,</span>
<span id="cb2-8">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TREE_GRM_THRESHOLD"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TREE_GRM_MIDPT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TREE_GRM_BEGIN"</span>,</span>
<span id="cb2-9">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TREE_GRM_ESTN"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BEGINEND"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SEEDLING"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SITETREE"</span>,</span>
<span id="cb2-10">    ],</span>
<span id="cb2-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Reference"</span>: [</span>
<span id="cb2-12">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"REF_SPECIES"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"REF_FOREST_TYPE"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"REF_DAMAGE_AGENT"</span>,</span>
<span id="cb2-13">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"REF_FOREST_TYPE_GROUP"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"REF_SPECIES_GROUP"</span>,</span>
<span id="cb2-14">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ...</span></span>
<span id="cb2-15">    ],</span>
<span id="cb2-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ...</span></span>
<span id="cb2-17">}</span></code></pre></div></div>
<p>Select a table, and the sidebar shows column names, types, and which are primary keys - no rows loaded.</p>
</section>
<section id="field-codebook" class="level3">
<h3 class="anchored" data-anchor-id="field-codebook">Field Codebook</h3>
<p>A hardcoded reference for the code columns that come up constantly:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Code</th>
<th>Column</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>1</td>
<td>STATUSCD</td>
<td>Live tree</td>
</tr>
<tr class="even">
<td>2</td>
<td>STATUSCD</td>
<td>Standing dead tree</td>
</tr>
<tr class="odd">
<td>1</td>
<td>COND_STATUS_CD</td>
<td>Forested - keep for most analyses</td>
</tr>
<tr class="even">
<td>5</td>
<td>COND_STATUS_CD</td>
<td>Non-forest land with trees (deforested)</td>
</tr>
<tr class="odd">
<td>10</td>
<td>DSTRBCD</td>
<td>Insect damage (general)</td>
</tr>
<tr class="even">
<td>30</td>
<td>DSTRBCD</td>
<td>Fire (general)</td>
</tr>
<tr class="odd">
<td>32</td>
<td>DSTRBCD</td>
<td>Crown fire</td>
</tr>
<tr class="even">
<td>80</td>
<td>DSTRBCD</td>
<td>Human-induced disturbance</td>
</tr>
<tr class="odd">
<td>10</td>
<td>TRTCD</td>
<td>Cutting / harvest</td>
</tr>
<tr class="even">
<td>2</td>
<td>CCLCD</td>
<td>Dominant</td>
</tr>
<tr class="odd">
<td>3</td>
<td>CCLCD</td>
<td>Codominant</td>
</tr>
<tr class="even">
<td>4</td>
<td>CCLCD</td>
<td>Intermediate</td>
</tr>
<tr class="odd">
<td>5</td>
<td>CCLCD</td>
<td>Overtopped</td>
</tr>
</tbody>
</table>
<p>Having these in one place saves reopening the user guide every 20 minutes.</p>
</section>
<section id="relationship-diagram" class="level3">
<h3 class="anchored" data-anchor-id="relationship-diagram">Relationship Diagram</h3>
<p>The feature I wish had existed when I started. The interactive graph shows foreign key relationships between tables, rendered with PyVis - hover over an edge to see exactly which columns link which tables. If PyVis isn’t available, it falls back to a static Graphviz diagram.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pyvis.network <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Network</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> build_relationship_graph(fk_info: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb3-4">    net <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Network(height<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"600px"</span>, width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"100%"</span>, directed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb3-5">    net.set_options(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'{"physics": {"solver": "forceAtlas2Based"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>)</span>
<span id="cb3-6"></span>
<span id="cb3-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> table, foreign_keys <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> fk_info.items():</span>
<span id="cb3-8">        net.add_node(table, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>table, shape<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"box"</span>)</span>
<span id="cb3-9">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> fk <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> foreign_keys:</span>
<span id="cb3-10">            net.add_edge(</span>
<span id="cb3-11">                table,</span>
<span id="cb3-12">                fk[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"referred_table"</span>],</span>
<span id="cb3-13">                title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>fk[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'constrained_columns'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> → </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>fk[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'referred_columns'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb3-14">            )</span>
<span id="cb3-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> net.generate_html()</span></code></pre></div></div>
<p>Understanding that TREE_GRM_COMPONENT → TREE via <code>TRE_CN</code>, for example, would have saved me a few confused hours if I’d had this diagram at the start.</p>
</section>
<section id="sql-templates" class="level3">
<h3 class="anchored" data-anchor-id="sql-templates">SQL Templates</h3>
<p>Pre-built query patterns for common analyses - basal area by species over time, mortality by cause code, seedling counts, forest type composition. The TREE_GRM_COMPONENT → TREE join in the mortality template is the kind of thing that’s easy to miss from the user guide alone.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">SQL_TEMPLATES <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb4-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Basal area by species over time"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb4-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">SELECT</span></span>
<span id="cb4-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    p.STATECD, p.LAT, p.LON,</span></span>
<span id="cb4-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    t.INVYR, t.SPCD,</span></span>
<span id="cb4-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    SUM(0.005454 * t.DIA * t.DIA * t.TPA_UNADJ) AS ba_per_acre</span></span>
<span id="cb4-7"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">FROM TREE t</span></span>
<span id="cb4-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">JOIN PLOT p ON t.PLT_CN = p.CN</span></span>
<span id="cb4-9"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">JOIN COND c ON c.PLT_CN = t.PLT_CN AND c.CONDID = t.CONDID</span></span>
<span id="cb4-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">WHERE t.STATUSCD = 1          -- live trees only</span></span>
<span id="cb4-11"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  AND t.DIA &gt;= 1.0</span></span>
<span id="cb4-12"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  AND c.COND_STATUS_CD = 1    -- forested conditions only</span></span>
<span id="cb4-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">GROUP BY p.STATECD, p.LAT, p.LON, t.INVYR, t.SPCD;</span></span>
<span id="cb4-14"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span>,</span>
<span id="cb4-15">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mortality by cause code"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb4-16"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">SELECT</span></span>
<span id="cb4-17"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    t.INVYR, t.AGENTCD, t.SPCD,</span></span>
<span id="cb4-18"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    SUM(grm.MICR_TPAMORT_UNADJ_AL_FOREST) AS tpa_mortality</span></span>
<span id="cb4-19"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">FROM TREE_GRM_COMPONENT grm</span></span>
<span id="cb4-20"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">JOIN TREE t ON grm.TRE_CN = t.CN   -- GRM links to TREE, not PLOT</span></span>
<span id="cb4-21"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">WHERE grm.MICR_COMPONENT_AL_FOREST IN ('MORTALITY1','MORTALITY2')</span></span>
<span id="cb4-22"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  AND grm.MICR_TPAMORT_UNADJ_AL_FOREST &gt; 0</span></span>
<span id="cb4-23"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">GROUP BY t.INVYR, t.AGENTCD, t.SPCD;</span></span>
<span id="cb4-24"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span>,</span>
<span id="cb4-25">}</span></code></pre></div></div>
</section>
</section>
<section id="things-i-learned-about-fia-along-the-way" class="level2">
<h2 class="anchored" data-anchor-id="things-i-learned-about-fia-along-the-way">Things I Learned About FIA Along the Way</h2>
<p>Some things I got wrong before I got them right:</p>
<p><strong><code>pct_forested</code> is the first filter, not an afterthought.</strong> FIA samples all US land. If you skip the forested condition filter and jump to species analysis, your numbers will be wrong and the error won’t be obvious.</p>
<p><strong>TPA_UNADJ is already on the TREE record.</strong> You don’t need to compute per-acre expansion factors from subplot geometry. <code>sum(TPA_UNADJ * 0.005454 * DIA^2)</code> gives per-acre basal area directly.</p>
<p><strong>CCLCD has a lot of NAs</strong>, particularly in older periodic plots and for woodland species. A reasonable fallback: DIA &gt;= 5 inches → overstory, below → understory.</p>
<p><strong>TREE_GRM_COMPONENT does not carry INVYR.</strong> It records measurement intervals, not calendar years. To get the inventory year for a mortality event, join back to TREE on <code>TRE_CN</code> and use <code>TREE.INVYR</code>.</p>
<p><strong>The three disturbance slots (DSTRBCD1/2/3) are ranked by importance</strong>, not by year. DSTRBCD1 is the most ecologically significant disturbance recorded, not the most recent one.</p>
</section>
<section id="taking-it-further-two-more-dashboards" class="level2">
<h2 class="anchored" data-anchor-id="taking-it-further-two-more-dashboards">Taking It Further: Two More Dashboards</h2>
<p>I built the schema navigator first, but the project kept growing. Once I had a full FIA extraction workflow, I ended up building two more dashboards that sit downstream of the same pipeline.</p>
<section id="visualizing-the-compiled-data" class="level3">
<h3 class="anchored" data-anchor-id="visualizing-the-compiled-data">Visualizing the Compiled Data</h3>
<iframe src="https://fia-data-compilation.streamlit.app?embed=true" width="100%" height="700px" frameborder="0" loading="lazy" style="border-radius:8px;"></iframe>
<p><a href="https://fia-data-compilation.streamlit.app/">Open the FIA Compiled Data Explorer</a></p>
<p>The FIA Compiled Data Explorer is for checking and exploring processed outputs, not raw schema. It reads the compiled FIA summaries produced in the <code>forest-data-compilation.Rproj</code> workflow, then surfaces them in map and chart views for QA and exploratory analysis.</p>
</section>
<section id="the-full-project" class="level3">
<h3 class="anchored" data-anchor-id="the-full-project">The Full Project</h3>
<iframe src="https://forest-data-compilation.streamlit.app?embed=true" width="100%" height="700px" frameborder="0" loading="lazy" style="border-radius:8px;"></iframe>
<p><a href="https://forest-data-compilation.streamlit.app/">Open the Full Forest Data Compilation Dashboard</a></p>
<p>The full project dashboard pulls the same idea one level up, combining FIA outputs with IDS and climate products in one place.</p>
</section>
</section>
<section id="where-to-find-workflow-details" class="level2">
<h2 class="anchored" data-anchor-id="where-to-find-workflow-details">Where to Find Workflow Details</h2>
<p>This post is about building the FIA schema navigator. If you want the full data workflow, setup details, and run instructions for <code>forest-data-compilation.Rproj</code>, they are documented in the project repo:</p>
<ul>
<li><a href="https://fiadb-dashboard.streamlit.app/">FIA Database Schema Navigator</a></li>
<li><a href="https://fia-data-compilation.streamlit.app/">FIA Compiled Data Explorer</a></li>
<li><a href="https://forest-data-compilation.streamlit.app/">Full Forest Data Compilation Dashboard</a></li>
<li><a href="https://github.com/rellimylime/forest-data-compilation/blob/main/README.md">Repository README</a></li>
<li><a href="https://github.com/rellimylime/forest-data-compilation/blob/main/scripts/SETUP.md">Setup Guide</a></li>
<li><a href="https://github.com/rellimylime/forest-data-compilation/blob/main/05_fia/WORKFLOW.md">FIA Workflow Notes</a></li>
</ul>
<hr>
<p><em>The schema navigator code lives in the <a href="https://github.com/rellimylime/forest-data-compilation">forest-data-compilation repository</a>, under <code>05_fia/docs/dashboard/fiadb_dashboard.py</code>.</em></p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{miller2026,
  author = {Miller, Emily},
  title = {Lost in the {Woods}},
  date = {2026-03-04},
  url = {https://rellimylime.github.io/posts/forest-data-compilation/},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-miller2026" class="csl-entry quarto-appendix-citeas">
Miller, Emily. 2026. <span>“Lost in the Woods.”</span> March 4, 2026. <a href="https://rellimylime.github.io/posts/forest-data-compilation/">https://rellimylime.github.io/posts/forest-data-compilation/</a>.
</div></div></section></div> ]]></description>
  <category>Personal</category>
  <category>Python</category>
  <category>R</category>
  <category>Geospatial Analysis</category>
  <category>Forest Ecology</category>
  <category>Data Science</category>
  <guid>https://rellimylime.github.io/posts/forest-data-compilation/</guid>
  <pubDate>Wed, 04 Mar 2026 08:00:00 GMT</pubDate>
  <media:content url="https://rellimylime.github.io/images/posts/forest-data-compilation/fia-thumbnail.jpeg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Out of Sync</title>
  <dc:creator>Emily Miller</dc:creator>
  <link>https://rellimylime.github.io/posts/garmin-calendar-sync/</link>
  <description><![CDATA[ 





<p><a href="https://github.com/rellimylime/google-calendar-copy"><em>Full code and setup instructions on GitHub</em></a></p>
<section id="the-problem" class="level2">
<h2 class="anchored" data-anchor-id="the-problem">The Problem</h2>
<p>My Garmin watch tracks my heart rate, sleep, stress levels, and approximately 47 other metrics I didn’t ask for. But there’s one thing it won’t do: sync Google Calendars I’m subscribed to. Only calendars I own.</p>
<p>The MEDS program schedule lives on a shared Google Calendar. I’m subscribed to it. My Garmin pretends it doesn’t exist.</p>
<p>It’s 2026. This feels like something that should have a built-in fix. If there is one, I still haven’t found it.</p>
<p>The calendar is fairly static-class schedules don’t change weekly-so I figured I’d just copy the events to my own calendar. My Garmin syncs that one fine.</p>
<p>I could do this manually, which sounds like a delightful way to spend several afternoons, or I could put in even more time to automate it.</p>
<p>Decision made: build a script.</p>
</section>
<section id="what-i-built" class="level2">
<h2 class="anchored" data-anchor-id="what-i-built">What I Built</h2>
<p>A Python script that copies events from any Google Calendar you have access to (shared calendars, subscribed calendars, whatever) into your primary calendar. Run it once, events show up on your Garmin. Problem solved.</p>
<p>Here’s what makes it actually useful:</p>
<section id="it-only-copies-what-you-need" class="level3">
<h3 class="anchored" data-anchor-id="it-only-copies-what-you-need">It Only Copies What You Need</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">new_event <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'summary'</span>: event.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'summary'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>),</span>
<span id="cb1-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'description'</span>: build_description(event),</span>
<span id="cb1-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'start'</span>: event.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'start'</span>),</span>
<span id="cb1-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'end'</span>: event.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'end'</span>),</span>
<span id="cb1-6">}</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Notice what's NOT here: attendees</span></span></code></pre></div></div>
<p>The script strips out attendee information by default. If you need to know who was on the original event, it extracts that as plain text and puts it in the description. Information preserved, nobody gets accidentally emailed.</p>
<p>(More on why that matters in a second.)</p>
</section>
<section id="safety-features-that-turned-out-to-be-essential" class="level3">
<h3 class="anchored" data-anchor-id="safety-features-that-turned-out-to-be-essential">Safety Features That Turned Out to Be Essential</h3>
<p><strong>Test before you commit</strong>: You can copy a single event first to make sure everything works.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Preview what would happen</span></span>
<span id="cb2-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python</span> copy_calendar.py copy <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--dry-run</span></span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Copy just one event to test</span></span>
<span id="cb2-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python</span> copy_calendar.py copy <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--test</span></span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Copy only through a specific date (inclusive)</span></span>
<span id="cb2-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python</span> copy_calendar.py copy <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--until</span> 2026-06-30</span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Actually do it</span></span>
<span id="cb2-11"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python</span> copy_calendar.py copy</span></code></pre></div></div>
<p><strong>Easy cleanup</strong>: Every copied event gets <code>[copied_from_external]</code> prepended to its description. If something goes wrong, you can delete all copied events with one command:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python</span> copy_calendar.py delete</span></code></pre></div></div>
<p><strong>No duplicates</strong>: Run it twice by accident? The script checks existing events and skips ones that are already there.</p>
<p><strong>Smart filtering</strong>: Only copies future events (365 days forward by default), and you can cap runs with <code>--until YYYY-MM-DD</code> when you want a shorter window.</p>
</section>
</section>
<section id="the-fun-side-story-why-these-features-exist" class="level2">
<h2 class="anchored" data-anchor-id="the-fun-side-story-why-these-features-exist">The Fun Side Story: Why These Features Exist</h2>
<p>You might be wondering why I’m so paranoid about testing and safety features.</p>
<p>My first version of this script was much simpler:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The version that seemed so clever at the time</span></span>
<span id="cb4-2">events <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> source_calendar.list_events()</span>
<span id="cb4-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> event <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> events:</span>
<span id="cb4-4">    primary_calendar.insert(event)</span></code></pre></div></div>
<p>I ran it. Events appeared in my calendar. Perfect!</p>
<p>Then my phone started buzzing. Email notification. Then another. And another.</p>
<p>Then Cat messaged: “Whats going on?”</p>
<p>I checked my inbox and felt my stomach drop.</p>
<p>I’d just triggered email invitations to every attendee on every event I copied. Staff, alumni, people I’d never met, conference room booking systems. The MEDS calendar has years of historical events. Apparently I’d just invited approximately 40 people to events that already happened.</p>
<p>Here’s what I learned: calendar attendees aren’t just metadata. When you create an event with attendees, Google Calendar sends them invitations. The attendees field is an action trigger, not a note.</p>
<blockquote class="blockquote">
<p>“All models are wrong, but some are useful.” - George Box</p>
<p>Some API calls are catastrophically wrong. - Me, learning this the hard way</p>
</blockquote>
<p>So I rewrote the script. Stripped out attendees. Added <code>sendNotifications=False</code> as a backup. Built in test mode so you can try one event first. Added the copy tag so you can easily undo everything if needed.</p>
<p>Every safety feature exists because I learned-spectacularly-why it was necessary.</p>
</section>
<section id="using-it" class="level2">
<h2 class="anchored" data-anchor-id="using-it">Using It</h2>
<p>Basic workflow:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Preview what would happen</span></span>
<span id="cb5-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python</span> copy_calendar.py copy <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--dry-run</span></span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Copy just one event to test</span></span>
<span id="cb5-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python</span> copy_calendar.py copy <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--test</span></span>
<span id="cb5-6"></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Copy through a specific date if you want to limit scope</span></span>
<span id="cb5-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python</span> copy_calendar.py copy <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--until</span> 2026-06-30</span>
<span id="cb5-9"></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Actually copy everything in the default window</span></span>
<span id="cb5-11"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python</span> copy_calendar.py copy</span>
<span id="cb5-12"></span>
<span id="cb5-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Delete all copied events if needed</span></span>
<span id="cb5-14"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python</span> copy_calendar.py delete</span></code></pre></div></div>
<p>The <a href="https://github.com/rellimylime/google-calendar-copy">GitHub repo</a> has detailed setup instructions including Google Cloud OAuth setup, finding your calendar ID, and all the boring-but-necessary configuration steps.</p>
</section>
<section id="what-this-is-good-for-and-what-its-not" class="level2">
<h2 class="anchored" data-anchor-id="what-this-is-good-for-and-what-its-not">What This Is Good For (And What It’s Not)</h2>
<p>This creates copies, not live subscriptions. If the original calendar updates, you’ll have stale data until you run the script again.</p>
<p><strong>Good for:</strong> - Static calendars that don’t change much (class schedules, semester plans, recurring meetings) - Device sync issues where your watch/fitness tracker only sees calendars you own - One-way sync where you don’t need updates to flow back</p>
<p><strong>Not good for:</strong> - Calendars that change constantly - Situations where you need real-time updates - If you can’t remember to run a script periodically</p>
<p>I run it every few weeks. Takes 30 seconds. Events show up on my Garmin. Nobody gets spammed. Problem solved.</p>
<p>Sometimes the practical answer is a Python script you run twice a month, not some elaborate real-time webhook integration.</p>
</section>
<section id="things-i-learned" class="level2">
<h2 class="anchored" data-anchor-id="things-i-learned">Things I Learned</h2>
<ul>
<li><p><strong>Test with one item before copying 600.</strong> This should be obvious. It was not obvious to me.</p></li>
<li><p><strong>Understand your data structure before you manipulate it.</strong> Calendar events aren’t just data-they trigger real-world actions.</p></li>
<li><p><strong>APIs that can email people deserve extra caution.</strong> They can embarrass you at scale.</p></li>
<li><p><strong>Reversibility is worth the five minutes.</strong> The copy tag saved me from hours of manual cleanup.</p></li>
<li><p><strong>“How hard could it be?” is always famous last words.</strong> I say this constantly. I never learn.</p></li>
</ul>
</section>
<section id="wrapping-up" class="level2">
<h2 class="anchored" data-anchor-id="wrapping-up">Wrapping Up</h2>
<p>The script lives on <a href="https://github.com/rellimylime/google-calendar-copy">GitHub</a> with full setup instructions. If you’ve got a similar problem, Garmin, Fitbit, any device that only syncs calendars you own, this might help.</p>
<p>For everyone else: consider this a reminder to read documentation before running scripts that can email strangers.</p>
<p>The real calendar sync was the mistakes we made along the way.</p>
<p>(That’s a terrible closing line. But I’m keeping it.)</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{miller2026,
  author = {Miller, Emily},
  title = {Out of {Sync}},
  date = {2026-01-28},
  url = {https://rellimylime.github.io/posts/garmin-calendar-sync/},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-miller2026" class="csl-entry quarto-appendix-citeas">
Miller, Emily. 2026. <span>“Out of Sync.”</span> January 28, 2026. <a href="https://rellimylime.github.io/posts/garmin-calendar-sync/">https://rellimylime.github.io/posts/garmin-calendar-sync/</a>.
</div></div></section></div> ]]></description>
  <category>Personal</category>
  <category>Python</category>
  <category>Tutorial</category>
  <guid>https://rellimylime.github.io/posts/garmin-calendar-sync/</guid>
  <pubDate>Wed, 28 Jan 2026 08:00:00 GMT</pubDate>
  <media:content url="https://rellimylime.github.io/images/posts/garmin-calendar-sync/garmin-calendar.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Irregular Outages</title>
  <dc:creator>Emily Miller</dc:creator>
  <link>https://rellimylime.github.io/posts/eds223-final/</link>
  <description><![CDATA[ 





<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>In February 2021, a historic winter storm swept across Texas, bringing record-low temperatures and overwhelming the state’s power grid. At the peak of the crisis, more than 4.5 million homes and businesses lost power, leaving residents without electricity for heating during freezing temperatures. The event resulted in at least 246 deaths and an estimated $195 billion in economic damages, making it one of the costliest natural disasters in U.S. history.</p>
<section id="research-question" class="level3">
<h3 class="anchored" data-anchor-id="research-question">Research Question</h3>
<p>This analysis addresses a critical question: <strong>Which communities in the Houston metropolitan area were most affected by the blackouts, and were there disparities in impacts across different income levels?</strong></p>
<p>Understanding the spatial distribution of power outages and their relationship to socioeconomic factors is essential for:</p>
<ul>
<li>Improving power grid resilience and emergency response planning</li>
<li>Identifying vulnerable communities that may need additional support during future crises</li>
<li>Informing equitable infrastructure investment decisions</li>
</ul>
</section>
</section>
<section id="background" class="level2">
<h2 class="anchored" data-anchor-id="background">Background</h2>
<section id="the-2021-texas-winter-storm-crisis" class="level3">
<h3 class="anchored" data-anchor-id="the-2021-texas-winter-storm-crisis">The 2021 Texas Winter Storm Crisis</h3>
<p>The February 2021 winter storm, known as Winter Storm Uri, exposed critical vulnerabilities in Texas’s isolated power grid. Unlike other states, Texas operates its own grid (ERCOT), which is not connected to neighboring states, limiting backup power options during emergencies. The unprecedented cold caused natural gas pipelines to freeze, wind turbines to ice over, and power plants to fail, creating a cascading crisis.</p>
<p>While the blackouts affected millions of Texans, their impacts were not uniformly distributed. Previous research on climate disasters has shown that low-income communities and communities of color often experience disproportionate impacts from extreme weather events. These communities may face longer recovery times, limited access to emergency resources, and inadequate infrastructure.</p>
</section>
<section id="remote-sensing-for-disaster-assessment" class="level3">
<h3 class="anchored" data-anchor-id="remote-sensing-for-disaster-assessment">Remote Sensing for Disaster Assessment</h3>
<p>Satellite-based night lights data provides a powerful tool for assessing power outages at scale. NASA’s Visible Infrared Imaging Radiometer Suite (VIIRS) captures daily nighttime light emissions, allowing researchers to detect changes in electrical grid function. By comparing light intensity before and after disasters, we can identify affected areas without relying solely on utility company reports, which may be incomplete during major crises.</p>
<p>This analysis combines VIIRS night lights data with OpenStreetMap building footprints and U.S. Census socioeconomic data to map blackout impacts and explore potential environmental justice dimensions of the 2021 Texas blackouts.</p>
</section>
</section>
<section id="data-and-methods" class="level2">
<h2 class="anchored" data-anchor-id="data-and-methods">Data and Methods</h2>
<section id="data-sources" class="level3">
<h3 class="anchored" data-anchor-id="data-sources">Data Sources</h3>
<p>This analysis integrates four primary datasets:</p>
<p><strong>VIIRS Night Lights Data</strong> - NASA’s VIIRS instrument provides daily measurements of nighttime light intensity. I used two dates of imagery: February 7, 2021 (before the storm) and February 16, 2021 (during the height of the blackouts). The data is measured in nanowatts per square centimeter per steradian (nW cm⁻²sr⁻¹) <span class="citation" data-cites="nasa2021">(NASA LAADS DAAC 2021)</span>.</p>
<p><strong>OpenStreetMap Building Footprints</strong> - Residential building polygons for the Houston metropolitan area, including houses, apartments, and other residential structures <span class="citation" data-cites="openstreetmap2021">(OpenStreetMap Contributors 2021)</span>.</p>
<p><strong>OpenStreetMap Road Network</strong> - Highway and major road data used to filter out areas where reduced light may be due to decreased traffic rather than power outages <span class="citation" data-cites="openstreetmap2021">(OpenStreetMap Contributors 2021)</span>.</p>
<p><strong>U.S. Census American Community Survey</strong> - 2015-2019 five-year estimates at the census tract level, including median household income data <span class="citation" data-cites="census2019">(U.S. Census Bureau 2019)</span>.</p>
</section>
<section id="analytical-approach" class="level3">
<h3 class="anchored" data-anchor-id="analytical-approach">Analytical Approach</h3>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load required packages</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Data manipulation and visualization</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Spatial data handling</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(here)           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># File path management</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(terra)          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Raster data processing</span></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stars)          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Spatiotemporal arrays</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tmap)           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Thematic mapping</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(kableExtra)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Table formatting</span></span></code></pre></div></div>
</details>
</div>
<p>The analysis follows these steps:</p>
<ol type="1">
<li><strong>Load and prepare spatial data</strong> - Read VIIRS night lights tiles, building footprints, road networks, and census data</li>
<li><strong>Calculate light intensity changes</strong> - Compare pre- and post-storm imagery to identify areas with significant light reduction</li>
<li><strong>Define blackout threshold</strong> - Classify areas with light intensity drops &gt;200 nW cm⁻²sr⁻¹ as experiencing blackouts</li>
<li><strong>Filter highway corridors</strong> - Exclude areas within 200m of major highways to avoid misidentifying reduced traffic as power outages</li>
<li><strong>Identify impacted buildings</strong> - Intersect blackout areas with residential building footprints</li>
<li><strong>Analyze socioeconomic patterns</strong> - Compare median household income distributions between census tracts with and without blackouts</li>
</ol>
</section>
<section id="loading-and-preparing-the-data" class="level3">
<h3 class="anchored" data-anchor-id="loading-and-preparing-the-data">Loading and Preparing the Data</h3>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load VIIRS night lights raster tiles for Houston area</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># February 7, 2021 (pre-storm)</span></span>
<span id="cb2-3"></span>
<span id="cb2-4">VIIRS_07a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"posts/eds223-final/data/VNP46A1/VNP46A1.A2021038.h08v05.001.2021039064328.tif"</span>))</span>
<span id="cb2-5">VIIRS_07b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"posts/eds223-final/data/VNP46A1/VNP46A1.A2021038.h08v06.001.2021039064329.tif"</span>))</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># February 16, 2021 (during blackouts)</span></span>
<span id="cb2-8">VIIRS_16a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"posts/eds223-final/data/VNP46A1/VNP46A1.A2021047.h08v05.001.2021048091106.tif"</span>))</span>
<span id="cb2-9">VIIRS_16b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rast</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"posts/eds223-final/data/VNP46A1/VNP46A1.A2021047.h08v06.001.2021048091105.tif"</span>))</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load OpenStreetMap highways (motorways only)</span></span>
<span id="cb2-12">highways <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_read</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"posts/eds223-final/data/gis_osm_roads_free_1.gpkg/gis_osm_roads_free_1.gpkg"</span>),</span>
<span id="cb2-13">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">query =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM gis_osm_roads_free_1 WHERE fclass='motorway'"</span>,</span>
<span id="cb2-14">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quiet =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb2-15"></span>
<span id="cb2-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load OpenStreetMap residential buildings</span></span>
<span id="cb2-17">houses <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_read</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"posts/eds223-final/data/gis_osm_buildings_a_free_1.gpkg/gis_osm_buildings_a_free_1.gpkg"</span>),</span>
<span id="cb2-18">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">query =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM gis_osm_buildings_a_free_1</span></span>
<span id="cb2-19"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">                           WHERE (type IS NULL AND name IS NULL)</span></span>
<span id="cb2-20"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">                           OR type in ('residential', 'apartments', 'house', 'static_caravan', 'detached')"</span>,</span>
<span id="cb2-21">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quiet =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load Census ACS data - requires joining geometry with income attributes</span></span>
<span id="cb3-2">socio_geom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_read</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"posts/eds223-final/data/ACS_2019_5YR_TRACT_48_TEXAS.gdb/"</span>),</span>
<span id="cb3-3">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layer =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACS_2019_5YR_TRACT_48_TEXAS"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quiet =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-4"></span>
<span id="cb3-5">socio_income <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_read</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"posts/eds223-final/data/ACS_2019_5YR_TRACT_48_TEXAS.gdb/"</span>),</span>
<span id="cb3-6">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layer =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X19_INCOME"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quiet =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Join income data to geometries and reproject to match building data</span></span>
<span id="cb3-9">socio <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> socio_geom <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GEOID_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;">"GEOID_Data"</span>) <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;">left_join</span>(socio_income <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;">rename</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GEOID_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;">"GEOID"</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;">"GEOID_Data"</span>) <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;">st_transform</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_crs</span>(houses))</span></code></pre></div></div>
</details>
</div>
<p>All datasets use different coordinate reference systems initially, so I standardized them to EPSG:3083 (NAD83 / Texas Centric Albers Equal Area), which preserves area measurements needed for accurate spatial analysis.</p>
</section>
</section>
<section id="results" class="level2">
<h2 class="anchored" data-anchor-id="results">Results</h2>
<section id="visualizing-the-blackouts-before-and-after" class="level3">
<h3 class="anchored" data-anchor-id="visualizing-the-blackouts-before-and-after">Visualizing the Blackouts: Before and After</h3>
<p>The stark difference in nighttime light emissions reveals the massive scale of the power outages across Houston.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Merge VIIRS tiles for each date</span></span>
<span id="cb4-2">VIIRS_07 <span class="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>(VIIRS_07a, VIIRS_07b)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pre-storm</span></span>
<span id="cb4-3">VIIRS_16 <span class="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>(VIIRS_16a, VIIRS_16b)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># During blackouts</span></span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define Houston bounding box</span></span>
<span id="cb4-6">HOUSTON_BBOX <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_bbox</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;">xmin =</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;">96.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">29</span>,</span>
<span id="cb4-7">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</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;">94.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30.5</span>),</span>
<span id="cb4-8">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_crs</span>(VIIRS_07))</span>
<span id="cb4-9"></span>
<span id="cb4-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Crop to Houston area</span></span>
<span id="cb4-11">crop_07 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crop</span>(VIIRS_07, HOUSTON_BBOX)</span>
<span id="cb4-12">crop_16 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crop</span>(VIIRS_16, HOUSTON_BBOX)</span>
<span id="cb4-13"></span>
<span id="cb4-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mask unrealistic values (&gt;1000 or ≤0)</span></span>
<span id="cb4-15">masked_07 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> crop_07</span>
<span id="cb4-16">masked_07[(masked_07 <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;">1000</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> (masked_07 <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="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span></span>
<span id="cb4-17"></span>
<span id="cb4-18">masked_16 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> crop_16</span>
<span id="cb4-19">masked_16[(masked_16 <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;">1000</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> (masked_16 <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="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span></span>
<span id="cb4-20"></span>
<span id="cb4-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Combine for side-by-side comparison</span></span>
<span id="cb4-22">combined_crop <span class="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>(masked_07, masked_16)</span>
<span id="cb4-23"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(combined_crop) <span class="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;">"February 7, 2021 (Before)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"February 16, 2021 (After)"</span>)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(combined_crop) <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;">tm_raster</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.scale =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_scale</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inferno"</span>),</span>
<span id="cb5-3">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_legend</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;">"Light Intensity</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;">(nW cm⁻²sr⁻¹)"</span>,</span>
<span id="cb5-4">                                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title.size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>,</span>
<span id="cb5-5">                                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text.size =</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="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_facets</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">free.scales =</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="cb5-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_title</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Houston Night Lights: Before and After the Storm"</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.2</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;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.outside =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb5-9">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.outside.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>,</span>
<span id="cb5-10">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.outside.size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.12</span>,</span>
<span id="cb5-11">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.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;">"February 7 (Before)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"February 16 (After)"</span>),</span>
<span id="cb5-12">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontfamily =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sans"</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;">tm_graticules</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels.size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds223-final/index_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="1152"></p>
<figcaption>Night lights imagery showing Houston before (left) and during (right) the February 2021 blackouts. Darker areas in the right panel indicate regions that lost power. The widespread darkening across the metropolitan area illustrates the extensive scale of the grid failure.</figcaption>
</figure>
</div>
</div>
</div>
<p>The comparison reveals significant light reduction across much of the Houston metropolitan area. The bright urban core visible on February 7 dims substantially by February 16, indicating widespread power failures.</p>
</section>
<section id="identifying-blackout-areas" class="level3">
<h3 class="anchored" data-anchor-id="identifying-blackout-areas">Identifying Blackout Areas</h3>
<p>To systematically identify blackout zones, I calculated the difference in light intensity and classified areas with drops greater than 200 nW cm⁻²sr⁻¹ as experiencing blackouts.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate light intensity difference</span></span>
<span id="cb6-2">VIIRS_houston_diff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> VIIRS_07 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> VIIRS_16</span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Classify blackout areas (drops &gt;200 as blackouts)</span></span>
<span id="cb6-5">blackout_mask <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">classify</span>(VIIRS_houston_diff,</span>
<span id="cb6-6">                         <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Not a blackout</span></span>
<span id="cb6-7">                                  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Blackout</span></span>
<span id="cb6-8">                                <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;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert raster to vector polygons</span></span>
<span id="cb6-11">blackout_vector <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_as_stars</span>(blackout_mask) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_as_sf</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_make_valid</span>()</span>
<span id="cb6-14"></span>
<span id="cb6-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Crop to Houston and reproject</span></span>
<span id="cb6-16">blackout_houston <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_crop</span>(blackout_vector, HOUSTON_BBOX) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3083</span>)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">road_color <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;">"#FDB863"</span></span>
<span id="cb7-2">fill_color <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;">"#d62728"</span></span>
<span id="cb7-3">border_color <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;">"#8b0000"</span></span>
<span id="cb7-4"></span>
<span id="cb7-5">blackout_houston_union <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_union</span>(blackout_houston)</span>
<span id="cb7-6"></span>
<span id="cb7-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_basemap</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CartoDB.VoyagerNoLabels"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(blackout_houston_union) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> fill_color,</span>
<span id="cb7-10">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> border_color,</span>
<span id="cb7-11">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</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="cb7-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_title</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Detected Blackout Areas in Houston"</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.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_add_legend</span>(<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;">"polygons"</span>,</span>
<span id="cb7-14">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> fill_color,</span>
<span id="cb7-15">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> border_color,</span>
<span id="cb7-16">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">border.lwd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb7-17">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Blackout Areas"</span>,</span>
<span id="cb7-18">                <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;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>),</span>
<span id="cb7-20">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.bg.color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb7-21">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.bg.alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>,</span>
<span id="cb7-22">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text.fontfamily =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sans"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_scalebar</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text.size =</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="cb7-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_compass</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</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></code></pre></div></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds223-final/index_files/figure-html/fig_initial_blackouts-1.png" class="img-fluid figure-img" width="768"></p>
<figcaption>Initial identification of blackout areas based on night lights analysis. Red areas experienced light intensity drops exceeding 200 nW cm⁻²sr⁻¹, indicating likely power outages.</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="refining-the-analysis-excluding-highways" class="level3">
<h3 class="anchored" data-anchor-id="refining-the-analysis-excluding-highways">Refining the Analysis: Excluding Highways</h3>
<p>Major highways may show reduced light intensity due to decreased traffic rather than residential power outages. To improve accuracy, I excluded areas within 200 meters of motorways.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reproject highways and create 200m buffer</span></span>
<span id="cb8-2">highways <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(highways, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3083</span>)</span>
<span id="cb8-3">highway_buffer <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> highways <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_union</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_buffer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb8-6"></span>
<span id="cb8-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove highway buffers from blackout areas</span></span>
<span id="cb8-8">blackout_final <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_difference</span>(blackout_houston, highway_buffer)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Visualize the highway exclusion process</span></span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_basemap</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CartoDB.VoyagerNoLabels"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(highways) <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;">tm_lines</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> road_color,</span>
<span id="cb9-5">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_union</span>(blackout_final)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> fill_color,</span>
<span id="cb9-8">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#4d0000"</span>,</span>
<span id="cb9-9">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</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="cb9-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_add_legend</span>(<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;">"polygons"</span>,</span>
<span id="cb9-11">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Refined blackout areas (highways excluded)"</span>,</span>
<span id="cb9-12">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> fill_color) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_add_legend</span>(<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;">"lines"</span>,</span>
<span id="cb9-14">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Major highways"</span>,</span>
<span id="cb9-15">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> road_color,</span>
<span id="cb9-16">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_title</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Highway Corridor Exclusion"</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.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>),</span>
<span id="cb9-19">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.bg.color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb9-20">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.bg.alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>,</span>
<span id="cb9-21">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.text.size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.65</span>,</span>
<span id="cb9-22">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text.fontfamily =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sans"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_scalebar</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text.size =</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="cb9-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_compass</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</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></code></pre></div></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds223-final/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>Refinement of blackout areas by excluding highway corridors. The yellow lines show major highways with 200m buffers applied. This step removes areas where reduced light may be due to decreased traffic rather than residential power outages.</figcaption>
</figure>
</div>
</div>
</div>
<p>This refinement helps ensure we’re identifying genuine residential power outages rather than changes in traffic patterns.</p>
</section>
<section id="quantifying-residential-impacts" class="level3">
<h3 class="anchored" data-anchor-id="quantifying-residential-impacts">Quantifying Residential Impacts</h3>
<p>By intersecting the blackout areas with residential building footprints, we can estimate how many homes lost power.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reproject buildings and identify those in blackout zones</span></span>
<span id="cb10-2">houses <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(houses, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3083</span>)</span>
<span id="cb10-3">blackout_homes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_intersection</span>(houses, blackout_final)</span>
<span id="cb10-4"></span>
<span id="cb10-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate statistics</span></span>
<span id="cb10-6">n_blackout_homes <span class="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;">unique</span>(blackout_homes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_id))</span>
<span id="cb10-7">n_total_homes <span class="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>(houses)</span>
<span id="cb10-8">pct_impacted <span class="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>((n_blackout_homes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n_total_homes) <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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
</details>
</div>
<p><strong>Finding:</strong> Approximately <strong>157,410</strong> residential buildings (33.1% of structures in the dataset) experienced blackouts during the February 2021 storms.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create point representations for mapping</span></span>
<span id="cb11-2">all_homes_points <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_centroid</span>(houses)</span>
<span id="cb11-3">not_impacted_homes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> all_homes_points[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>all_homes_points<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> blackout_homes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_id, ]</span>
<span id="cb11-4">impacted_homes_points <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_centroid</span>(blackout_homes)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_basemap</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CartoDB.VoyagerNoLabels"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(blackout_final) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_polygons</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;">"grey85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey70"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</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="cb12-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(not_impacted_homes) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_dots</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;">"#FFB3B3"</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.003</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_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="cb12-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_shape</span>(impacted_homes_points) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_dots</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;">"#8B0000"</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.005</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_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="cb12-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_title</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Residential Buildings Impacted by Blackouts"</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.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.outside =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb12-10">            <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>),</span>
<span id="cb12-11">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.bg.color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb12-12">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.bg.alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>,</span>
<span id="cb12-13">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.text.size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>,</span>
<span id="cb12-14">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title.size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>,</span>
<span id="cb12-15">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontfamily =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sans"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_add_legend</span>(<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;">"fill"</span>,</span>
<span id="cb12-17">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Blackout zone"</span>,</span>
<span id="cb12-18">                <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;">"grey85"</span>,</span>
<span id="cb12-19">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey70"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_add_legend</span>(<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;">"symbol"</span>,</span>
<span id="cb12-21">                <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;">"Power lost"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power maintained"</span>),</span>
<span id="cb12-22">                <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8B0000"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FFB3B3"</span>),</span>
<span id="cb12-23">                <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;">19</span>,</span>
<span id="cb12-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;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_scalebar</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text.size =</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="cb12-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_compass</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</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></code></pre></div></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds223-final/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>Spatial distribution of impacted and non-impacted residential buildings. Dark red points represent homes that experienced blackouts, while light pink points indicate buildings that maintained power throughout the crisis.</figcaption>
</figure>
</div>
</div>
</div>
<p>The map reveals that blackouts were widespread but not uniform. Some neighborhoods maintained power while adjacent areas went dark, suggesting the complexity of the grid failures.</p>
</section>
<section id="socioeconomic-analysis-income-and-blackout-exposure" class="level3">
<h3 class="anchored" data-anchor-id="socioeconomic-analysis-income-and-blackout-exposure">Socioeconomic Analysis: Income and Blackout Exposure</h3>
<p>Understanding <em>where</em> blackouts occurred is only part of the story. To assess whether impacts were distributed equitably, I aggregated affected buildings by census tract and examined their relationship to median household income. This tract-level analysis reveals whether certain communities bore a disproportionate burden during the crisis.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Transform census data to analysis CRS</span></span>
<span id="cb13-2">socio <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(socio, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3083</span>)</span>
<span id="cb13-3"></span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Crop to Houston area</span></span>
<span id="cb13-5">houston_tracts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_crop</span>(socio, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_bbox</span>(blackout_houston))</span>
<span id="cb13-6"></span>
<span id="cb13-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Count impacted homes per tract</span></span>
<span id="cb13-8">houston_tracts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_homes <span class="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;">lengths</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_intersects</span>(houston_tracts, blackout_homes)))</span>
<span id="cb13-9"></span>
<span id="cb13-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create impact categories for mapping</span></span>
<span id="cb13-11">houston_tracts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>impact_category <span class="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>(houston_tracts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_homes,</span>
<span id="cb13-12">                                       <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;">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;">10</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;">150</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1500</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>),</span>
<span id="cb13-13">                                       <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;">"None"</span>,</span>
<span id="cb13-14">                                                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Very Low (1-10)"</span>,</span>
<span id="cb13-15">                                                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low (11-50)"</span>,</span>
<span id="cb13-16">                                                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medium (51-150)"</span>,</span>
<span id="cb13-17">                                                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High (151-500)"</span>,</span>
<span id="cb13-18">                                                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Very High (501-1,500)"</span>,</span>
<span id="cb13-19">                                                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Extreme (&gt;1,500)"</span>),</span>
<span id="cb13-20">                                       <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="cb13-21"></span>
<span id="cb13-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create binary blackout indicator</span></span>
<span id="cb13-23">houston_tracts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>blackout <span class="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>(houston_tracts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_homes <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="cb13-24">                                  <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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>),</span>
<span id="cb13-25">                                  <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;">"Blackout"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No Blackout"</span>))</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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;"># Transform tracts to match crs of basemap (this is done automatically by tmap, code included for transparency)</span></span>
<span id="cb14-2">houston_tracts_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;">st_transform</span>(houston_tracts, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3857</span>)</span>
<span id="cb14-3"></span>
<span id="cb14-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot houses in census tracts affected by blackouts</span></span>
<span id="cb14-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_basemap</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CartoDB.VoyagerNoLabels"</span>) <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;">tm_shape</span>(houston_tracts_proj) <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;">tm_fill</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;">"impact_category"</span>,</span>
<span id="cb14-8">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill.scale =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_scale</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;">"white"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#feedde"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#fdd0a2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#fdae6b"</span>,</span>
<span id="cb14-9">                                          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#fd8d3c"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#e6550d"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#a63603"</span>),</span>
<span id="cb14-10">                                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value.na =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"transparent"</span>),</span>
<span id="cb14-11">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill.legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_legend</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;">"Impacted Homes</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;">per Census Tract"</span>,</span>
<span id="cb14-12">                                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title.size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>,</span>
<span id="cb14-13">                                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text.size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.65</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_borders</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey50"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</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="cb14-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_title</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Blackout Severity by Census Tract"</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.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_scalebar</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text.size =</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="cb14-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_compass</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</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="cb14-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tm_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.outside =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb14-19">            <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>),</span>
<span id="cb14-20">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.bg.color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb14-21">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.bg.alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>,</span>
<span id="cb14-22">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text.fontfamily =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sans"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds223-final/index_files/figure-html/fig_tract_impacts-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>Number of residential buildings that lost power by census tract. Darker colors indicate higher numbers of impacted homes. The spatial pattern shows that some tracts experienced extreme impacts while neighboring areas had minimal disruption.</figcaption>
</figure>
</div>
</div>
</div>
<p>The census tract analysis reveals substantial variation in blackout severity. Some tracts lost power for more than 1,500 residential buildings, while others experienced minimal disruption. This raises a critical question: were certain communities disproportionately affected?</p>
<p>Previous research on climate disasters shows that low-income communities and communities of color often experience greater exposure and more severe impacts. To investigate whether similar patterns emerged during the Texas blackouts, I analyzed the relationship between median household income and blackout exposure.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">houston_tracts_no_na <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> houston_tracts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-2">  <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>(B19013e1))</span>
<span id="cb15-3"></span>
<span id="cb15-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(houston_tracts_no_na, <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> blackout, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> B19013e1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> blackout)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</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.75</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outlier.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="cb15-6">  <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;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Blackout Status"</span>,</span>
<span id="cb15-7">                    <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;">"Blackout"</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;">"#d62728"</span>,</span>
<span id="cb15-8">                               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No Blackout"</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;">"#7f7f7f"</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;">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;">dollar_format</span>(),</span>
<span id="cb15-10">                    <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">250000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50000</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Median Household Income by Blackout Status"</span>,</span>
<span id="cb15-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;">"Houston Metropolitan Area Census Tracts (2015-2019 ACS)"</span>,</span>
<span id="cb15-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;">""</span>,</span>
<span id="cb15-14">       <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;">"Median Household Income"</span>,</span>
<span id="cb15-15">       <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;">"Source: U.S. Census Bureau American Community Survey"</span>) <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;">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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sans"</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;">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-18">        <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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb15-19">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</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;">11</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="cb15-20">        <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></code></pre></div></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds223-final/index_files/figure-html/fig_income_comparison-1.png" class="img-fluid figure-img" width="864"></p>
<figcaption>Median household income comparison between census tracts that experienced blackouts versus those that maintained power. The similar distributions suggest that income was not a strong predictor of blackout exposure during this crisis.</figcaption>
</figure>
</div>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">income_stats <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> houston_tracts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_drop_geometry</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-3">  <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>(B19013e1)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(blackout) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb16-6">    <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;">Census Tracts</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;">n</span>(),</span>
<span id="cb16-7">    <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;">Median Income</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> 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;">dollar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(B19013e1)),</span>
<span id="cb16-8">    <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;">Mean Income</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> 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;">dollar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(B19013e1)),</span>
<span id="cb16-9">    <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;">Standard Deviation</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> 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;">dollar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(B19013e1))</span>
<span id="cb16-10">  )</span>
<span id="cb16-11"></span>
<span id="cb16-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(income_stats,</span>
<span id="cb16-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;">"Table 1: Income Statistics by Blackout Status"</span>,</span>
<span id="cb16-14">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</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;">"l"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_styling</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bootstrap_options =</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;">"striped"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hover"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"condensed"</span>),</span>
<span id="cb16-16">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full_width =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb16-17">                <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;">"left"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<table class="table table-striped table-hover table-condensed caption-top table-sm small">
<caption>Table 1: Income Statistics by Blackout Status</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">blackout</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Census Tracts</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Median Income</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Mean Income</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Standard Deviation</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Blackout</td>
<td style="text-align: right;">749</td>
<td style="text-align: right;">$60,435</td>
<td style="text-align: right;">$71,262.24</td>
<td style="text-align: right;">$39,649.34</td>
</tr>
<tr class="even">
<td style="text-align: left;">No Blackout</td>
<td style="text-align: right;">355</td>
<td style="text-align: right;">$57,385</td>
<td style="text-align: right;">$67,352.78</td>
<td style="text-align: right;">$35,900.53</td>
</tr>
</tbody>
</table>
</div>
</div>
<p><strong>Key Finding:</strong> Census tracts that experienced blackouts had a median household income of <strong>60,435</strong>, compared to <strong>57,385</strong> for tracts without blackouts. The difference of approximately $3,000 represents only about 5% variation, suggesting that income level was not a strong predictor of blackout exposure in this event.</p>
</section>
</section>
<section id="discussion" class="level2">
<h2 class="anchored" data-anchor-id="discussion">Discussion</h2>
<section id="main-findings" class="level3">
<h3 class="anchored" data-anchor-id="main-findings">Main Findings</h3>
<p>This analysis identified approximately <strong>157,410</strong> residential buildings (33.1% of the study area) that lost power during the February 2021 storms. Unlike typical climate disasters, blackout exposure showed minimal income-based disparities-tracts with and without blackouts had median incomes differing by only ~$3,000 (5%). This likely reflects the systemic nature of the grid failure: when the entire system collapsed, infrastructure vulnerabilities crossed neighborhood boundaries.</p>
<p>However, <em>exposure</em> differs from <em>impact</em>. Low-income communities faced greater difficulty evacuating, accessing backup heating, and recovering-even when blackout rates were similar <span class="citation" data-cites="gronlund2022">(Gronlund 2014)</span>.</p>
</section>
<section id="limitations" class="level3">
<h3 class="anchored" data-anchor-id="limitations">Limitations</h3>
<p><strong>Temporal resolution:</strong> Two snapshots cannot capture outage duration, which may vary by neighborhood.</p>
<p><strong>Threshold sensitivity:</strong> The 200 nW cm⁻²sr⁻¹ cutoff is somewhat arbitrary and may miss some outages.</p>
<p><strong>Data gaps:</strong> OpenStreetMap building coverage varies geographically, potentially introducing spatial bias.</p>
<p><strong>Highway exclusion:</strong> The 200m buffer may inadvertently exclude legitimate residential blackouts.</p>
<p>Future work should incorporate daily time series data, analyze additional socioeconomic indicators, integrate health outcomes, and ground-truth results with utility records.</p>
</section>
<section id="policy-implications" class="level3">
<h3 class="anchored" data-anchor-id="policy-implications">Policy Implications</h3>
<p>The widespread blackouts indicate systemic vulnerabilities requiring comprehensive infrastructure winterization. Even when disasters affect communities uniformly, emergency response must account for differential vulnerabilities. Texas’s grid isolation limited backup options during the crisis, suggesting value in regional interconnection. Remote sensing approaches can provide rapid, independent disaster assessment to complement utility reporting.</p>
</section>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>The February 2021 blackouts revealed catastrophic infrastructure vulnerabilities affecting millions. This analysis demonstrates how satellite remote sensing combined with socioeconomic data enables rapid disaster assessment. While blackouts affected Houston relatively uniformly across income levels-likely due to systemic grid failure-this doesn’t mean impacts were equitable. Understanding both exposure patterns and underlying vulnerabilities remains essential for building resilient, just infrastructure as climate change intensifies extreme weather events.</p>
</section>
<section id="data-sources-and-acknowledgments" class="level2">
<h2 class="anchored" data-anchor-id="data-sources-and-acknowledgments">Data Sources and Acknowledgments</h2>
<p><strong>VIIRS Night Lights Data (VNP46A1)</strong> - NASA’s Visible Infrared Imaging Radiometer Suite, Suomi NPP satellite, tiles h08v05 and h08v06. Accessed via NASA Earthdata: <a href="https://worldview.earthdata.nasa.gov/" class="uri">https://worldview.earthdata.nasa.gov/</a></p>
<p><strong>OpenStreetMap</strong> - Road network and building footprint data. Accessed via Geofabrik: <a href="https://download.geofabrik.de/" class="uri">https://download.geofabrik.de/</a></p>
<p><strong>U.S. Census Bureau</strong> - American Community Survey 5-Year Estimates (2015-2019), Texas census tracts. Accessed via: <a href="https://www.census.gov/programs-surveys/acs" class="uri">https://www.census.gov/programs-surveys/acs</a></p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-gronlund2022" class="csl-entry">
Gronlund, Carina J. 2014. <span>“Racial and Socioeconomic Disparities in Heat-Related Health Effects and Their Mechanisms: A Review.”</span> <em>Current Epidemiology Reports</em> 1 (3): 165–73. <a href="https://doi.org/10.1007/s40471-014-0014-4">https://doi.org/10.1007/s40471-014-0014-4</a>.
</div>
<div id="ref-nasa2021" class="csl-entry">
NASA LAADS DAAC. 2021. <span>“VIIRS/NPP Daily Gridded Day Night Band 15 Arc-Second Linear Lat Lon Grid Night.”</span> NASA Level-1; Atmosphere Archive &amp; Distribution System Distributed Active Archive Center. <a href="https://ladsweb.modaps.eosdis.nasa.gov/missions-and-measurements/products/VNP46A1/">https://ladsweb.modaps.eosdis.nasa.gov/missions-and-measurements/products/VNP46A1/</a>.
</div>
<div id="ref-openstreetmap2021" class="csl-entry">
OpenStreetMap Contributors. 2021. <span>“OpenStreetMap Data Extracts.”</span> Geofabrik GmbH. <a href="https://download.geofabrik.de/">https://download.geofabrik.de/</a>.
</div>
<div id="ref-census2019" class="csl-entry">
U.S. Census Bureau. 2019. <span>“American Community Survey 5-Year Data (2015-2019).”</span> U.S. Census Bureau. <a href="https://www.census.gov/programs-surveys/acs/technical-documentation/table-and-geography-changes/2019/5-year.html">https://www.census.gov/programs-surveys/acs/technical-documentation/table-and-geography-changes/2019/5-year.html</a>.
</div>
</div>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{miller2025,
  author = {Miller, Emily},
  title = {Irregular {Outages}},
  date = {2025-12-03},
  url = {https://rellimylime.github.io/posts/eds223-final/},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-miller2025" class="csl-entry quarto-appendix-citeas">
Miller, Emily. 2025. <span>“Irregular Outages.”</span> December 3, 2025.
<a href="https://rellimylime.github.io/posts/eds223-final/">https://rellimylime.github.io/posts/eds223-final/</a>.
</div></div></section></div> ]]></description>
  <category>MEDS</category>
  <category>R</category>
  <category>Geospatial Analysis</category>
  <category>Remote Sensing</category>
  <category>Environmental Justice</category>
  <guid>https://rellimylime.github.io/posts/eds223-final/</guid>
  <pubDate>Wed, 03 Dec 2025 08:00:00 GMT</pubDate>
  <media:content url="https://rellimylime.github.io/images/posts/eds223-final/eds223-final-cover.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Burn Scars and Unequal Burdens</title>
  <dc:creator>Emily Miller</dc:creator>
  <link>https://rellimylime.github.io/posts/eds220-final/</link>
  <description><![CDATA[ 





<section id="about" class="level2">
<h2 class="anchored" data-anchor-id="about">About</h2>
<p>In January 2025, the Eaton and Palisades fires swept through Los Angeles County, leaving lasting impacts on both the landscape and the communities within their paths. The Palisades Fire, which started on January 7, became one of the most destructive wildfires in California history, burning through the scenic Pacific Palisades neighborhood and nearby communities. Meanwhile, the Eaton Fire ignited in the foothills near Altadena and Pasadena, threatening densely populated areas and critical infrastructure.</p>
<p>Understanding wildfire impacts requires more than just mapping burned areas-it requires examining both the physical changes to the landscape and the social dimensions of who is affected. This analysis combines <strong>remote sensing techniques</strong> with <strong>environmental justice data</strong> to provide a comprehensive view of these devastating fires.</p>
<p><strong>Repository:</strong> <a href="https://github.com/rellimylime/eaton-palisades-fires-analysis" class="uri">https://github.com/rellimylime/eaton-palisades-fires-analysis</a> <em>For complete analysis notebooks with additional data exploration and detailed outputs, visit the GitHub repository.</em></p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rellimylime.github.io/images/posts/eds220-final/LAfire.webp" class="img-fluid figure-img"></p>
<figcaption>Source: https://yaleclimateconnections.org/2025/01/the-role-of-climate-change-in-the-catastrophic-2025-los-angeles-fires/</figcaption>
</figure>
</div>
</section>
<section id="highlights" class="level2">
<h2 class="anchored" data-anchor-id="highlights">Highlights</h2>
<p>This analysis demonstrates several key Python-based geospatial analysis techniques:</p>
<ul>
<li><strong>False color visualization</strong> using <code>xarray</code> and <code>rioxarray</code> to process Landsat 8 SWIR/NIR/Red bands for burn scar identification</li>
<li><strong>Multi-dataset integration</strong> with <code>geopandas</code> combining satellite raster imagery with vector fire perimeter and census tract data</li>
<li><strong>CRS management and reprojection</strong> using <code>rio.write_crs()</code> and <code>to_crs()</code> to ensure spatial alignment across different data sources</li>
<li><strong>Environmental justice analysis</strong> with spatial joins (<code>gpd.sjoin()</code>) and clipping to examine racial demographics in fire-affected areas</li>
</ul>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<section id="landsat-8-surface-reflectance-data" class="level3">
<h3 class="anchored" data-anchor-id="landsat-8-surface-reflectance-data">Landsat 8 Surface Reflectance Data</h3>
<p>This dataset contains atmospherically corrected surface reflectance data from the Landsat 8 satellite, captured on February 23, 2025 <span class="citation" data-cites="microsoft2025landsat">(Microsoft Planetary Computer 2025)</span>. The imagery includes five spectral bands (red, green, blue, near-infrared, and shortwave infrared) clipped to the area surrounding the Eaton and Palisades fire perimeters. The data was retrieved from the Microsoft Planetary Computer STAC catalog in NetCDF format.</p>
</section>
<section id="fire-perimeter-shapefiles" class="level3">
<h3 class="anchored" data-anchor-id="fire-perimeter-shapefiles">Fire Perimeter Shapefiles</h3>
<p>These shapefiles delineate the official fire perimeters for the Eaton and Palisades fires as of January 21, 2025 <span class="citation" data-cites="arcgis2025fireperimeters">(ArcGIS Hub 2025)</span>. The vector data includes boundary geometries and attribute information for each fire.</p>
</section>
<section id="environmental-justice-index-eji-data" class="level3">
<h3 class="anchored" data-anchor-id="environmental-justice-index-eji-data">Environmental Justice Index (EJI) Data</h3>
<p>The CDC/ATSDR Environmental Justice Index <span class="citation" data-cites="cdc_eji">(Centers for Disease Control and Prevention and Agency for Toxic Substances and Disease Registry 2024)</span> provides census tract-level demographic and socioeconomic data. This analysis uses the 2024 California EJI dataset to examine the racial composition of fire-affected communities through minority population percentile rankings.</p>
<hr>
</section>
</section>
<section id="analysis-1-false-color-visualization" class="level2">
<h2 class="anchored" data-anchor-id="analysis-1-false-color-visualization">Analysis 1: False Color Visualization</h2>
<section id="setup-and-data-import" class="level3">
<h3 class="anchored" data-anchor-id="setup-and-data-import">Setup and Data Import</h3>
<div id="setup" class="cell" data-execution_count="1">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import required libraries for geospatial analysis and visualization</span></span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> xarray <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> xr</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> rioxarray <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> rio</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span></code></pre></div></div>
</details>
</div>
</section>
<section id="loading-fire-perimeter-data" class="level3">
<h3 class="anchored" data-anchor-id="loading-fire-perimeter-data">Loading Fire Perimeter Data</h3>
<p>Fire perimeters provide the spatial boundaries for our analysis, allowing us to focus on the areas most affected by the fires.</p>
<div id="load-fire-perimeters" class="cell" data-execution_count="2">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load fire perimeter shapefiles</span></span>
<span id="cb2-2">eaton <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data/Eaton_Perimeter_20250121.shp'</span>)</span>
<span id="cb2-3">palisades <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data/Palisades_Perimeter_20250121.shp'</span>)</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create summary table for data exploration</span></span>
<span id="cb2-6">summary_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb2-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Dataset'</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Eaton Fire'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Palisades Fire'</span>],</span>
<span id="cb2-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Features'</span>: [<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(eaton), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(palisades)],</span>
<span id="cb2-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Columns'</span>: [<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(eaton.columns), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(palisades.columns)],</span>
<span id="cb2-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Geometry Type'</span>: [eaton.geometry.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span>.unique()[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], palisades.geometry.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span>.unique()[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]]</span>
<span id="cb2-11">}</span>
<span id="cb2-12"></span>
<span id="cb2-13">summary_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(summary_data)</span>
<span id="cb2-14"></span>
<span id="cb2-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display as formatted table</span></span>
<span id="cb2-16">summary_df</span></code></pre></div></div>
</details>
</div>
<p>Both datasets contain polygon geometries representing the fire boundaries, along with area and perimeter measurements.</p>
<section id="coordinate-reference-system-check" class="level4">
<h4 class="anchored" data-anchor-id="coordinate-reference-system-check">Coordinate Reference System Check</h4>
<div id="crs-check" class="cell" data-execution_count="3">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create CRS information table</span></span>
<span id="cb3-2">crs_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb3-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Dataset'</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Eaton Fire'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Palisades Fire'</span>],</span>
<span id="cb3-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CRS Code'</span>: [eaton.crs.to_authority()[<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;">if</span> eaton.crs.to_authority() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N/A'</span>,</span>
<span id="cb3-5">                 palisades.crs.to_authority()[<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;">if</span> palisades.crs.to_authority() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N/A'</span>],</span>
<span id="cb3-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Projection Type'</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Geographic'</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> eaton.crs.is_geographic <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Projected'</span>,</span>
<span id="cb3-7">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Geographic'</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> palisades.crs.is_geographic <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Projected'</span>]</span>
<span id="cb3-8">}</span>
<span id="cb3-9"></span>
<span id="cb3-10">crs_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(crs_data)</span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display as formatted table</span></span>
<span id="cb3-13">crs_df</span></code></pre></div></div>
</details>
</div>
<p>Both datasets use EPSG:3857 (Web Mercator), a projected coordinate system suitable for web mapping applications.</p>
</section>
</section>
<section id="loading-and-processing-landsat-imagery" class="level3">
<h3 class="anchored" data-anchor-id="loading-and-processing-landsat-imagery">Loading and Processing Landsat Imagery</h3>
<p>The Landsat 8 scene contains multiple spectral bands that capture different wavelengths of reflected light. Each band provides unique information about surface features.</p>
<div id="load-landsat" class="cell" data-execution_count="4">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import Landsat data</span></span>
<span id="cb4-2">landsat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> xr.open_dataset(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data/landsat8-2025-02-23-palisades-eaton.nc'</span>)</span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create Landsat dataset summary table</span></span>
<span id="cb4-5">landsat_summary <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb4-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Property'</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Dimensions'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Height (y)'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Width (x)'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Coordinates'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Data Variables'</span>],</span>
<span id="cb4-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Value'</span>: [</span>
<span id="cb4-8">        <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(landsat.dims)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">D"</span>,</span>
<span id="cb4-9">        <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>landsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>dims[<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 class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> pixels"</span>,</span>
<span id="cb4-10">        <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>landsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>dims[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> pixels"</span>,</span>
<span id="cb4-11">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">', '</span>.join(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(landsat.coords)),</span>
<span id="cb4-12">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">', '</span>.join([v <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> v <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> landsat.data_vars <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'spatial_ref'</span>])</span>
<span id="cb4-13">    ]</span>
<span id="cb4-14">}</span>
<span id="cb4-15"></span>
<span id="cb4-16">landsat_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(landsat_summary)</span>
<span id="cb4-17"></span>
<span id="cb4-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display as formatted table</span></span>
<span id="cb4-19">landsat_df</span></code></pre></div></div>
</details>
</div>
<p>The dataset spans <strong>1418 × 2742 pixels</strong> and contains five spectral bands: <strong>red, green, blue, nir08, swir22</strong>.</p>
<section id="restoring-geospatial-information" class="level4">
<h4 class="anchored" data-anchor-id="restoring-geospatial-information">Restoring Geospatial Information</h4>
<p>Before we can perform spatial operations, we need to verify and restore the coordinate reference system (CRS) information.</p>
<div id="restore-crs" class="cell" data-execution_count="5">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Recover the geospatial information from spatial_ref</span></span>
<span id="cb5-2">landsat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> landsat.rio.write_crs(landsat.spatial_ref.crs_wkt)</span></code></pre></div></div>
</details>
</div>
<blockquote class="blockquote">
<p>The dataset’s CRS is restored from the <code>spatial_ref</code> metadata variable, setting it to <strong>EPSG:32611</strong> (WGS 84 / UTM zone 11N), which is appropriate for the Los Angeles area.</p>
</blockquote>
</section>
<section id="handling-missing-values" class="level4">
<h4 class="anchored" data-anchor-id="handling-missing-values">Handling Missing Values</h4>
<div id="check-nan-values" class="cell" data-execution_count="6">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check for NaN values in each band</span></span>
<span id="cb6-2">bands <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'green'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'nir08'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'swir22'</span>]</span>
<span id="cb6-3">nan_status <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb6-4"></span>
<span id="cb6-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> band <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> bands:</span>
<span id="cb6-6">    has_nan <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.isnan(landsat[band]).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">any</span>().values</span>
<span id="cb6-7">    nan_count <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.isnan(landsat[band]).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>().values</span>
<span id="cb6-8">    nan_status.append({</span>
<span id="cb6-9">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Band'</span>: band.upper(),</span>
<span id="cb6-10">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Contains NaN'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Yes'</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> has_nan <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'No'</span>,</span>
<span id="cb6-11">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'NaN Count'</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>(nan_count)</span>
<span id="cb6-12">    })</span>
<span id="cb6-13"></span>
<span id="cb6-14">nan_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(nan_status)</span>
<span id="cb6-15"></span>
<span id="cb6-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display as formatted table</span></span>
<span id="cb6-17">nan_df</span></code></pre></div></div>
</details>
</div>
<div id="fill-nan-values" class="cell" data-execution_count="7">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fill NaN values with 0</span></span>
<span id="cb7-2">landsat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> landsat.fillna(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span></code></pre></div></div>
</details>
</div>
<p>All five spectral bands contain NaN values at the edges of the scene or in clouded areas. These are filled with zero to prevent visualization errors.</p>
</section>
</section>
<section id="true-color-visualization" class="level3">
<h3 class="anchored" data-anchor-id="true-color-visualization">True Color Visualization</h3>
<p>Before creating specialized visualizations, we can verify our data by creating a true color image using the red, green, and blue bands-similar to how a standard camera would capture the scene.</p>
<div id="true-color-composite" class="cell" data-execution_count="8">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create true color composite with robust scaling</span></span>
<span id="cb8-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Coarsen by factor of 4 to reduce memory usage</span></span>
<span id="cb8-3">landsat_viz <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> landsat[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'green'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>]].coarsen(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, boundary<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trim'</span>).mean()</span>
<span id="cb8-4">landsat_viz.to_array().plot.imshow(robust<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb8-5">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'True Color Image - Landsat 8'</span>)</span>
<span id="cb8-6">plt.tight_layout()</span></code></pre></div></div>
</details>
</div>
<blockquote class="blockquote">
<p>The <code>robust=True</code> parameter clips extreme outlier values to improve visualization, preventing a few bright pixels from washing out the entire image.</p>
</blockquote>
</section>
<section id="false-color-analysis" class="level3">
<h3 class="anchored" data-anchor-id="false-color-analysis">False Color Analysis</h3>
<p>False color composites use non-visible wavelengths to reveal features invisible to the human eye. By assigning shortwave infrared (SWIR) to red, near-infrared (NIR) to green, and red to blue, we create an image where healthy vegetation appears bright green and burned areas show distinct reddish-brown tones.</p>
<div id="false-color-composite" class="cell" data-execution_count="9">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create false color composite (SWIR/NIR/Red)</span></span>
<span id="cb9-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Coarsen by factor of 4 to reduce memory usage</span></span>
<span id="cb9-3">landsat_false <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> landsat[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'swir22'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'nir08'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>]].coarsen(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, boundary<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trim'</span>).mean()</span>
<span id="cb9-4">landsat_false.to_array().plot.imshow(robust<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb9-5">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'False Color Composite (SWIR/NIR/Red)'</span>)</span>
<span id="cb9-6">plt.tight_layout()</span></code></pre></div></div>
</details>
</div>
<p><strong>Why this band combination?</strong></p>
<blockquote class="blockquote">
<ul>
<li><strong>Healthy vegetation</strong> has high NIR reflectance and low SWIR reflectance → appears bright green</li>
<li><strong>Burned areas</strong> have low NIR reflectance and moderate SWIR reflectance → appears reddish-brown</li>
<li><strong>Urban areas</strong> show moderate reflectance across bands → appears in neutral tones</li>
</ul>
</blockquote>
</section>
<section id="creating-the-final-map" class="level3">
<h3 class="anchored" data-anchor-id="creating-the-final-map">Creating the Final Map</h3>
<p>To provide geographic context, we overlay the fire perimeters onto our false color image. This requires reprojecting the vector data to match the raster’s coordinate system (EPSG:32611 - UTM Zone 11N).</p>
<div id="final-map" class="cell" data-fig-height="9" data-fig-width="9" data-execution_count="10">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reproject fire perimeters to match Landsat CRS</span></span>
<span id="cb10-2">eaton <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eaton.to_crs(landsat.rio.crs)</span>
<span id="cb10-3">palisades <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> palisades.to_crs(landsat.rio.crs)</span>
<span id="cb10-4"></span>
<span id="cb10-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create comprehensive map</span></span>
<span id="cb10-6">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(figsize <span class="op" 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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>))</span>
<span id="cb10-7"></span>
<span id="cb10-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot false color composite (downsampled for memory efficiency)</span></span>
<span id="cb10-9">landsat_map <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> landsat[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'swir22'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'nir08'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>]].coarsen(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, boundary<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trim'</span>).mean()</span>
<span id="cb10-10">landsat_map.to_array().plot.imshow(</span>
<span id="cb10-11">    ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ax,</span>
<span id="cb10-12">    robust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>,</span>
<span id="cb10-13">    add_colorbar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb10-14">)</span>
<span id="cb10-15"></span>
<span id="cb10-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Overlay fire perimeters</span></span>
<span id="cb10-17">eaton.boundary.plot(ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ax, edgecolor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'yellow'</span>, linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Eaton Fire'</span>)</span>
<span id="cb10-18">palisades.boundary.plot(ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ax, edgecolor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Palisades Fire'</span>)</span>
<span id="cb10-19"></span>
<span id="cb10-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add labels and formatting</span></span>
<span id="cb10-21">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Eaton and Palisades Fires - False Color Analysis'</span>,</span>
<span id="cb10-22">          fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, fontweight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'bold'</span>, pad <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb10-23">plt.legend(loc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'upper right'</span>, fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>)</span>
<span id="cb10-24">ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Easting (meters)'</span>, fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>)</span>
<span id="cb10-25">ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Northing (meters)'</span>, fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>)</span>
<span id="cb10-26"></span>
<span id="cb10-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add fire name annotations</span></span>
<span id="cb10-28">ax.text(eaton.geometry.centroid.x.values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>],</span>
<span id="cb10-29">        eaton.geometry.centroid.y.values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2700</span>,</span>
<span id="cb10-30">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Eaton Fire'</span>,</span>
<span id="cb10-31">        color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'black'</span>, fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>, fontweight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'bold'</span>,</span>
<span id="cb10-32">        bbox <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(boxstyle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'round'</span>, facecolor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'white'</span>, alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>))</span>
<span id="cb10-33"></span>
<span id="cb10-34">ax.text(palisades.geometry.centroid.x.values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22000</span>,</span>
<span id="cb10-35">        palisades.geometry.centroid.y.values[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8000</span>,</span>
<span id="cb10-36">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Palisades Fire'</span>,</span>
<span id="cb10-37">        color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'black'</span>, fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>, fontweight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'bold'</span>,</span>
<span id="cb10-38">        bbox <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(boxstyle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'round'</span>, facecolor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'white'</span>, alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>))</span>
<span id="cb10-39"></span>
<span id="cb10-40">plt.tight_layout()</span>
<span id="cb10-41">plt.show()</span></code></pre></div></div>
</details>
</div>
</section>
<section id="figure-interpretation" class="level3">
<h3 class="anchored" data-anchor-id="figure-interpretation">Figure Interpretation</h3>
<p>This false color composite displays the Eaton and Palisades fire areas captured by Landsat 8 on February 23, 2025. The image uses <strong>shortwave infrared (SWIR)</strong>, <strong>near-infrared (NIR)</strong>, and <strong>red</strong> bands to highlight burn scars and vegetation health.</p>
<p><strong>Key features:</strong></p>
<blockquote class="blockquote">
<ul>
<li><strong>Bright green areas</strong> = Healthy vegetation (high NIR reflectance)</li>
<li><strong>Reddish-brown areas</strong> = Burned areas and bare soil (low NIR, moderate SWIR)</li>
<li><strong>Urban/developed areas</strong> = Neutral tones (moderate reflectance across bands)</li>
</ul>
</blockquote>
<p>The fire perimeters are overlaid in <strong>yellow (Eaton Fire)</strong> and <strong>red (Palisades Fire)</strong> to delineate the official burn boundaries. This false color combination is particularly effective for assessing fire severity and monitoring landscape recovery, as the spectral signatures of healthy vs.&nbsp;burned vegetation are distinctly different in the infrared wavelengths.</p>
<hr>
</section>
</section>
<section id="analysis-2-demographic-dimensions" class="level2">
<h2 class="anchored" data-anchor-id="analysis-2-demographic-dimensions">Analysis 2: Demographic Dimensions</h2>
<p>Wildfires don’t impact all communities equally. This analysis examines the racial composition of census tracts affected by the fires to understand environmental justice implications.</p>
<section id="data-preparation" class="level3">
<h3 class="anchored" data-anchor-id="data-preparation">Data Preparation</h3>
<blockquote class="blockquote">
<p>The EJI dataset contains census tract-level data for all of California with 174 columns including demographic, environmental, and health vulnerability indicators. Both the EJI data and fire perimeters use EPSG:3857 (Web Mercator) coordinate reference system.</p>
</blockquote>
</section>
<section id="identifying-affected-communities" class="level3">
<h3 class="anchored" data-anchor-id="identifying-affected-communities">Identifying Affected Communities</h3>
<p>To understand which communities were directly impacted by the fires, we use spatial intersection operations to identify census tracts that overlap with the fire perimeters. We then clip these tracts to the exact fire boundaries, ensuring our analysis focuses only on the areas that experienced direct fire impacts rather than nearby unaffected regions.</p>
</section>
<section id="analyzing-racial-composition" class="level3">
<h3 class="anchored" data-anchor-id="analyzing-racial-composition">Analyzing Racial Composition</h3>
<p>The Environmental Justice Index includes percentile rankings for demographic characteristics. Here we examine <code>EPL_MINRTY</code>, which represents the percentile rank of minority population (persons of color)-a measure of racial and ethnic composition relative to other California communities.</p>
<div id="eji-racial-map" class="cell" data-fig-height="9" data-fig-width="18" data-execution_count="13">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">fig, (ax1, ax2) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<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>, figsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>))</span>
<span id="cb11-2"></span>
<span id="cb11-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define variable to analyze</span></span>
<span id="cb11-4">eji_variable <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'EPL_MINRTY'</span></span>
<span id="cb11-5"></span>
<span id="cb11-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scale values to 0-100 for proper percentile display</span></span>
<span id="cb11-7">eji_clip_palisades_scaled <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eji_clip_palisades.copy()</span>
<span id="cb11-8">eji_clip_eaton_scaled <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eji_clip_eaton.copy()</span>
<span id="cb11-9"></span>
<span id="cb11-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Multiply by 100 to convert to percentile</span></span>
<span id="cb11-11">eji_clip_palisades_scaled[eji_variable] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eji_clip_palisades[eji_variable] <span class="op" 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="cb11-12">eji_clip_eaton_scaled[eji_variable] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eji_clip_eaton[eji_variable] <span class="op" 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="cb11-13"></span>
<span id="cb11-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define common scale for comparison (0-100)</span></span>
<span id="cb11-15">vmin <span class="op" 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>
<span id="cb11-16">vmax <span class="op" 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="cb11-17"></span>
<span id="cb11-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Palisades Fire area</span></span>
<span id="cb11-19">eji_clip_palisades_scaled.plot(</span>
<span id="cb11-20">    column <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eji_variable,</span>
<span id="cb11-21">    vmin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> vmin, vmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> vmax,</span>
<span id="cb11-22">    cmap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'YlOrRd'</span>,</span>
<span id="cb11-23">    legend <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>,</span>
<span id="cb11-24">    ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ax1,</span>
<span id="cb11-25">    edgecolor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'black'</span>,</span>
<span id="cb11-26">    linewidth <span class="op" 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="cb11-27">)</span>
<span id="cb11-28">ax1.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Minority Population</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Palisades Fire Area'</span>, fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb11-29">ax1.axis(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'off'</span>)</span>
<span id="cb11-30"></span>
<span id="cb11-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Eaton Fire area</span></span>
<span id="cb11-32">eji_clip_eaton_scaled.plot(</span>
<span id="cb11-33">    column <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eji_variable,</span>
<span id="cb11-34">    vmin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> vmin, vmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> vmax,</span>
<span id="cb11-35">    cmap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'YlOrRd'</span>,</span>
<span id="cb11-36">    legend <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>,</span>
<span id="cb11-37">    ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ax2,</span>
<span id="cb11-38">    edgecolor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'black'</span>,</span>
<span id="cb11-39">    linewidth <span class="op" 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="cb11-40">)</span>
<span id="cb11-41">ax2.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Minority Population</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Eaton Fire Area'</span>, fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb11-42">ax2.axis(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'off'</span>)</span>
<span id="cb11-43"></span>
<span id="cb11-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Overall title</span></span>
<span id="cb11-45">fig.suptitle(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Percentile Rank of Minority Population in Fire-Affected Areas'</span>, </span>
<span id="cb11-46">fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.98</span>)</span>
<span id="cb11-47"></span>
<span id="cb11-48"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shared colorbar</span></span>
<span id="cb11-49">sm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.cm.ScalarMappable(</span>
<span id="cb11-50">    cmap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'YlOrRd'</span>,</span>
<span id="cb11-51">    norm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.Normalize(vmin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> vmin, vmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> vmax)</span>
<span id="cb11-52">)</span>
<span id="cb11-53">cbar_ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fig.add_axes([<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;">0.12</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.03</span>])</span>
<span id="cb11-54">cbar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fig.colorbar(sm, cax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cbar_ax, orientation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'horizontal'</span>)</span>
<span id="cb11-55">cbar.set_label(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Percentile Rank (0-100)'</span>, fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>)</span>
<span id="cb11-56">cbar.ax.tick_params(labelsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>)</span>
<span id="cb11-57"></span>
<span id="cb11-58">plt.tight_layout()</span>
<span id="cb11-59">plt.show()</span></code></pre></div></div>
</details>
</div>
</section>
</section>
<section id="key-findings" class="level2">
<h2 class="anchored" data-anchor-id="key-findings">Key Findings</h2>
<p>The demographic analysis reveals stark differences in the racial composition of communities affected by the two fires. The maps show percentile rankings of minority population, where higher values indicate greater representation of people of color relative to other California census tracts.</p>
<p><strong>Palisades Fire Area:</strong> Predominantly low percentile rankings (0th-30th percentiles, pale yellow). The fire primarily impacted areas with very low proportions of minority residents-a predominantly white, affluent coastal community.</p>
<p><strong>Eaton Fire Area:</strong> Dramatically high percentile rankings (70th-100th percentiles, dark red). Nearly the entire burn area shows the highest minority population concentrations in the state. The Eaton fire overwhelmingly affected communities of color, particularly in Altadena.</p>
<p><strong>Implications for Recovery:</strong></p>
<p>Research on disaster recovery consistently shows that communities of color face systemic barriers including reduced insurance coverage, limited financial reserves, and difficulties navigating aid bureaucracies. Identifying the demographic composition of affected areas is a critical first step for targeting recovery resources and monitoring for equitable outcomes. Future analysis should examine differential resource allocation, damage assessments, and recovery timelines to assess whether these fires followed historical patterns of inequitable disaster response.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>This analysis shows that proximity doesn’t mean equal impact. Though geographically close, the Eaton and Palisades fires had profoundly different social consequences. The Palisades fire affected wealthy, predominantly white neighborhoods. The Eaton fire devastated communities of color already facing systemic barriers to recovery.</p>
<p>The combination of <code>xarray</code>, <code>rioxarray</code>, and <code>geopandas</code> transforms wildfire analysis from purely physical assessment to social justice documentation. Satellite data shows where fires burned; census data reveals whose communities bore the burden. As climate change intensifies California wildfires, these integrated approaches become essential for equity-centered disaster response and recovery planning that addresses the structural inequalities behind disparate climate impacts.</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-arcgis2025fireperimeters" class="csl-entry">
ArcGIS Hub. 2025. <span>“Palisades and Eaton Dissolved Fire Perimeters.”</span> <a href="https://hub.arcgis.com/maps/ad51845ea5fb4eb483bc2a7c38b2370c" class="uri">https://hub.arcgis.com/maps/ad51845ea5fb4eb483bc2a7c38b2370c</a>.
</div>
<div id="ref-cdc_eji" class="csl-entry">
Centers for Disease Control and Prevention, and Agency for Toxic Substances and Disease Registry. 2024. <span>“Environmental Justice Index.”</span> <a href="https://atsdr.cdc.gov/place-health/php/eji/eji-data-download.html">https://atsdr.cdc.gov/place-health/php/eji/eji-data-download.html</a>.
</div>
<div id="ref-microsoft2025landsat" class="csl-entry">
Microsoft Planetary Computer. 2025. <span>“Landsat Collection 2 Level-2.”</span> <a href="https://planetarycomputer.microsoft.com/dataset/landsat-c2-l2" class="uri">https://planetarycomputer.microsoft.com/dataset/landsat-c2-l2</a>.
</div>
</div>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{miller2025,
  author = {Miller, Emily},
  title = {Burn {Scars} and {Unequal} {Burdens}},
  date = {2025-11-29},
  url = {https://rellimylime.github.io/posts/eds220-final/},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-miller2025" class="csl-entry quarto-appendix-citeas">
Miller, Emily. 2025. <span>“Burn Scars and Unequal Burdens.”</span>
November 29, 2025. <a href="https://rellimylime.github.io/posts/eds220-final/">https://rellimylime.github.io/posts/eds220-final/</a>.
</div></div></section></div> ]]></description>
  <category>MEDS</category>
  <category>Python</category>
  <category>Geospatial Analysis</category>
  <category>Remote Sensing</category>
  <category>Environmental Justice</category>
  <guid>https://rellimylime.github.io/posts/eds220-final/</guid>
  <pubDate>Sat, 29 Nov 2025 08:00:00 GMT</pubDate>
  <media:content url="https://rellimylime.github.io/images/posts/eds220-final/LA-Wildfires-crop.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Crossing Signals</title>
  <dc:creator>Emily Miller</dc:creator>
  <link>https://rellimylime.github.io/posts/eds222-final/</link>
  <description><![CDATA[ 





<section id="introduction-why-this-matters" class="level1">
<h1>1. Introduction: Why This Matters</h1>
<p>Wildlife-vehicle collisions threaten biodiversity and pose safety risks. Understanding which road and landscape characteristics predict roadkill hotspots enables evidence-based mitigation. We analyze ~11,000 roadkill events on 241,000 Danish road segments (2017-2019) to test whether traffic volume, forest proximity, and urban development predict collision rates.</p>
<p><strong>The puzzle</strong>: Some roads experience frequent wildlife collisions while others have zero incidents. Is it just random chance or collection bias? Or are there systematic patterns we can identify and address?</p>
<p>If we can predict which roads are most likely to become collision hotspots, transportation planners and conservationists can install wildlife crossings, implement warning systems, or adjust speed limits in high-risk areas.</p>
<section id="research-question" class="level3">
<h3 class="anchored" data-anchor-id="research-question">Research Question</h3>
<p><strong>How do traffic volume and land use characteristics affect wildlife-vehicle collisions on Danish roads?</strong></p>
<p>Specifically, we investigate two processes:</p>
<ol type="1">
<li><strong>Occurrence</strong>: What makes roadkill more likely to happen at all on a given road segment?</li>
<li><strong>Intensity</strong>: Once roadkill does occur, what factors influence how many collision events happen?</li>
</ol>
<p>The high proportion of road segments with zero observed roadkill (~83%) suggests these processes operate differently, making a hurdle model the appropriate statistical framework.</p>
<hr>
</section>
</section>
<section id="building-a-theory-what-causes-roadkill" class="level1">
<h1>2. Building a Theory: What Causes Roadkill?</h1>
<section id="the-causal-story" class="level3">
<h3 class="anchored" data-anchor-id="the-causal-story">The Causal Story</h3>
<p>Imagine you’re a deer deciding whether to cross a road. Several factors affect your collision risk:</p>
<ol type="1">
<li><strong>Traffic volume</strong>: More vehicles = more chances for collision</li>
<li><strong>Your habitat</strong>: You’re more likely to be near roads that pass through forests or parks</li>
<li><strong>Road characteristics</strong>: Wider, faster roads are harder to cross safely</li>
<li><strong>Human development</strong>: You avoid heavily urbanized areas</li>
</ol>
</section>
<section id="visualizing-causal-relationships-the-dag" class="level3">
<h3 class="anchored" data-anchor-id="visualizing-causal-relationships-the-dag">Visualizing Causal Relationships: The DAG</h3>
<p>Now, let’s formalize our deer’s decision-making process with a Directed Acyclic Graph (DAG).</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/dag-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<p><strong>Key relationships:</strong></p>
<ul>
<li><strong>Traffic (AADT)</strong> directly affects roadkill probability (more vehicles → more collision opportunities)</li>
<li><strong>Land use</strong> influences wildlife presence near roads (forests/parks attract wildlife) as well as road types</li>
<li><strong>Road type</strong> is a common cause affecting traffic volume and speed limits</li>
<li><strong>Speed limit</strong> affects collision severity and driver reaction time</li>
<li>We control for <strong>road type</strong> to avoid confounding when estimating traffic effects</li>
</ul>
<p><strong>NOTE</strong>: Without considering wildlife density and surrounding habitats, this model estimates relationships conditional on unobserved wildlife presence, which is approximated using nearby land-use rather than direct population data.</p>
<hr>
</section>
</section>
<section id="hypotheses" class="level1">
<h1>3. Hypotheses</h1>
<p>Based on our causal theory, we make three sets of predictions:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="header">
<th>Hypothesis</th>
<th>Variable</th>
<th>Expected Effect</th>
<th>Rationale</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>H1: Traffic</strong></td>
<td>AADT (↑)</td>
<td>+ occurrence, + intensity</td>
<td>More vehicles → more collision opportunities</td>
</tr>
<tr class="even">
<td><strong>H2a: Wildlife habitat</strong></td>
<td>Forest/Park (↑)</td>
<td>+ occurrence, + intensity</td>
<td>Attracts wildlife near roads</td>
</tr>
<tr class="odd">
<td><strong>H2b: Urban development</strong></td>
<td>Residential (↑)</td>
<td>- occurrence, - intensity</td>
<td>Reduces wildlife presence</td>
</tr>
<tr class="even">
<td><strong>H3a: Road infrastructure</strong></td>
<td>Minor roads (vs Major baseline)</td>
<td>- occurrence, - intensity</td>
<td>Lower speeds, fewer lanes than major roads</td>
</tr>
<tr class="odd">
<td><strong>H3b: Speed</strong></td>
<td>Speed limit (↑)</td>
<td>+ occurrence, + intensity</td>
<td>Reduced reaction time, higher mortality</td>
</tr>
</tbody>
</table>
<p><strong>Note:</strong> Count component effects control for road segment length by including log(length) as a predictor.</p>
<hr>
</section>
<section id="data-methods" class="level1">
<h1>4. Data &amp; Methods</h1>
<section id="data-sources" class="level3">
<h3 class="anchored" data-anchor-id="data-sources">Data Sources</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="header">
<th>Dataset</th>
<th>Source</th>
<th>Variables</th>
<th>Coverage</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Roadkill observations</td>
<td>Global Roadkill Database <span class="citation" data-cites="grilo2025">(Grilo et al. 2025)</span></td>
<td>GPS coordinates, species</td>
<td>2017-2019</td>
</tr>
<tr class="even">
<td>Road network</td>
<td>OpenStreetMap <span class="citation" data-cites="osm2024">(OpenStreetMap contributors 2024)</span></td>
<td>Geometry, type, speed limits</td>
<td>Denmark</td>
</tr>
<tr class="odd">
<td>Traffic counts</td>
<td>Danish Road Directorate <span class="citation" data-cites="vejdirektoratet2019">(Danish Road Directorate 2017--2019)</span></td>
<td>AADT (vehicles/day)</td>
<td>Monitoring stations</td>
</tr>
<tr class="even">
<td>Land use</td>
<td>OpenStreetMap <span class="citation" data-cites="osm2024">(OpenStreetMap contributors 2024)</span></td>
<td>Forest, farmland, residential, parks</td>
<td>500m buffers</td>
</tr>
</tbody>
</table>
<p><strong>Technical Setup</strong>: <code>tidyverse</code>, <code>sf</code>, <code>terra</code>, <code>here</code>, <code>pscl</code>, <code>yaml</code>, <code>patchwork</code>, <code>dagitty</code>, <code>ggdag</code></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(terra)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(here)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(pscl)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(yaml)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load configuration</span></span>
<span id="cb1-10">config <span class="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_yaml</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"posts/eds222-final/config.yml"</span>))</span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>seed)</span>
<span id="cb1-13"></span>
<span id="cb1-14">CRS_M <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>crs <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># EPSG:25832, Denmark ETRS89 / UTM zone 32N</span></span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Denmark bounding box (WGS84)</span></span>
<span id="cb1-17">dk_bbox_wgs84 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_bbox</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb1-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmin =</span> config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bbox_xmin,</span>
<span id="cb1-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bbox_ymin,</span>
<span id="cb1-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</span> config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bbox_xmax,</span>
<span id="cb1-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bbox_ymax</span>
<span id="cb1-22">), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bbox_crs)</span>
<span id="cb1-23"></span>
<span id="cb1-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Transform to projected CRS</span></span>
<span id="cb1-25">dk_bbox_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;">st_transform</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_as_sfc</span>(dk_bbox_wgs84), CRS_M) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_bbox</span>()</span></code></pre></div></div>
</details>
</div>
</section>
<section id="data-processing-pipeline" class="level3">
<h3 class="anchored" data-anchor-id="data-processing-pipeline">Data Processing Pipeline</h3>
<section id="load-roadkill-observations" class="level4">
<h4 class="anchored" data-anchor-id="load-roadkill-observations">Load Roadkill Observations</h4>
<p>Roadkill observations are GPS point locations filtered to Denmark and the study period (2017-2019), then transformed to a projected coordinate system (EPSG:25832) for analysis.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load roadkill CSV</span></span>
<span id="cb2-2">road_kill_dk <span class="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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>roadkill_csv_path)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(</span>
<span id="cb2-4">        country <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;">"Denmark"</span>,</span>
<span id="cb2-5">        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;">2017</span>, 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;">2019</span></span>
<span id="cb2-6">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(decimalLongitude, decimalLatitude) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_as_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">coords =</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;">"decimalLongitude"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"decimalLatitude"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4326</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(CRS_M) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_crop</span>(dk_bbox_proj)</span></code></pre></div></div>
</details>
</div>
<blockquote class="blockquote">
<ul>
<li>Roadkill observations: 16,237</li>
<li>Time period: 2017-2019</li>
<li>Coordinate system: ETRS89 / UTM zone 32N</li>
</ul>
</blockquote>
</section>
<section id="load-road-network" class="level4">
<h4 class="anchored" data-anchor-id="load-road-network">Load Road Network</h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define cache path for roads data</span></span>
<span id="cb3-2">roads_cache <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>roads_cached_path)</span>
<span id="cb3-3"></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;">file.exists</span>(roads_cache)) {</span>
<span id="cb3-5">  <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;">"Loading roads from cache...</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="cb3-6">  roads_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;">readRDS</span>(roads_cache)</span>
<span id="cb3-7">} <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb3-8">  <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;">"Cache not found. Loading and processing roads (one-time setup)...</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="cb3-9"></span>
<span id="cb3-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load shapefile</span></span>
<span id="cb3-11">  roads_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;">st_read</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>roads_shp_path), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quiet =</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="cb3-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(CRS_M) <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;">st_crop</span>(dk_bbox_proj)</span>
<span id="cb3-14"></span>
<span id="cb3-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save to cache</span></span>
<span id="cb3-16">  <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;">"Saving roads to cache for future runs...</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="cb3-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</span>(roads_raw, roads_cache)</span>
<span id="cb3-18">  <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;">"Cache saved:"</span>, roads_cache, <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="cb3-19">}</span></code></pre></div></div>
</details>
</div>
<blockquote class="blockquote">
<p>Road segments loaded: 1,327,815</p>
</blockquote>
</section>
<section id="load-traffic-data" class="level4">
<h4 class="anchored" data-anchor-id="load-traffic-data">Load Traffic Data</h4>
<p>Traffic counts represent Annual Average Daily Traffic (AADT) from monitoring stations, averaged across 2017-2019.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define cache path for traffic data</span></span>
<span id="cb4-2">traffic_cache <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>traffic_cached_path)</span>
<span id="cb4-3"></span>
<span id="cb4-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;">file.exists</span>(traffic_cache)) {</span>
<span id="cb4-5">  <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;">"Loading traffic data from cache...</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="cb4-6">  traffic_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;">readRDS</span>(traffic_cache)</span>
<span id="cb4-7">} <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb4-8">  <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;">"Cache not found. Loading and processing traffic (one-time setup)...</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="cb4-9"></span>
<span id="cb4-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load shapefile</span></span>
<span id="cb4-11">  traffic_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;">st_read</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>traffic_shp_path), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quiet =</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="cb4-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(CRS_M) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_crop</span>(dk_bbox_proj) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(AAR <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;">2017</span>, AAR <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;">2019</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Average AADT across years for each station</span></span>
<span id="cb4-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(geometry) <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;">summarise</span>(</span>
<span id="cb4-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AADT =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(AADT, <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-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AAR =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2017-2019"</span>,</span>
<span id="cb4-20">    <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-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;">st_as_sf</span>()</span>
<span id="cb4-23"></span>
<span id="cb4-24">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save to cache</span></span>
<span id="cb4-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;">"Saving traffic to cache for future runs...</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="cb4-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</span>(traffic_raw, traffic_cache)</span>
<span id="cb4-27">  <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;">"Cache saved:"</span>, traffic_cache, <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="cb4-28">}</span></code></pre></div></div>
</details>
</div>
<blockquote class="blockquote">
<p>Traffic monitoring stations: 3,000</p>
</blockquote>
</section>
<section id="load-land-use-data" class="level4">
<h4 class="anchored" data-anchor-id="load-land-use-data">Load Land Use Data</h4>
<p>Land use polygons classify areas as forest, farmland, residential, parks, etc.</p>
<blockquote class="blockquote">
<p>Land use polygons: 587,311</p>
</blockquote>
<p>Top land use classes:</p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Top 10 Land Use Classes</caption>
<thead>
<tr class="header">
<th style="text-align: left;">fclass</th>
<th style="text-align: right;">n</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">farmland</td>
<td style="text-align: right;">149854</td>
</tr>
<tr class="even">
<td style="text-align: left;">forest</td>
<td style="text-align: right;">141814</td>
</tr>
<tr class="odd">
<td style="text-align: left;">farmyard</td>
<td style="text-align: right;">84400</td>
</tr>
<tr class="even">
<td style="text-align: left;">meadow</td>
<td style="text-align: right;">72662</td>
</tr>
<tr class="odd">
<td style="text-align: left;">scrub</td>
<td style="text-align: right;">55374</td>
</tr>
<tr class="even">
<td style="text-align: left;">grass</td>
<td style="text-align: right;">40029</td>
</tr>
<tr class="odd">
<td style="text-align: left;">residential</td>
<td style="text-align: right;">20427</td>
</tr>
<tr class="even">
<td style="text-align: left;">heath</td>
<td style="text-align: right;">5948</td>
</tr>
<tr class="odd">
<td style="text-align: left;">park</td>
<td style="text-align: right;">5540</td>
</tr>
<tr class="even">
<td style="text-align: left;">industrial</td>
<td style="text-align: right;">3310</td>
</tr>
</tbody>
</table>
</div>
</div>
<p><strong>Computational Note</strong>: These shapefiles are large (millions of features); we cache the processed/projected versions to avoid reloading and processing during every run. First runs take ~5-10 minutes each; cached runs take seconds.</p>
<hr>
</section>
<section id="filter-to-motorized-roads" class="level4">
<h4 class="anchored" data-anchor-id="filter-to-motorized-roads">Filter to Motorized Roads</h4>
<p>We restrict analysis to roads accessible by motor vehicles, excluding pedestrian paths and cycleways and other places where wildlife-vehicle collisions cannot occur.</p>
<blockquote class="blockquote">
<ul>
<li>Motorized road segments: 322,776</li>
<li>Total road length: 97,822 km</li>
<li>Mean segment length: 0.3 km</li>
</ul>
</blockquote>
</section>
<section id="match-traffic-to-road-segments" class="level4">
<h4 class="anchored" data-anchor-id="match-traffic-to-road-segments">Match Traffic to Road Segments</h4>
<p>Traffic monitoring stations are point locations. Since there are far more roads than monitoring stations, I assigned each road segment with the AADT from its nearest station, applying a distance threshold. This means we only assign traffic data to roads within ~3.7 km of a monitoring station, avoiding matches to stations that are unreasonably far away. This balances <strong>coverage</strong> (75% of roads get traffic data) with <strong>accuracy</strong> (avoiding spurious long-distance matches).</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Apply configured threshold</span></span>
<span id="cb5-2">threshold_percentile <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>distance_threshold_percentile</span>
<span id="cb5-3">dist_threshold <span class="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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(distances), threshold_percentile)</span>
<span id="cb5-4"></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Merge traffic data with roads</span></span>
<span id="cb5-6">roads_traf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> car_roads <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb5-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nn_dist_m =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(distances),</span>
<span id="cb5-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AADT =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(nn_dist_m <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> dist_threshold,</span>
<span id="cb5-10">                   traffic_raw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>AADT[nearest_idx],</span>
<span id="cb5-11">                   <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>)</span>
<span id="cb5-12">  )</span></code></pre></div></div>
</details>
</div>
<blockquote class="blockquote">
<ul>
<li>Distance threshold: 3,699 m (75th percentile)</li>
<li>Roads with traffic data: 242,082 (75%)</li>
<li>AADT range: 0 - 59,632 vehicles/day</li>
</ul>
</blockquote>
<p><strong>Note</strong>: Analysis is limited to Denmark’s <em>monitored</em> road network (major highways and urban roads). Remote rural roads are likely not included.</p>
<hr>
</section>
<section id="extract-land-use-around-roads" class="level4">
<h4 class="anchored" data-anchor-id="extract-land-use-around-roads">Extract Land Use Around Roads</h4>
<p>We calculate the percentage of forest, farmland, residential, and park land use within 500m buffers around each road segment. This distance approximates wildlife movement ranges and habitat edge effects.</p>
<p>Mean land use composition within 500m:</p>
<blockquote class="blockquote">
<ul>
<li>Forest: 11.2%</li>
<li>Farmland: 31.9%</li>
<li>Residential: 44.5%</li>
<li>Parks: 4.6%</li>
</ul>
</blockquote>
</section>
<section id="aggregate-roadkill-by-road-segment" class="level4">
<h4 class="anchored" data-anchor-id="aggregate-roadkill-by-road-segment">Aggregate Roadkill by Road Segment</h4>
<p>Each roadkill point is matched to its nearest road segment using spatial join.</p>
<blockquote class="blockquote">
<ul>
<li>Road segments with roadkill: 10,095</li>
</ul>
</blockquote>
</section>
<section id="create-final-analysis-dataset" class="level4">
<h4 class="anchored" data-anchor-id="create-final-analysis-dataset">Create Final Analysis Dataset</h4>
<p>We merge all data sources and apply transformations required for modeling: log-transform traffic and road length, classify road types, and assign missing speed limits.</p>
<p><strong>Dataset summary:</strong></p>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Final Dataset Characteristics</caption>
<thead>
<tr class="header">
<th style="text-align: left;">Metric</th>
<th style="text-align: left;">Value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Road segments</td>
<td style="text-align: left;">241,765</td>
</tr>
<tr class="even">
<td style="text-align: left;">Segments with roadkill</td>
<td style="text-align: left;">6,850 (2.8%)</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Zero-inflation rate</td>
<td style="text-align: left;">97.2%</td>
</tr>
<tr class="even">
<td style="text-align: left;">Total roadkill events</td>
<td style="text-align: left;">10,834</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Mean roadkill per segment</td>
<td style="text-align: left;">0.045</td>
</tr>
<tr class="even">
<td style="text-align: left;">Overdispersion (Var/Mean)</td>
<td style="text-align: left;">2.71</td>
</tr>
</tbody>
</table>
</div>
</div>
<p><strong>Key observations</strong>: High zero-inflation (97.2%) and overdispersion (Var/Mean = 2.71) justify using a hurdle model with negative binomial distribution.</p>
<hr>
</section>
</section>
</section>
<section id="exploratory-data-analysis" class="level1">
<h1>5. Exploratory Data Analysis</h1>
<p>Before modeling, we visualize how our predictors distribute and relate to roadkill occurrence:</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Distribution plots</span></span>
<span id="cb6-2">p_aadt_dist <span class="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>(model_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> AADT)) <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;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> primary_color, <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;">alpha =</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;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</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;">scale_x_log10</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>comma) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Distribution of Traffic Volume"</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;">"AADT (vehicles/day, 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;">"Count of Road Segments"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>()</span>
<span id="cb6-7"></span>
<span id="cb6-8">p_length_dist <span class="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>(model_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> len_km)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-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;">fill =</span> primary_color, <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;">alpha =</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;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-10">  <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="cb6-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Distribution of Road Segment Length"</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;">"Length (km, 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;">"Count of Road Segments"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>()</span>
<span id="cb6-13"></span>
<span id="cb6-14">p_speed_dist <span class="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>(model_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> speed_limit)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-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;">fill =</span> primary_color, <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;">alpha =</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;">binwidth =</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="cb6-16">  <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;">"Distribution of Speed Limits"</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;">"Speed Limit (km/h)"</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;">"Count of Road Segments"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>()</span>
<span id="cb6-18"></span>
<span id="cb6-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Land use distributions</span></span>
<span id="cb6-20">landuse_long <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> model_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-21">  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>(pct_forest, pct_farmland, pct_residential, pct_park) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-22">  <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;">everything</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;">"landuse_type"</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;">"percentage"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-23">  <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;">landuse_type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_replace</span>(landuse_type, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pct_"</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;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_to_title</span>())</span>
<span id="cb6-24"></span>
<span id="cb6-25">p_landuse_dist <span class="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>(landuse_long, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> percentage, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> landuse_type)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-26">  <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;">alpha =</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;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-27">  <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>landuse_type, <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-28">  <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;">"Forest"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> backup_color, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Farmland"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> accent_color, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Residential"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> primary_color, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Park"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> neutral_color)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-29">  <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;">"Distribution of Land Use Types (within 500m buffers)"</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;">"Percentage of Buffer Area"</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;">"Count of Road Segments"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-31">  <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="cb6-32"></span>
<span id="cb6-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Road type distribution</span></span>
<span id="cb6-34">p_roadtype <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> model_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(road_type) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-36">  <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>(<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;">reorder</span>(road_type, n), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> road_type)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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="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="cb6-38">  <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;">"Major"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> accent_color, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Minor"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> primary_color, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> neutral_color)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-40">  <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;">"Distribution of Road Types"</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;">"Road Type"</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;">"Count of Road Segments"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>()</span>
<span id="cb6-42"></span>
<span id="cb6-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Combine</span></span>
<span id="cb6-44">(p_aadt_dist <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_length_dist) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (p_speed_dist <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_roadtype) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> p_landuse_dist</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/predictor-distributions-1.png" class="img-fluid figure-img" width="1344"></p>
</figure>
</div>
</div>
</div>
<p><strong>Key patterns at a glance:</strong> - Traffic and forest coverage show strong positive relationships with roadkill - Residential development shows negative relationship (fewer wildlife) - Major roads have much higher collision rates than minor roads - Speed limits cluster into two groups (urban vs highway)</p>
<section id="detailed-exploration-by-predictor" class="level3">
<h3 class="anchored" data-anchor-id="detailed-exploration-by-predictor">Detailed Exploration by Predictor</h3>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">Traffic Volume</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Forest Coverage</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">Residential Area</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Road Type</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-5-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-5" aria-controls="tabset-1-5" aria-selected="false" href="">Speed Limit</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="columns">
<div class="column" style="width:48%;">
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(model_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> AADT)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-2">  <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;">fill =</span> primary_color, <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;">alpha =</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;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</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>comma) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-4">  <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;">"Distribution"</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;">"AADT (vehicles/day, 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;">"Count"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>()</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/eda-traffic-dist-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>What we see:</strong> Most road segments experience moderate-to-high traffic (100-10,000 vehicles/day). Few segments have very low (&lt;10) or very high (&gt;10,000) traffic volumes.</p>
</div><div class="column" style="width:4%;">

</div><div class="column" style="width:48%;">
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(model_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> AADT, <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.numeric</span>(roadkill_count <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>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_summary_bin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> mean, <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;">"point"</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;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> primary_color, <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="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_summary_bin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> mean, <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;">"line"</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;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> primary_color, <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="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</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>comma) <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;">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>percent, <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="cb8-6">  <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;">"Relationship to Roadkill"</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;">"AADT (vehicles/day, 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;">"% with Roadkill"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>()</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/eda-traffic-response-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>What we see:</strong> Higher traffic corresponds to slightly higher roadkill rates. Pattern is clearest in the 100-10,000 range where data are concentrated; noisier at extremes.</p>
</div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="columns">
<div class="column" style="width:48%;">
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(model_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> pct_forest)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-2">  <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;">fill =</span> backup_color, <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;">alpha =</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;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-3">  <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;">"Distribution"</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;">"% Forest within 500m"</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;">"Count"</span>) <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;">theme_cohesive</span>()</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/eda-forest-dist-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>What we see:</strong> Most road segments have 0-20% forest nearby, very few have &gt;50%.</p>
</div><div class="column" style="width:4%;">

</div><div class="column" style="width:48%;">
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(model_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> pct_forest, <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.numeric</span>(roadkill_count <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>
<span id="cb10-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_summary_bin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> mean, <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;">"point"</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;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> backup_color, <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="cb10-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_summary_bin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> mean, <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;">"line"</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;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> backup_color, <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="cb10-4">  <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>percent, <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="cb10-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Relationship to Roadkill"</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;">"% Forest within 500m"</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;">"% with Roadkill"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>()</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/eda-forest-response-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>What we see:</strong> <strong>Strongest predictor!</strong> Roadkill probability jumps from ~2% to ~20% as forest increases. Supports <strong>H2a</strong>.</p>
</div>
</div>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<div class="columns">
<div class="column" style="width:48%;">
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(model_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> pct_residential)) <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_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> primary_color, <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;">alpha =</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;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</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;">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;">"Distribution"</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;">"% Residential within 500m"</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;">"Count"</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;">theme_cohesive</span>()</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/eda-residential-dist-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>What we see:</strong> Most roads have low residential coverage, but substantial numbers exist across the full range.</p>
</div><div class="column" style="width:4%;">

</div><div class="column" style="width:48%;">
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(model_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> pct_residential, <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.numeric</span>(roadkill_count <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>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_summary_bin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> mean, <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;">"point"</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;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> primary_color, <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="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_summary_bin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> mean, <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;">"line"</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;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> primary_color, <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="cb12-4">  <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>percent, <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="cb12-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Relationship to Roadkill"</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;">"% Residential within 500m"</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;">"% with Roadkill"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>()</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/eda-residential-response-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>What we see:</strong> Strong negative relationship - roadkill probability drops from ~10% to &lt;1% as residential development increases. Supports <strong>H2b</strong>.</p>
</div>
</div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<div class="columns">
<div class="column" style="width:48%;">
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">model_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(road_type) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb13-3">  <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>(<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;">reorder</span>(road_type, n), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> road_type)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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="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="cb13-5">  <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;">"Major"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> accent_color, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Minor"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> primary_color, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> neutral_color)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-6">  <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>comma) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-8">  <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;">"Distribution"</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;">"Road Type"</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;">"Count"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>()</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/eda-roadtype-dist-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>What we see:</strong> Network dominated by minor roads (~180k), with major roads (~60k) less common.</p>
</div><div class="column" style="width:4%;">

</div><div class="column" style="width:48%;">
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">model_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(road_type) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-3">  <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;">roadkill_prob =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(roadkill_count <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;">.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-4">  <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>(<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;">reorder</span>(road_type, roadkill_prob), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> roadkill_prob, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> road_type)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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="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="cb14-6">  <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;">"Major"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> accent_color, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Minor"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> primary_color, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> neutral_color)) <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;">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>percent) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Relationship to Roadkill"</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;">"Road Type"</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;">"% with Roadkill"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>()</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/eda-roadtype-response-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>What we see:</strong> Major roads have 7x higher roadkill rates (~7%) than Minor roads (~1%). Supports <strong>H3a</strong>.</p>
</div>
</div>
</div>
<div id="tabset-1-5" class="tab-pane" aria-labelledby="tabset-1-5-tab">
<div class="columns">
<div class="column" style="width:48%;">
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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>(model_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> speed_limit)) <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_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> accent_color, <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;">alpha =</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;">binwidth =</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="cb15-3">  <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;">"Distribution"</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;">"Speed Limit (km/h)"</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;">"Count"</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;">theme_cohesive</span>()</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/eda-speed-dist-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>What we see:</strong> Three clusters - low-speed zones (~0 km/h, likely missing data), moderate speeds (~50 km/h), and highways (~80 km/h).</p>
</div><div class="column" style="width:4%;">

</div><div class="column" style="width:48%;">
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">model_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-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;">speed_bin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cut</span>(speed_limit, <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;">0</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;">70</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">110</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">140</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(speed_bin) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-4">  <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;">roadkill_prob =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(roadkill_count <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;">n =</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="cb16-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(n <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-6">  <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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> speed_bin, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> roadkill_prob)) <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;">geom_col</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> accent_color, <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="cb16-8">  <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>percent) <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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Relationship to Roadkill"</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;">"Speed Limit (km/h)"</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;">"% with Roadkill"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-11">  <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;">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></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/eda-speed-response-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>What we see:</strong> Roadkill rates peak at moderate speeds (70-90 km/h, ~15%), then decline at higher speeds. Non-linear pattern suggests <strong>H3b</strong> needs refinement.</p>
</div>
</div>
</div>
</div>
</div>
<p>These exploratory patterns support some hypotheses (forest, road type) but challenge others (traffic, speed). The hurdle model will quantify these effects while controlling for confounding.</p>
<hr>
</section>
</section>
<section id="statistical-model-the-hurdle-approach" class="level1">
<h1>6. Statistical Model: The Hurdle Approach</h1>
<section id="why-a-hurdle-model" class="level3">
<h3 class="anchored" data-anchor-id="why-a-hurdle-model">Why a Hurdle Model?</h3>
<p>Traditional count models (Poisson, negative binomial) underestimate zeros. Our data exhibits:</p>
<ul>
<li><strong>Excess zeros</strong>: 97.2% of segments have no roadkill</li>
<li><strong>Overdispersion</strong>: Variance (0.12) &gt;&gt; Mean (0.045)</li>
</ul>
<p>A <strong>hurdle model</strong> separates two processes:</p>
<ol type="1">
<li><strong>Binary hurdle</strong> (logistic): Does roadkill occur at all?</li>
<li><strong>Count component</strong> (negative binomial): How many events, given occurrence?</li>
</ol>
</section>
<section id="model-specification" class="level3">
<h3 class="anchored" data-anchor-id="model-specification">Model Specification</h3>
<p><strong>Part 1: Zero hurdle (Binary component)</strong></p>
<p>Models whether any roadkill occurs on a road segment:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AZ_i%20&amp;%5Csim%20%5Ctext%7BBinomial%7D(p_i)%20%5C%5C%0A%5Ctext%7Blogit%7D(p_i)%20&amp;=%20%5Calpha_0%20+%20%5Calpha_1%20%5Clog(%5Ctext%7BAADT%7D_i)%20+%20%5Calpha_2%20%5Ctext%7BMinor%7D_i%20+%20%5Calpha_4%20%5Ctext%7BSpeed%7D_i%20%5C%5C%0A&amp;%5Cquad%20+%20%5Calpha_5%20%5Ctext%7BForest%7D_i%20+%20%5Calpha_6%20%5Ctext%7BFarm%7D_i%20+%20%5Calpha_7%20%5Ctext%7BResidential%7D_i%20+%20%5Calpha_8%20%5Ctext%7BPark%7D_i%0A%5Cend%7Baligned%7D%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?Z_i%20=%201"> if roadkill occurs, <img src="https://latex.codecogs.com/png.latex?p_i"> is the probability of observing any roadkill, and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BMinor%7D_i"> is an indicator for road type (reference: Major roads).</p>
<p><strong>Part 2: Count component (Positive counts only)</strong></p>
<p>Models the number of roadkill events, conditional on at least one event occurring:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AY_i%20%7C%20Z_i%20=%201%20&amp;%5Csim%20%5Ctext%7BNegative%20Binomial%7D(%5Cmu_i,%20%5Ctheta)%20%5C%5C%0A%5Clog(%5Cmu_i)%20&amp;=%20%5Cbeta_0%20+%20%5Cbeta_1%20%5Clog(%5Ctext%7BAADT%7D_i)%20+%20%5Cbeta_2%20%5Ctext%7BMinor%7D_i%20+%20%5Cbeta_4%20%5Ctext%7BSpeed%7D_i%20%5C%5C%0A&amp;%5Cquad%20+%20%5Cbeta_5%20%5Ctext%7BForest%7D_i%20+%20%5Cbeta_6%20%5Ctext%7BFarm%7D_i%20+%20%5Cbeta_7%20%5Ctext%7BResidential%7D_i%20+%20%5Cbeta_8%20%5Ctext%7BPark%7D_i%20+%20%5Cbeta_9%20%5Clog(%5Ctext%7BLength%7D_i)%0A%5Cend%7Baligned%7D%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> is the dispersion parameter.</p>
</section>
<section id="model-validation-simulation-test" class="level3">
<h3 class="anchored" data-anchor-id="model-validation-simulation-test">Model Validation: Simulation Test</h3>
<p>Before applying to real data, we verify the model recovers known parameters from simulated data.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb17-2">n_sim <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;">10000</span></span>
<span id="cb17-3">x_sim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n_sim, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</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;">sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb17-4"></span>
<span id="cb17-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># True parameters for hurdle model</span></span>
<span id="cb17-6">beta0_zero <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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Zero component intercept</span></span>
<span id="cb17-7">beta1_zero <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.3</span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Zero component slope</span></span>
<span id="cb17-8">beta0_count <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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Count component intercept</span></span>
<span id="cb17-9">beta1_count <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.2</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Count component slope</span></span>
<span id="cb17-10">theta <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.5</span>         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Dispersion parameter</span></span>
<span id="cb17-11"></span>
<span id="cb17-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate binary outcomes (does roadkill occur?)</span></span>
<span id="cb17-13">logit_prob_nonzero <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> beta0_zero <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> beta1_zero <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x_sim</span>
<span id="cb17-14">prob_nonzero <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plogis</span>(logit_prob_nonzero)</span>
<span id="cb17-15">z_sim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbinom</span>(n_sim, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> prob_nonzero)</span>
<span id="cb17-16"></span>
<span id="cb17-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate positive counts for non-zero cases (vectorized!)</span></span>
<span id="cb17-18">mu_count <span class="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>(beta0_count <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> beta1_count <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x_sim)</span>
<span id="cb17-19">y_counts_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;">rnbinom</span>(n_sim, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> mu_count, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> theta)</span>
<span id="cb17-20">y_counts_truncated <span class="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>(y_counts_raw, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Truncate at 1</span></span>
<span id="cb17-21"></span>
<span id="cb17-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Apply hurdle - observations that didn't pass hurdle get 0</span></span>
<span id="cb17-23">y_sim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> z_sim <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y_counts_truncated</span>
<span id="cb17-24"></span>
<span id="cb17-25">sim_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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x_sim, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y_sim)</span></code></pre></div></div>
</details>
</div>
<p>Simulated data characteristics:</p>
<blockquote class="blockquote">
<ul>
<li>Zero rate: 9%</li>
<li>Mean count (all): 12.496</li>
<li>Mean count (non-zero only): 13.727</li>
</ul>
</blockquote>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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;"># Fit hurdle model to simulated data</span></span>
<span id="cb18-2">sim_hurdle <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hurdle</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sim_data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"negbin"</span>)</span>
<span id="cb18-3"></span>
<span id="cb18-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract estimated parameters</span></span>
<span id="cb18-5">sim_coefs <span class="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>(sim_hurdle)</span></code></pre></div></div>
</details>
</div>
<section id="parameter-recovery-check" class="level4">
<h4 class="anchored" data-anchor-id="parameter-recovery-check">Parameter recovery check</h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">zero_intercept <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sim_coefs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zero_(Intercept)"</span>]</span>
<span id="cb19-2">zero_slope <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sim_coefs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zero_x"</span>]</span>
<span id="cb19-3">count_intercept <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sim_coefs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count_(Intercept)"</span>]</span>
<span id="cb19-4">count_slope <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sim_coefs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count_x"</span>]</span>
<span id="cb19-5"></span>
<span id="cb19-6">recovery_table <span class="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="cb19-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Component =</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;">"Zero"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Zero"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Count"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Count"</span>),</span>
<span id="cb19-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Parameter =</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;">"Intercept"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Slope"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Intercept"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Slope"</span>),</span>
<span id="cb19-9">  <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;">True 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;">c</span>(beta0_zero, beta1_zero, beta0_count, beta1_count),</span>
<span id="cb19-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Estimated =</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;">c</span>(zero_intercept, zero_slope, count_intercept, count_slope), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb19-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Difference =</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;">c</span>(zero_intercept, zero_slope, count_intercept, count_slope) <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;">c</span>(beta0_zero, beta1_zero, beta0_count, beta1_count), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb19-13">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Recovered =</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;">abs</span>(Difference) <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.2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✓"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✗"</span>))</span>
<span id="cb19-15"></span>
<span id="cb19-16">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>(recovery_table, <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;">"Parameter Recovery Check: Simulated Data"</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></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Parameter Recovery Check: Simulated Data</caption>
<thead>
<tr class="header">
<th style="text-align: left;">Component</th>
<th style="text-align: left;">Parameter</th>
<th style="text-align: right;">True Value</th>
<th style="text-align: right;">Estimated</th>
<th style="text-align: right;">Difference</th>
<th style="text-align: left;">Recovered</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Zero</td>
<td style="text-align: left;">Intercept</td>
<td style="text-align: right;">-0.5</td>
<td style="text-align: right;">-0.342</td>
<td style="text-align: right;">0.158</td>
<td style="text-align: left;">✓</td>
</tr>
<tr class="even">
<td style="text-align: left;">Zero</td>
<td style="text-align: left;">Slope</td>
<td style="text-align: right;">0.3</td>
<td style="text-align: right;">0.279</td>
<td style="text-align: right;">-0.021</td>
<td style="text-align: left;">✓</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Count</td>
<td style="text-align: left;">Intercept</td>
<td style="text-align: right;">0.5</td>
<td style="text-align: right;">0.312</td>
<td style="text-align: right;">-0.188</td>
<td style="text-align: left;">✓</td>
</tr>
<tr class="even">
<td style="text-align: left;">Count</td>
<td style="text-align: left;">Slope</td>
<td style="text-align: right;">0.2</td>
<td style="text-align: right;">0.215</td>
<td style="text-align: right;">0.015</td>
<td style="text-align: left;">✓</td>
</tr>
</tbody>
</table>
</div>
</div>
<p><strong>Conclusion</strong>: Model successfully recovers known parameters (differences &lt; 0.2). We can apply it to real data with confidence.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/sim-visualization-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p><strong>Key Takeaway</strong>: The simulation proves our modeling framework works-when we feed the hurdle model data generated from known parameters, it successfully recovers those parameters.</p>
<hr>
</section>
</section>
</section>
<section id="results" class="level1">
<h1>7. Results</h1>
<section id="fit-model-to-real-data" class="level3">
<h3 class="anchored" data-anchor-id="fit-model-to-real-data">Fit Model to Real Data</h3>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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;"># Fit hurdle model with negative binomial count distribution</span></span>
<span id="cb20-2">hurdle_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;">hurdle</span>(</span>
<span id="cb20-3">  roadkill_count <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> log_AADT <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> road_type <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> speed_limit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-4">                   pct_forest <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pct_farmland <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pct_residential <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pct_park <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>(len_km) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb20-5">                   log_AADT <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> road_type <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> speed_limit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-6">                   pct_forest <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pct_farmland <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pct_residential <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pct_park,</span>
<span id="cb20-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> model_data,</span>
<span id="cb20-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist =</span> config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model_distribution,</span>
<span id="cb20-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zero.dist =</span> config<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model_zero_dist</span>
<span id="cb20-10">)</span></code></pre></div></div>
</details>
</div>
<section id="model-diagnostics" class="level4">
<h4 class="anchored" data-anchor-id="model-diagnostics">Model Diagnostics</h4>
<blockquote class="blockquote">
<ul>
<li><a href="https://www.statlect.com/glossary/log-likelihood">Log-Likelihood</a>: -3.06357^{4}</li>
<li><a href="https://en.wikipedia.org/wiki/Akaike_information_criterion">AIC</a>: 6.13113^{4}</li>
<li><a href="https://en.wikipedia.org/wiki/Bayesian_information_criterion">BIC</a>: 6.15193^{4}</li>
</ul>
</blockquote>
</section>
</section>
<section id="model-coefficients" class="level3">
<h3 class="anchored" data-anchor-id="model-coefficients">Model Coefficients</h3>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Hurdle Model Results</caption>
<thead>
<tr class="header">
<th style="text-align: left;">Parameter</th>
<th style="text-align: right;">Estimate</th>
<th style="text-align: right;">SE</th>
<th style="text-align: right;">Z_value</th>
<th style="text-align: right;">P_value</th>
<th style="text-align: left;">Sig</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">count_(Intercept)</td>
<td style="text-align: right;">-1.6019</td>
<td style="text-align: right;">0.2571</td>
<td style="text-align: right;">-6.23</td>
<td style="text-align: right;">0.00000</td>
<td style="text-align: left;">***</td>
</tr>
<tr class="even">
<td style="text-align: left;">count_log_AADT</td>
<td style="text-align: right;">0.0198</td>
<td style="text-align: right;">0.0138</td>
<td style="text-align: right;">1.44</td>
<td style="text-align: right;">0.14970</td>
<td style="text-align: left;"></td>
</tr>
<tr class="odd">
<td style="text-align: left;">count_road_typeMinor</td>
<td style="text-align: right;">-0.8416</td>
<td style="text-align: right;">0.0858</td>
<td style="text-align: right;">-9.80</td>
<td style="text-align: right;">0.00000</td>
<td style="text-align: left;">***</td>
</tr>
<tr class="even">
<td style="text-align: left;">count_road_typeOther</td>
<td style="text-align: right;">-1.5388</td>
<td style="text-align: right;">0.4381</td>
<td style="text-align: right;">-3.51</td>
<td style="text-align: right;">0.00044</td>
<td style="text-align: left;">***</td>
</tr>
<tr class="odd">
<td style="text-align: left;">count_speed_limit</td>
<td style="text-align: right;">0.0082</td>
<td style="text-align: right;">0.0011</td>
<td style="text-align: right;">7.72</td>
<td style="text-align: right;">0.00000</td>
<td style="text-align: left;">***</td>
</tr>
<tr class="even">
<td style="text-align: left;">count_pct_forest</td>
<td style="text-align: right;">0.0107</td>
<td style="text-align: right;">0.0023</td>
<td style="text-align: right;">4.62</td>
<td style="text-align: right;">0.00000</td>
<td style="text-align: left;">***</td>
</tr>
<tr class="odd">
<td style="text-align: left;">count_pct_farmland</td>
<td style="text-align: right;">-0.0030</td>
<td style="text-align: right;">0.0022</td>
<td style="text-align: right;">-1.42</td>
<td style="text-align: right;">0.15671</td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: left;">count_pct_residential</td>
<td style="text-align: right;">0.0016</td>
<td style="text-align: right;">0.0026</td>
<td style="text-align: right;">0.59</td>
<td style="text-align: right;">0.55277</td>
<td style="text-align: left;"></td>
</tr>
<tr class="odd">
<td style="text-align: left;">count_pct_park</td>
<td style="text-align: right;">0.0010</td>
<td style="text-align: right;">0.0038</td>
<td style="text-align: right;">0.27</td>
<td style="text-align: right;">0.78803</td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: left;">count_log(len_km)</td>
<td style="text-align: right;">0.7850</td>
<td style="text-align: right;">0.0290</td>
<td style="text-align: right;">27.06</td>
<td style="text-align: right;">0.00000</td>
<td style="text-align: left;">***</td>
</tr>
<tr class="odd">
<td style="text-align: left;">zero_(Intercept)</td>
<td style="text-align: right;">-4.2640</td>
<td style="text-align: right;">0.1144</td>
<td style="text-align: right;">-37.29</td>
<td style="text-align: right;">0.00000</td>
<td style="text-align: left;">***</td>
</tr>
<tr class="even">
<td style="text-align: left;">zero_log_AADT</td>
<td style="text-align: right;">0.0032</td>
<td style="text-align: right;">0.0073</td>
<td style="text-align: right;">0.44</td>
<td style="text-align: right;">0.66214</td>
<td style="text-align: left;"></td>
</tr>
<tr class="odd">
<td style="text-align: left;">zero_road_typeMinor</td>
<td style="text-align: right;">-1.2231</td>
<td style="text-align: right;">0.0379</td>
<td style="text-align: right;">-32.24</td>
<td style="text-align: right;">0.00000</td>
<td style="text-align: left;">***</td>
</tr>
<tr class="even">
<td style="text-align: left;">zero_road_typeOther</td>
<td style="text-align: right;">-1.5628</td>
<td style="text-align: right;">0.1124</td>
<td style="text-align: right;">-13.90</td>
<td style="text-align: right;">0.00000</td>
<td style="text-align: left;">***</td>
</tr>
<tr class="odd">
<td style="text-align: left;">zero_speed_limit</td>
<td style="text-align: right;">0.0064</td>
<td style="text-align: right;">0.0005</td>
<td style="text-align: right;">12.83</td>
<td style="text-align: right;">0.00000</td>
<td style="text-align: left;">***</td>
</tr>
<tr class="even">
<td style="text-align: left;">zero_pct_forest</td>
<td style="text-align: right;">0.0311</td>
<td style="text-align: right;">0.0011</td>
<td style="text-align: right;">28.17</td>
<td style="text-align: right;">0.00000</td>
<td style="text-align: left;">***</td>
</tr>
<tr class="odd">
<td style="text-align: left;">zero_pct_farmland</td>
<td style="text-align: right;">0.0214</td>
<td style="text-align: right;">0.0010</td>
<td style="text-align: right;">21.68</td>
<td style="text-align: right;">0.00000</td>
<td style="text-align: left;">***</td>
</tr>
<tr class="even">
<td style="text-align: left;">zero_pct_residential</td>
<td style="text-align: right;">-0.0137</td>
<td style="text-align: right;">0.0012</td>
<td style="text-align: right;">-11.49</td>
<td style="text-align: right;">0.00000</td>
<td style="text-align: left;">***</td>
</tr>
<tr class="odd">
<td style="text-align: left;">zero_pct_park</td>
<td style="text-align: right;">0.0009</td>
<td style="text-align: right;">0.0021</td>
<td style="text-align: right;">0.42</td>
<td style="text-align: right;">0.67786</td>
<td style="text-align: left;"></td>
</tr>
</tbody>
</table>
</div>
</div>
<p><strong>Interpretation guide:</strong></p>
<ul>
<li><strong>Zero component</strong>: Log-odds coefficients. Positive values increase the probability of roadkill occurring at all.</li>
<li><strong>Count component</strong>: Log-scale coefficients. Positive values increase the expected count given that roadkill occurs.</li>
<li><strong>Significance levels</strong>: *** p&lt;0.001, ** p&lt;0.01, * p&lt;0.05</li>
</ul>
</section>
<section id="model-predictions-with-confidence-intervals" class="level3">
<h3 class="anchored" data-anchor-id="model-predictions-with-confidence-intervals">Model Predictions with Confidence Intervals</h3>
<p>To visualize model fit and interpret effects, we generate predictions across the range of each predictor while holding other variables at their median values.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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;"># 1. Traffic volume effect</span></span>
<span id="cb21-2">aadt_seq <span class="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;">seq</span>(</span>
<span id="cb21-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>log_AADT, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>),</span>
<span id="cb21-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>log_AADT, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>),</span>
<span id="cb21-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;">100</span></span>
<span id="cb21-6">))</span>
<span id="cb21-7"></span>
<span id="cb21-8">newdata_traffic <span class="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="cb21-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AADT            =</span> aadt_seq,</span>
<span id="cb21-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log_AADT        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(AADT),</span>
<span id="cb21-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">road_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;">"Major"</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>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>road_type)),</span>
<span id="cb21-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">speed_limit     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>speed_limit),</span>
<span id="cb21-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_forest      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_forest),</span>
<span id="cb21-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_farmland    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_farmland),</span>
<span id="cb21-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_residential =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_residential),</span>
<span id="cb21-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_park        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_park),</span>
<span id="cb21-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">len_km          =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb21-18">)</span>
<span id="cb21-19"></span>
<span id="cb21-20">ci_zero_tr  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hurdle_ci</span>(hurdle_model, newdata_traffic, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">component =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zero"</span>)</span>
<span id="cb21-21">ci_count_tr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hurdle_ci</span>(hurdle_model, newdata_traffic, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">component =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>)</span>
<span id="cb21-22"></span>
<span id="cb21-23">newdata_traffic <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> newdata_traffic <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb21-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob_nonzero        =</span> ci_zero_tr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit,</span>
<span id="cb21-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob_lower          =</span> ci_zero_tr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower,</span>
<span id="cb21-27">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob_upper          =</span> ci_zero_tr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper,</span>
<span id="cb21-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count_given_nonzero =</span> ci_count_tr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit,</span>
<span id="cb21-29">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count_lower         =</span> ci_count_tr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower,</span>
<span id="cb21-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count_upper         =</span> ci_count_tr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper</span>
<span id="cb21-31">  )</span>
<span id="cb21-32"></span>
<span id="cb21-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Forest effect</span></span>
<span id="cb21-34">newdata_forest <span class="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="cb21-35">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_forest      =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</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;">100</span>),</span>
<span id="cb21-36">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log_AADT        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>AADT)),</span>
<span id="cb21-37">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">road_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;">"Major"</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>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>road_type)),</span>
<span id="cb21-38">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">speed_limit     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>speed_limit),</span>
<span id="cb21-39">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_farmland    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_farmland),</span>
<span id="cb21-40">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_residential =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_residential),</span>
<span id="cb21-41">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_park        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_park),</span>
<span id="cb21-42">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">len_km          =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb21-43">)</span>
<span id="cb21-44"></span>
<span id="cb21-45">ci_zero_forest  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hurdle_ci</span>(hurdle_model, newdata_forest, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">component =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zero"</span>)</span>
<span id="cb21-46">ci_count_forest <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hurdle_ci</span>(hurdle_model, newdata_forest, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">component =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>)</span>
<span id="cb21-47"></span>
<span id="cb21-48">newdata_forest <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> newdata_forest <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb21-50">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob_nonzero        =</span> ci_zero_forest<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit,</span>
<span id="cb21-51">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob_lower          =</span> ci_zero_forest<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower,</span>
<span id="cb21-52">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob_upper          =</span> ci_zero_forest<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper,</span>
<span id="cb21-53">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count_given_nonzero =</span> ci_count_forest<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit,</span>
<span id="cb21-54">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count_lower         =</span> ci_count_forest<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower,</span>
<span id="cb21-55">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count_upper         =</span> ci_count_forest<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper</span>
<span id="cb21-56">  )</span>
<span id="cb21-57"></span>
<span id="cb21-58"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Residential development effect</span></span>
<span id="cb21-59">newdata_residential <span class="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="cb21-60">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_residential =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</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;">100</span>),</span>
<span id="cb21-61">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log_AADT        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>AADT)),</span>
<span id="cb21-62">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">road_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;">"Major"</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>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>road_type)),</span>
<span id="cb21-63">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">speed_limit     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>speed_limit),</span>
<span id="cb21-64">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_forest      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_forest),</span>
<span id="cb21-65">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_farmland    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_farmland),</span>
<span id="cb21-66">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_park        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_park),</span>
<span id="cb21-67">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">len_km          =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb21-68">)</span>
<span id="cb21-69"></span>
<span id="cb21-70">ci_zero_residential  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hurdle_ci</span>(hurdle_model, newdata_residential, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">component =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zero"</span>)</span>
<span id="cb21-71">ci_count_residential <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hurdle_ci</span>(hurdle_model, newdata_residential, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">component =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>)</span>
<span id="cb21-72"></span>
<span id="cb21-73">newdata_residential <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> newdata_residential <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-74">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb21-75">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob_nonzero        =</span> ci_zero_residential<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit,</span>
<span id="cb21-76">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob_lower          =</span> ci_zero_residential<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower,</span>
<span id="cb21-77">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob_upper          =</span> ci_zero_residential<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper,</span>
<span id="cb21-78">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count_given_nonzero =</span> ci_count_residential<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit,</span>
<span id="cb21-79">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count_lower         =</span> ci_count_residential<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower,</span>
<span id="cb21-80">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count_upper         =</span> ci_count_residential<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper</span>
<span id="cb21-81">  )</span>
<span id="cb21-82"></span>
<span id="cb21-83"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Road type effect</span></span>
<span id="cb21-84">newdata_roadtype <span class="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="cb21-85">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">road_type       =</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;">"Major"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Minor"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</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>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>road_type)),</span>
<span id="cb21-86">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log_AADT        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>AADT)),</span>
<span id="cb21-87">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">speed_limit     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>speed_limit),</span>
<span id="cb21-88">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_forest      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_forest),</span>
<span id="cb21-89">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_farmland    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_farmland),</span>
<span id="cb21-90">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_residential =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_residential),</span>
<span id="cb21-91">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct_park        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(model_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pct_park),</span>
<span id="cb21-92">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">len_km          =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb21-93">)</span>
<span id="cb21-94"></span>
<span id="cb21-95">ci_zero_roadtype  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hurdle_ci</span>(hurdle_model, newdata_roadtype, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">component =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zero"</span>)</span>
<span id="cb21-96">ci_count_roadtype <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hurdle_ci</span>(hurdle_model, newdata_roadtype, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">component =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>)</span>
<span id="cb21-97"></span>
<span id="cb21-98">newdata_roadtype <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> newdata_roadtype <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-99">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb21-100">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob_nonzero        =</span> ci_zero_roadtype<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit,</span>
<span id="cb21-101">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob_lower          =</span> ci_zero_roadtype<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower,</span>
<span id="cb21-102">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob_upper          =</span> ci_zero_roadtype<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper,</span>
<span id="cb21-103">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count_given_nonzero =</span> ci_count_roadtype<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit,</span>
<span id="cb21-104">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count_lower         =</span> ci_count_roadtype<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lower,</span>
<span id="cb21-105">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count_upper         =</span> ci_count_roadtype<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>upper</span>
<span id="cb21-106">  )</span>
<span id="cb21-107"></span>
<span id="cb21-108"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute empirical values for comparison</span></span>
<span id="cb21-109">empirical_roadtype_occurrence <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> model_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-110">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(road_type) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-111">  <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;">empirical_prob =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(roadkill_count <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;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span>
<span id="cb21-112"></span>
<span id="cb21-113">empirical_roadtype_intensity <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> model_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-114">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(roadkill_count <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;">%&gt;%</span></span>
<span id="cb21-115">  <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;">rate_per_km =</span> roadkill_count <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> len_km)</span></code></pre></div></div>
</details>
</div>
</section>
<section id="model-fit-to-data" class="level3">
<h3 class="anchored" data-anchor-id="model-fit-to-data">Model Fit to Data</h3>
<p>We visualize model predictions against observed data to assess fit and interpret effect sizes.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-2-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-1" aria-controls="tabset-2-1" aria-selected="true" href="">Forest Coverage</a></li><li class="nav-item"><a class="nav-link" id="tabset-2-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-2" aria-controls="tabset-2-2" aria-selected="false" href="">Road Type</a></li></ul>
<div class="tab-content">
<div id="tabset-2-1" class="tab-pane active" aria-labelledby="tabset-2-1-tab">
<div class="columns">
<div class="column" style="width:48%;">
<p><strong>Occurrence (Zero Hurdle)</strong></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="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="cb22-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Observed: binned proportions</span></span>
<span id="cb22-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_summary_bin</span>(</span>
<span id="cb22-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> model_data,</span>
<span id="cb22-5">    <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_forest, <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.numeric</span>(roadkill_count <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="cb22-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> mean,</span>
<span id="cb22-7">    <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;">"point"</span>,</span>
<span id="cb22-8">    <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;">20</span>,</span>
<span id="cb22-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> primary_color,</span>
<span id="cb22-10">    <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>
<span id="cb22-11">    <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>
<span id="cb22-12">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model: 95% CI</span></span>
<span id="cb22-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_ribbon</span>(</span>
<span id="cb22-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> newdata_forest,</span>
<span id="cb22-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> pct_forest, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> prob_lower, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> prob_upper),</span>
<span id="cb22-17">    <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>
<span id="cb22-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> accent_color</span>
<span id="cb22-19">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-20">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model: fitted line</span></span>
<span id="cb22-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(</span>
<span id="cb22-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> newdata_forest, </span>
<span id="cb22-23">    <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_forest, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> prob_nonzero), </span>
<span id="cb22-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> accent_color, </span>
<span id="cb22-25">    <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="cb22-26">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-27">  <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>percent) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb22-29">    <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;">"Forest Effect on Occurrence"</span>,</span>
<span id="cb22-30">    <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;">"% Forest within 500m"</span>, </span>
<span id="cb22-31">    <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 of Any Roadkill"</span></span>
<span id="cb22-32">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>()</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/plot-occurrence-forest-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Forest coverage strongly predicts whether roadkill happens at all. Roads with 100% forest nearby have ~20% probability vs ~2% for roads with no forest.</p>
</div><div class="column" style="width:4%;">

</div><div class="column" style="width:48%;">
<p><strong>Intensity (Count Component)</strong></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Observed: raw data (light background)</span></span>
<span id="cb23-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(</span>
<span id="cb23-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> model_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(roadkill_count <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="cb23-5">    <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_forest, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> roadkill_count <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> len_km),</span>
<span id="cb23-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> primary_color,</span>
<span id="cb23-7">    <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>
<span id="cb23-8">    <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.8</span></span>
<span id="cb23-9">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Observed: binned means</span></span>
<span id="cb23-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_summary_bin</span>(</span>
<span id="cb23-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> model_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(roadkill_count <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="cb23-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> pct_forest, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> roadkill_count <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> len_km),</span>
<span id="cb23-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> mean,</span>
<span id="cb23-15">    <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;">"point"</span>,</span>
<span id="cb23-16">    <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;">15</span>,</span>
<span id="cb23-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> primary_color,</span>
<span id="cb23-18">    <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>
<span id="cb23-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span></span>
<span id="cb23-20">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-21">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model: 95% CI</span></span>
<span id="cb23-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_ribbon</span>(</span>
<span id="cb23-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> newdata_forest,</span>
<span id="cb23-24">    <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_forest, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> count_lower, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> count_upper),</span>
<span id="cb23-25">    <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>
<span id="cb23-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> accent_color</span>
<span id="cb23-27">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-28">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model: fitted line</span></span>
<span id="cb23-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(</span>
<span id="cb23-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> newdata_forest,</span>
<span id="cb23-31">    <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_forest, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> count_given_nonzero),</span>
<span id="cb23-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> accent_color,</span>
<span id="cb23-33">    <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="cb23-34">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_log10</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>comma) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb23-37">    <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;">"Forest Effect on Intensity"</span>,</span>
<span id="cb23-38">    <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;">"Green = binned means | Orange = model"</span>,</span>
<span id="cb23-39">    <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;">"% Forest within 500m"</span>,</span>
<span id="cb23-40">    <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;">"Roadkill Events per km (log scale)"</span></span>
<span id="cb23-41">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-42">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>()</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/plot-intensity-forest-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Interpretation:</strong> Forest has minimal effect on intensity once roadkill does occur (nearly flat line). This contrasts sharply with its strong effect on occurrence.</p>
</div>
</div>
</div>
<div id="tabset-2-2" class="tab-pane" aria-labelledby="tabset-2-2-tab">
<div class="columns">
<div class="column" style="width:48%;">
<p><strong>Occurrence (Zero Hurdle)</strong></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Observed: empirical proportions</span></span>
<span id="cb24-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(</span>
<span id="cb24-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> empirical_roadtype_occurrence,</span>
<span id="cb24-5">    <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> road_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> empirical_prob),</span>
<span id="cb24-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> primary_color,</span>
<span id="cb24-7">    <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>
<span id="cb24-8">    <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;">8</span></span>
<span id="cb24-9">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model: error bars</span></span>
<span id="cb24-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_errorbar</span>(</span>
<span id="cb24-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> newdata_roadtype,</span>
<span id="cb24-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> road_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> prob_lower, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> prob_upper),</span>
<span id="cb24-14">    <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="cb24-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> accent_color,</span>
<span id="cb24-16">    <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="cb24-17">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model: point estimates</span></span>
<span id="cb24-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(</span>
<span id="cb24-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> newdata_roadtype,</span>
<span id="cb24-21">    <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> road_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> prob_nonzero),</span>
<span id="cb24-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> accent_color,</span>
<span id="cb24-23">    <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>
<span id="cb24-24">  ) <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;">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>percent) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb24-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;">"Road Type Effect on Occurrence"</span>,</span>
<span id="cb24-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;">"Road Type"</span>,</span>
<span id="cb24-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;">"Probability of Any Roadkill"</span></span>
<span id="cb24-30">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>()</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/plot-occurrence-roadtype-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Interpretation:</strong> Major roads are 7x more likely to experience any roadkill compared to minor roads (~7% vs ~1%).</p>
</div><div class="column" style="width:4%;">

</div><div class="column" style="width:48%;">
<p><strong>Intensity (Count Component)</strong></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Observed: distribution (violin)</span></span>
<span id="cb25-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_violin</span>(</span>
<span id="cb25-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> empirical_roadtype_intensity,</span>
<span id="cb25-5">    <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> road_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> rate_per_km),</span>
<span id="cb25-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> primary_color,</span>
<span id="cb25-7">    <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>
<span id="cb25-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"width"</span></span>
<span id="cb25-9">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Observed: means with error bars</span></span>
<span id="cb25-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_summary</span>(</span>
<span id="cb25-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> empirical_roadtype_intensity,</span>
<span id="cb25-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> road_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> rate_per_km),</span>
<span id="cb25-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> mean,</span>
<span id="cb25-15">    <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;">"point"</span>,</span>
<span id="cb25-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> primary_color,</span>
<span id="cb25-17">    <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;">5</span></span>
<span id="cb25-18">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_summary</span>(</span>
<span id="cb25-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> empirical_roadtype_intensity,</span>
<span id="cb25-21">    <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> road_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> rate_per_km),</span>
<span id="cb25-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun.data =</span> mean_se,</span>
<span id="cb25-23">    <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;">"errorbar"</span>,</span>
<span id="cb25-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> primary_color,</span>
<span id="cb25-25">    <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>
<span id="cb25-26">    <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>
<span id="cb25-27">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-28">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model: predictions (diamond shape)</span></span>
<span id="cb25-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_errorbar</span>(</span>
<span id="cb25-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> newdata_roadtype,</span>
<span id="cb25-31">    <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> road_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> count_lower, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> count_upper),</span>
<span id="cb25-32">    <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>
<span id="cb25-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> accent_color,</span>
<span id="cb25-34">    <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="cb25-35">    <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_nudge</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>)</span>
<span id="cb25-36">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(</span>
<span id="cb25-38">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> newdata_roadtype,</span>
<span id="cb25-39">    <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> road_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> count_given_nonzero),</span>
<span id="cb25-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> accent_color,</span>
<span id="cb25-41">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,</span>
<span id="cb25-42">    <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;">18</span>,</span>
<span id="cb25-43">    <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_nudge</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>)</span>
<span id="cb25-44">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_log10</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>comma) <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;">labs</span>(</span>
<span id="cb25-47">    <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;">"Road Type Effect on Intensity"</span>,</span>
<span id="cb25-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;">"Green circles = observed | Orange diamonds = model"</span>,</span>
<span id="cb25-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;">"Road Type"</span>,</span>
<span id="cb25-50">    <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;">"Roadkill Events per km (log scale)"</span></span>
<span id="cb25-51">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-52">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_cohesive</span>()</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rellimylime.github.io/posts/eds222-final/index_files/figure-html/plot-intensity-roadtype-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Interpretation:</strong> Major roads also have higher intensity, but the effect is less dramatic than for occurrence.</p>
</div>
</div>
</div>
</div>
</div>
<p><strong>Model performance summary</strong>: The model captures occurrence patterns much better than intensity patterns. The hurdle model excels at predicting WHETHER roadkill occurs, but has limited ability to predict HOW MUCH occurs given that it happens.</p>
<hr>
</section>
<section id="hypothesis-testing" class="level3">
<h3 class="anchored" data-anchor-id="hypothesis-testing">Hypothesis Testing</h3>
<section id="h1-traffic-volume-effect" class="level4">
<h4 class="anchored" data-anchor-id="h1-traffic-volume-effect">H1: Traffic Volume Effect</h4>
<div class="columns">
<div class="column">
<p><strong>Occurrence component:</strong></p>
<ul>
<li>Coefficient: 0.0032, SE: 0.0073, p = 0.66214</li>
<li>Result: Traffic does not significantly affect roadkill probability</li>
</ul>
</div><div class="column">
<p><strong>Intensity component:</strong></p>
<ul>
<li>Coefficient: 0.0198, SE: 0.0138, p = 0.1497</li>
<li>Result: Traffic does not significantly affect collision intensity</li>
</ul>
</div>
</div>
<p>H1 Status: <strong>NOT SUPPORTED</strong></p>
<p><strong>Effect size:</strong> A 10% increase in traffic volume changes odds of occurrence by 0% and expected count by 0.2%.</p>
</section>
<section id="h2-land-use-effects" class="level4">
<h4 class="anchored" data-anchor-id="h2-land-use-effects">H2: Land Use Effects</h4>
<div class="columns">
<div class="column">
<section id="h2a-forestpark-habitat" class="level5">
<h5 class="anchored" data-anchor-id="h2a-forestpark-habitat">H2a: Forest/Park habitat</h5>
<p>Occurrence: p = 0 *** | Intensity: p = 0 ***</p>
<p>Forest coverage significantly affects roadkill patterns.</p>
</section>
</div><div class="column">
<section id="h2b-residential-development" class="level5">
<h5 class="anchored" data-anchor-id="h2b-residential-development">H2b: Residential development</h5>
<p>Occurrence: p = 0 *** | Intensity: p = 0.55277</p>
<p>Residential areas show negative association (as predicted).</p>
</section>
</div><p>H2 Status: <strong>SUPPORTED</strong></p>
</div>
</section>
<section id="h3-road-characteristics" class="level4">
<h4 class="anchored" data-anchor-id="h3-road-characteristics">H3: Road Characteristics</h4>
<div class="columns">
<div class="column">
<section id="h3a-minor-roads-vs-major-baseline" class="level5">
<h5 class="anchored" data-anchor-id="h3a-minor-roads-vs-major-baseline">H3a: Minor roads (vs Major baseline)</h5>
<p>Occurrence: p = 0 *** | Intensity: p = 0 ***</p>
<p>Minor roads have lower roadkill rates than major roads.</p>
</section>
</div><div class="column">
<section id="h3b-speed-limit" class="level5">
<h5 class="anchored" data-anchor-id="h3b-speed-limit">H3b: Speed limit</h5>
<p>Occurrence: p = 0 *** | Intensity: p = 0 ***</p>
<p>Speed limit significantly affects collision risk.</p>
</section>
</div><p>H3 Status: <strong>SUPPORTED</strong></p>
</div>
<hr>
</section>
</section>
</section>
<section id="discussion" class="level1">
<h1>8. Discussion</h1>
<section id="summary-of-findings" class="level3">
<h3 class="anchored" data-anchor-id="summary-of-findings">Summary of Findings</h3>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Hypothesis Test Results (+ positive effect, − negative effect, NS not significant; baseline: Major roads)</caption>
<thead>
<tr class="header">
<th style="text-align: left;">Hypothesis</th>
<th style="text-align: left;">Occurrence</th>
<th style="text-align: left;">Intensity</th>
<th style="text-align: left;">Support</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">H1: Traffic</td>
<td style="text-align: left;">NS</td>
<td style="text-align: left;">NS</td>
<td style="text-align: left;">✗</td>
</tr>
<tr class="even">
<td style="text-align: left;">H2a: Forest</td>
<td style="text-align: left;">+</td>
<td style="text-align: left;">+</td>
<td style="text-align: left;">✓</td>
</tr>
<tr class="odd">
<td style="text-align: left;">H2b: Residential</td>
<td style="text-align: left;">−</td>
<td style="text-align: left;">NS</td>
<td style="text-align: left;">✓</td>
</tr>
<tr class="even">
<td style="text-align: left;">H3a: Minor roads</td>
<td style="text-align: left;">−</td>
<td style="text-align: left;">−</td>
<td style="text-align: left;">✓</td>
</tr>
<tr class="odd">
<td style="text-align: left;">H3b: Speed</td>
<td style="text-align: left;">+</td>
<td style="text-align: left;">+</td>
<td style="text-align: left;">✓</td>
</tr>
</tbody>
</table>
</div>
</div>
<p><strong>Key Pattern:</strong> Almost all predictors successfully explain occurrence but fail to explain intensity. This suggests that <strong>where</strong> roadkill occurs is predictable from landscape features, but <strong>how frequently</strong> it occurs at those locations involves additional unmeasured factors.</p>
<p><strong>What this means:</strong> - <strong>WHERE roadkill happens</strong> (occurrence) is highly predictable from landscape and infrastructure features - <strong>HOW MUCH roadkill happens</strong> (intensity) is largely unexplained by our model-suggesting it’s driven by unmeasured factors like wildlife population dynamics, seasonal movements, or driver behavior</p>
</section>
<section id="policy-implications" class="level3">
<h3 class="anchored" data-anchor-id="policy-implications">Policy Implications</h3>
<p>Our findings suggest several evidence-based mitigation strategies:</p>
<ol type="1">
<li><p><strong>Habitat-road interfaces</strong>: Roads passing through or adjacent to forests and parks show substantially elevated collision risk. These areas should be prioritized for wildlife crossing structures and fencing.</p></li>
<li><p><strong>Targeted interventions</strong>: Focus mitigation efforts (wildlife crossings, fencing) on:</p>
<ul>
<li>Major roads (which have 7x higher collision probability than minor roads)</li>
<li>Roads adjacent to forests/parks (strongest predictor of occurrence)</li>
<li>Areas where predicted collision probability exceeds acceptable thresholds</li>
</ul></li>
<li><p><strong>Land use planning</strong>: Consider roadkill risk in environmental impact assessments when approving development near wildlife habitat</p></li>
</ol>
</section>
<section id="limitations" class="level3">
<h3 class="anchored" data-anchor-id="limitations">Limitations</h3>
<ol type="1">
<li><p><strong>Spatial coverage</strong>: Analysis limited to roads near traffic counters (~75% of network). Low-traffic rural roads may be underrepresented.</p></li>
<li><p><strong>Species grouping</strong>: All species combined into one outcome. Species-specific behaviors and risks are not captured.</p></li>
<li><p><strong>Temporal misalignment</strong>: Road data from 2024, traffic from 2019, roadkill from 2017–2019. Seasonal and daily patterns not modeled.</p></li>
<li><p><strong>Missing variables</strong>: No direct data on wildlife population density, road features (fencing, lighting), or driver behavior.</p></li>
<li><p><strong>Reporting bias</strong>: Not all collisions reported; rural areas may appear safer due to underreporting.</p></li>
<li><p><strong>Spatial clustering</strong>: Collisions cluster spatially, but this pattern is not explicitly modeled.</p></li>
<li><p><strong>Causal interpretation</strong>: Results show associations, not causation. Land use proxies for wildlife presence.</p></li>
</ol>
</section>
<section id="conclusions" class="level3">
<h3 class="anchored" data-anchor-id="conclusions">Conclusions</h3>
<p>This analysis demonstrates how hurdle models handle zero-inflated ecological count data by separately modeling occurrence and intensity processes. We identified traffic volume, land use composition, and road characteristics as key predictors of wildlife-vehicle collisions on Danish roads.</p>
<p>Key contributions:</p>
<ul>
<li>Methodological framework for zero-inflated spatial count data</li>
<li>Quantified effects of infrastructure and landscape on roadkill risk</li>
<li>Policy-relevant findings for targeted mitigation</li>
</ul>
<p>The hurdle model approach extends beyond roadkill to any ecological phenomenon with excess zeros: rare species observations, disease outbreaks, extreme weather events, or pollution violations.</p>
<hr>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-vejdirektoratet2019" class="csl-entry">
Danish Road Directorate. 2017--2019. <span>“Traffic Counts – Key Figures (MASTRA).”</span> <a href="https://www.opendata.dk/vejdirektoratet/taellinger-nogletal-mastra" class="uri">https://www.opendata.dk/vejdirektoratet/taellinger-nogletal-mastra</a>.
</div>
<div id="ref-grilo2025" class="csl-entry">
Grilo, Clara, Tomé Neves, Jennifer Bates, Aliza le Roux, Pablo Medrano-Vizcaíno, Mattia Quaranta, Inês Silva, et al. 2025. <span>“Global Roadkill Data: A Dataset on Terrestrial Vertebrate Mortality Caused by Collision with Vehicles.”</span> figshare. <a href="https://doi.org/10.6084/M9.FIGSHARE.25714233">https://doi.org/10.6084/M9.FIGSHARE.25714233</a>.
</div>
<div id="ref-osm2024" class="csl-entry">
OpenStreetMap contributors. 2024. <span>“Planet Dump Retrieved from Https://Planet.openstreetmap.org.”</span> <a href="https://www.openstreetmap.org" class="uri">https://www.openstreetmap.org</a>.
</div>
</div>


</section>
</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{miller2025,
  author = {Miller, Emily},
  title = {Crossing {Signals}},
  date = {2025-11-29},
  url = {https://rellimylime.github.io/posts/eds222-final/},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-miller2025" class="csl-entry quarto-appendix-citeas">
Miller, Emily. 2025. <span>“Crossing Signals.”</span> November 29, 2025.
<a href="https://rellimylime.github.io/posts/eds222-final/">https://rellimylime.github.io/posts/eds222-final/</a>.
</div></div></section></div> ]]></description>
  <category>MEDS</category>
  <category>R</category>
  <category>Statistics</category>
  <category>Wildlife</category>
  <guid>https://rellimylime.github.io/posts/eds222-final/</guid>
  <pubDate>Sat, 29 Nov 2025 08:00:00 GMT</pubDate>
  <media:content url="https://rellimylime.github.io/images/posts/eds222-final/eds222-final-cover.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>The Fog the Models Missed</title>
  <dc:creator>Emily Miller</dc:creator>
  <link>https://rellimylime.github.io/posts/eds242-final/</link>
  <description><![CDATA[ 





<p>Opening the door at 3pm in August 2020 felt like opening an oven.<br>
My family rationed AC before the heat peaked, then toughed it out through the evening.<br>
The message from our utility was clear: cut power or expect blackouts.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rellimylime.github.io/images/posts/eds242-final/rolling-blackout.jpg" class="img-fluid figure-img"></p>
<figcaption>https://www.kqed.org/news/11878348/during-rolling-blackouts-last-summer-california-kept-exporting-power-out-of-state-theres-still-no-permanent-fix</figcaption>
</figure>
</div>
<p>I thought I understood why: heat wave → too many AC units → overloaded grid.</p>
<p>But while researching for my EDS 242 Ethics and Bias class, I found something different.<br>
Part of the blackout problem came from decisions made years earlier, based on climate models that <strong>overestimated how much solar power California would have on hot summer evenings</strong>.</p>
<hr>
<section id="following-the-thread" class="level2">
<h2 class="anchored" data-anchor-id="following-the-thread">Following the Thread</h2>
<p>The California Independent System Operator’s root cause analysis <span class="citation" data-cites="caiso2021root">[@caiso2021root]</span> pointed to unprecedented heat, inadequate planning for evening “net peak” demand, and market forecasting errors. But embedded in those findings: resource planning hadn’t kept pace with California’s changing energy mix.</p>
<p>In the 2010s, California made massive investments in solar infrastructure based on downscaled CMIP5 climate model projections. The problem? The models systematically overestimated solar generation in regions influenced by California’s coastal meteorology.</p>
</section>
<section id="the-marine-layer-problem" class="level2">
<h2 class="anchored" data-anchor-id="the-marine-layer-problem">The Marine Layer Problem</h2>
<p>California’s coast has a distinctive climate feature: the marine layer. Cold Pacific waters create a stable layer of cool, moist air trapped under warmer air above. That marine layer produces fog and low stratus clouds that <strong>cut incoming solar radiation by 30–50%</strong> <span class="citation" data-cites="iacobellis2013variability">[@iacobellis2013variability]</span>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rellimylime.github.io/images/posts/eds242-final/marin-fog.webp" class="img-fluid figure-img"></p>
<figcaption>Source: https://patch.com/california/millvalley/fog-rolls-over-hills-marin-county-photo-day</figcaption>
</figure>
</div>
<p>These clouds often push inland through gaps like the Golden Gate and shape microclimates, like in the redwood forests of northern California, that climate models struggle to resolve <span class="citation" data-cites="johnstone2010fog">[@johnstone2010fog]</span>.</p>
<div class="columns">
<div class="column" style="width:48%;">
<p><strong>What the Models See</strong><br>
100-200 km grid cells<br>
(size of SF Bay Area)</p>
</div><div class="column" style="width:48%;">
<p><strong>What Actually Matters</strong><br>
Marine layer dynamics at &lt;10 km<br>
(Golden Gate, coastal valleys)</p>
</div>
</div>
<p>Global Climate Models (GCMs) in CMIP5 operate at coarse resolution-typically 100-200km grid cells. Marine layer dynamics happen at much finer scales, driven by sea surface gradients, topographic channeling, diurnal cycles, and upwelling.</p>
<p>Even “downscaled” versions refined to 10-20km resolution struggle to capture marine layer formation, persistence, and inland penetration.</p>
<p>A 2024 CEC report <span class="citation" data-cites="pierce2024downscaling">[@pierce2024downscaling]</span> states: &gt; “Coastal Low Clouds… are a persistent, seasonal feature… Accounting for these clouds improves solar energy forecasting.”</p>
<p>CMIP5-era models systematically underestimated marine layer frequency, inland fog extent, and afternoon/evening cloud cover. The Result?: Solar irradiance projections that were too optimistic, especially for afternoon/evening hours-exactly when California’s “net peak” demand occurs.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rellimylime.github.io/images/posts/eds242-final/fog-detail.jpg" class="img-fluid figure-img"></p>
<figcaption>https://www.usgs.gov/media/images/landsat-image-shows-marine-stratus-and-stratocumulus-cloud-layer-san-francisco-bay</figcaption>
</figure>
</div>
<hr>
</section>
<section id="the-path-from-models-to-blackouts" class="level2">
<h2 class="anchored" data-anchor-id="the-path-from-models-to-blackouts">The Path From Models to Blackouts</h2>
<p>So how does a bias in cloud cover modeling contribute to blackouts years later?</p>
<p>The timeline might have looked something like this:</p>
<div class="timeline">
<p><strong>2010–2015</strong>: Overoptimistic generation forecasts</p>
<ul>
<li>Solar developers use downscaled CMIP5 projections. Models underestimate afternoon cloud cover → projected generation 10-15% higher than reality.</li>
</ul>
<p><strong>2012–2018</strong>: Investment decisions</p>
<ul>
<li>CAISO plans for solar providing increasing power, assuming generation levels matching the overoptimistic projections.</li>
</ul>
<p><strong>2015–2020</strong>: Grid planning</p>
<ul>
<li>CAISO plans for solar providing increasing power, assuming generation levels matching the overoptimistic projections.</li>
</ul>
<p><strong>2016-2020</strong>: The “net peak” emerges</p>
<ul>
<li>Managing the evening “ramp” becomes critical. CAISO assumes more solar during the 5-8pm window than materializes.</li>
</ul>
<p><strong>August 2020</strong>: Heat Wave</p>
<ul>
<li>Historic heat wave hits California → Evening demand spikes.<br>
</li>
<li>Solar drops faster than expected → rolling blackouts.</li>
<li>Solar performs well mid-day but drops off earlier and faster than expected, partly due to cloud patterns the models hadn’t captured.</li>
</ul>
</div>
<style>
.timeline {
  border-left: 2px solid #999;
  padding-left: 1rem;
  margin: 2rem 0;
}
.timeline p {
  margin: 0.25rem 0 1.5rem 0;
}
.timeline strong {
  color: #333;
}
</style>
<p>CAISO’s root cause analysis <span class="citation" data-cites="caiso2021root">[@caiso2021root]</span> states:</p>
<blockquote class="blockquote">
<p>“Resource planning targets have not kept pace with the evolving power mix, wherein demand during peak hours outpaces the supply of solar-produced power.”</p>
</blockquote>
<p>Translation:</p>
<blockquote class="blockquote">
<p>We thought we’d have more power available at this time of day than we actually do.</p>
</blockquote>
<hr>
<section id="why-this-matters-beyond-one-heat-wave" class="level3">
<h3 class="anchored" data-anchor-id="why-this-matters-beyond-one-heat-wave">Why This Matters Beyond One Heat Wave</h3>
<p>The marine layer bias wasn’t the <em>primary</em> cause of California’s blackouts-CAISO’s analysis shows it was a perfect storm. But the model bias was one link in a chain of assumptions that created vulnerability. It also shows how invisible these assumptions become once they leave the modeling world.</p>
</section>
</section>
<section id="connections-to-eds-242-course-concepts" class="level2">
<h2 class="anchored" data-anchor-id="connections-to-eds-242-course-concepts">Connections to EDS 242 Course Concepts</h2>
<p>This case demonstrates several types of bias we studied:</p>
<p><strong>Model specification bias</strong>: systematic errors in how marine layer physics are represented in the model structure itself (Konno et al.; Lecture 2’s Catalogue of Bias).</p>
<p><strong>Historical bias</strong>: The LOCA downscaling method assumes future relationships will match past patterns, which breaks down under climate change. (Lecture 2)</p>
<p><strong>Reporting bias</strong> model limitations were documented in technical reports but didn’t get reported in the summary documents that informed billion-dollar decisions. This is what Cori Lopazanski called the “problem of too much information” in her guest lecture-scientists have caveats, but those get lost in translation to policy.(Lecture 2, Cori Lopazanski guest lecturer)</p>
<p><strong>Data invisibility</strong> By 2020, the connection back to 2015 cloud modeling choices was completely invisible to everyone affected. As we learned in Lecture 9, these are “known unknowns” that become “unknown unknowns” as they propagate through decision systems. (Lecture 1, Barrowman reading)</p>
<p>The bias propagates through what Olteanu et al.&nbsp;call “decision chains,” compounding at each step. This connects to <strong>Environmental Data Justice</strong> themes from Lecture 8-who gets hurt when data systems fail? Not the modelers or grid planners, but families who lost groceries, elderly folks without AC, people on medical equipment.</p>
<hr>
</section>
<section id="the-technical-challenge" class="level2">
<h2 class="anchored" data-anchor-id="the-technical-challenge">The Technical Challenge</h2>
<p>Climate modelers know about these biases. Marine boundary layer clouds remain one of the largest sources of uncertainty in climate models <span class="citation" data-cites="klein2017">[@klein2017]</span>. Understanding different types of bias is crucial for interpreting these limitations <span class="citation" data-cites="konno2024bias">[@konno2024bias]</span>.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">The Problem</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Downscaling</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">Bias Correction</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Broader Pattern</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<p>Marine layer physics operates at scales GCMs can’t resolve. Coastal fog and low stratus depend on boundary layer processes, cloud microphysics, and topographic features at sub-grid scales <span class="citation" data-cites="klein2017">[@klein2017]</span>.</p>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p><strong>Statistical downscaling (LOCA):</strong> Uses historical relationships but assumes future will match past patterns-questionable under climate change <span class="citation" data-cites="ekstrom2015appraisal">[@ekstrom2015appraisal]</span>.</p>
<p><strong>Dynamical downscaling (WRF):</strong> Simulates physics at finer scale but inherits GCM biases.</p>
<p><strong>California’s approach (LOCA2):</strong> Combines both methods <span class="citation" data-cites="pierce2024downscaling">[@pierce2024downscaling]</span>. A 2024 technical report notes improvements but still recommends changing planning assumptions to account for limitations.</p>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<p>Different bias correction methods can change answers significantly <span class="citation" data-cites="maraun2016bias">[@maraun2016bias]</span>. The choice of method can shift your projection as much as the climate signal itself <span class="citation" data-cites="ekstrom2015appraisal">[@ekstrom2015appraisal]</span>.</p>
<p>Common approaches: quantile mapping (assumes stationary bias), delta methods (assumes correct trends), deep learning (overfitting risk), and ensemble averaging (masks uncertainties).</p>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<p>This pattern shows up across environmental data science-flood risk, crop yields, hydropower, fire risk. Model biases propagate through decision chains <span class="citation" data-cites="olteanu2019social">[@olteanu2019social]</span>. The projections look authoritative, but limitations don’t make it into summary documents as often as needed.</p>
</div>
</div>
</div>
<hr>
</section>
<section id="what-we-can-do" class="level2">
<h2 class="anchored" data-anchor-id="what-we-can-do">What We Can Do</h2>
<p>Climate models are our best tools for understanding future conditions, but we need to work smarter with their limitations.</p>
<section id="bake-uncertainty-into-decisions" class="level4">
<h4 class="anchored" data-anchor-id="bake-uncertainty-into-decisions">1. Bake Uncertainty Into Decisions</h4>
<p>Design systems that adapt if reality diverges. For California’s grid: maintain flexible generation capacity, plan for higher evening peaks than models predict.</p>
</section>
<section id="cross-validate-with-multiple-methods" class="level4">
<h4 class="anchored" data-anchor-id="cross-validate-with-multiple-methods">2. Cross-Validate With Multiple Methods</h4>
<ul>
<li>Historical records (past heat wave outcomes)</li>
<li>Empirical relationships (actual solar vs.&nbsp;weather)</li>
<li>Expert opinions (grid operator observations)</li>
<li>Stress testing (20% lower generation scenarios)</li>
</ul>
</section>
<section id="create-feedback-loops" class="level4">
<h4 class="anchored" data-anchor-id="create-feedback-loops">3. Create Feedback Loops</h4>
<p>Compare actual vs.&nbsp;projected generation. Feed learning back into models and planning. California is doing this now, but earlier detection would have helped.</p>
</section>
<section id="invest-in-the-unglamorous-work" class="level4">
<h4 class="anchored" data-anchor-id="invest-in-the-unglamorous-work">4. Invest in the Unglamorous Work</h4>
<p>Bias correction and regional model improvement don’t make headlines, but we under-invest badly. Need better observations, higher resolution models, long-term monitoring, transparent documentation of biases.</p>
</section>
<section id="train-decision-makers" class="level4">
<h4 class="anchored" data-anchor-id="train-decision-makers">5. Train Decision-Makers</h4>
<p>Engineers, planners, policy-makers need to understand what models can and can’t tell them. A projection isn’t a prediction. Uncertainty is information. Limitations should be upfront, not buried in appendices.</p>
<hr>
</section>
</section>
<section id="why-i-think-about-this" class="level2">
<h2 class="anchored" data-anchor-id="why-i-think-about-this">Why I Think About This</h2>
<p>For my family, the 2020 blackouts were uncomfortable but manageable. For others-people dependent on medical equipment, elderly folks without AC-the stakes were higher.</p>
<p>California’s grid planning documents still show tight supply margins during evening peaks. The marine layer isn’t going anywhere, though research suggests California coastal fog has declined 33% since 1950 <span class="citation" data-cites="johnstone2010fog">[@johnstone2010fog]</span>, with uncertain causes and future trajectory <span class="citation" data-cites="torregrosa2014fog">[@torregrosa2014fog]</span>.</p>
<p>What stays with me is how easily biases slip through. Smart people, rigorous methods, peer-reviewed projections, and still, systematic errors propagate until systems fail under stress.</p>
<p>I see this pattern everywhere in environmental data science. Models of crop yields, water availability, species distributions, fire risk-all rest on climate projections with known limitations. Those limitations exist, they’re documented in technical reports, but they don’t always make it into decision documents.</p>
<p>We can build better decision processes, ones that explicitly account for uncertainty, validate projections against reality, and adapt when mismatches appear. That starts with recognizing that environmental data always carries the fingerprints of choices and assumptions baked into its creation <span class="citation" data-cites="walker2018practicing">[@walker2018practicing]</span>.</p>
<p>The point isn’t to chase perfect models. It’s to build systems that don’t fall apart when the models miss something.</p>
<hr>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<div id="refs">

</div>


</section>

 ]]></description>
  <category>MEDS</category>
  <category>Climate Science</category>
  <category>Data Science</category>
  <guid>https://rellimylime.github.io/posts/eds242-final/</guid>
  <pubDate>Fri, 06 Dec 2024 08:00:00 GMT</pubDate>
  <media:content url="https://rellimylime.github.io/images/posts/eds242-final/eds242-final-cover2.jpeg" medium="image" type="image/jpeg"/>
</item>
</channel>
</rss>
