<?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>UchidaMizuki</title>
<link>https://uchidamizuki.quarto.pub/blog/en/</link>
<atom:link href="https://uchidamizuki.quarto.pub/blog/en/index.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.6.40</generator>
<lastBuildDate>Tue, 25 Nov 2025 15:00:00 GMT</lastBuildDate>
<item>
  <title>Reconstructing an income distribution from the median and Gini coefficient using the log-logistic distribution</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2025/11/income-distribution-from-median-and-gini-log-logistic.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2025/11/income-distribution-from-median-and-gini-log-logistic.html">my Japanese article</a>.</p>
<p>In this article, I introduce a simple method for estimating an income distribution using only two representative statistics: the median income<sup>1</sup> and the Gini coefficient<sup>2</sup>.</p>
<p>In this article, I focus on the <a href="https://en.wikipedia.org/wiki/Log-logistic_distribution">log-logistic distribution</a> as a probability distribution that can easily reproduce an income distribution from these two statistics.</p>
<section id="modeling-the-income-distribution" class="level2">
<h2 class="anchored" data-anchor-id="modeling-the-income-distribution">Modeling the income distribution</h2>
<p>The log-logistic distribution is also known in economics as the Fisk distribution, and it is commonly used to represent income distributions. The advantage of this probability distribution is that its parameters can be easily estimated from the median income and the Gini coefficient.</p>
<p>Specifically, this probability distribution has two parameters, a scale parameter <img src="https://latex.codecogs.com/png.latex?%5Calpha"> and a shape parameter <img src="https://latex.codecogs.com/png.latex?%5Cbeta">, and the following relationships are known to hold for these parameters. Using these relationships, we can easily set the parameters of the log-logistic distribution from the median income and the Gini coefficient.</p>
<ul>
<li><p>The scale parameter <img src="https://latex.codecogs.com/png.latex?%5Calpha"> equals the median income <img src="https://latex.codecogs.com/png.latex?m"> <img src="https://latex.codecogs.com/png.latex?%0A%5Calpha%20=%20m%0A"></p></li>
<li><p>The shape parameter <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> equals the reciprocal of the Gini coefficient <img src="https://latex.codecogs.com/png.latex?G"> <img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta%20=%20%5Cfrac%7B1%7D%7BG%7D%0A"></p></li>
</ul>
</section>
<section id="data-used" class="level2">
<h2 class="anchored" data-anchor-id="data-used">Data used</h2>
<p>In this article, I use the following data published by the OECD in <a href="https://www.oecd.org/en/publications/society-at-a-glance-2024_918d8db3-en.html">Society at a Glance 2024: OECD Social Indicators</a>.</p>
<ul>
<li>Median household income<sup>3</sup>: <a href="https://www.oecd.org/en/publications/society-at-a-glance-2024_918d8db3-en/full-report/household-income_3ee61044.html#title-b344384343">Annual median equivalised disposable income, in 2021, USD at PPP rates</a></li>
<li>Gini coefficient: <a href="https://www.oecd.org/en/publications/society-at-a-glance-2024_918d8db3-en/full-report/income-and-wealth-inequalities_7ac4178f.html#title-dd39bc1fa5">Gini coefficient of household disposable income and gap between richest and poorest 10%, in 2021</a></li>
</ul>
<p>First, download the data with the following code.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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>(fs)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"></span>
<span id="cb1-4">file_median_income <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"income-distribution-from-median-and-gini-log-logistic/median_income_oecd_2021.xlsx"</span></span>
<span id="cb1-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;">file_exists</span>(file_median_income)) {</span>
<span id="cb1-6"> curl<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;">curl_download</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://stat.link/1or2b8"</span>, file_median_income)</span>
<span id="cb1-7">}</span>
<span id="cb1-8"></span>
<span id="cb1-9">file_gini_coefficient <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"income-distribution-from-median-and-gini-log-logistic/gini_coefficient_oecd_2021.xlsx"</span></span>
<span id="cb1-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;">file_exists</span>(file_gini_coefficient)) {</span>
<span id="cb1-11"> curl<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;">curl_download</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://stat.link/3f18vc"</span>, file_gini_coefficient)</span>
<span id="cb1-12">}</span></code></pre></div>
</details>
</div>
</section>
<section id="visualizing-the-income-distribution" class="level2">
<h2 class="anchored" data-anchor-id="visualizing-the-income-distribution">Visualizing the income distribution</h2>
<p>First, load the necessary data from the downloaded Excel files.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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>(readxl)</span>
<span id="cb2-2"></span>
<span id="cb2-3">median_income <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_excel</span>(</span>
<span id="cb2-4">  file_median_income,</span>
<span id="cb2-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sheet =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Data4.1"</span>,</span>
<span id="cb2-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">range =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cell_limits</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ul =</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;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lr =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)),</span>
<span id="cb2-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_names =</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;">"country_name"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country_code"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median_income_usd_ppp"</span>),</span>
<span id="cb2-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_types =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"numeric"</span>)</span>
<span id="cb2-9">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(country_name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(country_name)</span>
<span id="cb2-12"></span>
<span id="cb2-13">gini_coefficient <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_excel</span>(</span>
<span id="cb2-14">  file_gini_coefficient,</span>
<span id="cb2-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sheet =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>,</span>
<span id="cb2-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">range =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cell_limits</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ul =</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;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lr =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)),</span>
<span id="cb2-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_names =</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;">"country_name"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gini_coefficient"</span>),</span>
<span id="cb2-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_types =</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;">"text"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"numeric"</span>)</span>
<span id="cb2-19">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(country_name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(country_name)</span></code></pre></div>
</details>
</div>
<p>The loaded data are as follows.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">median_income</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["country_name"],"name":[1],"type":["chr"],"align":["left"]},{"label":["country_code"],"name":[2],"type":["chr"],"align":["left"]},{"label":["median_income_usd_ppp"],"name":[3],"type":["dbl"],"align":["right"]}],"data":[{"1":"Australia","2":"AUS","3":"36834.837"},{"1":"Austria","2":"AUT","3":"37714.846"},{"1":"Belgium","2":"BEL","3":"37110.441"},{"1":"Bulgaria","2":"BLG","3":"14990.033"},{"1":"Canada","2":"CAN","3":"39387.628"},{"1":"Chile","2":"CHL","3":"10100.831"},{"1":"Costa Rica","2":"CRI","3":"8915.386"},{"1":"Croatia","2":"HRV","3":"19680.079"},{"1":"Czechia","2":"CZE","3":"23802.299"},{"1":"Denmark","2":"DNK","3":"34061.237"},{"1":"Estonia","2":"EST","3":"26074.691"},{"1":"Finland","2":"FIN","3":"30726.570"},{"1":"France","2":"FRA","3":"30622.362"},{"1":"Germany","2":"DEU","3":"35536.523"},{"1":"Greece","2":"GRC","3":"16773.929"},{"1":"Hungary","2":"HUN","3":"15361.443"},{"1":"Iceland","2":"ISL","3":"36852.796"},{"1":"Ireland","2":"IRL","3":"31392.192"},{"1":"Israel","2":"ISR","3":"21365.833"},{"1":"Italy","2":"ITA","3":"27949.214"},{"1":"Japan","2":"JPN","3":"21281.542"},{"1":"Korea","2":"KOR","3":"31881.753"},{"1":"Latvia","2":"LVA","3":"19908.287"},{"1":"Lithuania","2":"LTU","3":"20856.016"},{"1":"Luxembourg","2":"LUX","3":"49747.636"},{"1":"Mexico","2":"MEX","3":"6090.371"},{"1":"Netherlands","2":"NLD","3":"35891.178"},{"1":"New Zealand","2":"NZL","3":"32158.179"},{"1":"Norway","2":"NOR","3":"41620.755"},{"1":"OECD","2":"OECD","3":"27896.910"},{"1":"Poland","2":"POL","3":"24263.746"},{"1":"Portugal","2":"PRT","3":"19147.071"},{"1":"Romania","2":"ROM","3":"15897.789"},{"1":"Slovak Republic","2":"SVK","3":"16409.566"},{"1":"Slovenia","2":"SVN","3":"28697.901"},{"1":"South Africa","2":"ZAF","3":"6067.958"},{"1":"Spain","2":"ESP","3":"26629.704"},{"1":"Sweden","2":"SWE","3":"33471.533"},{"1":"Switzerland","2":"CHE","3":"39697.516"},{"1":"Türkiye","2":"TUR","3":"10340.996"},{"1":"United Kingdom","2":"GBR","3":"26883.862"},{"1":"United States","2":"USA","3":"46625.000"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">gini_coefficient</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["country_name"],"name":[1],"type":["chr"],"align":["left"]},{"label":["gini_coefficient"],"name":[2],"type":["dbl"],"align":["right"]}],"data":[{"1":"Australia","2":"0.3180000"},{"1":"Austria","2":"0.2808453"},{"1":"Belgium","2":"0.2555835"},{"1":"Bulgaria","2":"0.3832172"},{"1":"Canada","2":"0.2920000"},{"1":"Chile","2":"0.4481475"},{"1":"Costa Rica","2":"0.4718672"},{"1":"Croatia","2":"0.2876739"},{"1":"Czechia","2":"0.2548822"},{"1":"Denmark","2":"0.2678000"},{"1":"Estonia","2":"0.3214108"},{"1":"Finland","2":"0.2734400"},{"1":"France","2":"0.2980000"},{"1":"Germany","2":"0.3025600"},{"1":"Greece","2":"0.3123081"},{"1":"Hungary","2":"0.2777098"},{"1":"Iceland","2":"0.2496089"},{"1":"Ireland","2":"0.2911581"},{"1":"Israel","2":"0.3398200"},{"1":"Italy","2":"0.3296868"},{"1":"Japan","2":"0.3340000"},{"1":"Korea","2":"0.3330000"},{"1":"Latvia","2":"0.3434060"},{"1":"Lithuania","2":"0.3664048"},{"1":"Luxembourg","2":"0.2838724"},{"1":"Mexico","2":"0.4204694"},{"1":"Netherlands","2":"0.2970000"},{"1":"New Zealand","2":"0.3200000"},{"1":"Norway","2":"0.2850800"},{"1":"OECD","2":"0.3128692"},{"1":"Poland","2":"0.2609328"},{"1":"Portugal","2":"0.3126923"},{"1":"Romania","2":"0.3169575"},{"1":"Slovak Republic","2":"0.2166340"},{"1":"Slovenia","2":"0.2415357"},{"1":"South Africa","2":"0.6175785"},{"1":"Spain","2":"0.3195552"},{"1":"Sweden","2":"0.2860000"},{"1":"Switzerland","2":"0.3198277"},{"1":"Türkiye","2":"0.4028740"},{"1":"United Kingdom","2":"0.3540643"},{"1":"United States","2":"0.3953721"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>Next, join the loaded data and compute the parameters of the log-logistic distribution.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">income_distribution_params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> median_income <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inner_join</span>(gini_coefficient, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join_by</span>(country_name)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb5-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale_param =</span> median_income_usd_ppp,</span>
<span id="cb5-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape_param =</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> gini_coefficient,</span>
<span id="cb5-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unused"</span></span>
<span id="cb5-7">  )</span>
<span id="cb5-8">income_distribution_params</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["country_name"],"name":[1],"type":["chr"],"align":["left"]},{"label":["country_code"],"name":[2],"type":["chr"],"align":["left"]},{"label":["scale_param"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["shape_param"],"name":[4],"type":["dbl"],"align":["right"]}],"data":[{"1":"Australia","2":"AUS","3":"36834.837","4":"3.144654"},{"1":"Austria","2":"AUT","3":"37714.846","4":"3.560679"},{"1":"Belgium","2":"BEL","3":"37110.441","4":"3.912615"},{"1":"Bulgaria","2":"BLG","3":"14990.033","4":"2.609487"},{"1":"Canada","2":"CAN","3":"39387.628","4":"3.424658"},{"1":"Chile","2":"CHL","3":"10100.831","4":"2.231408"},{"1":"Costa Rica","2":"CRI","3":"8915.386","4":"2.119240"},{"1":"Croatia","2":"HRV","3":"19680.079","4":"3.476158"},{"1":"Czechia","2":"CZE","3":"23802.299","4":"3.923381"},{"1":"Denmark","2":"DNK","3":"34061.237","4":"3.734130"},{"1":"Estonia","2":"EST","3":"26074.691","4":"3.111283"},{"1":"Finland","2":"FIN","3":"30726.570","4":"3.657109"},{"1":"France","2":"FRA","3":"30622.362","4":"3.355705"},{"1":"Germany","2":"DEU","3":"35536.523","4":"3.305130"},{"1":"Greece","2":"GRC","3":"16773.929","4":"3.201966"},{"1":"Hungary","2":"HUN","3":"15361.443","4":"3.600881"},{"1":"Iceland","2":"ISL","3":"36852.796","4":"4.006267"},{"1":"Ireland","2":"IRL","3":"31392.192","4":"3.434561"},{"1":"Israel","2":"ISR","3":"21365.833","4":"2.942734"},{"1":"Italy","2":"ITA","3":"27949.214","4":"3.033182"},{"1":"Japan","2":"JPN","3":"21281.542","4":"2.994012"},{"1":"Korea","2":"KOR","3":"31881.753","4":"3.003003"},{"1":"Latvia","2":"LVA","3":"19908.287","4":"2.912005"},{"1":"Lithuania","2":"LTU","3":"20856.016","4":"2.729222"},{"1":"Luxembourg","2":"LUX","3":"49747.636","4":"3.522709"},{"1":"Mexico","2":"MEX","3":"6090.371","4":"2.378294"},{"1":"Netherlands","2":"NLD","3":"35891.178","4":"3.367003"},{"1":"New Zealand","2":"NZL","3":"32158.179","4":"3.125000"},{"1":"Norway","2":"NOR","3":"41620.755","4":"3.507787"},{"1":"OECD","2":"OECD","3":"27896.910","4":"3.196224"},{"1":"Poland","2":"POL","3":"24263.746","4":"3.832404"},{"1":"Portugal","2":"PRT","3":"19147.071","4":"3.198032"},{"1":"Romania","2":"ROM","3":"15897.789","4":"3.154997"},{"1":"Slovak Republic","2":"SVK","3":"16409.566","4":"4.616080"},{"1":"Slovenia","2":"SVN","3":"28697.901","4":"4.140175"},{"1":"South Africa","2":"ZAF","3":"6067.958","4":"1.619227"},{"1":"Spain","2":"ESP","3":"26629.704","4":"3.129350"},{"1":"Sweden","2":"SWE","3":"33471.533","4":"3.496503"},{"1":"Switzerland","2":"CHE","3":"39697.516","4":"3.126684"},{"1":"Türkiye","2":"TUR","3":"10340.996","4":"2.482166"},{"1":"United Kingdom","2":"GBR","3":"26883.862","4":"2.824346"},{"1":"United States","2":"USA","3":"46625.000","4":"2.529263"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>Next, visualize the income distribution for each country. In the code below, I plot the income distribution of the G7 countries using the probability density function of the log-logistic distribution.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">data_income_distribution_params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> income_distribution_params <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># G7</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(</span>
<span id="cb6-4">    country_name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span></span>
<span id="cb6-5">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb6-6">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Canada"</span>,</span>
<span id="cb6-7">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"France"</span>,</span>
<span id="cb6-8">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Germany"</span>,</span>
<span id="cb6-9">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Italy"</span>,</span>
<span id="cb6-10">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Japan"</span>,</span>
<span id="cb6-11">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"United Kingdom"</span>,</span>
<span id="cb6-12">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"United States"</span></span>
<span id="cb6-13">      )</span>
<span id="cb6-14">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(</span>
<span id="cb6-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">income =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span>
<span id="cb6-17">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb6-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">density =</span> VGAM<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;">dfisk</span>(</span>
<span id="cb6-20">      income,</span>
<span id="cb6-21">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> scale_param,</span>
<span id="cb6-22">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape1.a =</span> shape_param</span>
<span id="cb6-23">    )</span>
<span id="cb6-24">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</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;">c</span>(scale_param, shape_param))</span>
<span id="cb6-26"></span>
<span id="cb6-27">plot_income_distribution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data_income_distribution_params <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> income, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> density, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> country_name)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-30">  <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;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dollar_format</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prefix =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"$"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">big.mark =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comma_format</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0001</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb6-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Estimated income distribution of OECD countries (log-logistic distribution)"</span>,</span>
<span id="cb6-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Income (USD PPP)"</span>,</span>
<span id="cb6-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Probability density"</span>,</span>
<span id="cb6-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Country"</span></span>
<span id="cb6-37">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span>
<span id="cb6-39"></span>
<span id="cb6-40">plotly<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;">ggplotly</span>(plot_income_distribution)</span></code></pre></div>
<div class="cell-output-display">
<div class="plotly html-widget html-fill-item" id="htmlwidget-4acdafc47d0d99386fb3" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="htmlwidget-4acdafc47d0d99386fb3">{"x":{"data":[{"x":[0,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000,11000,12000,13000,14000,15000,16000,17000,18000,19000,20000,21000,22000,23000,24000,25000,26000,27000,28000,29000,30000,31000,32000,33000,34000,35000,36000,37000,38000,39000,40000,41000,42000,43000,44000,45000,46000,47000,48000,49000,50000,51000,52000,53000,54000,55000,56000,57000,58000,59000,60000,61000,62000,63000,64000,65000,66000,67000,68000,69000,70000,71000,72000,73000,74000,75000,76000,77000,78000,79000,80000,81000,82000,83000,84000,85000,86000,87000,88000,89000,90000,91000,92000,93000,94000,95000,96000,97000,98000,99000,100000,101000,102000,103000,104000,105000,106000,107000,108000,109000,110000,111000,112000,113000,114000,115000,116000,117000,118000,119000,120000,121000,122000,123000,124000,125000,126000,127000,128000,129000,130000,131000,132000,133000,134000,135000,136000,137000,138000,139000,140000,141000,142000,143000,144000,145000,146000,147000,148000,149000,150000],"y":[6.2673432059471163e-16,1.1777499470089848e-08,6.3229340536610095e-08,1.6895970330132547e-07,3.3923527969740933e-07,5.8221005384866047e-07,9.0453753393014771e-07,1.3115698803912088e-06,1.8073795347521747e-06,2.3946872346288967e-06,3.0747375402576935e-06,3.8471482263829794e-06,4.7097545054094589e-06,5.6584671658208506e-06,6.6871629692893845e-06,7.7876248663246906e-06,8.9495480631329325e-06,1.0160625270765466e-05,1.1406720380948462e-05,1.2672134356207187e-05,1.3939960548680338e-05,1.519251945841399e-05,1.6411855785817603e-05,1.7580274314426684e-05,1.8680886468895702e-05,1.96981369902596e-05,2.0618280468312849e-05,2.1429780547820603e-05,2.2123610195695904e-05,2.2693438865837152e-05,2.3135700877923788e-05,2.3449547886656689e-05,2.3636696055036297e-05,2.3701184729994697e-05,2.3649067583781277e-05,2.3488059159257367e-05,2.3227159649827134e-05,2.2876278883335698e-05,2.24458773302993e-05,2.1946638038311336e-05,2.1389179200697943e-05,2.0783813014237463e-05,2.0140352874863058e-05,1.9467967991631209e-05,1.8775082250198293e-05,1.8069312622750664e-05,1.7357441534698638e-05,1.6645417255077787e-05,1.593837645717076e-05,1.5240683478454297e-05,1.4555981386103078e-05,1.3887250635004231e-05,1.3236871818464192e-05,1.260668970584604e-05,1.1998076401844967e-05,1.1411992028872987e-05,1.0849041818019407e-05,1.030952889428041e-05,9.7935023625972008e-06,9.3008005503928196e-06,8.8310894490841334e-06,8.3838965312884009e-06,7.9586402116602247e-06,7.5546552762229236e-06,7.1712146353961072e-06,6.8075477662342159e-06,6.4628562051289572e-06,6.1363264377974604e-06,5.8271405122660336e-06,5.5344846754526067e-06,5.2575563068761327e-06,4.9955693954611164e-06,4.7477587784030452e-06,4.5133833353279416e-06,4.2917283069571217e-06,4.0821068854300756e-06,3.8838612034462934e-06,3.6963628314672952e-06,3.5190128763037098e-06,3.3512417603886501e-06,3.1925087487669518e-06,3.042301280158463e-06,2.9001341492228074e-06,2.765548579211506e-06,2.6381112173908186e-06,2.5174130798177518e-06,2.4030684671273158e-06,2.2947138688241564e-06,2.1920068700647807e-06,2.0946250719757513e-06,2.0022650340974536e-06,1.9146412455017364e-06,1.8314851294436489e-06,1.7525440850180019e-06,1.6775805681557586e-06,1.6063712133720724e-06,1.5387059969344134e-06,1.4743874415252183e-06,1.413229862005097e-06,1.3550586515178435e-06,1.2997096069002564e-06,1.2470282921519078e-06,1.1968694385710781e-06,1.1490963800611079e-06,1.1035805220480765e-06,1.0602008424186546e-06,1.0188434228791912e-06,9.79401009149084e-07,9.4177259842853265e-07,9.0586305261943942e-07,8.7158273582550539e-07,8.3884717471118013e-07,8.0757674035689936e-07,7.7769635030867276e-07,7.4913518958203045e-07,7.2182644944270286e-07,6.9570708284855838e-07,6.7071757549815988e-07,6.4680173149098733e-07,6.2390647266197112e-07,6.0198165070862686e-07,5.8097987128243272e-07,5.60856329266989e-07,5.4156865451400314e-07,5.2307676735429315e-07,5.05342743244423e-07,4.8833068595095815e-07,4.7200660871297016e-07,4.5633832286016039e-07,4.4129533339815834e-07,4.2684874110496149e-07,4.1297115071256997e-07,3.9963658477632729e-07,3.8682040286089135e-07,3.744992256966284e-07,3.6265086398332689e-07,3.5125425153981546e-07,3.4028938251826348e-07,3.2973725242075633e-07,3.1957980267341859e-07,3.0979986852962983e-07,3.0038113008928618e-07,2.913080662352317e-07,2.8256591130131234e-07,2.7414061429886228e-07,2.6601880053992557e-07,2.5818773550636667e-07,2.5063529082387971e-07,2.4334991220936994e-07,2.3632058926876449e-07,2.2953682703045733e-07],"text":["income:      0<br />density: 6.267343e-16<br />country_name: Canada","income:   1000<br />density: 1.177750e-08<br />country_name: Canada","income:   2000<br />density: 6.322934e-08<br />country_name: Canada","income:   3000<br />density: 1.689597e-07<br />country_name: Canada","income:   4000<br />density: 3.392353e-07<br />country_name: Canada","income:   5000<br />density: 5.822101e-07<br />country_name: Canada","income:   6000<br />density: 9.045375e-07<br />country_name: Canada","income:   7000<br />density: 1.311570e-06<br />country_name: Canada","income:   8000<br />density: 1.807380e-06<br />country_name: Canada","income:   9000<br />density: 2.394687e-06<br />country_name: Canada","income:  10000<br />density: 3.074738e-06<br />country_name: Canada","income:  11000<br />density: 3.847148e-06<br />country_name: Canada","income:  12000<br />density: 4.709755e-06<br />country_name: Canada","income:  13000<br />density: 5.658467e-06<br />country_name: Canada","income:  14000<br />density: 6.687163e-06<br />country_name: Canada","income:  15000<br />density: 7.787625e-06<br />country_name: Canada","income:  16000<br />density: 8.949548e-06<br />country_name: Canada","income:  17000<br />density: 1.016063e-05<br />country_name: Canada","income:  18000<br />density: 1.140672e-05<br />country_name: Canada","income:  19000<br />density: 1.267213e-05<br />country_name: Canada","income:  20000<br />density: 1.393996e-05<br />country_name: Canada","income:  21000<br />density: 1.519252e-05<br />country_name: Canada","income:  22000<br />density: 1.641186e-05<br />country_name: Canada","income:  23000<br />density: 1.758027e-05<br />country_name: Canada","income:  24000<br />density: 1.868089e-05<br />country_name: Canada","income:  25000<br />density: 1.969814e-05<br />country_name: Canada","income:  26000<br />density: 2.061828e-05<br />country_name: Canada","income:  27000<br />density: 2.142978e-05<br />country_name: Canada","income:  28000<br />density: 2.212361e-05<br />country_name: Canada","income:  29000<br />density: 2.269344e-05<br />country_name: Canada","income:  30000<br />density: 2.313570e-05<br />country_name: Canada","income:  31000<br />density: 2.344955e-05<br />country_name: Canada","income:  32000<br />density: 2.363670e-05<br />country_name: Canada","income:  33000<br />density: 2.370118e-05<br />country_name: Canada","income:  34000<br />density: 2.364907e-05<br />country_name: Canada","income:  35000<br />density: 2.348806e-05<br />country_name: Canada","income:  36000<br />density: 2.322716e-05<br />country_name: Canada","income:  37000<br />density: 2.287628e-05<br />country_name: Canada","income:  38000<br />density: 2.244588e-05<br />country_name: Canada","income:  39000<br />density: 2.194664e-05<br />country_name: Canada","income:  40000<br />density: 2.138918e-05<br />country_name: Canada","income:  41000<br />density: 2.078381e-05<br />country_name: Canada","income:  42000<br />density: 2.014035e-05<br />country_name: Canada","income:  43000<br />density: 1.946797e-05<br />country_name: Canada","income:  44000<br />density: 1.877508e-05<br />country_name: Canada","income:  45000<br />density: 1.806931e-05<br />country_name: Canada","income:  46000<br />density: 1.735744e-05<br />country_name: Canada","income:  47000<br />density: 1.664542e-05<br />country_name: Canada","income:  48000<br />density: 1.593838e-05<br />country_name: Canada","income:  49000<br />density: 1.524068e-05<br />country_name: Canada","income:  50000<br />density: 1.455598e-05<br />country_name: Canada","income:  51000<br />density: 1.388725e-05<br />country_name: Canada","income:  52000<br />density: 1.323687e-05<br />country_name: Canada","income:  53000<br />density: 1.260669e-05<br />country_name: Canada","income:  54000<br />density: 1.199808e-05<br />country_name: Canada","income:  55000<br />density: 1.141199e-05<br />country_name: Canada","income:  56000<br />density: 1.084904e-05<br />country_name: Canada","income:  57000<br />density: 1.030953e-05<br />country_name: Canada","income:  58000<br />density: 9.793502e-06<br />country_name: Canada","income:  59000<br />density: 9.300801e-06<br />country_name: Canada","income:  60000<br />density: 8.831089e-06<br />country_name: Canada","income:  61000<br />density: 8.383897e-06<br />country_name: Canada","income:  62000<br />density: 7.958640e-06<br />country_name: Canada","income:  63000<br />density: 7.554655e-06<br />country_name: Canada","income:  64000<br />density: 7.171215e-06<br />country_name: Canada","income:  65000<br />density: 6.807548e-06<br />country_name: Canada","income:  66000<br />density: 6.462856e-06<br />country_name: Canada","income:  67000<br />density: 6.136326e-06<br />country_name: Canada","income:  68000<br />density: 5.827141e-06<br />country_name: Canada","income:  69000<br />density: 5.534485e-06<br />country_name: Canada","income:  70000<br />density: 5.257556e-06<br />country_name: Canada","income:  71000<br />density: 4.995569e-06<br />country_name: Canada","income:  72000<br />density: 4.747759e-06<br />country_name: Canada","income:  73000<br />density: 4.513383e-06<br />country_name: Canada","income:  74000<br />density: 4.291728e-06<br />country_name: Canada","income:  75000<br />density: 4.082107e-06<br />country_name: Canada","income:  76000<br />density: 3.883861e-06<br />country_name: Canada","income:  77000<br />density: 3.696363e-06<br />country_name: Canada","income:  78000<br />density: 3.519013e-06<br />country_name: Canada","income:  79000<br />density: 3.351242e-06<br />country_name: Canada","income:  80000<br />density: 3.192509e-06<br />country_name: Canada","income:  81000<br />density: 3.042301e-06<br />country_name: Canada","income:  82000<br />density: 2.900134e-06<br />country_name: Canada","income:  83000<br />density: 2.765549e-06<br />country_name: Canada","income:  84000<br />density: 2.638111e-06<br />country_name: Canada","income:  85000<br />density: 2.517413e-06<br />country_name: Canada","income:  86000<br />density: 2.403068e-06<br />country_name: Canada","income:  87000<br />density: 2.294714e-06<br />country_name: Canada","income:  88000<br />density: 2.192007e-06<br />country_name: Canada","income:  89000<br />density: 2.094625e-06<br />country_name: Canada","income:  90000<br />density: 2.002265e-06<br />country_name: Canada","income:  91000<br />density: 1.914641e-06<br />country_name: Canada","income:  92000<br />density: 1.831485e-06<br />country_name: Canada","income:  93000<br />density: 1.752544e-06<br />country_name: Canada","income:  94000<br />density: 1.677581e-06<br />country_name: Canada","income:  95000<br />density: 1.606371e-06<br />country_name: Canada","income:  96000<br />density: 1.538706e-06<br />country_name: Canada","income:  97000<br />density: 1.474387e-06<br />country_name: Canada","income:  98000<br />density: 1.413230e-06<br />country_name: Canada","income:  99000<br />density: 1.355059e-06<br />country_name: Canada","income: 100000<br />density: 1.299710e-06<br />country_name: Canada","income: 101000<br />density: 1.247028e-06<br />country_name: Canada","income: 102000<br />density: 1.196869e-06<br />country_name: Canada","income: 103000<br />density: 1.149096e-06<br />country_name: Canada","income: 104000<br />density: 1.103581e-06<br />country_name: Canada","income: 105000<br />density: 1.060201e-06<br />country_name: Canada","income: 106000<br />density: 1.018843e-06<br />country_name: Canada","income: 107000<br />density: 9.794010e-07<br />country_name: Canada","income: 108000<br />density: 9.417726e-07<br />country_name: Canada","income: 109000<br />density: 9.058631e-07<br />country_name: Canada","income: 110000<br />density: 8.715827e-07<br />country_name: Canada","income: 111000<br />density: 8.388472e-07<br />country_name: Canada","income: 112000<br />density: 8.075767e-07<br />country_name: Canada","income: 113000<br />density: 7.776964e-07<br />country_name: Canada","income: 114000<br />density: 7.491352e-07<br />country_name: Canada","income: 115000<br />density: 7.218264e-07<br />country_name: Canada","income: 116000<br />density: 6.957071e-07<br />country_name: Canada","income: 117000<br />density: 6.707176e-07<br />country_name: Canada","income: 118000<br />density: 6.468017e-07<br />country_name: Canada","income: 119000<br />density: 6.239065e-07<br />country_name: Canada","income: 120000<br />density: 6.019817e-07<br />country_name: Canada","income: 121000<br />density: 5.809799e-07<br />country_name: Canada","income: 122000<br />density: 5.608563e-07<br />country_name: Canada","income: 123000<br />density: 5.415687e-07<br />country_name: Canada","income: 124000<br />density: 5.230768e-07<br />country_name: Canada","income: 125000<br />density: 5.053427e-07<br />country_name: Canada","income: 126000<br />density: 4.883307e-07<br />country_name: Canada","income: 127000<br />density: 4.720066e-07<br />country_name: Canada","income: 128000<br />density: 4.563383e-07<br />country_name: Canada","income: 129000<br />density: 4.412953e-07<br />country_name: Canada","income: 130000<br />density: 4.268487e-07<br />country_name: Canada","income: 131000<br />density: 4.129712e-07<br />country_name: Canada","income: 132000<br />density: 3.996366e-07<br />country_name: Canada","income: 133000<br />density: 3.868204e-07<br />country_name: Canada","income: 134000<br />density: 3.744992e-07<br />country_name: Canada","income: 135000<br />density: 3.626509e-07<br />country_name: Canada","income: 136000<br />density: 3.512543e-07<br />country_name: Canada","income: 137000<br />density: 3.402894e-07<br />country_name: Canada","income: 138000<br />density: 3.297373e-07<br />country_name: Canada","income: 139000<br />density: 3.195798e-07<br />country_name: Canada","income: 140000<br />density: 3.097999e-07<br />country_name: Canada","income: 141000<br />density: 3.003811e-07<br />country_name: Canada","income: 142000<br />density: 2.913081e-07<br />country_name: Canada","income: 143000<br />density: 2.825659e-07<br />country_name: Canada","income: 144000<br />density: 2.741406e-07<br />country_name: Canada","income: 145000<br />density: 2.660188e-07<br />country_name: Canada","income: 146000<br />density: 2.581877e-07<br />country_name: Canada","income: 147000<br />density: 2.506353e-07<br />country_name: Canada","income: 148000<br />density: 2.433499e-07<br />country_name: Canada","income: 149000<br />density: 2.363206e-07<br />country_name: Canada","income: 150000<br />density: 2.295368e-07<br />country_name: Canada"],"type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"rgba(248,118,109,1)","dash":"solid"},"hoveron":"points","name":"Canada","legendgroup":"Canada","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000,11000,12000,13000,14000,15000,16000,17000,18000,19000,20000,21000,22000,23000,24000,25000,26000,27000,28000,29000,30000,31000,32000,33000,34000,35000,36000,37000,38000,39000,40000,41000,42000,43000,44000,45000,46000,47000,48000,49000,50000,51000,52000,53000,54000,55000,56000,57000,58000,59000,60000,61000,62000,63000,64000,65000,66000,67000,68000,69000,70000,71000,72000,73000,74000,75000,76000,77000,78000,79000,80000,81000,82000,83000,84000,85000,86000,87000,88000,89000,90000,91000,92000,93000,94000,95000,96000,97000,98000,99000,100000,101000,102000,103000,104000,105000,106000,107000,108000,109000,110000,111000,112000,113000,114000,115000,116000,117000,118000,119000,120000,121000,122000,123000,124000,125000,126000,127000,128000,129000,130000,131000,132000,133000,134000,135000,136000,137000,138000,139000,140000,141000,142000,143000,144000,145000,146000,147000,148000,149000,150000],"y":[2.9645763593468622e-15,3.4599337189343061e-08,1.7706096823655461e-07,4.5991350708574256e-07,9.0451187560089085e-07,1.5263757332889302e-06,2.336253982059655e-06,3.3401196644184103e-06,4.5387399580839407e-06,5.9270888880467239e-06,7.4937738342947157e-06,9.2206164105461629e-06,1.1082512280972762e-05,1.3047674674537809e-05,1.5078334931355737e-05,1.7131928179126666e-05,1.9162735646652444e-05,2.1123893783267741e-05,2.296962388927939e-05,2.465749496477611e-05,2.6150515867604793e-05,2.7418865338657179e-05,2.8441108909835775e-05,2.9204813242900027e-05,2.9706539943955073e-05,2.99512698099268e-05,2.9951363800332522e-05,2.9725201662916822e-05,2.9295650825866815e-05,2.8688509169561139e-05,2.793104101785664e-05,2.7050692906262278e-05,2.6074040868806038e-05,2.5025989205467388e-05,2.3929215116204354e-05,2.2803835530858939e-05,2.1667261776484828e-05,2.0534203312925659e-05,1.9416782099172543e-05,1.8324722636012762e-05,1.7265587962484087e-05,1.6245037769090221e-05,1.526709056449344e-05,1.4334377025574328e-05,1.3448376039294762e-05,1.2609628442532814e-05,1.1817926124290872e-05,1.1072476074386368e-05,1.0372040270020496e-05,9.7150531151455599e-06,9.0997186053574504e-06,8.5240895841797706e-06,7.9861314671784567e-06,7.4837727022967272e-06,7.0149440560246708e-06,6.5776086000880688e-06,6.1697840461962725e-06,5.7895588528409799e-06,5.4351033179457622e-06,5.1046766797627517e-06,4.7966310782188998e-06,4.5094130803598632e-06,4.2415633457829402e-06,3.9917148994263e-06,3.7585903878487612e-06,3.5409986191283173e-06,3.3378306236942607e-06,3.1480554218760721e-06,2.9707156419639922e-06,2.8049230985893416e-06,2.6498544138998424e-06,2.5047467421716464e-06,2.3688936411778096e-06,2.2416411199976926e-06,2.122383882306747e-06,2.0105617759591117e-06,1.905656453390646e-06,1.8071882426376432e-06,1.7147132252694898e-06,1.6278205150117383e-06,1.5461297290812394e-06,1.4692886430964525e-06,1.3969710197296809e-06,1.3288746009235957e-06,1.2647192534165047e-06,1.2042452574400345e-06,1.1472117287157661e-06,1.0933951642409407e-06,1.0425881027847754e-06,9.9459789149087724e-07,9.4924555047774004e-07,9.0636472783412858e-07,8.6580073790774399e-07,8.274096762764461e-07,7.9105760526528837e-07,7.5661980432619846e-07,7.2398008002754374e-07,6.9303013080691493e-07,6.6366896202128166e-07,6.358023471847101e-07,6.0934233161494757e-07,5.8420677501775663e-07,5.6031892982234936e-07,5.3760705234423864e-07,5.1600404409406763e-07,4.9544712077420536e-07,4.7587750670987079e-07,4.5724015265004125e-07,4.3948347504611345e-07,4.2255911507481272e-07,4.0642171581694696e-07,3.910287161365906e-07,3.7634015992692937e-07,3.6231851950037815e-07,3.4892853200240442e-07,3.3613704782172442e-07,3.2391289005471642e-07,3.1222672415983889e-07,3.0105093700912564e-07,2.9035952460902366e-07,2.801279878224257e-07,2.7033323547836238e-07,2.6095349430564447e-07,2.5196822517245519e-07,2.4335804515569385e-07,2.3510465500209107e-07,2.2719077157821405e-07,2.1960006493849906e-07,2.1231709966994207e-07,2.0532728019893512e-07,1.9861679977051553e-07,1.9217259283291197e-07,1.8598229058106515e-07,1.8003417943189696e-07,1.7431716222157542e-07,1.6882072193115395e-07,1.6353488776169102e-07,1.5845020339358134e-07,1.5355769727726331e-07,1.488488548140167e-07,1.4431559229605912e-07,1.3995023248493841e-07,1.3574548171612858e-07,1.3169440842601709e-07,1.2779042300505973e-07,1.2402725888788509e-07,1.2039895479762275e-07,1.1689983806765079e-07,1.1352450896951291e-07,1.1026782598080251e-07,1.0712489193152139e-07],"text":["income:      0<br />density: 2.964576e-15<br />country_name: France","income:   1000<br />density: 3.459934e-08<br />country_name: France","income:   2000<br />density: 1.770610e-07<br />country_name: France","income:   3000<br />density: 4.599135e-07<br />country_name: France","income:   4000<br />density: 9.045119e-07<br />country_name: France","income:   5000<br />density: 1.526376e-06<br />country_name: France","income:   6000<br />density: 2.336254e-06<br />country_name: France","income:   7000<br />density: 3.340120e-06<br />country_name: France","income:   8000<br />density: 4.538740e-06<br />country_name: France","income:   9000<br />density: 5.927089e-06<br />country_name: France","income:  10000<br />density: 7.493774e-06<br />country_name: France","income:  11000<br />density: 9.220616e-06<br />country_name: France","income:  12000<br />density: 1.108251e-05<br />country_name: France","income:  13000<br />density: 1.304767e-05<br />country_name: France","income:  14000<br />density: 1.507833e-05<br />country_name: France","income:  15000<br />density: 1.713193e-05<br />country_name: France","income:  16000<br />density: 1.916274e-05<br />country_name: France","income:  17000<br />density: 2.112389e-05<br />country_name: France","income:  18000<br />density: 2.296962e-05<br />country_name: France","income:  19000<br />density: 2.465749e-05<br />country_name: France","income:  20000<br />density: 2.615052e-05<br />country_name: France","income:  21000<br />density: 2.741887e-05<br />country_name: France","income:  22000<br />density: 2.844111e-05<br />country_name: France","income:  23000<br />density: 2.920481e-05<br />country_name: France","income:  24000<br />density: 2.970654e-05<br />country_name: France","income:  25000<br />density: 2.995127e-05<br />country_name: France","income:  26000<br />density: 2.995136e-05<br />country_name: France","income:  27000<br />density: 2.972520e-05<br />country_name: France","income:  28000<br />density: 2.929565e-05<br />country_name: France","income:  29000<br />density: 2.868851e-05<br />country_name: France","income:  30000<br />density: 2.793104e-05<br />country_name: France","income:  31000<br />density: 2.705069e-05<br />country_name: France","income:  32000<br />density: 2.607404e-05<br />country_name: France","income:  33000<br />density: 2.502599e-05<br />country_name: France","income:  34000<br />density: 2.392922e-05<br />country_name: France","income:  35000<br />density: 2.280384e-05<br />country_name: France","income:  36000<br />density: 2.166726e-05<br />country_name: France","income:  37000<br />density: 2.053420e-05<br />country_name: France","income:  38000<br />density: 1.941678e-05<br />country_name: France","income:  39000<br />density: 1.832472e-05<br />country_name: France","income:  40000<br />density: 1.726559e-05<br />country_name: France","income:  41000<br />density: 1.624504e-05<br />country_name: France","income:  42000<br />density: 1.526709e-05<br />country_name: France","income:  43000<br />density: 1.433438e-05<br />country_name: France","income:  44000<br />density: 1.344838e-05<br />country_name: France","income:  45000<br />density: 1.260963e-05<br />country_name: France","income:  46000<br />density: 1.181793e-05<br />country_name: France","income:  47000<br />density: 1.107248e-05<br />country_name: France","income:  48000<br />density: 1.037204e-05<br />country_name: France","income:  49000<br />density: 9.715053e-06<br />country_name: France","income:  50000<br />density: 9.099719e-06<br />country_name: France","income:  51000<br />density: 8.524090e-06<br />country_name: France","income:  52000<br />density: 7.986131e-06<br />country_name: France","income:  53000<br />density: 7.483773e-06<br />country_name: France","income:  54000<br />density: 7.014944e-06<br />country_name: France","income:  55000<br />density: 6.577609e-06<br />country_name: France","income:  56000<br />density: 6.169784e-06<br />country_name: France","income:  57000<br />density: 5.789559e-06<br />country_name: France","income:  58000<br />density: 5.435103e-06<br />country_name: France","income:  59000<br />density: 5.104677e-06<br />country_name: France","income:  60000<br />density: 4.796631e-06<br />country_name: France","income:  61000<br />density: 4.509413e-06<br />country_name: France","income:  62000<br />density: 4.241563e-06<br />country_name: France","income:  63000<br />density: 3.991715e-06<br />country_name: France","income:  64000<br />density: 3.758590e-06<br />country_name: France","income:  65000<br />density: 3.540999e-06<br />country_name: France","income:  66000<br />density: 3.337831e-06<br />country_name: France","income:  67000<br />density: 3.148055e-06<br />country_name: France","income:  68000<br />density: 2.970716e-06<br />country_name: France","income:  69000<br />density: 2.804923e-06<br />country_name: France","income:  70000<br />density: 2.649854e-06<br />country_name: France","income:  71000<br />density: 2.504747e-06<br />country_name: France","income:  72000<br />density: 2.368894e-06<br />country_name: France","income:  73000<br />density: 2.241641e-06<br />country_name: France","income:  74000<br />density: 2.122384e-06<br />country_name: France","income:  75000<br />density: 2.010562e-06<br />country_name: France","income:  76000<br />density: 1.905656e-06<br />country_name: France","income:  77000<br />density: 1.807188e-06<br />country_name: France","income:  78000<br />density: 1.714713e-06<br />country_name: France","income:  79000<br />density: 1.627821e-06<br />country_name: France","income:  80000<br />density: 1.546130e-06<br />country_name: France","income:  81000<br />density: 1.469289e-06<br />country_name: France","income:  82000<br />density: 1.396971e-06<br />country_name: France","income:  83000<br />density: 1.328875e-06<br />country_name: France","income:  84000<br />density: 1.264719e-06<br />country_name: France","income:  85000<br />density: 1.204245e-06<br />country_name: France","income:  86000<br />density: 1.147212e-06<br />country_name: France","income:  87000<br />density: 1.093395e-06<br />country_name: France","income:  88000<br />density: 1.042588e-06<br />country_name: France","income:  89000<br />density: 9.945979e-07<br />country_name: France","income:  90000<br />density: 9.492456e-07<br />country_name: France","income:  91000<br />density: 9.063647e-07<br />country_name: France","income:  92000<br />density: 8.658007e-07<br />country_name: France","income:  93000<br />density: 8.274097e-07<br />country_name: France","income:  94000<br />density: 7.910576e-07<br />country_name: France","income:  95000<br />density: 7.566198e-07<br />country_name: France","income:  96000<br />density: 7.239801e-07<br />country_name: France","income:  97000<br />density: 6.930301e-07<br />country_name: France","income:  98000<br />density: 6.636690e-07<br />country_name: France","income:  99000<br />density: 6.358023e-07<br />country_name: France","income: 100000<br />density: 6.093423e-07<br />country_name: France","income: 101000<br />density: 5.842068e-07<br />country_name: France","income: 102000<br />density: 5.603189e-07<br />country_name: France","income: 103000<br />density: 5.376071e-07<br />country_name: France","income: 104000<br />density: 5.160040e-07<br />country_name: France","income: 105000<br />density: 4.954471e-07<br />country_name: France","income: 106000<br />density: 4.758775e-07<br />country_name: France","income: 107000<br />density: 4.572402e-07<br />country_name: France","income: 108000<br />density: 4.394835e-07<br />country_name: France","income: 109000<br />density: 4.225591e-07<br />country_name: France","income: 110000<br />density: 4.064217e-07<br />country_name: France","income: 111000<br />density: 3.910287e-07<br />country_name: France","income: 112000<br />density: 3.763402e-07<br />country_name: France","income: 113000<br />density: 3.623185e-07<br />country_name: France","income: 114000<br />density: 3.489285e-07<br />country_name: France","income: 115000<br />density: 3.361370e-07<br />country_name: France","income: 116000<br />density: 3.239129e-07<br />country_name: France","income: 117000<br />density: 3.122267e-07<br />country_name: France","income: 118000<br />density: 3.010509e-07<br />country_name: France","income: 119000<br />density: 2.903595e-07<br />country_name: France","income: 120000<br />density: 2.801280e-07<br />country_name: France","income: 121000<br />density: 2.703332e-07<br />country_name: France","income: 122000<br />density: 2.609535e-07<br />country_name: France","income: 123000<br />density: 2.519682e-07<br />country_name: France","income: 124000<br />density: 2.433580e-07<br />country_name: France","income: 125000<br />density: 2.351047e-07<br />country_name: France","income: 126000<br />density: 2.271908e-07<br />country_name: France","income: 127000<br />density: 2.196001e-07<br />country_name: France","income: 128000<br />density: 2.123171e-07<br />country_name: France","income: 129000<br />density: 2.053273e-07<br />country_name: France","income: 130000<br />density: 1.986168e-07<br />country_name: France","income: 131000<br />density: 1.921726e-07<br />country_name: France","income: 132000<br />density: 1.859823e-07<br />country_name: France","income: 133000<br />density: 1.800342e-07<br />country_name: France","income: 134000<br />density: 1.743172e-07<br />country_name: France","income: 135000<br />density: 1.688207e-07<br />country_name: France","income: 136000<br />density: 1.635349e-07<br />country_name: France","income: 137000<br />density: 1.584502e-07<br />country_name: France","income: 138000<br />density: 1.535577e-07<br />country_name: France","income: 139000<br />density: 1.488489e-07<br />country_name: France","income: 140000<br />density: 1.443156e-07<br />country_name: France","income: 141000<br />density: 1.399502e-07<br />country_name: France","income: 142000<br />density: 1.357455e-07<br />country_name: France","income: 143000<br />density: 1.316944e-07<br />country_name: France","income: 144000<br />density: 1.277904e-07<br />country_name: France","income: 145000<br />density: 1.240273e-07<br />country_name: France","income: 146000<br />density: 1.203990e-07<br />country_name: France","income: 147000<br />density: 1.168998e-07<br />country_name: France","income: 148000<br />density: 1.135245e-07<br />country_name: France","income: 149000<br />density: 1.102678e-07<br />country_name: France","income: 150000<br />density: 1.071249e-07<br />country_name: France"],"type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"rgba(196,154,0,1)","dash":"solid"},"hoveron":"points","name":"France","legendgroup":"France","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000,11000,12000,13000,14000,15000,16000,17000,18000,19000,20000,21000,22000,23000,24000,25000,26000,27000,28000,29000,30000,31000,32000,33000,34000,35000,36000,37000,38000,39000,40000,41000,42000,43000,44000,45000,46000,47000,48000,49000,50000,51000,52000,53000,54000,55000,56000,57000,58000,59000,60000,61000,62000,63000,64000,65000,66000,67000,68000,69000,70000,71000,72000,73000,74000,75000,76000,77000,78000,79000,80000,81000,82000,83000,84000,85000,86000,87000,88000,89000,90000,91000,92000,93000,94000,95000,96000,97000,98000,99000,100000,101000,102000,103000,104000,105000,106000,107000,108000,109000,110000,111000,112000,113000,114000,115000,116000,117000,118000,119000,120000,121000,122000,123000,124000,125000,126000,127000,128000,129000,130000,131000,132000,133000,134000,135000,136000,137000,138000,139000,140000,141000,142000,143000,144000,145000,146000,147000,148000,149000,150000],"y":[3.0103587704678246e-15,2.4774245093568354e-08,1.2242094585639689e-07,3.115928508174046e-07,6.0422315771336226e-07,1.0090076903536468e-06,1.532216890934451e-06,2.1778751710882132e-06,2.947689544238419e-06,3.8408664413649023e-06,4.8538899357910159e-06,5.9803124013460186e-06,7.210599679348894e-06,8.5320675291320925e-06,9.9289405941246506e-06,1.1382557541783111e-05,1.2871735743316173e-05,1.4373295978819358e-05,1.5862733027232723e-05,1.7315003059625598e-05,1.8705385309018901e-05,2.0010365425517123e-05,2.1208482844213445e-05,2.2281085375916232e-05,2.3212941199967546e-05,2.399267069015867e-05,2.4612976432277485e-05,2.5070667277248205e-05,2.5366489076179038e-05,2.5504788889831934e-05,2.5493049555029264e-05,2.5341336858547458e-05,2.5061702285148729e-05,2.4667581043794242e-05,2.417321889630833e-05,2.3593153428706337e-05,2.294176697299059e-05,2.2232920357931786e-05,2.1479669711788957e-05,2.0694063035727661e-05,1.9887009333456833e-05,1.9068210642972389e-05,1.8246146165173756e-05,1.7428097552245155e-05,1.6620205023171082e-05,1.5827545052974782e-05,1.5054221715832344e-05,1.4303465179102427e-05,1.3577732223337863e-05,1.2878804923613928e-05,1.2207884726285509e-05,1.1565680075366912e-05,1.0952486485423371e-05,1.0368258536275819e-05,9.812673698565725e-06,9.2851882106170218e-06,8.7850854384404562e-06,8.3115172830050018e-06,7.8635392704615183e-06,7.4401399874961093e-06,7.0402655182642597e-06,6.6628395116711964e-06,6.3067794660856089e-06,5.9710097689197193e-06,5.6544719753039344e-06,5.3561327564583965e-06,5.0749898964566722e-06,4.8100766672540236e-06,4.5604648669138435e-06,4.3252667652973386e-06,4.1036361651722897e-06,3.8947687546478103e-06,3.6979018988166428e-06,3.5123139941833402e-06,3.3373234885279381e-06,3.1722876509499822e-06,3.0166011616053163e-06,2.8696945777593862e-06,2.7310327219293791e-06,2.6001130287943366e-06,2.4764638799688925e-06,2.3596429494410023e-06,2.2492355772723971e-06,2.1448531848852176e-06,2.0461317417619138e-06,1.9527302905426478e-06,1.8643295352091743e-06,1.7806304952030617e-06,1.7013532268634623e-06,1.6262356124183536e-06,1.5550322158698897e-06,1.487513204430971e-06,1.4234633336595188e-06,1.3626809940652675e-06,1.3049773167057556e-06,1.2501753351199766e-06,1.1981092008524062e-06,1.1486234497812673e-06,1.1015723164700812e-06,1.0568190938014322e-06,1.0142355352170901e-06,9.7370129697334391e-07,9.3510341791821374e-07,8.9833583440482279e-07,8.6329892806782763e-07,8.2989910430596452e-07,7.9804839943021136e-07,7.6766411455283789e-07,7.3866847440598607e-07,7.1098830938874873e-07,6.8455475924809e-07,6.5930299690096295e-07,6.351719710023315e-07,6.1210416595638088e-07,5.900453781556777e-07,5.689445073157553e-07,5.4875336185043691e-07,5.2942647730625331e-07,5.1092094694281153e-07,4.9319626361012005e-07,4.7621417213374067e-07,4.5993853147458234e-07,4.4433518598218052e-07,4.2937184510893771e-07,4.1501797099788392e-07,4.0124467339852876e-07,3.8802461140440312e-07,3.7533190154209354e-07,3.6314203177528382e-07,3.5143178101844517e-07,3.4017914378388e-07,3.2936325961261155e-07,3.1896434696457209e-07,3.0896364126661284e-07,2.9934333683830089e-07,2.9008653243527916e-07,2.8117718016840949e-07,2.7260003757394404e-07,2.6434062262578326e-07,2.5638517149557467e-07,2.487205988799222e-07,2.4133446072666193e-07,2.3421491920375503e-07,2.2735070976526784e-07,2.2073111017888689e-07,2.1434591138877449e-07,2.0818539009623776e-07,2.0224028294864596e-07,1.9650176223457321e-07,1.9096141298997272e-07,1.8561121142666921e-07],"text":["income:      0<br />density: 3.010359e-15<br />country_name: Germany","income:   1000<br />density: 2.477425e-08<br />country_name: Germany","income:   2000<br />density: 1.224209e-07<br />country_name: Germany","income:   3000<br />density: 3.115929e-07<br />country_name: Germany","income:   4000<br />density: 6.042232e-07<br />country_name: Germany","income:   5000<br />density: 1.009008e-06<br />country_name: Germany","income:   6000<br />density: 1.532217e-06<br />country_name: Germany","income:   7000<br />density: 2.177875e-06<br />country_name: Germany","income:   8000<br />density: 2.947690e-06<br />country_name: Germany","income:   9000<br />density: 3.840866e-06<br />country_name: Germany","income:  10000<br />density: 4.853890e-06<br />country_name: Germany","income:  11000<br />density: 5.980312e-06<br />country_name: Germany","income:  12000<br />density: 7.210600e-06<br />country_name: Germany","income:  13000<br />density: 8.532068e-06<br />country_name: Germany","income:  14000<br />density: 9.928941e-06<br />country_name: Germany","income:  15000<br />density: 1.138256e-05<br />country_name: Germany","income:  16000<br />density: 1.287174e-05<br />country_name: Germany","income:  17000<br />density: 1.437330e-05<br />country_name: Germany","income:  18000<br />density: 1.586273e-05<br />country_name: Germany","income:  19000<br />density: 1.731500e-05<br />country_name: Germany","income:  20000<br />density: 1.870539e-05<br />country_name: Germany","income:  21000<br />density: 2.001037e-05<br />country_name: Germany","income:  22000<br />density: 2.120848e-05<br />country_name: Germany","income:  23000<br />density: 2.228109e-05<br />country_name: Germany","income:  24000<br />density: 2.321294e-05<br />country_name: Germany","income:  25000<br />density: 2.399267e-05<br />country_name: Germany","income:  26000<br />density: 2.461298e-05<br />country_name: Germany","income:  27000<br />density: 2.507067e-05<br />country_name: Germany","income:  28000<br />density: 2.536649e-05<br />country_name: Germany","income:  29000<br />density: 2.550479e-05<br />country_name: Germany","income:  30000<br />density: 2.549305e-05<br />country_name: Germany","income:  31000<br />density: 2.534134e-05<br />country_name: Germany","income:  32000<br />density: 2.506170e-05<br />country_name: Germany","income:  33000<br />density: 2.466758e-05<br />country_name: Germany","income:  34000<br />density: 2.417322e-05<br />country_name: Germany","income:  35000<br />density: 2.359315e-05<br />country_name: Germany","income:  36000<br />density: 2.294177e-05<br />country_name: Germany","income:  37000<br />density: 2.223292e-05<br />country_name: Germany","income:  38000<br />density: 2.147967e-05<br />country_name: Germany","income:  39000<br />density: 2.069406e-05<br />country_name: Germany","income:  40000<br />density: 1.988701e-05<br />country_name: Germany","income:  41000<br />density: 1.906821e-05<br />country_name: Germany","income:  42000<br />density: 1.824615e-05<br />country_name: Germany","income:  43000<br />density: 1.742810e-05<br />country_name: Germany","income:  44000<br />density: 1.662021e-05<br />country_name: Germany","income:  45000<br />density: 1.582755e-05<br />country_name: Germany","income:  46000<br />density: 1.505422e-05<br />country_name: Germany","income:  47000<br />density: 1.430347e-05<br />country_name: Germany","income:  48000<br />density: 1.357773e-05<br />country_name: Germany","income:  49000<br />density: 1.287880e-05<br />country_name: Germany","income:  50000<br />density: 1.220788e-05<br />country_name: Germany","income:  51000<br />density: 1.156568e-05<br />country_name: Germany","income:  52000<br />density: 1.095249e-05<br />country_name: Germany","income:  53000<br />density: 1.036826e-05<br />country_name: Germany","income:  54000<br />density: 9.812674e-06<br />country_name: Germany","income:  55000<br />density: 9.285188e-06<br />country_name: Germany","income:  56000<br />density: 8.785085e-06<br />country_name: Germany","income:  57000<br />density: 8.311517e-06<br />country_name: Germany","income:  58000<br />density: 7.863539e-06<br />country_name: Germany","income:  59000<br />density: 7.440140e-06<br />country_name: Germany","income:  60000<br />density: 7.040266e-06<br />country_name: Germany","income:  61000<br />density: 6.662840e-06<br />country_name: Germany","income:  62000<br />density: 6.306779e-06<br />country_name: Germany","income:  63000<br />density: 5.971010e-06<br />country_name: Germany","income:  64000<br />density: 5.654472e-06<br />country_name: Germany","income:  65000<br />density: 5.356133e-06<br />country_name: Germany","income:  66000<br />density: 5.074990e-06<br />country_name: Germany","income:  67000<br />density: 4.810077e-06<br />country_name: Germany","income:  68000<br />density: 4.560465e-06<br />country_name: Germany","income:  69000<br />density: 4.325267e-06<br />country_name: Germany","income:  70000<br />density: 4.103636e-06<br />country_name: Germany","income:  71000<br />density: 3.894769e-06<br />country_name: Germany","income:  72000<br />density: 3.697902e-06<br />country_name: Germany","income:  73000<br />density: 3.512314e-06<br />country_name: Germany","income:  74000<br />density: 3.337323e-06<br />country_name: Germany","income:  75000<br />density: 3.172288e-06<br />country_name: Germany","income:  76000<br />density: 3.016601e-06<br />country_name: Germany","income:  77000<br />density: 2.869695e-06<br />country_name: Germany","income:  78000<br />density: 2.731033e-06<br />country_name: Germany","income:  79000<br />density: 2.600113e-06<br />country_name: Germany","income:  80000<br />density: 2.476464e-06<br />country_name: Germany","income:  81000<br />density: 2.359643e-06<br />country_name: Germany","income:  82000<br />density: 2.249236e-06<br />country_name: Germany","income:  83000<br />density: 2.144853e-06<br />country_name: Germany","income:  84000<br />density: 2.046132e-06<br />country_name: Germany","income:  85000<br />density: 1.952730e-06<br />country_name: Germany","income:  86000<br />density: 1.864330e-06<br />country_name: Germany","income:  87000<br />density: 1.780630e-06<br />country_name: Germany","income:  88000<br />density: 1.701353e-06<br />country_name: Germany","income:  89000<br />density: 1.626236e-06<br />country_name: Germany","income:  90000<br />density: 1.555032e-06<br />country_name: Germany","income:  91000<br />density: 1.487513e-06<br />country_name: Germany","income:  92000<br />density: 1.423463e-06<br />country_name: Germany","income:  93000<br />density: 1.362681e-06<br />country_name: Germany","income:  94000<br />density: 1.304977e-06<br />country_name: Germany","income:  95000<br />density: 1.250175e-06<br />country_name: Germany","income:  96000<br />density: 1.198109e-06<br />country_name: Germany","income:  97000<br />density: 1.148623e-06<br />country_name: Germany","income:  98000<br />density: 1.101572e-06<br />country_name: Germany","income:  99000<br />density: 1.056819e-06<br />country_name: Germany","income: 100000<br />density: 1.014236e-06<br />country_name: Germany","income: 101000<br />density: 9.737013e-07<br />country_name: Germany","income: 102000<br />density: 9.351034e-07<br />country_name: Germany","income: 103000<br />density: 8.983358e-07<br />country_name: Germany","income: 104000<br />density: 8.632989e-07<br />country_name: Germany","income: 105000<br />density: 8.298991e-07<br />country_name: Germany","income: 106000<br />density: 7.980484e-07<br />country_name: Germany","income: 107000<br />density: 7.676641e-07<br />country_name: Germany","income: 108000<br />density: 7.386685e-07<br />country_name: Germany","income: 109000<br />density: 7.109883e-07<br />country_name: Germany","income: 110000<br />density: 6.845548e-07<br />country_name: Germany","income: 111000<br />density: 6.593030e-07<br />country_name: Germany","income: 112000<br />density: 6.351720e-07<br />country_name: Germany","income: 113000<br />density: 6.121042e-07<br />country_name: Germany","income: 114000<br />density: 5.900454e-07<br />country_name: Germany","income: 115000<br />density: 5.689445e-07<br />country_name: Germany","income: 116000<br />density: 5.487534e-07<br />country_name: Germany","income: 117000<br />density: 5.294265e-07<br />country_name: Germany","income: 118000<br />density: 5.109209e-07<br />country_name: Germany","income: 119000<br />density: 4.931963e-07<br />country_name: Germany","income: 120000<br />density: 4.762142e-07<br />country_name: Germany","income: 121000<br />density: 4.599385e-07<br />country_name: Germany","income: 122000<br />density: 4.443352e-07<br />country_name: Germany","income: 123000<br />density: 4.293718e-07<br />country_name: Germany","income: 124000<br />density: 4.150180e-07<br />country_name: Germany","income: 125000<br />density: 4.012447e-07<br />country_name: Germany","income: 126000<br />density: 3.880246e-07<br />country_name: Germany","income: 127000<br />density: 3.753319e-07<br />country_name: Germany","income: 128000<br />density: 3.631420e-07<br />country_name: Germany","income: 129000<br />density: 3.514318e-07<br />country_name: Germany","income: 130000<br />density: 3.401791e-07<br />country_name: Germany","income: 131000<br />density: 3.293633e-07<br />country_name: Germany","income: 132000<br />density: 3.189643e-07<br />country_name: Germany","income: 133000<br />density: 3.089636e-07<br />country_name: Germany","income: 134000<br />density: 2.993433e-07<br />country_name: Germany","income: 135000<br />density: 2.900865e-07<br />country_name: Germany","income: 136000<br />density: 2.811772e-07<br />country_name: Germany","income: 137000<br />density: 2.726000e-07<br />country_name: Germany","income: 138000<br />density: 2.643406e-07<br />country_name: Germany","income: 139000<br />density: 2.563852e-07<br />country_name: Germany","income: 140000<br />density: 2.487206e-07<br />country_name: Germany","income: 141000<br />density: 2.413345e-07<br />country_name: Germany","income: 142000<br />density: 2.342149e-07<br />country_name: Germany","income: 143000<br />density: 2.273507e-07<br />country_name: Germany","income: 144000<br />density: 2.207311e-07<br />country_name: Germany","income: 145000<br />density: 2.143459e-07<br />country_name: Germany","income: 146000<br />density: 2.081854e-07<br />country_name: Germany","income: 147000<br />density: 2.022403e-07<br />country_name: Germany","income: 148000<br />density: 1.965018e-07<br />country_name: Germany","income: 149000<br />density: 1.909614e-07<br />country_name: Germany","income: 150000<br />density: 1.856112e-07<br />country_name: Germany"],"type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"rgba(83,180,0,1)","dash":"solid"},"hoveron":"points","name":"Germany","legendgroup":"Germany","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000,11000,12000,13000,14000,15000,16000,17000,18000,19000,20000,21000,22000,23000,24000,25000,26000,27000,28000,29000,30000,31000,32000,33000,34000,35000,36000,37000,38000,39000,40000,41000,42000,43000,44000,45000,46000,47000,48000,49000,50000,51000,52000,53000,54000,55000,56000,57000,58000,59000,60000,61000,62000,63000,64000,65000,66000,67000,68000,69000,70000,71000,72000,73000,74000,75000,76000,77000,78000,79000,80000,81000,82000,83000,84000,85000,86000,87000,88000,89000,90000,91000,92000,93000,94000,95000,96000,97000,98000,99000,100000,101000,102000,103000,104000,105000,106000,107000,108000,109000,110000,111000,112000,113000,114000,115000,116000,117000,118000,119000,120000,121000,122000,123000,124000,125000,126000,127000,128000,129000,130000,131000,132000,133000,134000,135000,136000,137000,138000,139000,140000,141000,142000,143000,144000,145000,146000,147000,148000,149000,150000],"y":[9.891271006004752e-14,1.2438304065562205e-07,5.0880793264422169e-07,1.1584412457006697e-06,2.0725749372979653e-06,3.2452295615043042e-06,4.6643607804457505e-06,6.3110007923625019e-06,8.1586479008630129e-06,1.0173085448126626e-05,1.2312755551526469e-05,1.4529757004514227e-05,1.6771467298770567e-05,1.8982709238351783e-05,2.1108303855996454e-05,2.3095787526629123e-05,2.4898035861255369e-05,2.6475539037655948e-05,2.7798113974825841e-05,2.884591109608977e-05,2.9609663567841646e-05,3.0090217989980608e-05,3.0297461916705265e-05,3.0248814444368015e-05,2.9967467135675492e-05,2.9480555845026926e-05,2.8817416264058798e-05,2.8008036056340354e-05,2.7081772950628989e-05,2.6066367968900126e-05,2.4987250406257354e-05,2.3867108091906404e-05,2.2725682712538561e-05,2.1579744183613724e-05,2.0443198240222558e-05,1.9327285551579774e-05,1.8240836961281942e-05,1.7190556543029089e-05,1.6181311090511512e-05,1.5216410860475471e-05,1.4297871584285853e-05,1.3426651887009562e-05,1.2602863365081671e-05,1.1825952804283246e-05,1.1094857526421224e-05,1.0408135793203326e-05,9.7640747116396781e-06,9.1607782971266814e-06,8.5962383547560057e-06,8.0683907106911138e-06,7.5751591187618401e-06,7.1144889220452541e-06,6.6843722920164881e-06,6.2828666163664481e-06,5.908107371398147e-06,5.5583166017581142e-06,5.2318079415205046e-06,4.9269889465160759e-06,4.642361367135798e-06,4.3765198717147113e-06,4.1281496307359778e-06,3.8960230891031092e-06,3.678996185291173e-06,3.4760042201480295e-06,3.2860575325313335e-06,3.1082371021137737e-06,2.9416901700842165e-06,2.7856259448355253e-06,2.6393114409855379e-06,2.5020674853178468e-06,2.3732649116993016e-06,2.2523209581080043e-06,2.1386958720737865e-06,2.0318897256727922e-06,1.9314394373852446e-06,1.836915995342217e-06,1.7479218745286061e-06,1.6640886391930471e-06,1.5850747208964885e-06,1.5105633621931333e-06,1.4402607157871972e-06,1.3738940890736992e-06,1.3112103241932925e-06,1.2519743040640194e-06,1.1959675752624128e-06,1.1429870790841069e-06,1.0928439826000485e-06,1.0453626020221027e-06,1.0003794111893311e-06,9.5774212847528574e-07,9.1730887589041733e-07,8.7894740460839992e-07,8.4253438157758824e-07,8.079547322874574e-07,7.7510103514392947e-07,7.4387296326673274e-07,7.1417676985683427e-07,6.8592481359290811e-07,6.5903512080421298e-07,6.3343098143353887e-07,6.0904057604984665e-07,5.8579663139672315e-07,5.6363610217111853e-07,5.4249987691837808e-07,5.2233250610538152e-07,5.030819505948965e-07,4.8469934889207155e-07,4.6713880166956507e-07,4.5035717220179255e-07,4.3431390145242893e-07,4.1897083666330924e-07,4.0429207238800089e-07,3.9024380300042919e-07,3.7679418578865662e-07,3.6391321381675447e-07,3.5157259780452414e-07,3.3974565633582143e-07,3.2840721376216e-07,3.1753350521939257e-07,3.0710208822218537e-07,2.9709176034377123e-07,2.8748248252790596e-07,2.7825530761581686e-07,2.693923137039976e-07,2.6087654197893561e-07,2.5269193870256646e-07,2.4482330104770993e-07,2.3725622650607094e-07,2.2997706561283632e-07,2.2297287775152921e-07,2.1623138982090551e-07,2.0974095756224848e-07,2.034905293606807e-07,1.9746961234817972e-07,1.916682406488609e-07,1.8607694561899473e-07,1.806867279451646e-07,1.7548903147405035e-07,1.7047571865657543e-07,1.6563904749778848e-07,1.6097164991164067e-07,1.5646651138718614e-07,1.5211695187940998e-07,1.4791660784408428e-07,1.438594153418169e-07,1.3993959414170435e-07,1.3615163275993835e-07,1.3249027437319857e-07,1.2895050355088871e-07,1.2552753375410759e-07,1.2221679555286877e-07],"text":["income:      0<br />density: 9.891271e-14<br />country_name: Italy","income:   1000<br />density: 1.243830e-07<br />country_name: Italy","income:   2000<br />density: 5.088079e-07<br />country_name: Italy","income:   3000<br />density: 1.158441e-06<br />country_name: Italy","income:   4000<br />density: 2.072575e-06<br />country_name: Italy","income:   5000<br />density: 3.245230e-06<br />country_name: Italy","income:   6000<br />density: 4.664361e-06<br />country_name: Italy","income:   7000<br />density: 6.311001e-06<br />country_name: Italy","income:   8000<br />density: 8.158648e-06<br />country_name: Italy","income:   9000<br />density: 1.017309e-05<br />country_name: Italy","income:  10000<br />density: 1.231276e-05<br />country_name: Italy","income:  11000<br />density: 1.452976e-05<br />country_name: Italy","income:  12000<br />density: 1.677147e-05<br />country_name: Italy","income:  13000<br />density: 1.898271e-05<br />country_name: Italy","income:  14000<br />density: 2.110830e-05<br />country_name: Italy","income:  15000<br />density: 2.309579e-05<br />country_name: Italy","income:  16000<br />density: 2.489804e-05<br />country_name: Italy","income:  17000<br />density: 2.647554e-05<br />country_name: Italy","income:  18000<br />density: 2.779811e-05<br />country_name: Italy","income:  19000<br />density: 2.884591e-05<br />country_name: Italy","income:  20000<br />density: 2.960966e-05<br />country_name: Italy","income:  21000<br />density: 3.009022e-05<br />country_name: Italy","income:  22000<br />density: 3.029746e-05<br />country_name: Italy","income:  23000<br />density: 3.024881e-05<br />country_name: Italy","income:  24000<br />density: 2.996747e-05<br />country_name: Italy","income:  25000<br />density: 2.948056e-05<br />country_name: Italy","income:  26000<br />density: 2.881742e-05<br />country_name: Italy","income:  27000<br />density: 2.800804e-05<br />country_name: Italy","income:  28000<br />density: 2.708177e-05<br />country_name: Italy","income:  29000<br />density: 2.606637e-05<br />country_name: Italy","income:  30000<br />density: 2.498725e-05<br />country_name: Italy","income:  31000<br />density: 2.386711e-05<br />country_name: Italy","income:  32000<br />density: 2.272568e-05<br />country_name: Italy","income:  33000<br />density: 2.157974e-05<br />country_name: Italy","income:  34000<br />density: 2.044320e-05<br />country_name: Italy","income:  35000<br />density: 1.932729e-05<br />country_name: Italy","income:  36000<br />density: 1.824084e-05<br />country_name: Italy","income:  37000<br />density: 1.719056e-05<br />country_name: Italy","income:  38000<br />density: 1.618131e-05<br />country_name: Italy","income:  39000<br />density: 1.521641e-05<br />country_name: Italy","income:  40000<br />density: 1.429787e-05<br />country_name: Italy","income:  41000<br />density: 1.342665e-05<br />country_name: Italy","income:  42000<br />density: 1.260286e-05<br />country_name: Italy","income:  43000<br />density: 1.182595e-05<br />country_name: Italy","income:  44000<br />density: 1.109486e-05<br />country_name: Italy","income:  45000<br />density: 1.040814e-05<br />country_name: Italy","income:  46000<br />density: 9.764075e-06<br />country_name: Italy","income:  47000<br />density: 9.160778e-06<br />country_name: Italy","income:  48000<br />density: 8.596238e-06<br />country_name: Italy","income:  49000<br />density: 8.068391e-06<br />country_name: Italy","income:  50000<br />density: 7.575159e-06<br />country_name: Italy","income:  51000<br />density: 7.114489e-06<br />country_name: Italy","income:  52000<br />density: 6.684372e-06<br />country_name: Italy","income:  53000<br />density: 6.282867e-06<br />country_name: Italy","income:  54000<br />density: 5.908107e-06<br />country_name: Italy","income:  55000<br />density: 5.558317e-06<br />country_name: Italy","income:  56000<br />density: 5.231808e-06<br />country_name: Italy","income:  57000<br />density: 4.926989e-06<br />country_name: Italy","income:  58000<br />density: 4.642361e-06<br />country_name: Italy","income:  59000<br />density: 4.376520e-06<br />country_name: Italy","income:  60000<br />density: 4.128150e-06<br />country_name: Italy","income:  61000<br />density: 3.896023e-06<br />country_name: Italy","income:  62000<br />density: 3.678996e-06<br />country_name: Italy","income:  63000<br />density: 3.476004e-06<br />country_name: Italy","income:  64000<br />density: 3.286058e-06<br />country_name: Italy","income:  65000<br />density: 3.108237e-06<br />country_name: Italy","income:  66000<br />density: 2.941690e-06<br />country_name: Italy","income:  67000<br />density: 2.785626e-06<br />country_name: Italy","income:  68000<br />density: 2.639311e-06<br />country_name: Italy","income:  69000<br />density: 2.502067e-06<br />country_name: Italy","income:  70000<br />density: 2.373265e-06<br />country_name: Italy","income:  71000<br />density: 2.252321e-06<br />country_name: Italy","income:  72000<br />density: 2.138696e-06<br />country_name: Italy","income:  73000<br />density: 2.031890e-06<br />country_name: Italy","income:  74000<br />density: 1.931439e-06<br />country_name: Italy","income:  75000<br />density: 1.836916e-06<br />country_name: Italy","income:  76000<br />density: 1.747922e-06<br />country_name: Italy","income:  77000<br />density: 1.664089e-06<br />country_name: Italy","income:  78000<br />density: 1.585075e-06<br />country_name: Italy","income:  79000<br />density: 1.510563e-06<br />country_name: Italy","income:  80000<br />density: 1.440261e-06<br />country_name: Italy","income:  81000<br />density: 1.373894e-06<br />country_name: Italy","income:  82000<br />density: 1.311210e-06<br />country_name: Italy","income:  83000<br />density: 1.251974e-06<br />country_name: Italy","income:  84000<br />density: 1.195968e-06<br />country_name: Italy","income:  85000<br />density: 1.142987e-06<br />country_name: Italy","income:  86000<br />density: 1.092844e-06<br />country_name: Italy","income:  87000<br />density: 1.045363e-06<br />country_name: Italy","income:  88000<br />density: 1.000379e-06<br />country_name: Italy","income:  89000<br />density: 9.577421e-07<br />country_name: Italy","income:  90000<br />density: 9.173089e-07<br />country_name: Italy","income:  91000<br />density: 8.789474e-07<br />country_name: Italy","income:  92000<br />density: 8.425344e-07<br />country_name: Italy","income:  93000<br />density: 8.079547e-07<br />country_name: Italy","income:  94000<br />density: 7.751010e-07<br />country_name: Italy","income:  95000<br />density: 7.438730e-07<br />country_name: Italy","income:  96000<br />density: 7.141768e-07<br />country_name: Italy","income:  97000<br />density: 6.859248e-07<br />country_name: Italy","income:  98000<br />density: 6.590351e-07<br />country_name: Italy","income:  99000<br />density: 6.334310e-07<br />country_name: Italy","income: 100000<br />density: 6.090406e-07<br />country_name: Italy","income: 101000<br />density: 5.857966e-07<br />country_name: Italy","income: 102000<br />density: 5.636361e-07<br />country_name: Italy","income: 103000<br />density: 5.424999e-07<br />country_name: Italy","income: 104000<br />density: 5.223325e-07<br />country_name: Italy","income: 105000<br />density: 5.030820e-07<br />country_name: Italy","income: 106000<br />density: 4.846993e-07<br />country_name: Italy","income: 107000<br />density: 4.671388e-07<br />country_name: Italy","income: 108000<br />density: 4.503572e-07<br />country_name: Italy","income: 109000<br />density: 4.343139e-07<br />country_name: Italy","income: 110000<br />density: 4.189708e-07<br />country_name: Italy","income: 111000<br />density: 4.042921e-07<br />country_name: Italy","income: 112000<br />density: 3.902438e-07<br />country_name: Italy","income: 113000<br />density: 3.767942e-07<br />country_name: Italy","income: 114000<br />density: 3.639132e-07<br />country_name: Italy","income: 115000<br />density: 3.515726e-07<br />country_name: Italy","income: 116000<br />density: 3.397457e-07<br />country_name: Italy","income: 117000<br />density: 3.284072e-07<br />country_name: Italy","income: 118000<br />density: 3.175335e-07<br />country_name: Italy","income: 119000<br />density: 3.071021e-07<br />country_name: Italy","income: 120000<br />density: 2.970918e-07<br />country_name: Italy","income: 121000<br />density: 2.874825e-07<br />country_name: Italy","income: 122000<br />density: 2.782553e-07<br />country_name: Italy","income: 123000<br />density: 2.693923e-07<br />country_name: Italy","income: 124000<br />density: 2.608765e-07<br />country_name: Italy","income: 125000<br />density: 2.526919e-07<br />country_name: Italy","income: 126000<br />density: 2.448233e-07<br />country_name: Italy","income: 127000<br />density: 2.372562e-07<br />country_name: Italy","income: 128000<br />density: 2.299771e-07<br />country_name: Italy","income: 129000<br />density: 2.229729e-07<br />country_name: Italy","income: 130000<br />density: 2.162314e-07<br />country_name: Italy","income: 131000<br />density: 2.097410e-07<br />country_name: Italy","income: 132000<br />density: 2.034905e-07<br />country_name: Italy","income: 133000<br />density: 1.974696e-07<br />country_name: Italy","income: 134000<br />density: 1.916682e-07<br />country_name: Italy","income: 135000<br />density: 1.860769e-07<br />country_name: Italy","income: 136000<br />density: 1.806867e-07<br />country_name: Italy","income: 137000<br />density: 1.754890e-07<br />country_name: Italy","income: 138000<br />density: 1.704757e-07<br />country_name: Italy","income: 139000<br />density: 1.656390e-07<br />country_name: Italy","income: 140000<br />density: 1.609716e-07<br />country_name: Italy","income: 141000<br />density: 1.564665e-07<br />country_name: Italy","income: 142000<br />density: 1.521170e-07<br />country_name: Italy","income: 143000<br />density: 1.479166e-07<br />country_name: Italy","income: 144000<br />density: 1.438594e-07<br />country_name: Italy","income: 145000<br />density: 1.399396e-07<br />country_name: Italy","income: 146000<br />density: 1.361516e-07<br />country_name: Italy","income: 147000<br />density: 1.324903e-07<br />country_name: Italy","income: 148000<br />density: 1.289505e-07<br />country_name: Italy","income: 149000<br />density: 1.255275e-07<br />country_name: Italy","income: 150000<br />density: 1.222168e-07<br />country_name: Italy"],"type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"rgba(0,192,148,1)","dash":"solid"},"hoveron":"points","name":"Italy","legendgroup":"Italy","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000,11000,12000,13000,14000,15000,16000,17000,18000,19000,20000,21000,22000,23000,24000,25000,26000,27000,28000,29000,30000,31000,32000,33000,34000,35000,36000,37000,38000,39000,40000,41000,42000,43000,44000,45000,46000,47000,48000,49000,50000,51000,52000,53000,54000,55000,56000,57000,58000,59000,60000,61000,62000,63000,64000,65000,66000,67000,68000,69000,70000,71000,72000,73000,74000,75000,76000,77000,78000,79000,80000,81000,82000,83000,84000,85000,86000,87000,88000,89000,90000,91000,92000,93000,94000,95000,96000,97000,98000,99000,100000,101000,102000,103000,104000,105000,106000,107000,108000,109000,110000,111000,112000,113000,114000,115000,116000,117000,118000,119000,120000,121000,122000,123000,124000,125000,126000,127000,128000,129000,130000,131000,132000,133000,134000,135000,136000,137000,138000,139000,140000,141000,142000,143000,144000,145000,146000,147000,148000,149000,150000],"y":[3.2973150461300675e-13,3.1630393543171964e-07,1.2581223645510486e-06,2.8127004820439081e-06,4.9534201951555377e-06,7.6324137981813068e-06,1.0775665939345666e-05,1.4281041438955013e-05,1.8020018757504468e-05,2.1843521451289087e-05,2.5591540150365888e-05,2.9105418456846875e-05,3.2240989133477534e-05,3.4880437976034541e-05,3.6940978808180966e-05,3.8379102234625272e-05,3.9190103016813642e-05,3.9403507549702274e-05,3.9075666378034771e-05,3.8281026937543289e-05,3.7103483231991809e-05,3.5628836439278888e-05,3.3938948244901456e-05,3.2107754320297613e-05,3.0199001271250239e-05,2.8265395477189396e-05,2.6348789035250639e-05,2.4481042772315602e-05,2.2685264288279623e-05,2.0977192787918018e-05,1.936657422091483e-05,1.7858430714477543e-05,1.6454174321076805e-05,1.5152547256327394e-05,1.3950391364121446e-05,1.2843261319944174e-05,1.1825901692757953e-05,1.0892609581138966e-05,1.003750373318681e-05,9.2547189798438058e-06,8.5385421972442929e-06,7.8835033181304333e-06,7.2844323884897496e-06,6.7364914357620181e-06,6.2351880205294407e-06,5.7763757790586365e-06,5.3562459996118798e-06,4.9713132710127984e-06,4.618397455414279e-06,4.2946036288849304e-06,3.9973011681503744e-06,3.7241028096955855e-06,3.4728442437127127e-06,3.2415646100248741e-06,3.0284881201444504e-06,2.832006926448958e-06,2.6506652862693441e-06,2.4831450179083316e-06,2.3282522114308126e-06,2.1849051350715931e-06,2.0521232649449981e-06,1.929017358900674e-06,1.8147804929755746e-06,1.7086799795520103e-06,1.6100500890235947e-06,1.5182855007496869e-06,1.4328354138077544e-06,1.3531982531506928e-06,1.2789169119762563e-06,1.2095744762377515e-06,1.1447903821495253e-06,1.0842169621943162e-06,1.0275363394800853e-06,9.7445763430309126e-07,9.2471445044828363e-07,8.7806261210548791e-07,8.3427812531450974e-07,7.9315534059271975e-07,7.5450529586608047e-07,7.1815422104020095e-07,6.8394218753388303e-07,6.5172188787460591e-07,6.2135753204354278e-07,5.9272384867563335e-07,5.6570518048547501e-07,5.4019466441779027e-07,5.1609348802713598e-07,4.9331021448772146e-07,4.7176016943304783e-07,4.5136488353702441e-07,4.3205158538272367e-07,4.1375273973070858e-07,3.9640562680330366e-07,3.7995195865127889e-07,3.6433752907098854e-07,3.4951189389863318e-07,3.3542807882857731e-07,3.2204231218892489e-07,3.0931378036348376e-07,2.9720440377828513e-07,2.8567863157581349e-07,2.7470325328371309e-07,2.6424722594936617e-07,2.5428151535935194e-07,2.447789500952434e-07,2.3571408729623274e-07,2.2706308910594599e-07,2.188036088769855e-07,2.1091468629323686e-07,2.0337665064789808e-07,1.9617103158537957e-07,1.8928047667852002e-07,1.8268867526968591e-07,1.7638028805589314e-07,1.7034088194466289e-07,1.6455686974947806e-07,1.5901545433181296e-07,1.5370457683120689e-07,1.4861286865613735e-07,1.4372960693676963e-07,1.3904467316638175e-07,1.3454851478161737e-07,1.3023210945289012e-07,1.2608693187556387e-07,1.2210492287004511e-07,1.1827846061488537e-07,1.1460033385151384e-07,1.110637169124577e-07,1.0766214643699464e-07,1.0438949964917684e-07,1.0123997408323157e-07,9.8208068650537341e-08,9.5288565950755319e-08,9.2476515737383026e-08,8.9767219455039188e-08,8.7156215772193936e-08,8.4639267038989545e-08,8.2212346605173905e-08,7.9871626938146305e-08,7.7613468485658078e-08,7.5434409231886811e-08,7.3331154899452309e-08,7.1300569753456608e-08,6.9339667966896904e-08,6.7445605509768678e-08,6.561567252693578e-08,6.3847286172386181e-08,6.213798386980944e-08,6.0485416971604576e-08,5.8887344790402224e-08,5.7341628979025488e-08],"text":["income:      0<br />density: 3.297315e-13<br />country_name: Japan","income:   1000<br />density: 3.163039e-07<br />country_name: Japan","income:   2000<br />density: 1.258122e-06<br />country_name: Japan","income:   3000<br />density: 2.812700e-06<br />country_name: Japan","income:   4000<br />density: 4.953420e-06<br />country_name: Japan","income:   5000<br />density: 7.632414e-06<br />country_name: Japan","income:   6000<br />density: 1.077567e-05<br />country_name: Japan","income:   7000<br />density: 1.428104e-05<br />country_name: Japan","income:   8000<br />density: 1.802002e-05<br />country_name: Japan","income:   9000<br />density: 2.184352e-05<br />country_name: Japan","income:  10000<br />density: 2.559154e-05<br />country_name: Japan","income:  11000<br />density: 2.910542e-05<br />country_name: Japan","income:  12000<br />density: 3.224099e-05<br />country_name: Japan","income:  13000<br />density: 3.488044e-05<br />country_name: Japan","income:  14000<br />density: 3.694098e-05<br />country_name: Japan","income:  15000<br />density: 3.837910e-05<br />country_name: Japan","income:  16000<br />density: 3.919010e-05<br />country_name: Japan","income:  17000<br />density: 3.940351e-05<br />country_name: Japan","income:  18000<br />density: 3.907567e-05<br />country_name: Japan","income:  19000<br />density: 3.828103e-05<br />country_name: Japan","income:  20000<br />density: 3.710348e-05<br />country_name: Japan","income:  21000<br />density: 3.562884e-05<br />country_name: Japan","income:  22000<br />density: 3.393895e-05<br />country_name: Japan","income:  23000<br />density: 3.210775e-05<br />country_name: Japan","income:  24000<br />density: 3.019900e-05<br />country_name: Japan","income:  25000<br />density: 2.826540e-05<br />country_name: Japan","income:  26000<br />density: 2.634879e-05<br />country_name: Japan","income:  27000<br />density: 2.448104e-05<br />country_name: Japan","income:  28000<br />density: 2.268526e-05<br />country_name: Japan","income:  29000<br />density: 2.097719e-05<br />country_name: Japan","income:  30000<br />density: 1.936657e-05<br />country_name: Japan","income:  31000<br />density: 1.785843e-05<br />country_name: Japan","income:  32000<br />density: 1.645417e-05<br />country_name: Japan","income:  33000<br />density: 1.515255e-05<br />country_name: Japan","income:  34000<br />density: 1.395039e-05<br />country_name: Japan","income:  35000<br />density: 1.284326e-05<br />country_name: Japan","income:  36000<br />density: 1.182590e-05<br />country_name: Japan","income:  37000<br />density: 1.089261e-05<br />country_name: Japan","income:  38000<br />density: 1.003750e-05<br />country_name: Japan","income:  39000<br />density: 9.254719e-06<br />country_name: Japan","income:  40000<br />density: 8.538542e-06<br />country_name: Japan","income:  41000<br />density: 7.883503e-06<br />country_name: Japan","income:  42000<br />density: 7.284432e-06<br />country_name: Japan","income:  43000<br />density: 6.736491e-06<br />country_name: Japan","income:  44000<br />density: 6.235188e-06<br />country_name: Japan","income:  45000<br />density: 5.776376e-06<br />country_name: Japan","income:  46000<br />density: 5.356246e-06<br />country_name: Japan","income:  47000<br />density: 4.971313e-06<br />country_name: Japan","income:  48000<br />density: 4.618397e-06<br />country_name: Japan","income:  49000<br />density: 4.294604e-06<br />country_name: Japan","income:  50000<br />density: 3.997301e-06<br />country_name: Japan","income:  51000<br />density: 3.724103e-06<br />country_name: Japan","income:  52000<br />density: 3.472844e-06<br />country_name: Japan","income:  53000<br />density: 3.241565e-06<br />country_name: Japan","income:  54000<br />density: 3.028488e-06<br />country_name: Japan","income:  55000<br />density: 2.832007e-06<br />country_name: Japan","income:  56000<br />density: 2.650665e-06<br />country_name: Japan","income:  57000<br />density: 2.483145e-06<br />country_name: Japan","income:  58000<br />density: 2.328252e-06<br />country_name: Japan","income:  59000<br />density: 2.184905e-06<br />country_name: Japan","income:  60000<br />density: 2.052123e-06<br />country_name: Japan","income:  61000<br />density: 1.929017e-06<br />country_name: Japan","income:  62000<br />density: 1.814780e-06<br />country_name: Japan","income:  63000<br />density: 1.708680e-06<br />country_name: Japan","income:  64000<br />density: 1.610050e-06<br />country_name: Japan","income:  65000<br />density: 1.518286e-06<br />country_name: Japan","income:  66000<br />density: 1.432835e-06<br />country_name: Japan","income:  67000<br />density: 1.353198e-06<br />country_name: Japan","income:  68000<br />density: 1.278917e-06<br />country_name: Japan","income:  69000<br />density: 1.209574e-06<br />country_name: Japan","income:  70000<br />density: 1.144790e-06<br />country_name: Japan","income:  71000<br />density: 1.084217e-06<br />country_name: Japan","income:  72000<br />density: 1.027536e-06<br />country_name: Japan","income:  73000<br />density: 9.744576e-07<br />country_name: Japan","income:  74000<br />density: 9.247145e-07<br />country_name: Japan","income:  75000<br />density: 8.780626e-07<br />country_name: Japan","income:  76000<br />density: 8.342781e-07<br />country_name: Japan","income:  77000<br />density: 7.931553e-07<br />country_name: Japan","income:  78000<br />density: 7.545053e-07<br />country_name: Japan","income:  79000<br />density: 7.181542e-07<br />country_name: Japan","income:  80000<br />density: 6.839422e-07<br />country_name: Japan","income:  81000<br />density: 6.517219e-07<br />country_name: Japan","income:  82000<br />density: 6.213575e-07<br />country_name: Japan","income:  83000<br />density: 5.927238e-07<br />country_name: Japan","income:  84000<br />density: 5.657052e-07<br />country_name: Japan","income:  85000<br />density: 5.401947e-07<br />country_name: Japan","income:  86000<br />density: 5.160935e-07<br />country_name: Japan","income:  87000<br />density: 4.933102e-07<br />country_name: Japan","income:  88000<br />density: 4.717602e-07<br />country_name: Japan","income:  89000<br />density: 4.513649e-07<br />country_name: Japan","income:  90000<br />density: 4.320516e-07<br />country_name: Japan","income:  91000<br />density: 4.137527e-07<br />country_name: Japan","income:  92000<br />density: 3.964056e-07<br />country_name: Japan","income:  93000<br />density: 3.799520e-07<br />country_name: Japan","income:  94000<br />density: 3.643375e-07<br />country_name: Japan","income:  95000<br />density: 3.495119e-07<br />country_name: Japan","income:  96000<br />density: 3.354281e-07<br />country_name: Japan","income:  97000<br />density: 3.220423e-07<br />country_name: Japan","income:  98000<br />density: 3.093138e-07<br />country_name: Japan","income:  99000<br />density: 2.972044e-07<br />country_name: Japan","income: 100000<br />density: 2.856786e-07<br />country_name: Japan","income: 101000<br />density: 2.747033e-07<br />country_name: Japan","income: 102000<br />density: 2.642472e-07<br />country_name: Japan","income: 103000<br />density: 2.542815e-07<br />country_name: Japan","income: 104000<br />density: 2.447790e-07<br />country_name: Japan","income: 105000<br />density: 2.357141e-07<br />country_name: Japan","income: 106000<br />density: 2.270631e-07<br />country_name: Japan","income: 107000<br />density: 2.188036e-07<br />country_name: Japan","income: 108000<br />density: 2.109147e-07<br />country_name: Japan","income: 109000<br />density: 2.033767e-07<br />country_name: Japan","income: 110000<br />density: 1.961710e-07<br />country_name: Japan","income: 111000<br />density: 1.892805e-07<br />country_name: Japan","income: 112000<br />density: 1.826887e-07<br />country_name: Japan","income: 113000<br />density: 1.763803e-07<br />country_name: Japan","income: 114000<br />density: 1.703409e-07<br />country_name: Japan","income: 115000<br />density: 1.645569e-07<br />country_name: Japan","income: 116000<br />density: 1.590155e-07<br />country_name: Japan","income: 117000<br />density: 1.537046e-07<br />country_name: Japan","income: 118000<br />density: 1.486129e-07<br />country_name: Japan","income: 119000<br />density: 1.437296e-07<br />country_name: Japan","income: 120000<br />density: 1.390447e-07<br />country_name: Japan","income: 121000<br />density: 1.345485e-07<br />country_name: Japan","income: 122000<br />density: 1.302321e-07<br />country_name: Japan","income: 123000<br />density: 1.260869e-07<br />country_name: Japan","income: 124000<br />density: 1.221049e-07<br />country_name: Japan","income: 125000<br />density: 1.182785e-07<br />country_name: Japan","income: 126000<br />density: 1.146003e-07<br />country_name: Japan","income: 127000<br />density: 1.110637e-07<br />country_name: Japan","income: 128000<br />density: 1.076621e-07<br />country_name: Japan","income: 129000<br />density: 1.043895e-07<br />country_name: Japan","income: 130000<br />density: 1.012400e-07<br />country_name: Japan","income: 131000<br />density: 9.820807e-08<br />country_name: Japan","income: 132000<br />density: 9.528857e-08<br />country_name: Japan","income: 133000<br />density: 9.247652e-08<br />country_name: Japan","income: 134000<br />density: 8.976722e-08<br />country_name: Japan","income: 135000<br />density: 8.715622e-08<br />country_name: Japan","income: 136000<br />density: 8.463927e-08<br />country_name: Japan","income: 137000<br />density: 8.221235e-08<br />country_name: Japan","income: 138000<br />density: 7.987163e-08<br />country_name: Japan","income: 139000<br />density: 7.761347e-08<br />country_name: Japan","income: 140000<br />density: 7.543441e-08<br />country_name: Japan","income: 141000<br />density: 7.333115e-08<br />country_name: Japan","income: 142000<br />density: 7.130057e-08<br />country_name: Japan","income: 143000<br />density: 6.933967e-08<br />country_name: Japan","income: 144000<br />density: 6.744561e-08<br />country_name: Japan","income: 145000<br />density: 6.561567e-08<br />country_name: Japan","income: 146000<br />density: 6.384729e-08<br />country_name: Japan","income: 147000<br />density: 6.213798e-08<br />country_name: Japan","income: 148000<br />density: 6.048542e-08<br />country_name: Japan","income: 149000<br />density: 5.888734e-08<br />country_name: Japan","income: 150000<br />density: 5.734163e-08<br />country_name: Japan"],"type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"rgba(0,182,235,1)","dash":"solid"},"hoveron":"points","name":"Japan","legendgroup":"Japan","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000,11000,12000,13000,14000,15000,16000,17000,18000,19000,20000,21000,22000,23000,24000,25000,26000,27000,28000,29000,30000,31000,32000,33000,34000,35000,36000,37000,38000,39000,40000,41000,42000,43000,44000,45000,46000,47000,48000,49000,50000,51000,52000,53000,54000,55000,56000,57000,58000,59000,60000,61000,62000,63000,64000,65000,66000,67000,68000,69000,70000,71000,72000,73000,74000,75000,76000,77000,78000,79000,80000,81000,82000,83000,84000,85000,86000,87000,88000,89000,90000,91000,92000,93000,94000,95000,96000,97000,98000,99000,100000,101000,102000,103000,104000,105000,106000,107000,108000,109000,110000,111000,112000,113000,114000,115000,116000,117000,118000,119000,120000,121000,122000,123000,124000,125000,126000,127000,128000,129000,130000,131000,132000,133000,134000,135000,136000,137000,138000,139000,140000,141000,142000,143000,144000,145000,146000,147000,148000,149000,150000],"y":[8.7196828786547932e-13,2.5909505605877148e-07,9.1655262034077428e-07,1.915140977347073e-06,3.2204427794368759e-06,4.7998259188274347e-06,6.6172846193115543e-06,8.6314240531155547e-06,1.0794948432835422e-05,1.3055202227008301e-05,1.5355554808164428e-05,1.7637444128592685e-05,1.9842860452160837e-05,2.1917011012857903e-05,2.381088724843264e-05,2.5483470855908937e-05,2.6903365263197279e-05,2.8049717694586068e-05,2.8912389722545285e-05,2.9491424486850204e-05,2.9795931950727692e-05,2.9842560129198832e-05,2.9653737189035844e-05,2.9255859704913721e-05,2.867757326658112e-05,2.7948251823078416e-05,2.7096739871430008e-05,2.6150383176445965e-05,2.5134342989835895e-05,2.4071167216032437e-05,2.2980579343014273e-05,2.1879440798969945e-05,2.0781842803510656e-05,1.9699287829512746e-05,1.8640926816087382e-05,1.7613825000513304e-05,1.6623235794795845e-05,1.5672867998626214e-05,1.4765136564046818e-05,1.3901391046657682e-05,1.3082118850833746e-05,1.230712252271691e-05,1.1575671810176181e-05,1.0886632139190714e-05,1.0238571683228611e-05,9.6298494381333938e-06,9.0586867501035843e-06,8.523224648489743e-06,8.0215691605059314e-06,7.5518265690983155e-06,7.1121303441033042e-06,6.7006612476325682e-06,6.3156618979852261e-06,5.9554468783145522e-06,5.6184092995370428e-06,5.3030245721987957e-06,5.007852008480257e-06,4.7315347617087376e-06,4.4727985147274422e-06,4.2304492481653562e-06,4.0033703530029993e-06,3.7905192968908513e-06,3.5909240086760848e-06,3.4036791089495859e-06,3.2279420847563719e-06,3.0629294827289531e-06,2.9079131757871737e-06,2.7622167433429724e-06,2.6252119929349289e-06,2.4963156417968026e-06,2.3749861695396348e-06,2.260720847492028e-06,2.1530529459603537e-06,2.051549117467995e-06,1.9558069516864718e-06,1.8654526961013578e-06,1.7801391353152886e-06,1.6995436211630197e-06,1.6233662454031278e-06,1.5513281465837659e-06,1.4831699426948919e-06,1.4186502813690669e-06,1.3575444996399644e-06,1.2996433855825939e-06,1.2447520345186692e-06,1.1926887928572393e-06,1.1432842830409286e-06,1.0963805034713958e-06,1.0518299976867289e-06,1.0094950874524483e-06,9.6924716480307062e-07,9.3096603842958971e-07,8.9453933014882657e-07,8.5986191751155987e-07,8.2683541890809355e-07,7.9536771781201633e-07,7.6537252306600872e-07,7.3676896235805951e-07,7.0948120626329538e-07,6.8343812043674257e-07,6.5857294373609729e-07,6.3482299023296287e-07,6.1212937323573876e-07,5.9043674959952166e-07,5.6969308273795704e-07,5.4984942288060567e-07,5.3085970323736225e-07,5.1268055083986042e-07,4.952711109293175e-07,4.7859288385155947e-07,4.626095735037681e-07,4.4728694645427691e-07,4.3259270092727956e-07,4.1849634490895071e-07,4.0496908269072415e-07,3.9198370921992454e-07,3.7951451167780015e-07,3.6753717775076109e-07,3.560287101026961e-07,3.4496734659465451e-07,3.3433248583369892e-07,3.2410461766514148e-07,3.1426525825218984e-07,3.0479688941446912e-07,2.9568290192209118e-07,2.8690754246498186e-07,2.7845586403857642e-07,2.7031367950645725e-07,2.6246751811860207e-07,2.5490458478037669e-07,2.4761272188276546e-07,2.4058037351829626e-07,2.3379655192009311e-07,2.272508059734225e-07,2.2093319166006582e-07,2.1483424430602566e-07,2.0894495251239573e-07,2.0325673365789434e-07,1.9776141086948877e-07,1.9245119136496978e-07,1.8731864607805882e-07,1.8235669048300088e-07,1.7755856654135262e-07,1.7291782569909175e-07,1.6842831286714394e-07,1.6408415132303311e-07,1.5987972847563234e-07,1.5580968243895553e-07,1.5186888936459156e-07,1.4805245148578166e-07,1.4435568582930663e-07],"text":["income:      0<br />density: 8.719683e-13<br />country_name: United Kingdom","income:   1000<br />density: 2.590951e-07<br />country_name: United Kingdom","income:   2000<br />density: 9.165526e-07<br />country_name: United Kingdom","income:   3000<br />density: 1.915141e-06<br />country_name: United Kingdom","income:   4000<br />density: 3.220443e-06<br />country_name: United Kingdom","income:   5000<br />density: 4.799826e-06<br />country_name: United Kingdom","income:   6000<br />density: 6.617285e-06<br />country_name: United Kingdom","income:   7000<br />density: 8.631424e-06<br />country_name: United Kingdom","income:   8000<br />density: 1.079495e-05<br />country_name: United Kingdom","income:   9000<br />density: 1.305520e-05<br />country_name: United Kingdom","income:  10000<br />density: 1.535555e-05<br />country_name: United Kingdom","income:  11000<br />density: 1.763744e-05<br />country_name: United Kingdom","income:  12000<br />density: 1.984286e-05<br />country_name: United Kingdom","income:  13000<br />density: 2.191701e-05<br />country_name: United Kingdom","income:  14000<br />density: 2.381089e-05<br />country_name: United Kingdom","income:  15000<br />density: 2.548347e-05<br />country_name: United Kingdom","income:  16000<br />density: 2.690337e-05<br />country_name: United Kingdom","income:  17000<br />density: 2.804972e-05<br />country_name: United Kingdom","income:  18000<br />density: 2.891239e-05<br />country_name: United Kingdom","income:  19000<br />density: 2.949142e-05<br />country_name: United Kingdom","income:  20000<br />density: 2.979593e-05<br />country_name: United Kingdom","income:  21000<br />density: 2.984256e-05<br />country_name: United Kingdom","income:  22000<br />density: 2.965374e-05<br />country_name: United Kingdom","income:  23000<br />density: 2.925586e-05<br />country_name: United Kingdom","income:  24000<br />density: 2.867757e-05<br />country_name: United Kingdom","income:  25000<br />density: 2.794825e-05<br />country_name: United Kingdom","income:  26000<br />density: 2.709674e-05<br />country_name: United Kingdom","income:  27000<br />density: 2.615038e-05<br />country_name: United Kingdom","income:  28000<br />density: 2.513434e-05<br />country_name: United Kingdom","income:  29000<br />density: 2.407117e-05<br />country_name: United Kingdom","income:  30000<br />density: 2.298058e-05<br />country_name: United Kingdom","income:  31000<br />density: 2.187944e-05<br />country_name: United Kingdom","income:  32000<br />density: 2.078184e-05<br />country_name: United Kingdom","income:  33000<br />density: 1.969929e-05<br />country_name: United Kingdom","income:  34000<br />density: 1.864093e-05<br />country_name: United Kingdom","income:  35000<br />density: 1.761383e-05<br />country_name: United Kingdom","income:  36000<br />density: 1.662324e-05<br />country_name: United Kingdom","income:  37000<br />density: 1.567287e-05<br />country_name: United Kingdom","income:  38000<br />density: 1.476514e-05<br />country_name: United Kingdom","income:  39000<br />density: 1.390139e-05<br />country_name: United Kingdom","income:  40000<br />density: 1.308212e-05<br />country_name: United Kingdom","income:  41000<br />density: 1.230712e-05<br />country_name: United Kingdom","income:  42000<br />density: 1.157567e-05<br />country_name: United Kingdom","income:  43000<br />density: 1.088663e-05<br />country_name: United Kingdom","income:  44000<br />density: 1.023857e-05<br />country_name: United Kingdom","income:  45000<br />density: 9.629849e-06<br />country_name: United Kingdom","income:  46000<br />density: 9.058687e-06<br />country_name: United Kingdom","income:  47000<br />density: 8.523225e-06<br />country_name: United Kingdom","income:  48000<br />density: 8.021569e-06<br />country_name: United Kingdom","income:  49000<br />density: 7.551827e-06<br />country_name: United Kingdom","income:  50000<br />density: 7.112130e-06<br />country_name: United Kingdom","income:  51000<br />density: 6.700661e-06<br />country_name: United Kingdom","income:  52000<br />density: 6.315662e-06<br />country_name: United Kingdom","income:  53000<br />density: 5.955447e-06<br />country_name: United Kingdom","income:  54000<br />density: 5.618409e-06<br />country_name: United Kingdom","income:  55000<br />density: 5.303025e-06<br />country_name: United Kingdom","income:  56000<br />density: 5.007852e-06<br />country_name: United Kingdom","income:  57000<br />density: 4.731535e-06<br />country_name: United Kingdom","income:  58000<br />density: 4.472799e-06<br />country_name: United Kingdom","income:  59000<br />density: 4.230449e-06<br />country_name: United Kingdom","income:  60000<br />density: 4.003370e-06<br />country_name: United Kingdom","income:  61000<br />density: 3.790519e-06<br />country_name: United Kingdom","income:  62000<br />density: 3.590924e-06<br />country_name: United Kingdom","income:  63000<br />density: 3.403679e-06<br />country_name: United Kingdom","income:  64000<br />density: 3.227942e-06<br />country_name: United Kingdom","income:  65000<br />density: 3.062929e-06<br />country_name: United Kingdom","income:  66000<br />density: 2.907913e-06<br />country_name: United Kingdom","income:  67000<br />density: 2.762217e-06<br />country_name: United Kingdom","income:  68000<br />density: 2.625212e-06<br />country_name: United Kingdom","income:  69000<br />density: 2.496316e-06<br />country_name: United Kingdom","income:  70000<br />density: 2.374986e-06<br />country_name: United Kingdom","income:  71000<br />density: 2.260721e-06<br />country_name: United Kingdom","income:  72000<br />density: 2.153053e-06<br />country_name: United Kingdom","income:  73000<br />density: 2.051549e-06<br />country_name: United Kingdom","income:  74000<br />density: 1.955807e-06<br />country_name: United Kingdom","income:  75000<br />density: 1.865453e-06<br />country_name: United Kingdom","income:  76000<br />density: 1.780139e-06<br />country_name: United Kingdom","income:  77000<br />density: 1.699544e-06<br />country_name: United Kingdom","income:  78000<br />density: 1.623366e-06<br />country_name: United Kingdom","income:  79000<br />density: 1.551328e-06<br />country_name: United Kingdom","income:  80000<br />density: 1.483170e-06<br />country_name: United Kingdom","income:  81000<br />density: 1.418650e-06<br />country_name: United Kingdom","income:  82000<br />density: 1.357544e-06<br />country_name: United Kingdom","income:  83000<br />density: 1.299643e-06<br />country_name: United Kingdom","income:  84000<br />density: 1.244752e-06<br />country_name: United Kingdom","income:  85000<br />density: 1.192689e-06<br />country_name: United Kingdom","income:  86000<br />density: 1.143284e-06<br />country_name: United Kingdom","income:  87000<br />density: 1.096381e-06<br />country_name: United Kingdom","income:  88000<br />density: 1.051830e-06<br />country_name: United Kingdom","income:  89000<br />density: 1.009495e-06<br />country_name: United Kingdom","income:  90000<br />density: 9.692472e-07<br />country_name: United Kingdom","income:  91000<br />density: 9.309660e-07<br />country_name: United Kingdom","income:  92000<br />density: 8.945393e-07<br />country_name: United Kingdom","income:  93000<br />density: 8.598619e-07<br />country_name: United Kingdom","income:  94000<br />density: 8.268354e-07<br />country_name: United Kingdom","income:  95000<br />density: 7.953677e-07<br />country_name: United Kingdom","income:  96000<br />density: 7.653725e-07<br />country_name: United Kingdom","income:  97000<br />density: 7.367690e-07<br />country_name: United Kingdom","income:  98000<br />density: 7.094812e-07<br />country_name: United Kingdom","income:  99000<br />density: 6.834381e-07<br />country_name: United Kingdom","income: 100000<br />density: 6.585729e-07<br />country_name: United Kingdom","income: 101000<br />density: 6.348230e-07<br />country_name: United Kingdom","income: 102000<br />density: 6.121294e-07<br />country_name: United Kingdom","income: 103000<br />density: 5.904367e-07<br />country_name: United Kingdom","income: 104000<br />density: 5.696931e-07<br />country_name: United Kingdom","income: 105000<br />density: 5.498494e-07<br />country_name: United Kingdom","income: 106000<br />density: 5.308597e-07<br />country_name: United Kingdom","income: 107000<br />density: 5.126806e-07<br />country_name: United Kingdom","income: 108000<br />density: 4.952711e-07<br />country_name: United Kingdom","income: 109000<br />density: 4.785929e-07<br />country_name: United Kingdom","income: 110000<br />density: 4.626096e-07<br />country_name: United Kingdom","income: 111000<br />density: 4.472869e-07<br />country_name: United Kingdom","income: 112000<br />density: 4.325927e-07<br />country_name: United Kingdom","income: 113000<br />density: 4.184963e-07<br />country_name: United Kingdom","income: 114000<br />density: 4.049691e-07<br />country_name: United Kingdom","income: 115000<br />density: 3.919837e-07<br />country_name: United Kingdom","income: 116000<br />density: 3.795145e-07<br />country_name: United Kingdom","income: 117000<br />density: 3.675372e-07<br />country_name: United Kingdom","income: 118000<br />density: 3.560287e-07<br />country_name: United Kingdom","income: 119000<br />density: 3.449673e-07<br />country_name: United Kingdom","income: 120000<br />density: 3.343325e-07<br />country_name: United Kingdom","income: 121000<br />density: 3.241046e-07<br />country_name: United Kingdom","income: 122000<br />density: 3.142653e-07<br />country_name: United Kingdom","income: 123000<br />density: 3.047969e-07<br />country_name: United Kingdom","income: 124000<br />density: 2.956829e-07<br />country_name: United Kingdom","income: 125000<br />density: 2.869075e-07<br />country_name: United Kingdom","income: 126000<br />density: 2.784559e-07<br />country_name: United Kingdom","income: 127000<br />density: 2.703137e-07<br />country_name: United Kingdom","income: 128000<br />density: 2.624675e-07<br />country_name: United Kingdom","income: 129000<br />density: 2.549046e-07<br />country_name: United Kingdom","income: 130000<br />density: 2.476127e-07<br />country_name: United Kingdom","income: 131000<br />density: 2.405804e-07<br />country_name: United Kingdom","income: 132000<br />density: 2.337966e-07<br />country_name: United Kingdom","income: 133000<br />density: 2.272508e-07<br />country_name: United Kingdom","income: 134000<br />density: 2.209332e-07<br />country_name: United Kingdom","income: 135000<br />density: 2.148342e-07<br />country_name: United Kingdom","income: 136000<br />density: 2.089450e-07<br />country_name: United Kingdom","income: 137000<br />density: 2.032567e-07<br />country_name: United Kingdom","income: 138000<br />density: 1.977614e-07<br />country_name: United Kingdom","income: 139000<br />density: 1.924512e-07<br />country_name: United Kingdom","income: 140000<br />density: 1.873186e-07<br />country_name: United Kingdom","income: 141000<br />density: 1.823567e-07<br />country_name: United Kingdom","income: 142000<br />density: 1.775586e-07<br />country_name: United Kingdom","income: 143000<br />density: 1.729178e-07<br />country_name: United Kingdom","income: 144000<br />density: 1.684283e-07<br />country_name: United Kingdom","income: 145000<br />density: 1.640842e-07<br />country_name: United Kingdom","income: 146000<br />density: 1.598797e-07<br />country_name: United Kingdom","income: 147000<br />density: 1.558097e-07<br />country_name: United Kingdom","income: 148000<br />density: 1.518689e-07<br />country_name: United Kingdom","income: 149000<br />density: 1.480525e-07<br />country_name: United Kingdom","income: 150000<br />density: 1.443557e-07<br />country_name: United Kingdom"],"type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"rgba(165,138,255,1)","dash":"solid"},"hoveron":"points","name":"United Kingdom","legendgroup":"United Kingdom","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null},{"x":[0,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000,11000,12000,13000,14000,15000,16000,17000,18000,19000,20000,21000,22000,23000,24000,25000,26000,27000,28000,29000,30000,31000,32000,33000,34000,35000,36000,37000,38000,39000,40000,41000,42000,43000,44000,45000,46000,47000,48000,49000,50000,51000,52000,53000,54000,55000,56000,57000,58000,59000,60000,61000,62000,63000,64000,65000,66000,67000,68000,69000,70000,71000,72000,73000,74000,75000,76000,77000,78000,79000,80000,81000,82000,83000,84000,85000,86000,87000,88000,89000,90000,91000,92000,93000,94000,95000,96000,97000,98000,99000,100000,101000,102000,103000,104000,105000,106000,107000,108000,109000,110000,111000,112000,113000,114000,115000,116000,117000,118000,119000,120000,121000,122000,123000,124000,125000,126000,127000,128000,129000,130000,131000,132000,133000,134000,135000,136000,137000,138000,139000,140000,141000,142000,143000,144000,145000,146000,147000,148000,149000,150000],"y":[3.9339651341556778e-12,1.5225314337378857e-07,4.392084410607657e-07,8.1549425623883448e-07,1.263529966049945e-06,1.772019479500477e-06,2.3322242843699004e-06,2.9366245216334768e-06,3.5783132244178886e-06,4.2506927461238552e-06,4.9473202114613083e-06,5.661836233821446e-06,6.3879444600592832e-06,7.1194239379188358e-06,7.8501631582687705e-06,8.5742081309786954e-06,9.2858187811385707e-06,9.9795291122317055e-06,1.0650207370974728e-05,1.1293113065971496e-05,1.1903948241133672e-05,1.2478900933147372e-05,1.3014679267455285e-05,1.3508535168724903e-05,1.3958277169066254e-05,1.436227227518385e-05,1.4719437288086088e-05,1.5029220341519573e-05,1.5291573726941745e-05,1.5506919296984853e-05,1.5676107884088816e-05,1.5800374238902191e-05,1.5881288990636559e-05,1.5920709068304357e-05,1.5920727909135508e-05,1.5883626630901204e-05,1.5811827170835318e-05,1.5707848207064841e-05,1.5574264489326521e-05,1.541367002292338e-05,1.5228645380140663e-05,1.5021729261520821e-05,1.4795394298532731e-05,1.4552026980679574e-05,1.429391150404957e-05,1.402321727376025e-05,1.3741989747930531e-05,1.3452144283495597e-05,1.3155462631841934e-05,1.2853591732273568e-05,1.2548044461195279e-05,1.2240202012237528e-05,1.1931317605222099e-05,1.1622521248023409e-05,1.1314825303458017e-05,1.1009130642063113e-05,1.0706233190002815e-05,1.0406830708612653e-05,1.011152966772496e-05,9.8208520985406348e-06,9.5352423332312043e-06,9.2550735575721854e-06,8.9806541197353503e-06,8.7122335529731908e-06,8.4500082824421238e-06,8.1941269969822695e-06,7.9446956754788244e-06,7.7017822646536423e-06,7.4654210109577286e-06,7.2356164538361631e-06,7.0123470911789844e-06,6.7955687304134483e-06,6.5852175405755408e-06,6.3812128219450014e-06,6.1834595105563186e-06,5.9918504352008646e-06,5.8062683445019722e-06,5.6265877213457719e-06,5.4526764014501003e-06,5.2843970122012638e-06,5.1216082471308481e-06,4.9641659905743607e-06,4.8119243061813298e-06,4.664736302055585e-06,4.522454884413179e-06,4.3849334107667266e-06,4.2520262527929271e-06,4.123589278219302e-06,3.9994802602836632e-06,3.8795592225810483e-06,3.7636887264159428e-06,3.6517341071282195e-06,3.5435636652553724e-06,3.4390488178334376e-06,3.3380642146210715e-06,3.2404878235557986e-06,3.1462009893145971e-06,3.0550884684523222e-06,2.9670384442272499e-06,2.8819425238922548e-06,2.7996957209292788e-06,2.7201964244326081e-06,2.6433463576001204e-06,2.5690505270695468e-06,2.4972171646362679e-06,2.427757662709381e-06,2.3605865047004989e-06,2.2956211913949782e-06,2.2327821642252618e-06,2.1719927262495107e-06,2.1131789615355274e-06,2.0562696535568717e-06,2.001196203126204e-06,1.9478925463174632e-06,1.8962950727635306e-06,1.8463425446585634e-06,1.7979760167432649e-06,1.7511387575062857e-06,1.7057761717954127e-06,1.6618357249973219e-06,1.6192668689139629e-06,1.5780209694371411e-06,1.5380512360989066e-06,1.4993126535555846e-06,1.4617619150450854e-06,1.4253573578420271e-06,1.3900589007219165e-06,1.3558279834343199e-06,1.3226275081752756e-06,1.2904217830408182e-06,1.2591764674368848e-06,1.2288585194146789e-06,1.1994361448961104e-06,1.1708787487497975e-06,1.1431568876750778e-06,1.1162422248489854e-06,1.0901074862892141e-06,1.0647264188847793e-06,1.040073750045083e-06,1.0161251489176233e-06,9.9285718912422954e-07,9.7024731296589341e-07,9.4827379704645056e-07,9.2691571926596149e-07,9.0615292713517315e-07,8.8596600736338307e-07,8.6633625667288253e-07,8.4724565379413524e-07,8.2867683259704359e-07,8.1061305631470127e-07,7.9303819281730253e-07],"text":["income:      0<br />density: 3.933965e-12<br />country_name: United States","income:   1000<br />density: 1.522531e-07<br />country_name: United States","income:   2000<br />density: 4.392084e-07<br />country_name: United States","income:   3000<br />density: 8.154943e-07<br />country_name: United States","income:   4000<br />density: 1.263530e-06<br />country_name: United States","income:   5000<br />density: 1.772019e-06<br />country_name: United States","income:   6000<br />density: 2.332224e-06<br />country_name: United States","income:   7000<br />density: 2.936625e-06<br />country_name: United States","income:   8000<br />density: 3.578313e-06<br />country_name: United States","income:   9000<br />density: 4.250693e-06<br />country_name: United States","income:  10000<br />density: 4.947320e-06<br />country_name: United States","income:  11000<br />density: 5.661836e-06<br />country_name: United States","income:  12000<br />density: 6.387944e-06<br />country_name: United States","income:  13000<br />density: 7.119424e-06<br />country_name: United States","income:  14000<br />density: 7.850163e-06<br />country_name: United States","income:  15000<br />density: 8.574208e-06<br />country_name: United States","income:  16000<br />density: 9.285819e-06<br />country_name: United States","income:  17000<br />density: 9.979529e-06<br />country_name: United States","income:  18000<br />density: 1.065021e-05<br />country_name: United States","income:  19000<br />density: 1.129311e-05<br />country_name: United States","income:  20000<br />density: 1.190395e-05<br />country_name: United States","income:  21000<br />density: 1.247890e-05<br />country_name: United States","income:  22000<br />density: 1.301468e-05<br />country_name: United States","income:  23000<br />density: 1.350854e-05<br />country_name: United States","income:  24000<br />density: 1.395828e-05<br />country_name: United States","income:  25000<br />density: 1.436227e-05<br />country_name: United States","income:  26000<br />density: 1.471944e-05<br />country_name: United States","income:  27000<br />density: 1.502922e-05<br />country_name: United States","income:  28000<br />density: 1.529157e-05<br />country_name: United States","income:  29000<br />density: 1.550692e-05<br />country_name: United States","income:  30000<br />density: 1.567611e-05<br />country_name: United States","income:  31000<br />density: 1.580037e-05<br />country_name: United States","income:  32000<br />density: 1.588129e-05<br />country_name: United States","income:  33000<br />density: 1.592071e-05<br />country_name: United States","income:  34000<br />density: 1.592073e-05<br />country_name: United States","income:  35000<br />density: 1.588363e-05<br />country_name: United States","income:  36000<br />density: 1.581183e-05<br />country_name: United States","income:  37000<br />density: 1.570785e-05<br />country_name: United States","income:  38000<br />density: 1.557426e-05<br />country_name: United States","income:  39000<br />density: 1.541367e-05<br />country_name: United States","income:  40000<br />density: 1.522865e-05<br />country_name: United States","income:  41000<br />density: 1.502173e-05<br />country_name: United States","income:  42000<br />density: 1.479539e-05<br />country_name: United States","income:  43000<br />density: 1.455203e-05<br />country_name: United States","income:  44000<br />density: 1.429391e-05<br />country_name: United States","income:  45000<br />density: 1.402322e-05<br />country_name: United States","income:  46000<br />density: 1.374199e-05<br />country_name: United States","income:  47000<br />density: 1.345214e-05<br />country_name: United States","income:  48000<br />density: 1.315546e-05<br />country_name: United States","income:  49000<br />density: 1.285359e-05<br />country_name: United States","income:  50000<br />density: 1.254804e-05<br />country_name: United States","income:  51000<br />density: 1.224020e-05<br />country_name: United States","income:  52000<br />density: 1.193132e-05<br />country_name: United States","income:  53000<br />density: 1.162252e-05<br />country_name: United States","income:  54000<br />density: 1.131483e-05<br />country_name: United States","income:  55000<br />density: 1.100913e-05<br />country_name: United States","income:  56000<br />density: 1.070623e-05<br />country_name: United States","income:  57000<br />density: 1.040683e-05<br />country_name: United States","income:  58000<br />density: 1.011153e-05<br />country_name: United States","income:  59000<br />density: 9.820852e-06<br />country_name: United States","income:  60000<br />density: 9.535242e-06<br />country_name: United States","income:  61000<br />density: 9.255074e-06<br />country_name: United States","income:  62000<br />density: 8.980654e-06<br />country_name: United States","income:  63000<br />density: 8.712234e-06<br />country_name: United States","income:  64000<br />density: 8.450008e-06<br />country_name: United States","income:  65000<br />density: 8.194127e-06<br />country_name: United States","income:  66000<br />density: 7.944696e-06<br />country_name: United States","income:  67000<br />density: 7.701782e-06<br />country_name: United States","income:  68000<br />density: 7.465421e-06<br />country_name: United States","income:  69000<br />density: 7.235616e-06<br />country_name: United States","income:  70000<br />density: 7.012347e-06<br />country_name: United States","income:  71000<br />density: 6.795569e-06<br />country_name: United States","income:  72000<br />density: 6.585218e-06<br />country_name: United States","income:  73000<br />density: 6.381213e-06<br />country_name: United States","income:  74000<br />density: 6.183460e-06<br />country_name: United States","income:  75000<br />density: 5.991850e-06<br />country_name: United States","income:  76000<br />density: 5.806268e-06<br />country_name: United States","income:  77000<br />density: 5.626588e-06<br />country_name: United States","income:  78000<br />density: 5.452676e-06<br />country_name: United States","income:  79000<br />density: 5.284397e-06<br />country_name: United States","income:  80000<br />density: 5.121608e-06<br />country_name: United States","income:  81000<br />density: 4.964166e-06<br />country_name: United States","income:  82000<br />density: 4.811924e-06<br />country_name: United States","income:  83000<br />density: 4.664736e-06<br />country_name: United States","income:  84000<br />density: 4.522455e-06<br />country_name: United States","income:  85000<br />density: 4.384933e-06<br />country_name: United States","income:  86000<br />density: 4.252026e-06<br />country_name: United States","income:  87000<br />density: 4.123589e-06<br />country_name: United States","income:  88000<br />density: 3.999480e-06<br />country_name: United States","income:  89000<br />density: 3.879559e-06<br />country_name: United States","income:  90000<br />density: 3.763689e-06<br />country_name: United States","income:  91000<br />density: 3.651734e-06<br />country_name: United States","income:  92000<br />density: 3.543564e-06<br />country_name: United States","income:  93000<br />density: 3.439049e-06<br />country_name: United States","income:  94000<br />density: 3.338064e-06<br />country_name: United States","income:  95000<br />density: 3.240488e-06<br />country_name: United States","income:  96000<br />density: 3.146201e-06<br />country_name: United States","income:  97000<br />density: 3.055088e-06<br />country_name: United States","income:  98000<br />density: 2.967038e-06<br />country_name: United States","income:  99000<br />density: 2.881943e-06<br />country_name: United States","income: 100000<br />density: 2.799696e-06<br />country_name: United States","income: 101000<br />density: 2.720196e-06<br />country_name: United States","income: 102000<br />density: 2.643346e-06<br />country_name: United States","income: 103000<br />density: 2.569051e-06<br />country_name: United States","income: 104000<br />density: 2.497217e-06<br />country_name: United States","income: 105000<br />density: 2.427758e-06<br />country_name: United States","income: 106000<br />density: 2.360587e-06<br />country_name: United States","income: 107000<br />density: 2.295621e-06<br />country_name: United States","income: 108000<br />density: 2.232782e-06<br />country_name: United States","income: 109000<br />density: 2.171993e-06<br />country_name: United States","income: 110000<br />density: 2.113179e-06<br />country_name: United States","income: 111000<br />density: 2.056270e-06<br />country_name: United States","income: 112000<br />density: 2.001196e-06<br />country_name: United States","income: 113000<br />density: 1.947893e-06<br />country_name: United States","income: 114000<br />density: 1.896295e-06<br />country_name: United States","income: 115000<br />density: 1.846343e-06<br />country_name: United States","income: 116000<br />density: 1.797976e-06<br />country_name: United States","income: 117000<br />density: 1.751139e-06<br />country_name: United States","income: 118000<br />density: 1.705776e-06<br />country_name: United States","income: 119000<br />density: 1.661836e-06<br />country_name: United States","income: 120000<br />density: 1.619267e-06<br />country_name: United States","income: 121000<br />density: 1.578021e-06<br />country_name: United States","income: 122000<br />density: 1.538051e-06<br />country_name: United States","income: 123000<br />density: 1.499313e-06<br />country_name: United States","income: 124000<br />density: 1.461762e-06<br />country_name: United States","income: 125000<br />density: 1.425357e-06<br />country_name: United States","income: 126000<br />density: 1.390059e-06<br />country_name: United States","income: 127000<br />density: 1.355828e-06<br />country_name: United States","income: 128000<br />density: 1.322628e-06<br />country_name: United States","income: 129000<br />density: 1.290422e-06<br />country_name: United States","income: 130000<br />density: 1.259176e-06<br />country_name: United States","income: 131000<br />density: 1.228859e-06<br />country_name: United States","income: 132000<br />density: 1.199436e-06<br />country_name: United States","income: 133000<br />density: 1.170879e-06<br />country_name: United States","income: 134000<br />density: 1.143157e-06<br />country_name: United States","income: 135000<br />density: 1.116242e-06<br />country_name: United States","income: 136000<br />density: 1.090107e-06<br />country_name: United States","income: 137000<br />density: 1.064726e-06<br />country_name: United States","income: 138000<br />density: 1.040074e-06<br />country_name: United States","income: 139000<br />density: 1.016125e-06<br />country_name: United States","income: 140000<br />density: 9.928572e-07<br />country_name: United States","income: 141000<br />density: 9.702473e-07<br />country_name: United States","income: 142000<br />density: 9.482738e-07<br />country_name: United States","income: 143000<br />density: 9.269157e-07<br />country_name: United States","income: 144000<br />density: 9.061529e-07<br />country_name: United States","income: 145000<br />density: 8.859660e-07<br />country_name: United States","income: 146000<br />density: 8.663363e-07<br />country_name: United States","income: 147000<br />density: 8.472457e-07<br />country_name: United States","income: 148000<br />density: 8.286768e-07<br />country_name: United States","income: 149000<br />density: 8.106131e-07<br />country_name: United States","income: 150000<br />density: 7.930382e-07<br />country_name: United States"],"type":"scatter","mode":"lines","line":{"width":1.8897637795275593,"color":"rgba(251,97,215,1)","dash":"solid"},"hoveron":"points","name":"United States","legendgroup":"United States","showlegend":true,"xaxis":"x","yaxis":"y","hoverinfo":"text","frame":null}],"layout":{"margin":{"t":40.840182648401829,"r":7.3059360730593621,"b":37.260273972602747,"l":60.639269406392714},"paper_bgcolor":"rgba(255,255,255,1)","font":{"color":"rgba(0,0,0,1)","family":"","size":14.611872146118724},"title":{"text":"Estimated income distribution of OECD countries (log-logistic distribution)","font":{"color":"rgba(0,0,0,1)","family":"","size":17.534246575342465},"x":0,"xref":"paper"},"xaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[-7500,157500],"tickmode":"array","ticktext":["$0","$50,000","$100,000","$150,000"],"tickvals":[0,50000.000000000007,100000,150000],"categoryorder":"array","categoryarray":["$0","$50,000","$100,000","$150,000"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":3.6529680365296811,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":11.68949771689498},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(235,235,235,1)","gridwidth":0.66417600664176002,"zeroline":false,"anchor":"y","title":{"text":"Income (USD PPP)","font":{"color":"rgba(0,0,0,1)","family":"","size":14.611872146118724}},"hoverformat":".2f"},"yaxis":{"domain":[0,1],"automargin":true,"type":"linear","autorange":false,"range":[-1.9701753768270424e-06,4.1373682927156054e-05],"tickmode":"array","ticktext":["0.0000","0.0000","0.0000","0.0000","0.0000"],"tickvals":[0,1.0000000000000003e-05,2.0000000000000002e-05,3.0000000000000004e-05,4.0000000000000003e-05],"categoryorder":"array","categoryarray":["0.0000","0.0000","0.0000","0.0000","0.0000"],"nticks":null,"ticks":"","tickcolor":null,"ticklen":3.6529680365296811,"tickwidth":0,"showticklabels":true,"tickfont":{"color":"rgba(77,77,77,1)","family":"","size":11.68949771689498},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"gridcolor":"rgba(235,235,235,1)","gridwidth":0.66417600664176002,"zeroline":false,"anchor":"x","title":{"text":"Probability density","font":{"color":"rgba(0,0,0,1)","family":"","size":14.611872146118724}},"hoverformat":".2f"},"shapes":[],"showlegend":true,"legend":{"bgcolor":null,"bordercolor":null,"borderwidth":0,"font":{"color":"rgba(0,0,0,1)","family":"","size":11.68949771689498},"title":{"text":"Country","font":{"color":"rgba(0,0,0,1)","family":"","size":14.611872146118724}}},"hovermode":"closest","barmode":"relative"},"config":{"doubleClick":"reset","modeBarButtonsToAdd":["hoverclosest","hovercompare"],"showSendToCloud":false},"source":"A","attrs":{"1b803bf34668":{"x":{},"y":{},"colour":{},"type":"scatter"}},"cur_data":"1b803bf34668","visdat":{"1b803bf34668":["function (y) ","x"]},"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.20000000000000001,"selected":{"opacity":1},"debounce":0},"shinyEvents":["plotly_hover","plotly_click","plotly_selected","plotly_relayout","plotly_brushed","plotly_brushing","plotly_clickannotation","plotly_doubleclick","plotly_deselect","plotly_afterplot","plotly_sunburstclick"],"base_url":"https://plot.ly"},"evals":[],"jsHooks":[]}</script>
</div>
</div>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>In this article, I introduced a method for reconstructing an income distribution from its median and Gini coefficient using the log-logistic distribution. By using the log-logistic distribution, we can capture the overall shape of an income distribution from just these two statistics: the median income and the Gini coefficient.</p>
<p>Note that this method may differ from the actual income distribution. To estimate a more accurate income distribution, additional data and a more complex model would be required.</p>


</section>


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

<ol>
<li id="fn1"><p>The median income is widely used as a representative statistic for income distributions because, unlike the mean, it is less affected by high-income earners.↩︎</p></li>
<li id="fn2"><p><a href="https://ja.wikipedia.org/wiki/%E3%82%B8%E3%83%8B%E4%BF%82%E6%95%B0">The Gini coefficient</a> is a standard measure of the inequality of income distribution.↩︎</p></li>
<li id="fn3"><p><a href="https://ja.wikipedia.org/wiki/%E8%B3%BC%E8%B2%B7%E5%8A%9B%E5%B9%B3%E4%BE%A1%E8%AA%AC#OECD%E7%B5%B1%E8%A8%88%E3%81%AE%E7%9B%B8%E5%AF%BE%E7%9A%84%E7%89%A9%E4%BE%A1%E6%B0%B4%E6%BA%96" class="uri">https://ja.wikipedia.org/wiki/%E8%B3%BC%E8%B2%B7%E5%8A%9B%E5%B9%B3%E4%BE%A1%E8%AA%AC#OECD%E7%B5%B1%E8%A8%88%E3%81%AE%E7%9B%B8%E5%AF%BE%E7%9A%84%E7%89%A9%E4%BE%A1%E6%B0%B4%E6%BA%96</a>↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>Statistics</category>
  <category>R</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2025/11/income-distribution-from-median-and-gini-log-logistic.html</guid>
  <pubDate>Tue, 25 Nov 2025 15:00:00 GMT</pubDate>
</item>
<item>
  <title>Guiding data processing with adverbial::step_by_step() in R</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2025/05/guiding-data-processing-with-adverbial-step_by_step-in-r.html</link>
  <description><![CDATA[ 





<p>Implementing the right functions is essential for efficiently sharing data processing knowledge. However, finding the right balance between usability and customization can be challenging.</p>
<p>One approach to achieving this balance is to break the data processing flow into multiple functions. To ensure the effectiveness of this approach, users must have a clear understanding of the overall workflow.</p>
<p>One of the advantages of programming, as I see it, is that it allows me to make explicit implicit knowledge that previously had to be accurately understood by humans. This principle inspired the implementation of step-by-step data processing functions in the R adverbial package.</p>
<p>The step-by-step data processing functionality provided by this package consists mainly of the following three functions.</p>
<ol type="1">
<li><code>step_by_step()</code> defines a step-by-step data processing workflow.</li>
<li><code>as_step()</code> converts a function into a step that can be used in the workflow.</li>
<li><code>end_step()</code> ends a step in the workflow.</li>
</ol>
<p>These functions provide a clear framework for data processing, making it easier to share knowledge and collaborate on similarly structured data analysis tasks.</p>
<section id="example" class="level2">
<h2 class="anchored" data-anchor-id="example">Example</h2>
<p>Let’s turn the following data process into a step-by-step data process. This process uses the <code>penguins</code> data to calculate the average weight of penguins by island, species and year.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>(penguins) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(species, island, body_mass, year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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>(body_mass)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb1-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert body_mass to kg</span></span>
<span id="cb1-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">body_mass =</span> units<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;">set_units</span>(body_mass, g) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-9">      units<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;">set_units</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kg"</span>)</span>
<span id="cb1-10">  ) <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;">summarise</span>(</span>
<span id="cb1-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_body_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass),</span>
<span id="cb1-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(island, species, year)</span>
<span id="cb1-14">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(island, species, year)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 15 × 4
   island    species    year mean_body_mass
   &lt;fct&gt;     &lt;fct&gt;     &lt;int&gt;           [kg]
 1 Biscoe    Adelie     2007           3.62
 2 Biscoe    Adelie     2008           3.63
 3 Biscoe    Adelie     2009           3.86
 4 Biscoe    Gentoo     2007           5.07
 5 Biscoe    Gentoo     2008           5.02
 6 Biscoe    Gentoo     2009           5.14
 7 Dream     Adelie     2007           3.67
 8 Dream     Adelie     2008           3.76
 9 Dream     Adelie     2009           3.65
10 Dream     Chinstrap  2007           3.69
11 Dream     Chinstrap  2008           3.8 
12 Dream     Chinstrap  2009           3.72
13 Torgersen Adelie     2007           3.76
14 Torgersen Adelie     2008           3.86
15 Torgersen Adelie     2009           3.49</code></pre>
</div>
</div>
<section id="defining-a-step-by-step-data-processing-workflow" class="level3">
<h3 class="anchored" data-anchor-id="defining-a-step-by-step-data-processing-workflow">Defining a step-by-step data processing workflow</h3>
<p>This process can be broken down into the following steps:</p>
<ol type="1">
<li>Select columns from the data frame with <code>select()</code>.</li>
<li>Filter rows from data frames with <code>filter()</code>.</li>
<li>Mutate columns in the data frame with <code>mutate()</code>.</li>
<li>Summarise the data frame with <code>summarise()</code>.</li>
<li>Order rows in the data frame with <code>arrange()</code>.</li>
</ol>
<p>Thus, we can define a step-by-step data processing workflow using the <code>adverbial</code> package as follows. Now we can use the <code>data_wrangler()</code> to process the data step by step. It shows the steps involved in processing data in the header, making it easier to understand and customise workflows.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(adverbial)</span>
<span id="cb3-2"></span>
<span id="cb3-3">data_wrangler <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;">step_by_step</span>(</span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb3-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">select_step =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Select columns from the data frame"</span>,</span>
<span id="cb3-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filter_step =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Filter rows from data frames"</span>,</span>
<span id="cb3-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mutate_step =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mutate columns in the data frame"</span>,</span>
<span id="cb3-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">summarise_step =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Summarise the data frame"</span>,</span>
<span id="cb3-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrange_step =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Order rows in the data frame"</span></span>
<span id="cb3-10">  )</span>
<span id="cb3-11">)</span>
<span id="cb3-12"></span>
<span id="cb3-13">select_step <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_step</span>(select, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"select_step"</span>)</span>
<span id="cb3-14">filter_step <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_step</span>(filter, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"filter_step"</span>)</span>
<span id="cb3-15">mutate_step <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_step</span>(mutate, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mutate_step"</span>)</span>
<span id="cb3-16">summarise_step <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_step</span>(summarise, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"summarise_step"</span>)</span>
<span id="cb3-17">arrange_step <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_step</span>(arrange, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"arrange_step"</span>)</span></code></pre></div>
</div>
<div class="cell">
<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;">data_wrangler</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>(penguins))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># Steps:
# ☒ 1. select_step:    Select columns from the data frame
# ☐ 2. filter_step:    Filter rows from data frames
# ☐ 3. mutate_step:    Mutate columns in the data frame
# ☐ 4. summarise_step: Summarise the data frame
# ☐ 5. arrange_step:   Order rows in the data frame
# ℹ Please call `select_step()` to continue.
#
# A tibble: 344 × 8
   species island    bill_len bill_dep flipper_len body_mass sex     year
 * &lt;fct&gt;   &lt;fct&gt;        &lt;dbl&gt;    &lt;dbl&gt;       &lt;int&gt;     &lt;int&gt; &lt;fct&gt;  &lt;int&gt;
 1 Adelie  Torgersen     39.1     18.7         181      3750 male    2007
 2 Adelie  Torgersen     39.5     17.4         186      3800 female  2007
 3 Adelie  Torgersen     40.3     18           195      3250 female  2007
 4 Adelie  Torgersen     NA       NA            NA        NA &lt;NA&gt;    2007
 5 Adelie  Torgersen     36.7     19.3         193      3450 female  2007
 6 Adelie  Torgersen     39.3     20.6         190      3650 male    2007
 7 Adelie  Torgersen     38.9     17.8         181      3625 female  2007
 8 Adelie  Torgersen     39.2     19.6         195      4675 male    2007
 9 Adelie  Torgersen     34.1     18.1         193      3475 &lt;NA&gt;    2007
10 Adelie  Torgersen     42       20.2         190      4250 &lt;NA&gt;    2007
# ℹ 334 more rows</code></pre>
</div>
</div>
</section>
<section id="processing-data-step-by-step" class="level3">
<h3 class="anchored" data-anchor-id="processing-data-step-by-step">Processing data step by step</h3>
<p>You can process the data step by step as follows (up to step 3). You can also use the <code>as_step()</code> function to convert any function into a step that can be used in the workflow. If you specify incorrect step names, an error will occur.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_wrangler</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>(penguins)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select_step</span>(species, island, body_mass, year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter_step</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>(body_mass)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate_step</span>(</span>
<span id="cb6-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert body_mass to kg</span></span>
<span id="cb6-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">body_mass =</span> units<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;">set_units</span>(body_mass, g) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-7">      units<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;">set_units</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kg"</span>)</span>
<span id="cb6-8">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># You can use another function during the step-by-step process.</span></span>
<span id="cb6-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_step</span>(head)(<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;">3</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># Steps:
# ☒ 1. select_step:    Select columns from the data frame
# ☒ 2. filter_step:    Filter rows from data frames
# ☒ 3. mutate_step:    Mutate columns in the data frame
# ☐ 4. summarise_step: Summarise the data frame
# ☐ 5. arrange_step:   Order rows in the data frame
# ℹ Please call `summarise_step()` to continue.
#
# A tibble: 3 × 4
  species island    body_mass  year
* &lt;fct&gt;   &lt;fct&gt;          [kg] &lt;int&gt;
1 Adelie  Torgersen      3.75  2007
2 Adelie  Torgersen      3.8   2007
3 Adelie  Torgersen      3.25  2007</code></pre>
</div>
</div>
</section>
<section id="completing-the-data-processing" class="level3">
<h3 class="anchored" data-anchor-id="completing-the-data-processing">Completing the data processing</h3>
<p>You can complete the data processing by adding the remaining steps and ending the workflow with <code>end_step()</code>. The following code performs almost the same processing as the original code, the only difference being that it successively informs the user which function to apply next.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_wrangler</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>(penguins)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select_step</span>(species, island, body_mass, year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter_step</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>(body_mass)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate_step</span>(</span>
<span id="cb8-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert body_mass to kg</span></span>
<span id="cb8-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">body_mass =</span> units<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;">set_units</span>(body_mass, g) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-7">      units<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;">set_units</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kg"</span>)</span>
<span id="cb8-8">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise_step</span>(</span>
<span id="cb8-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_body_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass),</span>
<span id="cb8-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(island, species, year)</span>
<span id="cb8-12">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange_step</span>(island, species, year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">end_step</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 15 × 4
   island    species    year mean_body_mass
 * &lt;fct&gt;     &lt;fct&gt;     &lt;int&gt;           [kg]
 1 Biscoe    Adelie     2007           3.62
 2 Biscoe    Adelie     2008           3.63
 3 Biscoe    Adelie     2009           3.86
 4 Biscoe    Gentoo     2007           5.07
 5 Biscoe    Gentoo     2008           5.02
 6 Biscoe    Gentoo     2009           5.14
 7 Dream     Adelie     2007           3.67
 8 Dream     Adelie     2008           3.76
 9 Dream     Adelie     2009           3.65
10 Dream     Chinstrap  2007           3.69
11 Dream     Chinstrap  2008           3.8 
12 Dream     Chinstrap  2009           3.72
13 Torgersen Adelie     2007           3.76
14 Torgersen Adelie     2008           3.86
15 Torgersen Adelie     2009           3.49</code></pre>
</div>
</div>
</section>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>adverbial package provides a way of breaking down data processing tasks into smaller, more manageable steps. This approach allows users to understand the overall workflow and adapt it to their needs. The aim is to eliminate the need for workers to remember each mentally demanding step of data processing.</p>


</section>

 ]]></description>
  <category>adverbial</category>
  <category>R</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2025/05/guiding-data-processing-with-adverbial-step_by_step-in-r.html</guid>
  <pubDate>Sat, 24 May 2025 15:00:00 GMT</pubDate>
</item>
<item>
  <title>Solving Utility Maximisation Problems with Julia JuMP (Using Constructors)</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2025/04/solving-utility-maximisation-problems-with-julia-jump-constructors.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2025/04/solving-utility-maximisation-problems-with-julia-jump-constructors.html">my Japanese article</a>.</p>
<p>In <a href="https://uchidamizuki.quarto.pub/blog/en/posts/2025/02/solving-utility-maximisation-problems-with-julia-jump.html">my previous article</a>, I introduced how to solve utility maximisation problems using JuMP, Julia’s mathematical optimisation library.</p>
<p>In that article, I introduced, as a simple example, how to solve a utility maximisation problem using the Cobb-Douglas utility function. However, economic models use a variety of utility functions besides the Cobb-Douglas type, such as the CES utility function. With the implementation approach used in the previous article, though, we would have needed to modify the optimisation problem’s implementation every time we changed the utility function.</p>
<p>So, in this article, I’ll introduce how to use Julia’s constructors to solve utility maximisation problems for different utility functions with the same code.</p>
<section id="implementing-the-cobb-douglas-utility-function" class="level2">
<h2 class="anchored" data-anchor-id="implementing-the-cobb-douglas-utility-function">Implementing the Cobb-Douglas Utility Function</h2>
<p>A utility function can be thought of as a function that takes a vector of quantities consumed as its argument and returns utility (a scalar value). On the other hand, when defining a utility function, we need to set parameters in advance, such as elasticity parameters, in addition to the quantity vector.</p>
<p>Since the parameters differ depending on the utility function, we define the utility function as the abstract type <code>AbstractEconomicUtility</code>, and define the Cobb-Douglas utility function <code>CobbDouglasUtility</code> as a subtype of it. We define <code>CobbDouglasUtility</code> as a type that holds the elasticity parameter <code>weights</code>.</p>
<div id="2" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">abstract type</span> AbstractEconomicUtility <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> CobbDouglasUtility <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;"> AbstractEconomicUtility</span></span>
<span id="cb1-4">    weights<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vector{Float64}</span></span>
<span id="cb1-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div>
</div>
<p>Next, by defining function-like objects, we make it possible to compute utility by calling <code>f(quantities)</code> for a utility function type <code>f</code>. Note that since the utility function’s parameters can be obtained from the type <code>f</code>, the function’s only argument is <code>quantities</code>. This allows us to call the same code, <code>f(quantities)</code>, for various utility function types <code>f</code>.</p>
<p>With this, we can define the Cobb-Douglas utility function.</p>
<div id="4" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CobbDouglasUtility</span>)(quantities)</span>
<span id="cb2-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prod</span>(quantities <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.^</span> f.weights)</span>
<span id="cb2-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div>
</div>
<div id="6" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the utility function</span></span>
<span id="cb3-2">cobb_douglas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">CobbDouglasUtility</span>([<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>])</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute utility</span></span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cobb_douglas</span>([<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>., <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>., <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>.])</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>3.0963389922845703</code></pre>
</div>
</div>
<p>Next, as in <a href="https://uchidamizuki.quarto.pub/blog/en/posts/2025/02/solving-utility-maximisation-problems-with-julia-jump.html">the previous article</a>, we define the Marshallian demand function, <code>demand_marshallian()</code>.</p>
<p>With <code>demand_marshallian()</code>, we can find the analytical solution to the utility maximisation problem for the Cobb-Douglas utility function.</p>
<div id="8" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">demand_marshallian</span>(</span>
<span id="cb5-2">  f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CobbDouglasUtility</span>;</span>
<span id="cb5-3">  prices,</span>
<span id="cb5-4">  income</span>
<span id="cb5-5">)</span>
<span id="cb5-6">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> income <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> f.weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(f.weights) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">./</span> prices</span>
<span id="cb5-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>demand_marshallian (generic function with 1 method)</code></pre>
</div>
</div>
<div id="10" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb7-1">quantities_analytical_cobb_douglas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">demand_marshallian</span>(</span>
<span id="cb7-2">  cobb_douglas;</span>
<span id="cb7-3">  prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>., <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>., <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>.],</span>
<span id="cb7-4">  income <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>.</span>
<span id="cb7-5">)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>3-element Vector{Float64}:
 30.0
 20.0
 10.0</code></pre>
</div>
</div>
</section>
<section id="numerical-solution-to-the-utility-maximisation-problem" class="level2">
<h2 class="anchored" data-anchor-id="numerical-solution-to-the-utility-maximisation-problem">Numerical Solution to the Utility Maximisation Problem</h2>
<p>By slightly modifying the code from <a href="https://uchidamizuki.quarto.pub/blog/en/posts/2025/02/solving-utility-maximisation-problems-with-julia-jump.html">the previous article</a>, we can solve the utility maximisation problem numerically.</p>
<p>In the previous article, we defined code specific to the Cobb-Douglas utility function. However, the function <code>demand_marshallian_numerical</code> below takes the abstract type <code>AbstractEconomicUtility</code> as its argument, allowing the same code to be used for a variety of utility functions.</p>
<div id="12" class="cell" data-execution_count="0">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Pkg</span></span>
<span id="cb9-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Pkg</span>.<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"JuMP"</span>)</span>
<span id="cb9-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Pkg</span>.<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ipopt"</span>)</span></code></pre></div>
</div>
<div id="14" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb10-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">using</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">JuMP</span></span>
<span id="cb10-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">using</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Ipopt</span></span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">demand_marshallian_numerical</span>(</span>
<span id="cb10-5">  f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">AbstractEconomicUtility</span>;</span>
<span id="cb10-6">  prices<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vector{Float64}</span>,</span>
<span id="cb10-7">  income<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Float64</span></span>
<span id="cb10-8">)</span>
<span id="cb10-9">    n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(prices)</span>
<span id="cb10-10">    model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Model</span>(Ipopt.Optimizer)</span>
<span id="cb10-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_silent</span>(model)</span>
<span id="cb10-12">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@variable</span>(model, quantities[<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb10-13">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@objective</span>(model, Max, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(quantities))</span>
<span id="cb10-14">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@constraint</span>(model, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.*</span> quantities) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> income)</span>
<span id="cb10-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">optimize!</span>(model)</span>
<span id="cb10-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">value</span>.(quantities)</span>
<span id="cb10-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>demand_marshallian_numerical (generic function with 1 method)</code></pre>
</div>
</div>
<p>By applying <code>demand_marshallian_numerical()</code> to the <code>cobb_douglas</code> defined above, we can confirm, as in the previous article, that the analytical and numerical solutions to the Cobb-Douglas utility maximisation problem approximately agree.</p>
<div id="16" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb12-1">quantities_numerical_cobb_douglas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">demand_marshallian_numerical</span>(</span>
<span id="cb12-2">  cobb_douglas;</span>
<span id="cb12-3">  prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>., <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>., <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>.],</span>
<span id="cb12-4">  income <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>.</span>
<span id="cb12-5">)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
 Ipopt is released as open source code under the Eclipse Public License (EPL).
         For more information visit https://github.com/coin-or/Ipopt
******************************************************************************
</code></pre>
</div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>3-element Vector{Float64}:
 30.000000297265974
 20.00000019590169
 10.000000099089569</code></pre>
</div>
</div>
<div id="18" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb15-1">quantities_analytical_cobb_douglas</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>3-element Vector{Float64}:
 30.0
 20.0
 10.0</code></pre>
</div>
</div>
</section>
<section id="implementing-the-ces-utility-function" class="level2">
<h2 class="anchored" data-anchor-id="implementing-the-ces-utility-function">Implementing the CES Utility Function</h2>
<p>To get a feel for the benefits of using constructors, let’s implement a utility function other than the Cobb-Douglas type.</p>
<p>Below, let’s implement the CES utility function. The CES utility function is a generalisation of the Cobb-Douglas utility function and has a parameter <code>substitution</code> that represents the degree of substitutability. As with the Cobb-Douglas utility function, the CES utility function can be defined as follows.</p>
<p>I’ll skip the derivation of the Marshallian demand function, but by specifying the argument type <code>f::CESUtility</code>, we can change the behaviour of <code>demand_marshallian()</code> depending on the utility function. This mechanism is called multiple dispatch.</p>
<div id="20" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb17-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> CESUtility <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;"> AbstractEconomicUtility</span></span>
<span id="cb17-2">    substitution<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Float64</span></span>
<span id="cb17-3">    weights<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vector{Float64}</span></span>
<span id="cb17-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb17-5"></span>
<span id="cb17-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CESUtility</span>)(quantities)</span>
<span id="cb17-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(f.weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.*</span> quantities <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.^</span> f.substitution) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> f.substitution)</span>
<span id="cb17-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb17-9"></span>
<span id="cb17-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">demand_marshallian</span>(</span>
<span id="cb17-11">  f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CESUtility</span>;</span>
<span id="cb17-12">  prices,</span>
<span id="cb17-13">  income</span>
<span id="cb17-14">)</span>
<span id="cb17-15">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> f.weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.^</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> f.substitution)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.*</span> prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.^</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (f.substitution <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb17-16">    income <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(f.weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.^</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> f.substitution)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.*</span> prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.^</span> (f.substitution <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (f.substitution <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)))</span>
<span id="cb17-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>demand_marshallian (generic function with 2 methods)</code></pre>
</div>
</div>
<div id="22" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb19-1">ces <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">CESUtility</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>])</span>
<span id="cb19-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ces</span>([<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>., <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>., <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>.])</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>3.196603520188051</code></pre>
</div>
</div>
<p>By applying <code>demand_marshallian()</code> and <code>demand_marshallian_numerical()</code> to the <code>ces</code> defined above, we can find the analytical and numerical solutions to the CES utility maximisation problem.</p>
<div id="24" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb21-1">quantities_analytical_ces <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">demand_marshallian</span>(</span>
<span id="cb21-2">  ces;</span>
<span id="cb21-3">  prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>., <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>., <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>.],</span>
<span id="cb21-4">  income <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>.</span>
<span id="cb21-5">)</span>
<span id="cb21-6"></span>
<span id="cb21-7">quantities_numerical_ces <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">demand_marshallian_numerical</span>(</span>
<span id="cb21-8">  ces;</span>
<span id="cb21-9">  prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>., <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>., <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>.],</span>
<span id="cb21-10">  income <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>.</span>
<span id="cb21-11">)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>3-element Vector{Float64}:
 45.00000043558223
 20.000000194987294
  5.000000053970371</code></pre>
</div>
</div>
<div id="26" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb23-1">quantities_analytical_ces</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>3-element Vector{Float64}:
 45.0
 20.000000000000004
  4.999999999999999</code></pre>
</div>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>In this article, I showed that by extending the utility maximisation problem introduced in <a href="https://uchidamizuki.quarto.pub/blog/en/posts/2025/02/solving-utility-maximisation-problems-with-julia-jump.html">my previous article</a> using Julia’s constructors, we can use the same code for a variety of utility functions.</p>
<p>By taking advantage of this mechanism, whenever we implement a new utility function, we can automatically check the consistency between the analytical and numerical solutions to the optimisation problem, which is expected to make it easier to implement tests for utility functions<sup>1</sup>.</p>


</section>


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

<ol>
<li id="fn1"><p>However, when I separately looked at the Leontief utility function, I was not able to successfully obtain a numerical solution. For special utility functions, it may be difficult to find a numerical solution, so it may be necessary to implement additional tests separately.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>JuMP</category>
  <category>Julia</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2025/04/solving-utility-maximisation-problems-with-julia-jump-constructors.html</guid>
  <pubDate>Tue, 29 Apr 2025 15:00:00 GMT</pubDate>
</item>
<item>
  <title>Removing observation noise from proportion data</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2025/04/removal-of-observation-noise-from-proportion.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2025/04/removal-of-observation-noise-from-proportion.html">my Japanese article</a>.</p>
<p>This article explains how to deal with the small number problem that often arises when working with proportion data (values obtained by division), such as the following.</p>
<ul>
<li>Infection rates of a disease by region</li>
<li>Number of visitors per unit time by store</li>
<li>Acceptance rate for prestigious schools, by school</li>
</ul>
<p>The small number problem refers to the phenomenon where the variability of proportion data becomes large when the observed values are small. Below, we consider how to deal with the small number problem for the Poisson distribution, which count data often follow (a similar argument can be developed for the binomial distribution). When the denominator, such as a population, is <img src="https://latex.codecogs.com/png.latex?N"> and the true value of the proportion is <img src="https://latex.codecogs.com/png.latex?%5Ctheta">, the numerator count data (e.g., the number of infected people) <img src="https://latex.codecogs.com/png.latex?n"> is considered to follow the Poisson distribution below.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0An%20%5Csim%20%5Ctext%7BPoisson%7D(N%20%5Ctheta)%0A"></p>
<p>Furthermore, the variance of this Poisson distribution is expressed as follows.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D%5Bn%5D%20=%20N%20%5Ctheta%0A"></p>
<p>As a naive method for estimating the (true) proportion, we can consider dividing <img src="https://latex.codecogs.com/png.latex?n"> by <img src="https://latex.codecogs.com/png.latex?N">. However, as shown below, the variance of the proportion obtained by this method has the property that it becomes large when <img src="https://latex.codecogs.com/png.latex?N"> is small. This property is common to distributions such as the Poisson and binomial distributions, in which the variance (observation noise) is proportional to the expected value.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D%5Cleft%5B%5Cfrac%7Bn%7D%7BN%7D%5Cright%5D%20=%20%5Cfrac%7B%5Ctheta%7D%7BN%7D%0A"></p>
<p>This property of observation noise can become a problem when, for example, considering a ranking of schools by acceptance rate for prestigious schools. Let’s compare School A and School B below, whose total numbers of examinees differ greatly.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 9%">
<col style="width: 31%">
<col style="width: 38%">
<col style="width: 20%">
</colgroup>
<thead>
<tr class="header">
<th>School</th>
<th>Total number of examinees</th>
<th>Number of successful examinees</th>
<th>Acceptance rate</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>School A</td>
<td>100</td>
<td>30</td>
<td>30 %</td>
</tr>
<tr class="even">
<td>School B</td>
<td>5</td>
<td>2</td>
<td>40 %</td>
</tr>
</tbody>
</table>
<p>In this case, for School B, even a small change in the number of successful examinees causes a large change in the acceptance rate, so its acceptance rate can be considered less precise than that of School A. In a situation like this, if we build a ranking of acceptance rates, schools with a small total number of examinees that happened to have many successful examinees — even though their true acceptance rate is not necessarily high — will tend to be ranked highly. This phenomenon can be problematic when, for example, predicting future acceptance rates.</p>
<p>Below, we describe a method for smoothing proportion data using only the observed values from data on multiple regions, stores, schools, and so on.</p>
<section id="smoothing-using-bayes-theorem" class="level2">
<h2 class="anchored" data-anchor-id="smoothing-using-bayes-theorem">Smoothing using Bayes’ theorem</h2>
<p>Assume that the count data follows a Poisson distribution, and that the true value of the proportion follows a Gamma distribution, which is the conjugate prior of the Poisson distribution.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0A%20%20%5Ctheta%20&amp;%5Csim%20%5Ctext%7BGamma%7D(%5Calpha,%20%5Cbeta)%20%5C%5C%0A%20%20n%20%5Cmid%20%5Ctheta%20&amp;%5Csim%20%5Ctext%7BPoisson%7D(N%20%5Ctheta)%0A%5Cend%7Balign%7D%0A"></p>
<p>Then, using Bayes’ theorem, the posterior distribution of <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> — that is, the distribution of <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> given the observed value <img src="https://latex.codecogs.com/png.latex?n"> — is as follows.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0A%20%20P(%5Ctheta%20%5Cmid%20n)%20&amp;%5Cpropto%20P(n%20%5Cmid%20%5Ctheta)%20P(%5Ctheta)%20%5C%5C%0A%20%20&amp;%5Cpropto%20(N%20%5Ctheta)%5En%20%5Cexp(-N%20%5Ctheta)%20%5Ccdot%20%5Ctheta%5E%7B%5Calpha%20-%201%7D%20%5Cexp(-%5Cbeta%5Ctheta)%20%5C%5C%0A%20%20&amp;%5Cpropto%20%5Ctheta%5E%7Bn%20+%20%5Calpha%20-%201%7D%20%5Cexp%5B-(N%20+%20%5Cbeta)%5Ctheta%5D%20%5C%5C%0A%20%20%5Ctheta%20%5Cmid%20n%20&amp;%5Csim%20%5Ctext%7BGamma%7D(n%20+%20%5Calpha,%20N%20+%20%5Cbeta)%0A%5Cend%7Balign%7D%0A"></p>
<p>Therefore, the expected value of the posterior distribution of <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> can be expressed by the following simple formula, and by using this formula we can remove the observation noise (i.e., smooth the data).</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BE%7D%5B%5Ctheta%20%5Cmid%20n%5D%20=%20%5Cfrac%7Bn%20+%20%5Calpha%7D%7BN%20+%20%5Cbeta%7D%0A"></p>
<p>On the other hand, to perform this kind of smoothing, we need to determine the values of <img src="https://latex.codecogs.com/png.latex?%5Calpha"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. Below, we describe a method for estimating the prior distribution parameters <img src="https://latex.codecogs.com/png.latex?%5Calpha"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> from the observed values.</p>
</section>
<section id="estimating-prior-distribution-parameters-with-empirical-bayes" class="level2">
<h2 class="anchored" data-anchor-id="estimating-prior-distribution-parameters-with-empirical-bayes">Estimating prior distribution parameters with empirical Bayes</h2>
<p>Empirical Bayes is a method for estimating the parameters of the prior distribution (empirically) from the observed values. Possible approaches for estimating the prior distribution parameters with empirical Bayes include the method of moments and maximum likelihood estimation; below, we describe the approach using maximum likelihood estimation.</p>
<p>Before performing maximum likelihood estimation, let’s first check the distribution that <img src="https://latex.codecogs.com/png.latex?n"> follows. <img src="https://latex.codecogs.com/png.latex?n"> is considered to follow a mixture of the Gamma and Poisson distributions, as shown below. Normally, computing a mixture distribution requires integral calculus, but it is known that the mixture of the Gamma and Poisson distributions follows a negative binomial distribution. Therefore, we derive the probability distribution of <img src="https://latex.codecogs.com/png.latex?n"> in advance, as follows.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0A%20%20P(n)%20&amp;=%20%5Cint_%7B0%7D%5E%7B%5Cinfty%7D%20P(n%20%5Cmid%20%5Ctheta)%20P(%5Ctheta)%20d%5Ctheta%20%5C%5C%0A%20%20&amp;=%20%5Cint_%7B0%7D%5E%7B%5Cinfty%7D%20%5Cfrac%7B1%7D%7Bn!%7D%20(N%20%5Ctheta)%5En%20%5Cexp(-N%20%5Ctheta)%20%5Ccdot%20%5Cfrac%7B%5Cbeta%5E%7B%5Calpha%7D%7D%7B%5CGamma(%5Calpha)%7D%20%5Ctheta%5E%7B%5Calpha%20-%201%7D%20%5Cexp(-%5Cbeta%5Ctheta)%20d%5Ctheta%20%5C%5C%0A%20%20&amp;=%20%5Cfrac%7BN%5En%20%5Cbeta%5E%7B%5Calpha%7D%7D%7Bn!%20%5CGamma(%5Calpha)%7D%20%5Cint_%7B0%7D%5E%7B%5Cinfty%7D%20%5Ctheta%5E%7Bn%20+%20%5Calpha%20-%201%7D%20%5Cexp%5B-(N%20+%20%5Cbeta)%5Ctheta%5D%20d%5Ctheta%20%5C%5C%0A%20%20&amp;=%20%5Cfrac%7BN%5En%20%5Cbeta%5E%7B%5Calpha%7D%7D%7Bn!%20%5CGamma(%5Calpha)%7D%20%5Ccdot%20%5Cfrac%7B%5CGamma(n%20+%20%5Calpha)%7D%7B(N%20+%20%5Cbeta)%5E%7Bn%20+%20%5Calpha%7D%7D%20%5Cquad%20%5Ctext%7B(the%20integral%20of%20the%20Gamma%20distribution%20kernel%20equals%20the%20reciprocal%20of%20the%20normalizing%20constant)%7D%20%5C%5C%0A%20%20&amp;=%20%5Cfrac%7B%5CGamma(n%20+%20%5Calpha)%7D%7B%5CGamma(n%20+%201)%20%5CGamma(%5Calpha)%7D%20%5Cleft(%20%5Cfrac%7BN%7D%7BN%20+%20%5Cbeta%7D%20%5Cright)%5E%7Bn%7D%20%5Cleft(%20%5Cfrac%7B%5Cbeta%7D%7BN%20+%20%5Cbeta%7D%20%5Cright)%5E%5Calpha%20%5C%5C%0A%20%20&amp;=%20%5Cbinom%7Bn%20+%20%5Calpha%20-%201%7D%7Bn%7D%20%5Cleft(%20%5Cfrac%7B%5Cfrac%7B%5Calpha%7D%7B%5Cbeta%7D%20N%7D%7B%5Cfrac%7B%5Calpha%7D%7B%5Cbeta%7D%20N%20+%20%5Calpha%7D%20%5Cright)%5E%7Bn%7D%20%5Cleft(%20%5Cfrac%7B%5Calpha%7D%7B%5Cfrac%7B%5Calpha%7D%7B%5Cbeta%7D%20N%20+%20%5Calpha%7D%20%5Cright)%5E%7B%5Calpha%7D%20%5C%5C%0A%5Cend%7Balign%7D%0A"></p>
<p>From the formula above, we can see that <img src="https://latex.codecogs.com/png.latex?n"> follows a negative binomial distribution as shown below. Note, however, that there are several ways to parameterize the negative binomial distribution, so some care is needed. The parameterization used here is the same as <a href="https://mc-stan.org/docs/functions-reference/unbounded_discrete_distributions.html#nbalt">Stan’s NegBinomial2</a>.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0An%20%5Csim%20%5Ctext%7BNegBinomial2%7D(%5Cmu,%20%5Calpha),%20%5Cquad%20%5Cmu%20=%20%5Cfrac%7B%5Calpha%7D%7B%5Cbeta%7D%20N%0A"></p>
<p>In this way, we find that <img src="https://latex.codecogs.com/png.latex?n"> follows a common probability distribution called the negative binomial distribution, which means we can perform maximum likelihood estimation using existing functions provided in R. Below, using experimentally created test data, we perform maximum likelihood estimation with the <code>glm.nb()</code> function from R’s MASS package, and use it to smooth the proportion data.</p>
</section>
<section id="a-worked-example-of-smoothing" class="level2">
<h2 class="anchored" data-anchor-id="a-worked-example-of-smoothing">A worked example of smoothing</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><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></code></pre></div>
</div>
<section id="creating-test-data" class="level3">
<h3 class="anchored" data-anchor-id="creating-test-data">Creating test data</h3>
<p>First, we set the sample size to 10,000 and randomly generate the true values of the proportion from a Gamma distribution with <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%205,%20%5Cbeta%20=%2050">.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1234</span>)</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sample size</span></span>
<span id="cb2-4">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e4</span></span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Proportion (true value)</span></span>
<span id="cb2-7">alpha_true <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;">5</span></span>
<span id="cb2-8">beta_true <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb2-9">rate_true <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;">rgamma</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> alpha_true, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate =</span> beta_true)</span></code></pre></div>
</div>
<p>The true value of the proportion, <code>rate_true</code>, has the following distribution.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate_true =</span> rate_true) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(rate_true)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-2</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2025/04/removal-of-observation-noise-from-proportion_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Next, we randomly select the population (denominator), <code>population</code>, from integers between 1 and 100. Then, based on the true value of the proportion, <code>rate_true</code>, we generate the count data (numerator), <code>count</code>, from a Poisson distribution, and combine these results into the data frame <code>data</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Population (denominator)</span></span>
<span id="cb4-2">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb4-3">population <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;">sample</span>(x, n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Count data (numerator)</span></span>
<span id="cb4-6">count <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;">rpois</span>(n, rate_true <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> population)</span>
<span id="cb4-7"></span>
<span id="cb4-8">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb4-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">population =</span> population,</span>
<span id="cb4-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate_true =</span> rate_true,</span>
<span id="cb4-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> count</span>
<span id="cb4-12">)</span>
<span id="cb4-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["population"],"name":[1],"type":["int"],"align":["right"]},{"label":["rate_true"],"name":[2],"type":["dbl"],"align":["right"]},{"label":["count"],"name":[3],"type":["int"],"align":["right"]}],"data":[{"1":"50","2":"0.04607358","3":"5"},{"1":"1","2":"0.10383167","3":"0"},{"1":"22","2":"0.10588879","3":"5"},{"1":"7","2":"0.06167660","3":"1"},{"1":"94","2":"0.11275060","3":"11"},{"1":"29","2":"0.06726748","3":"2"},{"1":"62","2":"0.13655770","3":"9"},{"1":"63","2":"0.06552866","3":"2"},{"1":"76","2":"0.07089299","3":"7"},{"1":"45","2":"0.03104801","3":"0"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</section>
<section id="checking-for-the-small-number-problem" class="level3">
<h3 class="anchored" data-anchor-id="checking-for-the-small-number-problem">Checking for the small number problem</h3>
<p>Now, let’s use the test data to check whether the small number problem is occurring. First, we compute the naive proportion estimate, <code>rate_crude</code>, using division, and draw a scatter plot with <code>population</code> on the x-axis and <code>rate_crude</code> on the y-axis. As shown below, we can see a tendency for <code>rate_crude</code> to become large when <code>population</code> is small. If a similar tendency is seen in your own data, the small number problem may be occurring there as well.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">data_crude <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>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb5-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate_crude =</span> count <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> population</span>
<span id="cb5-4">  )</span>
<span id="cb5-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data_crude, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["population"],"name":[1],"type":["int"],"align":["right"]},{"label":["rate_true"],"name":[2],"type":["dbl"],"align":["right"]},{"label":["count"],"name":[3],"type":["int"],"align":["right"]},{"label":["rate_crude"],"name":[4],"type":["dbl"],"align":["right"]}],"data":[{"1":"50","2":"0.04607358","3":"5","4":"0.10000000"},{"1":"1","2":"0.10383167","3":"0","4":"0.00000000"},{"1":"22","2":"0.10588879","3":"5","4":"0.22727273"},{"1":"7","2":"0.06167660","3":"1","4":"0.14285714"},{"1":"94","2":"0.11275060","3":"11","4":"0.11702128"},{"1":"29","2":"0.06726748","3":"2","4":"0.06896552"},{"1":"62","2":"0.13655770","3":"9","4":"0.14516129"},{"1":"63","2":"0.06552866","3":"2","4":"0.03174603"},{"1":"76","2":"0.07089299","3":"7","4":"0.09210526"},{"1":"45","2":"0.03104801","3":"0","4":"0.00000000"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">data_crude <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(population, rate_crude)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2025/04/removal-of-observation-noise-from-proportion_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>With ordinary data, the true value of the proportion, <code>rate_true</code>, is unknown, but here we can also check the relationship between <code>rate_true</code> and <code>rate_crude</code>. As shown below, drawing a scatter plot with <code>rate_true</code> on the x-axis and <code>rate_crude</code> on the y-axis shows that <code>rate_crude</code> tends to deviate greatly from the 45-degree line when <code>population</code> is small. From these results, we have confirmed that the small number problem is occurring in the test data.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">data_crude <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(rate_true, rate_crude, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> population)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_abline</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="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"turbo"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-6">  tune<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;">coord_obs_pred</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2025/04/removal-of-observation-noise-from-proportion_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="smoothing-using-the-negative-binomial-distribution" class="level2">
<h2 class="anchored" data-anchor-id="smoothing-using-the-negative-binomial-distribution">Smoothing using the negative binomial distribution</h2>
<p>Now, let’s perform smoothing using the negative binomial distribution. Here, we perform maximum likelihood estimation using the <code>glm.nb()</code> function from R’s MASS package, which is readily available. <code>glm.nb()</code> lets us estimate a generalized linear model in which the response variable follows a negative binomial distribution.</p>
<p>The parameters of the negative binomial distribution can be estimated with simple code like the following. Here, we specify <code>log(population)</code> as an offset term so that the expected value is proportional to <code>population</code>.</p>
<div class="cell">
<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">model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> MASS<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;">glm.nb</span>(</span>
<span id="cb8-2">  count <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;">offset</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(population)),</span>
<span id="cb8-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data</span>
<span id="cb8-4">)</span></code></pre></div>
</div>
<p>Next, we obtain the parameters of the Gamma distribution from <code>model</code>, which stores the output of <code>glm.nb()</code>. The Gamma distribution’s <img src="https://latex.codecogs.com/png.latex?%5Calpha"> can be obtained from <code>model$theta</code>. We can see that the estimated value of <img src="https://latex.codecogs.com/png.latex?%5Calpha">, <code>alpha_estimated</code>, roughly matches the true value, <code>alpha_true</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">alpha_estimated <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> model<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>theta</span>
<span id="cb9-2"></span>
<span id="cb9-3">alpha_true</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5</code></pre>
</div>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">alpha_estimated</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5.014545</code></pre>
</div>
</div>
<p>Also, the Gamma distribution’s <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> can be obtained from the relationship for <img src="https://latex.codecogs.com/png.latex?%5Cmu"> shown above, as follows. Although <img src="https://latex.codecogs.com/png.latex?%5Cmu"> is output once for each sample, we can see that the estimated values of <img src="https://latex.codecogs.com/png.latex?%5Cbeta">, <code>beta_estimated</code>, are all nearly identical, and that they roughly match the true value, <code>beta_true</code>.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta%20=%20%5Cfrac%7Ba%20N%7D%7B%5Cmu%7D%0A"></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">mu <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;">predict</span>(model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>)</span>
<span id="cb13-2">beta_estimated <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> alpha_estimated <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> population <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> mu</span>
<span id="cb13-3"></span>
<span id="cb13-4">beta_true</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 50</code></pre>
</div>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">near</span>(beta_estimated, beta_estimated[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(beta_estimated)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>       1        2        3        4        5        6 
50.07359 50.07359 50.07359 50.07359 50.07359 50.07359 </code></pre>
</div>
</div>
<p>Finally, let’s use the estimated Gamma distribution parameters <img src="https://latex.codecogs.com/png.latex?%5Calpha"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> to smooth the proportion. We reproduce the smoothing formula below for reference. We compute the smoothed proportion, <code>rate_smoothed</code>, following this formula, and draw the same kind of plots as before. As a result, we can see that there is no tendency for the proportion to be over- or under-estimated even when <code>population</code> is small, confirming that the small number problem has been resolved.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7Bn%20+%20%5Calpha%7D%7BN%20+%20%5Cbeta%7D%0A"></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">data_smoothed <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>
<span id="cb19-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb19-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate_smoothed =</span> (count <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> alpha_estimated) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (population <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> beta_estimated)</span>
<span id="cb19-4">  )</span>
<span id="cb19-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data_smoothed, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["population"],"name":[1],"type":["int"],"align":["right"]},{"label":["rate_true"],"name":[2],"type":["dbl"],"align":["right"]},{"label":["count"],"name":[3],"type":["int"],"align":["right"]},{"label":["rate_smoothed"],"name":[4],"type":["dbl"],"align":["right"]}],"data":[{"1":"50","2":"0.04607358","3":"5","4":"0.10007180"},{"1":"1","2":"0.10383167","3":"0","4":"0.09818273"},{"1":"22","2":"0.10588879","3":"5","4":"0.13894887"},{"1":"7","2":"0.06167660","3":"1","4":"0.10538227"},{"1":"94","2":"0.11275060","3":"11","4":"0.11115531"},{"1":"29","2":"0.06726748","3":"2","4":"0.08870907"},{"1":"62","2":"0.13655770","3":"9","4":"0.12504770"},{"1":"63","2":"0.06552866","3":"2","4":"0.06203521"},{"1":"76","2":"0.07089299","3":"7","4":"0.09529787"},{"1":"45","2":"0.03104801","3":"0","4":"0.05274382"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">data_smoothed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb20-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(population, rate_smoothed)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2025/04/removal-of-observation-noise-from-proportion_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">data_smoothed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb21-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(rate_true, rate_smoothed, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> population)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_abline</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="cb21-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"turbo"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-6">  tune<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;">coord_obs_pred</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2025/04/removal-of-observation-noise-from-proportion_files/figure-html/unnamed-chunk-10-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>In this article, we explained a smoothing method using empirical Bayes as an approach for dealing with the small number problem in proportion data. The phenomenon where the variability of proportion data becomes large when the denominator, such as the population, is small is likely to be a problem in practice as well. In such cases, one option is to exclude data with a small denominator based on some threshold, but the method described in this article has the advantage of allowing us to make use of all the data.</p>
<p>The <code>glm.nb()</code> function from the MASS package used in this article is a function for generalized linear models, so it is possible to include terms other than the denominator term, <code>offset(log(population))</code>, as explanatory variables. This makes it possible to reproduce situations in which <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> varies depending on factors such as regional characteristics.</p>
<p>Also, although we focused on the Poisson distribution, which we consider to be highly versatile, the same framework can also be applied to the binomial distribution. In the case of the binomial distribution, if we use a Beta distribution as the prior, the smoothing formula becomes as follows. In addition, since the mixture distribution in this case is the Beta-binomial distribution, parameter estimation should be relatively straightforward using existing R packages (e.g., bbmle).</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7Bn%20+%20%5Calpha%7D%7BN%20+%20%5Calpha%20+%20%5Cbeta%7D%0A"></p>


</section>

 ]]></description>
  <category>Statistics</category>
  <category>R</category>
  <category>Bayes</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2025/04/removal-of-observation-noise-from-proportion.html</guid>
  <pubDate>Sat, 12 Apr 2025 15:00:00 GMT</pubDate>
</item>
<item>
  <title>Solving Utility Maximisation Problems with Julia’s JuMP (A Simple Example)</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2025/02/solving-utility-maximisation-problems-with-julia-jump.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2025/02/solving-utility-maximisation-problems-with-julia-jump.html">my Japanese article</a>.</p>
<p>In this article, I use Julia’s mathematical optimization library JuMP to solve a utility maximization problem based on the Cobb-Douglas utility function, which is commonly used in economics.</p>
<p>The utility maximization of a Cobb-Douglas utility function can be solved analytically, so it isn’t strictly necessary to use Julia JuMP to solve it. Here, however, in order to learn how to use Julia JuMP, I deliberately compare the analytical solution with the numerical one.</p>
<section id="cobb-douglas-utility-function" class="level2">
<h2 class="anchored" data-anchor-id="cobb-douglas-utility-function">Cobb-Douglas utility function</h2>
<p>The Cobb-Douglas utility function is defined as follows. Here, <img src="https://latex.codecogs.com/png.latex?x_i"> is the consumption quantity of production factor <img src="https://latex.codecogs.com/png.latex?i">, and <img src="https://latex.codecogs.com/png.latex?%5Calpha_i"> is the elasticity parameter of production factor <img src="https://latex.codecogs.com/png.latex?i">.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AU%20=%20%5Cprod_%7Bi=1%7D%5E%7Bn%7D%20x_i%5E%7B%5Calpha_i%7D%0A"></p>
<p>Implementing this utility function in Julia looks like this.</p>
<div id="2" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cobb_douglas</span>(quantities; weights)</span>
<span id="cb1-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prod</span>(quantities <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.^</span> weights)</span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>cobb_douglas (generic function with 1 method)</code></pre>
</div>
</div>
<p>By passing the consumption quantities <code>quantities</code> and the elasticity parameters <code>weights</code> to the implemented <code>cobb_douglas</code> function, we can compute the utility as follows.</p>
<div id="4" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cobb_douglas</span>(</span>
<span id="cb3-2">  [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>],</span>
<span id="cb3-3">  weights<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>]</span>
<span id="cb3-4">)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>3.0963389922845703</code></pre>
</div>
</div>
</section>
<section id="analytical-solution-to-the-utility-maximization-problem" class="level2">
<h2 class="anchored" data-anchor-id="analytical-solution-to-the-utility-maximization-problem">Analytical solution to the utility maximization problem</h2>
<p>In general, a utility maximization problem is formulated as the problem of finding the consumption quantities that maximize utility given prices and income.</p>
<p>These consumption quantities (demand quantities) are known as the Marshallian demand functions. For the Cobb-Douglas utility function, the Marshallian demand functions are derived as follows<sup>1</sup>. Here, <img src="https://latex.codecogs.com/png.latex?p_i"> is the price of production factor <img src="https://latex.codecogs.com/png.latex?i">, and <img src="https://latex.codecogs.com/png.latex?Y"> is income.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ax_i%20=%20%5Cfrac%7B1%7D%7Bp_i%7D%5Cfrac%7Bw_i%7D%7B%5Csum_%7Bj=1%7D%5E%7Bn%7D%20w_j%7DY%0A"></p>
<div id="6" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">demand_marshallian_cobb_douglas_analytical</span>(prices, income; weights)</span>
<span id="cb5-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> income <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.*</span> weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">./</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(weights) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">./</span> prices</span>
<span id="cb5-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>demand_marshallian_cobb_douglas_analytical (generic function with 1 method)</code></pre>
</div>
</div>
<p>By passing the prices <code>prices</code>, the income <code>income</code>, and the elasticity parameters <code>weights</code> to the implemented <code>demand_marshallian_cobb_douglas_analytical</code> function, we can compute the demand quantities as follows.</p>
<div id="8" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb7-1">quantities_analytical <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">demand_marshallian_cobb_douglas_analytical</span>(</span>
<span id="cb7-2">  [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>],</span>
<span id="cb7-3">  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,</span>
<span id="cb7-4">  weights<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>]</span>
<span id="cb7-5">)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>3-element Vector{Float64}:
 30.0
 20.0
 10.0</code></pre>
</div>
</div>
</section>
<section id="numerical-solution-to-the-utility-maximization-problem" class="level2">
<h2 class="anchored" data-anchor-id="numerical-solution-to-the-utility-maximization-problem">Numerical solution to the utility maximization problem</h2>
<p>Next, let’s use Julia JuMP to solve the utility maximization problem numerically. Before we start, let’s install the JuMP and Ipopt packages. Ipopt provides a solver for nonlinear optimization problems.</p>
<div id="10" class="cell" data-execution_count="0">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Pkg</span></span>
<span id="cb9-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Pkg</span>.<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"JuMP"</span>)</span>
<span id="cb9-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Pkg</span>.<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ipopt"</span>)</span></code></pre></div>
</div>
<p>The utility maximization problem with the Cobb-Douglas utility function is formulated as follows. With JuMP, we can easily describe and numerically solve the utility maximization problem using the <code>cobb_douglas</code> function we already defined.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%20%20%5Ctext%7Bmaximize%7D%20&amp;&amp;%20U%20=%20%5Cprod_%7Bi=1%7D%5E%7Bn%7D%20x_i%5E%7B%5Calpha_i%7D%20%5C%5C%0A%20%20%5Ctext%7Bsubject%20to%7D%20&amp;&amp;%20%5Csum_%7Bi=1%7D%5E%7Bn%7D%20p_i%20x_i%20%5Cleq%20Y%20%5C%5C%0A%5Cend%7Baligned%7D%0A"></p>
<div id="12" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb10-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">using</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">JuMP</span></span>
<span id="cb10-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">using</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Ipopt</span></span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">demand_marshallian_cobb_douglas_numerical</span>(prices, income; weights)</span>
<span id="cb10-5">    n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(prices)</span>
<span id="cb10-6">    model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Model</span>(Ipopt.Optimizer)</span>
<span id="cb10-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_silent</span>(model)</span>
<span id="cb10-8">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@variable</span>(model, quantities[<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define consumption quantities as variables</span></span>
<span id="cb10-9">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@objective</span>(model, Max, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cobb_douglas</span>(quantities; weights)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Maximize utility</span></span>
<span id="cb10-10">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@constraint</span>(model, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(prices <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.*</span> quantities) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> income) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Budget constraint</span></span>
<span id="cb10-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">optimize!</span>(model)</span>
<span id="cb10-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">value</span>.(quantities)</span>
<span id="cb10-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>demand_marshallian_cobb_douglas_numerical (generic function with 1 method)</code></pre>
</div>
</div>
<p>In this formulation of the utility maximization problem, we are doing the following.</p>
<ol type="1">
<li>Using the <code>@variable</code> macro to define the consumption quantities <code>quantities</code> as variables</li>
<li>Using the <code>@objective</code> macro to define the objective function that maximizes utility</li>
<li>Using the <code>@constraint</code> macro to define the budget constraint</li>
</ol>
<p>Now, let’s use the implemented <code>demand_marshallian_cobb_douglas_numerical</code> function to find the numerical solution.</p>
<div id="14" class="cell" data-messages="false" data-execution_count="1">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb12-1">quantities_numerical <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">demand_marshallian_cobb_douglas_numerical</span>(</span>
<span id="cb12-2">  [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>],</span>
<span id="cb12-3">  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,</span>
<span id="cb12-4">  weights<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>]</span>
<span id="cb12-5">)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
 Ipopt is released as open source code under the Eclipse Public License (EPL).
         For more information visit https://github.com/coin-or/Ipopt
******************************************************************************
</code></pre>
</div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>3-element Vector{Float64}:
 30.000000297265974
 20.00000019590169
 10.000000099089569</code></pre>
</div>
</div>
<p>The final results are as follows, and we can see that the numerical solution almost matches the analytical solution.</p>
<div id="16" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb15-1">quantities_numerical</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>3-element Vector{Float64}:
 30.000000297265974
 20.00000019590169
 10.000000099089569</code></pre>
</div>
</div>
<div id="18" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb17-1">quantities_analytical</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>3-element Vector{Float64}:
 30.0
 20.0
 10.0</code></pre>
</div>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>In this article, I used Julia JuMP to solve a utility maximization problem based on the Cobb-Douglas utility function.</p>
<p>We confirmed that Julia JuMP makes it easy to formulate and numerically solve nonlinear optimization problems.</p>
<p>With Julia JuMP, simple functions can be used as-is as objective functions and the like, so it may also be useful for implementing economic models.</p>


</section>


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

<ol>
<li id="fn1"><p>I omit the detailed derivation here, but it can be derived from the following conditions.</p>
<ol type="1">
<li>Utility maximization condition: <img src="https://latex.codecogs.com/png.latex?MRS_%7Bij%7D=%5Cfrac%7B%5Cpartial%20U/%5Cpartial%20x_i%7D%7B%5Cpartial%20U/%5Cpartial%20x_j%7D=%5Cfrac%7Bp_i%7D%7Bp_j%7D"></li>
<li>Budget constraint: <img src="https://latex.codecogs.com/png.latex?%5Csum_%7Bi=1%7D%5E%7Bn%7D%20p_i%20x_i%20=%20Y"></li>
</ol>
↩︎</li>
</ol>
</section></div> ]]></description>
  <category>JuMP</category>
  <category>Julia</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2025/02/solving-utility-maximisation-problems-with-julia-jump.html</guid>
  <pubDate>Sun, 16 Feb 2025 15:00:00 GMT</pubDate>
</item>
<item>
  <title>Tips for Tidying Excel Files with R</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2024/12/tips-for-tidying-excel-file-with-r.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2024/12/tips-for-tidying-excel-file-with-r.html">my Japanese article</a>.</p>
<p>This article is the 23rd-day entry for <a href="https://qiita.com/advent-calendar/2024/rlang">R Advent Calendar 2024</a>.</p>
<p>In data analysis, data preprocessing is unavoidable. Recently, creating machine-readable data has become increasingly important, but arranging data into a format that is easy to read for both humans and machines is not easy.</p>
<p>So, in this article, I introduce some tips for tidying Excel files with R—files that are often created with human readers in mind. This article mainly focuses on tips for Excel files, but I think they can also be applied to other data formats such as CSV files.</p>
<p>There are mainly two approaches to tidying Excel files. In this article, I will first explain how to interpret the table structure of an Excel file, and then introduce data-tidying methods using the following approaches.</p>
<ol type="1">
<li>Using the <a href="https://readxl.tidyverse.org">readxl</a> package</li>
<li>Using the <a href="https://nacnudus.github.io/tidyxl/">tidyxl</a> and <a href="https://nacnudus.github.io/unpivotr/">unpivotr</a> packages</li>
</ol>
<p>Also, since several articles on tidying Excel file data have already been published in Japanese, the following articles may also be helpful.</p>
<ul>
<li><a href="https://bunseki-data.com/r-onlinecourse/2023/11/09/%E7%A5%9Eexcel%E3%82%92r%E3%81%A7tidy%E5%8C%96%E3%81%97%E3%81%A6%E3%81%BF%E3%82%88%E3%81%86/">Tidying a “god-tier” Excel file with R (in Japanese)</a></li>
<li><a href="https://y-mattu.hatenablog.com/entry/read_dark-excel">Defense techniques against “dark” Excel files (in Japanese)</a></li>
</ul>
<section id="before-tidying-an-excel-file" class="level2">
<h2 class="anchored" data-anchor-id="before-tidying-an-excel-file">Before tidying an Excel file</h2>
<p>Before getting into the main topic, there’s one thing I’d like to note: check whether the data you want to use for analysis is available in a more machine-readable format.</p>
<p>Excel files are convenient for viewing data, but they are often not very machine-readable. Therefore, it’s advisable to check in advance whether the data is available in a more suitable format. For example, e-Stat, Japan’s portal site for government statistics, sometimes lets you use its API to obtain data that is more machine-readable than a typical Excel file.</p>
</section>
<section id="the-gap-between-humans-and-machines-in-data-formats" class="level2">
<h2 class="anchored" data-anchor-id="the-gap-between-humans-and-machines-in-data-formats">The “gap between humans and machines” in data formats</h2>
<p>Data that is easy for us to read isn’t necessarily easy for a machine to read. A classic example of this is weekly weather forecast data<sup>1</sup>.</p>
<p>For example, let’s say we have the following dummy weekly weather forecast data. If a human looks at this data, they can immediately understand that it’s “weather by region and date.” However, it may be surprisingly difficult for a machine to mechanically grasp the following:</p>
<ul>
<li>That the column names from the second column onward represent dates</li>
<li>That the values in the columns from the second column onward represent weather</li>
</ul>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"></span>
<span id="cb1-3">region <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sapporo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tokyo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nagoya"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Osaka"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fukuoka"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Naha"</span>)</span>
<span id="cb1-4">date <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;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ymd</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-12-20"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ymd</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-12-26"</span>), <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;">"day"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%m/%d"</span>)</span>
<span id="cb1-6">weather <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"🌞"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"⛅"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"☔"</span>)</span>
<span id="cb1-7"></span>
<span id="cb1-8"><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;">1234</span>)</span>
<span id="cb1-9">weather_forecast <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;">expand_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">region =</span> region, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> date) <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;">weather =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(weather, <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;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb1-11"></span>
<span id="cb1-12">weather_forecast_wider <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> weather_forecast <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> weather)</span>
<span id="cb1-14">weather_forecast_wider</span></code></pre></div>
</details>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["region"],"name":[1],"type":["chr"],"align":["left"]},{"label":["12/20"],"name":[2],"type":["chr"],"align":["left"]},{"label":["12/21"],"name":[3],"type":["chr"],"align":["left"]},{"label":["12/22"],"name":[4],"type":["chr"],"align":["left"]},{"label":["12/23"],"name":[5],"type":["chr"],"align":["left"]},{"label":["12/24"],"name":[6],"type":["chr"],"align":["left"]},{"label":["12/25"],"name":[7],"type":["chr"],"align":["left"]},{"label":["12/26"],"name":[8],"type":["chr"],"align":["left"]}],"data":[{"1":"Sapporo","2":"⛅","3":"⛅","4":"🌞","5":"☔","6":"🌞","7":"🌞","8":"⛅"},{"1":"Tokyo","2":"⛅","3":"☔","4":"⛅","5":"⛅","6":"⛅","7":"☔","8":"⛅"},{"1":"Nagoya","2":"⛅","3":"⛅","4":"⛅","5":"🌞","6":"☔","7":"☔","8":"☔"},{"1":"Osaka","2":"🌞","3":"⛅","4":"🌞","5":"⛅","6":"⛅","7":"☔","8":"☔"},{"1":"Fukuoka","2":"☔","3":"☔","4":"🌞","5":"☔","6":"⛅","7":"⛅","8":"☔"},{"1":"Naha","2":"⛅","3":"🌞","4":"☔","5":"⛅","6":"🌞","7":"⛅","8":"☔"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>To make it clearer that the data above represents “weather by region and date,” data like the following is more suitable. In this data, the observed value—weather—is properly mapped to the axes (dimensions) of region and date, and this kind of long-format data is called tidy data. However, if data in this long format were shown on the news, most people would probably find it hard to read<sup>2</sup>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">weather_forecast</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["region"],"name":[1],"type":["chr"],"align":["left"]},{"label":["date"],"name":[2],"type":["chr"],"align":["left"]},{"label":["weather"],"name":[3],"type":["chr"],"align":["left"]}],"data":[{"1":"Sapporo","2":"12/20","3":"⛅"},{"1":"Sapporo","2":"12/21","3":"⛅"},{"1":"Sapporo","2":"12/22","3":"🌞"},{"1":"Sapporo","2":"12/23","3":"☔"},{"1":"Sapporo","2":"12/24","3":"🌞"},{"1":"Sapporo","2":"12/25","3":"🌞"},{"1":"Sapporo","2":"12/26","3":"⛅"},{"1":"Tokyo","2":"12/20","3":"⛅"},{"1":"Tokyo","2":"12/21","3":"☔"},{"1":"Tokyo","2":"12/22","3":"⛅"},{"1":"Tokyo","2":"12/23","3":"⛅"},{"1":"Tokyo","2":"12/24","3":"⛅"},{"1":"Tokyo","2":"12/25","3":"☔"},{"1":"Tokyo","2":"12/26","3":"⛅"},{"1":"Nagoya","2":"12/20","3":"⛅"},{"1":"Nagoya","2":"12/21","3":"⛅"},{"1":"Nagoya","2":"12/22","3":"⛅"},{"1":"Nagoya","2":"12/23","3":"🌞"},{"1":"Nagoya","2":"12/24","3":"☔"},{"1":"Nagoya","2":"12/25","3":"☔"},{"1":"Nagoya","2":"12/26","3":"☔"},{"1":"Osaka","2":"12/20","3":"🌞"},{"1":"Osaka","2":"12/21","3":"⛅"},{"1":"Osaka","2":"12/22","3":"🌞"},{"1":"Osaka","2":"12/23","3":"⛅"},{"1":"Osaka","2":"12/24","3":"⛅"},{"1":"Osaka","2":"12/25","3":"☔"},{"1":"Osaka","2":"12/26","3":"☔"},{"1":"Fukuoka","2":"12/20","3":"☔"},{"1":"Fukuoka","2":"12/21","3":"☔"},{"1":"Fukuoka","2":"12/22","3":"🌞"},{"1":"Fukuoka","2":"12/23","3":"☔"},{"1":"Fukuoka","2":"12/24","3":"⛅"},{"1":"Fukuoka","2":"12/25","3":"⛅"},{"1":"Fukuoka","2":"12/26","3":"☔"},{"1":"Naha","2":"12/20","3":"⛅"},{"1":"Naha","2":"12/21","3":"🌞"},{"1":"Naha","2":"12/22","3":"☔"},{"1":"Naha","2":"12/23","3":"⛅"},{"1":"Naha","2":"12/24","3":"🌞"},{"1":"Naha","2":"12/25","3":"⛅"},{"1":"Naha","2":"12/26","3":"☔"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>Why does this “gap between humans and machines” arise?</p>
<p>The figure below shows the long-format data above plotted as a scatter plot with ggplot2. Comparing this scatter plot with the table shown earlier, you can see that they look quite similar. For us, a table with axes in both the vertical and horizontal directions may be more intuitive to understand than one with only a vertical axis.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">weather_forecast <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(date, region, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> weather)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> rev)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/12/tips-for-tidying-excel-file-with-r_files/figure-html/weather-forecast-plot-1.png" class="img-fluid figure-img" width="4000"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="interpreting-the-table-structure-of-an-excel-file" class="level2">
<h2 class="anchored" data-anchor-id="interpreting-the-table-structure-of-an-excel-file">Interpreting the table structure of an Excel file</h2>
<p>With this “gap between humans and machines” in mind, let’s interpret the table structure of an actual Excel file.</p>
<p>With this “gap between humans and machines” in mind, let’s interpret the table structure of an actual Excel file. If the data is as simple as “weather by region and date,” the table structure isn’t too complex. But what about data such as “population by year, region, sex, and age group,” for example? Let’s actually look at <a href="https://www.e-stat.go.jp/stat-search/files?page=1&amp;layout=datalist&amp;toukei=00200521&amp;tstat=000001011777&amp;cycle=0&amp;tclass1=000001011778&amp;stat_infid=000001085927&amp;tclass2val=0">this Excel file</a> published on e-Stat. The image below shows an excerpt of the top part of the Excel file.</p>
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/12/tips-tidying-excel-file-with-r/population_by_year_sex_age_class.png" class="img-fluid"></p>
<p>Leaving aside the explanatory text at the top of the data, we can see it has the following data format. If we color-code the axes (dimensions)—year, region, sex, and age—in light blue (<span style="color:#a6cee3;">■</span>), and the type/unit of the observed values—population, population ratio, and sex ratio—in blue (<span style="color:#1f78b4;">■</span>), we get the figure shown below. To summarize the details of the data format:</p>
<ul>
<li>Columns 1 and 2 store the axis (dimension) information for region and age group, respectively</li>
<li>The part corresponding to the column names from column 3 onward mixes axis (dimension) information with observed-value information
<ul>
<li>Rows 1 and 3 store the axis (dimension) information for year and sex, respectively</li>
<li>Rows 2 and 4 record the type and unit of the observed values—population, population ratio, and sex ratio, respectively</li>
</ul></li>
</ul>
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/12/tips-tidying-excel-file-with-r/population_by_year_sex_age_class_colored.png" class="img-fluid"></p>
<p>When tidying an Excel file, it’s important to keep the following points in mind:</p>
<ul>
<li>When column names span multiple rows, you need to sort out in advance whether the information in each row corresponds to an axis (dimension) or to an observed value</li>
<li>To avoid duplicate column names, some column names are often abbreviated, or cells are merged</li>
</ul>
</section>
<section id="tidying-an-excel-file-with-readxl" class="level2">
<h2 class="anchored" data-anchor-id="tidying-an-excel-file-with-readxl">Tidying an Excel file with readxl</h2>
<p>Now, let’s actually tidy an Excel file. Below, I show the code for tidying the “population by year, region, sex, and age” Excel file introduced above.</p>
<p>The <a href="https://readxl.tidyverse.org">readxl</a> package is useful for reading Excel files<sup>3</sup>. Here, since we’ll use the readxl package to tidy the Excel file, let’s load the readxl and tidyverse packages in advance.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install packages as needed</span></span>
<span id="cb4-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install.packages("pak")</span></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pak::pak("readxl")</span></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pak::pak("tidyverse")</span></span>
<span id="cb4-5"></span>
<span id="cb4-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readxl)</span>
<span id="cb4-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code used to download the data</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(fs)</span>
<span id="cb5-2"></span>
<span id="cb5-3">exdir <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tips-tidying-excel-data-with-r"</span></span>
<span id="cb5-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir_create</span>(exdir)</span>
<span id="cb5-5"></span>
<span id="cb5-6">destfile <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;">path</span>(exdir, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"population_by_year_sex_age_class"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ext =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xlsx"</span>)</span>
<span id="cb5-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;">file_exists</span>(destfile)) {</span>
<span id="cb5-8">  curl<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;">curl_download</span>(</span>
<span id="cb5-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.e-stat.go.jp/stat-search/file-download?statInfId=000001085927&amp;fileKind=0"</span>,</span>
<span id="cb5-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> destfile</span>
<span id="cb5-11">  )</span>
<span id="cb5-12">}</span></code></pre></div>
</details>
</div>
<section id="reading-column-names-and-handling-merged-cells" class="level3">
<h3 class="anchored" data-anchor-id="reading-column-names-and-handling-merged-cells">1. Reading column names and handling merged cells</h3>
<p>As the first step in reading the Excel file, let’s start by getting the column names. Normally, for tabular data such as CSV files, the column names are recorded in the first row.</p>
<p>However, in the Excel file shown above, the column names are recorded across multiple rows starting from the third column, so some work is needed to properly set them as the column names of the data frame.</p>
<p>So, let’s first read the column names using the <code>read_excel()</code> function from the readxl package. Although there are some differences, such as how the <code>col_types</code> argument is specified, <code>read_excel()</code> can basically read Excel files in a format similar to readr’s <code>read_csv()</code> function<sup>4</sup>.</p>
<p>Since the column names from the third column onward store axis (dimension) and observed-value information horizontally, we transpose them with the <code>t()</code> function before converting to a data frame. Furthermore, if we exclude the first two rows (region and age group), we get the following data.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Location of the Excel file downloaded in advance</span></span>
<span id="cb6-2">file <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tips-tidying-excel-file-with-r/population_by_year_sex_age_class.xlsx"</span></span>
<span id="cb6-3">sheet <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"da03"</span></span>
<span id="cb6-4"></span>
<span id="cb6-5">data_col_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_excel</span>(</span>
<span id="cb6-6">  file,</span>
<span id="cb6-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sheet =</span> sheet, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sheet name</span></span>
<span id="cb6-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skip =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Skip the explanatory section</span></span>
<span id="cb6-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_max =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Only read the column-name section</span></span>
<span id="cb6-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb6-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_types =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>,</span>
<span id="cb6-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.name_repair =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"minimal"</span></span>
<span id="cb6-13">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Transpose, then convert to a data frame</span></span>
<span id="cb6-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>(</span>
<span id="cb6-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.name_repair =</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value_type"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sex"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value_unit"</span>)</span>
<span id="cb6-18">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(year, value_type, sex, value_unit) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-20"></span>
<span id="cb6-21">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Exclude the first two rows (region and age group)</span></span>
<span id="cb6-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_tail</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</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;">2</span>)</span>
<span id="cb6-23"></span>
<span id="cb6-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data_col_names, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["year"],"name":[1],"type":["chr"],"align":["left"]},{"label":["value_type"],"name":[2],"type":["chr"],"align":["left"]},{"label":["sex"],"name":[3],"type":["chr"],"align":["left"]},{"label":["value_unit"],"name":[4],"type":["chr"],"align":["left"]}],"data":[{"1":"1920年","2":"人口","3":"総数","4":"（人）"},{"1":"大正9年","2":"NA","3":"男","4":"（人）"},{"1":"NA","2":"NA","3":"女","4":"（人）"},{"1":"NA","2":"年齢，男女別割合","3":"総数","4":"（％）"},{"1":"NA","2":"NA","3":"男","4":"（％）"},{"1":"NA","2":"NA","3":"女","4":"（％）"},{"1":"NA","2":"人口性比","3":"NA","4":"-"},{"1":"1925年","2":"人口","3":"総数","4":"（人）"},{"1":"大正14年","2":"NA","3":"男","4":"（人）"},{"1":"NA","2":"NA","3":"女","4":"（人）"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>Looking at <code>data_col_names</code>, we can see the following:</p>
<ul>
<li>The <code>year</code> column contains a mix of Gregorian and Japanese-era years</li>
<li>In the <code>year</code> and <code>value_type</code> columns, some column names are omitted and become <code>NA</code> to avoid duplicate column names</li>
</ul>
<p>So, the code below mainly performs the following processing:</p>
<ul>
<li>Extract only the Gregorian year number from the <code>year</code> column</li>
<li>Use tidyr’s <code>fill()</code> function to fill in the <code>NA</code> values in the <code>year</code> and <code>value_type</code> columns</li>
</ul>
<p>With this, we’re now ready to create the column names.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">data_col_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data_col_names <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract only the Gregorian year number</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb7-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-5">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+(?=年$)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-6">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(),</span>
<span id="cb7-7"></span>
<span id="cb7-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Replace with an empty string if value_unit is "-"</span></span>
<span id="cb7-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value_unit =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(value_unit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, value_unit)</span>
<span id="cb7-10">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-11"></span>
<span id="cb7-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fill in NA values in year and value_type</span></span>
<span id="cb7-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fill</span>(year, value_type)</span>
<span id="cb7-14"></span>
<span id="cb7-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data_col_names, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["year"],"name":[1],"type":["int"],"align":["right"]},{"label":["value_type"],"name":[2],"type":["chr"],"align":["left"]},{"label":["sex"],"name":[3],"type":["chr"],"align":["left"]},{"label":["value_unit"],"name":[4],"type":["chr"],"align":["left"]}],"data":[{"1":"1920","2":"人口","3":"総数","4":"（人）"},{"1":"1920","2":"人口","3":"男","4":"（人）"},{"1":"1920","2":"人口","3":"女","4":"（人）"},{"1":"1920","2":"年齢，男女別割合","3":"総数","4":"（％）"},{"1":"1920","2":"年齢，男女別割合","3":"男","4":"（％）"},{"1":"1920","2":"年齢，男女別割合","3":"女","4":"（％）"},{"1":"1920","2":"人口性比","3":"NA","4":""},{"1":"1925","2":"人口","3":"総数","4":"（人）"},{"1":"1925","2":"人口","3":"男","4":"（人）"},{"1":"1925","2":"人口","3":"女","4":"（人）"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</section>
<section id="creating-column-names-and-reading-the-data" class="level3">
<h3 class="anchored" data-anchor-id="creating-column-names-and-reading-the-data">2. Creating column names and reading the data</h3>
<p>Now, let’s create the column names using <code>data_col_names</code>. Here, the column names were created using the following steps:</p>
<ul>
<li>First, combine the <code>value_type</code> and <code>value_unit</code> columns to create the <code>value_type</code> column</li>
<li>Next, combine the <code>year</code>, <code>sex</code>, and <code>value_type</code> columns in that order, separated by <code>"/"</code>, to create the <code>col_name</code> column</li>
<li>Finally, combine the data from <code>"region"</code>, <code>"age_class"</code>, and the <code>col_name</code> column to create the column names</li>
</ul>
<p>The code below uses tidyr’s <code>unite()</code> function to combine the data frame’s column names. With this, the column names needed for reading the data have been created.</p>
<div class="cell">
<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">col_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data_col_names <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unite</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value_type"</span>, value_type, value_unit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unite</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col_name"</span>, year, sex, value_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(col_name)</span>
<span id="cb8-5">col_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"region"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age_class"</span>, col_names)</span>
<span id="cb8-6"></span>
<span id="cb8-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(col_names, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "region"                           "age_class"                       
 [3] "1920/総数/人口（人）"             "1920/男/人口（人）"              
 [5] "1920/女/人口（人）"               "1920/総数/年齢，男女別割合（％）"
 [7] "1920/男/年齢，男女別割合（％）"   "1920/女/年齢，男女別割合（％）"  
 [9] "1920/NA/人口性比"                 "1925/総数/人口（人）"            </code></pre>
</div>
</div>
<p>Let’s use <code>col_names</code> to read the data. By specifying <code>col_names = col_names</code> in the <code>read_excel()</code> function, we can read the data using the column names we just created.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_excel</span>(</span>
<span id="cb10-2">  file,</span>
<span id="cb10-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sheet =</span> sheet,</span>
<span id="cb10-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skip =</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;">5</span>,</span>
<span id="cb10-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_names =</span> col_names,</span>
<span id="cb10-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_types =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>,</span>
<span id="cb10-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.name_repair =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"minimal"</span></span>
<span id="cb10-8">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-9"></span>
<span id="cb10-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove duplicate columns found at the end (likely a mistake made when the original data was created)</span></span>
<span id="cb10-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_of</span>(vctrs<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;">vec_unique_loc</span>(col_names)))</span>
<span id="cb10-12"></span>
<span id="cb10-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["region"],"name":[1],"type":["chr"],"align":["left"]},{"label":["age_class"],"name":[2],"type":["chr"],"align":["left"]},{"label":["1920/総数/人口（人）"],"name":[3],"type":["chr"],"align":["left"]},{"label":["1920/男/人口（人）"],"name":[4],"type":["chr"],"align":["left"]},{"label":["1920/女/人口（人）"],"name":[5],"type":["chr"],"align":["left"]},{"label":["1920/総数/年齢，男女別割合（％）"],"name":[6],"type":["chr"],"align":["left"]},{"label":["1920/男/年齢，男女別割合（％）"],"name":[7],"type":["chr"],"align":["left"]},{"label":["1920/女/年齢，男女別割合（％）"],"name":[8],"type":["chr"],"align":["left"]},{"label":["1920/NA/人口性比"],"name":[9],"type":["chr"],"align":["left"]},{"label":["1925/総数/人口（人）"],"name":[10],"type":["chr"],"align":["left"]},{"label":["1925/男/人口（人）"],"name":[11],"type":["chr"],"align":["left"]},{"label":["1925/女/人口（人）"],"name":[12],"type":["chr"],"align":["left"]},{"label":["1925/総数/年齢，男女別割合（％）"],"name":[13],"type":["chr"],"align":["left"]},{"label":["1925/男/年齢，男女別割合（％）"],"name":[14],"type":["chr"],"align":["left"]},{"label":["1925/女/年齢，男女別割合（％）"],"name":[15],"type":["chr"],"align":["left"]},{"label":["1925/NA/人口性比"],"name":[16],"type":["chr"],"align":["left"]},{"label":["1930/総数/人口（人）"],"name":[17],"type":["chr"],"align":["left"]},{"label":["1930/男/人口（人）"],"name":[18],"type":["chr"],"align":["left"]},{"label":["1930/女/人口（人）"],"name":[19],"type":["chr"],"align":["left"]},{"label":["1930/総数/年齢，男女別割合（％）"],"name":[20],"type":["chr"],"align":["left"]},{"label":["1930/男/年齢，男女別割合（％）"],"name":[21],"type":["chr"],"align":["left"]},{"label":["1930/女/年齢，男女別割合（％）"],"name":[22],"type":["chr"],"align":["left"]},{"label":["1930/NA/人口性比"],"name":[23],"type":["chr"],"align":["left"]},{"label":["1935/総数/人口（人）"],"name":[24],"type":["chr"],"align":["left"]},{"label":["1935/男/人口（人）"],"name":[25],"type":["chr"],"align":["left"]},{"label":["1935/女/人口（人）"],"name":[26],"type":["chr"],"align":["left"]},{"label":["1935/総数/年齢，男女別割合（％）"],"name":[27],"type":["chr"],"align":["left"]},{"label":["1935/男/年齢，男女別割合（％）"],"name":[28],"type":["chr"],"align":["left"]},{"label":["1935/女/年齢，男女別割合（％）"],"name":[29],"type":["chr"],"align":["left"]},{"label":["1935/NA/人口性比"],"name":[30],"type":["chr"],"align":["left"]},{"label":["1940/総数/人口（人）"],"name":[31],"type":["chr"],"align":["left"]},{"label":["1940/男/人口（人）"],"name":[32],"type":["chr"],"align":["left"]},{"label":["1940/女/人口（人）"],"name":[33],"type":["chr"],"align":["left"]},{"label":["1940/総数/年齢，男女別割合（％）"],"name":[34],"type":["chr"],"align":["left"]},{"label":["1940/男/年齢，男女別割合（％）"],"name":[35],"type":["chr"],"align":["left"]},{"label":["1940/女/年齢，男女別割合（％）"],"name":[36],"type":["chr"],"align":["left"]},{"label":["1940/NA/人口性比"],"name":[37],"type":["chr"],"align":["left"]},{"label":["1945/総数/人口（人）"],"name":[38],"type":["chr"],"align":["left"]},{"label":["1945/男/人口（人）"],"name":[39],"type":["chr"],"align":["left"]},{"label":["1945/女/人口（人）"],"name":[40],"type":["chr"],"align":["left"]},{"label":["1945/総数/年齢，男女別割合（％）"],"name":[41],"type":["chr"],"align":["left"]},{"label":["1945/男/年齢，男女別割合（％）"],"name":[42],"type":["chr"],"align":["left"]},{"label":["1945/女/年齢，男女別割合（％）"],"name":[43],"type":["chr"],"align":["left"]},{"label":["1945/NA/人口性比"],"name":[44],"type":["chr"],"align":["left"]},{"label":["1950/総数/人口（人）"],"name":[45],"type":["chr"],"align":["left"]},{"label":["1950/男/人口（人）"],"name":[46],"type":["chr"],"align":["left"]},{"label":["1950/女/人口（人）"],"name":[47],"type":["chr"],"align":["left"]},{"label":["1950/総数/年齢，男女別割合（％）"],"name":[48],"type":["chr"],"align":["left"]},{"label":["1950/男/年齢，男女別割合（％）"],"name":[49],"type":["chr"],"align":["left"]},{"label":["1950/女/年齢，男女別割合（％）"],"name":[50],"type":["chr"],"align":["left"]},{"label":["1950/NA/人口性比"],"name":[51],"type":["chr"],"align":["left"]},{"label":["1955/総数/人口（人）"],"name":[52],"type":["chr"],"align":["left"]},{"label":["1955/男/人口（人）"],"name":[53],"type":["chr"],"align":["left"]},{"label":["1955/女/人口（人）"],"name":[54],"type":["chr"],"align":["left"]},{"label":["1955/総数/年齢，男女別割合（％）"],"name":[55],"type":["chr"],"align":["left"]},{"label":["1955/男/年齢，男女別割合（％）"],"name":[56],"type":["chr"],"align":["left"]},{"label":["1955/女/年齢，男女別割合（％）"],"name":[57],"type":["chr"],"align":["left"]},{"label":["1955/NA/人口性比"],"name":[58],"type":["chr"],"align":["left"]},{"label":["1960/総数/人口（人）"],"name":[59],"type":["chr"],"align":["left"]},{"label":["1960/男/人口（人）"],"name":[60],"type":["chr"],"align":["left"]},{"label":["1960/女/人口（人）"],"name":[61],"type":["chr"],"align":["left"]},{"label":["1960/総数/年齢，男女別割合（％）"],"name":[62],"type":["chr"],"align":["left"]},{"label":["1960/男/年齢，男女別割合（％）"],"name":[63],"type":["chr"],"align":["left"]},{"label":["1960/女/年齢，男女別割合（％）"],"name":[64],"type":["chr"],"align":["left"]},{"label":["1960/NA/人口性比"],"name":[65],"type":["chr"],"align":["left"]},{"label":["1965/総数/人口（人）"],"name":[66],"type":["chr"],"align":["left"]},{"label":["1965/男/人口（人）"],"name":[67],"type":["chr"],"align":["left"]},{"label":["1965/女/人口（人）"],"name":[68],"type":["chr"],"align":["left"]},{"label":["1965/総数/年齢，男女別割合（％）"],"name":[69],"type":["chr"],"align":["left"]},{"label":["1965/男/年齢，男女別割合（％）"],"name":[70],"type":["chr"],"align":["left"]},{"label":["1965/女/年齢，男女別割合（％）"],"name":[71],"type":["chr"],"align":["left"]},{"label":["1965/NA/人口性比"],"name":[72],"type":["chr"],"align":["left"]},{"label":["1970/総数/人口（人）"],"name":[73],"type":["chr"],"align":["left"]},{"label":["1970/男/人口（人）"],"name":[74],"type":["chr"],"align":["left"]},{"label":["1970/女/人口（人）"],"name":[75],"type":["chr"],"align":["left"]},{"label":["1970/総数/年齢，男女別割合（％）"],"name":[76],"type":["chr"],"align":["left"]},{"label":["1970/男/年齢，男女別割合（％）"],"name":[77],"type":["chr"],"align":["left"]},{"label":["1970/女/年齢，男女別割合（％）"],"name":[78],"type":["chr"],"align":["left"]},{"label":["1970/NA/人口性比"],"name":[79],"type":["chr"],"align":["left"]},{"label":["1975/総数/人口（人）"],"name":[80],"type":["chr"],"align":["left"]},{"label":["1975/男/人口（人）"],"name":[81],"type":["chr"],"align":["left"]},{"label":["1975/女/人口（人）"],"name":[82],"type":["chr"],"align":["left"]},{"label":["1975/総数/年齢，男女別割合（％）"],"name":[83],"type":["chr"],"align":["left"]},{"label":["1975/男/年齢，男女別割合（％）"],"name":[84],"type":["chr"],"align":["left"]},{"label":["1975/女/年齢，男女別割合（％）"],"name":[85],"type":["chr"],"align":["left"]},{"label":["1975/NA/人口性比"],"name":[86],"type":["chr"],"align":["left"]},{"label":["1980/総数/人口（人）"],"name":[87],"type":["chr"],"align":["left"]},{"label":["1980/男/人口（人）"],"name":[88],"type":["chr"],"align":["left"]},{"label":["1980/女/人口（人）"],"name":[89],"type":["chr"],"align":["left"]},{"label":["1980/総数/年齢，男女別割合（％）"],"name":[90],"type":["chr"],"align":["left"]},{"label":["1980/男/年齢，男女別割合（％）"],"name":[91],"type":["chr"],"align":["left"]},{"label":["1980/女/年齢，男女別割合（％）"],"name":[92],"type":["chr"],"align":["left"]},{"label":["1980/NA/人口性比"],"name":[93],"type":["chr"],"align":["left"]},{"label":["1985/総数/人口（人）"],"name":[94],"type":["chr"],"align":["left"]},{"label":["1985/男/人口（人）"],"name":[95],"type":["chr"],"align":["left"]},{"label":["1985/女/人口（人）"],"name":[96],"type":["chr"],"align":["left"]},{"label":["1985/総数/年齢，男女別割合（％）"],"name":[97],"type":["chr"],"align":["left"]},{"label":["1985/男/年齢，男女別割合（％）"],"name":[98],"type":["chr"],"align":["left"]},{"label":["1985/女/年齢，男女別割合（％）"],"name":[99],"type":["chr"],"align":["left"]},{"label":["1985/NA/人口性比"],"name":[100],"type":["chr"],"align":["left"]},{"label":["1990/総数/人口（人）"],"name":[101],"type":["chr"],"align":["left"]},{"label":["1990/男/人口（人）"],"name":[102],"type":["chr"],"align":["left"]},{"label":["1990/女/人口（人）"],"name":[103],"type":["chr"],"align":["left"]},{"label":["1990/総数/年齢，男女別割合（％）"],"name":[104],"type":["chr"],"align":["left"]},{"label":["1990/男/年齢，男女別割合（％）"],"name":[105],"type":["chr"],"align":["left"]},{"label":["1990/女/年齢，男女別割合（％）"],"name":[106],"type":["chr"],"align":["left"]},{"label":["1990/NA/人口性比"],"name":[107],"type":["chr"],"align":["left"]},{"label":["1995/総数/人口（人）"],"name":[108],"type":["chr"],"align":["left"]},{"label":["1995/男/人口（人）"],"name":[109],"type":["chr"],"align":["left"]},{"label":["1995/女/人口（人）"],"name":[110],"type":["chr"],"align":["left"]},{"label":["1995/総数/年齢，男女別割合（％）"],"name":[111],"type":["chr"],"align":["left"]},{"label":["1995/男/年齢，男女別割合（％）"],"name":[112],"type":["chr"],"align":["left"]},{"label":["1995/女/年齢，男女別割合（％）"],"name":[113],"type":["chr"],"align":["left"]},{"label":["1995/NA/人口性比"],"name":[114],"type":["chr"],"align":["left"]},{"label":["2000/総数/人口（人）"],"name":[115],"type":["chr"],"align":["left"]},{"label":["2000/男/人口（人）"],"name":[116],"type":["chr"],"align":["left"]},{"label":["2000/女/人口（人）"],"name":[117],"type":["chr"],"align":["left"]},{"label":["2000/総数/年齢，男女別割合（％）"],"name":[118],"type":["chr"],"align":["left"]},{"label":["2000/男/年齢，男女別割合（％）"],"name":[119],"type":["chr"],"align":["left"]},{"label":["2000/女/年齢，男女別割合（％）"],"name":[120],"type":["chr"],"align":["left"]},{"label":["2000/NA/人口性比"],"name":[121],"type":["chr"],"align":["left"]},{"label":["2005/総数/人口（人）"],"name":[122],"type":["chr"],"align":["left"]},{"label":["2005/男/人口（人）"],"name":[123],"type":["chr"],"align":["left"]},{"label":["2005/女/人口（人）"],"name":[124],"type":["chr"],"align":["left"]},{"label":["2005/総数/年齢，男女別割合（％）"],"name":[125],"type":["chr"],"align":["left"]},{"label":["2005/男/年齢，男女別割合（％）"],"name":[126],"type":["chr"],"align":["left"]},{"label":["2005/女/年齢，男女別割合（％）"],"name":[127],"type":["chr"],"align":["left"]},{"label":["2005/NA/人口性比"],"name":[128],"type":["chr"],"align":["left"]},{"label":["2010/総数/人口（人）"],"name":[129],"type":["chr"],"align":["left"]},{"label":["2010/男/人口（人）"],"name":[130],"type":["chr"],"align":["left"]},{"label":["2010/女/人口（人）"],"name":[131],"type":["chr"],"align":["left"]},{"label":["2010/総数/年齢，男女別割合（％）"],"name":[132],"type":["chr"],"align":["left"]},{"label":["2010/男/年齢，男女別割合（％）"],"name":[133],"type":["chr"],"align":["left"]},{"label":["2010/女/年齢，男女別割合（％）"],"name":[134],"type":["chr"],"align":["left"]},{"label":["2010/NA/人口性比"],"name":[135],"type":["chr"],"align":["left"]},{"label":["2015/総数/人口（人）"],"name":[136],"type":["chr"],"align":["left"]},{"label":["2015/男/人口（人）"],"name":[137],"type":["chr"],"align":["left"]},{"label":["2015/女/人口（人）"],"name":[138],"type":["chr"],"align":["left"]},{"label":["2015/総数/年齢，男女別割合（％）"],"name":[139],"type":["chr"],"align":["left"]},{"label":["2015/男/年齢，男女別割合（％）"],"name":[140],"type":["chr"],"align":["left"]},{"label":["2015/女/年齢，男女別割合（％）"],"name":[141],"type":["chr"],"align":["left"]},{"label":["2015/NA/人口性比"],"name":[142],"type":["chr"],"align":["left"]},{"label":["2020/総数/人口（人）"],"name":[143],"type":["chr"],"align":["left"]},{"label":["2020/男/人口（人）"],"name":[144],"type":["chr"],"align":["left"]},{"label":["2020/女/人口（人）"],"name":[145],"type":["chr"],"align":["left"]},{"label":["2020/総数/年齢，男女別割合（％）"],"name":[146],"type":["chr"],"align":["left"]},{"label":["2020/男/年齢，男女別割合（％）"],"name":[147],"type":["chr"],"align":["left"]},{"label":["2020/女/年齢，男女別割合（％）"],"name":[148],"type":["chr"],"align":["left"]},{"label":["2020/NA/人口性比"],"name":[149],"type":["chr"],"align":["left"]}],"data":[{"1":"01000_北海道","2":"総数","3":"2359183","4":"1244322","5":"1114861","6":"100","7":"52.743767651767584","8":"47.256232348232416","9":"111.61229964991151","10":"2498679","11":"1305473","12":"1193206","13":"100","14":"52.246527064901095","15":"47.753472935098905","16":"109.40885312343384","17":"2812335","18":"1468540","19":"1343795","20":"100","21":"52.217819000936949","22":"47.782180999063058","23":"109.28303796337984","24":"3068282","25":"1593845","26":"1474437","27":"100","28":"51.945844612718126","29":"48.054155387281874","30":"108.09854880201731","31":"3272012","32":"1695083","33":"1576929","34":"100","35":"51.805525163110644","36":"48.194474836889349","37":"107.49266453974782","38":"3518389","39":"1738623","40":"1779766","41":"100","42":"49.415314793219281","43":"50.584685206780719","44":"97.688291606874174","45":"4295567","46":"2169393","47":"2126174","48":"100","49":"50.503065136686267","50":"49.496934863313733","51":"102.03271228036841","52":"4773087","53":"2428833","54":"2344254","55":"100","56":"50.885998935280256","57":"49.114001064719744","58":"103.60792815113038","59":"5039206","60":"2544753","61":"2494453","62":"100","63":"50.49908656244655","64":"49.500913437553457","65":"102.01647415285034","66":"5171800","67":"2583159","68":"2588641","69":"100","70":"49.947001044123901","71":"50.052998955876092","72":"99.788228649704607","73":"5184287","74":"2552806","75":"2631481","76":"100","77":"49.241216776771815","78":"50.758783223228185","79":"97.010238721085202","80":"5338206","81":"2621285","82":"2716921","83":"100","84":"49.104230897046683","85":"50.895769102953317","86":"96.479985984134245","87":"5575989","88":"2737089","89":"2838900","90":"100","91":"49.080556217873472","92":"50.919443782126528","93":"96.413716580365644","94":"5679439","95":"2766296","96":"2913143","97":"100","98":"48.704381249257025","99":"51.295618750742975","100":"94.959155798393695","101":"5643647","102":"2722988","103":"2920659","104":"100","105":"48.229443139139867","106":"51.770556860860125","107":"93.231972647268989","108":"5692321","109":"2736844","110":"2955477","111":"100","112":"48.062997980468722","113":"51.937002019531278","114":"92.602446237950758","115":"5683062","116":"2719389","117":"2963673","118":"100","119":"47.793507502290161","120":"52.206492497709831","121":"91.757390238396752","122":"5627737","123":"2675033","124":"2952704","125":"100","126":"47.531825022465611","127":"52.468174977534389","128":"90.596043490983178","129":"5506419","130":"2603345","131":"2903074","132":"100","133":"47.258969406184463","134":"52.74103059381553","135":"89.675461252451711","136":"5381733","137":"2537089","138":"2844644","139":"100","140":"47.103278085415795","141":"52.896721914584212","142":"89.188278041118679","143":"5224614","144":"2465088","145":"2759526","146":"100","147":"47.084940000000003","148":"52.915059999999997","149":"89.330119999999994"},{"1":"01000_北海道","2":"0～4歳","3":"376676","4":"190044","5":"186632","6":"15.966374800089692","7":"8.0555005694768056","8":"7.910874230612885","9":"101.82819666509495","10":"405607","11":"204576","12":"201031","13":"16.232857441872287","14":"8.1873662043023536","15":"8.0454912375699319","16":"101.76340962339141","17":"445457","18":"224549","19":"220908","20":"15.839400355931993","21":"7.9844328645058287","22":"7.854967491426164","23":"101.64819743965813","24":"462550","25":"234131","26":"228419","27":"15.075211470132146","28":"7.6306871402302665","29":"7.4445243299018804","30":"102.50066763272758","31":"456318","32":"230441","33":"225877","34":"13.946157668800533","35":"7.0428221533142752","36":"6.9033355154862557","37":"102.02056871660238","38":"497219","39":"252253","40":"244966","41":"14.13200757505779","42":"7.1695597047398678","43":"6.9624478703179209","44":"102.97469852959186","45":"663628","46":"338126","47":"325502","48":"15.449431205090928","49":"7.8716605924592908","50":"7.5777706126316344","51":"103.87831718391899","52":"578015","53":"295270","54":"282745","55":"12.110004594558978","56":"6.1862080683640208","57":"5.9237965261949572","58":"104.42978655679144","59":"455213","60":"233417","61":"221796","62":"9.0334270914902071","63":"4.6320194094069578","64":"4.4014076820832493","65":"105.23949935977204","66":"451390","67":"230709","68":"220681","69":"8.7279090452066992","70":"4.4609033605321171","71":"4.2670056846745821","72":"104.54411571453819","73":"438566","74":"224166","75":"214400","76":"8.4595239422508826","77":"4.3239504294418882","78":"4.1355735128089943","79":"104.55503731343283","80":"462080","81":"236947","82":"225133","83":"8.657810747541852","84":"4.4395825034578404","85":"4.2182282440840098","86":"105.24756477282318","87":"407498","88":"208437","89":"199061","90":"7.3108774369785818","91":"3.7395456182153155","92":"3.5713318187632659","93":"104.71011398516032","94":"352073","95":"180019","96":"172054","97":"6.2004605352952309","98":"3.1703672394739506","99":"3.0300932958212807","100":"104.62936054959488","101":"291820","102":"149665","103":"142155","104":"5.1797625388965871","105":"2.6565319730791508","106":"2.5232305658174368","107":"105.28296577679295","108":"260027","109":"132618","110":"127409","111":"4.572733021622561","112":"2.3321682281514642","113":"2.2405647934710964","114":"104.08840819722312","115":"238264","116":"121481","117":"116783","118":"4.211988178488002","119":"2.1475192891536317","120":"2.0644688893343699","121":"104.02284579091135","122":"220680","123":"112055","124":"108625","125":"3.9261224310433382","126":"1.993572815889801","127":"1.9325496151535375","128":"103.15765247410818","129":"200977","130":"102766","131":"98211","132":"3.6557564107204863","133":"1.8693057578931991","134":"1.7864506528272872","135":"104.6379733431082","136":"186010","137":"95022","138":"90988","139":"3.4719636277232215","140":"1.7736300620048169","141":"1.6983335657184049","142":"104.43355167714424","143":"162373","144":"83004","145":"79369","146":"3.1433800000000001","147":"1.6068800000000001","148":"1.53651","149":"104.57987"},{"1":"01000_北海道","2":"5～9歳","3":"319370","4":"161814","5":"157556","6":"13.537313553039338","7":"6.8588998818658835","8":"6.6784136711734527","9":"102.70253116352282","10":"336438","11":"170315","12":"166123","13":"13.46463471298234","14":"6.8162016809682227","15":"6.6484330320141156","16":"102.52343143333553","17":"383080","18":"193478","19":"189602","20":"13.621421345607832","21":"6.8796213822322025","22":"6.7417999633756294","23":"102.04428223330977","24":"417967","25":"210495","26":"207472","27":"13.622183358635223","28":"6.8603537745226806","29":"6.7618295841125429","30":"101.45706408575614","31":"430141","32":"217615","33":"212526","34":"13.146126617436806","35":"6.6508292486731344","36":"6.4952973687636719","37":"102.39453055155604","38":"447774","39":"226133","40":"221641","41":"12.726676896727451","42":"6.4271744824122639","43":"6.2995024143151879","44":"102.02670083603664","45":"524173","46":"266014","47":"258159","48":"12.202882794375954","49":"6.1928746113651894","50":"6.0100081830107666","51":"103.04269849201462","52":"652105","53":"332268","54":"319837","55":"13.662265764962644","56":"6.9613539555633031","57":"6.7009118093993401","58":"103.88666727114125","59":"576230","60":"294325","61":"281905","62":"11.434936376881597","63":"5.8407018883530464","64":"5.5942344885285502","65":"104.40573952217946","66":"446543","67":"228280","68":"218263","69":"8.634189257125179","70":"4.4139371205383044","71":"4.2202521365868755","72":"104.58941735429275","73":"436424","74":"222706","75":"213718","76":"8.4182067852339202","77":"4.2957884083192157","78":"4.1224183769147045","79":"104.20554188229349","80":"427004","81":"218215","82":"208789","83":"8.0006055670952225","84":"4.0886084060657133","85":"3.9119971610295088","86":"104.51460565451245","87":"465277","88":"238711","89":"226566","90":"8.3474842115668864","91":"4.2826881699016788","92":"4.0647960416652094","93":"105.3604689141354","94":"405226","95":"207356","96":"197870","97":"7.1365535581414798","98":"3.6518071387373583","99":"3.4847464194041224","100":"104.7940567038965","101":"345826","102":"176765","103":"169061","104":"6.1383611807842211","105":"3.1375530299090375","106":"3.0008081508751832","107":"104.55693507077326","108":"294029","109":"150901","110":"143128","111":"5.1706788818648066","112":"2.6536859083705386","113":"2.5169929734942675","114":"105.43080319713822","115":"260721","116":"132985","117":"127736","118":"4.6089789892034476","119":"2.3508849340069289","120":"2.2580940551965192","121":"104.10925659172044","122":"238798","123":"122052","124":"116746","125":"4.2484601426875432","126":"2.171429649056106","127":"2.0770304936314372","128":"104.54490946156614","129":"219180","130":"111287","131":"107893","132":"3.9868676022714848","133":"2.0243021026279164","134":"1.9625654996435684","135":"103.14570917483061","136":"202269","137":"103549","138":"98720","139":"3.7754454653833038","140":"1.9327905042046765","141":"1.8426549611786271","142":"104.89161264181523","143":"189483","144":"96778","145":"92705","146":"3.6682000000000001","147":"1.8735299999999999","148":"1.7946800000000001","149":"104.39351000000001"},{"1":"01000_北海道","2":"10～14歳","3":"270012","4":"138018","5":"131994","6":"11.445148595933423","7":"5.8502456146894923","8":"5.5949029812439308","9":"104.56384381108232","10":"300821","11":"153388","12":"147433","13":"12.039201514080041","14":"6.1387637227511016","15":"5.9004377913289385","16":"104.0391228558057","17":"329334","18":"167701","19":"161633","20":"11.710340339966612","21":"5.9630520546094257","22":"5.7472882853571852","23":"103.75418386096899","24":"372110","25":"188761","26":"183349","27":"12.127633639932705","28":"6.1520094958677207","29":"5.9756241440649855","30":"102.9517477597369","31":"401194","32":"202527","33":"198667","34":"12.261437812614801","35":"6.1897042724353746","36":"6.0717335401794257","37":"101.94294976015141","38":"436104","39":"220621","40":"215483","41":"12.394991002984606","42":"6.2705118734739109","43":"6.1244791295106937","44":"102.38441083519349","45":"472030","46":"238681","47":"233349","48":"10.988980289769374","49":"5.5565553133115353","50":"5.4324249764578392","51":"102.28498943642354","52":"520095","53":"263869","54":"256226","55":"10.896521439075373","56":"5.5283250475535803","57":"5.3681963915217921","58":"102.98291352165667","59":"650036","60":"330838","61":"319198","62":"12.899571876998085","63":"6.5652803239240471","64":"6.3342915530740358","65":"103.64663938997111","66":"564190","67":"287232","68":"276958","69":"10.90896786418655","70":"5.5538110522448658","71":"5.3551568119416837","72":"103.70958773532448","73":"434497","74":"221689","75":"212808","76":"8.3810367751631034","77":"4.2761714388111614","78":"4.1048653363519421","79":"104.17324536671553","80":"423527","81":"216154","82":"207373","83":"7.9354583891840313","84":"4.0499922617818589","85":"3.8854661274021733","86":"104.23439888510076","87":"425549","88":"217367","89":"208182","90":"7.6347284708852516","91":"3.8997577800227812","92":"3.7349706908624705","93":"104.41200488034508","94":"460660","95":"236038","96":"224622","97":"8.1128179388623991","98":"4.1569342262258555","99":"3.9558837126365423","100":"105.08231606877332","101":"396605","102":"202723","103":"193882","104":"7.0396810422146556","105":"3.5983037529049855","106":"3.441377289309671","107":"104.55999009706936","108":"344617","109":"176055","110":"168562","111":"6.0602996447003656","112":"3.0960343045982146","113":"2.964265340102151","114":"104.44524863255063","115":"293367","116":"150621","117":"142746","118":"5.1860891110637342","119":"2.6626509730049075","120":"2.5234381380588271","121":"105.51679206422597","122":"259579","123":"132431","124":"127148","125":"4.6181753422503116","126":"2.3560826521003277","127":"2.2620926901499838","128":"104.15500047189103","129":"237155","130":"121300","131":"115855","132":"4.313831491088119","133":"2.2064378143787349","134":"2.1073936767093846","135":"104.69984031763843","136":"220017","137":"111816","138":"108201","139":"4.1067201842953605","140":"2.0870979248293087","141":"2.0196222594660518","142":"103.34100424210497","143":"203948","144":"104728","145":"99220","146":"3.9482300000000001","147":"2.0274299999999998","148":"1.9208000000000001","149":"105.5513"},{"1":"01000_北海道","2":"15～19歳","3":"239092","4":"125670","5":"113422","6":"10.134525384423336","7":"5.3268440811925144","8":"4.8076813032308223","9":"110.79861049884502","10":"252275","11":"131986","12":"120289","13":"10.096334903362937","14":"5.2822311309295831","15":"4.814103772433354","16":"109.72408117117939","17":"296826","18":"154310","19":"142516","20":"10.554432526708233","21":"5.4868996758920972","22":"5.0675328508161366","23":"108.27556204215666","24":"317212","25":"164046","26":"153166","27":"10.3384239127955","28":"5.3465098709962122","29":"4.9919140417992871","30":"107.10340414974601","31":"354389","32":"182608","33":"171781","34":"10.830966278096748","35":"5.5809325066824611","36":"5.2500337714142855","37":"106.30279250906678","38":"398031","39":"200381","40":"197650","41":"11.312876432935642","42":"5.6952485924666094","43":"5.6176278404690327","44":"101.38173539084241","45":"455200","46":"230778","47":"224422","48":"10.597173543848948","49":"5.3725714325623297","50":"5.2246021112866181","51":"102.83216440455926","52":"478640","53":"243826","54":"234814","55":"10.027996849804433","56":"5.1084037270190867","57":"4.9195931227853462","58":"103.83793129881522","59":"508799","60":"254493","61":"254306","62":"10.096808902037345","63":"5.0502599020559984","64":"5.0465489999813462","65":"100.0735334596903","66":"599110","67":"298657","68":"300453","69":"11.584167987934569","70":"5.7747206001778881","71":"5.8094473877566806","72":"99.402235957038215","73":"499412","74":"250272","75":"249140","76":"9.6331858170660691","77":"4.8275105139819612","78":"4.805675303084108","79":"100.45436300875011","80":"397272","81":"201906","82":"195366","83":"7.443528807343851","84":"3.7830331042096281","85":"3.660495703134222","86":"103.3475630355333","87":"410100","88":"209528","89":"200572","90":"7.357559636869178","91":"3.759119130928859","92":"3.5984405059403186","93":"104.46522944379075","94":"412479","95":"209552","96":"202927","97":"7.2642882616333591","98":"3.690481536761371","99":"3.5738067248719876","100":"103.264720810932","101":"443367","102":"226108","103":"217259","104":"7.8696997381363971","105":"4.0133841002838375","106":"3.8563156378525587","107":"104.07301883926559","108":"394609","109":"201737","110":"192872","111":"6.939439384869484","112":"3.547667902114283","113":"3.3917714827552006","114":"104.59631258036417","115":"341843","116":"175008","117":"166835","118":"6.0430391284410323","119":"3.0937599769198378","120":"2.949279151521194","121":"104.89885215931909","122":"292235","123":"150839","124":"141396","125":"5.199158911709036","126":"2.6835797597251498","127":"2.5155791519838857","128":"106.67840674417948","129":"258530","130":"132356","131":"126174","132":"4.7026411224347431","133":"2.407545617146841","134":"2.2950955052879021","135":"104.89958311538035","136":"239098","137":"123196","138":"115902","139":"4.462875971514257","140":"2.2995109460834899","141":"2.1633650254307661","142":"106.29324774378355","143":"221229","144":"113445","145":"107784","146":"4.2827700000000002","147":"2.19618","148":"2.0865900000000002","149":"105.25217000000001"},{"1":"01000_北海道","2":"20～24歳","3":"208337","4":"109351","5":"98986","6":"8.8308961195464697","7":"4.6351215653893743","8":"4.1957745541570963","9":"110.47117774230699","10":"215391","11":"112790","12":"102601","13":"8.6201949109909677","14":"4.5139851897742771","15":"4.1062097212166906","16":"109.93070242980087","17":"248258","18":"132268","19":"115990","20":"8.8274689892918161","21":"4.7031381396597487","22":"4.1243308496320674","23":"114.03396844555564","24":"278229","25":"145037","26":"133192","27":"9.0679083604440525","28":"4.7269775072825775","29":"4.3409308531614759","30":"108.89317676737342","31":"291584","32":"150654","33":"140930","34":"8.911496889668026","35":"4.6043426676911174","36":"4.3071542219769086","37":"106.89987937273824","38":"296266","39":"125878","40":"170388","41":"8.4205015420409737","42":"3.5777169608022312","43":"4.8427845812387433","44":"73.877268352231368","45":"414482","46":"212766","47":"201716","48":"9.6492479894586989","49":"4.9532474214203983","50":"4.6960005680383006","51":"105.47799877054868","52":"477693","53":"253023","54":"224670","55":"10.008156232604106","56":"5.3010902701990368","57":"4.7070659624050677","58":"112.61984243557217","59":"466201","60":"234547","61":"231654","62":"9.2514773160692378","63":"4.6544435770238408","64":"4.597033739045397","65":"101.24884526060418","66":"451306","67":"213227","68":"238079","69":"8.7262848524691599","70":"4.1228779148458949","71":"4.6034069376232649","72":"89.561448090759782","73":"514232","74":"238736","75":"275496","76":"9.9190496205167644","77":"4.6049919690017163","78":"5.3140576515150491","79":"86.656793565060838","80":"443148","81":"209711","82":"233437","83":"8.3030893290159202","84":"3.9292723114563479","85":"4.3738170175595714","86":"89.836229903571422","87":"376820","88":"185415","89":"191405","90":"6.7604867650939857","91":"3.3265104122655416","92":"3.4339763528284446","93":"96.870510174760327","94":"376187","95":"183074","96":"193113","97":"6.6251392392802266","98":"3.2241697376357723","99":"3.4009695016444548","100":"94.801489283476513","101":"364934","102":"176755","103":"188179","104":"6.47752540048553","105":"3.1373755313640816","106":"3.3401498691214475","107":"93.929184446723596","108":"415477","109":"207782","110":"207695","111":"7.3064158630629779","112":"3.6539729055012709","113":"3.6524429575617057","114":"100.04188834589181","115":"358051","116":"179696","117":"178355","118":"6.3295612400354546","119":"3.1766335985359939","120":"3.1529276414994611","121":"100.7518712679768","122":"314753","123":"159089","124":"155664","125":"5.5997771140936372","126":"2.8303556798633935","127":"2.7694214342302437","128":"102.20025182444239","129":"264185","130":"133097","131":"131088","132":"4.8055051442015344","133":"2.4210243510335241","134":"2.3844807931680099","135":"101.53255828145977","136":"234274","137":"117965","138":"116309","139":"4.3728337558261918","140":"2.2018718850834356","141":"2.1709618707427567","142":"101.42379351554909","143":"220409","144":"112616","145":"107793","146":"4.2668999999999997","147":"2.1801300000000001","148":"2.08677","149":"104.47432000000001"},{"1":"01000_北海道","2":"25～29歳","3":"175257","4":"96638","5":"78619","6":"7.4287157884742303","7":"4.096248574188607","8":"3.3324672142856233","9":"122.91939607474021","10":"186159","11":"99237","12":"86922","13":"7.450296736795722","14":"3.9715785821227936","15":"3.4787181546729293","16":"114.16787464623455","17":"214713","18":"116156","19":"98557","20":"7.6346879016902323","21":"4.1302334181383085","22":"3.5044544835519242","23":"117.85667177369442","24":"234977","25":"126338","26":"108639","27":"7.6582595732725993","28":"4.1175485173787809","29":"3.5407110558938193","30":"116.2915711668922","31":"259621","32":"138081","33":"121540","34":"7.9346319893838571","35":"4.2200820416149396","36":"3.7145499477689166","37":"113.60951127200923","38":"251138","39":"113300","40":"137838","41":"7.1378690645065106","42":"3.2202238012908753","43":"3.9176452632156362","44":"82.1979425122245","45":"334066","46":"160615","47":"173451","48":"7.7771427440673166","49":"3.7391586747480199","50":"4.0379840693192968","51":"92.599639091155424","52":"417087","53":"217471","54":"199616","55":"8.7383986338258008","56":"4.5562395598441832","57":"4.1821590739816177","58":"108.94467377364541","59":"456601","60":"236265","61":"220336","62":"9.0609711133063424","63":"4.6885362495599505","64":"4.3724348637463919","65":"107.22941325974875","66":"443552","67":"218388","68":"225164","69":"8.576356394292123","70":"4.2226690900653541","71":"4.3536873042267681","72":"96.990637935016252","73":"432539","74":"204712","75":"227827","76":"8.3432688043698207","77":"3.9487011425100498","78":"4.39456766185977","79":"89.854143714309544","80":"515030","81":"246916","82":"268114","83":"9.6499140177165845","84":"4.626367725372325","85":"5.0235462923442604","86":"92.093661651387094","87":"460048","88":"223093","89":"236955","90":"8.2536712894962001","91":"4.0024873252086213","92":"4.2511839642875788","93":"94.1499440822097","94":"372732","95":"181763","96":"190969","97":"6.5642922241741415","98":"3.2010813333509449","99":"3.3632108908231957","100":"95.179322298383511","101":"355206","102":"169103","103":"186103","104":"6.3048548159526456","105":"3.0015536447639968","106":"3.3033011711886493","107":"90.86527353132405","108":"359523","109":"172479","110":"187044","111":"6.322430725012433","112":"3.0331481685995594","113":"3.2892825564128736","114":"92.2130621671906","115":"398170","116":"195963","117":"202207","118":"7.0387777130769562","119":"3.4641987015287423","120":"3.574579011548213","121":"96.912075249620429","122":"343983","123":"169948","124":"174035","125":"6.1198086468985888","126":"3.0235483728065673","127":"3.096260274092022","128":"97.651621800212595","129":"289677","130":"142807","131":"146870","132":"5.2692026937822654","133":"2.5976485157294644","134":"2.6715541780528014","135":"97.233607952611152","136":"247587","137":"123076","138":"124511","139":"4.6213271259454292","140":"2.2972710899718471","141":"2.3240560359735825","142":"98.847491386303219","143":"224130","144":"112587","145":"111543","146":"4.3389300000000004","147":"2.17957","148":"2.1593599999999999","149":"100.93595999999999"},{"1":"01000_北海道","2":"30～34歳","3":"157450","4":"86287","5":"71163","6":"6.673920590306051","7":"3.6574949887312682","8":"3.0164256015747823","9":"121.25261723086436","10":"152114","11":"83238","12":"68876","13":"6.0877767812512129","14":"3.3312802484832988","15":"2.7564965327679145","16":"120.85196585167549","17":"180075","18":"97392","19":"82683","20":"6.4030423118156268","21":"3.4630298310834235","22":"2.9400124807322028","23":"117.78963027466347","24":"201819","25":"108882","26":"92937","27":"6.5775896739608681","28":"3.5486307972995963","29":"3.0289588766612718","30":"117.15678362761869","31":"220332","32":"119732","33":"100600","34":"6.7338671967403396","35":"3.6592931902770109","36":"3.0745740064633291","37":"119.01789264413519","38":"226494","39":"106578","40":"119916","41":"6.437434860102166","42":"3.0291704527270862","43":"3.4082644073750799","44":"88.87721404983489","45":"276842","46":"133284","47":"143558","48":"6.4449532474214202","49":"3.1028859372108157","50":"3.3420673102106049","51":"92.84331071761936","52":"331041","53":"160057","54":"170984","55":"6.9356470523903342","56":"3.3533576211539948","57":"3.5822894312363385","58":"93.609343564310109","59":"406635","60":"210205","61":"196430","62":"8.0694260167177134","63":"4.1713912866431739","64":"3.898034730074539","65":"107.0126762714453","66":"444456","67":"229144","68":"215312","69":"8.5938358018484866","70":"4.4306431029815538","71":"4.1631926988669319","72":"106.42416586163334","73":"422693","74":"206700","75":"215993","76":"8.1533487632918469","77":"3.9870477849702377","78":"4.1663009783216092","79":"95.69754575379757","80":"426829","81":"205196","82":"221633","83":"7.9973266611031431","84":"3.8446765368607116","85":"4.1526501242424327","86":"92.583685642481043","87":"520637","88":"252957","89":"267680","90":"9.3406919694236983","91":"4.5382741113472731","92":"4.8024178580764243","93":"94.499775851763303","94":"455435","95":"221540","96":"233895","97":"8.0207989362779415","98":"3.9016057095809833","99":"4.1191932266969582","100":"94.717715214091797","101":"361257","102":"175788","103":"185469","104":"6.4122591855053273","105":"3.1202114220668675","106":"3.2920477634384588","107":"94.780259773870569","108":"359859","109":"173236","110":"186623","111":"6.3283394894686831","112":"3.0464604742346215","113":"3.2818790152340607","114":"92.826714820788439","115":"358152","116":"172198","117":"185954","118":"6.3313466998868266","119":"3.044085301847014","120":"3.2872613980398127","121":"92.602471578992663","122":"398071","123":"196201","124":"201870","125":"7.0820893703455363","126":"3.4906160372173916","127":"3.5914733331281439","128":"97.191757071382582","129":"338063","130":"166953","131":"171110","132":"6.1493403696811066","133":"3.0368624272380291","134":"3.1124779424430775","135":"97.570568640056095","136":"287674","137":"141876","138":"145798","139":"5.3695697255075006","140":"2.6481818807959776","141":"2.7213878447115225","142":"97.309976817240297","143":"247463","144":"124006","145":"123457","146":"4.7906399999999998","147":"2.40063","148":"2.3900100000000002","149":"100.44468999999999"},{"1":"01000_北海道","2":"35～39歳","3":"143281","4":"78926","5":"64355","6":"6.0733313185115358","7":"3.3454801937789478","8":"2.7278511247325872","9":"122.64159738948021","10":"139774","11":"76040","12":"63734","13":"5.5939158251219947","14":"3.0432080311236458","15":"2.5507077939983485","16":"119.30837543540341","17":"148585","18":"81969","19":"66616","20":"5.2833321777099806","21":"2.9146243246270447","22":"2.3687078530829364","23":"123.04701573195629","24":"169407","25":"91739","26":"77668","27":"5.5212330548495867","28":"2.9899142256155073","29":"2.5313188292340794","30":"118.11685636298088","31":"190365","32":"103303","33":"87062","34":"5.8180047787315274","35":"3.1571840814083623","36":"2.6608206973231647","37":"118.65452206473547","38":"205518","39":"104367","40":"101151","41":"5.8412529143309628","42":"2.9663291921387884","43":"2.874923722192174","44":"103.17940504789868","45":"249904","46":"125239","47":"124665","48":"5.8178296513664929","49":"2.915596259793714","50":"2.9022333915727794","51":"100.46043396302089","52":"272037","53":"131605","54":"140432","55":"5.6994529897840724","56":"2.7572591622482707","57":"2.9421938275358017","58":"93.714395579355141","59":"325295","60":"156892","61":"168403","62":"6.4552828362245958","63":"3.1134269962371057","64":"3.34185583998749","65":"93.164611081750323","66":"395352","67":"204354","68":"190998","69":"7.6443791329904478","70":"3.9513128891295102","71":"3.6930662438609381","72":"106.99274337951184","73":"420657","74":"214338","75":"206319","76":"8.114076246164613","77":"4.1343775913640588","78":"3.9796986548005542","79":"103.88669972227473","80":"408639","81":"200120","82":"208519","83":"7.6565078039836267","84":"3.7495695264847542","85":"3.9069382774988735","86":"95.972069691490944","87":"424505","88":"204233","89":"220272","90":"7.6159981800759589","91":"3.6641221100139059","92":"3.9518760700620521","93":"92.718547977046555","94":"511932","95":"247804","96":"264128","97":"9.0157841207782425","98":"4.3641486921414003","99":"4.6516354286368422","100":"93.819663193603091","101":"442849","102":"214554","103":"228295","104":"7.8605053135076925","105":"3.8083022814420477","106":"4.0522030320656448","107":"93.981033312161898","108":"362359","109":"177080","110":"185279","111":"6.3723035107205384","112":"3.1140595533114759","113":"3.2582439574090634","114":"95.574781815532248","115":"357383","116":"172185","117":"185198","118":"6.3177524560679652","119":"3.0438554901829762","120":"3.273896965884989","121":"92.973466236136446","122":"357532","123":"172188","124":"185344","125":"6.3608591853171417","126":"3.0634002590016784","127":"3.2974589263154637","128":"92.901847375690608","129":"393511","130":"194191","131":"199320","132":"7.1579352907995917","133":"3.5323195846003372","134":"3.6256157061992536","135":"97.426750953241012","136":"337369","137":"166514","138":"170855","139":"6.2971501377418182","140":"3.1080616714515594","141":"3.1890884662902588","142":"97.459249070849552","143":"287154","144":"142111","145":"145043","146":"5.5590200000000003","147":"2.7511299999999999","148":"2.80789","149":"97.978530000000006"},{"1":"01000_北海道","2":"40～44歳","3":"127242","4":"70818","5":"56424","6":"5.3934773182071929","7":"3.0018018949780494","8":"2.3916754232291435","9":"125.51042109740536","10":"126620","11":"68948","12":"57672","13":"5.0674776551930041","14":"2.7593780553644547","15":"2.3080995998285494","16":"119.55194895269801","17":"132094","18":"71915","19":"60179","20":"4.6969511100206764","21":"2.5571277959418062","22":"2.1398233140788703","23":"119.50181957161136","24":"136412","25":"75205","26":"61207","27":"4.4458755746701248","28":"2.4510458947384888","29":"1.9948296799316361","30":"122.86993317757774","31":"156820","32":"84964","33":"71856","34":"4.7927902156419417","35":"2.5967008537291281","36":"2.1960893619128128","37":"118.2420396348252","38":"180418","39":"94652","40":"85766","41":"5.127858232844634","42":"2.6902085016750563","43":"2.4376497311695777","44":"110.3607490147611","45":"212512","46":"110617","47":"101895","48":"4.9473342358313435","49":"2.5751923240332584","50":"2.372141911798086","51":"108.5597919426861","52":"244333","53":"123275","54":"121058","55":"5.1190258948338343","56":"2.5827371545621793","57":"2.536288740271655","58":"101.83135356605924","59":"265390","60":"128209","61":"137181","62":"5.2665042865880061","63":"2.5442301822945916","64":"2.7222741042934144","65":"93.459735677681309","66":"314889","67":"151735","68":"163154","69":"6.0885765110793146","70":"2.9338914884566303","71":"3.1546850226226844","72":"93.001090993785013","73":"373007","74":"190592","75":"182415","76":"7.1949527485650391","77":"3.676339677953786","78":"3.5186130706112526","79":"104.48263574815668","80":"404726","81":"205156","82":"199570","83":"7.5831914660007422","84":"3.8439270726339507","85":"3.7392643933667915","86":"102.7990178884602","87":"401796","88":"196248","89":"205548","90":"7.2085784731906566","91":"3.5208640907493356","92":"3.6877143824413214","93":"95.475509370074136","94":"414669","95":"198446","96":"216223","97":"7.3028569919032087","98":"3.4948905238038632","99":"3.8079664680993455","100":"91.778395452842659","101":"495423","102":"238040","103":"257383","104":"8.7936861637576733","105":"4.2251753641249525","106":"4.5685107996327199","107":"92.484740639436168","108":"441130","109":"214022","110":"227108","111":"7.7575394779325233","112":"3.7637071025459039","113":"3.9938323753866198","114":"94.237983690578929","115":"358952","116":"174914","117":"184038","118":"6.3454889561353172","119":"3.0920982618106403","120":"3.2533906943246773","121":"95.04232821482519","122":"354700","123":"170573","124":"184127","125":"6.3104750149133233","126":"3.0346677606958283","127":"3.275807254217495","128":"92.638776496657186","129":"354218","130":"170760","131":"183458","132":"6.4431985963199239","133":"3.1061114689473439","134":"3.3370871273725804","135":"93.078524784964415","136":"391243","137":"193093","138":"198150","139":"7.3027335390641168","140":"3.6041711347129723","141":"3.6985624043511445","142":"97.447893010345695","143":"336549","144":"166377","145":"170172","146":"6.51525","147":"3.2208899999999998","148":"3.2943600000000002","149":"97.769900000000007"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</section>
<section id="converting-to-tidy-data" class="level3">
<h3 class="anchored" data-anchor-id="converting-to-tidy-data">3. Converting to tidy data</h3>
<p>Finally, let’s tidy the data and convert it into tidy data. Since tidy data is usually in long format, tidyr’s <code>pivot_longer()</code> function is useful. With <code>pivot_longer()</code>, the <code>names_sep</code> argument lets us expand multiple pieces of information contained in the column names into separate columns.</p>
<p>In this Excel file, the year and sex information—besides the axes (dimensions) of region and age group—are included in the column names, so we expand them using the <code>names_sep = "/"</code> argument. Furthermore, since year and sex are stored in the 1st and 2nd positions, respectively, of the parts separated by <code>"/"</code>, we specify <code>"year"</code> and <code>"sex"</code> for the 1st and 2nd positions of the <code>names_to</code> argument.</p>
<p>Furthermore, the 3rd position of the parts separated by <code>"/"</code>—<code>人口（人）</code> (population count), <code>年齢，男女別割合（％）</code> (percentage by age and sex), and <code>人口性比</code> (sex ratio)—correspond to observed values, and in this case, we want to keep them as column names rather than expanding them into columns. This can be achieved by specifying <code>".value"</code> for the 3rd position of the <code>names_to</code> argument of <code>pivot_longer()</code>.</p>
<p>Therefore, the code below converts the data into long-format data.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">data <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>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(</span>
<span id="cb11-3">    <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;">c</span>(region, age_class),</span>
<span id="cb11-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sex"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".value"</span>),</span>
<span id="cb11-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>,</span>
<span id="cb11-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_transform =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb11-7">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sex =</span> \(x)</span>
<span id="cb11-8">        x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-9">          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na_if</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NA"</span>)</span>
<span id="cb11-10">    )</span>
<span id="cb11-11">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert the population, percentage-by-age-and-sex, and sex-ratio columns to numeric</span></span>
<span id="cb11-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</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;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">人口（人）</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">年齢，男女別割合（％）</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, 人口性比), \(x) {</span>
<span id="cb11-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_number</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>)</span>
<span id="cb11-15">  })) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">relocate</span>(year, region, sex, age_class)</span>
<span id="cb11-17"></span>
<span id="cb11-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["year"],"name":[1],"type":["chr"],"align":["left"]},{"label":["region"],"name":[2],"type":["chr"],"align":["left"]},{"label":["sex"],"name":[3],"type":["chr"],"align":["left"]},{"label":["age_class"],"name":[4],"type":["chr"],"align":["left"]},{"label":["人口（人）"],"name":[5],"type":["dbl"],"align":["right"]},{"label":["年齢，男女別割合（％）"],"name":[6],"type":["dbl"],"align":["right"]},{"label":["人口性比"],"name":[7],"type":["dbl"],"align":["right"]}],"data":[{"1":"1920","2":"01000_北海道","3":"総数","4":"総数","5":"2359183","6":"100.00000","7":"NA"},{"1":"1920","2":"01000_北海道","3":"男","4":"総数","5":"1244322","6":"52.74377","7":"NA"},{"1":"1920","2":"01000_北海道","3":"女","4":"総数","5":"1114861","6":"47.25623","7":"NA"},{"1":"1920","2":"01000_北海道","3":"NA","4":"総数","5":"NA","6":"NA","7":"111.6123"},{"1":"1925","2":"01000_北海道","3":"総数","4":"総数","5":"2498679","6":"100.00000","7":"NA"},{"1":"1925","2":"01000_北海道","3":"男","4":"総数","5":"1305473","6":"52.24653","7":"NA"},{"1":"1925","2":"01000_北海道","3":"女","4":"総数","5":"1193206","6":"47.75347","7":"NA"},{"1":"1925","2":"01000_北海道","3":"NA","4":"総数","5":"NA","6":"NA","7":"109.4089"},{"1":"1930","2":"01000_北海道","3":"総数","4":"総数","5":"2812335","6":"100.00000","7":"NA"},{"1":"1930","2":"01000_北海道","3":"男","4":"総数","5":"1468540","6":"52.21782","7":"NA"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>Looking at <code>data</code>, we can see that the <code>sex</code> column is always <code>NA</code> for <code>人口性比</code> (sex ratio). So, in the code below, we split <code>data</code> into <code>data_population</code>, which contains <code>人口（人）</code> (population count) and <code>年齢，男女別割合（％）</code> (percentage by age and sex), and <code>data_sex_ratio</code>, which contains <code>人口性比</code> (sex ratio). Doing this makes the meaning of each dataset clearer.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">data_population <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>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(sex) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>人口性比)</span>
<span id="cb12-4"></span>
<span id="cb12-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data_population, <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 class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["year"],"name":[1],"type":["chr"],"align":["left"]},{"label":["region"],"name":[2],"type":["chr"],"align":["left"]},{"label":["sex"],"name":[3],"type":["chr"],"align":["left"]},{"label":["age_class"],"name":[4],"type":["chr"],"align":["left"]},{"label":["人口（人）"],"name":[5],"type":["dbl"],"align":["right"]},{"label":["年齢，男女別割合（％）"],"name":[6],"type":["dbl"],"align":["right"]}],"data":[{"1":"1920","2":"01000_北海道","3":"総数","4":"総数","5":"2359183","6":"100.00000"},{"1":"1920","2":"01000_北海道","3":"男","4":"総数","5":"1244322","6":"52.74377"},{"1":"1920","2":"01000_北海道","3":"女","4":"総数","5":"1114861","6":"47.25623"},{"1":"1925","2":"01000_北海道","3":"総数","4":"総数","5":"2498679","6":"100.00000"},{"1":"1925","2":"01000_北海道","3":"男","4":"総数","5":"1305473","6":"52.24653"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">data_sex_ratio <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>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</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;">c</span>(sex, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">人口（人）</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">年齢，男女別割合（％）</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>))</span>
<span id="cb13-4"></span>
<span id="cb13-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data_sex_ratio, <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 class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["year"],"name":[1],"type":["chr"],"align":["left"]},{"label":["region"],"name":[2],"type":["chr"],"align":["left"]},{"label":["age_class"],"name":[3],"type":["chr"],"align":["left"]},{"label":["人口性比"],"name":[4],"type":["dbl"],"align":["right"]}],"data":[{"1":"1920","2":"01000_北海道","3":"総数","4":"111.6123"},{"1":"1925","2":"01000_北海道","3":"総数","4":"109.4089"},{"1":"1930","2":"01000_北海道","3":"総数","4":"109.2830"},{"1":"1935","2":"01000_北海道","3":"総数","4":"108.0985"},{"1":"1940","2":"01000_北海道","3":"総数","4":"107.4927"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</section>
</section>
<section id="tidying-an-excel-file-with-tidyxl-and-unpivotr" class="level2">
<h2 class="anchored" data-anchor-id="tidying-an-excel-file-with-tidyxl-and-unpivotr">Tidying an Excel file with tidyxl and unpivotr</h2>
<p>Next, I’ll introduce how to tidy an Excel file using the <a href="https://nacnudus.github.io/tidyxl/">tidyxl</a> and <a href="https://nacnudus.github.io/unpivotr/">unpivotr</a> packages.</p>
<p>With the readxl package, we needed to go through many steps: creating column names, reading the data, and converting it to tidy data. With the tidyxl and unpivotr packages, by using long-format data that expands the Excel file’s table into “values by row and column,” we can tidy data more flexibly and efficiently than with readxl.</p>
<p>First, let’s load the packages we’ll need.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install packages as needed</span></span>
<span id="cb14-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install.packages("pak")</span></span>
<span id="cb14-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pak::pak("tidyxl")</span></span>
<span id="cb14-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pak::pak("unpivotr")</span></span>
<span id="cb14-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pak::pak("tidyverse")</span></span>
<span id="cb14-6"></span>
<span id="cb14-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyxl)</span>
<span id="cb14-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(unpivotr)</span>
<span id="cb14-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div>
</div>
<section id="reading-the-excel-file" class="level3">
<h3 class="anchored" data-anchor-id="reading-the-excel-file">1. Reading the Excel file</h3>
<p>Let’s read the Excel file using the <code>xlsx_cells()</code> function from the tidyxl package. This function lets us retrieve the Excel file’s cell information by row and column.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">cells <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;">xlsx_cells</span>(file, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sheets =</span> sheet)</span>
<span id="cb15-2"></span>
<span id="cb15-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(cells, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["sheet"],"name":[1],"type":["chr"],"align":["left"]},{"label":["address"],"name":[2],"type":["chr"],"align":["left"]},{"label":["row"],"name":[3],"type":["int"],"align":["right"]},{"label":["col"],"name":[4],"type":["int"],"align":["right"]},{"label":["is_blank"],"name":[5],"type":["lgl"],"align":["right"]},{"label":["content"],"name":[6],"type":["chr"],"align":["left"]},{"label":["data_type"],"name":[7],"type":["chr"],"align":["left"]},{"label":["error"],"name":[8],"type":["chr"],"align":["left"]},{"label":["logical"],"name":[9],"type":["lgl"],"align":["right"]},{"label":["numeric"],"name":[10],"type":["dbl"],"align":["right"]},{"label":["date"],"name":[11],"type":["dttm"],"align":["right"]},{"label":["character"],"name":[12],"type":["chr"],"align":["left"]},{"label":["character_formatted"],"name":[13],"type":["list"],"align":["right"]},{"label":["formula"],"name":[14],"type":["chr"],"align":["left"]},{"label":["is_array"],"name":[15],"type":["lgl"],"align":["right"]},{"label":["formula_ref"],"name":[16],"type":["chr"],"align":["left"]},{"label":["formula_group"],"name":[17],"type":["int"],"align":["right"]},{"label":["comment"],"name":[18],"type":["chr"],"align":["left"]},{"label":["height"],"name":[19],"type":["dbl"],"align":["right"]},{"label":["width"],"name":[20],"type":["dbl"],"align":["right"]},{"label":["row_outline_level"],"name":[21],"type":["dbl"],"align":["right"]},{"label":["col_outline_level"],"name":[22],"type":["dbl"],"align":["right"]},{"label":["style_format"],"name":[23],"type":["chr"],"align":["left"]},{"label":["local_format_id"],"name":[24],"type":["int"],"align":["right"]}],"data":[{"1":"da03","2":"A1","3":"1","4":"1","5":"FALSE","6":"0","7":"character","8":"NA","9":"NA","10":"NA","11":"<NA>","12":"国勢調査（総務省統計局）　時系列データ　男女，年齢，配偶関係","13":"<tibble[,14]>","14":"NA","15":"FALSE","16":"NA","17":"NA","18":"NA","19":"19.5","20":"16.750","21":"1","22":"1","23":"標準 2","24":"73"},{"1":"da03","2":"B1","3":"1","4":"2","5":"TRUE","6":"NA","7":"blank","8":"NA","9":"NA","10":"NA","11":"<NA>","12":"NA","13":"<NULL>","14":"NA","15":"FALSE","16":"NA","17":"NA","18":"NA","19":"19.5","20":"19.875","21":"1","22":"1","23":"標準 2","24":"74"},{"1":"da03","2":"C1","3":"1","4":"3","5":"TRUE","6":"NA","7":"blank","8":"NA","9":"NA","10":"NA","11":"<NA>","12":"NA","13":"<NULL>","14":"NA","15":"FALSE","16":"NA","17":"NA","18":"NA","19":"19.5","20":"10.000","21":"1","22":"1","23":"標準 2","24":"74"},{"1":"da03","2":"D1","3":"1","4":"4","5":"TRUE","6":"NA","7":"blank","8":"NA","9":"NA","10":"NA","11":"<NA>","12":"NA","13":"<NULL>","14":"NA","15":"FALSE","16":"NA","17":"NA","18":"NA","19":"19.5","20":"10.000","21":"1","22":"1","23":"標準 2","24":"74"},{"1":"da03","2":"E1","3":"1","4":"5","5":"TRUE","6":"NA","7":"blank","8":"NA","9":"NA","10":"NA","11":"<NA>","12":"NA","13":"<NULL>","14":"NA","15":"FALSE","16":"NA","17":"NA","18":"NA","19":"19.5","20":"10.000","21":"1","22":"1","23":"標準 2","24":"74"},{"1":"da03","2":"F1","3":"1","4":"6","5":"TRUE","6":"NA","7":"blank","8":"NA","9":"NA","10":"NA","11":"<NA>","12":"NA","13":"<NULL>","14":"NA","15":"FALSE","16":"NA","17":"NA","18":"NA","19":"19.5","20":"10.000","21":"1","22":"1","23":"標準 2","24":"74"},{"1":"da03","2":"G1","3":"1","4":"7","5":"TRUE","6":"NA","7":"blank","8":"NA","9":"NA","10":"NA","11":"<NA>","12":"NA","13":"<NULL>","14":"NA","15":"FALSE","16":"NA","17":"NA","18":"NA","19":"19.5","20":"10.000","21":"1","22":"1","23":"標準 2","24":"74"},{"1":"da03","2":"H1","3":"1","4":"8","5":"TRUE","6":"NA","7":"blank","8":"NA","9":"NA","10":"NA","11":"<NA>","12":"NA","13":"<NULL>","14":"NA","15":"FALSE","16":"NA","17":"NA","18":"NA","19":"19.5","20":"10.000","21":"1","22":"1","23":"標準 2","24":"74"},{"1":"da03","2":"I1","3":"1","4":"9","5":"TRUE","6":"NA","7":"blank","8":"NA","9":"NA","10":"NA","11":"<NA>","12":"NA","13":"<NULL>","14":"NA","15":"FALSE","16":"NA","17":"NA","18":"NA","19":"19.5","20":"10.000","21":"1","22":"1","23":"標準 2","24":"74"},{"1":"da03","2":"J1","3":"1","4":"10","5":"TRUE","6":"NA","7":"blank","8":"NA","9":"NA","10":"NA","11":"<NA>","12":"NA","13":"<NULL>","14":"NA","15":"FALSE","16":"NA","17":"NA","18":"NA","19":"19.5","20":"10.000","21":"1","22":"1","23":"標準 2","24":"74"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>By applying unpivotr’s <code>behead()</code> function to the <code>cells</code> we read, we can add axis (dimension) information to the data. Specifically, we add this information by specifying the direction <code>direction</code> in which the axis (dimension) information lies relative to the data. In particular, specifying something like <code>direction = "up-left"</code> lets us handle blank cells and merged cells, which is very convenient.</p>
<p>To use the <code>behead()</code> function, you need to have already worked out the table structure beforehand. For details on how to specify the <code>direction</code> relative to the data, the following is a helpful reference:</p>
<ul>
<li><a href="https://nacnudus.github.io/unpivotr/reference/direction.html">Directions from data cells to headers</a></li>
</ul>
<p>In the code below, based on the table structure we worked out above, we create <code>data2</code>, which has the same information as the <code>data</code> we created with readxl. You can see that we’re able to read the data with less code than when using readxl.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">data2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cells <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Exclude the first 10 rows, which are the explanatory section</span></span>
<span id="cb16-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(row <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-4"></span>
<span id="cb16-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Column 1 represents region (on the left side, as seen from the data)</span></span>
<span id="cb16-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">behead</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"region"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-7"></span>
<span id="cb16-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Column 2 represents age group (on the left side, as seen from the data)</span></span>
<span id="cb16-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">behead</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age_class"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-10"></span>
<span id="cb16-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row 1 represents the calendar year (up-left side, as seen from the data)</span></span>
<span id="cb16-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(row <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(character, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+年$"</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;"># Remove Japanese-era years</span></span>
<span id="cb16-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">behead</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"up-left"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-14"></span>
<span id="cb16-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row 2 represents the value type (up-left side, as seen from the data)</span></span>
<span id="cb16-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">behead</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"up-left"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value_type"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-17"></span>
<span id="cb16-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row 3 represents sex (up side, as seen from the data)</span></span>
<span id="cb16-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">behead</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"up"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sex"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-20"></span>
<span id="cb16-21">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row 5 represents the unit (up side, as seen from the data)</span></span>
<span id="cb16-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">behead</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"up"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Assign a placeholder name to the blank row 4 (to be removed later)</span></span>
<span id="cb16-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">behead</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"up"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value_unit"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-24"></span>
<span id="cb16-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(year, region, age_class, sex, value_type, value_unit, numeric)</span>
<span id="cb16-26"></span>
<span id="cb16-27"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data2, <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 class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["year"],"name":[1],"type":["chr"],"align":["left"]},{"label":["region"],"name":[2],"type":["chr"],"align":["left"]},{"label":["age_class"],"name":[3],"type":["chr"],"align":["left"]},{"label":["sex"],"name":[4],"type":["chr"],"align":["left"]},{"label":["value_type"],"name":[5],"type":["chr"],"align":["left"]},{"label":["value_unit"],"name":[6],"type":["chr"],"align":["left"]},{"label":["numeric"],"name":[7],"type":["dbl"],"align":["right"]}],"data":[{"1":"1920年","2":"01000_北海道","3":"総数","4":"総数","5":"人口","6":"（人）","7":"2359183"},{"1":"1920年","2":"01000_北海道","3":"総数","4":"男","5":"人口","6":"（人）","7":"1244322"},{"1":"1920年","2":"01000_北海道","3":"総数","4":"女","5":"人口","6":"（人）","7":"1114861"},{"1":"1920年","2":"01000_北海道","3":"0～4歳","4":"総数","5":"人口","6":"（人）","7":"376676"},{"1":"1920年","2":"01000_北海道","3":"0～4歳","4":"男","5":"人口","6":"（人）","7":"190044"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</section>
<section id="converting-to-tidy-data-1" class="level3">
<h3 class="anchored" data-anchor-id="converting-to-tidy-data-1">2. Converting to tidy data</h3>
<p>The <code>data2</code> we just created is long-format data, but the value type <code>value_type</code> (population, percentage by age and sex, or sex ratio) is still expanded across columns, so it’s not necessarily in a format well-suited for data analysis.</p>
<p>So, let’s use tidyr’s <code>pivot_wider()</code> function to expand the value type <code>value_type</code> into rows. In the code below, we combine the <code>value_type</code> and <code>value_unit</code> columns before converting to wide-format data. This gives us data that is nearly equivalent to the <code>data</code> we created using readxl.</p>
<p>To avoid repetition, I’ll skip the details, but as with readxl, if we split the data into one containing <code>人口（人）</code> (population count) and <code>年齢，男女別割合（％）</code> (percentage by age and sex), and one containing sex ratio, the data tidying is complete.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">data2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create the value_type column, same as when using readxl</span></span>
<span id="cb17-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value_unit =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(value_unit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, value_unit)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unite</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value_type"</span>, value_type, value_unit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-5"></span>
<span id="cb17-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove duplicates</span></span>
<span id="cb17-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_head</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(year, region, age_class, sex, value_type)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-8"></span>
<span id="cb17-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expand the value type into columns to create wide-format data</span></span>
<span id="cb17-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> value_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> numeric)</span>
<span id="cb17-11"></span>
<span id="cb17-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["year"],"name":[1],"type":["chr"],"align":["left"]},{"label":["region"],"name":[2],"type":["chr"],"align":["left"]},{"label":["age_class"],"name":[3],"type":["chr"],"align":["left"]},{"label":["sex"],"name":[4],"type":["chr"],"align":["left"]},{"label":["人口（人）"],"name":[5],"type":["dbl"],"align":["right"]},{"label":["年齢，男女別割合（％）"],"name":[6],"type":["dbl"],"align":["right"]},{"label":["人口性比"],"name":[7],"type":["dbl"],"align":["right"]}],"data":[{"1":"1920年","2":"01000_北海道","3":"総数","4":"総数","5":"2359183","6":"100.000000","7":"NA"},{"1":"1920年","2":"01000_北海道","3":"総数","4":"男","5":"1244322","6":"52.743768","7":"NA"},{"1":"1920年","2":"01000_北海道","3":"総数","4":"女","5":"1114861","6":"47.256232","7":"NA"},{"1":"1920年","2":"01000_北海道","3":"0～4歳","4":"総数","5":"376676","6":"15.966375","7":"NA"},{"1":"1920年","2":"01000_北海道","3":"0～4歳","4":"男","5":"190044","6":"8.055501","7":"NA"},{"1":"1920年","2":"01000_北海道","3":"0～4歳","4":"女","5":"186632","6":"7.910874","7":"NA"},{"1":"1920年","2":"01000_北海道","3":"5～9歳","4":"総数","5":"319370","6":"13.537314","7":"NA"},{"1":"1920年","2":"01000_北海道","3":"5～9歳","4":"男","5":"161814","6":"6.858900","7":"NA"},{"1":"1920年","2":"01000_北海道","3":"5～9歳","4":"女","5":"157556","6":"6.678414","7":"NA"},{"1":"1920年","2":"01000_北海道","3":"10～14歳","4":"総数","5":"270012","6":"11.445149","7":"NA"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</section>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>In this article, I introduced some tips for tidying Excel files. As approaches to data tidying, I introduced the method using the readxl package and the method using the tidyxl and unpivotr packages. In either case, interpreting the table structure of the data is important.</p>
<p>If you have any suggestions for improvement, additional content, or questions about the data-tidying workflow introduced in this article, I’d appreciate your comments.</p>


</section>


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

<ol>
<li id="fn1"><p>This example is also used on the <a href="https://ja.wikipedia.org/wiki/Tidy_data">Tidy data</a> page of the Japanese Wikipedia.↩︎</p></li>
<li id="fn2"><p>You’d need a portrait-oriented TV to check a weekly weather forecast like this!↩︎</p></li>
<li id="fn3"><p>The <a href="https://docs.ropensci.org/writexl/">writexl</a> package is also useful.↩︎</p></li>
<li id="fn4"><p>By using <a href="https://readxl.tidyverse.org/reference/cell-specification.html">cellranger</a>, you can also specify cell ranges in detail.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>R</category>
  <category>Excel</category>
  <category>Tidy Data</category>
  <category>Data Wrangling</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2024/12/tips-for-tidying-excel-file-with-r.html</guid>
  <pubDate>Sun, 22 Dec 2024 15:00:00 GMT</pubDate>
</item>
<item>
  <title>Visualizing Municipal Mergers in Japan with R’s ggraph</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph.html">my Japanese article</a>.</p>
<p>Yesterday, I introduced the jpcity package in <a href="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/lets-smartly-format-municipal-data-in-r.html">this article</a>. Internally, the jpcity package builds a network of Japan’s past municipal mergers (formally called <em>haichi bungō</em>, i.e., the establishment, abolition, and merger of municipalities) in order to map municipality codes across different points in time.</p>
<p>In this article, I’ll visualize the municipal merger network used internally by the jpcity package, following these guidelines:</p>
<ul>
<li>Visualize municipal mergers and city/town incorporations from April 1, 1970 to April 1, 2024, by prefecture</li>
<li>Node ordering does not take chronology into account (it’s possible to align positions with time, but this makes the graph harder to read)</li>
<li>Ward separations/splits are excluded</li>
</ul>
<section id="references" class="level3">
<h3 class="anchored" data-anchor-id="references">References</h3>
<ul>
<li><a href="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/lets-smartly-format-municipal-data-in-r.html">Let’s Smartly Format Municipal Data in R (aggregating municipal mergers, designated cities, etc.)</a></li>
</ul>
</section>
<section id="visualization-results-of-municipal-mergers-by-prefecture" class="level3">
<h3 class="anchored" data-anchor-id="visualization-results-of-municipal-mergers-by-prefecture">Visualization results of municipal mergers by prefecture</h3>
<p>Below are the visualization results of municipal mergers. If you notice any issues with the data, I’d appreciate it if you could let me know in the comments. Please note that in some places the labels overlap and are hard to read.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidygraph)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggraph)</span>
<span id="cb1-4"></span>
<span id="cb1-5">date_end <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;">ymd</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-04-01"</span>)</span>
<span id="cb1-6"></span>
<span id="cb1-7">graph_city <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> jpcity<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:::</span>graph_city<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>graph_city <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-8"></span>
<span id="cb1-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(nodes) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<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;">node_is_isolated</span>(),</span>
<span id="cb1-11">         <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.infinite</span>(interval) <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;">int_end</span>(interval) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> date_end) <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;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(interval)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-13"></span>
<span id="cb1-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace_na</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_desig_name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb1-15">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-16">  <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;">city_name =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_c</span>(city_desig_name, city_name)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>city_desig_name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-18"></span>
<span id="cb1-19">  <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;">group_components</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-20"></span>
<span id="cb1-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(edges) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-22">  <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>type <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</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;">"分離"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"分割"</span>))</span>
<span id="cb1-23"></span>
<span id="cb1-24">jpcity<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;">parse_pref</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">47</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">walk</span>(\(pref) {</span>
<span id="cb1-26">    pref_name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> jpcity<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;">pref_name</span>(pref)</span>
<span id="cb1-27"></span>
<span id="cb1-28">    plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> graph_city <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-29">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(nodes) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-30">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(pref_name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> .env<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pref_name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-31"></span>
<span id="cb1-32">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_layout</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fabric"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-33"></span>
<span id="cb1-34">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># It's possible to align label positions with chronology, but this is omitted since it makes the graph harder to read</span></span>
<span id="cb1-35">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mutate(y = int_start(interval)) |&gt;</span></span>
<span id="cb1-36"></span>
<span id="cb1-37">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggraph</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-38">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_edge_diagonal</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;">"gray"</span>,</span>
<span id="cb1-39">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrow</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mm'</span>)),</span>
<span id="cb1-40">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end_cap =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">circle</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mm'</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-41">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_node_label</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;">label =</span> city_name,</span>
<span id="cb1-42">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_factor</span>(group)),</span>
<span id="cb1-43">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb1-44">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-45">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_hue</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">l =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-46">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-47"></span>
<span id="cb1-48">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> pref_name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-49">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>()</span>
<span id="cb1-50"></span>
<span id="cb1-51">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(plot)</span>
<span id="cb1-52">  })</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-3.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-4.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-5.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-6.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-7.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-8.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-9.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-10.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-11.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-12.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-13.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-14.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-15.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-16.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-17.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-18.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-19.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-20.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-21.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-22.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-23.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-24.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-25.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-26.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-27.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-28.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-29.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-30.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-31.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-32.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-33.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-34.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-35.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-36.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-37.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-38.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-39.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-40.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-41.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-42.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-43.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-44.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-45.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-46.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph_files/figure-html/市町村合併の可視化-47.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>


</section>

 ]]></description>
  <category>jpcity</category>
  <category>R</category>
  <category>ggraph</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/visualizing-municipal-mergers-in-japan-with-r-ggraph.html</guid>
  <pubDate>Sat, 25 May 2024 15:00:00 GMT</pubDate>
</item>
<item>
  <title>Let’s Smartly Format Municipal Data in R (Municipal Mergers, Designated-City Data Aggregation, and More)</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/lets-smartly-format-municipal-data-in-r.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2024/05/lets-smartly-format-municipal-data-in-r.html">my Japanese article</a>.</p>
<p>This article first explains points to watch out for when analyzing municipal data, and then introduces how to smartly format municipal data in R using the jpcity package.</p>
<section id="what-should-you-watch-out-for-when-analyzing-municipal-data" class="level2">
<h2 class="anchored" data-anchor-id="what-should-you-watch-out-for-when-analyzing-municipal-data">What should you watch out for when analyzing municipal data?</h2>
<p>What should you be careful about when analyzing statistical data broken down by municipality? Below, I’ll introduce three main points to watch out for.</p>
<section id="watch-out-for-duplicate-municipality-names" class="level3">
<h3 class="anchored" data-anchor-id="watch-out-for-duplicate-municipality-names">1. Watch out for duplicate municipality names</h3>
<p>The first thing to watch out for is that there can be multiple municipalities with the same name. In fact, as of January 1, 2020, there are just under 60 municipalities whose kanji notation overlaps with another, as shown below<sup>1</sup>.</p>
<p>Here, the 5-digit number in the leftmost column of the table, <code>city_code</code>, is called the municipality code (the National Local Government Code), a number used to uniquely identify each municipality. For this reason, when analyzing municipal data, it’s preferable to identify municipalities by their municipality code rather than by name.</p>
<p>Incidentally, the first two digits of the municipality code are the identification number for the prefecture, known as the prefecture code.</p>
<div class="cell">
<details class="code-fold">
<summary>Extracting duplicate municipality names</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(jpcity)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_city</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-01-01"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-5">  jpcity<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;">city_data</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(),</span>
<span id="cb1-7">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(city_desig_name, city_name)) <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;">filter</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <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;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(n), city_desig_name, city_name) <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;">select</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;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"city_desig_name"</span>), n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-11">  rmarkdown<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;">paged_table</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["city_code"],"name":[1],"type":["chr"],"align":["left"]},{"label":["pref_name"],"name":[2],"type":["chr"],"align":["left"]},{"label":["city_name"],"name":[3],"type":["chr"],"align":["left"]},{"label":["city_name_kana"],"name":[4],"type":["chr"],"align":["left"]}],"data":[{"1":"01644","2":"北海道","3":"池田町","4":"いけだちょう"},{"1":"18382","2":"福井県","3":"池田町","4":"いけだちょう"},{"1":"20481","2":"長野県","3":"池田町","4":"いけだまち"},{"1":"21404","2":"岐阜県","3":"池田町","4":"いけだちょう"},{"1":"02445","2":"青森県","3":"南部町","4":"なんぶちょう"},{"1":"19366","2":"山梨県","3":"南部町","4":"なんぶちょう"},{"1":"31389","2":"鳥取県","3":"南部町","4":"なんぶちょう"},{"1":"06323","2":"山形県","3":"朝日町","4":"あさひまち"},{"1":"16343","2":"富山県","3":"朝日町","4":"あさひまち"},{"1":"24343","2":"三重県","3":"朝日町","4":"あさひちょう"},{"1":"18442","2":"福井県","3":"美浜町","4":"みはまちょう"},{"1":"23446","2":"愛知県","3":"美浜町","4":"みはまちょう"},{"1":"30381","2":"和歌山県","3":"美浜町","4":"みはまちょう"},{"1":"05434","2":"秋田県","3":"美郷町","4":"みさとちょう"},{"1":"32448","2":"島根県","3":"美郷町","4":"みさとちょう"},{"1":"45431","2":"宮崎県","3":"美郷町","4":"みさとちょう"},{"1":"04505","2":"宮城県","3":"美里町","4":"みさとまち"},{"1":"11381","2":"埼玉県","3":"美里町","4":"みさとまち"},{"1":"43348","2":"熊本県","3":"美里町","4":"みさとまち"},{"1":"01233","2":"北海道","3":"伊達市","4":"だてし"},{"1":"07213","2":"福島県","3":"伊達市","4":"だてし"},{"1":"10383","2":"群馬県","3":"南牧村","4":"なんもくむら"},{"1":"20305","2":"長野県","3":"南牧村","4":"みなみまきむら"},{"1":"27381","2":"大阪府","3":"太子町","4":"たいしちょう"},{"1":"28464","2":"兵庫県","3":"太子町","4":"たいしちょう"},{"1":"06401","2":"山形県","3":"小国町","4":"おぐにまち"},{"1":"43424","2":"熊本県","3":"小国町","4":"おぐにまち"},{"1":"20304","2":"長野県","3":"川上村","4":"かわかみむら"},{"1":"29452","2":"奈良県","3":"川上村","4":"かわかみむら"},{"1":"04324","2":"宮城県","3":"川崎町","4":"かわさきまち"},{"1":"40605","2":"福岡県","3":"川崎町","4":"かわさきまち"},{"1":"06382","2":"山形県","3":"川西町","4":"かわにしまち"},{"1":"29361","2":"奈良県","3":"川西町","4":"かわにしちょう"},{"1":"30362","2":"和歌山県","3":"広川町","4":"ひろがわちょう"},{"1":"40544","2":"福岡県","3":"広川町","4":"ひろかわまち"},{"1":"13206","2":"東京都","3":"府中市","4":"ふちゅうし"},{"1":"34208","2":"広島県","3":"府中市","4":"ふちゅうし"},{"1":"25383","2":"滋賀県","3":"日野町","4":"ひのちょう"},{"1":"31402","2":"鳥取県","3":"日野町","4":"ひのちょう"},{"1":"01601","2":"北海道","3":"日高町","4":"ひだかちょう"},{"1":"30382","2":"和歌山県","3":"日高町","4":"ひだかちょう"},{"1":"10522","2":"群馬県","3":"明和町","4":"めいわまち"},{"1":"24442","2":"三重県","3":"明和町","4":"めいわちょう"},{"1":"07446","2":"福島県","3":"昭和村","4":"しょうわむら"},{"1":"10448","2":"群馬県","3":"昭和村","4":"しょうわむら"},{"1":"01331","2":"北海道","3":"松前町","4":"まつまえちょう"},{"1":"38401","2":"愛媛県","3":"松前町","4":"まさきちょう"},{"1":"01345","2":"北海道","3":"森町","4":"もりまち"},{"1":"22461","2":"静岡県","3":"森町","4":"もりまち"},{"1":"01403","2":"北海道","3":"泊村","4":"とまりむら"},{"1":"01696","2":"北海道","3":"泊村","4":"とまりむら"},{"1":"01636","2":"北海道","3":"清水町","4":"しみずちょう"},{"1":"22341","2":"静岡県","3":"清水町","4":"しみずちょう"},{"1":"06361","2":"山形県","3":"金山町","4":"かねやままち"},{"1":"07445","2":"福島県","3":"金山町","4":"かねやままち"},{"1":"10428","2":"群馬県","3":"高山村","4":"たかやまむら"},{"1":"20543","2":"長野県","3":"高山村","4":"たかやまむら"},{"1":"20403","2":"長野県","3":"高森町","4":"たかもりまち"},{"1":"43428","2":"熊本県","3":"高森町","4":"たかもりまち"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</section>
<section id="watch-out-for-municipality-code-changes-due-to-municipal-mergers-haichi-bungō" class="level3">
<h3 class="anchored" data-anchor-id="watch-out-for-municipality-code-changes-due-to-municipal-mergers-haichi-bungō">2. Watch out for municipality code changes due to municipal mergers (haichi-bungō)</h3>
<p>Does that mean everything is fine as long as you use municipality codes? Unfortunately, there is another pitfall in analysis based on municipality codes: municipality codes can change due to municipal mergers (haichi-bungō, i.e., the abolition, establishment, division, and merger of municipalities).</p>
<p>Municipality codes came into use on April 1, 1970, but the number of municipalities subsequently decreased significantly due to the Great Heisei Mergers (1999-2010) and other consolidations. Because of this, analyzing municipal data from around 1999-2010 in particular can require separate, tedious aggregation and apportionment processing (so-called haichi-bungō processing).</p>
<p>As an example, let’s extract the municipality formed from the largest number of merged municipalities between April 1, 1970 and January 1, 2020. The results (table below) show that the municipality that has undergone the most municipal mergers is Joetsu City in Niigata Prefecture, formed from the merger of 15 municipalities.</p>
<p>Municipality codes can also change due to reasons other than mergers, such as a village being reclassified as a town or a town being reclassified as a city. For this reason, when analyzing municipal data, it can be necessary to format the data while taking into account the reason for and timing of these municipality code changes.</p>
<div class="cell">
<details class="code-fold">
<summary>Extracting the municipality formed from the most merged municipalities</summary>
<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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_after =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_city</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-01-01"</span>),</span>
<span id="cb2-2">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_before =</span> city_after <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-3">         <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">city_convert</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-01-01"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1970-04-01"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size_city_before =</span> vctrs<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;">list_sizes</span>(city_before)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_max</span>(size_city_before, </span>
<span id="cb2-6">            <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;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>size_city_before) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(city_before) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_code_before =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">city_code</span>(city_before),</span>
<span id="cb2-10">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_name_before =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">city_name</span>(city_before),</span>
<span id="cb2-11">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_code_after =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">city_code</span>(city_after),</span>
<span id="cb2-12">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_name_after =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">city_name</span>(city_after),</span>
<span id="cb2-13">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unused"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-14">  rmarkdown<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;">paged_table</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["city_code_before"],"name":[1],"type":["chr"],"align":["left"]},{"label":["city_name_before"],"name":[2],"type":["chr"],"align":["left"]},{"label":["city_code_after"],"name":[3],"type":["chr"],"align":["left"]},{"label":["city_name_after"],"name":[4],"type":["chr"],"align":["left"]}],"data":[{"1":"15201","2":"新潟市","3":"15100","4":"新潟市"},{"1":"15207","2":"新津市","3":"15100","4":"新潟市"},{"1":"15220","2":"白根市","3":"15100","4":"新潟市"},{"1":"15305","2":"豊栄町","3":"15100","4":"新潟市"},{"1":"15321","2":"小須戸町","3":"15100","4":"新潟市"},{"1":"15323","2":"横越村","3":"15100","4":"新潟市"},{"1":"15324","2":"亀田町","3":"15100","4":"新潟市"},{"1":"15341","2":"岩室村","3":"15100","4":"新潟市"},{"1":"15345","2":"巻町","3":"15100","4":"新潟市"},{"1":"15346","2":"西川町","3":"15100","4":"新潟市"},{"1":"15347","2":"黒埼村","3":"15100","4":"新潟市"},{"1":"15348","2":"味方村","3":"15100","4":"新潟市"},{"1":"15349","2":"潟東村","3":"15100","4":"新潟市"},{"1":"15350","2":"月潟村","3":"15100","4":"新潟市"},{"1":"15351","2":"中之口村","3":"15100","4":"新潟市"},{"1":"15203","2":"高田市","3":"15222","4":"上越市"},{"1":"15214","2":"直江津市","3":"15222","4":"上越市"},{"1":"15521","2":"安塚町","3":"15222","4":"上越市"},{"1":"15522","2":"浦川原村","3":"15222","4":"上越市"},{"1":"15525","2":"大島村","3":"15222","4":"上越市"},{"1":"15526","2":"牧村","3":"15222","4":"上越市"},{"1":"15541","2":"柿崎町","3":"15222","4":"上越市"},{"1":"15542","2":"大潟町","3":"15222","4":"上越市"},{"1":"15543","2":"頸城村","3":"15222","4":"上越市"},{"1":"15544","2":"吉川町","3":"15222","4":"上越市"},{"1":"15546","2":"中郷村","3":"15222","4":"上越市"},{"1":"15548","2":"板倉町","3":"15222","4":"上越市"},{"1":"15549","2":"清里村","3":"15222","4":"上越市"},{"1":"15550","2":"三和村","3":"15222","4":"上越市"},{"1":"15561","2":"名立町","3":"15222","4":"上越市"},{"1":"33201","2":"岡山市","3":"33100","4":"岡山市"},{"1":"33301","2":"御津町","3":"33100","4":"岡山市"},{"1":"33302","2":"一宮町","3":"33100","4":"岡山市"},{"1":"33303","2":"建部町","3":"33100","4":"岡山市"},{"1":"33304","2":"津高町","3":"33100","4":"岡山市"},{"1":"33321","2":"瀬戸町","3":"33100","4":"岡山市"},{"1":"33381","2":"上道町","3":"33100","4":"岡山市"},{"1":"33401","2":"灘崎町","3":"33100","4":"岡山市"},{"1":"33402","2":"興除村","3":"33100","4":"岡山市"},{"1":"33403","2":"藤田村","3":"33100","4":"岡山市"},{"1":"33421","2":"吉備町","3":"33100","4":"岡山市"},{"1":"33424","2":"妹尾町","3":"33100","4":"岡山市"},{"1":"33426","2":"福田村","3":"33100","4":"岡山市"},{"1":"33501","2":"高松町","3":"33100","4":"岡山市"},{"1":"33502","2":"足守町","3":"33100","4":"岡山市"},{"1":"34201","2":"広島市","3":"34100","4":"広島市"},{"1":"34301","2":"安芸町","3":"34100","4":"広島市"},{"1":"34303","2":"船越町","3":"34100","4":"広島市"},{"1":"34305","2":"瀬野川町","3":"34100","4":"広島市"},{"1":"34306","2":"熊野跡村","3":"34100","4":"広島市"},{"1":"34308","2":"矢野町","3":"34100","4":"広島市"},{"1":"34321","2":"五日市町","3":"34100","4":"広島市"},{"1":"34341","2":"祇園町","3":"34100","4":"広島市"},{"1":"34342","2":"安古市町","3":"34100","4":"広島市"},{"1":"34343","2":"佐東町","3":"34100","4":"広島市"},{"1":"34344","2":"沼田町","3":"34100","4":"広島市"},{"1":"34345","2":"安佐町","3":"34100","4":"広島市"},{"1":"34346","2":"可部町","3":"34100","4":"広島市"},{"1":"34347","2":"高陽町","3":"34100","4":"広島市"},{"1":"34387","2":"白木町","3":"34100","4":"広島市"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[20],"max":[20]},"pages":{}}}
  </script>
</div>
</div>
</div>
</section>
<section id="watch-out-for-how-wards-of-designated-cities-seirei-shitei-toshi-are-handled" class="level3">
<h3 class="anchored" data-anchor-id="watch-out-for-how-wards-of-designated-cities-seirei-shitei-toshi-are-handled">3. Watch out for how wards of designated cities (seirei shitei toshi) are handled</h3>
<p>Once you’ve grasped the points above, you can say you’ve cleared the biggest hurdle in formatting municipal data. Finally, though it’s a minor point, let’s look at how wards of designated cities are handled.</p>
<p>Depending on the statistical data, you may only be able to obtain municipal data aggregated at the city level, rather than municipal data available at the ward level for designated cities.</p>
<p>Since the city and its wards in a designated city are each assigned different municipality codes, mapping between municipality codes is necessary when integrating data with these different regional breakdowns.</p>
<p>As an example, the table below shows the mapping between the city and its wards for Yokohama City.</p>
<div class="cell">
<details class="code-fold">
<summary>Mapping between the city and wards of a designated city</summary>
<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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">find_city</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"横浜市"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-01-01"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb3-2">         <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">city_desig_merge</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb3-3">         vctrs<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;">vec_unique</span>(),</span>
<span id="cb3-4">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_desig =</span> city <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;">city_desig_split</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;">unnest</span>(city_desig) <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_code =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">city_code</span>(city),</span>
<span id="cb3-8">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_name =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">city_name</span>(city),</span>
<span id="cb3-9">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_desig_code =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">city_code</span>(city_desig),</span>
<span id="cb3-10">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_desig_name =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">city_name</span>(city_desig),</span>
<span id="cb3-11">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unused"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb3-12">  rmarkdown<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;">paged_table</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["city_code"],"name":[1],"type":["chr"],"align":["left"]},{"label":["city_name"],"name":[2],"type":["chr"],"align":["left"]},{"label":["city_desig_code"],"name":[3],"type":["chr"],"align":["left"]},{"label":["city_desig_name"],"name":[4],"type":["chr"],"align":["left"]}],"data":[{"1":"14100","2":"横浜市","3":"14101","4":"横浜市鶴見区"},{"1":"14100","2":"横浜市","3":"14102","4":"横浜市神奈川区"},{"1":"14100","2":"横浜市","3":"14103","4":"横浜市西区"},{"1":"14100","2":"横浜市","3":"14104","4":"横浜市中区"},{"1":"14100","2":"横浜市","3":"14105","4":"横浜市南区"},{"1":"14100","2":"横浜市","3":"14106","4":"横浜市保土ケ谷区"},{"1":"14100","2":"横浜市","3":"14107","4":"横浜市磯子区"},{"1":"14100","2":"横浜市","3":"14108","4":"横浜市金沢区"},{"1":"14100","2":"横浜市","3":"14109","4":"横浜市港北区"},{"1":"14100","2":"横浜市","3":"14110","4":"横浜市戸塚区"},{"1":"14100","2":"横浜市","3":"14111","4":"横浜市港南区"},{"1":"14100","2":"横浜市","3":"14112","4":"横浜市旭区"},{"1":"14100","2":"横浜市","3":"14113","4":"横浜市緑区"},{"1":"14100","2":"横浜市","3":"14114","4":"横浜市瀬谷区"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[20],"max":[20]},"pages":{}}}
  </script>
</div>
</div>
</div>
</section>
</section>
<section id="smartly-formatting-municipal-data-in-r" class="level2">
<h2 class="anchored" data-anchor-id="smartly-formatting-municipal-data-in-r">Smartly formatting municipal data in R</h2>
<p>With the points above in mind, let’s introduce how to smartly format municipal data using R. In fact, all of the examples shown above are based on information published by e-Stat in <a href="https://www.e-stat.go.jp/municipalities/cities/areacode">“Search Municipalities”</a> and <a href="https://www.e-stat.go.jp/municipalities/cities/absorption-separation-of-municipalities">“Search Municipal Merger Information”</a>.</p>
<p>However, “Search Municipal Merger Information” describes changes to municipality codes due to mergers and other events in text form, which makes it difficult to use directly for data formatting.</p>
<p>So, to make it easy to perform haichi-bungō processing and map between the cities and wards of designated cities without having to do this kind of complex data formatting, I created a new package called <a href="https://uchidamizuki.github.io/jpcity/">jpcity</a><sup>2</sup>. Incidentally, all of the tables shown above were created using the jpcity package. Since jpcity is registered on CRAN, it can be installed by running the code below. From here, I’ll briefly explain how to use jpcity.</p>
<div class="cell">
<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;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jpcity"</span>)</span></code></pre></div>
</div>
<section id="reading-in-municipality-codes-and-names" class="level3">
<h3 class="anchored" data-anchor-id="reading-in-municipality-codes-and-names">1. Reading in municipality codes and names</h3>
<p>In jpcity, you can build a municipality object from a municipality code using the <code>parse_city()</code> function<sup>3</sup>. You can also build a municipality object from a municipality name using the <code>find_city()</code> function. However, since <code>find_city()</code> returns all matching municipalities when the name is a duplicate, it’s generally recommended to use <code>parse_city()</code>. Furthermore, both <code>parse_city()</code> and <code>find_city()</code> let you specify a date as the second argument, <code>when</code>, to retrieve the municipality as of that point in time<sup>4</sup>.</p>
<p>You can also use the <code>city_code()</code> and <code>city_name()</code> functions to convert a municipality object to a (character) municipality code or municipality name, respectively.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(jpcity)</span>
<span id="cb5-3"></span>
<span id="cb5-4">city <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_parsed =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_city</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"15222"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-01-01"</span>),</span>
<span id="cb5-5">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_found =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">find_city</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"上越市"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-01-01"</span>))</span>
<span id="cb5-6"></span>
<span id="cb5-7">city</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 2
  city_parsed          city_found          
  &lt;city&gt;               &lt;city&gt;              
1 15222 [新潟県上越市] 15222 [新潟県上越市]</code></pre>
</div>
</div>
</section>
<section id="municipal-merger-haichi-bungō-processing" class="level3">
<h3 class="anchored" data-anchor-id="municipal-merger-haichi-bungō-processing">2. Municipal merger (haichi-bungō) processing</h3>
<p>You can perform municipal merger (haichi-bungō) processing using the <code>city_convert()</code> function. Here, let’s take the Joetsu City we read in earlier as an example and convert it to the municipality as of January 1, 2000<sup>5</sup>. For <code>city_convert()</code>, specify the pre-conversion date <code>"2020-01-01"</code> as the second argument, <code>from</code>, and the post-conversion date <code>"2000-01-01"</code> as the third argument, <code>to</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">city <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(city_parsed) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_converted =</span> city_parsed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb7-4">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">city_convert</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-01-01"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2000-01-01"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(city_converted)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 14 × 2
   city_parsed          city_converted        
   &lt;city&gt;               &lt;city&gt;                
 1 15222 [新潟県上越市] 15521 [新潟県安塚町]  
 2 15222 [新潟県上越市] 15522 [新潟県浦川原村]
 3 15222 [新潟県上越市] 15525 [新潟県大島村]  
 4 15222 [新潟県上越市] 15526 [新潟県牧村]    
 5 15222 [新潟県上越市] 15541 [新潟県柿崎町]  
 6 15222 [新潟県上越市] 15542 [新潟県大潟町]  
 7 15222 [新潟県上越市] 15543 [新潟県頸城村]  
 8 15222 [新潟県上越市] 15544 [新潟県吉川町]  
 9 15222 [新潟県上越市] 15546 [新潟県中郷村]  
10 15222 [新潟県上越市] 15548 [新潟県板倉町]  
11 15222 [新潟県上越市] 15549 [新潟県清里村]  
12 15222 [新潟県上越市] 15550 [新潟県三和村]  
13 15222 [新潟県上越市] 15561 [新潟県名立町]  
14 15222 [新潟県上越市] 15222 [新潟県上越市]  </code></pre>
</div>
</div>
</section>
<section id="mapping-between-the-city-and-wards-of-a-designated-city" class="level3">
<h3 class="anchored" data-anchor-id="mapping-between-the-city-and-wards-of-a-designated-city">3. Mapping between the city and wards of a designated city</h3>
<p>You can map between the city and wards of a designated city using the <code>city_desig_split()</code> and <code>city_desig_merge()</code> functions. Here, let’s perform this mapping for Yokohama City, the example we used earlier.</p>
<p>The <code>city_desig_split()</code> function splits a designated city into its wards. Using <code>city_desig_split()</code>, we can see that Yokohama City has 14 wards. The <code>city_desig_merge()</code> function merges the wards of a designated city back into the city. Using <code>city_desig_merge()</code>, we can convert the wards of Yokohama City we just split back into the city.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Split the designated city into its wards</span></span>
<span id="cb9-2">city_desig <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_city</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"14100"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-01-01"</span>),</span>
<span id="cb9-3">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city_desig =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">city_desig_split</span>(city)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb9-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(city_desig)</span>
<span id="cb9-5">city_desig</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 14 × 2
   city                   city_desig                      
   &lt;city&gt;                 &lt;city&gt;                          
 1 14100 [神奈川県横浜市] 14101 [神奈川県横浜市鶴見区]    
 2 14100 [神奈川県横浜市] 14102 [神奈川県横浜市神奈川区]  
 3 14100 [神奈川県横浜市] 14103 [神奈川県横浜市西区]      
 4 14100 [神奈川県横浜市] 14104 [神奈川県横浜市中区]      
 5 14100 [神奈川県横浜市] 14105 [神奈川県横浜市南区]      
 6 14100 [神奈川県横浜市] 14106 [神奈川県横浜市保土ケ谷区]
 7 14100 [神奈川県横浜市] 14107 [神奈川県横浜市磯子区]    
 8 14100 [神奈川県横浜市] 14108 [神奈川県横浜市金沢区]    
 9 14100 [神奈川県横浜市] 14109 [神奈川県横浜市港北区]    
10 14100 [神奈川県横浜市] 14110 [神奈川県横浜市戸塚区]    
11 14100 [神奈川県横浜市] 14111 [神奈川県横浜市港南区]    
12 14100 [神奈川県横浜市] 14112 [神奈川県横浜市旭区]      
13 14100 [神奈川県横浜市] 14113 [神奈川県横浜市緑区]      
14 14100 [神奈川県横浜市] 14114 [神奈川県横浜市瀬谷区]    </code></pre>
</div>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Merge the wards of the designated city back into the city (showing only the first few rows)</span></span>
<span id="cb11-2">city_desig <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(city_desig) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-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;">city =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">city_desig_merge</span>(city_desig)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 2
  city_desig                       city                  
  &lt;city&gt;                           &lt;city&gt;                
1 14101 [神奈川県横浜市鶴見区]     14100 [神奈川県横浜市]
2 14102 [神奈川県横浜市神奈川区]   14100 [神奈川県横浜市]
3 14103 [神奈川県横浜市西区]       14100 [神奈川県横浜市]
4 14104 [神奈川県横浜市中区]       14100 [神奈川県横浜市]
5 14105 [神奈川県横浜市南区]       14100 [神奈川県横浜市]
6 14106 [神奈川県横浜市保土ケ谷区] 14100 [神奈川県横浜市]</code></pre>
</div>
</div>
</section>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>In this article, I introduced points to watch out for when analyzing municipal data, and how to smartly format municipal data in R using the jpcity package. If you have any requests, bug reports, or questions about the jpcity package, feel free to reach out via <a href="https://github.com/uchidaMizuki/jpcity">GitHub</a> or the comment section below.</p>


</section>


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

<ol>
<li id="fn1"><p>Here, municipalities are counted as “duplicates” if their kanji notation is the same, even if the hiragana reading differs. Also, ward names of designated cities (seirei shitei toshi) are excluded, since there are many overlaps among them.↩︎</p></li>
<li id="fn2"><p>jpcity is built on data published by e-Stat in “Search Municipalities” and “Search Municipal Merger Information”.↩︎</p></li>
<li id="fn3"><p><code>parse_city()</code> also supports 6-digit municipality codes that include a check digit.↩︎</p></li>
<li id="fn4"><p>If the <code>when</code> argument isn’t specified, jpcity infers which period the municipality belongs to and reads it in accordingly. If municipalities with inconsistent periods are included, a read error will occur.↩︎</p></li>
<li id="fn5"><p>Joetsu City was newly established on April 29, 1971 through the merger of Takada City and Naoetsu City, and later, on January 1, 2005, 13 towns and villages were merged into Joetsu City.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>jpcity</category>
  <category>R</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2024/05/lets-smartly-format-municipal-data-in-r.html</guid>
  <pubDate>Fri, 24 May 2024 15:00:00 GMT</pubDate>
  <media:content url="https://raw.githubusercontent.com/UchidaMizuki/jpcity/main/man/figures/logo.png?raw=true" medium="image"/>
</item>
<item>
  <title>Deriving the marginal distribution of a normal distribution whose mean follows a normal distribution</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2024/03/normal-distribution-with-mean-following-a-normal-distribution.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2024/03/normal-distribution-with-mean-following-a-normal-distribution.html">my Japanese article</a>.</p>
<p>Suppose there is a random variable <img src="https://latex.codecogs.com/png.latex?Y"> that follows a normal distribution with mean <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> and variance <img src="https://latex.codecogs.com/png.latex?%5Ctau%5E2">, and that <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> itself follows a normal distribution with mean <img src="https://latex.codecogs.com/png.latex?%5Cmu"> and variance <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2">.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0AY%20%5Cmid%20%5Ctheta%20%5Csim%20%5Cmathcal%7BN%7D%20(%20%5Ctheta,%20%5Ctau%5E2%20)%20&amp;%5Ciff%0Ap(%20y%20%5Cmid%20%5Ctheta%20)%20=%20%5Cfrac%7B1%7D%7B%5Csqrt%7B2%20%5Cpi%7D%20%5Ctau%7D%0A%5Cexp%20%5Cleft%5B%0A%7B-%5Cfrac%7B1%7D%7B2%7D%7D%20%5Cleft(%0A%5Cfrac%7By%20-%20%5Ctheta%7D%7B%5Ctau%7D%0A%5Cright)%20%5E2%0A%5Cright%5D%20%5C%5C%0A%5Ctheta%20%5Csim%20%5Cmathcal%7BN%7D%20(%5Cmu,%20%5Csigma%5E2)%20&amp;%5Ciff%0Ap(%20%5Ctheta%20)%20=%20%5Cfrac%7B1%7D%7B%5Csqrt%7B2%20%5Cpi%7D%20%5Csigma%7D%0A%5Cexp%20%5Cleft%5B%0A%7B-%5Cfrac%7B1%7D%7B2%7D%7D%20%5Cleft(%0A%5Cfrac%7B%5Ctheta%20-%20%5Cmu%7D%7B%5Csigma%7D%0A%5Cright)%20%5E2%0A%5Cright%5D%0A%5Cend%7Balign%7D%0A"></p>
<p>In this case, it is known that the marginal distribution of <img src="https://latex.codecogs.com/png.latex?Y"> follows a normal distribution with mean <img src="https://latex.codecogs.com/png.latex?%5Cmu"> and variance <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2%20+%20%5Ctau%5E2">. This article records the derivation of this property as a personal memo (there may actually be a smarter way to derive it).</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AY%20%5Csim%20%5Cmathcal%7BN%7D%20%5Cleft(%20%5Cmu,%20%5Csigma%5E2%20+%20%5Ctau%5E2%20%5Cright)%0A"></p>
<p>The marginal probability density function <img src="https://latex.codecogs.com/png.latex?p(y)"> of <img src="https://latex.codecogs.com/png.latex?Y"> can be obtained from the following integral. By completing the square, the integrand <img src="https://latex.codecogs.com/png.latex?p(%20y%20%5Cmid%20%5Ctheta%20)%20p%20(%20%5Ctheta%20)"> can be transformed as follows.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0Ap(y)%0A&amp;=%20%5Cint_%7B-%5Cinfty%7D%5E%7B%5Cinfty%7D%0Ap(%20y,%20%5Ctheta%20)%0Ad%5Ctheta%20%5C%5C%0A&amp;=%20%5Cint_%7B-%5Cinfty%7D%5E%7B%5Cinfty%7D%0Ap(%20y%20%5Cmid%20%5Ctheta%20)%20p%20(%20%5Ctheta%20)%0Ad%5Ctheta%20%5C%5C%0A%5Cend%7Balign%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0Ap(%20y%20%5Cmid%20%5Ctheta%20)%20p%20(%20%5Ctheta%20)%0A&amp;=%20%5Cfrac%7B1%7D%7B2%20%5Cpi%20%5Ctau%20%5Csigma%7D%0A%5Cexp%20%5Cleft%5B%0A-%5Cfrac%7B1%7D%7B2%7D%0A%5Cfrac%7B%0A%5Csigma%5E2%20%5Cleft(%20y%20-%20%5Ctheta%20%5Cright)%5E2%20+%0A%5Ctau%5E2%20%5Cleft(%20%5Ctheta%20-%20%5Cmu%20%5Cright)%5E2%0A%7D%7B%5Ctau%5E2%20%5Csigma%5E2%7D%0A%5Cright%5D%20%5C%5C%0A&amp;=%20%5Cfrac%7B1%7D%7B2%20%5Cpi%20%5Ctau%20%5Csigma%7D%0A%5Cexp%20%5Cleft%5B%0A-%5Cfrac%7B1%7D%7B2%7D%0A%5Cfrac%7B%0A%5Cleft(%20%5Csigma%5E2%20+%20%5Ctau%5E2%20%5Cright)%20%5Ctheta%5E2%20-%0A2%20%5Cleft(%20%5Csigma%5E2%20y%20+%20%5Ctau%5E2%20%5Cmu%20%5Cright)%20%5Ctheta%20+%0A%5Cleft(%20%5Csigma%5E2%20y%5E2%20+%20%5Ctau%5E2%20%5Cmu%5E2%20%5Cright)%0A%7D%7B%5Ctau%5E2%20%5Csigma%5E2%7D%0A%5Cright%5D%20%5C%5C%0A&amp;=%20%5Cfrac%7B1%7D%7B2%20%5Cpi%20%5Ctau%20%5Csigma%7D%0A%5Cexp%20%5Cleft%5B%0A-%5Cfrac%7B1%7D%7B2%7D%20%5Cleft(%0A%5Cfrac%7B%5Csqrt%7B%5Csigma%5E2%20+%20%5Ctau%5E2%7D%7D%7B%5Ctau%20%5Csigma%7D%20%5Ctheta%20-%0A%5Cfrac%7B%5Csigma%5E2%20y%20+%20%5Ctau%5E2%20%5Cmu%7D%7B%5Ctau%20%5Csigma%20%5Csqrt%7B%5Csigma%5E2%20+%20%5Ctau%5E2%7D%7D%0A%5Cright)%5E2%20-%0A%5Cfrac%7B1%7D%7B2%7D%20%5Cfrac%7B%5Cleft(%20y%20-%20%5Cmu%20%5Cright)%5E2%7D%7B%5Csigma%5E2%20+%20%5Ctau%5E2%7D%0A%5Cright%5D%20%5Cquad%20%5Cleft(%20%5Cbecause%20%5Ctext%7Bcompleting%20the%20square%20with%20respect%20to%20%7D%20%5Ctheta%20%5Cright)%20%5C%5C%0A&amp;=%20%5Cfrac%7B1%7D%7B2%20%5Cpi%20%5Ctau%20%5Csigma%7D%0A%5Cexp%20%5Cleft%5B%0A-%5Cfrac%7B1%7D%7B2%7D%20%5Cleft(%0A%5Cfrac%7By%20-%20%5Cmu%7D%7B%5Csqrt%7B%5Csigma%5E2%20+%20%5Ctau%5E2%7D%7D%0A%5Cright)%5E2%0A%5Cright%5D%0A%5Cexp%20%5Cleft%5B%0A-%5Cfrac%7B1%7D%7B2%7D%20%5Cleft(%0A%5Cfrac%7B%5Ctheta%20-%20%5Cfrac%7B%5Csigma%5E2%20y%20+%20%5Ctau%5E2%20%5Cmu%7D%7B%5Csigma%5E2%20+%20%5Ctau%5E2%7D%7D%7B%5Cfrac%7B%5Ctau%20%5Csigma%7D%7B%5Csqrt%7B%5Csigma%5E2%20+%20%5Ctau%5E2%7D%7D%7D%0A%5Cright)%5E2%0A%5Cright%5D%20%5C%5C%0A%5Cend%7Balign%7D%0A"></p>
<p>Integrating <img src="https://latex.codecogs.com/png.latex?p(%20y%20%5Cmid%20%5Ctheta%20)%20p%20(%20%5Ctheta%20)"> with respect to <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> yields the integral of the kernel of a normal distribution in <img src="https://latex.codecogs.com/png.latex?%5Ctheta">. This integral can therefore be replaced by the reciprocal of the normalizing constant. In the end, we derive that the marginal probability density function <img src="https://latex.codecogs.com/png.latex?p(y)"> of <img src="https://latex.codecogs.com/png.latex?Y"> follows a normal distribution with mean <img src="https://latex.codecogs.com/png.latex?%5Cmu"> and variance <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2%20+%20%5Ctau%5E2">.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0Ap(y)%0A&amp;=%20%5Cint_%7B-%5Cinfty%7D%5E%7B%5Cinfty%7D%0Ap(%20y%20%5Cmid%20%5Ctheta%20)%20p%20(%20%5Ctheta%20)%0Ad%5Ctheta%20%5C%5C%0A&amp;=%20%5Cfrac%7B1%7D%7B2%20%5Cpi%20%5Ctau%20%5Csigma%7D%0A%5Cexp%20%5Cleft%5B%0A-%5Cfrac%7B1%7D%7B2%7D%20%5Cleft(%0A%5Cfrac%7By%20-%20%5Cmu%7D%7B%5Csqrt%7B%5Csigma%5E2%20+%20%5Ctau%5E2%7D%7D%0A%5Cright)%5E2%0A%5Cright%5D%0A%5Cint_%7B-%5Cinfty%7D%5E%7B%5Cinfty%7D%0A%5Cexp%20%5Cleft%5B%0A-%5Cfrac%7B1%7D%7B2%7D%20%5Cleft(%0A%5Cfrac%7B%5Ctheta%20-%20%5Cfrac%7B%5Csigma%5E2%20y%20+%20%5Ctau%5E2%20%5Cmu%7D%7B%5Csigma%5E2%20+%20%5Ctau%5E2%7D%7D%7B%5Cfrac%7B%5Ctau%20%5Csigma%7D%7B%5Csqrt%7B%5Csigma%5E2%20+%20%5Ctau%5E2%7D%7D%7D%0A%5Cright)%5E2%0A%5Cright%5D%0Ad%5Ctheta%20%5C%5C%0A&amp;=%20%5Cfrac%7B1%7D%7B2%20%5Cpi%20%5Ctau%20%5Csigma%7D%0A%5Cexp%20%5Cleft%5B%0A-%5Cfrac%7B1%7D%7B2%7D%20%5Cleft(%0A%5Cfrac%7By%20-%20%5Cmu%7D%7B%5Csqrt%7B%5Csigma%5E2%20+%20%5Ctau%5E2%7D%7D%0A%5Cright)%5E2%0A%5Cright%5D%0A%5Csqrt%7B2%20%5Cpi%7D%20%5Cfrac%7B%5Ctau%20%5Csigma%7D%7B%5Csqrt%7B%5Csigma%5E2%20+%20%5Ctau%5E2%7D%7D%0A%5Cquad%20%5Cleft(%20%5Cbecause%20%5Ctext%7Bintegral%20of%20the%20kernel%20of%20the%20normal%20distribution%20with%20variance%20%7D%20%5Cfrac%7B%5Ctau%5E2%20%5Csigma%5E2%7D%7B%5Csigma%5E2%20+%20%5Ctau%5E2%7D%20%5Cright)%20%5C%5C%0A&amp;=%20%5Cfrac%7B1%7D%7B%5Csqrt%7B2%20%5Cpi%7D%20%5Csqrt%7B%5Csigma%5E2%20+%20%5Ctau%5E2%7D%7D%0A%5Cexp%20%5Cleft%5B%0A-%5Cfrac%7B1%7D%7B2%7D%20%5Cleft(%0A%5Cfrac%7By%20-%20%5Cmu%7D%7B%5Csqrt%7B%5Csigma%5E2%20+%20%5Ctau%5E2%7D%7D%0A%5Cright)%5E2%0A%5Cright%5D%20%5Ciff%20Y%20%5Csim%20%5Cmathcal%7BN%7D%20%5Cleft(%20%5Cmu,%20%5Csigma%5E2%20+%20%5Ctau%5E2%20%5Cright)%5C%5C%0A%5Cend%7Balign%7D%0A"></p>



 ]]></description>
  <category>Statistics</category>
  <category>Bayes</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2024/03/normal-distribution-with-mean-following-a-normal-distribution.html</guid>
  <pubDate>Fri, 22 Mar 2024 15:00:00 GMT</pubDate>
</item>
<item>
  <title>Be careful with function forms in causal discovery with LiNGAM</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2024/03/be-careful-with-function-forms-in-lingam.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2024/03/be-careful-with-function-forms-in-lingam.html">my Japanese article</a>.</p>
<section id="causal-discovery-with-lingam" class="level2">
<h2 class="anchored" data-anchor-id="causal-discovery-with-lingam">Causal discovery with LiNGAM</h2>
<p>LiNGAM (Linear Non-Gaussian Acyclic Model) is one of the representative causal discovery methods, and it seems to be increasingly used in practice in recent years, being implemented in commercial software and other tools. As its name suggests, LiNGAM is a method that can reveal causal relationships from data when the functional form is linear and the error terms follow a non-Gaussian (non-normal) distribution.</p>
<p>LiNGAM can be used via the Python lingam package or the R pcalg package, and simply feeding a data frame into the functions provided by these packages lets you easily estimate a causal graph. A causal graph is a network structure built by treating observed variables as nodes and causal relationships as arrows. In particular, causal discovery typically estimates a causal graph in the form of a DAG (Directed Acyclic Graph).</p>
<p>As an example of a causal graph, I created the following causal graph related to running a restaurant business. In practice, profit is likely to fluctuate due to various factors such as the day of the week, weather, and the location of the store, so it is expected that even more complex causal relationships would be the subject of analysis.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">flowchart LR
  ingredient-quality --&gt; food-deliciousness
  ingredient-quality --&gt; cost
  chef-skill --&gt; food-deliciousness
  chef-skill --&gt; wages
  food-deliciousness --&gt; customer-count
  food-deliciousness --&gt; spend-per-customer
  customer-count --&gt; revenue
  spend-per-customer --&gt; revenue
  revenue --&gt; profit
  cost --&gt; expenses
  wages --&gt; expenses
  fixed-costs --&gt; expenses
  expenses --&gt; profit
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
</section>
<section id="the-effect-of-functional-form-on-lingam-estimation" class="level2">
<h2 class="anchored" data-anchor-id="the-effect-of-functional-form-on-lingam-estimation">The effect of functional form on LiNGAM estimation</h2>
<p>LiNGAM is a method that estimates causal relationships under the assumption that the functional form is linear and the error terms follow a non-Gaussian distribution. Therefore, if the functional form is not linear (or is difficult to approximate linearly), incorrect causal relationships may be estimated. Packages and commercial software make it easy to run LiNGAM, but users need to understand that these assumptions exist before using LiNGAM.</p>
<p>Here, let’s apply LiNGAM to data with a simpler causal relationship than the causal graph shown above. Specifically, we will check how the results of LiNGAM change when a nonlinear function is placed in <code>fun</code> in the flowchart below.</p>
<p>Note that in this article, we use Direct LiNGAM, the most common variant of LiNGAM, with a uniform distribution (a non-Gaussian distribution) for the error terms. We also ignore any causal relationships whose coefficient has an absolute value less than 0.001.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">flowchart LR
  x11 --&gt; add1(+)
  x12 --&gt; add1
  add1 --&gt; x21
  x21 --&gt; fun2(fun)
  x22 --&gt; fun2
  fun2 --&gt; x31
style fun2 stroke:red,color:red
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<p>Before estimating the causal graph, let’s prepare a data frame with 1,000 rows and columns <code>x11</code>, <code>x12</code>, and <code>x22</code>, along with functions for causal discovery.</p>
<div id="ca4b8b54" class="cell" data-execution_count="1">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> lingam</span>
<span id="cb1-5"></span>
<span id="cb1-6">rng <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.default_rng(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1234</span>)</span>
<span id="cb1-7"></span>
<span id="cb1-8">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb1-9">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb1-10">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x11'</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.random(n),</span>
<span id="cb1-11">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x12'</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.random(n),</span>
<span id="cb1-12">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x22'</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.random(n)</span>
<span id="cb1-13">})</span>
<span id="cb1-14"></span>
<span id="cb1-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Function that returns the DirectLiNGAM result as a data frame</span></span>
<span id="cb1-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> discover_causality(data):</span>
<span id="cb1-17">  model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lingam.DirectLiNGAM()</span>
<span id="cb1-18">  model.fit(data)</span>
<span id="cb1-19"></span>
<span id="cb1-20">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> pd.DataFrame(</span>
<span id="cb1-21">    model.adjacency_matrix_,</span>
<span id="cb1-22">    columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>data.columns,</span>
<span id="cb1-23">    index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>data.columns</span>
<span id="cb1-24">  )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-25">  .reset_index(names <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'node_to'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-26">  .melt(</span>
<span id="cb1-27">    id_vars<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'node_to'</span>,</span>
<span id="cb1-28">    var_name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'node_from'</span></span>
<span id="cb1-29">  )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-30">  .pipe(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> df: df[np.logical_not(np.isclose(df.value, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, rtol<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, atol<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-3</span>))])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-31">  .reindex(columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'node_from'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'node_to'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'value'</span>])</span>
<span id="cb1-32"></span>
<span id="cb1-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Function that writes a mermaid file</span></span>
<span id="cb1-34"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> write_mermaid(df, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>):</span>
<span id="cb1-35">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb1-36">    f.write(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'flowchart LR</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>)</span>
<span id="cb1-37">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> df.itertuples():</span>
<span id="cb1-38">      f.write(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">--&gt;|</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{:.3f}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">|</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(row.node_from, row.value, row.node_to))</span>
<span id="cb1-39"></span>
<span id="cb1-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output folder</span></span>
<span id="cb1-41"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dir</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'be-careful-with-function-forms-in-lingam'</span></span>
<span id="cb1-42"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> os.path.exists(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dir</span>):</span>
<span id="cb1-43">  os.makedirs(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dir</span>)</span></code></pre></div>
</details>
</div>
<section id="case-where-the-causal-graph-includes-multiplication-fun-is" class="level3">
<h3 class="anchored" data-anchor-id="case-where-the-causal-graph-includes-multiplication-fun-is">Case where the causal graph includes multiplication (<code>fun</code> is <code>*</code>)</h3>
<p>Let’s see what the Direct LiNGAM estimation results look like when <code>fun</code> is multiplication (<code>*</code>). The estimated causal graph has the same structure as the true causal graph, so in this case we can see that the causal graph was estimated correctly. This suggests that causal relationship estimation can work well even for multiplication rather than addition (linear) cases. However, the situation may differ significantly depending on the number of observed variables and the amount of data.</p>
<div id="acd13c97" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">data_nonlinear_prod <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb2-2"> .assign(</span>
<span id="cb2-3">    x21<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> df: df.x11 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> df.x12 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.random(n),</span>
<span id="cb2-4">    x31<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> df: df.x21 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> df.x22 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.random(n),</span>
<span id="cb2-5">  )</span>
<span id="cb2-6"></span>
<span id="cb2-7">causality_nonlinear_prod <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> discover_causality(data_nonlinear_prod)</span>
<span id="cb2-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(causality_nonlinear_prod)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   node_from node_to     value
3        x11     x21  0.975641
8        x12     x21  0.999905
14       x22     x31  4.524066
19       x21     x31  3.477884</code></pre>
</div>
</div>
<div id="fbe26c1e" class="cell" data-execution_count="3">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">write_mermaid(causality_nonlinear_prod, os.path.join(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dir</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dag_lingam_nonlinear_prod.mmd'</span>))</span></code></pre></div>
</details>
</div>
<div class="cell" data-file="be-careful-with-function-forms-in-lingam/dag_lingam_nonlinear_prod.mmd" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">flowchart LR
  x11--&gt;|0.976|x21
  x12--&gt;|1.000|x21
  x22--&gt;|4.524|x31
  x21--&gt;|3.478|x31
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
</section>
<section id="case-where-the-causal-graph-includes-exponentiation-fun-is" class="level3">
<h3 class="anchored" data-anchor-id="case-where-the-causal-graph-includes-exponentiation-fun-is">Case where the causal graph includes exponentiation (<code>fun</code> is <code>**</code>)</h3>
<p>Next, let’s see what the Direct LiNGAM estimation results look like when <code>fun</code> is exponentiation (<code>**</code>). The estimated causal graph has a different structure from the true causal graph, and we can see that incorrect causal relationships are also estimated between upstream observed variables that originally had no causal relationship. This shows that when Direct LiNGAM involves a nonlinear function, there is a risk that incorrect causal relationships will be estimated even between observed variables that are not directly related to that function.</p>
<div id="a6ef1ab4" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">data_nonlinear_power <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb5-2"> .assign(</span>
<span id="cb5-3">    x21<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> df: df.x11 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> df.x12 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.random(n),</span>
<span id="cb5-4">    x31<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> df: df.x21 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> df.x22 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.random(n),</span>
<span id="cb5-5">  )</span>
<span id="cb5-6"></span>
<span id="cb5-7">causality_nonlinear_power <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> discover_causality(data_nonlinear_power)</span>
<span id="cb5-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(causality_nonlinear_power)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   node_from node_to     value
1        x11     x12 -0.147973
3        x11     x21  0.832946
8        x12     x21  0.798157
17       x21     x22 -0.400541
22       x31     x22  0.002306</code></pre>
</div>
</div>
<div id="dd40992b" class="cell" data-execution_count="5">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">write_mermaid(causality_nonlinear_power, os.path.join(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dir</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dag_lingam_nonlinear_power.mmd'</span>))</span></code></pre></div>
</details>
</div>
<div class="cell" data-file="be-careful-with-function-forms-in-lingam/dag_lingam_nonlinear_power.mmd" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">flowchart LR
  x11--&gt;|-0.148|x12
  x11--&gt;|0.833|x21
  x12--&gt;|0.798|x21
  x21--&gt;|-0.401|x22
  x31--&gt;|0.002|x22
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
</section>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>In this article, we showed that applying LiNGAM to data with nonlinear functions can result in incorrect causal relationships being estimated. Therefore, when applying LiNGAM, it is important to confirm that the causal relationships can be expressed linearly.</p>
<p>On the other hand, it seems rare to be in a situation where causal relationships are unknown, yet it is known that they can be expressed linearly. Therefore, careful identification of causal relationships is required, through means such as verifying linearity after the fact or conducting experiments.</p>


</section>

 ]]></description>
  <category>R</category>
  <category>Python</category>
  <category>Causal Inference</category>
  <category>LiNGAM</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2024/03/be-careful-with-function-forms-in-lingam.html</guid>
  <pubDate>Sat, 09 Mar 2024 15:00:00 GMT</pubDate>
</item>
<item>
  <title>Scraping dynamic sites with rvest (without Selenium)</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2024/02/scraping-dynamic-sites-with-rvest.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2024/02/scraping-dynamic-sites-with-rvest.html">my Japanese article</a>.</p>
<section id="web-scraping-for-dynamic-sites-in-r" class="level2">
<h2 class="anchored" data-anchor-id="web-scraping-for-dynamic-sites-in-r">Web scraping for dynamic sites in R</h2>
<p>rvest is a popular package for web scraping in R, bur it could not be used for dynamic sites where the contents changes with the operations on the browser.</p>
<p>Therefore, for scraping dynamic sites in R, you needed to use other packages such as RSelenium with rvest and had the following issues.</p>
<ul>
<li><p>When using Selenium, it is troublesome to set up the environment (e.g., you need to download the driver in advance, etc.).</p></li>
<li><p>We couldn’t seamlessly apply rvest functions to HTML from other packages.</p></li>
</ul>
<p>In <a href="https://cran.r-project.org/web/packages/rvest/news/news.html">rvest 1.0.4</a>, however, a new <a href="https://rvest.tidyverse.org/reference/read_html_live.html"><code>read_html_live()</code></a> function has been added to allow dynamic site scraping with rvest alone. By using <code>read_html_live()</code>, you can automate browser operations with methods such as <code>$click()</code> and <code>$type()</code>. Not only that, but you can seamlessly call rvest functions such as <code>html_elements()</code> and <code>html_attr()</code> with <code>read_html_live()</code>.</p>
<p><code>read_html_live()</code> uses <a href="https://rstudio.github.io/chromote/">chromote</a> package for Google Chrome automation internally. Therefore to use the function, you need to install Google Chrome (browser) and chromote (R package) beforehand.</p>
</section>
<section id="lets-use-read_html_live" class="level2">
<h2 class="anchored" data-anchor-id="lets-use-read_html_live">Let’s use <code>read_html_live()</code></h2>
<p>From here, let’s use <code>read_html_live()</code> to perform the same process in <a href="https://joshuamccrain.com/tutorials/web_scraping_R_selenium.html">this RSelenium tutorial</a>.</p>
<p>This tutorial automates the process of collecting information from local TV stations in <a href="https://www.fcc.gov/media/engineering/dtvmaps">this site</a>. This process requires a search for U.S. zip codes.</p>
<p>By using <code>read_html_live()</code>, the code to access the site in RSelenium can be rewritten in rvest as follows.</p>
<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;"># RSelenium</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Source: https://joshuamccrain.com/tutorials/web_scraping_R_selenium.html</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(RSelenium)</span>
<span id="cb1-4"></span>
<span id="cb1-5">rD <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;">rsDriver</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">browser=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"firefox"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">port=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4545</span>L, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose=</span>F)</span>
<span id="cb1-6">remDr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rD[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"client"</span>]]</span>
<span id="cb1-7">remDr<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;">navigate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.fcc.gov/media/engineering/dtvmaps"</span>)</span></code></pre></div>
<p>⏬</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rvest</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rvest)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb2-4"></span>
<span id="cb2-5">html <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_html_live</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.fcc.gov/media/engineering/dtvmaps"</span>)</span></code></pre></div>
</div>
<p>You can use the <code>$view()</code> method to see the LiveHTML object. You can also select elements on the site by <code>Ctrl+Shift+C</code> and then right-click⏩<code>Copy</code>⏩<code>Copy selector</code> to copy the CSS selector, and use it as an argument to <code>$type()</code> or <code>$click()</code>.</p>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rvest</span></span>
<span id="cb3-2">html<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;">view</span>()</span></code></pre></div>
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/02/scraping-dynamic-sites-with-rvest/html-view.png" class="img-fluid"></p>
<p>Next, enter the zip code in the center form and click the <code>Go!</code> button. Here, the code in RSelenium can be rewritten in rvest as follows.</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># RSelenium</span></span>
<span id="cb4-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Source: https://joshuamccrain.com/tutorials/web_scraping_R_selenium.html</span></span>
<span id="cb4-3">zip <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"30308"</span></span>
<span id="cb4-4">remDr<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;">findElement</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">using =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"startpoint"</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;">sendKeysToElement</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(zip))</span>
<span id="cb4-5">remDr<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;">findElements</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"btnSub"</span>)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">clickElement</span>()</span></code></pre></div>
<p>⏬</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rvest</span></span>
<span id="cb5-2">zip <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"30308"</span></span>
<span id="cb5-3">html<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;">type</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#startpoint"</span>, zip)</span>
<span id="cb5-4">html<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;">click</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#btnSub"</span>)</span></code></pre></div>
</div>
<p>Finally, let’s check we got the same data as in the RSelenium tutorial above.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rvest</span></span>
<span id="cb6-2">html <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html_elements</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table.tbl_mapReception"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">insistently</span>(chuck)(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html_table</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</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;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, IA)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename_with</span>(str_to_lower) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ch_num =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ch#</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_tail</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(callsign <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["callsign"],"name":[1],"type":["chr"],"align":["left"]},{"label":["network"],"name":[2],"type":["chr"],"align":["left"]},{"label":["ch_num"],"name":[3],"type":["chr"],"align":["left"]},{"label":["band"],"name":[4],"type":["chr"],"align":["left"]}],"data":[{"1":"WXIA-TV","2":"NBC","3":"11","4":"Hi-V"},{"1":"WSB-TV","2":"ABC","3":"2","4":"UHF"},{"1":"WUVG-DT","2":"UNIV","3":"34","4":"UHF"},{"1":"WHSG-TV","2":"TRIN","3":"63","4":"UHF"},{"1":"WAGA-TV","2":"FOX","3":"5","4":"UHF"},{"1":"WANF","2":"CBS","3":"46","4":"UHF"},{"1":"WUPA","2":"THE","3":"69","4":"UHF"},{"1":"WPCH-TV","2":"IND","3":"17","4":"UHF"},{"1":"WATL","2":"MY N","3":"36","4":"UHF"},{"1":"WIRE-CD","2":"","3":"","4":"UHF"},{"1":"WABE-TV","2":"PBS","3":"30","4":"UHF"},{"1":"WATC-DT","2":"ETV","3":"57","4":"UHF"},{"1":"WYGA-CD","2":"","3":"","4":"UHF"},{"1":"WGTV","2":"PBS","3":"8","4":"Hi-V"},{"1":"WANN-CD","2":"","3":"","4":"UHF"},{"1":"WPXA-TV","2":"ION","3":"14","4":"UHF"},{"1":"WKTB-CD","2":"","3":"","4":"UHF"},{"1":"WSKC-CD","2":"","3":"","4":"UHF"},{"1":"WNGH-TV","2":"PBS","3":"18","4":"Lo-V"},{"1":"WJSP-TV","2":"PBS","3":"28","4":"Lo-V"},{"1":"WCIQ","2":"PBS","3":"7","4":"Hi-V"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>As above, by using <code>read_html_live()</code> that was added in rvest 1.0.4, we found that seamless scraping of static and dynamic sites is possible with rvest alone. In addition, it should be noted that rvest’s code was simpler than RSelenium’s.</p>
<p>In addition to rvest, a web scraping package called <a href="https://ashbythorpe.github.io/selenider/">selenider</a> is also being developed for R. It is expected that the development of such packages will make web scraping in R even more convenient!</p>


</section>

 ]]></description>
  <category>rvest</category>
  <category>R</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2024/02/scraping-dynamic-sites-with-rvest.html</guid>
  <pubDate>Wed, 14 Feb 2024 15:00:00 GMT</pubDate>
  <media:content url="https://uchidamizuki.quarto.pub/blog/en/posts/2024/02/scraping-dynamic-sites-with-rvest/html-view.png" medium="image" type="image/png" height="94" width="144"/>
</item>
<item>
  <title>Improve the quality of data preprocessing with R’s dm package</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2024/01/relational-data-models-with-r-s-dm.html</link>
  <description><![CDATA[ 





<p>Note: This article is a machine-translated translation of <a href="https://uchidamizuki.quarto.pub/blog/posts/2024/01/relational-data-models-with-r-s-dm.html">my Japanese article</a> with simple modifications.</p>
<section id="how-do-we-reduce-errors-in-data-preprocessing" class="level2">
<h2 class="anchored" data-anchor-id="how-do-we-reduce-errors-in-data-preprocessing">How do we reduce errors in data preprocessing?</h2>
<p>It is said that 80% of the time required for data analysis is spent on preprocessing. Even though preprocessing determines the quality of subsequent data analysis, the more time spent on pre-processing means the probability of making mistakes is also high.</p>
<p>Preprocessing tasks include “extraction,” “aggregation,” and “joining (merging)” of data frames. Among these, data joining is a task that tends to make the code longer and more prone to errors. And since the data frames needed for analysis are rarely combined into one, joining data frames is a widespread process.</p>
<p>Typical mistakes that tend to occur when joining data frames are as follows<sup>1</sup>,</p>
<ul>
<li>The keys of the data frame to be joined are not “<a href="https://en.wikipedia.org/wiki/MECE_principle">MECE</a>”</li>
<li>Mistaken keys to join data frames</li>
</ul>
<p>If you have experienced any mistakes or near-misses in joining data frames, you may be better off relying on some sort of package instead of letting them go unattended. Besides, even if there were no mistakes, we tend to be skeptical about whether we did the right thing in the past.</p>
<p>This article introduces the R dm package, a powerful solution to the problems of errors and skepticism in joining data frames.</p>
</section>
<section id="relational-data-model-with-dm" class="level2">
<h2 class="anchored" data-anchor-id="relational-data-model-with-dm">Relational data model with dm</h2>
<p>The relational data model provided by dm manages the relationships among multiple data on our behalf.</p>
<p>This article describes the use and advantages of the relational data model provided by dm. the Star Wars data set provided by the repurrrsive package. For this purpose, let’s use the Star Wars dataset provided by the repurrrsive package. tidyverse, dm, and repurrrsive packages must be pre-loaded.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dm)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: package 'dm' was built under R version 4.6.1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(repurrrsive)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: package 'repurrrsive' was built under R version 4.6.1</code></pre>
</div>
</div>
<p>The Star Wars dataset provided by repurrrsive includes the followings<sup>2</sup>.</p>
<div class="cell">
<details class="code-fold">
<summary>StarWars dataset provided by repurrrsive</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">package =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"repurrrsive"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chuck</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"results"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_starts</span>(Item, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sw_"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(Item)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "sw_films"     "sw_people"    "sw_planets"   "sw_species"   "sw_starships"
[6] "sw_vehicles" </code></pre>
</div>
</div>
<p>This article will illustrate the use of the relational data model through the following two analytical examples. These examples require dealing with relationships among multiple data sets.</p>
<ol type="1">
<li>Basic: Building a relational data model with dm</li>
<li>Application: Extending the model and validating keys with dm</li>
</ol>
<section id="basic-building-a-relational-data-model-with-dm" class="level3">
<h3 class="anchored" data-anchor-id="basic-building-a-relational-data-model-with-dm">1. Basic: Building a relational data model with dm</h3>
<p>As a basic example, let us build a relational data model to determine the composition ratio of the home planets of the characters in Star Wars movies. To perform this analysis, three data frames, <code>films</code>, <code>people</code>, and <code>planets</code>, are prepared as shown below. Here, only the necessary data are selected to simplify the analysis<sup>3</sup>．</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">films <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">film =</span> sw_films) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest_wider</span>(film) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(url, title, characters)</span>
<span id="cb7-4">people <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">person =</span> sw_people) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest_wider</span>(person) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb7-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(url, name, homeworld, species)</span>
<span id="cb7-7">planets <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">planet =</span> sw_planets) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest_wider</span>(planet) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb7-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(url, name)</span></code></pre></div>
</div>
<div class="cell">
<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">films</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["url"],"name":[1],"type":["chr"],"align":["left"]},{"label":["title"],"name":[2],"type":["chr"],"align":["left"]},{"label":["characters"],"name":[3],"type":["list"],"align":["right"]}],"data":[{"1":"http://swapi.co/api/films/1/","2":"A New Hope","3":"<chr [18]>"},{"1":"http://swapi.co/api/films/5/","2":"Attack of the Clones","3":"<chr [40]>"},{"1":"http://swapi.co/api/films/4/","2":"The Phantom Menace","3":"<chr [34]>"},{"1":"http://swapi.co/api/films/6/","2":"Revenge of the Sith","3":"<chr [34]>"},{"1":"http://swapi.co/api/films/3/","2":"Return of the Jedi","3":"<chr [20]>"},{"1":"http://swapi.co/api/films/2/","2":"The Empire Strikes Back","3":"<chr [16]>"},{"1":"http://swapi.co/api/films/7/","2":"The Force Awakens","3":"<chr [11]>"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
<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">people</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["url"],"name":[1],"type":["chr"],"align":["left"]},{"label":["name"],"name":[2],"type":["chr"],"align":["left"]},{"label":["homeworld"],"name":[3],"type":["chr"],"align":["left"]},{"label":["species"],"name":[4],"type":["chr"],"align":["left"]}],"data":[{"1":"http://swapi.co/api/people/1/","2":"Luke Skywalker","3":"http://swapi.co/api/planets/1/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/2/","2":"C-3PO","3":"http://swapi.co/api/planets/1/","4":"http://swapi.co/api/species/2/"},{"1":"http://swapi.co/api/people/3/","2":"R2-D2","3":"http://swapi.co/api/planets/8/","4":"http://swapi.co/api/species/2/"},{"1":"http://swapi.co/api/people/4/","2":"Darth Vader","3":"http://swapi.co/api/planets/1/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/5/","2":"Leia Organa","3":"http://swapi.co/api/planets/2/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/6/","2":"Owen Lars","3":"http://swapi.co/api/planets/1/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/7/","2":"Beru Whitesun lars","3":"http://swapi.co/api/planets/1/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/8/","2":"R5-D4","3":"http://swapi.co/api/planets/1/","4":"http://swapi.co/api/species/2/"},{"1":"http://swapi.co/api/people/9/","2":"Biggs Darklighter","3":"http://swapi.co/api/planets/1/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/10/","2":"Obi-Wan Kenobi","3":"http://swapi.co/api/planets/20/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/11/","2":"Anakin Skywalker","3":"http://swapi.co/api/planets/1/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/12/","2":"Wilhuff Tarkin","3":"http://swapi.co/api/planets/21/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/13/","2":"Chewbacca","3":"http://swapi.co/api/planets/14/","4":"http://swapi.co/api/species/3/"},{"1":"http://swapi.co/api/people/14/","2":"Han Solo","3":"http://swapi.co/api/planets/22/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/15/","2":"Greedo","3":"http://swapi.co/api/planets/23/","4":"http://swapi.co/api/species/4/"},{"1":"http://swapi.co/api/people/16/","2":"Jabba Desilijic Tiure","3":"http://swapi.co/api/planets/24/","4":"http://swapi.co/api/species/5/"},{"1":"http://swapi.co/api/people/18/","2":"Wedge Antilles","3":"http://swapi.co/api/planets/22/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/19/","2":"Jek Tono Porkins","3":"http://swapi.co/api/planets/26/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/20/","2":"Yoda","3":"http://swapi.co/api/planets/28/","4":"http://swapi.co/api/species/6/"},{"1":"http://swapi.co/api/people/21/","2":"Palpatine","3":"http://swapi.co/api/planets/8/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/22/","2":"Boba Fett","3":"http://swapi.co/api/planets/10/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/23/","2":"IG-88","3":"http://swapi.co/api/planets/28/","4":"http://swapi.co/api/species/2/"},{"1":"http://swapi.co/api/people/24/","2":"Bossk","3":"http://swapi.co/api/planets/29/","4":"http://swapi.co/api/species/7/"},{"1":"http://swapi.co/api/people/25/","2":"Lando Calrissian","3":"http://swapi.co/api/planets/30/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/26/","2":"Lobot","3":"http://swapi.co/api/planets/6/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/27/","2":"Ackbar","3":"http://swapi.co/api/planets/31/","4":"http://swapi.co/api/species/8/"},{"1":"http://swapi.co/api/people/28/","2":"Mon Mothma","3":"http://swapi.co/api/planets/32/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/29/","2":"Arvel Crynyd","3":"http://swapi.co/api/planets/28/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/30/","2":"Wicket Systri Warrick","3":"http://swapi.co/api/planets/7/","4":"http://swapi.co/api/species/9/"},{"1":"http://swapi.co/api/people/31/","2":"Nien Nunb","3":"http://swapi.co/api/planets/33/","4":"http://swapi.co/api/species/10/"},{"1":"http://swapi.co/api/people/32/","2":"Qui-Gon Jinn","3":"http://swapi.co/api/planets/28/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/33/","2":"Nute Gunray","3":"http://swapi.co/api/planets/18/","4":"http://swapi.co/api/species/11/"},{"1":"http://swapi.co/api/people/34/","2":"Finis Valorum","3":"http://swapi.co/api/planets/9/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/36/","2":"Jar Jar Binks","3":"http://swapi.co/api/planets/8/","4":"http://swapi.co/api/species/12/"},{"1":"http://swapi.co/api/people/37/","2":"Roos Tarpals","3":"http://swapi.co/api/planets/8/","4":"http://swapi.co/api/species/12/"},{"1":"http://swapi.co/api/people/38/","2":"Rugor Nass","3":"http://swapi.co/api/planets/8/","4":"http://swapi.co/api/species/12/"},{"1":"http://swapi.co/api/people/39/","2":"Ric Olié","3":"http://swapi.co/api/planets/8/","4":"NA"},{"1":"http://swapi.co/api/people/40/","2":"Watto","3":"http://swapi.co/api/planets/34/","4":"http://swapi.co/api/species/13/"},{"1":"http://swapi.co/api/people/41/","2":"Sebulba","3":"http://swapi.co/api/planets/35/","4":"http://swapi.co/api/species/14/"},{"1":"http://swapi.co/api/people/42/","2":"Quarsh Panaka","3":"http://swapi.co/api/planets/8/","4":"NA"},{"1":"http://swapi.co/api/people/43/","2":"Shmi Skywalker","3":"http://swapi.co/api/planets/1/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/44/","2":"Darth Maul","3":"http://swapi.co/api/planets/36/","4":"http://swapi.co/api/species/22/"},{"1":"http://swapi.co/api/people/45/","2":"Bib Fortuna","3":"http://swapi.co/api/planets/37/","4":"http://swapi.co/api/species/15/"},{"1":"http://swapi.co/api/people/46/","2":"Ayla Secura","3":"http://swapi.co/api/planets/37/","4":"http://swapi.co/api/species/15/"},{"1":"http://swapi.co/api/people/48/","2":"Dud Bolt","3":"http://swapi.co/api/planets/39/","4":"http://swapi.co/api/species/17/"},{"1":"http://swapi.co/api/people/49/","2":"Gasgano","3":"http://swapi.co/api/planets/40/","4":"http://swapi.co/api/species/18/"},{"1":"http://swapi.co/api/people/50/","2":"Ben Quadinaros","3":"http://swapi.co/api/planets/41/","4":"http://swapi.co/api/species/19/"},{"1":"http://swapi.co/api/people/51/","2":"Mace Windu","3":"http://swapi.co/api/planets/42/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/52/","2":"Ki-Adi-Mundi","3":"http://swapi.co/api/planets/43/","4":"http://swapi.co/api/species/20/"},{"1":"http://swapi.co/api/people/53/","2":"Kit Fisto","3":"http://swapi.co/api/planets/44/","4":"http://swapi.co/api/species/21/"},{"1":"http://swapi.co/api/people/54/","2":"Eeth Koth","3":"http://swapi.co/api/planets/45/","4":"http://swapi.co/api/species/22/"},{"1":"http://swapi.co/api/people/55/","2":"Adi Gallia","3":"http://swapi.co/api/planets/9/","4":"http://swapi.co/api/species/23/"},{"1":"http://swapi.co/api/people/56/","2":"Saesee Tiin","3":"http://swapi.co/api/planets/47/","4":"http://swapi.co/api/species/24/"},{"1":"http://swapi.co/api/people/57/","2":"Yarael Poof","3":"http://swapi.co/api/planets/48/","4":"http://swapi.co/api/species/25/"},{"1":"http://swapi.co/api/people/58/","2":"Plo Koon","3":"http://swapi.co/api/planets/49/","4":"http://swapi.co/api/species/26/"},{"1":"http://swapi.co/api/people/59/","2":"Mas Amedda","3":"http://swapi.co/api/planets/50/","4":"http://swapi.co/api/species/27/"},{"1":"http://swapi.co/api/people/60/","2":"Gregar Typho","3":"http://swapi.co/api/planets/8/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/61/","2":"Cordé","3":"http://swapi.co/api/planets/8/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/62/","2":"Cliegg Lars","3":"http://swapi.co/api/planets/1/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/63/","2":"Poggle the Lesser","3":"http://swapi.co/api/planets/11/","4":"http://swapi.co/api/species/28/"},{"1":"http://swapi.co/api/people/64/","2":"Luminara Unduli","3":"http://swapi.co/api/planets/51/","4":"http://swapi.co/api/species/29/"},{"1":"http://swapi.co/api/people/65/","2":"Barriss Offee","3":"http://swapi.co/api/planets/51/","4":"http://swapi.co/api/species/29/"},{"1":"http://swapi.co/api/people/66/","2":"Dormé","3":"http://swapi.co/api/planets/8/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/67/","2":"Dooku","3":"http://swapi.co/api/planets/52/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/68/","2":"Bail Prestor Organa","3":"http://swapi.co/api/planets/2/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/69/","2":"Jango Fett","3":"http://swapi.co/api/planets/53/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/70/","2":"Zam Wesell","3":"http://swapi.co/api/planets/54/","4":"http://swapi.co/api/species/30/"},{"1":"http://swapi.co/api/people/71/","2":"Dexter Jettster","3":"http://swapi.co/api/planets/55/","4":"http://swapi.co/api/species/31/"},{"1":"http://swapi.co/api/people/72/","2":"Lama Su","3":"http://swapi.co/api/planets/10/","4":"http://swapi.co/api/species/32/"},{"1":"http://swapi.co/api/people/73/","2":"Taun We","3":"http://swapi.co/api/planets/10/","4":"http://swapi.co/api/species/32/"},{"1":"http://swapi.co/api/people/74/","2":"Jocasta Nu","3":"http://swapi.co/api/planets/9/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/47/","2":"Ratts Tyerell","3":"http://swapi.co/api/planets/38/","4":"http://swapi.co/api/species/16/"},{"1":"http://swapi.co/api/people/75/","2":"R4-P17","3":"http://swapi.co/api/planets/28/","4":"NA"},{"1":"http://swapi.co/api/people/76/","2":"Wat Tambor","3":"http://swapi.co/api/planets/56/","4":"http://swapi.co/api/species/33/"},{"1":"http://swapi.co/api/people/77/","2":"San Hill","3":"http://swapi.co/api/planets/57/","4":"http://swapi.co/api/species/34/"},{"1":"http://swapi.co/api/people/78/","2":"Shaak Ti","3":"http://swapi.co/api/planets/58/","4":"http://swapi.co/api/species/35/"},{"1":"http://swapi.co/api/people/79/","2":"Grievous","3":"http://swapi.co/api/planets/59/","4":"http://swapi.co/api/species/36/"},{"1":"http://swapi.co/api/people/80/","2":"Tarfful","3":"http://swapi.co/api/planets/14/","4":"http://swapi.co/api/species/3/"},{"1":"http://swapi.co/api/people/81/","2":"Raymus Antilles","3":"http://swapi.co/api/planets/2/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/82/","2":"Sly Moore","3":"http://swapi.co/api/planets/60/","4":"NA"},{"1":"http://swapi.co/api/people/83/","2":"Tion Medon","3":"http://swapi.co/api/planets/12/","4":"http://swapi.co/api/species/37/"},{"1":"http://swapi.co/api/people/84/","2":"Finn","3":"http://swapi.co/api/planets/28/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/85/","2":"Rey","3":"http://swapi.co/api/planets/28/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/86/","2":"Poe Dameron","3":"http://swapi.co/api/planets/28/","4":"http://swapi.co/api/species/1/"},{"1":"http://swapi.co/api/people/87/","2":"BB8","3":"http://swapi.co/api/planets/28/","4":"http://swapi.co/api/species/2/"},{"1":"http://swapi.co/api/people/88/","2":"Captain Phasma","3":"http://swapi.co/api/planets/28/","4":"NA"},{"1":"http://swapi.co/api/people/35/","2":"Padmé Amidala","3":"http://swapi.co/api/planets/8/","4":"http://swapi.co/api/species/1/"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
<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">planets</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["url"],"name":[1],"type":["chr"],"align":["left"]},{"label":["name"],"name":[2],"type":["chr"],"align":["left"]}],"data":[{"1":"http://swapi.co/api/planets/2/","2":"Alderaan"},{"1":"http://swapi.co/api/planets/3/","2":"Yavin IV"},{"1":"http://swapi.co/api/planets/4/","2":"Hoth"},{"1":"http://swapi.co/api/planets/5/","2":"Dagobah"},{"1":"http://swapi.co/api/planets/6/","2":"Bespin"},{"1":"http://swapi.co/api/planets/7/","2":"Endor"},{"1":"http://swapi.co/api/planets/8/","2":"Naboo"},{"1":"http://swapi.co/api/planets/9/","2":"Coruscant"},{"1":"http://swapi.co/api/planets/10/","2":"Kamino"},{"1":"http://swapi.co/api/planets/11/","2":"Geonosis"},{"1":"http://swapi.co/api/planets/12/","2":"Utapau"},{"1":"http://swapi.co/api/planets/13/","2":"Mustafar"},{"1":"http://swapi.co/api/planets/14/","2":"Kashyyyk"},{"1":"http://swapi.co/api/planets/15/","2":"Polis Massa"},{"1":"http://swapi.co/api/planets/16/","2":"Mygeeto"},{"1":"http://swapi.co/api/planets/17/","2":"Felucia"},{"1":"http://swapi.co/api/planets/18/","2":"Cato Neimoidia"},{"1":"http://swapi.co/api/planets/19/","2":"Saleucami"},{"1":"http://swapi.co/api/planets/20/","2":"Stewjon"},{"1":"http://swapi.co/api/planets/21/","2":"Eriadu"},{"1":"http://swapi.co/api/planets/22/","2":"Corellia"},{"1":"http://swapi.co/api/planets/23/","2":"Rodia"},{"1":"http://swapi.co/api/planets/24/","2":"Nal Hutta"},{"1":"http://swapi.co/api/planets/25/","2":"Dantooine"},{"1":"http://swapi.co/api/planets/26/","2":"Bestine IV"},{"1":"http://swapi.co/api/planets/27/","2":"Ord Mantell"},{"1":"http://swapi.co/api/planets/28/","2":"unknown"},{"1":"http://swapi.co/api/planets/29/","2":"Trandosha"},{"1":"http://swapi.co/api/planets/30/","2":"Socorro"},{"1":"http://swapi.co/api/planets/31/","2":"Mon Cala"},{"1":"http://swapi.co/api/planets/32/","2":"Chandrila"},{"1":"http://swapi.co/api/planets/33/","2":"Sullust"},{"1":"http://swapi.co/api/planets/34/","2":"Toydaria"},{"1":"http://swapi.co/api/planets/35/","2":"Malastare"},{"1":"http://swapi.co/api/planets/36/","2":"Dathomir"},{"1":"http://swapi.co/api/planets/37/","2":"Ryloth"},{"1":"http://swapi.co/api/planets/38/","2":"Aleen Minor"},{"1":"http://swapi.co/api/planets/39/","2":"Vulpter"},{"1":"http://swapi.co/api/planets/40/","2":"Troiken"},{"1":"http://swapi.co/api/planets/41/","2":"Tund"},{"1":"http://swapi.co/api/planets/42/","2":"Haruun Kal"},{"1":"http://swapi.co/api/planets/43/","2":"Cerea"},{"1":"http://swapi.co/api/planets/44/","2":"Glee Anselm"},{"1":"http://swapi.co/api/planets/45/","2":"Iridonia"},{"1":"http://swapi.co/api/planets/46/","2":"Tholoth"},{"1":"http://swapi.co/api/planets/47/","2":"Iktotch"},{"1":"http://swapi.co/api/planets/48/","2":"Quermia"},{"1":"http://swapi.co/api/planets/49/","2":"Dorin"},{"1":"http://swapi.co/api/planets/50/","2":"Champala"},{"1":"http://swapi.co/api/planets/51/","2":"Mirial"},{"1":"http://swapi.co/api/planets/52/","2":"Serenno"},{"1":"http://swapi.co/api/planets/53/","2":"Concord Dawn"},{"1":"http://swapi.co/api/planets/54/","2":"Zolan"},{"1":"http://swapi.co/api/planets/55/","2":"Ojom"},{"1":"http://swapi.co/api/planets/56/","2":"Skako"},{"1":"http://swapi.co/api/planets/57/","2":"Muunilinst"},{"1":"http://swapi.co/api/planets/58/","2":"Shili"},{"1":"http://swapi.co/api/planets/59/","2":"Kalee"},{"1":"http://swapi.co/api/planets/60/","2":"Umbara"},{"1":"http://swapi.co/api/planets/1/","2":"Tatooine"},{"1":"http://swapi.co/api/planets/61/","2":"Jakku"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>Checking the prepared data frames, we can see that <code>url</code> column of each data frame is used as a key, so the relationship between the data frames can be summarized as shown in the following figure<sup>4</sup>.</p>
<p>Since it is common for a film to have multiple characters, it is important to note that <code>characters</code> column of <code>films</code> is a list of characters, so it isn’t possible to join <code>characters</code> column of <code>films</code> to <code>url</code> column of <code>people</code>.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">flowchart TB
  films.characters --&gt; people.url
  people.homeworld --&gt; planets.url
  subgraph films
    films.url[url]
    films.title[title]
    films.characters[List of characters] 
  end
  subgraph people
    people.url[url]
    people.name[name]
    people.homeworld[homeworld]
  end
  subgraph planets
    planets.url[url]
    planets.name[name]
  end
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<p>Therefore, we consider creating a new data file, <code>films_x_characters</code>, that represents the relationship between <code>films</code> and <code>people</code>, i.e., which characters appear in which films<sup>5</sup>．Through <code>films_x_characters</code>, the relationship between data can be summarized as shown in the following figure.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">flowchart TB
  films_x_characters.url --&gt; films.url
  films_x_characters.characters ---&gt; people.url
  people.homeworld --&gt; planets.url
  subgraph films_x_characters
    films_x_characters.url[url]
    films_x_characters.characters[characters]
  end
  subgraph films
    films.url[url]
    films.title[title]
  end
  subgraph people
    people.url[url]
    people.name[name]
    people.homeworld[homeworld]
  end
  subgraph planets
    planets.url[url]
    planets.name[name]
  end
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<p>Now, let’s build a relational data model according to the above image. First, create <code>films_x_characters</code> using <code>url</code> and <code>characters</code> columns of <code>films</code>. In addition, I delete the unnecessary <code>characters</code> column from <code>films</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create films_x_characters and remove characters column from films</span></span>
<span id="cb11-2">films_x_characters <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> films <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(url, characters) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest_longer</span>(characters)</span>
<span id="cb11-5">films <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> films <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>characters)</span>
<span id="cb11-7"></span>
<span id="cb11-8">films_x_characters</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["url"],"name":[1],"type":["chr"],"align":["left"]},{"label":["characters"],"name":[2],"type":["chr"],"align":["left"]}],"data":[{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/1/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/2/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/3/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/4/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/5/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/6/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/7/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/8/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/9/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/10/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/12/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/13/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/14/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/15/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/16/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/18/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/19/"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/81/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/2/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/3/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/6/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/7/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/10/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/11/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/20/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/21/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/22/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/33/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/36/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/40/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/43/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/46/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/51/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/52/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/53/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/58/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/59/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/60/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/61/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/62/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/63/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/64/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/65/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/66/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/67/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/68/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/69/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/70/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/71/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/72/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/73/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/74/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/75/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/76/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/77/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/78/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/82/"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/35/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/2/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/3/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/10/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/11/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/16/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/20/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/21/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/32/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/33/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/34/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/36/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/37/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/38/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/39/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/40/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/41/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/42/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/43/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/44/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/46/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/48/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/49/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/50/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/51/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/52/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/53/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/54/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/55/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/56/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/57/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/58/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/59/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/47/"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/35/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/1/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/2/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/3/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/4/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/5/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/6/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/7/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/10/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/11/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/12/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/13/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/20/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/21/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/33/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/46/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/51/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/52/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/53/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/54/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/55/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/56/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/58/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/63/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/64/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/67/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/68/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/75/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/78/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/79/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/80/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/81/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/82/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/83/"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/35/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/1/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/2/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/3/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/4/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/5/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/10/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/13/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/14/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/16/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/18/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/20/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/21/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/22/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/25/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/27/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/28/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/29/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/30/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/31/"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/45/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/1/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/2/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/3/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/4/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/5/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/10/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/13/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/14/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/18/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/20/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/21/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/22/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/23/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/24/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/25/"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/26/"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/1/"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/3/"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/5/"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/13/"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/14/"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/27/"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/84/"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/85/"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/86/"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/87/"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/88/"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>Finally, after passing the prepared <code>films</code>, <code>people</code>, <code>planets</code>, and <code>films_x_characters</code> to <code>dm()</code>, a relational data model can be built by adding primary keys and foreign keys.</p>
<p>In dm, primary keys are set with <code>dm_add_pk()</code><sup>6</sup> and foreign keys with <code>dm_add_fk()</code><sup>7</sup>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">dm_starwars_1 <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;">dm</span>(films, people, planets, films_x_characters) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-2">  </span>
<span id="cb12-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Add primary keys</span></span>
<span id="cb12-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_add_pk</span>(films, url) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_add_pk</span>(people, url) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_add_pk</span>(planets, url) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_add_pk</span>(films_x_characters, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(url, characters)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-8">  </span>
<span id="cb12-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Add foreign keys</span></span>
<span id="cb12-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_add_fk</span>(films_x_characters, url, films) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_add_fk</span>(films_x_characters, characters, people) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_add_fk</span>(people, homeworld, planets) </span>
<span id="cb12-13"></span>
<span id="cb12-14">dm_starwars_1</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>── Metadata ────────────────────────────────────────────────────────────────────
Tables: `films`, `people`, `planets`, `films_x_characters`
Columns: 10
Primary keys: 4
Foreign keys: 3</code></pre>
</div>
</div>
<p>The relational data model can be drawn using <code>dm_draw()</code>. The drawing reveals that the same relationships are built as in the image above.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_draw</span>(dm_starwars_1)</span></code></pre></div>
<div class="cell-output-display">
<div class="grViz html-widget html-fill-item" id="htmlwidget-7b93d539b73a663468ec" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="htmlwidget-7b93d539b73a663468ec">{"x":{"diagram":"#data_model\ndigraph {\ngraph [rankdir=LR tooltip=\"Data Model\" ]\n\nnode [margin=0 fontcolor = \"#444444\" ]\n\nedge [color = \"#555555\", arrowsize = 1, ]\n\npack=true\npackmode= \"node\"\n\n  \"films\" [id = \"films\", label = <<TABLE ALIGN=\"LEFT\" BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" COLOR=\"#555555\">\n    <TR>\n      <TD COLSPAN=\"1\" BGCOLOR=\"#EFEBDD\" BORDER=\"0\"><FONT COLOR=\"#000000\">films<\/FONT>\n<\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"url\"><U>url<\/U><\/TD>\n    <\/TR>\n  <\/TABLE>>, shape = \"plaintext\"] \n\n  \"films_x_characters\" [id = \"films_x_characters\", label = <<TABLE ALIGN=\"LEFT\" BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" COLOR=\"#555555\">\n    <TR>\n      <TD COLSPAN=\"1\" BGCOLOR=\"#EFEBDD\" BORDER=\"0\"><FONT COLOR=\"#000000\">films_x_characters<\/FONT>\n<\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"url\">url<\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"characters\">characters<\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"url, characters\"><U>url, characters<\/U><\/TD>\n    <\/TR>\n  <\/TABLE>>, shape = \"plaintext\"] \n\n  \"people\" [id = \"people\", label = <<TABLE ALIGN=\"LEFT\" BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" COLOR=\"#555555\">\n    <TR>\n      <TD COLSPAN=\"1\" BGCOLOR=\"#EFEBDD\" BORDER=\"0\"><FONT COLOR=\"#000000\">people<\/FONT>\n<\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"url\"><U>url<\/U><\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"homeworld\">homeworld<\/TD>\n    <\/TR>\n  <\/TABLE>>, shape = \"plaintext\"] \n\n  \"planets\" [id = \"planets\", label = <<TABLE ALIGN=\"LEFT\" BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" COLOR=\"#555555\">\n    <TR>\n      <TD COLSPAN=\"1\" BGCOLOR=\"#EFEBDD\" BORDER=\"0\"><FONT COLOR=\"#000000\">planets<\/FONT>\n<\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"url\"><U>url<\/U><\/TD>\n    <\/TR>\n  <\/TABLE>>, shape = \"plaintext\"] \n\n\"films_x_characters\":\"url\"->\"films\":\"url\" [id=\"films_x_characters_1\"]\n\"films_x_characters\":\"characters\"->\"people\":\"url\" [id=\"films_x_characters_2\"]\n\"people\":\"homeworld\"->\"planets\":\"url\" [id=\"people_1\"]\n}","config":{"engine":null,"options":null}},"evals":[],"jsHooks":[]}</script>
</div>
</div>
<p>By using <code>dm_flatten_to_tbl()</code> as shown below, a data frame can be created by joining <code>films_x_characters</code> data with <code>films</code>/<code>people</code>/<code>planets</code> data. In this case, if the same column names exist between different data, the column names are automatically renamed according to the data names. In this way, the relational data model manages the relationships between data on our behalf, allowing us to automatically join data frames based on the relationships among other data.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">data_films_x_characters_1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dm_starwars_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb15-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_flatten_to_tbl</span>(films_x_characters,</span>
<span id="cb15-3">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.recursive =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) </span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Renaming ambiguous columns: %&gt;%
  dm_rename(people, name.people = name) %&gt;%
  dm_rename(planets, name.planets = name)</code></pre>
</div>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">data_films_x_characters_1</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["url"],"name":[1],"type":["chr"],"align":["left"]},{"label":["characters"],"name":[2],"type":["chr"],"align":["left"]},{"label":["title"],"name":[3],"type":["chr"],"align":["left"]},{"label":["name.people"],"name":[4],"type":["chr"],"align":["left"]},{"label":["homeworld"],"name":[5],"type":["chr"],"align":["left"]},{"label":["species"],"name":[6],"type":["chr"],"align":["left"]},{"label":["name.planets"],"name":[7],"type":["chr"],"align":["left"]}],"data":[{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/1/","3":"A New Hope","4":"Luke Skywalker","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/2/","3":"A New Hope","4":"C-3PO","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/2/","7":"Tatooine"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/3/","3":"A New Hope","4":"R2-D2","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/2/","7":"Naboo"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/4/","3":"A New Hope","4":"Darth Vader","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/5/","3":"A New Hope","4":"Leia Organa","5":"http://swapi.co/api/planets/2/","6":"http://swapi.co/api/species/1/","7":"Alderaan"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/6/","3":"A New Hope","4":"Owen Lars","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/7/","3":"A New Hope","4":"Beru Whitesun lars","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/8/","3":"A New Hope","4":"R5-D4","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/2/","7":"Tatooine"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/9/","3":"A New Hope","4":"Biggs Darklighter","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/10/","3":"A New Hope","4":"Obi-Wan Kenobi","5":"http://swapi.co/api/planets/20/","6":"http://swapi.co/api/species/1/","7":"Stewjon"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/12/","3":"A New Hope","4":"Wilhuff Tarkin","5":"http://swapi.co/api/planets/21/","6":"http://swapi.co/api/species/1/","7":"Eriadu"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/13/","3":"A New Hope","4":"Chewbacca","5":"http://swapi.co/api/planets/14/","6":"http://swapi.co/api/species/3/","7":"Kashyyyk"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/14/","3":"A New Hope","4":"Han Solo","5":"http://swapi.co/api/planets/22/","6":"http://swapi.co/api/species/1/","7":"Corellia"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/15/","3":"A New Hope","4":"Greedo","5":"http://swapi.co/api/planets/23/","6":"http://swapi.co/api/species/4/","7":"Rodia"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/16/","3":"A New Hope","4":"Jabba Desilijic Tiure","5":"http://swapi.co/api/planets/24/","6":"http://swapi.co/api/species/5/","7":"Nal Hutta"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/18/","3":"A New Hope","4":"Wedge Antilles","5":"http://swapi.co/api/planets/22/","6":"http://swapi.co/api/species/1/","7":"Corellia"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/19/","3":"A New Hope","4":"Jek Tono Porkins","5":"http://swapi.co/api/planets/26/","6":"http://swapi.co/api/species/1/","7":"Bestine IV"},{"1":"http://swapi.co/api/films/1/","2":"http://swapi.co/api/people/81/","3":"A New Hope","4":"Raymus Antilles","5":"http://swapi.co/api/planets/2/","6":"http://swapi.co/api/species/1/","7":"Alderaan"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/2/","3":"Attack of the Clones","4":"C-3PO","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/2/","7":"Tatooine"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/3/","3":"Attack of the Clones","4":"R2-D2","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/2/","7":"Naboo"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/6/","3":"Attack of the Clones","4":"Owen Lars","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/7/","3":"Attack of the Clones","4":"Beru Whitesun lars","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/10/","3":"Attack of the Clones","4":"Obi-Wan Kenobi","5":"http://swapi.co/api/planets/20/","6":"http://swapi.co/api/species/1/","7":"Stewjon"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/11/","3":"Attack of the Clones","4":"Anakin Skywalker","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/20/","3":"Attack of the Clones","4":"Yoda","5":"http://swapi.co/api/planets/28/","6":"http://swapi.co/api/species/6/","7":"unknown"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/21/","3":"Attack of the Clones","4":"Palpatine","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/1/","7":"Naboo"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/22/","3":"Attack of the Clones","4":"Boba Fett","5":"http://swapi.co/api/planets/10/","6":"http://swapi.co/api/species/1/","7":"Kamino"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/33/","3":"Attack of the Clones","4":"Nute Gunray","5":"http://swapi.co/api/planets/18/","6":"http://swapi.co/api/species/11/","7":"Cato Neimoidia"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/36/","3":"Attack of the Clones","4":"Jar Jar Binks","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/12/","7":"Naboo"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/40/","3":"Attack of the Clones","4":"Watto","5":"http://swapi.co/api/planets/34/","6":"http://swapi.co/api/species/13/","7":"Toydaria"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/43/","3":"Attack of the Clones","4":"Shmi Skywalker","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/46/","3":"Attack of the Clones","4":"Ayla Secura","5":"http://swapi.co/api/planets/37/","6":"http://swapi.co/api/species/15/","7":"Ryloth"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/51/","3":"Attack of the Clones","4":"Mace Windu","5":"http://swapi.co/api/planets/42/","6":"http://swapi.co/api/species/1/","7":"Haruun Kal"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/52/","3":"Attack of the Clones","4":"Ki-Adi-Mundi","5":"http://swapi.co/api/planets/43/","6":"http://swapi.co/api/species/20/","7":"Cerea"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/53/","3":"Attack of the Clones","4":"Kit Fisto","5":"http://swapi.co/api/planets/44/","6":"http://swapi.co/api/species/21/","7":"Glee Anselm"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/58/","3":"Attack of the Clones","4":"Plo Koon","5":"http://swapi.co/api/planets/49/","6":"http://swapi.co/api/species/26/","7":"Dorin"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/59/","3":"Attack of the Clones","4":"Mas Amedda","5":"http://swapi.co/api/planets/50/","6":"http://swapi.co/api/species/27/","7":"Champala"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/60/","3":"Attack of the Clones","4":"Gregar Typho","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/1/","7":"Naboo"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/61/","3":"Attack of the Clones","4":"Cordé","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/1/","7":"Naboo"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/62/","3":"Attack of the Clones","4":"Cliegg Lars","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/63/","3":"Attack of the Clones","4":"Poggle the Lesser","5":"http://swapi.co/api/planets/11/","6":"http://swapi.co/api/species/28/","7":"Geonosis"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/64/","3":"Attack of the Clones","4":"Luminara Unduli","5":"http://swapi.co/api/planets/51/","6":"http://swapi.co/api/species/29/","7":"Mirial"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/65/","3":"Attack of the Clones","4":"Barriss Offee","5":"http://swapi.co/api/planets/51/","6":"http://swapi.co/api/species/29/","7":"Mirial"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/66/","3":"Attack of the Clones","4":"Dormé","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/1/","7":"Naboo"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/67/","3":"Attack of the Clones","4":"Dooku","5":"http://swapi.co/api/planets/52/","6":"http://swapi.co/api/species/1/","7":"Serenno"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/68/","3":"Attack of the Clones","4":"Bail Prestor Organa","5":"http://swapi.co/api/planets/2/","6":"http://swapi.co/api/species/1/","7":"Alderaan"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/69/","3":"Attack of the Clones","4":"Jango Fett","5":"http://swapi.co/api/planets/53/","6":"http://swapi.co/api/species/1/","7":"Concord Dawn"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/70/","3":"Attack of the Clones","4":"Zam Wesell","5":"http://swapi.co/api/planets/54/","6":"http://swapi.co/api/species/30/","7":"Zolan"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/71/","3":"Attack of the Clones","4":"Dexter Jettster","5":"http://swapi.co/api/planets/55/","6":"http://swapi.co/api/species/31/","7":"Ojom"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/72/","3":"Attack of the Clones","4":"Lama Su","5":"http://swapi.co/api/planets/10/","6":"http://swapi.co/api/species/32/","7":"Kamino"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/73/","3":"Attack of the Clones","4":"Taun We","5":"http://swapi.co/api/planets/10/","6":"http://swapi.co/api/species/32/","7":"Kamino"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/74/","3":"Attack of the Clones","4":"Jocasta Nu","5":"http://swapi.co/api/planets/9/","6":"http://swapi.co/api/species/1/","7":"Coruscant"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/75/","3":"Attack of the Clones","4":"R4-P17","5":"http://swapi.co/api/planets/28/","6":"NA","7":"unknown"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/76/","3":"Attack of the Clones","4":"Wat Tambor","5":"http://swapi.co/api/planets/56/","6":"http://swapi.co/api/species/33/","7":"Skako"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/77/","3":"Attack of the Clones","4":"San Hill","5":"http://swapi.co/api/planets/57/","6":"http://swapi.co/api/species/34/","7":"Muunilinst"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/78/","3":"Attack of the Clones","4":"Shaak Ti","5":"http://swapi.co/api/planets/58/","6":"http://swapi.co/api/species/35/","7":"Shili"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/82/","3":"Attack of the Clones","4":"Sly Moore","5":"http://swapi.co/api/planets/60/","6":"NA","7":"Umbara"},{"1":"http://swapi.co/api/films/5/","2":"http://swapi.co/api/people/35/","3":"Attack of the Clones","4":"Padmé Amidala","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/1/","7":"Naboo"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/2/","3":"The Phantom Menace","4":"C-3PO","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/2/","7":"Tatooine"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/3/","3":"The Phantom Menace","4":"R2-D2","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/2/","7":"Naboo"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/10/","3":"The Phantom Menace","4":"Obi-Wan Kenobi","5":"http://swapi.co/api/planets/20/","6":"http://swapi.co/api/species/1/","7":"Stewjon"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/11/","3":"The Phantom Menace","4":"Anakin Skywalker","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/16/","3":"The Phantom Menace","4":"Jabba Desilijic Tiure","5":"http://swapi.co/api/planets/24/","6":"http://swapi.co/api/species/5/","7":"Nal Hutta"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/20/","3":"The Phantom Menace","4":"Yoda","5":"http://swapi.co/api/planets/28/","6":"http://swapi.co/api/species/6/","7":"unknown"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/21/","3":"The Phantom Menace","4":"Palpatine","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/1/","7":"Naboo"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/32/","3":"The Phantom Menace","4":"Qui-Gon Jinn","5":"http://swapi.co/api/planets/28/","6":"http://swapi.co/api/species/1/","7":"unknown"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/33/","3":"The Phantom Menace","4":"Nute Gunray","5":"http://swapi.co/api/planets/18/","6":"http://swapi.co/api/species/11/","7":"Cato Neimoidia"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/34/","3":"The Phantom Menace","4":"Finis Valorum","5":"http://swapi.co/api/planets/9/","6":"http://swapi.co/api/species/1/","7":"Coruscant"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/36/","3":"The Phantom Menace","4":"Jar Jar Binks","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/12/","7":"Naboo"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/37/","3":"The Phantom Menace","4":"Roos Tarpals","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/12/","7":"Naboo"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/38/","3":"The Phantom Menace","4":"Rugor Nass","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/12/","7":"Naboo"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/39/","3":"The Phantom Menace","4":"Ric Olié","5":"http://swapi.co/api/planets/8/","6":"NA","7":"Naboo"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/40/","3":"The Phantom Menace","4":"Watto","5":"http://swapi.co/api/planets/34/","6":"http://swapi.co/api/species/13/","7":"Toydaria"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/41/","3":"The Phantom Menace","4":"Sebulba","5":"http://swapi.co/api/planets/35/","6":"http://swapi.co/api/species/14/","7":"Malastare"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/42/","3":"The Phantom Menace","4":"Quarsh Panaka","5":"http://swapi.co/api/planets/8/","6":"NA","7":"Naboo"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/43/","3":"The Phantom Menace","4":"Shmi Skywalker","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/44/","3":"The Phantom Menace","4":"Darth Maul","5":"http://swapi.co/api/planets/36/","6":"http://swapi.co/api/species/22/","7":"Dathomir"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/46/","3":"The Phantom Menace","4":"Ayla Secura","5":"http://swapi.co/api/planets/37/","6":"http://swapi.co/api/species/15/","7":"Ryloth"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/48/","3":"The Phantom Menace","4":"Dud Bolt","5":"http://swapi.co/api/planets/39/","6":"http://swapi.co/api/species/17/","7":"Vulpter"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/49/","3":"The Phantom Menace","4":"Gasgano","5":"http://swapi.co/api/planets/40/","6":"http://swapi.co/api/species/18/","7":"Troiken"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/50/","3":"The Phantom Menace","4":"Ben Quadinaros","5":"http://swapi.co/api/planets/41/","6":"http://swapi.co/api/species/19/","7":"Tund"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/51/","3":"The Phantom Menace","4":"Mace Windu","5":"http://swapi.co/api/planets/42/","6":"http://swapi.co/api/species/1/","7":"Haruun Kal"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/52/","3":"The Phantom Menace","4":"Ki-Adi-Mundi","5":"http://swapi.co/api/planets/43/","6":"http://swapi.co/api/species/20/","7":"Cerea"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/53/","3":"The Phantom Menace","4":"Kit Fisto","5":"http://swapi.co/api/planets/44/","6":"http://swapi.co/api/species/21/","7":"Glee Anselm"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/54/","3":"The Phantom Menace","4":"Eeth Koth","5":"http://swapi.co/api/planets/45/","6":"http://swapi.co/api/species/22/","7":"Iridonia"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/55/","3":"The Phantom Menace","4":"Adi Gallia","5":"http://swapi.co/api/planets/9/","6":"http://swapi.co/api/species/23/","7":"Coruscant"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/56/","3":"The Phantom Menace","4":"Saesee Tiin","5":"http://swapi.co/api/planets/47/","6":"http://swapi.co/api/species/24/","7":"Iktotch"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/57/","3":"The Phantom Menace","4":"Yarael Poof","5":"http://swapi.co/api/planets/48/","6":"http://swapi.co/api/species/25/","7":"Quermia"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/58/","3":"The Phantom Menace","4":"Plo Koon","5":"http://swapi.co/api/planets/49/","6":"http://swapi.co/api/species/26/","7":"Dorin"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/59/","3":"The Phantom Menace","4":"Mas Amedda","5":"http://swapi.co/api/planets/50/","6":"http://swapi.co/api/species/27/","7":"Champala"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/47/","3":"The Phantom Menace","4":"Ratts Tyerell","5":"http://swapi.co/api/planets/38/","6":"http://swapi.co/api/species/16/","7":"Aleen Minor"},{"1":"http://swapi.co/api/films/4/","2":"http://swapi.co/api/people/35/","3":"The Phantom Menace","4":"Padmé Amidala","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/1/","7":"Naboo"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/1/","3":"Revenge of the Sith","4":"Luke Skywalker","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/2/","3":"Revenge of the Sith","4":"C-3PO","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/2/","7":"Tatooine"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/3/","3":"Revenge of the Sith","4":"R2-D2","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/2/","7":"Naboo"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/4/","3":"Revenge of the Sith","4":"Darth Vader","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/5/","3":"Revenge of the Sith","4":"Leia Organa","5":"http://swapi.co/api/planets/2/","6":"http://swapi.co/api/species/1/","7":"Alderaan"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/6/","3":"Revenge of the Sith","4":"Owen Lars","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/7/","3":"Revenge of the Sith","4":"Beru Whitesun lars","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/10/","3":"Revenge of the Sith","4":"Obi-Wan Kenobi","5":"http://swapi.co/api/planets/20/","6":"http://swapi.co/api/species/1/","7":"Stewjon"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/11/","3":"Revenge of the Sith","4":"Anakin Skywalker","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/12/","3":"Revenge of the Sith","4":"Wilhuff Tarkin","5":"http://swapi.co/api/planets/21/","6":"http://swapi.co/api/species/1/","7":"Eriadu"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/13/","3":"Revenge of the Sith","4":"Chewbacca","5":"http://swapi.co/api/planets/14/","6":"http://swapi.co/api/species/3/","7":"Kashyyyk"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/20/","3":"Revenge of the Sith","4":"Yoda","5":"http://swapi.co/api/planets/28/","6":"http://swapi.co/api/species/6/","7":"unknown"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/21/","3":"Revenge of the Sith","4":"Palpatine","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/1/","7":"Naboo"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/33/","3":"Revenge of the Sith","4":"Nute Gunray","5":"http://swapi.co/api/planets/18/","6":"http://swapi.co/api/species/11/","7":"Cato Neimoidia"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/46/","3":"Revenge of the Sith","4":"Ayla Secura","5":"http://swapi.co/api/planets/37/","6":"http://swapi.co/api/species/15/","7":"Ryloth"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/51/","3":"Revenge of the Sith","4":"Mace Windu","5":"http://swapi.co/api/planets/42/","6":"http://swapi.co/api/species/1/","7":"Haruun Kal"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/52/","3":"Revenge of the Sith","4":"Ki-Adi-Mundi","5":"http://swapi.co/api/planets/43/","6":"http://swapi.co/api/species/20/","7":"Cerea"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/53/","3":"Revenge of the Sith","4":"Kit Fisto","5":"http://swapi.co/api/planets/44/","6":"http://swapi.co/api/species/21/","7":"Glee Anselm"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/54/","3":"Revenge of the Sith","4":"Eeth Koth","5":"http://swapi.co/api/planets/45/","6":"http://swapi.co/api/species/22/","7":"Iridonia"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/55/","3":"Revenge of the Sith","4":"Adi Gallia","5":"http://swapi.co/api/planets/9/","6":"http://swapi.co/api/species/23/","7":"Coruscant"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/56/","3":"Revenge of the Sith","4":"Saesee Tiin","5":"http://swapi.co/api/planets/47/","6":"http://swapi.co/api/species/24/","7":"Iktotch"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/58/","3":"Revenge of the Sith","4":"Plo Koon","5":"http://swapi.co/api/planets/49/","6":"http://swapi.co/api/species/26/","7":"Dorin"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/63/","3":"Revenge of the Sith","4":"Poggle the Lesser","5":"http://swapi.co/api/planets/11/","6":"http://swapi.co/api/species/28/","7":"Geonosis"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/64/","3":"Revenge of the Sith","4":"Luminara Unduli","5":"http://swapi.co/api/planets/51/","6":"http://swapi.co/api/species/29/","7":"Mirial"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/67/","3":"Revenge of the Sith","4":"Dooku","5":"http://swapi.co/api/planets/52/","6":"http://swapi.co/api/species/1/","7":"Serenno"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/68/","3":"Revenge of the Sith","4":"Bail Prestor Organa","5":"http://swapi.co/api/planets/2/","6":"http://swapi.co/api/species/1/","7":"Alderaan"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/75/","3":"Revenge of the Sith","4":"R4-P17","5":"http://swapi.co/api/planets/28/","6":"NA","7":"unknown"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/78/","3":"Revenge of the Sith","4":"Shaak Ti","5":"http://swapi.co/api/planets/58/","6":"http://swapi.co/api/species/35/","7":"Shili"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/79/","3":"Revenge of the Sith","4":"Grievous","5":"http://swapi.co/api/planets/59/","6":"http://swapi.co/api/species/36/","7":"Kalee"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/80/","3":"Revenge of the Sith","4":"Tarfful","5":"http://swapi.co/api/planets/14/","6":"http://swapi.co/api/species/3/","7":"Kashyyyk"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/81/","3":"Revenge of the Sith","4":"Raymus Antilles","5":"http://swapi.co/api/planets/2/","6":"http://swapi.co/api/species/1/","7":"Alderaan"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/82/","3":"Revenge of the Sith","4":"Sly Moore","5":"http://swapi.co/api/planets/60/","6":"NA","7":"Umbara"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/83/","3":"Revenge of the Sith","4":"Tion Medon","5":"http://swapi.co/api/planets/12/","6":"http://swapi.co/api/species/37/","7":"Utapau"},{"1":"http://swapi.co/api/films/6/","2":"http://swapi.co/api/people/35/","3":"Revenge of the Sith","4":"Padmé Amidala","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/1/","7":"Naboo"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/1/","3":"Return of the Jedi","4":"Luke Skywalker","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/2/","3":"Return of the Jedi","4":"C-3PO","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/2/","7":"Tatooine"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/3/","3":"Return of the Jedi","4":"R2-D2","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/2/","7":"Naboo"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/4/","3":"Return of the Jedi","4":"Darth Vader","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/5/","3":"Return of the Jedi","4":"Leia Organa","5":"http://swapi.co/api/planets/2/","6":"http://swapi.co/api/species/1/","7":"Alderaan"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/10/","3":"Return of the Jedi","4":"Obi-Wan Kenobi","5":"http://swapi.co/api/planets/20/","6":"http://swapi.co/api/species/1/","7":"Stewjon"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/13/","3":"Return of the Jedi","4":"Chewbacca","5":"http://swapi.co/api/planets/14/","6":"http://swapi.co/api/species/3/","7":"Kashyyyk"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/14/","3":"Return of the Jedi","4":"Han Solo","5":"http://swapi.co/api/planets/22/","6":"http://swapi.co/api/species/1/","7":"Corellia"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/16/","3":"Return of the Jedi","4":"Jabba Desilijic Tiure","5":"http://swapi.co/api/planets/24/","6":"http://swapi.co/api/species/5/","7":"Nal Hutta"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/18/","3":"Return of the Jedi","4":"Wedge Antilles","5":"http://swapi.co/api/planets/22/","6":"http://swapi.co/api/species/1/","7":"Corellia"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/20/","3":"Return of the Jedi","4":"Yoda","5":"http://swapi.co/api/planets/28/","6":"http://swapi.co/api/species/6/","7":"unknown"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/21/","3":"Return of the Jedi","4":"Palpatine","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/1/","7":"Naboo"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/22/","3":"Return of the Jedi","4":"Boba Fett","5":"http://swapi.co/api/planets/10/","6":"http://swapi.co/api/species/1/","7":"Kamino"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/25/","3":"Return of the Jedi","4":"Lando Calrissian","5":"http://swapi.co/api/planets/30/","6":"http://swapi.co/api/species/1/","7":"Socorro"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/27/","3":"Return of the Jedi","4":"Ackbar","5":"http://swapi.co/api/planets/31/","6":"http://swapi.co/api/species/8/","7":"Mon Cala"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/28/","3":"Return of the Jedi","4":"Mon Mothma","5":"http://swapi.co/api/planets/32/","6":"http://swapi.co/api/species/1/","7":"Chandrila"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/29/","3":"Return of the Jedi","4":"Arvel Crynyd","5":"http://swapi.co/api/planets/28/","6":"http://swapi.co/api/species/1/","7":"unknown"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/30/","3":"Return of the Jedi","4":"Wicket Systri Warrick","5":"http://swapi.co/api/planets/7/","6":"http://swapi.co/api/species/9/","7":"Endor"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/31/","3":"Return of the Jedi","4":"Nien Nunb","5":"http://swapi.co/api/planets/33/","6":"http://swapi.co/api/species/10/","7":"Sullust"},{"1":"http://swapi.co/api/films/3/","2":"http://swapi.co/api/people/45/","3":"Return of the Jedi","4":"Bib Fortuna","5":"http://swapi.co/api/planets/37/","6":"http://swapi.co/api/species/15/","7":"Ryloth"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/1/","3":"The Empire Strikes Back","4":"Luke Skywalker","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/2/","3":"The Empire Strikes Back","4":"C-3PO","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/2/","7":"Tatooine"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/3/","3":"The Empire Strikes Back","4":"R2-D2","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/2/","7":"Naboo"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/4/","3":"The Empire Strikes Back","4":"Darth Vader","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/5/","3":"The Empire Strikes Back","4":"Leia Organa","5":"http://swapi.co/api/planets/2/","6":"http://swapi.co/api/species/1/","7":"Alderaan"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/10/","3":"The Empire Strikes Back","4":"Obi-Wan Kenobi","5":"http://swapi.co/api/planets/20/","6":"http://swapi.co/api/species/1/","7":"Stewjon"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/13/","3":"The Empire Strikes Back","4":"Chewbacca","5":"http://swapi.co/api/planets/14/","6":"http://swapi.co/api/species/3/","7":"Kashyyyk"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/14/","3":"The Empire Strikes Back","4":"Han Solo","5":"http://swapi.co/api/planets/22/","6":"http://swapi.co/api/species/1/","7":"Corellia"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/18/","3":"The Empire Strikes Back","4":"Wedge Antilles","5":"http://swapi.co/api/planets/22/","6":"http://swapi.co/api/species/1/","7":"Corellia"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/20/","3":"The Empire Strikes Back","4":"Yoda","5":"http://swapi.co/api/planets/28/","6":"http://swapi.co/api/species/6/","7":"unknown"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/21/","3":"The Empire Strikes Back","4":"Palpatine","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/1/","7":"Naboo"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/22/","3":"The Empire Strikes Back","4":"Boba Fett","5":"http://swapi.co/api/planets/10/","6":"http://swapi.co/api/species/1/","7":"Kamino"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/23/","3":"The Empire Strikes Back","4":"IG-88","5":"http://swapi.co/api/planets/28/","6":"http://swapi.co/api/species/2/","7":"unknown"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/24/","3":"The Empire Strikes Back","4":"Bossk","5":"http://swapi.co/api/planets/29/","6":"http://swapi.co/api/species/7/","7":"Trandosha"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/25/","3":"The Empire Strikes Back","4":"Lando Calrissian","5":"http://swapi.co/api/planets/30/","6":"http://swapi.co/api/species/1/","7":"Socorro"},{"1":"http://swapi.co/api/films/2/","2":"http://swapi.co/api/people/26/","3":"The Empire Strikes Back","4":"Lobot","5":"http://swapi.co/api/planets/6/","6":"http://swapi.co/api/species/1/","7":"Bespin"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/1/","3":"The Force Awakens","4":"Luke Skywalker","5":"http://swapi.co/api/planets/1/","6":"http://swapi.co/api/species/1/","7":"Tatooine"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/3/","3":"The Force Awakens","4":"R2-D2","5":"http://swapi.co/api/planets/8/","6":"http://swapi.co/api/species/2/","7":"Naboo"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/5/","3":"The Force Awakens","4":"Leia Organa","5":"http://swapi.co/api/planets/2/","6":"http://swapi.co/api/species/1/","7":"Alderaan"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/13/","3":"The Force Awakens","4":"Chewbacca","5":"http://swapi.co/api/planets/14/","6":"http://swapi.co/api/species/3/","7":"Kashyyyk"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/14/","3":"The Force Awakens","4":"Han Solo","5":"http://swapi.co/api/planets/22/","6":"http://swapi.co/api/species/1/","7":"Corellia"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/27/","3":"The Force Awakens","4":"Ackbar","5":"http://swapi.co/api/planets/31/","6":"http://swapi.co/api/species/8/","7":"Mon Cala"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/84/","3":"The Force Awakens","4":"Finn","5":"http://swapi.co/api/planets/28/","6":"http://swapi.co/api/species/1/","7":"unknown"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/85/","3":"The Force Awakens","4":"Rey","5":"http://swapi.co/api/planets/28/","6":"http://swapi.co/api/species/1/","7":"unknown"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/86/","3":"The Force Awakens","4":"Poe Dameron","5":"http://swapi.co/api/planets/28/","6":"http://swapi.co/api/species/1/","7":"unknown"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/87/","3":"The Force Awakens","4":"BB8","5":"http://swapi.co/api/planets/28/","6":"http://swapi.co/api/species/2/","7":"unknown"},{"1":"http://swapi.co/api/films/7/","2":"http://swapi.co/api/people/88/","3":"The Force Awakens","4":"Captain Phasma","5":"http://swapi.co/api/planets/28/","6":"NA","7":"unknown"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>The <code>data_films_x_characters_1</code> can be used to plot the composition ratio of the characters’ home planets, as shown below. We will not discuss this plot further. However, we have confirmed that the relational data model can be used to automate the joining of data frames.</p>
<p>In the above analysis, it is easy to join data frames using <code>left_join()</code>, and you may not see the advantage of using a relational data model. In the application section, we will consider a more complex situation where a relational data model prevails.</p>
<div class="cell">
<details class="code-fold">
<summary>Plot of the composition of the characters’ home planets</summary>
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">data_films_x_characters_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb18-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name.planets =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_lump_n</span>(name.planets, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,</span>
<span id="cb18-3">                                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb18-4">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_relevel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</span>, </span>
<span id="cb18-5">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">after =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb18-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(title, name.planets) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb18-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;">prop =</span> n <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;">sum</span>(n),</span>
<span id="cb18-8">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> title,</span>
<span id="cb18-9">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unused"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb18-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_rev</span>(title), prop,</span>
<span id="cb18-11">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> name.planets)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_stack</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reverse =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</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;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(prop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5e-2</span>, </span>
<span id="cb18-14">                                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb18-15">                                scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label_percent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)(prop))),</span>
<span id="cb18-16">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_stack</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb18-17">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reverse =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_discrete</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Title"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Composition of the characters' home planets"</span>,</span>
<span id="cb18-20">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Planet Name"</span>,</span>
<span id="cb18-22">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guide_legend</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb18-26">                             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/01/relational-data-models-with-r-s-dm_files/figure-html/plot-starwars-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="application-extending-the-model-and-validating-keys-with-dm" class="level3">
<h3 class="anchored" data-anchor-id="application-extending-the-model-and-validating-keys-with-dm">2. Application: Extending the model and validating keys with dm</h3>
<p>In the application, we will examine the composition ratio of the species of the characters in Star Wars movies. The difficulty level of this analysis is not much different from that of the basic analysis. However, as the amount of data to be handled increases, the code tends to become more complicated, and the advantages of using a relational data model are significant. In addition, the advantages of the relational data model include the following,</p>
<ul>
<li>New data can be added to the existing relational data model</li>
<li>Possible to check key constraints defined to join data frames</li>
</ul>
<p>We prepare in advance the <code>species</code> data needed for this analysis.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">species <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">species =</span> sw_species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb19-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest_wider</span>(species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb19-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(url, name)</span>
<span id="cb19-4"></span>
<span id="cb19-5">species</span></code></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["url"],"name":[1],"type":["chr"],"align":["left"]},{"label":["name"],"name":[2],"type":["chr"],"align":["left"]}],"data":[{"1":"http://swapi.co/api/species/5/","2":"Hutt"},{"1":"http://swapi.co/api/species/6/","2":"Yoda's species"},{"1":"http://swapi.co/api/species/7/","2":"Trandoshan"},{"1":"http://swapi.co/api/species/8/","2":"Mon Calamari"},{"1":"http://swapi.co/api/species/9/","2":"Ewok"},{"1":"http://swapi.co/api/species/10/","2":"Sullustan"},{"1":"http://swapi.co/api/species/11/","2":"Neimodian"},{"1":"http://swapi.co/api/species/12/","2":"Gungan"},{"1":"http://swapi.co/api/species/13/","2":"Toydarian"},{"1":"http://swapi.co/api/species/14/","2":"Dug"},{"1":"http://swapi.co/api/species/15/","2":"Twi'lek"},{"1":"http://swapi.co/api/species/16/","2":"Aleena"},{"1":"http://swapi.co/api/species/17/","2":"Vulptereen"},{"1":"http://swapi.co/api/species/18/","2":"Xexto"},{"1":"http://swapi.co/api/species/19/","2":"Toong"},{"1":"http://swapi.co/api/species/20/","2":"Cerean"},{"1":"http://swapi.co/api/species/21/","2":"Nautolan"},{"1":"http://swapi.co/api/species/22/","2":"Zabrak"},{"1":"http://swapi.co/api/species/23/","2":"Tholothian"},{"1":"http://swapi.co/api/species/24/","2":"Iktotchi"},{"1":"http://swapi.co/api/species/25/","2":"Quermian"},{"1":"http://swapi.co/api/species/26/","2":"Kel Dor"},{"1":"http://swapi.co/api/species/27/","2":"Chagrian"},{"1":"http://swapi.co/api/species/28/","2":"Geonosian"},{"1":"http://swapi.co/api/species/29/","2":"Mirialan"},{"1":"http://swapi.co/api/species/30/","2":"Clawdite"},{"1":"http://swapi.co/api/species/31/","2":"Besalisk"},{"1":"http://swapi.co/api/species/32/","2":"Kaminoan"},{"1":"http://swapi.co/api/species/33/","2":"Skakoan"},{"1":"http://swapi.co/api/species/34/","2":"Muun"},{"1":"http://swapi.co/api/species/35/","2":"Togruta"},{"1":"http://swapi.co/api/species/36/","2":"Kaleesh"},{"1":"http://swapi.co/api/species/37/","2":"Pau'an"},{"1":"http://swapi.co/api/species/3/","2":"Wookiee"},{"1":"http://swapi.co/api/species/2/","2":"Droid"},{"1":"http://swapi.co/api/species/1/","2":"Human"},{"1":"http://swapi.co/api/species/4/","2":"Rodian"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>In dm, we can add new data to the relational data model using <code>dm()</code>. In this example, let’s build <code>dm_starwars_2</code> by adding the <code>species</code> data to <code>dm_starwars_1</code>. By using <code>dm_draw()</code>, we can see that the model has been updated.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">dm_starwars_2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dm_starwars_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb20-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm</span>(species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb20-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_add_pk</span>(species, url) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb20-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_add_fk</span>(people, species, species)</span>
<span id="cb20-5"></span>
<span id="cb20-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_draw</span>(dm_starwars_2)</span></code></pre></div>
<div class="cell-output-display">
<div class="grViz html-widget html-fill-item" id="htmlwidget-d0d4b4323a0131f0336f" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="htmlwidget-d0d4b4323a0131f0336f">{"x":{"diagram":"#data_model\ndigraph {\ngraph [rankdir=LR tooltip=\"Data Model\" ]\n\nnode [margin=0 fontcolor = \"#444444\" ]\n\nedge [color = \"#555555\", arrowsize = 1, ]\n\npack=true\npackmode= \"node\"\n\n  \"films\" [id = \"films\", label = <<TABLE ALIGN=\"LEFT\" BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" COLOR=\"#555555\">\n    <TR>\n      <TD COLSPAN=\"1\" BGCOLOR=\"#EFEBDD\" BORDER=\"0\"><FONT COLOR=\"#000000\">films<\/FONT>\n<\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"url\"><U>url<\/U><\/TD>\n    <\/TR>\n  <\/TABLE>>, shape = \"plaintext\"] \n\n  \"films_x_characters\" [id = \"films_x_characters\", label = <<TABLE ALIGN=\"LEFT\" BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" COLOR=\"#555555\">\n    <TR>\n      <TD COLSPAN=\"1\" BGCOLOR=\"#EFEBDD\" BORDER=\"0\"><FONT COLOR=\"#000000\">films_x_characters<\/FONT>\n<\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"url\">url<\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"characters\">characters<\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"url, characters\"><U>url, characters<\/U><\/TD>\n    <\/TR>\n  <\/TABLE>>, shape = \"plaintext\"] \n\n  \"people\" [id = \"people\", label = <<TABLE ALIGN=\"LEFT\" BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" COLOR=\"#555555\">\n    <TR>\n      <TD COLSPAN=\"1\" BGCOLOR=\"#EFEBDD\" BORDER=\"0\"><FONT COLOR=\"#000000\">people<\/FONT>\n<\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"url\"><U>url<\/U><\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"homeworld\">homeworld<\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"species\">species<\/TD>\n    <\/TR>\n  <\/TABLE>>, shape = \"plaintext\"] \n\n  \"planets\" [id = \"planets\", label = <<TABLE ALIGN=\"LEFT\" BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" COLOR=\"#555555\">\n    <TR>\n      <TD COLSPAN=\"1\" BGCOLOR=\"#EFEBDD\" BORDER=\"0\"><FONT COLOR=\"#000000\">planets<\/FONT>\n<\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"url\"><U>url<\/U><\/TD>\n    <\/TR>\n  <\/TABLE>>, shape = \"plaintext\"] \n\n  \"species\" [id = \"species\", label = <<TABLE ALIGN=\"LEFT\" BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" COLOR=\"#555555\">\n    <TR>\n      <TD COLSPAN=\"1\" BGCOLOR=\"#EFEBDD\" BORDER=\"0\"><FONT COLOR=\"#000000\">species<\/FONT>\n<\/TD>\n    <\/TR>\n    <TR>\n      <TD ALIGN=\"LEFT\" BGCOLOR=\"#FFFFFF\" PORT=\"url\"><U>url<\/U><\/TD>\n    <\/TR>\n  <\/TABLE>>, shape = \"plaintext\"] \n\n\"films_x_characters\":\"url\"->\"films\":\"url\" [id=\"films_x_characters_1\"]\n\"films_x_characters\":\"characters\"->\"people\":\"url\" [id=\"films_x_characters_2\"]\n\"people\":\"homeworld\"->\"planets\":\"url\" [id=\"people_1\"]\n\"people\":\"species\"->\"species\":\"url\" [id=\"people_2\"]\n}","config":{"engine":null,"options":null}},"evals":[],"jsHooks":[]}</script>
</div>
</div>
<p>Next, let us check key constraints to join data frames. This can be done using <code>dm_examine_constraints()</code>. Let’s check the behavior of <code>dm_examine_constraints()</code> by building a model that contains the two types of mistakes mentioned above as common mistakes. Here, <code>dm_starwars_2_strong_data</code> is data in which the first row of the <code>species</code> data has been deleted and the data is not “MECE”. <code>dm_starwars_2_wrong_pk</code> is the data with the wrong primary key in the <code>species</code> data.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">dm_starwars_2_wrong_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dm_starwars_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">species =</span> species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-3">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_add_pk</span>(species, url) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_add_fk</span>(people, species, species)</span>
<span id="cb21-6"></span>
<span id="cb21-7">dm_starwars_2_wrong_pk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dm_starwars_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm</span>(species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_add_pk</span>(species, name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_add_fk</span>(people, species, species)</span></code></pre></div>
</div>
<p>Let’s look at the result of <code>dm_examine_constraints()</code>. For the correct model, <code>dm_starwars_2</code>, the message <code>ℹ All constraints satisfied.</code> is displayed. On the other hand, <code>dm_starwars_2_strong_data</code> and <code>dm_starwars_2_strong_pk</code> show the message <code>! Unsatisfied constraints:</code> is displayed. This is because the primary key of the <code>species</code> data does not contain data that should be included. Thus, we see that <code>dm_examine_constraints()</code> can be used to easily check the key constraints.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_examine_constraints</span>(dm_starwars_2))</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>ℹ All constraints satisfied.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_examine_constraints</span>(dm_starwars_2_wrong_data))</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>! Unsatisfied constraints:</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>• Table `people`: foreign key `species` into table `species`: values of `people$species` not in `species$url`: http://swapi.co/api/species/5/ (1)</code></pre>
</div>
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_examine_constraints</span>(dm_starwars_2_wrong_pk))</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>! Unsatisfied constraints:</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>• Table `people`: foreign key `species` into table `species`: values of `people$species` not in `species$name`: http://swapi.co/api/species/1/ (35), http://swapi.co/api/species/2/ (5), http://swapi.co/api/species/12/ (3), http://swapi.co/api/species/15/ (2), http://swapi.co/api/species/22/ (2), …</code></pre>
</div>
</div>
<p>As described above, we have seen that we can add new data to the relational data model with <code>dm()</code> and check key constraints with <code>dm_examine_constraints()</code>. Finally, the following figure shows a composition of the species of the characters in the Star Wars films using <code>dm_starwars_2</code>. Again, we omit the discussion of the plots.</p>
<div class="cell">
<details class="code-fold">
<summary>Plot of the composition of the characters’ races</summary>
<div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1">dm_starwars_2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb30-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm_flatten_to_tbl</span>(films_x_characters,</span>
<span id="cb30-3">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.recursive =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb30-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;">name.species =</span> name.species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb30-5">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_na_value_to_level</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb30-6">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_lump_n</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,</span>
<span id="cb30-7">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb30-8">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_relevel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other"</span>, </span>
<span id="cb30-9">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">after =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb30-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(title, name.species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb30-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> n <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;">sum</span>(n),</span>
<span id="cb30-12">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> title,</span>
<span id="cb30-13">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unused"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb30-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_rev</span>(title), prop,</span>
<span id="cb30-15">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> name.species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_stack</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reverse =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</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;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(prop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5e-2</span>, </span>
<span id="cb30-18">                                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb30-19">                                scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label_percent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)(prop))),</span>
<span id="cb30-20">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_stack</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb30-21">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reverse =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_discrete</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Title"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Composition of the characters' races"</span>,</span>
<span id="cb30-24">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species name"</span>,</span>
<span id="cb30-26">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guide_legend</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb30-30">                             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2024/01/relational-data-models-with-r-s-dm_files/figure-html/plot-starwars-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>In this article, we have shown how to build a relational data model using dm. Once a relational data model is built using dm, it is no longer necessary to manage relationships among data by oneself, and data can be automatically joined using <code>dm_flatten_to_tbl()</code>. In addition, dm provides other useful functions to enhance the quality of data preprocessing, such as adding data to the model with <code>dm()</code> and checking key constraints with <code>dm_examine_constraints()</code>.</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li><a href="https://dm.cynkra.com/index.html">dm package site</a></li>
<li><a href="https://gihyo.jp/book/2018/978-4-7741-9647-3">Preprocessing Compendium [Practical SQL/R/Python techniques for data analysis] (in Japanese)</a></li>
<li><a href="https://pkg.garrickadenbuie.com/starwarsdb/">starwarsdb</a>
<ul>
<li>Star Wars relational data model available for download from CRAN</li>
</ul></li>
</ul>


</section>


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

<ol>
<li id="fn1"><p>Fortunately, recent versions of dplyr warn against duplicate keys when joining data frames.↩︎</p></li>
<li id="fn2"><p>Among the data exported by repurrrsive, the data whose name begins with <code>sw_</code> are related to StarWars, and the part after <code>sw_</code> represents the content of the data.↩︎</p></li>
<li id="fn3"><p>The <code>species</code> column of <code>sw_people</code> is not needed here, but is selected for use in the next analysis.↩︎</p></li>
<li id="fn4"><p>Here, I used mermaid to illustrate the relationship between data frames.↩︎</p></li>
<li id="fn5"><p>There is no clear hierarchical relationship between works and characters, so the name <code>characters_x_films</code> is acceptable.↩︎</p></li>
<li id="fn6"><p>For <code>films</code>, <code>people</code>, and <code>planets</code>, the <code>url</code> column is the primary key, and for <code>films_x_characters</code>, <code>url</code> and <code>characters</code> are the primary keys.↩︎</p></li>
<li id="fn7"><p>Following the arrows in the image above, I set foreign keys.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>dm</category>
  <category>R</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2024/01/relational-data-models-with-r-s-dm.html</guid>
  <pubDate>Sat, 27 Jan 2024 15:00:00 GMT</pubDate>
  <media:content url="https://uchidamizuki.quarto.pub/blog/en/posts/2024/01/relational-data-models-with-r-s-dm/mermaid-figure-2.png" medium="image" type="image/png" height="118" width="144"/>
</item>
<item>
  <title>Calculating the Area of a Circle Using Numerical Integration in R</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2023/09/circle-area-by-numerical-integration-in-r.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2023/09/circle-area-by-numerical-integration-in-r.html">my Japanese article</a>.</p>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>I recently learned that the <code>integrate()</code> function in R’s stats package<sup>1</sup> can perform numerical integration of one-dimensional functions. So in this article, referring to <a href="https://mathwords.net/enmensekibun">this article</a>, I tried calculating the area of a circle using integration.</p>
</section>
<section id="a-function-to-draw-a-semicircle" class="level2">
<h2 class="anchored" data-anchor-id="a-function-to-draw-a-semicircle">A function to draw a semicircle</h2>
<p>A function that draws a semicircle of radius <img src="https://latex.codecogs.com/png.latex?r"> can be expressed as Equation&nbsp;1. Written in R, this becomes the <code>semicircle()</code> function.</p>
<p><span id="eq-semicircle"><img src="https://latex.codecogs.com/png.latex?%0Ay%20=%20%5Csqrt%7Br%5E2%20-%20x%5E2%7D%20%5Cqquad%20(-r%20%5Cle%20x%20%5Cle%20r)%0A%5Ctag%7B1%7D"></span></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">semicircle <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, radius) {</span>
<span id="cb1-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(radius<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-3">}</span></code></pre></div>
</div>
<p>Let’s use the <code>semicircle()</code> function we implemented to draw a semicircle with radius <img src="https://latex.codecogs.com/png.latex?r%20=%205">.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">radius <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;">5</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">curve</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">semicircle</span>(x, radius), <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>radius, radius,</span>
<span id="cb2-3">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">asp =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set the aspect ratio to 1:1</span></span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2023/09/circle-area-by-numerical-integration-in-r_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="calculating-the-area-of-a-circle-with-numerical-integration" class="level2">
<h2 class="anchored" data-anchor-id="calculating-the-area-of-a-circle-with-numerical-integration">Calculating the area of a circle with numerical integration</h2>
<p>Next, let’s use the <code>stats::integrate()</code> function to numerically integrate the <code>semicircle()</code> function we just wrote. For radius <img src="https://latex.codecogs.com/png.latex?r%20=%205">, we can confirm that the integral is approximately equal to the area of a semicircle, <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B1%7D%7B2%7D%5Cpi%20r%5E2">.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set the radius to 5</span></span>
<span id="cb3-2">radius <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;">5</span></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integrate</span>(semicircle, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>radius, radius, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Integrate over the range from -radius to radius</span></span>
<span id="cb3-4">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">radius =</span> radius)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>39.26991 with absolute error &lt; 2.5e-08</code></pre>
</div>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">pi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 39.26991</code></pre>
</div>
</div>
<p>Therefore, we can write <code>circle_area_approx()</code>, which approximately computes the area of a circle, as follows (the result of the numerical integration is stored in the <code>value</code> element of the return value of <code>stats::integrate()</code>).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">circle_area_approx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(radius) {</span>
<span id="cb7-2">  out <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;">integrate</span>(semicircle, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>radius, radius,</span>
<span id="cb7-3">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">radius =</span> radius)</span>
<span id="cb7-4">  out<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb7-5">}</span>
<span id="cb7-6"></span>
<span id="cb7-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">circle_area_approx</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 78.53982</code></pre>
</div>
</div>
<p>Finally, let’s compare the approximate values from <code>circle_area_approx()</code> with the theoretical values from <code>circle_area_true()</code> (<img src="https://latex.codecogs.com/png.latex?%5Cpi%20r%5E2">) over the range <img src="https://latex.codecogs.com/png.latex?1%20%5Cle%20r%20%5Cle%2010">.</p>
<p>The calculation confirms that the approximate and theoretical circle areas are nearly equal.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">circle_area_true <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(radius) {</span>
<span id="cb9-2">  pi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> radius <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb9-3">}</span>
<span id="cb9-4"></span>
<span id="cb9-5">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">radius =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb9-6">data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>circle_area_approx <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;">sapply</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>radius, circle_area_approx)</span>
<span id="cb9-7">data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>circle_area_true <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;">sapply</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>radius, circle_area_true)</span>
<span id="cb9-8">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(data)</span></code></pre></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">radius</th>
<th style="text-align: right;">circle_area_approx</th>
<th style="text-align: right;">circle_area_true</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1</td>
<td style="text-align: right;">3.141593</td>
<td style="text-align: right;">3.141593</td>
</tr>
<tr class="even">
<td style="text-align: right;">2</td>
<td style="text-align: right;">12.566371</td>
<td style="text-align: right;">12.566371</td>
</tr>
<tr class="odd">
<td style="text-align: right;">3</td>
<td style="text-align: right;">28.274334</td>
<td style="text-align: right;">28.274334</td>
</tr>
<tr class="even">
<td style="text-align: right;">4</td>
<td style="text-align: right;">50.265482</td>
<td style="text-align: right;">50.265482</td>
</tr>
<tr class="odd">
<td style="text-align: right;">5</td>
<td style="text-align: right;">78.539816</td>
<td style="text-align: right;">78.539816</td>
</tr>
<tr class="even">
<td style="text-align: right;">6</td>
<td style="text-align: right;">113.097335</td>
<td style="text-align: right;">113.097335</td>
</tr>
<tr class="odd">
<td style="text-align: right;">7</td>
<td style="text-align: right;">153.938040</td>
<td style="text-align: right;">153.938040</td>
</tr>
<tr class="even">
<td style="text-align: right;">8</td>
<td style="text-align: right;">201.061930</td>
<td style="text-align: right;">201.061930</td>
</tr>
<tr class="odd">
<td style="text-align: right;">9</td>
<td style="text-align: right;">254.469005</td>
<td style="text-align: right;">254.469005</td>
</tr>
<tr class="even">
<td style="text-align: right;">10</td>
<td style="text-align: right;">314.159265</td>
<td style="text-align: right;">314.159265</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>We have seen that the <code>stats::integrate()</code> function makes it easy to perform numerical integration of one-dimensional functions.</p>
<section id="a-note-of-caution" class="level3">
<h3 class="anchored" data-anchor-id="a-note-of-caution">A note of caution</h3>
<p>The <code>stats::integrate()</code> function can also perform numerical integration over the range <img src="https://latex.codecogs.com/png.latex?-%5Cinfty%20%5Cle%20x%20%5Cle%20%5Cinfty">, but be aware that it may not integrate correctly when given large values such as <img src="https://latex.codecogs.com/png.latex?10%5E%7B-6%7D%20%5Cle%20x%20%5Cle%2010%5E6">.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># OK</span></span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integrate</span>(dnorm, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>1 with absolute error &lt; 9.4e-05</code></pre>
</div>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Not OK</span></span>
<span id="cb12-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integrate</span>(dnorm, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>0 with absolute error &lt; 0</code></pre>
</div>
</div>


</section>
</section>


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

<ol>
<li id="fn1"><p>The stats package is one of the packages that comes with R by default.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>R</category>
  <category>Numerical Integration</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2023/09/circle-area-by-numerical-integration-in-r.html</guid>
  <pubDate>Sat, 09 Sep 2023 15:00:00 GMT</pubDate>
</item>
<item>
  <title>Presented at TokyoR Using Quarto</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2023/09/presented-at-tokyor-using-quarto.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2023/09/presented-at-tokyor-using-quarto.html">my Japanese article</a>.</p>
<section id="talk-overview" class="level2">
<h2 class="anchored" data-anchor-id="talk-overview">Talk Overview</h2>
<p>At the LT (Lightning Talk) session of the <a href="https://tokyor.connpass.com/event/292545/">108th Tokyo.R Study Meetup (#TokyoR)</a>, I introduced the jpstat package, an R package for using the e-Stat API<sup>1</sup>.</p>
<p>LT stands for Lightning Talks, which are short presentations. At Tokyo.R, each speaker was given 5 minutes.</p>
<p>The presentation slides from the day are available <a href="https://uchidamizuki.github.io/slide-estat-with-r/">here</a>.</p>
<iframe class="slide-deck" src="https://uchidamizuki.github.io/slide-estat-with-r/"></iframe>
</section>
<section id="about-creating-the-slides" class="level2">
<h2 class="anchored" data-anchor-id="about-creating-the-slides">About Creating the Slides</h2>
<p>I have used Quarto for things like GitHub READMEs and building websites, but this was my first time trying to create slides with it.</p>
<p>Referring to the following pages, I tried making a trial slide deck to introduce my own jpstat package, and found that it was surprisingly easy to create slides, so I decided to go ahead and present it at the R study meetup.</p>
<ul>
<li><p><a href="https://quarto.org/docs/presentations/" class="uri">https://quarto.org/docs/presentations/</a></p></li>
<li><p><a href="https://quarto.org/docs/presentations/revealjs/" class="uri">https://quarto.org/docs/presentations/revealjs/</a></p></li>
</ul>
<p>The source code is available <a href="https://github.com/UchidaMizuki/slide-estat-with-r">here</a>. The Quarto content is written in <a href="https://github.com/UchidaMizuki/slide-estat-with-r/blob/main/slide-estat-with-r.qmd">this</a> qmd (Quarto Markdown) file.</p>
<p>Below, I’ll introduce the features I found especially useful when creating slides with Quarto and reveal.js.</p>
<section id="useful-feature-1-code-line-highlighting" class="level3">
<h3 class="anchored" data-anchor-id="useful-feature-1-code-line-highlighting">Useful Feature 1: <a href="https://quarto.org/docs/presentations/revealjs/#line-highlighting">Code Line Highlighting</a></h3>
<p>By specifying <code>code-line-numbers</code> as shown below, you can highlight specific lines of code.</p>
<p>Here, <code>code-line-numbers: "2-3"</code> highlights lines 2 and 3. It also seems possible to transition the highlighted lines over time by separating values with <code>|</code>, as in <code>code-line-numbers: "1|2-3"</code>.</p>
<p>This highlighting feature is very handy when you want to draw attention to just part of the code.</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb1-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb1-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| echo: true</span></span>
<span id="cb1-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-line-numbers: "2-3"</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">estat(statsDataId = "0003343671") |&gt;</span></span>
<span id="cb1-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  activate(cat01) |&gt;</span></span>
<span id="cb1-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  filter(str_detect(name, "チョコレート"))</span></span>
<span id="cb1-8"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</section>
<section id="useful-feature-2-auto-animate-and-fade-in" class="level3">
<h3 class="anchored" data-anchor-id="useful-feature-2-auto-animate-and-fade-in">Useful Feature 2: <a href="https://quarto.org/docs/presentations/revealjs/advanced.html#auto-animate">Auto-Animate</a> and Fade-In</h3>
<p>By adding <code>{auto-animate="true"}</code> next to a slide title that starts with <code>##</code>, Quarto automatically finds content shared with the next slide and animates the transition between them.</p>
<p>When there is repeated content across consecutive slides, I felt that using the auto-animate feature makes the connection between them easier to follow.</p>
<p>In addition, using <code>::: {.fragment .fade-in}</code> lets you reveal content partway through a slide. This is also useful when you want to draw attention to a specific piece of content.</p>
</section>
<section id="useful-feature-3-title-and-footer-settings" class="level3">
<h3 class="anchored" data-anchor-id="useful-feature-3-title-and-footer-settings">Useful Feature 3: Title and Footer Settings</h3>
<p>The title slide and the footer common to all slides are automatically laid out by writing them at the top of the qmd file, as shown below.</p>
<p>Here, besides the title, I also set the subtitle, author, and date, and it turned out I didn’t need to separately specify things like font size for these.</p>
<p>Also, while I used reveal.js’s default theme this time, it’s also possible to change the theme, as described <a href="https://quarto.org/docs/presentations/revealjs/themes.html">here</a>.</p>
<pre><code>---
title: "e-Stat🤝R"
subtitle: "Tokyo.R #108"
author: UchidaMizuki
date: "2023-09-02"
footer: &lt;https://github.com/UchidaMizuki/jpstat&gt;
format:
  revealjs
---</code></pre>
</section>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>I found that Quarto makes it easy to create presentation materials that are just the right fit for a Lightning Talk.</p>
<p>By the way, I had already been publishing pages created with Quarto to GitHub Pages, and this time I was able to easily publish the reveal.js slides as well by using the <a href="https://quarto.org/docs/publishing/github-pages.html#publish-command">Publish Command</a><sup>2</sup>.</p>


</section>


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

<ol>
<li id="fn1"><p>It seems that the 108th Tokyo.R event had exactly 108 registered participants.↩︎</p></li>
<li id="fn2"><p>Automatic updates via GitHub Actions didn’t work out, so this time I ran <code>quarto publish gh-pages</code> manually from RStudio’s Terminal.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>e-Stat</category>
  <category>LT</category>
  <category>Tokyo.R</category>
  <category>R</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2023/09/presented-at-tokyor-using-quarto.html</guid>
  <pubDate>Sat, 02 Sep 2023 15:00:00 GMT</pubDate>
</item>
<item>
  <title>Why is Bayes’ theorem important? A PCR testing example</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2023/04/why-is-bayes-theorem-important.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2023/04/why-is-bayes-theorem-important.html">my Japanese article</a>.</p>
<section id="what-is-bayes-theorem" class="level2">
<h2 class="anchored" data-anchor-id="what-is-bayes-theorem">What is Bayes’ theorem?</h2>
<p>In this article, I would like to explain the importance of Bayes’ theorem, one of the most famous theorems in statistics, using the PCR testing that became widespread during the COVID-19 pandemic as an example.</p>
<p>Bayes’ theorem can be expressed as shown in Equation&nbsp;1.</p>
<p><span id="eq-bayes"><img src="https://latex.codecogs.com/png.latex?%0AP(A%7CB)%20%5Cpropto%20P(B%7CA)%20P(A)%0A%5Ctag%7B1%7D"></span></p>
<p>Equation&nbsp;1 shows the following:</p>
<ul>
<li>The probability <img src="https://latex.codecogs.com/png.latex?P(A%7CB)"> that an observed result <img src="https://latex.codecogs.com/png.latex?B"> was caused by <img src="https://latex.codecogs.com/png.latex?A"></li>
<li>is proportional (<img src="https://latex.codecogs.com/png.latex?%5Cpropto">) to the product of the probability <img src="https://latex.codecogs.com/png.latex?P(A)"> that cause <img src="https://latex.codecogs.com/png.latex?A"> occurs and the probability <img src="https://latex.codecogs.com/png.latex?P(B%7CA)"> that result <img src="https://latex.codecogs.com/png.latex?B"> occurs given cause <img src="https://latex.codecogs.com/png.latex?A"></li>
</ul>
<p>Let’s use a more familiar example. Suppose we define:</p>
<ul>
<li>Cause <img src="https://latex.codecogs.com/png.latex?A">: whether or not someone is infected with COVID-19</li>
<li>Result <img src="https://latex.codecogs.com/png.latex?B">: the diagnostic result from a PCR test or similar test</li>
</ul>
<p>With this in mind, Equation&nbsp;1 can be rewritten as Equation&nbsp;2.</p>
<p><span id="eq-bayes-covid19"><img src="https://latex.codecogs.com/png.latex?%0AP(%5Ctext%7Binfection%20status%7D%7C%5Ctext%7Bdiagnosis%20result%7D)%20%5Cpropto%20P(%5Ctext%7Bdiagnosis%20result%7D%7C%5Ctext%7Binfection%20status%7D)%20P(%5Ctext%7Binfection%20status%7D)%0A%5Ctag%7B2%7D"></span></p>
</section>
<section id="why-is-bayes-theorem-important" class="level2">
<h2 class="anchored" data-anchor-id="why-is-bayes-theorem-important">Why is Bayes’ theorem important?</h2>
<p>Let’s think about the importance of Bayes’ theorem based on Equation&nbsp;2. In fact, all three probabilities that appear in the version of Bayes’ theorem shown in Equation&nbsp;2 correspond to very important indicators (see the table below). In other words, Equation&nbsp;2 tells us that even if a test’s accuracy (sensitivity and specificity) is high, the positive predictive value will be low if the pre-test probability is low.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 38%">
<col style="width: 61%">
</colgroup>
<thead>
<tr class="header">
<th>Probability</th>
<th><strong>Corresponding indicator in the medical field</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?P(%5Ctext%7Bdiagnosis%20result%7D%7C%5Ctext%7Binfection%20status%7D)"></td>
<td><p><strong>Post-test probability</strong>: called the <strong>posterior probability</strong> in statistics</p>
<ul>
<li><strong>Positive predictive value (PPV)</strong>: probability of actually being infected when diagnosed positive</li>
<li><strong>Negative predictive value (NPV)</strong>: probability of actually being uninfected when diagnosed negative</li>
</ul></td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?P(%5Ctext%7Bdiagnosis%20result%7D%7C%5Ctext%7Binfection%20status%7D)"></td>
<td><ul>
<li><strong>Sensitivity</strong>: probability that an infected person is diagnosed positive ↔︎ related to <strong>false negatives</strong></li>
<li><strong>Specificity</strong>: probability that an uninfected person is diagnosed negative ↔︎ related to <strong>false positives</strong></li>
</ul></td>
</tr>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?P(%5Ctext%7Binfection%20status%7D)"></td>
<td><p><strong>Pre-test probability</strong>: called the <strong>prior probability</strong> in statistics</p>
<ul>
<li>The proportion of infected individuals among those tested</li>
</ul></td>
</tr>
</tbody>
</table>
<p>For example, let’s consider the following situation:</p>
<ul>
<li>Both sensitivity and specificity are 99% (false negatives and false positives each occur with a 1% probability)</li>
<li>The pre-test probability is 1% (1 out of every 100 people tested is infected, and the remaining 99 are uninfected)</li>
</ul>
<p>In this case, the probability of being infected given a positive diagnosis, <img src="https://latex.codecogs.com/png.latex?P(%5Ctext%7Binfected%7D%7C%5Ctext%7Bpositive%7D)">, and the probability of being uninfected given a positive diagnosis, <img src="https://latex.codecogs.com/png.latex?P(%5Ctext%7Buninfected%7D%7C%5Ctext%7Bpositive%7D)">, are given by Equation&nbsp;3.</p>
<p><span id="eq-bayes-covid19-positive"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0A%20%20P(%5Ctext%7Binfected%7D%7C%5Ctext%7Bpositive%7D)%20%5Cpropto%20P(%5Ctext%7Bpositive%7D%7C%5Ctext%7Binfected%7D)P(%5Ctext%7Binfected%7D)%20&amp;=%200.99%20%5Ctimes%200.01%20=%200.0099%20%5C%5C%0A%20%20P(%5Ctext%7Buninfected%7D%7C%5Ctext%7Bpositive%7D)%20%5Cpropto%20P(%5Ctext%7Bpositive%7D%7C%5Ctext%7Buninfected%7D)P(%5Ctext%7Buninfected%7D)%20&amp;=%20(1%20-%200.99)%20%5Ctimes%20(1%20-%200.01)%20=%200.0099%0A%5Cend%7Balign%7D%0A%5Ctag%7B3%7D"></span></p>
<p>From Equation&nbsp;3, we can see that <img src="https://latex.codecogs.com/png.latex?P(%5Ctext%7Binfected%7D%7C%5Ctext%7Bpositive%7D)"> and <img src="https://latex.codecogs.com/png.latex?P(%5Ctext%7Buninfected%7D%7C%5Ctext%7Bpositive%7D)"> are equal, meaning the positive predictive value is 50%. In other words, among the people tested who receive a positive diagnosis, only 1 in 2 is actually infected.</p>
<p>In this way, using Bayes’ theorem reveals an important relationship: even if a test’s accuracy (sensitivity and specificity) is high, the positive predictive value will be low if the pre-test probability is low.</p>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>Using the PCR testing that became widespread during the COVID-19 pandemic as an example, we explained the importance of Bayes’ theorem.</p>
<p>If you are interested in more realistic calculation examples, we also recommend reading <a href="https://www.stat.go.jp/library/pdf/cgogai3102.pdf" target="_blank">this column published by the Statistics Bureau</a> (in Japanese).</p>


</section>

 ]]></description>
  <category>Bayes</category>
  <category>R</category>
  <category>Statistics</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2023/04/why-is-bayes-theorem-important.html</guid>
  <pubDate>Sat, 29 Apr 2023 15:00:00 GMT</pubDate>
  <media:content url="https://uchidamizuki.quarto.pub/blog/en/posts/2023/04/why-is-bayes-theorem-important/bayes-theorem.png" medium="image" type="image/png" height="14" width="144"/>
</item>
<item>
  <title>Quantifying the Generational ‘Vote-Value Disparity’</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2023/04/quantify-vote-disparity-between-generations.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2023/04/quantify-vote-disparity-between-generations.html">my Japanese article</a>.</p>
<section id="what-is-the-vote-value-disparity" class="level2">
<h2 class="anchored" data-anchor-id="what-is-the-vote-value-disparity">What Is the “Vote-Value Disparity”?</h2>
<p>Since it’s currently election season<sup>1</sup>, I’d like to use open data to quantify the “vote-value disparity,” an issue that often comes up.</p>
<p>The “vote-value disparity” usually refers to the fact that the number of eligible voters per elected representative differs by region (electoral district) — in other words, the number of votes needed to elect one representative differs by region. For example, in the 2022 House of Councillors election, the “vote-value disparity” is said to have reached a maximum of 3.03 times<sup>2</sup>.</p>
<p>Currently, urban and rural areas in Japan are said to have the relationship shown in the table below, and it has been pointed out that the “vote-value disparity” causes problems such as reduced economic efficiency — for example, new Shinkansen lines being built in rural areas while Tokyo’s commuter rush remains unresolved<sup>3</sup>.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: center;">Region</th>
<th style="text-align: center;">Flow of money (e.g., <u>local allocation tax</u>)</th>
<th style="text-align: center;">Political equality (“vote-value disparity”)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">Urban areas</td>
<td style="text-align: center;">“Pays” money to rural areas</td>
<td style="text-align: center;">Value of one vote is lighter — urban areas are “disadvantaged”</td>
</tr>
<tr class="even">
<td style="text-align: center;">Rural areas</td>
<td style="text-align: center;">“Receives” money from urban areas</td>
<td style="text-align: center;">Value of one vote is heavier — rural areas are “favored”</td>
</tr>
</tbody>
</table>
<p>On the other hand, rural social issues are expected to become even more serious in the future due to factors such as the continued concentration of population in Tokyo. For this reason, there are also concerns that making the “value of a vote” completely equal could actually work against — or “disadvantage” — rural areas.</p>
<p>As this shows, correcting the “vote-value disparity” is by no means a simple task, but it can be said that efforts toward correcting it need to continue.</p>
<section id="what-is-the-generational-vote-value-disparity" class="level3">
<h3 class="anchored" data-anchor-id="what-is-the-generational-vote-value-disparity">What Is the Generational “Vote-Value Disparity”?</h3>
<p>In addition to the regional “vote-value disparity” discussed so far, it is said that a “vote-value disparity” also exists between generations<sup>4</sup>. This is caused by the declining birthrate and aging population, which has resulted in a much larger number of eligible voters among older generations relative to younger generations<sup>5</sup>.</p>
<p>As with the urban-rural relationship shown earlier, younger and older generations are thought to have the relationship shown in the table below. It was only recently, as population decline and labor shortages in rural areas became severe, that the government positioned measures against the declining birthrate as one of the nation’s top priorities. The fact that such policies aimed at younger generations were not sufficiently implemented until now<sup>6</sup> may well be a result of the generational “vote-value disparity.”</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: center;">Generation</th>
<th style="text-align: center;">Flow of money (e.g., <u>social security spending</u>)</th>
<th style="text-align: center;">Political equality (“vote-value disparity”)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">Younger generations</td>
<td style="text-align: center;">“Pay” money to older generations</td>
<td style="text-align: center;">Value of one vote is lighter — younger generations are “disadvantaged”</td>
</tr>
<tr class="even">
<td style="text-align: center;">Older generations</td>
<td style="text-align: center;">“Receive” money from younger generations</td>
<td style="text-align: center;">Value of one vote is heavier — older generations are “favored”</td>
</tr>
</tbody>
</table>
</section>
</section>
<section id="quantifying-the-generational-vote-value-disparity" class="level2">
<h2 class="anchored" data-anchor-id="quantifying-the-generational-vote-value-disparity">Quantifying the Generational “Vote-Value Disparity”</h2>
<p>The regional “vote-value disparity” is commonly quantified as being some number of times larger (relative to urban areas), but the generational “vote-value disparity” does not seem to have been quantified much so far.</p>
<p>A simple approach to quantification would be to divide the population of each age group by some reference population. However, this kind of method fails to account for the fact that people pass away as they age, and would end up producing an evaluation along the lines of “the population is small, so the value of a vote for those aged 100 and over is low.”</p>
<p>So, I’ll try to quantify the generational “vote-value disparity” using the 2020 Population Census and the 2020 Life Tables. A life table is “a set of indicators, such as the probability that a person of a given age will die within one year, calculated under the assumption that the mortality conditions observed over a certain period will not change in the future”<sup>7</sup>.</p>
<p><u><strong>Please note that the generational “vote-value disparity” shown below is merely a simplified estimate based on open data.</strong></u></p>
<section id="about-the-data-used" class="level3">
<h3 class="anchored" data-anchor-id="about-the-data-used">About the Data Used</h3>
<p>This time, I’ll use the data shown in the table below. Here, the “stationary population” (nLx) in the table refers to “the population by age group obtained under a constant birth rate and (life-table) mortality rate”<sup>8</sup>.</p>
<p>For details on how the data was obtained and processed, please also refer to the collapsed code sections below.</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Content</th>
<th>Source</th>
<th>How obtained</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><a href="https://www.e-stat.go.jp/dbview?sid=0003445139">Population by sex and age</a></td>
<td>2020 Population Census</td>
<td>Using the e-Stat API via the <a href="https://uchidamizuki.github.io/jpstat/">jpstat</a> package</td>
</tr>
<tr class="even">
<td><a href="https://www.e-stat.go.jp/stat-search/files?page=1&amp;layout=datalist&amp;toukei=00450012&amp;tstat=000001031336&amp;cycle=7&amp;tclass1=000001060864&amp;tclass2=000001163166&amp;tclass3val=0">Stationary population by sex and age (nLx)</a></td>
<td>2020 Life Tables</td>
<td>Downloading the Excel file from the URL</td>
</tr>
</tbody>
</table>
<div class="cell">
<details class="code-fold">
<summary>Loading packages, etc.</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(fs)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(arrow)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readxl)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(jpstat)</span>
<span id="cb1-6"></span>
<span id="cb1-7"><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_light</span>())</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir_create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quantify-vote-disparity-between-generations"</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Downloading and processing the 2020 Population Census data</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Using the jpstat package to download the 2020 Population Census data</span></span>
<span id="cb2-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;">file_exists</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quantify-vote-disparity-between-generations/pop_2020.parquet"</span>)) {</span>
<span id="cb2-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note: the jpstat package requires an API key because it uses the e-Stat API</span></span>
<span id="cb2-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run Sys.setenv(ESTAT_API_KEY = "your appId") beforehand</span></span>
<span id="cb2-5">  census_2020 <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;">estat</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">statsDataId =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.e-stat.go.jp/dbview?sid=0003445139"</span>)</span>
<span id="cb2-6"></span>
<span id="cb2-7">  pop_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> census_2020 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(tab) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-10"></span>
<span id="cb2-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(cat01) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"うち日本人"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-14"></span>
<span id="cb2-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(cat02) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rekey</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sex"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</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;">"男"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"女"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-19"></span>
<span id="cb2-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(cat03) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rekey</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-23"></span>
<span id="cb2-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(area) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-25">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"全国"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-26">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-27"></span>
<span id="cb2-28">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(time) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-29">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-30"></span>
<span id="cb2-31">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pop"</span>)</span>
<span id="cb2-32"></span>
<span id="cb2-33">  pop_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pop_2020 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-34">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(age_name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+歳$"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> age_name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"100歳以上"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-35">    <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;">sex =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_factor</span>(sex_name),</span>
<span id="cb2-36">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(age_name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+歳$"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> age_name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-37">                             <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+"</span>),</span>
<span id="cb2-38">                           age_name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"100歳以上"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"100--Inf"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-39">             <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_factor</span>(),</span>
<span id="cb2-40">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_number</span>(pop),</span>
<span id="cb2-41">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unused"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-42">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">relocate</span>(sex, age, pop)</span>
<span id="cb2-43"></span>
<span id="cb2-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_parquet</span>(pop_2020, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quantify-vote-disparity-between-generations/pop_2020.parquet"</span>)</span>
<span id="cb2-45">}</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Downloading the 2020 Life Tables</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 23rd Life Tables (male)</span></span>
<span id="cb3-2">file_lifetable_male_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quantify-vote-disparity-between-generations/lifetable_male_2020.xlsx"</span></span>
<span id="cb3-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;">file_exists</span>(file_lifetable_male_2020)) {</span>
<span id="cb3-4">  curl<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;">curl_download</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.e-stat.go.jp/stat-search/file-download?statInfId=000032173232&amp;fileKind=0"</span>, file_lifetable_male_2020)</span>
<span id="cb3-5">}</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 23rd Life Tables (female)</span></span>
<span id="cb3-8">file_lifetable_female_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quantify-vote-disparity-between-generations/lifetable_female_2020.xlsx"</span></span>
<span id="cb3-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;">file_exists</span>(file_lifetable_female_2020)) {</span>
<span id="cb3-10">  curl<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;">curl_download</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.e-stat.go.jp/stat-search/file-download?statInfId=000032173233&amp;fileKind=0"</span>, file_lifetable_female_2020)</span>
<span id="cb3-11">}</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Processing the 2020 Life Tables stationary population data</summary>
<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">col_names_lifetable <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_excel</span>(file_lifetable_male_2020,</span>
<span id="cb4-2">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">range =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B3:J5"</span>,</span>
<span id="cb4-3">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_names =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</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;">t</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;">as_tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.name_repair =</span> \(x) <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;">"col_name_1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col_name_2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col_name_3"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unite</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col_name"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col_name"</span>),</span>
<span id="cb4-7">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-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;">col_name =</span> col_name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-9">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_remove_all</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">s"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(col_name)</span>
<span id="cb4-11"></span>
<span id="cb4-12">range_lifetable <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B6:J127"</span></span>
<span id="cb4-13"></span>
<span id="cb4-14">lifetable_male_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_excel</span>(file_lifetable_male_2020,</span>
<span id="cb4-15">                                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">range =</span> range_lifetable,</span>
<span id="cb4-16">                                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_names =</span> col_names_lifetable)</span>
<span id="cb4-17"></span>
<span id="cb4-18">lifetable_female_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_excel</span>(file_lifetable_female_2020,</span>
<span id="cb4-19">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">range =</span> range_lifetable,</span>
<span id="cb4-20">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_names =</span> col_names_lifetable)</span>
<span id="cb4-21"></span>
<span id="cb4-22">static_pop_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(男 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> lifetable_male_2020,</span>
<span id="cb4-23">                        女 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> lifetable_female_2020) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sex"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age =</span> 年齢_x,</span>
<span id="cb4-26">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">static_pop =</span> 定常人口_nLx_人) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(sex, age, static_pop) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_ends</span>(age, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"年"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-29">  <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;">sex =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_factor</span>(sex),</span>
<span id="cb4-30">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age =</span> age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-31">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-32">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_integer</span>(),</span>
<span id="cb4-33">         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Aggregate ages 100 and over</span></span>
<span id="cb4-34">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,</span>
<span id="cb4-35">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"100--Inf"</span>,</span>
<span id="cb4-36">                       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(age)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-37">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_factor</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-38">  <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;">static_pop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(static_pop),</span>
<span id="cb4-39">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(sex, age))</span>
<span id="cb4-40"></span>
<span id="cb4-41"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_parquet</span>(static_pop_2020, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quantify-vote-disparity-between-generations/static_pop_2020.parquet"</span>)</span></code></pre></div>
</details>
</div>
</section>
<section id="comparing-the-population-pyramids-of-the-census-population-and-the-life-table-stationary-population" class="level3">
<h3 class="anchored" data-anchor-id="comparing-the-population-pyramids-of-the-census-population-and-the-life-table-stationary-population">Comparing the “Population Pyramids” of the Census Population and the Life Table Stationary Population</h3>
<p>Let’s create a population pyramid using the data we obtained and processed. The graph below overlays the life table’s stationary population (red line) on top of the 2020 Population Census population<sup>9</sup>. Note that the stationary population has been rescaled so that the stationary population at age 0 equals the 2020 Population Census population at age 0<sup>10</sup>. In addition, the population and stationary population for ages 100 and over are aggregated and displayed at age 100.</p>
<div class="cell">
<details class="code-fold">
<summary>Plotting the population pyramid</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Replace ages 100 and over with the number 100</span></span>
<span id="cb5-2">as_integer_age <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(age) {</span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"100--Inf"</span>,</span>
<span id="cb5-4">          <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,</span>
<span id="cb5-5">          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_integer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(age),</span>
<span id="cb5-6">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"100--Inf"</span>))</span>
<span id="cb5-7">}</span>
<span id="cb5-8"></span>
<span id="cb5-9">pop_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_parquet</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quantify-vote-disparity-between-generations/pop_2020.parquet"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-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;">age =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_integer_age</span>(age))</span>
<span id="cb5-11">static_pop_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_parquet</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quantify-vote-disparity-between-generations/static_pop_2020.parquet"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-12">  <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;">age =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_integer_age</span>(age))</span>
<span id="cb5-13"></span>
<span id="cb5-14">ratio_pop_static_pop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> static_pop_2020 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>age) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(pop_2020 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-18">              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-19">              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>age),</span>
<span id="cb5-20">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join_by</span>(sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-21">  <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;">ratio_pop_static_pop =</span> pop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> static_pop,</span>
<span id="cb5-22">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unused"</span>)</span>
<span id="cb5-23"></span>
<span id="cb5-24">static_pop_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> static_pop_2020 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(ratio_pop_static_pop,</span>
<span id="cb5-26">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join_by</span>(sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-27">  <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;">pop =</span> static_pop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ratio_pop_static_pop,</span>
<span id="cb5-28">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unused"</span>)</span>
<span id="cb5-29"></span>
<span id="cb5-30">pop_2020 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-31">  <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;">pop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(sex <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"男"</span>,</span>
<span id="cb5-32">                       <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>pop,</span>
<span id="cb5-33">                       pop)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(age, pop,</span>
<span id="cb5-35">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> static_pop_2020 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-38">               <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;">pop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(sex <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"男"</span>,</span>
<span id="cb5-39">                                    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>pop,</span>
<span id="cb5-40">                                    pop)),</span>
<span id="cb5-41">            <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;">group =</span> sex,</span>
<span id="cb5-42">                <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;">"Stationary population</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(reference)"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age"</span>,</span>
<span id="cb5-44">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">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;">10</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Population [thousands]"</span>,</span>
<span id="cb5-46">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> \(x) {</span>
<span id="cb5-47">                       scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label_comma</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-3</span>)(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(x))</span>
<span id="cb5-48">                     }) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sex"</span>,</span>
<span id="cb5-50">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(男 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cornflowerblue"</span>,</span>
<span id="cb5-51">                               女 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightcoral"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-52">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb5-53">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Stationary population</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(reference)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="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="cb5-54">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-55">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guide_legend</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb5-56">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guide_legend</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2023/04/quantify-vote-disparity-between-generations_files/figure-html/plot-pop-pyramid-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>From the 2020 population pyramid, we can see population peaks in the early 70s and late 40s. These correspond to the generations known as the “dankai generation” (baby boomers) and the “dankai junior generation” (their children), respectively<sup>11</sup>. On the other hand, the population of younger generations is far smaller than that of the dankai generation and dankai junior generation, which illustrates just how severe the recent decline in the birthrate has become.</p>
</section>
<section id="quantifying-the-generational-vote-value-disparity-based-on-the-population-pyramid" class="level3">
<h3 class="anchored" data-anchor-id="quantifying-the-generational-vote-value-disparity-based-on-the-population-pyramid">Quantifying the Generational “Vote-Value Disparity” Based on the “Population Pyramid”</h3>
<p>Let’s quantify the generational “vote-value disparity” using the ratio of the census population to the stationary population (reference value) shown by the red line.</p>
<p>The graph below quantifies the generational “vote-value disparity,” in roughly 10-year intervals, for those aged 18 and over who are eligible to vote. Here, the “value of a vote” for those aged 18-29 is normalized to 1.</p>
<p>The graph shows that a single vote from those in their 40s to 60s is worth roughly 1.5 times that of a vote from those aged 18-29, and a single vote from those in their 70s is worth roughly 1.7 times that of a vote from those aged 18-29. We also found that the value of a vote for those in their 30s and those aged 80 and over — generations that don’t correspond to the “dankai generation” or “dankai junior generation” — is lower than for those in their 40s-60s and 70s (though this may be a result of the characteristics of the life table).</p>
<div class="cell">
<p>```{.r .cell-code code-fold=“true” code-summary=“Quantifying the generational”vote-value disparity”“} vote_disparity &lt;- pop_2020 |&gt; left_join(static_pop_2020 |&gt; rename(static_pop = pop), by = join_by(sex, age)) |&gt; filter(age &gt;= 18) |&gt; mutate(ageclass = case_when(between(age, 18, 29) ~”18-29”, between(age, 30, 39) ~ “30s”, between(age, 40, 49) ~ “40s”, between(age, 50, 59) ~ “50s”, between(age, 60, 69) ~ “60s”, between(age, 70, 79) ~ “70s”, between(age, 80, 89) ~ “80s”, 90 &lt;= age ~ “90+”)) |&gt; summarise(across(c(pop, static_pop), sum), .by = ageclass) |&gt; mutate(vote_disparity = pop / static_pop, .keep = “unused”)</p>
<p>vote_disparity &lt;- vote_disparity |&gt; bind_cols(vote_disparity |&gt; filter(ageclass == “18-29”) |&gt; select(!ageclass) |&gt; rename(vote_disparity_18to29 = vote_disparity)) |&gt; mutate(vote_disparity = vote_disparity / vote_disparity_18to29, .keep = “unused”)</p>
<p>plot_vote_disparity &lt;- vote_disparity |&gt; ggplot(aes(ageclass, vote_disparity)) + geom_col(fill = “lightblue”) + geom_text(aes(label = scales::label_comma(accuracy = 1e-2)(vote_disparity)), y = 1, vjust = -0.5, color = “red”) + geom_hline(yintercept = 1, color = “red”, linetype = “dashed”) + scale_x_discrete(“Age group”) + scale_y_continuous(“Generational "vote-value disparity" (ages 18-29 = 1)”)</p>
<p>ggsave(“quantify-vote-disparity-between-generations/plot_vote_disparity.png”, plot = plot_vote_disparity)</p>
<p>plot_vote_disparity ```</p>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2023/04/quantify-vote-disparity-between-generations_files/figure-html/get-vote-disparity-between-generation-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>Using the open data of the Population Census and the Life Tables, I quantified the generational “vote-value disparity.”</p>
<p>In fact, it’s said that low voter turnout among young people is also a major factor behind the generational “vote-value disparity.” Going forward, it will likely be necessary to deepen the discussion around the generational “vote-value disparity” while also proposing more appealing policies that raise voter turnout among the young.</p>


</section>


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

<ol>
<li id="fn1"><p>The <a href="https://www.soumu.go.jp/senkyo/20touitsu/index.html">20th Unified Local Elections</a>, which determine local government assembly members and heads, were held on Sunday, April 9, 2023, and Sunday, April 23, 2023.↩︎</p></li>
<li id="fn2"><p><a href="https://www3.nhk.or.jp/news/html/20230309/k10014002531000.html">House of Councillors election: “vote-value disparity of up to 3.03 times” — Supreme Court Grand Bench to hear the case</a>↩︎</p></li>
<li id="fn3"><p><a href="https://www.yokohama-cu.ac.jp/interview2/vol3/page2.html">What are the problems with the “vote-value disparity”?</a>↩︎</p></li>
<li id="fn4"><p><a href="https://dl.ndl.go.jp/view/download/digidepo_11643610_po_20200304.pdf?contentNo=1">The “Vote-Value Disparity” Between Generations</a>↩︎</p></li>
<li id="fn5"><p>This idea is similar to the notion mentioned above that, under the concentration of population in urban areas driven by Tokyo’s dominance, making the “value of a vote” completely equal could actually disadvantage rural areas.↩︎</p></li>
<li id="fn6"><p><a href="https://www5.cao.go.jp/keizai-shimon/kaigi/special/future/sentaku/s2_4.html">Choosing Our Future (4): Measures Against the Declining Birthrate</a>↩︎</p></li>
<li id="fn7"><p><a href="https://www.mhlw.go.jp/toukei/saikin/hw/seimei/list54-57-01.html">About Life Tables</a>↩︎</p></li>
<li id="fn8"><p><a href="https://www.mhlw.go.jp/toukei/saikin/hw/life/life03/sanko-1.html">Reference Material 1: Definitions of Life Table Functions</a>↩︎</p></li>
<li id="fn9"><p>The stationary population (reference value) shown by the red line represents how many of the men and women who were 0 years old in 2020 would still be alive at each age, under the assumption of a constant mortality rate.↩︎</p></li>
<li id="fn10"><p>The male-to-female ratio of the population at age 0 is approximately 105:100.↩︎</p></li>
<li id="fn11"><p><a href="https://www.stat.go.jp/info/today/114.htm">Seeing Japan’s Future Through the “Population Pyramid”!? — Aging and the “Dankai Generation,” the Declining Birthrate and the “Dankai Junior Generation”</a>↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>Politics</category>
  <category>e-Stat</category>
  <category>jpstat</category>
  <category>R</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2023/04/quantify-vote-disparity-between-generations.html</guid>
  <pubDate>Sat, 22 Apr 2023 15:00:00 GMT</pubDate>
  <media:content url="https://uchidamizuki.quarto.pub/blog/en/posts/2023/04/quantify-vote-disparity-between-generations/plot_vote_disparity.png" medium="image" type="image/png" height="103" width="144"/>
</item>
<item>
  <title>I built a web app for Japanese grid square codes (R Shiny &amp; jpgrid)</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2023/02/web-app-for-grid-square-codes-in-japan.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2023/02/web-app-for-grid-square-codes-in-japan.html">my Japanese article</a>.</p>
<section id="about-this-app" class="level2">
<h2 class="anchored" data-anchor-id="about-this-app">About this app</h2>
<p>I built a <a href="https://uchidamizuki.shinyapps.io/jpgrid-app/">web app for working with Japanese grid square (mesh) data</a> using <a href="https://shiny.rstudio.com">R Shiny</a>.</p>
<p>A <a href="https://www.stat.go.jp/data/mesh/m_tuite.html">grid square</a> is a division of (Japanese) regions into roughly square cells based on longitude and latitude, and it is commonly used as an aggregation unit for statistical data.</p>
<p>The app I built provides some of the functionality of the R package <a href="https://uchidamizuki.github.io/jpgrid/">jpgrid</a>. The app offers the following features:</p>
<ol type="1">
<li>Generate grid square data by municipality</li>
<li>Generate grid square data from tabular data containing grid code strings</li>
<li>Generate grid square data from tabular data containing longitude/latitude</li>
</ol>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://uchidamizuki.shinyapps.io/jpgrid-app/"><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2023/02/images/about.png" class="img-fluid figure-img" alt="App screenshot (click to open the app)"></a></p>
<figcaption>App screenshot (click to open the app)</figcaption>
</figure>
</div>
</section>
<section id="app-features" class="level2">
<h2 class="anchored" data-anchor-id="app-features">App features</h2>
<section id="generate-grid-square-data-by-municipality" class="level3">
<h3 class="anchored" data-anchor-id="generate-grid-square-data-by-municipality">Generate grid square data by municipality</h3>
<p>It retrieves grid squares by municipality from the <a href="https://www.stat.go.jp/data/mesh/m_itiran.html">List of grid square codes by municipality</a> published by the Statistics Bureau of Japan.</p>
<p>You can generate and save grid squares by municipality using the following steps:</p>
<ol type="1">
<li>Select prefecture(s) (multiple selection allowed)</li>
<li>Select municipality(ies) (multiple selection allowed)</li>
<li>Select a grid size (1 km, 10 km, or 80 km) and click “Show grid”</li>
<li>Select a data format (GeoPackage or CSV) and click “Download”</li>
</ol>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2023/02/images/grid_city.png" class="img-fluid figure-img"></p>
<figcaption>Example display of grid squares by municipality</figcaption>
</figure>
</div>
<p>The jpgrid package provides grid square data by municipality via the <code>grid_city_2020</code> dataset.</p>
<p>You can visualize grid square data by municipality as follows.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(jpgrid)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"></span>
<span id="cb1-4">JGD2011 <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;">6668</span></span>
<span id="cb1-5"></span>
<span id="cb1-6">grid_city_2020 <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;">filter</span>(city_name_ja <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</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;">"千葉市中央区"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"千葉市花見川区"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"千葉市稲毛区"</span>)) <span class="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;">grid_as_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6668</span>) <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;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> city_name_ja)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2023/02/web-app-for-grid-square-codes-in-japan_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="generate-grid-square-data-from-tabular-data-containing-grid-code-strings" class="level3">
<h3 class="anchored" data-anchor-id="generate-grid-square-data-from-tabular-data-containing-grid-code-strings">Generate grid square data from tabular data containing grid code strings</h3>
<p>You can generate and save grid squares from tabular data containing grid code strings using the following steps:</p>
<ol type="1">
<li>Select data (CSV or Excel)</li>
<li>Specify the column name containing the grid code strings (a point ID can also be specified) and click “Show grid”</li>
<li>Select a data format (GeoPackage or CSV) and click “Download”</li>
</ol>
<p>In the jpgrid package, <code>parse_grid()</code> can be used to generate grid squares from strings.</p>
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2023/02/images/parse_grid.png" class="img-fluid"></p>
</section>
<section id="generate-grid-square-data-from-tabular-data-containing-longitudelatitude" class="level3">
<h3 class="anchored" data-anchor-id="generate-grid-square-data-from-tabular-data-containing-longitudelatitude">Generate grid square data from tabular data containing longitude/latitude</h3>
<p>Similarly, you can generate and save grid squares from tabular data containing longitude/latitude using the following steps:</p>
<ol type="1">
<li>Select data (CSV or Excel)</li>
<li>Specify the column names for longitude (X) and latitude (Y) (a point ID can also be specified) and click “Show grid”</li>
<li>Select a data format (GeoPackage or CSV) and click “Download”</li>
</ol>
<p>In the jpgrid package, <code>coords_to_grid()</code> can be used to generate grid squares from strings.</p>
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2023/02/images/coords_to_grid.png" class="img-fluid"></p>
</section>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>I introduced a web app for grid square data built with R Shiny.</p>
<p>The jpgrid package used to build this app provides many other features not included in the app. For details, please see <a href="https://uchidamizuki.github.io/jpgrid/">here</a>.</p>
<p>For example, there is <code>geometry_to_grid()</code>, which converts geometries into grid squares.</p>
<p>I encourage you to try using the jpgrid package for your grid square data analysis as well.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">japan <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rnaturalearth<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;">ne_countries</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">country =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"japan"</span>,</span>
<span id="cb2-2">                                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>,</span>
<span id="cb2-3">                                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnclass =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sf"</span>)</span>
<span id="cb2-4">grid_japan <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> japan <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geometry_to_grid</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"80km"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-6">  dplyr<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;">first</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid_as_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> sf<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;">st_crs</span>(japan))</span>
<span id="cb2-8"></span>
<span id="cb2-9">japan <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">geom_sf</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;">geom_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> grid_japan,</span>
<span id="cb2-13">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"transparent"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2023/02/web-app-for-grid-square-codes-in-japan_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>


</section>

 ]]></description>
  <category>Shiny</category>
  <category>jpgrid</category>
  <category>R</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2023/02/web-app-for-grid-square-codes-in-japan.html</guid>
  <pubDate>Wed, 15 Feb 2023 15:00:00 GMT</pubDate>
  <media:content url="https://uchidamizuki.quarto.pub/blog/en/posts/2023/02/images/grid_city.png" medium="image" type="image/png" height="74" width="144"/>
</item>
<item>
  <title>Create an animation of a population pyramid in R</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2023/01/create-an-animation-of-a-population-pyramid-in-r.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2023/01/create-an-animation-of-a-population-pyramid-in-r.html">my Japanese article</a>.</p>
<section id="about-this-article" class="level2">
<h2 class="anchored" data-anchor-id="about-this-article">About this article</h2>
<p>Referring to the <a href="https://www.ipss.go.jp/site-ad/TopPageData/Pyramid_a.html">population pyramid transition animation</a> published by the National Institute of Population and Social Security Research (IPSS), I tried creating an animation like the one below using ggplot2 in R.</p>
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2023/01/poppyramid.gif" class="img-fluid"></p>
</section>
<section id="getting-time-series-data-from-the-population-census" class="level2">
<h2 class="anchored" data-anchor-id="getting-time-series-data-from-the-population-census">Getting time-series data from the Population Census</h2>
<p>First, let’s get the <a href="https://www.e-stat.go.jp/dbview?sid=0003410380">time-series data on population by sex and 5-year age group from the Population Census</a>, published on e-Stat, using R’s jpstat package.</p>
<p>To use the e-Stat API from the jpstat package, you need to obtain an application ID (<code>appId</code>).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(jpstat)</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Replace this with your own appId</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.setenv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ESTAT_API_KEY =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Your appId"</span>)</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">census <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;">estat</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">statsDataId =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.e-stat.go.jp/dbview?sid=0003410380"</span>)</span>
<span id="cb3-2"></span>
<span id="cb3-3">pop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> census <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(tab) <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>(name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"人口"</span>) <span class="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;">select</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-7"></span>
<span id="cb3-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract sex</span></span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(cat01) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rekey</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sex"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</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;">"男"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"女"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-13"></span>
<span id="cb3-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract age class</span></span>
<span id="cb3-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(cat02) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rekey</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ageclass"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+～</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+歳$"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb3-18">           name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"85歳以上"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb3-19">           name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"110歳以上"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-21"></span>
<span id="cb3-22">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract year</span></span>
<span id="cb3-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(time) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rekey</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+年$"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-27"></span>
<span id="cb3-28">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get e-Stat data</span></span>
<span id="cb3-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pop"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-30"></span>
<span id="cb3-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename_with</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> .x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-32">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_remove</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_name$"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-33">  <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;">sex =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_factor</span>(sex),</span>
<span id="cb3-34">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_number</span>(year),</span>
<span id="cb3-35"></span>
<span id="cb3-36">         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the minimum age for each age class</span></span>
<span id="cb3-37">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age_from =</span> ageclass <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-38">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-39">           stringi<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;">stri_trans_nfkc</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-40">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(),</span>
<span id="cb3-41"></span>
<span id="cb3-42">         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Treat "85歳以上" as the highest age class</span></span>
<span id="cb3-43">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ageclass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(age_from <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"85歳以上"</span>,</span>
<span id="cb3-44">                              <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> ageclass) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-45">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_factor</span>(),</span>
<span id="cb3-46"></span>
<span id="cb3-47">         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add age group</span></span>
<span id="cb3-48">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">agegroup =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">between</span>(age_from, <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;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Youth population"</span>,</span>
<span id="cb3-49">                              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">between</span>(age_from, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Working-age population"</span>,</span>
<span id="cb3-50">                              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">between</span>(age_from, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Young-old population"</span>,</span>
<span id="cb3-51">                              age_from <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Old-old population"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-52">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_factor</span>(),</span>
<span id="cb3-53">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_number</span>(pop)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-54"></span>
<span id="cb3-55">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Aggregate the "85歳以上" population</span></span>
<span id="cb3-56">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(year, sex, ageclass, agegroup) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-57">  <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;">pop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(pop),</span>
<span id="cb3-58">            <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></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>The total number of data is 796.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(pop))</span></code></pre></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">year</th>
<th style="text-align: left;">sex</th>
<th style="text-align: left;">ageclass</th>
<th style="text-align: left;">agegroup</th>
<th style="text-align: right;">pop</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1920</td>
<td style="text-align: left;">男</td>
<td style="text-align: left;">０～４歳</td>
<td style="text-align: left;">Youth population</td>
<td style="text-align: right;">3752627</td>
</tr>
<tr class="even">
<td style="text-align: right;">1920</td>
<td style="text-align: left;">男</td>
<td style="text-align: left;">５～９歳</td>
<td style="text-align: left;">Youth population</td>
<td style="text-align: right;">3467156</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1920</td>
<td style="text-align: left;">男</td>
<td style="text-align: left;">10～14歳</td>
<td style="text-align: left;">Youth population</td>
<td style="text-align: right;">3089225</td>
</tr>
<tr class="even">
<td style="text-align: right;">1920</td>
<td style="text-align: left;">男</td>
<td style="text-align: left;">15～19歳</td>
<td style="text-align: left;">Working-age population</td>
<td style="text-align: right;">2749022</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1920</td>
<td style="text-align: left;">男</td>
<td style="text-align: left;">20～24歳</td>
<td style="text-align: left;">Working-age population</td>
<td style="text-align: right;">2316479</td>
</tr>
<tr class="even">
<td style="text-align: right;">1920</td>
<td style="text-align: left;">男</td>
<td style="text-align: left;">25～29歳</td>
<td style="text-align: left;">Working-age population</td>
<td style="text-align: right;">2008005</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="creating-the-population-pyramid-animation" class="level2">
<h2 class="anchored" data-anchor-id="creating-the-population-pyramid-animation">Creating the population pyramid animation</h2>
<p>By using R’s gganimate package, you can turn a ggplot2 graph into an animation.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gganimate)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: package 'gganimate' was built under R version 4.6.1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Data for displaying age groups</span></span>
<span id="cb8-2">agegroup <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(sex, agegroup) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-4">  <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;">ageclass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(ageclass)),</span>
<span id="cb8-5">            <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-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;">hjust =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(sex <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"男"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.25</span>,</span>
<span id="cb8-7">                           sex <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"女"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>))</span>
<span id="cb8-8"></span>
<span id="cb8-9">poppyramid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-10"></span>
<span id="cb8-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert male population to negative values to create the population pyramid</span></span>
<span id="cb8-12">  <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;">pop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(sex <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"男"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>pop, pop)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-13"></span>
<span id="cb8-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(ageclass, pop,</span>
<span id="cb8-15">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> sex,</span>
<span id="cb8-16">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> agegroup)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> agegroup,</span>
<span id="cb8-19">            <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;">label =</span> agegroup,</span>
<span id="cb8-20">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> hjust),</span>
<span id="cb8-21">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_discrete</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>
<span id="cb8-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Population [thousands]"</span>,</span>
<span id="cb8-24"></span>
<span id="cb8-25">                     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert labels to absolute values in thousands</span></span>
<span id="cb8-26">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> purrr<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;">compose</span>(scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label_comma</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-3</span>), abs)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_flip</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> sex,</span>
<span id="cb8-30">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_x"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year {frame_time %/% 5 * 5}"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-33"></span>
<span id="cb8-34">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert to animation</span></span>
<span id="cb8-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transition_time</span>(year)</span>
<span id="cb8-36"></span>
<span id="cb8-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Change width and height</span></span>
<span id="cb8-38"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">animate</span>(poppyramid,</span>
<span id="cb8-39">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>,</span>
<span id="cb8-40">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">600</span>,</span>
<span id="cb8-41">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">units =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"px"</span>,</span>
<span id="cb8-42">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">res =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>,</span>
<span id="cb8-43">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">renderer =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gifski_renderer</span>())</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2023/01/create-an-animation-of-a-population-pyramid-in-r_files/figure-html/create-gif-1.gif" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>


</section>

 ]]></description>
  <category>ggplot2</category>
  <category>gganimate</category>
  <category>e-Stat</category>
  <category>jpstat</category>
  <category>R</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2023/01/create-an-animation-of-a-population-pyramid-in-r.html</guid>
  <pubDate>Sat, 31 Dec 2022 15:00:00 GMT</pubDate>
  <media:content url="https://uchidamizuki.quarto.pub/blog/en/posts/2023/01/poppyramid.gif" medium="image" type="image/gif"/>
</item>
<item>
  <title>Efficiently retrieve Japanese statistical data in R (with the e-Stat API and the jpstat package)</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2022/12/call-e-stat-api-in-r.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2022/12/call-e-stat-api-in-r.html">my Japanese article</a>.</p>
<section id="about-this-article" class="level2">
<h2 class="anchored" data-anchor-id="about-this-article">About this article</h2>
<p>This article is Day 12 of the “<a href="https://qiita.com/advent-calendar/2022/rlang"><strong>R Advent Calendar 2022</strong></a>”.</p>
<p>Last year marked 150 years since the establishment of government statistics in Japan (<a href="https://www.stat.go.jp/museum/toukei150/img/nenpyo/pdf/nenpyo_heisei_reiwa.pdf">Chronology of statistics in the Heisei and Reiwa eras</a>). More recently, it has become possible to browse and download a wide variety of government statistical data through the <a href="https://www.e-stat.go.jp">Portal Site of Official Statistics of Japan (e-Stat)</a>.</p>
<p>e-Stat also provides a convenient <a href="https://www.e-stat.go.jp/api/">API</a> (see the usage guide <a href="https://www.e-stat.go.jp/api/api-info/api-guide">here</a>; please check the <a href="https://www.e-stat.go.jp/api/agreement/">terms of use</a> beforehand. Before using the API, you also need to complete <a href="https://www.e-stat.go.jp/mypage/user/preregister">user registration</a>).</p>
<p>In this article, I introduce <strong>how to use the e-Stat API efficiently</strong> with R’s <a href="https://uchidamizuki.github.io/jpstat/"><strong>jpstat</strong></a> <strong>package</strong>.</p>
</section>
<section id="about-e-stat" class="level2">
<h2 class="anchored" data-anchor-id="about-e-stat">About e-Stat</h2>
<p>e-Stat organizes a wide variety of government statistical databases. Here, let’s take a look at the <a href="https://www.e-stat.go.jp/dbview?sid=0003224282"><strong>database on sleep duration</strong></a> from the results of the 2015 National Health and Nutrition Survey.</p>
<p>Opening the database displays a statistical table as shown below, and you can download the data from the “<strong>Download</strong>” button in the upper right.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2022/12/images/estat_1.png" class="img-fluid figure-img" style="width:75.0%"></p>
<figcaption>e-Stat database: statistical table view</figcaption>
</figure>
</div>
<p>Clicking the “<strong>Select display items</strong>” button in the upper left lets you choose which data items (age group, sex, etc.) to display.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2022/12/images/estat_2.png" class="img-fluid figure-img" style="width:75.0%"></p>
<figcaption>e-Stat database: display item selection screen</figcaption>
</figure>
</div>
<p>For example, if you want to select age groups, clicking the “<strong>Select items</strong>” button for age group brings up a screen like the one below, where you can choose the age groups.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2022/12/images/estat_3.png" class="img-fluid figure-img" style="width:75.0%"></p>
<figcaption>e-Stat database: display item settings screen</figcaption>
</figure>
</div>
<p>After selecting the display items, clicking the “<strong>Download</strong>” button lets you download only the data for the selected items.</p>
<p>In this way, e-Stat makes it easy to extract and download data. However, if you want to improve the <strong>reproducibility of your data-retrieval work</strong> or make <strong>programmatic data extraction and retrieval more efficient</strong>, using the <strong>e-Stat API</strong> is recommended.</p>
</section>
<section id="using-the-e-stat-api-with-the-jpstat-package" class="level2">
<h2 class="anchored" data-anchor-id="using-the-e-stat-api-with-the-jpstat-package">Using the e-Stat API with the jpstat package</h2>
<p>To perform, via the e-Stat API, the same data extraction and download process described above for e-Stat, you need to follow steps like these.</p>
<ol type="1">
<li><a href="https://www.e-stat.go.jp/api/api-info/e-stat-manual3-0#api_2_2"><strong>Retrieve metadata</strong></a> and set parameters: retrieve and select the display item data, and set the <a href="https://www.e-stat.go.jp/api/api-info/e-stat-manual3-0#api_3_4">API parameters</a> corresponding to the selected items</li>
<li><a href="https://www.e-stat.go.jp/api/api-info/e-stat-manual3-0#api_2_3"><strong>Retrieve statistical data</strong></a>: retrieve the selected data and format it into a table</li>
</ol>
<p>The <a href="https://uchidamizuki.github.io/jpstat/">jpstat</a> package was developed to carry out this series of tasks efficiently in R<sup>1</sup>. The jpstat package can be installed from CRAN.</p>
<p>Here, with the goal of <strong>plotting sleep duration by sex and age group</strong>, let’s retrieve data from the <a href="https://www.e-stat.go.jp/dbview?sid=0003224282">sleep duration database</a> (2015) introduced above. First, load the required packages.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jpstat"</span>)</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(jpstat)</span></code></pre></div>
</div>
<section id="step-1-display-and-extract-the-metadata-display-items" class="level3">
<h3 class="anchored" data-anchor-id="step-1-display-and-extract-the-metadata-display-items">Step 1: Display and extract the metadata (display items)</h3>
<p>To use the e-Stat API, you first need to complete <a href="https://www.e-stat.go.jp/mypage/user/preregister"><strong>user registration</strong></a> and <strong>obtain an application ID called <code>appId</code></strong><sup>2</sup>.</p>
<p>By passing the <code>appId</code> and the database URL (or the statistical table ID, <code>statsDataId</code>) to the <code>estat()</code> function, you can retrieve the metadata (display items)<sup>3</sup>.</p>
<p>First, let’s look at the metadata for “age group (<code>cat01</code>)” ( <code>cat01</code> is the classification name used in the API). The <code>activate()</code> function displays the metadata, and the <code>filter()</code> function lets you select items. Here, since we only need the data broken down by age group, we remove the “total” data<sup>4</sup>.</p>
<p>By using the pipe operator <code>|&gt;</code>, you can continue extracting metadata for parameters other than <code>cat01</code> as shown below. Here, we extract the <strong>number of respondents by sex, age group, and sleep duration</strong>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Replace this with your own appId</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.setenv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ESTAT_API_KEY =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Your appId"</span>)</span></code></pre></div>
</div>
<div class="cell">
<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">estat_sleeptime_2015 <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;">estat</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">statsDataId =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.e-stat.go.jp/dbview?sid=0003224282"</span>)</span></code></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># View and select the metadata</span></span>
<span id="cb5-2">estat_sleeptime_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(cat01) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"総数"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># ☐ tab:   表章項目     [2] &lt;code, name, level, unit&gt;
# ☒ cat01: 年齢階級     [6] &lt;code, name, level, parentCode&gt;
# ☐ cat02: 睡眠の質     [8] &lt;code, name, level, parentCode&gt;
# ☐ cat03: 性別         [3] &lt;code, name, level, parentCode&gt;
# ☐ cat04: 平均睡眠時間 [6] &lt;code, name, level, parentCode&gt;
# ☐ time:  時間軸(年次) [1] &lt;code, name, level&gt;
# 
# A tibble: 6 × 5
# Stickers: .estat_rowid
  .estat_rowid code  name      level parentCode
*        &lt;int&gt; &lt;chr&gt; &lt;chr&gt;     &lt;chr&gt; &lt;chr&gt;     
1            2 160   20歳-29歳 2     100       
2            3 170   30歳-39歳 2     100       
3            4 180   40歳-49歳 2     100       
4            5 190   50歳-59歳 2     100       
5            6 210   60歳-69歳 2     100       
6            7 220   70歳以上  2     100       </code></pre>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">estat_sleeptime_2015_filtered <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> estat_sleeptime_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-2"></span>
<span id="cb7-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tabulation item</span></span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(tab) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"人数"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-6"></span>
<span id="cb7-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Age group</span></span>
<span id="cb7-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(cat01) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"総数"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-10"></span>
<span id="cb7-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sleep quality</span></span>
<span id="cb7-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(cat02) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"総数"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-14"></span>
<span id="cb7-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sex</span></span>
<span id="cb7-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(cat03) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</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;">"男性"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"女性"</span>))</span></code></pre></div>
</div>
</section>
<section id="step-2-retrieve-download-the-statistical-data" class="level3">
<h3 class="anchored" data-anchor-id="step-2-retrieve-download-the-statistical-data">Step 2: Retrieve (download) the statistical data</h3>
<p>Applying <code>collect()</code> after extracting the data retrieves the statistical data. You can also use the <code>n</code> argument of <code>collect()</code> to name the column of retrieved data. Here, we name it <code>"person"</code>.</p>
<p>Looking at the retrieved data <code>data_sleeptime_2015</code>, you can see it has many columns, making it hard to work with for analysis. In <strong>Step 2+α</strong>, I explain how to retrieve and reshape the data at the same time.</p>
<div class="cell">
<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">data_sleeptime_2015 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> estat_sleeptime_2015_filtered <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-2"></span>
<span id="cb8-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Retrieve the data and convert to numeric</span></span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"person"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-5">  <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;">person =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_number</span>(person))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>The total number of data is 72.</code></pre>
</div>
<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">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data_sleeptime_2015, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span></code></pre></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 4%">
<col style="width: 5%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 4%">
<col style="width: 5%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 4%">
<col style="width: 5%">
<col style="width: 3%">
<col style="width: 7%">
<col style="width: 4%">
<col style="width: 5%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 2%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">tab_code</th>
<th style="text-align: left;">tab_name</th>
<th style="text-align: left;">tab_level</th>
<th style="text-align: left;">tab_unit</th>
<th style="text-align: left;">cat01_code</th>
<th style="text-align: left;">cat01_name</th>
<th style="text-align: left;">cat01_level</th>
<th style="text-align: left;">cat01_parentCode</th>
<th style="text-align: left;">cat02_code</th>
<th style="text-align: left;">cat02_name</th>
<th style="text-align: left;">cat02_level</th>
<th style="text-align: left;">cat02_parentCode</th>
<th style="text-align: left;">cat03_code</th>
<th style="text-align: left;">cat03_name</th>
<th style="text-align: left;">cat03_level</th>
<th style="text-align: left;">cat03_parentCode</th>
<th style="text-align: left;">cat04_code</th>
<th style="text-align: left;">cat04_name</th>
<th style="text-align: left;">cat04_level</th>
<th style="text-align: left;">cat04_parentCode</th>
<th style="text-align: left;">time_code</th>
<th style="text-align: left;">time_name</th>
<th style="text-align: left;">time_level</th>
<th style="text-align: right;">person</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">100</td>
<td style="text-align: left;">人数</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">人</td>
<td style="text-align: left;">160</td>
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">総数</td>
<td style="text-align: left;">1</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">110</td>
<td style="text-align: left;">男性</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">110</td>
<td style="text-align: left;">５時間未満</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">2015000000</td>
<td style="text-align: left;">2015年</td>
<td style="text-align: left;">1</td>
<td style="text-align: right;">23</td>
</tr>
<tr class="even">
<td style="text-align: left;">100</td>
<td style="text-align: left;">人数</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">人</td>
<td style="text-align: left;">160</td>
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">総数</td>
<td style="text-align: left;">1</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">110</td>
<td style="text-align: left;">男性</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">120</td>
<td style="text-align: left;">５時間以上６時間未満</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">2015000000</td>
<td style="text-align: left;">2015年</td>
<td style="text-align: left;">1</td>
<td style="text-align: right;">86</td>
</tr>
<tr class="odd">
<td style="text-align: left;">100</td>
<td style="text-align: left;">人数</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">人</td>
<td style="text-align: left;">160</td>
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">総数</td>
<td style="text-align: left;">1</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">110</td>
<td style="text-align: left;">男性</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">130</td>
<td style="text-align: left;">６時間以上７時間未満</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">2015000000</td>
<td style="text-align: left;">2015年</td>
<td style="text-align: left;">1</td>
<td style="text-align: right;">88</td>
</tr>
<tr class="even">
<td style="text-align: left;">100</td>
<td style="text-align: left;">人数</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">人</td>
<td style="text-align: left;">160</td>
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">総数</td>
<td style="text-align: left;">1</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">110</td>
<td style="text-align: left;">男性</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">140</td>
<td style="text-align: left;">７時間以上８時間未満</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">2015000000</td>
<td style="text-align: left;">2015年</td>
<td style="text-align: left;">1</td>
<td style="text-align: right;">37</td>
</tr>
<tr class="odd">
<td style="text-align: left;">100</td>
<td style="text-align: left;">人数</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">人</td>
<td style="text-align: left;">160</td>
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">総数</td>
<td style="text-align: left;">1</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">110</td>
<td style="text-align: left;">男性</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">150</td>
<td style="text-align: left;">８時間以上９時間未満</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">2015000000</td>
<td style="text-align: left;">2015年</td>
<td style="text-align: left;">1</td>
<td style="text-align: right;">19</td>
</tr>
<tr class="even">
<td style="text-align: left;">100</td>
<td style="text-align: left;">人数</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">人</td>
<td style="text-align: left;">160</td>
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">総数</td>
<td style="text-align: left;">1</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">110</td>
<td style="text-align: left;">男性</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">160</td>
<td style="text-align: left;">９時間以上</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">2015000000</td>
<td style="text-align: left;">2015年</td>
<td style="text-align: left;">1</td>
<td style="text-align: right;">3</td>
</tr>
<tr class="odd">
<td style="text-align: left;">100</td>
<td style="text-align: left;">人数</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">人</td>
<td style="text-align: left;">160</td>
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">総数</td>
<td style="text-align: left;">1</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">120</td>
<td style="text-align: left;">女性</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">110</td>
<td style="text-align: left;">５時間未満</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">2015000000</td>
<td style="text-align: left;">2015年</td>
<td style="text-align: left;">1</td>
<td style="text-align: right;">28</td>
</tr>
<tr class="even">
<td style="text-align: left;">100</td>
<td style="text-align: left;">人数</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">人</td>
<td style="text-align: left;">160</td>
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">総数</td>
<td style="text-align: left;">1</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">120</td>
<td style="text-align: left;">女性</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">120</td>
<td style="text-align: left;">５時間以上６時間未満</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">2015000000</td>
<td style="text-align: left;">2015年</td>
<td style="text-align: left;">1</td>
<td style="text-align: right;">106</td>
</tr>
<tr class="odd">
<td style="text-align: left;">100</td>
<td style="text-align: left;">人数</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">人</td>
<td style="text-align: left;">160</td>
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">総数</td>
<td style="text-align: left;">1</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">120</td>
<td style="text-align: left;">女性</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">130</td>
<td style="text-align: left;">６時間以上７時間未満</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">2015000000</td>
<td style="text-align: left;">2015年</td>
<td style="text-align: left;">1</td>
<td style="text-align: right;">94</td>
</tr>
<tr class="even">
<td style="text-align: left;">100</td>
<td style="text-align: left;">人数</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">人</td>
<td style="text-align: left;">160</td>
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">総数</td>
<td style="text-align: left;">1</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">120</td>
<td style="text-align: left;">女性</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">140</td>
<td style="text-align: left;">７時間以上８時間未満</td>
<td style="text-align: left;">2</td>
<td style="text-align: left;">100</td>
<td style="text-align: left;">2015000000</td>
<td style="text-align: left;">2015年</td>
<td style="text-align: left;">1</td>
<td style="text-align: right;">55</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="step-2α-retrieve-and-reshape-the-data-at-the-same-time" class="level3">
<h3 class="anchored" data-anchor-id="step-2α-retrieve-and-reshape-the-data-at-the-same-time">Step 2+α: Retrieve and reshape the data at the same time</h3>
<p>When you retrieve e-Stat data with jpstat, columns (such as <code>cat01_code</code> and <code>cat01_name</code>) are created from the parameter names (such as <code>cat01</code>) combined with the column name for each item (<code>code</code>, <code>name</code>, etc.).</p>
<p>In jpstat, you can tidy up the data by renaming parameters with the <code>rekey()</code> function and selecting columns per item with the <code>select()</code> function<sup>5</sup>. Writing the code as below produces a nicely tidied dataset.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">data_sleeptime_2015 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> estat_sleeptime_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(tab) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"人数"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-5"></span>
<span id="cb11-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(cat01) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rekey</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ageclass"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"総数"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-10"></span>
<span id="cb11-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(cat02) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"総数"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-14"></span>
<span id="cb11-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(cat03) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rekey</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sex"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</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;">"男性"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"女性"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-19"></span>
<span id="cb11-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(cat04) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rekey</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sleeptime"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-23"></span>
<span id="cb11-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">activate</span>(time) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-26"></span>
<span id="cb11-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"person"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-28">  <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;">person =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_number</span>(person))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>The total number of data is 72.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data_sleeptime_2015, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span></code></pre></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">ageclass_name</th>
<th style="text-align: left;">sex_name</th>
<th style="text-align: left;">sleeptime_name</th>
<th style="text-align: right;">person</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">男性</td>
<td style="text-align: left;">５時間未満</td>
<td style="text-align: right;">23</td>
</tr>
<tr class="even">
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">男性</td>
<td style="text-align: left;">５時間以上６時間未満</td>
<td style="text-align: right;">86</td>
</tr>
<tr class="odd">
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">男性</td>
<td style="text-align: left;">６時間以上７時間未満</td>
<td style="text-align: right;">88</td>
</tr>
<tr class="even">
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">男性</td>
<td style="text-align: left;">７時間以上８時間未満</td>
<td style="text-align: right;">37</td>
</tr>
<tr class="odd">
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">男性</td>
<td style="text-align: left;">８時間以上９時間未満</td>
<td style="text-align: right;">19</td>
</tr>
<tr class="even">
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">男性</td>
<td style="text-align: left;">９時間以上</td>
<td style="text-align: right;">3</td>
</tr>
<tr class="odd">
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">女性</td>
<td style="text-align: left;">５時間未満</td>
<td style="text-align: right;">28</td>
</tr>
<tr class="even">
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">女性</td>
<td style="text-align: left;">５時間以上６時間未満</td>
<td style="text-align: right;">106</td>
</tr>
<tr class="odd">
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">女性</td>
<td style="text-align: left;">６時間以上７時間未満</td>
<td style="text-align: right;">94</td>
</tr>
<tr class="even">
<td style="text-align: left;">20歳-29歳</td>
<td style="text-align: left;">女性</td>
<td style="text-align: left;">７時間以上８時間未満</td>
<td style="text-align: right;">55</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="bonus-plotting-the-retrieved-data" class="level3">
<h3 class="anchored" data-anchor-id="bonus-plotting-the-retrieved-data">Bonus: Plotting the retrieved data</h3>
<p>Finally, let’s plot the <strong>2015 sleep duration data by sex and age group</strong> that we retrieved. The plot shows that the pattern of sleep duration by age group differs between men and women.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">data_sleeptime_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ageclass_name =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_factor</span>(ageclass_name),</span>
<span id="cb14-3">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sex_name =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_factor</span>(sex_name),</span>
<span id="cb14-4">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sleeptime_name =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_factor</span>(sleeptime_name)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(ageclass_name, sex_name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> person <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;">sum</span>(person)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(ageclass_name, prop,</span>
<span id="cb14-9">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_rev</span>(sleeptime_name))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</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;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(prop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>,</span>
<span id="cb14-12">                                scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label_percent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)(prop),</span>
<span id="cb14-13">                                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)),</span>
<span id="cb14-14">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_stack</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_discrete</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age group"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Proportion"</span>,</span>
<span id="cb14-17">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label_percent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sleep duration"</span>,</span>
<span id="cb14-19">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Spectral"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> sex_name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</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;">guide_axis</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.dodge =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://uchidamizuki.quarto.pub/blog/en/posts/2022/12/call-e-stat-api-in-r_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>In this article, I introduced how to efficiently retrieve Japanese statistical data using the e-Stat API and the <a href="https://uchidamizuki.github.io/jpstat/">jpstat</a> package.</p>
<p>Retrieving statistical data in R improves the reproducibility and efficiency of your work. The jpstat package is also convenient because it lets you retrieve and reshape data at the same time. I encourage you to give it a try.</p>


</section>


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

<ol>
<li id="fn1"><p>The e-Stat API provides a variety of other functionality besides metadata retrieval and statistical data retrieval (see the <a href="https://www.e-stat.go.jp/api/api-info/e-stat-manual3-0">API specification</a>).↩︎</p></li>
<li id="fn2"><p>To obtain an application ID, you need to register a URL. If you are not going to use it on a public site, it is recommended that you enter a local address such as <code>http://test.localhost/</code> (for details, see the <a href="https://www.e-stat.go.jp/api/api-info/api-guide">usage guide</a>).↩︎</p></li>
<li id="fn3"><p>Clicking the “<strong>API</strong>” button in the upper right of an e-Stat page displays the corresponding API query. You can also retrieve metadata by directly entering the <code>statsDataId</code> found in that query.↩︎</p></li>
<li id="fn4"><p>Note, however, that each parameter has an upper limit of 100 items, so if filtering would still leave a large number of items, it is recommended to select all items without filtering.↩︎</p></li>
<li id="fn5"><p>You can also use the <code>select()</code> function to drop all columns for a given item. This is convenient when you want to remove items that aren’t needed for analysis, such as when only the “total” is selected.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>e-Stat</category>
  <category>jpstat</category>
  <category>R</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2022/12/call-e-stat-api-in-r.html</guid>
  <pubDate>Sun, 11 Dec 2022 15:00:00 GMT</pubDate>
  <media:content url="https://github.com/UchidaMizuki/jpstat/blob/main/man/figures/logo.png?raw=true" medium="image"/>
</item>
<item>
  <title>Let’s try using Parquet instead of CSV</title>
  <link>https://uchidamizuki.quarto.pub/blog/en/posts/2022/06/use-parquet-instead-of-csv.html</link>
  <description><![CDATA[ 





<p>Note: This article is translated from <a href="https://uchidamizuki.quarto.pub/blog/posts/2022/06/use-parquet-instead-of-csv.html">my Japanese article</a>.</p>
<p>In this article, I introduce <strong><a href="https://parquet.apache.org">Parquet</a>, a promising alternative to CSV that is also well-suited for big data analysis</strong>.</p>
<p>Now, <strong>data frames</strong> are one of the most fundamental data structures in data analysis. By using packages for manipulating data frames, such as <strong><a href="https://tibble.tidyverse.org">tibble</a>/<a href="https://dplyr.tidyverse.org">dplyr</a> in R</strong> and <strong><a href="https://pandas.pydata.org">pandas</a> in Python</strong>, we can <strong>carry out data analysis that used to be done with spreadsheet software like Excel even more efficiently</strong>.</p>
<p>While data analysis tools have become this well-developed, <strong>CSV, which has high compatibility with software like Excel,</strong> is still widely used for storing data. However, CSV is not necessarily a file format well-suited for data analysis. So, let’s compare Parquet, which is increasingly being used <strong>as an alternative to CSV</strong>, with CSV.</p>
<section id="preparing-sample-data" class="level2">
<h2 class="anchored" data-anchor-id="preparing-sample-data">Preparing sample data</h2>
<p>To compare CSV and Parquet, let’s first prepare sample data typical of data analysis. This time, we’ll create sample data from <code>who</code> (<a href="https://www.who.int/teams/global-tuberculosis-programme/data">World Health Organization (WHO) tuberculosis data</a>) provided by the tidyr package.</p>
<p>In recent years, the concept of <strong><a href="https://ja.wikipedia.org/wiki/Tidy_data">tidy data</a></strong> has become widespread in data analysis. Tidy data is data in which each variable forms a single column, and each observation (value) forms a single row.</p>
<p>So, can <code>who</code> be considered tidy data? <code>who</code> has many columns such as <code>"new_sp_m014"</code> through <code>"newrel_f65"</code>, but each of these columns actually contains multiple variables, such as the diagnosis result (<code>sp</code> or <code>sel</code>), gender (<code>m</code> or <code>f</code>), and age group (<code>014</code> or <code>65</code>). Therefore, <strong><code>who</code> is not tidy data</strong>. So, following <a href="https://tidyr.tidyverse.org/articles/pivot.html">this article</a>, we’ll <strong>transform it into <code>who_longer</code>, which is tidy data</strong>.</p>
<p>In data analysis, <code>who_longer</code>, being tidy data, is easier to work with than <code>who</code>. However, we can see that <code>who_longer</code> (about 400,000 rows) has roughly 50 times more rows than <code>who</code> (about 7,000 rows). Therefore, storing data like <code>who_longer</code>, which is tidy data, as a text file such as CSV results in a large increase in file size.</p>
<p>In this way, we can see that <strong>while tidy data is well-suited for data analysis, it is not well-suited for storage as a text file like CSV</strong>. However, <strong>this data storage problem can be solved by using Parquet</strong>.</p>
<p>Here, let’s compare <code>who</code>, which is not tidy data, with <code>who_longer</code>, which is tidy data.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(fs)</span></code></pre></div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">levels_gender <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"f"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"m"</span>)</span>
<span id="cb2-2">levels_age <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"014"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1524"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2534"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3544"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4554"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5564"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"65"</span>)</span>
<span id="cb2-3"></span>
<span id="cb2-4">who_longer <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> who <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> new_sp_m014<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>newrel_f65,</span>
<span id="cb2-6">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"diagnosis"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>), </span>
<span id="cb2-7">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"new_?(.*)_(.)(.*)"</span>,</span>
<span id="cb2-8">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_transform =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gender =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> .x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-9">                                        readr<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;">parse_factor</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> levels_gender),</span>
<span id="cb2-10">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> .x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-11">                                        readr<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;">parse_factor</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> levels_age,</span>
<span id="cb2-12">                                                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ordered =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)),</span>
<span id="cb2-13">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Before tidying</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(who, <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 class="cell-output cell-output-stdout">
<pre><code># A tibble: 7,240 × 60
  country   iso2  iso3   year new_sp_m014 new_sp_m1524 new_sp_m2534 new_sp_m3544
  &lt;chr&gt;     &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;        &lt;dbl&gt;        &lt;dbl&gt;
1 Afghanis… AF    AFG    1980          NA           NA           NA           NA
2 Afghanis… AF    AFG    1981          NA           NA           NA           NA
3 Afghanis… AF    AFG    1982          NA           NA           NA           NA
4 Afghanis… AF    AFG    1983          NA           NA           NA           NA
5 Afghanis… AF    AFG    1984          NA           NA           NA           NA
# ℹ 7,235 more rows
# ℹ 52 more variables: new_sp_m4554 &lt;dbl&gt;, new_sp_m5564 &lt;dbl&gt;,
#   new_sp_m65 &lt;dbl&gt;, new_sp_f014 &lt;dbl&gt;, new_sp_f1524 &lt;dbl&gt;,
#   new_sp_f2534 &lt;dbl&gt;, new_sp_f3544 &lt;dbl&gt;, new_sp_f4554 &lt;dbl&gt;,
#   new_sp_f5564 &lt;dbl&gt;, new_sp_f65 &lt;dbl&gt;, new_sn_m014 &lt;dbl&gt;,
#   new_sn_m1524 &lt;dbl&gt;, new_sn_m2534 &lt;dbl&gt;, new_sn_m3544 &lt;dbl&gt;,
#   new_sn_m4554 &lt;dbl&gt;, new_sn_m5564 &lt;dbl&gt;, new_sn_m65 &lt;dbl&gt;, …</code></pre>
</div>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># After tidying</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(who_longer, <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 class="cell-output cell-output-stdout">
<pre><code># A tibble: 405,440 × 8
  country     iso2  iso3   year diagnosis gender age   count
  &lt;chr&gt;       &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;chr&gt;     &lt;fct&gt;  &lt;ord&gt; &lt;dbl&gt;
1 Afghanistan AF    AFG    1980 sp        m      014      NA
2 Afghanistan AF    AFG    1980 sp        m      1524     NA
3 Afghanistan AF    AFG    1980 sp        m      2534     NA
4 Afghanistan AF    AFG    1980 sp        m      3544     NA
5 Afghanistan AF    AFG    1980 sp        m      4554     NA
# ℹ 405,435 more rows</code></pre>
</div>
</div>
</section>
<section id="saving-csv-and-parquet-files" class="level2">
<h2 class="anchored" data-anchor-id="saving-csv-and-parquet-files">Saving CSV and Parquet files</h2>
<p>In R, you can save CSV files with <code>write_csv()</code>. Similarly, you can save Parquet files with <code>write_parquet()</code> from the arrow package. Let’s try saving <code>who_longer</code> as both CSV and Parquet.</p>
<p>As you can see, both CSV and Parquet make it easy to save data.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(arrow)</span>
<span id="cb7-2"></span>
<span id="cb7-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save as CSV</span></span>
<span id="cb7-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_csv</span>(who_longer, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"who_longer.csv"</span>)</span>
<span id="cb7-5"></span>
<span id="cb7-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save as Parquet</span></span>
<span id="cb7-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_parquet</span>(who_longer, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"who_longer.parquet"</span>)</span></code></pre></div>
</div>
</section>
<section id="advantages-of-parquet-compared-to-csv" class="level2">
<h2 class="anchored" data-anchor-id="advantages-of-parquet-compared-to-csv">Advantages of Parquet compared to CSV</h2>
<p>From here, we’ll compare the saved CSV and Parquet files of <code>who_longer</code> and introduce the advantages of Parquet over CSV.</p>
<section id="advantage-1-smaller-file-size-than-csv" class="level3">
<h3 class="anchored" data-anchor-id="advantage-1-smaller-file-size-than-csv">Advantage 1: Smaller file size than CSV</h3>
<p>As mentioned earlier, tidy data tends to have many rows, which makes it poorly suited for storage as CSV, and using Parquet is preferable.</p>
<p>In fact, the file sizes of the CSV and Parquet versions of <code>who_longer</code> are 14.1 MB and 154 KB, respectively, showing that <strong>Parquet takes up only about 1% of the file size of CSV</strong>.</p>
<p>This level of size reduction can’t be expected in every case, but since <strong>Parquet is column-oriented and compresses data</strong>, it can be said to be <strong>a data format well-suited for storing tidy data</strong>, which is commonly used in R and elsewhere.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CSV</span></span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file_size</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"who_longer.csv"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>14.1M</code></pre>
</div>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Parquet</span></span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file_size</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"who_longer.parquet"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>161K</code></pre>
</div>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">units<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;">set_units</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file_size</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"who_longer.parquet"</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;">file_size</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"who_longer.csv"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-2">  units<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;">set_units</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">%</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>1.116534 [%]</code></pre>
</div>
</div>
</section>
<section id="advantage-2-easier-to-read-than-csv" class="level3">
<h3 class="anchored" data-anchor-id="advantage-2-easier-to-read-than-csv">Advantage 2: Easier to read than CSV</h3>
<p>Just as data can be written with <code>write_csv()</code>/<code>write_parquet()</code>, <strong>CSV/Parquet data can be read with <code>read_csv()</code>/<code>read_parquet()</code></strong>.</p>
<p>Since <strong>CSV</strong> is a text format, <strong>you need to specify the type of each column with <code>col_types</code> when reading it</strong> (by default, the types are guessed automatically).</p>
<p>On the other hand, since <strong>Parquet</strong> stores the type information of each column at write time, <strong>there is no need to specify types when reading it</strong>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read CSV</span></span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"who_longer.csv"</span>,</span>
<span id="cb14-3">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_types =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.default =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c"</span>,</span>
<span id="cb14-4">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"i"</span>,</span>
<span id="cb14-5">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"i"</span>))</span>
<span id="cb14-6"></span>
<span id="cb14-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read Parquet</span></span>
<span id="cb14-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_parquet</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"who_longer.parquet"</span>)</span></code></pre></div>
</div>
</section>
<section id="advantage-3-better-suited-than-csv-for-reading-and-aggregating-big-data" class="level3">
<h3 class="anchored" data-anchor-id="advantage-3-better-suited-than-csv-for-reading-and-aggregating-big-data">Advantage 3: Better suited than CSV for reading and aggregating big data</h3>
<p>CSV is not well-suited for storing big data, and until now, it has been necessary to use something like SQL for storing big data instead.</p>
<p>In R, packages such as dplyr (dbplyr) and DBI make it easy to use SQL, but since SQL requires connecting to and disconnecting from a database, it works differently from CSV and may be a hurdle for beginners.</p>
<p>Also, since (most?) <strong>SQL databases are row-oriented</strong>, they are <strong>well-suited for adding, updating, and deleting data</strong>, but for <strong>storing and aggregating data used in data analysis, the column-oriented Parquet seems better suited</strong>.</p>
<p>When aggregating big data using CSV files, you need to load all the data into memory at once. This can put pressure on memory during data loading.</p>
<p>With Parquet, by setting <strong><code>as_data_frame = FALSE</code> when reading</strong>, you can, just like with SQL, <strong>filter and aggregate data without loading it into memory</strong>.</p>
<p>Here, let’s calculate the number of patients in Japan by year and diagnosis. <strong>You can efficiently build the query using dplyr functions such as <code>filter()</code>, <code>group_by()</code>, and <code>summarise()</code></strong>. <strong>Finally, calling <code>collect()</code> outputs the result as a data frame</strong>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_parquet</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"who_longer.parquet"</span>,</span>
<span id="cb15-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">as_data_frame =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(country <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Japan"</span>,</span>
<span id="cb15-4">         <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>(count)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb15-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(country, year, diagnosis) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb15-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;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(count),</span>
<span id="cb15-7">            <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb15-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 33 × 4
   country  year diagnosis count
   &lt;chr&gt;   &lt;dbl&gt; &lt;chr&gt;     &lt;dbl&gt;
 1 Japan    1995 sp        14367
 2 Japan    1996 sp        12867
 3 Japan    1997 sp        13571
 4 Japan    1998 sp        11935
 5 Japan    2001 sp        11408
 6 Japan    2002 sp        10807
 7 Japan    2003 sp        10843
 8 Japan    2004 sp        10471
 9 Japan    1999 sp        12909
10 Japan    2000 sp        11853
# ℹ 23 more rows</code></pre>
</div>
</div>
</section>
<section id="advantage-4-can-handle-datasets-made-up-of-multiple-files" class="level3">
<h3 class="anchored" data-anchor-id="advantage-4-can-handle-datasets-made-up-of-multiple-files">Advantage 4: Can handle datasets made up of multiple files</h3>
<p>Because Parquet is column-oriented, unlike row-oriented SQL, it is not well-suited for adding, updating, or deleting data. However, <strong>Parquet makes it easy to read datasets made up of multiple files</strong>, which easily resolves this disadvantage.</p>
<p>Here, let’s use the <code>"who_longer_byage"</code> folder, which contains Parquet files with <code>who_longer</code> split by age group, as a sample dataset.</p>
<p>By using <strong><code>open_dataset("who_longer_byage")</code></strong>, we can <strong>easily perform the same data aggregation as before</strong>, even though the folder contains multiple Parquet files.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir_create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"who_longer_byage"</span>)</span>
<span id="cb17-2">who_longer <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb17-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(age) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb17-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_walk</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> .x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb17-5">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_parquet</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"who_longer_byage/who_longer_{.y$age}.parquet"</span>)),</span>
<span id="cb17-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">open_dataset</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"who_longer_byage"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb18-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(country <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Japan"</span>,</span>
<span id="cb18-3">         <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>(count)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb18-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(country, year, diagnosis) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb18-5">  <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;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(count),</span>
<span id="cb18-6">            <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb18-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 33 × 4
   country  year diagnosis count
   &lt;chr&gt;   &lt;dbl&gt; &lt;chr&gt;     &lt;dbl&gt;
 1 Japan    1995 sp        14367
 2 Japan    1996 sp        12867
 3 Japan    1997 sp        13571
 4 Japan    1998 sp        11935
 5 Japan    2001 sp        11408
 6 Japan    2002 sp        10807
 7 Japan    2003 sp        10843
 8 Japan    2004 sp        10471
 9 Japan    2005 sp        10931
10 Japan    2006 sp        10159
# ℹ 23 more rows</code></pre>
</div>
</div>
</section>
<section id="advantage-5-well-suited-for-exchanging-data-between-r-and-python" class="level3">
<h3 class="anchored" data-anchor-id="advantage-5-well-suited-for-exchanging-data-between-r-and-python">Advantage 5: Well-suited for exchanging data between R and Python</h3>
<p>Since Python’s pandas package supports reading and writing Parquet files, Parquet is also well-suited for exchanging data between R and Python.</p>
<p>Let’s try reading the <code>'who_longer.parquet'</code> file created in R with pandas.</p>
<div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb20-2"></span>
<span id="cb20-3">pd.read_parquet(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'who_longer.parquet'</span>)</span></code></pre></div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>            country iso2 iso3    year diagnosis gender   age   count
0       Afghanistan   AF  AFG  1980.0        sp      m   014     NaN
1       Afghanistan   AF  AFG  1980.0        sp      m  1524     NaN
2       Afghanistan   AF  AFG  1980.0        sp      m  2534     NaN
3       Afghanistan   AF  AFG  1980.0        sp      m  3544     NaN
4       Afghanistan   AF  AFG  1980.0        sp      m  4554     NaN
...             ...  ...  ...     ...       ...    ...   ...     ...
405435     Zimbabwe   ZW  ZWE  2013.0       rel      f  2534  4649.0
405436     Zimbabwe   ZW  ZWE  2013.0       rel      f  3544  3526.0
405437     Zimbabwe   ZW  ZWE  2013.0       rel      f  4554  1453.0
405438     Zimbabwe   ZW  ZWE  2013.0       rel      f  5564   811.0
405439     Zimbabwe   ZW  ZWE  2013.0       rel      f    65   725.0

[405440 rows x 8 columns]</code></pre>
</div>
</div>
</section>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>So far, we’ve introduced the advantages of Parquet available in both R and Python. Parquet is well-suited for storing and aggregating tidy data, which has become widespread in data analysis in recent years.</p>
<p>In addition, packages such as <a href="https://wcjochem.github.io/sfarrow/">sfarrow</a>, which can save data from the sf package (used for handling geographic data) as Parquet, have also recently appeared.</p>
<p>By using Parquet instead of CSV, data analysis is expected to become even easier.</p>


</section>

 ]]></description>
  <category>parquet</category>
  <category>arrow</category>
  <category>R</category>
  <category>Python</category>
  <guid>https://uchidamizuki.quarto.pub/blog/en/posts/2022/06/use-parquet-instead-of-csv.html</guid>
  <pubDate>Sat, 11 Jun 2022 15:00:00 GMT</pubDate>
</item>
</channel>
</rss>
