<?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>Datamares &amp; Dreamscapes</title>
<link>https://datamares.netlify.app/</link>
<atom:link href="https://datamares.netlify.app/index.xml" rel="self" type="application/rss+xml"/>
<description>Data science, statistics and programming blog by Alberson Miranda.</description>
<generator>quarto-1.8.27</generator>
<lastBuildDate>Fri, 27 Feb 2026 03:00:00 GMT</lastBuildDate>
<item>
  <title>fio 1.0.0: Multi-regional Analysis and More</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202602_fio-1-0-0/</link>
  <description><![CDATA[ 






<p>We are thrilled to announce that <strong>{fio} 1.0.0</strong> is now available on <a href="https://CRAN.R-project.org/package=fio">CRAN</a>! This release marks a significant milestone in the package’s development, introducing powerful multi-regional capabilities, support for closed models, and a more streamlined data management strategy.</p>
<p>{fio} (<em>Friendly Input-Output</em>) was built with a clear goal: to provide R users with a fast and intuitive interface for input-output analysis. By leveraging <a href="https://www.rust-lang.org/">Rust’s</a> performance and <a href="https://r6.r-lib.org/">R6</a> classes, {fio} allows you to handle complex linear algebra computations with ease and speed.</p>
<p>You can install it from CRAN with:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fio"</span>)</span></code></pre></div></div>
<section id="multi-regional-input-output-miom" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="multi-regional-input-output-miom"><span class="header-section-number">1</span> Multi-regional Input-Output (MIOM)</h2>
<p>The headline feature of version 1.0.0 is the introduction of <strong>Multi-regional Input-Output (MIOM)</strong> models. This allows analysts to go beyond single economies and study the interdependencies between different nations or regions.</p>
<p>The new <code>miom()</code> class creates a multi-regional model that inherits all the performance and functionality of the single-region <code>iom()</code> class, adding specialized tools for regional analysis:</p>
<ul>
<li><strong>Bilateral Trade</strong>: Easily extract trade flows between specific regions.</li>
<li><strong>Regional Multipliers</strong>: Compute intra-regional, inter-regional, and spillover multipliers.</li>
<li><strong>Interdependence Measures</strong>: Quantify how much regions rely on each other.</li>
</ul>
<p>Here is a quick look at how you can analyze global trade interdependencies using the <code>world_2000</code> dataset (now part of the <a href="https://cran.r-project.org/web/packages/fiodata/index.html">{fiodata}</a> package):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(fio)</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the multi-regional world dataset</span></span>
<span id="cb2-4">miom_world <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fiodata<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>world_2000</span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get bilateral trade from Brazil to China</span></span>
<span id="cb2-7">miom_world<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;">get_bilateral_trade</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BRA"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CHN"</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;">5</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;">2</span>]</span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                                                BRA_Agriculture, Hunting, Forestry and Fishing</span></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; CHN_Agriculture, Hunting, Forestry and Fishing                                    0.868486382</span></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; CHN_Mining and Quarrying                                                          0.677175460</span></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; CHN_Food, Beverages and Tobacco                                                   0.784219678</span></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; CHN_Textiles, leather and footwear                                                0.137408834</span></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; CHN_Pulp, paper, printing and publishing                                          0.003052195</span></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                                                BRA_Mining and Quarrying</span></span>
<span id="cb2-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; CHN_Agriculture, Hunting, Forestry and Fishing              0.001110208</span></span>
<span id="cb2-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; CHN_Mining and Quarrying                                    1.703217354</span></span>
<span id="cb2-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; CHN_Food, Beverages and Tobacco                             0.005707213</span></span>
<span id="cb2-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; CHN_Textiles, leather and footwear                          0.129627291</span></span>
<span id="cb2-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; CHN_Pulp, paper, printing and publishing                    0.015560710</span></span>
<span id="cb2-20"></span>
<span id="cb2-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute regional interdependence</span></span>
<span id="cb2-22">miom_world<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;">get_regional_interdependence</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span>
<span id="cb2-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   country self_reliance total_spillover_out total_spillover_in</span></span>
<span id="cb2-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1     AUS      1.968515           0.3168527        0.006511634</span></span>
<span id="cb2-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2     AUT      1.614535           0.4900724        0.004048698</span></span>
<span id="cb2-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3     BEL      1.649908           0.7652207        0.011148228</span></span>
<span id="cb2-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4     BRA      1.918948           0.2328115        0.004065824</span></span>
<span id="cb2-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5     CAN      1.650380           0.4280919        0.007634451</span></span>
<span id="cb2-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6     CHN      2.342241           0.2867934        0.016735715</span></span>
<span id="cb2-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   interdependence_index</span></span>
<span id="cb2-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1             0.1609602</span></span>
<span id="cb2-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2             0.3035377</span></span>
<span id="cb2-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3             0.4637960</span></span>
<span id="cb2-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4             0.1213225</span></span>
<span id="cb2-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5             0.2593898</span></span>
<span id="cb2-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6             0.1224440</span></span></code></pre></div></div>
</div>
<p>For a complete overview on <code>miom</code> class and its methods, check out the vignette: <a href="https://albersonmiranda.github.io/fio/articles/multiregional_analysis.html">Multi-regional Input-Output Analysis</a>.</p>
</section>
<section id="closing-the-model" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="closing-the-model"><span class="header-section-number">2</span> Closing the Model</h2>
<p>Input-output models are traditionally “open,” treating households and government as external to the production system. Version 1.0.0 introduces the <code>close_model()</code> method, which allows you to “internalize” these sectors.</p>
<p>By moving households or the government from final demand into the intermediate transactions matrix, you can better capture the “induced” effects of economic shocks—for instance, how an increase in production leads to higher wages, which in turn fuels more consumption.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">iom_br <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fiodata<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>br_2020</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Close the model for household consumption</span></span>
<span id="cb3-4">iom_br<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;">close_model</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"household"</span>)</span>
<span id="cb3-5"></span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Technical coefficients and multipliers now include the induced effects</span></span>
<span id="cb3-7">iom_br<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;">compute_tech_coeff</span>()</span>
<span id="cb3-8">iom_br<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;">compute_leontief_inverse</span>()</span>
<span id="cb3-9">iom_br<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;">compute_multiplier_output</span>()</span></code></pre></div></div>
</div>
</section>
<section id="direct-data-access" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="direct-data-access"><span class="header-section-number">3</span> Direct Data Access</h2>
<p>To make it even easier to start your analysis, we’ve added <code>download_wiod()</code>. This function allows you to download World Input-Output Database (WIOD) tables directly from their official source and load them into R. Combined with {fio}’s processing speed, this significantly reduces the time from data collection to insight.</p>
<p>For a complete example on working with WIOD data, check out the vignette: <a href="https://albersonmiranda.github.io/fio/articles/wiod_2014_analysis.html">Working with WIOD</a>.</p>
</section>
<section id="the-fiodata-split" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="the-fiodata-split"><span class="header-section-number">4</span> The {fiodata} Split</h2>
<p>As the package grew, the built-in datasets reached a size that exceeded CRAN’s recommendations. To keep {fio} lightweight and fast to install, we have moved the datasets (<code>br_2020</code> and <code>world_2000</code>) to a separate companion package: <a href="https://cran.r-project.org/web/packages/fiodata/index.html"><strong>{fiodata}</strong></a>.</p>
<p>When you install {fio}, you might want to install {fiodata} as well to follow along with our examples:</p>
<div class="code-copy-outer-scaffold"><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;"># Install from CRAN</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fiodata"</span>)</span></code></pre></div></div>
</section>
<section id="acknowledgements" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="acknowledgements"><span class="header-section-number">5</span> Acknowledgements</h2>
<p>This release would not have been possible without the robust ecosystem of R and Rust packages. Special thanks to the developers of the <code>extendr</code> and <code>faer</code> projects, which provide the foundation for {fio}’s hybrid architecture.</p>
<p>For a full list of changes, please check the <a href="https://github.com/albersonmiranda/fio/blob/main/NEWS.md">NEWS.md</a> file. Happy analyzing!</p>


</section>

 ]]></description>
  <category>R</category>
  <category>Economics</category>
  <category>fio</category>
  <category>Rust</category>
  <guid>https://datamares.netlify.app/posts/202602_fio-1-0-0/</guid>
  <pubDate>Fri, 27 Feb 2026 03:00:00 GMT</pubDate>
  <media:content url="https://albersonmiranda.github.io/fio/reference/figures/leontief.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Why Are Economists Always Wrong?</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202506_why-economists-wrong/</link>
  <description><![CDATA[ 






<p>In this post, I offer my reading of the first chapter of <span class="citation" data-cites="feijo">Barbieri and Feijó (2013)</span> concerning what they called the maximizing equilibrium paradigm, henceforth referred to simply as the paradigm, in economic science.</p>
<section id="economics-as-an-exact-science" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> ECONOMICS AS AN EXACT SCIENCE</h1>
<p>Still in their introduction, the authors argue about the position of economic science as an exact or human science:</p>
<blockquote class="blockquote">
<p>The first question that arises in the mind of the layman is whether economics is an exact science or a science in the field of the so-called humanities (human sciences). If we had to choose between one framework or another, we would say that economics belongs to the field of human sciences. However, it occupies a special position among the human sciences: a part of the scientific contributions of academic economists uses the method of exact sciences. Thus, it can be safely stated that there is an exact field within the scope of investigations made by economists. Not all studies offered by economists, however, appear as an effort in exact science. But an important part of them, indeed, the most expressive, employs the rigorous mathematical methods typical of the exact natural sciences that have physics as their central model.</p>
</blockquote>
<p>These exact methods would have social norms as their object, reinforced by penal laws, which “encompass all types of regular and standardized behavior, equal for all individuals within a group.” However, the authors themselves recognize the limited scope of economic science in the study of these social norms:</p>
<blockquote class="blockquote">
<p>For the economist, a few types of institutions, seen as stylized types, are sufficient, such as private property (which induces agents to respect property rights and contracts) and the optimizing agent (who maximizes utility or profit), or else communism (which coerces them to accept collective property) and the cooperating agent. A good part of the theoretical work of the economist refers to a few types of social institutions. For the paradigm of theoretical economics, the optimizing agent is of interest.</p>
</blockquote>
<p>I question, firstly, whether there is coherence in trying to specify an exact field within a human science as the authors did. I do not mean by this that there are no laws in the realm of humanities—at least in the horizon of human experience in which we find ourselves—they not only exist but are studied by philosophers and represented in art<sup>1</sup> throughout the ages. Of the greatest of them, Seneca already treated in his letters to his father-in-law Paulinus:</p>
<blockquote class="blockquote">
<p>Most mortals, Paulinus, complain about the malice of Nature, because they are born with the prospect of a short existence and because the years given to them pass quickly and swiftly. So that, with the exception of a few, for the others, it is precisely in the splendor of life that it abandon them. <span class="citation" data-cites="seneca">(Sêneca 2008)</span></p>
</blockquote>
<p>The individual’s time is restricted to their always insufficient years; their place in the universe, confined to a single rock amidst the infinitude of the cosmos; bound to the ground by gravity; captive to the surface by the air itself; hostage to the need to consume. However, this consumption I mention does not refer to the idea of an exchange economy that some theorists posit as a natural and spontaneous order independent of social space, historicity, and the specificities of each society. I refer solely to the food that every animal needs to survive. This, indeed, is the consumption that is natural law. The adoption of maximizing behavior as a natural law represents one of several generalizations and extrapolations of this nature that are at the center of the critique of the paradigm, not mathematization itself.</p>
<p>When the authors state that the object of exact methods in economics is “all types of regular and standardized behavior, equal for all individuals within a group,” they fail to see that there are very few of these regular and standardized behaviors. The regular behavior is: <em>man seeks food to survive</em>. However, <em>man knows the equilibrium price of food and will demand more of it if it is below this price</em> is not a natural law. If the mechanism of convergence to macroeconomic equilibrium is entirely anchored in the microfoundation of standardized rational behavior, the macroeconomics of the paradigm already becomes fragile, and any development and conclusion obtained from it, no matter how complex and mathematically advanced, will be biased.</p>
<p>It must be emphasized that the paradigm is not alone in these extrapolations. For example, Keynes standardizes human behavior with the <em>animal spirit</em>; Hayek assumes the invariability of the human spirit and the universality, in space and time, of the laws governing the economy in any society. <span class="citation" data-cites="feijo">Barbieri and Feijó (2013)</span> follow the same path by defending the existence of an objective reality that is separate from the subject investigating it:</p>
<blockquote class="blockquote">
<p>There is something beyond a purely mental exercise involved in the work of the social scientist, for an objective reality thrives that must be rationally studied. Just as in the natural sciences, in the field of social investigation, one must also separate the knowledge formulated about facts, on the one hand, from external and objective reality itself, on other. With this, the traditional separation between the ideas elaborated about reality and this reality itself also extends to the domain of social science. The separation, usual in the physical sciences, between subject and object, partially violated only in quantum mechanics, also applies to the field of social studies.</p>
</blockquote>
<p>The existence of a single objective reality is a presupposition of the positivist approach and an instrument of the method of exact sciences. In them, the researcher adopts a position of neutrality—they are a mere passive observer. Such reality does not exist in social science. The latter admits that reality can be seen from various perspectives, and the social researcher knows that their observation and conclusions are value-laden. For example, to assume the neutrality of Hayek’s thought, who wrote <em>The Road to Serfdom</em> in 1944 under the influence of Nazi-fascist totalitarianism on one side, and the Soviets on the other; or that of Marx, who wrote <em>Das Kapital</em> in the era of panoptic factory-prisons, is concerning.</p>
<p>The guise of exact science undoubtedly provides a creative and elegant disguise for economic science. However, to the extent that this view requires the adoption of unsustainable hypotheses, it is not surprising that, after centuries of development of economic theory, crises still take economists by surprise.</p>
<p>It was so in 2008. The year after the crash, Paul Krugman wrote in his New York Times article:</p>
<blockquote class="blockquote">
<p>As I see it, the economics profession went astray because economists, as a group, mistook beauty, clad in impressive-looking mathematics, for truth. […] the central cause of the profession’s failure was the desire for an all-encompassing, intellectually elegant approach that also gave economists a chance to show off their mathematical prowess. Unfortunately, this romanticized and sanitized vision of the economy led most economists to ignore all the things that can go wrong. They turned a blind eye to the limitations of human rationality that often lead to bubbles and busts; to the problems of institutions that run amok; to the imperfections of markets — especially financial markets — that can cause the economy’s operating system to undergo sudden, unpredictable crashes; and to the dangers created when regulators don’t believe in regulation. <span class="citation" data-cites="krugman">(Krugman 2009)</span></p>
</blockquote>
<p>This illusion is so ingrained in the paradigm that economists, both in academia and in the market, thought they had all the solutions<sup>2</sup>. Obviously, they did not. Now, suppose my argument up to this point is completely wrong and there is, in fact, an exact core in economic science. Then we have that the paradigm’s theory does not represent it, since, as Krugman points out, it has not been useful for making predictions. Naturally, I do not limit my critique to an instrumentalist position: the paradigm’s theory also fails to explain social phenomena. Take growth theory, for example. For decades, models were developed—we have Solow’s seminal formulation in 1956, still univariate; dynamic optimization with Ramsey-Cass-Koopmans in the 1960s; the attempt to explain it through human capital with Lucas in the 1980s; externalities and then technological innovation with Romer. Despite all the elegance and notation worthy of exact science academic theses, none of these works were able to explain the most basic questions about economic growth, notably the problems known as <em>catching up</em>, <em>falling behind</em>, and <em>forging ahead</em>. Therefore, the paradigm fails both from an instrumentalist and a conventionalist perspective.</p>
<p>I believe Krugman’s critique is very pertinent and answers the initial question well: if economics is a social science, it cannot be investigated from a supposed objective reality, nor deduced from initial conditions and universal laws. In this sense, <span class="citation" data-cites="herscovici">Herscovici (2002)</span> highlights the limits of Popperian epistemology in economic analysis:</p>
<blockquote class="blockquote">
<p>In most cases, <em>to make a law universal, it is necessary to empty it of its historical content</em>. The <em>formalism</em> that results from this operation is characterized by the fact that, if a scientific law wants to explain all possible systems, it is unable to explain any real system, in its historical and social specificities.</p>
</blockquote>
<p>These criticisms are not foreign to the authors. Still in the introduction of the work, they try to shield themselves:</p>
<blockquote class="blockquote">
<p>It seems naive to imagine that the economic agent is always a subject endowed with full rationality, perfect information, who acts in complete markets (with contingent commodities offered for all possible states of nature), who has clarity of their objectives and who always seeks the best result for themselves in the short term, without worrying about the remote consequences of their choices and without caring about others. Indeed, the economic paradigm has been criticized for the naivety of its behavioral assumptions in its analysis. But, in many cases, the label of naivety better characterizes the situation of those who make this type of criticism than the models of theoretical economists themselves, because the hypotheses listed above, in truth, are just simplifications from introductory textbooks. The frontier of theoretical knowledge in economics has long learned to deal with much bolder and more realistic behavioral hypotheses than these. Thus, current theoretical economists know perfectly well how to model agents with limited rationality, imperfect information, incomplete markets, undecided agents with multiple objectives, and who take into account the well-being of other people.</p>
</blockquote>
<p>Well, one moment they say that a few institutions are enough for economists, among them the optimizing agent; another moment they say that such an agent has already been overcome. Worse, stating that “current theoretical economists know how to ” indecision seems doubly incoherent to me. First, because, by definition, there is no such thing as a “perfect model.” Second, indecision, uncertainty, is stochastic by hypothesis. If one <em>assumes</em> uncertainty, one does not <em>perfectly model it</em>. But they go further:</p>
<blockquote class="blockquote">
<p>One can also criticize the assumptions of the economic paradigm for considering only the model of perfect competition. Another irrelevant criticism, because, varied (sic) market structures, from perfect competition to monopoly, began to be studied as early as the 19th century, and from the late 1920s theoretical knowledge deepened with the consideration of intermediate structures such as monopolistic competition and oligopoly. The assumptions of perfect competition were questioned and rethought by the influence of the criticism of economists of the so-called Austrian school of economics, but not only by it, and replaced by the view of the economic agent with active behavior in market exploration and discovery of opportunities. The basic model of monopoly was questioned and re-examined, in a dynamic context of the economy, by the renowned Joseph Schumpeter.</p>
</blockquote>
<p>Ignoring that Schumpeter was a critic of the paradigm’s methodological individualism, I draw attention to the issue of falsifiability in the field of exact sciences. The authors, while proposing a positivist view of economic science, invalidate it scientifically within Popperian philosophy itself by shielding the paradigm from the principle of falsifiability. According to the authors, one cannot question the adoption of the competitive hypothesis, despite it still being found in manuals and academic production; The problematic known as the Cambridge Controversy which, among other things, criticizes the use of production functions is also unfounded because economics today works with other mathematics — but all microeconomics manuals, from undergraduate to postgraduate, are built on Lagrangian optimization in production functions. How can the law be timeless and change so much? How can it be universal and be only at the frontier of knowledge? Was perfect competition a universal law before and now it is not? Will today’s frontier of knowledge be a universal law or will it have the same fate as the previous ones? By changing the demarcation of hypotheses to defend the theory, the paradigm becomes non-falsifiable and, therefore, ceases to be a scientific theory and becomes dogma.</p>
<p>Very clearly and somewhat contradictorily, the authors place the core of the maximizing equilibrium paradigm, the target of criticism from other schools of thought, as an incontestable truth:</p>
<blockquote class="blockquote">
<p>Traditional methodologists are greatly concerned with the empirical confrontation of theory and that the real world can effectively function as the ultimate judge in the validation and eventual discrediting of economic theories. The economic paradigm adheres to a method that indeed gives great importance to empirical evaluation. However, in this regard, two important observations are due: (1) not everything produced by the paradigm should be empirically tested; (2) not all theories are created to be tested. The economic paradigm can, therefore, be conceived as formed by a core containing only contributions of pure mathematics and a peripheral belt of theories that produce empirically testable conclusions.</p>
</blockquote>
<p>Not being a subtle contradiction, they try to evade them shortly after, confusing, once again, economics—as a phenomenon that cannot be dissociated from its social specificities—with pure mathematical abstraction:</p>
<blockquote class="blockquote">
<p>Radical Popperian methodologists would revolt at the idea of economic theories that never confront empirical evidence. But, in fact, in the mathematical core of the economic paradigm, mathematical economists (or economic mathematicians) work as if they were in the environment of the pure mathematician. No methodologist, however extreme, would consider mathematics innocuous for not confronting empirical facts. Pure mathematical economics should also not be required to build some bridge connecting abstract models with some procedure of verification in observed facts.</p>
</blockquote>
<p>The authors frequently compare the economic paradigm with the quantum-relativistic paradigm of modern physics. However, a superficial examination is enough to note the differences: has what is given as theory been abandoned? On the contrary, the body of knowledge is widely known and studied. It is not hidden only “at the frontier of knowledge,” as the authors claim the elements that absolve economic theory from criticism are. The results found by Einstein at the beginning of the 20th century are still used to find bodies in the cosmos, from planets to black holes, and with each experiment they are put to the test and remain capable of explanation and prediction. Falsifiability does not work with absolute truths. Scientific knowledge is momentary and not permanent. A theory can only be qualified as scientific and significant as long as it can be tested and disproven.</p>
<p>But the authors do not allow its contestation, and the entrenchment continues with <em>ad hominem</em> arguments: if an economist is critical of economics as an exact science, then they do not understand mathematics:</p>
<blockquote class="blockquote">
<p>Let’s look at the problem of misunderstanding mathematics. Critics of the use of mathematics in economics, in general, are not mathematical economists. Many of them did not have professional training in mathematics. They ventured into other areas. This does not mean they were not good students of mathematics. Marshall, for example, a critic of mathematics in economics, whose use, for him, should not be abusive, proved to be, before concentrating on economics, an excellent mathematician. Even Carl Menger, the father of the Austrian school, totally critical of the use of mathematics in economics, reportedly, was a good student of mathematics in high school. Keynes was a critic of mathematics, yet, a good mathematician, even without having the mind and experience of a professional mathematician. In fact, he graduated, with distinction, in this discipline.</p>
</blockquote>
<p>Which is obviously an absurd argument. Imagine criticizing Foucault by saying: “critics of the police state, in general, are not police officers. Many of them did not have professional training in militarism.” And they continue invalidating criticisms of the paradigm:</p>
<blockquote class="blockquote">
<p>The contrary view to mathematics of these classic exponents of scientific economics is perfectly understandable. Especially because they were bound by the mathematical development of their time. The problem is when, nowadays, the members of the schools bequeathed by them cling to the same criticisms of the use of mathematics, sustaining them in a crude and outdated view of mathematics. They criticize, for example, that economic variables do not obey functional relationships. But who said that current mathematical economics uses functions? Economists of the paradigm often work with correspondences, maps, and other mathematical techniques much more powerful than functions. They criticize the use of mathematics in economics based on the argument that economic variables do not describe a well-behaved trajectory; so that they cannot be accompanied by continuous and smooth curves, i.e., differentiable at all points. Who said that the current paradigm has to launch the hypothesis of differentiability and that it only uses differential and integral calculus? In fact, current economists of the paradigm work with convex analysis, which dispenses with the differentiability of functions, taking only the much weaker hypothesis of convexity. The existence of continuous curves is no longer imposed. They work with much more general and flexible descriptions, such as upper semicontinuity, etc. The use of calculus in mathematical economics has long been replaced by convex analysis, topology, and differential topology.</p>
</blockquote>
<p>Well then, let’s take the preface of an advanced mathematical economics work:</p>
<blockquote class="blockquote">
<p>In recent years, the usual optimisation techniques, which have proved so useful in microeconomic theory, have been extended to incorporate more powerful topological and differential methods, and these methods have led to new insights into the qualitative behaviour of general economic systems. These developments have necessarily resulted in an increase in the degree of formalism in the publications in the academic economic theory journals; a formalism which can often deter graduate students. My hope is that the progression of ideas presented here will familiarise the student with the geometric concepts underlying these topological methods, and, as a result, make modern mathematical economics and <strong>general equilibrium theory</strong> more accessible. <span class="citation" data-cites="schofield">(Schofield 2018)</span></p>
</blockquote>
<p>It is not a question of mathematical limitation or an absurd hierarchy of mathematics—as if topology were <em>better</em> than calculus, or something of the sort—but a question of propositions. It does not matter whether the theory studies consumer and firm optimization or Walrasian general equilibrium through topology or calculus, but it is the equilibrium itself, the maximizing behavior itself that is under criticism, and not the mathematics used to demonstrate it.</p>
</section>
<section id="the-programmed-economist" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> THE PROGRAMMED ECONOMIST</h1>
<p>So, why do theorists of the economic science paradigm continue to cling to such problematic concepts? In <em>Sistemas de Ensino e Sistemas de Pensamento</em>, Pierre Bourdieu posits the educational system as one of the most effective instruments for the moral and logical integration of society, which produces the “programmed” individual—homogeneous in perception, thought, and action:</p>
<blockquote class="blockquote">
<p>If one admits that culture and, in this particular case, erudite culture in its quality as a common code is what allows all holders of this code to associate the same meaning with the same works and, reciprocally, to express the same significant intention through the same words, the same behaviors, and the same works, one can understand why in terms of participation in a common sense understood as a condition for communication. <span class="citation" data-cites="bourdieu">(Bourdieu 2015)</span></p>
</blockquote>
<p>Perhaps, a similar logic has taken over scientific production. Apparently oblivious to the problem, the authors seem to celebrate homogeneous thought in economic science:</p>
<blockquote class="blockquote">
<p>Without explaining it for now, it is sufficient to say that the Kuhnian paradigm of economics is what undergraduate students are obliged to learn in theoretical disciplines. Every macroeconomics student studies textbooks written by authors such as Blanchard, Boyes, Dornbusch, […]. Then it is observed, in Brazil and in the “free world,” that students are subjected to the same training, because, despite differences in approach, style, and depth, all these didactic manuals of economic theory resemble each other in many aspects. In all of them, the institutions of private property, contract, maximizing agent, etc., thrive as presuppositions of theoretical models. All address economic problems with algebra and graphs, and develop equilibrium models. This homogeneous training of students is characteristic of the scientific practice that Kuhn calls normal science. Typical in sciences with consolidated paradigms. The economic paradigm subjects students to the same basic theoretical training. It is clear that similar training leads professionals trained in this environment to act in a standardized way in their work with their theories and in their empirical testing. <span class="citation" data-cites="feijo">(Barbieri and Feijó 2013)</span></p>
</blockquote>
<p>If we take poetic license to extend Bourdieu’s theory of fields to the scientific community, we can imagine academic production located in the subfield of erudite production and the editors of <em>journals</em> in the field of power. In order to obtain greater symbolic capital, producers must publish in the most prestigious periodicals. Consequently, production submits to the standardized thought desired by the field of power. Now, the data that allow us to reason in this way are provided by the authors themselves:</p>
<blockquote class="blockquote">
<p>[…] in one of the most prestigious economics journals (if not the most prestigious in the world), more than half (54%) of the articles were about mathematical models without any data, almost a quarter of them (24.6%) were about empirical analysis, using statistical inference on published data or based on simulation and artificial experiments. Only 21.4% of the articles in the <em>American Economic Review</em> were dedicated to other topics, of which only 11.6% concerned models without mathematics and without data. Consider the 31 most prestigious international economics journals, which received an A1 classification from CAPES, among the international tops. Twenty of them focus heavily on articles with mathematical theories and eventual econometric tests; five are dedicated to publishing essays in applied economics and two are focused on econometrics (<em>Journal of Applied Econometrics, Journal of Econometrics</em>). Three others are distributed among history of economic thought (<em>History of Political Economy</em>), studies in methodology of economics (<em>Journal of Economic Methodology</em>), and reviews (<em>Journal of Economic Literature</em>). Only a single A1 journal publishes topics from economic schools not inserted in the paradigm (<em>Journal of Post Keynesian Economics</em>). So, of the 31 economics journals classified as A1, there is only one dedicated to a school of economic thought outside the paradigm of economic science, which prioritizes works in the line of so-called post-Keynesianism. It is also considered that studies outside this paradigm, including also Marxian, Austrian, institutional economics, etc., can be accepted for publication in at least three more A1 journals: <em>Cambridge Journal of Economics, Journal of Economic Methodology, and History of Political Economy</em>.</p>
</blockquote>
<p>All this points to Bourdieu’s thesis that “molded minds are predisposed to maintain with their peers a relationship of complicity and immediate communication.” It is the common way of approaching common problems. They close themselves off in their common symbols, signs, language, and thought; they close themselves off to approaches and criticisms from those who do not use their common repertoire. And it is through this mechanism that economists continue to be caught off guard by crises that insist on finding them unprepared.</p>



</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-feijo" class="csl-entry">
Barbieri, Fabio, and Ricardo Luis Chaves Feijó. 2013. <em>Metodologia Do Pensamento Econômico</em>. Editora Atlas.
</div>
<div id="ref-bourdieu" class="csl-entry">
Bourdieu, Pierre. 2015. <span>“<span class="nocase">Sistemas de Ensino e Sistemas de Pensamento</span>.”</span> In <em>A Economia Das Trocas Simbólicas</em>, 8th ed. São Paulo: Perspectiva.
</div>
<div id="ref-herscovici" class="csl-entry">
Herscovici, Alain Pierre Claude Henri. 2002. <em>Dinâmica Macroeconômica: Uma Interpretação a Partir de Marx e de Keynes</em>. São Paulo; Vitória: EDUC; EDUFES.
</div>
<div id="ref-krugman" class="csl-entry">
Krugman, Paul. 2009. <span>“<span>How Did Economists Get It So Wrong</span>.”</span> <a href="https://www.nytimes.com/2009/09/06/magazine/06Economic-t.html" class="uri">https://www.nytimes.com/2009/09/06/magazine/06Economic-t.html</a>.
</div>
<div id="ref-schofield" class="csl-entry">
Schofield, Norman. 2018. <em>Mathematical Methods in Economics</em>. Londres: Routledge.
</div>
<div id="ref-seneca" class="csl-entry">
Sêneca, Lúcio Anneo. 2008. <em>Sobre a Brevidade Da Vida</em>. L&amp;PM.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Impossible to forget Macbeth’s classic monologue: “And all our yesterdays have lighted fools The way to dusty death.”↩︎</p></li>
<li id="fn2"><p>``in a 2008 paper titled ´The State of Macro’ (that is, macroeconomics, the study of big-picture issues like recessions), Olivier Blanchard of M.I.T., now the chief economist at the International Monetary Fund, declared that ´the state of macro is good’. The battles of yesteryear, he said, were over, and there had been a ´broad convergence of vision’. And in the real world, economists believed they had things under control: the ´central problem of depression-prevention has been solved’, declared Robert Lucas of the University of Chicago in his 2003 presidential address to the American Economic Association” <span class="citation" data-cites="krugman">(Krugman 2009)</span>.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>economics</category>
  <guid>https://datamares.netlify.app/posts/202506_why-economists-wrong/</guid>
  <pubDate>Sun, 15 Jun 2025 03:00:00 GMT</pubDate>
</item>
<item>
  <title>Creating a Linux Personal Package Archive (PPA)</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202506_ppa/</link>
  <description><![CDATA[ 






<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>TL;DR
</div>
</div>
<div class="callout-body-container callout-body">
<p>A new <a href="https://github.com/albersonmiranda/r_tools_ppa">PPA</a> is now available for Ubuntu and Fedora users, simplifying the installation of the latest RStudio, Quarto, and Positron versions with a single command. I maintain this PPA and update it daily with the newest releases of these tools.</p>
</div>
</div>
<p>As a millennial, I, like many, navigated the early internet by pirating software—from Windows XP to Adobe CS5, nothing was safe from my teenage rebellion. However, as I matured, I grew tired of proprietary software and decided to explore open-source alternatives. My journey began with Ubuntu, and soon after, I discovered Fedora, which I enjoyed even more. What particularly captivated me about Linux was the power of its package management system: the ability to install, update, and remove almost any software with a single command—and keep it updated! While this system is incredibly convenient, not all software is available in official repositories. This is where Personal Package Archives (PPAs) really shine. PPAs allow developers to distribute their software directly, providing access to newer versions or tools not found in the standard channels. In this post, I’m sharing my project which you can use to create your own PPA to distribute your software.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>What is a PPA?
</div>
</div>
<div class="callout-body-container callout-body">
<p>A Personal Package Archive (PPA) is a software repository that allows developers to distribute their software to Ubuntu users. That’s a Debian based nomenclature, but the concept is similar in other Linux distributions and I’ll call it PPA throughout this post.</p>
</div>
</div>
<section id="easy-installation-for-rstudio-quarto-and-positron" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="easy-installation-for-rstudio-quarto-and-positron"><span class="header-section-number">1</span> Easy installation for RStudio, Quarto and Positron</h2>
<p>Right now, there are no official repositories for Rstudio, Quarto or Positron for Linux. This means that users have to download the binaries from their respective websites and install them manually. But I wanted the same experience of macOS users, who can count on Positron software distribution via Homebrew. There are some unofficial PPAs available for Rstudio, but I couldn’t find any for Quarto or Positron, So I’ve decided to make my own and share with you!</p>
<p>What <a href="https://github.com/albersonmiranda/r_tools_ppa">this project</a> do is scraping the urls for the latest versions of Rstudio, Quarto and Positron from their respective websites or Github releases, download the files, upload them to <a href="https://sourceforge.net/projects/r-tools-ppa/files/">Sourceforge</a> and finally create a PPA that can be used to install these tools on Ubuntu, Fedora and other Linux distributions.</p>
<p><a href="https://github.com/albersonmiranda/r_tools_ppa/tree/main/scripts">This two scripts</a> holds the logic to create the PPA:</p>
<ol type="1">
<li>Define the latest versions of RStudio, Quarto and Positron.</li>
<li>Download the binaries from their respective websites or Github releases.</li>
<li>Upload the binaries to Sourceforge.</li>
<li>Generate the metadata files required for the PPA.</li>
<li>Create the release files for the PPA.</li>
<li>Generate checksums for the files.s</li>
</ol>
</section>
<section id="support" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="support"><span class="header-section-number">2</span> Support</h2>
<ul>
<li>.deb packages for Ubuntu and Debian-based distributions.
<ul>
<li>Rstudio (amd64)</li>
<li>Quarto (amd64 and arm64)</li>
<li>Positron (amd64 and arm64)</li>
</ul></li>
<li>.rpm packages for Fedora and Red Hat-based distributions.
<ul>
<li>Rstudio (x86_64)</li>
<li>Positron (x86_64 and aarch64)</li>
<li>Quarto (x86_64)</li>
</ul></li>
</ul>
</section>
<section id="debianubuntu-installation" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="debianubuntu-installation"><span class="header-section-number">3</span> Debian/Ubuntu Installation</h2>
<p>To enable this repository and install the latest RStudio, Quarto, or Positron:</p>
<ol type="1">
<li><p>Add the repository (amd64):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deb [trusted=yes] https://downloads.sourceforge.net/project/r-tools-ppa/deb_amd64 stable main"</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sudo</span> tee /etc/apt/sources.list.d/r_tools_ppa.list</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sudo</span> apt update</span></code></pre></div></div>
<p>Or for arm64:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deb [trusted=yes] https://downloads.sourceforge.net/project/r-tools-ppa/deb_arm64 stable main"</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sudo</span> tee /etc/apt/sources.list.d/r_tools_ppa.list</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sudo</span> apt update</span></code></pre></div></div></li>
<li><p>Install a package (e.g., RStudio):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sudo</span> apt install rstudio</span></code></pre></div></div></li>
</ol>
</section>
<section id="fedorared-hat-installation" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="fedorared-hat-installation"><span class="header-section-number">4</span> Fedora/Red Hat Installation</h2>
<p>To enable this repository and install the latest RStudio, Quarto, or Positron:</p>
<ol type="1">
<li><p>Download the repository file:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sudo</span> curl <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-L</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> /etc/yum.repos.d/r_tools_ppa.repo <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb4-2">  https://sourceforge.net/projects/r-tools-ppa/files/rpm_x86_64/repo.txt/download</span></code></pre></div></div>
<p>Or for aarch64 architecture:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sudo</span> curl <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> /etc/yum.repos.d/r_tools_ppa.repo <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb5-2">  https://sourceforge.net/projects/r-tools-ppa/files/rpm_aarch64/repo.txt/download</span></code></pre></div></div></li>
<li><p>Update &amp; install (e.g., RStudio):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sudo</span> dnf update</span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sudo</span> dnf install rstudio</span></code></pre></div></div></li>
</ol>


</section>

 ]]></description>
  <category>tools</category>
  <guid>https://datamares.netlify.app/posts/202506_ppa/</guid>
  <pubDate>Mon, 09 Jun 2025 03:00:00 GMT</pubDate>
</item>
<item>
  <title>All models are wrong, but some are useless</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202504_all-models-are-wrong/</link>
  <description><![CDATA[ 






<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>TL;DR
</div>
</div>
<div class="callout-body-container callout-body">
<p>When you do use the Pearson correlation coefficient, always test for significance. But don’t use it to analyze time series. It’s a common mistake. It violates the independence of observations and ignores the relationship between lags. Instead, use cross-correlogram analysis to identify relationships between time series, including lagged relationships.</p>
</div>
</div>
<p>“All models are wrong, but some are useful” is a recurring phrase among those who practice statistics. It originates from a statement by George Box, one of the great statisticians of the 20th century: “Since all models are wrong the scientist must be alert to what is importantly wrong.” <span class="citation" data-cites="box_science_1976">(Box 1976)</span>. “All models are wrong”, means that models are instrinsically limited and will not perfectly capture reality. In other words, a model is a simplified representation of reality, used to explain or predict a phenomenon. If it were a perfect explanation of this phenomenon, it would cease to be a model and become a law.</p>
<p>In statistics, we essentially deal with random or stochastic variables, that is, variables that have a probability distribution <span class="citation" data-cites="gujarati_econometria_2021">(Gujarati and Porter 2021)</span>. Our mission as analysts is to develop and utilize methods that tell us how to formulate functions allowing us to describe and predict the relationship between these variables, while minimizing stochastic errors.</p>
<p>Depending on the functional form and the chosen estimation method, there are a series of assumptions that must be met for any inference about the error, coefficients, predictors, and regressands to be valid. If these assumptions are ignored, there is no guarantee that the results found are an optimal approximation of the function one aims to estimate. Not only that, but the violation of some of these assumptions can generate misleading results, showing significant statistical relationships where none should exist, underestimating or overestimating the object of study.</p>
<p>In this post, I address some frequent methodological errors that cause some models to be useless.</p>
<section id="lets-get-some-data" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> LET’S GET SOME DATA</h1>
<p>Have you ever heard about <code>presidents</code> dataset? From the built-in {datasets}, it stores US presidents quarterly approval ratings from 1945 to 1974. Does it hold any relationship with consumer price index (CPI), real GDP, unemployment rate or interest rates? Let’s find out!</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># convert to dataframe</span></span>
<span id="cb1-2">presi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> zoo<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;">as.Date</span>(zoo<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;">as.yearqtr</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">time</span>(presidents))),</span>
<span id="cb1-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ratings =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.vector</span>(presidents)</span>
<span id="cb1-5">) <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;">na.omit</span>()</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># get CPI and funds rates from FRED</span></span>
<span id="cb1-9">quantmod<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;">getSymbols</span>(</span>
<span id="cb1-10">  <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;">"CPIAUCSL"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FEDFUNDS"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GDPC1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UNRATE"</span>),</span>
<span id="cb1-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">src =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FRED"</span></span>
<span id="cb1-12">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "CPIAUCSL" "FEDFUNDS" "GDPC1"    "UNRATE"  </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># convert to time series</span></span>
<span id="cb3-2">series <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;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(CPIAUCSL, FEDFUNDS, GDPC1, UNRATE), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(serie) {</span>
<span id="cb3-3"></span>
<span id="cb3-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># serie name</span></span>
<span id="cb3-5">  serie_name <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;">names</span>(serie)</span>
<span id="cb3-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># convert to dataframe</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb3-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> zoo<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;">as.Date</span>(zoo<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;">as.yearmon</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">time</span>(serie))),</span>
<span id="cb3-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.vector</span>(serie)</span>
<span id="cb3-10">  ) <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;">setNames</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;">"date"</span>, serie_name))</span>
<span id="cb3-12">})</span>
<span id="cb3-13"></span>
<span id="cb3-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># merge datasets</span></span>
<span id="cb3-15">presi_merged <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;">Reduce</span>(</span>
<span id="cb3-16">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, y) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(x, y, <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;">"date"</span>),</span>
<span id="cb3-17">  <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;">list</span>(presi), series)</span>
<span id="cb3-18">)</span>
<span id="cb3-19"></span>
<span id="cb3-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualize</span></span>
<span id="cb3-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kbl</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(presi_merged), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">booktabs =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_styling</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">latex_options =</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;">"striped"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"scale_down"</span>))</span></code></pre></div></div>
<div id="tbl-dados" class="cell quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-dados-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: Dataset
</figcaption>
<div aria-describedby="tbl-dados-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="table do-not-create-environment cell caption-top table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">date</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">ratings</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">CPIAUCSL</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">FEDFUNDS</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">GDPC1</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">UNRATE</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">1954-07-01</td>
<td style="text-align: right;">71</td>
<td style="text-align: right;">26.86</td>
<td style="text-align: right;">0.80</td>
<td style="text-align: right;">2880.48</td>
<td style="text-align: right;">5.8</td>
</tr>
<tr class="even">
<td style="text-align: left;">1954-10-01</td>
<td style="text-align: right;">57</td>
<td style="text-align: right;">26.72</td>
<td style="text-align: right;">0.85</td>
<td style="text-align: right;">2936.85</td>
<td style="text-align: right;">5.7</td>
</tr>
<tr class="odd">
<td style="text-align: left;">1955-01-01</td>
<td style="text-align: right;">71</td>
<td style="text-align: right;">26.77</td>
<td style="text-align: right;">1.39</td>
<td style="text-align: right;">3020.75</td>
<td style="text-align: right;">4.9</td>
</tr>
<tr class="even">
<td style="text-align: left;">1955-04-01</td>
<td style="text-align: right;">68</td>
<td style="text-align: right;">26.79</td>
<td style="text-align: right;">1.43</td>
<td style="text-align: right;">3069.91</td>
<td style="text-align: right;">4.7</td>
</tr>
<tr class="odd">
<td style="text-align: left;">1955-07-01</td>
<td style="text-align: right;">79</td>
<td style="text-align: right;">26.76</td>
<td style="text-align: right;">1.68</td>
<td style="text-align: right;">3111.38</td>
<td style="text-align: right;">4.0</td>
</tr>
<tr class="even">
<td style="text-align: left;">1955-10-01</td>
<td style="text-align: right;">73</td>
<td style="text-align: right;">26.82</td>
<td style="text-align: right;">2.24</td>
<td style="text-align: right;">3130.07</td>
<td style="text-align: right;">4.3</td>
</tr>
</tbody>
</table>
</div>
</div>
</figure>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot</span></span>
<span id="cb4-2">presi_merged <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-3">  tidyr<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;">pivot_longer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>date) <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;">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> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-5">  <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="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_labels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-series" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-series-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://datamares.netlify.app/posts/202504_all-models-are-wrong/index_files/figure-html/fig-series-1.png" class="img-fluid figure-img" style="width:100.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-series-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Time series in levels.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="sec-erro1" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> ERROR #1: NOT TESTING SIGNIFICANCE</h1>
<p>The first, and often only, resource for variable selection and model specification among those not initiated in time series analysis is the Pearson correlation coefficient <img src="https://latex.codecogs.com/png.latex?r">. If we try to use it, we’ll’ have the following results (Table&nbsp;2):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pearson correlation</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(presi_merged, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">select =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>date)) <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;">kbl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">booktabs =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span 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;">kable_styling</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">latex_options =</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;">"striped"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"scale_down"</span>))</span></code></pre></div></div>
<div id="tbl-pearson" class="cell quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-pearson-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;2: Pearson’s <img src="https://latex.codecogs.com/png.latex?r">.
</figcaption>
<div aria-describedby="tbl-pearson-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="table do-not-create-environment cell caption-top table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">ratings</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">CPIAUCSL</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">FEDFUNDS</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">GDPC1</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">UNRATE</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">ratings</td>
<td style="text-align: right;">1.00</td>
<td style="text-align: right;">-0.70</td>
<td style="text-align: right;">-0.64</td>
<td style="text-align: right;">-0.65</td>
<td style="text-align: right;">0.10</td>
</tr>
<tr class="even">
<td style="text-align: left;">CPIAUCSL</td>
<td style="text-align: right;">-0.70</td>
<td style="text-align: right;">1.00</td>
<td style="text-align: right;">0.86</td>
<td style="text-align: right;">0.93</td>
<td style="text-align: right;">-0.02</td>
</tr>
<tr class="odd">
<td style="text-align: left;">FEDFUNDS</td>
<td style="text-align: right;">-0.64</td>
<td style="text-align: right;">0.86</td>
<td style="text-align: right;">1.00</td>
<td style="text-align: right;">0.84</td>
<td style="text-align: right;">-0.35</td>
</tr>
<tr class="even">
<td style="text-align: left;">GDPC1</td>
<td style="text-align: right;">-0.65</td>
<td style="text-align: right;">0.93</td>
<td style="text-align: right;">0.84</td>
<td style="text-align: right;">1.00</td>
<td style="text-align: right;">-0.24</td>
</tr>
<tr class="odd">
<td style="text-align: left;">UNRATE</td>
<td style="text-align: right;">0.10</td>
<td style="text-align: right;">-0.02</td>
<td style="text-align: right;">-0.35</td>
<td style="text-align: right;">-0.24</td>
<td style="text-align: right;">1.00</td>
</tr>
</tbody>
</table>
</div>
</div>
</figure>
</div>
</div>
<p>According to the rule of thumb for interpreting the effect size of the Pearson correlation coefficient<sup>1</sup>, CPI, interest rates and GDP have all large correlation with presidential approval ratings, while unemployment rate has small correlation.</p>
<p>Even if it were appropriate to use the Pearson correlation coefficient to select variables, a test must be performed to verify if the found coefficient is statistically significant. In this case, we can verify that the 10% correlation between presidential approval and unemployment rate is not significant. Ignoring significance tests results in erroneous interpretation, leading the analyst to find relationships where, in fact, none exist.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># p-values for t-test</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(presi_merged, <span class="at" style="color: #657422;
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>(date, ratings)), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(serie) {</span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor.test</span>(presi_merged<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ratings, serie)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value</span>
<span id="cb6-4">}) <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;">kbl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">booktabs =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span 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;">kable_styling</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">latex_options =</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;">"striped"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"scale_down"</span>))</span></code></pre></div></div>
<div id="tbl-pearson-t" class="cell quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-pearson-t-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;3: P-values for <img src="https://latex.codecogs.com/png.latex?r">
</figcaption>
<div aria-describedby="tbl-pearson-t-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="table do-not-create-environment cell caption-top table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">x</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">CPIAUCSL</td>
<td style="text-align: right;">0.00</td>
</tr>
<tr class="even">
<td style="text-align: left;">FEDFUNDS</td>
<td style="text-align: right;">0.00</td>
</tr>
<tr class="odd">
<td style="text-align: left;">GDPC1</td>
<td style="text-align: right;">0.00</td>
</tr>
<tr class="even">
<td style="text-align: left;">UNRATE</td>
<td style="text-align: right;">0.38</td>
</tr>
</tbody>
</table>
</div>
</div>
</figure>
</div>
</div>
<p>When it comes to inferential statistics, making any statement requires performing hypothesis tests. Except in extreme cases that prevent the estimation of some coefficient, in most cases, it is possible to obtain some statistical measure, such as in regressions, analysis of variance, correlations, etc. However, obtaining a number does not mean it is valid or can be used.</p>
<p>The correlation coefficient calculated from a sample (<img src="https://latex.codecogs.com/png.latex?r">) is a point estimate of a population parameter (<img src="https://latex.codecogs.com/png.latex?%5Crho">). Like any sample-based estimate, it has sampling variability and, therefore, must be accompanied by a confidence interval (CI) that reflects the uncertainty of this estimate and provides a range of plausible values for the true population parameter. This value <img src="https://latex.codecogs.com/png.latex?r"> is a central point (mean) around which the confidence interval for the true value is constructed.</p>
<p>If, due to sampling variability, <img src="https://latex.codecogs.com/png.latex?0%20%5Cin%20CI"> (e.g., <img src="https://latex.codecogs.com/png.latex?r=0.05"> with a confidence interval of <img src="https://latex.codecogs.com/png.latex?%5B-0.01,0.11%5D">), then there is no statistical evidence to claim that the mean value of 0.05 is different from zero.</p>
</section>
<section id="error-2-using-the-pearson-correlation-coefficient" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> ERROR #2: USING THE PEARSON CORRELATION COEFFICIENT</h1>
<p>In section Section&nbsp;2, I said “even if it were appropriate,” because it is not. The Pearson correlation is inadequate for assessing the correlation between two time series because it violates fundamental assumptions of the Pearson coefficient.</p>
<p>First, it violates the independence of observations. One of the assumptions of <img src="https://latex.codecogs.com/png.latex?r"> is the independence of observations within each variable and between variables. Time series, by nature, usually exhibit autocorrelation, that is, an observation <img src="https://latex.codecogs.com/png.latex?y_t"> is correlated with <img src="https://latex.codecogs.com/png.latex?y_%7Bt-1%7D,%20y_%7Bt-2%7D,%20%5Cldots,%20y_%7Bt-n%7D">.</p>
<p>Second, it ignores the relationship between lags. The correlation between two time series <img src="https://latex.codecogs.com/png.latex?y_t"> and <img src="https://latex.codecogs.com/png.latex?z_t"> may not be contemporaneous. One series may influence the other with some delay (e.g., <img src="https://latex.codecogs.com/png.latex?y_t"> correlated with <img src="https://latex.codecogs.com/png.latex?z_%7Bt-3%7D"> but not with <img src="https://latex.codecogs.com/png.latex?z_t">). The Pearson correlation only measures the linear relationship at the same point in time, ignoring these lagged relationships.</p>
<p>There are some valid ways to identify the relationship between two time series. One of the simplest is the cross-correlogram analysis. It allows you to identify not only the contemporaneous relationship but also the relationship at each lag of the time series.</p>
<p>Now let’s look at the cross-correlogram between the time series of unemployment rate and presidential approval ratings:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ccf</span>(</span>
<span id="cb7-2">  presi_merged[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UNRATE"</span>]],</span>
<span id="cb7-3">  presi_merged[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ratings"</span>]],</span>
<span id="cb7-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span>
<span id="cb7-5">)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-ccf" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-ccf-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://datamares.netlify.app/posts/202504_all-models-are-wrong/index_files/figure-html/fig-ccf-1.png" class="img-fluid figure-img" style="width:100.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-ccf-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Cross-correlogram between unemployment rate and presidential approval ratings.
</figcaption>
</figure>
</div>
</div>
</div>
<p><em>If it were</em> appropriate to analyze the two time series directly, we would see that, although there is no significant contemporaneous correlation — as we see by the correlation within the confidence interval at <em>lag</em> 0 —, they appear from <img src="https://latex.codecogs.com/png.latex?t-2">, which would mean that unemployment rate from 2 months ago impacts the current presidential approval ratings.</p>



</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-box_science_1976" class="csl-entry">
Box, George Edard Pelham. 1976. <span>“Science and Statistics.”</span> <em>Journal of the American Statistical Association</em> 71 (356): 791–99.
</div>
<div id="ref-cambridge_university_faqeffectsize_2021" class="csl-entry">
Cambridge University. 2021. <span>“<span>FAQ</span>/<span class="nocase">effectSize</span> - <span>CBU</span> Statistics Wiki. <span>MRC</span> Cognition and Brain Sciences Unit.”</span> November 30, 2021. <a href="https://imaging.mrc-cbu.cam.ac.uk/statswiki/FAQ/effectSize">https://imaging.mrc-cbu.cam.ac.uk/statswiki/FAQ/effectSize</a>.
</div>
<div id="ref-gujarati_econometria_2021" class="csl-entry">
Gujarati, Damodar N., and Dawn C. Porter. 2021. <em>Econometria básica</em>. 5th ed. Porto alegre: Amgh.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>A summary can be found in <span class="citation" data-cites="cambridge_university_faqeffectsize_2021">Cambridge University (2021)</span>.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>time-series</category>
  <guid>https://datamares.netlify.app/posts/202504_all-models-are-wrong/</guid>
  <pubDate>Sat, 19 Apr 2025 03:00:00 GMT</pubDate>
</item>
<item>
  <title>Hierarchical Time Series: Theory</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202210_series-hierarquicas/</link>
  <description><![CDATA[ 






<p>For almost a decade now, every year around this time I work with time series forecasting at <a href="https://www.banestes.com.br/">Banestes</a>. When it comes to setting goals and budget objectives (balances, revenues, expenses, etc.), knowing how to model and forecast is a real lifesaver.</p>
<p>Of course, forecasts alone should not be adopted as final or absolute truth, since they do not use managerial information and stakeholder expectations, for example. But they are a reasonable starting point that serves as a basis for building expectations, action plans, etc.</p>
<section id="hierarchical-and-grouped-time-series" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Hierarchical and grouped time series</h1>
<p>Hierarchical time series are those that can be naturally aggregated or disaggregated in a nested structure <span class="citation" data-cites="hyndman2021">(R. J. Hyndman and Athanasopoulos 2021)</span>. To illustrate, take the Brazilian GDP series. It can be disaggregated by state, which in turn can be disaggregated by municipality.</p>
<div class="cell">
<div class="cell-output-display">
<div id="fig-h" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-h-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://datamares.netlify.app/posts/202210_series-hierarquicas/img/hierarq.png" class="img-fluid figure-img" width="600">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-h-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Hierarchical series.
</figcaption>
</figure>
</div>
</div>
</div>
<p>This structure can be represented by equations for any level of aggregation.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0Ay_t%20&amp;=%20y_%7BA,t%7D%20+%20y_%7BB,t%7D%20+%20y_%7BC,t%7D%20%5C%5C%0Ay_t%20&amp;=%20y_%7BAA,t%7D%20+%20y_%7BAB,t%7D%20+%20y_%7BAC,t%7D%20+%20y_%7BBA,t%7D%20+%20y_%7BBC,t%7D%20+%20y_%7BCA,t%7D%20%5C%5C%0Ay_%7BA,t%7D%20&amp;=%20y_%7BAA,t%7D%20+%20y_%7BAB,t%7D%20+%20y_%7BAC,t%7D%0A%5Cend%7Balign%7D%0A"></p>
<p>Thus, the national aggregate can be represented only by the state aggregates, as in (1), or as the aggregate of municipalities (2). The aggregate for the state of Espírito Santo is represented by (3).</p>
<p>Alternatively, we can describe the complete structure in matrix form:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20y_%7Bt%7D%20%5C%5C%0A%20%20%20%20y_%7BA,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BB,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BC,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BAA,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BAB,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BAC,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BBA,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BBB,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BBC,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BCA,%20t%7D%0A%5Cend%7Bbmatrix%7D%0A=%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20%5C%5C%0A%20%20%20%201%20&amp;%201%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20%5C%5C%0A%20%20%20%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20%5C%5C%0A%5Cend%7Bbmatrix%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20y_%7BAA,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BAB,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BAC,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BBA,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BBB,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BBC,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BCA,%20t%7D%0A%5Cend%7Bbmatrix%7D%0A"></p>
<p>On the other hand, GDP can also be disaggregated in a cross-sectional way according to economic activity—crops, livestock, manufacturing, extractive industry, capital goods, intermediate goods, clothing trade, automotive, services, etc. This structure cannot be naturally disaggregated in a single way, as in the hierarchy of states and municipalities. It cannot be nested by a single attribute like geography. This type of structure is called grouped series.</p>
<div class="cell">
<div class="cell-output-display">
<div id="fig-agrupadas" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-agrupadas-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://datamares.netlify.app/posts/202210_series-hierarquicas/img/agrupadas.png" class="img-fluid figure-img" width="600">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-agrupadas-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Grouped series.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Combining both, we have the structure of hierarchical grouped series. Unlike the hierarchical structure, which can only be aggregated in one way—as with municipalities under states—the addition of the grouped structure can occur either above (Figure&nbsp;3) or below (Figure&nbsp;4) the hierarchy.</p>
<div class="cell">
<div class="cell-output-display">
<div id="fig-ha1" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-ha1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://datamares.netlify.app/posts/202210_series-hierarquicas/img/hier_agrup.png" class="img-fluid figure-img" width="600">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-ha1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Hierarchical and grouped time series (a).
</figcaption>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="cell-output-display">
<div id="fig-ha2" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-ha2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://datamares.netlify.app/posts/202210_series-hierarquicas/img/hier_agrup_2.png" class="img-fluid figure-img" width="600">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-ha2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Hierarchical and grouped time series (b).
</figcaption>
</figure>
</div>
</div>
</div>
<p>In matrix notation, the structure of Figure&nbsp;4 is represented as below. Formally, the left-hand side of the equation is composed of the <img src="https://latex.codecogs.com/png.latex?n">-dimensional vector <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7By%7D_t"> with all observations at time <img src="https://latex.codecogs.com/png.latex?t"> for all hierarchy levels. The right-hand side is composed of the summing matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BS%7D"> of dimension <img src="https://latex.codecogs.com/png.latex?n%20%5Ctimes%20m"> that defines the equations for every aggregation level, and the matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bb%7D_t"> composed of the series at the most disaggregated level.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bequation%7D%0A%5Cmathbf%7By%7D_t=%5Cmathbf%7BSb%7D_t%0A%5Cend%7Bequation%7D"></p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bequation%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20y_%7Bt%7D%20%5C%5C%0A%20%20%20%20y_%7BA,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BB,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BC,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BX,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BY,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BZ,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BAX,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BAY,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BAZ,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BBX,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BBY,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BBZ,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BCX,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BCY,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BCZ,%20t%7D%0A%5Cend%7Bbmatrix%7D%0A=%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20%5C%5C%0A%20%20%20%201%20&amp;%201%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%201%20&amp;%201%20%5C%5C%0A%20%20%20%201%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%201%20%5C%5C%0A%20%20%20%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%0A%5Cend%7Bbmatrix%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20y_%7BAX,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BAY,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BAZ,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BBX,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BBY,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BBZ,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BCX,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BCY,%20t%7D%20%5C%5C%0A%20%20%20%20y_%7BCZ,%20t%7D%0A%5Cend%7Bbmatrix%7D%0A%5Cend%7Bequation%7D"></p>
</section>
<section id="top-down-and-bottom-up-approaches" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Top-down and bottom-up approaches</h1>
<p>Perhaps the most intuitive ways to think about forecasts for these types of structures are the <em>top-down</em> and <em>bottom-up</em> approaches. Take the structure described in Figure&nbsp;1, for example. We can forecast for the horizon <img src="https://latex.codecogs.com/png.latex?h"> of the aggregate Brazilian GDP, represented at the top of the hierarchy by <em>Total</em> (6), and then distribute the forecasted values proportionally among states and municipalities.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bequation%7D%0A%5Chat%7By%7D_%7BT+h%20%7C%20T%7D%20=%20E%5By_%7BT+h%7D%20%7C%20%5COmega_T%5D%0A%5Cend%7Bequation%7D"></p>
<p>This is the <em>top-down</em> approach. In it, the forecasts for the most disaggregated levels of the hierarchy are determined by a proportion <img src="https://latex.codecogs.com/png.latex?p_i"> of the aggregate level. For example, the forecasts for Vitória are given by equation (7).</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bequation%7D%0A%5Ctilde%7By%7D_%7BAA,%20T+h%20%7C%20T%7D%20=%20p_%7B1%7D%5Chat%7By%7D_%7BT+h%20%7C%20T%7D%0A%5Cend%7Bequation%7D"></p>
<p>For this, we have to define a matrix with all these weights, which, following the formulation of <span class="citation" data-cites="hyndman2021">R. J. Hyndman and Athanasopoulos (2021)</span>, we will call <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BG%7D">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bequation%7D%0A%5Cmathbf%7BG%7D%0A=%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20p_1%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%20p_2%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%20p_3%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%20p_4%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%20p_5%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%20p_6%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%20p_7%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%0A%5Cend%7Bbmatrix%7D%0A%5Cend%7Bequation%7D"></p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BG%7D"> is an <img src="https://latex.codecogs.com/png.latex?m%20%5Ctimes%20n"> matrix that multiplies the matrix <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cmathbf%7By%7D%7D_%7BT+h%7CT%7D">, which in turn is composed of the base forecasts—the individual forecasts for all aggregation levels. The equation for the <em>top-down</em> approach will then be:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bequation%7D%0A%5Cmathbf%7B%5Ctilde%7By%7D%7D_%7BT+h%20%7C%20T%7D%20=%20%5Cmathbf%7BSG%5Chat%7By%7D%7D_%7BT+h%20%7C%20T%7D%0A%5Cend%7Bequation%7D"></p>
<p>In matrix notation for the structure of Figure&nbsp;1, we have:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bequation%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20%5Ctilde%7By%7D_%7Bt%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BA,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BB,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BC,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BAA,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BAB,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BAC,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BBA,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BBB,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BBC,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BCA,%20t%7D%0A%5Cend%7Bbmatrix%7D%0A=%0A%5Cmathbf%7BS%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20p_1%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%20p_2%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%20p_3%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%20p_4%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%20p_5%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%20p_6%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%20p_7%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%0A%5Cend%7Bbmatrix%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20%5Chat%7By%7D_%7BT+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BA,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BB,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BC,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BAA,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BAB,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BAC,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BBA,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BBB,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BBC,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BCA,%20T+h%7CT%7D%0A%5Cend%7Bbmatrix%7D%0A%5Cend%7Bequation%7D"></p>
<p>Which gives us a proportion of the total for each element at the most disaggregated level. <img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bequation%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20%5Ctilde%7By%7D_%7Bt%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BA,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BB,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BC,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BAA,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BAB,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BAC,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BBA,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BBB,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BBC,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BCA,%20t%7D%0A%5Cend%7Bbmatrix%7D%0A=%0A%5Cmathbf%7BS%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20p_1%5Chat%7By%7D_%7BT+h%7CT%7D%20%5C%5C%0A%20%20%20%20p_2%5Chat%7By%7D_%7BT+h%7CT%7D%20%5C%5C%0A%20%20%20%20p_3%5Chat%7By%7D_%7BT+h%7CT%7D%20%5C%5C%0A%20%20%20%20p_4%5Chat%7By%7D_%7BT+h%7CT%7D%20%5C%5C%0A%20%20%20%20p_5%5Chat%7By%7D_%7BT+h%7CT%7D%20%5C%5C%0A%20%20%20%20p_6%5Chat%7By%7D_%7BT+h%7CT%7D%20%5C%5C%0A%20%20%20%20p_7%5Chat%7By%7D_%7BT+h%7CT%7D%0A%5Cend%7Bbmatrix%7D%0A%5Cend%7Bequation%7D"></p>
<p>Replacing the matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BS%7D">, we have the equations that define each forecast in the structure as a function of proportions of the aggregate forecast.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bequation%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20%5Ctilde%7By%7D_%7Bt%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BA,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BB,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BC,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BAA,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BAB,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BAC,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BBA,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BBB,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BBC,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BCA,%20t%7D%0A%5Cend%7Bbmatrix%7D%0A=%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20%5C%5C%0A%20%20%20%201%20&amp;%201%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20%5C%5C%0A%20%20%20%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%0A%5Cend%7Bbmatrix%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20p_1%5Chat%7By%7D_%7BT+h%7CT%7D%20%5C%5C%0A%20%20%20%20p_2%5Chat%7By%7D_%7BT+h%7CT%7D%20%5C%5C%0A%20%20%20%20p_3%5Chat%7By%7D_%7BT+h%7CT%7D%20%5C%5C%0A%20%20%20%20p_4%5Chat%7By%7D_%7BT+h%7CT%7D%20%5C%5C%0A%20%20%20%20p_5%5Chat%7By%7D_%7BT+h%7CT%7D%20%5C%5C%0A%20%20%20%20p_6%5Chat%7By%7D_%7BT+h%7CT%7D%20%5C%5C%0A%20%20%20%20p_7%5Chat%7By%7D_%7BT+h%7CT%7D%0A%5Cend%7Bbmatrix%7D%0A%5Cend%7Bequation%7D"></p>
<p>The <em>bottom-up</em> approach, on the other hand, starts from the opposite reasoning and defines the forecasts for each element of the structure from the forecasts of the most disaggregated elements. For this, just modify the matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BG%7D">.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bequation%7D%0A%5Cmathbf%7BG%7D%0A=%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%0A%5Cend%7Bbmatrix%7D%0A%5Cend%7Bequation%7D"></p>
<p>Which results in the desired equations. Therefore, <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BG%7D"> defines the approach—whether <em>top-down</em> or <em>bottom-up</em>—and <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BS%7D"> defines how the forecasts are summed to form the forecast equations for each element of the structure.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bequation%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20%5Ctilde%7By%7D_%7Bt%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BA,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BB,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BC,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BAA,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BAB,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BAC,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BBA,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BBB,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BBC,%20t%7D%20%5C%5C%0A%20%20%20%20%5Ctilde%7By%7D_%7BCA,%20t%7D%0A%5Cend%7Bbmatrix%7D%0A=%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%201%20%5C%5C%0A%20%20%20%201%20&amp;%201%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%201%20&amp;%201%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20%5C%5C%0A%20%20%20%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%20&amp;%200%20%5C%5C%0A%20%20%20%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%200%20&amp;%201%0A%5Cend%7Bbmatrix%7D%0A%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20%5Chat%7By%7D_%7BAA,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BAB,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BAC,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BBA,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BBB,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BBC,%20T+h%7CT%7D%20%5C%5C%0A%20%20%20%20%5Chat%7By%7D_%7BCA,%20T+h%7CT%7D%0A%5Cend%7Bbmatrix%7D%0A%5Cend%7Bequation%7D"></p>
</section>
<section id="coherence-and-reconciliation" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Coherence and reconciliation</h1>
<p>Whether by summing the forecasts from the most disaggregated level to form the upper levels of the hierarchy (<em>bottom-up</em>) or proportionally distributing the forecasts from the most aggregated level (<em>top-down</em>), the vector <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7B%5Ctilde%7By%7D%7D_t"> represents the <em>coherent</em> forecasts. This means that the forecasts “match”, i.e., they are correctly totaled—the forecasts for each aggregate element correspond to the sum of the forecasts for the lower levels of the hierarchy. This is ensured by the multiplication of the matrices <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BSG%7D">.</p>
<p>Without this pre-multiplication, nothing would guarantee the coherence of the forecasts. Taking the structure of Figure&nbsp;1 as an example, it would be an unlikely coincidence for the forecasts of the aggregate for the state of Espírito Santo to be exactly the sum of the individual forecasts of its municipalities. This is because each series may follow a different process (e.g., ARIMA) with different errors and variances.</p>
<p>The methods for generating coherent forecasts from base forecasts are called <em>reconciliation</em> methods. The traditional reconciliation methods presented, <em>top-down</em> and <em>bottom-up</em>, use limited information. In the <em>top-down</em> method, only information from the most aggregated level is used—which is why only the first column in (8) is nonzero. In the <em>bottom-up</em> approach, only information from the most disaggregated levels is used, resulting in the <img src="https://latex.codecogs.com/png.latex?m%20%5Ctimes%20m"> identity submatrix in (13), while the columns representing the most aggregated levels are zero.</p>
<p>Alternatively, we can think of any matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BG%7D"> that uses all available information and has some properties that ensure that the coherent forecasts have the smallest possible error. This is the research problem addressed in <em>optimal reconciliation</em>, and it is a relatively new topic. That is, point forecasts for hierarchical time series are an old thing. At least since the 1970s, research has been published on <em>bottom-up</em> and <em>top-down</em> approaches, their advantages and disadvantages, and attempts to define the best method<sup>1</sup>. However, it is only in <span class="citation" data-cites="hyndman2011">R. J. Hyndman et al. (2011)</span> that a practical approach is formalized that uses all available information (i.e., the forecasts of all elements at all levels of the hierarchy) by estimating the matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BG%7D"> via generalized least squares (GLS) regression.</p>
<p>However, to be able to estimate the model by GLS, the variance-covariance matrix of the errors is needed. <span class="citation" data-cites="hyndman2011">R. J. Hyndman et al. (2011)</span> use the coherence error matrix, i.e., the difference between the reconciled forecasts and the base forecasts, which is rank-deficient and unidentified and therefore cannot be estimated. The authors circumvent this problem by adopting, instead of the variance-covariance matrix of the errors, a constant diagonal matrix, i.e., they assume constant variance of the reconciliation errors, which ends up falling into the estimation of <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BG%7D"> by ordinary least squares (OLS).</p>
<p>Estimation by this method results in an optimal reconciliation that depends only on the matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BS%7D">, i.e., the hierarchical structure, and is independent of the variance and covariance of the base forecasts <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7B%5Chat%7By%7D_%7BT+h%7D%7D">—which is not a satisfactory conclusion.</p>
<p><span class="citation" data-cites="hyndman2016">Rob J. Hyndman, Lee, and Wang (2016)</span> attempt to improve the method by using the estimated variances of the base forecasts (within-sample) as an estimate for the variance-covariance matrix of the reconciliation errors, so as to use them as weights and perform optimal reconciliation by weighted least squares (WLS). Thus, more accurate base forecasts have greater weight than noisier ones. However, they do not provide a theoretical justification for using the diagonal of the variance-covariance matrix of <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7B%5Chat%7Be%7D_%7Bt%7D%7D">.</p>
<p><span class="citation" data-cites="wickramasuriya2019">Wickramasuriya, Athanasopoulos, and Hyndman (2019)</span> argue that what really matters is that the reconciled forecasts have the smallest error—it does not matter that the reconciled forecast is closer to the base forecast, but that the reconciled forecasts are as accurate as possible, since that is what will be used! So, they correct the optimal reconciliation approach to the objective of minimizing the errors of the reconciled forecasts <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7B%5Ctilde%7Be%7D_%7Bt+h%7D%7D%20=%20%5Cmathbf%7By_%7Bt+h%7D%20-%20%5Cmathbf%7B%5Ctilde%7By%7D_%7Bt+h%7D%7D%7D">, instead of the reconciliation errors of the base forecasts <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7B%5Chat%7By%7D_%7Bt+h%7D%7D%20-%20%5Cmathbf%7B%5Ctilde%7By%7D_%7Bt+h%7D%7D">. Since this implies minimizing the variance of <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7B%5Ctilde%7Be%7D_%7Bt+h%7D%7D">, i.e., minimizing the sum of the diagonal, the trace, of the variance-covariance matrix of <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7B%5Ctilde%7Be%7D_%7Bt+h%7D%7D">, they called this method Minimum Trace (MinT). In parallel, they use the triangle inequality to show that the reconciled forecasts obtained by this method are at least as good as the base forecasts.</p>
<p>And, from 2021 onwards, there have been interesting developments in the probabilistic extension of this theoretical body (and this is where I am conducting my master’s research). <span class="citation" data-cites="panagiotelis2021">Panagiotelis et al. (2021)</span> reinterpret the literature on coherence and reconciliation of point forecasts from a geometric approach, providing alternative proofs for previous conclusions while also providing new theorems. In addition, <span class="citation" data-cites="panagiotelis2021">Panagiotelis et al. (2021)</span> extend this geometric interpretation to the probabilistic context, providing parametric and non-parametric (via bootstrapping) methods for reconciliation of probabilistic forecasts, i.e., to reconcile forecasts <img src="https://latex.codecogs.com/png.latex?%5Chat%7By%7D_t"> obtained from the entire distribution, not just the mean.</p>
</section>
<section id="anyway" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Anyway…</h1>
<p>If you made it this far, I recommend you find something more interesting to do with your life. In the next post (maybe) I’ll show how to do all this in practice with our beloved #RStats.</p>



</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-athanasopoulos2009" class="csl-entry">
Athanasopoulos, George, Roman A. Ahmed, and Rob J. Hyndman. 2009. <span>“Hierarchical Forecasts for Australian Domestic Tourism.”</span> <em>International Journal of Forecasting</em> 25 (1): 146–66. https://doi.org/<a href="https://doi.org/10.1016/j.ijforecast.2008.07.004">https://doi.org/10.1016/j.ijforecast.2008.07.004</a>.
</div>
<div id="ref-hyndman2011" class="csl-entry">
Hyndman, R. J., R. A. Ahmed, G. Athanasopoulos, and H. L. Shang. 2011. <span>“Optimal Combination Forecasts for Hierarquical Time Series.”</span> <em>Computational Statistics and Data Analysis</em> 55: 2579–89.
</div>
<div id="ref-hyndman2021" class="csl-entry">
Hyndman, R. J., and G. Athanasopoulos. 2021. <em>Forecasting: Principles and Practice</em>. 3rd ed. Melbourne, Australia: OTexts. <a href="https://otexts.com/fpp3/">https://otexts.com/fpp3/</a>.
</div>
<div id="ref-hyndman2016" class="csl-entry">
Hyndman, Rob J., Alan J. Lee, and Earo Wang. 2016. <span>“Fast Computation of Reconciled Forecasts for Hierarchical and Grouped Time Series.”</span> <em>Computational Statistics and Data Analysis</em> 97: 16–32. https://doi.org/<a href="https://doi.org/10.1016/j.csda.2015.11.007">https://doi.org/10.1016/j.csda.2015.11.007</a>.
</div>
<div id="ref-panagiotelis2021" class="csl-entry">
Panagiotelis, Anastasios, George Athanasopoulos, Puwasala Gamakumara, and Rob J. Hyndman. 2021. <span>“Forecast Reconciliation: A Geometric View with New Insights on Bias Correction.”</span> <em>International Journal of Forecasting</em> 37 (1): 343–59. https://doi.org/<a href="https://doi.org/10.1016/j.ijforecast.2020.06.004">https://doi.org/10.1016/j.ijforecast.2020.06.004</a>.
</div>
<div id="ref-wickramasuriya2019" class="csl-entry">
Wickramasuriya, Shanika L., George Athanasopoulos, and Rob J. Hyndman. 2019. <span>“Optimal Forecast Reconciliation for Hierarchical and Grouped Time Series Through Trace Minimization.”</span> <em>Journal of the American Statistical Association</em> 114 (526): 804–19. <a href="https://doi.org/10.1080/01621459.2018.1448825">https://doi.org/10.1080/01621459.2018.1448825</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>A review of this literature can be found in <span class="citation" data-cites="athanasopoulos2009">Athanasopoulos, Ahmed, and Hyndman (2009)</span>.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>statistics</category>
  <guid>https://datamares.netlify.app/posts/202210_series-hierarquicas/</guid>
  <pubDate>Tue, 25 Oct 2022 03:00:00 GMT</pubDate>
  <media:content url="https://datamares.netlify.app/posts/202210_series-hierarquicas/img/hier_agrup.png" medium="image" type="image/png" height="50" width="144"/>
</item>
<item>
  <title>The Role Of Mathematics Education Under The Hegemony Of Capital</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202208_educacao-pre-capitalista/</link>
  <description><![CDATA[ 






<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was taken from a chapter of my <a href="https://repositorio.ifes.edu.br/handle/123456789/6060">undergraduate thesis in Mathematics</a>, defended in 2024.</p>
</div>
</div>
<p>In a capitalist society, education cannot be understood apart from labor relations. In addition to the techniques applied to education—including pedagogy in its philosophical and sociological dimensions—the prevailing norms and shared values of a given society are also reflected in and shape educational actions.</p>
<p>In this chapter, I seek to systematize the mechanisms of capitalism’s reproduction in education, highlighting how mathematics education, as long as it does not break with its role in reproducing social power relations, is ineffective in promoting structural changes in society.</p>
<p>Consider the following thought experiment: Suppose a society in a very distant future where work as a social relation no longer exists. Robots perform all labor activities. What would school look like in this society? What remains <em>pure</em> in education? What would its purpose be? Before trying to infer anything about such questions, we can equip ourselves with a brief overview of the purpose of education in the history of education.</p>
<p>According to <span class="citation" data-cites="cubberley_history_1920">Cubberley (1920)</span>, in Athens in the 5th century BC, before the time of the sophists, the basic curriculum consisted of reading, writing, music, and gymnastics, and was required to obtain the status of citizen. Only those holding this status were allowed to participate in the <em>ekklesia</em>, the main assembly of Athenian democracy. Education, which was exclusive to men, was private and fees depended on the parents’ ability to pay. Only teachers from large schools had any prestige, with the rest occupying low positions in the Athenian social hierarchy. Grammar, arithmetic, sciences, or foreign languages were not part of the curriculum—only what was necessary for the <em>moral normalization</em> of the Athenian individual: music, literature, their own religion, physical training, and instructions about the tasks and obligations of a citizen.</p>
<p>According to the author, Homer’s fables filled with heroism, the Iliad and the Odyssey, were the first and greatest readings of the Greeks, so that “<em>To appeal to the emotions and to stir the will along moral and civic lines was a fundamental purpose of the instruction</em>”. These works included lessons in ethics, politics, social life, and, of course, what was expected of a soldier. All the desirable elements for the moral integration of the future citizen: severe but simple and honest, hardworking, obedient to the laws, who rejects comfort and vice. The very portrait of Perseus reflected in every Greek boy.</p>
<p>Crossing the Ionian Sea to Rome, its first schools, around 300 BC, were more restricted than those of Athens and aimed to instruct young people for political careers, being composed of a small and select portion who had access to education. With the rise of the empire and the fall of Greece in the 2nd century BC, the great influx of educated Greeks to Rome caused the process of Hellenization of the city, so that Roman schools were, in fact, Greek schools slightly modified to adapt to Rome. In addition to grammar, composition, ethics, history, mythology, and geography, schools of rhetoric were developed to prepare professionals for law and public life in Rome. Homer remained the favorite author in Greek, but now schools also included Latin-Roman authors such as Virgil and Horace. In the sciences, a bit of geometry and astronomy were added for their practical utility. Thus, the seven liberal arts of the Middle Ages—grammar, rhetoric, dialectic, arithmetic, geometry, music, and astronomy—were already present <span class="citation" data-cites="cubberley_history_1920">(Cubberley 1920)</span>.</p>
<p>As in Athenian society, education was private and reserved for those who could pay for it. Teachers were either pagans or indifferent to religion; and, because of this, schools were less and less attended by Christians, who were rapidly growing in the empire’s population. By the 5th century, Roman schools went into decline, disappearing in the following century <span class="citation" data-cites="williams_history_2016">(Williams 2016)</span>.</p>
<p>According to <span class="citation" data-cites="williams_history_2016">Williams (2016)</span>, the intense instability of Europe after the fall of Rome, combined with the spread of Christianity, had a great impact on education, characterizing the early Middle Ages as a period of dense ignorance, not only among the general population but also among the nobility and only “slightly mitigated” among most of the clergy. He lists as causes for the prevalence of ignorance: 1) the rejection by the early Christians of their pagan oppressors, including their literature, not only because of its origin but also because of the mythology it carried; 2) Except for the Bible, books were expensive and rare compared to the empire, since they were handwritten and copied by slaves; 3) These few and expensive books were written in Latin, being unintelligible to the vast majority of the population, since the various dialects that emerged in the region after the barbarian invasions were not developed or predominant enough during the early Middle Ages; 4) The very idea and tradition of formal education was culturally lost, losing its value as something necessary; 5) Finally, from the 9th century onwards, with the expansion of the feudal system, isolation and the dangers associated with travel during the period increased the cost of obtaining education and carrying out the social interactions necessary for intellectual development.</p>
<p>Formal education at the beginning of the Christian church was only catechumenal, and its main concern was the <em>moral regeneration of society through the moral regeneration of converts</em> <span class="citation" data-cites="williams_history_2016">(Williams 2016)</span>. Furthermore, the church’s educational effort was closed in on itself, with the aim of creating its theological base, and not aimed at the intellectual formation of the society it served:</p>
<blockquote class="blockquote">
<p>Almost everything that we today mean by civilization in that age was found within the protecting walls of monastery or church, and these institutions were at first too busy building up the foundations upon which a future culture might rest to spend much time in preserving learning, much less in advancing it @[cubberley_history_1920].</p>
</blockquote>
<p>Contrary to classical schools, early Christian education had no intellectual vocation, but its appeal was moral and emotional. In fact, the Greek and Roman models were entirely rejected—after all, pagan intellectual education was the only one available and parents did not want their children to have contact with and end up admiring the deities of Olympus. It is only in the middle of the 2nd century, with the foundation of the catechetical school of Alexandria, that members of the clergy begin to receive training based on Greek education and philosophy, systematically formalizing the Christian faith and doctrine, which increasingly received influence from Greek thought and philosophy. However, such a movement would be gradually reversed until the beginning of the 5th century, when the Council of Carthage, under the influence of Saint Augustine, definitively prohibited the reading of pagan authors by the clergy <span class="citation" data-cites="cubberley_history_1920">(Cubberley 1920)</span>.</p>
<p>In the 6th century, with the foundation of the monastery of Monte Cassino by Saint Benedict in 529 and the promulgation of the Benedictine rule in 529, monasteries became centers of education, open not only to boys willing to take vows, but also, later (9th century), to external students with no intention of taking vows. Monastic schools offered instruction in reading and writing (in Latin), music, <em>Christian doctrine and rules of conduct</em>. The copying of manuscripts and preservation of ancient books was one of the main activities of the monks, and among Christians, the preservation of classical literature was largely due to their efforts <span class="citation" data-cites="williams_history_2016">(Williams 2016)</span>.</p>
<p>Outside the Roman Catholic world, the conservation of classical literature in Europe was partly due to the efforts of Saracen Spain in the west and the Byzantines in the east. Among the Mohammedans—here expanding the view to caliphates such as Baghdad, Bukhara, and Damascus—education began with literacy and study of the Quran, <em>with teachers funded by the caliphate</em>. For wealthy families, education continued with the teaching of logic, philosophy, theology, astronomy, and medicine. Unlike the European peoples of the early Middle Ages, dependent on Latin, the Saracens had Syriac translations of Greek science and Aristotelian philosophy, including Euclid’s mathematics and Diophantus’s algebra. They were also responsible for the creation of chemistry as a science, as well as great advances in algebra <span class="citation" data-cites="williams_history_2016">(Williams 2016)</span>.</p>
<p>In the following centuries, throughout the Late Middle Ages, the medieval educational system developed by the church became more organized, based on the <em>Trivium</em> and <em>Quadrivium</em>. However, education continued to be directed inward to the church, with theology as the only profession and career to be obtained through it.</p>
<blockquote class="blockquote">
<p>All these schools, too, were completely under the control of the Church. There were no private schools or teachers before about 1200. Only the chivalric education was under the control of princes or kings, and even this the Church kept under its supervision. The Church was still the State, to a large degree, and the Church, unlike Greece or Rome, took the education of the young upon itself as one of its most important functions. The schools taught what the Church approved, and the instruction was for religious and church ends @[cubberley_history_1920].</p>
</blockquote>
<p>In this sense, medieval Christian education was, in essence, an instrument for reproducing and perpetuating the church’s own power structure. For maintaining order and social hierarchy, not for the intellectual and critical formation of individuals.</p>
<p>In 1450, the movement to rescue classical literature and Greek and Roman philosophy, known as the Renaissance, began to spread across Europe from Italy, bringing with it the rediscovery of Greek and Arab mathematics, as well as adding the study of the humanities alongside moral and physical education. This period marks the break with clerical thought and education and lays the foundations for modern education. However, in form and content, there is little difference from classical Greek and Roman education. With the support of the ruling classes, the rise of Renaissance secondary schools and universities restored the education of Cicero’s time, which brought an aristocratic education, preparing for services in the Church, the State, and big business.</p>
<blockquote class="blockquote">
<p>History was introduced in these schools for the first time and as a new subject of study, though the history was the history of Greece and Rome and was drawn from the authors studied. Livy and Plutarch were the chief historical writers used. Nothing that happened after the fall of Rome was deemed as of importance. Much emphasis was placed on manners, morality, and reverence, with Livy and Plutarch again as the great guides to conduct. <img src="https://latex.codecogs.com/png.latex?%5B...%5D"> The result was an all-round physical, mental, and moral training, vastly superior to anything previously offered by the cathedral and other church schools @[cubberley_history_1920].</p>
</blockquote>
<p>There are several other events in history that can be analyzed from the perspective of their impact on education: the Protestant Reformation<sup>1</sup>, which, among many other developments, generates a new level of religious tolerance for knowledge that eventually paves the way for the emergence of the modern scientific method <span class="citation" data-cites="cubberley_history_1920">(Cubberley 1920)</span>; the success of the Counter-Reformation and Jesuit education in the colonization processes, which “promoted the <strong>control of faith and morals</strong> of the inhabitants” <span class="citation" data-cites="rosario_educacao_2015">(Rosário and Melo 2015)</span>; Puritanism in North America, which establishes the foundations of the American educational system. However, the purpose of this work is by no means to be exhaustive, but rather to argue a point.</p>
<p>Returning to the initial question: what remains pure in education when social relations of exploitation and power are removed? This student, when beginning the reading for this chapter, hypothesized and expected to find evidence that pre-capitalist education had at its core curiosity and the human spirit, with a focus on the arts. However, history shows us that the common element that appears in all societies is the moral normalization of the individual and the reproduction of prevailing social relations, through the transmission of values and social norms and the training of the children of the ruling classes to occupy certain positions in society.</p>





<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-cubberley_history_1920" class="csl-entry">
Cubberley, Ellwood. 1920. <em>The History of Education: Educational Practice and Progress Considered as a Phase of the Development and Spread of Western Civilization</em>. 2nd ed. Boston: Houghton Mifflin.
</div>
<div id="ref-rosario_educacao_2015" class="csl-entry">
Rosário, Maria José Aviz Do, and Clarice Nascimento De Melo. 2015. <span>“A Educação Jesuítica No Brasil Colônia.”</span> <em>Revista <span>HISTEDBR</span> On-Line</em> 15 (61): 379. <a href="https://doi.org/10.20396/rho.v15i61.8640534">https://doi.org/10.20396/rho.v15i61.8640534</a>.
</div>
<div id="ref-williams_history_2016" class="csl-entry">
Williams, Samuel Gardner. 2016. <em>The History of Mediaeval Education: An Account of the Course of Educational Opinion and Practice from the Sixth to the Fifteenth Centuries, Inclusive</em>. Palala Press.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Calvin writes to the Prince of Geneva, in 1541, that “the liberal arts and good education are good aids in the full knowledge of the Word”.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>education</category>
  <guid>https://datamares.netlify.app/posts/202208_educacao-pre-capitalista/</guid>
  <pubDate>Sun, 14 Aug 2022 03:00:00 GMT</pubDate>
</item>
<item>
  <title>Template for Theses in Quarto</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202208_template/</link>
  <description><![CDATA[ 






<p>Since I’m starting to write <a href="https://github.com/albersonmiranda/dissertacao/blob/master/dissertacao.pdf">my thesis</a> this semester, why not kill two birds with one stone (sorry, Luisa Mell) and get to know this <a href="https://quarto.org/">Quarto</a> everyone is talking about? Well, <a href="https://github.com/albersonmiranda/dissertacao">here is a template</a> for monograph/thesis/dissertation in Quarto<sup>1</sup>.</p>
<p>To make life easier (or not, time will tell), I parameterized the pre-textual elements — cover, title page, approval sheet, abstract, summary, etc. Here they are:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202208_template/img/preambulo.png" class="img-fluid figure-img"></p>
<figcaption>parameters for pre-textual elements</figcaption>
</figure>
</div>
<p>To use it, first the prerequisites:</p>
<ol type="1">
<li><a href="https://quarto.org/docs/get-started/">Install Quarto</a>;</li>
<li>Install a TeX distribution (since we’re talking about PDF generation) <sup>2</sup>;</li>
<li>Since I’m obviously using VSCode, install the <a href="https://marketplace.visualstudio.com/items?itemName=quarto.quarto">Quarto extension</a>.</li>
</ol>
<p>Now you can fork the project and get started! Just update the parameters in <code>config/preamble.tex</code> and write your work in <code>dissertacao.qmd</code>:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202208_template/img/1.gif" class="img-fluid figure-img"></p>
<figcaption>updating parameters and rendering</figcaption>
</figure>
</div>




<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 template is an adaptation of the <a href="http://www.uel.br/projetos/experimental/pages/modelo-monografiauel.php">standard UEL model</a>.↩︎</p></li>
<li id="fn2"><p>For R users, the easiest way is via <code>tinytex::install_tinytex()</code>.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>tools</category>
  <guid>https://datamares.netlify.app/posts/202208_template/</guid>
  <pubDate>Wed, 03 Aug 2022 03:00:00 GMT</pubDate>
  <media:content url="https://datamares.netlify.app/posts/202208_template/img/quarto.png" medium="image" type="image/png" height="53" width="144"/>
</item>
<item>
  <title>Python from R I: package importing (and why learning new languages sucks)</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202106_python-from-r-i-package-importing/</link>
  <description><![CDATA[ 






<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>TL;DR
</div>
</div>
<div class="callout-body-container callout-body">
<p>When learning a new programming language, simply finding equivalent code for the practices you already have may be misleading. Here we’re able to see that an equivalent of R’s <code>library()</code> call is actually considered a bad practice in Python and if you do that in a job interview, you should not expect they call you back.</p>
</div>
</div>
<section id="motivation" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Motivation</h1>
<p>It crossed my mind an analogy about learning a foreign language: <em>it’s impossible to learn a new language by translating word by word</em>. It’s not only a matter of vocabulary. I mean, each language has it’s own grammar, phrasal verbs, diction, expressions, pace etc. That kind of issue also appears when learning a new <em>programming language</em> and I think importing packages is a good, yet very simple, example of that.</p>
</section>
<section id="calling-a-function-from-a-package" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Calling A Function From A Package</h1>
<section id="r-experience" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="r-experience"><span class="header-section-number">2.1</span> R Experience</h2>
<p>In R, every package installed in the library trees are listed whenever a terminal is open. Those listed packages are available for users at all times and can be called explicitly. For example:</p>
<section id="case-1-explicit-call" class="level3" data-number="2.1.1">
<h3 data-number="2.1.1" class="anchored" data-anchor-id="case-1-explicit-call"><span class="header-section-number">2.1.1</span> Case 1: Explicit Call</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># search for machine learning measures that contais "AUC" in the {mlr3} package</span></span>
<span id="cb1-2">mlr3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>mlr_measures<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;">keys</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"auc"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "classif.auc"       "classif.mauc_au1p" "classif.mauc_au1u"
[4] "classif.mauc_aunp" "classif.mauc_aunu" "classif.mauc_mu"  
[7] "classif.prauc"    </code></pre>
</div>
</div>
<p>But that way of calling functions usually take place only if that particular package won’t be required very often. Otherwise, it’s cultural for R users to load and attach the entire package’s namespace to the search path<sup>1</sup> with a <code>library()</code> call.</p>
</section>
<section id="case-2-attaching" class="level3" data-number="2.1.2">
<h3 data-number="2.1.2" class="anchored" data-anchor-id="case-2-attaching"><span class="header-section-number">2.1.2</span> Case 2: Attaching</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># tired: explicitly calling from {ggplot2}</span></span>
<span id="cb3-2">t1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> mtcars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-3">  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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hp_by_cyl =</span> hp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> cyl)</span>
<span id="cb3-4"></span>
<span id="cb3-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># wired: attaching {ggplot2} namespace</span></span>
<span id="cb3-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Anexando pacote: 'dplyr'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Os seguintes objetos são mascarados por 'package:stats':

    filter, lag</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Os seguintes objetos são mascarados por 'package:base':

    intersect, setdiff, setequal, union</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">t2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> mtcars <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hp_by_cyl =</span> hp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> cyl)</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># are they equivalent?</span></span>
<span id="cb7-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all.equal</span>(t1, t2)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div>
<p>The problem appears when there are namespace conflicts. Did you notice the warning about objects being masked from {stats} and {base}?. Usually, users just don’t care for startup warnings 😨 and that may eventually lead them to inconsistent results or tricky errors.</p>
<p>That can be avoided by attaching only the specific functions you’re actually gonna use:</p>
</section>
<section id="case-3-attaching-specific-functions" class="level3" data-number="2.1.3">
<h3 data-number="2.1.3" class="anchored" data-anchor-id="case-3-attaching-specific-functions"><span class="header-section-number">2.1.3</span> Case 3: Attaching Specific Functions</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># detaching dplyr</span></span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">detach</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"package:dplyr"</span>)</span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># attaching only mutate():</span></span>
<span id="cb9-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include.only =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mutate"</span>)</span></code></pre></div></div>
</div>
<p>And no conflict warning will be triggered. Unfortunately, I don’t hear much of <code>include.only</code> argument from R community 🤷‍♂. On the contrary, meta packages such as {tidyverse}, which will load and attach A LOT of stuff into the namespace — often unnecessary for what you’re about to do —, is quite common.</p>
</section>
</section>
<section id="python-experience" class="level2" data-number="2.2">
<h2 data-number="2.2" class="anchored" data-anchor-id="python-experience"><span class="header-section-number">2.2</span> Python Experience</h2>
<p>All of the 3 cases stated before are possible in Python, but the community standards are very different. Specially regarding to the awareness of what is loaded into the namespace — or symbol table, as it is called in Python<sup>2</sup>.</p>
<p>Firstly, installed packages aren’t immediately available. So if I try, for example, listing {pandas} functions/methods/attributes it’ll result in an error:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># inspecting modules in {pandas}</span></span>
<span id="cb10-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas</span>
<span id="cb10-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dir</span>(pandas)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>['ArrowDtype', 'BooleanDtype', 'Categorical', 'CategoricalDtype', 'CategoricalIndex', 'DataFrame', 'DateOffset', 'DatetimeIndex', 'DatetimeTZDtype', 'ExcelFile', 'ExcelWriter', 'Flags', 'Float32Dtype', 'Float64Dtype', 'Grouper', 'HDFStore', 'Index', 'IndexSlice', 'Int16Dtype', 'Int32Dtype', 'Int64Dtype', 'Int8Dtype', 'Interval', 'IntervalDtype', 'IntervalIndex', 'MultiIndex', 'NA', 'NaT', 'NamedAgg', 'Period', 'PeriodDtype', 'PeriodIndex', 'RangeIndex', 'Series', 'SparseDtype', 'StringDtype', 'Timedelta', 'TimedeltaIndex', 'Timestamp', 'UInt16Dtype', 'UInt32Dtype', 'UInt64Dtype', 'UInt8Dtype', '__all__', '__builtins__', '__cached__', '__doc__', '__docformat__', '__file__', '__git_version__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_built_with_meson', '_config', '_is_numpy_dev', '_libs', '_pandas_datetime_CAPI', '_pandas_parser_CAPI', '_testing', '_typing', '_version_meson', 'annotations', 'api', 'array', 'arrays', 'bdate_range', 'col', 'compat', 'concat', 'core', 'crosstab', 'cut', 'date_range', 'describe_option', 'errors', 'eval', 'factorize', 'from_dummies', 'get_dummies', 'get_option', 'infer_freq', 'interval_range', 'io', 'isna', 'isnull', 'json_normalize', 'lreshape', 'melt', 'merge', 'merge_asof', 'merge_ordered', 'notna', 'notnull', 'offsets', 'option_context', 'options', 'pandas', 'period_range', 'pivot', 'pivot_table', 'plotting', 'qcut', 'read_clipboard', 'read_csv', 'read_excel', 'read_feather', 'read_fwf', 'read_hdf', 'read_html', 'read_iceberg', 'read_json', 'read_orc', 'read_parquet', 'read_pickle', 'read_sas', 'read_spss', 'read_sql', 'read_sql_query', 'read_sql_table', 'read_stata', 'read_table', 'read_xml', 'reset_option', 'set_eng_float_format', 'set_option', 'show_versions', 'test', 'testing', 'timedelta_range', 'to_datetime', 'to_numeric', 'to_pickle', 'to_timedelta', 'tseries', 'unique', 'util', 'wide_to_long']</code></pre>
</div>
</div>
<p>One can check the symbol table with the following statement.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># what is attached into the symbol table?</span></span>
<span id="cb12-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">globals</span>(), sep <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>__name__
__doc__
__package__
__loader__
__spec__
__builtins__
r
pandas</code></pre>
</div>
</div>
<p>Depending on what system/tools you’re using, Python interpreter will load a few modules or not. If you start a REPL — a Python interactive terminal —, no modules will be leaded. If you start a Jupyter notebook, a few modules necessary for it to run will be loaded. In this case, since I’m running Python from R via {reticulate}, some modules have been loaded:</p>
<ul>
<li><code>sys</code>: for accesses to some variables and functions used by the interpreter</li>
<li><code>os</code>: for OS routines for NT or Posix</li>
</ul>
<p>So if I want to work with {pandas}, I need to attach it to the symbol table with an equivalent to R’s <code>library()</code>. And just like it’s cousin function, Python’s <code>import</code> also comes in different flavours.</p>
<p>Firstly, <code>import pandas</code> will make the package available for explicit calls.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># import pandas</span></span>
<span id="cb14-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas</span>
<span id="cb14-3"></span>
<span id="cb14-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># what is attached into the symbol table?</span></span>
<span id="cb14-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">globals</span>(), sep <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>__name__
__doc__
__package__
__loader__
__spec__
__builtins__
r
pandas</code></pre>
</div>
</div>
<p>Note that only {pandas} is attached to the symbol table, not it’s functions/methods/attributes. So that statement it’s not an equivalent to <code>library()</code>. For us to create a simple dataframe with {pandas}:</p>
<section id="Case1py" class="level3" data-number="2.2.1">
<h3 data-number="2.2.1" class="anchored" data-anchor-id="Case1py"><span class="header-section-number">2.2.1</span> Case 1: Explicit Call</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this will result in a NameError: name 'DataFrame' is not defined</span></span>
<span id="cb16-2">DataFrame(</span>
<span id="cb16-3">  {</span>
<span id="cb16-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"capital"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vitoria"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"São Paulo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rio de Janeiro"</span>],</span>
<span id="cb16-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"state"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Espírito Santo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"São Paulo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rio de Janeiro"</span>]</span>
<span id="cb16-6">  }</span>
<span id="cb16-7">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>NameError: name 'DataFrame' is not defined</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this will work</span></span>
<span id="cb18-2">pandas.DataFrame(</span>
<span id="cb18-3">  {</span>
<span id="cb18-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"capital"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vitoria"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"São Paulo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rio de Janeiro"</span>],</span>
<span id="cb18-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"state"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Espírito Santo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"São Paulo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rio de Janeiro"</span>]</span>
<span id="cb18-6">  }</span>
<span id="cb18-7">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>          capital           state
0         Vitoria  Espírito Santo
1       São Paulo       São Paulo
2  Rio de Janeiro  Rio de Janeiro</code></pre>
</div>
</div>
<p>If we were to replicate <code>library()</code> behavior (i.e.&nbsp;load and attach the entire {pandas} functions/methods/attributes into the symbol table), then:</p>
</section>
<section id="Case2py" class="level3" data-number="2.2.2">
<h3 data-number="2.2.2" class="anchored" data-anchor-id="Case2py"><span class="header-section-number">2.2.2</span> Case 2: Attaching</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># importing entire {pandas} into symbol table</span></span>
<span id="cb20-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb20-3"></span>
<span id="cb20-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the updated symbol table</span></span>
<span id="cb20-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">globals</span>(), sep <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>__name__
__doc__
__package__
__loader__
__spec__
__builtins__
r
pandas
NA
ArrowDtype
BooleanDtype
Categorical
CategoricalDtype
CategoricalIndex
DataFrame
DateOffset
DatetimeIndex
DatetimeTZDtype
ExcelFile
ExcelWriter
Flags
Float32Dtype
Float64Dtype
Grouper
HDFStore
Index
IndexSlice
Int8Dtype
Int16Dtype
Int32Dtype
Int64Dtype
Interval
IntervalDtype
IntervalIndex
MultiIndex
NaT
NamedAgg
Period
PeriodDtype
PeriodIndex
RangeIndex
Series
SparseDtype
StringDtype
Timedelta
TimedeltaIndex
Timestamp
UInt8Dtype
UInt16Dtype
UInt32Dtype
UInt64Dtype
api
array
arrays
bdate_range
col
concat
crosstab
cut
date_range
describe_option
errors
eval
factorize
from_dummies
get_dummies
get_option
infer_freq
interval_range
io
isna
isnull
json_normalize
lreshape
melt
merge
merge_asof
merge_ordered
notna
notnull
offsets
option_context
options
period_range
pivot
pivot_table
plotting
qcut
read_clipboard
read_csv
read_excel
read_feather
read_fwf
read_hdf
read_html
read_iceberg
read_json
read_orc
read_parquet
read_pickle
read_sas
read_spss
read_sql
read_sql_query
read_sql_table
read_stata
read_table
read_xml
reset_option
set_eng_float_format
set_option
show_versions
test
testing
timedelta_range
to_datetime
to_numeric
to_pickle
to_timedelta
tseries
unique
wide_to_long</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># and now this works</span></span>
<span id="cb22-2">DataFrame(</span>
<span id="cb22-3">  {</span>
<span id="cb22-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"capital"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vitoria"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"São Paulo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rio de Janeiro"</span>],</span>
<span id="cb22-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"state"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Espírito Santo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"São Paulo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rio de Janeiro"</span>]</span>
<span id="cb22-6">  }</span>
<span id="cb22-7">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>          capital           state
0         Vitoria  Espírito Santo
1       São Paulo       São Paulo
2  Rio de Janeiro  Rio de Janeiro</code></pre>
</div>
</div>
<p>But you won’t see any experienced Python user doing that kind of thing because they’re worried about loading that amount of names into the symbol table and the possible conflicts it may cause. An acceptable approach would be attaching only a few frequent names as in:</p>
</section>
<section id="Case3py" class="level3" data-number="2.2.3">
<h3 data-number="2.2.3" class="anchored" data-anchor-id="Case3py"><span class="header-section-number">2.2.3</span> Case 3: Attaching Specific Functions</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># detaching {pandas}</span></span>
<span id="cb24-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">vars</span>(pandas):</span>
<span id="cb24-3">    <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> name.startswith(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'_'</span>):</span>
<span id="cb24-4">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">del</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">globals</span>()[name]</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>KeyError: 'annotations'</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># attaching only DataFrame()</span></span>
<span id="cb26-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> DataFrame</span>
<span id="cb26-3"></span>
<span id="cb26-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the updated symbol table</span></span>
<span id="cb26-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">globals</span>(), sep <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>__name__
__doc__
__package__
__loader__
__spec__
__builtins__
r
pandas
NA
ArrowDtype
BooleanDtype
Categorical
CategoricalDtype
CategoricalIndex
DataFrame
DateOffset
DatetimeIndex
DatetimeTZDtype
ExcelFile
ExcelWriter
Flags
Float32Dtype
Float64Dtype
Grouper
HDFStore
Index
IndexSlice
Int8Dtype
Int16Dtype
Int32Dtype
Int64Dtype
Interval
IntervalDtype
IntervalIndex
MultiIndex
NaT
NamedAgg
Period
PeriodDtype
PeriodIndex
RangeIndex
Series
SparseDtype
StringDtype
Timedelta
TimedeltaIndex
Timestamp
UInt8Dtype
UInt16Dtype
UInt32Dtype
UInt64Dtype
api
array
arrays
bdate_range
col
concat
crosstab
cut
date_range
describe_option
errors
eval
factorize
from_dummies
get_dummies
get_option
infer_freq
interval_range
io
isna
isnull
json_normalize
lreshape
melt
merge
merge_asof
merge_ordered
notna
notnull
offsets
option_context
options
period_range
pivot
pivot_table
plotting
qcut
read_clipboard
read_csv
read_excel
read_feather
read_fwf
read_hdf
read_html
read_iceberg
read_json
read_orc
read_parquet
read_pickle
read_sas
read_spss
read_sql
read_sql_query
read_sql_table
read_stata
read_table
read_xml
reset_option
set_eng_float_format
set_option
show_versions
test
testing
timedelta_range
to_datetime
to_numeric
to_pickle
to_timedelta
tseries
unique
wide_to_long
name</code></pre>
</div>
</div>
<p>According to The Hitchhiker’s Guide to Python <span class="citation" data-cites="pythonguide">[@pythonguide]</span>, case 2 is the worst possible scenario and it’s generally considered bad practice since it “makes code harder to read and makes dependencies less compartmentalized”. That claim is endorsed by Python’s official docs <span class="citation" data-cites="pythontutorial">[@pythontutorial]</span>:</p>
<blockquote class="blockquote">
<p>Although certain modules are designed to export only names that follow certain patterns when you use import *, it is still considered bad practice in production code” .</p>
</blockquote>
<p>In the opinion of the guide authors, case 3 would be a better option because it pinpoints specific names<sup>3</sup>, while case 1 would be the best practice, for “Being able to tell immediately where a class or function comes from greatly improves code readability and understandability in all but the simplest single file projects.”</p>


</section>
</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>An ordered list where R will look for a function. Can be accessed with <code>search()</code>.↩︎</p></li>
<li id="fn2"><p>I guess? I don’t know, still learning lol 😂↩︎</p></li>
<li id="fn3"><p>Python Foundation says “There is nothing wrong with using from package import specific_submodule! In fact, this is the recommended notation unless the importing module needs to use submodules with the same name from different packages.”↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>python</category>
  <guid>https://datamares.netlify.app/posts/202106_python-from-r-i-package-importing/</guid>
  <pubDate>Sat, 12 Jun 2021 03:00:00 GMT</pubDate>
  <media:content url="https://datamares.netlify.app/posts/202106_python-from-r-i-package-importing/img/r-python.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>R in 2021 with VSCode</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202101_r-vscode/</link>
  <description><![CDATA[ 






<p>I first installed VSCode in october 2020 when I’ve decided to learn Python. Looking for the ideal setup, I’ve read somewhere that Spyder would be the best IDE for R <span class="citation" data-cites="R">(R Core Team 2020)</span> users due to similarities with RStudio, but I wanted the same experience as a native Python user so I’ve decided to go with VSCode (even if that meant a tougher xp at the time).</p>
<p>I quickly fell in love with it’s flexibility and maturity. There are community extensions for about everything, which makes ux delightful! However, R ecosystem was still very much geared towards RStudio and I still found myself stuck with that IDE in my day-to-day life with R. That changed, of course, when I saw this tweet from <a href="https://fosstodon.org/@milesmcbain">Miles McBain</a>:</p>
<p>Moving on to consider VSCode as a real possibility for R, I found this <a href="https://renkun.me/2019/12/11/writing-r-in-vscode-a-fresh-start/">post</a> from <a href="https://twitter.com/renkun_ken">Kun Ren</a> that offers a setup for R with VSCode. In this post I write about my favorite features and what settings that I use.</p>
<section id="whats-so-good-about-it-tho" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> WHAT’S SO GOOD ABOUT IT THO 🤔?</h1>
<p>Most features implemented by R and Language Server extensions are detailed in <a href="https://renkun.me/2019/12/11/writing-r-in-vscode-a-fresh-start/">Kun Ren’s post</a>, so, if you didn’t read that yet, just GO! Here’s some stuff I like about it.</p>
<section id="intellisense" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="intellisense"><span class="header-section-number">1.1</span> Intellisense</h2>
<p>That’s what is called VSCode’s code editing features, like <strong>quick info</strong> (docs for functions, datasets etc) and <strong>parameter info</strong> (args definition), just by hovering over it. It also includes <strong>code completion</strong>, <strong>member list</strong> and more.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/intellisense.gif" class="img-fluid figure-img"></p>
<figcaption>code completion, hover, quick info, parameter info</figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/color.gif" class="img-fluid figure-img"></p>
<figcaption>color selection via IDE (｡◕‿◕｡)</figcaption>
</figure>
</div>
</section>
<section id="git-github-integration" class="level2" data-number="1.2">
<h2 data-number="1.2" class="anchored" data-anchor-id="git-github-integration"><span class="header-section-number">1.2</span> Git &amp; GitHub integration</h2>
<p><a href="https://marketplace.visualstudio.com/items?itemName=GitHub.vscode-pull-request-github">GitHub Pull Requests and Issues</a> and <a href="https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens">Git Lens</a> extensions provide a very useful GitHub integration, so you don’t have to leave your IDE for nothing. You can open, comment and close issues and PRs; submit, view and edit commits; execute most common bash commands via command pallet (push, pull, checkout, prune, rebase etc) and other stuff.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/issue.gif" class="img-fluid figure-img"></p>
<figcaption>new issue</figcaption>
</figure>
</div>
<p>Clicking on the issue, VSCode opens a new branch and checks it out for you to work on (and still triggers a personalized commit message!).</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/branch.gif" class="img-fluid figure-img"></p>
<figcaption>new branch via issue</figcaption>
</figure>
</div>
<p>And, at the end of the work, just click on create new PR to push the branch to origin and bring the PR interface. All without opening the browser or a terminal.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/pr.gif" class="img-fluid figure-img"></p>
<figcaption>new pull request</figcaption>
</figure>
</div>
</section>
<section id="multiple-terminals" class="level2" data-number="1.3">
<h2 data-number="1.3" class="anchored" data-anchor-id="multiple-terminals"><span class="header-section-number">1.3</span> Multiple terminals</h2>
<p>While your blog or Shiny app is rendering and consequently occupying one terminal, you can simply open another one and continue working normally!</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/terminais.gif" class="img-fluid figure-img"></p>
<figcaption>you can open as many terminals as you wish!</figcaption>
</figure>
</div>
</section>
<section id="draw.io" class="level2" data-number="1.4">
<h2 data-number="1.4" class="anchored" data-anchor-id="draw.io"><span class="header-section-number">1.4</span> Draw.io</h2>
<p>This is an example of one of the many useful extensions that the community makes available on VSCode. The <a href="https://marketplace.visualstudio.com/items?itemName=hediet.vscode-drawio">draw.io extension</a> integrates <a href="https://app.diagrams.net/">diagrams.net</a> into VSCode. With it, you can make diagrams very quickly and without having to leave your IDE!</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/draw.gif" class="img-fluid figure-img"></p>
<figcaption>to bring the extension interface, just create a .drawio file</figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/diagrama.png" class="img-fluid figure-img"></p>
<figcaption>diagram I made for <a href="https://datamares.netlify.app/post/2020-11-29-introducao-ao-mlr3-framework/">this post</a> using it</figcaption>
</figure>
</div>
</section>
<section id="live-share" class="level2" data-number="1.5">
<h2 data-number="1.5" class="anchored" data-anchor-id="live-share"><span class="header-section-number">1.5</span> Live Share</h2>
<p>Ever dreamed of working on the same script with your boys live? The <a href="https://marketplace.visualstudio.com/items?itemName=MS-vsliveshare.vsliveshare">Live Share extension</a> allows it and also provides chat and audio channels, which makes it unnecessary to open an audio call on another app while you work!</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/me_and_the_boys.png" class="img-fluid figure-img"></p>
<figcaption>pretty much everyone right now</figcaption>
</figure>
</div>
<p>Also, you don’t need to have the language interpreter installed to join the session. That means that even if you’re on another machine that doesn’t have R or Python (or else) installed, you can log in and collaborate on your colleagues’ scripts, even running the code through their terminals!</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/liveshare.gif" class="img-fluid figure-img"></p>
<figcaption>two machines visualizing each other during a VSCode Live Share session</figcaption>
</figure>
</div>
</section>
<section id="windows-subsystem-for-linux" class="level2" data-number="1.6">
<h2 data-number="1.6" class="anchored" data-anchor-id="windows-subsystem-for-linux"><span class="header-section-number">1.6</span> Windows Subsystem for Linux</h2>
<p>Are you in a Windows machine and and ever needed to debug some stuff in a Linux environment? Then you had the displeasure of installing virtual machines or dual boot (╯°□°）╯︵ ┻━┻</p>
<p>Good news: with the <a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl">Remote - WSL</a> extension you’re just one click away from happiness. It copies your folder (project) and reopens it in a Linux environment, with a terminal ready for business!</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/wsl.gif" class="img-fluid figure-img"></p>
<figcaption>starting a Linux session</figcaption>
</figure>
</div>
</section>
<section id="linter" class="level2" data-number="1.7">
<h2 data-number="1.7" class="anchored" data-anchor-id="linter"><span class="header-section-number">1.7</span> Linter</h2>
<p>R extension integrates the {lintr} package into the IDE, so you have real time styling updates.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/linter.gif" class="img-fluid figure-img"></p>
<figcaption>real time checks</figcaption>
</figure>
</div>
</section>
</section>
<section id="settings" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> SETTINGS</h1>
<p>To get started you only need VSCode and R and R-LSP extensions. But here I share the setup that I think provides the best experience:</p>
<ol type="1">
<li>Visual Studio Code and extensions:</li>
</ol>
<ul>
<li><a href="https://code.visualstudio.com/download">VSCode</a>: the IDE</li>
<li><a href="https://marketplace.visualstudio.com/items?itemName=Ikuyadeu.r">vscode-R</a>: R language support</li>
<li><del><a href="https://marketplace.visualstudio.com/items?itemName=REditorSupport.r-lsp">vscode-r-lsp</a>: R Language Server Protocol Client for VSCode</del> <sup>1</sup></li>
</ul>
<ol start="2" type="1">
<li>Packages:</li>
</ol>
<ul>
<li><a href="https://github.com/REditorSupport/languageserver">languageserver</a> <span class="citation" data-cites="lsp">(Lai 2020)</span>: R Language Server Protocol implementation</li>
<li><a href="https://github.com/nx10/httpgd">httpgd</a> <span class="citation" data-cites="httpgd">(Rupprecht 2021)</span> (recommended): asynchronous http server graphic device for R</li>
</ul>
<ol start="3" type="1">
<li>Python &amp; Radian:</li>
</ol>
<ul>
<li><a href="https://www.python.org/downloads/">Python</a> (recommended): requirement for Radian, implemented in Python</li>
<li><a href="https://github.com/randy3k/radian">Radian</a> (recommended): alternative R console, with multi line editing and rich syntax highlighting</li>
</ul>
<ol start="4" type="1">
<li><a href="https://pandoc.org/installing.html">Pandoc</a>: Universal document converter, responsible for converting R Markdown (.Rmd) files to html. You didn’t need to install it before because the RStudio installer comes with a version of Pandoc included.</li>
</ol>
</section>
<section id="first-steps" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> FIRST STEPS</h1>
<p>With VSCode installed, you’ll see the welcome screen, which looks like this:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/welcome.png" class="img-fluid figure-img"></p>
<figcaption>welcome screen</figcaption>
</figure>
</div>
<p>The first thing to note is that the RStudio project concept (.Rproj) does not exist in VSCode. You have a folder and that’s it, all relative paths point to it. Folder-specific settings can be defined in a .json file in the directory.</p>
<p>In the sidebar, the explorer tab will give you the option to open a folder (a project) or clone a repository. Opening my <strong>Advent of Code 2020</strong> folder, you will see my directories and files on the left. There is code in Python and in R. I can open it and organize it any way I want. The terminals are below and they can also be moved elsewhere.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/organizacao.gif" class="img-fluid figure-img"></p>
<figcaption>fully flexible editor</figcaption>
</figure>
</div>
<p>Extensions can be installed from the tab on the left. VSCode itself will recommend installing extensions when you open some kind of file that doesn’t have an interpreter or formatter installed, for example.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/extensoes.gif" class="img-fluid figure-img"></p>
<figcaption>community extensions</figcaption>
</figure>
</div>
<p><strong>Important: do not install the RTools extension together with the extensions mentioned above, as they are not complementary and will cause conflicts.</strong></p>
<p>With the extensions installed, let’s go to settings. First, open the command palette with ctrl+shift+p.&nbsp;There you’ll be able to access several commands and shortcuts in VSCode, such as:</p>
<ul>
<li>new terminal (R, Python etc)</li>
<li>launch <em>RStudio addins</em></li>
<li>compile RMarkdown files (knit .Rmd)</li>
<li>access settings and shortcut keys</li>
<li>install packages</li>
<li>format files and MANY others</li>
</ul>
<p>Accessing the VSCode settings, we can configure both the editor and its extensions:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202101_r-vscode/img/preferencias.gif" class="img-fluid figure-img"></p>
<figcaption>settings.json</figcaption>
</figure>
</div>
<p>Here are the ones I currently use. Change as needed and according to your preference. Obs.: by the time you’re reading this they may be changed, since R support is on active development.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode json code-with-copy"><code class="sourceCode json"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-2">    <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">VSCode</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">setup</span></span>
<span id="cb1-3">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"terminal.integrated.shell.windows"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">WINDOWS</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">System32</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">WindowsPowerShell</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">v1.0</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">powershell.exe"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-4">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"editor.formatOnPaste"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">recommended</span></span>
<span id="cb1-5">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"editor.formatOnType"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">recommended</span></span>
<span id="cb1-6">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"diffEditor.ignoreTrimWhitespace"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">false</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-7">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"files.autoSave"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"off"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-8"></span>
<span id="cb1-9">    <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Python</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">setup</span></span>
<span id="cb1-10">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"python.pythonPath"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:</span><span class="ch" 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="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">anaconda3</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">python.exe"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-11">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"jupyter.alwaysTrustNotebooks"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-12"></span>
<span id="cb1-13">    <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">R</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">setup</span></span>
<span id="cb1-14">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"[r]"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">required</span></span>
<span id="cb1-15">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"editor.defaultFormatter"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"REditorSupport.r-lsp"</span></span>
<span id="cb1-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">},</span></span>
<span id="cb1-17">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"files.associations"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">required</span></span>
<span id="cb1-18">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"*.rmd"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"markdown"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-19">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"*.Rmd"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rmd"</span></span>
<span id="cb1-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">},</span></span>
<span id="cb1-21">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"r.sessionWatcher"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">default</span></span>
<span id="cb1-22">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"r.bracketedPaste"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">required</span></span>
<span id="cb1-23">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"r.rterm.windows"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:</span><span class="ch" 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="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">anaconda3</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Scripts</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">radian.exe"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">for</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Radian</span></span>
<span id="cb1-24">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"r.rpath.windows"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:</span><span class="ch" 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="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">R</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">R-4.1.0</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">bin</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">R.exe"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">required</span></span>
<span id="cb1-25">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"r.lsp.debug"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">required</span></span>
<span id="cb1-26">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"r.alwaysUseActiveTerminal"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">required</span></span>
<span id="cb1-27">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"r.rtermSendDelay"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-28"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>And a few tweaks on your .Rprofile:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># options</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">options</span>(</span>
<span id="cb2-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># activate RStudio Addins on command pallet</span></span>
<span id="cb2-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vsc.rstudioapi =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb2-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># interactive plots with {httpgd}</span></span>
<span id="cb2-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vsc.use_httpgd =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb2-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># radian highlight scheme (choose what suits you)</span></span>
<span id="cb2-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">radian.color_scheme =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native"</span>,</span>
<span id="cb2-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># code completion triggers</span></span>
<span id="cb2-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">languageserver.server_capabilities =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb2-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">signatureHelpProvider =</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;">triggerCharacters =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"("</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="cb2-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">completionProvider =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb2-13">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resolveProvider =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">triggerCharacters =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</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="cb2-14">    )</span>
<span id="cb2-15">  )</span>
<span id="cb2-16">)</span></code></pre></div></div>
</div>
<p><strong>Important: not installing {httpgd} or not including this setting in .Rprofile will cause your graphics to be plotted in a fixed-size window, without the possibility of manipulating the plot size interactively.</strong></p>
</section>
<section id="at-last" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> AT LAST</h1>
<p>And that’s it for the basics! There’s still a lot of things to explore but you can already get an idea of the tool’s potential :p</p>



</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-lsp" class="csl-entry">
Lai, Randy. 2020. <em>Languageserver: Language Server Protocol</em>. <a href="https://CRAN.R-project.org/package=languageserver">https://CRAN.R-project.org/package=languageserver</a>.
</div>
<div id="ref-R" class="csl-entry">
R Core Team. 2020. <em>R: A Language and Environment for Statistical Computing</em>. Vienna, Austria: R Foundation for Statistical Computing. <a href="https://www.R-project.org/">https://www.R-project.org/</a>.
</div>
<div id="ref-httpgd" class="csl-entry">
Rupprecht, Florian. 2021. <em>Httpgd: A ’HTTP’ Server Graphics Device</em>. <a href="https://CRAN.R-project.org/package=httpgd">https://CRAN.R-project.org/package=httpgd</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>now integrated in vscode-R extension.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>tools</category>
  <guid>https://datamares.netlify.app/posts/202101_r-vscode/</guid>
  <pubDate>Tue, 01 Jun 2021 03:00:00 GMT</pubDate>
  <media:content url="https://datamares.netlify.app/posts/202101_r-vscode/img/vscode.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>A Bit of Concepts: Overfitting &amp; Resampling</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202101_overfitting-resampling/</link>
  <description><![CDATA[ 






<p>Although they are recurring terms in <em>machine learning</em>, <em>resampling</em> and <em>overfitting</em> are often discussed only in practice, frequently without a deep understanding. In this post<sup>1</sup>, I will try to introduce the concepts in a generic way.</p>
<section id="the-fitting-process-and-overfitting" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> THE FITTING PROCESS AND OVERFITTING</h1>
<p>Consider a fitting function <img src="https://latex.codecogs.com/png.latex?f">, a set of points <img src="https://latex.codecogs.com/png.latex?D%20=%20%7Bd_1,%20...,%20d_n%7D"> with <img src="https://latex.codecogs.com/png.latex?d_i%20=%20(x_i,%20y_i)">, decision variables or parameters <img src="https://latex.codecogs.com/png.latex?x_i%20%5Cin%20%5CR%5Em"> and output <img src="https://latex.codecogs.com/png.latex?y_i%20=%20f(x_i)%20%5Cin%20%5CR">. Unlike the classical approach, where, in the case of the classical linear regression model, there is a theoretical model with coefficients estimated by ordinary least squares (OLS) that is guaranteed by the Gauss-Markov theorem to be the best linear unbiased estimator (BLUE), in <em>machine learning</em> the goal is to iteratively find a meta-model that best approximates the function <img src="https://latex.codecogs.com/png.latex?f"> using the information contained in <img src="https://latex.codecogs.com/png.latex?D">, that is, we want to fit a regression function <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bf%7D_D"> to our data <img src="https://latex.codecogs.com/png.latex?D"> so that <img src="https://latex.codecogs.com/png.latex?%5Chat%7By%7D%20=%20%5Chat%7Bf%7D_D(x,%20%5Cvarepsilon)"> has the smallest approximation error <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon">.</p>
<p>To check how well the model <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bf%7D_D"> approximates the real function <img src="https://latex.codecogs.com/png.latex?f">, we need a loss function <img src="https://latex.codecogs.com/png.latex?L(y,%20%5Chat%7Bf%7D(x))"> which, in the case of regression, will be the quadratic loss <img src="https://latex.codecogs.com/png.latex?(y%20-%20%5Chat%7Bf%7D(x))%5E2"> or the absolute loss <img src="https://latex.codecogs.com/png.latex?%7Cy%20-%20%5Chat%7Bf%7D(x)%7C">. These values are averaged to form the cost functions mean squared error (MSE) and mean absolute error (MAE).</p>
<p>Given the loss function, we can define the risk associated with the model fitting function as <img src="https://latex.codecogs.com/png.latex?R(f,%20p)%20=%20%5Cint_%7B%5CR%7D%7B%7D%5Cint_%7B%5CR%5Em%7D%7B%7D%20L(y,%20f(x))p(x,%20y)dxdy"> where <img src="https://latex.codecogs.com/png.latex?p(x,%20y)"> is the joint probability density function. Since we do not have the real function but seek an estimated function that approximates it, we have <img src="https://latex.codecogs.com/png.latex?GE(%5Chat%7Bf%7D_D,%20p)%20=%20%5Cint_%7B%5CR%7D%7B%7D%5Cint_%7B%5CR%5Em%7D%7B%7D%20L(y,%20%5Chat%7Bf%7D_D(x))p(x,%20y)dxdy%20%5Ctag%7B1%7D"> which is the generalization error or conditional risk associated with the predictor.</p>
<p>Thus, we can estimate the generalization error of the model. Since we do not know the distribution <img src="https://latex.codecogs.com/png.latex?P">, we replace it with the test sample <img src="https://latex.codecogs.com/png.latex?D%5E*"> and get <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7BGE%7D(%5Chat%7Bf%7D_D,%20D%5E*)%20=%20%5Csum_%7B(x,%20y)%20%5Cin%20D%5E*%7D%20%5Cfrac%7BL(y,%20%5Chat%7Bf%7D_D(x))%7D%7B%7CD%5E*%7C%7D"> If we replace the test sample with the training sample <img src="https://latex.codecogs.com/png.latex?D"> used to fit the model, we have the so-called resubstitution error <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7BGE%7D_%5Ctext%7Bresub%7D%20=%20%5Cwidehat%7BGE%7D(%5Chat%7Bf%7D_D,%20D)"> Naturally, in this case we would be using the training data both to train the predictor and to estimate the generalization error, which would lead to a biased estimate of the generalization error. If we used this estimate for model selection, this bias would favor models more adapted to the sample.</p>
<p>The problem is that in these models, given enough iterations, the resubstitution error tends to zero. This happens because as the predictor adapts more and more to the training data, it will memorize the relationship between the set of points <img src="https://latex.codecogs.com/png.latex?D"> and the output <img src="https://latex.codecogs.com/png.latex?f(x_i)">, that is, it will fit perfectly to the shape of the function to be modeled. And a perfectly fitted model does not necessarily translate into the ability to predict future (out-of-sample) data.</p>
<p>In general, it is expected that the predictor reduces its bias during training just enough to be able to generalize its prediction to out-of-sample data at an optimal level of accuracy. Beyond this point, reducing bias is penalized by increased variance, i.e., by reducing its ability to predict future data. This process is called <em>overfitting</em>. This means we cannot consider the predictor’s performance on <img src="https://latex.codecogs.com/png.latex?D"> if we want to honestly estimate the real performance of the model.</p>
</section>
<section id="resampling" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> RESAMPLING</h1>
<p>One way to correct this problem is to split the sample into a training set <img src="https://latex.codecogs.com/png.latex?D_%5Ctext%7Btrain%7D"> and a test set <img src="https://latex.codecogs.com/png.latex?D_%5Ctext%7Btest%7D"> such that <img src="https://latex.codecogs.com/png.latex?D_%5Ctext%7Btrain%7D%20%5Ccup%20D_%5Ctext%7Btest%7D%20=%20D"> and <img src="https://latex.codecogs.com/png.latex?D_%5Ctext%7Btrain%7D%20%5Ccap%20D_%5Ctext%7Btest%7D%20=%20%5Cemptyset">. Thus, we can train the model on <img src="https://latex.codecogs.com/png.latex?D_%5Ctext%7Btrain%7D"> to obtain <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bf%7D_%7BD_%7B%5Ctext%7Btrain%7D%7D%7D"> and calculate its generalization error using the data from <img src="https://latex.codecogs.com/png.latex?D_%5Ctext%7Btest%7D">. This approach is called <em>hold-out</em> and it is simple to implement and use, since the observations in the test set are completely independent from those used to train the model. The estimate of the generalization error then becomes <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7BGE%7D_%5Ctext%7Bhold-out%7D%20=%20%5Cwidehat%7BGE%7D(%5Chat%7Bf%7D_%7BD_%7B%5Ctext%7Btrain%7D%7D%7D,%20D_%7B%5Ctext%7Btest%7D%7D)"> Two problems remain:</p>
<ol type="1">
<li><p>A large sample is required, since there must be enough data in both the training set to fit an adequate model and in the test set to perform a statistically valid performance evaluation.</p></li>
<li><p>This method is not sufficient to detect variance and instabilities in the training sample. More complex models, especially nonlinear ones, can produce very different results with small changes in the training data.</p></li>
</ol>
<p>It is precisely to deal with these situations that resampling techniques were developed. All these techniques repeatedly generate <img src="https://latex.codecogs.com/png.latex?i"> training subsets <img src="https://latex.codecogs.com/png.latex?D_%7B%5Ctext%7Btrain%7D%7D%5E%7B(i)%7D"> and test subsets <img src="https://latex.codecogs.com/png.latex?D_%7B%5Ctext%7Btest%7D%7D%5E%7B(i)%7D"> from the available dataset, fit a model with each training set, and assess its quality on the corresponding test set. The estimate of the generalization error then becomes <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7BGE%7D_%5Ctext%7Bsamp%7D%20=%20%5Cfrac%7B1%7D%7Bk%7D%5Csum_%7Bi=1%7D%5E%7Bk%7D%5Cwidehat%7BGE%7D(%5Chat%7Bf%7D_%7BD_%7B%5Ctext%7Btrain%7D%7D%5E%7B(i)%7D%7D,%20D_%7B%5Ctext%7Btest%7D%7D%5E%7B(i)%7D)%20%5Ctag%7B2%7D"> The generalization error given in equation (1) depends on both the size of the sample used to train and to test the fitted model. Therefore, we must ensure that the sample size used to check the generalization error of a model estimated from <img src="https://latex.codecogs.com/png.latex?n"> data points is close to <img src="https://latex.codecogs.com/png.latex?n">. If, for example, the training set is much smaller than the total sample, the error will be overestimated, since much less information was used to calculate the estimator.</p>
<p>Similarly, the quality of the generalization error estimator obtained in (2) from a resampling strategy also depends greatly on the size of the sets <img src="https://latex.codecogs.com/png.latex?D%5E%7B(i)%7D"> relative to the original sample, the number <img src="https://latex.codecogs.com/png.latex?k"> of subsets used, and the dependency structure between the subsets <img src="https://latex.codecogs.com/png.latex?D%5E%7B(i)%7D">—again, more complex models are more sensitive to changes in the dataset and the variance between subsets tends to be higher. The estimator’s error is usually measured by the mean squared error (MSE): <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BMSE%7D(%5Cwidehat%7BGE%7D_%5Ctext%7Bsamp%7D)%20=%20%5Cmathbb%7BE%7D%5B(%5Cwidehat%7BGE%7D_%5Ctext%7Bsamp%7D%20-%20GE(%5Chat%7Bf%7D_D,%20P))%5E2%5D"> This estimator can also be represented as the sum of the squared bias and the variance: <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BMSE%7D(%5Cwidehat%7BGE%7D_%5Ctext%7Bsamp%7D)%20=%20%5Ctext%7BBias%7D(%5Cwidehat%7BGE%7D_%5Ctext%7Bsamp%7D)%5E2%20+%20%5Ctext%7BVariance%7D(%5Cwidehat%7BGE%7D_%5Ctext%7Bsamp%7D)"> Where the bias expresses the average difference between an estimator and the true value, while the variance measures the average dispersion of the estimator. These quantities are defined as follows: <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BBias%7D(%5Cwidehat%7BGE%7D_%5Ctext%7Bsamp%7D)%20=%20%5Cmathbb%7BE%7D%5B%5Cwidehat%7BGE%7D_%5Ctext%7Bsamp%7D%5D%20-%20%5Cmathbb%7BE%7D%5BGE(%5Chat%7Bf%7D_D,%20p)%5D"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVariance%7D(%5Cwidehat%7BGE%7D_%5Ctext%7Bsamp%7D)%20=%20%5Cmathbb%7BE%7D%5B(%5Cwidehat%7BGE%7D_%5Ctext%7Bsamp%7D%20-%20%5Cmathbb%7BE%7D%5B%5Cwidehat%7BGE%7D_%5Ctext%7Bsamp%7D%5D)%5E2%5D"></p>
</section>
<section id="to-conclude" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> TO CONCLUDE</h1>
<p>With this introduction, I hope the concepts have become clearer and that it has helped a bit in understanding meta-modeling. I’m sure I won’t see the results the same way the next time I apply resampling techniques for selection and <em>tuning</em> of my models!</p>



</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-resampling" class="csl-entry">
Bischl B., Trautmann H., Mersmann O. 2012. <span>“Resampling Methods for Meta-Model Validation with Recommendations for Evolutionary Computation.”</span> <em>Evolutionary Computation</em>, May. <a href="https://doi.org/10.1162/EVCO_a_00069">https://doi.org/10.1162/EVCO_a_00069</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Heavily based on <span class="citation" data-cites="resampling">(Bischl B. 2012)</span>.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>machine learning</category>
  <guid>https://datamares.netlify.app/posts/202101_overfitting-resampling/</guid>
  <pubDate>Sat, 17 Apr 2021 03:00:00 GMT</pubDate>
  <media:content url="https://datamares.netlify.app/posts/202101_overfitting-resampling/img/overfitting.png" medium="image" type="image/png" height="70" width="144"/>
</item>
<item>
  <title>Introduction to the {mlr3} Framework</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202011_introducao-ao-mlr3-framework/</link>
  <description><![CDATA[ 






<p>This is the first post in a series about the {mlr3} ecosystem <span class="citation" data-cites="mlr3">(Lang et al. 2019)</span>. It is more complete and also much more complex than its predecessor, {mlr}, which had its initial version published on CRAN in 2013. The ecosystem provides an agnostic framework (i.e., it does not depend on the chosen algorithms), extensible and object-oriented, and currently supports various types of tasks such as classification, regression, survival analysis, forecasting, clustering, among others. {mlr3} has several advantages that make it, IMHO, the most complete <em>machine learning</em> framework for R <span class="citation" data-cites="R">(R Core Team 2020)</span>, and these will become clear throughout the next posts.</p>
<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> INTRODUCTION</h1>
<p>The standard workflow of a machine learning project consists of:</p>
<ol type="1">
<li>splitting your sample into training and test sets;</li>
<li>choosing the appropriate algorithm<sup>1</sup> for the task type;</li>
<li>passing the training sample to the algorithm to create a model of the relationship between the response variable (<em>output features</em>) and the explanatory variables (<em>input features</em>);</li>
<li>passing the test data to the trained model to produce predictions;</li>
<li>comparing the predictions with the sample data;</li>
<li>measuring the model’s performance using established accuracy metrics.</li>
</ol>
<p><img src="https://datamares.netlify.app/posts/202011_introducao-ao-mlr3-framework/img/basics.svg" class="img-fluid"></p>
<p>The process of repeating this workflow several times, splitting the training sample into different parts and using others as <em>fake test samples</em>, is called <em>resampling</em>, a vital process for the calibration stage and to avoid <em>overfitting</em>.</p>
<p>Depending on the data, the type of task, and the chosen algorithm, several filters may be necessary, such as normalization, feature selection, and handling outliers or missing data. For these cases, {mlr3} has new solutions that stand out not only compared to its predecessor {mlr} but also to other <em>machine learning</em> frameworks in R, such as {caret} and {tidymodels}.</p>
<section id="design-characteristics" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="design-characteristics"><span class="header-section-number">1.1</span> DESIGN CHARACTERISTICS</h2>
<p>Some general principles that guide the package’s development and greatly affect its use are:</p>
<ul>
<li><p>Focus on the <em>backend</em>. Most ecosystem packages aim to process and transform data, apply algorithms, and compute results. Visualizations are provided in external packages;</p></li>
<li><p>Adoption of the R6 class <span class="citation" data-cites="R-R6">(Chang 2020)</span> for object-oriented design, <em>modify-in-place</em>, and reference semantics (we’ll talk a bit about these concepts below);</p></li>
<li><p>Use of {data.table} <span class="citation" data-cites="R-data.table">(Dowle and Srinivasan 2020)</span> for data frame manipulations. The combination of {R6} + {data.table} makes performance one of the ecosystem’s strengths.</p></li>
<li><p>Low dependency. However, algorithms are not implemented in the ecosystem, as in Python’s scikit-learn. To run XGBoost <span class="citation" data-cites="xgboost">(Chen et al. 2020)</span>, for example, you must have the package that implements it installed.</p></li>
</ul>
</section>
<section id="out-of-scope" class="level2" data-number="1.2">
<h2 data-number="1.2" class="anchored" data-anchor-id="out-of-scope"><span class="header-section-number">1.2</span> OUT OF SCOPE</h2>
<p>As this is an introduction, the steps of <em>tuning</em> and <em>resampling</em>, as well as functionalities like <em>pipelines</em>, will be covered in future posts. In this post, we will only cover the basic workflow concepts.</p>
</section>
</section>
<section id="straight-to-the-point" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> STRAIGHT TO THE POINT</h1>
<p>To get to know the package’s basic functionalities, we’ll use one of the datasets included in R, <code>swiss</code>. This dataset consists of standardized measurements of fertility and socioeconomic indicators for 47 Swiss provinces in 1888.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># creating dataframe</span></span>
<span id="cb1-2">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> swiss</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># overview</span></span>
<span id="cb1-5">skimr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">skim</span>(data)</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Data summary</caption>
<tbody>
<tr class="odd">
<td style="text-align: left;">Name</td>
<td style="text-align: left;">data</td>
</tr>
<tr class="even">
<td style="text-align: left;">Number of rows</td>
<td style="text-align: left;">47</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Number of columns</td>
<td style="text-align: left;">6</td>
</tr>
<tr class="even">
<td style="text-align: left;">_______________________</td>
<td style="text-align: left;"></td>
</tr>
<tr class="odd">
<td style="text-align: left;">Column type frequency:</td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: left;">numeric</td>
<td style="text-align: left;">6</td>
</tr>
<tr class="odd">
<td style="text-align: left;">________________________</td>
<td style="text-align: left;"></td>
</tr>
<tr class="even">
<td style="text-align: left;">Group variables</td>
<td style="text-align: left;">None</td>
</tr>
</tbody>
</table>
<p><strong>Variable type: numeric</strong></p>
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 19%">
<col style="width: 11%">
<col style="width: 15%">
<col style="width: 6%">
<col style="width: 6%">
<col style="width: 6%">
<col style="width: 6%">
<col style="width: 6%">
<col style="width: 6%">
<col style="width: 6%">
<col style="width: 6%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">skim_variable</th>
<th style="text-align: right;">n_missing</th>
<th style="text-align: right;">complete_rate</th>
<th style="text-align: right;">mean</th>
<th style="text-align: right;">sd</th>
<th style="text-align: right;">p0</th>
<th style="text-align: right;">p25</th>
<th style="text-align: right;">p50</th>
<th style="text-align: right;">p75</th>
<th style="text-align: right;">p100</th>
<th style="text-align: left;">hist</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Fertility</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">70.14</td>
<td style="text-align: right;">12.49</td>
<td style="text-align: right;">35.00</td>
<td style="text-align: right;">64.70</td>
<td style="text-align: right;">70.40</td>
<td style="text-align: right;">78.45</td>
<td style="text-align: right;">92.5</td>
<td style="text-align: left;">▂▂▇▇▅</td>
</tr>
<tr class="even">
<td style="text-align: left;">Agriculture</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">50.66</td>
<td style="text-align: right;">22.71</td>
<td style="text-align: right;">1.20</td>
<td style="text-align: right;">35.90</td>
<td style="text-align: right;">54.10</td>
<td style="text-align: right;">67.65</td>
<td style="text-align: right;">89.7</td>
<td style="text-align: left;">▃▃▆▇▅</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Examination</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">16.49</td>
<td style="text-align: right;">7.98</td>
<td style="text-align: right;">3.00</td>
<td style="text-align: right;">12.00</td>
<td style="text-align: right;">16.00</td>
<td style="text-align: right;">22.00</td>
<td style="text-align: right;">37.0</td>
<td style="text-align: left;">▅▇▆▂▂</td>
</tr>
<tr class="even">
<td style="text-align: left;">Education</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">10.98</td>
<td style="text-align: right;">9.62</td>
<td style="text-align: right;">1.00</td>
<td style="text-align: right;">6.00</td>
<td style="text-align: right;">8.00</td>
<td style="text-align: right;">12.00</td>
<td style="text-align: right;">53.0</td>
<td style="text-align: left;">▇▃▁▁▁</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Catholic</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">41.14</td>
<td style="text-align: right;">41.70</td>
<td style="text-align: right;">2.15</td>
<td style="text-align: right;">5.20</td>
<td style="text-align: right;">15.14</td>
<td style="text-align: right;">93.12</td>
<td style="text-align: right;">100.0</td>
<td style="text-align: left;">▇▁▁▁▅</td>
</tr>
<tr class="even">
<td style="text-align: left;">Infant.Mortality</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">19.94</td>
<td style="text-align: right;">2.91</td>
<td style="text-align: right;">10.80</td>
<td style="text-align: right;">18.15</td>
<td style="text-align: right;">20.00</td>
<td style="text-align: right;">21.70</td>
<td style="text-align: right;">26.6</td>
<td style="text-align: left;">▁▂▇▆▂</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Among the available variables, we can choose to model infant mortality <code>Infant.Mortality</code> based on the other features, which are:</p>
<ul>
<li><code>Fertility</code>: Fertility measure. Like infant mortality, it is scaled between 0-100.</li>
<li><code>Agriculture</code>: Percentage of men involved in agriculture as an occupation.</li>
<li><code>Examination</code>: Percentage of conscripts well evaluated in army exams.</li>
<li><code>Education</code>: Percentage of conscripts with education above primary.</li>
<li><code>Catholic</code>: Percentage of Catholics (as opposed to Protestants).</li>
</ul>
<p>The workflow starts with the creation of the <code>task</code>, which is an object that contains the data and information about the task to be performed, such as the response variable<sup>2</sup> and the other features, as well as their types. Since we want to predict a continuous numeric variable, this is a regression task.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># importing package</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mlr3verse)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># creating task</span></span>
<span id="cb2-5">task_swiss <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> TaskRegr<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;">new</span>(</span>
<span id="cb2-6">  <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;">"swiss"</span>,</span>
<span id="cb2-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">backend =</span> data,</span>
<span id="cb2-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">target =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Infant.Mortality"</span></span>
<span id="cb2-9">)</span></code></pre></div></div>
</div>
<p>Notice anything unusual? Since {mlr3} works with the R6 class, its handling is more similar to other object-oriented languages, such as Python. This class has two special properties:</p>
<ul>
<li><p>Methods belong to objects and are called as <code>object$method()</code> and not as generic functions like <code>foo()</code>. This is the OOP (object-oriented programming) paradigm. In the example above, there is no function to create a task like <code>task_regr_new()</code>, but a <code>new()</code> method associated with the <code>TaskRegr</code> object;</p></li>
<li><p>R6 class objects are mutable, i.e., they are modified in place (<em>modify-in-place</em>) and therefore have reference semantics. This means they are not copied with each modification, unlike regular data frames (S3 class), which is a factor in memory allocation and, consequently, speed.</p></li>
</ul>
<p>The side effect is that this is not very familiar to people who only know R and at first it may seem unnatural and confusing.</p>
<p>Once the object is created, we can access it to check and visualize the information it contains:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># checking</span></span>
<span id="cb3-2">task_swiss</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
── &lt;TaskRegr&gt; (47x6) ───────────────────────────────────────────────────────────
• Target: Infant.Mortality
• Properties: -
• Features (5):
  • dbl (3): Agriculture, Catholic, Fertility
  • int (2): Education, Examination</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualizing</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">autoplot</span>(task_swiss, <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;">"pairs"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_introducao-ao-mlr3-framework/index_files/figure-html/task_cont-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We can see that only fertility is linearly correlated with infant mortality—the higher the fertility, the higher the mortality—and we can expect it to have more weight in predictions. The other variables do not show significant linear correlation with the response variable. However, they show moderate or strong correlation among themselves, but not enough to present collinearity, which would require treatment.</p>
<p>Now we select the algorithm<sup>3</sup> that will be used to train the model. Here I chose <em>XGBoost</em>. The full list can be accessed <a href="https://mlr3extralearners.mlr-org.com/articles/learners/learner_status.html">in this static list</a>, <a href="https://mlr3extralearners.mlr-org.com/articles/learners/list_learners.html">in this dynamic list</a>, or via the function <code>mlr3extralearners::list_mlr3learners()</code>. As mentioned earlier, algorithms are not implemented by the {mlr3} ecosystem, and the lists show the packages where the algorithms were implemented and that must be installed for use.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># defining the learner</span></span>
<span id="cb6-2">l_xgboost <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lrn</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regr.xgboost"</span>)</span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># checking</span></span>
<span id="cb6-5">l_xgboost</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
── &lt;LearnerRegrXgboost&gt; (regr.xgboost): Extreme Gradient Boosting ──────────────
• Model: -
• Parameters: nrounds=1000, nthread=1, verbose=0, verbosity=0
• Validate: NULL
• Packages: mlr3, mlr3learners, and xgboost
• Predict Types: [response]
• Feature Types: logical, integer, and numeric
• Encapsulation: none (fallback: -)
• Properties: hotstart_forward, importance, internal_tuning, missings, offset,
validation, and weights
• Other settings: use_weights = 'use'</code></pre>
</div>
</div>
<p>Let’s understand what the <code>l_xgboost</code> object tells us.</p>
<ol type="1">
<li><strong>Model</strong>: Empty, as there is no trained model yet;</li>
<li><strong>Parameters</strong>: The hyperparameters to be chosen and tuned for model performance;</li>
<li><strong>Packages</strong>: The package where the algorithm was implemented and from which it will be imported by {mlr3};</li>
<li><strong>Predict Type</strong>: If <code>response</code>, the prediction is returned as 0 or 1 for classification, or as a value for the response variable for regression—in this case, it will be infant mortality scaled in the [1, 100] range. If “prob”, for classification, the prediction returns the probability between 0 and 1;</li>
<li><strong>Feature Type</strong>: The types of variables the algorithm can handle. For <em>XGBoost</em>, for example, only numeric variables can be used. This means that factors must be converted into binary values (i.e., 0 or 1), that is, the matrix must be made sparse—for a factor <code>sex</code>, for example, in the preprocessing phase columns <code>sex.male</code> and <code>sex.female</code> would be created, each with values 1 or 0;</li>
<li><strong>Properties</strong>: Additional properties and capabilities of the algorithm. In this case, <strong>XGBoost</strong> can compute and return feature importance values for the model; handle missing data; and compute and return feature weights.</li>
</ol>
<p>As you can see in <em>parameters</em>, no hyperparameters are set. We can access them as follows:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># accessing hyperparameters</span></span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.table</span>(l_xgboost<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>param_set))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                  id    class lower upper               levels nlevels
              &lt;char&gt;   &lt;char&gt; &lt;num&gt; &lt;num&gt;               &lt;list&gt;   &lt;num&gt;
1:             alpha ParamDbl     0   Inf               [NULL]     Inf
2:     approxcontrib ParamLgl    NA    NA           TRUE,FALSE       2
3:        base_score ParamDbl  -Inf   Inf               [NULL]     Inf
4:           booster ParamFct    NA    NA gbtree,gblinear,dart       3
5:         callbacks ParamUty    NA    NA               [NULL]     Inf
6: colsample_bylevel ParamDbl     0     1               [NULL]     Inf
   is_bounded special_vals   default storage_type    tags
       &lt;lgcl&gt;       &lt;list&gt;    &lt;list&gt;       &lt;char&gt;  &lt;list&gt;
1:      FALSE    &lt;list[0]&gt;         0      numeric   train
2:       TRUE    &lt;list[0]&gt;     FALSE      logical predict
3:      FALSE    &lt;list[0]&gt;       0.5      numeric   train
4:       TRUE    &lt;list[0]&gt;    gbtree    character   train
5:      FALSE    &lt;list[0]&gt; &lt;list[0]&gt;         list   train
6:       TRUE    &lt;list[0]&gt;         1      numeric   train</code></pre>
</div>
</div>
<p>Since hyperparameter tuning is not the topic, let’s just set some basic things to demonstrate how this information is accessed and modified. The method for this is <code>param_set$values</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># hyperparameters</span></span>
<span id="cb10-2">l_xgboost<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>param_set<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb10-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># making the algorithm train more slowly</span></span>
<span id="cb10-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>,</span>
<span id="cb10-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># limiting tree depth</span></span>
<span id="cb10-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_depth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb10-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># maximum number of iterations</span></span>
<span id="cb10-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrounds =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,</span>
<span id="cb10-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># required with current xgboost versions</span></span>
<span id="cb10-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb10-11">)</span>
<span id="cb10-12"></span>
<span id="cb10-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># checking</span></span>
<span id="cb10-14">l_xgboost</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
── &lt;LearnerRegrXgboost&gt; (regr.xgboost): Extreme Gradient Boosting ──────────────
• Model: -
• Parameters: eta=0.1, max_depth=5, nrounds=100, verbose=1
• Validate: NULL
• Packages: mlr3, mlr3learners, and xgboost
• Predict Types: [response]
• Feature Types: logical, integer, and numeric
• Encapsulation: none (fallback: -)
• Properties: hotstart_forward, importance, internal_tuning, missings, offset,
validation, and weights
• Other settings: use_weights = 'use'</code></pre>
</div>
</div>
</section>
<section id="training-and-prediction" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> TRAINING AND PREDICTION</h1>
<p>The next steps are training and prediction—we’ll cover <em>tuning</em> and <em>resampling</em> in future posts. First, split the dataset into training and test sets. For this, we’ll use the <code>sample()</code> function on two methods of the <code>task_swiss</code> object, <code>row_ids</code> and <code>nrow</code>. The first enumerates the indices of each row:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># row_ids method</span></span>
<span id="cb12-2">task_swiss<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>row_ids</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47</code></pre>
</div>
</div>
<p>While the second returns the number of rows in the dataset:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># nrow method</span></span>
<span id="cb14-2">task_swiss<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nrow</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 47</code></pre>
</div>
</div>
<p>Thus, we can select the dataset indices into two random samples:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ensuring reproducibility</span></span>
<span id="cb16-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb16-3"></span>
<span id="cb16-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># indices for training sample</span></span>
<span id="cb16-5">train_set <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(task_swiss<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>row_ids, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> task_swiss<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nrow)</span>
<span id="cb16-6"></span>
<span id="cb16-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># indices for test sample</span></span>
<span id="cb16-8">test_set <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setdiff</span>(task_swiss<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>row_ids, train_set)</span>
<span id="cb16-9"></span>
<span id="cb16-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># checking</span></span>
<span id="cb16-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(train_set)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1]  4 39  1 34 23 14</code></pre>
</div>
</div>
<p>With the indices selected, we can train only on the randomly chosen 70% of the sample, without copying the data and allocating unnecessary memory:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># training</span></span>
<span id="cb18-2">l_xgboost<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;">train</span>(task_swiss, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row_ids =</span> train_set)</span>
<span id="cb18-3"></span>
<span id="cb18-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># checking</span></span>
<span id="cb18-5">l_xgboost<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>model</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>##### xgb.Booster
call:
  xgboost::xgb.train(params = pv[names(pv) %in% formalArgs(xgboost::xgb.params)], 
    data = xgb_data, nrounds = pv$nrounds, evals = pv$evals, 
    custom_metric = pv$custom_metric, verbose = pv$verbose, print_every_n = pv$print_every_n, 
    early_stopping_rounds = pv$early_stopping_rounds, maximize = pv$maximize, 
    save_period = pv$save_period, save_name = pv$save_name, callbacks = pv$callbacks %??% 
        list())
# of features: 5 
# of rounds:  100 </code></pre>
</div>
</div>
<p>As we can see, in the first iteration the model obtained an <em>rmse</em><sup>4</sup>. of 17.7, which is high considering the [1-100] scale of infant mortality. Throughout training, the error was reduced to 0.03, which does not mean that its performance will remain at this level when extrapolated to the test sample or new data, but it is a good sign. The expected result is that the real performance of the model, after being applied to the test sample, will be between the initial and final iteration. If it is <em>better</em> than the test performance, something is certainly wrong.</p>
<p>Let’s check the real performance after making predictions on the test sample. First, we pass the test indices to the learner object with the model and call the <code>predict()</code> method to get the predictions.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># predictions</span></span>
<span id="cb20-2">preds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> l_xgboost<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;">predict</span>(task_swiss, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row_ids =</span> test_set)</span>
<span id="cb20-3"></span>
<span id="cb20-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># checking</span></span>
<span id="cb20-5">preds</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
── &lt;PredictionRegr&gt; for 15 observations: ───────────────────────────────────────
 row_ids truth response
       8  24.9 19.90614
      11  24.5 19.05639
      13  19.1 18.36767
     ---   ---      ---
      40  20.5 20.66445
      43  20.0 21.60975
      47  19.3 19.03525</code></pre>
</div>
</div>
<p>In the predictions object, both the values predicted by the model (<code>response</code>) and the sample values (<code>truth</code>) are stored. These values can then be compared to calculate the model’s accuracy using the <code>score()</code> method:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># accuracy</span></span>
<span id="cb22-2">preds<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;">score</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb22-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">msr</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regr.rmse"</span>),</span>
<span id="cb22-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">msr</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regr.mae"</span>)</span>
<span id="cb22-5">))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>regr.rmse  regr.mae 
 2.419703  1.889676 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualizing</span></span>
<span id="cb24-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">autoplot</span>(preds)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_introducao-ao-mlr3-framework/index_files/figure-html/accuracy-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The model’s <em>rmse</em> on the test sample was only 2.59 units, which is very good performance!</p>
</section>
<section id="interpretation" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> INTERPRETATION</h1>
<p>Since XGBoost has the <em>feature importance</em> property, we can extract it with the <code>importance()</code> method:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># feature importance</span></span>
<span id="cb25-2">l_xgboost<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;">importance</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  Fertility    Catholic Agriculture Examination   Education 
 0.43614832  0.37091549  0.09587702  0.07577311  0.02128606 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualizing</span></span>
<span id="cb27-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">barplot</span>(l_xgboost<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;">importance</span>())</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_introducao-ao-mlr3-framework/index_files/figure-html/importance-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>However, importance alone does not describe the relationship of the feature with the response variable, nor its direction, being a very poor measure of interpretation. We will discuss interpretation techniques in other posts.</p>



</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-R-R6" class="csl-entry">
Chang, Winston. 2020. <em>R6: Encapsulated Classes with Reference Semantics</em>. <a href="https://CRAN.R-project.org/package=R6">https://CRAN.R-project.org/package=R6</a>.
</div>
<div id="ref-xgboost" class="csl-entry">
Chen, Tianqi, Tong He, Michael Benesty, Vadim Khotilovich, Yuan Tang, Hyunsu Cho, Kailong Chen, et al. 2020. <em>Xgboost: Extreme Gradient Boosting</em>. <a href="https://CRAN.R-project.org/package=xgboost">https://CRAN.R-project.org/package=xgboost</a>.
</div>
<div id="ref-R-data.table" class="csl-entry">
Dowle, Matt, and Arun Srinivasan. 2020. <em>Data.table: Extension of ‘Data.frame‘</em>. <a href="https://CRAN.R-project.org/package=data.table">https://CRAN.R-project.org/package=data.table</a>.
</div>
<div id="ref-mlr3" class="csl-entry">
Lang, Michel, Martin Binder, Jakob Richter, Patrick Schratz, Florian Pfisterer, Stefan Coors, Quay Au, Giuseppe Casalicchio, Lars Kotthoff, and Bernd Bischl. 2019. <span>“<span class="nocase">mlr3</span>: A Modern Object-Oriented Machine Learning Framework in <span>R</span>.”</span> <em>Journal of Open Source Software</em>, December. <a href="https://doi.org/10.21105/joss.01903">https://doi.org/10.21105/joss.01903</a>.
</div>
<div id="ref-R" class="csl-entry">
R Core Team. 2020. <em>R: A Language and Environment for Statistical Computing</em>. Vienna, Austria: R Foundation for Statistical Computing. <a href="https://www.R-project.org/">https://www.R-project.org/</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>In {mlr3} it’s called a <em>learner</em>.↩︎</p></li>
<li id="fn2"><p>Also called <em>output feature</em> or <em>label</em>.↩︎</p></li>
<li id="fn3"><p>Here we will work with just one, but in future posts we will use several—in pipelines with different features, stacking, etc.↩︎</p></li>
<li id="fn4"><p>Root Mean Squared Error↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>machine learning</category>
  <guid>https://datamares.netlify.app/posts/202011_introducao-ao-mlr3-framework/</guid>
  <pubDate>Sun, 27 Dec 2020 03:00:00 GMT</pubDate>
  <media:content url="https://datamares.netlify.app/posts/202011_introducao-ao-mlr3-framework/img/mlr3.png" medium="image" type="image/png" height="73" width="144"/>
</item>
<item>
  <title>Effect Size and Gender Income Inequality in Vitória</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202011_desigualdade-de-renda-entre-generos/</link>
  <description><![CDATA[ 






<p>There is still a lot to cover in this “Inference 101” series and, to continue our studies, I brought the 2017 RAIS (Annual Social Information Report) dataset<sup>1</sup>. With it, we will introduce the concept of <em>effect size</em> and, as a bonus, analyze gender inequality in our beloved capital, Vitória.</p>
<section id="data" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> DATA</h1>
<p>Here are two options to obtain this data:</p>
<ol type="1">
<li>Manually, by browsing ftp://ftp.mtps.gov.br/pdet/microdados/RAIS/2017/ES2017.7z</li>
<li>Programmatically via Python.</li>
</ol>
<p>I chose Python here because I don’t know of a CRAN<sup>2</sup> package that handles 7zip compression—if you know one, please let me know in the comments! If you choose to download and extract via script, we can run Python code from R using {reticulate}:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># activating python</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(reticulate)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># selecting the environment</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_condaenv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"base"</span>)</span></code></pre></div></div>
</div>
<p>After activating the Python environment, let’s download and extract the dataset to a folder called <code>data</code> inside our project directory. If you downloaded it manually, create the <code>data</code> folder and place the file there.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># importing modules</span></span>
<span id="cb2-2">import urllib.request as rq</span>
<span id="cb2-3">import py7zr</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># downloading data</span></span>
<span id="cb2-6">url <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;">"ftp://ftp.mtps.gov.br/pdet/microdados/RAIS/2017/ES2017.7z"</span></span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rq.urlretrieve</span>(url, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/rais.7z"</span>)</span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># extracting</span></span>
<span id="cb2-10">rais <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">py7zr.SevenZipFile</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/rais.7z"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mode=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>)</span>
<span id="cb2-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rais.extractall</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/"</span>)</span>
<span id="cb2-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rais.close</span>()</span></code></pre></div></div>
</div>
<p>We will also need the Brazilian Occupation Code table to understand the professions of the people analyzed. After <a href="http://www.mtecbo.gov.br/cbosite/pages/downloads.jsf">downloading it</a>, place it in the <code>data</code> folder along with the Rais file.</p>
<p>With the data downloaded, let’s import, explore, and start making sense of it.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># packages</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># importing Rais</span></span>
<span id="cb3-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># as the data contains special characters (accents),</span></span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># it is necessary to set the encoding to LATIN1</span></span>
<span id="cb3-7">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_delim</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/ES2017.txt"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delim =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">";"</span>,</span>
<span id="cb3-8">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">locale =</span> 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;">locale</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">encoding =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LATIN1"</span>),</span>
<span id="cb3-9">                  <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="cb3-10">                  <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;">1</span>)</span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># selecting variables of interest and renaming columns</span></span>
<span id="cb3-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># we also need to replace commas with dots</span></span>
<span id="cb3-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># if you prefer, you can do this step via encoding</span></span>
<span id="cb3-15">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data <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;">select</span>(X8, X20, X26, X31, X35, X38) <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;">rename</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cod_profissao"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> X8,</span>
<span id="cb3-18">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"idade"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> X20,</span>
<span id="cb3-19">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"municipio"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> X26,</span>
<span id="cb3-20">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cod_raca"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> X31,</span>
<span id="cb3-21">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rem_media"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> X35,</span>
<span id="cb3-22">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sexo"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> X38) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-23">  <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;">rem_media =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_replace_all</span>(rem_media, <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="cb3-24">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sexo =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(sexo),</span>
<span id="cb3-25">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">idade =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(idade))</span>
<span id="cb3-26"></span>
<span id="cb3-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># importing CBO table</span></span>
<span id="cb3-28">cbo <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_delim</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/CBO2002.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delim =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">";"</span>,</span>
<span id="cb3-29">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">locale =</span> 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;">locale</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">encoding =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LATIN1"</span>),</span>
<span id="cb3-30">                 <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;">"cod_profissao"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"profissao"</span>),</span>
<span id="cb3-31">                 <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;">"d"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c"</span>),</span>
<span id="cb3-32">                 <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;">1</span>)</span>
<span id="cb3-33"></span>
<span id="cb3-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># joining both tables and filtering only Vitória</span></span>
<span id="cb3-35">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inner_join</span>(cbo, <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;">"cod_profissao"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(municipio <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;">320530</span>)</span></code></pre></div></div>
</div>
<p>Our data now looks like this:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># viewing dataframe</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 7
  cod_profissao idade municipio cod_raca rem_media  sexo profissao              
          &lt;dbl&gt; &lt;int&gt;     &lt;dbl&gt; &lt;chr&gt;        &lt;dbl&gt; &lt;int&gt; &lt;chr&gt;                  
1        717020    42    320530 04            909.     1 Servente de obras      
2        715210    28    320530 02           1167.     1 Pedreiro               
3        717020    63    320530 02           1022.     1 Servente de obras      
4        717020    59    320530 02            880.     1 Servente de obras      
5        517410    29    320530 08           1880.     1 Porteiro de edifícios  
6        715115    61    320530 08              0      1 Operador de escavadeira</code></pre>
</div>
</div>
</section>
<section id="aggregate-differences" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> AGGREGATE DIFFERENCES</h1>
<p>First, let’s try to understand our sample:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of men and women</span></span>
<span id="cb6-2">data <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;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(sexo),</span>
<span id="cb6-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(sexo),</span>
<span id="cb6-5">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</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;">number</span>(..count..))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>,</span>
<span id="cb6-8">             <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>
<span id="cb6-9">             <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;">"grey30"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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>number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender"</span>,</span>
<span id="cb6-12">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"men"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"women"</span>),</span>
<span id="cb6-13">                    <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;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"salmon"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb6-15">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>,</span>
<span id="cb6-16">       <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;">"NUMBER OF MEN AND WOMEN"</span>,</span>
<span id="cb6-17">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sample from the city of Vitória-ES"</span>,</span>
<span id="cb6-18">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"source: Rais/2017"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-20">  <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;">text =</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;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Century Gothic"</span>,</span>
<span id="cb6-21">                            <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;">"grey30"</span>),</span>
<span id="cb6-22">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>())</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_desigualdade-de-renda-entre-generos/index_files/figure-html/barplot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We have almost the same number of men and women in our sample. Before performing any further analysis or applying filters, let’s calculate the average income:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># average income</span></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aggregate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data, rem_media <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> sexo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> mean)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  sexo rem_media
1    1  3925.187
2    2  2846.727</code></pre>
</div>
</div>
<p>In the aggregate, the average income of men in Vitória is almost 40% higher than that of women, and even when considering the median, it is still almost 30% higher. We can check if this difference is significant with a t-test, but first we should check if its assumptions are met.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># quartiles</span></span>
<span id="cb9-2">boxplot_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(sexo) <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;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stats =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fivenum</span>(rem_media))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(stats))</span>
<span id="cb9-6"></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># boxplot</span></span>
<span id="cb9-8">data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-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>
<span id="cb9-10">    <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;">factor</span>(sexo),</span>
<span id="cb9-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> rem_media,</span>
<span id="cb9-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(sexo)</span>
<span id="cb9-13">  )) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>(</span>
<span id="cb9-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> boxplot_data,</span>
<span id="cb9-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(sexo), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> stats, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</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;">number</span>(stats)),</span>
<span id="cb9-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nudge_x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>,</span>
<span id="cb9-19">    <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>
<span id="cb9-20">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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>number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(</span>
<span id="cb9-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender"</span>,</span>
<span id="cb9-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"men"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"women"</span>),</span>
<span id="cb9-26">    <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;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"salmon"</span>)</span>
<span id="cb9-27">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb9-29">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb9-30">    <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;">"average income"</span>,</span>
<span id="cb9-31">    <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;">"DISTRIBUTION OF AVERAGE INCOME BETWEEN MEN AND WOMEN"</span>,</span>
<span id="cb9-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sample from the city of Vitória-ES"</span>,</span>
<span id="cb9-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"source: Rais/2017"</span></span>
<span id="cb9-34">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb9-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(</span>
<span id="cb9-38">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Century Gothic"</span>,</span>
<span id="cb9-39">      <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;">"grey30"</span></span>
<span id="cb9-40">    ),</span>
<span id="cb9-41">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()</span>
<span id="cb9-42">  )</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_desigualdade-de-renda-entre-generos/index_files/figure-html/boxplot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<section id="checking-the-normality-assumption" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="checking-the-normality-assumption"><span class="header-section-number">2.1</span> CHECKING THE NORMALITY ASSUMPTION</h2>
<p>We know that the t-test is a parametric test and <a href="https://datamares.netlify.app/post/2020-11-20-comparando-variancias/">as discussed previously</a>, when the data does not follow a distribution close to normal, we need to perform transformations to achieve normality.</p>
<p>You might think that with such a large sample, the Central Limit Theorem guarantees normality. However, especially with data containing many extreme outliers, the sample size required for convergence can be huge, practically invalidating the CLT assertion<sup>3</sup>. Therefore, I argue that we should still be careful with this assumption even when dealing with large samples.</p>
<p>The first analysis in this sense is visual. We can see that the distribution has a fat tail and is far from normal, both in the histogram and the Q-Q plot.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># histogram</span></span>
<span id="cb10-2">data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(</span>
<span id="cb10-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> rem_media,</span>
<span id="cb10-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(sexo)</span>
<span id="cb10-6">  )) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20000</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-9">  <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>number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(</span>
<span id="cb10-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gender"</span>,</span>
<span id="cb10-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"men"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"women"</span>),</span>
<span id="cb10-14">    <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;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"salmon"</span>)</span>
<span id="cb10-15">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb10-17">    <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;">"average income"</span>,</span>
<span id="cb10-18">    <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;">"count"</span>,</span>
<span id="cb10-19">    <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;">"DISTRIBUTION OF AVERAGE INCOME BETWEEN MEN AND WOMEN"</span>,</span>
<span id="cb10-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sample from the city of Vitória-ES"</span>,</span>
<span id="cb10-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"source: Rais/2017"</span></span>
<span id="cb10-22">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-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;">text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(</span>
<span id="cb10-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Century Gothic"</span>,</span>
<span id="cb10-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey30"</span></span>
<span id="cb10-27">  ))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_desigualdade-de-renda-entre-generos/index_files/figure-html/qqplot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># qqplot</span></span>
<span id="cb11-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data will be on the line if normally distributed</span></span>
<span id="cb11-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</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>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb11-4"></span>
<span id="cb11-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qqnorm</span>(data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sexo <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>rem_media,</span>
<span id="cb11-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Q-Q PLOT: MEN"</span>)</span>
<span id="cb11-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qqline</span>(data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sexo <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>rem_media)</span>
<span id="cb11-8"></span>
<span id="cb11-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qqnorm</span>(data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sexo <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>rem_media,</span>
<span id="cb11-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Q-Q PLOT: WOMEN"</span>)</span>
<span id="cb11-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qqline</span>(data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sexo <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>rem_media)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_desigualdade-de-renda-entre-generos/index_files/figure-html/qqplot-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We can perform an experiment to check the speed of convergence to the normal distribution. By calculating the distribution of a thousand means of 30 men each, if it shows a distribution close to normal we can assume normality and proceed. Otherwise, we will need to treat the data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ensure reproducibility</span></span>
<span id="cb12-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb12-3"></span>
<span id="cb12-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of samples</span></span>
<span id="cb12-5">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb12-6"></span>
<span id="cb12-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># means</span></span>
<span id="cb12-8">means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, n)</span>
<span id="cb12-9"></span>
<span id="cb12-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># drawing samples and calculating means</span></span>
<span id="cb12-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n) {</span>
<span id="cb12-12">  means[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(</span>
<span id="cb12-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sexo <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>rem_media,</span>
<span id="cb12-14">      <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;">30</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="cb12-15">    )</span>
<span id="cb12-16">  )</span>
<span id="cb12-17">}</span>
<span id="cb12-18"></span>
<span id="cb12-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># qqplot</span></span>
<span id="cb12-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qqnorm</span>(means)</span>
<span id="cb12-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qqline</span>(means)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_desigualdade-de-renda-entre-generos/index_files/figure-html/simulation-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualization</span></span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(means,</span>
<span id="cb13-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DISTRIBUTION OF SAMPLE MEANS"</span>,</span>
<span id="cb13-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"means"</span>,</span>
<span id="cb13-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sub =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"seed = 1"</span></span>
<span id="cb13-6">)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_desigualdade-de-renda-entre-generos/index_files/figure-html/simulation-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Phew! The CLT held in our data and both the Q-Q plot and histogram showed a distribution close to normal, allowing us to proceed with the tests.</p>
</section>
<section id="t-test" class="level2" data-number="2.2">
<h2 data-number="2.2" class="anchored" data-anchor-id="t-test"><span class="header-section-number">2.2</span> T-TEST</h2>
<p>Now that we’ve ensured the prerequisites, let’s test if the difference between means is significant. For this, we’ll use the {infer} package:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># transforming `sexo` variable</span></span>
<span id="cb14-2">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sexo =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(sexo <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"man"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"woman"</span>))</span>
<span id="cb14-4"></span>
<span id="cb14-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># loading package</span></span>
<span id="cb14-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(infer)</span>
<span id="cb14-7"></span>
<span id="cb14-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating t statistic</span></span>
<span id="cb14-9">calculated_stat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">specify</span>(rem_media <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> sexo) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calculate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</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;">"man"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"woman"</span>))</span>
<span id="cb14-12"></span>
<span id="cb14-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># generating null distribution</span></span>
<span id="cb14-14">null_dist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">specify</span>(rem_media <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> sexo) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hypothesise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">null =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"independence"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"permute"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calculate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</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;">"man"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"woman"</span>))</span>
<span id="cb14-19"></span>
<span id="cb14-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualizing null distribution and test statistic</span></span>
<span id="cb14-21">null_dist <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">visualize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"both"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shade_p_value</span>(calculated_stat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"greater"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_desigualdade-de-renda-entre-generos/index_files/figure-html/t test agg-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating p-value</span></span>
<span id="cb15-2">null_dist <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;">get_p_value</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">obs_stat =</span> calculated_stat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"greater"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 1
  p_value
    &lt;dbl&gt;
1       0</code></pre>
</div>
</div>
<p>With a p-value of 0% and a calculated statistic <em>miles</em> away from the null distribution, it is clear that the difference is significant. However, <strong>this does not mean it is large or small</strong>. That’s where effect size indicators come in.</p>
</section>
<section id="effect-size-cohens-d" class="level2" data-number="2.3">
<h2 data-number="2.3" class="anchored" data-anchor-id="effect-size-cohens-d"><span class="header-section-number">2.3</span> EFFECT SIZE: <em>Cohen’s D</em></h2>
<p><a href="https://datamares.netlify.app/post/2020-11-06-chutou-ou-n%C3%A3o-chutou-o-teste-t-para-uma-amostra/">In this post</a> I showed that sample size is crucial for the t-test and we tend to reject the null hypothesis as the sample size increases. Here, with more than 140,000 observations for each gender, it would be hard for these differences not to be significant. To complement this test, we can calculate the <strong>effect size</strong> using Cohen’s d statistic.</p>
<p>Widely used in health experiments with paired t-tests, when it is necessary to define the effect of an action in a treatment group compared to a control group, Cohen’s d is an indicator of standardized differences and is particularly valuable for quantifying the effect of an intervention, whether in public policy or marketing actions in your company. This means it emphasizes the size of the difference between means, without confusing it with sample size.</p>
<p>Because it is standardized (i.e., not measured in the sample unit, here BR<img src="https://latex.codecogs.com/png.latex?,%20but%20in%20standard%20deviations),%20it%20can%20be%20easily%20compared%20across%20different%20experiments.%20Its%20calculation%20is%20as%20follows:">$ d = $$</p>
<p>For two fixed means, the smaller the standard deviation, the larger the d statistic. Conversely, the larger the standard deviation, the smaller the effect size.</p>
<p>In R, we’ll use the {effectsize} package:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># loading package</span></span>
<span id="cb17-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(effectsize)</span>
<span id="cb17-3"></span>
<span id="cb17-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating standardized difference</span></span>
<span id="cb17-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cohens_d</span>(</span>
<span id="cb17-6">  data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sexo <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;">"man"</span>, ]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rem_media,</span>
<span id="cb17-7">  data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sexo <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;">"woman"</span>, ]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rem_media</span>
<span id="cb17-8">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Cohen's d |       95% CI
------------------------
0.26      | [0.25, 0.26]

- Estimated using pooled SD.</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># interpreting d statistic</span></span>
<span id="cb19-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">interpret_cohens_d</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.26</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gignac2016"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "small"
(Rules: gignac2016)</code></pre>
</div>
</div>
<p>With a statistic <img src="https://latex.codecogs.com/png.latex?d=0.26">, the difference is considered small. On the Gignac &amp; Szodorai (2016) scale:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rules</span>(</span>
<span id="cb21-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.41</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.63</span>),</span>
<span id="cb21-3">  <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;">"very small"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"small"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"moderate"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"large"</span>),</span>
<span id="cb21-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gignac &amp; Szodorai (2016)"</span></span>
<span id="cb21-5">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># Reference Thresholds (Gignac &amp; Szodorai (2016))

         Label           
-------------------------
       very small &lt;=  0.2
 0.2 &lt;   small    &lt;= 0.41
0.41 &lt;  moderate  &lt;= 0.63
0.63 &lt;   large           </code></pre>
</div>
</div>
<p>In a situation of equity, 50% of women would have income below the average man, while the other half would have income above.</p>
<p>An effect of 0.26 means that 62% of women would have income below the average man in Vitória, while only 38% would earn more than the average man<sup>4</sup>.</p>
<p>Of course, analyzing the aggregate means looking at the average. What about these differences among various professions?</p>
</section>
</section>
<section id="differences-between-professions" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> DIFFERENCES BETWEEN PROFESSIONS</h1>
<p>We can extend our analysis by checking which professions have the greatest and smallest income gaps.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># creating dataframe of income differences</span></span>
<span id="cb23-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># selected professions</span></span>
<span id="cb23-3">differences <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-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;">profissao =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb23-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Advogado"</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;">"Lawyers"</span>,</span>
<span id="cb23-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Engenheiro"</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;">"Engineers"</span>,</span>
<span id="cb23-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Médico"</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;">"Doctors"</span>,</span>
<span id="cb23-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gerente"</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;">"Managers"</span>,</span>
<span id="cb23-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dirigentes|Diretor"</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;">"Executives"</span>,</span>
<span id="cb23-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Economista"</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;">"Economists"</span>,</span>
<span id="cb23-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Analista"</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;">"Analysts"</span>,</span>
<span id="cb23-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Técnico"</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;">"Technicians"</span>,</span>
<span id="cb23-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Enfermeiro"</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;">"Nurses"</span>,</span>
<span id="cb23-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(profissao,</span>
<span id="cb23-15">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(Professor)(.*)(ensino superior)(.*)"</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;">"Higher Ed. Professors"</span>,</span>
<span id="cb23-16">    <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 class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Others"</span></span>
<span id="cb23-17">  )) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(sexo, profissao) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb23-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">renda =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(rem_media),</span>
<span id="cb23-21">    <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="cb23-22">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-23">  <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="cb23-24">    <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> sexo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(renda, n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-25">    <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;">diff =</span> renda_man <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> renda_woman) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-26">    <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>(diff))</span>
<span id="cb23-27"></span>
<span id="cb23-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># viewing table</span></span>
<span id="cb23-29">differences</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 11 × 6
   profissao             renda_man renda_woman  n_man n_woman  diff
   &lt;chr&gt;                     &lt;dbl&gt;       &lt;dbl&gt;  &lt;int&gt;   &lt;int&gt; &lt;dbl&gt;
 1 Executives               13559.       6994.    393     228 6565.
 2 Engineers                13997.      10446.   1850     409 3550.
 3 Lawyers                   8625.       5658.    244     342 2966.
 4 Economists                8251.       5576.    168     184 2675.
 5 Managers                  6599.       4463.   3659    3477 2136.
 6 Technicians               4790.       2731.  10434   10493 2060.
 7 Analysts                  5647.       4039.   2931    1869 1607.
 8 Higher Ed. Professors     7384.       6470.   1248    1062  914.
 9 Others                    3427.       2606. 121362  120682  820.
10 Doctors                   8232.       7704.   1840    1984  528.
11 Nurses                    4166.       4439.    540    2139 -273.</code></pre>
</div>
</div>
<p>Among the selected professions, only nursing has a higher average income for women. Men have the advantage in all others. Let’s plot this:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot</span></span>
<span id="cb25-2">mean_diff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3925.18</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;">2846.727</span></span>
<span id="cb25-3">arrows <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb25-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.9</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.6</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.3</span>),</span>
<span id="cb25-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.4</span>),</span>
<span id="cb25-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y1 =</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;">2000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb25-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y2 =</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;">1200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6300</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;">270</span>)</span>
<span id="cb25-8">)</span>
<span id="cb25-9"></span>
<span id="cb25-10">differences <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reorder</span>(profissao, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(profissao)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> diff)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> mean_diff, <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;">"grey85"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(</span>
<span id="cb25-14">    <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;">y =</span> mean_diff, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> diff, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> profissao),</span>
<span id="cb25-15">    <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;">"grey85"</span>, <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;">7</span></span>
<span id="cb25-16">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</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;">size =</span> renda_man), <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;">"#F06337"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-18">  <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>number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_size_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>number, <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;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</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="cb25-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>,</span>
<span id="cb25-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>, <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;">2000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</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;">"grey85"</span>,</span>
<span id="cb25-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"the average difference is</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;">R$ 1,078"</span></span>
<span id="cb25-23">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>,</span>
<span id="cb25-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.5</span>, <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;">5500</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</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;">"grey85"</span>,</span>
<span id="cb25-26">    <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;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"executive positions"</span>,</span>
<span id="cb25-27">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"have the largest difference"</span>,</span>
<span id="cb25-28">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"and also the highest salaries"</span>,</span>
<span id="cb25-29">    <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;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb25-30">  )) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>,</span>
<span id="cb25-32">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <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;">250</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</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;">"grey85"</span>,</span>
<span id="cb25-33">  <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;">paste</span>(</span>
<span id="cb25-34">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"among these professions,"</span>,</span>
<span id="cb25-35">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"only female nurses"</span>,</span>
<span id="cb25-36">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"earn more than men"</span>,</span>
<span id="cb25-37">    <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;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>),</span>
<span id="cb25-38">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_curve</span>(</span>
<span id="cb25-40">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> arrows, <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> x1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> x2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> y2),</span>
<span id="cb25-41">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrow</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inch"</span>)),</span>
<span id="cb25-42">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</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;">"grey85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">curvature =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span></span>
<span id="cb25-43">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-44">  <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="cb25-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb25-46">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb25-47">    <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;">"income difference in R$"</span>,</span>
<span id="cb25-48">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"avg. income</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;">(men)"</span>,</span>
<span id="cb25-49">    <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;">"gender inequality"</span>,</span>
<span id="cb25-50">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"salary disparities show the long road ahead to overcome the view"</span>,</span>
<span id="cb25-51">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"of women as second-class professionals in the labor market"</span>,</span>
<span id="cb25-52">    <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;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>),</span>
<span id="cb25-53">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"source: Rais 2017, Vitória/ES | elaboration: Alberson Miranda"</span></span>
<span id="cb25-54">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-55">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb25-56">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb25-57">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span 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;">"#002538"</span>),</span>
<span id="cb25-58">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#002538"</span>),</span>
<span id="cb25-59">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(</span>
<span id="cb25-60">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Century Gothic"</span>,</span>
<span id="cb25-61">      <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;">"grey85"</span></span>
<span id="cb25-62">    ),</span>
<span id="cb25-63">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text =</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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey85"</span>, <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;">10</span>),</span>
<span id="cb25-64">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_line</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;">"grey85"</span>),</span>
<span id="cb25-65">    <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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">36</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)),</span>
<span id="cb25-66">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(</span>
<span id="cb25-67">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb25-68">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</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;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb25-69">      <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;">16</span></span>
<span id="cb25-70">    ),</span>
<span id="cb25-71">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.caption =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb25-72">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot"</span>,</span>
<span id="cb25-73">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.border =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</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;">"grey85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb25-74">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#002538"</span>),</span>
<span id="cb25-75">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.key =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#002538"</span>)</span>
<span id="cb25-76">  )</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_desigualdade-de-renda-entre-generos/index_files/figure-html/plot-1.png" class="img-fluid figure-img" width="1152"></p>
</figure>
</div>
</div>
</div>
<p>Among these selected professions, let’s analyze the effect size for doctors and executives (managers and directors).</p>
<section id="income-disparity-among-doctors" class="level2" data-number="3.1">
<h2 data-number="3.1" class="anchored" data-anchor-id="income-disparity-among-doctors"><span class="header-section-number">3.1</span> INCOME DISPARITY AMONG DOCTORS</h2>
<p>First, let’s check the significance of the difference. As expected, it is significant.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating t statistic</span></span>
<span id="cb26-2">calculated_stat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb26-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Médico"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb26-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">specify</span>(rem_media <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> sexo) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb26-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calculate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</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;">"man"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"woman"</span>))</span>
<span id="cb26-6"></span>
<span id="cb26-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># generating null distribution</span></span>
<span id="cb26-8">null_dist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb26-9">  <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>(profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Médico"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb26-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">specify</span>(rem_media <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> sexo) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb26-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hypothesise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">null =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"independence"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb26-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"permute"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb26-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calculate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</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;">"man"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"woman"</span>))</span>
<span id="cb26-14"></span>
<span id="cb26-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating confidence interval</span></span>
<span id="cb26-16">percentile_ci <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_ci</span>(null_dist)</span>
<span id="cb26-17"></span>
<span id="cb26-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualizing null distribution and test statistic</span></span>
<span id="cb26-19">null_dist <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb26-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">visualize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"both"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shade_p_value</span>(calculated_stat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"greater"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shade_confidence_interval</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">endpoints =</span> percentile_ci)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_desigualdade-de-renda-entre-generos/index_files/figure-html/doctors-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating p-value</span></span>
<span id="cb27-2">null_dist <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb27-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_p_value</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">obs_stat =</span> calculated_stat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"greater"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 1
  p_value
    &lt;dbl&gt;
1       0</code></pre>
</div>
</div>
<p>Next, let’s calculate the effect size.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating standardized difference</span></span>
<span id="cb29-2"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cohens_d</span>(</span>
<span id="cb29-3">   data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sexo <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;">"man"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb29-4">     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Médico"</span>), ]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rem_media,</span>
<span id="cb29-5">   data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sexo <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;">"woman"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb29-6">     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Médico"</span>), ]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rem_media</span>
<span id="cb29-7"> )</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Cohen's d |       95% CI
------------------------
0.12      | [0.06, 0.19]

- Estimated using pooled SD.</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># interpreting d statistic</span></span>
<span id="cb31-2"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">interpret_cohens_d</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.12</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gignac2016"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "very small"
(Rules: gignac2016)</code></pre>
</div>
</div>
<p>With a <em>d</em> statistic of 0.12, we have a <em>very small</em> effect on the Gignac &amp; Szodorai (2016) scale. This means that 54% of female doctors in Vitória would have income below the average male doctor in the city, while 46% would earn more than the average male doctor.</p>
</section>
<section id="income-disparity-in-executive-positions" class="level2" data-number="3.2">
<h2 data-number="3.2" class="anchored" data-anchor-id="income-disparity-in-executive-positions"><span class="header-section-number">3.2</span> INCOME DISPARITY IN EXECUTIVE POSITIONS</h2>
<p>Repeating the same procedure for people in executive positions, we see that the difference is also significant.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating t statistic</span></span>
<span id="cb33-2">calculated_stat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb33-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dirigentes|Diretor"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb33-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">specify</span>(rem_media <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> sexo) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb33-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calculate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</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;">"man"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"woman"</span>))</span>
<span id="cb33-6"></span>
<span id="cb33-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># generating null distribution</span></span>
<span id="cb33-8">null_dist <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="cb33-9">  <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>(profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dirigentes|Diretor"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb33-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">specify</span>(rem_media <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> sexo) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb33-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hypothesise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">null =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"independence"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb33-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"permute"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb33-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calculate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order =</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;">"man"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"woman"</span>))</span>
<span id="cb33-14"></span>
<span id="cb33-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating confidence interval</span></span>
<span id="cb33-16">percentile_ci <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;">get_ci</span>(null_dist)</span>
<span id="cb33-17"></span>
<span id="cb33-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualizing null distribution and test statistic</span></span>
<span id="cb33-19">null_dist <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb33-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">visualize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"both"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shade_p_value</span>(calculated_stat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direction =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"greater"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shade_confidence_interval</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">endpoints =</span> percentile_ci)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_desigualdade-de-renda-entre-generos/index_files/figure-html/executives-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>And calculating the effect size:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating standardized difference</span></span>
<span id="cb34-2"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cohens_d</span>(</span>
<span id="cb34-3">   data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sexo <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;">"man"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb34-4">     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dirigentes|Diretor"</span>), ]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rem_media,</span>
<span id="cb34-5">   data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sexo <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;">"woman"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span></span>
<span id="cb34-6">     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>profissao, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dirigentes|Diretor"</span>), ]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rem_media</span>
<span id="cb34-7"> )</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Cohen's d |       95% CI
------------------------
0.47      | [0.30, 0.63]

- Estimated using pooled SD.</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># interpreting d statistic</span></span>
<span id="cb36-2"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">interpret_cohens_d</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.47</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gignac2016"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "moderate"
(Rules: gignac2016)</code></pre>
</div>
</div>
<p>With a <em>d</em> statistic of 0.47, we have a <em>moderate</em> effect on the Gignac &amp; Szodorai (2016) scale. This means that 69% of women in executive and director positions would earn less than the average man in the same position.</p>
</section>
</section>
<section id="final-remarks" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> FINAL REMARKS</h1>
<p>The Gignac &amp; Szodorai scale is just one rule of thumb among others—besides it, two widely used rules are Cohen’s (1988) and Sawilowsky’s (2009). It may be that for the nature of this problem, the interpretation suggested by these rules is not reasonable. In them, a <em>d</em> statistic of 0.47 is considered small or moderate. However, when it comes to gender pay disparities, we can argue that a rate of 69% of women earning less than the average man is a striking result, justifying a specific scale.</p>
<p>In any case, the inequality is clear and there are still several variables to be explored in this context—if we add race, what will the behavior be?</p>
<p><em>If you made it this far and have suggestions for variables, interpretation scales, or other professions to analyze, leave your comment and we’ll update with an edit!</em></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 chose 2017 because the most recent data is grouped with Minas Gerais and Rio de Janeiro, making it too heavy for a casual analysis.↩︎</p></li>
<li id="fn2"><p>Comprehensive R Archive Network.↩︎</p></li>
<li id="fn3"><p>There are several studies on this, see <a href="https://rev-inv-ope.univ-paris1.fr/fileadmin/rev-inv-ope/files/40119/40119-10.pdf">this example</a>.↩︎</p></li>
<li id="fn4"><p>The effect size interpretation table can be found <a href="https://www.leeds.ac.uk/educol/documents/00002182.htm">in this article</a>.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>statistics</category>
  <guid>https://datamares.netlify.app/posts/202011_desigualdade-de-renda-entre-generos/</guid>
  <pubDate>Tue, 24 Nov 2020 03:00:00 GMT</pubDate>
  <media:content url="https://datamares.netlify.app/posts/202011_desigualdade-de-renda-entre-generos/img/gender.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Comparing Variances: The F Test</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202011_comparando-variancias/</link>
  <description><![CDATA[ 






<p>Before I dive into <em>machine learning</em>—because <del>it’s a bottomless pit</del> I should take my time when I do—I want to cover a bit more of the basics in inference.</p>
<p><a href="https://datamares.netlify.app/post/atestando-diferencas-em-medias-o-teste-t-para-amostras-independentes/">In this post</a> I mentioned that to perform the t-test for two independent samples, we should first know whether the variances of these samples are equal or different. Let’s see how to check this now. The dataset used will be the <a href="https://www.kaggle.com/uciml/german-credit">German credit data</a>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># importing data</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># note: -1 removes the first column, which is just the index</span></span>
<span id="cb1-3">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> 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;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"german_credit_data.csv"</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></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>New names:
Rows: 1000 Columns: 10
── Column specification
──────────────────────────────────────────────────────── Delimiter: "," chr
(5): Sex, Housing, Saving accounts, Checking account, Purpose dbl (5): ...1,
Age, Job, Credit amount, Duration
ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
Specify the column types or set `show_col_types = FALSE` to quiet this message.
• `` -&gt; `...1`</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># viewing data</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(data)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1,000 × 9
     Age Sex      Job Housing `Saving accounts` `Checking account`
   &lt;dbl&gt; &lt;chr&gt;  &lt;dbl&gt; &lt;chr&gt;   &lt;chr&gt;             &lt;chr&gt;             
 1    67 male       2 own     &lt;NA&gt;              little            
 2    22 female     2 own     little            moderate          
 3    49 male       1 own     little            &lt;NA&gt;              
 4    45 male       2 free    little            little            
 5    53 male       2 free    little            little            
 6    35 male       1 free    &lt;NA&gt;              &lt;NA&gt;              
 7    53 male       2 own     quite rich        &lt;NA&gt;              
 8    35 male       3 rent    little            moderate          
 9    61 male       1 own     rich              &lt;NA&gt;              
10    28 male       3 own     little            moderate          
# ℹ 990 more rows
# ℹ 3 more variables: `Credit amount` &lt;dbl&gt;, Duration &lt;dbl&gt;, Purpose &lt;chr&gt;</code></pre>
</div>
</div>
<p>Just like the t-test, we can test whether the measure of a sample is significantly different from a chosen value or compare it to another sample—whether greater, smaller, or different. For this exercise, let’s test whether the variance of the <code>Credit amount</code> variable (credit limit) is the same for men and women who rent their homes. First, let’s calculate the population standard deviations:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># getting samples</span></span>
<span id="cb5-2">men <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</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;">"male"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Housing <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;">"rent"</span>,]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Credit amount</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb5-3">women <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</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;">"female"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Housing <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;">"rent"</span>,]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Credit amount</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb5-4"></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating standard deviation</span></span>
<span id="cb5-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(men)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2846.647</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(women)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2235.225</code></pre>
</div>
</div>
<p>We see that men’s credit limit has a standard deviation of DM$ 2,846, while women’s is DM$ 2,235<sup>1</sup>, which means men’s credit limits vary more around the mean than women’s. What we want to know now is whether this difference is statistically significant. Let’s proceed to the test!</p>
<section id="the-test" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> THE TEST</h1>
<p>The F test, among its various applications, is used together with the two-sample t-test—when it’s necessary to know whether the two sampled populations have the same variance or not.</p>
<p>It is also a parametric test, which means it assumes the populations are approximately normally distributed. Therefore, we must first ensure this assumption is met.</p>
<section id="checking-the-normality-assumption" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="checking-the-normality-assumption"><span class="header-section-number">1.1</span> CHECKING THE NORMALITY ASSUMPTION</h2>
<p>First, let’s plot the densities to check if their distribution is plausible under the normality assumption:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># density data</span></span>
<span id="cb9-2">d1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">density</span>(men)</span>
<span id="cb9-3">d2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">density</span>(women)</span>
<span id="cb9-4"></span>
<span id="cb9-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># splitting the grid into 2 columns</span></span>
<span id="cb9-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</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>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb9-7"></span>
<span id="cb9-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualization</span></span>
<span id="cb9-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(d1,</span>
<span id="cb9-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density Plot: men"</span>)</span>
<span id="cb9-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">polygon</span>(d1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>)</span>
<span id="cb9-12"></span>
<span id="cb9-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(d2,</span>
<span id="cb9-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density Plot: women"</span>)</span>
<span id="cb9-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">polygon</span>(d2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"salmon"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_comparando-variancias/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>With this shape, normality is quite implausible and there’s no need to perform any tests. To address this, we can try a logarithmic transformation:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># logarithmic transformation</span></span>
<span id="cb10-2">log_men <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(men)</span>
<span id="cb10-3">log_women <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(women)</span>
<span id="cb10-4"></span>
<span id="cb10-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating variance after transformation</span></span>
<span id="cb10-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">var</span>(log_men)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.5614271</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">var</span>(log_women)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.5229548</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># density data</span></span>
<span id="cb14-2">d3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">density</span>(log_men)</span>
<span id="cb14-3">d4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">density</span>(log_women)</span>
<span id="cb14-4"></span>
<span id="cb14-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># splitting the grid into 2 columns</span></span>
<span id="cb14-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</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>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb14-7"></span>
<span id="cb14-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualization</span></span>
<span id="cb14-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(d3,</span>
<span id="cb14-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density Plot: log(men)"</span>)</span>
<span id="cb14-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">polygon</span>(d3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>)</span>
<span id="cb14-12"></span>
<span id="cb14-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(d4,</span>
<span id="cb14-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density Plot: log(women)"</span>)</span>
<span id="cb14-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">polygon</span>(d4, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"salmon"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_comparando-variancias/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The data now seem to follow a distribution close to normal. To check, we could perform a normality test, but since that’s not the topic here, we’ll explore it in another post. For now, let’s just note that the transformation was successful and the data now appear approximately normal.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># normality test</span></span>
<span id="cb15-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shapiro.test</span>(log_men)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    Shapiro-Wilk normality test

data:  log_men
W = 0.98624, p-value = 0.5147</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shapiro.test</span>(log_women)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    Shapiro-Wilk normality test

data:  log_women
W = 0.98171, p-value = 0.2071</code></pre>
</div>
</div>
</section>
<section id="the-hypotheses" class="level2" data-number="1.2">
<h2 data-number="1.2" class="anchored" data-anchor-id="the-hypotheses"><span class="header-section-number">1.2</span> THE HYPOTHESES</h2>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0AH_0:%20%5Csigma_1%20=%20%5Csigma_2%20%5C%5C%0AH_1:%20%5Csigma_1%20%5Cneq%20%5Csigma_2%0A%5Cend%7Bcases%7D%0A"></p>
<p>The null hypothesis is that we cannot infer, at a certain significance level, that the variances are different. The alternative hypothesis is that they are significantly different.</p>
</section>
<section id="significance-level" class="level2" data-number="1.3">
<h2 data-number="1.3" class="anchored" data-anchor-id="significance-level"><span class="header-section-number">1.3</span> SIGNIFICANCE LEVEL</h2>
<p><img src="https://latex.codecogs.com/png.latex?%20%5Calpha%20=%200.05%20"></p>
<p>We’ll use a standard significance level of 5%, which means the probability of rejecting the null hypothesis when it shouldn’t be rejected is only 5%. The lower this probability, the greater the difference between the variances must be for us to claim a significant difference.</p>
</section>
<section id="test-statistic" class="level2" data-number="1.4">
<h2 data-number="1.4" class="anchored" data-anchor-id="test-statistic"><span class="header-section-number">1.4</span> TEST STATISTIC</h2>
<p><img src="https://latex.codecogs.com/png.latex?%20F%20=%20%5Cfrac%7Bs%5E2_1%7D%7Bs%5E2_2%7D%20"></p>
<p>Since the test statistic is the ratio of the sample variances, the test checks whether this ratio is different from one. To check the tabulated statistic, we need to know the degrees of freedom in the samples:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># degrees of freedom (n-1)</span></span>
<span id="cb19-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Housing <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;">"rent"</span>,]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Sex)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
female   male 
    95     84 </code></pre>
</div>
</div>
<p>And then the tabulated statistic will be:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># F-statistic for the 95th percentile</span></span>
<span id="cb21-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qf</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">83</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">94</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.419123</code></pre>
</div>
</div>
</section>
<section id="critical-value" class="level2" data-number="1.5">
<h2 data-number="1.5" class="anchored" data-anchor-id="critical-value"><span class="header-section-number">1.5</span> CRITICAL VALUE</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating critical value</span></span>
<span id="cb23-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">var</span>(log_men) <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;">var</span>(log_women)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.073567</code></pre>
</div>
</div>
<p><img src="https://latex.codecogs.com/png.latex?%20F%20=%20%5Cfrac%7Bs%5E2_1%7D%7Bs%5E2_2%7D%20=%201.07%20"></p>
</section>
<section id="decision" class="level2" data-number="1.6">
<h2 data-number="1.6" class="anchored" data-anchor-id="decision"><span class="header-section-number">1.6</span> DECISION</h2>
<p>Since the value 1.07 does not exceed 1.42, we cannot reject the null hypothesis at the 5% significance level. The variances are not significantly different.</p>
</section>
</section>
<section id="the-f-test-in-r" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> THE F TEST IN R</h1>
<p>In base R, the syntax for the test is very similar to the t-test:</p>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>
    F test to compare two variances

data:  log_men and log_women
F = 1.0736, num df = 83, denom df = 94, p-value = 0.7362
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
 0.707196 1.638814
sample estimates:
ratio of variances 
          1.073567 </code></pre>
</div>
</div>
<p>The test summary tells us that to reject the null hypothesis at <img src="https://latex.codecogs.com/png.latex?%5Calpha"> = 5%, the ratio should be around 1.64 (variance of <code>log_men</code> greater than <code>log_women</code>) or 0.70 (variance of <code>log_men</code> less than <code>log_women</code>). Alternatively, we could reject the null hypothesis if we increased <img src="https://latex.codecogs.com/png.latex?%5Calpha"> to 1-0.7362 = 26.38%, which is a probability of making a type II error too high to be considered reasonable.</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>Deutsche Marks.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>statistics</category>
  <guid>https://datamares.netlify.app/posts/202011_comparando-variancias/</guid>
  <pubDate>Fri, 20 Nov 2020 03:00:00 GMT</pubDate>
  <media:content url="https://datamares.netlify.app/posts/202011_comparando-variancias/variancia.png" medium="image" type="image/png" height="72" width="144"/>
</item>
<item>
  <title>Guessed or not? The one-sample t-test</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202011_chutou-ou-nao/</link>
  <description><![CDATA[ 






<p>In the <a href="https://datamares.netlify.app/posts/202011_t-test/">previous post</a>, I talked about the t-test for two independent samples. Coincidentally, the next day, <a href="https://forumdematematica.org/viewtopic.php?f=69&amp;t=14319&amp;sid=88c3f5cb4248d2096eb36656bbb95c45">this question</a> appeared on the <a href="https://forumdematematica.org/">math forum</a> where I contribute. We can’t miss this opportunity, can we?</p>
<section id="the-problem" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> THE PROBLEM</h1>
<blockquote class="blockquote">
<p>Consider a multiple-choice question with four possible answers. The question was designed to be very difficult, with none of the four answers considered wrong, but with only one correct answer. A test was conducted with 400 students. The test aims to verify whether more people answer the question correctly than would be expected just by chance (i.e., if everyone guessed the correct answer by pure luck).</p>
<p>Perform the hypothesis test (following the 5 steps), knowing that out of 400 students, 125 answered the question correctly. Use α = 2%.</p>
</blockquote>
<p>I believe the biggest challenge here for beginner students is to understand the problem and structure it in a way that can be answered assertively. The key is in the following excerpt:</p>
<blockquote class="blockquote">
<p>than would be expected just by chance (i.e., if everyone guessed the correct answer by pure luck).</p>
</blockquote>
<p>The probability of choosing the correct answer among 4 options by chance is 25%. However, 125 out of 400 students got the question right, that is, 31.25%. The problem then is to infer whether the mean of 31.25% is significantly different from the expected mean, 25%, given the sample size and its variance. This can be achieved with the <em>one-sample t-test</em>.</p>
</section>
<section id="a-bit-of-simulation" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> A BIT OF SIMULATION</h1>
<p>Before we get into the test itself, we know it is quite simple to calculate but its concept is not always so easily assimilated. To help, let’s simulate the data and try to visually represent what we are going to do.</p>
<p>Since these are binary data (correct or incorrect), we will use 1 for correct and 0 for incorrect.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># creating sample</span></span>
<span id="cb1-2">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">125</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</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;">275</span>))</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># to get the same random numbers as the example</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># randomizing</span></span>
<span id="cb1-8">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(data)</span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># checking</span></span>
<span id="cb1-11">data</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  [1] 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0 0 0 0 1 1 1 1 0 1 1 1 0 1
 [38] 0 0 1 0 0 0 1 0 1 1 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0
 [75] 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 1 0 1 0 1 0 1 1
[112] 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 0 1 0 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0
[149] 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 1 0
[186] 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 1 1 0 0 1 1 1 0 0 1 0
[223] 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1
[260] 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 0 0 0 0 1
[297] 0 0 0 1 0 0 0 1 1 0 0 0 1 1 0 1 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1
[334] 1 1 0 0 1 1 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 1 1 0 1 1 0 1
[371] 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 1 0 0 1 0 0 1 1 0</code></pre>
</div>
</div>
<p>Now that we have our vector with the random data of students who got the questions right and wrong, let’s visualize it:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># bar chart</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mar =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</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;">0</span>))</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">barplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(data), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</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;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"salmon"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_chutou-ou-nao/index_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The intuition of the t-test is to say whether this difference between correct and incorrect answers occurs by mere chance in the sampling process or not. But what does that mean? In this case, the sample mean was 31.25% with 400 students. The characteristics of the distribution, the standard deviation, and the sample size allow us to attest whether the difference between the sample mean and the expected mean is significant?</p>
<p>To illustrate the reasoning, let’s take 10 samples of 10 students randomly and observe the behavior of the means. These samples of 10 students may have a mean of correct answers (red line) very close to the expected 25% (blue line) and far from the mean of the 400 students (green line).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ensure reproducibility</span></span>
<span id="cb4-2"><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;">3</span>)</span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of samples</span></span>
<span id="cb4-5">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># means</span></span>
<span id="cb4-8">means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, n)</span>
<span id="cb4-9"></span>
<span id="cb4-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># taking samples and calculating means</span></span>
<span id="cb4-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n) {</span>
<span id="cb4-12">  means[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(</span>
<span id="cb4-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</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="cb4-14">}</span>
<span id="cb4-15"></span>
<span id="cb4-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualization</span></span>
<span id="cb4-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(means,</span>
<span id="cb4-18">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1st draw: 10 samples of 10 students"</span>,</span>
<span id="cb4-19">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>,</span>
<span id="cb4-20">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sub =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"seed = 3"</span>)</span>
<span id="cb4-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(means, <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb4-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)</span>
<span id="cb4-23"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3125</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>)</span>
<span id="cb4-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb4-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hat</span>(mu)),</span>
<span id="cb4-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(mu, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"= 0.25"</span>)),</span>
<span id="cb4-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(mu, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"= 0.3325"</span>))),</span>
<span id="cb4-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</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;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>),</span>
<span id="cb4-29">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_chutou-ou-nao/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>But it is also possible to take another draw of 10 samples with 10 different students and find a completely different mean, close to 45%:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ensure reproducibility</span></span>
<span id="cb5-2"><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;">4</span>)</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of samples</span></span>
<span id="cb5-5">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb5-6"></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># means</span></span>
<span id="cb5-8">means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, n)</span>
<span id="cb5-9"></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># taking samples and calculating means</span></span>
<span id="cb5-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n) {</span>
<span id="cb5-12">  means[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(</span>
<span id="cb5-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</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="cb5-14">}</span>
<span id="cb5-15"></span>
<span id="cb5-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualization</span></span>
<span id="cb5-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(means,</span>
<span id="cb5-18">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2nd draw: 10 samples of 10 students"</span>,</span>
<span id="cb5-19">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>,</span>
<span id="cb5-20">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sub =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"seed = 4"</span>)</span>
<span id="cb5-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(means, <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb5-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)</span>
<span id="cb5-23"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3125</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>)</span>
<span id="cb5-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb5-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hat</span>(mu)),</span>
<span id="cb5-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(mu, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"= 0.25"</span>)),</span>
<span id="cb5-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(mu, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"= 0.3325"</span>))),</span>
<span id="cb5-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</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;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>),</span>
<span id="cb5-29">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_chutou-ou-nao/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>As we increase the number of samples, the standard deviation stabilizes and the sample mean approaches the population mean. See the means of 50 samples of 10 students each:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ensure reproducibility</span></span>
<span id="cb6-2"><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;">3</span>)</span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of samples</span></span>
<span id="cb6-5">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb6-6"></span>
<span id="cb6-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># means</span></span>
<span id="cb6-8">means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, n)</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># taking samples and calculating means</span></span>
<span id="cb6-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n) {</span>
<span id="cb6-12">  means[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(</span>
<span id="cb6-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</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="cb6-14">}</span>
<span id="cb6-15"></span>
<span id="cb6-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># separating the grid into 2 columns</span></span>
<span id="cb6-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</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>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb6-18"></span>
<span id="cb6-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualization 1st draw</span></span>
<span id="cb6-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(means,</span>
<span id="cb6-21">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1st draw: 50 samples of 10 students"</span>,</span>
<span id="cb6-22">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>,</span>
<span id="cb6-23">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sub =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"seed = 3"</span>)</span>
<span id="cb6-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(means, <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb6-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)</span>
<span id="cb6-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3125</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>)</span>
<span id="cb6-27"></span>
<span id="cb6-28"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################################################################################</span></span>
<span id="cb6-29"></span>
<span id="cb6-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ensure reproducibility</span></span>
<span id="cb6-31"><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;">4</span>)</span>
<span id="cb6-32"></span>
<span id="cb6-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of samples</span></span>
<span id="cb6-34">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb6-35"></span>
<span id="cb6-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># means</span></span>
<span id="cb6-37">means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, n)</span>
<span id="cb6-38"></span>
<span id="cb6-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># taking samples and calculating means</span></span>
<span id="cb6-40"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n) {</span>
<span id="cb6-41">  means[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(</span>
<span id="cb6-42">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max =</span> 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="cb6-43">}</span>
<span id="cb6-44"></span>
<span id="cb6-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualization 2nd draw</span></span>
<span id="cb6-46"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(means,</span>
<span id="cb6-47">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2nd draw: 50 samples of 10 students"</span>,</span>
<span id="cb6-48">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>,</span>
<span id="cb6-49">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sub =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"seed = 4"</span>)</span>
<span id="cb6-50"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(means, <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb6-51"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)</span>
<span id="cb6-52"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3125</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>)</span>
<span id="cb6-53"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb6-54">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hat</span>(mu)),</span>
<span id="cb6-55">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(mu, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"= 0.25"</span>)),</span>
<span id="cb6-56">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(mu, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"= 0.3325"</span>))),</span>
<span id="cb6-57">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</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;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>),</span>
<span id="cb6-58">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb6-59">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_chutou-ou-nao/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" style="width:80.0%"></p>
</figure>
</div>
</div>
</div>
<p>It can be observed that taking two draws of 50 samples of 10 students each, the means begin to converge to the mean observed in the total of 400 students.</p>
<p>And if we increase to 100 samples of 10 students?</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ensure reproducibility</span></span>
<span id="cb7-2"><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;">3</span>)</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of samples</span></span>
<span id="cb7-5">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb7-6"></span>
<span id="cb7-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># means</span></span>
<span id="cb7-8">means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, n)</span>
<span id="cb7-9"></span>
<span id="cb7-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># taking samples and calculating means</span></span>
<span id="cb7-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n) {</span>
<span id="cb7-12">  means[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(</span>
<span id="cb7-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</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="cb7-14">}</span>
<span id="cb7-15"></span>
<span id="cb7-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># separating the grid into 2 columns</span></span>
<span id="cb7-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</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>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb7-18"></span>
<span id="cb7-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualization 1st draw</span></span>
<span id="cb7-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(means,</span>
<span id="cb7-21">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1st draw: 100 samples of 10 students"</span>,</span>
<span id="cb7-22">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>,</span>
<span id="cb7-23">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sub =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"seed = 3"</span>)</span>
<span id="cb7-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(means, <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb7-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)</span>
<span id="cb7-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3125</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>)</span>
<span id="cb7-27"></span>
<span id="cb7-28"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################################################################################</span></span>
<span id="cb7-29"></span>
<span id="cb7-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ensure reproducibility</span></span>
<span id="cb7-31"><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;">4</span>)</span>
<span id="cb7-32"></span>
<span id="cb7-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># means</span></span>
<span id="cb7-34">means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, n)</span>
<span id="cb7-35"></span>
<span id="cb7-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># taking samples and calculating means</span></span>
<span id="cb7-37"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n) {</span>
<span id="cb7-38">  means[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(</span>
<span id="cb7-39">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</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="cb7-40">}</span>
<span id="cb7-41"></span>
<span id="cb7-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># visualization 2nd draw</span></span>
<span id="cb7-43"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(means,</span>
<span id="cb7-44">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2nd draw: 100 samples of 10 students"</span>,</span>
<span id="cb7-45">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>,</span>
<span id="cb7-46">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sub =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"seed = 4"</span>)</span>
<span id="cb7-47"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(means, <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span>
<span id="cb7-48"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)</span>
<span id="cb7-49"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3125</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>)</span>
<span id="cb7-50"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb7-51">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hat</span>(mu)),</span>
<span id="cb7-52">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(mu, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"= 0.25"</span>)),</span>
<span id="cb7-53">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(mu, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"= 0.3325"</span>))),</span>
<span id="cb7-54">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</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;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>),</span>
<span id="cb7-55">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb7-56">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202011_chutou-ou-nao/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Notice that with 100 random samples, the means are already very close to the observed 33.25% in both draws. This happens because as the number of observations increases, the standard deviation tends to stabilize and the uncertainty decreases. It would be very difficult for the first 400<sup>1</sup> students to have a certain mean of correct answers and the next 50 students all get it right or wrong in a way that would generate changes in the standard deviation and significantly affect the mean. That is why, ultimately, <strong>the t-test is a sample size test</strong>. The question behind everything is: is my sample large enough for the difference to be significant?</p>
<p>Now, with the intuition of the test in mind, let’s formalize it.</p>
</section>
<section id="the-test" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> THE TEST</h1>
<p>To make the process very transparent and reinforce the concepts, it is always recommended to use the 5-step hypothesis testing framework, which are:</p>
<section id="state-the-null-and-alternative-hypotheses" class="level2" data-number="3.1">
<h2 data-number="3.1" class="anchored" data-anchor-id="state-the-null-and-alternative-hypotheses"><span class="header-section-number">3.1</span> State the null and alternative hypotheses</h2>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bcases%7D%0A%20%20%20%20%20%20H_0:%20%5Cmu%20=%200.25%20%5C%5C%0A%20%20%20%20%20%20H_1:%20%5Cmu%20%5Cneq%200.25%20%5C%5C%0A%20%20%20%20%5Cend%7Bcases%7D"></p>
<p>The null hypothesis is that we cannot state that the observed mean of correct answers is significantly different from the expected mean. The alternative hypothesis is that they are significantly different.</p>
</section>
<section id="state-the-significance-level" class="level2" data-number="3.2">
<h2 data-number="3.2" class="anchored" data-anchor-id="state-the-significance-level"><span class="header-section-number">3.2</span> State the significance level</h2>
<p><img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%200.02"> The <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%200.02"> is what gives meaning to the term <strong>significantly different</strong>. It is the probability of making a type II error, that is, rejecting the null hypothesis when it should not be rejected. The lower the <img src="https://latex.codecogs.com/png.latex?%5Calpha">, the greater the difference between the means must be for it to be considered significant.</p>
</section>
<section id="calculate-the-test-statistic" class="level2" data-number="3.3">
<h2 data-number="3.3" class="anchored" data-anchor-id="calculate-the-test-statistic"><span class="header-section-number">3.3</span> Calculate the test statistic</h2>
<p><img src="https://latex.codecogs.com/png.latex?z%20=%20%5Cfrac%7B%5Cbar%7Bx%7D%20-%20%5Cmu_0%7D%7B%5Cfrac%7B%5Csigma%7D%7B%5Csqrt%7Bn%7D%7D%7D"> Note that: <img src="https://latex.codecogs.com/png.latex?%5Clim_%7Bn%20%5Cto%20%5Cinfty%7Dz(n)%20=%20%5Cinfty"> We can reject the null hypothesis if the critical <img src="https://latex.codecogs.com/png.latex?z"> is greater than the tabulated <img src="https://latex.codecogs.com/png.latex?z">. As <img src="https://latex.codecogs.com/png.latex?n"> increases, eventually the difference will be significant, demonstrating mathematically what we verified intuitively.</p>
</section>
<section id="calculate-the-critical-value" class="level2" data-number="3.4">
<h2 data-number="3.4" class="anchored" data-anchor-id="calculate-the-critical-value"><span class="header-section-number">3.4</span> Calculate the critical value</h2>
<p><img src="https://latex.codecogs.com/png.latex?z%20=%20%5Cfrac%7B0.3125%20-%200.25%7D%7B%5Cfrac%7B0.464%7D%7B%5Csqrt%7B400%7D%7D%7D%20=%202.693"></p>
</section>
<section id="decide-whether-to-reject-the-null-hypothesis" class="level2" data-number="3.5">
<h2 data-number="3.5" class="anchored" data-anchor-id="decide-whether-to-reject-the-null-hypothesis"><span class="header-section-number">3.5</span> Decide whether to reject the null hypothesis</h2>
<p>Since the value 2.693 exceeds the <img src="https://latex.codecogs.com/png.latex?z"> value at 98% significance (2.33), we can reject the null hypothesis. The difference is significant and cannot be attributed to sampling chance.</p>
</section>
<section id="ok-but-what-about-in-r" class="level2" data-number="3.6">
<h2 data-number="3.6" class="anchored" data-anchor-id="ok-but-what-about-in-r"><span class="header-section-number">3.6</span> Ok, but what about in R?</h2>
<p>In R, the test couldn’t be simpler:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf.level =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.98</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    One Sample t-test

data:  data
t = 2.6934, df = 399, p-value = 0.00737
alternative hypothesis: true mean is not equal to 0.25
98 percent confidence interval:
 0.2583002 0.3666998
sample estimates:
mean of x 
   0.3125 </code></pre>
</div>
</div>
<p>Note that 0.25 is not in the confidence interval, so we can reject <img src="https://latex.codecogs.com/png.latex?H_0">. As an illustration, so that we could not reject the null hypothesis, we would have to increase the significance level to 1 - p-value, that is, to 99.263%:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf.level =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99263</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    One Sample t-test

data:  data
t = 2.6934, df = 399, p-value = 0.00737
alternative hypothesis: true mean is not equal to 0.25
99.263 percent confidence interval:
 0.2499995 0.3750005
sample estimates:
mean of x 
   0.3125 </code></pre>
</div>
</div>
<p>Or increase the mean to 25.83%:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2583</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf.level =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.98</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    One Sample t-test

data:  data
t = 2.3357, df = 399, p-value = 0.02
alternative hypothesis: true mean is not equal to 0.2583
98 percent confidence interval:
 0.2583002 0.3666998
sample estimates:
mean of x 
   0.3125 </code></pre>
</div>
</div>
<p>Much easier than doing it by hand, right?</p>
</section>
</section>
<section id="conclusion" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> CONCLUSION</h1>
<p>Now that we have all the tools, we can answer the student’s question from the forum. The teacher, yes, can be proud, because we can reject the hypothesis that the students’ mean correct answer rate was just luck. Congratulations to the class! :p</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>Or a thousand, 10 thousand, 100 thousand… The larger <img src="https://latex.codecogs.com/png.latex?n"> is, the harder it is to cause changes in the standard deviation, so the uncertainty is increasingly smaller.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>statistics</category>
  <guid>https://datamares.netlify.app/posts/202011_chutou-ou-nao/</guid>
  <pubDate>Fri, 06 Nov 2020 03:00:00 GMT</pubDate>
  <media:content url="https://datamares.netlify.app/posts/202011_chutou-ou-nao/img/statistics.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Testing differences in means: the t-test for independent samples</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202011_t-test/</link>
  <description><![CDATA[ 






<p>Suppose you have two samples (e.g., the incomes of the Black and White populations in your city) and you want to prove that their means are <em>significantly</em> different, that is, that they are different even considering the variance and the sample size. This is possible with a Student’s t-test, one of the most popular tests in statistics.</p>
<p>Let’s use one of R’s built-in datasets to apply this concept, <code>mtcars</code>. First, let’s take a look at our data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> mtcars</span>
<span id="cb1-2">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), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">booktabs =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-3">  kableExtra<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_styling</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">latex_options =</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;">"striped"</span>))</span></code></pre></div></div>
<div id="tbl-dados" class="cell quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-dados-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: Dataset.
</figcaption>
<div aria-describedby="tbl-dados-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="table do-not-create-environment cell caption-top table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">mpg</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">cyl</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">disp</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">hp</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">drat</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">wt</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">qsec</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">vs</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">am</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">gear</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">carb</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Mazda RX4</td>
<td style="text-align: right;">21.0</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">160</td>
<td style="text-align: right;">110</td>
<td style="text-align: right;">3.90</td>
<td style="text-align: right;">2.62</td>
<td style="text-align: right;">16.46</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">4</td>
</tr>
<tr class="even">
<td style="text-align: left;">Mazda RX4 Wag</td>
<td style="text-align: right;">21.0</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">160</td>
<td style="text-align: right;">110</td>
<td style="text-align: right;">3.90</td>
<td style="text-align: right;">2.88</td>
<td style="text-align: right;">17.02</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">4</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Datsun 710</td>
<td style="text-align: right;">22.8</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">108</td>
<td style="text-align: right;">93</td>
<td style="text-align: right;">3.85</td>
<td style="text-align: right;">2.32</td>
<td style="text-align: right;">18.61</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="even">
<td style="text-align: left;">Hornet 4 Drive</td>
<td style="text-align: right;">21.4</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">258</td>
<td style="text-align: right;">110</td>
<td style="text-align: right;">3.08</td>
<td style="text-align: right;">3.21</td>
<td style="text-align: right;">19.44</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Hornet Sportabout</td>
<td style="text-align: right;">18.7</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">360</td>
<td style="text-align: right;">175</td>
<td style="text-align: right;">3.15</td>
<td style="text-align: right;">3.44</td>
<td style="text-align: right;">17.02</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">2</td>
</tr>
<tr class="even">
<td style="text-align: left;">Valiant</td>
<td style="text-align: right;">18.1</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">225</td>
<td style="text-align: right;">105</td>
<td style="text-align: right;">2.76</td>
<td style="text-align: right;">3.46</td>
<td style="text-align: right;">20.22</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">1</td>
</tr>
</tbody>
</table>
</div>
</div>
</figure>
</div>
</div>
<p>A good way to illustrate the test is to check whether the mean fuel consumption (<code>mpg</code>, miles per gallon) of cars with 4, 6, and 8 cylinders (<code>cyl</code>) differ significantly from each other.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sample means</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aggregate</span>(mpg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> cyl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> mean)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  cyl      mpg
1   4 26.66364
2   6 19.74286
3   8 15.10000</code></pre>
</div>
</div>
<p>We see that the sample means are different. We still need to know if they are <em>significantly</em> different. Plotting a boxplot can help us get an intuition. We can see that, except for the 4-cylinder group which has a higher variance, the groups are quite concentrated, so we might suspect that the differences are significant.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Boxplot</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">boxplot</span>(mpg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> cyl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-boxplot" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-boxplot-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://datamares.netlify.app/posts/202011_t-test/index_files/figure-html/fig-boxplot-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-boxplot-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Boxplot of fuel consumption by number of cylinders.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The t-test has several variations — one sample, two paired samples, two independent samples — and corrections to handle differences in variance. For this case, we have three independent samples and, for now, let’s assume that the variance of the 4-cylinder group differs from the others and that the variances of the 6- and 8-cylinder groups are equal — we’ll leave variance analysis for another post. This leaves us with the <strong>t-test for two independent samples</strong>.</p>
<p>The null hypothesis of the test is that the means are significantly equal. The alternative hypothesis can be formulated as the non-nullity of the difference between the means or <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX_1%7D"> greater or less than <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX_2%7D">. Here we will use the first option:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ah_0:%20%5Cbar%7BX_1%7D%20-%20%5Cbar%7BX_2%7D%20=%200%20%5C%5C%0Ah_1:%20%5Cbar%7BX_1%7D%20-%20%5Cbar%7BX_2%7D%20%5Cneq%200%0A"> The t statistic for this test is calculated as below. Note that if we take the limit of <img src="https://latex.codecogs.com/png.latex?t(n)">, with <img src="https://latex.codecogs.com/png.latex?n%20%5Crightarrow%20%5Cinfty">, <img src="https://latex.codecogs.com/png.latex?t%5Crightarrow%20%5Cinfty">, causing the rejection of <img src="https://latex.codecogs.com/png.latex?h_0">. Thus, ultimately, <strong>the t-test is a sample size test</strong>, that is, if your sample is large enough and the means diverge, they will also tend to be significantly different.</p>
<p><img src="https://latex.codecogs.com/png.latex?%20t%20=%20%5Cfrac%7B%5Cbar%7BX_1%7D%20-%20%5Cbar%7BX_2%7D%7D%7Bs_p%20.%20%5Csqrt%7B%5Cfrac%7B1%7D%7Bn_1%7D+%5Cfrac%7B1%7D%7Bn_2%7D%7D%7D%20"></p>
<p>As a first case, let’s compare the mean fuel consumption of vehicles with 6 and 8 cylinders. Since we are considering their variances to be equal, we must use the argument <code>var.equal = TRUE</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># t-test for 6 and 8 cylinders</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(mpg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> cyl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cyl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var.equal =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    Two Sample t-test

data:  mpg by cyl
t = 4.419, df = 19, p-value = 0.0002947
alternative hypothesis: true difference in means between group 6 and group 8 is not equal to 0
95 percent confidence interval:
 2.443809 6.841905
sample estimates:
mean in group 6 mean in group 8 
       19.74286        15.10000 </code></pre>
</div>
</div>
<p>With a p-value of zero, we can reject the null hypothesis and consider that the mean fuel consumption between vehicles with 6 and 8 cylinders differs.</p>
<p>For the other comparisons, let’s use the default for <code>var.equal</code>, which is <code>FALSE</code>. This means applying Welch’s correction for independent samples with different variances. As expected, we can also reject the null hypothesis and confirm the difference in mean fuel consumption between vehicles with 4 and 6 cylinders and 4 and 8 cylinders.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># t-test for 4 and 8 cylinders</span></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(mpg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> cyl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cyl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>),])</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    Welch Two Sample t-test

data:  mpg by cyl
t = 7.5967, df = 14.967, p-value = 1.641e-06
alternative hypothesis: true difference in means between group 4 and group 8 is not equal to 0
95 percent confidence interval:
  8.318518 14.808755
sample estimates:
mean in group 4 mean in group 8 
       26.66364        15.10000 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># t-test for 4 and 6 cylinders</span></span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(mpg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> cyl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cyl <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;">8</span>),])</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    Welch Two Sample t-test

data:  mpg by cyl
t = 4.7191, df = 12.956, p-value = 0.0004048
alternative hypothesis: true difference in means between group 4 and group 6 is not equal to 0
95 percent confidence interval:
  3.751376 10.090182
sample estimates:
mean in group 4 mean in group 6 
       26.66364        19.74286 </code></pre>
</div>
</div>
<p><em>Easy peasy lemon squeezy</em>, right?</p>



 ]]></description>
  <category>statistics</category>
  <guid>https://datamares.netlify.app/posts/202011_t-test/</guid>
  <pubDate>Mon, 02 Nov 2020 03:00:00 GMT</pubDate>
  <media:content url="https://datamares.netlify.app/posts/202011_t-test/img/statistics.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Graphic quality in the RStudio panel</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202010_graphics-rstudio/</link>
  <description><![CDATA[ 






<p>If you’ve ever plotted a line chart in R and, when you saw the plot in the RStudio panel, thought “wow, what terrible quality!”, you’re not alone. But don’t worry, the solution is quite simple!</p>
<p>First, let’s plot a chart using RStudio’s default settings:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">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;">include_graphics</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"img/Rplot.png"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-plot1" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-plot1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://datamares.netlify.app/posts/202010_graphics-rstudio/img/Rplot.png" class="img-fluid figure-img" width="411">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-plot1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Plot without anti-aliasing.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Wow, imagine using something like that on a poster! Let’s try again, now with Cairo as the graphics device and using anti-aliasing:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adding anti-aliasing</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trace</span>(grDevices<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>png, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quote</span>({</span>
<span id="cb2-3">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">missing</span>(type) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">missing</span>(antialias)) {</span>
<span id="cb2-4">    type <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;">"cairo-png"</span></span>
<span id="cb2-5">    antialias <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;">"subpixel"</span></span>
<span id="cb2-6">  }</span>
<span id="cb2-7">}), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">print =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Tracing function "png" in package "grDevices"</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "png"</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plotting again</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(mtcars<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mpg, <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;">"l"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-plot2" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-plot2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://datamares.netlify.app/posts/202010_graphics-rstudio/index_files/figure-html/fig-plot2-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-plot2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Plot with anti-aliasing.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Holy anti-aliasing, right? (Zoom in on both to really see the difference)</p>
<p>Well, do I need to do this every time I plot a chart or start an R session? Not at all, just add these lines to your .Rprofile. I always recommend using {usethis} to edit configuration files, as you might get lost among the possible paths R will check. Using {usethis} ensures you’re creating or editing the correct file.</p>
<p><code>usethis::edit_r_profile()</code> will open a window with the file for editing. Then, paste the call above (removing the plot line, of course), make sure your .Rprofile ends with a blank line (because R ignores the last line), save, and restart your session. Done! Now anti-aliasing will always be applied to your R plots, regardless of the package used, whether base or {ggplot}, for example.</p>



 ]]></description>
  <category>tools</category>
  <guid>https://datamares.netlify.app/posts/202010_graphics-rstudio/</guid>
  <pubDate>Mon, 19 Oct 2020 03:00:00 GMT</pubDate>
  <media:content url="https://datamares.netlify.app/posts/202010_graphics-rstudio/img/rstudio.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Configuring git behind a proxy server</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202010_git-proxy/</link>
  <description><![CDATA[ 






<p>If you are working in an organization that takes information security seriously, then you are probably behind a proxy server and having trouble using Git. To solve this, we need to go through 3 steps:</p>
<ol type="1">
<li>Find out the proxy server address</li>
<li>Choose an authentication method</li>
<li>Update git settings</li>
</ol>
<section id="proxy-address" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> PROXY ADDRESS</h1>
<p>The proxy server address is saved in a configuration file that can be obtained via your browser. If you are on Windows, you will have to access the infamous Internet Explorer, go to <code>Internet Options</code>, <code>Connections</code>, and <code>LAN Settings</code>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datamares.netlify.app/posts/202010_git-proxy/img/1.png" class="img-fluid figure-img"></p>
<figcaption>location of the configuration file</figcaption>
</figure>
</div>
<p>Copy and paste the path shown in the <code>Address</code> field into your browser and you will download a .dat file with the proxy settings. Open the file in a reader<sup>1</sup>. In the file, search for “PROXY”, this variable will contain the proxy server address in the format <code>domain.com:port</code>.</p>
</section>
<section id="choose-the-authentication-method" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> CHOOSE THE AUTHENTICATION METHOD</h1>
<p>With the proxy address in hand, we need to choose how we will authenticate:<br>
<br>
1. Enter the password for each push/pull<br>
2. Save the password in .gitconfig<br>
</p>
<p>The first method applies when we are concerned about our password, that is, when the computer is shared or the configuration is saved on a network, for example.</p>
<p>When this is not the case, you can simply save the username and password in the configuration file and push/pull will occur directly.</p>
<section id="gitconfig-without-saving-password" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="gitconfig-without-saving-password"><span class="header-section-number">2.1</span> .gitconfig without saving password</h2>
<p>I recommend using the {usethis} package to change any configuration file in R. For the first option, we would do as follows:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># open the configuration file</span></span>
<span id="cb1-2">usethis<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;">edit_git_config</span>()</span></code></pre></div></div>
</div>
<p>In the .gitconfig window that will open, add the following lines:</p>
<pre><code>[http]
    proxy = http[s]://domain.com:port

[credential]
    helper = wincred

[credential "helperselector"]
    selected = manager
</code></pre>
<p>Where “domain.com” is the proxy address you found in the .dat file and the other settings define how you will be prompted to enter username and password, in this case through a pop-up window for each push/pull.</p>
</section>
<section id="gitconfig-with-saved-password" class="level2" data-number="2.2">
<h2 data-number="2.2" class="anchored" data-anchor-id="gitconfig-with-saved-password"><span class="header-section-number">2.2</span> .gitconfig with saved password</h2>
<p>The other alternative is to save the username and password in the .gitconfig itself. Again, if the file is on a network or if other people have access to the machine, avoid this option. To save your username and password in .gitconfig, just add them before the domain. The advantage of this method is not having to enter the information for each push/pull.</p>
<pre><code>[http]
    proxy = http[s]://user:password@domain.com:port
</code></pre>
<p>Remember to update the password in .gitconfig whenever it is changed!</p>


</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>Avoid Notepad to display line breaks correctly. I suggest Wordpad.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>tools</category>
  <guid>https://datamares.netlify.app/posts/202010_git-proxy/</guid>
  <pubDate>Fri, 25 Sep 2020 03:00:00 GMT</pubDate>
  <media:content url="https://datamares.netlify.app/posts/202010_git-proxy/img/git.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Hello World</title>
  <dc:creator>Alberson Miranda</dc:creator>
  <link>https://datamares.netlify.app/posts/202009_hello-world/</link>
  <description><![CDATA[ 






<p>After a couple of posts on LinkedIn and Kaggle, it was about time for me to set up my own blog for data science, wasn’t it?</p>
<p><em>First things first</em>, what’s with the name? <strong>Datamares &amp; Dreamscapes</strong> refers to the title of a Stephen King short story collection <em>Nightmares &amp; Dreamscapes</em> and quite fittingly describes my relationship with this world. I always want to learn more, go further, conquer new horizons, but learning involves certain sacrifices and sleepless or poorly slept nights. However, when you achieve that expected result, it’s as if a portal opens to a new world.</p>
<p>So, what do I intend to talk about here? Mainly about the R projects I’m working on, machine learning, classical statistics, forecasting, frameworks… and a bit of mathematics, economics, and teaching. Basically ideas, insights, and works that I might need to revisit from time to time.</p>
<p>And if any of these things help you in some way, I’ll be very happy :p</p>



 ]]></description>
  <category>news</category>
  <guid>https://datamares.netlify.app/posts/202009_hello-world/</guid>
  <pubDate>Fri, 11 Sep 2020 03:00:00 GMT</pubDate>
</item>
</channel>
</rss>
