<?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>Marcin Kierczak</title>
<link>https://kierczak.net/blog.html</link>
<atom:link href="https://kierczak.net/blog.xml" rel="self" type="application/rss+xml"/>
<description>Marcin Kierczak, PhD — Training and Industry Outreach Coordinator and Data Scientist at NBIS and Associate Professor of Bioinformatics / SciLifeLab, Uppsala, Sweden.</description>
<generator>quarto-1.9.38</generator>
<lastBuildDate>Thu, 15 Oct 2026 22:00:00 GMT</lastBuildDate>
<item>
  <title>Statistics from the beginning: location and spread</title>
  <dc:creator>Marcin Kierczak</dc:creator>
  <link>https://kierczak.net/posts/stats-1-location-spread/</link>
  <description><![CDATA[ 





<div class="series-tag">
<p>Series · <em>Statistics from the beginning</em> · Part 1</p>
</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">
<span class="screen-reader-only">Note</span>About this series
</div>
</div>
<div class="callout-body-container callout-body">
<p>This series is adapted from lecture notes I wrote in 2017, <em>Introduction to Statistical Tests</em> (<a href="../../assets/docs/statests.pdf">original PDF</a>). I am turning them into short posts with fresh code and figures, starting from the very beginning. This first part covers <strong>location and spread</strong>. Later parts cover covariance and correlation, and then the tests themselves.</p>
</div>
</div>
<section id="statistics-and-a-statistic" class="level2">
<h2 class="anchored" data-anchor-id="statistics-and-a-statistic">Statistics, and <em>a</em> statistic</h2>
<p>Statistics is a branch of mathematics concerned with the collection, analysis, interpretation and presentation of data. A very similar word, <em>a statistic</em>, means something narrower: a single number that summarises some feature of a data sample, produced by a statistical procedure. The sample mean and the sample standard deviation are statistics.</p>
<p>One distinction runs through everything that follows:</p>
<ul>
<li><strong>distributions</strong> (populations) are described by <strong>parameters</strong>,</li>
<li><strong>samples</strong> are described by <strong>statistics</strong>.</li>
</ul>
<p>We almost never see the whole population, so we use statistics computed from a sample to estimate the parameters of the distribution it came from. Every distribution can be described by two kinds of parameters: <strong>location</strong> parameters (where are the data?) and <strong>spread</strong> (dispersion) parameters (how widely is it scattered?).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A shared look for the figures in this series</span></span>
<span id="cb1-4">accent <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;">"#8c2f39"</span>; gold <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;">"#b08d4a"</span>; ink <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;">"#5d6070"</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(</span>
<span id="cb1-6">  <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="cb1-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FFFDF8"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb1-8">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-9">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#EEEAE0"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>),</span>
<span id="cb1-10">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot"</span>)</span>
<span id="cb1-11">)</span></code></pre></div></div>
</div>
</section>
<section id="location-where-are-the-data" class="level2">
<h2 class="anchored" data-anchor-id="location-where-are-the-data">Location: where are the data?</h2>
<section id="mean" class="level3">
<h3 class="anchored" data-anchor-id="mean">Mean</h3>
<p>The mean measures the central tendency of the data: the sum of all values divided by the number of observations,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmu%20=%20%5Cfrac%7B%5Csum%20x%7D%7BN%7D.%0A"></p>
<p>The population mean is written <img src="https://latex.codecogs.com/png.latex?%5Cmu"> and the sample mean <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D">; <img src="https://latex.codecogs.com/png.latex?N"> is the number of observations.</p>
<p>The mean has a weakness: it uses <em>every</em> value, so it is pulled around by outliers and by skewed distributions. Five sheep weighing between 52 and 57 kg have a mean of 54.8 kg. Add one animal recorded as 120 kg (perhaps a typo) and see what happens:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">sheep <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">54</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">55</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">56</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">57</span>)</span>
<span id="cb2-2"><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;">mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(sheep), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">median =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(sheep))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  mean median 
  54.8   55.0 </code></pre>
</div>
<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">sheep_typo <span class="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>(sheep, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>)</span>
<span id="cb4-2"><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;">mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(sheep_typo), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">median =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(sheep_typo))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>    mean   median 
65.66667 55.50000 </code></pre>
</div>
</div>
<p>The mean jumps by more than ten kilos; the median barely moves. People handle this problem with variants of the mean, such as the <em>weighted mean</em> or the <em>Winsorised mean</em>, which tames extreme observations, but we will not go into those here.</p>
</section>
<section id="median" class="level3">
<h3 class="anchored" data-anchor-id="median">Median</h3>
<p>The median is another location parameter. Unlike the mean it does not use all the observations: it is simply the middle value of the ordered data. With an odd number of data points it is easy to find. With an even number, it is the mean of the two middle values.</p>
<p>Comparing the mean with the median tells us something about <strong>skewness</strong>:</p>
<ul>
<li>for a perfectly symmetric distribution, mean and median are equal,</li>
<li>for a <strong>right-skewed</strong> distribution (a long tail to the right), the mean is <em>greater</em> than the median, because the tail drags the mean along,</li>
<li>for a <strong>left-skewed</strong> distribution (a long tail to the left), the mean is <em>less</em> than the median.</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><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="cb6-2">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<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;">rgamma</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>))</span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(value)) <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;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</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;">"#d8cfba"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FFFDF8"</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;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> gold, <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-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> accent, <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-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value) <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.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb6-9">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> gold) <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;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value) <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.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb6-11">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> accent) <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;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</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;">"A right-skewed sample: mean &gt; median"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://kierczak.net/posts/stats-1-location-spread/index_files/figure-html/skew-1.png" class="img-fluid figure-img" alt="Histogram of a right-skewed sample with the median line to the left of the mean line." width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="spread-how-scattered-are-the-data" class="level2">
<h2 class="anchored" data-anchor-id="spread-how-scattered-are-the-data">Spread: how scattered are the data?</h2>
<section id="variance" class="level3">
<h3 class="anchored" data-anchor-id="variance">Variance</h3>
<p>Variance measures dispersion around the mean: the more spread out the observations, the larger the variance. The population variance is <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2"> and the sample variance is <img src="https://latex.codecogs.com/png.latex?s%5E2">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Csigma%5E2%20=%20%5Cfrac%7B%5Csum%20(x%20-%20%5Cmu)%5E2%7D%7BN%7D%0A%5Cqquad%5Cqquad%0As%5E2%20=%20%5Cfrac%7B%5Csum%20(x%20-%20%5Cbar%7Bx%7D)%5E2%7D%7Bn%20-%201%7D.%0A"></p>
<p>Why divide by <img src="https://latex.codecogs.com/png.latex?n%20-%201"> rather than <img src="https://latex.codecogs.com/png.latex?n">? We want <img src="https://latex.codecogs.com/png.latex?s%5E2"> to be as good an estimate of <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2"> as possible. Think of estimating the variance of weights in a herd of 2,000 sheep from a sample of 200. It is quite likely that our sample misses the few very light and very heavy animals, which would otherwise have contributed large <img src="https://latex.codecogs.com/png.latex?(x%20-%20%5Cbar%7Bx%7D)%5E2"> terms. There is a second, more precise way to see it: we measure the deviations from <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D">, a number computed <em>from the same sample</em>, and the sample is always closer to its own mean than to the true mean <img src="https://latex.codecogs.com/png.latex?%5Cmu">. Dividing by <img src="https://latex.codecogs.com/png.latex?n"> would therefore underestimate the variance systematically; dividing by <img src="https://latex.codecogs.com/png.latex?n%20-%201"> corrects it exactly.</p>
<p>Let’s check this with a simulation. We create a herd of 2,000 sheep, draw many samples of each size, and compare the two estimators with the true population variance.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="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;">2017</span>)</span>
<span id="cb7-2">herd <span class="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>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, <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;">55</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;">4</span>)</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># population variance: divide by N</span></span>
<span id="cb7-5">pop_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>((x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x)</span>
<span id="cb7-6">true_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pop_var</span>(herd)</span>
<span id="cb7-7"></span>
<span id="cb7-8">compare_var <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>(pop, n_draws, sample_size) {</span>
<span id="cb7-9">  draws <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replicate</span>(n_draws, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(pop, sample_size))   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># one sample per column</span></span>
<span id="cb7-10">  <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;">corrected   =</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;">apply</span>(draws, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, var)),             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># divides by n - 1</span></span>
<span id="cb7-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">uncorrected =</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;">apply</span>(draws, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, pop_var)))         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># divides by n</span></span>
<span id="cb7-12">}</span>
<span id="cb7-13"></span>
<span id="cb7-14">sizes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span 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="cb7-15">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;">t</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(sizes, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compare_var</span>(herd, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, n)))</span>
<span id="cb7-16">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;">data.frame</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;">rep</span>(sizes, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb7-17">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(sim[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"uncorrected"</span>], sim[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"corrected"</span>]),</span>
<span id="cb7-18">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">version =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"divide by n"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"divide by n − 1"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(sizes)))</span></code></pre></div></div>
</div>
<div class="cell">
<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>(sim, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(n, estimate, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> version)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> true_var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> ink, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dotted"</span>) <span class="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;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</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_colour_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"divide by n"</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;">"#2b2117"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"divide by n − 1"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> accent)) <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;">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;">"Sample size"</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;">"Average estimated variance"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb8-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The effect of using n − 1 in the denominator"</span>,</span>
<span id="cb8-7">       <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;">"Dotted line: true population variance, averaged over 1,000 samples per size"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://kierczak.net/posts/stats-1-location-spread/index_files/figure-html/variance-plot-1.png" class="img-fluid figure-img" alt="Average variance estimates against sample size. Dividing by n falls below the true variance for small samples, dividing by n minus 1 stays on it." width="672"></p>
</figure>
</div>
</div>
</div>
<p>For small samples, dividing by <img src="https://latex.codecogs.com/png.latex?n"> clearly underestimates the variance; for large samples the choice hardly matters, because <img src="https://latex.codecogs.com/png.latex?n"> and <img src="https://latex.codecogs.com/png.latex?n%20-%201"> are nearly the same number.</p>
<p>Variance can never be negative, since we square the deviations. A side effect of that squaring is that variance is expressed in <em>squared</em> units (kilograms squared for weights). That is why we usually report its square root instead.</p>
</section>
<section id="standard-deviation" class="level3">
<h3 class="anchored" data-anchor-id="standard-deviation">Standard deviation</h3>
<p>The standard deviation (sd, SD, std. dev.) is the square root of the variance, written <img src="https://latex.codecogs.com/png.latex?%5Csigma"> for a population and <img src="https://latex.codecogs.com/png.latex?s"> for a sample. It is in the same units as the data, which makes it far easier to interpret: for our sheep, an sd of about 4 means that a typical animal is within a few kilograms of the herd’s mean weight.</p>
</section>
<section id="putting-different-scales-on-the-same-footing" class="level3">
<h3 class="anchored" data-anchor-id="putting-different-scales-on-the-same-footing">Putting different scales on the same footing</h3>
<p>If two traits are measured on different scales (say, body weight in kilograms and fleece length in centimetres), their variances cannot be compared directly: the numbers depend on the units. The usual remedy is <strong>standardisation</strong>: we transform each variable so that it has mean 0 and standard deviation 1, i.e.&nbsp;it follows the <em>standard normal</em> scale <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BN%7D(0,%201)">. The standardised values are called <strong>z-scores</strong>; each one tells us how many standard deviations an observation lies from the mean:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Az%20=%20%5Cfrac%7Bx%20-%20%5Cbar%7Bx%7D%7D%7Bs%7D.%0A"></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><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;">7</span>)</span>
<span id="cb9-2">herd2 <span class="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;">weight_kg =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</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;">55</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb9-3">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fleece_cm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</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;">11</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>))</span>
<span id="cb9-4"></span>
<span id="cb9-5">ewe <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weight_kg =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">58</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fleece_cm =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">13.4</span>)</span>
<span id="cb9-6">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (ewe <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;">colMeans</span>(herd2)) <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;">sapply</span>(herd2, sd)</span>
<span id="cb9-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(z, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>weight_kg fleece_cm 
     0.65      1.56 </code></pre>
</div>
</div>
<p>On the raw scale, 58 kg and 13.4 cm cannot be compared. As z-scores they can: this hypothetical ewe is about two-thirds of a standard deviation heavier than average, but more than one and a half standard deviations above average in fleece length. Relative to the rest of the herd, her fleece is the more unusual feature.</p>
</section>
</section>
<section id="whats-next" class="level2">
<h2 class="anchored" data-anchor-id="whats-next">What’s next</h2>
<p>We now have the vocabulary to describe a single variable: where it sits and how much it varies. In the next part we look at <strong>two variables at once</strong>, with covariance and correlation. Spoiler: correlation is simply covariance after standardisation.</p>
<div class="series-nav">
<p><a href="../../blog.html#category=series" class="btn-pill">All parts in this series</a></p>
<p>Next in the series: covariance and correlation.</p>
</div>


</section>

 ]]></description>
  <category>statistics</category>
  <category>R</category>
  <category>teaching</category>
  <category>series</category>
  <guid>https://kierczak.net/posts/stats-1-location-spread/</guid>
  <pubDate>Thu, 15 Oct 2026 22:00:00 GMT</pubDate>
  <media:content url="https://kierczak.net/posts/stats-1-location-spread/preview.png" medium="image" type="image/png" height="103" width="144"/>
</item>
<item>
  <title>Pest in R using R6 classes</title>
  <dc:creator>Marcin Kierczak</dc:creator>
  <link>https://kierczak.net/posts/plague-sim/</link>
  <description><![CDATA[ 





<p><img src="https://kierczak.net/posts/plague-sim/assets/plague_doctor_crow3.webp" class="round img-fluid" style="float: left; margin: 0 1.2em .6em 0;" width="300"> Hello! In this post, I will show you how one can implement an agent-based simulation in R using object-oriented programming paradigm (OOP) and R6 classes.</p>
<p>This simulation is working yet it still has to be polished. It has been implemented as a live coding demo during “OOP in R” session at <a href="https://nbisweden.github.io/raukr-2024/">RaukR 2024</a>.</p>
<p>Our goal is to simulate epidemics dynamics on an island (an isolated world). Let us establish some simple rules:</p>
<ul>
<li>we have a N x N squares large rectangular world (think of each square as of, e.g.&nbsp;a building)</li>
<li>in each square dwell individuals that can be: healthy, immune (after recovering or vaccination), infected (= sick) or, unfortunately, also dead</li>
<li>in total, we have P individuals randomly distributed in our world’s squares</li>
<li>at every time step/round of our simulation individuals move and update their state</li>
<li>probability of becoming infected is proportional to the number of already infected individuals in the same square</li>
<li>probabilities of the remaining state transitions are given by simulation parameters</li>
<li>we continue the simulation for T rounds</li>
</ul>
<section id="an-individual" class="level2">
<h2 class="anchored" data-anchor-id="an-individual">An individual</h2>
<p>Let’s begin by formulating an R6 class that represents an individual (= agent):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(R6)</span>
<span id="cb1-2"></span>
<span id="cb1-3">Agent <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">R6Class</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Agent"</span>,</span>
<span id="cb1-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">public =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb1-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">character</span>(),</span>
<span id="cb1-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">initialize =</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;">state =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"healthy"</span>) {</span>
<span id="cb1-7">      self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> state</span>
<span id="cb1-8">    },</span>
<span id="cb1-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">update_state =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(sick_prob, recovery_prob, death_prob) {</span>
<span id="cb1-10">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If agent is healthy it can either stay healthy or get infected</span></span>
<span id="cb1-11">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state <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;">"healthy"</span>) {</span>
<span id="cb1-12">        new_state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"healthy"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sick"</span>), </span>
<span id="cb1-13">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, </span>
<span id="cb1-14">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>sick_prob, sick_prob))</span>
<span id="cb1-15">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A sick agent can continue being sick, recover and become immune or die</span></span>
<span id="cb1-16">      } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state <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;">"sick"</span>) { </span>
<span id="cb1-17">        new_state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sick"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"immune"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dead"</span>), </span>
<span id="cb1-18">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, </span>
<span id="cb1-19">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(recovery_prob <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> death_prob)), recovery_prob, death_prob))</span>
<span id="cb1-20">      </span>
<span id="cb1-21">      } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb1-22">        new_state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Immune and dead states do not change</span></span>
<span id="cb1-23">      }</span>
<span id="cb1-24">      self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> new_state  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Update state</span></span>
<span id="cb1-25">    }</span>
<span id="cb1-26">  )</span>
<span id="cb1-27">)</span></code></pre></div></div>
</div>
<p>Observe that:</p>
<ul>
<li>The default state of a new agent is “healthy”</li>
<li>We use <code>sample</code> and its <code>prob</code> argument to sample agent’s fate according to provided probabilities</li>
<li>“immune” and “dead” states do not change</li>
<li>when an individual is sick it can either continue to be sick, recover and become immune or die</li>
<li>we are using the fact that probabilities of mutually exclusive elementary events sum to 1</li>
</ul>
</section>
<section id="simulation-world" class="level2">
<h2 class="anchored" data-anchor-id="simulation-world">Simulation world</h2>
<p><img src="https://kierczak.net/posts/plague-sim/assets/world.webp" class="round img-fluid" style="float: left; margin: 0 1.2em .6em 0;" alt="medieval-style cartoon map of the world" width="300"> Now it is time to create a class for our world which we can next populate with our agents. We need the following:</p>
<ul>
<li>variable <code>size</code> where we store information about our world size N,</li>
<li>variable <code>world</code> which will store our representation of the world. Since the world is a square, we can use R base data structure – <em>matrix</em> and each cell in the matrix will contain a <em>list</em> of our individuals that dwell there – they are of class <code>Agent</code> that we have just created</li>
<li><em>method</em> <code>initialize</code> that creates the world</li>
<li><em>method</em> <code>add_agent</code> to populate our world</li>
<li><em>method</em> <code>move_agent</code> to move agents around at each simulation step</li>
<li><em>method</em> <code>update_states</code> that updates states of agents</li>
<li><em>method</em> <code>get_counts</code> that will record current state of the world (count of healthy, sick etc.)</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">World <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">R6Class</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"World"</span>,</span>
<span id="cb2-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">public =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb2-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb2-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">world =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb2-5">    </span>
<span id="cb2-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">initialize =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(size) {</span>
<span id="cb2-7">      self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> size</span>
<span id="cb2-8">      self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>world <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<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>, size <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> size), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> size)</span>
<span id="cb2-9">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>size) {</span>
<span id="cb2-10">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="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>size) {</span>
<span id="cb2-11">          self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>world[[i, j]] <span class="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="cb2-12">        }</span>
<span id="cb2-13">      }</span>
<span id="cb2-14">    },</span>
<span id="cb2-15">    </span>
<span id="cb2-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add_agent =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(row, col, agent) {</span>
<span id="cb2-17">      self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>world[[row, col]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>world[[row, col]], <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(agent))</span>
<span id="cb2-18">    },</span>
<span id="cb2-19">    </span>
<span id="cb2-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">move_agents =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {</span>
<span id="cb2-21">      new_world <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<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>, self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size)</span>
<span id="cb2-22">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size) {</span>
<span id="cb2-23">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="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>self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size) {</span>
<span id="cb2-24">          agents <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>world[[i, j]]</span>
<span id="cb2-25">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (agent <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> agents) {</span>
<span id="cb2-26">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (agent<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state <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;">"dead"</span>) { <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># dead do not move</span></span>
<span id="cb2-27">              move <span class="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>(<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;">mean =</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;">sd =</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;"># some arbitrary motility</span></span>
<span id="cb2-28">              new_row <span class="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;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])), self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size)</span>
<span id="cb2-29">              new_col <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(j <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])), self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size)</span>
<span id="cb2-30">              new_world[[new_row, new_col]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(new_world[[new_row, new_col]], <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(agent))</span>
<span id="cb2-31">            } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb2-32">              new_world[[i, j]] <span class="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>(new_world[[i, j]], <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(agent))</span>
<span id="cb2-33">            }</span>
<span id="cb2-34">          }</span>
<span id="cb2-35">        }</span>
<span id="cb2-36">      }</span>
<span id="cb2-37">      self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>world <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> new_world</span>
<span id="cb2-38">    },</span>
<span id="cb2-39">    </span>
<span id="cb2-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">update_states =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(beta, recovery_prob, death_prob) {</span>
<span id="cb2-41">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Iterate over all grid cells in the world and </span></span>
<span id="cb2-42">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># determine infection probability based on</span></span>
<span id="cb2-43">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the number of already sick individuals in that grid cell.</span></span>
<span id="cb2-44">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size) {</span>
<span id="cb2-45">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="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>self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size) {</span>
<span id="cb2-46">          agents <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>world[[i, j]]</span>
<span id="cb2-47">          num_sick <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(agents, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(agent) agent<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state <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;">"sick"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logical</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)))</span>
<span id="cb2-48">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Now, probability of becoming sick is naively proportional </span></span>
<span id="cb2-49">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># to the percentage of already sick individuals in the cell</span></span>
<span id="cb2-50">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># where beta is the transmission probability</span></span>
<span id="cb2-51">          sick_prob <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ((<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> beta) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> num_sick)</span>
<span id="cb2-52">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (agent <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> agents) {</span>
<span id="cb2-53">            agent<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;">update_state</span>(sick_prob, recovery_prob, death_prob)</span>
<span id="cb2-54">          }</span>
<span id="cb2-55">        }</span>
<span id="cb2-56">      }</span>
<span id="cb2-57">    },</span>
<span id="cb2-58">    </span>
<span id="cb2-59">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">get_counts =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {</span>
<span id="cb2-60">      counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb2-61">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integer</span>(),</span>
<span id="cb2-62">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integer</span>(),</span>
<span id="cb2-63">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">healthy =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integer</span>(),</span>
<span id="cb2-64">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">immune =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integer</span>(),</span>
<span id="cb2-65">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sick =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integer</span>(),</span>
<span id="cb2-66">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dead =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integer</span>()</span>
<span id="cb2-67">      )</span>
<span id="cb2-68">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size) {</span>
<span id="cb2-69">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="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>self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size) {</span>
<span id="cb2-70">          agents <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>world[[i, j]]</span>
<span id="cb2-71">          counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(counts, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb2-72">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row =</span> i,</span>
<span id="cb2-73">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> j,</span>
<span id="cb2-74">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">healthy =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(agents, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(agent) agent<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state <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;">"healthy"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logical</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))),</span>
<span id="cb2-75">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">immune =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(agents, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(agent) agent<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state <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;">"immune"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logical</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))),</span>
<span id="cb2-76">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sick =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(agents, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(agent) agent<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state <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;">"sick"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logical</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))),</span>
<span id="cb2-77">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dead =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(agents, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(agent) agent<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>state <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;">"dead"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logical</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)))</span>
<span id="cb2-78">          ))</span>
<span id="cb2-79">        }</span>
<span id="cb2-80">      }</span>
<span id="cb2-81">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(counts)</span>
<span id="cb2-82">    }</span>
<span id="cb2-83">  )</span>
<span id="cb2-84">)</span></code></pre></div></div>
</div>
<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>One line of code is probably a bit non-intuitive:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">sick_prob <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ((<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> beta) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> num_sick)</span></code></pre></div></div>
</div>
<p>Here, parameter <code>beta</code> (<img src="https://latex.codecogs.com/png.latex?%5Cbeta">) is the so-called <em>transmission probability</em> that is specific for each disease agent. Now, <img src="https://latex.codecogs.com/png.latex?P(%7Bnot%5C_sick%7D)%20=%20(1%20-%20%5Cbeta)%5E%7BN_%7Bsick%7D%7D"> is the probability of NOT catching the disease from any of <img src="https://latex.codecogs.com/png.latex?N_%7Bsick%7D"> neighbors. Finally, the chance of becoming sick in a given cell is <img src="https://latex.codecogs.com/png.latex?P_%7Bsick%7D%20=%201%20-%20P(%7Bnot%5C_sick%7D)%20=%201%20-%20(1%20-%20%5Cbeta)%5E%7BN_%7Bsick%7D%7D"></p>
</div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://kierczak.net/posts/plague-sim/assets/plague_doctor_crow.webp" class="round img-fluid quarto-figure quarto-figure-center figure-img" alt="Plague doctor crow." width="300"></p>
</figure>
</div>
</section>
<section id="auxiliary-functions" class="level2">
<h2 class="anchored" data-anchor-id="auxiliary-functions">Auxiliary functions</h2>
<p>I have written a function that visualizes states of our world as the simulation goes on using <code>ggplot2</code> and <code>gganimate</code> packages. Here is the code:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">visualize_simulation <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>(results) {</span>
<span id="cb4-2">  p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(results, <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> col, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> row)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> sick)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradient</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">low =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">high =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-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;">"Disease Spread Simulation"</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;">"X"</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;">"Y"</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;">"Sick Individuals"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transition_states</span>(step, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">transition_length =</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;">state_length =</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="cb4-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ease_aes</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'linear'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Disease Spread Simulation - Generation {closest_state}'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">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;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>))</span>
<span id="cb4-11">  </span>
<span id="cb4-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">animate</span>(p, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nframes =</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>(results<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>step)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fps =</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;">renderer =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gifski_renderer</span>())</span>
<span id="cb4-13">}</span></code></pre></div></div>
</div>
</section>
<section id="running-simulation" class="level2">
<h2 class="anchored" data-anchor-id="running-simulation">Running simulation</h2>
<p>Now, we need a function that will enable us to run the simulation:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gganimate)</span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(progress)</span>
<span id="cb5-4"></span>
<span id="cb5-5">run_simulation <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>(size, num_steps, initial_healthy, initial_immune, initial_sick, beta, recovery_prob, death_prob) {</span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(recovery_prob <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> death_prob <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># some simplistic input validation</span></span>
<span id="cb5-7">  pb <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> progress_bar<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;">new</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total =</span> num_steps <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-8">  </span>
<span id="cb5-9">  world <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> World<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;">new</span>(size)</span>
<span id="cb5-10">  </span>
<span id="cb5-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialize agents</span></span>
<span id="cb5-12">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (initial_healthy <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="cb5-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>initial_healthy) {</span>
<span id="cb5-14">      row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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>size, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-15">      col <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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>size, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-16">      world<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;">add_agent</span>(row, col, Agent<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;">new</span>())</span>
<span id="cb5-17">    }</span>
<span id="cb5-18">  }</span>
<span id="cb5-19">  </span>
<span id="cb5-20">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Introduce immune and sick agents</span></span>
<span id="cb5-21">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (initial_immune <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="cb5-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>initial_immune) {</span>
<span id="cb5-23">      row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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>size, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-24">      col <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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>size, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-25">      world<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;">add_agent</span>(row, col, Agent<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;">new</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"immune"</span>))</span>
<span id="cb5-26">    }</span>
<span id="cb5-27">  }</span>
<span id="cb5-28">  </span>
<span id="cb5-29">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (initial_sick <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="cb5-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>initial_sick) {</span>
<span id="cb5-31">      row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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>size, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-32">      col <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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>size, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-33">      world<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;">add_agent</span>(row, col, Agent<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;">new</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sick"</span>))</span>
<span id="cb5-34">    }</span>
<span id="cb5-35">  }</span>
<span id="cb5-36">  </span>
<span id="cb5-37">  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="cb5-38">  </span>
<span id="cb5-39">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save the initial state of the world</span></span>
<span id="cb5-40">  counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> world<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;">get_counts</span>()</span>
<span id="cb5-41">  counts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>step <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb5-42">  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> counts</span>
<span id="cb5-43">  </span>
<span id="cb5-44">  </span>
<span id="cb5-45">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate one generation</span></span>
<span id="cb5-46">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (step <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>num_steps <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb5-47">    world<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;">update_states</span>(beta, recovery_prob, death_prob)</span>
<span id="cb5-48">    world<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;">move_agents</span>()</span>
<span id="cb5-49">    counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> world<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;">get_counts</span>()</span>
<span id="cb5-50">    counts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>step <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> step</span>
<span id="cb5-51">    results[[step]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> counts</span>
<span id="cb5-52">    pb<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;">tick</span>()</span>
<span id="cb5-53">  }</span>
<span id="cb5-54">  </span>
<span id="cb5-55">  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;">do.call</span>(rbind, results)</span>
<span id="cb5-56">  </span>
<span id="cb5-57">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(results)</span>
<span id="cb5-58">}</span></code></pre></div></div>
</div>
<p>Finally, we can run our simulation:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ lubridate 1.9.5     ✔ tibble    3.3.1
✔ purrr     1.2.2     ✔ tidyr     1.3.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (&lt;http://conflicted.r-lib.org/&gt;) to force all conflicts to become errors</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Parameters</span></span>
<span id="cb8-2">size <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>                    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># World size</span></span>
<span id="cb8-3">num_steps <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;">50</span>               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Number of generations</span></span>
<span id="cb8-4">initial_healthy <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;">998</span></span>
<span id="cb8-5">initial_immune <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb8-6">initial_sick <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;">2</span></span>
<span id="cb8-7">beta <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;"># Transmission probability</span></span>
<span id="cb8-8">recovery_prob <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.1</span></span>
<span id="cb8-9">death_prob <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.05</span></span>
<span id="cb8-10"></span>
<span id="cb8-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run the simulation</span></span>
<span id="cb8-12">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;">run_simulation</span>(size, num_steps, initial_healthy, initial_immune, initial_sick, beta, recovery_prob, death_prob)</span>
<span id="cb8-13"></span>
<span id="cb8-14">results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(step) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-15">  <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;">n_sick =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(sick), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_healthy =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(healthy), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_immune =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(immune), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_dead =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(dead)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n_"</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;">'measure'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-17">  <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> step, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>measure)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://kierczak.net/posts/plague-sim/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>


</section>

 ]]></description>
  <category>R</category>
  <category>simulation</category>
  <category>teaching</category>
  <guid>https://kierczak.net/posts/plague-sim/</guid>
  <pubDate>Wed, 19 Jun 2024 22:00:00 GMT</pubDate>
  <media:content url="https://kierczak.net/posts/plague-sim/assets/plague_doctor_crow.webp" medium="image" type="image/webp"/>
</item>
<item>
  <title>Making more glamorous graphs in ggplot2</title>
  <dc:creator>Marcin Kierczak</dc:creator>
  <link>https://kierczak.net/posts/glamorous-ggplot2/</link>
  <description><![CDATA[ 





<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>Updated in 2026
</div>
</div>
<div class="callout-body-container callout-body">
<p>Lightly edited from a 2020 post. The plot is re-rendered with current packages (<code>size</code> became <code>linewidth</code> in ggplot2). The data stop in February 2020, and the scraper is shown but not run, since the repository pages it parsed have changed.</p>
</div>
</div>
<p>Inspired by <a href="https://www.williamrchase.com">William R. Chase’s</a> rstudio::conf 2020 talk <a href="https://www.williamrchase.com/slides/assets/player/KeynoteDHTMLPlayer.html#0">“The Glamour of Graphics”</a>, I decided to spruce up an old plot. It is the one I show my students every year to motivate them to learn R: the number of R packages available in various repositories over the years.</p>
<p>From William’s talk I took these lessons:</p>
<ol type="1">
<li>Left-align titles.</li>
<li>Use non-white backgrounds.</li>
<li>Skip axis labels if not necessary.</li>
<li>Use as few grid lines as possible.</li>
<li>Capture the legend in the title.</li>
</ol>
<p>There is much more in the talk that I do not implement here, for example using professional fonts.</p>
<section id="libraries" class="level2">
<h2 class="anchored" data-anchor-id="libraries">Libraries</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(curl)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggrepel)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(scales)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggtext)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(glue)</span></code></pre></div></div>
</div>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>I have an ugly function that scrapes CRAN, R-Forge, Bioconductor and, later, also GitHub to count R packages (GitHub is a bit problematic, but hopefully a good proxy). The function repeats code that I should have encapsulated and parametrised. No one is perfect.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">interrogate_repos <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="cb2-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CRAN</span></span>
<span id="cb2-3">  cran_con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> curl<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;">curl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://cran.r-project.org/web/packages/index.html'</span>)</span>
<span id="cb2-4">  cran <span class="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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readLines</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">con =</span> cran_con, <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;">20</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>)</span>
<span id="cb2-5">  cran_pkgs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> stringr<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;">str_match</span>(cran, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'repository features ([0-9]{1,}) available packages'</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb2-6"></span>
<span id="cb2-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># R-Forge</span></span>
<span id="cb2-8">  rforge_con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> curl<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;">curl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://r-forge.r-project.org'</span>)</span>
<span id="cb2-9">  rforge <span class="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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readLines</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">con =</span> rforge_con, <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;">200</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>)</span>
<span id="cb2-10">  rforge_pkgs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> stringr<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;">str_replace</span>(stringr<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;">str_match</span>(rforge, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Projects: &lt;strong&gt;([0-9]{1,},[0-9]{1,})&lt;/strong&gt;'</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">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="cb2-11"></span>
<span id="cb2-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bioconductor</span></span>
<span id="cb2-13">  bioconductor_con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> curl<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;">curl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://bioconductor.org'</span>)</span>
<span id="cb2-14">  bioconductor <span class="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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readLines</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">con =</span> bioconductor_con, <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;">200</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>)</span>
<span id="cb2-15">  bioconductor_pkgs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> stringr<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;">str_match</span>(bioconductor, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Software</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;">&gt;([0-9]{1,}).*software packages.'</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb2-16"></span>
<span id="cb2-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># GitHub</span></span>
<span id="cb2-18">  github_con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> curl<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;">curl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://github.com/search?l=R&amp;q=R+package&amp;type=Repositories'</span>)</span>
<span id="cb2-19">  github <span class="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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readLines</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">con =</span> github_con, <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;">1200</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>)</span>
<span id="cb2-20">  github_pkgs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> stringr<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;">str_replace</span>(stringr<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;">str_match</span>(github, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'([0-9]{1,},[0-9]{1,}) repository results'</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">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="cb2-21"></span>
<span id="cb2-22">  day <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.time</span>(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-%d"</span>)</span>
<span id="cb2-23">  result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(day, cran_pkgs, rforge_pkgs, bioconductor_pkgs, github_pkgs)</span>
<span id="cb2-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(result) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'date'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CRAN'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R-Forge'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Bioconductor'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'GitHub'</span>)</span>
<span id="cb2-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(result)</span>
<span id="cb2-26">}</span></code></pre></div></div>
</div>
<p>From time to time I ran the following code to update <a href="num_pkgs_data.csv">my dataset</a>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">prev_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;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'num_pkgs_data.csv'</span>)</span>
<span id="cb3-2">today <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">interrogate_repos</span>()</span>
<span id="cb3-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">last</span>(prev_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> today[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]) {</span>
<span id="cb3-4">  prev_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;">rbind</span>(prev_data, today)</span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_csv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> prev_data, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'num_pkgs_data.csv'</span>)</span>
<span id="cb3-6">}</span></code></pre></div></div>
</div>
<p>The dataset looks like this:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">prev_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;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'num_pkgs_data.csv'</span>)</span>
<span id="cb4-2">prev_data</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 10 × 5
   date        cran rforge bioconductor github
   &lt;date&gt;     &lt;dbl&gt;  &lt;dbl&gt;        &lt;dbl&gt;  &lt;dbl&gt;
 1 2011-05-17  2984    998          460     NA
 2 2011-11-22  3429   1182          516     NA
 3 2012-04-25  3745   1274          554     NA
 4 2013-07-11  4689   1584          671     NA
 5 2013-09-24  4846   1631          671     NA
 6 2016-08-31  9066   2024         1211     NA
 7 2017-03-22 10312   2048         1296     NA
 8 2017-10-20 11646   2063         1383     NA
 9 2018-11-11 13342   2086         1649     NA
10 2020-02-03 15330   2121         1823  28667</code></pre>
</div>
</div>
</section>
<section id="data-transformation" class="level2">
<h2 class="anchored" data-anchor-id="data-transformation">Data transformation</h2>
<p>To plot this sensibly, I convert it from wide to long format:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> prev_data <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;">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>(cran, rforge, bioconductor, github), <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;">'repo'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<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;">as.integer</span>(value))</span></code></pre></div></div>
</div>
</section>
<section id="plotting" class="level2">
<h2 class="anchored" data-anchor-id="plotting">Plotting</h2>
<p>I needed some cool colours. On my way home from rstudio::conf 2020 I watched Taika Waititi’s <a href="https://www.imdb.com/title/tt2584384/">“Jojo Rabbit”</a>, which I found visually appealing. I grabbed a screenshot and, with the <a href="http://colormind.io/image/">Colormind</a> tool, created two palettes from it.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">jojo1 <span class="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;">'#c0b3aa'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#2b2117'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#9a8268'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#d3a677'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#e1d0ba'</span>)</span>
<span id="cb7-2">jojo2 <span class="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;">'#221c17'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#947e66'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#da9869'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#e0c6a4'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#c1b3ab'</span>)</span></code></pre></div></div>
</div>
<p>And now the most glamorous version of the plot, with the legend living in the title:</p>
<div class="cell" data-layout-align="center">
<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">cols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'bioconductor'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> jojo2[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'cran'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> jojo2[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'github'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> jojo2[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rforge'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> jojo2[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>])</span>
<span id="cb8-2"></span>
<span id="cb8-3">title <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Log # of R packages available at'</span>,</span>
<span id="cb8-4">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' &lt;span style="color:{cols[2]}"&gt;**CRAN**&lt;/span&gt;,'</span>,</span>
<span id="cb8-5">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' &lt;span style="color:{cols[4]}"&gt;**R-Forge**&lt;/span&gt;,'</span>,</span>
<span id="cb8-6">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' &lt;span style="color:{cols[1]}"&gt;**Bioconductor**&lt;/span&gt;'</span>,</span>
<span id="cb8-7">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' and'</span>,</span>
<span id="cb8-8">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' &lt;span style="color:{cols[3]}"&gt;**GitHub**&lt;/span&gt;'</span>,</span>
<span id="cb8-9">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' over time.'</span>)</span>
<span id="cb8-10"></span>
<span id="cb8-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(value), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> repo)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-12">  <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;">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="cb8-13">  <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;">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="cb8-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text_repel</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;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comma</span>(value)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</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="cb8-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> cols) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-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> title, <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 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;">''</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#EEEEEE'</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.25</span>),</span>
<span id="cb8-19">        <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> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#FFFDF8'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#FFFDF8'</span>),</span>
<span id="cb8-20">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.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;">colour =</span> jojo2[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]),</span>
<span id="cb8-21">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> jojo2[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>),</span>
<span id="cb8-22">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb8-23">        <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">lineheight =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>),</span>
<span id="cb8-24">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'plot'</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://kierczak.net/posts/glamorous-ggplot2/index_files/figure-html/plotting-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Line chart of the logarithm of the number of R packages at CRAN, R-Forge, Bioconductor and GitHub from 2011 to 2020." width="768"></p>
</figure>
</div>
</div>
</div>


</section>

 ]]></description>
  <category>R</category>
  <category>ggplot2</category>
  <category>visualisation</category>
  <category>teaching</category>
  <guid>https://kierczak.net/posts/glamorous-ggplot2/</guid>
  <pubDate>Mon, 03 Feb 2020 23:00:00 GMT</pubDate>
  <media:content url="https://kierczak.net/posts/glamorous-ggplot2/preview.png" medium="image" type="image/png" height="90" width="144"/>
</item>
<item>
  <title>Plotting 23andMe marker density with karyoploteR</title>
  <dc:creator>Marcin Kierczak</dc:creator>
  <link>https://kierczak.net/posts/karyoploter-23andme/</link>
  <description><![CDATA[ 





<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>Updated in 2026
</div>
</div>
<div class="callout-body-container callout-body">
<p>This is a lightly edited version of a 2018 post. The prose and figures are the originals. The code is updated for current Bioconductor, but it is <strong>shown, not re-run</strong>: the SNPedia files the post relied on are no longer downloadable, so the figures below are the ones produced in 2018.</p>
</div>
</div>
<section id="background" class="level2">
<h2 class="anchored" data-anchor-id="background">Background</h2>
<p><a href="https://www.23andme.com">23andMe</a> is a personal genomics company that offers saliva-based, direct-to-consumer genetic testing. It is probably one of the most popular ways for people to learn more about their own genetic makeup, including ancestry.</p>
<p>For genotyping, the company uses a <a href="https://en.wikipedia.org/wiki/SNP_array">SNP array</a> (SNP-chip). In short, such a chip lets us “read” polymorphic sites in human DNA: sites known to vary between individuals. Different versions of these sites are associated with traits such as eye colour, blood group or the risk of developing a disease. A given version does not always <em>cause</em> the phenotype. Often a SNP is a good proxy: it <em>tags</em> the phenotype. This works because DNA is inherited in rather long chunks (haplotypes), so a variant tends to travel together with its neighbours, including the actual <em>causative</em> variant. Thanks to this, genotyping a limited number of SNPs can tell you a lot about many traits.</p>
<p>23andMe has used three versions of its chip, introduced in 2008, 2012 and 2014. I was curious how they differ, so I wanted to visualise the density of genotyped SNPs per fixed-length window along the genome.</p>
</section>
<section id="the-data" class="level2">
<h2 class="anchored" data-anchor-id="the-data">The data</h2>
<p>23andMe uses the Mendel family as its showcase. We look at the data for one family member, Lily Mendel (a real person: Linda Avey, a co-founder of the company). Data from all three chips were available at <a href="https://www.snpedia.com/index.php/User:Lilly_Mendel">SNPedia</a> (2008 v1, 2012 v3 and 2014 v4). The format is simple: one row per marker, with four columns:</p>
<ul>
<li><code>rsid</code> – unique rs identifier of the marker,</li>
<li><code>chr</code> – chromosome,</li>
<li><code>pos</code> – coordinate of the marker,</li>
<li><code>gtype</code> – the genotype, e.g.&nbsp;GG.</li>
</ul>
</section>
<section id="analysis" class="level2">
<h2 class="anchored" data-anchor-id="analysis">Analysis</h2>
<section id="preparations" class="level3">
<h3 class="anchored" data-anchor-id="preparations">Preparations</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install once:</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># if (!requireNamespace("BiocManager")) install.packages("BiocManager")</span></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># BiocManager::install(c("karyoploteR", "biomaRt"))</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(karyoploteR)</span></code></pre></div></div>
</div>
</section>
<section id="loading-the-data" class="level3">
<h3 class="anchored" data-anchor-id="loading-the-data">Loading the data</h3>
<p>We load the data, fix the column names and convert it into a <code>GRanges</code> object, which is what the density plot needs.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">gdata <span class="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.table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"genome_Lilly_Mendel_v4.txt"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">comment.char =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#"</span>)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(gdata) <span class="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;">"rsid"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pos"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gtype"</span>)</span>
<span id="cb2-3"></span>
<span id="cb2-4">regions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">GRanges</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seqnames =</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;">"chr"</span>, gdata<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>chr),</span>
<span id="cb2-5">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ranges =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">IRanges</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</span> gdata<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pos, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span></code></pre></div></div>
</div>
</section>
<section id="plotting-marker-density" class="level3">
<h3 class="anchored" data-anchor-id="plotting-marker-density">Plotting marker density</h3>
<p>A karyogram is an ideogram of ordered chromosomes: a map of the genome. We plot marker density over each chromosome with <code>kpPlotDensity()</code>. A nice feature of <strong>karyoploteR</strong> is that it also draws the G-bands, the pattern produced by <a href="https://en.wikipedia.org/wiki/G_banding">Giemsa staining</a>, which cytogeneticists use to visualise chromosome structure. The one important parameter here is the window size, which I set empirically to 1 Mbp.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">kp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plotKaryotype</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kpAddBaseNumbers</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kpPlotDensity</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> regions,</span>
<span id="cb3-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window.size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span>,</span>
<span id="cb3-5">                <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;">"slateblue"</span>,</span>
<span id="cb3-6">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">border =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"slateblue"</span>)</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://kierczak.net/posts/karyoploter-23andme/marker_density-1.png" class="img-fluid figure-img" alt="Karyogram of the human chromosomes with marker density drawn above each one."></p>
<figcaption>Density of genotyped markers (1 Mbp windows) along the human karyotype.</figcaption>
</figure>
</div>
</section>
<section id="so-what" class="level3">
<h3 class="anchored" data-anchor-id="so-what">So what?</h3>
<p>I think the visualisation is neat, although the y-axis is missing (we fix that next). We can see low marker density around centromeres (the central, red-stained parts of the chromosomes) and a long stretch with no markers on the Y chromosome, probably its non-pseudoautosomal region. The density is also very low at the start of chromosomes 13, 14, 15, 21 and 22, and exceptionally high on the short arm of chromosome 6. I had no idea why, and not much time to research it…</p>
</section>
<section id="plotting-the-actual-counts" class="level3">
<h3 class="anchored" data-anchor-id="plotting-the-actual-counts">Plotting the actual counts</h3>
<p>What if we want the counts per window rather than the density? Easy enough:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> kp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>latest.plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>computed.values<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>density</span>
<span id="cb4-2">data_counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">GRanges</span>(kp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>latest.plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>computed.values<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>windows, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> counts)</span>
<span id="cb4-3">max_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;">floor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(counts) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(counts))</span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plotting parameters established by trial and error</span></span>
<span id="cb4-6">pp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getDefaultPlotParams</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-7">pp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>leftmargin <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.15</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># keep chromosome names clear of the plot</span></span>
<span id="cb4-8">pp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>topmargin <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;">150</span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># avoid overlap between chromosomes</span></span>
<span id="cb4-9">pp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bottommargin <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;">150</span></span>
<span id="cb4-10"></span>
<span id="cb4-11">kp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plotKaryotype</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.params =</span> pp) <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;">kpAddBaseNumbers</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kpLines</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data_counts, <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;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> max_count) <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;">kpAxis</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</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;">ymax =</span> max_count)</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://kierczak.net/posts/karyoploter-23andme/marker_counts-1.png" class="img-fluid figure-img" alt="Karyogram with marker counts per window drawn as a line above each chromosome."></p>
<figcaption>Marker counts per 1 Mbp window.</figcaption>
</figure>
</div>
<p>I did not manage to avoid all overlap between chromosomes and their data panels. It can probably be refined by playing with the graphical parameters, but you get the idea.</p>
</section>
<section id="zooming-in" class="level3">
<h3 class="anchored" data-anchor-id="zooming-in">Zooming in</h3>
<p>We spotted a region of high marker density on the short arm of chromosome 6. Let’s take a closer look.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">pp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getDefaultPlotParams</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-2">pp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>leftmargin <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.15</span></span>
<span id="cb5-3">pp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>topmargin <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;">120</span></span>
<span id="cb5-4">to_zoom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">GRanges</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chr6"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">IRanges</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20e6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">60e6</span>))</span>
<span id="cb5-5"></span>
<span id="cb5-6">kp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plotKaryotype</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.params =</span> pp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zoom =</span> to_zoom) <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;">kpAddBaseNumbers</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kpAxis</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</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="cb5-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kpBars</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data_counts, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y1 =</span> data_counts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> max_count)</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://kierczak.net/posts/karyoploter-23andme/zoom_chr6-1.png" class="img-fluid figure-img" alt="Zoomed view of chromosome 6 between 20 and 60 megabases, with the highest marker counts around 30 to 35 megabases."></p>
<figcaption>Chromosome 6, 20-60 Mbp.</figcaption>
</figure>
</div>
<p>Almost perfect; only the y-axis has vanished in a mysterious way (my fault or a feature of the package, I never found out). The highest counts occur roughly between 30 and 35 Mbp. It would be interesting to see which genes sit there.</p>
<div class="callout callout-style-default callout-warning 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">Warning</span>What a real study would need
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li>Check the exact coordinates of the dense region rather than eyeballing 30-35 Mbp.</li>
<li>23andMe uses <em>hg19</em> coordinates, while current Ensembl uses <em>hg38</em>. The code below asks Ensembl for GRCh37 (= hg19) so the two agree. Otherwise you would lift the coordinates over, for instance with <code>liftOver</code>. I did that back then, and the regions were close enough not to matter for this post.</li>
</ul>
</div>
</div>
<p>To find the attributes and filters available in BioMart, see the <a href="https://www.ensembl.org/biomart/martview">BioMart web interface</a>; once you have set them, click <em>XML</em>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(biomaRt)</span>
<span id="cb6-2"></span>
<span id="cb6-3">ensembl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">useEnsembl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">biomart =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"genes"</span>,</span>
<span id="cb6-4">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dataset =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hsapiens_gene_ensembl"</span>,</span>
<span id="cb6-5">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GRCh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">37</span>)</span>
<span id="cb6-6"></span>
<span id="cb6-7">genes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getBM</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">attributes =</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;">"ensembl_gene_id"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"external_gene_name"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"description"</span>),</span>
<span id="cb6-8">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filters =</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;">"chromosome_name"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"start"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"end"</span>),</span>
<span id="cb6-9">               <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;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"6"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30000000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35000000</span>),</span>
<span id="cb6-10">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mart =</span> ensembl)</span>
<span id="cb6-11"></span>
<span id="cb6-12">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(genes)</span></code></pre></div></div>
</div>
</section>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>Now we know what sits in the region. Any guesses why 23andMe put so many markers there?</p>


</section>

 ]]></description>
  <category>R</category>
  <category>genetics</category>
  <category>visualisation</category>
  <guid>https://kierczak.net/posts/karyoploter-23andme/</guid>
  <pubDate>Tue, 30 Jan 2018 23:00:00 GMT</pubDate>
  <media:content url="https://kierczak.net/posts/karyoploter-23andme/marker_density-1.png" medium="image" type="image/png" height="144" width="144"/>
</item>
</channel>
</rss>
