<?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>Andrew Gard, Ph.D</title>
<link>https://equitableequations.com/posts.html#category=R-bloggers</link>
<atom:link href="https://equitableequations.com/posts-r-bloggers.xml" rel="self" type="application/rss+xml"/>
<description>Statistics and machine learning for everyone!</description>
<image>
<url>https://equitableequations.com/assets/me_contemplative_sm.jpg</url>
<title>Andrew Gard, Ph.D</title>
<link>https://equitableequations.com/posts.html#category=R-bloggers</link>
</image>
<generator>quarto-1.7.32</generator>
<lastBuildDate>Mon, 19 Oct 2026 05:00:00 GMT</lastBuildDate>
<item>
  <title>Lasso regression using tidymodels</title>
  <dc:creator>Andrew Gard</dc:creator>
  <link>https://equitableequations.com/posts/2026-10-19/</link>
  <description><![CDATA[ 




<p><strong>The lasso</strong> addresses instability in linear regression models by penalizing larger coefficient values. For a conceptual introduction, check out my explainer <a href="https://youtu.be/hBqEECNP5mQ">here</a>.</p>
<p>In this post, we’ll obtain a lasso model for <code>solubility</code> in chemical compounds using data from the <code>AppliedPredictiveModeling</code> package, which supports the excellent book of the same name. We’ll do this from the machine learning perspective using the <code>tidymodels</code> framework. If you haven’t worked with <code>tidymodels</code> before, I recommend pausing to check out my tutorial <a href="https://youtu.be/fRj8dr-aSsU">here</a>.</p>
<p>The <code>solubility</code> data has already been split into training and testing sets, but in each case the explanatory and response variables have been stored separately. We start by resolving this.</p>
<div class="cell" data-layout-align="center">
<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>(tidymodels)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(AppliedPredictiveModeling)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(solubility)</span>
<span id="cb1-5"></span>
<span id="cb1-6"><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;">0</span>)</span>
<span id="cb1-7"></span>
<span id="cb1-8">sol_train <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> solTrainXtrans <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-9">  <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;">solubility =</span> solTrainY)</span>
<span id="cb1-10">sol_test <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> solTestXtrans <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-11">  <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;">solubility =</span> solTestY)</span></code></pre></div>
</div>
<p>The training set includes 951 observations while the test test includes 316. There are 228 total predictors available as we attempt to model the response variable, <code>solubility</code>.</p>
<p>Penalized regression in R is typically carried out using the <code>glmnet()</code> engine. Since we’re using <code>tidymodels</code>, we’ll rely on the <code>linear_reg()</code> wrapper, which standardizes the syntax of modeling functions like <code>glmnnet()</code> and ensures their output is compatible with the tools we’ll use for tuning and evaluation.</p>
<div class="cell" data-layout-align="center">
<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">my_lasso <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">linear_reg</span>(</span>
<span id="cb2-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">engine =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"glmnet"</span>,</span>
<span id="cb2-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mixture =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># one for lasso, zero for ridge</span></span>
<span id="cb2-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">penalty =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tune</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># lambda </span><span class="al" style="color: #AD0000;
background-color: null;
font-style: inherit;">TBD</span></span>
<span id="cb2-5">  ) </span></code></pre></div>
</div>
<p>Since there are no missing values in these sets and the variables are all numeric, little preprocessing needs to be done with this data. The following recipe (which standardizes all predictors and removes any with zero-variance) is added for consistency and clarity.</p>
<div class="cell" data-layout-align="center">
<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">my_rec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">recipe</span>(solubility <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sol_train) <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;">step_zv</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_predictors</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;">step_normalize</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_numeric_predictors</span>())</span>
<span id="cb3-4"></span>
<span id="cb3-5">my_wf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">workflow</span>(</span>
<span id="cb3-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">spec =</span> my_lasso,</span>
<span id="cb3-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">preprocessor =</span> my_rec</span>
<span id="cb3-8">  )</span></code></pre></div>
</div>
<p>Since the <code>glmnet()</code> engine performs standardization by default, <code>step_normalize()</code> turns out to be redundant here.</p>
<p>As a reminder, <code>tidymodels</code> separates model specification (accomplished here with <code>linear_reg()</code>) from model fitting (which we’ll do later). This allows us to incorporate preprocessing in a programmatic way, via a <code>recipe</code>, ensuring our steps will be applied consistently whether we’re working with the training set, the testing set, or something else entirely.</p>
<p>We determine the best choice for the penalty, <img src="https://latex.codecogs.com/png.latex?%5Clambda">, using cross validation.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create 5 separate 10-fold splits</span></span>
<span id="cb4-2">folds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vfold_cv</span>(</span>
<span id="cb4-3">  sol_train,</span>
<span id="cb4-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb4-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repeats =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb4-6">  ) </span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># tune using a default grid of values</span></span>
<span id="cb4-9">tune_results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tune_grid</span>(</span>
<span id="cb4-10">  my_wf, </span>
<span id="cb4-11">  folds</span>
<span id="cb4-12">  ) </span>
<span id="cb4-13"></span>
<span id="cb4-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(tune_results)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 50
Columns: 5
$ splits   &lt;list&gt; [&lt;vfold_split[855 x 96 x 951 x 229]&gt;], [&lt;vfold_split[856 x 9…
$ id       &lt;chr&gt; "Repeat1", "Repeat1", "Repeat1", "Repeat1", "Repeat1", "Repea…
$ id2      &lt;chr&gt; "Fold01", "Fold02", "Fold03", "Fold04", "Fold05", "Fold06", "…
$ .metrics &lt;list&gt; [&lt;tbl_df[20 x 5]&gt;], [&lt;tbl_df[20 x 5]&gt;], [&lt;tbl_df[20 x 5]&gt;], …
$ .notes   &lt;list&gt; [&lt;tbl_df[0 x 4]&gt;], [&lt;tbl_df[0 x 4]&gt;], [&lt;tbl_df[0 x 4]&gt;], [&lt;t…</code></pre>
</div>
</div>
<p>The output of <code>tune_grid()</code> is a copy of the object created by <code>vfold_cv()</code>, that is, a tibble with one row per fold per repeat. This one, however, includes an extra column, <code>.metrics</code>, which tracks the performance of models built with different <img src="https://latex.codecogs.com/png.latex?%5Clambda"> values. Each element of <code>tune_results$.metrics</code> is itself a tibble.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for example:</span></span>
<span id="cb6-2">tune_results<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.metrics[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 20 × 5
    penalty .metric .estimator .estimate .config         
      &lt;dbl&gt; &lt;chr&gt;   &lt;chr&gt;          &lt;dbl&gt; &lt;chr&gt;           
 1 1.62e-10 rmse    standard       0.698 pre0_mod01_post0
 2 1.62e-10 rsq     standard       0.891 pre0_mod01_post0
 3 3.51e- 9 rmse    standard       0.698 pre0_mod02_post0
 4 3.51e- 9 rsq     standard       0.891 pre0_mod02_post0
 5 7.19e- 8 rmse    standard       0.698 pre0_mod03_post0
 6 7.19e- 8 rsq     standard       0.891 pre0_mod03_post0
 7 1.19e- 6 rmse    standard       0.698 pre0_mod04_post0
 8 1.19e- 6 rsq     standard       0.891 pre0_mod04_post0
 9 1.45e- 5 rmse    standard       0.698 pre0_mod05_post0
10 1.45e- 5 rsq     standard       0.891 pre0_mod05_post0
11 1.92e- 4 rmse    standard       0.697 pre0_mod06_post0
12 1.92e- 4 rsq     standard       0.891 pre0_mod06_post0
13 2.08e- 3 rmse    standard       0.659 pre0_mod07_post0
14 2.08e- 3 rsq     standard       0.902 pre0_mod07_post0
15 1.83e- 2 rmse    standard       0.633 pre0_mod08_post0
16 1.83e- 2 rsq     standard       0.909 pre0_mod08_post0
17 1.23e- 1 rmse    standard       0.801 pre0_mod09_post0
18 1.23e- 1 rsq     standard       0.866 pre0_mod09_post0
19 8.15e- 1 rmse    standard       1.77  pre0_mod10_post0
20 8.15e- 1 rsq     standard       0.501 pre0_mod10_post0</code></pre>
</div>
</div>
<p>We can now plot performance versus penalty to visually estimate the best value of <img src="https://latex.codecogs.com/png.latex?%5Clambda">.</p>
<div class="cell" data-layout-align="center">
<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">rmse_path <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect_metrics</span>(tune_results) <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;">filter</span>(.metric <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;">"rmse"</span>)</span>
<span id="cb8-3"></span>
<span id="cb8-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(rmse_path, <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> penalty, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mean)) <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_point</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;">geom_line</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label_log</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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;">"Penalty"</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;">"CV error"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb8-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(</span>
<span id="cb8-12">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E1E1D3"</span>,</span>
<span id="cb8-13">      <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;">"#595959"</span></span>
<span id="cb8-14">      )</span>
<span id="cb8-15">    )</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://equitableequations.com/posts/2026-10-19/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="480"></p>
</figure>
</div>
</div>
</div>
<p>We can zoom in on the apparent minimum by specifying a data frame of penalty values for <code>tune_grid()</code> to work with. Since the search grid for <img src="https://latex.codecogs.com/png.latex?%5Clambda"> must span several orders of magnitude, it’s best to specify it using a logarithmic scale, meaning that adjacent values will differ by the same <em>factor</em> rather than the same <em>constant</em>.</p>
<p>The grid can also be built using functions from the <code>dials</code> package, but for me this is usually more trouble than it’s worth.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># specify values </span></span>
<span id="cb9-2">my_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb9-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">penalty =</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb9-4">  )</span>
<span id="cb9-5"></span>
<span id="cb9-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># repeat tuning and plotting</span></span>
<span id="cb9-7">tune_results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tune_grid</span>(</span>
<span id="cb9-8">  my_wf, </span>
<span id="cb9-9">  folds, </span>
<span id="cb9-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grid =</span> my_grid</span>
<span id="cb9-11">  )</span>
<span id="cb9-12"></span>
<span id="cb9-13">rmse_path <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect_metrics</span>(tune_results) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb9-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(.metric <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;">"rmse"</span>)</span>
<span id="cb9-15"></span>
<span id="cb9-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(rmse_path, <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> penalty, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mean)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb9-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb9-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label_log</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-20">  <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;">"Penalty"</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;">"Test error"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb9-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(</span>
<span id="cb9-25">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E1E1D3"</span>,</span>
<span id="cb9-26">      <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;">"#595959"</span></span>
<span id="cb9-27">      )</span>
<span id="cb9-28">    )</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://equitableequations.com/posts/2026-10-19/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="480"></p>
</figure>
</div>
</div>
</div>
<p>Use <code>show_best()</code> to obtain a data frame of the best-performing hyperparameter values. Here we use root mean squared error to measure “best.”</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">show_best</span>(tune_results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rmse"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 7
  penalty .metric .estimator  mean     n std_err .config         
    &lt;dbl&gt; &lt;chr&gt;   &lt;chr&gt;      &lt;dbl&gt; &lt;int&gt;   &lt;dbl&gt; &lt;chr&gt;           
1 0.00546 rmse    standard   0.679    50 0.00708 pre0_mod12_post0
2 0.00379 rmse    standard   0.680    50 0.00714 pre0_mod11_post0
3 0.00785 rmse    standard   0.681    50 0.00690 pre0_mod13_post0
4 0.0113  rmse    standard   0.684    50 0.00677 pre0_mod14_post0
5 0.00264 rmse    standard   0.685    50 0.00731 pre0_mod10_post0</code></pre>
</div>
</div>
<p>Once we’re satisfied that we’ve found a good hyperparameter value, we can insert it into our model with <code>finalize_workflow()</code>.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># manual specification (less common)</span></span>
<span id="cb12-2">my_wf_final <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> my_wf <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;">finalize_workflow</span>(</span>
<span id="cb12-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameters =</span> <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;">penalty =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">00546</span>)</span>
<span id="cb12-4">  ) </span>
<span id="cb12-5"></span>
<span id="cb12-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># automated specification (more common)</span></span>
<span id="cb12-7">best_lambda <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select_best</span>(tune_results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rmse"</span>)</span>
<span id="cb12-8">my_wf_final <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> my_wf <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;">finalize_workflow</span>(best_lambda)</span>
<span id="cb12-9">my_wf_final</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>══ Workflow ════════════════════════════════════════════════════════════════════
Preprocessor: Recipe
Model: linear_reg()

── Preprocessor ────────────────────────────────────────────────────────────────
2 Recipe Steps

• step_zv()
• step_normalize()

── Model ───────────────────────────────────────────────────────────────────────
Linear Regression Model Specification (regression)

Main Arguments:
  penalty = 0.00545559478116851
  mixture = 1

Computational engine: glmnet </code></pre>
</div>
</div>
<p>A workflow is a set of instructions for building a model from data, not the model itself. To incorporate the training data and create an algorithm for making predictions, use <code>fit()</code>. To obtain model coefficients in a tibble, use <code>broom::tidy()</code>.</p>
<div class="cell" data-layout-align="center">
<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">my_wf_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> my_wf_final <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;">fit</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sol_train)</span>
<span id="cb14-3"></span>
<span id="cb14-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy</span>(my_wf_fit)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 229 × 3
   term        estimate penalty
   &lt;chr&gt;          &lt;dbl&gt;   &lt;dbl&gt;
 1 (Intercept)  -2.72   0.00546
 2 FP001         0      0.00546
 3 FP002         0.110  0.00546
 4 FP003        -0.0154 0.00546
 5 FP004        -0.106  0.00546
 6 FP005         0      0.00546
 7 FP006        -0.0341 0.00546
 8 FP007         0      0.00546
 9 FP008         0      0.00546
10 FP009         0      0.00546
# ℹ 219 more rows</code></pre>
</div>
</div>
<p>While <code>predict()</code> works perfectly well for applying a fitted model to new data, I often prefer another <code>broom</code> function: <code>augment()</code>. This returns a data frame that includes all relevant explanatory variables as well as the predicted values.</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># predictions only</span></span>
<span id="cb16-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(my_wf_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</span> sol_test) </span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 316 × 1
     .pred
     &lt;dbl&gt;
 1  0.599 
 2  0.168 
 3 -0.459 
 4  0.705 
 5 -0.0386
 6  1.47  
 7  0.532 
 8  0.409 
 9  0.477 
10 -0.555 
# ℹ 306 more rows</code></pre>
</div>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add predictions to df</span></span>
<span id="cb18-2">sol_test_aug <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">augment</span>(my_wf_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</span> sol_test) </span>
<span id="cb18-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(sol_test_aug)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 230
    .pred FP001 FP002 FP003 FP004 FP005 FP006 FP007 FP008 FP009 FP010 FP011
    &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
1  0.599      1     0     0     1     0     0     0     0     1     1     0
2  0.168      1     0     1     1     0     1     0     0     0     0     1
3 -0.459      0     1     0     0     1     0     0     0     0     1     0
4  0.705      0     0     1     0     0     1     0     0     0     0     0
5 -0.0386     1     1     0     1     1     1     0     1     0     0     1
6  1.47       1     0     0     1     0     0     0     0     0     0     0
# ℹ 218 more variables: FP012 &lt;int&gt;, FP013 &lt;int&gt;, FP014 &lt;int&gt;, FP015 &lt;int&gt;,
#   FP016 &lt;int&gt;, FP017 &lt;int&gt;, FP018 &lt;int&gt;, FP019 &lt;int&gt;, FP020 &lt;int&gt;,
#   FP021 &lt;int&gt;, FP022 &lt;int&gt;, FP023 &lt;int&gt;, FP024 &lt;int&gt;, FP025 &lt;int&gt;,
#   FP026 &lt;int&gt;, FP027 &lt;int&gt;, FP028 &lt;int&gt;, FP029 &lt;int&gt;, FP030 &lt;int&gt;,
#   FP031 &lt;int&gt;, FP032 &lt;int&gt;, FP033 &lt;int&gt;, FP034 &lt;int&gt;, FP035 &lt;int&gt;,
#   FP036 &lt;int&gt;, FP037 &lt;int&gt;, FP038 &lt;int&gt;, FP039 &lt;int&gt;, FP040 &lt;int&gt;,
#   FP041 &lt;int&gt;, FP042 &lt;int&gt;, FP043 &lt;int&gt;, FP044 &lt;int&gt;, FP045 &lt;int&gt;, …</code></pre>
</div>
</div>
<p>This augmented data frame is particularly convenient when applying assessment functions like <code>yardstick::rmse()</code></p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rmse</span>(sol_test_aug,</span>
<span id="cb20-2">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> solubility,</span>
<span id="cb20-3">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> .pred)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 3
  .metric .estimator .estimate
  &lt;chr&gt;   &lt;chr&gt;          &lt;dbl&gt;
1 rmse    standard       0.704</code></pre>
</div>
</div>
<p>On average, predictions made using this model miss true <code>solubility</code> by about .7 units.</p>
<section id="bonus-1-unpacking-the-recipe" class="level2">
<h2 class="anchored" data-anchor-id="bonus-1-unpacking-the-recipe">Bonus #1: unpacking the <code>recipe</code></h2>
<p>A major advantage to <code>tidymodels</code> is the ease with which we can incorporate preprocessing steps like variable standardization. In the above analysis, for instance, we said which steps we wanted to carry out and then forgot all about them, never dealing with <img src="https://latex.codecogs.com/png.latex?z">-scores directly at all. In this section, we extract those transformed variables.</p>
<p>A <code>recipe</code> is a set of instructions for preprocessing data. On its own, it doesn’t take into account the training data or perform any transformations. To accomplish the former, use <code>prep()</code>.</p>
<div class="cell" data-layout-align="center">
<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">rec_prepped <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prep</span>(my_rec)</span>
<span id="cb22-2">rec_prepped</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code></code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── Recipe ──────────────────────────────────────────────────────────────────────</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code></code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── Inputs </code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Number of variables by role</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>outcome:     1
predictor: 228</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code></code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── Training information </code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Training data contained 951 data points and no incomplete rows.</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code></code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── Operations </code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>• Zero variance filter removed: &lt;none&gt; | Trained</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>• Centering and scaling for: FP001, FP002, FP003, FP004, ... | Trained</code></pre>
</div>
</div>
<p>We now hove a <em>trained</em> recipe. While previously we only had a generic instruction to compute <img src="https://latex.codecogs.com/png.latex?z">-scores using the formula <img src="https://latex.codecogs.com/png.latex?z_%7Bij%7D%20=%20%5Cfrac%7Bx_%7Bij%7D%20-%20%5Coverline%7Bx%7D_j%7D%7Bs_j%7D,"></p>
<p>now sample means and standard deviations (<img src="https://latex.codecogs.com/png.latex?%5Coverline%7Bx%7D_j"> and <img src="https://latex.codecogs.com/png.latex?s_j">) from the training set have been computed and incorporated into those instructions. This fact is indicated by the word “trained” at the end of the <code>rec_prepped</code> printout.</p>
<p>To obtain a tibble of transformed predictors, apply <code>bake()</code> to a trained recipe.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bake</span>(rec_prepped, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</span> sol_train) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb36-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(</span>
<span id="cb36-3">    HydrophilicFactor,</span>
<span id="cb36-4">    NumAtoms,</span>
<span id="cb36-5">    NumBonds,</span>
<span id="cb36-6">    SurfaceArea1</span>
<span id="cb36-7">    ) </span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 951 × 4
   HydrophilicFactor NumAtoms NumBonds SurfaceArea1
               &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;        &lt;dbl&gt;
 1           -1.11      0.423    0.528       0.0232
 2            0.0110    1.62     1.63        0.676 
 3            0.0652    0.771    0.717       0.341 
 4           -1.84      0.267    0.245      -1.49  
 5            0.367     0.639    0.593       0.711 
 6           -0.477     0.706    0.593      -0.157 
 7           -0.669     0.896    0.998       0.479 
 8           -1.02      1.07     0.945       0.0392
 9            0.608     1.90     1.88        1.09  
10            0.734     1.01     0.998       0.434 
# ℹ 941 more rows</code></pre>
</div>
</div>
<p>We could have just as easily applied this recipe to any other set with compatible variables, including the test set.</p>
</section>
<section id="bonus-2-plotting-coefficient-trajectories" class="level2">
<h2 class="anchored" data-anchor-id="bonus-2-plotting-coefficient-trajectories">Bonus #2: plotting coefficient trajectories</h2>
<p>It’s often useful to visualize the trajectories of model coefficients as the penalty increases. Since <code>tune_grid()</code> doesn’t retain model coefficients, some new code is needed.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># desired penalty values for plot</span></span>
<span id="cb38-2">lambda_vals <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span>
<span id="cb38-3"></span>
<span id="cb38-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># model specification. penalty=1 is a placeholder ignored by glmnet()</span></span>
<span id="cb38-5">lasso_spec2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">linear_reg</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mixture =</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;">penalty =</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;">|&gt;</span>  </span>
<span id="cb38-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_engine</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"glmnet"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path_values =</span> lambda_vals)</span>
<span id="cb38-7"></span>
<span id="cb38-8">lasso_wf2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">workflow</span>(</span>
<span id="cb38-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">spec =</span> lasso_spec2,</span>
<span id="cb38-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">preprocessor =</span> my_rec</span>
<span id="cb38-11">)</span>
<span id="cb38-12"></span>
<span id="cb38-13">lasso_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb38-14">  lasso_wf2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb38-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sol_train)</span></code></pre></div>
</div>
<p>The output of <code>fit()</code> is an updated workflow object. In order to get model coefficients for each penalty value specified in <code>lambda_vals</code>, we use <code>extract_fit_engine()</code> followed by <code>tidy()</code>.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1">glmnet_object <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lasso_fit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb39-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_fit_engine</span>()</span>
<span id="cb39-3">coef_paths <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy</span>(glmnet_object)</span>
<span id="cb39-4">coef_paths</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 130,284 × 5
   term         step estimate lambda dev.ratio
   &lt;chr&gt;       &lt;dbl&gt;    &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;
 1 (Intercept)     1    -2.72  10            0
 2 (Intercept)     2    -2.72   9.84         0
 3 (Intercept)     3    -2.72   9.68         0
 4 (Intercept)     4    -2.72   9.53         0
 5 (Intercept)     5    -2.72   9.38         0
 6 (Intercept)     6    -2.72   9.22         0
 7 (Intercept)     7    -2.72   9.08         0
 8 (Intercept)     8    -2.72   8.93         0
 9 (Intercept)     9    -2.72   8.79         0
10 (Intercept)    10    -2.72   8.65         0
# ℹ 130,274 more rows</code></pre>
</div>
</div>
<p>For neatness, let’s restrict our plot to just a few interesting variables.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1">vars <span class="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>
<span id="cb41-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NumBonds"</span>,</span>
<span id="cb41-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NumAtoms"</span>,</span>
<span id="cb41-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HydrophilicFactor"</span>,</span>
<span id="cb41-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SurfaceArea1"</span></span>
<span id="cb41-6">  )</span>
<span id="cb41-7"></span>
<span id="cb41-8">coef_paths <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(term <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> vars) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb41-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(</span>
<span id="cb41-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(</span>
<span id="cb41-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lambda, </span>
<span id="cb41-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> estimate, </span>
<span id="cb41-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> term</span>
<span id="cb41-14">    )</span>
<span id="cb41-15">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-16">  <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="cb41-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(</span>
<span id="cb41-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</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;">6</span>,</span>
<span id="cb41-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb41-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, </span>
<span id="cb41-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dotted"</span></span>
<span id="cb41-22">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb41-24">    <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;">"Penalty"</span>,</span>
<span id="cb41-25">    <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;">"Coefficient"</span>,</span>
<span id="cb41-26">    <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;">"Variable"</span></span>
<span id="cb41-27">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label_log</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(</span>
<span id="cb41-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#5798A6"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ED8C3B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8050A5"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#4C9F70"</span>)</span>
<span id="cb41-31">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb41-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>, </span>
<span id="cb41-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(</span>
<span id="cb41-36">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E1E1D3"</span>,</span>
<span id="cb41-37">      <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;">"#595959"</span></span>
<span id="cb41-38">      )</span>
<span id="cb41-39">    )</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://equitableequations.com/posts/2026-10-19/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="480"></p>
</figure>
</div>
</div>
</div>
<p>Want to watch and listen to me explain this stuff? Check out my vid:</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/eLqbK-BExFk?si=momu6jwXS8-k-Zpk" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="">
</iframe>
<p><button class="btn btn-default downloadthis  " id="dnldts54882"><i class="bi bi-download"></i> Download quarto notes </button> <button class="btn btn-default downloadthis  " id="dnldts13069"><i class="bi bi-download"></i> Download html notes </button></p>


</section>

 ]]></description>
  <category>Machine learning</category>
  <category>tidymodels</category>
  <category>R-bloggers</category>
  <guid>https://equitableequations.com/posts/2026-10-19/</guid>
  <pubDate>Mon, 19 Oct 2026 05:00:00 GMT</pubDate>
  <media:content url="https://equitableequations.com/posts/2026-10-19/lasso with R thumb.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>Working with factors in R</title>
  <dc:creator>Andrew Gard</dc:creator>
  <link>https://equitableequations.com/posts/2026-09-07/</link>
  <description><![CDATA[ 




<p>Categorical variables (or <em>factors</em> as they are referred to when working in R) are variables that assign observations to specific groups. This post will cover some of the most common problems you may encounter while trying to visualize or model such variables.</p>
<p>Throughout, I’ll refer to the <code>scooby</code> data set, originally sourced from <a href="https://www.kaggle.com/datasets/thedevastator/the-unsolved-mysteries-of-scooby-doo">kaggle</a> but modified slightly for use in my vids and in this post. A download link can be found at the end of this tutorial.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load and prep data</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>())</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readxl)</span>
<span id="cb1-7">scooby <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_excel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_internal/scooby.xlsx"</span>) </span>
<span id="cb1-8"></span>
<span id="cb1-9">scooby_sm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> scooby <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(format <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;">"TV Series"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span>  </span>
<span id="cb1-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(title,  </span>
<span id="cb1-12">         date_aired,</span>
<span id="cb1-13">         imdb,</span>
<span id="cb1-14">         motive) </span></code></pre></div>
</div>
<section id="factor-character-and-other-vector-types" class="level2">
<h2 class="anchored" data-anchor-id="factor-character-and-other-vector-types">Factor, character, and other vector types</h2>
<p>As a general rule, <code>tidyverse</code> functions treat character vectors as factors. For instance, we can apply <code>dplyr::count</code> to <code>scooby$motive</code>, even though the latter just consists of strings.</p>
<div class="cell">
<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;">glimpse</span>(scooby_sm)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 374
Columns: 4
$ title      &lt;chr&gt; "What a Night for a Knight", "A Clue for Scooby Doo", "Hass…
$ date_aired &lt;dttm&gt; 1969-09-13, 1969-09-20, 1969-09-27, 1969-10-04, 1969-10-11…
$ imdb       &lt;dbl&gt; 8.1, 8.1, 8.0, 7.8, 7.5, 8.4, 7.6, 8.2, 8.1, 8.0, 8.5, 8.2,…
$ motive     &lt;chr&gt; "Theft", "Theft", "Treasure", "Natural Resource", "Competit…</code></pre>
</div>
<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">scooby_sm <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;">count</span>(motive)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 17 × 2
   motive               n
   &lt;chr&gt;            &lt;int&gt;
 1 Abduction            3
 2 Assistance           2
 3 Competition        130
 4 Conquer             33
 5 Counterfeit          6
 6 Entertainment        3
 7 Extortion            3
 8 Imagination          1
 9 Inheritance          6
10 Natural Resource    19
11 Preservation        10
12 Safety               2
13 Simulation           2
14 Smuggling           19
15 Theft               93
16 Treasure            40
17 &lt;NA&gt;                 2</code></pre>
</div>
</div>
<p>Use <code>as.factor()</code> to explicitly coerce variables to factors. This is particularly useful when category levels are numeric, like <code>am</code> (automatic/manual transmission) in the built-in <code>mtcars</code> set, since functions like <code>ggplot</code> will otherwise attempt to treat them as continuous variables. For instance, we can’t get directly get a side-by-side boxplot of <code>mpg</code> vs <code>am</code>.</p>
<div class="cell" data-layout-align="center">
<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;">glimpse</span>(mtcars)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 32
Columns: 11
$ mpg  &lt;dbl&gt; 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,…
$ cyl  &lt;dbl&gt; 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,…
$ disp &lt;dbl&gt; 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16…
$ hp   &lt;dbl&gt; 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180…
$ drat &lt;dbl&gt; 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,…
$ wt   &lt;dbl&gt; 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.…
$ qsec &lt;dbl&gt; 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18…
$ vs   &lt;dbl&gt; 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,…
$ am   &lt;dbl&gt; 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,…
$ gear &lt;dbl&gt; 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,…
$ carb &lt;dbl&gt; 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,…</code></pre>
</div>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(mtcars, <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> am, </span>
<span id="cb8-2">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mpg)) <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_boxplot</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># not what we want</span></span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Orientation is not uniquely specified when both the x and y aesthetics are
continuous. Picking default orientation 'x'.</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Continuous x aesthetic
ℹ did you forget `aes(group = ...)`?</code></pre>
</div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://equitableequations.com/posts/2026-09-07/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:60.0%"></p>
</figure>
</div>
</div>
</div>
<p>While it’s fine to follow the recommendation given in this warning message, it may be better to just convert <code>am</code> to a factor once and for all:</p>
<div class="cell" data-layout-align="center">
<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">mtcars2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mtcars <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">am =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(am)) </span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(mtcars2, <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> am, </span>
<span id="cb11-5">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mpg)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://equitableequations.com/posts/2026-09-07/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:60.0%"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="relabeling-factors" class="level2">
<h2 class="anchored" data-anchor-id="relabeling-factors">Relabeling factors</h2>
<p>There are many options for changing the text used to label factor levels, but <code>fct_recode()</code> is a solid choice.</p>
<div class="cell">
<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">mtcars2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mtcars2 <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">am =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_recode</span>(am, </span>
<span id="cb12-3">                         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"automatic"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span>,</span>
<span id="cb12-4">                         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"manual"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>))</span>
<span id="cb12-5">mtcars2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(am)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>         am  n
1 automatic 19
2    manual 13</code></pre>
</div>
</div>
<p>Notice the syntax, “new_name” = “old_name”, which is common in <code>tidyverse</code> R.</p>
<p>When you have a large number of different factor levels, it may be helpful to lump the less common ones into an “other” category. For instance, the next chunk combines all categories with fewer than 20 occurrences.</p>
<div class="cell">
<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">scooby_sm2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> scooby_sm <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">motive =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_lump_min</span>(motive,</span>
<span id="cb14-3">                               <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;">20</span>)) </span>
<span id="cb14-4">scooby_sm2 <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;">count</span>(motive) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb14-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>n)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 2
  motive          n
  &lt;fct&gt;       &lt;int&gt;
1 Competition   130
2 Theft          93
3 Other          76
4 Treasure       40
5 Conquer        33
6 &lt;NA&gt;            2</code></pre>
</div>
</div>
<p>Also check out <code>fct_lump_n()</code> to specify the number of levels retained, <code>fct_lump_prop()</code> to lump low-percentage levels, and <code>fct_lump_lowfreq()</code> to try to force the new “Other” level to have lowest frequency.</p>
</section>
<section id="rearranging-levels" class="level2">
<h2 class="anchored" data-anchor-id="rearranging-levels">Rearranging levels</h2>
<p>Sometimes the order of factor levels matters, for instance in a bar chart. The default odering is alphanumeric.</p>
<div class="cell" data-layout-align="center">
<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;">ggplot</span>(scooby_sm2, </span>
<span id="cb16-2">       <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> motive)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb16-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://equitableequations.com/posts/2026-09-07/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:60.0%"></p>
</figure>
</div>
</div>
</div>
<p>Use <code>fct_relevel()</code> to shift levels to the front. For instance, the next code chunk places the motives <code>Treasure</code> and <code>Theft</code> before <code>Compitition</code> and others.</p>
<div class="cell" data-layout-align="center">
<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">scooby_sm3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> scooby_sm2 <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;">motive =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_relevel</span>(motive,</span>
<span id="cb17-3">                              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Treasure"</span>,</span>
<span id="cb17-4">                              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Theft"</span>)) </span>
<span id="cb17-5"></span>
<span id="cb17-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(scooby_sm3, </span>
<span id="cb17-7">       <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> motive)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb17-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://equitableequations.com/posts/2026-09-07/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:60.0%"></p>
</figure>
</div>
</div>
</div>
<p>Often, you just want to put the most common factor levels first. Use <code>fct_infreq()</code> to accomplish this.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(scooby_sm2, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_infreq</span>(motive))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb18-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://equitableequations.com/posts/2026-09-07/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:60.0%"></p>
</figure>
</div>
</div>
</div>
<p>You can also reverse the order of the factor levels with <code>fct_rev</code>.</p>
</section>
<section id="reordering-levels-using-a-second-variable" class="level2">
<h2 class="anchored" data-anchor-id="reordering-levels-using-a-second-variable">Reordering levels using a second variable</h2>
<p>For some plots, it can be natural to order a categorical variable using a numeric one. For instance, in the next plot, it would be preferable to show the various motives in increasing order of imdb ratings.</p>
<div class="cell" data-layout-align="center">
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(scooby_sm3, <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> motive,</span>
<span id="cb19-2">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> imdb)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb19-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://equitableequations.com/posts/2026-09-07/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:60.0%"></p>
</figure>
</div>
</div>
</div>
<p>Use <code>fct_reorder</code> to accomplish this.</p>
<div class="cell" data-layout-align="center">
<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">scooby_sm4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> scooby_sm3 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb20-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;">motive =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_reorder</span>(motive,</span>
<span id="cb20-3">                              imdb))</span>
<span id="cb20-4"></span>
<span id="cb20-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(scooby_sm4, <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> motive,</span>
<span id="cb20-6">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> imdb)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb20-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://equitableequations.com/posts/2026-09-07/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:60.0%"></p>
</figure>
</div>
</div>
</div>
<p>By default, <code>fct_reorder</code> sorts using the median. You can specify a different function (like mean) by adding a <code>.fun</code> argument.</p>
</section>
<section id="ordered-factors" class="level2">
<h2 class="anchored" data-anchor-id="ordered-factors">Ordered factors</h2>
<p>Occasionally you may encounter ordered factors, ones of class <code>ord</code>. For the most part, you can treat these like regular factors.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(diamonds)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 53,940
Columns: 10
$ carat   &lt;dbl&gt; 0.23, 0.21, 0.23, 0.29, 0.31, 0.24, 0.24, 0.26, 0.22, 0.23, 0.…
$ cut     &lt;ord&gt; Ideal, Premium, Good, Premium, Good, Very Good, Very Good, Ver…
$ color   &lt;ord&gt; E, E, E, I, J, J, I, H, E, H, J, J, F, J, E, E, I, J, J, J, I,…
$ clarity &lt;ord&gt; SI2, SI1, VS1, VS2, SI2, VVS2, VVS1, SI1, VS2, VS1, SI1, VS1, …
$ depth   &lt;dbl&gt; 61.5, 59.8, 56.9, 62.4, 63.3, 62.8, 62.3, 61.9, 65.1, 59.4, 64…
$ table   &lt;dbl&gt; 55, 61, 65, 58, 58, 57, 57, 55, 61, 61, 55, 56, 61, 54, 62, 58…
$ price   &lt;int&gt; 326, 326, 327, 334, 335, 336, 336, 337, 337, 338, 339, 340, 34…
$ x       &lt;dbl&gt; 3.95, 3.89, 4.05, 4.20, 4.34, 3.94, 3.95, 4.07, 3.87, 4.00, 4.…
$ y       &lt;dbl&gt; 3.98, 3.84, 4.07, 4.23, 4.35, 3.96, 3.98, 4.11, 3.78, 4.05, 4.…
$ z       &lt;dbl&gt; 2.43, 2.31, 2.31, 2.63, 2.75, 2.48, 2.47, 2.53, 2.49, 2.39, 2.…</code></pre>
</div>
<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">diamonds <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;">count</span>(cut) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># several ordered factors</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 2
  cut           n
  &lt;ord&gt;     &lt;int&gt;
1 Fair       1610
2 Good       4906
3 Very Good 12082
4 Premium   13791
5 Ideal     21551</code></pre>
</div>
</div>
<p>Specifying that a factor is ordered (for instance using <code>ordered</code>) can occasionally matter in modeling applications and certain forms of statistical inference. Other than that, the most visible difference with ordered factors in R is how they’re displayed by <code>ggplot</code>.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(diamonds, <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> cut,</span>
<span id="cb25-2">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> cut)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb25-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://equitableequations.com/posts/2026-09-07/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:60.0%"></p>
</figure>
</div>
</div>
</div>
<p>By default, ordered factors are colored using the <code>viridis</code> palette, which is has a distinct order to it.</p>
</section>
<section id="want-more" class="level2">
<h2 class="anchored" data-anchor-id="want-more">Want more?</h2>
<p>If something here isn’t clear, there’s a good chance you’ll find an explanation in my vid on the subject:</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/Nkjy6UFKJsc?si=7OCFQeMoxzYjsK_l" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="">
</iframe>
<p><button class="btn btn-default downloadthis  " id="dnldts54169"><i class="bi bi-download"></i> Download scooby.xlsx </button> <button class="btn btn-default downloadthis  " id="dnldts2023"><i class="bi bi-download"></i> Download Quarto </button> <button class="btn btn-default downloadthis  " id="dnldts17316"><i class="bi bi-download"></i> Download html </button></p>
<p>This post will be cross-listed on <a href="https://www.r-bloggers.com/">R-bloggers</a>, a site that I totally recommend.</p>


</section>

 ]]></description>
  <category>forcats</category>
  <category>R-bloggers</category>
  <guid>https://equitableequations.com/posts/2026-09-07/</guid>
  <pubDate>Mon, 07 Sep 2026 05:00:00 GMT</pubDate>
  <media:content url="https://equitableequations.com/posts/2026-09-07/factors thumb.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>Regular expressions for R users</title>
  <dc:creator>Andrew Gard</dc:creator>
  <link>https://equitableequations.com/posts/2026-08-24/</link>
  <description><![CDATA[ 




<p>Searching and manipulating strings (those things that make up character vectors) is easy as long as you only need to match specific sequences of letters and numbers. To match patterns rather than specific substrings or to search symbols, you’ll need to use <em>regular expressions</em> (<em>regex</em> for short).</p>
<p>Throughout this post, I’ll make use three <code>stringr</code> functions: <code>str_subset</code>, <code>str_detect</code>, and <code>str_extract</code>, and a subset of the <code>sentences</code> vector.</p>
<div class="cell">
<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>(stringr)</span>
<span id="cb1-2"></span>
<span id="cb1-3">sentences <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sentences[<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;">10</span>]</span>
<span id="cb1-4">sentences</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "The birch canoe slid on the smooth planks." 
 [2] "Glue the sheet to the dark blue background."
 [3] "It's easy to tell the depth of a well."     
 [4] "These days a chicken leg is a rare dish."   
 [5] "Rice is often served in round bowls."       
 [6] "The juice of lemons makes fine punch."      
 [7] "The box was thrown beside the parked truck."
 [8] "The hogs were fed chopped corn and garbage."
 [9] "Four hours of steady work faced us."        
[10] "A large size in stockings is hard to sell." </code></pre>
</div>
<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;">str_subset</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"of"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># keep only elements with a match</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "It's easy to tell the depth of a well."
[2] "Rice is often served in round bowls."  
[3] "The juice of lemons makes fine punch." 
[4] "Four hours of steady work faced us."   </code></pre>
</div>
<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;">str_detect</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"of"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># TRUE when an element matches, FALSE otherwise</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE</code></pre>
</div>
<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;">str_extract</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"of"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pull out matching substrings</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] NA   NA   "of" NA   "of" "of" NA   NA   "of" NA  </code></pre>
</div>
</div>
<section id="special-characters" class="level2">
<h2 class="anchored" data-anchor-id="special-characters">Special characters</h2>
<p>The following characters (all of which are explained below) have special meanings and can’t be searched literally.</p>
<div class="cell">
<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> ( ) [ { <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> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ? </span></code></pre></div>
</div>
<p>For instance, a dot matches with <em>any</em> character, which explains the following output.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "T" "G" "I" "T" "R" "T" "T" "T" "F" "A"</code></pre>
</div>
</div>
<p>In order to match these metacharacters, precede them with two slashes, like so:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract</span>(sentences, <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;">."</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "." "." "." "." "." "." "." "." "." "."</code></pre>
</div>
</div>
</section>
<section id="position-and-relative-position" class="level2">
<h2 class="anchored" data-anchor-id="position-and-relative-position">Position and relative position</h2>
<p>To specify that a substring comes at the beginning of a string, use “^”. To specify that it comes at the end, use “$”.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_subset</span>(fruit, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^re"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "redcurrant"</code></pre>
</div>
<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;">str_subset</span>(fruit, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"er$"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># note order</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "bell pepper"  "chili pepper" "cucumber"    </code></pre>
</div>
</div>
<p>As mentioned above, a dot indicates that substrings are separated by an arbitrary character.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s.o"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "smo" NA    NA    NA    "s o" NA    NA    NA    "s o" "sto"</code></pre>
</div>
</div>
</section>
<section id="character-sets" class="level2">
<h2 class="anchored" data-anchor-id="character-sets">Character sets</h2>
<p>Use brackets to allow multiple options for the character to be matched.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_subset</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[oe]n"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "The birch canoe slid on the smooth planks."
[2] "These days a chicken leg is a rare dish."  
[3] "Rice is often served in round bowls."      
[4] "The juice of lemons makes fine punch."     </code></pre>
</div>
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[oe]n"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "on" NA   NA   "en" "en" "on" NA   NA   NA   NA  </code></pre>
</div>
</div>
<p>Ranges of letters and numbers are allowed.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_subset</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[a-j]o"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Rice is often served in round bowls."       
[2] "The box was thrown beside the parked truck."
[3] "The hogs were fed chopped corn and garbage."
[4] "Four hours of steady work faced us."        </code></pre>
</div>
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[a-j]o"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] NA   NA   NA   NA   "bo" NA   "bo" "ho" "ho" NA  </code></pre>
</div>
</div>
<p>Inside of brackets, “^” represents negation.</p>
<div class="cell">
<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;">str_extract</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[^a-j]o"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "no" "to" "to" NA   " o" " o" "ro" NA   "Fo" "to"</code></pre>
</div>
</div>
</section>
<section id="character-classes" class="level2">
<h2 class="anchored" data-anchor-id="character-classes">Character classes</h2>
<p>It’s also possible to specify classes of characters. “\d” matches any digit and “\w” matches and word character (a-z, A-Z, 0-9, and underscore), while “\D” and “\W” will match anything <em>except</em> digits and word characters, respectively. Many other classes are available. Enter <code>?regex</code> for a full list.</p>
</section>
<section id="the-role-of-in-quarto" class="level2">
<h2 class="anchored" data-anchor-id="the-role-of-in-quarto">The role of “\” in Quarto</h2>
<p>If you write “\d” in a Quarto document, it won’t render properly. This is because Quarto interprets the slash as an escape character. To actually render this string, use “\\d” instead.</p>
<p>Similarly, to render a slash, use \\.</p>
</section>
<section id="matching-whitespace" class="level2">
<h2 class="anchored" data-anchor-id="matching-whitespace">Matching whitespace</h2>
<p>Use <code>\\s</code> for space, <code>\\t</code> for tab, and <code>\\n</code> for a new line.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_subset</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"e</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;">sb"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "The birch canoe slid on the smooth planks." 
[2] "Glue the sheet to the dark blue background."
[3] "The box was thrown beside the parked truck."</code></pre>
</div>
</div>
</section>
<section id="repetition" class="level2">
<h2 class="anchored" data-anchor-id="repetition">Repetition</h2>
<p>Use curly braces to indicate how many times in a row a character must appear.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_subset</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"e{2}"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Glue the sheet to the dark blue background."</code></pre>
</div>
</div>
<p>Use “+” to indicate at least one occurrence, possibly more.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_subset</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"she+t"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Glue the sheet to the dark blue background."</code></pre>
</div>
</div>
<p>To indicate a character is optional, use “?”. For instance, the next command detects both “the” and “often”.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"th?e"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "the" "the" "te"  NA    "te"  NA    "the" NA    "te"  NA   </code></pre>
</div>
</div>
<p>Finally, “*” indicates an optional character that may occur more than once.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"she*"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] NA     "shee" NA     "sh"   NA     NA     NA     NA     NA     NA    </code></pre>
</div>
</div>
<p>Notice that these symbols come <em>after</em> the characters they refer to, unlike “\” which comes before.</p>
</section>
<section id="combining-substrings-with" class="level2">
<h2 class="anchored" data-anchor-id="combining-substrings-with">Combining substrings with <code>|</code></h2>
<p>Use the pipe to indicate an “or” statement. Expressions on either side are grouped by default.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_subset</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chicken|hog"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "These days a chicken leg is a rare dish."   
[2] "The hogs were fed chopped corn and garbage."</code></pre>
</div>
</div>
<p>You can still group using parentheses, though.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_subset</span>(sentences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(chicken|hog)s"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "The hogs were fed chopped corn and garbage."</code></pre>
</div>
</div>
</section>
<section id="want-more" class="level2">
<h2 class="anchored" data-anchor-id="want-more">Want more?</h2>
<p>If something here isn’t clear, there’s a good chance you’ll find an explanation in my vid on the subject:</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/wy0cEKYeaF8?si=RktCufxzNGzK8UEi" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="">
</iframe>
<p><button class="btn btn-default downloadthis  " id="dnldts64172"><i class="bi bi-download"></i> Download Quarto </button> <button class="btn btn-default downloadthis  " id="dnldts25292"><i class="bi bi-download"></i> Download html </button></p>
<p>This post will be cross-listed on <a href="https://www.r-bloggers.com/">R-bloggers</a>, a site that I totally recommend.</p>


</section>

 ]]></description>
  <category>regex</category>
  <category>stringr</category>
  <category>R-bloggers</category>
  <guid>https://equitableequations.com/posts/2026-08-24/</guid>
  <pubDate>Mon, 24 Aug 2026 05:00:00 GMT</pubDate>
  <media:content url="https://equitableequations.com/posts/2026-08-24/regex thumb.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>Discrete probability calculations in R</title>
  <dc:creator>Andrew Gard</dc:creator>
  <link>https://equitableequations.com/posts/2025-09-15/</link>
  <description><![CDATA[ 




<p>In R, functions that perform probabilities calculations follow a consistent structure:</p>
<ul>
<li><p>Probability mass functions (pmfs) start with the letter “d”. Use such a function to compute <img src="https://latex.codecogs.com/png.latex?P(X%20=%20x_0)"> for any <img src="https://latex.codecogs.com/png.latex?x_0"> of interest.</p></li>
<li><p>Cumulative distribution functions (cdfs) start with the letter “p”. Use such a function to compute <img src="https://latex.codecogs.com/png.latex?P(X%20%5Cle%20x_0)">.</p></li>
<li><p>Inverse cdfs start with the letter “q”. For a given probability <img src="https://latex.codecogs.com/png.latex?p">, they determine the least <img src="https://latex.codecogs.com/png.latex?x_0"> such that <img src="https://latex.codecogs.com/png.latex?P(X%20%5Cle%20x_0)%20%5Cge%20p">.</p></li>
<li><p>Functions to generate random values start with the letter “r”.</p></li>
</ul>
<p>The rest of the function name is a shortened version of the distribution name:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 28%">
<col style="width: 20%">
<col style="width: 17%">
<col style="width: 16%">
<col style="width: 16%">
</colgroup>
<thead>
<tr class="header">
<th>Distribution</th>
<th>Probability</th>
<th>Cumulative</th>
<th>Inverse</th>
<th>Random</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Bin<img src="https://latex.codecogs.com/png.latex?(n,%20p)"></td>
<td>dbinom()</td>
<td>pbinom()</td>
<td>qbinom()</td>
<td>rbinom()</td>
</tr>
<tr class="even">
<td>Geom<img src="https://latex.codecogs.com/png.latex?(p)"></td>
<td>dgeom()</td>
<td>pgeom()</td>
<td>qgeom()</td>
<td>rgeom()</td>
</tr>
<tr class="odd">
<td>NB<img src="https://latex.codecogs.com/png.latex?(r,%20p)"></td>
<td>dnbinom()</td>
<td>pnbinom()</td>
<td>qnbinom()</td>
<td>rnbinom()</td>
</tr>
<tr class="even">
<td>Hyp<img src="https://latex.codecogs.com/png.latex?(N_1,%20N_2,%20n)"></td>
<td>dhyper()</td>
<td>phyper()</td>
<td>qhyper()</td>
<td>rhyper()</td>
</tr>
<tr class="odd">
<td>Pois<img src="https://latex.codecogs.com/png.latex?(%5Clambda)"></td>
<td>dpois()</td>
<td>ppois()</td>
<td>qpois()</td>
<td>rpois()</td>
</tr>
</tbody>
</table>
<p>In case some of these distributions aren’t familiar to you:</p>
<ul>
<li><p>The <strong>binomial distribution</strong> models the total number of success in <img src="https://latex.codecogs.com/png.latex?n"> Bernouilli trials (identical and independent trials, each with only two possibly outcomes). The probability of success in each trial is <img src="https://latex.codecogs.com/png.latex?p">.</p></li>
<li><p>The <strong>geometric distribution</strong> models the number of failures in a sequence of Bernouilli trials prior to the first success.</p></li>
<li><p>The <strong>negative binomial distribution</strong> models the number of failures in a sequence of Bernouilli trials prior to the <img src="https://latex.codecogs.com/png.latex?r%5E%7Bth%7D"> success.</p></li>
<li><p>The <strong>hypergeometric distribution</strong> models the number of successes in a sample of size <img src="https://latex.codecogs.com/png.latex?n"> when selection is done without replacement from a pool that includes <img src="https://latex.codecogs.com/png.latex?N_1"> successes and <img src="https://latex.codecogs.com/png.latex?N_2"> failures.</p></li>
</ul>
<p>For instance, imagine a student guessing randomly on a 12-question quiz where each question has 4 possible answers. The probability that they get exactly 3 questions right is computed using <code>dbinom()</code>.</p>
<div class="cell">
<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;">dbinom</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.2581036</code></pre>
</div>
</div>
<p>Similarly, the probability that they get <em>at most</em> 3 questions right is given by</p>
<div class="cell">
<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;">pbinom</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.6487786</code></pre>
</div>
</div>
<p>If you need the probability that they get <em>at least</em> 3 questions right, add a <code>lower.tail</code> argument, like so:</p>
<div class="cell">
<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;">pbinom</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</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;">lower.tail =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.3512214</code></pre>
</div>
</div>
<p><code>qbinom()</code> reverses the logic of <code>pbinom()</code>, finding the number of successes with a specified cumulative probability. Said differently, <code>qbinom()</code> identifies <em>percentiles</em>. For instance, we might wonder how many questions the child would have to get right to do at least as well as 80% of randomly-guessing quiz takers.</p>
<div class="cell">
<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;">qbinom</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4</code></pre>
</div>
</div>
<p>Notice how the arguments of these functions follow a common pattern: first the value/percentile of interest, then the parameters, which come in a consistent order. This pattern continues to hold for <code>rbinom()</code>, which generates a specified number of random values</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For reproducibility</span></span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbinom</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 5 2 2 3 5 2 5 5 4 3</code></pre>
</div>
</div>
<p>All of R’s probability functions are <em>vectorized</em>, meaning that they can perform multiple calculations at once. For instance, the next line computes the probability of obtaining exactly <img src="https://latex.codecogs.com/png.latex?x"> Butterfingers when randomly grabbing 5 candy bars from a bowl with 8 Butterfingers and 10 Snickers. Note that <img src="https://latex.codecogs.com/png.latex?x%20%5Cin%20%5C%7B0,%201,%202,%20%5Ccdots,%205%5C%7D">.</p>
<div class="cell">
<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;">dhyper</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.029411765 0.196078431 0.392156863 0.294117647 0.081699346 0.006535948</code></pre>
</div>
</div>
<p>As you’d expect, these probabilities all add up to one.</p>
<p>Some caution is needed when working with the geometric and negative binomial distributions, both of which consider the number of failures occurring before a fixed number of successes. Some textbooks and computational tools define these distributions in terms on the total number of <em>trials</em> needed rather than the total number of <em>failures</em>, so some translation may be needed. Fortunately,</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Ctext%7Btrials%7D%5C%20=%5C%20%5Ctext%7Bfailures%7D%20+%20%5Ctext%7Bsuccesses%7D,"> so the two formulations differ only in the addition or subtraction of the parameter <img src="https://latex.codecogs.com/png.latex?r">.</p>
<p><strong>Example.</strong> A basketball player makes 85% of their free throws. If they shoot until they make 5 shots, what is the probability that 7 shots are needed?</p>
<p><strong>Solution.</strong> Making 5 out of 7 shots requires exactly 2 misses, so the probability is</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dnbinom</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;">5</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.1497505</code></pre>
</div>
</div>
<p>You might consider trying the next problem on your own before reading my solution.</p>
<p><strong>Example.</strong> Every day, a new author has a 10% chance of selling a copy of their ebook. Assume that purchases are independent of one another and that there are never multiple sales in a single day.</p>
<ol type="a">
<li><p>Find the probability that the first purchase occurs on the 10<img src="https://latex.codecogs.com/png.latex?%5E%7Bth%7D"> day.</p></li>
<li><p>Find the probability that the second purchase occurs during the second week (days 8-14).</p></li>
</ol>
<p><strong>Solution.</strong> Let <img src="https://latex.codecogs.com/png.latex?X"> be the number of days that pass before the first purchase and let <img src="https://latex.codecogs.com/png.latex?Y"> be the number of purchase-less days before the second. Then <img src="https://latex.codecogs.com/png.latex?X"> has a geometric distribution and <img src="https://latex.codecogs.com/png.latex?Y"> has a negative binomial distribution. In each case, <img src="https://latex.codecogs.com/png.latex?p=.10">.</p>
<ol type="a">
<li><p>If 10 trials are needed for a single success, then 9 failures must have occurred, so we compute <img src="https://latex.codecogs.com/png.latex?P(X%20=%209)%20="> <code>dgeom(9, .10)</code> = 0.038742</p></li>
<li><p>If between 8 and 14 days are needed for 2 sales, then between 6 and 12 days must have occurred without sales. <img src="https://latex.codecogs.com/png.latex?P(6%20%5Cle%20Y%20%5Cle%2012)%20="> <code>pnbinom(12, 2, .10) - pnbinom(5, 2, .10)</code> = 0.2656765</p></li>
</ol>
<p>Prefer to have this all clearly explained in a short vid? I’ve got you covered:</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/Z_11FDzyn7k?si=6wco1eMthtb-Rskj" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="">
</iframe>
<p><button class="btn btn-default downloadthis  " id="dnldts55550"><i class="bi bi-download"></i> Download slides </button></p>
<p>This post will be cross-listed on <a href="https://www.r-bloggers.com/">R-bloggers</a>, a site that I totally recommend.</p>



 ]]></description>
  <category>Probability</category>
  <category>R-bloggers</category>
  <guid>https://equitableequations.com/posts/2025-09-15/</guid>
  <pubDate>Mon, 15 Sep 2025 05:00:00 GMT</pubDate>
  <media:content url="https://equitableequations.com/posts/2025-09-15/probs in R thumb.png" medium="image" type="image/png" height="81" width="144"/>
</item>
</channel>
</rss>
