<?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>HOXO-M Blog</title>
<link>https://hoxo-m.github.io/blog/</link>
<atom:link href="https://hoxo-m.github.io/blog/index.xml" rel="self" type="application/rss+xml"/>
<description>Insights into R and Data Analysis</description>
<generator>quarto-1.9.38</generator>
<lastBuildDate>Wed, 17 Jun 2026 13:00:00 GMT</lastBuildDate>
<item>
  <title>TheseusPlot 0.3.0: Visualizing the Decomposition of Differences in Rate Metrics</title>
  <dc:creator>Koji Makiyama</dc:creator>
  <link>https://hoxo-m.github.io/blog/posts/TheseusPlot-0-3-0/</link>
  <description><![CDATA[ 





<p><strong>TheseusPlot</strong> is an R package that decomposes differences in a rate metric between two groups into subgroup-level contributions and visualizes the results as a “Theseus Plot”.</p>
<p>For example, when a click-through rate, conversion rate, or retention rate differs between two time periods or groups, TheseusPlot helps answer questions such as: which subgroup contributed most to the difference?</p>
<p>Suppose that the click-through rate (CTR) was 6.2% in 2024 and 5.2% in 2025, a decrease of 1.0 percentage point. A Theseus Plot can show how this decrease is decomposed: in this example, 0.8 percentage points are assigned to male users and 0.2 percentage points to female users under the decomposition.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/TheseusPlot-0-3-0/figures/example-1.png" class="img-fluid figure-img" width="500"></p>
</figure>
</div>
</div>
</div>
<p>Version 0.3.0 is now available on CRAN. This release fixes a compatibility issue with waterfalls 1.1.4, improves subgroup size bar rendering, and refines several plot defaults.</p>
<section id="whats-new-in-0.3.0" class="level2">
<h2 class="anchored" data-anchor-id="whats-new-in-0.3.0">What’s new in 0.3.0</h2>
<section id="cleaner-plot-labels" class="level3">
<h3 class="anchored" data-anchor-id="cleaner-plot-labels">Cleaner plot labels</h3>
<p>In earlier versions, TheseusPlot automatically displayed the analyzed column name as a subtitle. However, this was not always useful, especially when the plot was already used in a document or presentation where the context was clear.</p>
<p>In version 0.3.0, the automatic column-name subtitle has been removed. This makes the resulting plots cleaner and easier to combine with custom titles, captions, and surrounding text.</p>
<p>This release also adds an <code>xlab</code> argument to <code>create_ship()</code>, so you can customize the x-axis label used by <code>plot()</code> and <code>plot_flip()</code>.</p>
<p>For example:</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">ship <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;">create_ship</span>(</span>
<span id="cb1-2">  data_2024,</span>
<span id="cb1-3">  data_2025,</span>
<span id="cb1-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> clicked,</span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025"</span>),</span>
<span id="cb1-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gender"</span>,</span>
<span id="cb1-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CTR (%)"</span></span>
<span id="cb1-8">)</span>
<span id="cb1-9"></span>
<span id="cb1-10">ship<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;">plot</span>(gender)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/TheseusPlot-0-3-0/figures/xlab-1.png" class="img-fluid figure-img" width="500"></p>
</figure>
</div>
</div>
</div>
<p>This is useful when the column name in the data is short or technical, but you want a more readable label in the plot.</p>
</section>
<section id="better-default-labels" class="level3">
<h3 class="anchored" data-anchor-id="better-default-labels">Better default labels</h3>
<p>The default group labels have been changed from <code>"Original"</code> and <code>"Refitted"</code> to <code>"Baseline"</code> and <code>"Comparison"</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">ship <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;">create_ship</span>(</span>
<span id="cb2-2">  data_2024,</span>
<span id="cb2-3">  data_2025,</span>
<span id="cb2-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> clicked</span>
<span id="cb2-5">)</span>
<span id="cb2-6"></span>
<span id="cb2-7">ship<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;">plot</span>(gender)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/TheseusPlot-0-3-0/figures/no-labels-1.png" class="img-fluid figure-img" width="500"></p>
</figure>
</div>
</div>
</div>
<p>The previous labels reflected the internal idea of replacing one group with another, but they were not always intuitive for users. The new defaults better match common comparison scenarios, such as year-over-year comparisons, control versus treatment, and before-and-after analyses.</p>
<p>Of course, you can still specify your own labels:</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">ship <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;">create_ship</span>(</span>
<span id="cb3-2">  data_Nov,</span>
<span id="cb3-3">  data_Dec,</span>
<span id="cb3-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> on_time,</span>
<span id="cb3-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"November"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"December"</span>)</span>
<span id="cb3-6">)</span></code></pre></div></div>
</div>
</section>
<section id="simpler-numeric-display" class="level3">
<h3 class="anchored" data-anchor-id="simpler-numeric-display">Simpler numeric display</h3>
<p>The default number of displayed decimal places has been changed from 3 to 1.</p>
<p>In many plots, three decimal places made the labels more detailed than necessary. Since TheseusPlot is mainly intended to help users understand the structure of a metric difference, one decimal place is often enough for visual interpretation.</p>
<p>You can still control the precision with the <code>digits</code> argument when needed.</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">ship <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;">create_ship</span>(</span>
<span id="cb4-2">  data_2024,</span>
<span id="cb4-3">  data_2025,</span>
<span id="cb4-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> clicked,</span>
<span id="cb4-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025"</span>),</span>
<span id="cb4-6">  <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="cb4-7">)</span>
<span id="cb4-8"></span>
<span id="cb4-9">ship<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;">plot</span>(gender)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/TheseusPlot-0-3-0/figures/digits-1.png" class="img-fluid figure-img" width="500"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="plot-improvements-and-bug-fixes" class="level2">
<h2 class="anchored" data-anchor-id="plot-improvements-and-bug-fixes">Plot improvements and bug fixes</h2>
<p>Version 0.3.0 also includes several improvements and bug fixes related to plot rendering.</p>
<p>First, missing subgroup size bars in <code>plot()</code> and <code>plot_flip()</code> with waterfalls 1.1.4 have been fixed. Subgroup size bars are an important part of Theseus Plots because they show the sample size of each subgroup in both groups. Without them, it becomes harder to judge whether a large contribution comes from a large subgroup, a large rate difference, or both.</p>
<p>Second, subgroup size bar scaling has been improved. Bar heights are now computed consistently from the maximum plot score in both <code>plot()</code> and <code>plot_flip()</code>. This makes visual comparisons more stable across plot directions. The maximum height of these bars can still be controlled with the <code>bar_max_value</code> argument.</p>
<p>Third, <code>text_size</code> handling has been fixed when applying the current ggplot2 theme. This makes text scaling more predictable when users customize plot themes.</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">ship <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;">create_ship</span>(</span>
<span id="cb5-2">  data_2024,</span>
<span id="cb5-3">  data_2025,</span>
<span id="cb5-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> clicked,</span>
<span id="cb5-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025"</span>),</span>
<span id="cb5-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text_size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span></span>
<span id="cb5-7">)</span>
<span id="cb5-8"></span>
<span id="cb5-9">ship<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;">plot</span>(gender)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/TheseusPlot-0-3-0/figures/text_size-1.png" class="img-fluid figure-img" width="500"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="installation" class="level2">
<h2 class="anchored" data-anchor-id="installation">Installation</h2>
<p>You can install TheseusPlot from CRAN with:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TheseusPlot"</span>)</span></code></pre></div></div>
</section>
<section id="try-it-out" class="level2">
<h2 class="anchored" data-anchor-id="try-it-out">Try it out</h2>
<p>TheseusPlot is useful when you want to understand why rate metrics differ between two groups.</p>
<p>Typical examples include:</p>
<ul>
<li>click-through rate</li>
<li>conversion rate</li>
<li>retention rate</li>
<li>success rate</li>
<li>error rate</li>
</ul>
<p>For details, please see the package website:</p>
<ul>
<li><a href="https://hoxo-m.github.io/TheseusPlot/">https://hoxo-m.github.io/TheseusPlot/</a></li>
</ul>


</section>

 ]]></description>
  <category>R</category>
  <guid>https://hoxo-m.github.io/blog/posts/TheseusPlot-0-3-0/</guid>
  <pubDate>Wed, 17 Jun 2026 13:00:00 GMT</pubDate>
  <media:content url="https://hoxo-m.github.io/blog/posts/TheseusPlot-0-3-0/figures/example-1.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>TheseusPlot 0.2.0: Visualizing Decomposition of Differences in Rate Metrics</title>
  <dc:creator>Koji Makiyama</dc:creator>
  <link>https://hoxo-m.github.io/blog/posts/TheseusPlot-0-2-0/</link>
  <description><![CDATA[ 





<p><strong>TheseusPlot</strong> is an R package that decomposes differences in rate metrics between two groups into contributions from individual subgroups and visualizes the results as a “Theseus Plot”.</p>
<p>The package is inspired by the <a href="https://en.wikipedia.org/wiki/Ship_of_Theseus">Ship of Theseus</a> thought experiment. It replaces subgroup data step by step, recalculates the overall metric at each step, and interprets each change as that subgroup’s contribution to the overall difference.</p>
<p>Suppose you notice that the click-through rate is lower in 2025 than in 2024 and want to examine how a particular attribute, such as gender, contributed to the change. If you obtain a Theseus Plot like the one below, it suggests that men contributed more to the decline in click-through rate than women.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/TheseusPlot-0-2-0/man/figures/README-overview-1.png" class="img-fluid figure-img" width="500"></p>
</figure>
</div>
</div>
</div>
<section id="whats-new-in-0.2.0" class="level2">
<h2 class="anchored" data-anchor-id="whats-new-in-0.2.0">What’s new in 0.2.0</h2>
<p>Version 0.2.0 includes the following changes:</p>
<ul>
<li>a fix for continuous-variable discretization with <code>split = "rate"</code>, where bin boundaries for the second group could previously be computed from the first group’s data</li>
<li>a fix for the size bar of <code>"Sum of ... other attributes"</code>, which could incorrectly use the first group’s counts for both groups</li>
<li>a fix for warnings in <code>plot()</code> and <code>plot_flip()</code> when multiple subgroups were tied for the largest absolute contribution</li>
<li>suppression of warnings generated internally by <code>waterfalls::waterfall()</code> during plot creation.</li>
</ul>
<p>I would like to thank Kazuyuki Sano for reporting the first two issues and contributing to their fixes.</p>
</section>
<section id="installation" class="level2">
<h2 class="anchored" data-anchor-id="installation">Installation</h2>
<p>You can install <strong>TheseusPlot</strong> from CRAN with:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TheseusPlot"</span>)</span></code></pre></div></div>
</section>
<section id="try-it-out" class="level2">
<h2 class="anchored" data-anchor-id="try-it-out">Try it out</h2>
<p><strong>TheseusPlot</strong> may be useful when you want to understand why metrics such as conversion rate, retention rate, or click-through rate changed.</p>
<p>For details on how to use it, please see the package website: <a href="https://hoxo-m.github.io/TheseusPlot/">https://hoxo-m.github.io/TheseusPlot/</a>.</p>


</section>

 ]]></description>
  <category>R</category>
  <guid>https://hoxo-m.github.io/blog/posts/TheseusPlot-0-2-0/</guid>
  <pubDate>Fri, 10 Apr 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>deltatest 0.2.0: Statistical Hypothesis Testing Using the Delta Method for Online A/B Testing</title>
  <dc:creator>Koji Makiyama</dc:creator>
  <link>https://hoxo-m.github.io/blog/posts/deltatest-0-2-0/</link>
  <description><![CDATA[ 





<p>I’m happy to share a new release of <strong>deltatest</strong>.</p>
<p>This release includes two main changes:</p>
<ul>
<li>a new <code>tidy()</code> method for <code>deltatest</code> objects</li>
<li>a fix for p-value calculation in one-sided tests</li>
</ul>
<p>Before looking at what changed in this release, let’s briefly revisit the purpose of <strong>deltatest</strong>.</p>
<section id="what-deltatest-is-for" class="level2">
<h2 class="anchored" data-anchor-id="what-deltatest-is-for">What deltatest is for</h2>
<p>The <strong>deltatest</strong> package provides <code>deltatest()</code>, a function for performing two-sample Z-tests using the delta method.</p>
<p>It is designed for common settings in online A/B testing where:</p>
<ul>
<li>randomization is done at the user level, but</li>
<li>the metric is measured at a finer unit such as page views or sessions.</li>
</ul>
<p>In such settings, naive tests can underestimate uncertainty—for example, standard Z-tests, chi-squared tests, or tests for differences in proportions—because observations within a user are not independent. <code>deltatest()</code> addresses this issue by using a delta-method-based variance estimator.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install the released version from CRAN</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deltatest"</span>)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load packages</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(deltatest)</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate dummy data</span></span>
<span id="cb1-9">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> deltatest<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;">generate_dummy_data</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(group <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"control"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"treatment"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(user_id, group) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clicks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(metric), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pageviews =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run a test</span></span>
<span id="cb1-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">deltatest</span>(data, clicks <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> pageviews, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> group)</span></code></pre></div></div>
<p>Typical output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Two Sample Z-test Using the Delta Method</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; data:  clicks/pageviews by group</span></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Z = 0.31437, p-value = 0.7532</span></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; alternative hypothesis: true difference in means between control and treatment is not equal to 0</span></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 95 percent confidence interval:</span></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  -0.01410593  0.01949536</span></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; sample estimates:</span></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   mean in control mean in treatment        difference</span></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;       0.245959325       0.248654038       0.002694713</span></span></code></pre></div></div>
</section>
<section id="whats-new-in-0.2.0" class="level2">
<h2 class="anchored" data-anchor-id="whats-new-in-0.2.0">What’s new in 0.2.0</h2>
<section id="tidy-support-for-deltatest-objects" class="level3">
<h3 class="anchored" data-anchor-id="tidy-support-for-deltatest-objects"><code>tidy()</code> support for <code>deltatest</code> objects</h3>
<p>With this release, <code>deltatest()</code> results can now be converted directly into a tidy tibble with <code>broom::tidy()</code>.</p>
<p><code>deltatest()</code> returns an <code>htest</code>-class object, which is convenient for printing and interactive use. But in a tidyverse workflow, it is often much easier to work with results in a tidy tibble format. This makes it easier to combine results across many experiments or metrics, and to visualize patterns in estimates, confidence intervals, or p-values with tools like <strong>ggplot2</strong>.</p>
<p>First, here is a simple example of converting the result to a tidy format:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(deltatest)</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(broom)</span>
<span id="cb3-4"></span>
<span id="cb3-5">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> deltatest<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;">generate_dummy_data</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-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;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(group <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"control"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"treatment"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(user_id, group) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clicks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(metric), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pageviews =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span>
<span id="cb3-9"></span>
<span id="cb3-10">result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">deltatest</span>(data, clicks <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> pageviews, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> group)</span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy</span>(result)</span>
<span id="cb3-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 1 × 9</span></span>
<span id="cb3-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   estimate mean_ctrl mean_treat statistic p.value conf.low conf.high method     </span></span>
<span id="cb3-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      &lt;dbl&gt;     &lt;dbl&gt;      &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;chr&gt;      </span></span>
<span id="cb3-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1  0.00269     0.246      0.249     0.314   0.753  -0.0141    0.0195 Two Sample…</span></span>
<span id="cb3-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # ℹ 1 more variable: alternative &lt;chr&gt;</span></span></code></pre></div></div>
</div>
<p>Next, here is an example of using the tidy results to compare multiple experiments in a plot:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb4-2"></span>
<span id="cb4-3">data2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> deltatest<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;">generate_dummy_data</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xi =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-4">  <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;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(group <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"control"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"treatment"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(user_id, group) <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;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clicks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(metric), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pageviews =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span>
<span id="cb4-7"></span>
<span id="cb4-8">result2 <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;">deltatest</span>(data2, clicks <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> pageviews, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> group)</span>
<span id="cb4-9"></span>
<span id="cb4-10">result_tidy1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy</span>(result)  <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">experiment_id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"test01"</span>)</span>
<span id="cb4-11">result_tidy2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy</span>(result2) <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">experiment_id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"test02"</span>)</span>
<span id="cb4-12"></span>
<span id="cb4-13">result_tidy <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;">bind_rows</span>(result_tidy1, result_tidy2)</span>
<span id="cb4-14"></span>
<span id="cb4-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(result_tidy, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(experiment_id, estimate)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_pointrange</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;">ymin =</span> conf.low, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> conf.high)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlab</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</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;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Estimated CTR difference"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Treatment effects by experiment"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/deltatest-0-2-0/index_files/figure-html/tidy-example2-1.png" class="img-fluid figure-img" width="384"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="fix-for-one-sided-p-value-calculation" class="level3">
<h3 class="anchored" data-anchor-id="fix-for-one-sided-p-value-calculation">Fix for one-sided p-value calculation</h3>
<p>This release also fixes a bug in the p-value calculation for one-sided tests. In the previous version, p-values for one-sided tests could be incorrectly calculated using the two-sided formula. That behavior has now been fixed.</p>
<p>I would like to thank Kazuyuki Sano for reporting this issue and contributing to the fix.</p>
</section>
</section>
<section id="final-thoughts" class="level2">
<h2 class="anchored" data-anchor-id="final-thoughts">Final thoughts</h2>
<p>I’m glad to keep improving <strong>deltatest</strong> little by little. If you use R for online A/B experiments, I hope it is useful to you.</p>
<p>For more details, see:</p>
<ul>
<li>Package website: <a href="https://hoxo-m.github.io/deltatest/">https://hoxo-m.github.io/deltatest/</a></li>
<li>GitHub repository: <a href="https://github.com/hoxo-m/deltatest">https://github.com/hoxo-m/deltatest</a></li>
</ul>


</section>

 ]]></description>
  <category>R</category>
  <guid>https://hoxo-m.github.io/blog/posts/deltatest-0-2-0/</guid>
  <pubDate>Thu, 12 Mar 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>New R Package TheseusPlot: Visualizing Decomposition of Differences in Rate Metrics</title>
  <dc:creator>Koji Makiyama</dc:creator>
  <link>https://hoxo-m.github.io/blog/posts/TheseusPlot/</link>
  <description><![CDATA[ 





<section id="overview" class="level2">
<h2 class="anchored" data-anchor-id="overview">1. Overview</h2>
<p>In data analysis, when a metric differs between two groups, we sometimes want to investigate whether a particular subgroup is driving that difference. For example, when a key metric decline is detected compared to the previous year, you may want to conduct a more detailed analysis. In this analysis, you may focus on gender among the attributes and examine whether the decline occurred among male, female, or both. However, this type of analysis is challenging when the metric is a rate, because the magnitude of each subgroup’s contribution to the rate cannot be simply calculated, unlike in the case of volume metrics.</p>
<p>To address this issue, we propose an approach inspired by the story of the <em><a href="https://en.wikipedia.org/wiki/Ship_of_Theseus">Ship of Theseus</a></em>. This approach involves gradually replacing the components of one group with those of another, recalculating the metric at each step. The change in the metric at each step can then be interpreted as the contribution of each subgroup to the overall difference.</p>
<p>For instance, suppose the metric was 6.2% in 2024 and decreased to 5.2% in 2025. Again, we focus on gender. We replace the male data within the 2024 dataset with the male data from 2025 and recalculate the metric. As a result, the metric would drop by 0.8 percentage points, reaching 5.4%. In this case, the contribution of the male group to the change in the metric is -0.8 percentage points. Next, we replace the female data from 2024 with that from 2025. The dataset then consists entirely of 2025 data, and the metric drops by 0.2 percentage points, reaching 5.2%. Thus, the contribution of the female group is -0.2 percentage points.</p>
<p>When visualized, the results appear as follows:</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/TheseusPlot/index_files/figure-html/overview-1.png" class="img-fluid figure-img" width="480"></p>
</figure>
</div>
</div>
</div>
<p>From this plot, we can see that the decline in the metric is primarily driven by the male group. We call this visualization the “Theseus Plot.”</p>
<p>The <strong>TheseusPlot</strong> package is designed to make it easy to generate Theseus Plots for various attributes.</p>
</section>
<section id="installation" class="level2">
<h2 class="anchored" data-anchor-id="installation">2. Installation</h2>
<p>You can install the <strong>TheseusPlot</strong> package from <a href="https://cran.r-project.org/package=TheseusPlot">CRAN</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TheseusPlot"</span>)</span></code></pre></div></div>
<p>You can install the development version from <a href="https://github.com/hoxo-m/TheseusPlot">GitHub</a> with:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">remotes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hoxo-m/TheseusPlot"</span>)</span></code></pre></div></div>
</section>
<section id="details" class="level2">
<h2 class="anchored" data-anchor-id="details">3. Details</h2>
<section id="prepare-data" class="level3">
<h3 class="anchored" data-anchor-id="prepare-data">3.1 Prepare Data</h3>
<p>To create Theseus plots, you need two data frames that share common columns.</p>
<p>We use the 2013 New York City flight data from <a href="https://cran.r-project.org/package=nycflights13">nycflights13</a> as a demo dataset. Here, we will define the rate metric as the proportion of flights that arrived on time. In December 2013, the on-time arrival rate dropped substantially compared to November. We investigate the cause using a Theseus plot.</p>
<p>First, we create an <code>on_time</code> column in the data frame to indicate whether each flight arrived on time. Next, we extract the flights for November and December into separate data frames to form two comparison groups. The on-time arrival rate was 64% in November and dropped to 47% in December.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(nycflights13)</span>
<span id="cb3-3"></span>
<span id="cb3-4">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> flights <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</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;">is.na</span>(arr_delay)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-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;">on_time =</span> arr_delay <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;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Arrived on time</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(airlines, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"carrier"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-8">  <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;">carrier =</span> name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert carrier abbreviations to full names</span></span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(year, month, day, origin, dest, carrier, dep_delay, on_time)</span>
<span id="cb3-10"></span>
<span id="cb3-11">data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 6 × 8</span></span>
<span id="cb3-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    year month   day origin dest  carrier                dep_delay on_time</span></span>
<span id="cb3-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;chr&gt;  &lt;chr&gt; &lt;chr&gt;                      &lt;dbl&gt; &lt;lgl&gt;  </span></span>
<span id="cb3-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1  2013     1     1 EWR    IAH   United Air Lines Inc.          2 FALSE  </span></span>
<span id="cb3-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2  2013     1     1 LGA    IAH   United Air Lines Inc.          4 FALSE  </span></span>
<span id="cb3-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3  2013     1     1 JFK    MIA   American Airlines Inc.         2 FALSE  </span></span>
<span id="cb3-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4  2013     1     1 JFK    BQN   JetBlue Airways               -1 TRUE   </span></span>
<span id="cb3-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5  2013     1     1 LGA    ATL   Delta Air Lines Inc.          -6 TRUE   </span></span>
<span id="cb3-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6  2013     1     1 EWR    ORD   United Air Lines Inc.         -4 FALSE</span></span>
<span id="cb3-21"></span>
<span id="cb3-22">data_Nov <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data <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>(month <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;">11</span>)</span>
<span id="cb3-23">data_Dec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data <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>(month <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;">12</span>)</span>
<span id="cb3-24"></span>
<span id="cb3-25">data_Nov <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;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">on_time_rate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(on_time)) <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;">pull</span>(on_time_rate)</span>
<span id="cb3-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 0.6426161</span></span>
<span id="cb3-27">data_Dec <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;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">on_time_rate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(on_time)) <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;">pull</span>(on_time_rate)</span>
<span id="cb3-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 0.4672835</span></span></code></pre></div></div>
</div>
</section>
<section id="basics" class="level3">
<h3 class="anchored" data-anchor-id="basics">3.2 Basics</h3>
<p>Using the two prepared data frames, we first create a <code>ship</code> object. The <code>ship</code> object is an instance of the R6 class <code>ShipOfTheseus</code>, designed to create Theseus plots.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(TheseusPlot)</span>
<span id="cb4-2"></span>
<span id="cb4-3">ship <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;">create_ship</span>(data_Nov, data_Dec, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> on_time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"November"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"December"</span>))</span></code></pre></div></div>
</div>
<p>You can create a Theseus plot by passing column names to the <code>plot</code> method of a <code>ship</code> object. For example, to create a Theseus plot for the airport of origin:</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">ship<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;">plot</span>(origin)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/TheseusPlot/index_files/figure-html/plot_origin-1.png" class="img-fluid figure-img" width="480"></p>
</figure>
</div>
</div>
</div>
<p>New York City has three major airports, and Newark Liberty International Airport (EWR) accounted for the largest share of the decline in the on-time arrival rate.</p>
<p>Note that the number of flights at each airport matters, as a larger flight volume is expected to have a greater impact. To make this clear, the Theseus plot displays the data size for each group within each subgroup as a bar chart. From this, we see that the number of flights is similar across airports, allowing for direct comparison of contributions.</p>
<p>In summary, a Theseus plot consists of two components:</p>
<ul>
<li>A waterfall plot showing how much each subgroup contributed to the change in the metric.</li>
<li>A bar chart representing the sample size for each group within each subgroup.</li>
</ul>
<p>A <code>ship</code> object also provides the <code>table</code> method to inspect the exact values used in the Theseus plot.</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">ship<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;">table</span>(origin)</span>
<span id="cb6-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 3 × 8</span></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   origin contrib    n1    n2    x1    x2 rate1 rate2</span></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   &lt;chr&gt;    &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt;</span></span>
<span id="cb6-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1 EWR    -0.0831  9603  9410  6251  3901 0.651 0.415</span></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2 JFK    -0.0565  8645  8923  5702  4332 0.660 0.485</span></span>
<span id="cb6-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3 LGA    -0.0358  8723  8687  5379  4393 0.617 0.506</span></span></code></pre></div></div>
</div>
</section>
<section id="flipping-the-plot" class="level3">
<h3 class="anchored" data-anchor-id="flipping-the-plot">3.3 Flipping the Plot</h3>
<p>When there are many subgroups, a Theseus plot can become hard to read. In such cases, you can swap the x- and y-axes for better visualization.</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">ship<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;">plot_flip</span>(carrier)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/TheseusPlot/index_files/figure-html/plot_carrier-1.png" class="img-fluid figure-img" width="480"></p>
</figure>
</div>
</div>
</div>
<p>When the number of subgroups is large, those with small contributions are automatically grouped together. By default, this happens when there are more than 10 subgroups, but the threshold can be adjusted with the <code>n</code> argument.</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">ship<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;">plot_flip</span>(carrier, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/TheseusPlot/index_files/figure-html/prot_carrier_n-1.png" class="img-fluid figure-img" width="480"></p>
</figure>
</div>
</div>
</div>
<p>From this plot, JetBlue Airways and United Air Lines appear to have the largest contributions to the decline in on-time arrival rate.</p>
</section>
<section id="automatic-discretization-of-continuous-values" class="level3">
<h3 class="anchored" data-anchor-id="automatic-discretization-of-continuous-values">3.4 Automatic Discretization of Continuous Values</h3>
<p>Theseus plots do not directly support continuous variables. If a continuous column is provided, it is automatically discretized. For example, we can create a Theseus plot for departure delays.</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">ship<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;">plot_flip</span>(dep_delay)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/TheseusPlot/index_files/figure-html/plot_dep_delay-1.png" class="img-fluid figure-img" width="480"></p>
</figure>
</div>
</div>
</div>
<p>By default, continuous variables are discretized so that each subgroup has roughly equal sample sizes, with the number of bins set to 10. You can modify these settings by passing the return value of <code>continuous_config()</code> to the <code>continuous</code> argument.</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">ship<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;">plot_flip</span>(dep_delay, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">continuous =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">continuous_config</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/TheseusPlot/index_files/figure-html/plot_dep_delay_n-1.png" class="img-fluid figure-img" width="480"></p>
</figure>
</div>
</div>
</div>
<p>This result shows that both a decrease in on-time departures and an increase in delayed departures contributed to the decline in on-time arrival rate.</p>


</section>
</section>

 ]]></description>
  <category>R</category>
  <guid>https://hoxo-m.github.io/blog/posts/TheseusPlot/</guid>
  <pubDate>Sat, 30 Aug 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>New R Package deltatest: Statistical Hypothesis Testing Using the Delta Method for Online A/B Testing</title>
  <dc:creator>Koji Makiyama</dc:creator>
  <link>https://hoxo-m.github.io/blog/posts/deltatest/</link>
  <description><![CDATA[ 





<p>In online A/B testing, we often face a significant practical challenge: the randomization unit differs from the analysis unit. Typically, control and treatment groups are randomly assigned at the user level, while metrics—such as click-through rate—are measured at a more granular level (e.g., per page-view). In this case, the randomization unit is user, but the analysis unit is page-view.</p>
<p>This discrepancy raises concerns for statistical hypothesis testing, which assumes that data points are independent and identically distributed (i.i.d.). Specifically, a single user can generate multiple page-views, and each user may have a different probability of clicking. Consequently, the data may exhibit within-user correlation, thereby violating the i.i.d. assumption.</p>
<p>When the standard Z-test is applied to such correlated data, the resulting p-values do not follow the expected uniform distribution under the null hypothesis. As a result, smaller p-values tend to occur more frequently even when there is no true difference, increasing the risk of falsely detecting a significant difference.</p>
<p><img src="https://hoxo-m.github.io/blog/posts/deltatest/20250305012135.png" class="img-fluid"></p>
<p>To address this problem, Deng et al.&nbsp;(2018) proposed a modified statistical hypothesis testing method. Their approach replaces the standard variance estimation formula in the Z-test with an approximate formula derived via the Delta method, which accounts for within-user correlation. To simplify the application of this method, the <strong>deltatest</strong> package has been developed.</p>
<p>To illustrate how to use this package, we prepare a data frame that includes columns for the number of clicks and page-views aggregated for each user. This data frame also contains a column indicating whether each user was assigned to the control or treatment group.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-2"></span>
<span id="cb1-3">n_user <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;">2000</span></span>
<span id="cb1-4"></span>
<span id="cb1-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;">314</span>)</span>
<span id="cb1-6">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> deltatest<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;">generate_dummy_data</span>(n_user) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(group <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"control"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"treatment"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(user_id, group) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clicks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(metric), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pageviews =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span>
<span id="cb1-10"></span>
<span id="cb1-11">data</span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # A tibble: 2,000 × 4</span></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    user_id group     clicks pageviews</span></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      &lt;int&gt; &lt;chr&gt;      &lt;int&gt;     &lt;int&gt;</span></span>
<span id="cb1-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  1       1 treatment      1         6</span></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  2       2 treatment      2        11</span></span>
<span id="cb1-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  3       3 control        0        17</span></span>
<span id="cb1-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  4       4 control        4        12</span></span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  5       5 control        5        10</span></span>
<span id="cb1-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  6       6 control        1        15</span></span>
<span id="cb1-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  7       7 control        2         6</span></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  8       8 treatment      2        11</span></span>
<span id="cb1-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  9       9 treatment      2        16</span></span>
<span id="cb1-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 10      10 control        0        17</span></span>
<span id="cb1-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; # ℹ 1,990 more rows</span></span></code></pre></div></div>
</div>
<p>The statistical hypothesis test using the Delta method can then be performed on this data as follows:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(deltatest)</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">deltatest</span>(data, clicks <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> pageviews, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> group)</span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Two Sample Z-test Using the Delta Method</span></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; </span></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; data:  clicks/pageviews by group</span></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Z = 0.31437, p-value = 0.7532</span></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; alternative hypothesis: true difference in means between control and treatment is not equal to 0</span></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 95 percent confidence interval:</span></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  -0.01410593  0.01949536</span></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; sample estimates:</span></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   mean in control mean in treatment        difference </span></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;       0.245959325       0.248654038       0.002694713</span></span></code></pre></div></div>
</div>
<p>This version of the Z-test yields p-values that follow the expected uniform distribution under the null hypothesis, even when within-user correlation is present.</p>
<p><img src="https://hoxo-m.github.io/blog/posts/deltatest/20250305015654.png" class="img-fluid"></p>
<p>For more details, refer to <a href="https://hoxo-m.github.io/deltatest/">https://hoxo-m.github.io/deltatest/</a>.</p>
<section id="installation" class="level2">
<h2 class="anchored" data-anchor-id="installation">Installation</h2>
<p>You can install the <strong>deltatest</strong> package from CRAN.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deltatest"</span>)</span></code></pre></div></div>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li><a href="https://cran.r-project.org/package=deltatest">CRAN: Package deltatest</a></li>
<li><a href="https://github.com/hoxo-m/deltatest">hoxo-m/deltatest: Statistical Hypothesis Testing Using the Delta Method for Online A/B Testing</a></li>
<li>Deng, A., Knoblich, U., &amp; Lu, J. (2018). Applying the Delta Method in Metric Analytics: A Practical Guide with Novel Ideas. <em>Proceedings of the 24th ACM SIGKDD International Conference on Knowledge Discovery &amp; Data Mining.</em></li>
</ul>


</section>

 ]]></description>
  <category>R</category>
  <guid>https://hoxo-m.github.io/blog/posts/deltatest/</guid>
  <pubDate>Sat, 15 Mar 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>MLE Adjustment for High-Dimensional Logistic Regression</title>
  <dc:creator>Koji Makiyama</dc:creator>
  <link>https://hoxo-m.github.io/blog/posts/adjustMLE/</link>
  <description><![CDATA[ 





<p>The maximum likelihood estimator (MLE) of the logistic regression model is not an unbiased estimator. Therefore, estimates calculated with <code>glm()</code> contain bias. Since the MLE satisfies consistency and asymptotic normality, the bias can be disregarded when the sample size is large. However, in the analysis of high-dimensional data, the sample size is sometimes relatively small compared to the dimension of input variables.</p>
<p>For example, let’s consider a scenario where the number of input variables <code>p = 300</code>, and the sample size <code>n = 2000</code>. Additionally, the true parameters beta consist of</p>
<ul>
<li><code>beta = 10</code> for the first 1/3,</li>
<li><code>beta = -10</code> for the next 1/3, and</li>
<li><code>beta = 0</code> for the remaining 1/3.</li>
</ul>
<p>In such a case, the MLE returned by <code>glm()</code> contains a non-negligible bias.</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">p <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;">300</span></span>
<span id="cb1-2">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span></span>
<span id="cb1-3"></span>
<span id="cb1-4"><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;">314</span>)</span>
<span id="cb1-5">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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>n))</span>
<span id="cb1-6">X <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> p)</span>
<span id="cb1-7">beta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</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;">10</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> p, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-8">prob <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;">plogis</span>(X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> beta)</span>
<span id="cb1-9">y <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;">rbinom</span>(n, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, prob)</span>
<span id="cb1-10"></span>
<span id="cb1-11">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;">glm</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;">family =</span> binomial, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>())</span>
<span id="cb1-15">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(p), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(fit)[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb1-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(index, mle)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"segment"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</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;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>), </span>
<span id="cb1-19">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</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;">10</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-20">  <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;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylim</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;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</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;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Index of parameters"</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;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MLE"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True (black line) and MLE (blue point)"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/adjustMLE/index_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>You can see that the blue points (MLE) are significantly outside the perimeter of the black line (true).</p>
<p>The purpose of this package is to alleviate the bias by adjusting the MLE. To achieve this, we implemented two methods:</p>
<ul>
<li>“ProbeFrontier,” as proposed by Sur and Candès (2018), and</li>
<li>“SLOE,” as proposed by Yadlowsky et al.&nbsp;(2021).</li>
</ul>
<p>The <code>adjustMLE</code> function in our package is designed to mitigate this bias.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(adjustMLE)</span>
<span id="cb2-2"></span>
<span id="cb2-3">fit_adj <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;">adjustMLE</span>(fit)</span>
<span id="cb2-4"></span>
<span id="cb2-5">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(p), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(fit_adj)[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(index, mle)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"segment"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</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;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>), </span>
<span id="cb2-9">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</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;">10</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-10">  <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;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylim</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;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</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;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Index of parameters"</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;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adjusted MLE"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True (black line) and adjusted MLE (blue point)"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://hoxo-m.github.io/blog/posts/adjustMLE/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>For more details, refer to <a href="https://github.com/hoxo-m/adjustMLE">https://github.com/hoxo-m/adjustMLE</a>.</p>
<section id="installation" class="level2">
<h2 class="anchored" data-anchor-id="installation">Installation</h2>
<p>You can install the package from GitHub.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"remotes"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># if you have not installed "remotes" package</span></span>
<span id="cb3-2">remotes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hoxo-m/adjustMLE"</span>)</span></code></pre></div></div>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li>Sur, P., &amp; Candès, E.J. (2018). A modern maximum-likelihood theory for high-dimensional logistic regression. Proceedings of the National Academy of Sciences of the United States of America, 116, 14516 - 14525.</li>
<li>Yadlowsky, S., Yun, T., McLean, C.Y., &amp; D’Amour, A. (2021). SLOE: A Faster Method for Statistical Inference in High-Dimensional Logistic Regression. Neural Information Processing Systems.</li>
</ul>


</section>

 ]]></description>
  <category>R</category>
  <guid>https://hoxo-m.github.io/blog/posts/adjustMLE/</guid>
  <pubDate>Fri, 08 Nov 2024 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
