<?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>RTUG</title>
<link>https://rtug.unitn.it/</link>
<atom:link href="https://rtug.unitn.it/index.xml" rel="self" type="application/rss+xml"/>
<description>R-Trento Users Group</description>
<generator>quarto-1.9.38</generator>
<lastBuildDate>Thu, 16 Apr 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>mdbplyr: a tidy interface to MongoDB</title>
  <dc:creator>Paolo Bosetti</dc:creator>
  <link>https://rtug.unitn.it/posts/011-mdbplyr/mdbplyr.html</link>
  <description><![CDATA[ 




<section id="introduction" class="level1 page-columns page-full">
<h1>Introduction</h1>
<p>We are developing <a href="https://cran.r-project.org/package=mdbplyr"><code>mdbplyr</code></a>, an R package that provides a disciplined, lazy <code>dplyr</code>-style interface for MongoDB aggregation pipelines.</p>

<div class="no-row-height column-margin column-container"><div class="">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/011-mdbplyr/mdbplyr.png" class="img-fluid figure-img"></p>
<figcaption>mdbplyr logo</figcaption>
</figure>
</div>
</div></div><p><code>mdbplyr</code> sits between raw <code>mongolite</code> usage and broader compatibility layers. Compared with writing aggregation JSON by hand, it lets you express supported queries with tidy verbs and inspect the generated pipeline before execution. Compared with approaches that try to hide MongoDB behind table-like semantics, it stays explicit about scope, keeps translation conservative, and fails clearly when a verb or expression is outside the supported subset.</p>
<p>The practical advantages are:</p>
<ul>
<li>lazy query construction until <code>collect()</code>,</li>
<li>direct translation to MongoDB aggregation stages,</li>
<li><code>show_query()</code> for inspectable pipeline output,</li>
<li>explicit unsupported-feature errors instead of silent fallback,</li>
<li>a smaller, testable semantic surface aligned with MongoDB rather than SQL.</li>
</ul>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Important
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post is a quick introduction to <code>mdbplyr</code> with examples. The package is still in early development, so expect some rough edges and missing features. The API is not yet stable, and the documentation is still a work in progress. If you’re interested in trying it out or contributing, check out the <a href="https://github.com/pbosetti/mdbplyr">GitHub repository</a>.</p>
</div>
</div>
</section>
<section id="usage-examples" class="level1">
<h1>Usage examples</h1>
<section id="installation" class="level2">
<h2 class="anchored" data-anchor-id="installation">Installation</h2>
<p>Since April 22, 20226, <code>mdbplyr</code> is available on CRAN. You can install it with:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mdbplyr"</span>)</span></code></pre></div></div>
</div>
<p>Since <code>mdbplyr</code> is still in early development, you can alternatively install the latest development version from GitHub using the <code>devtools</code> package:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"devtools"</span>)</span>
<span id="cb2-2">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pbosetti/mdbplyr"</span>)</span></code></pre></div></div>
</div>
</section>
<section id="loading-data" class="level2">
<h2 class="anchored" data-anchor-id="loading-data">Loading data</h2>
<p>Suppose there is a running MongoDB instance on <code>localhost</code> with default port and no authentication. The code below loads <code>dplyr::starwars</code> into a collection named <code>starwars</code>.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mongolite)</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mdbplyr)</span>
<span id="cb3-4"></span>
<span id="cb3-5">starwars_collection <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mongolite<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mongo</span>(</span>
<span id="cb3-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collection =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"starwars"</span>,</span>
<span id="cb3-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">db =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mdbplyr"</span></span>
<span id="cb3-8">)</span>
<span id="cb3-9"></span>
<span id="cb3-10">starwars_collection<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop</span>()</span>
<span id="cb3-11">starwars_collection<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">insert</span>(dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>starwars)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>List of 5
 $ nInserted  : num 87
 $ nMatched   : num 0
 $ nRemoved   : num 0
 $ nUpserted  : num 0
 $ writeErrors: list()</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">starwars_tbl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_mongo</span>(</span>
<span id="cb5-2">  starwars_collection,</span>
<span id="cb5-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">schema =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>starwars)</span>
<span id="cb5-4">)</span></code></pre></div></div>
</div>
<p>Once the collection is loaded, <code>starwars_tbl</code> is the lazy table used in the examples below.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mdbplyr)</span>
<span id="cb6-3"></span>
<span id="cb6-4">starwars_collection <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mongolite<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mongo</span>(</span>
<span id="cb6-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collection =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"starwars"</span>,</span>
<span id="cb6-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">db =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mdbplyr"</span></span>
<span id="cb6-7">)</span>
<span id="cb6-8"></span>
<span id="cb6-9">starwars_tbl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_mongo</span>(starwars_collection) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">infer_schema</span>()</span></code></pre></div></div>
</div>
</section>
<section id="basic-inspection" class="level2">
<h2 class="anchored" data-anchor-id="basic-inspection">Basic inspection</h2>
<p>Inspect the known schema and the generated pipeline without executing the query.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">schema_fields</span>(starwars_tbl)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "_id"        "name"       "height"     "mass"       "hair_color"
 [6] "skin_color" "eye_color"  "birth_year" "sex"        "gender"    
[11] "homeworld"  "species"    "films"      "vehicles"   "starships" </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">starwars_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Human"</span>, height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name, height, mass) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">show_query</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[
  {
    "$match": {
      "$expr": {
        "$and": {
          "1": {
            "$eq": [
              "$species",
              "Human"
            ]
          },
          "2": {
            "$gt": [
              "$height",
              180
            ]
          }
        }
      }
    }
  },
  {
    "$project": {
      "name": 1,
      "height": 1,
      "mass": 1,
      "_id": 0
    }
  }
] </code></pre>
</div>
</div>
</section>
</section>
<section id="supported-verbs" class="level1">
<h1>Supported verbs</h1>
<p>Each subsection below shows one of the supported <code>dplyr</code>-like verbs on the <code>starwars</code> collection.</p>
<section id="filter" class="level2">
<h2 class="anchored" data-anchor-id="filter"><code>filter()</code></h2>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">starwars_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Droid"</span>, height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 87 × 15
   `_id`     name  height  mass hair_color skin_color eye_color birth_year sex  
   &lt;chr&gt;     &lt;chr&gt;  &lt;int&gt; &lt;dbl&gt; &lt;chr&gt;      &lt;chr&gt;      &lt;chr&gt;          &lt;dbl&gt; &lt;chr&gt;
 1 6a1ffb87… Luke…    172    77 blond      fair       blue            19   male 
 2 6a1ffb87… C-3PO    167    75 &lt;NA&gt;       gold       yellow         112   none 
 3 6a1ffb87… R2-D2     96    32 &lt;NA&gt;       white, bl… red             33   none 
 4 6a1ffb87… Dart…    202   136 none       white      yellow          41.9 male 
 5 6a1ffb87… Leia…    150    49 brown      light      brown           19   fema…
 6 6a1ffb87… Owen…    178   120 brown, gr… light      blue            52   male 
 7 6a1ffb87… Beru…    165    75 brown      light      blue            47   fema…
 8 6a1ffb87… R5-D4     97    32 &lt;NA&gt;       white, red red             NA   none 
 9 6a1ffb87… Bigg…    183    84 black      light      brown           24   male 
10 6a1ffb87… Obi-…    182    77 auburn, w… fair       blue-gray       57   male 
# ℹ 77 more rows
# ℹ 6 more variables: gender &lt;chr&gt;, homeworld &lt;chr&gt;, species &lt;chr&gt;,
#   films &lt;list&gt;, vehicles &lt;list&gt;, starships &lt;list&gt;</code></pre>
</div>
</div>
</section>
<section id="select" class="level2">
<h2 class="anchored" data-anchor-id="select"><code>select()</code></h2>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">starwars_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name, species, homeworld) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 87 × 3
   name               homeworld species
   &lt;chr&gt;              &lt;chr&gt;     &lt;chr&gt;  
 1 Luke Skywalker     Tatooine  Human  
 2 C-3PO              Tatooine  Droid  
 3 R2-D2              Naboo     Droid  
 4 Darth Vader        Tatooine  Human  
 5 Leia Organa        Alderaan  Human  
 6 Owen Lars          Tatooine  Human  
 7 Beru Whitesun Lars Tatooine  Human  
 8 R5-D4              Tatooine  Droid  
 9 Biggs Darklighter  Tatooine  Human  
10 Obi-Wan Kenobi     Stewjon   Human  
# ℹ 77 more rows</code></pre>
</div>
</div>
</section>
<section id="rename" class="level2">
<h2 class="anchored" data-anchor-id="rename"><code>rename()</code></h2>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">starwars_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">character_name =</span> name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">planet =</span> homeworld) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 87 × 15
   `_id`  character_name height  mass hair_color skin_color eye_color birth_year
   &lt;chr&gt;  &lt;chr&gt;           &lt;int&gt; &lt;dbl&gt; &lt;chr&gt;      &lt;chr&gt;      &lt;chr&gt;          &lt;dbl&gt;
 1 6a1ff… Luke Skywalker    172    77 blond      fair       blue            19  
 2 6a1ff… C-3PO             167    75 &lt;NA&gt;       gold       yellow         112  
 3 6a1ff… R2-D2              96    32 &lt;NA&gt;       white, bl… red             33  
 4 6a1ff… Darth Vader       202   136 none       white      yellow          41.9
 5 6a1ff… Leia Organa       150    49 brown      light      brown           19  
 6 6a1ff… Owen Lars         178   120 brown, gr… light      blue            52  
 7 6a1ff… Beru Whitesun…    165    75 brown      light      blue            47  
 8 6a1ff… R5-D4              97    32 &lt;NA&gt;       white, red red             NA  
 9 6a1ff… Biggs Darklig…    183    84 black      light      brown           24  
10 6a1ff… Obi-Wan Kenobi    182    77 auburn, w… fair       blue-gray       57  
# ℹ 77 more rows
# ℹ 7 more variables: sex &lt;chr&gt;, gender &lt;chr&gt;, planet &lt;chr&gt;, species &lt;chr&gt;,
#   films &lt;list&gt;, vehicles &lt;list&gt;, starships &lt;list&gt;</code></pre>
</div>
</div>
</section>
<section id="mutate" class="level2">
<h2 class="anchored" data-anchor-id="mutate"><code>mutate()</code></h2>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">starwars_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height_m =</span> height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bmi_like =</span> mass <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> height_m<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># can't be in the same mutate()!</span></span>
<span id="cb17-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name, height, mass, height_m, bmi_like) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 87 × 5
   name               height  mass height_m bmi_like
   &lt;chr&gt;               &lt;int&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;
 1 Luke Skywalker        172    77     1.72     26.0
 2 C-3PO                 167    75     1.67     26.9
 3 R2-D2                  96    32     0.96     34.7
 4 Darth Vader           202   136     2.02     33.3
 5 Leia Organa           150    49     1.5      21.8
 6 Owen Lars             178   120     1.78     37.9
 7 Beru Whitesun Lars    165    75     1.65     27.5
 8 R5-D4                  97    32     0.97     34.0
 9 Biggs Darklighter     183    84     1.83     25.1
10 Obi-Wan Kenobi        182    77     1.82     23.2
# ℹ 77 more rows</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Important
</div>
</div>
<div class="callout-body-container callout-body">
<p>Currently, <code>mdbplyr</code> implements mapping between MongoDB operators and:</p>
<ul>
<li>Arithmetic/predicate: <code>%%,</code> <code>^,</code> <code>%in%</code></li>
<li>Logs/math: <code>log(x, base)</code>, <code>log10()</code>, <code>floor()</code>, <code>ceiling()</code>, <code>trunc()</code></li>
<li>Trig/hyperbolic: <code>sin()</code>, <code>cos()</code>, <code>tan()</code>, <code>asin()</code>, <code>acos()</code>, <code>atan()</code>, <code>atan2()</code>, <code>sinh()</code>, <code>cosh()</code>, <code>tanh()</code>, <code>asinh()</code>, <code>acosh()</code>, <code>atanh()</code></li>
<li>Scalar min/max: <code>pmin()</code>, <code>pmax()</code></li>
<li>String ops: <code>tolower()</code>, <code>toupper()</code>, <code>nchar()</code>, <code>paste()</code>, <code>paste0()</code>, <code>substr()</code>, <code>substring()</code></li>
<li>Literal vector support for expressions like <code>x %in% c(1, 3, 5)</code></li>
</ul>
<p>Note though, that within the same <code>mutate</code> call you cannot use a newly created field in another expression, as the translation is currently one-to-one with MongoDB’s <code>$addFields</code> stage. So you need to split the BMI-like calculation into two <code>mutate()</code> calls, as in te above example.</p>
</div>
</div>
</section>
<section id="transmute" class="level2">
<h2 class="anchored" data-anchor-id="transmute"><code>transmute()</code></h2>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">starwars_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb19-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transmute</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height_m =</span> height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb19-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 87 × 2
   name               height_m
   &lt;chr&gt;                 &lt;dbl&gt;
 1 Luke Skywalker         1.72
 2 C-3PO                  1.67
 3 R2-D2                  0.96
 4 Darth Vader            2.02
 5 Leia Organa            1.5 
 6 Owen Lars              1.78
 7 Beru Whitesun Lars     1.65
 8 R5-D4                  0.97
 9 Biggs Darklighter      1.83
10 Obi-Wan Kenobi         1.82
# ℹ 77 more rows</code></pre>
</div>
</div>
</section>
<section id="arrange" class="level2">
<h2 class="anchored" data-anchor-id="arrange"><code>arrange()</code></h2>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">starwars_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb21-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(height), name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb21-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name, height) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb21-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_head</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb21-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 10 × 2
   name         height
   &lt;chr&gt;         &lt;int&gt;
 1 Yarael Poof     264
 2 Tarfful         234
 3 Lama Su         229
 4 Chewbacca       228
 5 Roos Tarpals    224
 6 Grievous        216
 7 Taun We         213
 8 Rugor Nass      206
 9 Tion Medon      206
10 Darth Vader     202</code></pre>
</div>
</div>
</section>
<section id="group_by" class="level2">
<h2 class="anchored" data-anchor-id="group_by"><code>group_by()</code></h2>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">starwars_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb23-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(species)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>&lt;tbl_mongo&gt; collection 
  Filters: 0 
  Projection: &lt;all&gt; 
  Computed: &lt;none&gt; 
  Groups: species 
  Summaries: &lt;none&gt; 
  Slice: &lt;none&gt; 
  Manual stages: 0 </code></pre>
</div>
</div>
</section>
<section id="summarise" class="level2">
<h2 class="anchored" data-anchor-id="summarise"><code>summarise()</code></h2>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">starwars_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb25-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb25-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb25-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(),</span>
<span id="cb25-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(height),</span>
<span id="cb25-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(mass)</span>
<span id="cb25-7">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb25-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb25-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 38 × 4
   species      n avg_height max_mass
   &lt;chr&gt;    &lt;int&gt;      &lt;dbl&gt;    &lt;dbl&gt;
 1 Human       35       178     136  
 2 Droid        6       131.    140  
 3 &lt;NA&gt;         4       175     110  
 4 Gungan       3       209.     82  
 5 Zabrak       2       173      80  
 6 Twi'lek      2       179      55  
 7 Mirialan     2       168      56.2
 8 Wookiee      2       231     136  
 9 Kaminoan     2       221      88  
10 Cerean       1       198      82  
# ℹ 28 more rows</code></pre>
</div>
</div>
</section>
<section id="slice_head" class="level2">
<h2 class="anchored" data-anchor-id="slice_head"><code>slice_head()</code></h2>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">starwars_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb27-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name, species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb27-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_head</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb27-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 2
  name           species
  &lt;chr&gt;          &lt;chr&gt;  
1 Luke Skywalker Human  
2 C-3PO          Droid  
3 R2-D2          Droid  
4 Darth Vader    Human  
5 Leia Organa    Human  </code></pre>
</div>
</div>
</section>
<section id="head" class="level2">
<h2 class="anchored" data-anchor-id="head"><code>head()</code></h2>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(starwars_tbl, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb29-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 15
  `_id`      name  height  mass hair_color skin_color eye_color birth_year sex  
  &lt;chr&gt;      &lt;chr&gt;  &lt;int&gt; &lt;dbl&gt; &lt;chr&gt;      &lt;chr&gt;      &lt;chr&gt;          &lt;dbl&gt; &lt;chr&gt;
1 6a1ffb87d… Luke…    172    77 blond      fair       blue            19   male 
2 6a1ffb87d… C-3PO    167    75 &lt;NA&gt;       gold       yellow         112   none 
3 6a1ffb87d… R2-D2     96    32 &lt;NA&gt;       white, bl… red             33   none 
4 6a1ffb87d… Dart…    202   136 none       white      yellow          41.9 male 
5 6a1ffb87d… Leia…    150    49 brown      light      brown           19   fema…
# ℹ 6 more variables: gender &lt;chr&gt;, homeworld &lt;chr&gt;, species &lt;chr&gt;,
#   films &lt;list&gt;, vehicles &lt;list&gt;, starships &lt;list&gt;</code></pre>
</div>
</div>
</section>
</section>
<section id="notes" class="level1">
<h1>Notes</h1>
<p>The examples above stay within the currently supported subset:</p>
<ul>
<li>explicit field references,</li>
<li>scalar expressions for <code>mutate()</code> and <code>transmute()</code>,</li>
<li>conservative grouped summaries,</li>
<li>no joins, window functions, or automatic client-side fallback.</li>
</ul>
<p>If a query falls outside that subset, <code>mdbplyr</code> is designed to fail explicitly rather than guess or silently change execution semantics.</p>
<div class="thatsall">
<p>That’s all, folks!</p>
</div>


</section>

 ]]></description>
  <category>post</category>
  <category>database</category>
  <category>MongoDB</category>
  <category>packages</category>
  <category>milestone</category>
  <guid>https://rtug.unitn.it/posts/011-mdbplyr/mdbplyr.html</guid>
  <pubDate>Thu, 16 Apr 2026 00:00:00 GMT</pubDate>
  <media:content url="https://rtug.unitn.it/posts/011-mdbplyr/mdbplyr.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>RTUG::25.2 meeting</title>
  <dc:creator>Paolo Bosetti</dc:creator>
  <link>https://rtug.unitn.it/meetings/25.2-meeting.html</link>
  <description><![CDATA[ 




<p>It is now time for planning the next R-Trento Users Meeting, code <code>RTUG::25.2</code>. The event is loosely planned for late Fall 2025. Content and location are still to be defined, but we are looking for contributions from the community, i.e.&nbsp;<strong>YOU!</strong></p>
<p>Details will be announced here and on our <a href="https://www.meetup.com/r-trento-users-group/">Meetup page</a>.</p>
<p>Remember: RTUG meetings are open to contributions, in the form of:</p>
<ul>
<li>presentations</li>
<li>workshops or hands-on sessions</li>
<li>discussions on open topics</li>
<li>request for help or collaboration on specific issues</li>
</ul>
<p>Drop your proposals to <script type="text/javascript">
<!--
h='&#x75;&#110;&#x69;&#116;&#110;&#46;&#x69;&#116;';a='&#64;';n='&#114;&#116;&#x75;&#x67;&#46;&#x67;&#114;&#x6f;&#x75;&#112;';e=n+a+h;
document.write('<a h'+'ref'+'="ma'+'ilto'+':'+e+'" clas'+'s="em' + 'ail email">'+e+'<\/'+'a'+'>');
// -->
</script><noscript>rtug.group at unitn dot it</noscript>.</p>
<hr>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c2062692070617463682d7175657374696f6e203e7d7d-topics" class="level2">
<h2 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c2062692070617463682d7175657374696f6e203e7d7d-topics"> Topics</h2>
<p>To be defined.</p>
</section>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c206269206275696c64696e67203e7d7d-venue" class="level2">
<h2 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c206269206275696c64696e67203e7d7d-venue"> Venue</h2>
<p>To be defined.</p>
</section>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c206269207a6f6f6d2d696e203e7d7d-details-on-topics" class="level2">
<h2 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c206269207a6f6f6d2d696e203e7d7d-details-on-topics"> Details on Topics</h2>
<p>To be defined.</p>


</section>

 ]]></description>
  <guid>https://rtug.unitn.it/meetings/25.2-meeting.html</guid>
  <pubDate>Mon, 02 Jun 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Tukey vs. Student</title>
  <dc:creator>Paolo Bosetti</dc:creator>
  <link>https://rtug.unitn.it/posts/008-tukey-vs-student/tukey-vs-student.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-note callout-titled" title="Packages that we need">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Packages that we need
</div>
</div>
<div class="callout-body-container callout-body">
<p>In this example, we are using the packages <code>tidyverse</code> and <code>adas.utils</code> version 1.1.4 (see <a href="https://github.com/pbosetti/adas.utils" class="uri">https://github.com/pbosetti/adas.utils</a>)</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(adas.utils)</span></code></pre></div></div>
</div>
</div>
</div>
<section id="repeated-t-test-vs.-tukeys-test" class="level1">
<h1>Repeated T-test vs.&nbsp;Tukey’s test</h1>
<section id="the-dataset" class="level2">
<h2 class="anchored" data-anchor-id="the-dataset">The dataset</h2>
<p>Let us compare the result of a Tukey’s test with a repeated Student’s T-test on all combinations. We consider the <code>cotton</code> dataset, which is included in the <code>adas.utils</code> package from version 1.1.4. The dataset contains the tensile strength of mixed cotton-synthetic yarns with different cotton content:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">cotton <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>Cotton, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>Strength, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group=</span>Cotton)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</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://rtug.unitn.it/posts/008-tukey-vs-student/tukey-vs-student_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:16cm"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="inference-on-strength" class="level2">
<h2 class="anchored" data-anchor-id="inference-on-strength">Inference on <code>Strength</code></h2>
<p>Now we want to compare all the possibile combinations of treatmentswith a set of pairwise T-tests.</p>
<p>First, we create the list of pairwise combinations, sorting each pair in descending order, as it is done by the <code>TukeyHSD</code> function:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">lvl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(cotton<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Cotton) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">combn</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN=</span>sort, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">decreasing=</span>T) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.name_repair=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"minimal"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>List of 10
 $ : chr [1:2] "20" "15"
 $ : chr [1:2] "25" "15"
 $ : chr [1:2] "30" "15"
 $ : chr [1:2] "35" "15"
 $ : chr [1:2] "25" "20"
 $ : chr [1:2] "30" "20"
 $ : chr [1:2] "35" "20"
 $ : chr [1:2] "30" "25"
 $ : chr [1:2] "35" "25"
 $ : chr [1:2] "35" "30"</code></pre>
</div>
</div>
<p>Now, for each pair we do a T-test on the corresponding <code>cotton</code> data-frame subset, and accumulate into a new tibble the values of interest. We get the <code>df</code> table that is analogous to the <code>TukeyHSD</code> output:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lvl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reduce</span>(\(acc, pair) {</span>
<span id="cb5-2">  tt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cotton <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(Cotton <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> pair) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(Strength<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Cotton, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var.equal=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(acc, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb5-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pair =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(pair[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, pair[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]),</span>
<span id="cb5-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diff =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>conf.int),</span>
<span id="cb5-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwr =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>conf.int[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb5-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upr =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>conf.int[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb5-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p.value =</span> tt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value</span>
<span id="cb5-11">  ))</span>
<span id="cb5-12">}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.init=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>()) </span>
<span id="cb5-13"></span>
<span id="cb5-14">df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">pair</th>
<th style="text-align: right;">diff</th>
<th style="text-align: right;">lwr</th>
<th style="text-align: right;">upr</th>
<th style="text-align: right;">p.value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">20-15</td>
<td style="text-align: right;">5.6</td>
<td style="text-align: right;">0.8740978</td>
<td style="text-align: right;">10.3259022</td>
<td style="text-align: right;">0.0257453</td>
</tr>
<tr class="even">
<td style="text-align: left;">25-15</td>
<td style="text-align: right;">7.8</td>
<td style="text-align: right;">3.7398608</td>
<td style="text-align: right;">11.8601392</td>
<td style="text-align: right;">0.0021967</td>
</tr>
<tr class="odd">
<td style="text-align: left;">30-15</td>
<td style="text-align: right;">11.8</td>
<td style="text-align: right;">7.4246648</td>
<td style="text-align: right;">16.1753352</td>
<td style="text-align: right;">0.0002541</td>
</tr>
<tr class="even">
<td style="text-align: left;">35-15</td>
<td style="text-align: right;">1.0</td>
<td style="text-align: right;">-3.5423014</td>
<td style="text-align: right;">5.5423014</td>
<td style="text-align: right;">0.6253800</td>
</tr>
<tr class="odd">
<td style="text-align: left;">25-20</td>
<td style="text-align: right;">2.2</td>
<td style="text-align: right;">-1.6724395</td>
<td style="text-align: right;">6.0724395</td>
<td style="text-align: right;">0.2265324</td>
</tr>
<tr class="even">
<td style="text-align: left;">30-20</td>
<td style="text-align: right;">6.2</td>
<td style="text-align: right;">1.9982605</td>
<td style="text-align: right;">10.4017395</td>
<td style="text-align: right;">0.0093233</td>
</tr>
<tr class="odd">
<td style="text-align: left;">35-20</td>
<td style="text-align: right;">-4.6</td>
<td style="text-align: right;">-8.9753352</td>
<td style="text-align: right;">-0.2246648</td>
<td style="text-align: right;">0.0415629</td>
</tr>
<tr class="even">
<td style="text-align: left;">30-25</td>
<td style="text-align: right;">4.0</td>
<td style="text-align: right;">0.5641312</td>
<td style="text-align: right;">7.4358688</td>
<td style="text-align: right;">0.0277266</td>
</tr>
<tr class="odd">
<td style="text-align: left;">35-25</td>
<td style="text-align: right;">-6.8</td>
<td style="text-align: right;">-10.4461127</td>
<td style="text-align: right;">-3.1538873</td>
<td style="text-align: right;">0.0026133</td>
</tr>
<tr class="even">
<td style="text-align: left;">35-30</td>
<td style="text-align: right;">-10.8</td>
<td style="text-align: right;">-14.7941163</td>
<td style="text-align: right;">-6.8058837</td>
<td style="text-align: right;">0.0002496</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>To be compared with Tukey’s values:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">ttdf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">TukeyHSD</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aov</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Strength<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Cotton, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>cotton)))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Cotton <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames_to_column</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pair"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p.value=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p adj</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span>
<span id="cb6-5">ttdf <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">pair</th>
<th style="text-align: right;">diff</th>
<th style="text-align: right;">lwr</th>
<th style="text-align: right;">upr</th>
<th style="text-align: right;">p.value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">20-15</td>
<td style="text-align: right;">5.6</td>
<td style="text-align: right;">0.2270417</td>
<td style="text-align: right;">10.9729583</td>
<td style="text-align: right;">0.0385024</td>
</tr>
<tr class="even">
<td style="text-align: left;">25-15</td>
<td style="text-align: right;">7.8</td>
<td style="text-align: right;">2.4270417</td>
<td style="text-align: right;">13.1729583</td>
<td style="text-align: right;">0.0025948</td>
</tr>
<tr class="odd">
<td style="text-align: left;">30-15</td>
<td style="text-align: right;">11.8</td>
<td style="text-align: right;">6.4270417</td>
<td style="text-align: right;">17.1729583</td>
<td style="text-align: right;">0.0000190</td>
</tr>
<tr class="even">
<td style="text-align: left;">35-15</td>
<td style="text-align: right;">1.0</td>
<td style="text-align: right;">-4.3729583</td>
<td style="text-align: right;">6.3729583</td>
<td style="text-align: right;">0.9797709</td>
</tr>
<tr class="odd">
<td style="text-align: left;">25-20</td>
<td style="text-align: right;">2.2</td>
<td style="text-align: right;">-3.1729583</td>
<td style="text-align: right;">7.5729583</td>
<td style="text-align: right;">0.7372438</td>
</tr>
<tr class="even">
<td style="text-align: left;">30-20</td>
<td style="text-align: right;">6.2</td>
<td style="text-align: right;">0.8270417</td>
<td style="text-align: right;">11.5729583</td>
<td style="text-align: right;">0.0188936</td>
</tr>
<tr class="odd">
<td style="text-align: left;">35-20</td>
<td style="text-align: right;">-4.6</td>
<td style="text-align: right;">-9.9729583</td>
<td style="text-align: right;">0.7729583</td>
<td style="text-align: right;">0.1162970</td>
</tr>
<tr class="even">
<td style="text-align: left;">30-25</td>
<td style="text-align: right;">4.0</td>
<td style="text-align: right;">-1.3729583</td>
<td style="text-align: right;">9.3729583</td>
<td style="text-align: right;">0.2101089</td>
</tr>
<tr class="odd">
<td style="text-align: left;">35-25</td>
<td style="text-align: right;">-6.8</td>
<td style="text-align: right;">-12.1729583</td>
<td style="text-align: right;">-1.4270417</td>
<td style="text-align: right;">0.0090646</td>
</tr>
<tr class="even">
<td style="text-align: left;">35-30</td>
<td style="text-align: right;">-10.8</td>
<td style="text-align: right;">-16.1729583</td>
<td style="text-align: right;">-5.4270417</td>
<td style="text-align: right;">0.0000624</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Now let’s join both tables and make a common plot:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">compared <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(ttdf, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join_by</span>(pair), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">suffix=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".student"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".tukey"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>pair, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stat"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"test"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(.*)</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.(student|tukey)$"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> stat)</span>
<span id="cb7-5"></span>
<span id="cb7-6">compared <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">pair</th>
<th style="text-align: left;">test</th>
<th style="text-align: right;">diff</th>
<th style="text-align: right;">lwr</th>
<th style="text-align: right;">upr</th>
<th style="text-align: right;">p.value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">20-15</td>
<td style="text-align: left;">student</td>
<td style="text-align: right;">5.6</td>
<td style="text-align: right;">0.8740978</td>
<td style="text-align: right;">10.3259022</td>
<td style="text-align: right;">0.0257453</td>
</tr>
<tr class="even">
<td style="text-align: left;">20-15</td>
<td style="text-align: left;">tukey</td>
<td style="text-align: right;">5.6</td>
<td style="text-align: right;">0.2270417</td>
<td style="text-align: right;">10.9729583</td>
<td style="text-align: right;">0.0385024</td>
</tr>
<tr class="odd">
<td style="text-align: left;">25-15</td>
<td style="text-align: left;">student</td>
<td style="text-align: right;">7.8</td>
<td style="text-align: right;">3.7398608</td>
<td style="text-align: right;">11.8601392</td>
<td style="text-align: right;">0.0021967</td>
</tr>
<tr class="even">
<td style="text-align: left;">25-15</td>
<td style="text-align: left;">tukey</td>
<td style="text-align: right;">7.8</td>
<td style="text-align: right;">2.4270417</td>
<td style="text-align: right;">13.1729583</td>
<td style="text-align: right;">0.0025948</td>
</tr>
<tr class="odd">
<td style="text-align: left;">30-15</td>
<td style="text-align: left;">student</td>
<td style="text-align: right;">11.8</td>
<td style="text-align: right;">7.4246648</td>
<td style="text-align: right;">16.1753352</td>
<td style="text-align: right;">0.0002541</td>
</tr>
<tr class="even">
<td style="text-align: left;">30-15</td>
<td style="text-align: left;">tukey</td>
<td style="text-align: right;">11.8</td>
<td style="text-align: right;">6.4270417</td>
<td style="text-align: right;">17.1729583</td>
<td style="text-align: right;">0.0000190</td>
</tr>
<tr class="odd">
<td style="text-align: left;">35-15</td>
<td style="text-align: left;">student</td>
<td style="text-align: right;">1.0</td>
<td style="text-align: right;">-3.5423014</td>
<td style="text-align: right;">5.5423014</td>
<td style="text-align: right;">0.6253800</td>
</tr>
<tr class="even">
<td style="text-align: left;">35-15</td>
<td style="text-align: left;">tukey</td>
<td style="text-align: right;">1.0</td>
<td style="text-align: right;">-4.3729583</td>
<td style="text-align: right;">6.3729583</td>
<td style="text-align: right;">0.9797709</td>
</tr>
<tr class="odd">
<td style="text-align: left;">25-20</td>
<td style="text-align: left;">student</td>
<td style="text-align: right;">2.2</td>
<td style="text-align: right;">-1.6724395</td>
<td style="text-align: right;">6.0724395</td>
<td style="text-align: right;">0.2265324</td>
</tr>
<tr class="even">
<td style="text-align: left;">25-20</td>
<td style="text-align: left;">tukey</td>
<td style="text-align: right;">2.2</td>
<td style="text-align: right;">-3.1729583</td>
<td style="text-align: right;">7.5729583</td>
<td style="text-align: right;">0.7372438</td>
</tr>
<tr class="odd">
<td style="text-align: left;">30-20</td>
<td style="text-align: left;">student</td>
<td style="text-align: right;">6.2</td>
<td style="text-align: right;">1.9982605</td>
<td style="text-align: right;">10.4017395</td>
<td style="text-align: right;">0.0093233</td>
</tr>
<tr class="even">
<td style="text-align: left;">30-20</td>
<td style="text-align: left;">tukey</td>
<td style="text-align: right;">6.2</td>
<td style="text-align: right;">0.8270417</td>
<td style="text-align: right;">11.5729583</td>
<td style="text-align: right;">0.0188936</td>
</tr>
<tr class="odd">
<td style="text-align: left;">35-20</td>
<td style="text-align: left;">student</td>
<td style="text-align: right;">-4.6</td>
<td style="text-align: right;">-8.9753352</td>
<td style="text-align: right;">-0.2246648</td>
<td style="text-align: right;">0.0415629</td>
</tr>
<tr class="even">
<td style="text-align: left;">35-20</td>
<td style="text-align: left;">tukey</td>
<td style="text-align: right;">-4.6</td>
<td style="text-align: right;">-9.9729583</td>
<td style="text-align: right;">0.7729583</td>
<td style="text-align: right;">0.1162970</td>
</tr>
<tr class="odd">
<td style="text-align: left;">30-25</td>
<td style="text-align: left;">student</td>
<td style="text-align: right;">4.0</td>
<td style="text-align: right;">0.5641312</td>
<td style="text-align: right;">7.4358688</td>
<td style="text-align: right;">0.0277266</td>
</tr>
<tr class="even">
<td style="text-align: left;">30-25</td>
<td style="text-align: left;">tukey</td>
<td style="text-align: right;">4.0</td>
<td style="text-align: right;">-1.3729583</td>
<td style="text-align: right;">9.3729583</td>
<td style="text-align: right;">0.2101089</td>
</tr>
<tr class="odd">
<td style="text-align: left;">35-25</td>
<td style="text-align: left;">student</td>
<td style="text-align: right;">-6.8</td>
<td style="text-align: right;">-10.4461127</td>
<td style="text-align: right;">-3.1538873</td>
<td style="text-align: right;">0.0026133</td>
</tr>
<tr class="even">
<td style="text-align: left;">35-25</td>
<td style="text-align: left;">tukey</td>
<td style="text-align: right;">-6.8</td>
<td style="text-align: right;">-12.1729583</td>
<td style="text-align: right;">-1.4270417</td>
<td style="text-align: right;">0.0090646</td>
</tr>
<tr class="odd">
<td style="text-align: left;">35-30</td>
<td style="text-align: left;">student</td>
<td style="text-align: right;">-10.8</td>
<td style="text-align: right;">-14.7941163</td>
<td style="text-align: right;">-6.8058837</td>
<td style="text-align: right;">0.0002496</td>
</tr>
<tr class="even">
<td style="text-align: left;">35-30</td>
<td style="text-align: left;">tukey</td>
<td style="text-align: right;">-10.8</td>
<td style="text-align: right;">-16.1729583</td>
<td style="text-align: right;">-5.4270417</td>
<td style="text-align: right;">0.0000624</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">compared <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>diff, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>pair, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span>test)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_errorbar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmin=</span>lwr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax=</span>upr), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_dodge</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Difference"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pair"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"95% pairwise confidence level"</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://rtug.unitn.it/posts/008-tukey-vs-student/tukey-vs-student_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:16cm"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="the-family-wise-error-rate" class="level2">
<h2 class="anchored" data-anchor-id="the-family-wise-error-rate">The Family-Wise Error Rate</h2>
<div class="callout callout-style-default callout-important callout-titled" title="The Family-Wise Error Rate">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Important</span>The Family-Wise Error Rate
</div>
</div>
<div class="callout-body-container callout-body">
<p>As expected, the Tukey’s test in the last plot shows larger confidence intervals, that is, it has reduced chances of a false positive (Type I Error). More specifically, Tukey’s test controls the <strong>family-wise error rate (FWER)</strong> — the probability of making any false positive in the full set of comparisons.</p>
</div>
</div>
<p>Let’s see why. If we set a confidence level of 0.95, it means that the probability of not making a Type I error on <strong>a single</strong> T-test is 0.95.</p>
<p>For 3 independent tests, the probability of no Type I error at all (in any of the tests) is: <img src="https://latex.codecogs.com/png.latex?%0A0.95%5E3%20%5Capprox%200.857%0A"> So the chance of making at least one Type I error is: <img src="https://latex.codecogs.com/png.latex?%0A1%20-%200.95%5E3%20%5Capprox%200.143%20%5Cquad%20%5Ctext%7B(14.3%5C%25)%7D%0A"> That’s almost triple the risk you thought you were accepting! Furthermore, this risk increases exponentially with the number of comparisons. Given <img src="https://latex.codecogs.com/png.latex?n"> elements, the number of possible combinations of <img src="https://latex.codecogs.com/png.latex?k"> elements is given by the <strong>binomial coefficient</strong>: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbinom%7Bn%7D%7Bk%7D%20=%20%5Cfrac%7Bn!%7D%7Bk!(n-k)!%7D%0A"> In R, the latter is provided by the <code>choose(n, k)</code> function:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 10</code></pre>
</div>
</div>
<p>so, with increasing number of classes to be compared, this is what happens to the probability of committing <strong>at least one</strong> Type-I error:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reduce</span>(\(acc, k) {</span>
<span id="cb11-2">    nt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(k, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb11-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(acc,</span>
<span id="cb11-4">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>k, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">-0.95</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nt)</span>
<span id="cb11-5">    )</span>
<span id="cb11-6">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.init=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb11-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>p)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylim</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb11-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"number of classes"</span>, </span>
<span id="cb11-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"probability of Type-I Error"</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://rtug.unitn.it/posts/008-tukey-vs-student/tukey-vs-student_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:16cm"></p>
</figure>
</div>
</div>
</div>
<p>And what happens if we change the confidence level? Let’s see, by creating a parametric plot similar to the ast one, but with different confidence levels. First we factor the last <code>reduce</code> opration into a function, <code>FWER</code>, that takes the confidence level as an argument. The function returns a tibble with the number of classes and the corresponding probability of Type I error.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">FWER <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(levels, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf.int=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>) {</span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reduce</span>(levels, \(acc, k) {</span>
<span id="cb12-3">    nt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(k, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb12-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(acc,</span>
<span id="cb12-5">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>k, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>conf.int<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nt)</span>
<span id="cb12-6">    )</span>
<span id="cb12-7">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.init=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>()) </span>
<span id="cb12-8">}</span></code></pre></div></div>
</div>
<p>Then we apply the <code>FWER</code> function to a set of confidence levels, and join the results into a single tibble via the usual <code>reduce</code>, and finally, we plot the results:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">cl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.995</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.999</span>) </span>
<span id="cb13-2">N <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span></span>
<span id="cb13-3">cl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reduce</span>(\(acc, ci) {</span>
<span id="cb13-5">    fwer <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">FWER</span>(N, ci) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cl-"</span>, ci)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span>p)</span>
<span id="cb13-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(acc, fwer, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join_by</span>(n))</span>
<span id="cb13-7">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.init=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>N)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb13-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cl"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_pattern=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(cl-)(.*)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb13-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span>cl)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb13-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb13-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb13-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylim</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb13-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb13-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"number of classes"</span>, </span>
<span id="cb13-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"probability of Type-I Error"</span>, </span>
<span id="cb13-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Conf. level"</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://rtug.unitn.it/posts/008-tukey-vs-student/tukey-vs-student_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:16cm"></p>
</figure>
</div>
</div>
</div>
<div class="thatsall">
<p>That’s all, folks!</p>
</div>


</section>
</section>

 ]]></description>
  <category>R</category>
  <category>tidyverse</category>
  <category>inference</category>
  <category>Tukey</category>
  <guid>https://rtug.unitn.it/posts/008-tukey-vs-student/tukey-vs-student.html</guid>
  <pubDate>Thu, 10 Apr 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rtug.unitn.it/posts/008-tukey-vs-student/image.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>R-Consortium supporting RTUG</title>
  <dc:creator>Paolo Bosetti</dc:creator>
  <link>https://rtug.unitn.it/posts/007-r-consortium/</link>
  <description><![CDATA[ 




<section id="were-supported-by-the-r-consortium" class="level1 page-columns page-full">
<h1>We’re supported by the R-Consortium</h1>

<div class="no-row-height column-margin column-container"><div class="">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/007-r-consortium/milestone.jpg" class="img-fluid figure-img"></p>
<figcaption>An important milestone</figcaption>
</figure>
</div>
</div></div><p>Today, March 28th, 2025, we are proud to announce that the R-Trento Users Group has been accepted by the <a href="https://www.r-consortium.org" target="_blank">R-Consortium</a>. This means that we are now officially recognized as a community of R users and developers, and we will receive support from the R-Consortium for our activities.</p>
<p>We thank the R-Consortium for the financial support that will allow us to organize our first in-person meeting in the next months and to have our meeting announcements published on <a href="https://www.meetup.com/r-rrento-users-group/"><img src="https://help.meetup.com/hc/theming_assets/01HZH3ZAT7AR9TR620NM18CDKN.png" style="height:2em"></a>.</p>


</section>

 ]]></description>
  <category>RTUG</category>
  <category>milestone</category>
  <category>announcement</category>
  <guid>https://rtug.unitn.it/posts/007-r-consortium/</guid>
  <pubDate>Fri, 28 Mar 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rtug.unitn.it/posts/007-r-consortium/milestone.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Tukey’s test plot in adas.utils</title>
  <dc:creator>Paolo Bosetti</dc:creator>
  <link>https://rtug.unitn.it/posts/006-tukey/</link>
  <description><![CDATA[ 




<section id="rationale" class="level1 page-columns page-full">
<h1>Rationale</h1>
<p>The new version v1.1.1 of the <code>adas.utils</code> package includes a new function to plot the results of Tukey’s test. The function is called <code>ggTukey</code> and it is used to plot the results of Tukey’s test provided by the <code>stas::TukeyHSD</code> function.</p>

<div class="no-row-height column-margin column-container"><div class="">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/006-tukey/Tukey.jpg" class="img-fluid figure-img"></p>
<figcaption>John W. Tukey</figcaption>
</figure>
</div>
</div></div><p>The standard <code>stats::TukeyHSD</code> function returns an S3 object that has the <code>print</code> and <code>plot</code> methods. The result of the <code>plot</code> method, though, is honestly not really appealing. Let’s see how it works, by loading a dataset and running a Tukey’s test on it. We load an online dataset by using the <code>adas.utils::examples_url</code> function<sup>1</sup>, and we begin with a simple boxplot of the data.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(adas.utils)</span>
<span id="cb1-3"></span>
<span id="cb1-4">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">examples_url</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"anova.dat"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Cotton=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Cotton)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 25
Columns: 3
$ Cotton      &lt;fct&gt; 15, 15, 15, 15, 15, 20, 20, 20, 20, 20, 25, 25, 25, 25, 25…
$ Observation &lt;int&gt; 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5…
$ Strength    &lt;int&gt; 7, 7, 15, 11, 9, 12, 17, 12, 18, 18, 14, 18, 18, 19, 19, 1…</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>Cotton, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>Strength, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group=</span>Cotton)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</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://rtug.unitn.it/posts/006-tukey/index_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:16cm"></p>
</figure>
</div>
</div>
</div>
<p>The Tukey’s test is built by using the <code>aov</code> function and the <code>TukeyHSD</code> function. The results are then plotted by the <code>plot</code> method of the <code>TukeyHSD</code> object:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aov</span>(Strength <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Cotton, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>.) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">TukeyHSD</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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://rtug.unitn.it/posts/006-tukey/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:16cm"></p>
</figure>
</div>
</div>
</div>
<p>The biggest problem with <code>plot.TukeyHSD</code> is that the labels of the differences are often partially hidden if there are many groups or the plot is too squat. This is the main reason for implementing an analogous function based on GGplot2 in <code>adas.utils</code>.</p>
</section>
<section id="enter-ggtukey" class="level1">
<h1>Enter <code>ggTukey</code></h1>
<p>Labels are much more readable in the <code>ggTukey</code> plot. Let’s see how it works:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aov</span>(Strength <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Cotton, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>.) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">TukeyHSD</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggTukey</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://rtug.unitn.it/posts/006-tukey/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:16cm"></p>
</figure>
</div>
</div>
</div>
<p>There’s actually more: you can also pass to <code>ggTukey</code> the data frame and the formula to be used in the <code>aov</code> model:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggTukey</span>(Strength <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Cotton)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/006-tukey/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:16cm"></p>
</figure>
</div>
</div>
</div>
<p>Which is nicely more tidiverse-y and readable, isn’t it?</p>
<p>Now let’s look at a different, slightly more complex dataset:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">examples_url</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"battery.dat"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Material=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(LETTERS[Material]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Temperature=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Temperature)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 36
Columns: 6
$ RunOrder      &lt;int&gt; 34, 25, 16, 7, 8, 1, 26, 36, 6, 13, 3, 31, 27, 29, 12, 1…
$ StandardOrder &lt;int&gt; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1…
$ Temperature   &lt;fct&gt; 15, 70, 125, 15, 70, 125, 15, 70, 125, 15, 70, 125, 15, …
$ Material      &lt;fct&gt; A, A, A, B, B, B, C, C, C, A, A, A, B, B, B, C, C, C, A,…
$ Repeat        &lt;int&gt; 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,…
$ Response      &lt;int&gt; 130, 34, 20, 150, 136, 25, 138, 174, 96, 155, 40, 70, 18…</code></pre>
</div>
</div>
<p>This one shows discharge test results for batteries with different dielectric materials (qualitative factors) and operating at different temperatures (quantitative factors). So inn this case we have two predictors. We want to create a set of Tukey’s tests for each level of <code>Material</code>. We need to pass <code>ggTukey</code> with the formula <code>Response ~ Temperature</code> (for the <code>aov</code> model) and the <code>splt</code> argument with another, one side formula that specifies the grouping factor: <code>~Material</code>. In a terse and clear way, we can write:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggTukey</span>(Response <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Temperature, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">splt=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Material)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/006-tukey/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:16cm"></p>
</figure>
</div>
</div>
</div>
<p>And that was not possible with the standard <code>plot.TukeyHSD</code> method, at least not in two lines of code.</p>
<div class="thatsall">
<p>That’s all, folks!</p>
</div>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>This function can load any data file listed on <a href="https://paolobosetti.quarto.pub/data" class="uri">https://paolobosetti.quarto.pub/data</a>↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>R</category>
  <category>packages</category>
  <guid>https://rtug.unitn.it/posts/006-tukey/</guid>
  <pubDate>Thu, 27 Mar 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rtug.unitn.it/posts/006-tukey/Tukey.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Moving to a new domain</title>
  <dc:creator>Paolo Bosetti</dc:creator>
  <link>https://rtug.unitn.it/posts/005-domain/</link>
  <description><![CDATA[ 




<p>Starting from February 26, 2025, RTUG website has a new domain under the <a href="https://unitn.it" target="_blank">University of Trento</a> domain: <code>rtug.unitn.it</code>. So the group’s homepage is now accessible as <a href="https://rtug.unitn.it" class="uri">https://rtug.unitn.it</a>.</p>
<p>Furthermore, there is a new contact email address: <script type="text/javascript">
<!--
h='&#x75;&#110;&#x69;&#116;&#110;&#46;&#x69;&#116;';a='&#64;';n='&#114;&#116;&#x75;&#x67;&#46;&#x67;&#114;&#x6f;&#x75;&#112;';e=n+a+h;
document.write('<a h'+'ref'+'="ma'+'ilto'+':'+e+'" clas'+'s="em' + 'ail uri">'+'&#x6d;&#x61;&#x69;&#108;&#116;&#x6f;&#58;&#114;&#116;&#x75;&#x67;&#46;&#x67;&#114;&#x6f;&#x75;&#112;&#64;&#x75;&#110;&#x69;&#116;&#110;&#46;&#x69;&#116;'+'<\/'+'a'+'>');
// -->
</script><noscript>mailto:rtug.group@unitn.it (rtug.group at unitn dot it)</noscript></p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The old address <a href="https://r-trento.github.io" class="uri">https://r-trento.github.io</a> will continue to work, automatically redirecting to the new domain name.</p>
</div>
</div>
<div class="thatsall">
<p>That’s all, folks!</p>
</div>



 ]]></description>
  <category>RTUG</category>
  <category>milestone</category>
  <guid>https://rtug.unitn.it/posts/005-domain/</guid>
  <pubDate>Wed, 26 Feb 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rtug.unitn.it/posts/005-domain/image.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Introducing the adas.utils package</title>
  <dc:creator>Paolo Bosetti</dc:creator>
  <link>https://rtug.unitn.it/posts/004-adas.utils/</link>
  <description><![CDATA[ 




<section id="factorial-plans" class="level1 page-columns page-full">
<h1>Factorial plans</h1>
<p>The package provides tools for dealing with factorial plan according to the <em>Design of Experiments</em> (DoE) protocols. The functions for dealing with DoE have names starting with <code>fp_</code>. As much as possible, we are aiming at a tidyverse-like syntax, so that the functions can be used in a pipe.</p>
<p>We are following conventions and techniques illustrated in the book <em>Design and Analysis of Experiments</em> by Douglas C. Montgomery.</p>
<section id="full-factorial-plan" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="full-factorial-plan">Full factorial plan</h2>

<div class="no-row-height column-margin column-container"><div class="">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/004-adas.utils/hypercube.jpg" class="img-fluid figure-img"></p>
<figcaption>A hypercube, clearly impossible to represent in 2D</figcaption>
</figure>
</div>
</div></div><p>You can create a full factorial plan with the <code>fp_design_matrix</code> function, passing the number of factors:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">(dm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_design_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rep=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())))</span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factorial Plan Design Matrix</span></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Defining Relationship:  ~ A * B </span></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factors:  A B </span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Levels:  -1 1 </span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Fraction:  NA </span></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Type:  plain </span></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  </span></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 8 × 7</span></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   StdOrder RunOrder .treat  .rep     A     B      Y</span></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      &lt;int&gt;    &lt;int&gt; &lt;chr&gt;  &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;</span></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1        1        5 (1)        1    -1    -1  1.06 </span></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2        2        3 a          1     1    -1 -0.670</span></span>
<span id="cb1-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3        3        6 b          1    -1     1 -0.800</span></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4        4        1 ab         1     1     1  1.27 </span></span>
<span id="cb1-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5        5        8 (1)        2    -1    -1  0.417</span></span>
<span id="cb1-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6        6        4 a          2     1    -1  2.29 </span></span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 7        7        2 b          2    -1     1 -1.71 </span></span>
<span id="cb1-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 8        8        7 ab         2     1     1  0.942</span></span></code></pre></div></div>
</div>
<p>In this case, the factors are the first <code>n</code> capital letters.</p>
<p>If you want different factor names, use a right-side-only formula combining all the named factors with <code>*</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_design_matrix</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Speed<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>Weight)</span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factorial Plan Design Matrix</span></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Defining Relationship:  ~ Speed * Weight </span></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factors:  Speed Weight </span></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Levels:  -1 1 </span></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Fraction:  NA </span></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Type:  plain </span></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  </span></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 4 × 7</span></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   StdOrder RunOrder .treat       .rep Speed Weight Y    </span></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      &lt;int&gt;    &lt;int&gt; &lt;chr&gt;       &lt;int&gt; &lt;dbl&gt;  &lt;dbl&gt; &lt;lgl&gt;</span></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1        1        4 (1)             1    -1     -1 NA   </span></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2        2        3 speed           1     1     -1 NA   </span></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3        3        2 weight          1    -1      1 NA   </span></span>
<span id="cb2-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4        4        1 speedweight     1     1      1 NA</span></span></code></pre></div></div>
</div>
<p><strong>NOTE</strong>, though, that using custom factor names is discouraged, and won’t work as expected if you are using the functions for dealing with <strong>fractional factorial plans</strong>, especially for the analysis of alias structures among factors.</p>
<p>The yield column <code>Y</code> must then be completed according to the randomized <code>RunOrder</code> column.</p>
<p>It is possible to add custom scales to the factors, and also add names to the factors:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_design_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_add_names</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temperature"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pressure"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_add_scale</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">125</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">suffix=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".scaled"</span>)</span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factorial Plan Design Matrix</span></span>
<span id="cb3-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Defining Relationship:  ~ A * B </span></span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factors:  A B </span></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Levels:  -1 1 </span></span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Fraction:  NA </span></span>
<span id="cb3-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Type:  plain </span></span>
<span id="cb3-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Scales suffix: .scaled</span></span>
<span id="cb3-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Scaled factors:</span></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     A.scaled: [20, 25]</span></span>
<span id="cb3-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     B.scaled: [75, 125]</span></span>
<span id="cb3-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factor names:</span></span>
<span id="cb3-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     A: Temperature</span></span>
<span id="cb3-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;     B: Pressure</span></span>
<span id="cb3-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  </span></span>
<span id="cb3-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 4 × 9</span></span>
<span id="cb3-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   StdOrder RunOrder .treat  .rep     A     B A.scaled B.scaled Y    </span></span>
<span id="cb3-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      &lt;int&gt;    &lt;int&gt; &lt;chr&gt;  &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt; &lt;lgl&gt;</span></span>
<span id="cb3-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1        1        4 (1)        1    -1    -1       20       75 NA   </span></span>
<span id="cb3-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2        2        1 a          1     1    -1       25       75 NA   </span></span>
<span id="cb3-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3        3        3 b          1    -1     1       20      125 NA   </span></span>
<span id="cb3-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4        4        2 ab         1     1     1       25      125 NA</span></span></code></pre></div></div>
</div>
</section>
<section id="custom-levels" class="level2">
<h2 class="anchored" data-anchor-id="custom-levels">Custom levels</h2>
<p>If you want a <img src="https://latex.codecogs.com/png.latex?k%5En"> factorial plan with custom levels, pass the <code>levels</code> argument. In this case, though, the <code>.treat</code> column with Yates’ treatment codes would be <code>NA</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_design_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factorial Plan Design Matrix</span></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Defining Relationship:  ~ A * B </span></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factors:  A B </span></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Levels:  -1 0 1 </span></span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Fraction:  NA </span></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Type:  plain </span></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  </span></span>
<span id="cb4-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 9 × 6</span></span>
<span id="cb4-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   StdOrder RunOrder  .rep     A     B Y    </span></span>
<span id="cb4-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      &lt;int&gt;    &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;lgl&gt;</span></span>
<span id="cb4-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1        1        5     1    -1    -1 NA   </span></span>
<span id="cb4-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2        2        4     1     0    -1 NA   </span></span>
<span id="cb4-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3        3        1     1     1    -1 NA   </span></span>
<span id="cb4-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4        4        2     1    -1     0 NA   </span></span>
<span id="cb4-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5        5        8     1     0     0 NA   </span></span>
<span id="cb4-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6        6        6     1     1     0 NA   </span></span>
<span id="cb4-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 7        7        7     1    -1     1 NA   </span></span>
<span id="cb4-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 8        8        9     1     0     1 NA   </span></span>
<span id="cb4-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 9        9        3     1     1     1 NA</span></span></code></pre></div></div>
</div>
</section>
<section id="augment-a-plan" class="level2">
<h2 class="anchored" data-anchor-id="augment-a-plan">Augment a plan</h2>
<p>You can augment a plan by adding a central point, typically repeated:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_design_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_augment_center</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rep=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factorial Plan Design Matrix</span></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Defining Relationship:  ~ A * B * C </span></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factors:  A B C </span></span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Levels:  -1 1 </span></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Fraction:  NA </span></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Type:  centered </span></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  </span></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 12 × 8</span></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    StdOrder RunOrder .treat  .rep     A     B     C Y    </span></span>
<span id="cb5-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;       &lt;int&gt;    &lt;int&gt; &lt;chr&gt;  &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;lgl&gt;</span></span>
<span id="cb5-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  1        1        2 (1)        1    -1    -1    -1 NA   </span></span>
<span id="cb5-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  2        2        8 a          1     1    -1    -1 NA   </span></span>
<span id="cb5-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  3        3        5 b          1    -1     1    -1 NA   </span></span>
<span id="cb5-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  4        4        1 ab         1     1     1    -1 NA   </span></span>
<span id="cb5-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  5        5        6 c          1    -1    -1     1 NA   </span></span>
<span id="cb5-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  6        6        4 ac         1     1    -1     1 NA   </span></span>
<span id="cb5-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  7        7        3 bc         1    -1     1     1 NA   </span></span>
<span id="cb5-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  8        8        7 abc        1     1     1     1 NA   </span></span>
<span id="cb5-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  9        9       12 center     1     0     0     0 NA   </span></span>
<span id="cb5-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 10       10        9 center     2     0     0     0 NA   </span></span>
<span id="cb5-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 11       11       11 center     3     0     0     0 NA   </span></span>
<span id="cb5-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 12       12       10 center     4     0     0     0 NA</span></span></code></pre></div></div>
</div>
<p>Then if needed (because the analysis show low p-value for the quadratic term) you can add axial points to get a central composite design:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_design_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_augment_center</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rep=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_augment_axial</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rep=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factorial Plan Design Matrix</span></span>
<span id="cb6-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Defining Relationship:  ~ A * B * C </span></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factors:  A B C </span></span>
<span id="cb6-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Levels:  -1 1 </span></span>
<span id="cb6-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Fraction:  NA </span></span>
<span id="cb6-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Type:  composite </span></span>
<span id="cb6-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  </span></span>
<span id="cb6-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 23 × 8</span></span>
<span id="cb6-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    StdOrder RunOrder .treat  .rep     A     B     C Y    </span></span>
<span id="cb6-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;       &lt;int&gt;    &lt;int&gt; &lt;chr&gt;  &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;lgl&gt;</span></span>
<span id="cb6-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  1        1        5 (1)        1    -1    -1    -1 NA   </span></span>
<span id="cb6-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  2        2        3 a          1     1    -1    -1 NA   </span></span>
<span id="cb6-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  3        3        2 b          1    -1     1    -1 NA   </span></span>
<span id="cb6-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  4        4        7 ab         1     1     1    -1 NA   </span></span>
<span id="cb6-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  5        5        6 c          1    -1    -1     1 NA   </span></span>
<span id="cb6-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  6        6        8 ac         1     1    -1     1 NA   </span></span>
<span id="cb6-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  7        7        4 bc         1    -1     1     1 NA   </span></span>
<span id="cb6-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  8        8        1 abc        1     1     1     1 NA   </span></span>
<span id="cb6-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  9        9       11 center     1     0     0     0 NA   </span></span>
<span id="cb6-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 10       10       10 center     2     0     0     0 NA   </span></span>
<span id="cb6-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # ℹ 13 more rows</span></span></code></pre></div></div>
</div>
<section id="full-example" class="level3">
<h3 class="anchored" data-anchor-id="full-example">Full example</h3>
<p>Let’s see a full example using the <code>ccd_experiment_yield</code> dataset, which contains a list of the yield data for three sequential experiments in a central composite design.</p>
<p>First, we design a <img src="https://latex.codecogs.com/png.latex?3%5Ccdot%202%5E2"> factorial plan, with two factors and two levels each:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">fp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_design_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rep=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
</div>
<p>Ideally, we would then sort the table according to the <code>RunOrder</code> column, and complete the <code>Y</code> column with the yield data from the the real experiments. For the sake of documenting the package, we can directly add the yield data from the <code>base</code> field of the <code>ccd_experiment_yield</code> dataset (which holds values <strong>in standard Yates’ order</strong>):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">fp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ccd_experiment_yield<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>base</span></code></pre></div></div>
</div>
<p>Now we can fit a linear model to the data, and check the p-values of the ANOVA:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">fp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>B, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>.) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>()</span>
<span id="cb9-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Analysis of Variance Table</span></span>
<span id="cb9-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb9-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Response: Y</span></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;           Df  Sum Sq Mean Sq F value    Pr(&gt;F)    </span></span>
<span id="cb9-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A          1  49.331  49.331  105.06 7.058e-06 ***</span></span>
<span id="cb9-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; B          1 131.224 131.224  279.46 1.659e-07 ***</span></span>
<span id="cb9-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A:B        1 216.931 216.931  461.99 2.311e-08 ***</span></span>
<span id="cb9-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Residuals  8   3.756   0.470                      </span></span>
<span id="cb9-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ---</span></span>
<span id="cb9-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</span></span></code></pre></div></div>
</div>
<p>All factors and their interactions are significant. But is the two-level model enough? Let’s check for the quadratic terms, by augmenting the plan with a central point repeated 4 times. We also load the <code>center</code> field from the <code>ccd_experiment_yield</code> dataset:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">fpc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb10-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_augment_center</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rep=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb10-3"></span>
<span id="cb10-4">fpc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y[fpc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.treat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ccd_experiment_yield<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>center</span></code></pre></div></div>
</div>
<p>Now we can fit a model with the quadratic term, using either <img src="https://latex.codecogs.com/png.latex?A"> or <img src="https://latex.codecogs.com/png.latex?B">: since we only have a central point, we cannot discriminate which factor is contributing to the curvature in the response surface. We get:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">fpc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>B<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>.) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>()</span>
<span id="cb11-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Analysis of Variance Table</span></span>
<span id="cb11-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb11-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Response: Y</span></span>
<span id="cb11-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;           Df  Sum Sq Mean Sq F value    Pr(&gt;F)    </span></span>
<span id="cb11-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A          1  49.331  49.331  96.801 8.695e-07 ***</span></span>
<span id="cb11-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; B          1 131.224 131.224 257.494 5.592e-09 ***</span></span>
<span id="cb11-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; I(A^2)     1  15.204  15.204  29.834 0.0001972 ***</span></span>
<span id="cb11-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A:B        1 216.931 216.931 425.673 3.827e-10 ***</span></span>
<span id="cb11-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Residuals 11   5.606   0.510                      </span></span>
<span id="cb11-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ---</span></span>
<span id="cb11-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</span></span></code></pre></div></div>
</div>
<p>So the contribution of the quadratic term <strong>is significant</strong>. This means that we have to further augment the plan with axial points and investigate a <em>Central Composite Design</em> (CCD). <strong>Note</strong>: if the quadratic term contribution were not significant, we would have to remove the quadratic term from the model and accept the two-level model.</p>
<p>So let’s load the axial points from the <code>axial</code> field of the <code>ccd_experiment_yield</code> dataset, and fit a model with the quadratic terms and their interactions:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">fpccd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fpc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_augment_axial</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rep=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb12-3"></span>
<span id="cb12-4">fpccd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y[fpccd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.treat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"axial"</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ccd_experiment_yield<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>axial</span>
<span id="cb12-5"></span>
<span id="cb12-6">fpccd <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb12-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>B<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(B<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>.) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb12-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>()</span>
<span id="cb12-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Analysis of Variance Table</span></span>
<span id="cb12-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb12-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Response: Y</span></span>
<span id="cb12-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;               Df  Sum Sq Mean Sq  F value    Pr(&gt;F)    </span></span>
<span id="cb12-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A              1  75.196  75.196  94.1952 7.395e-08 ***</span></span>
<span id="cb12-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; B              1 194.702 194.702 243.8964 1.097e-10 ***</span></span>
<span id="cb12-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; I(A^2)         1 101.355 101.355 126.9638 1.017e-08 ***</span></span>
<span id="cb12-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; I(B^2)         1   3.551   3.551   4.4479   0.05216 .  </span></span>
<span id="cb12-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A:B            1 216.931 216.931 271.7423 5.087e-11 ***</span></span>
<span id="cb12-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A:I(A^2)       1   0.235   0.235   0.2945   0.59530    </span></span>
<span id="cb12-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; B:I(A^2)       1   1.046   1.046   1.3106   0.27022    </span></span>
<span id="cb12-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; I(A^2):I(B^2)  1   0.490   0.490   0.6142   0.44542    </span></span>
<span id="cb12-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Residuals     15  11.974   0.798                       </span></span>
<span id="cb12-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ---</span></span>
<span id="cb12-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</span></span></code></pre></div></div>
</div>
<p>So we can finally state that a proper model would be <code>Y ~ A*B+I(A^2)</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">fpccd <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>B<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>.) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>()</span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb13-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Call:</span></span>
<span id="cb13-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; lm(formula = Y ~ A * B + I(A^2), data = .)</span></span>
<span id="cb13-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb13-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Residuals:</span></span>
<span id="cb13-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      Min       1Q   Median       3Q      Max </span></span>
<span id="cb13-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; -1.64925 -0.60624  0.00919  0.65165  1.67506 </span></span>
<span id="cb13-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb13-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Coefficients:</span></span>
<span id="cb13-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;             Estimate Std. Error t value Pr(&gt;|t|)    </span></span>
<span id="cb13-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; (Intercept)   1.0156     0.3061   3.318  0.00362 ** </span></span>
<span id="cb13-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A             1.9390     0.2134   9.088 2.40e-08 ***</span></span>
<span id="cb13-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; B             3.1201     0.2134  14.624 8.59e-12 ***</span></span>
<span id="cb13-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; I(A^2)        2.9905     0.2834  10.552 2.20e-09 ***</span></span>
<span id="cb13-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A:B           4.2518     0.2754  15.437 3.32e-12 ***</span></span>
<span id="cb13-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ---</span></span>
<span id="cb13-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</span></span>
<span id="cb13-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb13-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Residual standard error: 0.9541 on 19 degrees of freedom</span></span>
<span id="cb13-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Multiple R-squared:  0.9714, Adjusted R-squared:  0.9654 </span></span>
<span id="cb13-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; F-statistic: 161.5 on 4 and 19 DF,  p-value: 2.191e-14</span></span></code></pre></div></div>
</div>
</section>
</section>
<section id="save-toload-from-a-file" class="level2">
<h2 class="anchored" data-anchor-id="save-toload-from-a-file">Save to/load from a file</h2>
<p>Once the design matrix is prepared, you typically want to save it to a file and use it for collecting data form experiments. You can use the <code>write.csv</code> function, but it is recommended to use the <code>fp_write_csv</code> function, which will also save the design matrix properties as comments:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">dm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_design_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_add_names</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temperature"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pressure"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_add_scale</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">suffix=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_s"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_write_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"design_matrix.csv"</span>)</span></code></pre></div></div>
</div>
<p>Note that the <code>fp_write_csv</code> function invisibly returns the same design matrix, so you can use it in a pipe chain. Also, the CVS files has the rows arranged in the same order as the <code>RunOrder</code> column (i.e.&nbsp;randomized).</p>
<p>Once the CSV file has been completed, you can load it back into R using the <code>fp_read_csv</code> function:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">dm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb15-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"design_matrix.csv"</span>)</span></code></pre></div></div>
</div>
<p>Note that <code>fp_read_csv</code> returns the design matrix reordered according to Yates’ standard order.</p>
</section>
<section id="fractional-factorial-plan" class="level2">
<h2 class="anchored" data-anchor-id="fractional-factorial-plan">Fractional factorial plan</h2>
<p>It is possible to divide a design matrix into a fractional factorial plan using the <code>fp_fraction</code> function. The fraction uses a <strong>defining relationship</strong> (<em>dr</em>) as <img src="https://latex.codecogs.com/png.latex?I=ABCD">, which is mapped in R as a one side formula <code>~A*B*C*D</code>.</p>
<p>Any fraction is added to the <code>factorial.plan</code> object in the <code>fraction</code> attribute.</p>
<p>A full <img src="https://latex.codecogs.com/png.latex?2%5En"> factorial plan can be reduced to a fractional factorial plan <img src="https://latex.codecogs.com/png.latex?2%5E%7Bn-p%7D"> by applying the <code>fp_fraction</code> function <img src="https://latex.codecogs.com/png.latex?p"> times. For example, to get a <img src="https://latex.codecogs.com/png.latex?2%5E%7B5-2%7D"> plan with the defining relationships <img src="https://latex.codecogs.com/png.latex?I=ABCD"> and <img src="https://latex.codecogs.com/png.latex?I=BCDE">:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_design_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb16-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_fraction</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>B<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>C<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>D) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb16-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_fraction</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>B<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>C<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>D<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>E)</span>
<span id="cb16-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factorial Plan Design Matrix</span></span>
<span id="cb16-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Defining Relationship:  ~ A * B * C * D * E </span></span>
<span id="cb16-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factors:  A B C D E </span></span>
<span id="cb16-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Levels:  -1 1 </span></span>
<span id="cb16-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Fraction:  I=ABCD I=BCDE </span></span>
<span id="cb16-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Type:  fractional </span></span>
<span id="cb16-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  </span></span>
<span id="cb16-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 8 × 12</span></span>
<span id="cb16-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   StdOrder RunOrder .treat  .rep     A     B     C     D     E Y      ABCD  BCDE</span></span>
<span id="cb16-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      &lt;int&gt;    &lt;int&gt; &lt;chr&gt;  &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;lgl&gt; &lt;dbl&gt; &lt;dbl&gt;</span></span>
<span id="cb16-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1        1        7 (1)        1    -1    -1    -1    -1    -1 NA        1     1</span></span>
<span id="cb16-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2        7       29 bc         1    -1     1     1    -1    -1 NA        1     1</span></span>
<span id="cb16-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3       11       28 bd         1    -1     1    -1     1    -1 NA        1     1</span></span>
<span id="cb16-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4       13       23 cd         1    -1    -1     1     1    -1 NA        1     1</span></span>
<span id="cb16-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5       20       20 abe        1     1     1    -1    -1     1 NA        1     1</span></span>
<span id="cb16-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6       22       16 ace        1     1    -1     1    -1     1 NA        1     1</span></span>
<span id="cb16-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 7       26       26 ade        1     1    -1    -1     1     1 NA        1     1</span></span>
<span id="cb16-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 8       32       10 abcde      1     1     1     1     1     1 NA        1     1</span></span></code></pre></div></div>
</div>
<p>Note that with the <code>remove</code> option you can control if you want to keep both fractions, and later on <code>filter(ABC==1)</code> them out.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_design_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb17-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_fraction</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>B<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>C, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">remove=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb17-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factorial Plan Design Matrix</span></span>
<span id="cb17-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Defining Relationship:  ~ A * B * C </span></span>
<span id="cb17-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Factors:  A B C </span></span>
<span id="cb17-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Levels:  -1 1 </span></span>
<span id="cb17-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Fraction:  I=ABC </span></span>
<span id="cb17-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Type:  fractional </span></span>
<span id="cb17-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  </span></span>
<span id="cb17-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 8 × 9</span></span>
<span id="cb17-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   StdOrder RunOrder .treat  .rep     A     B     C Y       ABC</span></span>
<span id="cb17-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      &lt;int&gt;    &lt;int&gt; &lt;chr&gt;  &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;lgl&gt; &lt;dbl&gt;</span></span>
<span id="cb17-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1        1        8 (1)        1    -1    -1    -1 NA       -1</span></span>
<span id="cb17-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2        2        6 a          1     1    -1    -1 NA        1</span></span>
<span id="cb17-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3        3        3 b          1    -1     1    -1 NA        1</span></span>
<span id="cb17-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4        4        4 ab         1     1     1    -1 NA       -1</span></span>
<span id="cb17-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5        5        2 c          1    -1    -1     1 NA        1</span></span>
<span id="cb17-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6        6        7 ac         1     1    -1     1 NA       -1</span></span>
<span id="cb17-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 7        7        5 bc         1    -1     1     1 NA       -1</span></span>
<span id="cb17-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 8        8        1 abc        1     1     1     1 NA        1</span></span></code></pre></div></div>
</div>
<p>Also, note that the <code>remove</code> option is sticky, so that when you can apply the <code>fp_fraction</code> function multiple times and the first time has the option set to <code>remove=FALSE</code>, then all the following <code>fp_fraction</code> calls will have the same option set to <code>FALSE</code>. Setting <code>remove=FALSE</code> to any of the following calls <strong>can have unexpected behavior</strong>.</p>
</section>
<section id="alias-structure" class="level2">
<h2 class="anchored" data-anchor-id="alias-structure">Alias structure</h2>
<p>Any fraction of a factorial plan results in a set of <em>aliases</em> among effects. The package provides the following functions to deal with alias structures:</p>
<ul>
<li><code>fp_alias_matrix</code>: returns a matrix with the alias structure of the factors in the design matrix. The alias matrix has a plot method.</li>
<li><code>fp_all_drs</code>: given a set of defining relationships, returns the dependent one.</li>
<li><code>fp_merge_drs</code>: given a set of defining relationships, returns the merged one, i.e.&nbsp;the one having all the factors.</li>
<li><code>fp_gen2alias</code>: given a <em>generator</em> (i.e.&nbsp;the right side of a DR) and an effect name as strings, calculates the resulting alias.</li>
</ul>
<p>For example:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">(am <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fp_alias_matrix</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>B<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>C, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>B<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>C<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>D))</span>
<span id="cb18-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Defining relationships:</span></span>
<span id="cb18-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  I=ABC I=BCD I=AD </span></span>
<span id="cb18-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb18-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      A B AB C AC BC ABC D AD BD ABD CD ACD BCD ABCD</span></span>
<span id="cb18-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A    0 0  0 0  0  1   0 3  0  0   0  0   0   0    2</span></span>
<span id="cb18-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; B    0 0  0 0  1  0   0 0  0  0   3  2   0   0    0</span></span>
<span id="cb18-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; AB   0 0  0 1  0  0   0 0  0  3   0  0   2   0    0</span></span>
<span id="cb18-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; C    0 0  1 0  0  0   0 0  0  2   0  0   3   0    0</span></span>
<span id="cb18-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; AC   0 1  0 0  0  0   0 0  0  0   2  3   0   0    0</span></span>
<span id="cb18-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; BC   1 0  0 0  0  0   0 2  0  0   0  0   0   0    3</span></span>
<span id="cb18-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ABC  0 0  0 0  0  0   0 0  2  0   0  0   0   3    0</span></span>
<span id="cb18-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; D    3 0  0 0  0  2   0 0  0  0   0  0   0   0    1</span></span>
<span id="cb18-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; AD   0 0  0 0  0  0   2 0  0  0   0  0   0   1    0</span></span>
<span id="cb18-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; BD   0 0  3 2  0  0   0 0  0  0   0  0   1   0    0</span></span>
<span id="cb18-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ABD  0 3  0 0  2  0   0 0  0  0   0  1   0   0    0</span></span>
<span id="cb18-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; CD   0 2  0 0  3  0   0 0  0  0   1  0   0   0    0</span></span>
<span id="cb18-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ACD  0 0  2 3  0  0   0 0  0  1   0  0   0   0    0</span></span>
<span id="cb18-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; BCD  0 0  0 0  0  0   3 0  1  0   0  0   0   0    0</span></span>
<span id="cb18-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ABCD 2 0  0 0  0  3   0 1  0  0   0  0   0   0    0</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">am <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/004-adas.utils/index_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
<p>The design matrix can be converted to a tibble thanks to the proper <code>as_tibble.design.matrix</code> S3 method:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">am <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>()</span>
<span id="cb20-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 42 × 3</span></span>
<span id="cb20-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    Effect.x Effect.y generator</span></span>
<span id="cb20-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    &lt;chr&gt;    &lt;chr&gt;    &lt;chr&gt;    </span></span>
<span id="cb20-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  1 A        BC       ABC      </span></span>
<span id="cb20-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  2 A        D        AD       </span></span>
<span id="cb20-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  3 A        ABCD     BCD      </span></span>
<span id="cb20-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  4 B        AC       ABC      </span></span>
<span id="cb20-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  5 B        ABD      AD       </span></span>
<span id="cb20-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  6 B        CD       BCD      </span></span>
<span id="cb20-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  7 AB       C        ABC      </span></span>
<span id="cb20-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  8 AB       BD       AD       </span></span>
<span id="cb20-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  9 AB       ACD      BCD      </span></span>
<span id="cb20-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 10 C        AB       ABC      </span></span>
<span id="cb20-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # ℹ 32 more rows</span></span></code></pre></div></div>
</div>
</section>
</section>
<section id="statistics" class="level1">
<h1>Statistics</h1>
<p>The package also provides some useful functions for statistical analysis of data.</p>
<section id="plotting" class="level2">
<h2 class="anchored" data-anchor-id="plotting">Plotting</h2>
<section id="normal-probability-plot" class="level3">
<h3 class="anchored" data-anchor-id="normal-probability-plot">Normal probability plot</h3>
<p>The normal probability plot is provided as an alternative to the quantile-quantile plot:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb21-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xn =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb21-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xu =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>)</span>
<span id="cb21-4">)</span>
<span id="cb21-5"></span>
<span id="cb21-6">df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normplot</span>(xn)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/004-adas.utils/index_files/figure-html/unnamed-chunk-21-1.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normplot</span>(xu)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/004-adas.utils/index_files/figure-html/unnamed-chunk-21-2.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="pareto-chart" class="level3">
<h3 class="anchored" data-anchor-id="pareto-chart">Pareto chart</h3>
<p>The Pareto chart is a bar chart that displays the relative importance of problems in a format that is very easy to interpret. The bars are sorted in descending order, and the cumulative percentage of the total is shown by the line.</p>
<p>It can prove useful in the context of factorial plans, to identify the most important factors, or in sensitivity analysis, to identify the most important parameters.</p>
<p>The package provides a <em>generic function</em>, <code>pareto_chart</code>, that can be used with a tibble (or a data frame), or with a linear model (an <code>lm</code> object). In the latter case, the function produces the Pareto chart of the model effects.</p>
<p>For the general case, when you have a tibble with values and names:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb23-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb23-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">val=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb23-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cat=</span>LETTERS[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(val)]</span>
<span id="cb23-5">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pareto_chart</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels=</span>cat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values=</span>val)</span>
<span id="cb23-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Warning in pareto_chart.data.frame(., labels = cat, values = val): 'pareto_chart.data.frame' is deprecated.</span></span>
<span id="cb23-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Use 'geom_pareto' instead.</span></span>
<span id="cb23-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; See help("Deprecated")</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/004-adas.utils/index_files/figure-html/unnamed-chunk-22-1.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
<p>For the case of a linear model:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">filtration <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb24-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>B<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>C<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>D, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>.) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pareto_chart</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/004-adas.utils/index_files/figure-html/unnamed-chunk-23-1.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="daniels-plot" class="level3">
<h3 class="anchored" data-anchor-id="daniels-plot">Daniel’s plot</h3>
<p>In case of non-replicated factorial plans, the Daniel’s plot can be used to identify the most important factors: a quantile-quantile plot of the factors effects shows the significant factors and interactions off the diagonal.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">daniel_plot_qq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>B<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>C<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>D, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>filtration))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/004-adas.utils/index_files/figure-html/unnamed-chunk-24-1.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
<p>If you prefer, you can rather use a half-normal plot:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">filtration <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb26-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>B<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>C<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>D, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>.) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb26-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">daniel_plot_hn</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nlab=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repel=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/004-adas.utils/index_files/figure-html/unnamed-chunk-25-1.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
<p>It shows that none of the effects containing the <code>B</code> factor are significant, so we can reduce the model to <code>Y~A*C*D</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">filtration <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb27-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>C<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>D, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>.) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb27-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>()</span>
<span id="cb27-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Analysis of Variance Table</span></span>
<span id="cb27-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb27-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Response: Y</span></span>
<span id="cb27-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;           Df  Sum Sq Mean Sq F value    Pr(&gt;F)    </span></span>
<span id="cb27-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A          1 1870.56 1870.56 83.3677 1.667e-05 ***</span></span>
<span id="cb27-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; C          1  390.06  390.06 17.3844 0.0031244 ** </span></span>
<span id="cb27-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; D          1  855.56  855.56 38.1309 0.0002666 ***</span></span>
<span id="cb27-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A:C        1 1314.06 1314.06 58.5655 6.001e-05 ***</span></span>
<span id="cb27-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A:D        1 1105.56 1105.56 49.2730 0.0001105 ***</span></span>
<span id="cb27-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; C:D        1    5.06    5.06  0.2256 0.6474830    </span></span>
<span id="cb27-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; A:C:D      1   10.56   10.56  0.4708 0.5120321    </span></span>
<span id="cb27-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Residuals  8  179.50   22.44                      </span></span>
<span id="cb27-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ---</span></span>
<span id="cb27-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</span></span></code></pre></div></div>
</div>
<p>Even better, the model can be further reduced to <code>Y~A*C+A*D</code>. Compare this conclusion with the last Pareto chart above.</p>
</section>
</section>
</section>
<section id="utilities" class="level1">
<h1>Utilities</h1>
<p>This package also provides a function for easily loading data files made available on the accompanying course documentation on <a href="https://paolobosetti.quarto.pub/data" class="uri">https://paolobosetti.quarto.pub/data</a>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">examples_url</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"battery.dat"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb28-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    RunOrder StandardOrder Temperature Material Repeat Response</span></span>
<span id="cb28-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1        34             1          15        1      1      130</span></span>
<span id="cb28-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2        25             2          70        1      1       34</span></span>
<span id="cb28-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3        16             3         125        1      1       20</span></span>
<span id="cb28-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4         7             4          15        2      1      150</span></span>
<span id="cb28-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5         8             5          70        2      1      136</span></span>
<span id="cb28-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6         1             6         125        2      1       25</span></span>
<span id="cb28-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 7        26             7          15        3      1      138</span></span>
<span id="cb28-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 8        36             8          70        3      1      174</span></span>
<span id="cb28-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 9         6             9         125        3      1       96</span></span>
<span id="cb28-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 10       13            10          15        1      2      155</span></span>
<span id="cb28-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 11        3            11          70        1      2       40</span></span>
<span id="cb28-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 12       31            12         125        1      2       70</span></span>
<span id="cb28-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 13       27            13          15        2      2      188</span></span>
<span id="cb28-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 14       29            14          70        2      2      122</span></span>
<span id="cb28-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 15       12            15         125        2      2       70</span></span>
<span id="cb28-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 16       14            16          15        3      2      110</span></span>
<span id="cb28-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 17       30            17          70        3      2      120</span></span>
<span id="cb28-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 18       24            18         125        3      2      104</span></span>
<span id="cb28-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 19        5            19          15        1      3       74</span></span>
<span id="cb28-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 20       21            20          70        1      3       80</span></span>
<span id="cb28-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 21       10            21         125        1      3       82</span></span>
<span id="cb28-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 22       35            22          15        2      3      159</span></span>
<span id="cb28-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 23       19            23          70        2      3      106</span></span>
<span id="cb28-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 24       28            24         125        2      3       58</span></span>
<span id="cb28-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 25       18            25          15        3      3      168</span></span>
<span id="cb28-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 26       20            26          70        3      3      150</span></span>
<span id="cb28-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 27       33            27         125        3      3       82</span></span>
<span id="cb28-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 28       11            28          15        1      4      180</span></span>
<span id="cb28-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 29       23            29          70        1      4       75</span></span>
<span id="cb28-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 30       17            30         125        1      4       58</span></span>
<span id="cb28-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 31       15            31          15        2      4      126</span></span>
<span id="cb28-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 32       32            32          70        2      4      115</span></span>
<span id="cb28-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 33        4            33         125        2      4       45</span></span>
<span id="cb28-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 34       22            34          15        3      4      160</span></span>
<span id="cb28-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 35        2            35          70        3      4      139</span></span>
<span id="cb28-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 36        9            36         125        3      4       60</span></span></code></pre></div></div>
</div>
<div class="thatsall">
<p>That’s all, folks!</p>
</div>


</section>

 ]]></description>
  <category>packages</category>
  <category>post</category>
  <category>CRAN</category>
  <category>design of experiments</category>
  <guid>https://rtug.unitn.it/posts/004-adas.utils/</guid>
  <pubDate>Mon, 10 Feb 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rtug.unitn.it/posts/004-adas.utils/hypercube.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Logarithmic scales in GGPlot2</title>
  <dc:creator>Paolo Bosetti</dc:creator>
  <link>https://rtug.unitn.it/posts/003-logscale/</link>
  <description><![CDATA[ 




<section id="rationale" class="level1">
<h1>Rationale</h1>
<p>Recently I had to port in R some Matlab code using the <code>tf</code> and <code>bode</code> functions, which are respectively used to calculate the transfer function and create a Bode plot of it.</p>
<p>The R package <code>control</code> luckily provides the analogous functions, although its <code>control::bodeplot</code> function uses the base <code>plot</code> interface. Of course, I wanted to make an analogous plot with GGplot2 tools.</p>
</section>
<section id="example-vibration-isolation" class="level1">
<h1>Example: vibration isolation</h1>
<p>Let us use the example for a lumped parameters model of a vibration isolation system, that is, a 1-DoF mass-spring-damper system. Briefly, its transfer function can be created as:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">M <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb1-2">K <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb1-3">C <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb1-4"></span>
<span id="cb1-5">(H <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tf</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(C<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>K, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(M<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>K, C<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>K, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
y1:
        0.05 s^1 + 1 
  - - - - - - - - - - - - -
    0.01 s^2 + 0.05 s + 1 


Transfer Function: Continuous time model </code></pre>
</div>
</div>
<p>Its Bode representation can be obtained by:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bode</span>(H) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>List of 3
 $ w    : num [1:10000] 0 0.01 0.02 0.03 0.04 ...
 $ mag  : num [1:10000] 0.00 8.69e-06 3.48e-05 7.82e-05 1.39e-04 ...
 $ phase: num [1:10000] 0.00 -2.87e-08 -2.29e-07 -7.74e-07 -1.83e-06 ...</code></pre>
</div>
</div>
<p>That is, a list of three vectors reporting frequency (<code>w</code>), magnitude in dB (<code>mag</code>), and phase in degrees (<code>phase</code>). Why the <code>control</code> developers decided to return a list of equally sized vectors rather a data frame is beyond me, but let’s deal with what we have.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bode</span>(H, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> {</span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb5-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency (rad/s)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>w, </span>
<span id="cb5-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">magnitude (dB)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mag,</span>
<span id="cb5-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">phase (deg)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>phase</span>
<span id="cb5-6">  )</span>
<span id="cb5-7">} <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-9">  knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">frequency (rad/s)</th>
<th style="text-align: right;">magnitude (dB)</th>
<th style="text-align: right;">phase (deg)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1</td>
<td style="text-align: right;">0.0870762</td>
<td style="text-align: right;">-0.0288644</td>
</tr>
<tr class="even">
<td style="text-align: right;">2</td>
<td style="text-align: right;">0.3509189</td>
<td style="text-align: right;">-0.2362699</td>
</tr>
<tr class="odd">
<td style="text-align: right;">3</td>
<td style="text-align: right;">0.7993794</td>
<td style="text-align: right;">-0.8294252</td>
</tr>
<tr class="even">
<td style="text-align: right;">4</td>
<td style="text-align: right;">1.4452744</td>
<td style="text-align: right;">-2.0825653</td>
</tr>
<tr class="odd">
<td style="text-align: right;">5</td>
<td style="text-align: right;">2.3044892</td>
<td style="text-align: right;">-4.3987054</td>
</tr>
<tr class="even">
<td style="text-align: right;">6</td>
<td style="text-align: right;">3.3880407</td>
<td style="text-align: right;">-8.4155907</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>To make the Bode plot, which reports magnitude vs.&nbsp;frequency on top of phase vs.&nbsp;frequency, we make the tibble tidy and use <code>facet_wrap</code>:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bode</span>(H, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> {</span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb6-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency (rad/s)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>w, </span>
<span id="cb6-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">magnitude (dB)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mag,</span>
<span id="cb6-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">phase (deg)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>phase</span>
<span id="cb6-6">  )</span>
<span id="cb6-7">} <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency (rad/s)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency (rad/s)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</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://rtug.unitn.it/posts/003-logscale/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:16cm"></p>
</figure>
</div>
</div>
</div>
<p>Note the followings:</p>
<ol type="1">
<li>in <code>facet_wrap</code>, we use the option <code>scales="free"</code>: this allows to independently rescale the axes of each facet;</li>
<li>the horizontal axis is logarithmic, but there is only one secondary grid line, while we usually have secondary gridlines at 2, 3, …, 9;</li>
<li>the point density is not evenly spaced on the logarithmic axis.</li>
</ol>
<p>So, here we want to tackle the problems in 2. and 3..</p>
</section>
<section id="log-tickmarks-solution" class="level1">
<h1>Log-tickmarks: Solution</h1>
<p>The <code>scale_x_log10()</code> function allows to specify the <code>breaks</code> and the <code>minor_breaks</code>, which we can exploit to fix the gridlines. How can we get a log-spaced sequence? an elegant solution uses the <strong>outer product</strong> <code>%o%</code> of two vectors: that of the ticks, and that of the orders of magnitude (<code>ooms</code>):</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">ticks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span></span>
<span id="cb7-2">ooms <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb7-3"></span>
<span id="cb7-4">ticks <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%o%</span> ooms</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>     [,1] [,2] [,3] [,4]  [,5]
[1,]    2   20  200 2000 20000
[2,]    3   30  300 3000 30000
[3,]    4   40  400 4000 40000
[4,]    5   50  500 5000 50000
[5,]    6   60  600 6000 60000
[6,]    7   70  700 7000 70000
[7,]    8   80  800 8000 80000
[8,]    9   90  900 9000 90000</code></pre>
</div>
</div>
<p>Looking at the columns , in sequence, we have what we want, so:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">(breaks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.vector</span>(ticks <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%o%</span> ooms))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1]     2     3     4     5     6     7     8     9    20    30    40    50
[13]    60    70    80    90   200   300   400   500   600   700   800   900
[25]  2000  3000  4000  5000  6000  7000  8000  9000 20000 30000 40000 50000
[37] 60000 70000 80000 90000</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bode</span>(H, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> {</span>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb11-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency (rad/s)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>w, </span>
<span id="cb11-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">magnitude (dB)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mag,</span>
<span id="cb11-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">phase (deg)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>phase</span>
<span id="cb11-6">  )</span>
<span id="cb11-7">} <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency (rad/s)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb11-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency (rad/s)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">minor_breaks=</span>breaks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels=</span>scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>scientific)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/003-logscale/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:16cm"></p>
</figure>
</div>
</div>
</div>
<p>Bingo!</p>
<p>Now, we still have too sparse points to the left, and <strong>definitely too many points to the right</strong>. We can use a similar approach to define the frequency vector.</p>
</section>
<section id="log-spaced-points-solution" class="level1">
<h1>Log-spaced points: Solution</h1>
<p>We use the same outer product trick, multiplying the vector of orders of magnitude by a vector of positions, <strong>exponentially spaced</strong> within each magnitude:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">pts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tail</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb12-2">(freqs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.vector</span>(pts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%o%</span> ooms)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1]   1.258925   1.584893   1.995262   2.511886   3.162278   3.981072
 [7]   5.011872   6.309573   7.943282  10.000000  12.589254  15.848932
[13]  19.952623  25.118864  31.622777  39.810717  50.118723  63.095734
[19]  79.432823 100.000000</code></pre>
</div>
</div>
<p>Finally:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">pts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tail</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb14-2">freqs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.vector</span>(pts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%o%</span> ooms)</span>
<span id="cb14-3"></span>
<span id="cb14-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bode</span>(H, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w=</span>freqs) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> {</span>
<span id="cb14-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb14-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency (rad/s)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>w, </span>
<span id="cb14-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">magnitude (dB)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mag,</span>
<span id="cb14-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">phase (deg)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>phase</span>
<span id="cb14-9">  )</span>
<span id="cb14-10">} <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency (rad/s)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency (rad/s)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">minor_breaks=</span>breaks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels=</span>scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>scientific)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/003-logscale/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:16cm"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="putting-all-together" class="level1">
<h1>Putting all together</h1>
<p>We can then put everything together and make a useful function (note that we are now converting the frequencies to Hz):</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">ggbodeplot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(tf, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fmin=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fmax=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>) {</span>
<span id="cb15-2">  ticks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span></span>
<span id="cb15-3">  pts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, df) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tail</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb15-4">  ooms <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(fmin))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ceiling</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(fmax)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb15-5">  breaks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.vector</span>(ticks <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%o%</span> ooms)</span>
<span id="cb15-6">  freqs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.vector</span>(pts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%o%</span> ooms)</span>
<span id="cb15-7">  </span>
<span id="cb15-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bode</span>(tf, freqs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> {</span>
<span id="cb15-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">f=</span>.<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>w<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">magnitude (dB)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>.<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mag, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">phase (deg)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>.<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>phase)} <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb15-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>f) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb15-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>f, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">minor_breaks=</span>breaks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels=</span>scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>scientific) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frequency (Hz)"</span>)</span>
<span id="cb15-16">}</span>
<span id="cb15-17"></span>
<span id="cb15-18">H <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggbodeplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fmin=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fmax=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</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://rtug.unitn.it/posts/003-logscale/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:16cm"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="one-last-thing" class="level1">
<h1>One last thing…</h1>
<p>Well, the above is mostly of academic interest, at least for what pertains the logarithmic grid lines: it shows a nice and useful way for creating regularly spaced vectors, which is really useful to evenly distribute abscissa values when the axis scale is not linear. But thanks to the <code>scales</code> package there is a quick way for having <strong>any axis, whichever the scale</strong>, with a number of minor breaks different to 1 (the defaiult in GGplot). In fact, we can just use the <code>scales::minor_breaks_n()</code> function to generate minor grid lines at will:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">ggbodeplot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(tf, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fmin=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fmax=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>) {</span>
<span id="cb16-2">  pts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, df) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tail</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb16-3">  ooms <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(fmin))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ceiling</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(fmax)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb16-4">  freqs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.vector</span>(pts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%o%</span> ooms)</span>
<span id="cb16-5">  </span>
<span id="cb16-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bode</span>(tf, freqs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> {</span>
<span id="cb16-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">f=</span>.<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>w<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">magnitude (dB)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>.<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mag, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">phase (deg)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>.<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>phase)} <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb16-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>f) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb16-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>f, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</span>(</span>
<span id="cb16-12">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">minor_breaks=</span>scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">minor_breaks_n</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), </span>
<span id="cb16-13">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> latex2exp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">TeX</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"$10^{"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(.), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"}$"</span>))</span>
<span id="cb16-14">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frequency (Hz)"</span>)</span>
<span id="cb16-17">}</span>
<span id="cb16-18"></span>
<span id="cb16-19">H <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggbodeplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fmin=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fmax=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</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://rtug.unitn.it/posts/003-logscale/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:16cm"></p>
</figure>
</div>
</div>
</div>
<p>where the trick is to set <code>scale_x_log10(minor_breaks=scales::minor_breaks_n(10))</code>. Note that the argument is the <strong>number of intervals</strong> rather than the number of grid lines (so, 10 rather than 9). As a final suggestion, try and use the same command with a <code>scale_x_continuous</code>: it works whichever is the axis transformation (including identity). Also, note the <code>labels</code> lambda function used for formatting tick labels.</p>
<div class="thatsall">
<p>That’s all, folks!</p>
</div>


</section>

 ]]></description>
  <category>post</category>
  <category>R</category>
  <category>tidyverse</category>
  <category>GGPlot2</category>
  <category>signal analysis</category>
  <guid>https://rtug.unitn.it/posts/003-logscale/</guid>
  <pubDate>Sun, 09 Feb 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rtug.unitn.it/posts/003-logscale/steps.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>New Logo</title>
  <dc:creator>Paolo Bosetti</dc:creator>
  <link>https://rtug.unitn.it/posts/002-logo/</link>
  <description><![CDATA[ 




<section id="we-have-a-new-group-logo" class="level1 page-columns page-full">
<h1>We have a new group logo</h1>

<div class="no-row-height column-margin column-container"><div class="">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rtug.unitn.it/images/logo.png" class="img-fluid figure-img"></p>
<figcaption>RTUG logo</figcaption>
</figure>
</div>
</div></div><p>The new RTUG logo is a hexagon, on the style of Tidyverse packages, it takes the colors of Trentino Province lettering, and of course it incorporates the GNU-R language logo.</p>


</section>

 ]]></description>
  <category>RTUG</category>
  <category>website</category>
  <category>announcement</category>
  <guid>https://rtug.unitn.it/posts/002-logo/</guid>
  <pubDate>Thu, 06 Feb 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rtug.unitn.it/images/logo.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>First RTUG meeting</title>
  <dc:creator>Paolo Bosetti</dc:creator>
  <link>https://rtug.unitn.it/meetings/25.1-meeting.html</link>
  <description><![CDATA[ 




<p>The first R-Trento Users Meeting, code <code>RTUG::25.1</code>, is planned for <strong>Friday, May 30, 2025</strong> at 14:30 in the <em>Aula Rossa</em> of the Department of Economics and Management, via Inama 5, Trento.</p>
<p><strong>Participation is free</strong> but you need to subscribe to the event via <a href="https://www.meetup.com/r-trento-users-group/events/306973694/?eventOrigin=group_upcoming_events"><img src="https://help.meetup.com/hc/theming_assets/01HZH3ZAT7AR9TR620NM18CDKN.png" style="height:2em"></a>.</p>
<p><strong>Certificates of participation</strong> will be available upon request</p>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c2062692070617463682d7175657374696f6e203e7d7d-topics" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c2062692070617463682d7175657374696f6e203e7d7d-topics"> Topics</h2>

<div class="no-row-height column-margin column-container"><div class="">
<div class="callout callout-style-default callout-note callout-titled" title="Event flyer">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Event flyer
</div>
</div>
<div class="callout-body-container callout-body">
<p>Download <a href="../images/flyer.pdf" target="_blank">here </a> the event flyer.</p>
</div>
</div>
</div></div><ol type="1">
<li>Welcome and Introduction to the R-Trento Users Group (<em>P. Bosetti</em>)</li>
<li>The R Package <code>rmf</code> in Teaching (<em>R. Micciolo and G. Espa</em>)</li>
<li>The <code>adas.utils</code> Package: Design of Experiments, the Tidy Way (<em>P. Bosetti</em>)</li>
<li>Bioinformatics and R: Visualizing Genomic Data (<em>Y. Ciani</em>)</li>
<li>Spatial Sampling in R (<em>M. M. Dickson</em>)</li>
<li>Refreshments and networking!</li>
</ol>
<p>See below for details on the topics. The <a href="../slides.html">slides</a> page lists all the available presentations.</p>
</section>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c206269206275696c64696e67203e7d7d-venue" class="level2">
<h2 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c206269206275696c64696e67203e7d7d-venue"> Venue</h2>
<p><em>Aula Rossa</em> of the Department of Economics and Management, via Inama 5, Trento.</p>
</section>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c206269207a6f6f6d2d696e203e7d7d-details-on-topics" class="level2">
<h2 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c206269207a6f6f6d2d696e203e7d7d-details-on-topics"> Details on Topics</h2>
<section id="welcome" class="level3">
<h3 class="anchored" data-anchor-id="welcome">Welcome</h3>
<ul>
<li>Author: <strong>P. Bosetti</strong>, University of Trento</li>
<li>Style: <a href="../slides/rtug-25.1.html">Presentation</a></li>
</ul>
</section>
<section id="the-r-package-rmf-in-teaching" class="level3">
<h3 class="anchored" data-anchor-id="the-r-package-rmf-in-teaching">The R Package <code>rmf</code> in Teaching</h3>
<ul>
<li>Authors: <strong>R. Micciolo and G. Espa</strong>, University or Trento</li>
<li>Style: <a href="../slides/micciolo-espa-25.1.pdf">Presentation</a></li>
</ul>
<p>We introduce the <code>rmf</code> package, which contains a series of functions developed primarily for use in educational settings, particularly in basic university statistics courses.</p>
</section>
<section id="the-adas.utils-package-design-of-experiments-the-tidy-way" class="level3">
<h3 class="anchored" data-anchor-id="the-adas.utils-package-design-of-experiments-the-tidy-way">The <code>adas.utils</code> Package: Design of Experiments, the Tidy Way</h3>
<ul>
<li>Author: <strong>P. Bosetti</strong>, University or Trento</li>
<li>Style: <a href="../slides/bosetti-25.1">Presentation</a></li>
</ul>
<p>We introduce the <code>adas.utils</code> package, which provides a set of tools for designing factorial designs and analyzing results, in line with the Tidyverse package approach.</p>
</section>
<section id="bioinformatics-and-r-visualizing-genomic-data" class="level3">
<h3 class="anchored" data-anchor-id="bioinformatics-and-r-visualizing-genomic-data">Bioinformatics and R: Visualizing Genomic Data</h3>
<ul>
<li>Author: <strong>Y. Ciani</strong>, University or Trento</li>
<li>Style: <a href="../slides/ciani-25.1.pdf">Presentation</a></li>
</ul>
<p>An overview of the use of R in bioinformatics, particularly its use for visualizing genomic data.</p>
</section>
<section id="spatial-sampling-in-r" class="level3">
<h3 class="anchored" data-anchor-id="spatial-sampling-in-r">Spatial Sampling in R</h3>
<ul>
<li>Author: <strong>M. M. Dickson</strong>, University of Padua</li>
<li>Style: <a href="../slides/dickson-25.1.pdf">Presentation</a></li>
</ul>
<p>We introduce the <code>BalancedSampling</code> package, which allows the selection of balanced and spatially balanced samples, widely used in the environmental/forestry field, with recent diffusion also in the economic-social field.</p>
</section>
</section>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c2062692063616d657261203e7d7d-post-mortem" class="level2">
<h2 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c2062692063616d657261203e7d7d-post-mortem"> Post Mortem 😜</h2>
<p>So <code>RTUG::25.1</code> is over, and it was a great success! We had a good turnout, with more than 20 participants, and the feedback was very positive. The presentations were well received, and the discussions were lively.</p>
<p>We would like to thank all the speakers for their contributions, and all the participants for their interest and enthusiasm. We hope to see you again at the next RTUG meeting, which will be held in the fall of 2025.</p>
<p><a href="images/rtug-25.1-01.jpeg" class="lightbox" data-gallery="post_mortem"><img src="https://rtug.unitn.it/meetings/images/rtug-25.1-01.jpeg" class="img-fluid"></a></p>
<p><a href="images/rtug-25.1-02.jpeg" class="lightbox" data-gallery="post_mortem"><img src="https://rtug.unitn.it/meetings/images/rtug-25.1-02.jpeg" class="img-fluid"></a></p>


</section>

 ]]></description>
  <guid>https://rtug.unitn.it/meetings/25.1-meeting.html</guid>
  <pubDate>Wed, 05 Feb 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Opening!</title>
  <dc:creator>Paolo Bosetti</dc:creator>
  <link>https://rtug.unitn.it/posts/001-opening/001-opening.html</link>
  <description><![CDATA[ 




<section id="the-users-group-is-instantiated" class="level1 page-columns page-full">
<h1>The Users Group is instantiated</h1>

<div class="no-row-height column-margin column-container"><div class="">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rtug.unitn.it/posts/001-opening/milestone.jpg" class="img-fluid figure-img"></p>
<figcaption>An important milestone</figcaption>
</figure>
</div>
</div></div><p>On Feb.&nbsp;5th, 2025, Giuseppe <strong>Espa</strong>, Maria Michela <strong>Dickson</strong>, Flavio <strong>Santi</strong>, Diego <strong>Giuliani</strong> and Paolo <strong>Bosetti</strong> had the first founding meeting of the R-Trento Users Group.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">RTUG <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Giuseppe"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Maria Michela"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flavio"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Diego"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paolo"</span>)</span></code></pre></div></div>
</div>
<p>That’s a milestone.</p>


</section>

 ]]></description>
  <category>RTUG</category>
  <category>milestone</category>
  <category>announcement</category>
  <guid>https://rtug.unitn.it/posts/001-opening/001-opening.html</guid>
  <pubDate>Wed, 05 Feb 2025 00:00:00 GMT</pubDate>
  <media:content url="https://rtug.unitn.it/posts/001-opening/milestone.jpg" medium="image" type="image/jpeg"/>
</item>
</channel>
</rss>
