<?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>Francisco Cardozo</title>
<link>https://focardozom.github.io/posts.html</link>
<atom:link href="https://focardozom.github.io/posts.xml" rel="self" type="application/rss+xml"/>
<description>Francisco Cardozo is a prevention scientist at the University of Miami working on AI for program implementation, machine learning, and open-source R tools.</description>
<image>
<url>https://focardozom.github.io/img/yo.JPEG</url>
<title>Francisco Cardozo</title>
<link>https://focardozom.github.io/posts.html</link>
</image>
<generator>quarto-1.10.19</generator>
<lastBuildDate>Mon, 07 Jul 2025 00:00:00 GMT</lastBuildDate>
<item>
  <title>Welcome to prevention science meets sci-fi.</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <link>https://focardozom.github.io/posts/2025-03-19-sci-fi/ps-sci-fi.html</link>
  <description><![CDATA[ 




<blockquote class="blockquote">
<p>AI is knocking at the door of prevention science, carrying predictions instead of theories and probabilities instead of certainties. It’s exciting, slightly unsettling, and entirely unavoidable-welcome to efficacy’s existential crisis. <em>ChatGPT 4.0</em></p>
</blockquote>
<p>Prevention science has long been grounded in a stable architecture of evaluation. For decades, researchers have relied on established <a href="https://preventionresearch.org/advocacy/standards-of-evidence/">Standards of Evidence</a> to determine whether interventions are efficacious, effective, and ready to be scaled. These criteria, first articulated by Flay and colleagues, emphasized randomized controlled trials (RCTs), replication, and theoretical clarity as the hallmarks of trustworthy interventions. A decade later, the Society for Prevention Research refined and expanded these standards, incorporating dimensions such as <em>implementation quality</em>, <em>adaptation</em>, <em>documentation</em>, and <em>contextual fit</em>. These evolving guidelines built a shared language for what counts as evidence in prevention science, offering both clarity and continuity in an otherwise complex and variable field. They were not just methodological requirements-they were epistemological anchors.</p>
<p>But the clarity of that system is now encountering something much harder to categorize. Since the emergence of generative AI systems like ChatGPT, the boundaries of what constitutes an “intervention” have begun to blur. If we can no longer clearly define what a program is, then the standards for determining “what works” also become unclear. AI’s integration into preventive interventions is challenging our foundational assumptions. Unlike traditional programs with predefined modules and structured delivery, AI-driven tools can fully personalize content in response to each participant’s input, often without any direct oversight from a program team. These AI-facilitated interventions may autonomously decide what comes next based solely on general parameters and participant responses. While this may simulate the adaptive judgment of human facilitators, the key difference is that responsibility for content and implementation falls on the AI, an entity that, by nature, cannot assume responsibility in any meaningful sense</p>
<p>What are the standards for evaluating interventions when the treatment shifts with each user? What does fidelity mean when adaptation is the rule, not the exception? The field now faces interventions with the potential to resist standardization. In such a landscape, many of our familiar assumptions about replication, generalizability, even the boundaries of what constitutes an intervention, begin to feel less like scientific necessities and more like historical artifacts. Evaluation, as we’ve known it, is being asked to evaluate something that moves. And in doing so, it is singaling that science must now evolve to accommodate an intelligence that is not only artificial, but creative.</p>
<p>It may be time to imagine a new framework to assess what we call an effective program (maybe a name for what we define as an intervention). This does not mean abandoning rigor, but rather expanding its scope. New standards might require that AI-driven interventions meet performance benchmarks, undergo regular bias audits, and maintain transparent, open-source models. Simulation-based evidence might gain credibility, and real-time fidelity tracking could become the norm. In this context, evaluation itself becomes adaptive, built to monitor programs that do not repeat but respond.</p>
<p>What began as a stable, organized system is now moving into fluid territory. But the movement is not a collapse, <em>it is a transformation</em>. Prevention science remains anchored in its commitment to improving lives. That commitment just now has to stretch across a more dynamic, less predictable, and potentially more powerful set of tools. If the early decades of prevention science were about constructing a solid foundation, this moment is about learning to build on terrain that shifts beneath us. The questions we ask may stay the same, but the answers and the ways we find them are changing fast.</p>



<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>Prevention Science</category>
  <category>Machine Learning</category>
  <guid>https://focardozom.github.io/posts/2025-03-19-sci-fi/ps-sci-fi.html</guid>
  <pubDate>Mon, 07 Jul 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Three Components of a Successful Prevention Program</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <link>https://focardozom.github.io/posts/2024-12-28-three-components/three-components.html</link>
  <description><![CDATA[ 




<p>One of the core challenges in prevention science and public health is establishing whether a program causes the outcomes we observe in the field. Are the gains we see (i.e., reduced substance use, improved mental well-being, or lowered dropout rates) directly attributable to the intervention we designed and implemented? Or might they result from unrelated social trends, fluctuating resources, or measurement artifacts?</p>
<p>To approach these questions, Randomized Controlled Trials (RCTs) have become the gold standard. In an RCT, we randomly assign participants or sites to treatment and control (or comparison) groups, then compare outcomes. If the treatment group shows a significantly better result than the control group, we often conclude that the program “worked.” But such a conclusion can be deceptively straightforward when programs are, in fact, systems, they typically involve an interplay of Theory of Change (T), Implementation (I), and Evaluation (E).</p>
<section id="programs-as-systems" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="programs-as-systems"><span class="header-section-number">1</span> Programs as Systems</h2>
<p>Consider a program meant to decrease adolescent vaping. The Theory of Change sets out the blueprint of why specific activities (peer mentoring, parental engagement, etc.) are expected to make a difference. Implementation is the process of putting those activities into action (i.e., hiring and training staff, ensuring session fidelity, adapting schedules to participants’ needs, and more). Finally, Evaluation measures outcomes, seeking evidence that the program actually achieved its stated goals.</p>
<p>When a prevention program is conceptualized as this interconnected system, a typical RCT that simply asks, “Does the program work?” can obscure which part of the system is actually influencing the observed outcomes. This can lead to confusing or contradictory findings, positive results might mask a faulty theory (if staff happened to do something else that helped), and negative or null results might disguise an essentially correct theory that was never implemented well.</p>
</section>
<section id="why-typical-rct-frameworks-can-be-limiting" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="why-typical-rct-frameworks-can-be-limiting"><span class="header-section-number">2</span> Why Typical RCT Frameworks Can Be Limiting</h2>
<ol type="1">
<li>Black-Box Design</li>
</ol>
<p>Traditional RCTs treat the entire program as a black box. If we see a difference in outcomes, we infer that the program caused it. Yet we rarely dissect which portion of the program functioned well or poorly. If results are mediocre, we cannot easily pinpoint whether it was the theory that failed or the implementation.</p>
<p>These three components (T, I, E) can have different effects on the outcomes for example:</p>
<ul>
<li>If T is flawed (the theory is incorrect or incomplete), the intervention might accidentally help in other ways or might fail entirely.</li>
<li>If I is compromised (poor training, inadequate resources, low fidelity), the intervention as delivered differs substantially from the intervention as theorized.</li>
<li>If E is misaligned (measuring the wrong outcome or using unreliable methods), results can be misleading, even if the theory and the implementation were sound.</li>
</ul>
<ol start="2" type="1">
<li>Confounded by Implementation</li>
</ol>
<p>Consider an RCT run across multiple sites. Some sites deliver the program faithfully; others do not. If you average outcomes across all sites, the overall effect could appear modest, even though high-fidelity sites might have shown strong results. Hence, the classical RCT approach can mislead us into dismissing a promising program (or adopting one that only worked under special conditions).</p>
</section>
<section id="strengthening-causal-claims-a-systems-view" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="strengthening-causal-claims-a-systems-view"><span class="header-section-number">3</span> Strengthening Causal Claims: A Systems View</h2>
<p>By viewing prevention programs as systems, we can better diagnose why we see certain outcomes:</p>
<ol type="1">
<li><p>Disaggregate the Results Break down the program into T, I, and E. Evaluate fidelity, did staff deliver the planned activities (Implementation)? Did the underlying logic hold up in practice (Theory)? Did the measurement tools capture the actual changes (Evaluation)?</p></li>
<li><p>Assess Fidelity and Adaptation A high- or low-fidelity site comparison can reveal how much Implementation quality shapes outcomes. If only high-fidelity sites show significant improvements, the program’s fundamentals might be valid; the challenge is ensuring consistent delivery.</p></li>
</ol>
</section>
<section id="rcts-are-still-usefulbut-we-need-more" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="rcts-are-still-usefulbut-we-need-more"><span class="header-section-number">4</span> RCTs Are Still Useful—But We Need More</h2>
<p>This is not to say that RCTs lack value. Random assignment remains a powerful way to rule out many external factors that might otherwise explain an effect. The point is that an RCT, on its own, cannot fully disentangle why a program succeeded or failed—particularly if we do not measure or account for the intricacies of T, I, and E. The RCT’s rigorous comparison can show whether a difference in outcome exists, but interpreting that difference requires a deeper system-level understanding.</p>
</section>
<section id="conclusion" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="conclusion"><span class="header-section-number">5</span> Conclusion</h2>
<p>Determining whether an observed effect is genuinely caused by a prevention program involves more than a simple yes/no answer from an RCT. When we treat these interventions as systems—each with a distinct theory, mode of implementation, and evaluation metric—our classical causal framework can become opaque, leading to confusion and misguided conclusions. Flaws in implementation might overshadow a perfectly valid theory; incorrect or narrow evaluation metrics can inflate or hide real changes.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>Prevention Science</category>
  <category>Causal Inference</category>
  <guid>https://focardozom.github.io/posts/2024-12-28-three-components/three-components.html</guid>
  <pubDate>Sat, 28 Dec 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to Create a Bash Script for Easy File Conversion Using DuckDB</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <link>https://focardozom.github.io/posts/2024-11-17-to-duckdb/to-duckdb.html</link>
  <description><![CDATA[ 




<p>Working with different file formats can be challenging, especially when dealing with large datasets. Here’s a simple but powerful Bash script that leverages DuckDB to convert files between different formats (like CSV and Parquet) directly from your terminal.</p>
<p>I took the script from <a href="https://gist.github.com/mehd-io/31f74387f03e3589a6875d7e0572b9f0">here</a>.</p>
<p>Please visit the <a href="https://duckdb.org/2024/03/19/convert-files.html">Duckdb website</a>. They are doing some of the most amazing work in the data space.</p>
<p>Also, visite the <a href="https://www.youtube.com/@motherduckdb">Youtube channel</a> for some amazing content. And <a href="https://motherduck.com">Motherduck</a> for some more amazing things.</p>
<section id="step-by-step-guide" class="level1">
<h1>Step-by-Step Guide</h1>
<section id="step-1-install-duckdb" class="level2">
<h2 class="anchored" data-anchor-id="step-1-install-duckdb">Step 1: Install DuckDB</h2>
<p>Before we begin, make sure you have DuckDB installed. You can install it using Homebrew:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">brew</span> install duckdb</span></code></pre></div></div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>If you don’t have Homebrew installed, you can get it from <a href="https://brew.sh/">brew.sh</a>.</p>
</div>
</div>
</section>
<section id="step-2-prepare-your-workspace" class="level2">
<h2 class="anchored" data-anchor-id="step-2-prepare-your-workspace">Step 2: Prepare Your Workspace</h2>
<p>Just like in our previous scripts (<a href="https://focardozom.github.io/posts/2024-04-06-getref/getref.html">getref</a> and <a href="https://focardozom.github.io/posts/2024-11-09-organize-this/organize-this.html">organize</a>), we’ll store this function in your bin directory. If you haven’t created one yet:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> ~/bin</span></code></pre></div></div>
</section>
<section id="step-3-create-the-script-file" class="level2">
<h2 class="anchored" data-anchor-id="step-3-create-the-script-file">Step 3: Create the Script File</h2>
<p>Navigate to your bin directory and create a new file named <code>toduck</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> ~/bin</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">touch</span> toduck</span></code></pre></div></div>
</section>
<section id="step-4-add-the-script-code" class="level2">
<h2 class="anchored" data-anchor-id="step-4-add-the-script-code">Step 4: Add the Script Code</h2>
<p>Open the file in your preferred text editor:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nano</span> toduck</span></code></pre></div></div>
<p>Copy and paste the following code:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/bin/bash</span></span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">convert_file()</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">{</span></span>
<span id="cb5-4">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">local</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">input_file</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$1</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb5-5">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">local</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">output_extension</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$2</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb5-6"></span>
<span id="cb5-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extracting the filename without extension</span></span>
<span id="cb5-8">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">local</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">base_name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">basename</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$input_file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb5-9">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">local</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${base_name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>.<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb5-10"></span>
<span id="cb5-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Constructing the output filename</span></span>
<span id="cb5-12">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">local</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">output_file</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${name}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${output_extension}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb5-13"></span>
<span id="cb5-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Performing the conversion</span></span>
<span id="cb5-15">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">duckdb</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-c</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"copy (select * from '</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${input_file}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">') to '</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${output_file}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span></span>
<span id="cb5-16"></span>
<span id="cb5-17">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Conversion complete: </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${output_file}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb5-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">}</span></span>
<span id="cb5-19"></span>
<span id="cb5-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check if the number of arguments is less than 2</span></span>
<span id="cb5-21"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$#</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-lt</span> 2 <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">then</span></span>
<span id="cb5-22">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Usage: </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$0</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> &lt;input_file&gt; &lt;output_extension&gt;"</span></span>
<span id="cb5-23">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Example: </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$0</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> example.parquet csv"</span></span>
<span id="cb5-24">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">exit</span> 1</span>
<span id="cb5-25"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fi</span></span>
<span id="cb5-26"></span>
<span id="cb5-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Call the conversion function with the provided arguments</span></span>
<span id="cb5-28"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">convert_file</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$1</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$2</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span></code></pre></div></div>
<p>Exit the editor and save the file.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>This script uses DuckDB’s SQL capabilities to read and write files in different formats. DuckDB automatically detects the input format and handles the conversion seamlessly.</p>
</div>
</div>
</section>
<section id="step-5-make-the-script-executable" class="level2">
<h2 class="anchored" data-anchor-id="step-5-make-the-script-executable">Step 5: Make the Script Executable</h2>
<p>Make your script executable by running:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chmod</span> +x ~/bin/toduck</span></code></pre></div></div>
</section>
<section id="step-6-update-your-shell-configuration" class="level2">
<h2 class="anchored" data-anchor-id="step-6-update-your-shell-configuration">Step 6: Update Your Shell Configuration</h2>
<p>If you haven’t already added your bin directory to your PATH (from previous scripts), you’ll need to do so. Open your shell configuration file:</p>
<p>For Zsh users:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nano</span> ~/.zshrc</span></code></pre></div></div>
<p>For Bash users:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nano</span> ~/.bashrc</span></code></pre></div></div>
<p>Add this line if it’s not already there:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">export</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PATH</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$HOME</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/bin:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$PATH</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span></code></pre></div></div>
</section>
<section id="step-7-apply-the-changes" class="level2">
<h2 class="anchored" data-anchor-id="step-7-apply-the-changes">Step 7: Apply the Changes</h2>
<p>Source your configuration file to apply the changes:</p>
<p>For Zsh:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">source</span> ~/.zshrc</span></code></pre></div></div>
<p>For Bash:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">source</span> ~/.bashrc</span></code></pre></div></div>
</section>
</section>
<section id="how-to-use-your-new-script" class="level1">
<h1>How to Use Your New Script</h1>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Data Safety First
</div>
</div>
<div class="callout-body-container callout-body">
<p>Always make sure you have backups of your important data files before performing any conversions. While DuckDB is reliable, it’s always good practice to protect your data.</p>
</div>
</div>
<p>Using the script is straightforward.</p>
<p>Navigate to the directory containing the file you want to convert and run the script with the desired output format. The syntax is:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb12-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">toduck</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>input_file<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>output_extension<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span></code></pre></div></div>
<p>Examples:</p>
<ol type="1">
<li>Convert a Parquet file to CSV:</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb13-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">toduck</span> data.parquet csv</span></code></pre></div></div>
<ol start="2" type="1">
<li>Convert a CSV file to Parquet:</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb14-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">toduck</span> data.csv parquet</span></code></pre></div></div>
<p>The script will: 1. Take your input file 2. Create a new file with the same name but different extension 3. Convert the data using DuckDB 4. Show you the path to the converted file</p>
</section>
<section id="supported-formats" class="level1">
<h1>Supported Formats</h1>
<p>DuckDB supports various file formats including: - CSV - Parquet - JSON - Excel (xlsx)</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The exact formats available may depend on your DuckDB version and configuration.</p>
</div>
</div>
</section>
<section id="common-use-cases" class="level1">
<h1>Common Use Cases</h1>
<p>This script is particularly useful when you need to: - Convert large CSV files to Parquet for better compression and performance - Make Parquet files readable in spreadsheet software by converting to CSV - Quickly convert between different data formats for various tools and applications</p>
<p>That’s it! You now have a powerful tool for converting data files right from your terminal. If you have any questions or suggestions for improvements, feel free to leave a comment below.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>Bash</category>
  <guid>https://focardozom.github.io/posts/2024-11-17-to-duckdb/to-duckdb.html</guid>
  <pubDate>Sun, 17 Nov 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to Create a Bash Script to Automatically Organize Your Files</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <link>https://focardozom.github.io/posts/2024-11-09-organize-this/organize-this.html</link>
  <description><![CDATA[ 




<p>Ever found yourself drowning in a sea of unorganized files? Try this simple but powerful Bash function that automatically organizes files into appropriate folders based on their extensions. This blog post will guide you through setting up an <code>organize_files</code> function that will help keep your directories neat and tidy.</p>
<section id="step-by-step-guide" class="level1">
<h1>Step-by-Step Guide</h1>
<section id="step-1-prepare-your-workspace" class="level2">
<h2 class="anchored" data-anchor-id="step-1-prepare-your-workspace">Step 1: Prepare Your Workspace</h2>
<p>Just like in our previous scripts <a href="https://focardozom.github.io/posts/2024-04-06-getref/getref.html">here</a>, we’ll store this function in your bin directory. If you haven’t created one yet:</p>
<p>1.1. Open the terminal (cmd+space then type terminal).</p>
<p>1.2. Create a new folder named bin in your home directory:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> ~/bin</span></code></pre></div></div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>If you already have a bin folder from previous scripts, you can skip this step.</p>
</div>
</div>
</section>
<section id="step-2-create-the-script-file" class="level2">
<h2 class="anchored" data-anchor-id="step-2-create-the-script-file">Step 2: Create the Script File</h2>
<p>Navigate to your bin directory and create a new file named <code>organize</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> ~/bin</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">touch</span> organize</span></code></pre></div></div>
</section>
<section id="step-3-add-the-script-code" class="level2">
<h2 class="anchored" data-anchor-id="step-3-add-the-script-code">Step 3: Add the Script Code</h2>
<p>Open the file in your preferred text editor. You can use Nano for simplicity:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nano</span> organize</span></code></pre></div></div>
<p>Copy and paste the following code into the file:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/bin/bash</span></span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Enable nullglob to avoid errors when no files match a pattern</span></span>
<span id="cb4-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">shopt</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-s</span> nullglob</span>
<span id="cb4-5"></span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check if directory exists</span></span>
<span id="cb4-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">!</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-d</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$1</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">then</span></span>
<span id="cb4-8">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Error: Directory '</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$1</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' does not exist"</span></span>
<span id="cb4-9">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">exit</span> 1</span>
<span id="cb4-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fi</span></span>
<span id="cb4-11"></span>
<span id="cb4-12"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">target_dir</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$1</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb4-13"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Organizing files in: </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$target_dir</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb4-14"></span>
<span id="cb4-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create directories if they don't exist</span></span>
<span id="cb4-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$target_dir</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>/<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{Images</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Videos</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Pdf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">PowerPoint</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Archives</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Excel</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Others}</span></span>
<span id="cb4-17"></span>
<span id="cb4-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Change to target directory</span></span>
<span id="cb4-19"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$target_dir</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">||</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">exit</span> 1</span>
<span id="cb4-20"></span>
<span id="cb4-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Function to move files and handle duplicates</span></span>
<span id="cb4-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">move_file()</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">{</span></span>
<span id="cb4-23">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">local</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">file</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$1</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb4-24">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">local</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">target_folder</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$2</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb4-25">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">local</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">filename</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">basename</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb4-26">    </span>
<span id="cb4-27">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$target_folder</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$filename</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">then</span></span>
<span id="cb4-28">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If file with the same name exists, add "-duplicate" before the extension</span></span>
<span id="cb4-29">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">local</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${filename</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>.<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb4-30">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">local</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">extension</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${filename</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">##</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>.<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb4-31">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">local</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">duplicate_name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${name}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">-duplicate.</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${extension}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb4-32">        </span>
<span id="cb4-33">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check again in case a duplicate already exists</span></span>
<span id="cb4-34">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$target_folder</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$duplicate_name</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">do</span></span>
<span id="cb4-35">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${name}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">-duplicate"</span></span>
<span id="cb4-36">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">duplicate_name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${name}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${extension}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb4-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">done</span></span>
<span id="cb4-38">        </span>
<span id="cb4-39">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mv</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-v</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$target_folder</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$duplicate_name</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb4-40">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span></span>
<span id="cb4-41">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mv</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-v</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$target_folder</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/"</span></span>
<span id="cb4-42">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fi</span></span>
<span id="cb4-43"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">}</span></span>
<span id="cb4-44"></span>
<span id="cb4-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Move files based on their extensions</span></span>
<span id="cb4-46"></span>
<span id="cb4-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Images</span></span>
<span id="cb4-48"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> file <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>.<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{jpg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">jpeg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">png</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">gif</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">HEIC}</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">do</span></span>
<span id="cb4-49">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">move_file</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Images"</span></span>
<span id="cb4-50"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">done</span></span>
<span id="cb4-51"></span>
<span id="cb4-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Videos</span></span>
<span id="cb4-53"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> file <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>.<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{mp4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">avi</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">mov}</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">do</span></span>
<span id="cb4-54">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">move_file</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Videos"</span></span>
<span id="cb4-55"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">done</span></span>
<span id="cb4-56"></span>
<span id="cb4-57"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PDF</span></span>
<span id="cb4-58"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> file <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>.pdf<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">do</span></span>
<span id="cb4-59">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">move_file</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pdf"</span></span>
<span id="cb4-60"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">done</span></span>
<span id="cb4-61"></span>
<span id="cb4-62"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PowerPoint</span></span>
<span id="cb4-63"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> file <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>.pptx<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">do</span></span>
<span id="cb4-64">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">move_file</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PowerPoint"</span></span>
<span id="cb4-65"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">done</span></span>
<span id="cb4-66"></span>
<span id="cb4-67"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Text</span></span>
<span id="cb4-68"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> file <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>.<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{docx</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">txt}</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">do</span></span>
<span id="cb4-69">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">move_file</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Text"</span></span>
<span id="cb4-70"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">done</span></span>
<span id="cb4-71"></span>
<span id="cb4-72"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Archives</span></span>
<span id="cb4-73"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> file <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>.<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{zip</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">rar}</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">do</span></span>
<span id="cb4-74">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">move_file</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Archives"</span></span>
<span id="cb4-75"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">done</span></span>
<span id="cb4-76"></span>
<span id="cb4-77"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Excel</span></span>
<span id="cb4-78"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> file <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>.<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{xlsx</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">csv}</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">do</span></span>
<span id="cb4-79">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">move_file</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Excel"</span></span>
<span id="cb4-80"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">done</span></span>
<span id="cb4-81"></span>
<span id="cb4-82"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Others (any remaining files)</span></span>
<span id="cb4-83"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> file <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">do</span></span>
<span id="cb4-84">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-f</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">move_file</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$file</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Others"</span></span>
<span id="cb4-85"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">done</span></span>
<span id="cb4-86"></span>
<span id="cb4-87"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"File organization complete in </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$target_dir</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">!"</span></span></code></pre></div></div>
<p>Save and close (<code>cmd+x</code> then <code>Y</code> then <code>return</code>) the file.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>This script creates separate folders for different file types and moves files accordingly. It handles various common file formats including images, videos, documents, and more.</p>
</div>
</div>
</section>
<section id="step-4-make-the-script-executable" class="level2">
<h2 class="anchored" data-anchor-id="step-4-make-the-script-executable">Step 4: Make the Script Executable</h2>
<p>Make your script executable by running:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chmod</span> +x ~/bin/organize</span></code></pre></div></div>
</section>
<section id="step-5-update-your-shell-configuration" class="level2">
<h2 class="anchored" data-anchor-id="step-5-update-your-shell-configuration">Step 5: Update Your Shell Configuration</h2>
<p>If you haven’t already added your bin directory to your PATH (from previous <a href="https://focardozom.github.io/posts/2024-04-06-getref/getref.html">scripts</a>), you’ll need to do so. Open your shell configuration file:</p>
<p>For Zsh users:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nano</span> ~/.zshrc</span></code></pre></div></div>
<p>For Bash users:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nano</span> ~/.bashrc</span></code></pre></div></div>
<p>Add this line if it’s not already there:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">export</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PATH</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$HOME</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/bin:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$PATH</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span></code></pre></div></div>
</section>
<section id="step-6-apply-the-changes" class="level2">
<h2 class="anchored" data-anchor-id="step-6-apply-the-changes">Step 6: Apply the Changes</h2>
<p>Source your configuration file to apply the changes:</p>
<p>For Zsh:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">source</span> ~/.zshrc</span></code></pre></div></div>
<p>For Bash:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">source</span> ~/.bashrc</span></code></pre></div></div>
</section>
</section>
<section id="how-to-use-your-new-script" class="level1">
<h1>How to Use Your New Script</h1>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>With Great Power Comes Great Responsibility
</div>
</div>
<div class="callout-body-container callout-body">
<p>This script moves files automatically, which can be very powerful but also potentially dangerous if used incorrectly. Always: - Test it first in a non-critical directory - Make backups of important files before organizing them - Double-check the directory path you’re providing</p>
</div>
</div>
<p>Using the script is straightforward. Simply type <code>organize</code> followed by the path to the directory you want to organize. For example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">organize</span> ~/Downloads</span></code></pre></div></div>
<p>This will: 1. Create organized folders in your specified directory 2. Move files to their appropriate folders based on their extensions 3. Show you what files are being moved</p>
<p>The script will create the following folders: - Images (for jpg, jpeg, png, gif, HEIC files) - Videos (for mp4, avi, mov files) - Pdf (for pdf files) - PowerPoint (for pptx files) - Documents (for docx, txt files) - Archives (for zip, rar files) - Excel (for xlsx, csv files)</p>
</section>
<section id="customization" class="level1">
<h1>Customization</h1>
<p>You can easily customize this script by: - Adding new folders for different file types - Including additional file extensions - Modifying the folder names to match your preferences</p>
<p>Simply edit the script and add or modify the relevant lines in the code.</p>
<p>That’s it! You now have a powerful tool to keep your directories organized. No more messy folders! If you have any questions or suggestions for improvements, feel free to leave a comment below.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>Bash</category>
  <guid>https://focardozom.github.io/posts/2024-11-09-organize-this/organize-this.html</guid>
  <pubDate>Sat, 09 Nov 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How much do we trust a student’s answer about drug use?</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <dc:creator>Pablo Montero-Zamora</dc:creator>
  <link>https://focardozom.github.io/posts/2024-07-06-shiny-trust/Trust-live.html</link>
  <description><![CDATA[ 




<p>Confidence in students’ survey responses is often questioned when researching drug use, as it can be difficult to determine whether students are being truthful about their alcohol and other drug consumption. This concern is valid because self-reported drug use data can be influenced by various reporting biases. For instance, some students may underreport their drug use (i.e., deniers), while others may overreport (i.e., braggers). It is crucial to understand how these biases can impact the validity of research findings and to take steps to minimize their influence.</p>
<p>To examine this issue, we created a simulation-based app. This app can analyze the potential effects of deniers and braggers at different prevalence levels. It aims to improve understanding of how these biases affect research validity and to develop strategies to minimize their influence. By simulating different scenarios and analyzing the results, researchers can better understand the impact of deniers and braggers on research findings and take appropriate steps to mitigate these biases.</p>
<p>Let’s define some key concepts:</p>
<ul>
<li><strong>True prevalence:</strong> The true number of students using a drug divided by the total population.<br>
</li>
<li><strong>Deniers</strong>: Students who lie about not using drugs when they actually do.</li>
<li><strong>Braggers</strong>: Students who lie about using drugs when they actually don’t.</li>
</ul>
<p>We can use a student’s response to a drug use question to estimate the probability of their actual drug use behavior given hypothetical values of deniers, braggers, and true prevalence.</p>
<p>To model this probability, we can use <strong>Bayes’ theorem:</strong></p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP(A%20%7C%20B)%20=%20P(A)%20%5Cfrac%7BP(B%20%7C%20A)%7D%7BP(B)%7D%0A"></p>
<p>By replacing:</p>
<ul>
<li>(P(A|B)): The probability of drug use, given that the student says “yes” in the questionnaire.</li>
<li>(P(B|A)): The probability of saying “yes” in the questionnaire, given that the student has used drugs.</li>
<li>(P(A)): The probability of drug use (drug use prevalence).</li>
<li>(P(B)): The probability of saying “yes” in the questionnaire.</li>
</ul>
<p>We present simulated scenarios to understand how the trustworthiness of student responses changes based on the proportions of braggers, deniers, and the prevalence of alcohol use.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Definition of trust:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Btrust%7D%20=%20P(%5Ctext%7BActual%20Drug%20Use%7D%20%7C%20%5Ctext%7BReports%20Drug%20Use%7D)%0A"> The definition of <strong>trust</strong> we use is the conditional probability that a student who reports drug use in the survey is actually telling the truth, and it represents how much confidence we can have in the validity of a <em>yes</em> response from the students regarding their drug use.</p>
</div>
</div>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">Three Possible Scenarios</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Simulation Playground</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">Example Scenario</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<section id="three-possible-scenarios-1" class="level3">
<h3 class="anchored" data-anchor-id="three-possible-scenarios-1">Three Possible Scenarios</h3>
<p>We explore three different scenarios to understand how varying levels of dishonesty impact the trustworthiness of self-reported data on drug use among students. By adjusting the proportions of deniers (those who falsely deny drug use) and braggers (those who falsely claim drug use), we can observe the effects on data reliability in each scenario.</p>
<ol type="1">
<li><p><strong>Minimal Dishonesty</strong>: This scenario simulates a situation where almost nobody lies (5%) about their drug use, resulting in very low proportions of both deniers and braggers.</p></li>
<li><p><strong>High Dishonesty</strong>: This scenario represents an extreme where there are high proportions of both deniers and braggers (95%).</p></li>
<li><p><strong>Mixed Dishonesty</strong>: In this scenario, we simulate a situation with mixed levels of dishonesty, specifically with 20% deniers and 10% braggers.</p></li>
</ol>
<pre class="shinylive-r" data-engine="r"><code>#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 600
library(shiny)
library(ggplot2)
library(purrr)
library(tibble)
library(scales)
library(bslib)

ui &lt;- page_fixed(
    card(
        card_header("Low Dishonesty"),
        card_body(
            class = "lead container",
            plotOutput("plot_low")
        )
    ),
    card(
        card_header("High Dishonesty"),
        card_body(
            class = "lead container",
            plotOutput("plot_high")
        )
    ),
    card(
        card_header("Mixed Dishonesty"),
        card_body(
            class = "lead container",
            plotOutput("plot_mixed")
        )
    )
)

# Define server logic required to draw a histogram
server &lt;- function(input, output) {
    conf &lt;- function(prevalence, deniers, braggers) {
        tibble(
            trust = (prevalence * (1 - deniers)) /
                (
                    (prevalence * (1 - deniers)) + (braggers * (1 - prevalence))
                ),
            prevalence = prevalence,
            braggers = braggers,
            deniers = deniers,
            group = c(1)
        )
    }


    generate_df &lt;- function(deniers, braggers) {
        l &lt;- list(
            prevalence = seq(0.01, 0.99, 0.01),
            deniers = rep(deniers, 99),
            braggers = rep(braggers, 99)
        )
        pmap_dfr(l, conf)
    }

    df_low &lt;- reactive({
        generate_df(0.05, 0.05)
    })

    df_high &lt;- reactive({
        generate_df(0.95, 0.95)
    })

    df_mixed &lt;- reactive({
        generate_df(0.2, 0.1)
    })

    output$plot_low &lt;- renderPlot({
        ggplot(df_low(), aes(prevalence, trust, group = group)) +
            geom_line(size = 1.2, color = "#9966cc") +
            scale_y_continuous(breaks = seq(0, 1, 0.1), limits = c(0, 1)) +
            scale_x_continuous(labels = scales::percent, breaks = seq(0, 1, 0.15)) +
            labs(
                x = "Drug prevalence",
                y = "Trust"
            ) +
            theme_minimal()
    })

    output$plot_high &lt;- renderPlot({
        ggplot(df_high(), aes(prevalence, trust, group = group)) +
            geom_line(size = 1.2, color = "#9966cc") +
            scale_y_continuous(breaks = seq(0, 1, 0.1), limits = c(0, 1)) +
            scale_x_continuous(labels = scales::percent, breaks = seq(0, 1, 0.15)) +
            labs(
                x = "Drug prevalence",
                y = "Trust"
            ) +
            theme_minimal()
    })

    output$plot_mixed &lt;- renderPlot({
        ggplot(df_mixed(), aes(prevalence, trust, group = group)) +
            geom_line(size = 1.2, color = "#9966cc") +
            scale_y_continuous(breaks = seq(0, 1, 0.1), limits = c(0, 1)) +
            scale_x_continuous(labels = scales::percent, breaks = seq(0, 1, 0.15)) +
            labs(
                x = "Drug prevalence",
                y = "Trust"
            ) +
            theme_minimal()
    })
}

# Run the application
shinyApp(ui = ui, server = server)</code></pre>
</section>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p>You can explore different scenarios to understand the impact of dishonesty on self-reported data about drug use. Use the sliders to adjust the proportions of deniers and braggers, and observe how these changes affect the trustworthiness of the data across various scenarios.</p>
<pre class="shinylive-r" data-engine="r"><code>#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 600
library(shiny)
library(ggplot2)
library(purrr)
library(tibble)
library(scales)
library(bslib)

ui &lt;- page_fixed(
    layout_columns(
        card(sliderInput("deniers",
            "Deniers: say no, but yes",
            min = 0.001,
            max = 0.999,
            value = 0.001
        )),
        card(sliderInput("braggers",
            "Braggers: say yes, but no",
            min = 0.001,
            max = 0.999,
            value = 0.001
        ))
    ),
    card(
        plotOutput("plot_sim")
    )
)

# Define server logic required to draw a histogram
server &lt;- function(input, output) {
    conf &lt;- function(prevalence, deniers, braggers) {
        tibble(
            trust = (prevalence * (1 - deniers)) /
                ((prevalence * (1 - deniers)) + (braggers * (1 - prevalence))),
            prevalence = prevalence,
            braggers = braggers,
            deniers = deniers,
            group = c(1)
        )
    }

    df &lt;- reactive({
        l &lt;- list(
            prevalence = seq(0.01, 0.99, 0.01),
            deniers = rep(input$deniers, 99),
            braggers = rep(input$braggers, 99)
        )
        pmap_dfr(l, conf)
    })


    output$plot_sim &lt;- renderPlot({
        ggplot(df(), aes(prevalence, trust, group = group)) +
            geom_line(size = 1.2, color = "#9966cc") +
            scale_y_continuous(breaks = seq(0, 1, 0.1), limits = c(0, 1)) +
            scale_x_continuous(labels = scales::percent, breaks = seq(0, 1, 0.15)) +
            labs(
                x = "Drug prevalence",
                y = "Trust"
            ) +
            theme_minimal()
    })
}

# Run the application
shinyApp(ui = ui, server = server)</code></pre>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<p>Imagine a school with 100 students. Here’s how we can calculate the trust with different levels of deniers and braggers.</p>
<ol type="1">
<li><strong>True Prevalence of Drug Use</strong>: Let’s say the actual prevalence of drug use is 50%. So, 50 students actually use drugs.</li>
<li><strong>No Dishonesty</strong>: All students report honestly.</li>
<li><strong>Deniers (50%)</strong>: 50% of actual drug users deny using drugs.</li>
<li><strong>Braggers (5%)</strong>: 5% of non-drug users falsely claim to use drugs.</li>
</ol>
<section id="no-dishonesty" class="level4">
<h4 class="anchored" data-anchor-id="no-dishonesty">No Dishonesty</h4>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Actual Drug Users: 50</span></span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reported Drug Users: 50 (all 50 actual users report truthfully)</span></span>
<span id="cb3-3"></span>
<span id="cb3-4">prevalence <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb3-5">deniers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb3-6">braggers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb3-7"></span>
<span id="cb3-8">numerator <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> prevalence <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> deniers) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb3-9">denominator <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (prevalence <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> deniers) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> braggers <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> prevalence)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb3-10"></span>
<span id="cb3-11">trust <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> numerator <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> denominator</span>
<span id="cb3-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(trust, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"confidence"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1 confidence"</code></pre>
</div>
</div>
</section>
<section id="with-50-deniers" class="level4">
<h4 class="anchored" data-anchor-id="with-50-deniers">With 50% Deniers</h4>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Actual Drug Users: 50</span></span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Deniers (50%): 25 students who use drugs falsely report not using them.</span></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Truthful Reports from Drug Users: 25</span></span>
<span id="cb5-4"></span>
<span id="cb5-5">prevalence <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb5-6">deniers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb5-7">braggers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb5-8"></span>
<span id="cb5-9">numerator <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> prevalence <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> deniers) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb5-10">denominator <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (prevalence <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> deniers) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> braggers <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> prevalence)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb5-11"></span>
<span id="cb5-12">trust <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> numerator <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> denominator</span>
<span id="cb5-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(trust, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"confidence"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1 confidence"</code></pre>
</div>
</div>
<p>In this scenario, where there are 50% deniers and no braggers, the trust in a “yes” response is 100%. This means that if a student reports “yes” to using drugs, we can be completely confident that the student is indeed telling the truth, at a prevalence of 50%. This confidence arises because the 25 students who reported “yes” are all actual drug users, as there are no false positives. The presence of deniers, who are students that use drugs but falsely report “no,” does not affect the trust in a “yes” response. This is because these deniers simply reduce the number of “yes” responses from actual drug users, but the “yes” responses that do exist are entirely truthful.</p>
</section>
<section id="of-braggers" class="level4">
<h4 class="anchored" data-anchor-id="of-braggers">5% of Braggers</h4>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Actual Drug Users: 50</span></span>
<span id="cb7-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Truthful Reports from Drug Users: 50</span></span>
<span id="cb7-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Braggers (5%): 5% of 50 non-drug users falsely report using drugs: 5</span></span>
<span id="cb7-4"></span>
<span id="cb7-5">prevalence <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb7-6">deniers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb7-7">braggers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span></span>
<span id="cb7-8"></span>
<span id="cb7-9">numerator <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> prevalence <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> deniers) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb7-10">denominator <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (prevalence <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> deniers) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> braggers <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> prevalence)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb7-11"></span>
<span id="cb7-12">trust <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> numerator <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> denominator</span>
<span id="cb7-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(trust, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"confidence"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "0.952380952380952 confidence"</code></pre>
</div>
</div>
</section>
</div>
</div>
</div>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>This simulation app shows how varying levels of dishonesty among students impact the reliability of self-reported data on drug use. Whether dishonesty is minimal, high, or mixed, the accuracy of survey results is affected, underscoring the importance of addressing reporting biases in adolescent drug use research. By understanding the potential effects of underreporting and overreporting, researchers can develop more accurate data collection methods and analytical strategies, leading to more reliable findings. These insights are crucial for informing interventions aimed at addressing drug use among adolescents.</p>
</section>
<section id="some-recommendations-for-measuring-drug-use" class="level1">
<h1>Some Recommendations for Measuring Drug Use</h1>
<ol type="1">
<li><p><strong>Include a Fictitious Drug in the Report</strong>: Adding a fictitious drug to the questionnaire can help identify and exclude responses from students who overreport (braggers).</p></li>
<li><p><strong>Guarantee Anonymity</strong>: Ensure students’ anonymity to encourage honest reporting. For example, do not collect data in the presence of familiar adults, which can create pressure and lead to dishonest answers.</p></li>
<li><p><strong>Educate on the Importance of Honesty</strong>: Explain the benefits of honest reporting to students. Emphasize how accurate data can help create better policies and interventions that benefit their community.</p></li>
<li><p><strong>Compare with Other Reports</strong>: Cross-check your prevalence data with other studies and reports. This can help identify discrepancies and validate the accuracy of your findings.</p></li>
<li><p><strong>Conduct Consistency Analysis</strong>: Analyze the consistency of responses to ensure coherence. For example, lifetime prevalence should be consistent with last month’s prevalence; it is illogical for someone to report drug use in the past month but not in their lifetime.</p></li>
<li><p><strong>Randomized Response Techniques</strong>: Use randomized response techniques to increase the likelihood of truthful responses. This method allows respondents to answer sensitive questions while maintaining privacy, reducing the fear of disclosure.</p></li>
<li><p><strong>Use Validated Questionnaires</strong>: Employ well-validated and standardized questionnaires that have been proven to minimize bias and elicit more accurate responses.</p></li>
<li><p><strong>Provide Confidential Environments</strong>: Create a confidential and comfortable environment for survey administration. Ensure that students feel safe and that their responses will not be traced back to them.</p></li>
<li><p><strong>Implement Follow-Up Questions</strong>: Include follow-up questions that cross-validate initial responses. Inconsistencies in follow-up responses can highlight potential dishonesty.</p></li>
<li><p><strong>Train Data Collectors</strong>: Train those administering the surveys to build rapport with students and encourage honesty. Skilled data collectors can significantly impact the quality of the data collected.</p></li>
<li><p><strong>Pilot Testing</strong>: Conduct pilot testing of the survey to identify potential issues and biases before the full-scale implementation. This allows for adjustments to improve accuracy.</p></li>
<li><p><strong>Longitudinal Studies</strong>: Where possible, use longitudinal studies to track changes over time, which can help validate self-reported data through consistency checks over multiple points in time.</p></li>
</ol>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>R</category>
  <category>Prevention Science</category>
  <guid>https://focardozom.github.io/posts/2024-07-06-shiny-trust/Trust-live.html</guid>
  <pubDate>Sat, 06 Jul 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to Create a Bash Script to Extract Bibliographic References from DOIs</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <link>https://focardozom.github.io/posts/2024-04-06-getref/getref.html</link>
  <description><![CDATA[ 




<p>Dealing with references can feel like a real headache. However, recently I came across a fantastic solution by <a href="https://twitter.com/rlmcelreath">Richard Mclearn</a>. This blog post is all about making your life easier with a simple tutorial for Mac users. I’ll show you how to set up a <code>getref</code> function that quickly grabs bibliographic information in BibTeX format from a DOI or a DOI URL.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://focardozom.github.io/posts/2024-04-06-getref/x-image.png" class="img-fluid figure-img" style="width:70.0%"></p>
<figcaption><a href="https://twitter.com/rlmcelreath/status/1767812801404223535">source</a></figcaption>
</figure>
</div>
<section id="step-by-step-guide" class="level1">
<h1>Step-by-Step Guide</h1>
<section id="step-1-prepare-your-workspace" class="level2">
<h2 class="anchored" data-anchor-id="step-1-prepare-your-workspace">Step 1: Prepare Your Workspace</h2>
<p>First, we need to create a dedicated folder to store your <code>getref</code> function. This not only helps in organizing your scripts but also in managing your PATH environment efficiently.</p>
<p>You can create the folder following this steps:</p>
<p>1.1. Open the terminal (cmd+space then type terminal).</p>
<p>1.2. Create a new folder named bin in your home directory:</p>
<p>type</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">mkdir <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>bin</span></code></pre></div></div>
</div>
<p>and press return in your keyboard.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>I created the bin folder in my home directory. It will make it easier to access the script later on.</p>
</div>
</div>
</section>
<section id="step-2-move-to-the-bin-folder" class="level2">
<h2 class="anchored" data-anchor-id="step-2-move-to-the-bin-folder">Step 2: Move to the bin Folder</h2>
<p>Navigate to the newly created folder by typing</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">cd <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>bin</span></code></pre></div></div>
</div>
</section>
<section id="step-3-create-the-script-file" class="level2">
<h2 class="anchored" data-anchor-id="step-3-create-the-script-file">Step 3: Create the Script File</h2>
<p>Create a new file named getref by typing</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">touch getref</span></code></pre></div></div>
</div>
<p>This file will be our script.</p>
</section>
<section id="step-4-add-the-script-code" class="level2">
<h2 class="anchored" data-anchor-id="step-4-add-the-script-code">Step 4: Add the Script Code</h2>
<p>Open the <code>getref</code> file in your favorite text editor. For simplicity, you can use <a href="https://www.nano-editor.org">Nano</a>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">nano getref</span></code></pre></div></div>
</div>
<p>In the inside of the file, copy and paste the following code snippet. This script checks if a DOI or DOI URL is provided as an argument and then fetches the bibliographic information in BibTeX format.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/bin/bash</span></span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check if an argument is provided</span></span>
<span id="cb5-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> [ <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"$#"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>ne <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> ]; then</span>
<span id="cb5-4">    echo <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Usage: $0 &lt;DOI or DOI URL&gt;"</span></span>
<span id="cb5-5">    exit <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb5-6">fi</span>
<span id="cb5-7"></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Determine if the input is a DOI URL or just a DOI number</span></span>
<span id="cb5-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> [[ <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"$1"</span> <span class="ot" style="color: #003B4F;
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>https<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> ]]; then</span>
<span id="cb5-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract the DOI number from the URL</span></span>
<span id="cb5-11">    DOI<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">$</span>(echo <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"$1"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> sed <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s|https://doi.org/||'</span>)</span>
<span id="cb5-12"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span></span>
<span id="cb5-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Assume the input is just a DOI number</span></span>
<span id="cb5-14">    DOI<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="cb5-15">fi</span>
<span id="cb5-16"></span>
<span id="cb5-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract and print the reference</span></span>
<span id="cb5-18">curl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>LH <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Accept: application/x-bibtex"</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://doi.org/$DOI"</span></span></code></pre></div></div>
</div>
</section>
<section id="step-5-make-the-script-executable" class="level2">
<h2 class="anchored" data-anchor-id="step-5-make-the-script-executable">Step 5: Make the Script Executable</h2>
<p>Ensure the script can be executed by running in the terminal</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">chmod <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>x <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>bin<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>getref</span></code></pre></div></div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Notice that I used the path to my file, which is located in the bin folder in my home directory.</p>
</div>
</div>
</section>
<section id="step-6-update-your-shell-configuration" class="level2">
<h2 class="anchored" data-anchor-id="step-6-update-your-shell-configuration">Step 6: Update Your Shell Configuration</h2>
<p>Now, Open your shell configuration file (.zshrc for Zsh or .bashrc for Bash) using a text editor, e.g.,</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">nano <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>.zshrc</span></code></pre></div></div>
</div>
<p>You will need to add <strong>~/bin</strong> to your <strong>PATH</strong>. To do this, add the following line at the end of the file:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">export PATH<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;">"$HOME/bin:$PATH"</span></span></code></pre></div></div>
</div>
<p>Save and close the file.</p>
</section>
<section id="step-7-apply-the-changes" class="level2">
<h2 class="anchored" data-anchor-id="step-7-apply-the-changes">Step 7: Apply the Changes</h2>
<p>Apply the changes to your current session by sourcing your configuration file:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">source <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>.zshrc</span></code></pre></div></div>
</div>
<p>for Zsh</p>
<p>or</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">source <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>.bashrc</span></code></pre></div></div>
</div>
<p>for Bash.</p>
</section>
</section>
<section id="how-to-use-your-new-script" class="level1">
<h1>How to Use Your New Script</h1>
<p>To use your script, simply type <code>getref</code> in your termina followed by a DOI or a DOI URL. For example:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">getref <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.1080</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;">09687637</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2023.2167649</span></span></code></pre></div></div>
</div>
<p>or</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">getref https<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>doi.org<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.1080</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;">09687637</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2023.2167649</span></span></code></pre></div></div>
</div>
<p>The script will fetch and display the bibliographic information in BibTeX format.</p>
<p><strong>Done!</strong> This is a handy tool that simplifies the process of fetching bibliographic data. I hope you enjoy it. If you have any questions or suggestions, feel free to leave a comment below.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>Bash</category>
  <guid>https://focardozom.github.io/posts/2024-04-06-getref/getref.html</guid>
  <pubDate>Sat, 06 Apr 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Balancing Data in Machine Learning: When Good Intentions Meet Slippery Slopes</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <link>https://focardozom.github.io/posts/2024-03-12-imbalance/imbalance_ci.html</link>
  <description><![CDATA[ 




<p>Have you ever attended a party where the host tries so hard to make everyone happy that they end up achieving the exact opposite? That’s somewhat like what happens when we overenthusiastically balance data in machine learning. The intention is noble, but the outcome? Not always a hit.</p>
<p>In the debate over whether to balance data or not <span class="citation" data-cites="yotam2022 Goorbergh2022">(Elor &amp; Averbuch-Elor, 2022; Goorbergh et al., 2022)</span>, simulations often act as referees, showcasing results that typically advise against manipulating data to achieve balance. But let’s be honest, can we trust everything that comes out of a simulation? Shouldn’t we explore different angles to grasp why data balancing might not always be the wisest course of action?</p>
<section id="rubin-to-the-rescue-or-not" class="level3">
<h3 class="anchored" data-anchor-id="rubin-to-the-rescue-or-not">Rubin to the Rescue… Or Not?</h3>
<p>Let’s turn the spotlight onto a different perspective. Picture yourself in an examination hall, faced with a challenging test. In a moment of desperation, you decide to copy answers from the student next to you. However, you have no evidence they’re the top student. So, you start sizing them up. Glasses? Check. Philosophical expressions? Check. Or maybe they’re a math wiz because, obviously, that’s the universal sign of genius, right?</p>
<p>As your anxiety mounts, something extraordinary happens: a fairy godmother appears, not with a magic wand, but with important message: she whispers your neighbor’s grade. Bingo! With newfound confidence, you embark on your note-transcribing quest.</p>
<p>This little adventure parallels adjusting your data based on observed outcomes. Before the fairy godmother’s revelation, you’re in the dark, much like when we lack insight into the observed outcomes. You might make assumptions (glasses mean brains, right?), but that’s shaky ground and we might just be copying the wrong answers, mistaking luck for wisdom.</p>
<p>And that’s where Rubin’s thinking comes in. It’s not like the fairy godmother, it is much like venturing into an enchanted forest where paths diverge and outcomes remain uncertain.</p>
<p>The Rubin causal model, also known as the potential outcomes framework, is a way to think about cause and effect in a systematic manner. Imagine every time you make a decision, there are two parallel universes: one where you made one choice and another where you made a different one. The Rubin model compares what happens in these two universes to understand the true impact of that choice. It’s like having a twin who makes different decisions, and by looking at both your lives, you can see the effects of those decisions. In practice, since we can’t see parallel universes, this model uses statistics to estimate what would have happened in the alternate scenario, helping to isolate the cause of an outcome from all the noise. In this context, balancing data is contrary to observing divergence outcomes across parallel universes; it artificially aligns one universe to resemble the other, potentially obscuring causal relationships rather than revealing them.</p>
<p>But wait, as in fairy tales, there’s a twist! Machine learning loves predictions like cats love cardboard boxes. And in this arena, confounders could be seen as those annoying but sometimes helpful pieces of furniture. Sure, they make your model look cluttered, but they might just help it predict better, and we have the tools to deal with the ‘cluttering’: ’In the force of data, too much bias, the clarity it clouds; too much variance, the truth it obscures.</p>
<p>In essence, while machine learning and causal reasoning might seem like strange bedfellows, they both aim to make sense of our chaotic, data-driven world. Perhaps it’s time we invite them both to the party, have them shake hands, and work together. After all, the best parties are those where everyone gets along, right?</p>



</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-yotam2022" class="csl-entry">
Elor, Y., &amp; Averbuch-Elor, H. (2022). To SMOTE, or not to SMOTE? <em>CoRR</em>, <em>abs/2201.08528</em>. <a href="https://arxiv.org/abs/2201.08528">https://arxiv.org/abs/2201.08528</a>
</div>
<div id="ref-Goorbergh2022" class="csl-entry">
Goorbergh, R. van den, Smeden, M. van, Timmerman, D., &amp; Van Calster, B. (2022). <span class="nocase">The harm of class imbalance corrections for risk prediction models: illustration and simulation using logistic regression</span>. <em>Journal of the American Medical Informatics Association</em>, <em>29</em>(9), 1525–1534. <a href="https://doi.org/10.1093/jamia/ocac093">https://doi.org/10.1093/jamia/ocac093</a>
</div>
</div></section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>Machine Learning</category>
  <category>Causal Inference</category>
  <guid>https://focardozom.github.io/posts/2024-03-12-imbalance/imbalance_ci.html</guid>
  <pubDate>Tue, 12 Mar 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Health Literacy: A Double-Edged Sword?</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <link>https://focardozom.github.io/posts/2024-02-03-health_literacy/health_literacy.html</link>
  <description><![CDATA[ 




<p>In prevention science, information is often considered the cornerstone of any public health initiative. For example, the Health Belief Model, one of the most commonly employed frameworks in preventive interventions, posits that well-informed individuals are more likely to engage in preventive health behaviors <span class="citation" data-cites="rosenstockHealthBeliefModel2000">(Rosenstock, 2000)</span>. This model suggests that people’s perceptions of susceptibility, severity, benefits, and barriers related to a specific health issue can predict their behavior. Consequently, interventions that provide information—such as highlighting the benefits of a particular action—tend to increase people’s engagement in expected healthy behaviors. During the pandemic, governments and institutions made significant efforts to raise public awareness about various important concepts related to public health. This widespread dissemination of information led to a general increase in health literacy, something previously unseen in past years. As a result, conversations within families began to include public health concepts like R0, infection rates, and social distancing, among others.</p>
<p>While this mass education was intended to be beneficial, it also presented challenges. For instance, some people developed an overconfidence in their own expertise, leading to misunderstandings among the general population. The abundance of information gave rise to a wave of ‘influencers’ who negatively impacted pandemic response efforts. One example was the promotion of the medication ‘ivermectin’ by social media influencers as a preventive measure against COVID-19 complications, despite a lack of evidence supporting its efficacy <span class="citation" data-cites="Bryant2021">(Bryant et al., 2021)</span>. Furthermore, governments imposed non-medical interventions based on a flawed understanding of the crisis. Measures such as cleaning the soles of shoes and sanitizing streets and commercial spaces were enforced as precautions. However, these actions were later found to be ineffective, leading the general population to question the assumptions underlying interventions aimed at ‘flattening the curve.’ Politicians capitalized on these misunderstandings to connect with a broad audience, often promoting agendas not aligned with public health goals. This politicization of the pandemic subsequently affected compliance with other measures, such as mask-wearing and vaccination. The issue became so politicized that people could even identify others’ political affiliations based on their mask usage or vaccination status, a phenomenon unprecedented in modern history.</p>
<p>This presents a ‘double-edged sword’ dilemma, where the dissemination of information can be both beneficial and detrimental. On the positive side, both theory and practice suggest that well-informed populations are more likely to adopt healthier behaviors. Conversely, public health concepts can be weaponized to advance specific political agendas that may conflict with general health goals. As a result, prevention scientists face the challenge of finding the right balance between promoting health literacy and mitigating its potential misuse. These reflections lead me to question the idea that ‘more information is always better.’ At times, an abundance of information can have unintended consequences, rendering individuals more susceptible to the agendas of specific groups.</p>
<p>In my experience in the field, I’ve noticed that people in communities interpret risk assessments about adolescent behavior in one of two ways. Some see the data as validation of their existing views, often negative, about young people. For example, if the data shows high rates of alcohol use among teens, these individuals may push for punitive measures like curfews. Conversely, others use the same data to advocate for more preventive programs in families and schools, aiming to better engage and support adolescents. This divergence prompts a critical question: What is our responsibility as disseminators of this information? Or, how can we strike the “right balance” in promoting health literacy to ensure it benefits the community?</p>
<p>As a prevention scientist, I find hard to ensure the responsible use of data and insights generated from scientific research. Our field faces a significant gap in effective communication tools, leading many of us to navigate the complexities of conveying scientific concepts through trial and error. The COVID-19 pandemic showed us the detrimental impact of information misuse, particularly in the flawed implementation of safety measures that could have saved lives. Meanwhile, data collection systems have evolved to become increasingly omnipresent, obligatory, and comprehensive. Information about our health, activities, and personal history is now routinely gathered during medical consultations and intervention programs. Though stored with the assurance of benefiting us, there are no guarantees.</p>
<p>I’d like to leave some questions open for future discussion. Does expanding a population’s health vocabulary not only empower them but also make them more vulnerable to misinformation? Could the introduction of scientific terms into public discourse, such as ‘flatten the curve,’ potentially give institutions more control over individuals? If so, what strategies should we employ in communicating and disseminating these concepts to prevent institutional abuse?</p>
<section id="references" class="level1">




</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" data-line-spacing="2">
<div id="ref-Bryant2021" class="csl-entry">
Bryant, A., Lawrie, T., Dowswell, T., Fordham, E., Mitchell, S., Hill, S., &amp; Tham, T. (2021). <em>Ivermectin for prevention and treatment of COVID-19 infection: A systematic review and meta-analysis</em>. <a href="https://doi.org/10.21203/rs.3.rs-317485/v1">https://doi.org/10.21203/rs.3.rs-317485/v1</a>
</div>
<div id="ref-rosenstockHealthBeliefModel2000" class="csl-entry">
Rosenstock, I. M. (2000). <em>Health <span>Belief Model</span></em> (pp. 78–80). <span>Oxford University Press</span>. <a href="https://doi.org/10.1037/10519-035">https://doi.org/10.1037/10519-035</a>
</div>
</div></section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>Prevention Science</category>
  <guid>https://focardozom.github.io/posts/2024-02-03-health_literacy/health_literacy.html</guid>
  <pubDate>Sat, 03 Feb 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>To Explain or to Predict</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <link>https://focardozom.github.io/posts/2024-01-27-explain/explain.html</link>
  <description><![CDATA[ 




<p>There is a current debate regarding the appropriate circumstances to employ machine learning (ML) over traditional statistics for statistical modeling. Some traditionalists assert that ML often represents nothing more than a rebrand version of existing methods, occasionally rendering it unnecessary. Their primary criticism is that while ML can be advantageous in some circumstances, it frequently operates as a “black box”, offering limited transparency and scant insights into the underlying phenomena. On the other hand, ML advocates argue that traditional statistics often fail to grasp the intricate complexities of real-world data, positioning ML as the superior option. Both arguments, however, might be missing the essence of the issue. This debate is not merely about the characteristics of the tools used but more about the foundational objectives of scientific research.</p>
<p>Scientific research predominantly deals with questions to respond ‘why’ and ‘when’ something occurs. When scientists seek explanations, they are interested in the primary causes or influencers of observed events. Predictive endeavors, conversely, are inherently forward-looking, valuing future forecasts over causal understanding. While no single statistical method can directly answer causal questions, prediction can be approached using both traditional regression techniques and ML models. Thus, the real dilemma emerges in choosing methods for predictive situations, as tools for causal explanations are beyond mere statistical techniques.</p>
<p>When predictive modeling is chosen, machine learning often proves more advantageous than traditional statistical models. These advantages stem from distinct characteristics inherent in the construction of ML models. Primarily, this is because the construction of machine learning models is guided by out-of-sample metrics, tailored to maximize the model’s ability to generalize to unseen data. In essence, machine learning models seek a balance between variance and bias. Conversely, traditional statistical methods focus on hypothesis testing to draw conclusions from models, often overlooking concerns such as overfitting. This oversight includes practices like using the same dataset for both training and testing, and a lack of emphasis on metrics such as the Akaike Information Criterion (AIC), Bayesian Information Criterion (BIC), or adjusted R², which are crucial for evaluating model complexity. It should be mentioned that this traditional approach is not inherently wrong, but it is not the most appropriate for predictive modeling. Instead, these methods are excellent tools for descriptive analysis and most research in public health is descriptive (unfortunately?).</p>
<p>While machine learning models offer advantages, they also present several challenges. They frequently require extensive computational resources and, in numerous cases, cannot offer insights about underlying phenomena. Such shortcomings can be significant limitations in various scientific contexts. In these scenarios, traditional statistics often emerge as the preferred approach, due to their ability to summarize data relationships more straightforwardly.</p>
<p>In prevention science, differentiating between prediction and explanation is crucial. However, this distinction is often overlooked, resulting in innovative missteps. In the absence of clear guidelines, practitioners may opt for inappropriate tools, leading to confusion between predictive outcomes and causal relationships. For example, when assessing interventions, programs are typically evaluated with a focus on their theoretical foundations and the program’s underlying mechanisms, necessitating explanatory modeling. Yet, in practice, the models often tend to be predictive (or descriptive), thereby overlooking the methods essential for explanatory analysis.</p>
<p>In summary, the distinction between explanation and prediction is not merely about the methods or algorithms employed but centers on the overarching purpose of the research. While both are fundamental to scientific research, their practical applications and tools, especially in prevention science, can differ significantly.</p>



<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>Prevention Science</category>
  <category>Machine Learning</category>
  <guid>https://focardozom.github.io/posts/2024-01-27-explain/explain.html</guid>
  <pubDate>Sat, 27 Jan 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Balancing Efficiency and Flexibility: The Challenges of Over-Optimization in the Multiphase Optimization Strategy (MOST)</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <link>https://focardozom.github.io/posts/2023-12-03-most/most.html</link>
  <description><![CDATA[ 




<p>The <a href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2062525/" target="_blank">Multiphase Optimization Strategy (MOST)</a> is an approach to intervention development in prevention science. MOST is structured around three phases: the Screening Phase, where intervention components are initially selected or eliminated based on efficacy; the Refining Phase, which is dedicated to ‘calibrating’ these elements to determine their optimal levels and combinations; and the Confirming Phase, during which the refined intervention is subjected to rigorous evaluation via a conventional randomized controlled trial. Although this strategy is designed to optimize intervention development, it faces a significant challenge: the possibility that excessive optimization could be counterproductive.</p>
<p>Although it may seem counterintuitive, pursuing efficiency can sometimes lead to inferior outcomes. This paradox is named <a href="https://en.wikipedia.org/wiki/Goodhart%27s_law">Goodhart’s Law</a>, which suggests that once a measure becomes a target, it can end up distorting the very outcome it was intended to assess. This happens when individuals or groups start to ‘game the system’ to meet these targets, thereby neglecting the real improvements these measures are designed to track.</p>
<p>Regarding MOST, the issue does not always stem from intentional misbehavior or manipulation; rather, it arises from an overemphasis on efficiency that can result in an excessive focus on optimization, which may neglect components that benefit achieving the outcomes of the intervention. To illustrate how an excessive focus on optimization can lead to worse outcomes, consider students who are overly prepared to excel at standardized tests, which may cause them to neglect to develop a wider range of skills that are crucial for overall life success. Similarly, overly incentivizing researchers with bonuses can encourage fraudulent activities and undermine the integrity of scientific research.</p>
<p>Therefore, within MOST, over-selecting components may lead to overfitting in interventions, which is counterproductive given the need for interventions suitable for diverse populations. Moreover, there is a risk that prioritizing intervention components based solely on their individual ‘effectiveness’ can overlook the principle that the total impact may not always equal the sum of its parts. These two aspects represent fundamental challenges inherent in the MOST methodology.</p>
<p>To address this challenge, intervention developers must take into consideration an appropriate balance between the efficiency of components and the variability of their effects under diverse conditions. In addressing this, it is crucial to acknowledge that samples are less diverse than populations, that other factors such as implementation components also contribute to program effectiveness, and, overall, that interventions may not be universally beneficial across all population groups.</p>
<p>In conclusion, while MOST is a promising framework for intervention development, it is vital to recognize that it is not perfect. The applicability of the MOST methodology is not universal across all interventions, and the advantages of efficiency must be carefully weighed against the risks of overfitting.</p>



<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>Prevention Science</category>
  <guid>https://focardozom.github.io/posts/2023-12-03-most/most.html</guid>
  <pubDate>Sun, 03 Dec 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>The Chessboard and the Wise Courtier</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <link>https://focardozom.github.io/posts/2023-08-01-while/while.html</link>
  <description><![CDATA[ 




<section id="the-story" class="level1">
<h1>The Story</h1>
<p>Once upon a time in ancient India, a wise courtier presented a challenge to his king using a chessboard and some grains of rice. He placed a single grain of rice on the first square of the chessboard, then asked the king to double the number of grains on each subsequent square until all 64 squares were filled. Initially, the king thought that the result would be a meaningless amount of rice, but he soon realized the astronomical sum this would amount to.</p>
<div class="columns">
<div class="column" style="width:40%;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://focardozom.github.io/posts/2023-08-01-while/img/img.png" class="img-fluid figure-img"></p>
<figcaption>“Generated with Midjourney”</figcaption>
</figure>
</div>
</div><div class="column" style="width:5%;">

</div><div class="column" style="width:55%;">
<p>This story illustrates the power of exponential growth, a mathematical concept that can describe certain patterns in nature and society. Exponential growth occurs when the rate of growth is proportional to the current value, leading to increasingly rapid growth over time. This type of growth can be found in many real-world phenomena, including the spread of disease, and the adoption of new technologies.</p>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>Pandemics</strong>: In the early stages of a pandemic, such as COVID-19, the number of infections may double within a fixed time period, leading to an exponential increase.</p>
<p><strong>Social Media and Viral Content</strong>: Posts or videos that “go viral” on social media platforms can exhibit exponential growth. A piece of content might be shared by a few people initially, but if each person who sees it shares it with others, the total number of views can grow exponentially.</p>
<p><strong>Environmental Concerns</strong>: Exponential growth can also have negative consequences, such as in the spread of invasive species or the consumption of non-renewable resources, leading to potential environmental damage.</p>
</div>
</div>
<p>I will use the story of chess to demonstrate how quickly the number of grains grows when it doubles over the course of only 64 steps. I will also provide <code>Python</code> code to calculate the total number of grains on the chessboard.</p>
</section>
<section id="python-code-to-compute-the-total-number-of-grains" class="level1">
<h1>Python Code to Compute the Total Number of Grains</h1>
<p>Imagine you have a bag of grains, and you want to fill a chessboard with them. You start with one grain on the first square, then double the number of grains on each subsequent square until you reach the 64th square. You can think of this process like a snowball rolling down a hill, getting bigger and bigger as it goes.</p>
<p>In Python, we can use something called a <code>while loop</code> to replicate this process. It’s like having a robot that puts the grains on the squares for you, following your exact instructions: <em>“Start with one grain, then double the number, and keep going until you fill all 64 squares.”</em></p>
<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>While Loop
</div>
</div>
<div class="callout-body-container callout-body">
<p>A while loop continues to execute the block of code as long as a specified condition is true. It checks the condition before each iteration, and if the condition is false, it exits the loop.</p>
</div>
</div>
<p>Now, let’s think about a different way to approach the task using what’s called a <code>for</code> loop. Imagine instead that you have 64 small containers, each representing a square on the chessboard, and you know exactly how many grains should go in each one. Starting with the first container, you fill it with one grain, then move to the next one and fill it with double the grains of the previous container, and so on, until you reach the 64th container.</p>
<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>For Loop
</div>
</div>
<div class="callout-body-container callout-body">
<p>A <code>for loop</code> works like this staircase-building process. You tell the computer exactly how many times to repeat something, and it follows your instructions step by step. Unlike the snowball rolling down the hill, which might take an unpredictable path, building the staircase (or using a “for loop”) is more controlled and precise.</p>
</div>
</div>
</section>
<section id="while-loop-in-python" class="level1">
<h1>While Loop in python</h1>
<p>Lets to compute that number of grains using the <code>while</code> loop.</p>
<div class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialize variables</span></span>
<span id="cb1-2">square_number <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The number of the current square</span></span>
<span id="cb1-3">grains_on_square <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The number of grains on the current square</span></span>
<span id="cb1-4">total_grains <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</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;"># The total number of grains so far</span></span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Loop through each square on the chessboard</span></span>
<span id="cb1-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> square_number <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>:</span>
<span id="cb1-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add the number of grains on the current square to the total</span></span>
<span id="cb1-9">    total_grains <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> grains_on_square</span>
<span id="cb1-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Double the number of grains on the current square for the next square</span></span>
<span id="cb1-11">    grains_on_square <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Move to the next square</span></span>
<span id="cb1-13">    square_number <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-14"></span>
<span id="cb1-15"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"The total number of grains on the chessboard is: </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>total_grains<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:,}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>The total number of grains on the chessboard is: 
 18,446,744,073,709,551,615</code></pre>
</div>
</div>
<p>So, it’s a big number. Here are some interesting facts about this number:</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li>This number in words is <em>eighteen quintillion, four hundred forty-six quadrillion, seven hundred forty-four trillion, seventy-three billion, seven hundred nine million, five hundred fifty-one thousand, six hundred fifteen</em>, or approximately 18 quintillion, or 18 billion billion.</li>
<li>In seconds, it is equal to 293,274,701,009 years, 3 weeks, 3 days, 15 hours, 30 minutes, 7 seconds.</li>
<li>If each grain of rice were 0.05 grams in weight, then the total weight of rice would be 922337203685.48 tons.</li>
</ul>
</div>
</div>
<div class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Total number of grains as computed earlier</span></span>
<span id="cb3-2">total_grains <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18446744073709551615</span></span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Weight of a single grain in grams</span></span>
<span id="cb3-5">weight_per_grain <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span></span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute the total weight in grams</span></span>
<span id="cb3-8">total_weight_grams <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> total_grains <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> weight_per_grain</span>
<span id="cb3-9"></span>
<span id="cb3-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert to kilograms</span></span>
<span id="cb3-11">total_weight_kilograms <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> total_weight_grams <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb3-12"></span>
<span id="cb3-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert to metric tons</span></span>
<span id="cb3-14">total_weight_metric_tons <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> total_weight_kilograms <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb3-15"></span>
<span id="cb3-16"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"The total weight of the rice in grams is </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>total_weight_grams<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> grams"</span>)</span>
<span id="cb3-17"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"The total weight of the rice in kilograms is </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>total_weight_kilograms<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> kilograms"</span>)</span>
<span id="cb3-18"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"The total weight of the rice in metric tons is </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>total_weight_metric_tons<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> metric tons"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>The total weight of the rice in grams is 922337203685477632.00 grams
The total weight of the rice in kilograms is 922337203685477.62 kilograms
The total weight of the rice in metric tons is 922337203685.48 metric tons</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled" title="Tip with Title">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Tip with Title
</div>
</div>
<div class="callout-body-container callout-body">
<p>Find more facts here:</p>
<p><a href="&quot;https://numbermatics.com/n/18446744073709551615/?utm_source=shared&quot;" target="_blank">“18446744073709551615 - Facts”</a></p>
</div>
</div>
<p>Let’s finish this post by comparing the <code>while</code> loop with the <code>for</code> loop.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 12%">
<col style="width: 48%">
<col style="width: 39%">
</colgroup>
<thead>
<tr class="header">
<th>Aspect</th>
<th>While Loop</th>
<th>For Loop</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Condition vs Sequence</td>
<td>Uses a condition that can be any logical expression, continues iterating as long as the condition is true.</td>
<td>Iterates over a sequence of values, executing the code block once for each value.</td>
</tr>
<tr class="even">
<td>Control Over Iteration</td>
<td>You have full control over how many times the loop iterates by manipulating the condition.</td>
<td>The number of iterations is defined by the length of the sequence.</td>
</tr>
<tr class="odd">
<td>Potential for Infinite Loop</td>
<td>There’s a risk of creating an infinite loop if the condition never becomes false.</td>
<td>Generally not at risk for infinite loops, as it iterates over a finite sequence.</td>
</tr>
<tr class="even">
<td>Use Cases</td>
<td>Often used when you don’t know how many times you’ll need to iterate.</td>
<td>Used when you want to iterate a known number of times or over a specific sequence.</td>
</tr>
</tbody>
</table>
<section id="conclusion" class="level4">
<h4 class="anchored" data-anchor-id="conclusion">Conclusion</h4>
<p>The chessboard problem illustrates how quickly numbers can grow when they double with each step. By using a <code>while</code> loop in Python, we’ve computed this enormous figure, providing insight into the power of exponential growth.</p>
<p>This example shows how Python can be used to solve problems that would be otherwise challenging to compute manually. Feel free to explore the code further, perhaps by changing the number of squares or the initial number of grains. What other intriguing patterns might you discover?</p>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>Python</category>
  <category>Chess</category>
  <guid>https://focardozom.github.io/posts/2023-08-01-while/while.html</guid>
  <pubDate>Wed, 09 Aug 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Computing Sensitivity and Specificity Using collect_metrics</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <dc:creator>Catalina Cañizares</dc:creator>
  <link>https://focardozom.github.io/posts/2023-07-22-collect_metrics/collect_metrics.html</link>
  <description><![CDATA[ 




<p>(in progress)</p>
<p>In the last <a href="https://focardozom.github.io/posts/2023-07-15-yarstick/2023-07-15.html">post</a>, we discussed computing sensitivity and specificity using the <code>sensitivity()</code> and <code>specificity()</code> functions from the <code>yardstick</code> package. In this post, we’ll demonstrate how to compute these metrics using the <code>collect_metrics</code> function from the <code>tidymodels</code> package.</p>
<section id="dataset" class="level3">
<h3 class="anchored" data-anchor-id="dataset">Dataset</h3>
<p>First, let’s recreate the confusion matrix from the previous post.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load tidyverse package</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gt)         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load gt package</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gtsummary)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load gtsummary package</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a tibble with the confusion matrix data</span></span>
<span id="cb1-6">confusion_matrix_long <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb1-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Predicted Class'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</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;">'1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'0'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'0'</span>),</span>
<span id="cb1-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'True Class'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</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;">'True 1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'True 0'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'True 1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'True 0'</span>),</span>
<span id="cb1-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Count'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</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;">35</span>)</span>
<span id="cb1-10">)</span>
<span id="cb1-11"></span>
<span id="cb1-12">confusion_matrix_long <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;">gt</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div id="naxpadeuko" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#naxpadeuko table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#naxpadeuko thead, #naxpadeuko tbody, #naxpadeuko tfoot, #naxpadeuko tr, #naxpadeuko td, #naxpadeuko th {
  border-style: none;
}

#naxpadeuko p {
  margin: 0;
  padding: 0;
}

#naxpadeuko .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#naxpadeuko .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#naxpadeuko .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#naxpadeuko .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#naxpadeuko .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#naxpadeuko .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#naxpadeuko .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#naxpadeuko .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#naxpadeuko .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#naxpadeuko .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#naxpadeuko .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#naxpadeuko .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#naxpadeuko .gt_spanner_row {
  border-bottom-style: hidden;
}

#naxpadeuko .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#naxpadeuko .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#naxpadeuko .gt_from_md > :first-child {
  margin-top: 0;
}

#naxpadeuko .gt_from_md > :last-child {
  margin-bottom: 0;
}

#naxpadeuko .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#naxpadeuko .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#naxpadeuko .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#naxpadeuko .gt_row_group_first td {
  border-top-width: 2px;
}

#naxpadeuko .gt_row_group_first th {
  border-top-width: 2px;
}

#naxpadeuko .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#naxpadeuko .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#naxpadeuko .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#naxpadeuko .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#naxpadeuko .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#naxpadeuko .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#naxpadeuko .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#naxpadeuko .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#naxpadeuko .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#naxpadeuko .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#naxpadeuko .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#naxpadeuko .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#naxpadeuko .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#naxpadeuko .gt_left {
  text-align: left;
}

#naxpadeuko .gt_center {
  text-align: center;
}

#naxpadeuko .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#naxpadeuko .gt_font_normal {
  font-weight: normal;
}

#naxpadeuko .gt_font_bold {
  font-weight: bold;
}

#naxpadeuko .gt_font_italic {
  font-style: italic;
}

#naxpadeuko .gt_super {
  font-size: 65%;
}

#naxpadeuko .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#naxpadeuko .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#naxpadeuko .gt_indent_1 {
  text-indent: 5px;
}

#naxpadeuko .gt_indent_2 {
  text-indent: 10px;
}

#naxpadeuko .gt_indent_3 {
  text-indent: 15px;
}

#naxpadeuko .gt_indent_4 {
  text-indent: 20px;
}

#naxpadeuko .gt_indent_5 {
  text-indent: 25px;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id="Predicted Class" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">Predicted Class</th>
<th id="True Class" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">True Class</th>
<th id="Count" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">Count</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_right" headers="Predicted Class">1</td>
<td class="gt_row gt_left" headers="True Class">True 1</td>
<td class="gt_row gt_right" headers="Count">40</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="Predicted Class">1</td>
<td class="gt_row gt_left" headers="True Class">True 0</td>
<td class="gt_row gt_right" headers="Count">15</td>
</tr>
<tr class="odd">
<td class="gt_row gt_right" headers="Predicted Class">0</td>
<td class="gt_row gt_left" headers="True Class">True 1</td>
<td class="gt_row gt_right" headers="Count">10</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="Predicted Class">0</td>
<td class="gt_row gt_left" headers="True Class">True 0</td>
<td class="gt_row gt_right" headers="Count">35</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>The <code>tbl_cross()</code> function is useful for computing these metrics after a bit of data wrangling.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Duplicate each row of the tibble based on the value in the Count column</span></span>
<span id="cb2-2">confusion_matrix_long <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">uncount</span>(Count) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_cross</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percent =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col"</span>)  </span></code></pre></div></div>
<div class="cell-output-display">
<div id="qtqnjrriyo" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#qtqnjrriyo table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#qtqnjrriyo thead, #qtqnjrriyo tbody, #qtqnjrriyo tfoot, #qtqnjrriyo tr, #qtqnjrriyo td, #qtqnjrriyo th {
  border-style: none;
}

#qtqnjrriyo p {
  margin: 0;
  padding: 0;
}

#qtqnjrriyo .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#qtqnjrriyo .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#qtqnjrriyo .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#qtqnjrriyo .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#qtqnjrriyo .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qtqnjrriyo .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qtqnjrriyo .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qtqnjrriyo .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#qtqnjrriyo .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#qtqnjrriyo .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#qtqnjrriyo .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#qtqnjrriyo .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#qtqnjrriyo .gt_spanner_row {
  border-bottom-style: hidden;
}

#qtqnjrriyo .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#qtqnjrriyo .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#qtqnjrriyo .gt_from_md > :first-child {
  margin-top: 0;
}

#qtqnjrriyo .gt_from_md > :last-child {
  margin-bottom: 0;
}

#qtqnjrriyo .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#qtqnjrriyo .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#qtqnjrriyo .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#qtqnjrriyo .gt_row_group_first td {
  border-top-width: 2px;
}

#qtqnjrriyo .gt_row_group_first th {
  border-top-width: 2px;
}

#qtqnjrriyo .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qtqnjrriyo .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#qtqnjrriyo .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#qtqnjrriyo .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qtqnjrriyo .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qtqnjrriyo .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#qtqnjrriyo .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#qtqnjrriyo .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#qtqnjrriyo .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qtqnjrriyo .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qtqnjrriyo .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qtqnjrriyo .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qtqnjrriyo .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qtqnjrriyo .gt_left {
  text-align: left;
}

#qtqnjrriyo .gt_center {
  text-align: center;
}

#qtqnjrriyo .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#qtqnjrriyo .gt_font_normal {
  font-weight: normal;
}

#qtqnjrriyo .gt_font_bold {
  font-weight: bold;
}

#qtqnjrriyo .gt_font_italic {
  font-style: italic;
}

#qtqnjrriyo .gt_super {
  font-size: 65%;
}

#qtqnjrriyo .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#qtqnjrriyo .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#qtqnjrriyo .gt_indent_1 {
  text-indent: 5px;
}

#qtqnjrriyo .gt_indent_2 {
  text-indent: 10px;
}

#qtqnjrriyo .gt_indent_3 {
  text-indent: 15px;
}

#qtqnjrriyo .gt_indent_4 {
  text-indent: 20px;
}

#qtqnjrriyo .gt_indent_5 {
  text-indent: 25px;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings gt_spanner_row header">
<th rowspan="2" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"></th>
<th colspan="2" id="True Class" class="gt_center gt_columns_top_border gt_column_spanner_outer" data-quarto-table-cell-role="th" scope="colgroup"><span class="gt_column_spanner">True Class</span></th>
<th rowspan="2" id="Total" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">Total</th>
</tr>
<tr class="gt_col_headings even">
<th id="True 0" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">True 0</th>
<th id="True 1" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">True 1</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="label">Predicted Class</td>
<td class="gt_row gt_center" headers="stat_1"></td>
<td class="gt_row gt_center" headers="stat_2"></td>
<td class="gt_row gt_center" headers="stat_0"></td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;0</td>
<td class="gt_row gt_center" headers="stat_1">35 (70%)</td>
<td class="gt_row gt_center" headers="stat_2">10 (20%)</td>
<td class="gt_row gt_center" headers="stat_0">45 (45%)</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;1</td>
<td class="gt_row gt_center" headers="stat_1">15 (30%)</td>
<td class="gt_row gt_center" headers="stat_2">40 (80%)</td>
<td class="gt_row gt_center" headers="stat_0">55 (55%)</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">Total</td>
<td class="gt_row gt_center" headers="stat_1">50 (100%)</td>
<td class="gt_row gt_center" headers="stat_2">50 (100%)</td>
<td class="gt_row gt_center" headers="stat_0">100 (100%)</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>The <strong>sensitivity</strong> of our model is 80%, and the <strong>specificity</strong> is 70% - pretty good values!</p>
</section>
<section id="computing-sensitivity-and-specificity-using-collect_metrics" class="level3">
<h3 class="anchored" data-anchor-id="computing-sensitivity-and-specificity-using-collect_metrics">Computing Sensitivity and Specificity Using collect_metrics</h3>
<p>In machine learning, <strong>sensitivity</strong> and <strong>specificity</strong> are important metrics used to evaluate and improve model performance. When we tune model parameters across different datasets, it’s useful to compute these metrics for each dataset. This gives us useful data to make decisions on enhancing model performance. We can calculate these metrics efficiently using the <code>collect_metrics()</code> function from the tidymodels package.</p>
<p>We’ll use <code>fit_resamples()</code> to estimate the model many times. This uses different samples from our training data. Then, we’ll use <code>collect_metrics()</code> to compute the <strong>sensitivity</strong> and <strong>specificity</strong> for each dataset.</p>
</section>
<section id="cross-validation-folds" class="level3">
<h3 class="anchored" data-anchor-id="cross-validation-folds">Cross-Validation Folds</h3>
<p>We’ll start by using <code>vfold_cv()</code> from tidymodels to create 10 folds of data. To get a better estimate of the sensitivity and specificity, we’ll use the <em>repeats</em> argument to repeat the sampling process 10 times. This gives us 100 folds of data in total.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Cross-Validation
</div>
</div>
<div class="callout-body-container callout-body">
<p>You can learn more about cross-validation <a href="https://www.tidymodels.org/start/resampling/">here</a>.</p>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load packages</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidymodels) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load tidymodels package</span></span>
<span id="cb3-3">tidymodels<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;">tidymodels_prefer</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set tidymodels as the default modeling framework</span></span>
<span id="cb3-4"></span>
<span id="cb3-5"><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;">1906</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set seed for reproducibility</span></span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a tibble with the data</span></span>
<span id="cb3-8">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> confusion_matrix_long <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">uncount</span>(Count)  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb3-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(</span>
<span id="cb3-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Predicted Class</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, </span>
<span id="cb3-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">True Class</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb3-13">  )</span>
<span id="cb3-14"></span>
<span id="cb3-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create 10 folds of the data</span></span>
<span id="cb3-16">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>(df, <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 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;">10</span>)</span></code></pre></div></div>
</div>
</section>
<section id="model-specification" class="level3">
<h3 class="anchored" data-anchor-id="model-specification">Model Specification</h3>
<p>Having the folds ready, we can now specify the model.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define a logistic regression model specification</span></span>
<span id="cb4-2">model_spec <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;">logistic_reg</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_engine</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"glm"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_mode</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classification"</span>)</span>
<span id="cb4-5"></span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a recipe for preprocessing the data</span></span>
<span id="cb4-7">recipe_glm <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>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> df) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert all nominal variables to dummy variables</span></span>
<span id="cb4-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step_dummy</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_nominal</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;">all_outcomes</span>()) </span>
<span id="cb4-10"></span>
<span id="cb4-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define a workflow for fitting the logistic regression model</span></span>
<span id="cb4-12">wr_glm <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add the recipe to the workflow</span></span>
<span id="cb4-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_recipe</span>(recipe_glm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add the model specification to the workflow</span></span>
<span id="cb4-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_model</span>(model_spec)</span></code></pre></div></div>
</div>
<p>Perfect! Now we are ready to estimate the model.</p>
</section>
<section id="estimating-the-model" class="level3">
<h3 class="anchored" data-anchor-id="estimating-the-model">Estimating the Model</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Estimate the model 100 times</span></span>
<span id="cb5-2">model_resamples <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;">fit_resamples</span>(</span>
<span id="cb5-3">  wr_glm, </span>
<span id="cb5-4">  folds, </span>
<span id="cb5-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">control_resamples</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">save_pred =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb5-6">)</span></code></pre></div></div>
</div>
<p>Notice that we used the <em>save_pred</em> argument in <code>control_resample()</code> because we want to save the predictions of the model for each fold. This allows us to compute the sensitivity and specificity for each fold, which is the goal of this post.</p>
<p>Let’s take a look at one of the results.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract the metrics for the first resample</span></span>
<span id="cb6-2">model_resamples <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Select the first resample</span></span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</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="cb6-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unnest the metrics data</span></span>
<span id="cb6-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(.metrics)  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(.metric, .estimate) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>()  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt_number</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">decimals =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="cgnqskzvwa" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#cgnqskzvwa table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#cgnqskzvwa thead, #cgnqskzvwa tbody, #cgnqskzvwa tfoot, #cgnqskzvwa tr, #cgnqskzvwa td, #cgnqskzvwa th {
  border-style: none;
}

#cgnqskzvwa p {
  margin: 0;
  padding: 0;
}

#cgnqskzvwa .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#cgnqskzvwa .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#cgnqskzvwa .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#cgnqskzvwa .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#cgnqskzvwa .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#cgnqskzvwa .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#cgnqskzvwa .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#cgnqskzvwa .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#cgnqskzvwa .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#cgnqskzvwa .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#cgnqskzvwa .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#cgnqskzvwa .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#cgnqskzvwa .gt_spanner_row {
  border-bottom-style: hidden;
}

#cgnqskzvwa .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#cgnqskzvwa .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#cgnqskzvwa .gt_from_md > :first-child {
  margin-top: 0;
}

#cgnqskzvwa .gt_from_md > :last-child {
  margin-bottom: 0;
}

#cgnqskzvwa .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#cgnqskzvwa .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#cgnqskzvwa .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#cgnqskzvwa .gt_row_group_first td {
  border-top-width: 2px;
}

#cgnqskzvwa .gt_row_group_first th {
  border-top-width: 2px;
}

#cgnqskzvwa .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#cgnqskzvwa .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#cgnqskzvwa .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#cgnqskzvwa .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#cgnqskzvwa .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#cgnqskzvwa .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#cgnqskzvwa .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#cgnqskzvwa .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#cgnqskzvwa .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#cgnqskzvwa .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#cgnqskzvwa .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#cgnqskzvwa .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#cgnqskzvwa .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#cgnqskzvwa .gt_left {
  text-align: left;
}

#cgnqskzvwa .gt_center {
  text-align: center;
}

#cgnqskzvwa .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#cgnqskzvwa .gt_font_normal {
  font-weight: normal;
}

#cgnqskzvwa .gt_font_bold {
  font-weight: bold;
}

#cgnqskzvwa .gt_font_italic {
  font-style: italic;
}

#cgnqskzvwa .gt_super {
  font-size: 65%;
}

#cgnqskzvwa .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#cgnqskzvwa .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#cgnqskzvwa .gt_indent_1 {
  text-indent: 5px;
}

#cgnqskzvwa .gt_indent_2 {
  text-indent: 10px;
}

#cgnqskzvwa .gt_indent_3 {
  text-indent: 15px;
}

#cgnqskzvwa .gt_indent_4 {
  text-indent: 20px;
}

#cgnqskzvwa .gt_indent_5 {
  text-indent: 25px;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id=".metric" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">.metric</th>
<th id=".estimate" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">.estimate</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers=".metric">accuracy</td>
<td class="gt_row gt_right" headers=".estimate">0.700</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers=".metric">roc_auc</td>
<td class="gt_row gt_right" headers=".estimate">0.786</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>By default, we obtain <strong>accuracy</strong> and <strong>roc_auc</strong> metrics for classification models. However, we want a specific metric set (<strong>sensitivy</strong> and <strong>specificity</strong>). To achieve this, we can use <code>metric_set()</code> function from the <code>yardstick</code> package.</p>
<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>event_level = ?
</div>
</div>
<div class="callout-body-container callout-body">
<p>Be mindful of the event level used by yardstick. As we demonstrated in the <a href="https://focardozom.github.io/posts/2023-07-15-yarstick/2023-07-15.html">previous post</a>, <code>yardstick</code> uses the first level as the event of interest. To change this behavior, we need to specify the <em>event_level</em> argument when using the <code>fit_resamples()</code> function.</p>
</div>
</div>
</section>
<section id="computing-sensitivity-and-specificity" class="level3">
<h3 class="anchored" data-anchor-id="computing-sensitivity-and-specificity">Computing Sensitivity and Specificity</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the metrics to use</span></span>
<span id="cb7-2">my_metrics <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;">metric_set</span>(sens, spec)</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fit the logistic regression model using resampling</span></span>
<span id="cb7-5">model_resamples <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;">fit_resamples</span>(</span>
<span id="cb7-6">  wr_glm, </span>
<span id="cb7-7">  folds, </span>
<span id="cb7-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metrics =</span> my_metrics,</span>
<span id="cb7-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">control_resamples</span>(</span>
<span id="cb7-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save the predicted values for each resample</span></span>
<span id="cb7-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">save_pred =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb7-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set the event level to "second"</span></span>
<span id="cb7-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">event_level =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"second"</span></span>
<span id="cb7-14">  )</span>
<span id="cb7-15">)</span></code></pre></div></div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>You can find all available metrics in yardstick <a href="https://yardstick.tidymodels.org/reference/index.html">here</a>.</p>
</div>
</div>
<p>Now, let’s dive into the results. For a start, we’ll focus on the first resample. Although this may provide an imperfect estimation of our “true” values, theory assures us that as we increase the number of resamples, the average of the metrics will move closer to these “true” values. Let’s see this in action!</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract the metrics for the first resample</span></span>
<span id="cb8-2">model_resamples <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Select the first resample</span></span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</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="cb8-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unnest the metrics data</span></span>
<span id="cb8-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(.metrics) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Select the metric and estimate columns</span></span>
<span id="cb8-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(.metric, .estimate) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a gt table</span></span>
<span id="cb8-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Format the estimate column to 3 decimal places</span></span>
<span id="cb8-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt_number</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">decimals =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="uhfautfkkd" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#uhfautfkkd table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#uhfautfkkd thead, #uhfautfkkd tbody, #uhfautfkkd tfoot, #uhfautfkkd tr, #uhfautfkkd td, #uhfautfkkd th {
  border-style: none;
}

#uhfautfkkd p {
  margin: 0;
  padding: 0;
}

#uhfautfkkd .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#uhfautfkkd .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#uhfautfkkd .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#uhfautfkkd .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#uhfautfkkd .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#uhfautfkkd .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#uhfautfkkd .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#uhfautfkkd .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#uhfautfkkd .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#uhfautfkkd .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#uhfautfkkd .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#uhfautfkkd .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#uhfautfkkd .gt_spanner_row {
  border-bottom-style: hidden;
}

#uhfautfkkd .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#uhfautfkkd .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#uhfautfkkd .gt_from_md > :first-child {
  margin-top: 0;
}

#uhfautfkkd .gt_from_md > :last-child {
  margin-bottom: 0;
}

#uhfautfkkd .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#uhfautfkkd .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#uhfautfkkd .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#uhfautfkkd .gt_row_group_first td {
  border-top-width: 2px;
}

#uhfautfkkd .gt_row_group_first th {
  border-top-width: 2px;
}

#uhfautfkkd .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#uhfautfkkd .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#uhfautfkkd .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#uhfautfkkd .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#uhfautfkkd .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#uhfautfkkd .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#uhfautfkkd .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#uhfautfkkd .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#uhfautfkkd .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#uhfautfkkd .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#uhfautfkkd .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#uhfautfkkd .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#uhfautfkkd .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#uhfautfkkd .gt_left {
  text-align: left;
}

#uhfautfkkd .gt_center {
  text-align: center;
}

#uhfautfkkd .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#uhfautfkkd .gt_font_normal {
  font-weight: normal;
}

#uhfautfkkd .gt_font_bold {
  font-weight: bold;
}

#uhfautfkkd .gt_font_italic {
  font-style: italic;
}

#uhfautfkkd .gt_super {
  font-size: 65%;
}

#uhfautfkkd .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#uhfautfkkd .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#uhfautfkkd .gt_indent_1 {
  text-indent: 5px;
}

#uhfautfkkd .gt_indent_2 {
  text-indent: 10px;
}

#uhfautfkkd .gt_indent_3 {
  text-indent: 15px;
}

#uhfautfkkd .gt_indent_4 {
  text-indent: 20px;
}

#uhfautfkkd .gt_indent_5 {
  text-indent: 25px;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id=".metric" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">.metric</th>
<th id=".estimate" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">.estimate</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers=".metric">sens</td>
<td class="gt_row gt_right" headers=".estimate">1.000</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers=".metric">spec</td>
<td class="gt_row gt_right" headers=".estimate">0.571</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>Good, we have achieved a <strong>sensitivity</strong> of 100% and <strong>specificity</strong> of 57.1%, which, frankly, isn’t an ideal representation of our true values. But, let’s keep in mind, this is just a single estimate out of our 100 trials. Now, let’s take it to the next level and summarize these values across all our estimates.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Collect and summarise the metrics for all resamples</span></span>
<span id="cb9-2">model_resamples <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Collect the metrics and summarise them</span></span>
<span id="cb9-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect_metrics</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">summarise =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a gt table</span></span>
<span id="cb9-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Format the table to 3 decimal places</span></span>
<span id="cb9-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt_number</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">decimals =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="mrdtrkiqts" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#mrdtrkiqts table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#mrdtrkiqts thead, #mrdtrkiqts tbody, #mrdtrkiqts tfoot, #mrdtrkiqts tr, #mrdtrkiqts td, #mrdtrkiqts th {
  border-style: none;
}

#mrdtrkiqts p {
  margin: 0;
  padding: 0;
}

#mrdtrkiqts .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#mrdtrkiqts .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#mrdtrkiqts .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#mrdtrkiqts .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#mrdtrkiqts .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#mrdtrkiqts .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#mrdtrkiqts .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#mrdtrkiqts .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#mrdtrkiqts .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#mrdtrkiqts .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#mrdtrkiqts .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#mrdtrkiqts .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#mrdtrkiqts .gt_spanner_row {
  border-bottom-style: hidden;
}

#mrdtrkiqts .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#mrdtrkiqts .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#mrdtrkiqts .gt_from_md > :first-child {
  margin-top: 0;
}

#mrdtrkiqts .gt_from_md > :last-child {
  margin-bottom: 0;
}

#mrdtrkiqts .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#mrdtrkiqts .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#mrdtrkiqts .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#mrdtrkiqts .gt_row_group_first td {
  border-top-width: 2px;
}

#mrdtrkiqts .gt_row_group_first th {
  border-top-width: 2px;
}

#mrdtrkiqts .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#mrdtrkiqts .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#mrdtrkiqts .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#mrdtrkiqts .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#mrdtrkiqts .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#mrdtrkiqts .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#mrdtrkiqts .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#mrdtrkiqts .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#mrdtrkiqts .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#mrdtrkiqts .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#mrdtrkiqts .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#mrdtrkiqts .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#mrdtrkiqts .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#mrdtrkiqts .gt_left {
  text-align: left;
}

#mrdtrkiqts .gt_center {
  text-align: center;
}

#mrdtrkiqts .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#mrdtrkiqts .gt_font_normal {
  font-weight: normal;
}

#mrdtrkiqts .gt_font_bold {
  font-weight: bold;
}

#mrdtrkiqts .gt_font_italic {
  font-style: italic;
}

#mrdtrkiqts .gt_super {
  font-size: 65%;
}

#mrdtrkiqts .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#mrdtrkiqts .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#mrdtrkiqts .gt_indent_1 {
  text-indent: 5px;
}

#mrdtrkiqts .gt_indent_2 {
  text-indent: 10px;
}

#mrdtrkiqts .gt_indent_3 {
  text-indent: 15px;
}

#mrdtrkiqts .gt_indent_4 {
  text-indent: 20px;
}

#mrdtrkiqts .gt_indent_5 {
  text-indent: 25px;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id=".metric" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">.metric</th>
<th id=".estimator" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">.estimator</th>
<th id="mean" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">mean</th>
<th id="n" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">n</th>
<th id="std_err" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">std_err</th>
<th id=".config" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">.config</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers=".metric">sens</td>
<td class="gt_row gt_left" headers=".estimator">binary</td>
<td class="gt_row gt_right" headers="mean">0.810</td>
<td class="gt_row gt_right" headers="n">99.000</td>
<td class="gt_row gt_right" headers="std_err">0.017</td>
<td class="gt_row gt_left" headers=".config">Preprocessor1_Model1</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers=".metric">spec</td>
<td class="gt_row gt_left" headers=".estimator">binary</td>
<td class="gt_row gt_right" headers="mean">0.703</td>
<td class="gt_row gt_right" headers="n">100.000</td>
<td class="gt_row gt_right" headers="std_err">0.018</td>
<td class="gt_row gt_left" headers=".config">Preprocessor1_Model1</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>Great job! Our model’s estimated <strong>sensitivity</strong> is at 81.0% and <strong>specificity</strong> stands at 70.3%, quite near to our actual values.</p>
<p>With all this valuable data at hand, it’s tempting to visualize it. So, let’s move on to plotting the observed distribution for both sensitivity and specificity values to get a better understanding of our model’s performance.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">model_resamples  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb10-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(.metrics)  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb10-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> .estimate, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> .metric)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>.metric) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-6">  <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="cb10-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#00AFBB"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E7B800"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://focardozom.github.io/posts/2023-07-22-collect_metrics/collect_metrics_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Next time, we’ll look more closely at these values. We’ll work out which ones are bigger than 0.5 and see how spread out they are. But, we need to be careful because these numbers are connected; they all come from the same data. Don’t miss it!</p>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>In this blog post, we discussed the use of the <code>tidymodels</code> package to compute the sensitivity and specificity of a model - two important measures for evaluating model performance. We started with the recreation of a confusion matrix, then used the <code>tbl_cross()</code> function to perform some data wrangling.</p>
<p>Next, we leveraged <code>fit_resamples()</code> from the <code>tidymodels</code> package to estimate our model multiple times, using cross-validation with 10 repeated folds to create 100 datasets in total.</p>
<p>We set up a logistic regression model, fit it to our data, and estimated the model multiple times using the created folds. The saved predictions allowed us to compute sensitivity and specificity for each fold.</p>
<p>We defined a custom set of metrics - roc_auc, sensitivity, and specificity - using the <code>metric_set()</code> function from the <code>yardstick</code> package. Finally, we used <code>collect_metrics()</code> to compute the average of these metrics for all the folds, and visualized the distribution of the sensitivity and specificity values.</p>
<p>Future posts will delve into calculating confidence intervals and evaluating the variability of these values across resamples.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>R</category>
  <category>Machine Learning</category>
  <guid>https://focardozom.github.io/posts/2023-07-22-collect_metrics/collect_metrics.html</guid>
  <pubDate>Sat, 22 Jul 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Computing Sensitivity and Specificity using tidymodels</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <dc:creator>Kyle Grealis</dc:creator>
  <link>https://focardozom.github.io/posts/2023-07-15-yarstick/2023-07-15.html</link>
  <description><![CDATA[ 




<p>Sensitivity and specificity are two crucial metrics used to evaluate the performance of binary classification models. In this article, we delve into the process of computing these metrics using the ‘tidymodels’ package in R.</p>
<p>We begin by simulating a dataset. Assuming we have access to the true value of the phenomenon under consideration, as well as our model’s predictions, we can proceed to compute the sensitivity and specificity of our model.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load tidyverse package</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gt) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load gt package</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gtsummary) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load gtsummary package</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a tibble with the confusion matrix data</span></span>
<span id="cb1-7">confusion_matrix_long <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb1-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Predicted Class"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</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;">"1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span>),</span>
<span id="cb1-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True Class"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</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;">"True 1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True 0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True 1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True 0"</span>),</span>
<span id="cb1-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Count"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</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;">35</span>)</span>
<span id="cb1-11">)</span>
<span id="cb1-12"></span>
<span id="cb1-13">confusion_matrix_long <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;">gt</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div id="cftpcbaxni" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#cftpcbaxni table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#cftpcbaxni thead, #cftpcbaxni tbody, #cftpcbaxni tfoot, #cftpcbaxni tr, #cftpcbaxni td, #cftpcbaxni th {
  border-style: none;
}

#cftpcbaxni p {
  margin: 0;
  padding: 0;
}

#cftpcbaxni .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#cftpcbaxni .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#cftpcbaxni .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#cftpcbaxni .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#cftpcbaxni .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#cftpcbaxni .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#cftpcbaxni .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#cftpcbaxni .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#cftpcbaxni .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#cftpcbaxni .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#cftpcbaxni .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#cftpcbaxni .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#cftpcbaxni .gt_spanner_row {
  border-bottom-style: hidden;
}

#cftpcbaxni .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#cftpcbaxni .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#cftpcbaxni .gt_from_md > :first-child {
  margin-top: 0;
}

#cftpcbaxni .gt_from_md > :last-child {
  margin-bottom: 0;
}

#cftpcbaxni .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#cftpcbaxni .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#cftpcbaxni .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#cftpcbaxni .gt_row_group_first td {
  border-top-width: 2px;
}

#cftpcbaxni .gt_row_group_first th {
  border-top-width: 2px;
}

#cftpcbaxni .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#cftpcbaxni .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#cftpcbaxni .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#cftpcbaxni .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#cftpcbaxni .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#cftpcbaxni .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#cftpcbaxni .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#cftpcbaxni .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#cftpcbaxni .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#cftpcbaxni .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#cftpcbaxni .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#cftpcbaxni .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#cftpcbaxni .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#cftpcbaxni .gt_left {
  text-align: left;
}

#cftpcbaxni .gt_center {
  text-align: center;
}

#cftpcbaxni .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#cftpcbaxni .gt_font_normal {
  font-weight: normal;
}

#cftpcbaxni .gt_font_bold {
  font-weight: bold;
}

#cftpcbaxni .gt_font_italic {
  font-style: italic;
}

#cftpcbaxni .gt_super {
  font-size: 65%;
}

#cftpcbaxni .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#cftpcbaxni .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#cftpcbaxni .gt_indent_1 {
  text-indent: 5px;
}

#cftpcbaxni .gt_indent_2 {
  text-indent: 10px;
}

#cftpcbaxni .gt_indent_3 {
  text-indent: 15px;
}

#cftpcbaxni .gt_indent_4 {
  text-indent: 20px;
}

#cftpcbaxni .gt_indent_5 {
  text-indent: 25px;
}

#cftpcbaxni .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#cftpcbaxni div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id="Predicted-Class" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">Predicted Class</th>
<th id="True-Class" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">True Class</th>
<th id="Count" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">Count</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_right" headers="Predicted Class">1</td>
<td class="gt_row gt_left" headers="True Class">True 1</td>
<td class="gt_row gt_right" headers="Count">40</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="Predicted Class">1</td>
<td class="gt_row gt_left" headers="True Class">True 0</td>
<td class="gt_row gt_right" headers="Count">15</td>
</tr>
<tr class="odd">
<td class="gt_row gt_right" headers="Predicted Class">0</td>
<td class="gt_row gt_left" headers="True Class">True 1</td>
<td class="gt_row gt_right" headers="Count">10</td>
</tr>
<tr class="even">
<td class="gt_row gt_right" headers="Predicted Class">0</td>
<td class="gt_row gt_left" headers="True Class">True 0</td>
<td class="gt_row gt_right" headers="Count">35</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>Function <a href="https://www.danieldsjoberg.com/gtsummary/reference/tbl_cross.html" target="_blank"><code>tbl_cross()</code></a> offers a handy feature for obtaining percentages, which is useful when computing sensitivity and specificity.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Duplicate each row of the tibble based on the value in the Count column</span></span>
<span id="cb2-2">confusion_matrix_long <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">uncount</span>(Count) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_cross</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percent =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="xeamzjufxu" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#xeamzjufxu table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#xeamzjufxu thead, #xeamzjufxu tbody, #xeamzjufxu tfoot, #xeamzjufxu tr, #xeamzjufxu td, #xeamzjufxu th {
  border-style: none;
}

#xeamzjufxu p {
  margin: 0;
  padding: 0;
}

#xeamzjufxu .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#xeamzjufxu .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#xeamzjufxu .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#xeamzjufxu .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#xeamzjufxu .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#xeamzjufxu .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#xeamzjufxu .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#xeamzjufxu .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#xeamzjufxu .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#xeamzjufxu .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#xeamzjufxu .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#xeamzjufxu .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#xeamzjufxu .gt_spanner_row {
  border-bottom-style: hidden;
}

#xeamzjufxu .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#xeamzjufxu .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#xeamzjufxu .gt_from_md > :first-child {
  margin-top: 0;
}

#xeamzjufxu .gt_from_md > :last-child {
  margin-bottom: 0;
}

#xeamzjufxu .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#xeamzjufxu .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#xeamzjufxu .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#xeamzjufxu .gt_row_group_first td {
  border-top-width: 2px;
}

#xeamzjufxu .gt_row_group_first th {
  border-top-width: 2px;
}

#xeamzjufxu .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#xeamzjufxu .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#xeamzjufxu .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#xeamzjufxu .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#xeamzjufxu .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#xeamzjufxu .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#xeamzjufxu .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#xeamzjufxu .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#xeamzjufxu .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#xeamzjufxu .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#xeamzjufxu .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#xeamzjufxu .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#xeamzjufxu .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#xeamzjufxu .gt_left {
  text-align: left;
}

#xeamzjufxu .gt_center {
  text-align: center;
}

#xeamzjufxu .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#xeamzjufxu .gt_font_normal {
  font-weight: normal;
}

#xeamzjufxu .gt_font_bold {
  font-weight: bold;
}

#xeamzjufxu .gt_font_italic {
  font-style: italic;
}

#xeamzjufxu .gt_super {
  font-size: 65%;
}

#xeamzjufxu .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#xeamzjufxu .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#xeamzjufxu .gt_indent_1 {
  text-indent: 5px;
}

#xeamzjufxu .gt_indent_2 {
  text-indent: 10px;
}

#xeamzjufxu .gt_indent_3 {
  text-indent: 15px;
}

#xeamzjufxu .gt_indent_4 {
  text-indent: 20px;
}

#xeamzjufxu .gt_indent_5 {
  text-indent: 25px;
}

#xeamzjufxu .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#xeamzjufxu div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<colgroup>
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="gt_col_headings gt_spanner_row header">
<th rowspan="2" id="label" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"></th>
<th colspan="2" id="level 1; stat_1" class="gt_center gt_columns_top_border gt_column_spanner_outer" data-quarto-table-cell-role="th" scope="colgroup"><div class="gt_column_spanner">
True Class
</div></th>
<th rowspan="2" id="stat_0" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">Total</th>
</tr>
<tr class="gt_col_headings even">
<th id="stat_1" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">True 0</th>
<th id="stat_2" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">True 1</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="label">Predicted Class</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
<td class="gt_row gt_center" headers="stat_0"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;0</td>
<td class="gt_row gt_center" headers="stat_1">35 (70%)</td>
<td class="gt_row gt_center" headers="stat_2">10 (20%)</td>
<td class="gt_row gt_center" headers="stat_0">45 (45%)</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;1</td>
<td class="gt_row gt_center" headers="stat_1">15 (30%)</td>
<td class="gt_row gt_center" headers="stat_2">40 (80%)</td>
<td class="gt_row gt_center" headers="stat_0">55 (55%)</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">Total</td>
<td class="gt_row gt_center" headers="stat_1">50 (100%)</td>
<td class="gt_row gt_center" headers="stat_2">50 (100%)</td>
<td class="gt_row gt_center" headers="stat_0">100 (100%)</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>So, we know the values of specificity and sensitivity, which are essentially the results of these two equations:</p>
<ul>
<li><p>Sensitivity = TP / (TP + FN) = 40 / (40 + 10) = 0.80 (80%)</p></li>
<li><p>Specificity = TN / (TN + FP) = 35 / (35 + 15) = 0.70 (70%)</p></li>
</ul>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<blockquote class="blockquote">
<p>Refresh your memory on the meaning of these terms by looking at the following table:</p>
</blockquote>
<table class="caption-top table">
<thead>
<tr class="header">
<th></th>
<th style="text-align: center;">True 1 (Positive)</th>
<th style="text-align: center;">True 0 (Negative)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Predict 1 (Positive)</td>
<td style="text-align: center;">TP = 40</td>
<td style="text-align: center;">FP = 15</td>
</tr>
<tr class="even">
<td>Predict 0 (Negative)</td>
<td style="text-align: center;">FN = 10</td>
<td style="text-align: center;">TN = 35</td>
</tr>
</tbody>
</table>
<p>This table above is the confusion matrix, featuring the following terms:</p>
<ul>
<li>TP (True Positive): Instances where the model correctly predicted the positive class.</li>
<li>TN (True Negative): Instances where the model correctly predicted the negative class.</li>
<li>FP (False Positive): Instances where the model incorrectly predicted the positive class.</li>
<li>FN (False Negative): Instances where the model incorrectly predicted the negative class.</li>
</ul>
</div>
</div>
</div>
<p>However, the burning question remains: how do we calculate these in R? Let’s explore this while evaluating the performance of our models using the ‘tidymodels’ package.</p>
<section id="modeling-with-tidymodels" class="level3">
<h3 class="anchored" data-anchor-id="modeling-with-tidymodels">Modeling with tidymodels</h3>
<p>Let’s now assume that we have a dataset comprising Y and X values, and we aim to create a model that predicts Y based on X. We’ll use the same dataset we created earlier.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> confusion_matrix_long <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;">uncount</span>(Count) <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;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">True Class</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Predicted Class</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rename the columns to shorter names</span></span></code></pre></div></div>
</div>
<p>First, we’ll load the tidymodels library and specify our model.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the tidymodels package</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidymodels)</span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define a logistic regression model specification</span></span>
<span id="cb4-5">model_spec <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;">logistic_reg</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-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;">"glm"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_mode</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classification"</span>)</span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define a recipe for preprocessing the data</span></span>
<span id="cb4-10">recipe_glm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb4-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">recipe</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> df) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert all nominal variables to dummy variables</span></span>
<span id="cb4-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step_dummy</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_nominal</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;">all_outcomes</span>())</span></code></pre></div></div>
</div>
<p>Let’s examine the dataset we’ve created.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Preprocess the data using the recipe</span></span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This includes converting nominal variables to dummy variables</span></span>
<span id="cb5-3">recipe_glm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prep</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Apply the recipe to new data</span></span>
<span id="cb5-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bake</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</span> df) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># View the first few rows of the preprocessed data</span></span>
<span id="cb5-8">    skimr<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;">skim</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Data summary</caption>
<tbody>
<tr class="odd">
<td style="text-align: left;">Name</td>
<td style="text-align: left;">bake(prep(recipe_glm), ne…</td>
</tr>
<tr class="even">
<td style="text-align: left;">Number of rows</td>
<td style="text-align: left;">100</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Number of columns</td>
<td style="text-align: left;">2</td>
</tr>
<tr class="even">
<td style="text-align: left;">_______________________</td>
<td style="text-align: left;"></td>
</tr>
<tr class="odd">
<td style="text-align: left;">Column type frequency:</td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: left;">factor</td>
<td style="text-align: left;">1</td>
</tr>
<tr class="odd">
<td style="text-align: left;">numeric</td>
<td style="text-align: left;">1</td>
</tr>
<tr class="even">
<td style="text-align: left;">________________________</td>
<td style="text-align: left;"></td>
</tr>
<tr class="odd">
<td style="text-align: left;">Group variables</td>
<td style="text-align: left;">None</td>
</tr>
</tbody>
</table>
<p><strong>Variable type: factor</strong></p>
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 19%">
<col style="width: 13%">
<col style="width: 19%">
<col style="width: 11%">
<col style="width: 12%">
<col style="width: 23%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">skim_variable</th>
<th style="text-align: right;">n_missing</th>
<th style="text-align: right;">complete_rate</th>
<th style="text-align: left;">ordered</th>
<th style="text-align: right;">n_unique</th>
<th style="text-align: left;">top_counts</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">y</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">FALSE</td>
<td style="text-align: right;">2</td>
<td style="text-align: left;">Tru: 50, Tru: 50</td>
</tr>
</tbody>
</table>
<p><strong>Variable type: numeric</strong></p>
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 19%">
<col style="width: 13%">
<col style="width: 19%">
<col style="width: 6%">
<col style="width: 5%">
<col style="width: 4%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 6%">
<col style="width: 8%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">skim_variable</th>
<th style="text-align: right;">n_missing</th>
<th style="text-align: right;">complete_rate</th>
<th style="text-align: right;">mean</th>
<th style="text-align: right;">sd</th>
<th style="text-align: right;">p0</th>
<th style="text-align: right;">p25</th>
<th style="text-align: right;">p50</th>
<th style="text-align: right;">p75</th>
<th style="text-align: right;">p100</th>
<th style="text-align: left;">hist</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">x_X1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0.55</td>
<td style="text-align: right;">0.5</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">▆▁▁▁▇</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Remeber that our outcome is a binary variable with values <code>True 0</code> and <code>True 1</code>.</p>
<p>Next, we can create a workflow that combines our recipe and the model specification, and fit the model.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define a workflow for fitting the logistic regression model</span></span>
<span id="cb6-2">wr_glm <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_recipe</span>(recipe_glm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_model</span>(model_spec)</span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fit the logistic regression model to the preprocessed data</span></span>
<span id="cb6-7">model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> wr_glm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> df)</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract the model coefficients and create a summary table</span></span>
<span id="cb6-11">model <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_fit_parsnip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_regression</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div id="smxkrpvbkw" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#smxkrpvbkw table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#smxkrpvbkw thead, #smxkrpvbkw tbody, #smxkrpvbkw tfoot, #smxkrpvbkw tr, #smxkrpvbkw td, #smxkrpvbkw th {
  border-style: none;
}

#smxkrpvbkw p {
  margin: 0;
  padding: 0;
}

#smxkrpvbkw .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#smxkrpvbkw .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#smxkrpvbkw .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#smxkrpvbkw .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#smxkrpvbkw .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#smxkrpvbkw .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#smxkrpvbkw .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#smxkrpvbkw .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#smxkrpvbkw .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#smxkrpvbkw .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#smxkrpvbkw .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#smxkrpvbkw .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#smxkrpvbkw .gt_spanner_row {
  border-bottom-style: hidden;
}

#smxkrpvbkw .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#smxkrpvbkw .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#smxkrpvbkw .gt_from_md > :first-child {
  margin-top: 0;
}

#smxkrpvbkw .gt_from_md > :last-child {
  margin-bottom: 0;
}

#smxkrpvbkw .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#smxkrpvbkw .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#smxkrpvbkw .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#smxkrpvbkw .gt_row_group_first td {
  border-top-width: 2px;
}

#smxkrpvbkw .gt_row_group_first th {
  border-top-width: 2px;
}

#smxkrpvbkw .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#smxkrpvbkw .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#smxkrpvbkw .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#smxkrpvbkw .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#smxkrpvbkw .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#smxkrpvbkw .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#smxkrpvbkw .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#smxkrpvbkw .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#smxkrpvbkw .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#smxkrpvbkw .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#smxkrpvbkw .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#smxkrpvbkw .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#smxkrpvbkw .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#smxkrpvbkw .gt_left {
  text-align: left;
}

#smxkrpvbkw .gt_center {
  text-align: center;
}

#smxkrpvbkw .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#smxkrpvbkw .gt_font_normal {
  font-weight: normal;
}

#smxkrpvbkw .gt_font_bold {
  font-weight: bold;
}

#smxkrpvbkw .gt_font_italic {
  font-style: italic;
}

#smxkrpvbkw .gt_super {
  font-size: 65%;
}

#smxkrpvbkw .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#smxkrpvbkw .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#smxkrpvbkw .gt_indent_1 {
  text-indent: 5px;
}

#smxkrpvbkw .gt_indent_2 {
  text-indent: 10px;
}

#smxkrpvbkw .gt_indent_3 {
  text-indent: 15px;
}

#smxkrpvbkw .gt_indent_4 {
  text-indent: 20px;
}

#smxkrpvbkw .gt_indent_5 {
  text-indent: 25px;
}

#smxkrpvbkw .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#smxkrpvbkw div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id="label" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"><strong>Characteristic</strong></th>
<th id="estimate" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>log(OR)</strong></th>
<th id="conf.low" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>95% CI</strong></th>
<th id="p.value" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>p-value</strong></th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="label">x_X1</td>
<td class="gt_row gt_center" headers="estimate">2.2</td>
<td class="gt_row gt_center" headers="conf.low">1.3, 3.2</td>
<td class="gt_row gt_center" headers="p.value">&lt;0.001</td>
</tr>
</tbody><tfoot class="gt_sourcenotes">
<tr class="odd">
<td colspan="4" class="gt_sourcenote">Abbreviations: CI = Confidence Interval, OR = Odds Ratio</td>
</tr>
</tfoot>

</table>

</div>
</div>
</div>
<p>So, we’ve established that the model predicts the Y values based on the X values. <em>But how do we calculate the sensitivity and specificity using the model results?</em></p>
<p>First, we need to generate predictions. The <a href="https://broom.tidymodels.org/reference/augment.lm.html" target="_blank"><code>augment()</code></a> function from the <a href="https://dplyr.tidyverse.org/" target="_blank"><code>dplyr</code></a> package is an excellent choice for this task.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the reactable package</span></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(reactable)</span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(knitr)</span>
<span id="cb7-4"></span>
<span id="cb7-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use the augment() function to add predicted values to the data frame</span></span>
<span id="cb7-6">the_predictions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> model <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">augment</span>(df) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rename the "y" column to "Observed"</span></span>
<span id="cb7-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observed"</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;">"y"</span>)</span>
<span id="cb7-10"></span>
<span id="cb7-11"></span>
<span id="cb7-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create an interactive table using the reactable::reactable() function</span></span>
<span id="cb7-13">the_predictions <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactable</span>(</span>
<span id="cb7-15">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">groupBy =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observed"</span>,</span>
<span id="cb7-16">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb7-17">            <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;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aggregate =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unique"</span>),</span>
<span id="cb7-18">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.pred_class =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aggregate =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unique"</span>),</span>
<span id="cb7-19">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.pred_True 0</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aggregate =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colFormat</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)),</span>
<span id="cb7-20">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.pred_True 1</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aggregate =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colFormat</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb7-21">        )</span>
<span id="cb7-22">    )</span></code></pre></div></div>
<div class="cell-output-display">
<div class="reactable html-widget html-fill-item" id="htmlwidget-b8a746d94f06923150b3" style="width:auto;height:auto;"></div>
<script type="application/json" data-for="htmlwidget-b8a746d94f06923150b3">{"x":{"tag":{"name":"Reactable","attribs":{"data":{".pred_class":["True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0"],".pred_True 0":[0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.272727272727784,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535,0.777777777777535],".pred_True 1":[0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.727272727272216,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465,0.222222222222465],"x":["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],"Observed":["True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 1","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0","True 0"]},"columns":[{"id":".pred_class","name":".pred_class","type":"factor","aggregate":"unique"},{"id":".pred_True 0","name":".pred_True 0","type":"numeric","aggregate":"mean","format":{"cell":{"digits":2},"aggregated":{"digits":2}}},{"id":".pred_True 1","name":".pred_True 1","type":"numeric","aggregate":"mean","format":{"cell":{"digits":2},"aggregated":{"digits":2}}},{"id":"x","name":"x","type":"character","aggregate":"unique"},{"id":"Observed","name":"Observed","type":"character"}],"groupBy":["Observed"],"dataKey":"a226375cc63caadb6299f1e6a897e435"},"children":[]},"class":"reactR_markup"},"evals":[],"jsHooks":[]}</script>
</div>
</div>
<p>At this point, we have both the predicted class and the observed class. We could use the same method we employed in the earlier demonstration (<a href="https://www.danieldsjoberg.com/gtsummary/reference/tbl_cross.html" target="_blank"><code>tbl_cross()</code></a>), or we can make use of the <a href="https://yardstick.tidymodels.org/reference/sens.html" target="_blank"><code>sensitivity()</code></a> and <a href="https://yardstick.tidymodels.org/reference/spec.html" target="_blank"><code>specificity()</code></a> functions from the <a href="https://yardstick.tidymodels.org/" target="_blank"><code>yardstick</code></a> package. Let’s look at how this works.</p>
</section>
<section id="computing-sensitivity-and-specificity-using-a-cross-table" class="level3">
<h3 class="anchored" data-anchor-id="computing-sensitivity-and-specificity-using-a-cross-table">Computing sensitivity and specificity using a cross table</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">the_predictions <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;">tbl_cross</span>(.pred_class, Observed, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percent =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="yhgzpgsrqt" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#yhgzpgsrqt table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#yhgzpgsrqt thead, #yhgzpgsrqt tbody, #yhgzpgsrqt tfoot, #yhgzpgsrqt tr, #yhgzpgsrqt td, #yhgzpgsrqt th {
  border-style: none;
}

#yhgzpgsrqt p {
  margin: 0;
  padding: 0;
}

#yhgzpgsrqt .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#yhgzpgsrqt .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#yhgzpgsrqt .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#yhgzpgsrqt .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#yhgzpgsrqt .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#yhgzpgsrqt .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#yhgzpgsrqt .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#yhgzpgsrqt .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#yhgzpgsrqt .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#yhgzpgsrqt .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#yhgzpgsrqt .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#yhgzpgsrqt .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#yhgzpgsrqt .gt_spanner_row {
  border-bottom-style: hidden;
}

#yhgzpgsrqt .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#yhgzpgsrqt .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#yhgzpgsrqt .gt_from_md > :first-child {
  margin-top: 0;
}

#yhgzpgsrqt .gt_from_md > :last-child {
  margin-bottom: 0;
}

#yhgzpgsrqt .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#yhgzpgsrqt .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#yhgzpgsrqt .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#yhgzpgsrqt .gt_row_group_first td {
  border-top-width: 2px;
}

#yhgzpgsrqt .gt_row_group_first th {
  border-top-width: 2px;
}

#yhgzpgsrqt .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#yhgzpgsrqt .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#yhgzpgsrqt .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#yhgzpgsrqt .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#yhgzpgsrqt .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#yhgzpgsrqt .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#yhgzpgsrqt .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#yhgzpgsrqt .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#yhgzpgsrqt .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#yhgzpgsrqt .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#yhgzpgsrqt .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#yhgzpgsrqt .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#yhgzpgsrqt .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#yhgzpgsrqt .gt_left {
  text-align: left;
}

#yhgzpgsrqt .gt_center {
  text-align: center;
}

#yhgzpgsrqt .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#yhgzpgsrqt .gt_font_normal {
  font-weight: normal;
}

#yhgzpgsrqt .gt_font_bold {
  font-weight: bold;
}

#yhgzpgsrqt .gt_font_italic {
  font-style: italic;
}

#yhgzpgsrqt .gt_super {
  font-size: 65%;
}

#yhgzpgsrqt .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#yhgzpgsrqt .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#yhgzpgsrqt .gt_indent_1 {
  text-indent: 5px;
}

#yhgzpgsrqt .gt_indent_2 {
  text-indent: 10px;
}

#yhgzpgsrqt .gt_indent_3 {
  text-indent: 15px;
}

#yhgzpgsrqt .gt_indent_4 {
  text-indent: 20px;
}

#yhgzpgsrqt .gt_indent_5 {
  text-indent: 25px;
}

#yhgzpgsrqt .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#yhgzpgsrqt div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<colgroup>
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="gt_col_headings gt_spanner_row header">
<th rowspan="2" id="label" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"></th>
<th colspan="2" id="level 1; stat_1" class="gt_center gt_columns_top_border gt_column_spanner_outer" data-quarto-table-cell-role="th" scope="colgroup"><div class="gt_column_spanner">
Observed
</div></th>
<th rowspan="2" id="stat_0" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">Total</th>
</tr>
<tr class="gt_col_headings even">
<th id="stat_1" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">True 0</th>
<th id="stat_2" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">True 1</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="label">.pred_class</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
<td class="gt_row gt_center" headers="stat_0"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;True 0</td>
<td class="gt_row gt_center" headers="stat_1">35 (70%)</td>
<td class="gt_row gt_center" headers="stat_2">10 (20%)</td>
<td class="gt_row gt_center" headers="stat_0">45 (45%)</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;True 1</td>
<td class="gt_row gt_center" headers="stat_1">15 (30%)</td>
<td class="gt_row gt_center" headers="stat_2">40 (80%)</td>
<td class="gt_row gt_center" headers="stat_0">55 (55%)</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">Total</td>
<td class="gt_row gt_center" headers="stat_1">50 (100%)</td>
<td class="gt_row gt_center" headers="stat_2">50 (100%)</td>
<td class="gt_row gt_center" headers="stat_0">100 (100%)</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>This yields the same results as before.</p>
</section>
<section id="computing-sensitivity-and-specificity-using-the-yardstick-package" class="level3">
<h3 class="anchored" data-anchor-id="computing-sensitivity-and-specificity-using-the-yardstick-package">Computing sensitivity and specificity using the yardstick package</h3>
<p>Finally! we can use the <a href="https://yardstick.tidymodels.org/reference/sens.html" target="_blank"><code>sensitivity()</code></a> and <a href="https://yardstick.tidymodels.org/reference/spec.html" target="_blank"><code>specificity()</code></a> functions from the <a href="https://yardstick.tidymodels.org/" target="_blank"><code>yardstick</code></a> package to calculate the sensitivity and specificity of our model.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">sens <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> the_predictions <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Observed =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(Observed)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sensitivity</span>(Observed, .pred_class)</span>
<span id="cb9-4"></span>
<span id="cb9-5">spec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> the_predictions <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Observed =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(Observed)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">specificity</span>(Observed, .pred_class)</span>
<span id="cb9-8"></span>
<span id="cb9-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(sens, spec) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div id="hflppbzdpq" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#hflppbzdpq table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#hflppbzdpq thead, #hflppbzdpq tbody, #hflppbzdpq tfoot, #hflppbzdpq tr, #hflppbzdpq td, #hflppbzdpq th {
  border-style: none;
}

#hflppbzdpq p {
  margin: 0;
  padding: 0;
}

#hflppbzdpq .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#hflppbzdpq .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#hflppbzdpq .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#hflppbzdpq .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#hflppbzdpq .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#hflppbzdpq .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#hflppbzdpq .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#hflppbzdpq .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#hflppbzdpq .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#hflppbzdpq .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#hflppbzdpq .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#hflppbzdpq .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#hflppbzdpq .gt_spanner_row {
  border-bottom-style: hidden;
}

#hflppbzdpq .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#hflppbzdpq .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#hflppbzdpq .gt_from_md > :first-child {
  margin-top: 0;
}

#hflppbzdpq .gt_from_md > :last-child {
  margin-bottom: 0;
}

#hflppbzdpq .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#hflppbzdpq .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#hflppbzdpq .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#hflppbzdpq .gt_row_group_first td {
  border-top-width: 2px;
}

#hflppbzdpq .gt_row_group_first th {
  border-top-width: 2px;
}

#hflppbzdpq .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#hflppbzdpq .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#hflppbzdpq .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#hflppbzdpq .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#hflppbzdpq .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#hflppbzdpq .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#hflppbzdpq .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#hflppbzdpq .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#hflppbzdpq .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#hflppbzdpq .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#hflppbzdpq .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#hflppbzdpq .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#hflppbzdpq .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#hflppbzdpq .gt_left {
  text-align: left;
}

#hflppbzdpq .gt_center {
  text-align: center;
}

#hflppbzdpq .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#hflppbzdpq .gt_font_normal {
  font-weight: normal;
}

#hflppbzdpq .gt_font_bold {
  font-weight: bold;
}

#hflppbzdpq .gt_font_italic {
  font-style: italic;
}

#hflppbzdpq .gt_super {
  font-size: 65%;
}

#hflppbzdpq .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#hflppbzdpq .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#hflppbzdpq .gt_indent_1 {
  text-indent: 5px;
}

#hflppbzdpq .gt_indent_2 {
  text-indent: 10px;
}

#hflppbzdpq .gt_indent_3 {
  text-indent: 15px;
}

#hflppbzdpq .gt_indent_4 {
  text-indent: 20px;
}

#hflppbzdpq .gt_indent_5 {
  text-indent: 25px;
}

#hflppbzdpq .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#hflppbzdpq div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id="a.metric" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">.metric</th>
<th id="a.estimator" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">.estimator</th>
<th id="a.estimate" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">.estimate</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers=".metric">sensitivity</td>
<td class="gt_row gt_left" headers=".estimator">binary</td>
<td class="gt_row gt_right" headers=".estimate">0.7</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers=".metric">specificity</td>
<td class="gt_row gt_left" headers=".estimator">binary</td>
<td class="gt_row gt_right" headers=".estimate">0.8</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>Oops! That doesn’t look right. Why is that?</p>
<p>In the context of logistic regression, R’s default behavior is to take the first level of a factor as the reference category when using the <a href="https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/glm" target="_blank"><code>glm</code></a> function. This behavior becomes particularly important when we’re dealing with binary outcomes, often coded as 0 (absence of the event) and 1 (presence of the event). By default, R will take 0 as the reference level and compare it against 1, due to 0 coming before 1 in numerical order.</p>
<p>However, when we’re working with factors, the order in which they’re arranged is somewhat arbitrary and it doesn’t necessarily make sense to always treat 0 as the absence of an event and 1 as the presence of an event <span class="citation" data-cites="kuhn2022">(Kuhn and Silge 2022)</span>.</p>
<div class="callout callout-style-simple callout-note">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p>This is from <a href="https://www.tmwr.org/performance.html">here</a> or page 116 in the tidymodels book.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://focardozom.github.io/posts/2023-07-15-yarstick/tidymodels.png" class="img-fluid figure-img"></p>
<figcaption>tidymodels. pag 116</figcaption>
</figure>
</div>
</div>
</div>
</div>
<p><strong>The <code>yardstick</code> package interprets this setup a bit differently</strong>. It views the first factor as the most important, leading it to switch the factor levels in the process. This change in order may affect our sensitivity and specificity measures.</p>
<p>To maintain consistency with our earlier computations, it’s necessary to explicitly set the reference level in the <a href="https://yardstick.tidymodels.org/reference/sens.html" target="_blank"><code>sensitivity()</code></a> and <a href="https://yardstick.tidymodels.org/reference/spec.html" target="_blank"><code>specificity()</code></a> functions from the <a href="https://yardstick.tidymodels.org/" target="_blank"><code>yardstick</code></a> functions, using the <code>event_level</code> = “second” argument. This ensures that the factor levels are interpreted in a way that aligns with our initial demonstration.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">sens_y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> the_predictions <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Observed =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(Observed)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sensitivity</span>(Observed, .pred_class, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">event_level =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"second"</span>)</span>
<span id="cb10-4"></span>
<span id="cb10-5">spec_y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> the_predictions <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Observed =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(Observed)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">specificity</span>(Observed, .pred_class, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">event_level =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"second"</span>)</span>
<span id="cb10-8"></span>
<span id="cb10-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(sens_y, spec_y) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div id="kzilijarsk" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#kzilijarsk table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#kzilijarsk thead, #kzilijarsk tbody, #kzilijarsk tfoot, #kzilijarsk tr, #kzilijarsk td, #kzilijarsk th {
  border-style: none;
}

#kzilijarsk p {
  margin: 0;
  padding: 0;
}

#kzilijarsk .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#kzilijarsk .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#kzilijarsk .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#kzilijarsk .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#kzilijarsk .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#kzilijarsk .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#kzilijarsk .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#kzilijarsk .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#kzilijarsk .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#kzilijarsk .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#kzilijarsk .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#kzilijarsk .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#kzilijarsk .gt_spanner_row {
  border-bottom-style: hidden;
}

#kzilijarsk .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#kzilijarsk .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#kzilijarsk .gt_from_md > :first-child {
  margin-top: 0;
}

#kzilijarsk .gt_from_md > :last-child {
  margin-bottom: 0;
}

#kzilijarsk .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#kzilijarsk .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#kzilijarsk .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#kzilijarsk .gt_row_group_first td {
  border-top-width: 2px;
}

#kzilijarsk .gt_row_group_first th {
  border-top-width: 2px;
}

#kzilijarsk .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#kzilijarsk .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#kzilijarsk .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#kzilijarsk .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#kzilijarsk .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#kzilijarsk .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#kzilijarsk .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#kzilijarsk .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#kzilijarsk .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#kzilijarsk .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#kzilijarsk .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#kzilijarsk .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#kzilijarsk .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#kzilijarsk .gt_left {
  text-align: left;
}

#kzilijarsk .gt_center {
  text-align: center;
}

#kzilijarsk .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#kzilijarsk .gt_font_normal {
  font-weight: normal;
}

#kzilijarsk .gt_font_bold {
  font-weight: bold;
}

#kzilijarsk .gt_font_italic {
  font-style: italic;
}

#kzilijarsk .gt_super {
  font-size: 65%;
}

#kzilijarsk .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#kzilijarsk .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#kzilijarsk .gt_indent_1 {
  text-indent: 5px;
}

#kzilijarsk .gt_indent_2 {
  text-indent: 10px;
}

#kzilijarsk .gt_indent_3 {
  text-indent: 15px;
}

#kzilijarsk .gt_indent_4 {
  text-indent: 20px;
}

#kzilijarsk .gt_indent_5 {
  text-indent: 25px;
}

#kzilijarsk .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#kzilijarsk div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings header">
<th id="a.metric" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">.metric</th>
<th id="a.estimator" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col">.estimator</th>
<th id="a.estimate" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">.estimate</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers=".metric">sensitivity</td>
<td class="gt_row gt_left" headers=".estimator">binary</td>
<td class="gt_row gt_right" headers=".estimate">0.8</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers=".metric">specificity</td>
<td class="gt_row gt_left" headers=".estimator">binary</td>
<td class="gt_row gt_right" headers=".estimate">0.7</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>Now we’ve restored our original results. 😎</p>
<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>Caution
</div>
</div>
<div class="callout-body-container callout-body">
<p>This ‘switching’ behavior in yardstick is also apparent in the <a href="https://tune.tidymodels.org/reference/collect_predictions.html" target="_blank"><code>collect_metrics()</code></a> function, making it essential to check the event level. Failure to do so may result in inadvertently switching the event level.</p>
</div>
</div>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>In this blog post, we’ve walked through the process of computing sensitivity and specificity using the tidymodels package in R, demonstrating it with a simulated dataset. These metrics are indispensable for evaluating the performance of binary classification models. Don’t forget: for the most accurate and realistic results, always evaluate your models using separate test data.</p>



</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-kuhn2022" class="csl-entry">
Kuhn, Max, and Julia Silge. 2022. <em>Tidy Modeling with r</em>. O’Reilly Media, Inc.
</div>
</div></section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>R</category>
  <category>Machine Learning</category>
  <guid>https://focardozom.github.io/posts/2023-07-15-yarstick/2023-07-15.html</guid>
  <pubDate>Sun, 16 Jul 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>An Introduction to the Recipes Package for Data Preprocessing</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <link>https://focardozom.github.io/posts/2023-05-10-recipes/recipes.html</link>
  <description><![CDATA[ 




<p>I liked this presentation done by Max Kuhn.</p>
<iframe width="853" height="480" src="https://www.youtube.com/embed/GdR_S8bYaag" title="Cooking Your Data with Recipes in R with Max Kuhn" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="">
</iframe>
<p><strong>Here are some notes:</strong></p>
<p>The <code>recipes</code> package provides a framework for preprocessing data prior to modeling or visualization. With its pipeable sequence of steps and syntax similar to <code>dplyr</code>, the package simplifies a wide range of preprocessing tasks, from data normalization and missing data imputation, to categorical variable encoding and data transformation.</p>
<p>It’s important to remember when using the <code>recipes</code> package, the type of model you’re fitting can determine the necessary preprocessing steps for your data.</p>
<p>In addition to model-driven preprocessing steps, the <code>recipes</code> package also provides functions for feature engineering. This involves representing your data in ways most effective for your particular problem. For instance, you might create interaction terms, polynomial terms, or spline terms to capture non-linear relationships between predictors and the outcome.</p>
<p>Here are some useful preprocessing steps:</p>
<ol type="1">
<li><p><strong>Data normalization</strong>: The <code>step_normalize()</code> function normalizes your data by centering and scaling the variables. This is useful when working with models that require predictors to be on the same scale, such as k-nearest neighbors or neural networks.</p></li>
<li><p><strong>Missing data imputation</strong>: The <code>step_impute_*()</code> functions impute missing data using various methods, like mean imputation, median imputation, or k-nearest neighbors imputation.</p></li>
<li><p><strong>Categorical variable encoding</strong>: The <code>step_dummy()</code> function creates dummy variables for categorical predictors. This is handy when working with models that can’t handle categorical predictors directly, like linear regression or logistic regression.</p></li>
<li><p><strong>Data transformation</strong>: The <code>step_*()</code> functions transform your data in various ways, such as applying the logarithm, square root, or Box-Cox transformation to a variable. This is useful when working with data that isn’t normally distributed or when trying to improve the linearity of the relationship between predictors and the outcome.</p></li>
<li><p><strong>Feature engineering</strong>: The <code>step_*()</code> functions are also used for feature engineering, such as creating interaction terms, polynomial terms, or spline terms. This is beneficial when trying to capture non-linear relationships between predictors and the outcome.</p></li>
</ol>
<p><a href="https://www.tidymodels.org/start/recipes/">Link to the package documentation</a></p>
<p><strong>Things to think:</strong></p>
<p>A point of confusion might be whether preprocessing is considered part of data cleaning or data transformation for modeling. It appears that there’s an overlap between data cleaning and data transformation, and it can sometimes be difficult to distinguish between these stages. It would be helpful to clarify the difference between these concepts and data preprocessing.</p>
<p>When I try to imagine where recipes fits into these models, it’s not completely clear to me.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.tmwr.org/premade/data-science-model.svg" class="img-fluid figure-img"></p>
<figcaption>The data science process. From R for Data Science</figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.tmwr.org/premade/modeling-process.svg" class="img-fluid figure-img"></p>
<figcaption>Modeling Process. From Tidymodels book</figcaption>
</figure>
</div>



<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>R</category>
  <category>Machine Learning</category>
  <guid>https://focardozom.github.io/posts/2023-05-10-recipes/recipes.html</guid>
  <pubDate>Wed, 10 May 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Maximizing the Impact of Your R Teaching: Insights from Garrett Grolemund</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <link>https://focardozom.github.io/posts/2023-01-10-posit-education/Posit.html</link>
  <description><![CDATA[ 




<p>I recently watched a presentation by Garrett Grolemund, the Director of Learning at Rstudio, on the POSIT Enterprise Community Meetup on YouTube. I wanted to share with you some of the interesting thoughts about teaching R that he mentioned.</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/ElO5gDCG3X8?start=19" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="">
</iframe>
<section id="notes" class="level1">
<h1>Notes</h1>
<section id="context" class="level2">
<h2 class="anchored" data-anchor-id="context">Context</h2>
<ul>
<li>Garret believes that the success of the training ultimately depends on how it’s delivered.</li>
<li>Teachers out there really aren’t that good and online courses are not so great.</li>
<li>People who become experts in R by years of practice. However, they may not have had the opportunity to devote those years to being a good teacher.</li>
<li>There is theories in psychology(such as cognitive load theory and multimedia learning theory) that can be applied to make training more successful.</li>
<li>Research shows that students only retain about 56% of the content from a lecture</li>
<li>The retention rate decreases over time.</li>
<li>Studies on procedural instruction show similar results, with only 60% of students able to reproduce the procedure immediately after training.</li>
<li>Six months later, the number drops to 40%, and a year later it drops to 30%</li>
<li>Training outcomes are not always great and this is a “dirty secret” of the training industry.</li>
<li>Trainers are not always trained to be trainers, which is a challenge in the field.</li>
<li>Education is not simply about transferring information from an educator to a student, and the student then becoming an expert.</li>
</ul>
</section>
<section id="insights" class="level2">
<h2 class="anchored" data-anchor-id="insights">Insights</h2>
<section id="practice" class="level3">
<h3 class="anchored" data-anchor-id="practice">Practice</h3>
<ul>
<li>The more the task is practiced, the more the brain will conserve the neural networks and retain the ability to perform the task. This is not likely to occur in a workshop that lasts only half a day or two days.</li>
<li>To build a robust neural network, it is important to sleep.</li>
<li>The ability to perform a new skill in six months from now dependent on the amount of practice they receive after initial instruction, rather than the instruction itself.</li>
</ul>
<blockquote class="blockquote">
<p>“Well, the revelation we had at our studio is that data science is a skill, and if you want to learn to do good data science with code or otherwise, you have to practice it and learn it as a skill.”</p>
</blockquote>
</section>
<section id="mentors-mates-and-accountability" class="level3">
<h3 class="anchored" data-anchor-id="mentors-mates-and-accountability">Mentors, Mates and Accountability</h3>
<ul>
<li>As a student, it is possible to practice a skill incorrectly without even being aware of it. To practice effectively and efficiently, it is crucial to receive feedback and guidance to make sure the proper techniques are being utilized.</li>
<li>Individuals do not acquire knowledge in a vacuum, they are driven by social influences and the identity they construct by participating in a community that is studying data science.</li>
<li>The lacking factor in online courses is motivation, which can be obtained through interaction with a mentor or peers. Talking to them can provide the necessary inspiration.</li>
<li>Having a mentor or being part of a group provides accountability, as one is expected to show up with something to demonstrate to them.</li>
</ul>
<blockquote class="blockquote">
<p>“So, as you go through the course, not only do you have an expert who has your back who’s coaching you, you also have fellow travelers who you could discuss things with. You could work through problems together and, you might not even realize it, but you can hold each other accountable and motivated as you go through the process.”</p>
</blockquote>
</section>
<section id="recomendations" class="level3">
<h3 class="anchored" data-anchor-id="recomendations">Recomendations</h3>
<ul>
<li><p><em>How Learning Happens</em> by Paul Kirschner and Carl. <a href="Hedentrick.(https://www.amazon.com/How-Learning-Happens-Paul-Kirschner/dp/0367184575)">link to amazon. But also consult with your librarian friend</a></p></li>
<li><p><a href="https://posit.co/products/enterprise/academy/">Visit Posit Academy</a>. You can learn in detail about the POSIT Academy model in minute 24.</p></li>
</ul>


</section>
</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>R</category>
  <category>Teaching</category>
  <guid>https://focardozom.github.io/posts/2023-01-10-posit-education/Posit.html</guid>
  <pubDate>Fri, 10 Feb 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Can ELO Predict the Outcome of Chess Games?</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <link>https://focardozom.github.io/posts/2022-12-01-ChessOlympiad22/chess-olympiad22.html</link>
  <description><![CDATA[ 




<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ChessOlympiad22)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rpart.plot)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidymodels)</span></code></pre></div></div>
</details>
</div>
<p>The <a href="https://en.wikipedia.org/wiki/Elo_rating_system" target="_blank">ELO rating system</a> is a method used to calculate the relative skill levels of chess players, based on their game results. A higher ELO rating indicates a higher perceived skill level.</p>
<p>In this document, the ELO difference between two players is used to evaluate the probability of one player winning the match.</p>
<p>I will use the The <a href="https://github.com/focardozom/ChessOlympiad22" target="_blank">ChessOlympiad</a> package. It contains two datasets: <code>players</code> and <code>results</code>. The <code>players</code> dataset contains the ELO ratings of all players participating in the 44th Chess Olympiad, which took place in Chennai, India, in 2022. The <code>results</code> dataset contains the results of all matches played in the tournament.</p>
<section id="distribution-of-elo" class="level3">
<h3 class="anchored" data-anchor-id="distribution-of-elo">Distribution of ELO</h3>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"players"</span>)</span></code></pre></div></div>
</details>
</div>
<p>First, let’s visualize the ELO distribution of the players.</p>
<p>It is important to filter out players with an ELO rating of zero from the analysis, as these players have not yet been rated and do not have a known skill level.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">players <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;">filter</span>(rtg <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;">0</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;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(rtg)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray90"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray95"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2900</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-6">    <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="cb3-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb3-8">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-9">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()</span>
<span id="cb3-10">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb3-12">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Elo Rating System (Higher values indicate greater player strength)"</span>,</span>
<span id="cb3-13">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb3-14">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Elo Rating System of players in Chess Olympiad, 2022. Chennai, India "</span>,</span>
<span id="cb3-15">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Magnus Carlsen has the highest Elo rating in the tournament: 2864"</span></span>
<span id="cb3-16">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(</span>
<span id="cb3-18">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> players <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-19">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(rtg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2800</span>),</span>
<span id="cb3-20">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(</span>
<span id="cb3-21">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2550</span>,</span>
<span id="cb3-22">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span>,</span>
<span id="cb3-23">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Magnus Carlsen"</span></span>
<span id="cb3-24">        ),</span>
<span id="cb3-25">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb3-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;">"black"</span></span>
<span id="cb3-27">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-28">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_curve</span>(</span>
<span id="cb3-29">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> players <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>(rtg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2800</span>),</span>
<span id="cb3-30">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(</span>
<span id="cb3-31">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2700</span>,</span>
<span id="cb3-32">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2864.8</span>,</span>
<span id="cb3-33">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-34">        ),</span>
<span id="cb3-35">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrow</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>)),</span>
<span id="cb3-36">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>,</span>
<span id="cb3-37">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb3-38">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">curvature =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb3-39">    )</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://focardozom.github.io/posts/2022-12-01-ChessOlympiad22/chess-olympiad22_files/figure-html/plots-with-palyers-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>It may be interesting to plot the ELO ratings of players by federation, as the Chess Olympiad is played by national teams. By examining the ELO ratings of players within each federation, we can get a sense of the overall strength of the teams participating in the event. This analysis could potentially provide insight into the results of the Chess Olympiad and help predict the outcomes of matches.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">players <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;">filter</span>(rtg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2600</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(fed, rtg), rtg)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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;">""</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;">"ELO"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://focardozom.github.io/posts/2022-12-01-ChessOlympiad22/chess-olympiad22_files/figure-html/elo-by-federation-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>According to ELO ratings, the United States fielded the strongest team in the tournament.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"results"</span>)</span></code></pre></div></div>
</details>
</div>
</section>
<section id="differences-in-elo-by-round" class="level3">
<h3 class="anchored" data-anchor-id="differences-in-elo-by-round">Differences in ELO by round</h3>
<p>The Chess Olympiad followed a Swiss-style tournament, meaning that players are paired with opponents with similar scores in each round. Specifically, in the first round, the highest-ranked player is matched against the median-ranked player, followed by the second-highest ranked player against the next below median, and so forth.</p>
<p>A visual representation of the differences in ELO by round are presented in the following graph.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(</span>
<span id="cb6-3">        elo_difference <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</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;">1000</span>,</span>
<span id="cb6-4">        elo_difference <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,</span>
<span id="cb6-5">        <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;">is.na</span>(elo_white), <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;">is.na</span>(elo_black), elo_white <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;">0</span>, elo_black <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;">0</span></span>
<span id="cb6-6">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(elo_white), elo_difference,</span>
<span id="cb6-8">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(result_white),</span>
<span id="cb6-9">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(result_white)</span>
<span id="cb6-10">    )) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(</span>
<span id="cb6-12">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>,</span>
<span id="cb6-13">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stroke =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb6-14">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-15">    <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="cb6-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lost"</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;">"black"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Draw"</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;">"gray50"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Won"</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;">"white"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lost"</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;">"black"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Draw"</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;">"black"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Won"</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;">"black"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb6-19">        <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;">"Result"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Result"</span>,</span>
<span id="cb6-20">        <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;">"Player Elo"</span>,</span>
<span id="cb6-21">        <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;">"Elo difference"</span>,</span>
<span id="cb6-22">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Difference greater than zero indicates stronger player</span></span>
<span id="cb6-23"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">       44th Chess Olympiad. Chennai, 2022 Open"</span></span>
<span id="cb6-24">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-25">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>round)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://focardozom.github.io/posts/2022-12-01-ChessOlympiad22/chess-olympiad22_files/figure-html/plot-data-diff-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="model-the-winning-chances-for-players-with-the-white-pieces-based-on-elo-difference" class="level1">
<h1>Model the winning chances for players with the white pieces based on ELO difference</h1>
<p>Now, let’s try to identify the optimal divisions in ELO rating differences that could potentially classify the outcomes of chess games. For this, I will be using the <code>tidymodels</code> package to estimate a Classification and Regression Trees (CART) model.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">cart_spec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb7-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">decision_tree</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_engine</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rpart"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_mode</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classification"</span>)</span></code></pre></div></div>
</details>
</div>
<p>I will add two steps in the recipe. One to filter the data set by round, and the other to convert results in a factor variable. I also will limit my analysis to players with more than 1600 in ELO.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> 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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(elo_white) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1600</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(elo_black) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1600</span>)</span>
<span id="cb8-4"></span>
<span id="cb8-5">recipe <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>(</span>
<span id="cb8-6">    result_white <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> elo_difference <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> round_number,</span>
<span id="cb8-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> results</span>
<span id="cb8-8">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step_filter</span>(round_number <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> round)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">wrkfl <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_model</span>(cart_spec) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_recipe</span>(recipe)</span></code></pre></div></div>
</details>
</div>
<p>Let’s estimate the model for the round 1.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">round <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb10-2"></span>
<span id="cb10-3">cart_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> wrkfl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-4">    <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> results) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_fit_parsnip</span>()</span></code></pre></div></div>
</details>
</div>
<section id="draw-an-tree-to-understand-the-results" class="level3">
<h3 class="anchored" data-anchor-id="draw-an-tree-to-understand-the-results">Draw an tree to understand the results</h3>
<p>Finally, I will create a tree showing the splits</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">cart_fit <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;">repair_call</span>(cart_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> round)</span>
<span id="cb11-2"></span>
<span id="cb11-3">cart_tree_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cart_fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit</span>
<span id="cb11-4"></span>
<span id="cb11-5">rpart.plot<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;">rpart.plot</span>(cart_tree_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">roundint =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://focardozom.github.io/posts/2022-12-01-ChessOlympiad22/chess-olympiad22_files/figure-html/draw-tree-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>According to the model, a difference of 12 in the ELO is sufficient to accurately predict the winner in 95% of cases and the loser in 91% of cases.</p>
</section>
</section>
<section id="model-the-last-round" class="level1">
<h1>Model the last round</h1>
<p>Now, let’s apply the model to the final round, which featured matches between the most formidable opponents.</p>
<p>I will add a the <code>tree_depth</code> parameter to my model. The depth of the tree refers to the number of levels the tree has.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">cart_spec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb12-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">decision_tree</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tree_depth =</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;">|&gt;</span></span>
<span id="cb12-3">    <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;">"rpart"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_mode</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classification"</span>)</span>
<span id="cb12-5"></span>
<span id="cb12-6">wrkfl <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_model</span>(cart_spec) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_recipe</span>(recipe)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">round <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;">11</span></span>
<span id="cb13-2"></span>
<span id="cb13-3">cart_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> wrkfl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> results)</span>
<span id="cb13-5"></span>
<span id="cb13-6">cart_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> wrkfl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-7">    <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> results) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_fit_parsnip</span>()</span>
<span id="cb13-9"></span>
<span id="cb13-10">cart_fit <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;">repair_call</span>(cart_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> round)</span>
<span id="cb13-11"></span>
<span id="cb13-12">cart_tree_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cart_fit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit</span>
<span id="cb13-13"></span>
<span id="cb13-14">rpart.plot<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;">rpart.plot</span>(cart_tree_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">roundint =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://focardozom.github.io/posts/2022-12-01-ChessOlympiad22/chess-olympiad22_files/figure-html/last-round-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rpart.rules</span>(cart_tree_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cover =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>  ..y  Dra Los Won                                       cover
 Draw [.45 .30 .25] when elo_difference is -176 to  85     51%
 Draw [.55 .09 .36] when elo_difference &gt;=         333      4%
 Draw [.69 .15 .15] when elo_difference is   95 to 115      4%
 Lost [.29 .61 .10] when elo_difference &lt;  -176            20%
  Won [.10 .30 .60] when elo_difference is   85 to  95      3%
  Won [.17 .20 .63] when elo_difference is  115 to 333     18%</code></pre>
</div>
</div>
<p>The color-coding of the decision tree leaves suggests that differences in ELO ratings remain a critical factor, even in the final round of the chess tournament. It seems that the model is particularly adept at predicting outcomes when the ELO differences are substantial. For example, if you have more than 333 points in ELO, the model predicts 63% wining chances for you. However, if the ELO difference is less than 333 (but more than 115), the model predicts 55% of draw.</p>
<p>When the ELO difference is less than 115, the model’s predictions become more interesting. If a player has 176 points less than their opponent, the model is more likely to classify them as a loser (61%). However, if the ELO difference is less than 85 points, the player still has a good chance of winning the game. This could be seen as an indicator of the performance of some players with lower ELO ratings who are having a strong tournament. On the other hand, if the ELO difference is greater than 85 points, most of the chances are for a draw.</p>
<p>This serves as a quick demonstration of how the ChessOlympiad package can be utilized in predictive modeling.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>R</category>
  <category>Machine Learning</category>
  <category>Chess</category>
  <guid>https://focardozom.github.io/posts/2022-12-01-ChessOlympiad22/chess-olympiad22.html</guid>
  <pubDate>Thu, 01 Dec 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Trustworthy</title>
  <dc:creator>Francisco Cardozo</dc:creator>
  <dc:creator>Pablo Montero-Zamora</dc:creator>
  <link>https://focardozom.github.io/posts/2022-11-27-trustworthy/trustworthy.html</link>
  <description><![CDATA[ 




<section id="how-much-do-we-trust-a-students-response-when-asked-if-they-have-consumed-drugs" class="level1">
<h1>How much do we trust a students’ response when asked if they have consumed drugs?</h1>
<p><a href="https://github.com/focardozom">Francisco Cardozo</a>, <a href="https://scholar.google.es/citations?user=jw7I6NUAAAAJ&amp;hl=en">Pablo Montero-Zamora</a></p>
<p>Confidence in students’ survey responses is commonly questioned when performing drug use research. For example, one’s can wonder <strong>how do you know students are telling the truth about their alcohol consumption?</strong> We recognize this as a legitimate question that can be even more complex when considering two types of students: 1) those who say they have not used alcohol but used it (i.e., deniers), and 2) those who say they have used it but never use it (i.e., braggers). It is crucial to understand how this reporting bias affects the validity of our measurements and research findings. Therefore, we propose the following app to know how the proportions of deniers, braggers, and drug use prevalence can influence confidence levels in self-reported measures collected in adolescents.</p>
<ul>
<li><strong>True prevalence:</strong> number of students using a drug divided by the population.<br>
</li>
<li><strong>Deniers:</strong> students who say they have not used a drug but used it.</li>
<li><strong>Braggers:</strong> students who say they have used a drug but never used it.</li>
</ul>
<p>Given the information above, we can estimate the probability of a student’s drug use behavior if they respond <strong>Yes</strong> or <strong>No</strong> to a drug use question.</p>
<p>To model this probability, we can use the <strong>Bayes theorem:</strong></p>
<ul>
<li><strong>P(A|B)</strong> = Drug use given they say yes in the questionnaire.</li>
<li><strong>P(B|A)</strong> = Say yes given that they have used drugs.</li>
<li><strong>P(A)</strong> = Drug use prevalence.</li>
<li><strong>P(B)</strong> = Say yes in the questionnaire.</li>
</ul>
<section id="visit-the-app-here" class="level2">
<h2 class="anchored" data-anchor-id="visit-the-app-here">Visit the app <a href="https://francisco-cardozo.shinyapps.io/thrusworthy/">here</a></h2>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>Prevention Science</category>
  <guid>https://focardozom.github.io/posts/2022-11-27-trustworthy/trustworthy.html</guid>
  <pubDate>Sun, 27 Nov 2022 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
