<?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>Sat, 31 Oct 2026 23:00:00 GMT</lastBuildDate>
<item>
  <title>Statistics from the beginning: hypotheses, normality and one-sample tests</title>
  <dc:creator>Marcin Kierczak</dc:creator>
  <link>https://kierczak.net/posts/stats-3-hypothesis-one-sample/</link>
  <description><![CDATA[ 





<div class="series-tag">
<p>Series · <em>Statistics from the beginning</em> · Part 3</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 my 2017 lecture notes, <em>Introduction to Statistical Tests</em> (<a href="../../assets/docs/statests.pdf">original PDF</a>). <a href="../../posts/stats-1-location-spread/index.html">Part 1</a> covered location and spread, <a href="../../posts/stats-2-covariance-correlation/index.html">Part 2</a> covariance and correlation. From here on we move to <strong>tests</strong>, beginning with the simplest situation: a single sample.</p>
</div>
</div>
<section id="a-test-is-a-question-put-to-the-data" class="level2">
<h2 class="anchored" data-anchor-id="a-test-is-a-question-put-to-the-data">A test is a question put to the data</h2>
<p>So far we have <em>described</em> data. A statistical test lets us <em>decide</em> something about the population behind a sample. Suppose the breed standard says that mature ewes of our breed weigh 55 kg on average. We weigh 30 ewes from our flock, and their mean is about 56.7 kg. Is the flock really heavier than the standard, or is that extra 1.7 kg just the luck of the draw?</p>
<p>The procedure always has the same shape:</p>
<ol type="1">
<li>State a <strong>null hypothesis</strong>, <img src="https://latex.codecogs.com/png.latex?H_0">: the boring explanation, here “the flock’s true mean weight is 55 kg”. The <strong>alternative</strong>, <img src="https://latex.codecogs.com/png.latex?H_1">, is that it is not.</li>
<li>Compute a <strong>test statistic</strong> from the sample, a number that is large when the data sit far from what <img src="https://latex.codecogs.com/png.latex?H_0"> predicts.</li>
<li>Ask how often a statistic <em>at least this extreme</em> would turn up if <img src="https://latex.codecogs.com/png.latex?H_0"> were true. That probability is the <strong>p-value</strong>.</li>
<li>If the p-value is below a threshold chosen in advance, the <strong>significance level</strong> <img src="https://latex.codecogs.com/png.latex?%5Calpha"> (conventionally 0.05), we reject <img src="https://latex.codecogs.com/png.latex?H_0">. Otherwise we do not.</li>
</ol>
<p>Two cautions about wording. First, the p-value is <em>not</em> the probability that <img src="https://latex.codecogs.com/png.latex?H_0"> is true. It is the probability of data this extreme <em>given</em> that <img src="https://latex.codecogs.com/png.latex?H_0"> is true. Second, failing to reject <img src="https://latex.codecogs.com/png.latex?H_0"> is not proof that <img src="https://latex.codecogs.com/png.latex?H_0"> holds. It means the data did not give us enough reason to doubt it.</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 id="two-kinds-of-mistake" class="level3">
<h3 class="anchored" data-anchor-id="two-kinds-of-mistake">Two kinds of mistake</h3>
<p>A test can go wrong in two ways. Rejecting a true <img src="https://latex.codecogs.com/png.latex?H_0"> is a <strong>Type I error</strong>, and its probability is exactly <img src="https://latex.codecogs.com/png.latex?%5Calpha">. Failing to reject a false <img src="https://latex.codecogs.com/png.latex?H_0"> is a <strong>Type II error</strong>, with probability <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. The quantity <img src="https://latex.codecogs.com/png.latex?1%20-%20%5Cbeta"> is the <strong>power</strong> of the test: the chance of noticing a real effect.</p>
<p>We can see the first kind directly by simulation. We draw many samples from a population where <img src="https://latex.codecogs.com/png.latex?H_0"> <em>is</em> true (mean exactly 55 kg), test each one, and look at the p-values:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb2-2">p_null <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replicate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</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;">30</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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">55</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(p_null <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.0502</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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> p_null), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(p)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">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="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p-value"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb4-4">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"When H0 is true, p-values are uniformly distributed"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://kierczak.net/posts/stats-3-hypothesis-one-sample/index_files/figure-html/pvalues-1.png" class="img-fluid figure-img" alt="Histogram of 5,000 p-values from tests where the null hypothesis is true. The bars are roughly equal in height, so p-values are uniformly distributed." width="672"></p>
</figure>
</div>
</div>
</div>
<p>About 5% of the tests reject <img src="https://latex.codecogs.com/png.latex?H_0"> even though it is true, which is what <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%200.05"> promises. This is also why a single “significant” result should not be over-interpreted: run twenty tests on pure noise and one of them will come out significant on average.</p>
</section>
<section id="many-tests-at-once-p-values-and-q-values" class="level3">
<h3 class="anchored" data-anchor-id="many-tests-at-once-p-values-and-q-values">Many tests at once: p-values and q-values</h3>
<p>Now suppose we are not weighing one flock but a thousand. Every flock gets its own test against the 55 kg standard. At <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%200.05">, about 50 flocks will look “significant” by luck alone, even if none of them differs from the standard.</p>
<p>The classical fix is the <strong>Bonferroni correction</strong>. It keeps the chance of <em>even one</em> false alarm at 5%. That is safe, but it is also so strict that real differences slip through.</p>
<p>Often we can live with a few false alarms, as long as we know how many to expect. This is the idea behind the <strong>false discovery rate</strong> (FDR): the share of false alarms among the flocks we flag. The <strong>q-value</strong> is the p-value’s counterpart for this. The q-value of a test is the smallest FDR at which that test would still be flagged. A p-value of 0.01 says that 1% of true nulls look this extreme. A q-value of 0.01 says that about 1% of the flagged flocks are expected to be false alarms.</p>
<p>In practice you rarely compute q-values by hand. The Benjamini–Hochberg procedure, <code>p.adjust(p, method = "BH")</code>, gives FDR-adjusted p-values. Storey’s <code>qvalue</code> package (Bioconductor) goes a step further and estimates how many of the nulls are true, which makes it a little less conservative. Here is a toy flock survey: 900 flocks truly at 55 kg, and 100 whose true mean is 57 kg.</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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb5-2">mu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">55</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">900</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">57</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>))</span>
<span id="cb5-3">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;">sapply</span>(mu, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(m) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</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;">30</span>, m, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">55</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value)</span>
<span id="cb5-4"></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># How many flocks does each method flag?</span></span>
<span id="cb5-6"><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;">raw_p      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>),</span>
<span id="cb5-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bonferroni =</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;">p.adjust</span>(p, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bonferroni"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>),</span>
<span id="cb5-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">BH_q       =</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;">p.adjust</span>(p, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BH"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>     raw_p bonferroni       BH_q 
       119          8         39 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Of the flocks flagged at FDR 5%, how many are false alarms?</span></span>
<span id="cb7-2">hits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p.adjust</span>(p, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BH"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span></span>
<span id="cb7-3"><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;">flagged =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(hits), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">false_alarms =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(hits <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> mu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">55</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>     flagged false_alarms 
          39            2 </code></pre>
</div>
</div>
<p>The raw threshold flags dozens of flocks that are in fact normal. Bonferroni flags almost no false ones, but it also misses most of the 57 kg flocks. The FDR approach finds many more of the real ones, and its false alarms stay near the 5% we asked for.</p>
</section>
</section>
<section id="parametric-or-non-parametric" class="level2">
<h2 class="anchored" data-anchor-id="parametric-or-non-parametric">Parametric or non-parametric?</h2>
<p>Tests fall into two families:</p>
<ul>
<li><strong>parametric tests</strong> assume that the data come from a distribution described by a few parameters, most often the normal distribution;</li>
<li><strong>non-parametric tests</strong> make no such assumption, typically working with the ranks of the observations instead of the values.</li>
</ul>
<p>One may ask: why not use non-parametric tests all the time? Because when the parametric assumptions <em>do</em> hold, the parametric test has higher power. The sensible workflow is to check the assumptions first and choose the test accordingly.</p>
</section>
<section id="checking-normality" class="level2">
<h2 class="anchored" data-anchor-id="checking-normality">Checking normality</h2>
<p>The most common assumption to check is that the sample comes from a normal distribution. There are two complementary ways to do it: a formal test and a picture.</p>
<p>The <strong>Shapiro–Wilk test</strong> has <img src="https://latex.codecogs.com/png.latex?H_0">: <em>the sample comes from a normal distribution</em>. Let us try it on a sample that is normal and on one that is uniform:</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;">11</span>)</span>
<span id="cb9-2">normal_sample  <span class="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;">100</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>
<span id="cb9-3">uniform_sample <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min =</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;">max =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb9-4"></span>
<span id="cb9-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shapiro.test</span>(normal_sample)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    Shapiro-Wilk normality test

data:  normal_sample
W = 0.98751, p-value = 0.473</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shapiro.test</span>(uniform_sample)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    Shapiro-Wilk normality test

data:  uniform_sample
W = 0.9583, p-value = 0.003048</code></pre>
</div>
</div>
<p>For the normal sample the p-value is large, so we have no reason to reject normality. For the uniform sample it is small, and we reject. The <strong>quantile–quantile (Q–Q) plot</strong> shows <em>how</em> a sample departs from normality: it plots the sample’s quantiles against those of a normal distribution, so a normal sample falls along a straight line.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">qq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</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;">sample =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal sample"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> normal_sample),</span>
<span id="cb13-2">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sample =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"uniform sample"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> uniform_sample))</span>
<span id="cb13-3"></span>
<span id="cb13-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(qq, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sample =</span> x)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_qq</span>(<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;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_qq_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> ink) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> sample, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Theoretical quantiles"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sample quantiles"</span>,</span>
<span id="cb13-9">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Q-Q plots: a straight line means normality"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://kierczak.net/posts/stats-3-hypothesis-one-sample/index_files/figure-html/qq-1.png" class="img-fluid figure-img" alt="Two Q-Q plots. The normal sample follows the straight line; the uniform sample curves away from it in an S shape." width="672"></p>
</figure>
</div>
</div>
</div>
<p>A word of caution. A formal test is sensitive to sample size: with thousands of observations it flags departures that are harmless, and with a dozen it can miss serious ones. Treat Shapiro–Wilk and the Q–Q plot together, and think about whether the departure matters for the test you intend to run.</p>
</section>
<section id="one-sample-t-test" class="level2">
<h2 class="anchored" data-anchor-id="one-sample-t-test">One-sample t-test</h2>
<p>Back to our flock. We weigh 30 ewes and ask whether their population mean differs from the breed standard of 55 kg:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="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;">2026</span>)</span>
<span id="cb14-2">flock <span class="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;">30</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;">57</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="cb14-3"></span>
<span id="cb14-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shapiro.test</span>(flock)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># normality looks fine</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.3245917</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(flock, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">55</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    One Sample t-test

data:  flock
t = 2.1376, df = 29, p-value = 0.04111
alternative hypothesis: true mean is not equal to 55
95 percent confidence interval:
 55.07387 58.34670
sample estimates:
mean of x 
 56.71029 </code></pre>
</div>
</div>
<p>The output has several parts. The test statistic is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0At%20=%20%5Cfrac%7B%5Cbar%7Bx%7D%20-%20%5Cmu_0%7D%7Bs%20/%20%5Csqrt%7Bn%7D%7D,%0A"></p>
<p>the distance between the sample mean and the hypothesised mean <img src="https://latex.codecogs.com/png.latex?%5Cmu_0">, measured in units of the standard error. We can check it by hand:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">t_stat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(flock) <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;">55</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;">sd</span>(flock) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(flock)))</span>
<span id="cb18-2">t_stat</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.137558</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="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;">pt</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;">abs</span>(t_stat), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(flock) <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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># two-sided p-value</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.04111133</code></pre>
</div>
</div>
<p>With a p-value just below 0.05 we reject <img src="https://latex.codecogs.com/png.latex?H_0"> at <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%200.05">, but only narrowly: a borderline result like this deserves a second look rather than a celebration. The p-value is the probability of a <img src="https://latex.codecogs.com/png.latex?t"> at least this far from zero in either direction under <img src="https://latex.codecogs.com/png.latex?H_0">, using the <img src="https://latex.codecogs.com/png.latex?t"> distribution with <img src="https://latex.codecogs.com/png.latex?n%20-%201"> degrees of freedom. The output also gives a <strong>95% confidence interval</strong> for the population mean. If we repeated the experiment many times, about 95% of such intervals would contain the true mean. Note that it does <em>not</em> contain 55 whenever the test rejects at <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%200.05">: the interval and the test tell the same story.</p>
<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">
<span class="screen-reader-only">Tip</span>A brewery statistic
</div>
</div>
<div class="callout-body-container callout-body">
<p>The <img src="https://latex.codecogs.com/png.latex?t"> distribution and the <img src="https://latex.codecogs.com/png.latex?t">-test were introduced by William Sealy Gosset, who worked for the Guinness brewery and used them to monitor the quality of stout. Because the brewery did not allow employees to publish under their own names, he wrote as “Student”, hence <em>Student’s t-test</em>.</p>
</div>
</div>
<section id="when-the-standard-deviation-is-known-the-z-test" class="level3">
<h3 class="anchored" data-anchor-id="when-the-standard-deviation-is-known-the-z-test">When the standard deviation is known: the z-test</h3>
<p>If the population standard deviation <img src="https://latex.codecogs.com/png.latex?%5Csigma"> is <em>known</em> in advance, we can replace <img src="https://latex.codecogs.com/png.latex?s"> by <img src="https://latex.codecogs.com/png.latex?%5Csigma"> and use the normal distribution instead of <img src="https://latex.codecogs.com/png.latex?t">. This is the <strong>z-test</strong>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">sigma <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;">4</span>                                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># known population sd</span></span>
<span id="cb22-2">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(flock) <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;">55</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (sigma <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(flock)))</span>
<span id="cb22-3"><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;">z =</span> z, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">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;">pnorm</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;">abs</span>(z)))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>         z          p 
2.34190706 0.01918549 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(flock) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">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;">qnorm</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.975</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sigma <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(flock))   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 95% CI</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 55.27893 58.14164</code></pre>
</div>
</div>
<p>The z-test gives a smaller p-value here because it ignores the extra uncertainty that comes from estimating the standard deviation from only 30 animals: the <img src="https://latex.codecogs.com/png.latex?t"> distribution has heavier tails to account for it. In practice <img src="https://latex.codecogs.com/png.latex?%5Csigma"> is almost never known, which is why the <img src="https://latex.codecogs.com/png.latex?t">-test is the one you will use. With large samples the two give almost identical answers.</p>
</section>
</section>
<section id="one-sample-wilcoxon-test" class="level2">
<h2 class="anchored" data-anchor-id="one-sample-wilcoxon-test">One-sample Wilcoxon test</h2>
<p>What if the data are clearly not normal? The non-parametric counterpart of the one-sample <img src="https://latex.codecogs.com/png.latex?t">-test is the <strong>Wilcoxon signed-rank test</strong>. It ranks the absolute differences from the hypothesised centre and asks whether positive and negative differences are balanced. It assumes the distribution is <em>symmetric</em> but not that it is normal. Our uniform sample from before qualifies:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(uniform_sample, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.5275405</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wilcox.test</span>(uniform_sample, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    Wilcoxon signed rank test with continuity correction

data:  uniform_sample
V = 2719, p-value = 0.5058
alternative hypothesis: true location is not equal to 3</code></pre>
</div>
</div>
<p>The population this sample came from is uniform between 2 and 4, so its true centre is 3, and neither test finds reason to reject <img src="https://latex.codecogs.com/png.latex?H_0">. When the data really are normal the <img src="https://latex.codecogs.com/png.latex?t">-test is a little more powerful. When they are not, the Wilcoxon test is the safer choice.</p>
</section>
<section id="whats-next" class="level2">
<h2 class="anchored" data-anchor-id="whats-next">What’s next</h2>
<p>We have the logic of testing and the tools for one sample. In the next part we move to <strong>two samples</strong>: are two groups drawn from the same population? We will meet the F-test for variances, the <img src="https://latex.codecogs.com/png.latex?t">-test in its Welch form, and the Mann–Whitney (Wilcoxon rank-sum) test.</p>
<div class="series-nav">
<p><a href="../../blog.html#category=series" class="btn-pill">All parts in this series</a></p>
<p>Previous: <a href="../../posts/stats-2-covariance-correlation/index.html">covariance and correlation</a>. Next in the series: two-sample tests.</p>
</div>


</section>

 ]]></description>
  <category>statistics</category>
  <category>R</category>
  <category>teaching</category>
  <category>series</category>
  <guid>https://kierczak.net/posts/stats-3-hypothesis-one-sample/</guid>
  <pubDate>Sat, 31 Oct 2026 23:00:00 GMT</pubDate>
  <media:content url="https://kierczak.net/posts/stats-3-hypothesis-one-sample/preview.png" medium="image" type="image/png" height="103" width="144"/>
</item>
<item>
  <title>Statistics from the beginning: covariance and correlation</title>
  <dc:creator>Marcin Kierczak</dc:creator>
  <link>https://kierczak.net/posts/stats-2-covariance-correlation/</link>
  <description><![CDATA[ 





<div class="series-tag">
<p>Series · <em>Statistics from the beginning</em> · Part 2</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 my 2017 lecture notes, <em>Introduction to Statistical Tests</em> (<a href="../../assets/docs/statests.pdf">original PDF</a>). <a href="../../posts/stats-1-location-spread/index.html">Part 1</a> covered location and spread of a single variable. This part looks at <strong>two variables at once</strong>. Next come the tests themselves.</p>
</div>
</div>
<section id="from-one-variable-to-two" class="level2">
<h2 class="anchored" data-anchor-id="from-one-variable-to-two">From one variable to two</h2>
<p>Variance told us how much a single variable scatters around its mean. When we have two variables, say <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y">, a more interesting question appears: when <img src="https://latex.codecogs.com/png.latex?x"> is above its mean, is <img src="https://latex.codecogs.com/png.latex?y"> also above <em>its</em> mean? Do the deviations of the two variables tend to point the same way?</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>
<p>Back to our sheep. Let us simulate 150 ewes, recording body weight and the weight of the fleece shorn from each. Heavier animals tend to carry more wool, so we build in a positive relationship:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>)</span>
<span id="cb2-2">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span></span>
<span id="cb2-3">ewes <span class="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;">body_kg =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n, <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="cb2-4">ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fleece_kg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(ewes, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  body_kg fleece_kg
1   55.77      4.31
2   53.26      3.39
3   58.65      3.93
4   62.17      4.29</code></pre>
</div>
</div>
</section>
<section id="covariance" class="level2">
<h2 class="anchored" data-anchor-id="covariance">Covariance</h2>
<p>Covariance multiplies, for each animal, the deviation of <img src="https://latex.codecogs.com/png.latex?x"> from its mean by the deviation of <img src="https://latex.codecogs.com/png.latex?y"> from its mean, and averages the products (with the same <img src="https://latex.codecogs.com/png.latex?n%20-%201"> as for variance):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7BCov%7D_%7Bx,y%7D%20=%20%5Cfrac%7B%5Csum%20(x%20-%20%5Cbar%7Bx%7D)(y%20-%20%5Cbar%7By%7D)%7D%7Bn%20-%201%7D.%0A"></p>
<p>The sign of each product is what matters. If both deviations are positive (heavy animal, heavy fleece) or both are negative (light animal, light fleece), the product is positive. If one is positive and the other negative, the product is negative. Drawing lines through the two means splits the plot into four quadrants and shows this directly:</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">ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sign <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>((ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb4-2">                    (ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fleece_kg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fleece_kg)) <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="cb4-3">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"positive product"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"negative product"</span>)</span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(ewes, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(body_kg, fleece_kg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> sign)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_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>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg), <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="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fleece_kg), <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="cb4-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-9">  <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;">"positive product"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> accent, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"negative product"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> gold)) <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;">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;">"Body weight (kg)"</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;">"Fleece weight (kg)"</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="cb4-11">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Most points fall in the quadrants that add to the covariance"</span>,</span>
<span id="cb4-12">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dotted lines: the two means"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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-2-covariance-correlation/index_files/figure-html/quadrants-1.png" class="img-fluid figure-img" alt="Scatterplot of body weight against fleece weight with lines at the means. Points in the upper right and lower left quadrants, which contribute positive products, are red; the others are gold." width="672"></p>
</figure>
</div>
</div>
</div>
<p>Most of the points lie in the upper right and lower left quadrants, so the sum is positive: <strong>positive covariance</strong> means that <img src="https://latex.codecogs.com/png.latex?y"> tends to increase with <img src="https://latex.codecogs.com/png.latex?x">. If the relationship ran the other way, most points would fall in the other two quadrants and the covariance would be <strong>negative</strong>. If the quadrants were populated about evenly, the positive and negative products would cancel and the covariance would be close to zero.</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;">cov</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg, ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fleece_kg)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.6676829</code></pre>
</div>
</div>
<p>There is a neat connection to the previous part. Substitute <img src="https://latex.codecogs.com/png.latex?x"> for <img src="https://latex.codecogs.com/png.latex?y"> in the formula and you get <img src="https://latex.codecogs.com/png.latex?%5Csum%20(x%20-%20%5Cbar%7Bx%7D)%5E2%20/%20(n%20-%201)">, which is exactly the variance. <strong>Variance is the covariance of a variable with itself:</strong></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;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cov</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg, ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">var</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>     cov      var 
14.25627 14.25627 </code></pre>
</div>
</div>
</section>
<section id="covariance-depends-on-the-units" class="level2">
<h2 class="anchored" data-anchor-id="covariance-depends-on-the-units">Covariance depends on the units</h2>
<p>Like variance, covariance is <strong>scale dependent</strong>. Suppose a colleague records the same fleece weights in grams rather than kilograms. Nothing about the animals has changed, yet:</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">fleece_g <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fleece_kg <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;">1000</span></span>
<span id="cb9-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;">kg =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cov</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg, ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fleece_kg),</span>
<span id="cb9-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cov</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg, fleece_g))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>         kg           g 
  0.6676829 667.6828594 </code></pre>
</div>
</div>
<p>The covariance grows a thousandfold. Its size says nothing about how <em>strong</em> the relationship is, so we cannot compare covariances between different pairs of variables, or judge whether 0.4 is “large”. We need to remove the units.</p>
</section>
<section id="correlation" class="level2">
<h2 class="anchored" data-anchor-id="correlation">Correlation</h2>
<p>We already know how to remove units: <strong>standardise</strong>. Dividing the covariance by the standard deviations of both variables gives the <em>Pearson correlation coefficient</em>,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7BCor%7D_%7Bx,y%7D%20=%20%5Cfrac%7B%5Cmathrm%7BCov%7D_%7Bx,y%7D%7D%7B%5Csqrt%7Bs_x%5E2%20%5Ccdot%20s_y%5E2%7D%7D%20=%20%5Cfrac%7B%5Cmathrm%7BCov%7D_%7Bx,y%7D%7D%7Bs_x%20%5C,%20s_y%7D.%0A"></p>
<p>It is bounded between <img src="https://latex.codecogs.com/png.latex?-1"> and <img src="https://latex.codecogs.com/png.latex?1">: <img src="https://latex.codecogs.com/png.latex?1"> means a perfect increasing linear relationship, <img src="https://latex.codecogs.com/png.latex?-1"> a perfect decreasing one, and <img src="https://latex.codecogs.com/png.latex?0"> no <em>linear</em> relationship. And now the choice of units does not matter:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><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;">kg =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg, ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fleece_kg),</span>
<span id="cb11-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg, fleece_g))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>       kg         g 
0.5927476 0.5927476 </code></pre>
</div>
</div>
<p>Here is the fact I promised at the end of Part 1: <strong>correlation is simply the covariance of the z-scores.</strong> Standardise both variables, compute their covariance, and you have the correlation:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">z_body   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg   <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg))   <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg)</span>
<span id="cb13-2">z_fleece <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fleece_kg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fleece_kg)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fleece_kg)</span>
<span id="cb13-3"></span>
<span id="cb13-4"><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;">cov_of_zscores =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cov</span>(z_body, z_fleece),</span>
<span id="cb13-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cor            =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>body_kg, ewes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fleece_kg))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>cov_of_zscores            cor 
     0.5927476      0.5927476 </code></pre>
</div>
</div>
</section>
<section id="what-correlation-does-not-tell-you" class="level2">
<h2 class="anchored" data-anchor-id="what-correlation-does-not-tell-you">What correlation does not tell you</h2>
<p>A correlation coefficient is one number, and one number can hide a lot. Four small caveats are worth keeping in mind.</p>
<p><strong>It only sees linear relationships.</strong> Perfect dependence can give a correlation of zero if the shape is not a line. Below, <img src="https://latex.codecogs.com/png.latex?y"> is completely determined by <img src="https://latex.codecogs.com/png.latex?x">, yet the correlation is about zero, because the left and right halves of the curve cancel:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb15-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;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>)</span>
<span id="cb15-3"></span>
<span id="cb15-4">shapes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(</span>
<span id="cb15-5">  <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;">panel =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"linear"</span>,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> 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;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>)),</span>
<span id="cb15-6">  <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;">panel =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quadratic"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> 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;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>)),</span>
<span id="cb15-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"one outlier"</span>,</span>
<span id="cb15-8">             <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">59</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">59</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>))</span>
<span id="cb15-9">)</span>
<span id="cb15-10">r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(shapes, shapes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>panel), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(d) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y))</span>
<span id="cb15-11">shapes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>panel <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(shapes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>panel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(r),</span>
<span id="cb15-12">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%s (r = %.2f)"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(r), r))</span>
<span id="cb15-13"></span>
<span id="cb15-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(shapes, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<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;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> panel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">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;">"The same coefficient can describe very different data"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://kierczak.net/posts/stats-2-covariance-correlation/index_files/figure-html/shapes-1.png" class="img-fluid figure-img" alt="Three scatterplots: a strong linear relationship, a parabola whose correlation is near zero, and an unrelated cloud with one extreme outlier that creates a sizeable correlation." width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>It is sensitive to outliers.</strong> In the right-hand panel, 59 unrelated points plus a single extreme one produce a sizeable correlation, just as one mistyped sheep distorted the mean in Part 1. Always plot your data before trusting the number.</p>
<p><strong>Independence implies zero covariance, but not the other way round.</strong> If two variables are independent, their covariance is zero. The parabola shows that the converse fails: zero correlation means no <em>linear</em> relationship, not no relationship.</p>
<p><strong>Correlation is not causation.</strong> Body weight and fleece weight move together, but that alone does not tell us whether one drives the other, whether both depend on age or nutrition, or whether it is coincidence. Correlation is a description of the sample, not an explanation of it.</p>
</section>
<section id="whats-next" class="level2">
<h2 class="anchored" data-anchor-id="whats-next">What’s next</h2>
<p>We can now describe one variable (location and spread) and the relationship between two (covariance and correlation). That is enough vocabulary to start asking questions of the data. In the next part we begin with <strong>statistical tests</strong>: the difference between parametric and non-parametric tests, and checking normality, which is where most analyses start.</p>
<div class="series-nav">
<p><a href="../../blog.html#category=series" class="btn-pill">All parts in this series</a></p>
<p>Previous: <a href="../../posts/stats-1-location-spread/index.html">location and spread</a>. Next in the series: <a href="../../posts/stats-3-hypothesis-one-sample/index.html">hypotheses, normality and one-sample tests</a>.</p>
</div>


</section>

 ]]></description>
  <category>statistics</category>
  <category>R</category>
  <category>teaching</category>
  <category>series</category>
  <guid>https://kierczak.net/posts/stats-2-covariance-correlation/</guid>
  <pubDate>Thu, 22 Oct 2026 22:00:00 GMT</pubDate>
  <media:content url="https://kierczak.net/posts/stats-2-covariance-correlation/preview.png" medium="image" type="image/png" height="103" width="144"/>
</item>
<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: <a href="../../posts/stats-2-covariance-correlation/index.html">covariance and correlation</a>.</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>
<item>
  <title>Receiving ACARS messages with a software-defined radio on a Mac</title>
  <dc:creator>Marcin Kierczak</dc:creator>
  <link>https://kierczak.net/posts/archive-rtl-acars-osx/</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>From the archive
</div>
</div>
<div class="callout-body-container callout-body">
<p>First published on my old blog in September 2014 as <em>Compiling and installing rtl_acars on OS X 10.9 Mavericks</em>. These steps are for a 2014 system and are <strong>very likely out of date</strong>: the software, the build tools and macOS itself have all changed since. I keep the post for the fun of it.</p>
</div>
</div>
<p>I struggled for a while, but in the end I managed to install <code>rtl_acars</code> on my Mac, and after some hours of waiting I received my first ACARS messages. ACARS is the short-message system that aircraft use to talk to ground stations. Now it is only a matter of building a better antenna. Here is my path to success.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clone 9nut's version of rtl-sdr from GitHub</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> clone https://github.com/9nut/rtl-sdr</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add missing lines in Makefile.am</span></span>
<span id="cb1-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">rtl_acars_SOURCES</span> = rtl_acars.c</span>
<span id="cb1-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">rtl_acars_LDADD</span> = librtlsdr.la <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">LIBM</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I had previously installed Homebrew to get the necessary libraries</span></span>
<span id="cb1-9"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">brew</span> install libtool</span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aclocal</span></span>
<span id="cb1-11"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">glibtoolize</span></span>
<span id="cb1-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">autoconf</span></span>
<span id="cb1-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">automake</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--add-missing</span></span>
<span id="cb1-14"></span>
<span id="cb1-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Choose your own destination</span></span>
<span id="cb1-16"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">./configure</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--prefix</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>/Users/user/rtl/</span>
<span id="cb1-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make</span></span>
<span id="cb1-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make</span> install</span></code></pre></div></div>
<p>And my first received ACARS message came from a United Airlines plane:</p>
<pre><code>RX_IDX: 0
ACARS mode: V, message label: H1
Aircraft reg: .N14121, flight id: UA0069
Block id: 55, msg. no: D02A
Message content:-
#DFBCL2N14121ESSAKEWR3008072336 222 -075-215 20028064524168000691CLMBB
349 031887031259 961 963 899 904 906 903708702 8480 8512168616890310
85065008080040</code></pre>



 ]]></description>
  <category>archive</category>
  <category>SDR</category>
  <category>radio</category>
  <category>aviation</category>
  <guid>https://kierczak.net/posts/archive-rtl-acars-osx/</guid>
  <pubDate>Tue, 02 Sep 2014 22:00:00 GMT</pubDate>
  <media:content url="https://kierczak.net/posts/archive-rtl-acars-osx/preview.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Mapping between genome assemblies in R</title>
  <dc:creator>Marcin Kierczak</dc:creator>
  <link>https://kierczak.net/posts/archive-genome-assembly-mapping/</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>From the archive
</div>
</div>
<div class="callout-body-container callout-body">
<p>First published on my old blog in March 2014. I have kept the idea and the original <code>plyr</code> solution, and added a version with today’s <code>dplyr</code>.</p>
</div>
</div>
<p>Genome arrays are designed against one particular version of a genome assembly. New assemblies are published, but the array stays the same, so you often need to map between the two. In my case, the task was to map between two assemblies of the canine genome, from canFam2 to CanFam3, for a SNP array. I had two <code>.map</code> files with genomic coordinates per marker, which look like this:</p>
<pre><code>chrom name position
1 BICF2G630707759 14548
1 BICF2S2358127 68723
1 BICF2P1173580 80040</code></pre>
<p>I wanted to join them so that the marker names come from the canFam2 file, while the chromosome and position come from the matching marker in the canFam3 file.</p>
<section id="the-pitfall-of-merge" class="level2">
<h2 class="anchored" data-anchor-id="the-pitfall-of-merge">The pitfall of <code>merge()</code></h2>
<p>The first thing that came to mind was <code>merge()</code>. The problem is that a marker that has no match in canFam3 is moved to the end of the result, which breaks the marker order. What we want instead is <code>NA</code> in both the chromosome and the position when the marker cannot be found in canFam3.</p>
</section>
<section id="the-solution-a-left-join" class="level2">
<h2 class="anchored" data-anchor-id="the-solution-a-left-join">The solution: a left join</h2>
<p>The R package <code>plyr</code> has exactly that, in its <code>join()</code> function:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">new.map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(map, canFam3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"marker"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>)</span></code></pre></div></div>
<p>Today the same thing is usually written with <code>dplyr</code>. Here it is with a tiny made-up example, so you can see that the order is preserved and the missing marker gets <code>NA</code>:</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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb3-2"></span>
<span id="cb3-3">canFam2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb3-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">marker =</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;">"BICF2G630707759"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BICF2S2358127"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BICF2P1173580"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BICF2X0000001"</span>),</span>
<span id="cb3-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">chr2 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pos2 =</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;">14548</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">68723</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80040</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90111</span>)</span>
<span id="cb3-6">)</span>
<span id="cb3-7">canFam3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb3-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">marker =</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;">"BICF2P1173580"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BICF2G630707759"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BICF2S2358127"</span>),</span>
<span id="cb3-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">chr3 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pos3 =</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;">81233</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15021</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">69300</span>)</span>
<span id="cb3-10">)</span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(canFam2, canFam3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"marker"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>           marker chr2  pos2 chr3  pos3
1 BICF2G630707759    1 14548    1 15021
2   BICF2S2358127    1 68723    1 69300
3   BICF2P1173580    1 80040    1 81233
4   BICF2X0000001    1 90111   NA    NA</code></pre>
</div>
</div>
<p>The unmatched marker <code>BICF2X0000001</code> stays in its place and gets <code>NA</code> for the new coordinates, which is what we wanted. In base R, <code>match()</code> does the same job: <code>canFam3$pos3[match(canFam2$marker, canFam3$marker)]</code>.</p>
<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">
<span class="screen-reader-only">Tip</span>When marker names are not shared
</div>
</div>
<div class="callout-body-container callout-body">
<p>This trick works because the <em>same array markers</em> appear in both assemblies. To convert coordinates that have no shared identifiers, use a chain-file based lift-over instead, for example <code>liftOver()</code> in the Bioconductor package <code>rtracklayer</code>.</p>
</div>
</div>


</section>

 ]]></description>
  <category>archive</category>
  <category>R</category>
  <category>genetics</category>
  <category>dog</category>
  <guid>https://kierczak.net/posts/archive-genome-assembly-mapping/</guid>
  <pubDate>Thu, 06 Mar 2014 23:00:00 GMT</pubDate>
  <media:content url="https://kierczak.net/posts/archive-genome-assembly-mapping/preview.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Variance-controlling genes</title>
  <dc:creator>Marcin Kierczak</dc:creator>
  <link>https://kierczak.net/posts/archive-variance-controlling-genes/</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>From the archive
</div>
</div>
<div class="callout-body-container callout-body">
<p>First published on my old blog in August 2012; lightly edited for this site.</p>
</div>
</div>
<p>The August 2012 issue of <em>PLoS Genetics</em> brought an interesting article by Xia Shen and colleagues from the Computational Genetics Section at the Swedish University of Agricultural Sciences. They show that polymorphisms and mutations can affect not only the <strong>mean</strong> of a trait but also its <strong>variance</strong>.</p>
<p>After developing the necessary theory, the authors use an <em>Arabidopsis thaliana</em> dataset from Magnus Nordborg’s lab to pinpoint examples of such variance-controlling genes. It is clearly shown that the classical GWAS approach can completely miss genetic effects that influence trait variance. They propose an extended approach, called <strong>vGWAS</strong>, that helps discover variance-controlling genes.</p>
<p>Read it: Shen X, Pettersson M, Rönnegård L, Carlborg Ö (2012). <em>Inheritance beyond plain heritability: variance-controlling genes in</em> Arabidopsis thaliana. PLoS Genetics 8(8): e1002839. <a href="https://doi.org/10.1371/journal.pgen.1002839">doi:10.1371/journal.pgen.1002839</a></p>



 ]]></description>
  <category>archive</category>
  <category>genetics</category>
  <category>GWAS</category>
  <guid>https://kierczak.net/posts/archive-variance-controlling-genes/</guid>
  <pubDate>Wed, 22 Aug 2012 22:00:00 GMT</pubDate>
  <media:content url="https://kierczak.net/posts/archive-variance-controlling-genes/preview.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Command-line basics: one-liners, aliases and SSH keys</title>
  <dc:creator>Marcin Kierczak</dc:creator>
  <link>https://kierczak.net/posts/archive-bash-ssh-basics/</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>From the archive
</div>
</div>
<div class="callout-body-container callout-body">
<p>This merges three short posts from my old blog (<em>Bash one-liners, text manipulation</em>, <em>Bash shells and aliases</em> and <em>Automatic SSH login</em>, all February 2011). I have corrected a few mistakes in the originals and updated a few commands that have since changed. The corrections are listed at the end.</p>
</div>
</div>
<section id="text-one-liners" class="level2">
<h2 class="anchored" data-anchor-id="text-one-liners">Text one-liners</h2>
<p>Bash is very powerful for text manipulation, and you can save a lot of time by learning a few short commands, the so-called one-liners.</p>
<p>To convert a string to upper case (or lower case), use <code>tr</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lowercase"</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tr</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[:lower:]"</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[:upper:]"</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># LOWERCASE</span></span></code></pre></div></div>
<p>Two commands that are indispensable for any bioinformatician when checking large files are <code>head</code> and <code>tail</code>, which show the first or last N lines of a file:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span> myfile.txt            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the first 10 lines</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-n</span> 5 myfile.txt       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># only the first 5 lines</span></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tail</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-n</span> 5 myfile.csv       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the last 5 lines</span></span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tail</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-n</span> +2 myfile.csv      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># everything from line 2 on (skips the header)</span></span></code></pre></div></div>
<p>Now the really useful part: you can chain these commands with a pipe.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-n</span> 10 myfile.txt <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tail</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-n</span> 5   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># lines 6 to 10</span></span></code></pre></div></div>
<p>Quite often you need only a part of a line. Say you have the line <code>Accuracy.mean=0.98</code> and you want just <code>0.98</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acc_mean</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grep</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Accuracy.mean"</span> myfile.txt <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">awk</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-F</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'{print $NF}'</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>And when a line contains unwanted spaces, here is the remedy:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"   I   a m    full of s p a ces  "</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tr</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span></span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Iamfullofspaces</span></span></code></pre></div></div>
</section>
<section id="start-up-files-and-aliases" class="level2">
<h2 class="anchored" data-anchor-id="start-up-files-and-aliases">Start-up files and aliases</h2>
<p>Some commands are long and cumbersome to type, yet you need them several times a day. <strong>Aliases</strong> are a good remedy.</p>
<p>Each user has two important files in the home directory: <code>.bash_profile</code> and <code>.bashrc</code>. They are read by a <em>login shell</em> and a <em>non-login shell</em>, respectively. A login shell is the one you get when you log in; every shell you open after that is a non-login shell. Typically, commands you want to run only at login, such as displaying system or network information, go into <code>.bash_profile</code>, and everything else, including aliases, goes into <code>.bashrc</code>. To avoid repetition, end your <code>.bash_profile</code> by including <code>.bashrc</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-f</span> ~/.bashrc <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">then</span></span>
<span id="cb6-2">   <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">source</span> ~/.bashrc</span>
<span id="cb6-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fi</span></span></code></pre></div></div>
<p><strong>macOS users:</strong> Terminal opens a login shell every time, so it reads <code>.bash_profile</code> (or <code>.zprofile</code>/<code>.zshrc</code> if you use zsh, which is the default today).</p>
<p><strong>Beginners’ note:</strong> file names that start with a dot, such as <code>.bashrc</code>, are hidden. You can see them with:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-al</span></span></code></pre></div></div>
<p>Say you often use <code>ls -alh</code>, which lists all files, including hidden ones, with their sizes in human-readable form. To have something shorter, add this line to the end of your <code>.bashrc</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">alias</span> ll=<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'ls -alh'</span></span></code></pre></div></div>
<p>From now on, typing <code>ll</code> gives you all the information about your files. After editing the profile files, re-open the shell (or run <code>source ~/.bashrc</code>) to use the aliases.</p>
<p>A useful maintenance alias on Debian or Ubuntu updates the whole system with one word:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">alias</span> admin=<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'sudo apt update &amp;&amp; sudo apt -y full-upgrade &amp;&amp; sudo apt -y autoclean'</span></span></code></pre></div></div>
</section>
<section id="logging-in-over-ssh-without-a-password" class="level2">
<h2 class="anchored" data-anchor-id="logging-in-over-ssh-without-a-password">Logging in over SSH without a password</h2>
<p>If you log into a server over SSH several times a day, typing the password every time gets annoying. It is easy to set up key-based login. Suppose you want to allow your <code>workstation</code> to connect to the <code>server</code>. First, generate a key pair on the workstation:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ssh-keygen</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-t</span> ed25519</span></code></pre></div></div>
<p>and answer the questions: where to save the key, and an optional passphrase (a passphrase is a good idea). The command prints a fingerprint and a small picture, the key’s <em>randomart</em>:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://kierczak.net/posts/archive-bash-ssh-basics/images/ssh-randomart.png" class="img-fluid figure-img" width="125"></p>
<figcaption>The randomart image of an SSH key</figcaption>
</figure>
</div>
<p>The image is a visual representation of the key’s fingerprint. It is meant to be easier for humans to compare and verify than a string of hexadecimal characters, such as <code>ef:f2:5b:9c:…</code>. They are fun, anyway.</p>
<p>Now the public key has to be added to the list of trusted keys on the server. The simplest way is:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">ssh-copy-id</span> user@server</span></code></pre></div></div>
<p>This creates <code>~/.ssh</code> on the server if needed and appends your public key to <code>~/.ssh/authorized_keys</code>. If <code>ssh-copy-id</code> is not available, the same can be done by hand:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ssh</span> user@server mkdir <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> .ssh</span>
<span id="cb12-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span> ~/.ssh/id_ed25519.pub <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ssh</span> user@server <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'cat &gt;&gt; .ssh/authorized_keys'</span></span></code></pre></div></div>
<p>You should now be able to log in without typing the password. If it still asks, check the permissions on the server: <code>~/.ssh</code> should be <code>700</code> and <code>~/.ssh/authorized_keys</code> should be <code>600</code> (or at least not writable by others).</p>
</section>
<section id="corrections-to-the-original-posts" class="level2">
<h2 class="anchored" data-anchor-id="corrections-to-the-original-posts">Corrections to the original posts</h2>
<ul>
<li><code>tail -n myfile.csv</code> was missing the number; <code>tail +2</code> is the obsolete form of <code>tail -n +2</code>.</li>
<li><code>head -n 10 … | tail -n 5</code> shows lines <strong>6 to 10</strong>, not “5 to 10”.</li>
<li><code>awk '{FS="="}…'</code> sets the separator too late for the first line; <code>-F=</code> is the right way.</li>
<li>The <code>ll</code> alias was announced as <code>ls -alh</code> but defined as <code>ls -al</code>; it is now <code>ls -alh</code>.</li>
<li>The <code>admin</code> alias used <code>aptitude</code>; it now uses <code>apt</code>.</li>
<li>The SSH key type was RSA (<code>ssh-keygen -t rsa</code>); <code>ed25519</code> is the usual choice today, and <code>ssh-copy-id</code> replaces the manual steps.</li>
</ul>


</section>

 ]]></description>
  <category>archive</category>
  <category>bash</category>
  <category>Linux</category>
  <category>bioinformatics</category>
  <guid>https://kierczak.net/posts/archive-bash-ssh-basics/</guid>
  <pubDate>Mon, 31 Jan 2011 23:00:00 GMT</pubDate>
  <media:content url="https://kierczak.net/posts/archive-bash-ssh-basics/preview.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>All that modernism…</title>
  <dc:creator>Marcin Kierczak</dc:creator>
  <link>https://kierczak.net/posts/archive-modernism-poland/</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>From the archive
</div>
</div>
<div class="callout-body-container callout-body">
<p>First published on my old blog in October 2010; lightly edited for this site.</p>
</div>
</div>
<p>I really like Modernism. When I was a student, I used to live in a house built in the 1930s, designed according to the principles of Modernism and Functionalism, and I loved it. Then, walking the streets of Barcelona, I suddenly realised why I like it so much: it felt like being a student again, walking home from a nice party!</p>
<p>In Poland, Modernism has two phases: before 1939 and after 1945. The Second World War and Socrealism right after 1945 sit between the two layers of this modernist waffle. Below is a selection of the most famous buildings, and some of my favourites. Click a picture to enlarge it.</p>
<p>All pictures come from <a href="https://commons.wikimedia.org/">Wikimedia Commons</a>; the file names are those used there, where the author and licence of each image are listed.</p>
<div class="quarto-layout-panel" data-layout-ncol="3">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Bielsko-Biala_Zywiecka_23.jpg" class="lightbox" data-gallery="poland-modernism" title="Bielsko Biała - Żywiecka 23 St."><img src="https://kierczak.net/posts/archive-modernism-poland/images/Bielsko-Biala_Zywiecka_23.jpg" class="img-fluid figure-img" alt="Bielsko Biała - Żywiecka 23 St."></a></p>
<figcaption>Bielsko Biała - Żywiecka 23 St.</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Bydgoszcz_Gdanska_27.jpg" class="lightbox" data-gallery="poland-modernism" title="Bydgoszcz - Gdańska 27 St."><img src="https://kierczak.net/posts/archive-modernism-poland/images/Bydgoszcz_Gdanska_27.jpg" class="img-fluid figure-img" alt="Bydgoszcz - Gdańska 27 St."></a></p>
<figcaption>Bydgoszcz - Gdańska 27 St.</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Krakow_Muzeum_Narodowe_Krakow.jpg" class="lightbox" data-gallery="poland-modernism" title="Cracow - The National Museum"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Krakow_Muzeum_Narodowe_Krakow.jpg" class="img-fluid figure-img" alt="Cracow - The National Museum"></a></p>
<figcaption>Cracow - The National Museum</figcaption>
</figure>
</div>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Gdynia_Empik.jpg" class="lightbox" data-gallery="poland-modernism" title="Gdynia - EMPiK bookstore"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Gdynia_Empik.jpg" class="img-fluid figure-img" alt="Gdynia - EMPiK bookstore"></a></p>
<figcaption>Gdynia - EMPiK bookstore</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Gdynia_Office_building.jpg" class="lightbox" data-gallery="poland-modernism" title="Gdynia - former Polish Oceanic Lines building"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Gdynia_Office_building.jpg" class="img-fluid figure-img" alt="Gdynia - former Polish Oceanic Lines building"></a></p>
<figcaption>Gdynia - former Polish Oceanic Lines building</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_ASP.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - Fine Arts Academy"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_ASP.jpg" class="img-fluid figure-img" alt="Katowice - Fine Arts Academy"></a></p>
<figcaption>Katowice - Fine Arts Academy</figcaption>
</figure>
</div>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_Kosciol_garnizonowy.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - Garrison Church"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_Kosciol_garnizonowy.jpg" class="img-fluid figure-img" alt="Katowice - Garrison Church"></a></p>
<figcaption>Katowice - Garrison Church</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_Mickiewicza_21.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - Mickiewicza 21 St."><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_Mickiewicza_21.jpg" class="img-fluid figure-img" alt="Katowice - Mickiewicza 21 St."></a></p>
<figcaption>Katowice - Mickiewicza 21 St.</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_Slaska_Wyzsza_Szkola_Zarzadzania.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - Silesian High School of Management"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_Slaska_Wyzsza_Szkola_Zarzadzania.jpg" class="img-fluid figure-img" alt="Katowice - Silesian High School of Management"></a></p>
<figcaption>Katowice - Silesian High School of Management</figcaption>
</figure>
</div>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_Sejm_Slaski.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - Silesian Parliament"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_Sejm_Slaski.jpg" class="img-fluid figure-img" alt="Katowice - Silesian Parliament"></a></p>
<figcaption>Katowice - Silesian Parliament</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice__Gmach_Sejmu_Slaskiego.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - Silesian Parliament building"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice__Gmach_Sejmu_Slaskiego.jpg" class="img-fluid figure-img" alt="Katowice - Silesian Parliament building"></a></p>
<figcaption>Katowice - Silesian Parliament building</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_Sejm_Slaski_Sala_Marmurowa.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - Silesian Parliament, Marble Room"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_Sejm_Slaski_Sala_Marmurowa.jpg" class="img-fluid figure-img" alt="Katowice - Silesian Parliament, Marble Room"></a></p>
<figcaption>Katowice - Silesian Parliament, Marble Room</figcaption>
</figure>
</div>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_Gmach_Politechniki_Slaskiej.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - Silesian Polytechnic University"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_Gmach_Politechniki_Slaskiej.jpg" class="img-fluid figure-img" alt="Katowice - Silesian Polytechnic University"></a></p>
<figcaption>Katowice - Silesian Polytechnic University</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_Kukurydze.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - The Corns"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_Kukurydze.jpg" class="img-fluid figure-img" alt="Katowice - The Corns"></a></p>
<figcaption>Katowice - The Corns</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_Spodek.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - The Dish (or The UFO)"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_Spodek.jpg" class="img-fluid figure-img" alt="Katowice - The Dish (or The UFO)"></a></p>
<figcaption>Katowice - The Dish (or The UFO)</figcaption>
</figure>
</div>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_Gwiazdy.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - The Stars"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_Gwiazdy.jpg" class="img-fluid figure-img" alt="Katowice - The Stars"></a></p>
<figcaption>Katowice - The Stars</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_Osiedle_Gwiazdy.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - The Stars"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_Osiedle_Gwiazdy.jpg" class="img-fluid figure-img" alt="Katowice - The Stars"></a></p>
<figcaption>Katowice - The Stars</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_ul_Warszawska_28.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - Warszawska 28 St."><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_ul_Warszawska_28.jpg" class="img-fluid figure-img" alt="Katowice - Warszawska 28 St."></a></p>
<figcaption>Katowice - Warszawska 28 St.</figcaption>
</figure>
</div>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_Drapacz_Chmur.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - a skyscraper"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_Drapacz_Chmur.jpg" class="img-fluid figure-img" alt="Katowice - a skyscraper"></a></p>
<figcaption>Katowice - a skyscraper</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_Modernizm.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - an example of ‘upgraded’ Modernism"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_Modernizm.jpg" class="img-fluid figure-img" alt="Katowice - an example of ‘upgraded’ Modernism"></a></p>
<figcaption>Katowice - an example of ‘upgraded’ Modernism</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Katowice_BSK-ING_przy_Rynku.jpg" class="lightbox" data-gallery="poland-modernism" title="Katowice - building by the Market Square"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_BSK-ING_przy_Rynku.jpg" class="img-fluid figure-img" alt="Katowice - building by the Market Square"></a></p>
<figcaption>Katowice - building by the Market Square</figcaption>
</figure>
</div>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Kielce_Centrum_Kultury.jpg" class="lightbox" data-gallery="poland-modernism" title="Kielce - Culture Centre"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Kielce_Centrum_Kultury.jpg" class="img-fluid figure-img" alt="Kielce - Culture Centre"></a></p>
<figcaption>Kielce - Culture Centre</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Schronisko_Sniezka.jpg" class="lightbox" data-gallery="poland-modernism" title="Mt Śnieżka - astronomical observatory"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Schronisko_Sniezka.jpg" class="img-fluid figure-img" alt="Mt Śnieżka - astronomical observatory"></a></p>
<figcaption>Mt Śnieżka - astronomical observatory</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Poznan_Okraglak.jpg" class="lightbox" data-gallery="poland-modernism" title="Poznań - The “Okrąglak” (The Round Building)"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Poznan_Okraglak.jpg" class="img-fluid figure-img" alt="Poznań - The “Okrąglak” (The Round Building)"></a></p>
<figcaption>Poznań - The “Okrąglak” (The Round Building)</figcaption>
</figure>
</div>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Sosnowiec_Prezydium1.jpg" class="lightbox" data-gallery="poland-modernism" title="Sosnowiec - City Hall"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Sosnowiec_Prezydium1.jpg" class="img-fluid figure-img" alt="Sosnowiec - City Hall"></a></p>
<figcaption>Sosnowiec - City Hall</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Sosnowiec_ZUS.jpg" class="lightbox" data-gallery="poland-modernism" title="Sosnowiec - The Social Insurances Company building"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Sosnowiec_ZUS.jpg" class="img-fluid figure-img" alt="Sosnowiec - The Social Insurances Company building"></a></p>
<figcaption>Sosnowiec - The Social Insurances Company building</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Sosnowiec_Zwyciestwa.jpg" class="lightbox" data-gallery="poland-modernism" title="Sosnowiec - Zwycięstwa St."><img src="https://kierczak.net/posts/archive-modernism-poland/images/Sosnowiec_Zwyciestwa.jpg" class="img-fluid figure-img" alt="Sosnowiec - Zwycięstwa St."></a></p>
<figcaption>Sosnowiec - Zwycięstwa St.</figcaption>
</figure>
</div>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Torun_UMK_biblioteka.jpg" class="lightbox" data-gallery="poland-modernism" title="Toruń - University Library"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Torun_UMK_biblioteka.jpg" class="img-fluid figure-img" alt="Toruń - University Library"></a></p>
<figcaption>Toruń - University Library</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Warszawa_Sejm_detale_glowa_weza.jpg" class="lightbox" data-gallery="poland-modernism" title="Warszawa - Parliament, interior detail - snake"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Warszawa_Sejm_detale_glowa_weza.jpg" class="img-fluid figure-img" alt="Warszawa - Parliament, interior detail - snake"></a></p>
<figcaption>Warszawa - Parliament, interior detail - snake</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Warszawa_Sluzewiec.jpg" class="lightbox" data-gallery="poland-modernism" title="Warszawa - Służewiec horse-racing stadium"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Warszawa_Sluzewiec.jpg" class="img-fluid figure-img" alt="Warszawa - Służewiec horse-racing stadium"></a></p>
<figcaption>Warszawa - Służewiec horse-racing stadium</figcaption>
</figure>
</div>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Warszawa_Dom_Brukalskich_1927.jpg" class="lightbox" data-gallery="poland-modernism" title="Warszawa - The Brukalski’s Villa"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Warszawa_Dom_Brukalskich_1927.jpg" class="img-fluid figure-img" alt="Warszawa - The Brukalski’s Villa"></a></p>
<figcaption>Warszawa - The Brukalski’s Villa</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Warszawa_Muzeum_Narodowe.jpg" class="lightbox" data-gallery="poland-modernism" title="Warszawa - The National Museum"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Warszawa_Muzeum_Narodowe.jpg" class="img-fluid figure-img" alt="Warszawa - The National Museum"></a></p>
<figcaption>Warszawa - The National Museum</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Warszawa_Sejm_RP_front.jpg" class="lightbox" data-gallery="poland-modernism" title="Warszawa - The Parliament (Sejm)"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Warszawa_Sejm_RP_front.jpg" class="img-fluid figure-img" alt="Warszawa - The Parliament (Sejm)"></a></p>
<figcaption>Warszawa - The Parliament (Sejm)</figcaption>
</figure>
</div>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Warszawa_Hotel_Victoria.jpg" class="lightbox" data-gallery="poland-modernism" title="Warszawa - Victoria Hotel"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Warszawa_Hotel_Victoria.jpg" class="img-fluid figure-img" alt="Warszawa - Victoria Hotel"></a></p>
<figcaption>Warszawa - Victoria Hotel</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Warszawa_Dom_Wedla.jpg" class="lightbox" data-gallery="poland-modernism" title="Warszawa - Wedel’s House"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Warszawa_Dom_Wedla.jpg" class="img-fluid figure-img" alt="Warszawa - Wedel’s House"></a></p>
<figcaption>Warszawa - Wedel’s House</figcaption>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Zabrze_St._Josephs_Church_facade.jpg" class="lightbox" data-gallery="poland-modernism" title="Zabrze - St Joseph Church"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Zabrze_St._Josephs_Church_facade.jpg" class="img-fluid figure-img" alt="Zabrze - St Joseph Church"></a></p>
<figcaption>Zabrze - St Joseph Church</figcaption>
</figure>
</div>
</div>
</div>
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: flex-start;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="images/Zabrze_St_Josephs_Church_interior.jpg" class="lightbox" data-gallery="poland-modernism" title="Zabrze - St.&nbsp;Joseph church, interior"><img src="https://kierczak.net/posts/archive-modernism-poland/images/Zabrze_St_Josephs_Church_interior.jpg" class="img-fluid figure-img" alt="Zabrze - St.&nbsp;Joseph church, interior"></a></p>
<figcaption>Zabrze - St.&nbsp;Joseph church, interior</figcaption>
</figure>
</div>
</div>
</div>
</div>



 ]]></description>
  <category>archive</category>
  <category>architecture</category>
  <category>Poland</category>
  <guid>https://kierczak.net/posts/archive-modernism-poland/</guid>
  <pubDate>Thu, 28 Oct 2010 22:00:00 GMT</pubDate>
  <media:content url="https://kierczak.net/posts/archive-modernism-poland/images/Katowice_Spodek.jpg" medium="image" type="image/jpeg"/>
</item>
</channel>
</rss>
