<?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>Cormac Monaghan&#39;s blog</title>
<link>https://c-monaghan.github.io/atom.html</link>
<atom:link href="https://c-monaghan.github.io/atom.xml" rel="self" type="application/rss+xml"/>
<description>Cormac Monaghan&#39;s blog</description>
<language>en</language>
<generator>quarto-1.8.25</generator>
<lastBuildDate>Fri, 14 Mar 2025 00:00:00 GMT</lastBuildDate>
<item>
  <title>Doing multiple linear regression by hand</title>
  <dc:creator>Cormac Monaghan</dc:creator>
  <link>https://c-monaghan.github.io/posts/2025/03/14/MLR/</link>
  <description><![CDATA[ <p>In my last post I showed how to do <a href="https://c-monaghan.github.io/posts/2025/03/12/Linear-Regression/">simple linear regression by hand</a>. However, naturally, in most cases with statistics, you never have just one predictor variable, but rather multiple. As such, in this post, I will show how to do multiple linear regression by hand. Multiple linear regression is an extension of simple linear regression that allows for more than one predictor variable. It’s a powerful tool for understanding the relationship between several independent variables and a single dependent variable.</p>
<p>While the calculations can get a bit more involved, the core idea remains the same: we’re trying to find the best-fitting linear relationship between our predictors and the outcome.</p>
<section id="what-is-multiple-linear-regression" class="level2"><h2 class="anchored" data-anchor-id="what-is-multiple-linear-regression">What is multiple linear regression?</h2>
<p>If simple linear regression is like baking a cake with just flour, multiple linear regression is like adding sugar, eggs, and butter to the mix. It’s a richer and more complex way of understanding relationships in your data.</p>
<p>In the real world, things are rarely influenced by just one factor. For example, your happiness isn’t just determined by how much coffee you drink (though that’s a big part of it). It’s also influenced by how much sleep you got, how many emails are in your inbox, and whether your cat decided to knock over your favorite mug. Multiple linear regression helps us model these kinds of multi-faceted relationships.</p>
<section id="the-equation" class="level3"><h3 class="anchored" data-anchor-id="the-equation">The equation</h3>
<p>At its core, multiple linear regression is about finding the best-fitting linear relationship between <strong>multiple predictor variables</strong> (independent variables) and a <strong>response variable</strong> (dependent variable). The equation is similar to that of simple linear regression, but with more predictor variables:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AY%20=%20%5Cbeta_0%20+%20%5Cbeta_1X_1%20+%20%5Cbeta_2X_2%20+%20%5Cdots%20+%20%5Cbeta_jX_j%20+%20%5Cepsilon%0A"></p>
<p>Where:</p>
<ul>
<li>
<img src="https://latex.codecogs.com/png.latex?Y"> is your dependent variable—the thing you’re trying to predict or explain. (Think: your happiness score.)</li>
<li>
<img src="https://latex.codecogs.com/png.latex?%5Cbeta_0"> is the <strong>intercept</strong>. It’s the value of <img src="https://latex.codecogs.com/png.latex?Y"> when all the predictor variables are zero. (If you drank no coffee, got zero sleep, and had no emails, how happy would you be? Probably not very.)</li>
<li>
<img src="https://latex.codecogs.com/png.latex?%5Cbeta_1,%20%5Cbeta_2,%20%5Cdots,%20%5Cbeta_j"> are the <strong>coefficients</strong> for each predictor variable <img src="https://latex.codecogs.com/png.latex?(X_1,%20X_2,%20%5Cdots,%20X_j)">. They tell you how much each predictor contributes to YY. (For example, does coffee make you happier than sleep? These coefficients will tell you!)</li>
<li>
<img src="https://latex.codecogs.com/png.latex?%5Cepsilon"> is the error term. It accounts for the randomness in life that the model can’t explain. (Maybe your cat did something unpredictable, like actually not knocking over your mug.)</li>
</ul>
<p>The goal of multiple linear regression is to find the values of <img src="https://latex.codecogs.com/png.latex?%5Cbeta_0,%20%5Cbeta_1,%20%5Cdots,%20%5Cbeta_j"> that minimize the sum of squared errors—basically, the difference between what the model predicts <img src="https://latex.codecogs.com/png.latex?(%5Chat%7BY%7D)"> and what actually happens <img src="https://latex.codecogs.com/png.latex?(Y)">. In other words, <strong>we’re trying to make our predictions as accurate as possible</strong>.</p>
</section><section id="generating-some-data" class="level3"><h3 class="anchored" data-anchor-id="generating-some-data">Generating some data</h3>
<p>First, let’s again load some packages and set up our theme.</p>
<div class="cell">
<details class="code-fold"><summary>Check out my code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Loading packages</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></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;"><a href="https://ggplot2.tidyverse.org">ggplot2</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For plotting</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></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;"><a href="https://nanx.me/ggsci/">ggsci</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Going with a simpsons theme</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></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;"><a href="https://github.com/erocoar/gghalves">gghalves</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Using half plots</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></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;"><a href="https://dplyr.tidyverse.org">dplyr</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Data manipulation</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></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;"><a href="https://tidyr.tidyverse.org">tidyr</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Data manipulation</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Setting default theme</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/get_theme.html">theme_set</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggtheme.html">theme_minimal</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/theme.html">theme</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>      panel.grid.minor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">element_blank</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>      plot.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">element_text</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>hjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">rel</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>      axis.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">element_text</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">rel</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>      strip.background <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">element_rect</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>fill <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;">"grey80"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details>
</div>
<p>To illustrate multiple linear regression, let’s generate some data. For this example, let’s stick with the happiness theme we have going. We’ll create a dataset with three predictor variables <img src="https://latex.codecogs.com/png.latex?(X_1,%20X_2,%20X_3)"> representing daily coffee intake, sleep, and emails, and one response variable <img src="https://latex.codecogs.com/png.latex?(Y)"> representing daily happiness.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">321</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generating our predictor variables</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">coffee</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, sd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Coffee intake (cups per day)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sleep</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, sd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sleep (hours per night)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">emails</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, sd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Emails (received per day)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generating our response variable</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">happiness</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">coffee</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</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;">sleep</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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;">emails</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, sd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Creating data set</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">happiness_data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  happiness <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">ceiling</a></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;">happiness</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  coffee    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">floor</a></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;">coffee</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  sleep     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">ceiling</a></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;">sleep</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  emails    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">ceiling</a></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;">emails</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></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;">happiness_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">##   happiness coffee sleep emails</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 1         7      3     8     13</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 2        10      1     9     10</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 3         9      1     8     13</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 4        12      1    10      7</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 5         8      1     6      8</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 6         8      2     7     11</span></span></code></pre></div></div>
</div>
<p>Before we dive into regression, let’s take a moment to visualize our data. We’ll use a combination of boxplots and jittered points to show the distribution of each variable</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">happiness_long</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">happiness_data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    cols <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyselect.r-lib.org/reference/everything.html">everything</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    names_to <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;">"variable"</span>,</span>
<span>    values_to <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;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>variable <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">variable</span>, levels <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></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;">"coffee"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sleep"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"emails"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"happiness"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">happiness_plot</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">happiness_long</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <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;">variable</span>, y <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;">value</span>, fill <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;">variable</span>, colour <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;">variable</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gghalves</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/gghalves/man/geom_half_point.html">geom_half_point</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    transformation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggbeeswarm</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/ggbeeswarm/man/position_quasirandom.html">position_quasirandom</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    side <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;">"r"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gghalves</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/gghalves/man/geom_half_boxplot.html">geom_half_boxplot</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>side <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;">"l"</span>, colour <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://nanx.me/ggsci/reference/scale_simpsons.html">scale_fill_simpsons</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://nanx.me/ggsci/reference/scale_simpsons.html">scale_colour_simpsons</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/scale_continuous.html">scale_y_continuous</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>breaks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, by <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <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;">"Variables"</span>, y <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;">"Value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/guides.html">guides</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>fill <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;">"none"</span>, colour <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;">"none"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">happiness_plot</span></span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-visualisation" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-visualisation-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<a href="index_files/figure-html/fig-visualisation-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1" title="Figure&nbsp;1: Visualising the distribution of our variables"><img src="https://c-monaghan.github.io/posts/2025/03/14/MLR/index_files/figure-html/fig-visualisation-1.png" class="img-fluid figure-img" width="1152"></a>
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-visualisation-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Visualising the distribution of our variables
</figcaption></figure>
</div>
</div>
</div>
<p><strong>What Do We See?</strong></p>
<p>From the plot, we can start to get a sense of how our variables are distributed:</p>
<ul>
<li>Coffee intake hovers around 2-3 cups per day.</li>
<li>Sleep is mostly in the 7-8 hour range.</li>
<li>Emails are all over the place, because inboxes are chaos.</li>
<li>Happiness scores are a bit more spread out, reflecting the combined influence of coffee, sleep, and emails.</li>
</ul>
<p>This visualization sets the stage for our regression analysis. Next up, we’ll dive into the math and find out exactly how much each of these factors contributes to happiness.</p>
</section></section><section id="calculating-coefficients-using-equations" class="level2"><h2 class="anchored" data-anchor-id="calculating-coefficients-using-equations">Calculating coefficients (using equations)</h2>
<p>We’ll calculate the coefficients for the regression equation of the form:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Bhappiness%7D%20=%20%5Cbeta_0%20+%20%5Cbeta_1(%5Ctext%7Bcoffee%7D)%20+%20%5Cbeta_2(%5Ctext%7Bsleep%7D)%20+%20%5Cbeta_3(%5Ctext%7Bemails%7D)%0A"></p>
<p>In order to obtain these coefficients we use least squares estimation. This involves finding the values of <img src="https://latex.codecogs.com/png.latex?%5Cbeta_0,%20%5Cbeta_1,%20%5Cbeta_2,%20%5Cbeta_3"> that minimize the sum of squared errors between the observed happiness scores and the predicted happiness scores.</p>
<p><strong>Our formulas are</strong>:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%5Cbeta_0%20&amp;=%20%5Cbar%7By%7D%20-%20%5Cbeta_1%20%5Cbar%7Bx_1%7D%20-%20%5Cbeta_2%20%5Cbar%7Bx_2%7D%20-%20%5Cbeta_3%20%5Cbar%7Bx_3%7D%20%5C%5C%5B12pt%5D%0A%0A%5Cbeta_1%20&amp;=%20%5Cfrac%7BS_%7Bx_1%20y%7D%20S_%7Bx_2%20x_2%7D%20S_%7Bx_3%20x_3%7D%20-%20S_%7Bx_2%20y%7D%20S_%7Bx_1%20x_2%7D%20S_%7Bx_3%20x_3%7D%20-%20S_%7Bx_3%20y%7D%20S_%7Bx_1%20x_3%7D%20S_%7Bx_2%20x_2%7D%20+%20S_%7Bx_2%20y%7D%20S_%7Bx_1%20x_3%7D%20S_%7Bx_2%20x_3%7D%20+%20S_%7Bx_3%20y%7D%20S_%7Bx_1%20x_2%7D%20S_%7Bx_2%20x_3%7D%20-%20S_%7Bx_1%20y%7D%20S_%7Bx_2%20x_3%7D%5E2%7D%7BS_%7Bx_1%20x_1%7D%20S_%7Bx_2%20x_2%7D%20S_%7Bx_3%20x_3%7D%20-%20S_%7Bx_1%20x_2%7D%5E2%20S_%7Bx_3%20x_3%7D%20-%20S_%7Bx_1%20x_3%7D%5E2%20S_%7Bx_2%20x_2%7D%20+%202%20S_%7Bx_1%20x_2%7D%20S_%7Bx_1%20x_3%7D%20S_%7Bx_2%20x_3%7D%20-%20S_%7Bx_2%20x_3%7D%5E2%20S_%7Bx_1%20x_1%7D%7D%20%5C%5C%5B12pt%5D%0A%0A%5Cbeta_2%20&amp;=%20%5Cfrac%7BS_%7Bx_2%20y%7D%20S_%7Bx_1%20x_1%7D%20S_%7Bx_3%20x_3%7D%20-%20S_%7Bx_1%20y%7D%20S_%7Bx_1%20x_2%7D%20S_%7Bx_3%20x_3%7D%20-%20S_%7Bx_3%20y%7D%20S_%7Bx_1%20x_1%7D%20S_%7Bx_2%20x_3%7D%20+%20S_%7Bx_1%20y%7D%20S_%7Bx_1%20x_3%7D%20S_%7Bx_2%20x_3%7D%20+%20S_%7Bx_3%20y%7D%20S_%7Bx_1%20x_2%7D%20S_%7Bx_1%20x_3%7D%20-%20S_%7Bx_2%20y%7D%20S_%7Bx_1%20x_3%7D%5E2%7D%7BS_%7Bx_1%20x_1%7D%20S_%7Bx_2%20x_2%7D%20S_%7Bx_3%20x_3%7D%20-%20S_%7Bx_1%20x_2%7D%5E2%20S_%7Bx_3%20x_3%7D%20-%20S_%7Bx_1%20x_3%7D%5E2%20S_%7Bx_2%20x_2%7D%20+%202%20S_%7Bx_1%20x_2%7D%20S_%7Bx_1%20x_3%7D%20S_%7Bx_2%20x_3%7D%20-%20S_%7Bx_2%20x_3%7D%5E2%20S_%7Bx_1%20x_1%7D%7D%20%5C%5C%5B12pt%5D%0A%0A%5Cbeta_3%20&amp;=%20%5Cfrac%7BS_%7Bx_3%20y%7D%20S_%7Bx_1%20x_1%7D%20S_%7Bx_2%20x_2%7D%20-%20S_%7Bx_1%20y%7D%20S_%7Bx_1%20x_3%7D%20S_%7Bx_2%20x_2%7D%20-%20S_%7Bx_2%20y%7D%20S_%7Bx_1%20x_1%7D%20S_%7Bx_2%20x_3%7D%20+%20S_%7Bx_1%20y%7D%20S_%7Bx_1%20x_2%7D%20S_%7Bx_2%20x_3%7D%20+%20S_%7Bx_2%20y%7D%20S_%7Bx_1%20x_3%7D%20S_%7Bx_1%20x_2%7D%20-%20S_%7Bx_3%20y%7D%20S_%7Bx_1%20x_2%7D%5E2%7D%7BS_%7Bx_1%20x_1%7D%20S_%7Bx_2%20x_2%7D%20S_%7Bx_3%20x_3%7D%20-%20S_%7Bx_1%20x_2%7D%5E2%20S_%7Bx_3%20x_3%7D%20-%20S_%7Bx_1%20x_3%7D%5E2%20S_%7Bx_2%20x_2%7D%20+%202%20S_%7Bx_1%20x_2%7D%20S_%7Bx_1%20x_3%7D%20S_%7Bx_2%20x_3%7D%20-%20S_%7Bx_2%20x_3%7D%5E2%20S_%7Bx_1%20x_1%7D%7D%0A%0A%0A%5Cend%7Balign*%7D"></p>
<p>where:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0AS_%7Bx_1%20y%7D%20&amp;=%20%5Csum%20(x_1%20-%20%5Cbar%7Bx_1%7D)(y%20-%20%5Cbar%7By%7D)%20%5Cquad%20%5Ctext%7B(cross-deviations%20between%20coffee%20and%20happiness)%7D%20%5C%5C%0AS_%7Bx_2%20y%7D%20&amp;=%20%5Csum%20(x_2%20-%20%5Cbar%7Bx_2%7D)(y%20-%20%5Cbar%7By%7D)%20%5Cquad%20%5Ctext%7B(cross-deviations%20between%20sleep%20and%20happiness)%7D%20%5C%5C%0AS_%7Bx_3%20y%7D%20&amp;=%20%5Csum%20(x_3%20-%20%5Cbar%7Bx_3%7D)(y%20-%20%5Cbar%7By%7D)%20%5Cquad%20%5Ctext%7B(cross-deviations%20between%20emails%20and%20happiness)%7D%20%5C%5C%0AS_%7Bx_1%20x_1%7D%20&amp;=%20%5Csum%20(x_1%20-%20%5Cbar%7Bx_1%7D)%5E2%20%5Cquad%20%5Ctext%7B(squared%20deviations%20for%20coffee)%7D%20%5C%5C%0AS_%7Bx_2%20x_2%7D%20&amp;=%20%5Csum%20(x_2%20-%20%5Cbar%7Bx_2%7D)%5E2%20%5Cquad%20%5Ctext%7B(squared%20deviations%20for%20sleep)%7D%20%5C%5C%0AS_%7Bx_3%20x_3%7D%20&amp;=%20%5Csum%20(x_3%20-%20%5Cbar%7Bx_3%7D)%5E2%20%5Cquad%20%5Ctext%7B(squared%20deviations%20for%20emails)%7D%20%5C%5C%0AS_%7Bx_1%20x_2%7D%20&amp;=%20%5Csum%20(x_1%20-%20%5Cbar%7Bx_1%7D)(x_2%20-%20%5Cbar%7Bx_2%7D)%20%5Cquad%20%5Ctext%7B(cross-deviations%20between%20coffee%20and%20sleep)%7D%20%5C%5C%0AS_%7Bx_1%20x_3%7D%20&amp;=%20%5Csum%20(x_1%20-%20%5Cbar%7Bx_1%7D)(x_3%20-%20%5Cbar%7Bx_3%7D)%20%5Cquad%20%5Ctext%7B(cross-deviations%20between%20coffee%20and%20emails)%7D%20%5C%5C%0AS_%7Bx_2%20x_3%7D%20&amp;=%20%5Csum%20(x_2%20-%20%5Cbar%7Bx_2%7D)(x_3%20-%20%5Cbar%7Bx_3%7D)%20%5Cquad%20%5Ctext%7B(cross-deviations%20between%20sleep%20and%20emails)%7D%0A%5Cend%7Balign*%7D"></p>
<section id="step-1-computing-means" class="level3"><h3 class="anchored" data-anchor-id="step-1-computing-means">Step 1: Computing means</h3>
<p>First, calculate the means of happiness, coffee, sleep, and emails.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%5Cbar%7By%7D%20%20%20&amp;=%20%5Cfrac%7B%5Csum%20y_i%7D%7Bn%7D%20%20%20%20=%20%5Cfrac%7B7%20+%2010%20+%209%20+%20%5Cdots%20+%209%7D%7B10%7D%20%20%20=%20%5Cfrac%7B92%7D%7B10%7D%20=%20%209.2%20%5C%5C%5B12pt%5D%0A%5Cbar%7Bx_1%7D%20&amp;=%20%5Cfrac%7B%5Csum%20x_%7B1i%7D%7D%7Bn%7D%20=%20%5Cfrac%7B3%20+%201%20+%201%20+%20%5Cdots%20+%201%7D%7B10%7D%20%20%20%20=%20%5Cfrac%7B16%7D%7B10%7D%20=%201.6%20%20%5C%5C%5B12pt%5D%0A%5Cbar%7Bx_2%7D%20&amp;=%20%5Cfrac%7B%5Csum%20x_%7B2i%7D%7D%7Bn%7D%20=%20%5Cfrac%7B8%20+%209%20+%208%20+%20%5Cdots%20+%208%7D%7B10%7D%20%20%20%20=%20%5Cfrac%7B80%7D%7B10%7D%20=%208%20%20%20%20%5C%5C%5B12pt%5D%0A%5Cbar%7Bx_3%7D%20&amp;=%20%5Cfrac%7B%5Csum%20x_%7B3i%7D%7D%7Bn%7D%20=%20%5Cfrac%7B13%20+%2010%20+%2013%20+%20%5Cdots%20+%209%7D%7B10%7D%20=%20%5Cfrac%7B94%7D%7B10%7D%20=%209.4%0A%5Cend%7Balign*%7D"></p>
</section><section id="step-2-computing-deviations" class="level3"><h3 class="anchored" data-anchor-id="step-2-computing-deviations">Step 2: Computing deviations</h3>
<p>Next, we calculate the deviations for each variable from their mean.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 13%">
<col style="width: 21%">
<col style="width: 20%">
<col style="width: 22%">
<col style="width: 22%">
</colgroup>
<thead><tr class="header">
<th>Observation</th>
<th><img src="https://latex.codecogs.com/png.latex?y%20-%20%5Cbar%7By%7D"></th>
<th><img src="https://latex.codecogs.com/png.latex?x_1%20-%20%5Cbar%7Bx_1%7D"></th>
<th><img src="https://latex.codecogs.com/png.latex?x_2%20-%20%5Cbar%7Bx_2%7D"></th>
<th><img src="https://latex.codecogs.com/png.latex?x_3%20-%20%5Cbar%7Bx_3%7D"></th>
</tr></thead>
<tbody>
<tr class="odd">
<td>1</td>
<td>7 - 9.2 = -2.2</td>
<td>3 - 1.6 = 1.4</td>
<td>8 - 8 = 0</td>
<td>13 - 9.4 = 3.6</td>
</tr>
<tr class="even">
<td>2</td>
<td>10 - 9.2 = 0.8</td>
<td>1 - 1.6 = -0.6</td>
<td>9 - 8 = 1</td>
<td>10 - 9.4 = 0.6</td>
</tr>
<tr class="odd">
<td>3</td>
<td>9 - 9.2 = -0.2</td>
<td>1 - 1.6 = -0.6</td>
<td>8 - 8 = 0</td>
<td>13 - 9.4 = 3.6</td>
</tr>
<tr class="even">
<td>4</td>
<td>12 - 9.2 = 2.8</td>
<td>1 - 1.6 = -0.6</td>
<td>10 - 8 = 2</td>
<td>7 - 9.4 = -2.4</td>
</tr>
<tr class="odd">
<td>5</td>
<td>8 - 9.2 = -1.2</td>
<td>1 - 1.6 = -0.6</td>
<td>6 - 8 = -2</td>
<td>8 - 9.4 = -1.4</td>
</tr>
<tr class="even">
<td>6</td>
<td>8 - 9.2 = -1.2</td>
<td>2 - 1.6 = 0.4</td>
<td>7 - 8 = -1</td>
<td>11 - 9.4 = 1.6</td>
</tr>
<tr class="odd">
<td>7</td>
<td>8 - 9.2 = -1.2</td>
<td>2 - 1.6 = 0.4</td>
<td>8 - 8 = 0</td>
<td>4 - 9.4 = -5.4</td>
</tr>
<tr class="even">
<td>8</td>
<td>11 - 9.2 = 1.8</td>
<td>2 - 1.6 = 0.4</td>
<td>8 - 8 = 0</td>
<td>12 - 9.4 = 2.6</td>
</tr>
<tr class="odd">
<td>9</td>
<td>10 - 9.2 = 0.8</td>
<td>2 - 1.6 = 0.4</td>
<td>8 - 8 = 0</td>
<td>7 - 9.4 = -2.4</td>
</tr>
<tr class="even">
<td>10</td>
<td>9 - 9.2 = -0.2</td>
<td>1 - 1.6 = -0.6</td>
<td>8 - 8 = 0</td>
<td>9 - 9.4 = -0.4</td>
</tr>
</tbody>
</table></section><section id="step-3-computing-cross-deviations" class="level3"><h3 class="anchored" data-anchor-id="step-3-computing-cross-deviations">Step 3: Computing cross deviations</h3>
<p>Now that we know our means and deviations we are able to solve for our set of cross-deviations.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0AS_%7Bx_1%20y%7D%20&amp;=%20(1.4%20%5Ctimes%20-2.2)%20+%20(-0.6%20%5Ctimes%200.8)%20+%20(-0.6%20%5Ctimes%20-0.2)%20+%20%5Cdots%20(-0.6%20%5Ctimes%20-0.2)%20=%20-4.2%20%5C%5C%5B12pt%5D%0A%0AS_%7Bx_2%20y%7D%20&amp;=%20(0%20%5Ctimes%20-2.2)%20+%20(1%20%5Ctimes%200.8)%20+%20(0%20%5Ctimes%20-0.2)%20+%20%5Cdots%20+%20(0%20%5Ctimes%20-0.2)%20=%2010%20%5C%5C%5B12pt%5D%0A%0AS_%7Bx_3%20y%7D%20&amp;=%20(3.6%20%5Ctimes%20-2.2)%20+%20(0.6%20%5Ctimes%200.8)%20+%20(3.6%20%5Ctimes%20-0.2)%20%5Cdots%20+%20(-0.4%20%5Ctimes%20-0.2)%20=%20-5.8%20%5C%5C%5B24pt%5D%0A%0AS_%7Bx_1x_2%7D%20&amp;=%20(1.4%20%5Ctimes%200)%20+%20(-0.6%20%5Ctimes%201)%20+%20(-0.6%20%5Ctimes%200)%20+%20%5Cdots%20(-0.6%20%5Ctimes%200)%20=%20-1%20%5C%5C%5B12pt%5D%0A%0AS_%7Bx_1x_3%7D%20&amp;=%20(1.4%20%5Ctimes%203.6)%20+%20(-0.6%20%5Ctimes%200.6)%20+%20(-0.6%20%5Ctimes%203.6)%20+%20%5Cdots%20+%20(-0.6%20%5Ctimes%20-0.4)%20=%203.6%20%5C%5C%5B12pt%5D%0A%0AS_%7Bx_2x_3%7D%20&amp;=%20(0%20%5Ctimes%203.6)%20+%20(1%20%5Ctimes%200.6)%20+%20(0%20%5Ctimes%203.6)%20+%20%5Cdots%20(0%20%5Ctimes%20-0.4)%20=%20-3%0A%5Cend%7Balign*%7D"></p>
</section><section id="step-4-computing-squared-deviations" class="level3"><h3 class="anchored" data-anchor-id="step-4-computing-squared-deviations">Step 4: Computing squared deviations</h3>
<p>Finally, we can calculate the squared deviations for each variable.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0AS_%7Bx_1%20x_1%7D%20&amp;=%20(1.4%5E2)%20+%20(-0.6%5E2)%20+%20(-0.6%5E2)%20+%20%5Cdots%20+%20(-0.6%5E2)%20=%204.4%20%5C%5C%5B12pt%5D%0A%0AS_%7Bx_2%20x_2%7D%20&amp;=%20(0%5E2)%20+%20(1%5E2)%20+%20(0%5E2)%20+%20%5Cdots%20+%20(0%5E2)%20=%2010%20%5C%5C%5B12pt%5D%0A%0AS_%7Bx_3%20x_3%7D%20&amp;=%20(3.6%5E2)%20+%20(0.6%5E2)%20+%20(3.6%5E2)%20+%20%5Cdots%20+%20(-0.4%5E2)%20=%2078.4%20%5C%5C%5B12pt%5D%0A%5Cend%7Balign*%7D"></p>
</section><section id="step-5-computing-our-coefficients" class="level3"><h3 class="anchored" data-anchor-id="step-5-computing-our-coefficients">Step 5: Computing our coefficients</h3>
<p>Now that we have all the necessary values, we can calculate our coefficients.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%5Cbeta_1%20&amp;=%20%5Cfrac%7B-4.2%20%5Ctimes%2010%20%5Ctimes%2078.4%20-%2010%20%5Ctimes%20-1%20%5Ctimes%20-3%20-%205.8%20%5Ctimes%204%20%5Ctimes%2010%20+%2010%20%5Ctimes%203.6%20%5Ctimes%20-3%20-%205.8%20%5Ctimes%20-1%20%5Ctimes%20-3%20+%204.2%20%5Ctimes%20(-3)%5E2%7D%7B4.4%20%5Ctimes%2010%20%5Ctimes%2078.4%20-%2010%20%5Ctimes%20(-1)%5E2%20%5Ctimes%2078.4%20-%2010%20%5Ctimes%203.6%5E2%20+%202%20%5Ctimes%20-1%20%5Ctimes%203.6%20%5Ctimes%20-3%20-%203%5E2%20%5Ctimes%2010%7D%20%5Capprox%20-0.74%20%5C%5C%5B12pt%5D%0A%0A%5Cbeta_2%20&amp;=%20%5Cfrac%7B10%20%5Ctimes%204.4%20%5Ctimes%2078.4%20-%20-4.2%20%5Ctimes%20-1%20%5Ctimes%2078.4%20-%205.8%20%5Ctimes%204.4%20%5Ctimes%20-3%20+%20-4.2%20%5Ctimes%203.6%20%5Ctimes%20-3%20+%205.8%20%5Ctimes%20-1%20%5Ctimes%20-1%20-%2010%20%5Ctimes%20(-3)%5E2%7D%7B4.4%20%5Ctimes%2010%20%5Ctimes%2078.4%20-%2010%20%5Ctimes%20(-1)%5E2%20%5Ctimes%2078.4%20-%2010%20%5Ctimes%203.6%5E2%20+%202%20%5Ctimes%20-1%20%5Ctimes%203.6%20%5Ctimes%20-3%20-%203%5E2%20%5Ctimes%2010%7D%20%5Capprox%200.92%20%5C%5C%5B12pt%5D%0A%0A%5Cbeta_3%20&amp;=%20%5Cfrac%7B-5.8%20%5Ctimes%204.4%20%5Ctimes%2010%20-%20-4.2%20%5Ctimes%203.6%20%5Ctimes%2010%20-%2010%20%5Ctimes%204.4%20%5Ctimes%20-3%20+%20-4.2%20%5Ctimes%20-1%20%5Ctimes%20-3%20+%2010%20%5Ctimes%203.6%20%5Ctimes%20-1%20-%2010%20%5Ctimes%20(-3)%5E2%7D%7B4.4%20%5Ctimes%2010%20%5Ctimes%2078.4%20-%2010%20%5Ctimes%20(-1)%5E2%20%5Ctimes%2078.4%20-%2010%20%5Ctimes%203.6%5E2%20+%202%20%5Ctimes%20-1%20%5Ctimes%203.6%20%5Ctimes%20-3%20-%203%5E2%20%5Ctimes%2010%7D%20%5Capprox%20-0.005%20%5C%5C%5B12pt%5D%0A%0A%5Cbeta_0%20&amp;=%209.2%20-%20(-0.74%20%5Ctimes%201.6)%20-%200.92%20%5Ctimes%208%20-%20(-0.005%20%5Ctimes%209.4)%20=%209.2%20+%201.924%20-%207.36%20+%200.047%20%5Capprox%203.81%0A%5Cend%7Balign*%7D"></p>
</section><section id="step-6-writing-our-regression-equation" class="level3"><h3 class="anchored" data-anchor-id="step-6-writing-our-regression-equation">Step 6: Writing our regression equation</h3>
<p>Finally, we can write our regression equation:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Chat%7BY%7D%20=%203.07%20-%200.74X_1%20+%200.92X_2%20-%200.005X_3"> Phew, that was a lot of work!!</p>
<p><strong>But what does this now tells us??</strong></p>
<ul>
<li>For every additional cup of coffee you drink, your happiness score decreases by 0.74.</li>
<li>For every additional hour of sleep you get, your happiness score increases by 0.92.</li>
<li>For every additional email you receive, your happiness score decreases by 0.005.</li>
</ul>
<p>Let’s verify this by running the <code><a href="https://rdrr.io/r/stats/lm.html">lm()</a></code> function in R</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/lm.html">lm</a></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;">happiness</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;">coffee</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;">sleep</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;">emails</span>, data <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;">happiness_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">broom</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://generics.r-lib.org/reference/tidy.html">tidy</a></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;">fit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## # A tibble: 4 × 5</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">##   term        estimate std.error statistic p.value</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">##   &lt;chr&gt;          &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 1 (Intercept)  3.03        3.75     0.808   0.450 </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 2 coffee      -0.741       0.608   -1.22    0.269 </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 3 sleep        0.925       0.398    2.32    0.0591</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 4 emails      -0.00459     0.143   -0.0321  0.975</span></span></code></pre></div></div>
</div>
<p>The coefficients are very close to what we calculated by hand.</p>
</section></section><section id="calculating-coefficients-using-matrices" class="level2"><h2 class="anchored" data-anchor-id="calculating-coefficients-using-matrices">Calculating coefficients (using matrices)</h2>
<p>While the manual method of calculating coefficients for multiple linear regression is a great way to understand the underlying mechanics, it can become tedious, especially when dealing with multiple predictor variables. Imagine having five or six predictors—the calculations would quickly become overwhelming! Fortunately, matrices provide a more efficient and streamlined approach.</p>
<section id="step-1-setting-up-our-matrices" class="level3"><h3 class="anchored" data-anchor-id="step-1-setting-up-our-matrices">Step 1: Setting up our matrices</h3>
<p>To begin, let’s define the key components of our regression model:</p>
<ul>
<li>
<img src="https://latex.codecogs.com/png.latex?y"> as the response variable</li>
<li>
<img src="https://latex.codecogs.com/png.latex?X"> as the matrix of predictor variables</li>
<li>
<img src="https://latex.codecogs.com/png.latex?%5Cbeta"> as the vector of coefficients we want to estimate</li>
<li>
<img src="https://latex.codecogs.com/png.latex?%5Cepsilon"> as the error term</li>
</ul>
<p>The regression model can be expressed in matrix form as:</p>
<p><img src="https://latex.codecogs.com/png.latex?y%20=%20X%20%5Cbeta%20+%20%5Cepsilon"></p>
<p>Our goal is to solve for <img src="https://latex.codecogs.com/png.latex?%5Cbeta">, the vector of coefficients. The least squares solution for <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> is given by:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%20(X%5ETX)%5E%7B-1%7D%20X%5ETy"> Let’s define the <img src="https://latex.codecogs.com/png.latex?X"> matrix (including our intercept) and <img src="https://latex.codecogs.com/png.latex?y"> matrix</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AX%20=%20%5Cbegin%7Bbmatrix%7D%0A1%20&amp;%203%20&amp;%208%20&amp;%2013%20%5C%5C%0A1%20&amp;%201%20&amp;%209%20&amp;%2010%20%5C%5C%0A1%20&amp;%201%20&amp;%208%20&amp;%2013%20%5C%5C%0A1%20&amp;%201%20&amp;%2010%20&amp;%207%20%5C%5C%0A1%20&amp;%201%20&amp;%206%20&amp;%208%20%5C%5C%0A1%20&amp;%202%20&amp;%207%20&amp;%2011%20%5C%5C%0A1%20&amp;%202%20&amp;%208%20&amp;%204%20%5C%5C%0A1%20&amp;%202%20&amp;%208%20&amp;%2012%20%5C%5C%0A1%20&amp;%202%20&amp;%208%20&amp;%207%20%5C%5C%0A1%20&amp;%201%20&amp;%208%20&amp;%209%20%5C%5C%0A%5Cend%7Bbmatrix%7D%20%5Cqquad%0Ay%20=%20%5Cbegin%7Bbmatrix%7D%0A7%20%5C%5C%0A10%20%5C%5C%0A9%20%5C%5C%0A12%20%5C%5C%0A8%20%5C%5C%0A8%20%5C%5C%0A8%20%5C%5C%0A11%20%5C%5C%0A10%20%5C%5C%0A9%20%5C%5C%0A%5Cend%7Bbmatrix%7D%0A"></p>
</section><section id="step-2-transposing-x" class="level3"><h3 class="anchored" data-anchor-id="step-2-transposing-x">Step 2: Transposing <img src="https://latex.codecogs.com/png.latex?X">
</h3>
<p>The next step is to compute the transpose of <img src="https://latex.codecogs.com/png.latex?X"> denoted as <img src="https://latex.codecogs.com/png.latex?X%5ET">. Transposing a matrix involves flipping its rows and columns. For our <img src="https://latex.codecogs.com/png.latex?X"> matrix, this results in:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AX%5ET%20=%20%5Cbegin%7Bbmatrix%7D%0A1%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20%5C%5C%0A3%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%202%20&amp;%202%20&amp;%202%20&amp;%202%20&amp;%201%20%5C%5C%0A8%20&amp;%209%20&amp;%208%20&amp;%2010%20&amp;%206%20&amp;%207%20&amp;%208%20&amp;%208%20&amp;%208%20&amp;%208%20%5C%5C%0A13%20&amp;%2010%20&amp;%2013%20&amp;%207%20&amp;%208%20&amp;%2011%20&amp;%204%20&amp;%2012%20&amp;%207%20&amp;%209%20%5C%5C%0A%5Cend%7Bbmatrix%7D%0A"></p>
</section><section id="step-3-computing-matrix-products" class="level3"><h3 class="anchored" data-anchor-id="step-3-computing-matrix-products">Step 3: Computing matrix products</h3>
<p>With <img src="https://latex.codecogs.com/png.latex?X%5ET"> calculated, we now compute two key matrix products:</p>
<ul>
<li>
<img src="https://latex.codecogs.com/png.latex?X%5ETX">: This product of the transposed predictor matrix and original predictor matrix.</li>
<li>
<img src="https://latex.codecogs.com/png.latex?X%5ETy">: This product of the transposed predictor matrix and the response variable matrix.</li>
</ul>
<p>Matrix multiplication involves multiplying corresponding elements of each row and column. For example, the first element of the resulting matrix is the sum of the products of the first row of the first matrix <img src="https://latex.codecogs.com/png.latex?X%5ETX"> and the first column of the second matrix <img src="https://latex.codecogs.com/png.latex?X">. To illustrate, the first two elements of <img src="https://latex.codecogs.com/png.latex?X%5ETX"> would be:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A(1%20%5Ctimes%201)%20+%20(1%20%5Ctimes%201)%20+%20%5Cdots%20+%20(1%20%5Ctimes%201)%20=%2010%20%5C%5C%5B8pt%5D%0A(1%20%5Ctimes%203)%20+%20(1%20%5Ctimes%201)%20+%20%5Cdots%20+%20(1%20%5Ctimes%201)%20=%2016%0A"></p>
<p>Continuing this process for all elements of <img src="https://latex.codecogs.com/png.latex?X%5ETX"> and <img src="https://latex.codecogs.com/png.latex?X%5ETy"> results in:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AX%5ET%20X%20=%20%5Cbegin%7Bbmatrix%7D%0A10%20&amp;%2016%20&amp;%2080%20&amp;%2094%20%5C%5C%0A16%20&amp;%2030%20&amp;%20127%20&amp;%20154%20%5C%5C%0A80%20&amp;%20127%20&amp;%20650%20&amp;%20749%20%5C%5C%0A94%20&amp;%20154%20&amp;%20749%20&amp;%20962%20%5C%5C%0A%5Cend%7Bbmatrix%7D%20%5Cqquad%0AX%5ET%20y%20=%20%5Cbegin%7Bbmatrix%7D%0A92%20%5C%5C%0A143%20%5C%5C%0A746%20%5C%5C%0A859%20%5C%5C%0A%5Cend%7Bbmatrix%7D%0A"></p>
</section><section id="step-4-computing-the-inverse" class="level3"><h3 class="anchored" data-anchor-id="step-4-computing-the-inverse">Step 4: Computing the inverse</h3>
<p>The next step is to compute the inverse of <img src="https://latex.codecogs.com/png.latex?X%5ETX">, otherwise denoted as <img src="https://latex.codecogs.com/png.latex?(X%5ETX)%5E%7B-1%7D">. The inverse of a matrix is a special matrix that, when multiplied by the original matrix, yields the identity matrix. In other words:</p>
<p><img src="https://latex.codecogs.com/png.latex?(X%5ETX)%5E%7B-1%7D(X%5ETX)%20=%20I"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?I"> is the identity matrix. The inverse of a matrix can be computed using various methods, with <a href="https://en.wikipedia.org/wiki/Gaussian_elimination">Gaussian elimination</a> being one of the most common. Gaussian elimination transforms the matrix into row echelon form through a series of row operations, ultimately reducing it to the identity matrix.</p>
<ul>
<li>Start by augmenting <img src="https://latex.codecogs.com/png.latex?X%5ETX"> with an identity matrix <img src="https://latex.codecogs.com/png.latex?I"> such that <img src="https://latex.codecogs.com/png.latex?%5BX%5ETX%20%5C:%20%5Cvert%20%5C:%20I%5D">:</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5BX%5ETX%20%5C:%20%5Cvert%20%5C:%20I%5D%20=%20%5Cbegin%7Bbmatrix%7D%0A10%20&amp;%2016%20&amp;%2080%20&amp;%2094%20&amp;%20%5Cvert%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A16%20&amp;%2030%20&amp;%20127%20&amp;%20154%20&amp;%20%5Cvert%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20%5C%5C%0A80%20&amp;%20127%20&amp;%20650%20&amp;%20749%20&amp;%20%5Cvert%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20%5C%5C%0A94%20&amp;%20154%20&amp;%20749%20&amp;%20962%20&amp;%20%5Cvert%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20%5C%5C%0A%5Cend%7Bbmatrix%7D%0A"></p>
<ul>
<li>Perform row operations to reduce the left side of the matrix to the identity matrix, resulting in the inverse of <img src="https://latex.codecogs.com/png.latex?X%5ETX"> on the right side.</li>
<li>The resulting matrix on the right side will be the inverse of <img src="https://latex.codecogs.com/png.latex?X%5ETX">.</li>
</ul>
<p>For my own mental sanity I will not perform this operation by hand, but rather use the <code><a href="https://rdrr.io/r/base/solve.html">solve()</a></code> function in R to calculate the inverse of <img src="https://latex.codecogs.com/png.latex?X%5ETX">.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">XT_X</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/matrix.html">matrix</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">94</span>,</span>
<span>  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">127</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">154</span>,</span>
<span>  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">127</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">650</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">749</span>,</span>
<span>  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">94</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">154</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">749</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">962</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, nrow <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, ncol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, byrow <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/solve.html">solve</a></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;">XT_X</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>digits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">##        [,1]   [,2]   [,3]   [,4]</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## [1,]  9.162 -0.456 -0.885 -0.133</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## [2,] -0.456  0.240  0.021 -0.010</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## [3,] -0.885  0.021  0.103  0.003</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## [4,] -0.133 -0.010  0.003  0.013</span></span></code></pre></div></div>
</div>
<p>We can then write <img src="https://latex.codecogs.com/png.latex?(X%5ETX)%5E%7B-1%7D"> as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A(X%5ETX)%5E%7B-1%7D%20=%20%5Cbegin%7Bbmatrix%7D%0A9.162%20&amp;%20-0.446%20&amp;%20-0.885%20&amp;%20-0.133%20%5C%5C%0A-0.456%20&amp;%200.240%20&amp;%200.021%20&amp;%20-0.010%20%5C%5C%0A-0.885%20&amp;%200.021%20&amp;%200.103%20&amp;%200.003%20%5C%5C%0A-0.133%20&amp;%20-0.010%20&amp;%200.003%20&amp;%200.013%0A%5Cend%7Bbmatrix%7D%0A"></p>
</section><section id="step-5-computing-beta" class="level3"><h3 class="anchored" data-anchor-id="step-5-computing-beta">Step 5: Computing <img src="https://latex.codecogs.com/png.latex?%5Cbeta">
</h3>
<p>With <img src="https://latex.codecogs.com/png.latex?(X%5ETX)%5E%7B-1%7D"> calculated, we can now compute <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> using the formula:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%20(X%5ETX)%5E%7B-1%7D%20(X%5ETy)"></p>
<p>Multiplying <img src="https://latex.codecogs.com/png.latex?(X%5ETX)%5E%7B-1%7D"> by <img src="https://latex.codecogs.com/png.latex?X%5ETy"> gives us:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta%20=%20%5Cbegin%7Bbmatrix%7D%0A3.239%20%5C%5C%0A-0.556%20%5C%5C%0A0.998%20%5C%5C%0A-0.261%20%5C%5C%0A%5Cend%7Bbmatrix%7D%0A"></p>
</section><section id="step-6-writing-our-regression-equation-1" class="level3"><h3 class="anchored" data-anchor-id="step-6-writing-our-regression-equation-1">Step 6: Writing our regression equation</h3>
<p>Finally, we can write our regression equation:</p>
<p><img src="https://latex.codecogs.com/png.latex?y%20=%203.239%20-%200.556X_1%20+%200.998X_2%20-%200.261X_3"></p>
<p><strong>But wait, this isn’t what we got by doing it by hand, nor does it match the <code><a href="https://rdrr.io/r/stats/lm.html">lm()</a></code> output.</strong></p>
<p>This is because the <code><a href="https://rdrr.io/r/base/solve.html">solve()</a></code> function in R is able to calculate the inverse of a matrix without any rounding errors. This is not the case when doing it by hand. If I removed <code>|&gt; round(digits = 3)</code> from the above we get a much more precise estimate of <img src="https://latex.codecogs.com/png.latex?(X%5ETX)%5E%7B-1%7D"></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/solve.html">solve</a></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;">XT_X</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">##            [,1]        [,2]         [,3]         [,4]</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## [1,]  9.1623030 -0.45619804 -0.885469661 -0.132832858</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## [2,] -0.4561980  0.24041444  0.020970344 -0.010237002</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## [3,] -0.8854697  0.02097034  0.102990445  0.002978037</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## [4,] -0.1328329 -0.01023700  0.002978037  0.013339124</span></span></code></pre></div></div>
</div>
<p>and then ultimately an exact match to the <code><a href="https://rdrr.io/r/stats/lm.html">lm()</a></code> output</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">XT_y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/matrix.html">matrix</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">92</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">143</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">746</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">859</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, nrow <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, ncol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, byrow <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/solve.html">solve</a></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;">XT_X</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/matmult.html">%*%</a></span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">XT_y</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>            [,1]
[1,]  3.03176573
[2,] -0.74066261
[3,]  0.92455640
[4,] -0.00459114</code></pre>
</div>
</div>
<p>So finally, we could say our regression equation is:</p>
<p><img src="https://latex.codecogs.com/png.latex?y%20=%203.03%20-%200.74X_1%20+%200.92X_2%20-%200.005X_3"></p>
<p>Just goes to show how much rounding makes a difference!</p>


<!-- -->

</section></section><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{monaghan2025,
  author = {Monaghan, Cormac},
  title = {Doing Multiple Linear Regression by Hand},
  date = {2025-03-14},
  url = {https://c-monaghan.github.io/posts/2025/03/14/MLR/},
  doi = {10.59350/63ca3-q4n51},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-monaghan2025" class="csl-entry quarto-appendix-citeas">
Monaghan, Cormac. 2025. <span>“Doing Multiple Linear Regression by
Hand.”</span> March 14, 2025. <a href="https://doi.org/10.59350/63ca3-q4n51">https://doi.org/10.59350/63ca3-q4n51</a>.
</div></div></section></div> ]]></description>
  <category>statistics</category>
  <category>regression</category>
  <category>r</category>
  <guid>https://c-monaghan.github.io/posts/2025/03/14/MLR/</guid>
  <pubDate>Fri, 14 Mar 2025 00:00:00 GMT</pubDate>
  <media:content url="https://c-monaghan.github.io/posts/2025/thumbnails/regression_mlr_thumb.png" medium="image" type="image/png" height="96" width="144"/>
</item>
<item>
  <title>Doing linear regression by hand</title>
  <dc:creator>Cormac Monaghan</dc:creator>
  <link>https://c-monaghan.github.io/posts/2025/03/12/Linear-Regression/</link>
  <description><![CDATA[ <p>For the past few years I’ve been a statistics tutor in the psychology department at Maynooth University. We cover everything from the basics of descriptive statistics and graphs to the nitty-gritty of t-tests, correlations, and ANOVAs. However, there’s one topic where I always slow down and dive deep: regression analysis. Why? Because linear regression is basically the Swiss Army knife of statistics—it’s everywhere, it’s versatile, and it’s way cooler than it sounds.</p>
<p>Seriously, linear regression is like that one friend who shows up at every party. Lectures? Check. Assignments? Obviously. Research papers? You bet. Even casual chats about data somehow always circle back to it. It’s the ultimate tool for understanding how variables play together. Whether you’re predicting exam scores based on study hours (or lack thereof) or calculating how much coffee it takes to survive finals week (spoiler: <em>a lot</em>), regression has your back.</p>
<p>One thing I always like doing is having students calculate regression coefficients by hand. It’s a great way to understand the mechanics of the model and appreciate the magic of least squares. But, full disclosure: I <em>always</em> forget the formulas mid-session and end up frantically Googling them. So, I decided to write them down here—partly for fun, partly as a future reference for myself (<a href="https://media.tenor.com/0CpFOKGVaeMAAAAi/hand-waving-hand.gif%5D">hey future me!!</a>).</p>
<section id="the-linear-regression-model" class="level2"><h2 class="anchored" data-anchor-id="the-linear-regression-model">The linear regression model</h2>
<p>At its core, linear regression is all about finding relationships between variables. The model is a simple equation that describes how a dependent variable <img src="https://latex.codecogs.com/png.latex?(Y)"> relates to one or more independent variables <img src="https://latex.codecogs.com/png.latex?(X)">. In its simplest form, it looks like this:</p>
<p><img src="https://latex.codecogs.com/png.latex?%20Y%20=%20%5Cbeta_0%20+%20%5Cbeta_1X%20+%20%5Cepsilon%20"></p>
<p>where:</p>
<ul>
<li>
<img src="https://latex.codecogs.com/png.latex?Y"> the dependent variable (the thing you’re trying to predict).</li>
<li>
<img src="https://latex.codecogs.com/png.latex?X"> the independent variable (the thing you’re using to make predictions).</li>
<li>
<img src="https://latex.codecogs.com/png.latex?%5Cbeta_0"> is the intercept (where the line crosses the Y-axis).</li>
<li>
<img src="https://latex.codecogs.com/png.latex?%5Cbeta_1"> is the slope (how much <img src="https://latex.codecogs.com/png.latex?Y"> changes for every unit change in <img src="https://latex.codecogs.com/png.latex?X">).</li>
<li>
<img src="https://latex.codecogs.com/png.latex?%5Cepsilon"> the error term (because let’s face it, life is messy).</li>
</ul>
<section id="generating-some-data" class="level3"><h3 class="anchored" data-anchor-id="generating-some-data">Generating some data</h3>
<p>First let’s load some packages and set our theme</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Loading packages</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></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;"><a href="https://ggplot2.tidyverse.org">ggplot2</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Setting default theme</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/get_theme.html">theme_set</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggtheme.html">theme_minimal</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/theme.html">theme</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>      plot.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">element_text</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>hjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">rel</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>      axis.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">element_text</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">rel</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>To make this more concrete, let’s create a small dataset in R. We’ll simulate some data for study hours and exam scores, and then use linear regression to model the relationship between them.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hours of study per week (our x variable)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">study_hours</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, sd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Exam scores (our y variable)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">exam_scores</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">study_hours</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, sd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">test_data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  study_hours <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">ceiling</a></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;">study_hours</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  exam_scores <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">ceiling</a></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;">exam_scores</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></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;">test_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  study_hours exam_scores
1           6          57
2           7          59
3          11          77
4           8          61
5           8          61
6          11          81</code></pre>
</div>
</div>
<p>If we plot this data we can see the relationship between study hours and exam scores:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">exam_plot</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">test_data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <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;">study_hours</span>, y <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;">exam_scores</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    title <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;">"Exam scores vs. study hours"</span>,</span>
<span>    x <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;">"Study hours"</span>,</span>
<span>    y <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;">"Exam scores"</span></span>
<span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">exam_plot</span></span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-exam-scores" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-exam-scores-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<a href="index_files/figure-html/fig-exam-scores-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1" title="Figure&nbsp;1: Scatter plot of exam scores vs.&nbsp;study hours. Each point represents a student’s exam score based on the number of study hours per week."><img src="https://c-monaghan.github.io/posts/2025/03/12/Linear-Regression/index_files/figure-html/fig-exam-scores-1.png" class="img-fluid figure-img" width="1152"></a>
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-exam-scores-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Scatter plot of exam scores vs.&nbsp;study hours. Each point represents a student’s exam score based on the number of study hours per week.
</figcaption></figure>
</div>
</div>
</div>
<p>From the plot, you can see a clear trend: <strong>the more hours a student spends studying, the higher their exam score tends to be</strong>. This is the relationship we want to model using linear regression.</p>
</section><section id="fitting-a-model-in-r" class="level3"><h3 class="anchored" data-anchor-id="fitting-a-model-in-r">Fitting a model in R</h3>
<p>In R, fitting a linear regression model is as simple as using the <code><a href="https://rdrr.io/r/stats/lm.html">lm()</a></code> function. We provide the formula for the model and the dataset, and R does the rest. Let’s fit a linear model to our data:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/lm.html">lm</a></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;">exam_scores</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;">study_hours</span>, data <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;">test_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">broom</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://generics.r-lib.org/reference/tidy.html">tidy</a></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;">fit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 5
  term        estimate std.error statistic    p.value
  &lt;chr&gt;          &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;      &lt;dbl&gt;
1 (Intercept)    21.3      3.93       5.42 0.000627  
2 study_hours     5.23     0.496     10.5  0.00000569</code></pre>
</div>
</div>
<p>The <code><a href="https://rdrr.io/r/stats/lm.html">lm()</a></code> function fits a linear model where <code>exam_scores</code> is the dependent variable <img src="https://latex.codecogs.com/png.latex?(Y)"> and <code>study_hours</code> is the independent variable <img src="https://latex.codecogs.com/png.latex?(X)">. The output gives us the estimated coefficients for the intercept <img src="https://latex.codecogs.com/png.latex?(%5Cbeta_0)"> and slope <img src="https://latex.codecogs.com/png.latex?(%5Cbeta_1)">.</p>
<ul>
<li>The intercept <img src="https://latex.codecogs.com/png.latex?(%5Cbeta_0)"> is <strong>21.33</strong>, meaning a student who studies 0 hours is predicted to score approximately 21% on the exam.</li>
<li>The slope <img src="https://latex.codecogs.com/png.latex?(%5Cbeta_1)"> is <strong>5.23</strong>, indicating that for every additional hour of study, a student’s predicted exam score increases by 5.23%.</li>
</ul>
<p>Visually this looks like</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">exam_plot</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">exam_plot</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    method <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;">"lm"</span>,</span>
<span>    colour <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggokabeito</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://malcolmbarrett.github.io/ggokabeito/reference/palette_okabe_ito.html">palette_okabe_ito</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    se <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adding equation of the line</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/annotate.html">annotate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>,</span>
<span>    label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/coef.html">coef</a></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;">fit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</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;">" + "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/coef.html">coef</a></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;">fit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</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;">"X"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggokabeito</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://malcolmbarrett.github.io/ggokabeito/reference/palette_okabe_ito.html">palette_okabe_ito</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">rel</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, fontface <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span></span>
<span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">exam_plot</span></span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-exam-scores-regression" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-exam-scores-regression-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<a href="index_files/figure-html/fig-exam-scores-regression-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2" title="Figure&nbsp;2: Scatter plot of exam scores vs.&nbsp;study hours with a linear regression line. The equation of the line is displayed on the plot."><img src="https://c-monaghan.github.io/posts/2025/03/12/Linear-Regression/index_files/figure-html/fig-exam-scores-regression-1.png" class="img-fluid figure-img" width="1152"></a>
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-exam-scores-regression-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Scatter plot of exam scores vs.&nbsp;study hours with a linear regression line. The equation of the line is displayed on the plot.
</figcaption></figure>
</div>
</div>
</div>
<p><strong>But how are these values calculated</strong></p>
</section></section><section id="calculating-regression-coefficients-by-hand" class="level2"><h2 class="anchored" data-anchor-id="calculating-regression-coefficients-by-hand">Calculating Regression Coefficients by Hand</h2>
<p>While R’s <code><a href="https://rdrr.io/r/stats/lm.html">lm()</a></code> function makes fitting a linear regression model incredibly easy, it’s helpful to understand how the coefficients <img src="https://latex.codecogs.com/png.latex?(%5Cbeta_0%20%5C:%20%5Ctext%7Band%7D%20%5C:%20%5Cbeta_1)"> are derived.</p>
<p>The goal of linear regression is to find the best-fitting line through the data, which is done by minimizing the sum of squared errors. The sum of squared errors is calculated as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%20SSE%20=%20%5Csum_%7Bi=1%7D%5E%7Bn%7D%20(Y_i%20-%20%5Chat%7BY_i%7D)%5E2%20"></p>
<p>where:</p>
<ul>
<li>
<img src="https://latex.codecogs.com/png.latex?Y_i"> is the actual value of the dependent variable.</li>
<li>
<img src="https://latex.codecogs.com/png.latex?%5Chat%7BY_i%7D"> is the predicted value of the dependent variable.</li>
<li>
<img src="https://latex.codecogs.com/png.latex?n"> is the number of observations.</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add predicted values to the dataset</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">test_data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">test_data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>predicted <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/coef.html">coef</a></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;">fit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/coef.html">coef</a></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;">fit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</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;">study_hours</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Visualizing SSE</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">exam_plot</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    data <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;">test_data</span>,</span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <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;">study_hours</span>, y <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;">predicted</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>,</span>
<span>    colour <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggokabeito</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://malcolmbarrett.github.io/ggokabeito/reference/palette_okabe_ito.html">palette_okabe_ito</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_segment.html">geom_segment</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  data <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;">test_data</span>,</span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <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;">study_hours</span>, xend <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;">study_hours</span>,</span>
<span>      y <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;">exam_scores</span>,  yend <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;">predicted</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  colour <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggokabeito</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://malcolmbarrett.github.io/ggokabeito/reference/palette_okabe_ito.html">palette_okabe_ito</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.25</span>, linetype <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;">"dashed"</span>, alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-SSE" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-SSE-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<a href="index_files/figure-html/fig-SSE-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3" title="Figure&nbsp;3: Visual representation of the sum of squared errors (SSE) in linear regression. The goal is to minimize the vertical distance between the data points and the regression line."><img src="https://c-monaghan.github.io/posts/2025/03/12/Linear-Regression/index_files/figure-html/fig-SSE-1.png" class="img-fluid figure-img" width="1152"></a>
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-SSE-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Visual representation of the sum of squared errors (SSE) in linear regression. The goal is to minimize the vertical distance between the data points and the regression line.
</figcaption></figure>
</div>
</div>
</div>
<p>To minimize the SSE, we need to find the values of <img src="https://latex.codecogs.com/png.latex?%5Cbeta_0"> (intercept) and <img src="https://latex.codecogs.com/png.latex?%5Cbeta_1"> (slope) that make the line fit the data as closely as possible. The formulas for <img src="https://latex.codecogs.com/png.latex?%5Cbeta_0"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta_1"> are:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%5Cbeta_1%20&amp;=%20%5Cfrac%7B%5Csum(X_i%20-%20%5Cbar%7BX%7D)(Y_i%20-%20%5Cbar%7BY%7D)%7D%7B%5Csum(X_i%20-%20X)%5E2%7D%20%5C%5C%5B18pt%5D%0A%0A%5Cbeta_0%20&amp;=%20%5Cbar%7BY%7D%20-%20%5Cbeta_1%5Cbar%7BX%7D%0A%5Cend%7Balign*%7D"></p>
<p>where:</p>
<ul>
<li>
<img src="https://latex.codecogs.com/png.latex?X_i"> and <img src="https://latex.codecogs.com/png.latex?Y_i"> are the individual data points.</li>
<li>
<img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BY%7D"> are the means of the independent and dependent variables, respectively.</li>
</ul>
<section id="step-1-calculate-the-means" class="level3"><h3 class="anchored" data-anchor-id="step-1-calculate-the-means">Step 1: Calculate the means</h3>
<p>The first step is to calculate the means of <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y">. These are the averages of the independent and dependent variables, respectively.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%5Cbar%7BX%7D%20=%20%5Cfrac%7B%5Csum%20X_i%7D%7Bn%7D%20=%20%5Cfrac%7B77%7D%7B10%7D%20=%207.7%20%5C%5C%5B12pt%5D%0A%0A%5Cbar%7BY%7D%20=%20%5Cfrac%7B%5Csum%20Y_i%7D%7Bn%7D%20=%20%5Cfrac%7B616%7D%7B10%7D%20=%2061.6%0A%5Cend%7Balign*%7D"></p>
</section><section id="step-2-calculate-the-deviations-from-the-mean" class="level3"><h3 class="anchored" data-anchor-id="step-2-calculate-the-deviations-from-the-mean">Step 2: Calculate the deviations from the mean</h3>
<p>Next, we calculate the deviations from the mean for each data point and their products. These are the building blocks for the slope (<img src="https://latex.codecogs.com/png.latex?%5Cbeta_1)"></p>
<table class="caption-top table">
<colgroup>
<col style="width: 12%">
<col style="width: 6%">
<col style="width: 8%">
<col style="width: 45%">
<col style="width: 28%">
</colgroup>
<thead><tr class="header">
<th style="text-align: center;">Student</th>
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?X"></th>
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?Y"></th>
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?(x_i%20-%20%5Cbar%7Bx%7D)(y_i%20-%20%5Cbar%7By%7D)"></th>
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?(x_i%20-%20%5Cbar%7Bx%7D)%5E2"></th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">1</td>
<td style="text-align: center;">6</td>
<td style="text-align: center;">57</td>
<td style="text-align: center;">7.82</td>
<td style="text-align: center;">2.89</td>
</tr>
<tr class="even">
<td style="text-align: center;">2</td>
<td style="text-align: center;">7</td>
<td style="text-align: center;">59</td>
<td style="text-align: center;">1.82</td>
<td style="text-align: center;">0.49</td>
</tr>
<tr class="odd">
<td style="text-align: center;">3</td>
<td style="text-align: center;">11</td>
<td style="text-align: center;">77</td>
<td style="text-align: center;">50.82</td>
<td style="text-align: center;">10.89</td>
</tr>
<tr class="even">
<td style="text-align: center;">4</td>
<td style="text-align: center;">8</td>
<td style="text-align: center;">61</td>
<td style="text-align: center;">-0.18</td>
<td style="text-align: center;">0.09</td>
</tr>
<tr class="odd">
<td style="text-align: center;">5</td>
<td style="text-align: center;">8</td>
<td style="text-align: center;">61</td>
<td style="text-align: center;">-0.18</td>
<td style="text-align: center;">0.09</td>
</tr>
<tr class="even">
<td style="text-align: center;">6</td>
<td style="text-align: center;">11</td>
<td style="text-align: center;">81</td>
<td style="text-align: center;">64.02</td>
<td style="text-align: center;">10.89</td>
</tr>
<tr class="odd">
<td style="text-align: center;">7</td>
<td style="text-align: center;">8</td>
<td style="text-align: center;">66</td>
<td style="text-align: center;">1.32</td>
<td style="text-align: center;">0.09</td>
</tr>
<tr class="even">
<td style="text-align: center;">8</td>
<td style="text-align: center;">5</td>
<td style="text-align: center;">44</td>
<td style="text-align: center;">47.52</td>
<td style="text-align: center;">7.29</td>
</tr>
<tr class="odd">
<td style="text-align: center;">9</td>
<td style="text-align: center;">6</td>
<td style="text-align: center;">55</td>
<td style="text-align: center;">11.22</td>
<td style="text-align: center;">2.89</td>
</tr>
<tr class="even">
<td style="text-align: center;">10</td>
<td style="text-align: center;">7</td>
<td style="text-align: center;">55</td>
<td style="text-align: center;">4.62</td>
<td style="text-align: center;">0.49</td>
</tr>
<tr class="odd">
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Csum"></td>
<td style="text-align: center;">77</td>
<td style="text-align: center;">616</td>
<td style="text-align: center;">188.8</td>
<td style="text-align: center;">36.1</td>
</tr>
</tbody>
</table></section><section id="step-3-calculate-the-slope-beta_1" class="level3"><h3 class="anchored" data-anchor-id="step-3-calculate-the-slope-beta_1">Step 3: Calculate the slope <img src="https://latex.codecogs.com/png.latex?(%5Cbeta_1)">
</h3>
<p>Using the formula for <img src="https://latex.codecogs.com/png.latex?%5Cbeta_1"> we now get:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%5Cbeta_1%20&amp;=%20%5Cfrac%7B%5Csum(X_i%20-%20%5Cbar%7BX%7D)(Y_i%20-%20%5Cbar%7BY%7D)%7D%7B%5Csum(X_i%20-%20X)%5E2%7D%20%5C%5C%5B12pt%5D%0A%0A&amp;=%20%5Cfrac%7B188.8%7D%7B36.1%7D%20%5C%5C%5B12pt%5D%0A%0A&amp;%5Capprox%205.23%0A%5Cend%7Balign*%7D"></p>
</section><section id="step-4-calculating-the-intercept-beta_0" class="level3"><h3 class="anchored" data-anchor-id="step-4-calculating-the-intercept-beta_0">Step 4: Calculating the intercept <img src="https://latex.codecogs.com/png.latex?(%5Cbeta_0)">
</h3>
<p>Using the formula for <img src="https://latex.codecogs.com/png.latex?%5Cbeta_0"> we now get:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign*%7D%0A%5Cbeta_0%20&amp;=%20%5Cbar%7BY%7D%20-%20%5Cbeta_1%5Cbar%7BX%7D%20%5C%5C%5B12pt%5D%0A%0A&amp;=%2061.6%20-%205.23(7.7)%20%5C%5C%5B12pt%5D%0A%0A&amp;%5Capprox%2021.32%0A%5Cend%7Balign*%7D"></p>
</section><section id="step-5-writing-our-regression-equation" class="level3"><h3 class="anchored" data-anchor-id="step-5-writing-our-regression-equation">Step 5: Writing our regression equation</h3>
<p>Now that we have values for <img src="https://latex.codecogs.com/png.latex?%5Cbeta_0%20%5C:%20%5Ctext%7Band%7D%20%5C:%20%5Cbeta_1"> we can write the regression equation:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Chat%7BY%7D%20=%2021.3%20+%205.23X"></p>
<p>which if you remember from before is the same as our <code><a href="https://rdrr.io/r/stats/lm.html">lm()</a></code> model in R.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">broom</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://generics.r-lib.org/reference/tidy.html">tidy</a></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;">fit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 5
  term        estimate std.error statistic    p.value
  &lt;chr&gt;          &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;      &lt;dbl&gt;
1 (Intercept)    21.3      3.93       5.42 0.000627  
2 study_hours     5.23     0.496     10.5  0.00000569</code></pre>
</div>
</div>


<!-- -->

</section></section><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{monaghan2025,
  author = {Monaghan, Cormac},
  title = {Doing Linear Regression by Hand},
  date = {2025-03-12},
  url = {https://c-monaghan.github.io/posts/2025/03/12/Linear-Regression/},
  doi = {10.59350/nj895-ta783},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-monaghan2025" class="csl-entry quarto-appendix-citeas">
Monaghan, Cormac. 2025. <span>“Doing Linear Regression by Hand.”</span>
March 12, 2025. <a href="https://doi.org/10.59350/nj895-ta783">https://doi.org/10.59350/nj895-ta783</a>.
</div></div></section></div> ]]></description>
  <category>statistics</category>
  <category>regression</category>
  <category>r</category>
  <guid>https://c-monaghan.github.io/posts/2025/03/12/Linear-Regression/</guid>
  <pubDate>Wed, 12 Mar 2025 00:00:00 GMT</pubDate>
  <media:content url="https://c-monaghan.github.io/posts/2025/thumbnails/regression_model_thumb.png" medium="image" type="image/png" height="96" width="144"/>
</item>
<item>
  <title>Bird Abundance</title>
  <dc:creator>Cormac Monaghan</dc:creator>
  <link>https://c-monaghan.github.io/posts/2024/10/07/TT-W41/</link>
  <description><![CDATA[ <div id="fig-bird" class="lightbox quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-bird-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<a href="tt_2024_w41.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1" title="Figure&nbsp;1: The image displays the abundance of birds for the 15 most visited National Parks in the USA"><img src="https://c-monaghan.github.io/posts/2024/10/07/TT-W41/tt_2024_w41.png" class="img-fluid figure-img"></a>
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-bird-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: The image displays the abundance of birds for the 15 most visited National Parks in the USA
</figcaption></figure>
</div>
<p>This document analyzes a dataset of species from the 15 most visited <a href="https://irma.nps.gov/NPSpecies/Search/SpeciesList">National Parks</a> in the USA provided by <a href="https://github.com/rfordatascience/tidytuesday">#TidyTuesday</a>. The main focus of this analysis will be on <strong>birds</strong>.</p>
<section id="setting-up" class="level1"><h1>Setting up</h1>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Loading packages -------------------------------------------------------------</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacman</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/pacman/man/p_load.html">p_load</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tidyverse</span>,        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Easily Install and Load the 'Tidyverse'</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggtext</span>,           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Improved Text Rendering Support for 'ggplot2'</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">showtext</span>,         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Using Fonts More Easily in R Graphs</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggeasy</span>,           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Makes theming plots easier</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glue</span>,             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Interpreted String Literals</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggfx</span>              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pixel Filters for "ggplot2" and "grid"</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Visualization Parameters -----------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot aesthetics</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">title_col</span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray20"</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">subtitle_col</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray20"</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">caption_col</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray30"</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">text_col</span>     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray20"</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Icons</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tt</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_glue</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;">"#TidyTuesday: { 2024 } Week { 41 } &amp;bull; Source: National Park Species&lt;br&gt;"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">li</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_glue</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;">"&lt;span style='font-family:fa6-brands'&gt;&amp;#xf08c;&lt;/span&gt;"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">gh</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_glue</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;">"&lt;span style='font-family:fa6-brands'&gt;&amp;#xf09b;&lt;/span&gt;"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Text</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">title_text</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_glue</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;">"Abundance of birds per national park"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">caption_text</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_glue</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;">"{tt} {li} c-monaghan &amp;bull; {gh} c-monaghan &amp;bull; #rstats #ggplot2"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fonts</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">font_add</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;">"fa6-brands"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://here.r-lib.org/reference/here.html">here</a></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;">"fonts/6.4.2/Font Awesome 6 Brands-Regular-400.otf"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">font_add_google</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;">"Oswald"</span>, regular.wt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>, family <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;">"title"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">font_add_google</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;">"Noto Sans"</span>, regular.wt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>, family <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;">"caption"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">font_add_google</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;">"Merriweather Sans"</span>, regular.wt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>, family <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;">"text"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">showtext_auto</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>enable <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Theme</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span><span class="op" 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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>base_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_update</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  plot.title.position   <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;">"plot"</span>,</span>
<span>  plot.caption.position <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;">"plot"</span>,</span>
<span>  panel.background      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  panel.grid            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  panel.grid.major.x    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  axis.text.x           <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, family <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;">"text"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  axis.text.y           <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  legend.position       <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;">"bottom"</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variables --------------------------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paths</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">path</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"posts/2024/10/07"</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">folder</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TT-W41/"</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reading in data --------------------------------------------------------------</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidytuesdayR</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dslc-io.github.io/tidytuesdayR/reference/tt_load.html">tt_load</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2024</span>, week <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">41</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">species</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</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;">most_visited_nps_species_data</span></span></code></pre></div></div>
</div>
</section><section id="species-data" class="level1"><h1>Species data</h1>
<p>The dataset includes various information on animal and plant species from 15 of the most visited parks in the USA. In particular, we are interested in the abundance of birds in each park.</p>
<section id="calculating-abundance" class="level2"><h2 class="anchored" data-anchor-id="calculating-abundance">Calculating abundance</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bird_abundance</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">species</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</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;">ParkName</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">CategoryName</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</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;">ParkName</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">CategoryName</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>Abundance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>Park_name <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;">ParkName</span>, Species <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;">CategoryName</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>Park_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/factor.html">as.factor</a></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;">Park_name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/filter.html">filter</a></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;">Species</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;">"Bird"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bird_abundance</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 3
# Groups:   Park_name [6]
  Park_name                     Species Abundance
  &lt;fct&gt;                         &lt;chr&gt;       &lt;int&gt;
1 Acadia National Park          Bird          364
2 Bryce Canyon National Park    Bird          218
3 Cuyahoga Valley National Park Bird          246
4 Glacier National Park         Bird          277
5 Grand Canyon National Park    Bird          456
6 Grand Teton National Park     Bird          266</code></pre>
</div>
</div>
</section></section><section id="visualising-bird-abundance" class="level1"><h1>Visualising bird abundance</h1>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bird_abundance_plot</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bird_abundance</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add horizontal reference lines at intervals of 125</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">125</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>yintercept <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;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a bar plot in polar coordinates</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/reorder.factor.html">reorder</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_wrap</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;">Park_name</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</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;">Abundance</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        y <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;">Abundance</span>,</span>
<span>        fill <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;">Abundance</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    position <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;">"dodge2"</span>, show.legend <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use polar coordinates</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_polar</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set y-axis limits, expand to control padding, and custom breaks for reference lines</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    limits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    expand <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    breaks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Apply a color gradient for the bars based on abundance levels</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradientn</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Abundance"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Title</span></span>
<span>    colours <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></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;">"#e9b91c"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#db9a17"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ce7b12"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#be471b"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ae1324"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Custom colours</span></span>
<span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Customize the legend</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guide_colorsteps</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>      barwidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>,</span>
<span>      barheight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span>      title.position <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;">"top"</span>,</span>
<span>      title.hjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add titles and labels</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    title <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;">title_text</span>,</span>
<span>    caption <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;">caption_text</span>,</span>
<span>    x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span>    y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Apply theme customization</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Title</span></span>
<span>    plot.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>      size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rel</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>      family <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;">"title"</span>,</span>
<span>      face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>,</span>
<span>      colour <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;">title_col</span>,</span>
<span>      lineheight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>,</span>
<span>      hjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span>      margin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Caption</span></span>
<span>    plot.caption <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_markdown</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>      size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rel</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.25</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>      family <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;">"caption"</span>,</span>
<span>      colour <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;">caption_col</span>,</span>
<span>      lineheight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>,</span>
<span>      hjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span>      margin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    legend.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    legend.text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bird_abundance_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://c-monaghan.github.io/posts/2024/10/07/TT-W41/index_files/figure-html/plotting-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="864"></p>
</figure>
</div>
</div>
</div>
</section><section id="saving" class="level1"><h1>Saving</h1>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Saving plot</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  filename <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://here.r-lib.org/reference/here.html">here</a></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;">path</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">folder</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tt_2024_w41.png"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  plot <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;">bird_abundance_plot</span>,</span>
<span>  width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>,</span>
<span>  height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,</span>
<span>  units <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;">"in"</span>,</span>
<span>  dpi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">320</span></span>
<span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Thumbnail</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">magick</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/magick/reference/editing.html">image_read</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://here.r-lib.org/reference/here.html">here</a></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;">path</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">folder</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tt_2024_w41.png"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">magick</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/magick/reference/transform.html">image_resize</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>geometry <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;">"800"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">magick</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/magick/reference/editing.html">image_write</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://here.r-lib.org/reference/here.html">here</a></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;">"posts/2024/thumbnails/tt_2024_w41_thumb.png"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>


<!-- -->

</section><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{monaghan2024,
  author = {Monaghan, Cormac},
  title = {Bird {Abundance}},
  date = {2024-10-08},
  url = {https://c-monaghan.github.io/posts/2024/10/07/TT-W41/},
  doi = {10.59350/j4p1m-m9296},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-monaghan2024" class="csl-entry quarto-appendix-citeas">
Monaghan, Cormac. 2024. <span>“Bird Abundance .”</span> October 8, 2024.
<a href="https://doi.org/10.59350/j4p1m-m9296">https://doi.org/10.59350/j4p1m-m9296</a>.
</div></div></section></div> ]]></description>
  <category>#TidyTuesday</category>
  <category>data visualisation</category>
  <guid>https://c-monaghan.github.io/posts/2024/10/07/TT-W41/</guid>
  <pubDate>Mon, 07 Oct 2024 23:00:00 GMT</pubDate>
  <media:content url="https://c-monaghan.github.io/posts/2024/thumbnails/tt_2024_w41_thumb.png" medium="image" type="image/png" height="96" width="144"/>
</item>
<item>
  <title>Visualising positional moves in chess</title>
  <dc:creator>Cormac Monaghan</dc:creator>
  <link>https://c-monaghan.github.io/posts/2024/09/28/TT-W40/</link>
  <description><![CDATA[ <p><a href="tt_2024_w40.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1" title="The image displays the positional movements of major white chess pieces. Denser curves indicate that pieces are move often moved in that direction."><img src="https://c-monaghan.github.io/posts/2024/09/28/TT-W40/tt_2024_w40.png" id="fig-position" class="img-fluid" alt="The image displays the positional movements of major white chess pieces. Denser curves indicate that pieces are move often moved in that direction."></a> This document analyzes a dataset of chess games from <a href="https://lichess.org/">Lichess</a> provided by <a href="https://github.com/rfordatascience/tidytuesday">#TidyTuesday</a>. It contains over 20,000 games, including information such as player ratings, move sequences, and other metrics. The main focus of this analysis will be on the <strong>specific moves played during each game</strong>.</p>
<section id="setting-up" class="level1"><h1>Setting up</h1>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">321</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For reproducibility</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Loading packages -------------------------------------------------------------</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacman</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/pacman/man/p_load.html">p_load</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tidyverse</span>,        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Easily Install and Load the 'Tidyverse'</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rchess</span>,           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Working with chess PGN notation</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggtext</span>,           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Improved Text Rendering Support for 'ggplot2'</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">showtext</span>,         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Using Fonts More Easily in R Graphs</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggeasy</span>,           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Makes theming plots easier</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">furrr</span>,            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Parallel processing</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glue</span>,             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Interpreted String Literals</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggfx</span>             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pixel Filters for "ggplot2" and "grid"</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Visualization Parameters -----------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot aesthetics</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">title_col</span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray20"</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">subtitle_col</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray20"</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">caption_col</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray30"</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">text_col</span>     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray20"</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col_palette</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paletteer</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/paletteer/man/paletteer_d.html">paletteer_d</a></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;">"peRReo::don"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chess_tiles</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></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;">"burlywood3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"burlywood4"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Traditional board colours</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Icons</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tt</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_glue</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;">"#TidyTuesday: { 2024 } Week { 40 } &amp;bull; Source: Chess Game Dataset (Lichess)&lt;br&gt;"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">li</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_glue</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;">"&lt;span style='font-family:fa6-brands'&gt;&amp;#xf08c;&lt;/span&gt;"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">gh</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_glue</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;">"&lt;span style='font-family:fa6-brands'&gt;&amp;#xf09b;&lt;/span&gt;"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Text</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">title_text</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_glue</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;">"Positional movements of major chess pieces"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">caption_text</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_glue</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;">"{tt} {li} c-monaghan &amp;bull; {gh} c-monaghan &amp;bull; #rstats #ggplot2"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fonts</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">font_add</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;">"fa6-brands"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://here.r-lib.org/reference/here.html">here</a></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;">"fonts/6.4.2/Font Awesome 6 Brands-Regular-400.otf"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">font_add_google</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;">"Oswald"</span>, regular.wt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>, family <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;">"title"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">font_add_google</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;">"Noto Sans"</span>, regular.wt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>, family <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;">"caption"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">showtext_auto</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>enable <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot theme</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>base_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>, base_family <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;">"text"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_update</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    plot.title.position   <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;">"plot"</span>,</span>
<span>    plot.caption.position <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;">"plot"</span>,</span>
<span>    legend.position       <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;">'plot'</span>,</span>
<span>    panel.grid            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    strip.text            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>,</span>
<span>                                         margin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    axis.text             <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    axis.ticks            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variables --------------------------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Paths</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">path</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"posts/2024/09/28/"</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">folder</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TT-W40/"</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># White Pieces</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">white_pieces</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></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;">"a1 Rook"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b1 Knight"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c1 Bishop"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"White Queen"</span>,</span>
<span>                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"White King"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"f1 Bishop"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g1 Knight"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"h1 Rook"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reading in data --------------------------------------------------------------</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidytuesdayR</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dslc-io.github.io/tidytuesdayR/reference/tt_load.html">tt_load</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2024</span>, week <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</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;">chess</span></span></code></pre></div></div>
</div>
</section><section id="chess-data" class="level1"><h1>Chess Data</h1>
<p>The dataset includes the move sequences made by each player, represented as a string of moves. Below is a preview of the first two rows of the dataset.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#|</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</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;">game_id</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">moves</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>game_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/nrow.html">nrow</a></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;">data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ID <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;">game_id</span>, Moves <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;">moves</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## # A tibble: 2 × 2</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">##      ID Moves                                                           </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">##   &lt;int&gt; &lt;chr&gt;                                                           </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 1     1 d4 d5 c4 c6 cxd5 e6 dxe6 fxe6 Nf3 Bb4+ Nc3 Ba5 Bf4              </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 2     2 d4 Nc6 e4 e5 f4 f6 dxe5 fxe5 fxe5 Nxe5 Qd4 Nc6 Qe5+ Nxe5 c4 Bb4+</span></span></code></pre></div></div>
</div>
<p>Each row displays the move sequence for a game, starting with White’s move. For example, the <strong>Slav Defense</strong> (1. d4 d5 2. c4 c6) or the <strong>Nimzowitsch Defense</strong> (1. d4 Nc6 2. e4 e5). Chess moves are typically recorded using <a href="https://www.chess.com/terms/chess-pgn">Portable Game Notation (PGN)</a>, which makes it easy to replicate games.</p>
<section id="pgn-conversion" class="level2"><h2 class="anchored" data-anchor-id="pgn-conversion">PGN Conversion</h2>
<p>To facilitate further analysis, we will convert the raw move strings into the PGN format, using a custom <code>convert_to_pgn()</code> function that I wrote.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#|</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert moves into PGN format ------------------------------------------------</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">convert_to_pgn</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</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;">moves</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">game_id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Split the moves string into a list of individual moves</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">move_list</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/strsplit.html">strsplit</a></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;">moves</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialize an empty string for the PGN format</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pgn</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Loop through the moves two at a time (each move is a pair: white and black)</span></span>
<span>  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</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;">i</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></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;">move_list</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, by <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">move_number</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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;">i</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Move number calculation</span></span>
<span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add both white's and black's moves, if available</span></span>
<span>    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;">i</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></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;">move_list</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>      <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pgn</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/paste.html">paste0</a></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;">pgn</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">move_number</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">". "</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">move_list</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;">i</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;">" "</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">move_list</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;">i</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</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;">" "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># In case the game ends on white's move (no black move)</span></span>
<span>      <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pgn</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/paste.html">paste0</a></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;">pgn</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">move_number</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">". "</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">move_list</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;">i</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Returning pgn string</span></span>
<span>  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/function.html">return</a></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;">pgn</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Converting moves into pgn format</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chess_games</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</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;">game_id</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">moves</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    game_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/nrow.html">nrow</a></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;">data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    moves <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mapply.html">mapply</a></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;">convert_to_pgn</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">moves</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">game_id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Displaying converted dataset</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chess_games</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ID <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;">game_id</span>, Moves <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;">moves</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## # A tibble: 2 × 2</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">##      ID Moves                                                                   </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">##   &lt;int&gt; &lt;chr&gt;                                                                   </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 1     1 "1. d4 d5 2. c4 c6 3. cxd5 e6 4. dxe6 fxe6 5. Nf3 Bb4+ 6. Nc3 Ba5 7. Bf…</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 2     2 "1. d4 Nc6 2. e4 e5 3. f4 f6 4. dxe5 fxe5 5. fxe5 Nxe5 6. Qd4 Nc6 7. Qe…</span></span></code></pre></div></div>
</div>
</section><section id="extracting-game-history" class="level2"><h2 class="anchored" data-anchor-id="extracting-game-history">Extracting game history</h2>
<p>Even with parallel processing the below code takes extremely long to run when rendering in quarto (4+ hrs). In a normal R session it takes about 20 minutes. I am not quite sure as to why the time difference is so extreme. However, to make my life a little easier and so I’m not waiting for hours when I need to re-render this document I will be reading in the data that this code outputs from an excel file.</p>
<p>However, for those interested I have left the parallel processing code below</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="true" aria-label="Toggle callout">
<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>Game history code
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Now that the data is in PGN format, we can use the <code>history_detail()</code> function from the <code>rhcess</code> package to extract the game history. However, because the dataset contains over 20,000 rows, processing every game sequentially would be time-consuming. To speed up this process, we will implement parallel processing using the <code>furrr</code> package.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#|</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Function to extract game history</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">process_moves</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</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;">p</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chss</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Chess</span><span class="op" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialize a new chess object</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chss</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load_pgn</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;">p</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the PGN notation into the chess object</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chss</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">history_detail</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract detailed history of the game</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plan</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;">multisession</span>, workers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parallel</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/parallel/detectCores.html">detectCores</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Parallel processing: Convert PGN format to game history</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chess_games</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chess_games</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">future_map</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;">moves</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">process_moves</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</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;">moves</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>cols <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></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;">data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>The game history data contains 1,393,586 observations on both white and black’s moves. For the purpose of analysis we are only interested in plotting white’s moves. Additionally, we will sample 25,000 moves from white so as not to create an over-populated graph.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#|</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># White pieces</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">white_pieces</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a1 Rook"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b1 Knight"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c1 Bishop"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"White Queen"</span>,</span>
<span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"White King"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"f1 Bishop"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g1 Knight"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"h1 Rook"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chess_games</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chess_games</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/filter.html">filter</a></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;">piece</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/match.html">%in%</a></span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">white_pieces</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_n</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#|</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chess_games</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readxl</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://readxl.tidyverse.org/reference/read_excel.html">read_xlsx</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://here.r-lib.org/reference/here.html">here</a></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;">"data/chess_history.xlsx"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chess_games</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</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;">game_id</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">piece</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">from</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">to</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## # A tibble: 4 × 4</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">##   game_id piece       from  to   </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">##     &lt;dbl&gt; &lt;chr&gt;       &lt;chr&gt; &lt;chr&gt;</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 1    7298 b1 Knight   b1    d2   </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 2   16254 g1 Knight   e5    c4   </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 3   19492 h1 Rook     h1    e1   </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## 4   10827 White Queen b6    e3</span></span></code></pre></div></div>
</div>
</section></section><section id="positional-movements" class="level1"><h1>Positional Movements</h1>
<section id="creating-a-chess-board" class="level2"><h2 class="anchored" data-anchor-id="creating-a-chess-board">Creating a chess board</h2>
<p>First, we will create a chessboard using <code>ggplot</code>. We can do this using the <code>.chessboard()</code> function, again from the <code>rchess</code> package. This function generates the grid and coordinates for each square which can piped into ggplot.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#|</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Creating a chess board -------------------------------------------------------</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">board</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rchess</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.chessboarddata</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</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;">cell</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cc</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">board</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 6
  cell  col     row     x     y cc   
  &lt;chr&gt; &lt;chr&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;chr&gt;
1 a1    a         1     1     1 b    
2 b1    b         1     2     1 w    
3 c1    c         1     3     1 b    
4 d1    d         1     4     1 w    </code></pre>
</div>
</div>
</section><section id="adding-board-movements" class="level2"><h2 class="anchored" data-anchor-id="adding-board-movements">Adding board movements</h2>
<p>Next, we will join the chessboard data with the game data to calculate the origin and destination of each piece movement. This allows us to visualize the trajectories of pieces as they move across the board.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Join board data with game data to get move origin and destination</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chess_games</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chess_games</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</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;">board</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>from <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;">cell</span>, x.from <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;">x</span>, y.from <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;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, by <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;">"from"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</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;">board</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>to <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;">cell</span>, x.to <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;">x</span>, y.to <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;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</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;">cc</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;">col</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;">row</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Exclude unnecessary columns</span></span>
<span>            by <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;">"to"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x_gt_y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/MathFun.html">abs</a></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;">x.to</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;">x.from</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/MathFun.html">abs</a></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;">y.to</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;">y.from</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check if x movement is greater than y</span></span>
<span>         xy_sign <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/sign.html">sign</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</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;">x.to</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;">x.from</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</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;">y.to</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;">y.from</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check if X and Y move in the same direction</span></span>
<span>         x_gt_y_equal_xy_sign <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;">x_gt_y</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;">xy_sign</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check if both conditions hold</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Previewing</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chess_games</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</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;">piece</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">from</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">to</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x.from</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y.from</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x.to</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y.to</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 7
  piece       from  to    x.from y.from  x.to  y.to
  &lt;chr&gt;       &lt;chr&gt; &lt;chr&gt;  &lt;int&gt;  &lt;int&gt; &lt;int&gt; &lt;int&gt;
1 b1 Knight   b1    d2         2      1     4     2
2 g1 Knight   e5    c4         5      5     3     4
3 h1 Rook     h1    e1         8      1     5     1
4 White Queen b6    e3         2      6     5     3</code></pre>
</div>
</div>
</section><section id="plotting-piece-movements" class="level2"><h2 class="anchored" data-anchor-id="plotting-piece-movements">Plotting piece movements</h2>
<p>Now we can visualize their paths on a chessboard.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">chess_games</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adding board data</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <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;">board</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</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;">x</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>, fill <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;">cc</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adding piece movement</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_curve</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    data <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;">chess_games</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/filter.html">filter</a></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;">x_gt_y_equal_xy_sign</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <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;">x.from</span>, y <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;">y.from</span>, xend <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;">x.to</span>, yend <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;">y.to</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    position <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_jitter</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    curvature <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.50</span>, angle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>, alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span>    linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.02</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_curve</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    data <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;">chess_games</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/filter.html">filter</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</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;">x_gt_y_equal_xy_sign</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <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;">x.from</span>, y <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;">y.from</span>, xend <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;">x.to</span>, yend <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;">y.to</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    position <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_jitter</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    curvature <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.50</span>, angle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>, alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span>    linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.02</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Labs</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    title <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;">title_text</span>,</span>
<span>    caption <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;">caption_text</span>,</span>
<span>    x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span>    y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scales</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>values <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;">chess_tiles</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Facet</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/factor.html">factor</a></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;">piece</span>, levels <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;">white_pieces</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, ncol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Theme</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    plot.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>      size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,</span>
<span>      family <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;">"title"</span>,</span>
<span>      face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>,</span>
<span>      colour <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;">title_col</span>,</span>
<span>      lineheight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>,</span>
<span>      hjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span>      margin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    plot.caption <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_markdown</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>      size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rel</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.25</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>      family <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;">"caption"</span>,</span>
<span>      colour <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;">caption_col</span>,</span>
<span>      lineheight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>,</span>
<span>      hjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span>      margin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</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://c-monaghan.github.io/posts/2024/09/28/TT-W40/index_files/figure-html/piece-movement-1.png" class="img-fluid figure-img" width="720"></p>
<figcaption>Positional movements of major white chess pieces. Denser curves indicate that pieces are move often moved in that direction.</figcaption></figure>
</div>
</div>
</div>
</section></section><section id="saving" class="level1"><h1>Saving</h1>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Saving plot</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  filename <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://here.r-lib.org/reference/here.html">here</a></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;">path</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">folder</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tt_2024_w40.png"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  plot <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;">p</span>,</span>
<span>  width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.5</span>,</span>
<span>  height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span>  units <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;">"in"</span>,</span>
<span>  dpi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">320</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Thumbnail</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">magick</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/magick/reference/editing.html">image_read</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://here.r-lib.org/reference/here.html">here</a></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;">path</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">folder</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tt_2024_w40.png"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">magick</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/magick/reference/transform.html">image_resize</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>geometry <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;">"800"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">magick</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/magick/reference/editing.html">image_write</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://here.r-lib.org/reference/here.html">here</a></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;">"posts/2024/thumbnails/tt_2024_w40_thumb.png"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>


<!-- -->

</section><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{monaghan2024,
  author = {Monaghan, Cormac},
  title = {Visualising Positional Moves in Chess},
  date = {2024-09-28},
  url = {https://c-monaghan.github.io/posts/2024/09/28/TT-W40/},
  doi = {10.59350/0qppj-j7r10},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-monaghan2024" class="csl-entry quarto-appendix-citeas">
Monaghan, Cormac. 2024. <span>“Visualising Positional Moves in
Chess.”</span> September 28, 2024. <a href="https://doi.org/10.59350/0qppj-j7r10">https://doi.org/10.59350/0qppj-j7r10</a>.
</div></div></section></div> ]]></description>
  <category>#TidyTuesday</category>
  <category>data visualisation</category>
  <guid>https://c-monaghan.github.io/posts/2024/09/28/TT-W40/</guid>
  <pubDate>Fri, 27 Sep 2024 23:00:00 GMT</pubDate>
  <media:content url="https://c-monaghan.github.io/posts/2024/thumbnails/tt_2024_w40_thumb.png" medium="image" type="image/png" height="96" width="144"/>
</item>
<item>
  <title>Agent Based Models</title>
  <dc:creator>Cormac Monaghan</dc:creator>
  <link>https://c-monaghan.github.io/posts/2024/09/27/ABM/</link>
  <description><![CDATA[ 





<p>Recently, I attended the “Introduction to Agent-Based Modelling for Public Health Research” workshop, organised by the <a href="https://www.ncrm.ac.uk/">National Centre for Research Methods</a> and held at the University of Glasgow. The workshop covered the principles, concepts, and steps involved in building and analysing agent-based models using the software <a href="https://github.com/NetLogo/NetLogo">NetLogo</a>. This post provides an overview of what agent-based modelling is.Recently, I attended the “Introduction to Agent-Based Modelling for Public Health Research” workshop, organised by the <a href="https://www.ncrm.ac.uk/">National Centre for Research Methods</a> and held at the University of Glasgow. The workshop covered the principles, concepts, and steps involved in building and analysing agent-based models using the software <a href="https://github.com/NetLogo/NetLogo">NetLogo</a>. This post provides an overview of what agent-based modelling is.</p>
<section id="agent-based-models" class="level2">
<h2 class="anchored" data-anchor-id="agent-based-models">Agent Based Models</h2>
<section id="overview" class="level3">
<h3 class="anchored" data-anchor-id="overview">Overview</h3>
<p>Agent-based models are computer simulations used to study the interactions between individuals, entities, environments, and time. These stochastic models are built from the bottom up, meaning individual agents (oftentimes representing people) are assigned specific attributes. The agents are programmed to behave and interact with other agents and their environment in predetermined ways, producing emergent effects that differ from the effects observed at the individual level.</p>
<p><a href="./ABMs.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://c-monaghan.github.io/posts/2024/09/27/ABM/ABMs.png" class="img-fluid"></a></p>
<p>Agent-based models differ from the traditional regression-based methods in several key ways. For starters, regression-based methods focus on dependent and independent variables. Researchers often ask themselves <em>What variables do I need to consider in my model?</em>. In contrast, agent-based models shift the focus to <strong>dynamic processes</strong>, prompting the question <em>What processes do I need to consider?</em>.</p>
</section>
<section id="regression-based-methods" class="level3">
<h3 class="anchored" data-anchor-id="regression-based-methods">Regression-Based Methods</h3>
<p>In regression-based methods, the primary goal is to identify and quantify the relationships between variables. For example, a researcher might want to understand the relationship between smoking and lung cancer. Using regression analysis, the researcher would collect data on smoking (independent variable) and lung cancer incidence (dependent variable), then apply statistical techniques to determine the strength and significance of the association.</p>
<ul>
<li><strong>Research Question</strong>: How does smoking affect the risk of lung cancer?</li>
<li><strong>Independent Variable</strong>: Number of cigarettes smoked per day.</li>
<li><strong>Dependent Variable</strong>: Incidence of lung cancer.</li>
<li><strong>Outcome</strong>: A statistical measure (e.g., odds ratio) that quantifies the increased risk of lung cancer associated with smoking.</li>
</ul>
</section>
<section id="agent-based-methods" class="level3">
<h3 class="anchored" data-anchor-id="agent-based-methods">Agent Based Methods</h3>
<p>Agent based models, on the other hand, shift the focus to <strong>dynamic processes</strong>. These models focus on simulating the interactions between individual agents and their environment. Instead of looking solely at variable relationships, agent based models explore how processes and behaviors lead to emergent phenomena at the system level. This approach is particularly useful for studying complex systems where individual actions and interactions create outcomes that are not easily predictable from the behavior of individual agents alone.</p>
<ul>
<li><strong>Research Question</strong>: How does an infectious disease spread through a population?</li>
<li><strong>Processes to Consider</strong>: Individual movement patterns, infection transmission rates, social interactions, and public health interventions.</li>
<li><strong>Simulation</strong>: Each agent represents a person who can move, interact, become infected, and recover or succumb to the disease. The model simulates how these processes unfold over time, providing insights into potential outbreak scenarios and intervention impacts.</li>
</ul>
<p>By focusing on dynamic processes, ABMs allow researchers to capture the complexity of real-world systems. They provide a way to explore “what-if” scenarios and understand the potential effects of various interventions.</p>
</section>
</section>
<section id="netlogo" class="level2">
<h2 class="anchored" data-anchor-id="netlogo">NetLogo</h2>
<p>During the workshop we developed and employed agent based models using the NetLogo software. Netlogo is a versatile, multi-agent programmable modeling environment widely used for simulating natural and social phenomena. It allows users to create and explore complex systems through agent-based models, which consist of numerous individual entities, known as “agents,” that interact within a defined environment according to specific rules.</p>
<section id="key-features" class="level3">
<h3 class="anchored" data-anchor-id="key-features">Key Features</h3>
<ol type="1">
<li><p><strong>Agents</strong>: The core of NetLogo models are agents, which can be of different types:</p>
<ul>
<li><strong>Turtles</strong>: The primary agents that move around the world.</li>
<li><strong>Patches</strong>: The grid cells that make up the world; they don’t move but can change state.</li>
<li><strong>Links</strong>: Connections between turtles that can represent relationships or flows.</li>
<li><strong>Observer</strong>: A special agent that monitors and controls the simulation.</li>
</ul></li>
<li><p><strong>Environment</strong>: NetLogo’s world is typically a two-dimensional grid of patches where turtles move and interact. The environment can be customized to simulate various real-world scenarios, from ecosystems to traffic systems.</p></li>
<li><p><strong>Programming Language</strong>: NetLogo uses a Logo-based programming language that for building both simple and complex models.</p></li>
</ol>
</section>
</section>
<section id="further-reading" class="level2">
<h2 class="anchored" data-anchor-id="further-reading">Further Reading</h2>
<ul>
<li><a href="https://www.ncrm.ac.uk/resources/online/ABM_index.php">Agent-based modelling for social research</a></li>
<li><a href="https://research.criticalconnections.com.au/abmbook/">Agent-Based Modelling for the Self Learner</a></li>
<li><a href="https://ccl.northwestern.edu/netlogo/docs/dictionary.html">NetLogo Dictionary</a></li>
</ul>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{monaghan2024,
  author = {Monaghan, Cormac},
  title = {Agent {Based} {Models}},
  date = {2024-07-10},
  url = {https://c-monaghan.github.io/posts/2024/09/27/ABM/},
  doi = {10.59350/50r9q-kyb55},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-monaghan2024" class="csl-entry quarto-appendix-citeas">
Monaghan, Cormac. 2024. <span>“Agent Based Models.”</span> July 10,
2024. <a href="https://doi.org/10.59350/50r9q-kyb55">https://doi.org/10.59350/50r9q-kyb55</a>.
</div></div></section></div> ]]></description>
  <guid>https://c-monaghan.github.io/posts/2024/09/27/ABM/</guid>
  <pubDate>Tue, 09 Jul 2024 23:00:00 GMT</pubDate>
  <media:content url="https://c-monaghan.github.io/posts/2024/09/27/ABM/ABMs.png" medium="image" type="image/png" height="84" width="144"/>
</item>
</channel>
</rss>
