<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Descriptive statistics on Stats and R</title>
    <link>https://statsandr.com/tags/descriptive-statistics/</link>
    <description>Recent content in Descriptive statistics on Stats and R</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Tue, 05 Sep 2023 00:00:00 +0000</lastBuildDate>
    
	<atom:link href="https://statsandr.com/tags/descriptive-statistics/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Pearson, Spearman and Kendall correlation coefficients by hand</title>
      <link>https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/</link>
      <pubDate>Tue, 05 Sep 2023 00:00:00 +0000</pubDate>
      
      <guid>https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/</guid>
      <description>
&lt;script src=&#34;https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/index_files/htmlwidgets/htmlwidgets.js&#34;&gt;&lt;/script&gt;
&lt;link href=&#34;https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/index_files/datatables-css/datatables-crosstalk.css&#34; rel=&#34;stylesheet&#34; /&gt;
&lt;script src=&#34;https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/index_files/datatables-binding/datatables.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/index_files/jquery/jquery-3.6.0.min.js&#34;&gt;&lt;/script&gt;
&lt;link href=&#34;https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/index_files/dt-core/css/jquery.dataTables.min.css&#34; rel=&#34;stylesheet&#34; /&gt;
&lt;link href=&#34;https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/index_files/dt-core/css/jquery.dataTables.extra.css&#34; rel=&#34;stylesheet&#34; /&gt;
&lt;script src=&#34;https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/index_files/dt-core/js/jquery.dataTables.min.js&#34;&gt;&lt;/script&gt;
&lt;link href=&#34;https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/index_files/crosstalk/css/crosstalk.min.css&#34; rel=&#34;stylesheet&#34; /&gt;
&lt;script src=&#34;https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/index_files/crosstalk/js/crosstalk.min.js&#34;&gt;&lt;/script&gt;

&lt;div id=&#34;TOC&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#introduction&#34; id=&#34;toc-introduction&#34;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#data&#34; id=&#34;toc-data&#34;&gt;Data&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#with-ties&#34; id=&#34;toc-with-ties&#34;&gt;With ties&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#without-ties&#34; id=&#34;toc-without-ties&#34;&gt;Without ties&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#correlation-coefficients-by-hand&#34; id=&#34;toc-correlation-coefficients-by-hand&#34;&gt;Correlation coefficients by hand&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#pearson&#34; id=&#34;toc-pearson&#34;&gt;Pearson&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#with-and-without-ties&#34; id=&#34;toc-with-and-without-ties&#34;&gt;With and without ties&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#spearman&#34; id=&#34;toc-spearman&#34;&gt;Spearman&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#with-ties-1&#34; id=&#34;toc-with-ties-1&#34;&gt;With ties&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#without-ties-1&#34; id=&#34;toc-without-ties-1&#34;&gt;Without ties&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#kendall&#34; id=&#34;toc-kendall&#34;&gt;Kendall&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#without-ties-2&#34; id=&#34;toc-without-ties-2&#34;&gt;Without ties&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#with-ties-2&#34; id=&#34;toc-with-ties-2&#34;&gt;With ties&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#verification-in-r&#34; id=&#34;toc-verification-in-r&#34;&gt;Verification in R&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#conclusion&#34; id=&#34;toc-conclusion&#34;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img src=&#34;images/pearson-spearman-kendall-correlation-by-hand.jpeg&#34; style=&#34;width:100.0%&#34; /&gt;&lt;/p&gt;
&lt;div id=&#34;introduction&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;In statistics, a correlation is used to evaluate the relationship between two variables.&lt;/p&gt;
&lt;p&gt;In a previous post, we showed how to &lt;a href=&#34;https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/&#34;&gt;compute a correlation and perform a correlation test in R&lt;/a&gt;. In this post, we illustrate how to compute the Pearson, Spearman and Kendall correlation coefficients by hand and under two different scenarios (i.e., with and without ties).&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;data&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Data&lt;/h1&gt;
&lt;p&gt;To illustrate the methods with and without ties, we consider two different datasets, one with ties and another without ties.&lt;/p&gt;
&lt;div id=&#34;with-ties&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;With ties&lt;/h2&gt;
&lt;p&gt;For the illustrations of the scenarios with ties, suppose we have the following sample of size 5:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-1&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-1&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[-1,3,5,5,2],[-3,1,0,2,-1]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/index_files/figure-html/unnamed-chunk-1-2.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;As we can see, there are some ties since there are two identical observations in the variable &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;without-ties&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Without ties&lt;/h2&gt;
&lt;p&gt;For the scenarios which require no ties, we will consider the following sample of size 3:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-2&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-2&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[3,5,2],[5,1,-1]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/index_files/figure-html/unnamed-chunk-2-2.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;correlation-coefficients-by-hand&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Correlation coefficients by hand&lt;/h1&gt;
&lt;p&gt;The three most common correlation methods are:&lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;Pearson, used for two &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#continuous&#34;&gt;quantitative continuous&lt;/a&gt; variables which have a linear relationship&lt;/li&gt;
&lt;li&gt;Spearman, used for two quantitative variables if the link is partially linear, or for one &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#ordinal&#34;&gt;qualitative ordinal&lt;/a&gt; variable and one quantitative variable&lt;/li&gt;
&lt;li&gt;Kendall, often used for two qualitative ordinal variables&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Each method is presented in the next sections.&lt;/p&gt;
&lt;p&gt;Note that the aim of this post is to illustrate how to compute the three correlation coefficients by hand and under two different scenarios; we are not interested in verifying the underlying assumptions.&lt;/p&gt;
&lt;div id=&#34;pearson&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Pearson&lt;/h2&gt;
&lt;p&gt;Luckily, the procedure for computing the Pearson correlation coefficient is the same whether there are ties or not so we do not distinguish the two scenarios.&lt;/p&gt;
&lt;div id=&#34;with-and-without-ties&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;With and without ties&lt;/h3&gt;
&lt;p&gt;The Pearson correlation coefficient, denoted &lt;span class=&#34;math inline&#34;&gt;\(r\)&lt;/span&gt; in the case of a sample, can be computed as follows&lt;a href=&#34;#fn2&#34; class=&#34;footnote-ref&#34; id=&#34;fnref2&#34;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
r = \frac{\sum^n_{i = 1} x_i y_i - n \bar{x} \bar{y}}{(n - 1) s_x s_y}
\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; is the sample size&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(x_i\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(y_i\)&lt;/span&gt; are the observations&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\bar{x}\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(\bar{y}\)&lt;/span&gt; are the sample &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand/#mean&#34;&gt;means&lt;/a&gt; of &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(s_x\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(s_y\)&lt;/span&gt; are the sample &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand/#standard-deviation&#34;&gt;standard deviations&lt;/a&gt; of &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We show how to compute it step by step.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As you can see, before computing the correlation coefficient we first need to compute the mean and the standard deviation for each of the two variables.&lt;/p&gt;
&lt;p&gt;The mean of &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt; is computed as follows:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\bar{x} = \frac{1}{n}\sum^n_{i = 1} x_i\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The standard deviation of &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt; is computed as follows:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[s_x = \sqrt{\frac{1}{n - 1} \sum^n_{i = 1}(x_i - \bar{x})^2}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The formulas can be used analogously for the variable &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;We start by computing the means of the two variables:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\bar{x} = \frac{-1 + 3 + 5 + 5 + 2}{5} = 2.8\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\bar{y} = \frac{-3 + 1 + 0 + 2 - 1}{5} = -0.2\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We now need to compute the standard deviation of each variable. To ease the computations, it is best to use a table, starting with the observations:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-3&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-3&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[-1,3,5,5,2],[-3,1,0,2,-1]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;To compute the standard deviations, we need the sum of the squared differences between each observation and its mean, that is, &lt;span class=&#34;math inline&#34;&gt;\(\sum^n_{i = 1}(x_i - \bar{x})^2\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(\sum^n_{i = 1}(y_i - \bar{y})^2\)&lt;/span&gt;. We start by creating two new columns in the table, denoted &lt;code&gt;x-xbar&lt;/code&gt; and &lt;code&gt;y-ybar&lt;/code&gt;, corresponding to &lt;span class=&#34;math inline&#34;&gt;\((x_i - \bar{x})\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\((y_i - \bar{y})\)&lt;/span&gt;:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-4&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-4&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[-1,3,5,5,2],[-3,1,0,2,-1],[-3.8,0.2,2.2,2.2,-0.8],[-2.8,1.2,0.2,2.2,-0.8]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n      &lt;th&gt;x-xbar&lt;\/th&gt;\n      &lt;th&gt;y-ybar&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1,2,3]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;We take the square of these two new columns to have &lt;span class=&#34;math inline&#34;&gt;\((x_i - \bar{x})^2\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\((y_i - \bar{y})^2\)&lt;/span&gt;, denoted &lt;code&gt;(x-xbar)^2&lt;/code&gt; and &lt;code&gt;(y-ybar)^2&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-5&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-5&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[-1,3,5,5,2],[-3,1,0,2,-1],[-3.8,0.2,2.2,2.2,-0.8],[-2.8,1.2,0.2,2.2,-0.8],[14.44,0.04,4.84,4.84,0.64],[7.84,1.44,0.04,4.84,0.64]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n      &lt;th&gt;x-xbar&lt;\/th&gt;\n      &lt;th&gt;y-ybar&lt;\/th&gt;\n      &lt;th&gt;(x-xbar)^2&lt;\/th&gt;\n      &lt;th&gt;(y-ybar)^2&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1,2,3,4,5]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;We then sum these two columns, which gives:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;for &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt;: &lt;span class=&#34;math inline&#34;&gt;\(\sum^n_{i = 1}(x_i - \bar{x})^2 =\)&lt;/span&gt; 24.8&lt;/li&gt;
&lt;li&gt;for &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt;: &lt;span class=&#34;math inline&#34;&gt;\(\sum^n_{i = 1}(y_i - \bar{y})^2 =\)&lt;/span&gt; 14.8&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The standard deviations are thus:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(s_x = \sqrt{\frac{24.8}{5-1}} = 2.49\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(s_y = \sqrt{\frac{14.8}{5-1}} = 1.92\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We now need &lt;span class=&#34;math inline&#34;&gt;\(\sum^n_{i = 1} x_i y_i\)&lt;/span&gt;, so we add a new column in the table, corresponding to &lt;span class=&#34;math inline&#34;&gt;\(x_iy_i\)&lt;/span&gt; and denoted &lt;code&gt;x*y&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-6&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-6&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[-1,3,5,5,2],[-3,1,0,2,-1],[-3.8,0.2,2.2,2.2,-0.8],[-2.8,1.2,0.2,2.2,-0.8],[14.44,0.04,4.84,4.84,0.64],[7.84,1.44,0.04,4.84,0.64],[3,3,0,10,-2]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n      &lt;th&gt;x-xbar&lt;\/th&gt;\n      &lt;th&gt;y-ybar&lt;\/th&gt;\n      &lt;th&gt;(x-xbar)^2&lt;\/th&gt;\n      &lt;th&gt;(y-ybar)^2&lt;\/th&gt;\n      &lt;th&gt;x*y&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1,2,3,4,5,6]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We take the sum of that last column, which gives &lt;span class=&#34;math inline&#34;&gt;\(\sum^n_{i = 1} x_i y_i =\)&lt;/span&gt; 14.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Finally, the Pearson correlation coefficient can be computed by plugging values found above in the initial formula:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
\begin{split}
r &amp;amp;= \frac{\sum^n_{i = 1} x_i y_i - n \bar{x} \bar{y}}{(n - 1) s_x s_y} \\
&amp;amp;= \frac{14 - (5\times2.8\times-0.2)}{(5-1) \times 2.49\times1.92} \\
&amp;amp;= 0.88
\end{split}
\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Note that there are other formulas to compute the Pearson correlation coefficient. For instance,&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
\begin{split}
r &amp;amp;= \frac{1}{n - 1} \sum^n_{i = 1} \left(\frac{x_i - \bar{x}}{s_x}\right)\left(\frac{y_i - \bar{x}}{s_y}\right) \\
&amp;amp;= \frac{\sum^n_{i = 1}(x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum^n_{i = 1}(x_i - \bar{x})^2} \sqrt{\sum^n_{i = 1}(y_i - \bar{y})^2}}
\end{split}
\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;All formulas will of course give the exact same results.&lt;/p&gt;
&lt;p&gt;For your information, squaring the Pearson correlation coefficient &lt;span class=&#34;math inline&#34;&gt;\(r\)&lt;/span&gt; gives the coefficient of determination &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; in the context of a simple &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/&#34;&gt;linear regression&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;spearman&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Spearman&lt;/h2&gt;
&lt;p&gt;We now present the Spearman correlation coefficient, also referred as Spearman’s rank correlation coefficient. This coefficient is actually the same than Pearson coefficient, except that the computations are based on the &lt;em&gt;ranked values&lt;/em&gt; rather than on the raw observations.&lt;/p&gt;
&lt;p&gt;Again, we present how to compute it by hand step by step, but this time we distinguish two scenarios:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;if there are ties&lt;/li&gt;
&lt;li&gt;if there are no ties&lt;/li&gt;
&lt;/ol&gt;
&lt;div id=&#34;with-ties-1&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;With ties&lt;/h3&gt;
&lt;p&gt;The Spearman correlation coefficient (with ties), denoted &lt;span class=&#34;math inline&#34;&gt;\(r_s\)&lt;/span&gt;, is defined as follows&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
r_s = \frac{\sum^n_{i = 1} Rx_i Ry_i - n \overline{Rx} \overline{Ry}}{(n - 1) s_{Rx} s_{Ry}}
\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; is the sample size&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(Rx_i\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(Ry_i\)&lt;/span&gt; are the ranks for the two variables&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\overline{Rx}\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(\overline{Ry}\)&lt;/span&gt; are the sample means of the ranks for the two variables&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(s_{Rx}\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(s_{Ry}\)&lt;/span&gt; are the sample standard deviations of the ranks for the two variables&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is how to compute it by hand.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As mentioned before, the Spearman coefficient is based on the ranks. So we first need to add the ranks of the observations (from lowest to highest), separately for each of the two variables.&lt;/p&gt;
&lt;p&gt;For &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt;, we see that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;-3 is the smallest value, so we assign it the rank 1&lt;/li&gt;
&lt;li&gt;-1 is the second smallest value, so we assign it the rank 2&lt;/li&gt;
&lt;li&gt;then comes 0, so we assign it the rank 3&lt;/li&gt;
&lt;li&gt;then comes 1, so we assign it the rank 4&lt;/li&gt;
&lt;li&gt;finally, 2 is the largest value, so we assign it the rank 5&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The same goes for &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt;, except that here we have two observations with the value 5 (so there will be ties in the ranks). In this case, we take the mean rank:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;-1 is the smallest value, so we assign it the rank 1&lt;/li&gt;
&lt;li&gt;2 is the second smallest value, so we assign it the rank 2&lt;/li&gt;
&lt;li&gt;then comes 3, so we assign it the rank 3&lt;/li&gt;
&lt;li&gt;finally, the two largest values belongs to rank 4 and 5, so we assign both of them the rank 4.5&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We include the ranks in the table, denoted &lt;code&gt;Rx&lt;/code&gt; and &lt;code&gt;Ry&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-7&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-7&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[-1,3,5,5,2],[-3,1,0,2,-1],[1,3,4.5,4.5,2],[1,4,3,5,2]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1,2,3]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;From there, it is similar than the Pearson coefficient except that we work on the ranks and not on the initial observations anymore. To avoid any confusion in the remaining steps, we remove the initial observations from the table and we keep only the ranks:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-8&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-8&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[1,3,4.5,4.5,2],[1,4,3,5,2]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;We start with the means of the ranks:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\overline{Rx} = \frac{1+3+4.5+4.5+2}{5} = 3\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\overline{Ry} = \frac{1+4+3+5+2}{5} = 3\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For the standard deviations, we use the table as we did for the Pearson coefficient:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-9&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-9&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[1,3,4.5,4.5,2],[1,4,3,5,2],[-2,0,1.5,1.5,-1],[-2,1,0,2,-1],[4,0,2.25,2.25,1],[4,1,0,4,1]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n      &lt;th&gt;Rx-Rxbar&lt;\/th&gt;\n      &lt;th&gt;Ry-Rybar&lt;\/th&gt;\n      &lt;th&gt;(Rx-Rxbar)^2&lt;\/th&gt;\n      &lt;th&gt;(Ry-Rybar)^2&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1,2,3,4,5]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;We sum the last two columns, which gives:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;for &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt;: &lt;span class=&#34;math inline&#34;&gt;\(\sum^n_{i = 1}(Rx_i - \overline{Rx})^2 =\)&lt;/span&gt; 9.5&lt;/li&gt;
&lt;li&gt;for &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt;: &lt;span class=&#34;math inline&#34;&gt;\(\sum^n_{i = 1}(Ry_i - \overline{Ry})^2 =\)&lt;/span&gt; 10&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The standard deviations are thus:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(s_{Rx} = \sqrt{\frac{9.5}{5-1}} = 1.54\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(s_{Ry} = \sqrt{\frac{10}{5-1}} = 1.58\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We now need &lt;span class=&#34;math inline&#34;&gt;\(\sum^n_{i = 1} Rx_i Ry_i\)&lt;/span&gt;, so we add a new column in the table, corresponding to &lt;span class=&#34;math inline&#34;&gt;\(Rx_iRy_i\)&lt;/span&gt; and denoted &lt;code&gt;Rx*Ry&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-10&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-10&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[1,3,4.5,4.5,2],[1,4,3,5,2],[-2,0,1.5,1.5,-1],[-2,1,0,2,-1],[4,0,2.25,2.25,1],[4,1,0,4,1],[1,12,13.5,22.5,4]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n      &lt;th&gt;Rx-Rxbar&lt;\/th&gt;\n      &lt;th&gt;Ry-Rybar&lt;\/th&gt;\n      &lt;th&gt;(Rx-Rxbar)^2&lt;\/th&gt;\n      &lt;th&gt;(Ry-Rybar)^2&lt;\/th&gt;\n      &lt;th&gt;Rx*Ry&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1,2,3,4,5,6]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We take the sum of that last column, which gives &lt;span class=&#34;math inline&#34;&gt;\(\sum^n_{i = 1} Rx_i Ry_i =\)&lt;/span&gt; 53.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Finally, the Spearman correlation coefficient can be computed by plugging all values in the initial formula:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
\begin{split}
r_s &amp;amp;= \frac{\sum^n_{i = 1} Rx_i Ry_i - n \overline{Rx} \overline{Ry}}{(n - 1) s_{Rx} s_{Ry}} \\
&amp;amp;= \frac{53 - (5\times3\times3)}{(5-1) \times 1.54\times1.58} \\
&amp;amp;= 0.82
\end{split}
\]&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;without-ties-1&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Without ties&lt;/h3&gt;
&lt;p&gt;When all initial values are different within each variable, meaning that all ranks are distinct integers, there are no ties. In that specific case, the Spearman coefficient can be computed with the following shortened formula:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
r_s = 1 - \frac{6 \sum^n_{i = 1}(Rx_i - Ry_i)^2}{n (n^2 - 1)}
\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;For example, suppose the sample without ties introduced at the beginning of the post:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-11&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-11&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[3,5,2],[5,1,-1]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;All observations within each variable are different, so all ranks are distinct integers and there are no ties:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-12&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-12&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[3,5,2],[5,1,-1],[2,3,1],[3,2,1]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1,2,3]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We only need to compute the difference between the two ranks of each row, denoted &lt;code&gt;Rx-Ry&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-13&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-13&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[3,5,2],[5,1,-1],[2,3,1],[3,2,1],[-1,1,0]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n      &lt;th&gt;Rx-Ry&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1,2,3,4]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;Take the square of these differences, denoted &lt;code&gt;(Rx-Ry)^2&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-14&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-14&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[3,5,2],[5,1,-1],[2,3,1],[3,2,1],[-1,1,0],[1,1,0]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n      &lt;th&gt;Rx-Ry&lt;\/th&gt;\n      &lt;th&gt;(Rx-Ry)^2&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1,2,3,4,5]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;And then take the sum of that last column, which gives &lt;span class=&#34;math inline&#34;&gt;\(\sum^n_{i = 1}(Rx_i - Ry_i)^2 =\)&lt;/span&gt; 2.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Finally, we can fill in the initial formula to find the Spearman coefficient:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
\begin{split}
r_s &amp;amp;= 1 - \frac{6 \sum^n_{i = 1}(Rx_i - Ry_i)^2}{n (n^2 - 1)} \\
&amp;amp;= 1 - \frac{6 \times2}{3 (3^2 - 1)} \\
&amp;amp;= 1 - \frac{12}{24} \\
&amp;amp;= 0.5
\end{split}
\]&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;kendall&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Kendall&lt;/h2&gt;
&lt;p&gt;Kendall coefficient correlation, also known as Kendall’s &lt;span class=&#34;math inline&#34;&gt;\(\tau\)&lt;/span&gt; coefficient, is similar than Spearman coefficient, except that it is often preferred for small samples and when many rank ties.&lt;/p&gt;
&lt;p&gt;Here also we distinguish between two scenarios:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;if there are no ties&lt;/li&gt;
&lt;li&gt;if there are ties&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Unlike Spearman coefficient, we first illustrate the scenario when there are no ties, and then when there are ties.&lt;/p&gt;
&lt;div id=&#34;without-ties-2&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Without ties&lt;/h3&gt;
&lt;p&gt;When there are no ties, the Kendall coefficient, denoted &lt;span class=&#34;math inline&#34;&gt;\(\tau_a\)&lt;/span&gt;, is defined as follows&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
\tau_a = \frac{C - D}{C + D}
\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(C\)&lt;/span&gt; is the number of concordant pairs&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(D\)&lt;/span&gt; is the number of discordant pairs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This coefficient, also referred as Kendall tau-a, does not make any adjustment for ties.&lt;/p&gt;
&lt;p&gt;Let’s see what are concordant and discordant pairs using the data without ties (the same data than for Spearman correlation without ties):&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-15&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-15&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[3,5,2],[5,1,-1]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We start by computing the ranks for each variable (&lt;code&gt;Rx&lt;/code&gt; and &lt;code&gt;Ry&lt;/code&gt;), as we did for the Spearman coefficient:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-16&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-16&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[3,5,2],[5,1,-1],[2,3,1],[3,2,1]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1,2,3]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We then arbitrarily choose a reference variable among the two, &lt;code&gt;Rx&lt;/code&gt; or &lt;code&gt;Ry&lt;/code&gt;. Suppose we take &lt;code&gt;Rx&lt;/code&gt; as the reference variable here.&lt;/p&gt;
&lt;p&gt;We sort the dataset by this reference variable:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-17&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-17&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[2,3,5],[-1,5,1],[1,2,3],[1,3,2]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1,2,3]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;From now, we only look at the ranks of the second variable (the one which is &lt;em&gt;not&lt;/em&gt; the reference level, here &lt;code&gt;Ry&lt;/code&gt;), so to avoid any confusion in the remaining steps we keep only the &lt;code&gt;Ry&lt;/code&gt; column:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-18&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-18&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[1,3,2]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:0}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We now take each row of &lt;code&gt;Ry&lt;/code&gt; one by one and check whether the rows below it in the table are smaller or larger.&lt;/p&gt;
&lt;p&gt;In our table, the first row of &lt;code&gt;Ry&lt;/code&gt; is 1. We see that the value just below it is 3, which is larger than 1. Since 3 is larger than 1, this is called a concordant pair. We write it in the table:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-19&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-19&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[1,3,2],[null,&#34;C&#34;,null]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n      &lt;th&gt;1&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:0}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;The next row, 2, is also larger than 1, so it is also a concordant pair. We also write it in the table:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-20&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-20&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[1,3,2],[null,&#34;C&#34;,&#34;C&#34;]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n      &lt;th&gt;1&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:0}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;We start again with the second row of &lt;code&gt;Ry&lt;/code&gt;, which is 3. Again, we look at the row below it and check whether it is larger or smaller. Here, the row below it is 2, which is smaller than 3, so we have a discordant pair. We add this information into the table:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-21&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-21&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[1,3,2],[null,&#34;C&#34;,&#34;C&#34;],[null,null,&#34;D&#34;]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n      &lt;th&gt;1&lt;\/th&gt;\n      &lt;th&gt;3&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:0}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We now compute the total number of concordant and discordant pairs. There are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;2 concordant pairs so &lt;span class=&#34;math inline&#34;&gt;\(C = 2\)&lt;/span&gt;, and&lt;/li&gt;
&lt;li&gt;1 discordant pair so &lt;span class=&#34;math inline&#34;&gt;\(D = 1\)&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Finally, we plug the values we have just found in the initial formula:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
\begin{split}
\tau_a &amp;amp;= \frac{C - D}{C + D}\\
&amp;amp;= \frac{2 - 1}{2 + 1}\\
&amp;amp;= \frac{1}{3}\\
&amp;amp;= 0.33
\end{split}
\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Alternatively, we can also use the following formulas&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
\begin{split}
\tau_a &amp;amp;= 1 - \frac{4D}{n(n - 1)}\\
&amp;amp;= \frac{4C}{n(n - 1)} - 1
\end{split}
\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;where &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; is still the sample size, and which both give the exact same results.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;with-ties-2&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;With ties&lt;/h3&gt;
&lt;p&gt;I must admit that the process with ties is slightly more complex than without ties.&lt;/p&gt;
&lt;p&gt;The Kendall tau-b coefficient, which makes adjustments for ties, is defined as follows&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
\tau_b = \frac{C - D}{\sqrt{(C^2_n - n_x)(C^2_n - n_y)}}
\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(C\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(D\)&lt;/span&gt; are still the number of concordant and discordant pairs, respectively&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(C^2_n\)&lt;/span&gt; is the total number of possible pairs&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(n_x\)&lt;/span&gt; is the number of possible pairs &lt;em&gt;with a tie&lt;/em&gt; among the &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt; values&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(n_y\)&lt;/span&gt; is the number of possible pairs &lt;em&gt;with a tie&lt;/em&gt; among the &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt; values&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that here, the letter &lt;span class=&#34;math inline&#34;&gt;\(C\)&lt;/span&gt; in &lt;span class=&#34;math inline&#34;&gt;\(C^2_n\)&lt;/span&gt; denotes “combination” and not “concordant”.&lt;/p&gt;
&lt;p&gt;Let’s illustrate that scenario and the formula with the dataset used for the Pearson correlation and the Spearman correlation with ties, that is:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-22&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-22&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[-1,3,5,5,2],[-3,1,0,2,-1]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Similarly to with no ties, the process with ties is based on the ranks so we start by adding the ranks for each variable, denoted as usual as &lt;code&gt;Rx&lt;/code&gt; and &lt;code&gt;Ry&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-23&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-23&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[-1,3,5,5,2],[-3,1,0,2,-1],[1,3,4.5,4.5,2],[1,4,3,5,2]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;x&lt;\/th&gt;\n      &lt;th&gt;y&lt;\/th&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1,2,3]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;One of the two variables (&lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt; or &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt;) must be selected as the reference variable. This time, we do not choose it arbitrarily, but we choose the one which does not have any ties. In our case, variable &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt; has ties, while variable &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt; does not have any ties. So &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt; will be our reference variable.&lt;/p&gt;
&lt;p&gt;We then:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;order the dataset by the reference variable, here &lt;code&gt;Ry&lt;/code&gt;, and&lt;/li&gt;
&lt;li&gt;we keep only the necessary columns to avoid any confusion in the remaining steps:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-24&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-24&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[1,2,3,4,5],[1,2,4.5,3,4.5]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We check whether it is a concordant or discordant pair the same way we did without ties, but this time we do not count ties.&lt;/p&gt;
&lt;p&gt;The first row in column &lt;code&gt;Rx&lt;/code&gt; is 1. We compare all rows below it in the table with that value. All rows below 1 in the table are larger, so we write that they are concordant pairs:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-25&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-25&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[1,2,3,4,5],[1,2,4.5,3,4.5],[null,&#34;C&#34;,&#34;C&#34;,&#34;C&#34;,&#34;C&#34;]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;1&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;We repeat the same process for each row.&lt;/p&gt;
&lt;p&gt;For instance, for the second row of the column &lt;code&gt;Rx&lt;/code&gt;, we have the value 2. Again, all rows below in the table are larger so we write that they are concordant pairs:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-26&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-26&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[1,2,3,4,5],[1,2,4.5,3,4.5],[null,&#34;C&#34;,&#34;C&#34;,&#34;C&#34;,&#34;C&#34;],[null,null,&#34;C&#34;,&#34;C&#34;,&#34;C&#34;]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;1&lt;\/th&gt;\n      &lt;th&gt;2&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;For the third row of the column &lt;code&gt;Rx&lt;/code&gt;, we have the value 4.5. The row just below in the table (= 3) is smaller than 4, so we write D for discordant pair:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-27&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-27&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[1,2,3,4,5],[1,2,4.5,3,4.5],[null,&#34;C&#34;,&#34;C&#34;,&#34;C&#34;,&#34;C&#34;],[null,null,&#34;C&#34;,&#34;C&#34;,&#34;C&#34;],[null,null,null,&#34;D&#34;,null]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;1&lt;\/th&gt;\n      &lt;th&gt;2&lt;\/th&gt;\n      &lt;th&gt;4.5&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;Now, as you can see, the last row is also 4.5. Therefore, since it is equal to the value we are comparing to, it is neither a concordant nor a discordant pair, so we write “T” in the table for “ties”:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-28&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-28&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[1,2,3,4,5],[1,2,4.5,3,4.5],[null,&#34;C&#34;,&#34;C&#34;,&#34;C&#34;,&#34;C&#34;],[null,null,&#34;C&#34;,&#34;C&#34;,&#34;C&#34;],[null,null,null,&#34;D&#34;,&#34;T&#34;]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;1&lt;\/th&gt;\n      &lt;th&gt;2&lt;\/th&gt;\n      &lt;th&gt;4.5&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;Last, the fourth row in the column &lt;code&gt;Rx&lt;/code&gt; is 3, which we compare to 4.5 to see that 4.5 is larger so it is a concordant pair:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-29&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-29&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[1,2,3,4,5],[1,2,4.5,3,4.5],[null,&#34;C&#34;,&#34;C&#34;,&#34;C&#34;,&#34;C&#34;],[null,null,&#34;C&#34;,&#34;C&#34;,&#34;C&#34;],[null,null,null,&#34;D&#34;,&#34;T&#34;],[null,null,null,null,&#34;C&#34;]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n      &lt;th&gt;1&lt;\/th&gt;\n      &lt;th&gt;2&lt;\/th&gt;\n      &lt;th&gt;4.5&lt;\/th&gt;\n      &lt;th&gt;3&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We then sum the number of concordant and discordant pairs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(C = 8\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(D = 1\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We now have all the information required to compute the numerator of &lt;span class=&#34;math inline&#34;&gt;\(\tau_b\)&lt;/span&gt;, but we still need to find &lt;span class=&#34;math inline&#34;&gt;\(C^2_n\)&lt;/span&gt;, &lt;span class=&#34;math inline&#34;&gt;\(n_x\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(n_y\)&lt;/span&gt; to compute the denominator.&lt;/p&gt;
&lt;p&gt;As mentioned above, &lt;span class=&#34;math inline&#34;&gt;\(C^2_n\)&lt;/span&gt; is the total number of possible pairs, thus corresponding to the number of combinations of two values. This number of pairs can be found with the formula of the &lt;a href=&#34;https://statsandr.com/blog/the-9-concepts-and-formulas-in-probability-that-every-data-scientist-should-know/#combination&#34;&gt;combination&lt;/a&gt;, that is, &lt;span class=&#34;math inline&#34;&gt;\(C^2_n = \frac{n!}{2!(n - 2)!}\)&lt;/span&gt; where &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; is the sample size.&lt;/p&gt;
&lt;p&gt;In our case, we have a sample of size 5, so we have:&lt;a href=&#34;#fn3&#34; class=&#34;footnote-ref&#34; id=&#34;fnref3&#34;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
\begin{split}
C^2_n &amp;amp;= \frac{n!}{2!(n - 2)!}\\
&amp;amp;= \frac{5!}{2!(5-2)!}\\
&amp;amp;= 10
\end{split}
\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Furthermore, &lt;span class=&#34;math inline&#34;&gt;\(n_x\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(n_y\)&lt;/span&gt; are the number of possible pairs &lt;strong&gt;with a tie&lt;/strong&gt; among the &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt; variables, respectively.&lt;/p&gt;
&lt;p&gt;When looking at &lt;code&gt;Ry&lt;/code&gt; and &lt;code&gt;Rx&lt;/code&gt; from the table above:&lt;/p&gt;
&lt;div class=&#34;datatables html-widget html-fill-item-overflow-hidden html-fill-item&#34; id=&#34;htmlwidget-30&#34; style=&#34;width:100%;height:auto;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-30&#34;&gt;{&#34;x&#34;:{&#34;filter&#34;:&#34;none&#34;,&#34;vertical&#34;:false,&#34;data&#34;:[[1,2,3,4,5],[1,2,4.5,3,4.5]],&#34;container&#34;:&#34;&lt;table class=\&#34;display\&#34;&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th&gt;Ry&lt;\/th&gt;\n      &lt;th&gt;Rx&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n&lt;\/table&gt;&#34;,&#34;options&#34;:{&#34;dom&#34;:&#34;t&#34;,&#34;columnDefs&#34;:[{&#34;className&#34;:&#34;dt-right&#34;,&#34;targets&#34;:[0,1]}],&#34;order&#34;:[],&#34;autoWidth&#34;:false,&#34;orderClasses&#34;:false}},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;We see that there are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;2 identical values for the variable &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;0 identical value for the variable &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This means that the number of possible &lt;strong&gt;pairs&lt;/strong&gt; with a tie is equal to:&lt;a href=&#34;#fn4&#34; class=&#34;footnote-ref&#34; id=&#34;fnref4&#34;&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;1 for the variable &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt; (the only possible pair with a tie is the pair {4.5, 4.5})&lt;/li&gt;
&lt;li&gt;0 for the variable &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt; (since all ranks are distinct, there exists no pair with a tie)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Therefore,&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
\begin{split}
n_x &amp;amp;= 1\\
n_y &amp;amp;= 0
\end{split}
\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;And we now have all the necessary information to compute &lt;span class=&#34;math inline&#34;&gt;\(\tau_b\)&lt;/span&gt;!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 6.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;By plugging the values found above in the initial formula, we have&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
\begin{split}
\tau_b &amp;amp;= \frac{C - D}{\sqrt{(C^2_n - n_x)(C^2_n - n_y)}}\\
&amp;amp;= \frac{8-1}{\sqrt{(10 - 1)(10 - 0)}}\\
&amp;amp;= \frac{7}{\sqrt{90}}\\
&amp;amp;= 0.74
\end{split}
\]&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;verification-in-r&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Verification in R&lt;/h1&gt;
&lt;p&gt;For the sake of completeness, we verify our results with the help of R for each coefficient and scenario.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Pearson:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;x &amp;lt;- c(-1, 3, 5, 5, 2)
y &amp;lt;- c(-3, 1, 0, 2, -1)

cor(x, y, method = &amp;quot;pearson&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.8769051&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Spearman with ties:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cor(x, y, method = &amp;quot;spearman&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.8207827&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Spearman without ties:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;x2 &amp;lt;- c(3, 5, 2)
y2 &amp;lt;- c(5, 1, -1)

cor(x2, y2, method = &amp;quot;spearman&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.5&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Kendall without ties:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cor(x2, y2, method = &amp;quot;kendall&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.3333333&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Kendall with ties:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cor(x, y, method = &amp;quot;kendall&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.7378648&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We indeed find the same results by hand than with R (any discrepancies is due to rounding)!&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;conclusion&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;Remember that a correlation coefficient (no matter whether it is Pearson, Spearman or Kendall) ranges from -1 to 1, with 0 being no correlation and the closer to 1 in absolute terms, the stronger the correlation.&lt;/p&gt;
&lt;p&gt;Broadly speaking, a positive correlation means that high values of one variable are associated with high values of the other variable (and vice versa). A negative correlation means that high values of one variable are associated with low values of the other variable.
&lt;!-- And a correlation close to 0 means that there are no linear relationship between the two variables of interest. --&gt;&lt;/p&gt;
&lt;p&gt;Last but not least, remember that the conclusions drawn from a correlation coefficient computed within a sample cannot be generalized to the population without a proper statistical test, that is, a &lt;a href=&#34;https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/#correlation-test&#34;&gt;correlation test&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Thanks for reading.&lt;/p&gt;
&lt;p&gt;I hope this article helped you to compute the Pearson, Spearman and Kendall correlation coefficients by hand (with and without ties).&lt;/p&gt;
&lt;p&gt;As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;footnotes footnotes-end-of-document&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;For your information, the Pearson correlation coefficient is considered as a parametric procedure, whereas the Spearman and Kendall correlation coefficients are considered as non-parametric procedures.&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn2&#34;&gt;&lt;p&gt;Here, we suppose that we have a sample and not a population. In the case where the observations you have represent the entire population, the correlation coefficient is denoted &lt;span class=&#34;math inline&#34;&gt;\(\rho\)&lt;/span&gt; and the formula differs slightly. See a recap of the &lt;a href=&#34;https://statsandr.com/blog/what-is-the-difference-between-population-and-sample/&#34;&gt;difference between a sample and a population&lt;/a&gt; if needed.&lt;a href=&#34;#fnref2&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn3&#34;&gt;&lt;p&gt;Remember that the factorial of &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt;, denoted &lt;span class=&#34;math inline&#34;&gt;\(n!\)&lt;/span&gt;, is &lt;span class=&#34;math inline&#34;&gt;\(n! = n \times (n - 1) \times \cdots \times 1\)&lt;/span&gt;. So for instance, &lt;span class=&#34;math inline&#34;&gt;\(5! = 5\times4\times3\times2\times1 = 120\)&lt;/span&gt;.&lt;a href=&#34;#fnref3&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn4&#34;&gt;&lt;p&gt;If you have many identical values and want to count the number of possible pairs thanks to a formula rather than by counting them manually, you can again use the formula of the &lt;a href=&#34;https://statsandr.com/blog/the-9-concepts-and-formulas-in-probability-that-every-data-scientist-should-know/#combination&#34;&gt;combination&lt;/a&gt;, that is, &lt;span class=&#34;math inline&#34;&gt;\(C^2_n = \frac{n!}{x!(n - x)!}\)&lt;/span&gt; where &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; is the number of identical values in a given group of ties. Bear in mind that the number of combinations must be summed up for all group of ties. In our case, we have only one group of ties with 2 values, so &lt;span class=&#34;math inline&#34;&gt;\(n_x = C^2_2 = \frac{2!}{2!(2 - 2)!} = 1\)&lt;/span&gt;.&lt;a href=&#34;#fnref4&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>What is survival analysis? Examples by hand and in R</title>
      <link>https://statsandr.com/blog/what-is-survival-analysis/</link>
      <pubDate>Thu, 22 Dec 2022 00:00:00 +0000</pubDate>
      
      <guid>https://statsandr.com/blog/what-is-survival-analysis/</guid>
      <description>

&lt;div id=&#34;TOC&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#introduction&#34; id=&#34;toc-introduction&#34;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#what-is-survival-analysis&#34; id=&#34;toc-what-is-survival-analysis&#34;&gt;What is survival analysis?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#why-do-we-need-special-methods-for-survival-analysis&#34; id=&#34;toc-why-do-we-need-special-methods-for-survival-analysis&#34;&gt;Why do we need special methods for survival analysis?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#common-functions-in-survival-analysis&#34; id=&#34;toc-common-functions-in-survival-analysis&#34;&gt;Common functions in survival analysis&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#survival-function&#34; id=&#34;toc-survival-function&#34;&gt;Survival function&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#cumulative-hazard-function&#34; id=&#34;toc-cumulative-hazard-function&#34;&gt;Cumulative hazard function&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#hazard-function&#34; id=&#34;toc-hazard-function&#34;&gt;Hazard function&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#estimation&#34; id=&#34;toc-estimation&#34;&gt;Estimation&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#by-hand&#34; id=&#34;toc-by-hand&#34;&gt;By hand&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#in-r&#34; id=&#34;toc-in-r&#34;&gt;In R&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#hypothesis-testing&#34; id=&#34;toc-hypothesis-testing&#34;&gt;Hypothesis testing&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#log-rank-test&#34; id=&#34;toc-log-rank-test&#34;&gt;Log-rank test&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#by-hand-1&#34; id=&#34;toc-by-hand-1&#34;&gt;By hand&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#in-r-1&#34; id=&#34;toc-in-r-1&#34;&gt;In R&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#to-go-further&#34; id=&#34;toc-to-go-further&#34;&gt;To go further&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#references&#34; id=&#34;toc-references&#34;&gt;References&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img src=&#34;images/what-is-survival-analysis.jpeg&#34; style=&#34;width:100.0%&#34; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note that this article is inspired from:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;the lecture notes of Prof. Van Keilegom and my personal notes as teaching assistant for her course entitled “Analysis of Survival and Duration Data” given at UCLouvain&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;the lecture notes of Prof. Legrand for her course entitled “Statistics in clinical trials” given at UCLouvain&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div id=&#34;introduction&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;For the last post of the year, I would like to present a rather unknown (yet important) statistical method–&lt;strong&gt;survival analysis&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Although survival analysis is a branch of statistics, it is usually not covered in introductory statistics courses and it is rather unknown to the general public. It is mostly taught in biostatistics courses or advanced statistics study programs.&lt;/p&gt;
&lt;p&gt;In this article, I will explain what is survival analysis, in which context and how it is used. I will explain the main tools and methods used by biostatisticians to analyze survival data and how to estimate and interpret survival curves.&lt;/p&gt;
&lt;p&gt;I will show in detail how to apply these techniques in R with concrete examples. In practice, survival analysis is almost always done via a statistical program and never done by hand. However, as for any statistical concept, I believe that doing it by hand allows to really understand the concepts and what these programs actually do. For this reason, I will also show a brief example on how to perform a basic survival analysis by hand.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;what-is-survival-analysis&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;What is survival analysis?&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;Survival analysis&lt;/strong&gt; (also called time-to-event analysis or duration analysis) is a branch of statistics aimed at &lt;strong&gt;analyzing the duration of time from a well-defined time origin until one or more events happen&lt;/strong&gt;, called survival times or duration times.&lt;/p&gt;
&lt;p&gt;In other words, in survival analysis, we are interested in a certain event and want to &lt;strong&gt;analyze the time until the event happens&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;While the event of interest is often death (in this case we study the time to death for patients having a specific disease) or recurrence (in this case we study the time to relapse of a certain disease), it is not limited to the medical field or epidemiology.&lt;/p&gt;
&lt;p&gt;In fact, it can be used in many domains. For example, we may also analyze the time until:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;getting cured from a certain disease&lt;/li&gt;
&lt;li&gt;finding a new job after a period of unemployment&lt;/li&gt;
&lt;li&gt;being arrested again after having been released from jail&lt;/li&gt;
&lt;li&gt;the first pregnancy&lt;/li&gt;
&lt;li&gt;the failure of a mechanical system or a machine&lt;/li&gt;
&lt;li&gt;a bank or a company goes bankrupt&lt;/li&gt;
&lt;li&gt;a customer buys a new product or stops its current subscription&lt;/li&gt;
&lt;li&gt;a letter is delivered&lt;/li&gt;
&lt;li&gt;a taxi picks you up after having called the taxi company&lt;/li&gt;
&lt;li&gt;an employee leaves the company&lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you can see, the event of interest does not necessarily have to be the death or the occurrence of a disease, but in all situations &lt;strong&gt;we are interested in analyzing the time until a specific event occurs&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;why-do-we-need-special-methods-for-survival-analysis&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Why do we need special methods for survival analysis?&lt;/h1&gt;
&lt;p&gt;Survival data, also referred as time-to-event data, requires a special set of statistical methods for three main reasons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Duration times&lt;/strong&gt; are &lt;strong&gt;always positive&lt;/strong&gt;: the time until an event of interest occurs cannot be less than 0. Moreover, the distribution of survival times is right-skewed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Different measures&lt;/strong&gt; are of interest depending on the research question, context, etc. For instance, we could be interested in:
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;probability&lt;/strong&gt; that a cancer patient survives longer than 5 years after diagnosis?&lt;/li&gt;
&lt;li&gt;The typical &lt;strong&gt;waiting time&lt;/strong&gt; for a cab to arrive after having called the taxi company?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How many&lt;/strong&gt;, out of 100 unemployed people, are expected to have a job again after 2 months of unemployment?&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Censoring&lt;/strong&gt; is almost always an issue:
&lt;ul&gt;
&lt;li&gt;When the event occurred before the end of the study, the survival time is known.&lt;/li&gt;
&lt;li&gt;However, sometimes, the event is not yet observed at the end of the study. Suppose that we study the time until death of patients with breast cancer. Luckily, some patients will not die before the end of the study.
&lt;!-- + Other times, another event occurs before the event of interest which prevents it from ever happening. For example, a cancer patient may die from a car accident. --&gt;&lt;/li&gt;
&lt;li&gt;It can also happen that the patient withdraws from the study or moves to another country before the end of the study (known as lost to follow up or drop out).&lt;/li&gt;
&lt;li&gt;In all situations, his or her survival time cannot be observed because the event is not observed for the duration of the study.&lt;/li&gt;
&lt;li&gt;Censoring can be seen, in some sense, as a type of missing data.&lt;/li&gt;
&lt;li&gt;For these reasons, many “standard” statistical tools such as &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/&#34;&gt;descriptive statistics&lt;/a&gt;, &lt;a href=&#34;https://statsandr.com/blog/hypothesis-test-by-hand/&#34;&gt;hypothesis tests&lt;/a&gt; and &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/&#34;&gt;regression models&lt;/a&gt; are not appropriate for this kind of data. Specific statistical methods are required to take into account the fact that the &lt;em&gt;exact&lt;/em&gt; survival duration for some patients is missing. It is known that they survived a certain amount of time (until the end of the study or until the time of withdrawal), but their exact survival time is unknown.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For your information, there are three types of censoring:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;right-censoring (the most frequent),&lt;/li&gt;
&lt;li&gt;left-censoring (the least frequent) and&lt;/li&gt;
&lt;li&gt;interval-censoring.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When the event is not yet observed at the end of the study (i.e., the survival time is greater than the observed duration), this is referred as right-censoring. Left-censoring occurs if a participant is entered into the study when the event of interest occurred prior to study entry but we do not know exactly when. Interval-censoring implies that the event occurred within a time interval (between two known dates, two visits, etc.); the exact moment of occurrence is not known. The goal is of course to analyze all available data, including information about censored patients.&lt;/p&gt;
&lt;p&gt;The goal of survival analysis is thus to model and describe time-to-event data in an appropriate way, taking the particularities of this type of data into account.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;common-functions-in-survival-analysis&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Common functions in survival analysis&lt;/h1&gt;
&lt;p&gt;We are not going to go to much into the details, but it is important to lay the foundation with the most common functions in survival analysis.&lt;/p&gt;
&lt;p&gt;Let &lt;span class=&#34;math inline&#34;&gt;\(T\)&lt;/span&gt; be a non-negative continuous random variable, representing the time until the event of interest. We consider the following functions:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;Survival function&lt;/li&gt;
&lt;li&gt;Cumulative hazard function&lt;/li&gt;
&lt;li&gt;Hazard function&lt;/li&gt;
&lt;/ol&gt;
&lt;div id=&#34;survival-function&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Survival function&lt;/h2&gt;
&lt;p&gt;The most common one is the survival function.&lt;/p&gt;
&lt;p&gt;Let &lt;span class=&#34;math inline&#34;&gt;\(T\)&lt;/span&gt; be a non-negative continuous random variable, representing the time until the event of interest. The survival function &lt;span class=&#34;math inline&#34;&gt;\(S(t)\)&lt;/span&gt; is the probability that a randomly chosen individual is still at risk at time &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt;, where &lt;span class=&#34;math inline&#34;&gt;\(0 \le t \le +\infty\)&lt;/span&gt;. For each &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt;, it is given by&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
\begin{align*}
    S(t) &amp;amp;= P(T &amp;gt; t)\\
    &amp;amp;= 1 - P(T \le t)\\
    &amp;amp;= 1 - F(t)\\
    &amp;amp;= 1 - \int^t_0 f(u)\text{d}u,
\end{align*}
\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;where &lt;span class=&#34;math inline&#34;&gt;\(f(\cdot)\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(F(\cdot)\)&lt;/span&gt; are the density and the cumulative distribution functions of &lt;span class=&#34;math inline&#34;&gt;\(T\)&lt;/span&gt;, respectively.&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math inline&#34;&gt;\(S(t)\)&lt;/span&gt; represents, for each time &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt;, the probability that the time until the event is greater than this time &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt;. In other words, it models the probability that the event of interest happens &lt;strong&gt;after&lt;/strong&gt; &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;In the context of our examples mentioned above, it gives the probability that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a randomly selected patient will survive beyond time &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt; or the proportion of patients still alive after time &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt;,&lt;/li&gt;
&lt;li&gt;a cab takes more than &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt; minutes to arrive, or&lt;/li&gt;
&lt;li&gt;an unemployed person take more than &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt; months to find a new job.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The survival function &lt;span class=&#34;math inline&#34;&gt;\(S(t)\)&lt;/span&gt; is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a decreasing function,&lt;/li&gt;
&lt;li&gt;taking values in &lt;span class=&#34;math inline&#34;&gt;\([0, 1]\)&lt;/span&gt; (since it is a probability), and&lt;/li&gt;
&lt;li&gt;equal to 1 at &lt;span class=&#34;math inline&#34;&gt;\(t = 0\)&lt;/span&gt; (i.e., &lt;span class=&#34;math inline&#34;&gt;\(S(0) = 1\)&lt;/span&gt;) and 0 at &lt;span class=&#34;math inline&#34;&gt;\(t = \infty\)&lt;/span&gt; (i.e., &lt;span class=&#34;math inline&#34;&gt;\(S(\infty) = 0\)&lt;/span&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Visually we have:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/what-is-survival-analysis/index_files/figure-html/unnamed-chunk-1-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The curve shows the proportion of individuals (or experimental units) who, as time goes on, have not experienced the event of interest. As time progresses, events occur, so the proportion who have not experienced the event decreases.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;cumulative-hazard-function&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Cumulative hazard function&lt;/h2&gt;
&lt;p&gt;The cumulative hazard function, which is the total hazard experienced up to time &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt;, is defined as:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[H(t) = -log\left(S(t)\right)\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;and has the following properties:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;increasing function,&lt;/li&gt;
&lt;li&gt;taking value in &lt;span class=&#34;math inline&#34;&gt;\([0, +\infty]\)&lt;/span&gt;, and&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(S(t) = exp(-H(t))\)&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;hazard-function&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Hazard function&lt;/h2&gt;
&lt;p&gt;The hazard function &lt;span class=&#34;math inline&#34;&gt;\(h(t)\)&lt;/span&gt;, or hazard rate, defines the instantaneous event rate at time &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt; for an individual still at risk at that time. It can be obtained by&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[
\begin{align*}
h(t) &amp;amp;= \lim_{\Delta t \rightarrow 0} \frac{P(t \le T &amp;lt; t + \Delta t | T \ge t)}{\Delta t}\\
&amp;amp;= \frac{d}{dt} H(t)\\
&amp;amp;= \frac{f(t)}{S(t)}.
\end{align*}
\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;and has the following properties:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;positive function (not necessarily increasing or decreasing)&lt;/li&gt;
&lt;li&gt;the hazard function &lt;span class=&#34;math inline&#34;&gt;\(h(t)\)&lt;/span&gt; can have many different shapes and is therefore a useful tool to summarize survival data&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the context of cancer research when death is the event of interest, &lt;span class=&#34;math inline&#34;&gt;\(h(t)\)&lt;/span&gt; measures the instantaneous risk of dying right after time &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt; given the individual is alive at time &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;To link the hazard rate with the survival function; the survival curve represents the hazard rates. A steeper slope indicates a higher hazard rate because events happen more frequently, reducing the proportion of individuals who have not experienced the event at a faster rate. On the contrary, a gradual and flatter slope indicates a lower hazard rate because events occur less frequently, reducing the proportion of individuals who have not experiences the event at a slower rate. More formally:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[S(t) = \exp\left(-\int^t_0 h(u) \text{d}u\right).\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Note that, in contrast to the survival function which focuses on not having an event, the hazard function focuses on the event occurring.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;estimation&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Estimation&lt;/h1&gt;
&lt;p&gt;To estimate the survival function, we need to use an estimator which is able to deal with censoring. The most common one is the nonparametric &lt;strong&gt;&lt;span class=&#34;citation&#34;&gt;Kaplan and Meier (&lt;a href=&#34;#ref-kaplan1958nonparametric&#34;&gt;1958&lt;/a&gt;)&lt;/span&gt; estimator&lt;/strong&gt; (also sometimes referred as the product-limit estimator, or more simply, the K-M estimator).&lt;/p&gt;
&lt;p&gt;The advantages of the Kaplan-Meier estimator are that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;it is simple and straightforward to use and interpret&lt;/li&gt;
&lt;li&gt;it is a nonparametric estimator, so it constructs a survival curve from the data and no assumptions is made about the shape of the underlying distribution&lt;/li&gt;
&lt;li&gt;it gives a graphical representation of the survival function(s), useful for illustrative purposes&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The principle behind this estimator is that surviving beyond time &lt;span class=&#34;math inline&#34;&gt;\(t_i\)&lt;/span&gt; implies surviving beyond time &lt;span class=&#34;math inline&#34;&gt;\(t_{i-1}\)&lt;/span&gt; and surviving at time &lt;span class=&#34;math inline&#34;&gt;\(t_i\)&lt;/span&gt;. Note that an important assumption for the estimation to hold is that censoring is independent of the occurrence of events. We say that censoring is non-informative, that is, censored subjects have the same survival prospects as subjects who are not censored and who continue to be followed.&lt;/p&gt;
&lt;div id=&#34;by-hand&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;By hand&lt;/h2&gt;
&lt;p&gt;To understand how it works, let’s first estimate it by hand on the following dataset:&lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div id=&#34;ezedjhlbwf&#34; style=&#34;padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;&#34;&gt;
&lt;style&gt;#ezedjhlbwf table {
  font-family: system-ui, &#39;Segoe UI&#39;, Roboto, Helvetica, Arial, sans-serif, &#39;Apple Color Emoji&#39;, &#39;Segoe UI Emoji&#39;, &#39;Segoe UI Symbol&#39;, &#39;Noto Color Emoji&#39;;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#ezedjhlbwf .gt_from_md &gt; :first-child {
  margin-top: 0;
}

#ezedjhlbwf .gt_from_md &gt; :last-child {
  margin-bottom: 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#ezedjhlbwf div.Reactable &gt; div.rt-table &gt; div.rt-thead &gt; div.rt-tr.rt-tr-group-header &gt; div.rt-th-group:after {
  height: 0px !important;
}
&lt;/style&gt;
&lt;table class=&#34;gt_table&#34; data-quarto-disable-processing=&#34;false&#34; data-quarto-bootstrap=&#34;false&#34;&gt;
  &lt;thead&gt;
    &lt;tr class=&#34;gt_col_headings&#34;&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_right&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;subject&#34;&gt;subject&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_right&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;time&#34;&gt;time&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_right&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;event&#34;&gt;event&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody class=&#34;gt_table_body&#34;&gt;
    &lt;tr&gt;&lt;td headers=&#34;subject&#34; class=&#34;gt_row gt_right&#34;&gt;1&lt;/td&gt;
&lt;td headers=&#34;time&#34; class=&#34;gt_row gt_right&#34;&gt;3&lt;/td&gt;
&lt;td headers=&#34;event&#34; class=&#34;gt_row gt_right&#34;&gt;0&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;subject&#34; class=&#34;gt_row gt_right&#34;&gt;2&lt;/td&gt;
&lt;td headers=&#34;time&#34; class=&#34;gt_row gt_right&#34;&gt;5&lt;/td&gt;
&lt;td headers=&#34;event&#34; class=&#34;gt_row gt_right&#34;&gt;1&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;subject&#34; class=&#34;gt_row gt_right&#34;&gt;3&lt;/td&gt;
&lt;td headers=&#34;time&#34; class=&#34;gt_row gt_right&#34;&gt;7&lt;/td&gt;
&lt;td headers=&#34;event&#34; class=&#34;gt_row gt_right&#34;&gt;1&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;subject&#34; class=&#34;gt_row gt_right&#34;&gt;4&lt;/td&gt;
&lt;td headers=&#34;time&#34; class=&#34;gt_row gt_right&#34;&gt;2&lt;/td&gt;
&lt;td headers=&#34;event&#34; class=&#34;gt_row gt_right&#34;&gt;1&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;subject&#34; class=&#34;gt_row gt_right&#34;&gt;5&lt;/td&gt;
&lt;td headers=&#34;time&#34; class=&#34;gt_row gt_right&#34;&gt;18&lt;/td&gt;
&lt;td headers=&#34;event&#34; class=&#34;gt_row gt_right&#34;&gt;0&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;subject&#34; class=&#34;gt_row gt_right&#34;&gt;6&lt;/td&gt;
&lt;td headers=&#34;time&#34; class=&#34;gt_row gt_right&#34;&gt;16&lt;/td&gt;
&lt;td headers=&#34;event&#34; class=&#34;gt_row gt_right&#34;&gt;1&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;subject&#34; class=&#34;gt_row gt_right&#34;&gt;7&lt;/td&gt;
&lt;td headers=&#34;time&#34; class=&#34;gt_row gt_right&#34;&gt;2&lt;/td&gt;
&lt;td headers=&#34;event&#34; class=&#34;gt_row gt_right&#34;&gt;1&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;subject&#34; class=&#34;gt_row gt_right&#34;&gt;8&lt;/td&gt;
&lt;td headers=&#34;time&#34; class=&#34;gt_row gt_right&#34;&gt;9&lt;/td&gt;
&lt;td headers=&#34;event&#34; class=&#34;gt_row gt_right&#34;&gt;1&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;subject&#34; class=&#34;gt_row gt_right&#34;&gt;9&lt;/td&gt;
&lt;td headers=&#34;time&#34; class=&#34;gt_row gt_right&#34;&gt;16&lt;/td&gt;
&lt;td headers=&#34;event&#34; class=&#34;gt_row gt_right&#34;&gt;1&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;subject&#34; class=&#34;gt_row gt_right&#34;&gt;10&lt;/td&gt;
&lt;td headers=&#34;time&#34; class=&#34;gt_row gt_right&#34;&gt;5&lt;/td&gt;
&lt;td headers=&#34;event&#34; class=&#34;gt_row gt_right&#34;&gt;0&lt;/td&gt;&lt;/tr&gt;
  &lt;/tbody&gt;
  
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;subject&lt;/code&gt; is the individual’s identifier&lt;/li&gt;
&lt;li&gt;&lt;code&gt;time&lt;/code&gt; is the time to event (in years)&lt;a href=&#34;#fn2&#34; class=&#34;footnote-ref&#34; id=&#34;fnref2&#34;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;event&lt;/code&gt; is the event status (0 = censored, 1 = event happened)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Remember that for each subject, we need to know at least 2 pieces of information:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;the time until the event of interest or the time until the censoring, and&lt;/li&gt;
&lt;li&gt;whether we have observed the event of interest or if we have observed censoring.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We first need to count the number of distinct event times. Ignoring censored observations, we have 5 distinct event times:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;2&lt;/em&gt;, &lt;em&gt;5&lt;/em&gt;, &lt;em&gt;7&lt;/em&gt;, &lt;em&gt;9&lt;/em&gt; and &lt;em&gt;16&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The easiest way to do the calculation by hand is by filling the following table (a table with 5 rows since there are 5 distinct event times):&lt;/p&gt;
&lt;!-- preamble start --&gt;

    &lt;script src=&#34;https://cdn.jsdelivr.net/gh/vincentarelbundock/tinytable@main/inst/tinytable.js&#34;&gt;&lt;/script&gt;

    &lt;script&gt;
      // Create table-specific functions using external factory
      const tableFns_gqi6wniv912vuheaxhyt = TinyTable.createTableFunctions(&#34;tinytable_gqi6wniv912vuheaxhyt&#34;);
      // tinytable span after
      window.addEventListener(&#39;load&#39;, function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: &#39;5&#39;, j: 1 }, { i: &#39;5&#39;, j: 2 }, { i: &#39;5&#39;, j: 3 }, { i: &#39;5&#39;, j: 4 }, { i: &#39;5&#39;, j: 5 } ], css_id: &#39;tinytable_css_63tzhumrxmy06nvag0kk&#39;,}, 
          { positions: [ { i: &#39;1&#39;, j: 1 }, { i: &#39;2&#39;, j: 1 }, { i: &#39;3&#39;, j: 1 }, { i: &#39;4&#39;, j: 1 }, { i: &#39;1&#39;, j: 2 }, { i: &#39;2&#39;, j: 2 }, { i: &#39;3&#39;, j: 2 }, { i: &#39;4&#39;, j: 2 }, { i: &#39;1&#39;, j: 3 }, { i: &#39;2&#39;, j: 3 }, { i: &#39;3&#39;, j: 3 }, { i: &#39;4&#39;, j: 3 }, { i: &#39;1&#39;, j: 4 }, { i: &#39;2&#39;, j: 4 }, { i: &#39;3&#39;, j: 4 }, { i: &#39;4&#39;, j: 4 }, { i: &#39;1&#39;, j: 5 }, { i: &#39;2&#39;, j: 5 }, { i: &#39;3&#39;, j: 5 }, { i: &#39;4&#39;, j: 5 } ], css_id: &#39;tinytable_css_odk2y8mjqugbkfu3h9e3&#39;,}, 
          { positions: [ { i: &#39;0&#39;, j: 1 }, { i: &#39;0&#39;, j: 2 }, { i: &#39;0&#39;, j: 3 }, { i: &#39;0&#39;, j: 4 }, { i: &#39;0&#39;, j: 5 } ], css_id: &#39;tinytable_css_x7q8xsf3ainkuuago1c3&#39;,}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  tableFns_gqi6wniv912vuheaxhyt.styleCell(cell.i, cell.j, group.css_id);
              });
          });
      });
    &lt;/script&gt;

    &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdn.jsdelivr.net/gh/vincentarelbundock/tinytable@main/inst/tinytable.css&#34;&gt;
    &lt;style&gt;
    /* tinytable css entries after */
    #tinytable_gqi6wniv912vuheaxhyt td.tinytable_css_63tzhumrxmy06nvag0kk, #tinytable_gqi6wniv912vuheaxhyt th.tinytable_css_63tzhumrxmy06nvag0kk {  position: relative; --border-bottom: 1; --border-left: 0; --border-right: 0; --border-top: 0; --line-color-bottom: var(--tt-line-color); --line-color-left: var(--tt-line-color); --line-color-right: var(--tt-line-color); --line-color-top: var(--tt-line-color); --line-width-bottom: 0.08em; --line-width-left: 0.1em; --line-width-right: 0.1em; --line-width-top: 0.1em; --trim-bottom-left: 0%; --trim-bottom-right: 0%; --trim-left-bottom: 0%; --trim-left-top: 0%; --trim-right-bottom: 0%; --trim-right-top: 0%; --trim-top-left: 0%; --trim-top-right: 0%; ; text-align: left }
    #tinytable_gqi6wniv912vuheaxhyt td.tinytable_css_odk2y8mjqugbkfu3h9e3, #tinytable_gqi6wniv912vuheaxhyt th.tinytable_css_odk2y8mjqugbkfu3h9e3 { text-align: left }
    #tinytable_gqi6wniv912vuheaxhyt td.tinytable_css_x7q8xsf3ainkuuago1c3, #tinytable_gqi6wniv912vuheaxhyt th.tinytable_css_x7q8xsf3ainkuuago1c3 {  position: relative; --border-bottom: 1; --border-left: 0; --border-right: 0; --border-top: 1; --line-color-bottom: var(--tt-line-color); --line-color-left: var(--tt-line-color); --line-color-right: var(--tt-line-color); --line-color-top: var(--tt-line-color); --line-width-bottom: 0.05em; --line-width-left: 0.1em; --line-width-right: 0.1em; --line-width-top: 0.08em; --trim-bottom-left: 0%; --trim-bottom-right: 0%; --trim-left-bottom: 0%; --trim-left-top: 0%; --trim-right-bottom: 0%; --trim-right-top: 0%; --trim-top-left: 0%; --trim-top-right: 0%; ; text-align: left }
    &lt;/style&gt;
    &lt;div class=&#34;container&#34;&gt;
      &lt;table class=&#34;tinytable&#34; id=&#34;tinytable_gqi6wniv912vuheaxhyt&#34; style=&#34;width: auto; margin-left: auto; margin-right: auto;&#34; data-quarto-disable-processing=&#39;true&#39;&gt;
        
        &lt;thead&gt;
              &lt;tr&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;1&#34;&gt;$j$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;2&#34;&gt;$y_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;3&#34;&gt;$d_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;4&#34;&gt;$R_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;5&#34;&gt;$1 - \frac{d_{(j)}}{R_{(j)}}$&lt;/th&gt;
              &lt;/tr&gt;
        &lt;/thead&gt;
        
        &lt;tbody&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;1&#34;&gt;1&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;2&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;3&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;1&#34;&gt;2&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;2&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;3&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;1&#34;&gt;3&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;2&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;3&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;1&#34;&gt;4&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;2&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;3&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;1&#34;&gt;5&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;2&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;3&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
        &lt;/tbody&gt;
      &lt;/table&gt;
    &lt;/div&gt;
&lt;!-- hack to avoid NA insertion in last line --&gt;
&lt;p&gt;We fill columns one by one:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(y_{(j)}\)&lt;/span&gt; = the ordered distinct event times:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;2&lt;/em&gt;, &lt;em&gt;5&lt;/em&gt;, &lt;em&gt;7&lt;/em&gt;, &lt;em&gt;9&lt;/em&gt; and &lt;em&gt;16&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So the table becomes:&lt;/p&gt;
&lt;!-- preamble start --&gt;

    &lt;script src=&#34;https://cdn.jsdelivr.net/gh/vincentarelbundock/tinytable@main/inst/tinytable.js&#34;&gt;&lt;/script&gt;

    &lt;script&gt;
      // Create table-specific functions using external factory
      const tableFns_pfttpztdnahkfolt80rf = TinyTable.createTableFunctions(&#34;tinytable_pfttpztdnahkfolt80rf&#34;);
      // tinytable span after
      window.addEventListener(&#39;load&#39;, function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: &#39;5&#39;, j: 1 }, { i: &#39;5&#39;, j: 2 }, { i: &#39;5&#39;, j: 3 }, { i: &#39;5&#39;, j: 4 }, { i: &#39;5&#39;, j: 5 } ], css_id: &#39;tinytable_css_6a61u2qsubu2inwtd56y&#39;,}, 
          { positions: [ { i: &#39;1&#39;, j: 1 }, { i: &#39;2&#39;, j: 1 }, { i: &#39;3&#39;, j: 1 }, { i: &#39;4&#39;, j: 1 }, { i: &#39;1&#39;, j: 2 }, { i: &#39;2&#39;, j: 2 }, { i: &#39;3&#39;, j: 2 }, { i: &#39;4&#39;, j: 2 }, { i: &#39;1&#39;, j: 3 }, { i: &#39;2&#39;, j: 3 }, { i: &#39;3&#39;, j: 3 }, { i: &#39;4&#39;, j: 3 }, { i: &#39;1&#39;, j: 4 }, { i: &#39;2&#39;, j: 4 }, { i: &#39;3&#39;, j: 4 }, { i: &#39;4&#39;, j: 4 }, { i: &#39;1&#39;, j: 5 }, { i: &#39;2&#39;, j: 5 }, { i: &#39;3&#39;, j: 5 }, { i: &#39;4&#39;, j: 5 } ], css_id: &#39;tinytable_css_8lne9bdpjx6dn6p4x3gd&#39;,}, 
          { positions: [ { i: &#39;0&#39;, j: 1 }, { i: &#39;0&#39;, j: 2 }, { i: &#39;0&#39;, j: 3 }, { i: &#39;0&#39;, j: 4 }, { i: &#39;0&#39;, j: 5 } ], css_id: &#39;tinytable_css_bniz86tc4lp4m19bq9xc&#39;,}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  tableFns_pfttpztdnahkfolt80rf.styleCell(cell.i, cell.j, group.css_id);
              });
          });
      });
    &lt;/script&gt;

    &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdn.jsdelivr.net/gh/vincentarelbundock/tinytable@main/inst/tinytable.css&#34;&gt;
    &lt;style&gt;
    /* tinytable css entries after */
    #tinytable_pfttpztdnahkfolt80rf td.tinytable_css_6a61u2qsubu2inwtd56y, #tinytable_pfttpztdnahkfolt80rf th.tinytable_css_6a61u2qsubu2inwtd56y {  position: relative; --border-bottom: 1; --border-left: 0; --border-right: 0; --border-top: 0; --line-color-bottom: var(--tt-line-color); --line-color-left: var(--tt-line-color); --line-color-right: var(--tt-line-color); --line-color-top: var(--tt-line-color); --line-width-bottom: 0.08em; --line-width-left: 0.1em; --line-width-right: 0.1em; --line-width-top: 0.1em; --trim-bottom-left: 0%; --trim-bottom-right: 0%; --trim-left-bottom: 0%; --trim-left-top: 0%; --trim-right-bottom: 0%; --trim-right-top: 0%; --trim-top-left: 0%; --trim-top-right: 0%; ; text-align: left }
    #tinytable_pfttpztdnahkfolt80rf td.tinytable_css_8lne9bdpjx6dn6p4x3gd, #tinytable_pfttpztdnahkfolt80rf th.tinytable_css_8lne9bdpjx6dn6p4x3gd { text-align: left }
    #tinytable_pfttpztdnahkfolt80rf td.tinytable_css_bniz86tc4lp4m19bq9xc, #tinytable_pfttpztdnahkfolt80rf th.tinytable_css_bniz86tc4lp4m19bq9xc {  position: relative; --border-bottom: 1; --border-left: 0; --border-right: 0; --border-top: 1; --line-color-bottom: var(--tt-line-color); --line-color-left: var(--tt-line-color); --line-color-right: var(--tt-line-color); --line-color-top: var(--tt-line-color); --line-width-bottom: 0.05em; --line-width-left: 0.1em; --line-width-right: 0.1em; --line-width-top: 0.08em; --trim-bottom-left: 0%; --trim-bottom-right: 0%; --trim-left-bottom: 0%; --trim-left-top: 0%; --trim-right-bottom: 0%; --trim-right-top: 0%; --trim-top-left: 0%; --trim-top-right: 0%; ; text-align: left }
    &lt;/style&gt;
    &lt;div class=&#34;container&#34;&gt;
      &lt;table class=&#34;tinytable&#34; id=&#34;tinytable_pfttpztdnahkfolt80rf&#34; style=&#34;width: auto; margin-left: auto; margin-right: auto;&#34; data-quarto-disable-processing=&#39;true&#39;&gt;
        
        &lt;thead&gt;
              &lt;tr&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;1&#34;&gt;$j$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;2&#34;&gt;$y_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;3&#34;&gt;$d_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;4&#34;&gt;$R_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;5&#34;&gt;$1 - \frac{d_{(j)}}{R_{(j)}}$&lt;/th&gt;
              &lt;/tr&gt;
        &lt;/thead&gt;
        
        &lt;tbody&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;1&#34;&gt;1&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;2&#34;&gt;2&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;3&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;1&#34;&gt;2&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;2&#34;&gt;5&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;3&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;1&#34;&gt;3&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;2&#34;&gt;7&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;3&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;1&#34;&gt;4&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;2&#34;&gt;9&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;3&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;1&#34;&gt;5&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;2&#34;&gt;16&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;3&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
        &lt;/tbody&gt;
      &lt;/table&gt;
    &lt;/div&gt;
&lt;!-- hack to avoid NA insertion in last line --&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(d_{(j)}\)&lt;/span&gt; = the number of observations for each distinct event time. For this, the frequency for each distinct event time is useful:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code&gt;## time
##  2  5  7  9 16 
##  2  1  1  1  2&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The table becomes:&lt;/p&gt;
&lt;!-- preamble start --&gt;

    &lt;script src=&#34;https://cdn.jsdelivr.net/gh/vincentarelbundock/tinytable@main/inst/tinytable.js&#34;&gt;&lt;/script&gt;

    &lt;script&gt;
      // Create table-specific functions using external factory
      const tableFns_jyqpf16iauwvmx2i0oim = TinyTable.createTableFunctions(&#34;tinytable_jyqpf16iauwvmx2i0oim&#34;);
      // tinytable span after
      window.addEventListener(&#39;load&#39;, function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: &#39;5&#39;, j: 1 }, { i: &#39;5&#39;, j: 2 }, { i: &#39;5&#39;, j: 3 }, { i: &#39;5&#39;, j: 4 }, { i: &#39;5&#39;, j: 5 } ], css_id: &#39;tinytable_css_p57hqjnq2thdknhk1o3h&#39;,}, 
          { positions: [ { i: &#39;1&#39;, j: 1 }, { i: &#39;2&#39;, j: 1 }, { i: &#39;3&#39;, j: 1 }, { i: &#39;4&#39;, j: 1 }, { i: &#39;1&#39;, j: 2 }, { i: &#39;2&#39;, j: 2 }, { i: &#39;3&#39;, j: 2 }, { i: &#39;4&#39;, j: 2 }, { i: &#39;1&#39;, j: 3 }, { i: &#39;2&#39;, j: 3 }, { i: &#39;3&#39;, j: 3 }, { i: &#39;4&#39;, j: 3 }, { i: &#39;1&#39;, j: 4 }, { i: &#39;2&#39;, j: 4 }, { i: &#39;3&#39;, j: 4 }, { i: &#39;4&#39;, j: 4 }, { i: &#39;1&#39;, j: 5 }, { i: &#39;2&#39;, j: 5 }, { i: &#39;3&#39;, j: 5 }, { i: &#39;4&#39;, j: 5 } ], css_id: &#39;tinytable_css_uci8h8g7nt7wfi557l1d&#39;,}, 
          { positions: [ { i: &#39;0&#39;, j: 1 }, { i: &#39;0&#39;, j: 2 }, { i: &#39;0&#39;, j: 3 }, { i: &#39;0&#39;, j: 4 }, { i: &#39;0&#39;, j: 5 } ], css_id: &#39;tinytable_css_689d9x9gs8twi4f8vbpv&#39;,}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  tableFns_jyqpf16iauwvmx2i0oim.styleCell(cell.i, cell.j, group.css_id);
              });
          });
      });
    &lt;/script&gt;

    &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdn.jsdelivr.net/gh/vincentarelbundock/tinytable@main/inst/tinytable.css&#34;&gt;
    &lt;style&gt;
    /* tinytable css entries after */
    #tinytable_jyqpf16iauwvmx2i0oim td.tinytable_css_p57hqjnq2thdknhk1o3h, #tinytable_jyqpf16iauwvmx2i0oim th.tinytable_css_p57hqjnq2thdknhk1o3h {  position: relative; --border-bottom: 1; --border-left: 0; --border-right: 0; --border-top: 0; --line-color-bottom: var(--tt-line-color); --line-color-left: var(--tt-line-color); --line-color-right: var(--tt-line-color); --line-color-top: var(--tt-line-color); --line-width-bottom: 0.08em; --line-width-left: 0.1em; --line-width-right: 0.1em; --line-width-top: 0.1em; --trim-bottom-left: 0%; --trim-bottom-right: 0%; --trim-left-bottom: 0%; --trim-left-top: 0%; --trim-right-bottom: 0%; --trim-right-top: 0%; --trim-top-left: 0%; --trim-top-right: 0%; ; text-align: left }
    #tinytable_jyqpf16iauwvmx2i0oim td.tinytable_css_uci8h8g7nt7wfi557l1d, #tinytable_jyqpf16iauwvmx2i0oim th.tinytable_css_uci8h8g7nt7wfi557l1d { text-align: left }
    #tinytable_jyqpf16iauwvmx2i0oim td.tinytable_css_689d9x9gs8twi4f8vbpv, #tinytable_jyqpf16iauwvmx2i0oim th.tinytable_css_689d9x9gs8twi4f8vbpv {  position: relative; --border-bottom: 1; --border-left: 0; --border-right: 0; --border-top: 1; --line-color-bottom: var(--tt-line-color); --line-color-left: var(--tt-line-color); --line-color-right: var(--tt-line-color); --line-color-top: var(--tt-line-color); --line-width-bottom: 0.05em; --line-width-left: 0.1em; --line-width-right: 0.1em; --line-width-top: 0.08em; --trim-bottom-left: 0%; --trim-bottom-right: 0%; --trim-left-bottom: 0%; --trim-left-top: 0%; --trim-right-bottom: 0%; --trim-right-top: 0%; --trim-top-left: 0%; --trim-top-right: 0%; ; text-align: left }
    &lt;/style&gt;
    &lt;div class=&#34;container&#34;&gt;
      &lt;table class=&#34;tinytable&#34; id=&#34;tinytable_jyqpf16iauwvmx2i0oim&#34; style=&#34;width: auto; margin-left: auto; margin-right: auto;&#34; data-quarto-disable-processing=&#39;true&#39;&gt;
        
        &lt;thead&gt;
              &lt;tr&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;1&#34;&gt;$j$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;2&#34;&gt;$y_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;3&#34;&gt;$d_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;4&#34;&gt;$R_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;5&#34;&gt;$1 - \frac{d_{(j)}}{R_{(j)}}$&lt;/th&gt;
              &lt;/tr&gt;
        &lt;/thead&gt;
        
        &lt;tbody&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;1&#34;&gt;1&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;2&#34;&gt;2&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;3&#34;&gt;2&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;1&#34;&gt;2&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;2&#34;&gt;5&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;3&#34;&gt;1&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;1&#34;&gt;3&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;2&#34;&gt;7&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;3&#34;&gt;1&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;1&#34;&gt;4&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;2&#34;&gt;9&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;3&#34;&gt;1&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;1&#34;&gt;5&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;2&#34;&gt;16&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;3&#34;&gt;2&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;4&#34;&gt;&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
        &lt;/tbody&gt;
      &lt;/table&gt;
    &lt;/div&gt;
&lt;!-- hack to avoid NA insertion in last line --&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(R_{(j)}\)&lt;/span&gt; = the remaining number of individuals at risk. For this, the distribution of time (censored and not censored) is useful:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code&gt;## time
##  2  3  5  7  9 16 18 
##  2  1  2  1  1  2  1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We see that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;At the beginning there are 10 subjects&lt;/li&gt;
&lt;li&gt;Just before time &lt;span class=&#34;math inline&#34;&gt;\(t = 5\)&lt;/span&gt;, there are 7 subjects left (10 subjects - 2 who had the event - 1 who is censored)&lt;/li&gt;
&lt;li&gt;Just before time &lt;span class=&#34;math inline&#34;&gt;\(t = 7\)&lt;/span&gt;, there are 5 subjects left (= 10 - 2 - 1 - 2)&lt;/li&gt;
&lt;li&gt;Just before time &lt;span class=&#34;math inline&#34;&gt;\(t = 9\)&lt;/span&gt;, there are 4 subjects left (= 10 - 2 - 1 - 2 - 1)&lt;/li&gt;
&lt;li&gt;Just before time &lt;span class=&#34;math inline&#34;&gt;\(t = 16\)&lt;/span&gt;, there are 3 subjects left (= 10 - 2 - 1 - 2 - 1 - 1)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The table becomes:&lt;/p&gt;
&lt;!-- preamble start --&gt;

    &lt;script src=&#34;https://cdn.jsdelivr.net/gh/vincentarelbundock/tinytable@main/inst/tinytable.js&#34;&gt;&lt;/script&gt;

    &lt;script&gt;
      // Create table-specific functions using external factory
      const tableFns_13fjo3fhqofxmr77b0ro = TinyTable.createTableFunctions(&#34;tinytable_13fjo3fhqofxmr77b0ro&#34;);
      // tinytable span after
      window.addEventListener(&#39;load&#39;, function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: &#39;5&#39;, j: 1 }, { i: &#39;5&#39;, j: 2 }, { i: &#39;5&#39;, j: 3 }, { i: &#39;5&#39;, j: 4 }, { i: &#39;5&#39;, j: 5 } ], css_id: &#39;tinytable_css_05oab03bspdqdzznbf9c&#39;,}, 
          { positions: [ { i: &#39;1&#39;, j: 1 }, { i: &#39;2&#39;, j: 1 }, { i: &#39;3&#39;, j: 1 }, { i: &#39;4&#39;, j: 1 }, { i: &#39;1&#39;, j: 2 }, { i: &#39;2&#39;, j: 2 }, { i: &#39;3&#39;, j: 2 }, { i: &#39;4&#39;, j: 2 }, { i: &#39;1&#39;, j: 3 }, { i: &#39;2&#39;, j: 3 }, { i: &#39;3&#39;, j: 3 }, { i: &#39;4&#39;, j: 3 }, { i: &#39;1&#39;, j: 4 }, { i: &#39;2&#39;, j: 4 }, { i: &#39;3&#39;, j: 4 }, { i: &#39;4&#39;, j: 4 }, { i: &#39;1&#39;, j: 5 }, { i: &#39;2&#39;, j: 5 }, { i: &#39;3&#39;, j: 5 }, { i: &#39;4&#39;, j: 5 } ], css_id: &#39;tinytable_css_h96li1wtavwkgoy72cy6&#39;,}, 
          { positions: [ { i: &#39;0&#39;, j: 1 }, { i: &#39;0&#39;, j: 2 }, { i: &#39;0&#39;, j: 3 }, { i: &#39;0&#39;, j: 4 }, { i: &#39;0&#39;, j: 5 } ], css_id: &#39;tinytable_css_cb86r59qgslap7na03m7&#39;,}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  tableFns_13fjo3fhqofxmr77b0ro.styleCell(cell.i, cell.j, group.css_id);
              });
          });
      });
    &lt;/script&gt;

    &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdn.jsdelivr.net/gh/vincentarelbundock/tinytable@main/inst/tinytable.css&#34;&gt;
    &lt;style&gt;
    /* tinytable css entries after */
    #tinytable_13fjo3fhqofxmr77b0ro td.tinytable_css_05oab03bspdqdzznbf9c, #tinytable_13fjo3fhqofxmr77b0ro th.tinytable_css_05oab03bspdqdzznbf9c {  position: relative; --border-bottom: 1; --border-left: 0; --border-right: 0; --border-top: 0; --line-color-bottom: var(--tt-line-color); --line-color-left: var(--tt-line-color); --line-color-right: var(--tt-line-color); --line-color-top: var(--tt-line-color); --line-width-bottom: 0.08em; --line-width-left: 0.1em; --line-width-right: 0.1em; --line-width-top: 0.1em; --trim-bottom-left: 0%; --trim-bottom-right: 0%; --trim-left-bottom: 0%; --trim-left-top: 0%; --trim-right-bottom: 0%; --trim-right-top: 0%; --trim-top-left: 0%; --trim-top-right: 0%; ; text-align: left }
    #tinytable_13fjo3fhqofxmr77b0ro td.tinytable_css_h96li1wtavwkgoy72cy6, #tinytable_13fjo3fhqofxmr77b0ro th.tinytable_css_h96li1wtavwkgoy72cy6 { text-align: left }
    #tinytable_13fjo3fhqofxmr77b0ro td.tinytable_css_cb86r59qgslap7na03m7, #tinytable_13fjo3fhqofxmr77b0ro th.tinytable_css_cb86r59qgslap7na03m7 {  position: relative; --border-bottom: 1; --border-left: 0; --border-right: 0; --border-top: 1; --line-color-bottom: var(--tt-line-color); --line-color-left: var(--tt-line-color); --line-color-right: var(--tt-line-color); --line-color-top: var(--tt-line-color); --line-width-bottom: 0.05em; --line-width-left: 0.1em; --line-width-right: 0.1em; --line-width-top: 0.08em; --trim-bottom-left: 0%; --trim-bottom-right: 0%; --trim-left-bottom: 0%; --trim-left-top: 0%; --trim-right-bottom: 0%; --trim-right-top: 0%; --trim-top-left: 0%; --trim-top-right: 0%; ; text-align: left }
    &lt;/style&gt;
    &lt;div class=&#34;container&#34;&gt;
      &lt;table class=&#34;tinytable&#34; id=&#34;tinytable_13fjo3fhqofxmr77b0ro&#34; style=&#34;width: auto; margin-left: auto; margin-right: auto;&#34; data-quarto-disable-processing=&#39;true&#39;&gt;
        
        &lt;thead&gt;
              &lt;tr&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;1&#34;&gt;$j$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;2&#34;&gt;$y_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;3&#34;&gt;$d_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;4&#34;&gt;$R_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;5&#34;&gt;$1 - \frac{d_{(j)}}{R_{(j)}}$&lt;/th&gt;
              &lt;/tr&gt;
        &lt;/thead&gt;
        
        &lt;tbody&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;1&#34;&gt;1&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;2&#34;&gt;2&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;3&#34;&gt;2&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;4&#34;&gt;10&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;1&#34;&gt;2&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;2&#34;&gt;5&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;3&#34;&gt;1&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;4&#34;&gt;7&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;1&#34;&gt;3&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;2&#34;&gt;7&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;3&#34;&gt;1&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;4&#34;&gt;5&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;1&#34;&gt;4&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;2&#34;&gt;9&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;3&#34;&gt;1&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;4&#34;&gt;4&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;1&#34;&gt;5&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;2&#34;&gt;16&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;3&#34;&gt;2&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;4&#34;&gt;3&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;5&#34;&gt;&lt;/td&gt;
                &lt;/tr&gt;
        &lt;/tbody&gt;
      &lt;/table&gt;
    &lt;/div&gt;
&lt;!-- hack to avoid NA insertion in last line --&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(1 - \frac{d_{(j)}}{R_{(j)}}\)&lt;/span&gt; is straightforward, so the table becomes:&lt;/li&gt;
&lt;/ul&gt;
&lt;!-- preamble start --&gt;

    &lt;script src=&#34;https://cdn.jsdelivr.net/gh/vincentarelbundock/tinytable@main/inst/tinytable.js&#34;&gt;&lt;/script&gt;

    &lt;script&gt;
      // Create table-specific functions using external factory
      const tableFns_pwro8an6lcdmghyyn84k = TinyTable.createTableFunctions(&#34;tinytable_pwro8an6lcdmghyyn84k&#34;);
      // tinytable span after
      window.addEventListener(&#39;load&#39;, function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: &#39;5&#39;, j: 1 }, { i: &#39;5&#39;, j: 2 }, { i: &#39;5&#39;, j: 3 }, { i: &#39;5&#39;, j: 4 }, { i: &#39;5&#39;, j: 5 } ], css_id: &#39;tinytable_css_kvroag0647on9aibrtmk&#39;,}, 
          { positions: [ { i: &#39;1&#39;, j: 1 }, { i: &#39;2&#39;, j: 1 }, { i: &#39;3&#39;, j: 1 }, { i: &#39;4&#39;, j: 1 }, { i: &#39;1&#39;, j: 2 }, { i: &#39;2&#39;, j: 2 }, { i: &#39;3&#39;, j: 2 }, { i: &#39;4&#39;, j: 2 }, { i: &#39;1&#39;, j: 3 }, { i: &#39;2&#39;, j: 3 }, { i: &#39;3&#39;, j: 3 }, { i: &#39;4&#39;, j: 3 }, { i: &#39;1&#39;, j: 4 }, { i: &#39;2&#39;, j: 4 }, { i: &#39;3&#39;, j: 4 }, { i: &#39;4&#39;, j: 4 }, { i: &#39;1&#39;, j: 5 }, { i: &#39;2&#39;, j: 5 }, { i: &#39;3&#39;, j: 5 }, { i: &#39;4&#39;, j: 5 } ], css_id: &#39;tinytable_css_jdhilsydd0ovjj4g5zxc&#39;,}, 
          { positions: [ { i: &#39;0&#39;, j: 1 }, { i: &#39;0&#39;, j: 2 }, { i: &#39;0&#39;, j: 3 }, { i: &#39;0&#39;, j: 4 }, { i: &#39;0&#39;, j: 5 } ], css_id: &#39;tinytable_css_2l0ch1qqpad19ftfdsmj&#39;,}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  tableFns_pwro8an6lcdmghyyn84k.styleCell(cell.i, cell.j, group.css_id);
              });
          });
      });
    &lt;/script&gt;

    &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdn.jsdelivr.net/gh/vincentarelbundock/tinytable@main/inst/tinytable.css&#34;&gt;
    &lt;style&gt;
    /* tinytable css entries after */
    #tinytable_pwro8an6lcdmghyyn84k td.tinytable_css_kvroag0647on9aibrtmk, #tinytable_pwro8an6lcdmghyyn84k th.tinytable_css_kvroag0647on9aibrtmk {  position: relative; --border-bottom: 1; --border-left: 0; --border-right: 0; --border-top: 0; --line-color-bottom: var(--tt-line-color); --line-color-left: var(--tt-line-color); --line-color-right: var(--tt-line-color); --line-color-top: var(--tt-line-color); --line-width-bottom: 0.08em; --line-width-left: 0.1em; --line-width-right: 0.1em; --line-width-top: 0.1em; --trim-bottom-left: 0%; --trim-bottom-right: 0%; --trim-left-bottom: 0%; --trim-left-top: 0%; --trim-right-bottom: 0%; --trim-right-top: 0%; --trim-top-left: 0%; --trim-top-right: 0%; ; text-align: left }
    #tinytable_pwro8an6lcdmghyyn84k td.tinytable_css_jdhilsydd0ovjj4g5zxc, #tinytable_pwro8an6lcdmghyyn84k th.tinytable_css_jdhilsydd0ovjj4g5zxc { text-align: left }
    #tinytable_pwro8an6lcdmghyyn84k td.tinytable_css_2l0ch1qqpad19ftfdsmj, #tinytable_pwro8an6lcdmghyyn84k th.tinytable_css_2l0ch1qqpad19ftfdsmj {  position: relative; --border-bottom: 1; --border-left: 0; --border-right: 0; --border-top: 1; --line-color-bottom: var(--tt-line-color); --line-color-left: var(--tt-line-color); --line-color-right: var(--tt-line-color); --line-color-top: var(--tt-line-color); --line-width-bottom: 0.05em; --line-width-left: 0.1em; --line-width-right: 0.1em; --line-width-top: 0.08em; --trim-bottom-left: 0%; --trim-bottom-right: 0%; --trim-left-bottom: 0%; --trim-left-top: 0%; --trim-right-bottom: 0%; --trim-right-top: 0%; --trim-top-left: 0%; --trim-top-right: 0%; ; text-align: left }
    &lt;/style&gt;
    &lt;div class=&#34;container&#34;&gt;
      &lt;table class=&#34;tinytable&#34; id=&#34;tinytable_pwro8an6lcdmghyyn84k&#34; style=&#34;width: auto; margin-left: auto; margin-right: auto;&#34; data-quarto-disable-processing=&#39;true&#39;&gt;
        
        &lt;thead&gt;
              &lt;tr&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;1&#34;&gt;$j$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;2&#34;&gt;$y_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;3&#34;&gt;$d_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;4&#34;&gt;$R_{(j)}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;5&#34;&gt;$1 - \frac{d_{(j)}}{R_{(j)}}$&lt;/th&gt;
              &lt;/tr&gt;
        &lt;/thead&gt;
        
        &lt;tbody&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;1&#34;&gt;1.00&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;2&#34;&gt;2.00&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;3&#34;&gt;2.00&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;4&#34;&gt;10.00&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;5&#34;&gt;0.80&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;1&#34;&gt;2.00&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;2&#34;&gt;5.00&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;3&#34;&gt;1.00&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;4&#34;&gt;7.00&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;5&#34;&gt;0.86&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;1&#34;&gt;3.00&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;2&#34;&gt;7.00&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;3&#34;&gt;1.00&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;4&#34;&gt;5.00&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;5&#34;&gt;0.80&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;1&#34;&gt;4.00&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;2&#34;&gt;9.00&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;3&#34;&gt;1.00&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;4&#34;&gt;4.00&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;5&#34;&gt;0.75&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;1&#34;&gt;5.00&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;2&#34;&gt;16.00&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;3&#34;&gt;2.00&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;4&#34;&gt;3.00&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;5&#34;&gt;0.33&lt;/td&gt;
                &lt;/tr&gt;
        &lt;/tbody&gt;
      &lt;/table&gt;
    &lt;/div&gt;
&lt;!-- hack to avoid NA insertion in last line --&gt;
&lt;p&gt;The Kaplan-Meier estimator is:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\hat{S}_{KM}(t) = \prod_{j:y_{(j)} \le t} \left(1 - \frac{d_{(j)}}{R_{(j)}} \right)\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;For each &lt;span class=&#34;math inline&#34;&gt;\(j\)&lt;/span&gt;, we thus take the cumulative product:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(j_1 = 0.8\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(j_2 = 0.8 \cdot 0.857 = 0.6856\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(j_3 = 0.6856 \cdot 0.8 = 0.54848\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(j_4 = 0.54848 \cdot 0.75 = 0.41136\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(j_5 = 0.41136 \cdot 0.333 = 0.1369829\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So finally, we have the survival probabilities (rounded to 3 digits):&lt;/p&gt;
&lt;!-- preamble start --&gt;

    &lt;script src=&#34;https://cdn.jsdelivr.net/gh/vincentarelbundock/tinytable@main/inst/tinytable.js&#34;&gt;&lt;/script&gt;

    &lt;script&gt;
      // Create table-specific functions using external factory
      const tableFns_9g2vmc4l85a2ai8cd30l = TinyTable.createTableFunctions(&#34;tinytable_9g2vmc4l85a2ai8cd30l&#34;);
      // tinytable span after
      window.addEventListener(&#39;load&#39;, function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: &#39;5&#39;, j: 1 }, { i: &#39;5&#39;, j: 2 }, { i: &#39;5&#39;, j: 3 } ], css_id: &#39;tinytable_css_0gsa3vc5mq6m4zyan8g2&#39;,}, 
          { positions: [ { i: &#39;1&#39;, j: 1 }, { i: &#39;2&#39;, j: 1 }, { i: &#39;3&#39;, j: 1 }, { i: &#39;4&#39;, j: 1 }, { i: &#39;1&#39;, j: 2 }, { i: &#39;2&#39;, j: 2 }, { i: &#39;3&#39;, j: 2 }, { i: &#39;4&#39;, j: 2 }, { i: &#39;1&#39;, j: 3 }, { i: &#39;2&#39;, j: 3 }, { i: &#39;3&#39;, j: 3 }, { i: &#39;4&#39;, j: 3 } ], css_id: &#39;tinytable_css_qudi29yolqth1f9pviev&#39;,}, 
          { positions: [ { i: &#39;0&#39;, j: 1 }, { i: &#39;0&#39;, j: 2 }, { i: &#39;0&#39;, j: 3 } ], css_id: &#39;tinytable_css_v46u97wgn8bmam0b8xgb&#39;,}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  tableFns_9g2vmc4l85a2ai8cd30l.styleCell(cell.i, cell.j, group.css_id);
              });
          });
      });
    &lt;/script&gt;

    &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdn.jsdelivr.net/gh/vincentarelbundock/tinytable@main/inst/tinytable.css&#34;&gt;
    &lt;style&gt;
    /* tinytable css entries after */
    #tinytable_9g2vmc4l85a2ai8cd30l td.tinytable_css_0gsa3vc5mq6m4zyan8g2, #tinytable_9g2vmc4l85a2ai8cd30l th.tinytable_css_0gsa3vc5mq6m4zyan8g2 {  position: relative; --border-bottom: 1; --border-left: 0; --border-right: 0; --border-top: 0; --line-color-bottom: var(--tt-line-color); --line-color-left: var(--tt-line-color); --line-color-right: var(--tt-line-color); --line-color-top: var(--tt-line-color); --line-width-bottom: 0.08em; --line-width-left: 0.1em; --line-width-right: 0.1em; --line-width-top: 0.1em; --trim-bottom-left: 0%; --trim-bottom-right: 0%; --trim-left-bottom: 0%; --trim-left-top: 0%; --trim-right-bottom: 0%; --trim-right-top: 0%; --trim-top-left: 0%; --trim-top-right: 0%; ; text-align: left }
    #tinytable_9g2vmc4l85a2ai8cd30l td.tinytable_css_qudi29yolqth1f9pviev, #tinytable_9g2vmc4l85a2ai8cd30l th.tinytable_css_qudi29yolqth1f9pviev { text-align: left }
    #tinytable_9g2vmc4l85a2ai8cd30l td.tinytable_css_v46u97wgn8bmam0b8xgb, #tinytable_9g2vmc4l85a2ai8cd30l th.tinytable_css_v46u97wgn8bmam0b8xgb {  position: relative; --border-bottom: 1; --border-left: 0; --border-right: 0; --border-top: 1; --line-color-bottom: var(--tt-line-color); --line-color-left: var(--tt-line-color); --line-color-right: var(--tt-line-color); --line-color-top: var(--tt-line-color); --line-width-bottom: 0.05em; --line-width-left: 0.1em; --line-width-right: 0.1em; --line-width-top: 0.08em; --trim-bottom-left: 0%; --trim-bottom-right: 0%; --trim-left-bottom: 0%; --trim-left-top: 0%; --trim-right-bottom: 0%; --trim-right-top: 0%; --trim-top-left: 0%; --trim-top-right: 0%; ; text-align: left }
    &lt;/style&gt;
    &lt;div class=&#34;container&#34;&gt;
      &lt;table class=&#34;tinytable&#34; id=&#34;tinytable_9g2vmc4l85a2ai8cd30l&#34; style=&#34;width: auto; margin-left: auto; margin-right: auto;&#34; data-quarto-disable-processing=&#39;true&#39;&gt;
        
        &lt;thead&gt;
              &lt;tr&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;1&#34;&gt;$j$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;2&#34;&gt;$1 - \frac{d_{(j)}}{R_{(j)}}$&lt;/th&gt;
                &lt;th scope=&#34;col&#34; data-row=&#34;0&#34; data-col=&#34;3&#34;&gt;$\hat{S}_{KM}(t)$&lt;/th&gt;
              &lt;/tr&gt;
        &lt;/thead&gt;
        
        &lt;tbody&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;1&#34;&gt;1.00&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;2&#34;&gt;0.80&lt;/td&gt;
                  &lt;td data-row=&#34;1&#34; data-col=&#34;3&#34;&gt;0.80&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;1&#34;&gt;2.00&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;2&#34;&gt;0.86&lt;/td&gt;
                  &lt;td data-row=&#34;2&#34; data-col=&#34;3&#34;&gt;0.69&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;1&#34;&gt;3.00&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;2&#34;&gt;0.80&lt;/td&gt;
                  &lt;td data-row=&#34;3&#34; data-col=&#34;3&#34;&gt;0.55&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;1&#34;&gt;4.00&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;2&#34;&gt;0.75&lt;/td&gt;
                  &lt;td data-row=&#34;4&#34; data-col=&#34;3&#34;&gt;0.41&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;1&#34;&gt;5.00&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;2&#34;&gt;0.33&lt;/td&gt;
                  &lt;td data-row=&#34;5&#34; data-col=&#34;3&#34;&gt;0.14&lt;/td&gt;
                &lt;/tr&gt;
        &lt;/tbody&gt;
      &lt;/table&gt;
    &lt;/div&gt;
&lt;!-- hack to avoid NA insertion in last line --&gt;
&lt;p&gt;We can now represent graphically the Kaplan-Meier estimator:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/what-is-survival-analysis/index_files/figure-html/unnamed-chunk-13-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;To draw this survival curve, remember that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the x-axis corresponds to the &lt;code&gt;time&lt;/code&gt; variable in the initial dataset, and&lt;/li&gt;
&lt;li&gt;the y-axis corresponds to the survival probabilities found above.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;in-r&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;In R&lt;/h2&gt;
&lt;p&gt;We now compare our results with the results found in R.&lt;/p&gt;
&lt;p&gt;We first create the dataset with the time and event variables:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# create dataset
dat &amp;lt;- data.frame(
  time = c(3, 5, 7, 2, 18, 16, 2, 9, 16, 5),
  event = c(0, 1, 1, 1, 0, 1, 1, 1, 1, 0)
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We then run the Kaplan-Meier estimator with the &lt;code&gt;survfit()&lt;/code&gt; and &lt;code&gt;Surv()&lt;/code&gt; functions:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# KM
library(survival)

km &amp;lt;- survfit(Surv(time, event) ~ 1,
  data = dat
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice that the &lt;code&gt;Surv()&lt;/code&gt; function accepts two arguments:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;the &lt;code&gt;time&lt;/code&gt; variable, and&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;event&lt;/code&gt; variable.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The &lt;code&gt;~ 1&lt;/code&gt; in the &lt;code&gt;survfit()&lt;/code&gt; function indicates that we estimate the Kaplan-Meier without any grouping. See more on this later in the post.&lt;/p&gt;
&lt;p&gt;Finally, we display the results and draw the Kaplan-Meier plot:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# results
summary(km)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Call: survfit(formula = Surv(time, event) ~ 1, data = dat)
## 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     2     10       2    0.800   0.126       0.5868        1.000
##     5      7       1    0.686   0.151       0.4447        1.000
##     7      5       1    0.549   0.172       0.2963        1.000
##     9      4       1    0.411   0.176       0.1782        0.950
##    16      3       2    0.137   0.126       0.0225        0.834&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# plot
plot(km,
  xlab = &amp;quot;Time&amp;quot;,
  ylab = &amp;quot;Survival probability&amp;quot;,
  conf.int = FALSE
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/what-is-survival-analysis/index_files/figure-html/unnamed-chunk-16-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The survival probabilities can be found in the &lt;code&gt;survival&lt;/code&gt; column. Remark that results by hand and in R are similar (any difference with the results by hand is due to rounding).&lt;/p&gt;
&lt;p&gt;Alternatively, we can use the &lt;code&gt;ggsurvplot()&lt;/code&gt; function within the &lt;code&gt;{survminer}&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(survminer)

# plot
ggsurvplot(km,
  conf.int = FALSE,
  legend = &amp;quot;none&amp;quot;
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/what-is-survival-analysis/index_files/figure-html/unnamed-chunk-17-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note that the crosses on the survival curve denote the censored observations.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The advantage with the &lt;code&gt;ggsurvplot()&lt;/code&gt; function is that it is easy to draw the &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/#median&#34;&gt;median&lt;/a&gt; survival directly on the plot:&lt;a href=&#34;#fn3&#34; class=&#34;footnote-ref&#34; id=&#34;fnref3&#34;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggsurvplot(km,
  conf.int = FALSE,
  surv.median.line = &amp;quot;hv&amp;quot;,
  legend = &amp;quot;none&amp;quot;
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/what-is-survival-analysis/index_files/figure-html/unnamed-chunk-18-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;To find the median survival:&lt;a href=&#34;#fn4&#34; class=&#34;footnote-ref&#34; id=&#34;fnref4&#34;&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;summary(km)$table[&amp;quot;median&amp;quot;]&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## median 
##      9&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# or more simply
km&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Call: survfit(formula = Surv(time, event) ~ 1, data = dat)
## 
##       n events median 0.95LCL 0.95UCL
## [1,] 10      7      9       5      NA&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Suppose that the event of interest is death:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;At time zero, the survival probability is 1 (100% of the subjects are alive).&lt;/li&gt;
&lt;li&gt;The median indicates that the median survival time is 9 years.&lt;a href=&#34;#fn5&#34; class=&#34;footnote-ref&#34; id=&#34;fnref5&#34;&gt;&lt;sup&gt;5&lt;/sup&gt;&lt;/a&gt; This is the time at which the survival &lt;span class=&#34;math inline&#34;&gt;\(S(t)\)&lt;/span&gt; is 50%. In other words, is the time after which half of the subjects are expected to have died.&lt;/li&gt;
&lt;li&gt;From the plot, we also see that &lt;span class=&#34;math inline&#34;&gt;\(S(5) = P(T &amp;gt; 5 \text{ years}) =\)&lt;/span&gt; Probability of survival of more than 5 years for these subjects = 68.6%. This means that 68.6% of all subjects survive longer than 5 years, and that 31.4% of all subjects die within the first 5 years.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For the sake of completeness, let’s do another example with a much larger dataset; the &lt;code&gt;tongue&lt;/code&gt; dataset within the &lt;code&gt;{KMsurv}&lt;/code&gt; package.&lt;a href=&#34;#fn6&#34; class=&#34;footnote-ref&#34; id=&#34;fnref6&#34;&gt;&lt;sup&gt;6&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# load data
library(KMsurv)
data(tongue)

# preview data
head(tongue)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   type time delta
## 1    1    1     1
## 2    1    3     1
## 3    1    3     1
## 4    1    4     1
## 5    1   10     1
## 6    1   13     1&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;type&lt;/code&gt; is the tumor DNA profile (1 = aneuploid tumor, 2 = diploid tumor)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;time&lt;/code&gt; is the time to death or on-study time (in weeks)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;delta&lt;/code&gt; is the death indicator (0 = alive, 1 = dead)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For this example, we focus on the aneuploid type:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;anaploid &amp;lt;- subset(tongue, type == 1)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can now plot the estimated survival function and estimate the median time to death. Since it is an estimator, we can also construct a confidence interval for the estimated survival at each time &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt; and for the estimated median survival time.&lt;a href=&#34;#fn7&#34; class=&#34;footnote-ref&#34; id=&#34;fnref7&#34;&gt;&lt;sup&gt;7&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# results
fit &amp;lt;- survfit(Surv(time, delta) ~ 1,
  data = anaploid,
  conf.type = &amp;quot;log-log&amp;quot;
)

fit&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Call: survfit(formula = Surv(time, delta) ~ 1, data = anaploid, conf.type = &amp;quot;log-log&amp;quot;)
## 
##       n events median 0.95LCL 0.95UCL
## [1,] 52     31     93      65     157&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# plot
ggsurvplot(fit,
  surv.median.line = &amp;quot;hv&amp;quot;,
  legend = &amp;quot;none&amp;quot;
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/what-is-survival-analysis/index_files/figure-html/unnamed-chunk-22-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The median survival time is estimated to be 93 weeks, with a 95% confidence interval between 65 and 157 weeks.&lt;/p&gt;
&lt;p&gt;Kaplan-Meier curves can be seen as &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/&#34;&gt;descriptive statistics&lt;/a&gt; for survival data. We now focus on the second branch of statistics, &lt;a href=&#34;https://statsandr.com/blog/hypothesis-test-by-hand/&#34;&gt;hypothesis testing&lt;/a&gt; which allows to draw conclusions on the population based on a sample (see a quick reminder about the &lt;a href=&#34;https://statsandr.com/blog/what-is-the-difference-between-population-and-sample/&#34;&gt;difference between population and sample&lt;/a&gt; if you need).&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;hypothesis-testing&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Hypothesis testing&lt;/h1&gt;
&lt;p&gt;Hypothesis testing in the field of survival analysis mostly concerns:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The hazard function of &lt;strong&gt;one population&lt;/strong&gt;: in this case we test whether a censored sample comes from a population with a known hazard function &lt;span class=&#34;math inline&#34;&gt;\(h_0(t)\)&lt;/span&gt;. For example, we may be interested to compare survival in a sample of patients to the survival in the overall population (derived from the life tables).&lt;/li&gt;
&lt;li&gt;The comparison of the hazard function of &lt;strong&gt;two or more populations&lt;/strong&gt;: in this case we are interested in assessing whether there are differences in survival among different groups of subjects. For example:
&lt;ul&gt;
&lt;li&gt;2 groups: we are interested in comparing survival for female and male colon cancer patients&lt;/li&gt;
&lt;li&gt;3 groups or more: we are interested in comparing survival for melanoma cancer patients according to their treatments (with treatments A, B and C for example)&lt;a href=&#34;#fn8&#34; class=&#34;footnote-ref&#34; id=&#34;fnref8&#34;&gt;&lt;sup&gt;8&lt;/sup&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div id=&#34;log-rank-test&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Log-rank test&lt;/h2&gt;
&lt;p&gt;In this article, we focus on comparing survival between two groups using the &lt;strong&gt;log-rank test&lt;/strong&gt; (also known as Mantel-Cox test). This test is the most common hypothesis test to compare survival between two groups.&lt;/p&gt;
&lt;p&gt;The intuition behind the test is that if the two groups have different hazard rates, the two survival curves (so their slopes) will differ. More precisely, the log-rank test compares the observed number of events in each group to what would be expected if the survival curves were identical (i.e., if the null hypothesis were true).&lt;/p&gt;
&lt;p&gt;Note that, as for the Kaplan-Meier estimator, the log-rank test is a nonparametric test, which makes no assumptions about the survival distributions.&lt;/p&gt;
&lt;p&gt;For this example, consider the following dataset:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;##    patient group time event
## 1        1     1  4.1     1
## 2        2     1  7.8     0
## 3        3     1 10.0     1
## 4        4     1 10.0     1
## 5        5     1 12.3     0
## 6        6     1 17.2     1
## 7        7     2  9.7     1
## 8        8     2 10.0     1
## 9        9     2 11.1     0
## 10      10     2 13.1     0
## 11      11     2 19.7     1
## 12      12     2 24.1     0&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;patient&lt;/code&gt; is the patient’s identifier&lt;/li&gt;
&lt;li&gt;&lt;code&gt;group&lt;/code&gt; is the group (group 1 or 2)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;time&lt;/code&gt; is the time to death (in years)&lt;a href=&#34;#fn9&#34; class=&#34;footnote-ref&#34; id=&#34;fnref9&#34;&gt;&lt;sup&gt;9&lt;/sup&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;event&lt;/code&gt; is the event status (0 = censored, 1 = death)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Suppose we are interested in comparing group 1 and 2 in terms of survival, that is, we compare survival curves between the 2 groups:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(H_0 : S_1(t) = S_2(t)\)&lt;/span&gt; for all &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(H_1 : S_1(t) \ne S_2(t)\)&lt;/span&gt; for some &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It is a statistical test, so if the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value &amp;lt; &lt;span class=&#34;math inline&#34;&gt;\(\alpha\)&lt;/span&gt; (usually 0.05), we reject the null hypothesis and we conclude that survival (or the time to event) is significantly different between the two groups considered.&lt;/p&gt;
&lt;p&gt;To perform the log-rank test, the following test statistic will be useful:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{eqnarray}
U &amp;amp;=&amp;amp; \sum_{j=1}^r w(y_{(j)})\left(O_j - E_j\right) \\
&amp;amp;=&amp;amp; \sum_{j=1}^r w(y_{(j)})\left( d_{(j)1} - \frac{d_{(j)}R_{(j)1}}{R_{(j)}}\right)
\end{eqnarray}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;with &lt;span class=&#34;math inline&#34;&gt;\(U^{obs} = \frac{U}{\sqrt{Var(U)}} \sim N(0,1)\)&lt;/span&gt; and&lt;a href=&#34;#fn10&#34; class=&#34;footnote-ref&#34; id=&#34;fnref10&#34;&gt;&lt;sup&gt;10&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{eqnarray}
Var(U) &amp;amp;=&amp;amp; \sum_{j=1}^r w^2(y_{(j)}) \frac{N_{(j)}}{ D_{(j)}  }\\
&amp;amp;=&amp;amp; \sum_{j=1}^r w^2(y_{(j)}) \frac{ d_{(j)} \frac{R_{(j)1}}{R_{(j)} } \left( 1 - \frac{R_{(j)1}}{R_{(j)} } \right) \left( R_{(j)} - d_{(j)}\right) }{ R_{(j)} - 1  }
\end{eqnarray}\]&lt;/span&gt;&lt;/p&gt;
&lt;div id=&#34;by-hand-1&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;By hand&lt;/h3&gt;
&lt;p&gt;As for the Kaplan-Meier estimator by hand, it is best to also fill in a table for the log-rank test by hand.&lt;/p&gt;
&lt;p&gt;Let’s present the final table and comment below on how to fill it, column by column:&lt;/p&gt;
&lt;table style=&#34;width:100%;&#34;&gt;
&lt;colgroup&gt;
&lt;col width=&#34;5%&#34; /&gt;
&lt;col width=&#34;6%&#34; /&gt;
&lt;col width=&#34;7%&#34; /&gt;
&lt;col width=&#34;7%&#34; /&gt;
&lt;col width=&#34;7%&#34; /&gt;
&lt;col width=&#34;7%&#34; /&gt;
&lt;col width=&#34;6%&#34; /&gt;
&lt;col width=&#34;6%&#34; /&gt;
&lt;col width=&#34;5%&#34; /&gt;
&lt;col width=&#34;5%&#34; /&gt;
&lt;col width=&#34;10%&#34; /&gt;
&lt;col width=&#34;6%&#34; /&gt;
&lt;col width=&#34;6%&#34; /&gt;
&lt;col width=&#34;11%&#34; /&gt;
&lt;/colgroup&gt;
&lt;thead&gt;
&lt;tr class=&#34;header&#34;&gt;
&lt;th align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(j\)&lt;/span&gt;&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(y_{(j)}\)&lt;/span&gt;&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(d_{(j)1}\)&lt;/span&gt;&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(R_{(j)1}\)&lt;/span&gt;&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(d_{(j)2}\)&lt;/span&gt;&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(R_{(j)2}\)&lt;/span&gt;&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(d_{(j)}\)&lt;/span&gt;&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(R_{(j)}\)&lt;/span&gt;&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(E_{j}\)&lt;/span&gt;&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(O_{j}\)&lt;/span&gt;&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(O_{j} - E_{j}\)&lt;/span&gt;&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(N_{(j)}\)&lt;/span&gt;&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(D_{(j)}\)&lt;/span&gt;&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(N_{(j)}/D_{(j)}\)&lt;/span&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;4.1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;6&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;6&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;12&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0.5&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0.5&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;2.75&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;11&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0.25&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;center&#34;&gt;2&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;9.7&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;4&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;6&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;10&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0.4&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;-0.4&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;2.16&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;9&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0.24&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;center&#34;&gt;3&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;10&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;2&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;4&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;5&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;3&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;9&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1.333&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;2&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0.667&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;4.44&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;8&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0.555&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;center&#34;&gt;4&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;17.2&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;2&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;3&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0.333&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0.667&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0.44&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;2&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0.22&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;center&#34;&gt;5&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;19.7&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;2&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;2&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0.00&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;0.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(Total\)&lt;/span&gt;&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;4&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;3&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;7&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;2.566&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1.433&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;1.265&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Column &lt;span class=&#34;math inline&#34;&gt;\(j\)&lt;/span&gt;&lt;/strong&gt; is the number of distinct event times. We see that there are 5 (ignoring censored observations), so we write 1 to 5 in the table.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Column &lt;span class=&#34;math inline&#34;&gt;\(y_{(j)}\)&lt;/span&gt;&lt;/strong&gt; is the ordered distinct event times:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;4.1&lt;/em&gt;, &lt;em&gt;9.7&lt;/em&gt;, &lt;em&gt;10&lt;/em&gt;, &lt;em&gt;17.2&lt;/em&gt; and &lt;em&gt;19.7&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Column &lt;span class=&#34;math inline&#34;&gt;\(d_{(j)1}\)&lt;/span&gt;&lt;/strong&gt; is the number of observations for each distinct event time, for group 1:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;## time
##  4.1   10 17.2 
##    1    2    1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When there is no event, we simply write 0 in the table.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Column &lt;span class=&#34;math inline&#34;&gt;\(R_{(j)1}\)&lt;/span&gt;&lt;/strong&gt; is the remaining number of patients at risk, for group 1. For this, the distribution of time (censored and not censored, for group 1) is useful:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;## time
##  4.1  7.8   10 12.3 17.2 
##    1    1    2    1    1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We see that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;At the beginning, there are 6 patients&lt;/li&gt;
&lt;li&gt;Before time 9.7, there are 4 patients left (6 - 1 who had the event at time 4.1 - 1 who was censored at time 7.8)&lt;/li&gt;
&lt;li&gt;Before time 10, there are 4 patients left (6 - 2)&lt;/li&gt;
&lt;li&gt;Before time 17.2, there are 1 patient left (6 - 5)&lt;/li&gt;
&lt;li&gt;Before time 19.7, there are 0 patient left (6 - 6)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Columns &lt;span class=&#34;math inline&#34;&gt;\(d_{(j)2}\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(R_{(j)2}\)&lt;/span&gt;&lt;/strong&gt; follow the same principle, but for group 2 this time. So we have, respectively for &lt;span class=&#34;math inline&#34;&gt;\(d_{(j)2}\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(R_{(j)2}\)&lt;/span&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;## time
##  9.7   10 19.7 
##    1    1    1&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## time
##  9.7   10 11.1 13.1 19.7 24.1 
##    1    1    1    1    1    1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Columns &lt;span class=&#34;math inline&#34;&gt;\(d_{(j)}\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(R_{(j)}\)&lt;/span&gt;&lt;/strong&gt; also follow the same principle, but this time considering both groups. So we have, respectively for &lt;span class=&#34;math inline&#34;&gt;\(d_{(j)}\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(R_{(j)}\)&lt;/span&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;## time
##  4.1  9.7   10 17.2 19.7 
##    1    1    3    1    1&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## time
##  4.1  7.8  9.7   10 11.1 12.3 13.1 17.2 19.7 24.1 
##    1    1    1    3    1    1    1    1    1    1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Column &lt;span class=&#34;math inline&#34;&gt;\(E_{j}\)&lt;/span&gt;&lt;/strong&gt; is the expected number of events in the first group assuming that &lt;span class=&#34;math inline&#34;&gt;\(h_1 \equiv h_2\)&lt;/span&gt;. It is obtained as follows&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[ E_{j} = \frac{d_{(j)}R_{(j)1}}{R_{(j)}}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Column &lt;span class=&#34;math inline&#34;&gt;\(O_{j}\)&lt;/span&gt;&lt;/strong&gt; is the observed number of events in the first group, so it is equal to the &lt;span class=&#34;math inline&#34;&gt;\(d_{(j)1}\)&lt;/span&gt; column.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Column &lt;span class=&#34;math inline&#34;&gt;\(O_{j} - E_{j}\)&lt;/span&gt;&lt;/strong&gt; is straightforward.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Column &lt;span class=&#34;math inline&#34;&gt;\(N_{(j)}\)&lt;/span&gt;&lt;/strong&gt; is defined as follows&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[N_{(j)} = d_{(j)} \frac{R_{(j)1}}{R_{(j)} } \left( 1 - \frac{R_{(j)1}}{R_{(j)} } \right) \left( R_{(j)} - d_{(j)}\right)\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Column &lt;span class=&#34;math inline&#34;&gt;\(D_{(j)}\)&lt;/span&gt;&lt;/strong&gt; is &lt;span class=&#34;math inline&#34;&gt;\(R_{(j)} - 1\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Column &lt;span class=&#34;math inline&#34;&gt;\(N_{(j)}/D_{(j)}\)&lt;/span&gt;&lt;/strong&gt; is straightforward.&lt;/p&gt;
&lt;p&gt;Since &lt;span class=&#34;math inline&#34;&gt;\(w(y_{(j)}) = w^2(y_{(j)}) = 1\)&lt;/span&gt; for a log-rank test, we have&lt;a href=&#34;#fn11&#34; class=&#34;footnote-ref&#34; id=&#34;fnref11&#34;&gt;&lt;sup&gt;11&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[ U^{obs} = \frac{U}{\sqrt{Var(U)}} = \frac{1.434}{\sqrt{1.265}} = 1.275.\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;We reject &lt;span class=&#34;math inline&#34;&gt;\(H_0\)&lt;/span&gt; if &lt;span class=&#34;math inline&#34;&gt;\(|U^{obs}|&amp;gt;z_{1-\alpha/2}\)&lt;/span&gt;, so at the 5% significance level we reject &lt;span class=&#34;math inline&#34;&gt;\(H_0\)&lt;/span&gt; if &lt;span class=&#34;math inline&#34;&gt;\(|U^{obs}|&amp;gt;z_{0.975}=1.96\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;We have &lt;span class=&#34;math inline&#34;&gt;\(|U^{obs}| = 1.275 &amp;lt; z_{0.975}=1.96\)&lt;/span&gt;. Hence, at the 5% significance level we do not reject &lt;span class=&#34;math inline&#34;&gt;\(H_0\)&lt;/span&gt;. This means that, based on the data, we are not able to conclude that survival is different between the two groups (which is equivalent than saying that we do not reject the hypothesis that survival is equal between the two groups).&lt;/p&gt;
&lt;p&gt;If you are interested in computing the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value &lt;span class=&#34;math inline&#34;&gt;\(= 2\times P(Z&amp;gt;1.275) = 2 \times 0.101 = 0.202 &amp;gt; 0.05\)&lt;/span&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;in-r-1&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;In R&lt;/h3&gt;
&lt;p&gt;We now compare our results in R with the &lt;code&gt;survdiff()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat &amp;lt;- data.frame(
  group = c(rep(1, 6), rep(2, 6)),
  time = c(4.1, 7.8, 10, 10, 12.3, 17.2, 9.7, 10, 11.1, 13.1, 19.7, 24.1),
  event = c(1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0)
)

dat&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##    group time event
## 1      1  4.1     1
## 2      1  7.8     0
## 3      1 10.0     1
## 4      1 10.0     1
## 5      1 12.3     0
## 6      1 17.2     1
## 7      2  9.7     1
## 8      2 10.0     1
## 9      2 11.1     0
## 10     2 13.1     0
## 11     2 19.7     1
## 12     2 24.1     0&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;survdiff(Surv(time, event) ~ group,
  data = dat
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Call:
## survdiff(formula = Surv(time, event) ~ group, data = dat)
## 
##         N Observed Expected (O-E)^2/E (O-E)^2/V
## group=1 6        4     2.57     0.800      1.62
## group=2 6        3     4.43     0.463      1.62
## 
##  Chisq= 1.6  on 1 degrees of freedom, p= 0.2&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Alternatively, we can use the &lt;code&gt;ggsurvplot()&lt;/code&gt; function to draw the survival curves and perform the log-rank test at the same time:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;fit &amp;lt;- survfit(Surv(time, event) ~ group, data = dat)

ggsurvplot(fit,
  pval = TRUE,
  pval.method = TRUE
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/what-is-survival-analysis/index_files/figure-html/unnamed-chunk-30-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;As we can see, the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-values and the conclusions are the same (any difference with the results by hand is due to rounding).&lt;/p&gt;
&lt;p&gt;As for the Kaplan-Meier estimation, we do another example on a larger dataset. Consider the data on the times until staphylococcus infection of burn patients, also available in the &lt;code&gt;{KMsurv}&lt;/code&gt;:&lt;a href=&#34;#fn12&#34; class=&#34;footnote-ref&#34; id=&#34;fnref12&#34;&gt;&lt;sup&gt;12&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# load data
data(burn)

# preview data
head(burn)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   Obs Z1 Z2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 T1 D1 T2 D2 T3 D3
## 1   1  0  0  0 15  0  0  1  1  0   0   2 12  0 12  0 12  0
## 2   2  0  0  1 20  0  0  1  0  0   0   4  9  0  9  0  9  0
## 3   3  0  0  1 15  0  0  0  1  1   0   2 13  0 13  0  7  1
## 4   4  0  0  0 20  1  0  1  0  0   0   2 11  1 29  0 29  0
## 5   5  0  0  1 70  1  1  1  1  0   0   2 28  1 31  0  4  1
## 6   6  0  0  1 20  1  0  1  0  0   0   4 11  0 11  0  8  1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using the log-rank test, we want to test the hypothesis of difference in the time to staphylococcus infection (&lt;code&gt;T3&lt;/code&gt; variable) between patients whose burns were cared for with a routine bathing care method (&lt;code&gt;Z1 = 0&lt;/code&gt;) versus those whose body cleansing was initially performed using 4% chlorhexidine gluconate (&lt;code&gt;Z1 = 1&lt;/code&gt;). The event indicator is in variable &lt;code&gt;D3&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For this test, we use a two-sided alternative and a 5% significance level.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# fit
fit &amp;lt;- survfit(Surv(T3, D3) ~ Z1, data = burn)

# plot with log-rank test
ggsurvplot(fit,
  pval = TRUE,
  pval.method = TRUE
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/what-is-survival-analysis/index_files/figure-html/unnamed-chunk-32-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;In the sample&lt;/em&gt;, it seems that the time to infection for patients with routine bathing (&lt;code&gt;Z1 = 0&lt;/code&gt;) is smaller than for patients with body cleansing (&lt;code&gt;Z1 = 1&lt;/code&gt;). This is the case because the percentage of patients who have not experienced the infection decreases more quickly, so the hazard rate is greater.&lt;/p&gt;
&lt;p&gt;However, this conclusion cannot be generalized to the &lt;em&gt;population&lt;/em&gt; without performing a sound statistical test. And based on the result of the log-rank test, we do not reject the hypothesis that time to infection is the same between the two groups of patients (&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value = 0.051).&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;to-go-further&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;To go further&lt;/h1&gt;
&lt;p&gt;In this article, we have presented what is survival analysis, when, why and how to use it. We discussed about censoring and survival curves. We showed how to estimate the survival function via the Kaplan-Meier estimator and how to test survival between two groups via the log-rank test. We illustrated these approaches both by hand and in R.&lt;/p&gt;
&lt;p&gt;As you noticed, we did not show how to &lt;em&gt;model&lt;/em&gt; survival data. There are several regression models that can be applied to survival data, the most common one being the semiparametric Cox Proportional Hazards model &lt;span class=&#34;citation&#34;&gt;(&lt;a href=&#34;#ref-cox1972regression&#34;&gt;1972&lt;/a&gt;)&lt;/span&gt;. It originated from the medical area to investigate and assess the relationship between the survival times of patients and their corresponding predictor variables.&lt;/p&gt;
&lt;p&gt;We have seen that the Kaplan-Meier estimator is useful to visualize survival between groups and the log-rank test to test whether survival significantly differs between groups (so both approaches use a &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#qualitative&#34;&gt;categorical variable&lt;/a&gt; as predictor). However, it does not work well for assessing the effect of &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#quantitative&#34;&gt;quantitative predictor&lt;/a&gt;. The Cox model has the advantage that it works for both quantitative as well as for categorical predictors, and for several risk factors at the same time (so it can model the effect of multiple variables at once).&lt;/p&gt;
&lt;p&gt;With the Cox model, we model the impact of different factors &lt;span class=&#34;math inline&#34;&gt;\(X_1, X_2, \ldots, X_q\)&lt;/span&gt; on survival via their impact on the hazard function:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[h(t|\textbf{X}) = h_0 (t) exp(\beta_1 X_1 + \beta_2 X_2 + \cdots + \beta_q X_q),\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(h(t|\textbf{X})\)&lt;/span&gt; is the instantaneous death rate conditional on having survived up to time &lt;span class=&#34;math inline&#34;&gt;\(t\)&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(h_0 (t)\)&lt;/span&gt; is the population-level baseline hazard – the underlying hazard function. It describes how the average person’s risk evolves over time.&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(exp(\beta_1 X_1 + \beta_2 X_2 + \cdots + \beta_q X_q)\)&lt;/span&gt; describes how covariates affect the hazard. In particular, a unit increase in &lt;span class=&#34;math inline&#34;&gt;\(x_i\)&lt;/span&gt; leads to an increase of the hazard by a factor of &lt;span class=&#34;math inline&#34;&gt;\(\exp(\beta_i)\)&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This post aimed at presenting the introductory concepts in survival analysis, so this model will be developed in another post. In the meantime, if you would like to learn more about modeling survival data (thanks to the Cox model and other models), see this &lt;a href=&#34;https://rviews.rstudio.com/2022/09/06/deep-survival/&#34;&gt;post&lt;/a&gt; from Joseph Rickert.&lt;/p&gt;
&lt;p&gt;Thanks for reading.&lt;/p&gt;
&lt;p&gt;As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;references&#34; class=&#34;section level1 unnumbered&#34;&gt;
&lt;h1&gt;References&lt;/h1&gt;
&lt;div id=&#34;refs&#34; class=&#34;references csl-bib-body hanging-indent&#34;&gt;
&lt;div id=&#34;ref-cox1972regression&#34; class=&#34;csl-entry&#34;&gt;
Cox, David R. 1972. &lt;span&gt;“Regression Models and Life-Tables.”&lt;/span&gt; &lt;em&gt;Journal of the Royal Statistical Society: Series B (Methodological)&lt;/em&gt; 34 (2): 187–202.
&lt;/div&gt;
&lt;div id=&#34;ref-kaplan1958nonparametric&#34; class=&#34;csl-entry&#34;&gt;
Kaplan, Edward L, and Paul Meier. 1958. &lt;span&gt;“Nonparametric Estimation from Incomplete Observations.”&lt;/span&gt; &lt;em&gt;Journal of the American Statistical Association&lt;/em&gt; 53 (282): 457–81.
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;footnotes footnotes-end-of-document&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;Note that in survival analysis, the precision of the estimators (and the power of the tests) does not depend on the number of patients but on the number of events. So it is best to have many observations where the event does occur for the analyses to be effective. Here we work on a small sample for the sake of illustration.&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn2&#34;&gt;&lt;p&gt;Note that the &lt;code&gt;time&lt;/code&gt; variable can be expressed in other units, such as seconds, days, weeks, months, etc.&lt;a href=&#34;#fnref2&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn3&#34;&gt;&lt;p&gt;Median is preferred over mean in survival analysis because survival functions are often skewed to the right. The mean is often influenced by &lt;a href=&#34;https://statsandr.com/blog/outliers-detection-in-r/&#34;&gt;outliers&lt;/a&gt;, whereas the median is not. See a discussion comparing the two in this &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand/#mean-vs.-median&#34;&gt;section&lt;/a&gt;.&lt;a href=&#34;#fnref3&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn4&#34;&gt;&lt;p&gt;Note that if the survival curve does not cross 50% (because survival is greater than 50% at the last time point), then the median survival cannot be computed and is simply undefined.&lt;a href=&#34;#fnref4&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn5&#34;&gt;&lt;p&gt;Note that the median survival is expressed in the same unit than the unit of the &lt;code&gt;time&lt;/code&gt; variable in the initial dataset. So if the time unit was months, the median survival time would be 9 months.&lt;a href=&#34;#fnref5&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn6&#34;&gt;&lt;p&gt;More information about the dataset can be found on &lt;a href=&#34;https://cran.r-project.org/web/packages/KMsurv/&#34;&gt;CRAN&lt;/a&gt; or with &lt;code&gt;?tongue&lt;/code&gt;.&lt;a href=&#34;#fnref6&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn7&#34;&gt;&lt;p&gt;See the reason we use &lt;code&gt;log-log&lt;/code&gt; for the confidence interval in this &lt;a href=&#34;https://stats.stackexchange.com/questions/361354/choosing-conf-type-for-survfit-in-r&#34;&gt;thread&lt;/a&gt;.&lt;a href=&#34;#fnref7&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn8&#34;&gt;&lt;p&gt;Note that if the groups to compare have a natural ordering (such as the educational level; none, low, medium, high), tests that take it into consideration have more power to detect significant effects. These tests are referred as tests for trend.&lt;a href=&#34;#fnref8&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn9&#34;&gt;&lt;p&gt;Remember that the time unit can be different than years.&lt;a href=&#34;#fnref9&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn10&#34;&gt;&lt;p&gt;This is the case for large samples. The example described here does not meet this condition, but we still show it as an illustration.&lt;a href=&#34;#fnref10&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn11&#34;&gt;&lt;p&gt;Note that other weights can be considered, but this is beyond the scope of this article.&lt;a href=&#34;#fnref11&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn12&#34;&gt;&lt;p&gt;More information about the dataset can be found on &lt;a href=&#34;https://cran.r-project.org/web/packages/KMsurv/&#34;&gt;CRAN&lt;/a&gt; or with &lt;code&gt;?burn&lt;/code&gt;.&lt;a href=&#34;#fnref12&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Graphics in R with ggplot2</title>
      <link>https://statsandr.com/blog/graphics-in-r-with-ggplot2/</link>
      <pubDate>Fri, 21 Aug 2020 00:00:00 +0000</pubDate>
      
      <guid>https://statsandr.com/blog/graphics-in-r-with-ggplot2/</guid>
      <description>
&lt;link href=&#34;https://statsandr.com/rmarkdown-libs/htmltools-fill/fill.css&#34; rel=&#34;stylesheet&#34; /&gt;
&lt;script src=&#34;https://statsandr.com/rmarkdown-libs/htmlwidgets/htmlwidgets.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;https://statsandr.com/rmarkdown-libs/plotly-binding/plotly.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;https://statsandr.com/rmarkdown-libs/typedarray/typedarray.min.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;https://statsandr.com/rmarkdown-libs/jquery/jquery.min.js&#34;&gt;&lt;/script&gt;
&lt;link href=&#34;https://statsandr.com/rmarkdown-libs/crosstalk/css/crosstalk.min.css&#34; rel=&#34;stylesheet&#34; /&gt;
&lt;script src=&#34;https://statsandr.com/rmarkdown-libs/crosstalk/js/crosstalk.min.js&#34;&gt;&lt;/script&gt;
&lt;link href=&#34;https://statsandr.com/rmarkdown-libs/plotly-htmlwidgets-css/plotly-htmlwidgets.css&#34; rel=&#34;stylesheet&#34; /&gt;
&lt;script src=&#34;https://statsandr.com/rmarkdown-libs/plotly-main/plotly-latest.min.js&#34;&gt;&lt;/script&gt;

&lt;div id=&#34;TOC&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#introduction&#34; id=&#34;toc-introduction&#34;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#data&#34; id=&#34;toc-data&#34;&gt;Data&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#basic-principles-of-ggplot2&#34; id=&#34;toc-basic-principles-of-ggplot2&#34;&gt;Basic principles of &lt;code&gt;{ggplot2}&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#create-plots-with-ggplot2&#34; id=&#34;toc-create-plots-with-ggplot2&#34;&gt;Create plots with &lt;code&gt;{ggplot2}&lt;/code&gt;&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#scatter-plot&#34; id=&#34;toc-scatter-plot&#34;&gt;Scatter plot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#line-plot&#34; id=&#34;toc-line-plot&#34;&gt;Line plot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#combination-of-line-and-points&#34; id=&#34;toc-combination-of-line-and-points&#34;&gt;Combination of line and points&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#histogram&#34; id=&#34;toc-histogram&#34;&gt;Histogram&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#density-plot&#34; id=&#34;toc-density-plot&#34;&gt;Density plot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#combination-of-histogram-and-densities&#34; id=&#34;toc-combination-of-histogram-and-densities&#34;&gt;Combination of histogram and densities&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#dotplot&#34; id=&#34;toc-dotplot&#34;&gt;Dotplot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#boxplot&#34; id=&#34;toc-boxplot&#34;&gt;Boxplot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#barplot&#34; id=&#34;toc-barplot&#34;&gt;Barplot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#raincloud-plot&#34; id=&#34;toc-raincloud-plot&#34;&gt;Raincloud plot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#further-personalization&#34; id=&#34;toc-further-personalization&#34;&gt;Further personalization&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#title-and-axis-labels&#34; id=&#34;toc-title-and-axis-labels&#34;&gt;Title and axis labels&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#axis-ticks&#34; id=&#34;toc-axis-ticks&#34;&gt;Axis ticks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#log-transformations&#34; id=&#34;toc-log-transformations&#34;&gt;Log transformations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#limits&#34; id=&#34;toc-limits&#34;&gt;Limits&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#scales-for-better-axis-formats&#34; id=&#34;toc-scales-for-better-axis-formats&#34;&gt;Scales for better axis formats&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#legend&#34; id=&#34;toc-legend&#34;&gt;Legend&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#shape-color-size-and-transparency&#34; id=&#34;toc-shape-color-size-and-transparency&#34;&gt;Shape, color, size and transparency&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#text-and-labels&#34; id=&#34;toc-text-and-labels&#34;&gt;Text and labels&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#smooth-and-regression-lines&#34; id=&#34;toc-smooth-and-regression-lines&#34;&gt;Smooth and regression lines&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#facets&#34; id=&#34;toc-facets&#34;&gt;Facets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#themes&#34; id=&#34;toc-themes&#34;&gt;Themes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#interactive-plot-with-plotly&#34; id=&#34;toc-interactive-plot-with-plotly&#34;&gt;Interactive plot with &lt;code&gt;{plotly}&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#combine-plots-with-patchwork&#34; id=&#34;toc-combine-plots-with-patchwork&#34;&gt;Combine plots with &lt;code&gt;{patchwork}&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#flip-coordinates&#34; id=&#34;toc-flip-coordinates&#34;&gt;Flip coordinates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#save-plot&#34; id=&#34;toc-save-plot&#34;&gt;Save plot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#managing-dates&#34; id=&#34;toc-managing-dates&#34;&gt;Managing dates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#highlight-data-with-gghighlight&#34; id=&#34;toc-highlight-data-with-gghighlight&#34;&gt;Highlight data with &lt;code&gt;{gghighlight}&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#tip&#34; id=&#34;toc-tip&#34;&gt;Tip&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#to-go-further&#34; id=&#34;toc-to-go-further&#34;&gt;To go further&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#conclusion&#34; id=&#34;toc-conclusion&#34;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;center&gt;
&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/graphics-in-r-with-ggplot2.png&#34; style=&#34;width:50.0%&#34; /&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note that this article is inspired from the lecture notes of Prof. Anouar El Ghouch and my personal notes as teaching assistant for his course entitled “Statistics and data sciences with R: Advanced programming” given at UCLouvain.&lt;/em&gt;&lt;/p&gt;
&lt;div id=&#34;introduction&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;R is known to be a really powerful programming language when it comes to graphics and visualizations (in addition to &lt;a href=&#34;https://statsandr.com/tags/statistics/&#34;&gt;statistics&lt;/a&gt; and data science of course!).&lt;/p&gt;
&lt;p&gt;To keep it short, graphics in R can be done in three ways, via the:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;code&gt;{graphics}&lt;/code&gt; package (the base graphics in R, loaded by default)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;{lattice}&lt;/code&gt; package which adds more functionalities to the base package&lt;/li&gt;
&lt;li&gt;&lt;code&gt;{ggplot2}&lt;/code&gt; package (which needs to be &lt;a href=&#34;https://statsandr.com/blog/an-efficient-way-to-install-and-load-r-packages/&#34;&gt;installed and loaded&lt;/a&gt; beforehand)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The &lt;code&gt;{graphics}&lt;/code&gt; package comes with a large choice of plots (such as &lt;code&gt;plot&lt;/code&gt;, &lt;code&gt;hist&lt;/code&gt;, &lt;code&gt;barplot&lt;/code&gt;, &lt;code&gt;boxplot&lt;/code&gt;, &lt;code&gt;pie&lt;/code&gt;, &lt;code&gt;mosaicplot&lt;/code&gt;, etc.) and additional related features (e.g., &lt;code&gt;abline&lt;/code&gt;, &lt;code&gt;lines&lt;/code&gt;, &lt;code&gt;legend&lt;/code&gt;, &lt;code&gt;mtext&lt;/code&gt;, &lt;code&gt;rect&lt;/code&gt;, etc.). It is often the preferred way to draw plots for most R users, and in particular for beginners to intermediate users.&lt;/p&gt;
&lt;p&gt;Since its creation in 2005 by Hadley Wickham, &lt;strong&gt;&lt;code&gt;{ggplot2}&lt;/code&gt;&lt;/strong&gt; has grown in use to become one of the most popular R packages and the &lt;strong&gt;most popular package for graphics and data visualizations&lt;/strong&gt;. The &lt;code&gt;{ggplot2}&lt;/code&gt; package is a much more modern approach to creating professional-quality graphics. More information about the package can be found at &lt;a href=&#34;https://ggplot2.tidyverse.org/&#34; target=&#34;_blank&#34;&gt;ggplot2.tidyverse.org&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this article, we will see how to create common plots such as scatter plots, line plots, histograms, boxplots, barplots, density plots in R with this package. If you are unfamiliar with any of these types of graph, you will find more information about each one (when to use it, its purpose, what does it show, etc.) in my article about &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/&#34;&gt;descriptive statistics in R&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;data&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Data&lt;/h1&gt;
&lt;p&gt;To illustrate plots with the &lt;code&gt;{ggplot2}&lt;/code&gt; package we will use the &lt;code&gt;mpg&lt;/code&gt; dataset available in the package.&lt;/p&gt;
&lt;p&gt;The dataset contains observations collected by the US Environmental Protection Agency on fuel economy from 1999 to 2008 for 38 popular models of cars (run &lt;code&gt;?mpg&lt;/code&gt; for more information about the data):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(ggplot2)
dat &amp;lt;- ggplot2::mpg&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Before going further, let’s transform the &lt;code&gt;cyl&lt;/code&gt;, &lt;code&gt;drv&lt;/code&gt;, &lt;code&gt;fl&lt;/code&gt;, &lt;code&gt;year&lt;/code&gt; and &lt;code&gt;class&lt;/code&gt; variables in &lt;a href=&#34;https://statsandr.com/blog/data-types-in-r/#factor&#34;&gt;factor&lt;/a&gt; with the &lt;code&gt;transform()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat &amp;lt;- transform(dat,
  cyl = factor(cyl),
  drv = factor(drv),
  fl = factor(fl),
  year = factor(year),
  class = factor(class)
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For the interested reader, see more &lt;a href=&#34;https://statsandr.com/blog/data-manipulation-in-r/&#34;&gt;data manipulation techniques in R&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;basic-principles-of-ggplot2&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Basic principles of &lt;code&gt;{ggplot2}&lt;/code&gt;&lt;/h1&gt;
&lt;p&gt;The &lt;code&gt;{ggplot2}&lt;/code&gt; package is based on the principles of “The Grammar of Graphics” (hence “gg” in the name of &lt;code&gt;{ggplot2}&lt;/code&gt;), that is, a coherent system for describing and building graphs. The main idea is to &lt;strong&gt;design a graphic as a succession of layers&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The main layers are:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;The &lt;strong&gt;dataset&lt;/strong&gt; that contains the variables that we want to represent. This is done with the &lt;code&gt;ggplot()&lt;/code&gt; function and comes first.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;variable(s)&lt;/strong&gt; to represent on the x and/or y-axis, and the aesthetic elements (such as color, size, fill, shape and transparency) of the objects to be represented. This is done with the &lt;code&gt;aes()&lt;/code&gt; function (abbreviation of aesthetic).&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;type of graphical representation&lt;/strong&gt; (scatter plot, line plot, barplot, histogram, boxplot, etc.). This is done with the functions &lt;code&gt;geom_point()&lt;/code&gt;, &lt;code&gt;geom_line()&lt;/code&gt;, &lt;code&gt;geom_bar()&lt;/code&gt;, &lt;code&gt;geom_histogram()&lt;/code&gt;, &lt;code&gt;geom_boxplot()&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li&gt;If needed, additional layers (such as labels, annotations, scales, axis ticks, legends, themes, facets, etc.) can be added to personalize the plot.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To create a plot, we thus first need to specify the data in the &lt;code&gt;ggplot()&lt;/code&gt; function and then add the required layers such as the variables, the aesthetic elements and the type of plot:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(data) +
  aes(x = var_x, y = var_y) +
  geom_x()&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;data&lt;/code&gt; in &lt;code&gt;ggplot()&lt;/code&gt; is the name of the data frame which contains the variables &lt;code&gt;var_x&lt;/code&gt; and &lt;code&gt;var_y&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;+&lt;/code&gt; symbol is used to indicate the different layers that will be added to the plot. Make sure to write the &lt;code&gt;+&lt;/code&gt; &lt;em&gt;symbol at the end of the line&lt;/em&gt; of code and not at the beginning of the line, otherwise &lt;a href=&#34;https://statsandr.com/blog/top-10-errors-in-r/#forgetting-the-sign-in-ggplot2&#34;&gt;R throws an error&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The layer &lt;code&gt;aes()&lt;/code&gt; indicates what variables will be used in the plot and more generally, the aesthetic elements of the plot.&lt;/li&gt;
&lt;li&gt;Finally, &lt;code&gt;x&lt;/code&gt; in &lt;code&gt;geom_x()&lt;/code&gt; represents the type of plot.&lt;/li&gt;
&lt;li&gt;Other layers are usually not required unless we want to personalize the plot further.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that it is a good practice to write one line of code per layer to improve code readability.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;create-plots-with-ggplot2&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Create plots with &lt;code&gt;{ggplot2}&lt;/code&gt;&lt;/h1&gt;
&lt;p&gt;In the following sections we will show how to draw the following plots:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;scatter plot&lt;/li&gt;
&lt;li&gt;line plot&lt;/li&gt;
&lt;li&gt;histogram&lt;/li&gt;
&lt;li&gt;density plot&lt;/li&gt;
&lt;li&gt;dotplot&lt;/li&gt;
&lt;li&gt;boxplot&lt;/li&gt;
&lt;li&gt;barplot&lt;/li&gt;
&lt;li&gt;raincloud plot&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In order to focus on the construction of the different plots and the use of &lt;code&gt;{ggplot2}&lt;/code&gt;, we will restrict ourselves to drawing basic (yet beautiful) plots without unnecessary layers. For the sake of completeness, we will briefly discuss and illustrate different layers to further personalize a plot at the end of the article (see this &lt;a href=&#34;https://statsandr.com/blog/graphics-in-r-with-ggplot2/#further-personalization&#34;&gt;section&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Note that if you still struggle to create plots with &lt;code&gt;{ggplot2}&lt;/code&gt; after reading this tutorial, you may find the &lt;a href=&#34;https://statsandr.com/blog/rstudio-addins-or-how-to-make-your-coding-life-easier/#esquisse&#34;&gt;{esquisse} addin&lt;/a&gt; useful. This addin allows you to &lt;strong&gt;interactively&lt;/strong&gt; (that is, by dragging and dropping variables) create plots with the &lt;code&gt;{ggplot2}&lt;/code&gt; package. Give it a try!&lt;/p&gt;
&lt;div id=&#34;scatter-plot&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Scatter plot&lt;/h2&gt;
&lt;p&gt;We start by creating a &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/#scatterplot&#34;&gt;scatter plot&lt;/a&gt; using &lt;code&gt;geom_point&lt;/code&gt;. Remember that a scatter plot is used to visualize the relation between two &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#quantitative&#34;&gt;quantitative variables&lt;/a&gt;.&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;We start by specifying the data:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) # data&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-4-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;ol start=&#34;2&#34; style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;Then we add the variables to be represented with the &lt;code&gt;aes()&lt;/code&gt; function:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) + # data
  aes(x = displ, y = hwy) # variables&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-5-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;ol start=&#34;3&#34; style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;Finally, we indicate the type of plot:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) + # data
  aes(x = displ, y = hwy) + # variables
  geom_point() # type of plot&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-6-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;You will also sometimes see the aesthetic elements (&lt;code&gt;aes()&lt;/code&gt; with the variables) inside the &lt;code&gt;ggplot()&lt;/code&gt; function in addition to the dataset:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-7-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;This second method gives the exact same plot than the first method. I tend to prefer the first method over the second for better readability, but this is more a matter of taste so the choice is up to you.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;line-plot&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Line plot&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/#line-plot&#34;&gt;Line plots&lt;/a&gt;, particularly useful in time series or finance, can be created similarly but by using &lt;code&gt;geom_line()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = displ, y = hwy) +
  geom_line()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-8-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;(Note that this might not be the most appropriate plot since there are multiple points for each value of &lt;code&gt;displ&lt;/code&gt;, but this is just an example to show how to create a line plot.)&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;combination-of-line-and-points&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Combination of line and points&lt;/h2&gt;
&lt;p&gt;An advantage of &lt;code&gt;{ggplot2}&lt;/code&gt; is the ability to combine several types of plots and its flexibility in designing it. For instance, we can add a line to a scatter plot by simply adding a layer to the initial scatter plot:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = displ, y = hwy) +
  geom_point() +
  geom_line() # add line&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-9-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;histogram&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Histogram&lt;/h2&gt;
&lt;p&gt;A &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/#histogram&#34;&gt;histogram&lt;/a&gt; (useful to visualize distributions and detect potential &lt;a href=&#34;https://statsandr.com/blog/outliers-detection-in-r/&#34;&gt;outliers&lt;/a&gt;) can be plotted using &lt;code&gt;geom_histogram()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = hwy) +
  geom_histogram()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-10-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;By default, the number of bins is equal to 30. You can change this value using the &lt;code&gt;bins&lt;/code&gt; argument inside the &lt;code&gt;geom_histogram()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = hwy) +
  geom_histogram(bins = round(sqrt(nrow(dat))))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-11-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Here I specify the number of bins to be equal to the square root of the number of observations (following the square-root rule) but you can specify any integer number.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;density-plot&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Density plot&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/#density-plot&#34;&gt;Density plots&lt;/a&gt; can be created using &lt;code&gt;geom_density()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = hwy) +
  geom_density()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-12-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;combination-of-histogram-and-densities&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Combination of histogram and densities&lt;/h2&gt;
&lt;p&gt;We can also superimpose a histogram and a density curve on the same plot:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = hwy, y = after_stat(density)) +
  geom_histogram() +
  geom_density()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-13-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Or superimpose several densities:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = hwy, color = drv, fill = drv) +
  geom_density(alpha = 0.25) # add transparency&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-14-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The argument &lt;code&gt;alpha = 0.25&lt;/code&gt; has been added for some transparency. More information about this argument can be found in this &lt;a href=&#34;https://statsandr.com/blog/graphics-in-r-with-ggplot2/#further-personalization&#34;&gt;section&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;dotplot&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Dotplot&lt;/h2&gt;
&lt;p&gt;A &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/#dotplot&#34;&gt;dotplot&lt;/a&gt; in &lt;code&gt;{ggplot2}&lt;/code&gt; can be built with &lt;code&gt;geom_dotplot()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Dotplot for one variable
ggplot(dat) +
  aes(x = &amp;quot;&amp;quot;, y = hwy) +
  geom_dotplot(binaxis = &amp;quot;y&amp;quot;, stackdir = &amp;quot;center&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-15-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Dotplot by factor
ggplot(dat) +
  aes(x = drv, y = hwy) +
  geom_dotplot(
    binaxis = &amp;quot;y&amp;quot;, stackdir = &amp;quot;center&amp;quot;,
    dotsize = 0.75 # decrease dot size
  )&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-15-2.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Dotplots are more appropriate with small samples (because the plot may be hard to read with too many points). For large samples, a boxplot may be used.&lt;/p&gt;
&lt;p&gt;For the interested reader, see many personalization that is possible with a dotplot in this &lt;a href=&#34;http://www.sthda.com/english/wiki/ggplot2-dot-plot-quick-start-guide-r-software-and-data-visualization&#34; target=&#34;_blank&#34;&gt;tutorial&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;boxplot&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Boxplot&lt;/h2&gt;
&lt;p&gt;A &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/#boxplot&#34;&gt;boxplot&lt;/a&gt; (also very useful to visualize distributions and detect potential &lt;a href=&#34;https://statsandr.com/blog/outliers-detection-in-r/&#34;&gt;outliers&lt;/a&gt;) can be plotted using &lt;code&gt;geom_boxplot()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Boxplot for one variable
ggplot(dat) +
  aes(x = &amp;quot;&amp;quot;, y = hwy) +
  geom_boxplot()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-16-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Boxplot by factor
ggplot(dat) +
  aes(x = drv, y = hwy) +
  geom_boxplot()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-16-2.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;It is also possible to plot the points on the boxplot with &lt;code&gt;geom_jitter()&lt;/code&gt;, and to vary the width of the boxes according to the size (i.e., the number of observations) of each level with &lt;code&gt;varwidth = TRUE&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = drv, y = hwy) +
  geom_boxplot(varwidth = TRUE) + # vary boxes width according to n obs.
  geom_jitter(alpha = 0.25, width = 0.2) # adds random noise and limit its width&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-17-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;geom_jitter()&lt;/code&gt; layer adds some random variation to each point in order to prevent them from overlapping (an issue known as overplotting).&lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; Moreover, the &lt;code&gt;alpha&lt;/code&gt; argument adds some transparency to the points (see more in this &lt;a href=&#34;https://statsandr.com/blog/graphics-in-r-with-ggplot2/#further-personalization&#34;&gt;section&lt;/a&gt;) to keep the focus on the boxes and not on the points.&lt;/p&gt;
&lt;p&gt;Finally, it is also possible to divide boxplots into several panels according to the levels of a &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#qualitative&#34;&gt;qualitative variable&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = drv, y = hwy) +
  geom_boxplot(varwidth = TRUE) + # vary boxes width according to n obs.
  geom_jitter(alpha = 0.25, width = 0.2) + # adds random noise and limit its width
  facet_wrap(~year) # divide into 2 panels&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-18-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;For a visually more appealing plot, it is also possible to use some colors for the boxes depending on the x variable:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = drv, y = hwy, fill = drv) + # add color to boxes with fill
  geom_boxplot(varwidth = TRUE) + # vary boxes width according to n obs.
  geom_jitter(alpha = 0.25, width = 0.2) + # adds random noise and limit its width
  facet_wrap(~year) + # divide into 2 panels
  theme(legend.position = &amp;quot;none&amp;quot;) # remove legend&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-19-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;In that case, it best to remove the legend as it becomes redundant. See more information about the legend in this &lt;a href=&#34;https://statsandr.com/blog/graphics-in-r-with-ggplot2/#legend&#34;&gt;section&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If you are unhappy with the default colors provided in &lt;code&gt;{ggplot2}&lt;/code&gt;, you can change them manually with the &lt;code&gt;scale_fill_manual()&lt;/code&gt; layer:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = drv, y = hwy, fill = drv) + # add color to boxes with fill
  geom_boxplot(varwidth = TRUE) + # vary boxes width according to n obs.
  geom_jitter(alpha = 0.25, width = 0.2) + # adds random noise and limit its width
  facet_wrap(~year) + # divide into 2 panels
  theme(legend.position = &amp;quot;none&amp;quot;) + # remove legend
  scale_fill_manual(values = c(&amp;quot;darkred&amp;quot;, &amp;quot;darkgreen&amp;quot;, &amp;quot;steelblue&amp;quot;)) # change fill color manually&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-20-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;barplot&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Barplot&lt;/h2&gt;
&lt;p&gt;A &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/#barplot&#34;&gt;barplot&lt;/a&gt; (useful to visualize qualitative variables) can be plotted using &lt;code&gt;geom_bar()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = drv) +
  geom_bar()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-21-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Bars’ heights correspond to the observed frequencies (i.e., the number of observations) for each level of the variable of interest (&lt;code&gt;drv&lt;/code&gt; in our case).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;: by default, the order of the bars follows the initial order (by alphabetical order or numerical order if you did not change it). If you want to order the levels by frequency (largest first), use the &lt;code&gt;fct_infreq()&lt;/code&gt; function from the &lt;code&gt;{forcats}&lt;/code&gt; package.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(forcats)

ggplot(dat) +
  aes(x = fct_infreq(drv)) + # order by frequency
  geom_bar()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-22-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;If you want to order levels in an increasing order (i.e., category with the smallest frequency first), use the &lt;code&gt;fct_rev()&lt;/code&gt; in addition to the &lt;code&gt;fct_infreq()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = fct_rev(fct_infreq(drv))) + # order by frequency
  geom_bar()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-23-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;(Label for the x-axis can then easily be edited with the &lt;code&gt;labs()&lt;/code&gt; function. See &lt;a href=&#34;https://statsandr.com/blog/graphics-in-r-with-ggplot2/#title-and-axis-labels&#34;&gt;below&lt;/a&gt; for more information.)&lt;/p&gt;
&lt;p&gt;Again, for a more appealing plot, we can add some colors to the bars with the &lt;code&gt;fill&lt;/code&gt; argument:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = drv, fill = drv) + # add colors to bars
  geom_bar() +
  theme(legend.position = &amp;quot;none&amp;quot;) # remove legend&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-24-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;We can also create a barplot with two qualitative variables:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = drv, fill = year) + # fill by years
  geom_bar()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-25-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;In order to compare proportions across groups, it is best to make each bar the same height using &lt;code&gt;position = &#34;fill&#34;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  geom_bar(aes(x = drv, fill = year), position = &amp;quot;fill&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-26-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;To draw the bars next to each other for each group, use &lt;code&gt;position = &#34;dodge&#34;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  geom_bar(aes(x = drv, fill = year), position = &amp;quot;dodge&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-27-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;raincloud-plot&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Raincloud plot&lt;/h2&gt;
&lt;p&gt;A raincloud plot is a graph that combines 3 visualizations:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;a density plot,&lt;/li&gt;
&lt;li&gt;a boxplot,&lt;/li&gt;
&lt;li&gt;and the raw data in the form of a dotplot or jittered points.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The advantage of this plot is that it illustrates, &lt;strong&gt;all at once&lt;/strong&gt;, the distribution (with the density curve), the summary measures (first, second and third &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/#first-and-third-quartile&#34;&gt;quartiles&lt;/a&gt;, and maximum/mininum without &lt;a href=&#34;https://statsandr.com/blog/outliers-detection-in-r/&#34;&gt;outliers&lt;/a&gt; thanks to the boxplot) and the number of observations (either via a dotplot or via jittered points).&lt;/p&gt;
&lt;p&gt;Let’s illustrate the raincloud plot, first with jittered points (more appropriate with large samples):&lt;a href=&#34;#fn2&#34; class=&#34;footnote-ref&#34; id=&#34;fnref2&#34;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(tidyverse)
library(ggdist)

# density plot:
dat %&amp;gt;%
  ggplot(aes(x = drv, y = hwy, fill = drv)) +
  stat_halfeye(
    adjust = 0.5, # set the smoothing parameter
    width = 0.5, # set the height of the curves
    justification = -0.2, # move curves to the right
    .width = 0, point_colour = NA # remove interval present by default
  ) +
  # boxplot:
  geom_boxplot(
    width = 0.12, # width of boxes
    outlier.color = NA, # remove color of outliers
    alpha = 0.5 # add transparency
  ) +
  # jittered points:
  geom_point(aes(colour = drv), # add color on points
    size = 1.3, # size of points
    alpha = .3, # add transparency
    position = position_jitter( # obtain shifted points
      seed = 1, # set seed for same random representation
      width = .09 # manage the width of the offset
    )
  ) +
  # further personalization:
  coord_flip() + # rotate plot
  theme(legend.position = &amp;quot;none&amp;quot;) # remove legend&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-28-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Now the same chart but with dotplots this time (more appropriate with small &lt;a href=&#34;https://statsandr.com/blog/what-is-the-difference-between-population-and-sample/&#34;&gt;samples&lt;/a&gt;):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# density plot:
dat %&amp;gt;%
  sample_n(100) %&amp;gt;% # random sample of size 100
  ggplot(aes(x = drv, y = hwy, fill = drv)) +
  stat_halfeye(
    adjust = 0.5, # set the smoothing parameter
    width = 0.5, # set the height of the curves
    justification = -0.2, # move curves to the right
    .width = 0, point_colour = NA # remove interval present by default
  ) +
  # boxplot:
  geom_boxplot(
    width = 0.12, # width of boxes
    outlier.color = NA, # remove color of outliers
    alpha = 0.5 # add transparency
  ) +
  # dotplot:
  stat_dots(
    dotsize = 0.5, # size of points
    side = &amp;quot;left&amp;quot;, # place points on opposite side of density curve
    justification = 1.1, # move points away from boxplot
    binwidth = 1 # group points together
  ) +
  # further personalization:
  coord_flip() + # rotate plot
  theme(legend.position = &amp;quot;none&amp;quot;) # remove legend&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-29-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The code is much longer compared to the other plots, but the only line(s) to edit to adapt to your dataset is the aesthetics (&lt;code&gt;aes()&lt;/code&gt;). The rest is mainly adjustments that should not be changed.&lt;/p&gt;
&lt;p&gt;You may have notice that at the end of the code, there are some personalization which allow to improve the plot even further. The most common personalization are presented in the next section.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;further-personalization&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Further personalization&lt;/h2&gt;
&lt;div id=&#34;title-and-axis-labels&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Title and axis labels&lt;/h3&gt;
&lt;p&gt;The first things to personalize in a plot is the labels to make the plot more informative to the audience. We can easily add a title, subtitle, caption and edit axis labels with the &lt;code&gt;labs()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p &amp;lt;- ggplot(dat) +
  aes(x = displ, y = hwy) +
  geom_point()

p + labs(
  title = &amp;quot;Fuel efficiency for 38 popular models of car&amp;quot;,
  subtitle = &amp;quot;Period 1999-2008&amp;quot;,
  caption = &amp;quot;Data: ggplot2::mpg. See more at statsandr.com&amp;quot;,
  x = &amp;quot;Engine displacement (litres)&amp;quot;,
  y = &amp;quot;Highway miles per gallon (mpg)&amp;quot;
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-30-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;As you can see in the above code, you can save one or more layers of the plot in an object for later use.&lt;/p&gt;
&lt;p&gt;This way, you can save your “main” plot, and add more layers of personalization until you get the desired output. Here we saved the main scatter plot in an object called &lt;code&gt;p&lt;/code&gt; and we will refer to it for the subsequent personalization.&lt;/p&gt;
&lt;p&gt;You can also edit the alignment, the size and the shape of the title and subtitle via the &lt;code&gt;theme()&lt;/code&gt; layer and the &lt;code&gt;element_text()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + labs(
  title = &amp;quot;Fuel efficiency for 38 popular models of car&amp;quot;,
  subtitle = &amp;quot;Period 1999-2008&amp;quot;,
  caption = &amp;quot;Data: ggplot2::mpg. See more at statsandr.com&amp;quot;,
  x = &amp;quot;Engine displacement (litres)&amp;quot;,
  y = &amp;quot;Highway miles per gallon (mpg)&amp;quot;
) +
  theme(
    plot.title = element_text(
      hjust = 0.5, # center
      size = 12,
      color = &amp;quot;steelblue&amp;quot;,
      face = &amp;quot;bold&amp;quot;
    ),
    plot.subtitle = element_text(
      hjust = 0.5, # center
      size = 10,
      color = &amp;quot;gray&amp;quot;,
      face = &amp;quot;italic&amp;quot;
    )
  )&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-31-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;If the title or subtitle is long and you want to divide it into multiple lines, use &lt;code&gt;\n&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + labs(
  title = &amp;quot;Fuel efficiency for 38 popular \n models of car&amp;quot;,
  subtitle = &amp;quot;Period 1999-2008&amp;quot;,
  caption = &amp;quot;Data: ggplot2::mpg. See more at statsandr.com&amp;quot;,
  x = &amp;quot;Engine displacement (litres)&amp;quot;,
  y = &amp;quot;Highway miles per gallon (mpg)&amp;quot;
) +
  theme(
    plot.title = element_text(
      hjust = 0.5, # center
      size = 12,
      color = &amp;quot;steelblue&amp;quot;,
      face = &amp;quot;bold&amp;quot;
    ),
    plot.subtitle = element_text(
      hjust = 0.5, # center
      size = 10,
      color = &amp;quot;gray&amp;quot;,
      face = &amp;quot;italic&amp;quot;
    )
  )&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-32-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;axis-ticks&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Axis ticks&lt;/h3&gt;
&lt;p&gt;Axis ticks can be adjusted using &lt;code&gt;scale_x_continuous()&lt;/code&gt; and &lt;code&gt;scale_y_continuous()&lt;/code&gt; for the x and y-axis, respectively:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Adjust ticks
p + scale_x_continuous(breaks = seq(from = 1, to = 7, by = 0.5)) + # x-axis
  scale_y_continuous(breaks = seq(from = 10, to = 45, by = 5)) # y-axis&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-33-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;log-transformations&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Log transformations&lt;/h3&gt;
&lt;p&gt;In some cases, it is useful to plot the log transformation of the variables. This can be done with the &lt;code&gt;scale_x_log10()&lt;/code&gt; and &lt;code&gt;scale_y_log10()&lt;/code&gt; functions:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + scale_x_log10() +
  scale_y_log10()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-34-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;limits&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Limits&lt;/h3&gt;
&lt;p&gt;The most convenient way to control the limits of the plot is to use again the &lt;code&gt;scale_x_continuous()&lt;/code&gt; and &lt;code&gt;scale_y_continuous()&lt;/code&gt; functions in addition to the &lt;code&gt;limits&lt;/code&gt; argument:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + scale_x_continuous(limits = c(3, 6)) +
  scale_y_continuous(limits = c(20, 30))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-35-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;It is also possible to simply take a subset of the dataset with the &lt;code&gt;subset()&lt;/code&gt; or &lt;code&gt;filter()&lt;/code&gt; function. See how to &lt;a href=&#34;https://statsandr.com/blog/data-manipulation-in-r/#subset-a-data-frame&#34;&gt;subset a dataset&lt;/a&gt; if you need a reminder.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;scales-for-better-axis-formats&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Scales for better axis formats&lt;/h3&gt;
&lt;p&gt;Depending on your data, it is possible to format axes in a certain way with the &lt;code&gt;{scales}&lt;/code&gt; package. The format I use the most is &lt;code&gt;comma&lt;/code&gt; which formats large numbers in a more-readable way.&lt;/p&gt;
&lt;p&gt;For this example, we multiply both variables by 10000 to have larger numbers and then we apply the format to the y-axis (only to the y-axis so we can see the difference with the x-axis which is not formatted):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = displ * 10000, y = hwy * 10000) +
  geom_point() +
  scale_y_continuous(labels = scales::comma) # format y-axis&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-36-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;As you can see, numbers on the y-axis are displayed as 200,000, 300,000, etc. instead of 200000, 300000, etc., which makes it more readable.&lt;/p&gt;
&lt;p&gt;Another common format is &lt;code&gt;percent&lt;/code&gt; to display numbers as percentages:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = displ, y = hwy / 100) +
  geom_point() +
  scale_y_continuous(labels = scales::percent) # format y-axis&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-37-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;These two formats make large numbers and percentages easier to read. Other formats are possible such as using dollar signs, dates etc. See more information in the &lt;a href=&#34;https://scales.r-lib.org/&#34; target=&#34;_blank&#34;&gt;package’s documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;legend&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Legend&lt;/h3&gt;
&lt;p&gt;By default, the legend is located to the right side of the plot (when there is a legend to be displayed of course).&lt;/p&gt;
&lt;p&gt;To control the position of the legend, we need to use the &lt;code&gt;theme()&lt;/code&gt; function in addition to the &lt;code&gt;legend.position&lt;/code&gt; argument:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + aes(color = class) +
  theme(legend.position = &amp;quot;top&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-38-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Replace &lt;code&gt;&#34;top&#34;&lt;/code&gt; by &lt;code&gt;&#34;left&#34;&lt;/code&gt; or &lt;code&gt;&#34;bottom&#34;&lt;/code&gt; to change its position and by &lt;code&gt;&#34;none&#34;&lt;/code&gt; to remove it.&lt;/p&gt;
&lt;p&gt;The title of the legend can be edited with the &lt;code&gt;labs()&lt;/code&gt; layer:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + aes(color = class) +
  labs(color = &amp;quot;Car&amp;#39;s class&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-39-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Note that the argument inside &lt;code&gt;labs()&lt;/code&gt; must match the one inside the &lt;code&gt;aes()&lt;/code&gt; layer (in this case: &lt;code&gt;color&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;The title of the legend can also be removed with &lt;code&gt;legend.title = element_blank()&lt;/code&gt; inside the &lt;code&gt;theme()&lt;/code&gt; layer:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + aes(color = class) +
  theme(
    legend.title = element_blank(),
    legend.position = &amp;quot;bottom&amp;quot;
  )&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-40-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The legend now appears at the bottom of the plot, without the legend title.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;shape-color-size-and-transparency&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Shape, color, size and transparency&lt;/h3&gt;
&lt;p&gt;There are a very large number of options to improve the quality of the plot or to add additional information. These include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;shape,&lt;/li&gt;
&lt;li&gt;size,&lt;/li&gt;
&lt;li&gt;color, and&lt;/li&gt;
&lt;li&gt;alpha (transparency).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can for instance change the shape of all points in a scatter plot by adding &lt;code&gt;shape&lt;/code&gt; to &lt;code&gt;geom_point()&lt;/code&gt;, or vary the shape according to the values taken by another variable (in that case, the &lt;code&gt;shape&lt;/code&gt; argument must be inside &lt;code&gt;aes()&lt;/code&gt;):&lt;a href=&#34;#fn3&#34; class=&#34;footnote-ref&#34; id=&#34;fnref3&#34;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Change shape of all points
ggplot(dat) +
  aes(x = displ, y = hwy) +
  geom_point(shape = 4)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-41-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Change shape of points based on a categorical variable
ggplot(dat) +
  aes(x = displ, y = hwy, shape = drv) +
  geom_point()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-41-2.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Following the same principle, we can modify the color, size and transparency of the points based on a &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#qualitative&#34;&gt;qualitative&lt;/a&gt; or &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#quantitative&#34;&gt;quantitative&lt;/a&gt; variable. Here are some examples:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p &amp;lt;- ggplot(dat) +
  aes(x = displ, y = hwy) +
  geom_point()

# Change color for all points
p + geom_point(color = &amp;quot;steelblue&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-42-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Change color based on a qualitative variable
p + aes(color = drv)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-42-2.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Change color based on a quantitative variable
p + aes(color = cty)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-42-3.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Change color based on a criterion (median of cty variable)
p + aes(color = cty &amp;gt; median(cty))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-42-4.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Change size of all points
p + geom_point(size = 4)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-42-5.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Change size of points based on a quantitative variable
p + aes(size = cty)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-42-6.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Change transparency based on a quantitative variable
p + aes(alpha = cty)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-42-7.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;We can of course mix several options (shape, color, size, alpha) to build more complex graphics:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + geom_point(size = 0.5) +
  aes(color = drv, shape = year, alpha = cty)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-43-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;If you are unhappy with the default colors, you can change them manually with the &lt;code&gt;scale_colour_manual()&lt;/code&gt; layer (for qualitative variables) and the &lt;code&gt;scale_colour_gradient2()&lt;/code&gt; layer (for quantitative variables):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Change color based on a qualitative variable
p + aes(color = drv) +
  scale_colour_manual(values = c(&amp;quot;red&amp;quot;, &amp;quot;blue&amp;quot;, &amp;quot;green&amp;quot;))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-44-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Change color based on a quantitative variable
p + aes(color = cty) +
  scale_colour_gradient2(
    low = &amp;quot;green&amp;quot;,
    mid = &amp;quot;gray&amp;quot;,
    high = &amp;quot;red&amp;quot;,
    midpoint = median(dat$cty)
  )&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-44-2.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;For your information, you can emulate &lt;code&gt;{ggplot2}&lt;/code&gt; default color palette for a desired number of colors and produce a character vector of HEX colors. For example, with 4 colors:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(scales)
show_col(hue_pal()(4))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-45-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;text-and-labels&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Text and labels&lt;/h3&gt;
&lt;p&gt;To add a label on a point (for example the row number), we can use the &lt;code&gt;geom_text()&lt;/code&gt; and &lt;code&gt;aes()&lt;/code&gt; functions:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + geom_text(aes(label = rownames(dat)),
  check_overlap = TRUE,
  size = 2,
  vjust = -1
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-46-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;To add text on the plot, we use the &lt;code&gt;annotate()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + annotate(&amp;quot;text&amp;quot;,
  x = 6,
  y = 40,
  label = &amp;quot;hwy and displ are \n negatively correlated \n (rho = -0.77, p-value &amp;lt; 0.001)&amp;quot;,
  size = 3
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-47-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Read the article on &lt;a href=&#34;https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/&#34;&gt;correlation coefficient and correlation test in R&lt;/a&gt; to see how I computed the correlation coefficient (rho) and the &lt;em&gt;p&lt;/em&gt;-value of the correlation test.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;smooth-and-regression-lines&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Smooth and regression lines&lt;/h3&gt;
&lt;p&gt;In a scatter plot, it is possible to add a smooth line fitted to the data:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + geom_smooth()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-48-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;In the context of simple &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/&#34;&gt;linear regression&lt;/a&gt;, it is often the case that the regression line is displayed on the plot.&lt;/p&gt;
&lt;p&gt;This can be done by adding &lt;code&gt;method = lm&lt;/code&gt; (&lt;code&gt;lm&lt;/code&gt; stands for linear model) in the &lt;code&gt;geom_smooth()&lt;/code&gt; layer:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + geom_smooth(method = lm)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-49-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;It is also possible to draw a regression line for each level of a categorical variable:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + aes(color = drv, shape = drv) +
  geom_smooth(method = lm, se = FALSE)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-50-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;se = FALSE&lt;/code&gt; argument removes the confidence interval around the regression lines.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;facets&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Facets&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;facet_grid&lt;/code&gt; allows you to divide the same graphic into several panels according to the values of one or two qualitative variables:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# According to one variable
p + facet_grid(. ~ drv)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-51-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# According to 2 variables
p + facet_grid(drv ~ year)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-51-2.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;It is then possible to add a regression line to each facet:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + facet_grid(. ~ drv) +
  geom_smooth(method = lm)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-52-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;facet_wrap()&lt;/code&gt; can also be used, as illustrated in this &lt;a href=&#34;https://statsandr.com/blog/graphics-in-r-with-ggplot2/#boxplot&#34;&gt;section&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;themes&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Themes&lt;/h3&gt;
&lt;p&gt;Several functions are available in the &lt;code&gt;{ggplot2}&lt;/code&gt; package to change the theme of the plot.&lt;/p&gt;
&lt;p&gt;The most common themes after the default theme (i.e., &lt;code&gt;theme_gray()&lt;/code&gt;) are the black and white (&lt;code&gt;theme_bw()&lt;/code&gt;), minimal (&lt;code&gt;theme_minimal()&lt;/code&gt;) and classic (&lt;code&gt;theme_classic()&lt;/code&gt;) themes:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Black and white theme
p + theme_bw()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-53-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Minimal theme
p + theme_minimal()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-53-2.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Classic theme
p + theme_classic()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-53-3.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;I tend to use the minimal theme for most of my &lt;a href=&#34;https://statsandr.com/blog/getting-started-in-r-markdown/&#34;&gt;R Markdown&lt;/a&gt; reports as it brings out the patterns and points and not the layout of the plot, but again this is a matter of personal taste. See more themes at &lt;a href=&#34;https://ggplot2.tidyverse.org/reference/ggtheme.html&#34; target=&#34;_blank&#34;&gt;ggplot2.tidyverse.org/reference/ggtheme.html&lt;/a&gt; and in the &lt;code&gt;{ggthemes}&lt;/code&gt; package.&lt;/p&gt;
&lt;p&gt;In order to avoid having to change the theme for each plot you create, you can change the theme for the current R session using the &lt;code&gt;theme_set()&lt;/code&gt; function as follows:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;theme_set(theme_minimal())&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;interactive-plot-with-plotly&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Interactive plot with &lt;code&gt;{plotly}&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;You can easily make your plots created with &lt;code&gt;{ggplot2}&lt;/code&gt; interactive with the &lt;code&gt;{plotly}&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(plotly)
ggplotly(p + aes(color = year))&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&#34;plotly html-widget html-fill-item&#34; id=&#34;htmlwidget-1&#34; style=&#34;width:100%;height:480px;&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-1&#34;&gt;{&#34;x&#34;:{&#34;data&#34;:[{&#34;x&#34;:[1.8,1.8,2.7999999999999998,2.7999999999999998,1.8,1.8,2.7999999999999998,2.7999999999999998,2.7999999999999998,5.7000000000000002,5.7000000000000002,5.7000000000000002,5.7000000000000002,6.5,2.3999999999999999,3.1000000000000001,2.3999999999999999,3,3.2999999999999998,3.2999999999999998,3.7999999999999998,3.7999999999999998,3.8999999999999999,3.8999999999999999,5.2000000000000002,5.2000000000000002,3.8999999999999999,5.2000000000000002,5.9000000000000004,5.2000000000000002,5.2000000000000002,5.9000000000000004,4.5999999999999996,5.4000000000000004,4,4,4,5,4.2000000000000002,4.2000000000000002,4.5999999999999996,4.5999999999999996,5.4000000000000004,3.7999999999999998,3.7999999999999998,4.5999999999999996,4.5999999999999996,1.6000000000000001,1.6000000000000001,1.6000000000000001,1.6000000000000001,1.6000000000000001,2.3999999999999999,2.3999999999999999,2.5,2.5,2,2,4,4.7000000000000002,4,4.5999999999999996,5.4000000000000004,5.4000000000000004,4,5,2.3999999999999999,2.3999999999999999,3,3,3.2999999999999998,3.2999999999999998,3.1000000000000001,3.7999999999999998,3.7999999999999998,2.5,2.5,2.2000000000000002,2.2000000000000002,2.5,2.5,2.7000000000000002,2.7000000000000002,3.3999999999999999,3.3999999999999999,2.2000000000000002,2.2000000000000002,3,3,2.2000000000000002,2.2000000000000002,3,3,1.8,1.8,1.8,4.7000000000000002,2.7000000000000002,2.7000000000000002,3.3999999999999999,3.3999999999999999,2,2,2.7999999999999998,1.8999999999999999,2,2,2.7999999999999998,2.7999999999999998,1.8999999999999999,1.8999999999999999,2,2,1.8,1.8,2.7999999999999998,2.7999999999999998],&#34;y&#34;:[29,29,26,26,26,25,25,25,24,17,26,23,15,17,27,26,24,24,22,22,22,21,17,17,17,15,17,16,15,15,16,15,17,17,17,19,17,17,17,17,16,16,15,26,25,21,22,33,32,32,29,32,26,27,26,26,26,29,20,17,15,15,17,16,17,17,29,27,26,25,17,17,26,26,27,25,24,26,26,26,26,20,20,19,17,29,27,26,26,27,29,26,26,30,33,35,15,20,20,17,19,29,26,24,44,29,26,23,24,44,41,29,26,29,29,26,26],&#34;text&#34;:[&#34;year: 1999&lt;br /&gt;displ: 1.8&lt;br /&gt;hwy: 29&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.8&lt;br /&gt;hwy: 29&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.8&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.8&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.8&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.8&lt;br /&gt;hwy: 25&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.8&lt;br /&gt;hwy: 25&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.8&lt;br /&gt;hwy: 25&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.8&lt;br /&gt;hwy: 24&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.7&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.7&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.7&lt;br /&gt;hwy: 23&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.7&lt;br /&gt;hwy: 15&#34;,&#34;year: 1999&lt;br /&gt;displ: 6.5&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.4&lt;br /&gt;hwy: 27&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.1&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.4&lt;br /&gt;hwy: 24&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.0&lt;br /&gt;hwy: 24&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.3&lt;br /&gt;hwy: 22&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.3&lt;br /&gt;hwy: 22&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.8&lt;br /&gt;hwy: 22&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.8&lt;br /&gt;hwy: 21&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.9&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.9&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.2&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.2&lt;br /&gt;hwy: 15&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.9&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.2&lt;br /&gt;hwy: 16&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.9&lt;br /&gt;hwy: 15&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.2&lt;br /&gt;hwy: 15&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.2&lt;br /&gt;hwy: 16&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.9&lt;br /&gt;hwy: 15&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.6&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.4&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 19&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.0&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.2&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.2&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.6&lt;br /&gt;hwy: 16&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.6&lt;br /&gt;hwy: 16&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.4&lt;br /&gt;hwy: 15&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.8&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.8&lt;br /&gt;hwy: 25&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.6&lt;br /&gt;hwy: 21&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.6&lt;br /&gt;hwy: 22&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.6&lt;br /&gt;hwy: 33&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.6&lt;br /&gt;hwy: 32&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.6&lt;br /&gt;hwy: 32&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.6&lt;br /&gt;hwy: 29&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.6&lt;br /&gt;hwy: 32&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.4&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.4&lt;br /&gt;hwy: 27&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 29&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 20&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 15&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.6&lt;br /&gt;hwy: 15&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.4&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.4&lt;br /&gt;hwy: 16&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 5.0&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.4&lt;br /&gt;hwy: 29&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.4&lt;br /&gt;hwy: 27&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.0&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.0&lt;br /&gt;hwy: 25&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.3&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.3&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.1&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.8&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.8&lt;br /&gt;hwy: 27&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 25&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 24&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.2&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.2&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.7&lt;br /&gt;hwy: 20&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.7&lt;br /&gt;hwy: 20&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.4&lt;br /&gt;hwy: 19&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.4&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.2&lt;br /&gt;hwy: 29&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.2&lt;br /&gt;hwy: 27&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.0&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.0&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.2&lt;br /&gt;hwy: 27&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.2&lt;br /&gt;hwy: 29&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.0&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.0&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.8&lt;br /&gt;hwy: 30&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.8&lt;br /&gt;hwy: 33&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.8&lt;br /&gt;hwy: 35&#34;,&#34;year: 1999&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 15&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.7&lt;br /&gt;hwy: 20&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.7&lt;br /&gt;hwy: 20&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.4&lt;br /&gt;hwy: 17&#34;,&#34;year: 1999&lt;br /&gt;displ: 3.4&lt;br /&gt;hwy: 19&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 29&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.8&lt;br /&gt;hwy: 24&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.9&lt;br /&gt;hwy: 44&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 29&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.8&lt;br /&gt;hwy: 23&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.8&lt;br /&gt;hwy: 24&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.9&lt;br /&gt;hwy: 44&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.9&lt;br /&gt;hwy: 41&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 29&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.8&lt;br /&gt;hwy: 29&#34;,&#34;year: 1999&lt;br /&gt;displ: 1.8&lt;br /&gt;hwy: 29&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.8&lt;br /&gt;hwy: 26&#34;,&#34;year: 1999&lt;br /&gt;displ: 2.8&lt;br /&gt;hwy: 26&#34;],&#34;type&#34;:&#34;scatter&#34;,&#34;mode&#34;:&#34;markers&#34;,&#34;marker&#34;:{&#34;autocolorscale&#34;:false,&#34;color&#34;:&#34;rgba(248,118,109,1)&#34;,&#34;opacity&#34;:1,&#34;size&#34;:5.6692913385826778,&#34;symbol&#34;:&#34;circle&#34;,&#34;line&#34;:{&#34;width&#34;:1.8897637795275593,&#34;color&#34;:&#34;rgba(248,118,109,1)&#34;}},&#34;hoveron&#34;:&#34;points&#34;,&#34;name&#34;:&#34;1999&#34;,&#34;legendgroup&#34;:&#34;1999&#34;,&#34;showlegend&#34;:true,&#34;xaxis&#34;:&#34;x&#34;,&#34;yaxis&#34;:&#34;y&#34;,&#34;hoverinfo&#34;:&#34;text&#34;,&#34;frame&#34;:null},{&#34;x&#34;:[2,2,3.1000000000000001,2,2,3.1000000000000001,3.1000000000000001,3.1000000000000001,4.2000000000000002,5.2999999999999998,5.2999999999999998,5.2999999999999998,6,6.2000000000000002,6.2000000000000002,7,5.2999999999999998,5.2999999999999998,2.3999999999999999,3.5,3.6000000000000001,3.2999999999999998,3.2999999999999998,3.2999999999999998,3.7999999999999998,4,3.7000000000000002,3.7000000000000002,4.7000000000000002,4.7000000000000002,4.7000000000000002,4.7000000000000002,4.7000000000000002,4.7000000000000002,5.7000000000000002,4.7000000000000002,4.7000000000000002,4.7000000000000002,4.7000000000000002,4.7000000000000002,4.7000000000000002,5.7000000000000002,5.4000000000000004,4,4.5999999999999996,4.5999999999999996,5.4000000000000004,4,4,4.5999999999999996,4.5999999999999996,5.4000000000000004,1.8,1.8,1.8,2,2.3999999999999999,2.3999999999999999,3.2999999999999998,2,2,2.7000000000000002,2.7000000000000002,2.7000000000000002,3,3.7000000000000002,4.7000000000000002,4.7000000000000002,5.7000000000000002,6.0999999999999996,4.2000000000000002,4.4000000000000004,5.4000000000000004,4,4.5999999999999996,2.5,2.5,3.5,3.5,3.5,4,5.5999999999999996,3.7999999999999998,5.2999999999999998,2.5,2.5,2.5,2.5,2.5,2.5,2.5,2.5,4,4.7000000000000002,2.3999999999999999,2.3999999999999999,3.5,2.3999999999999999,2.3999999999999999,3.2999999999999998,1.8,1.8,5.7000000000000002,2.7000000000000002,4,4,2,2,2,2,2.5,2.5,2.5,2.5,2,2,3.6000000000000001],&#34;y&#34;:[31,30,27,28,27,25,25,25,23,20,15,20,17,26,25,24,19,14,30,29,26,24,24,17,23,23,19,18,19,19,12,17,12,17,18,16,12,17,17,16,12,17,18,19,19,17,17,26,24,23,22,20,34,36,36,29,30,31,28,28,27,24,24,24,22,19,12,19,18,14,18,18,18,19,19,31,32,27,26,25,20,18,28,25,27,25,26,23,25,27,25,27,20,17,31,31,28,31,31,27,37,35,18,22,18,20,29,29,29,29,29,29,28,29,28,29,26],&#34;text&#34;:[&#34;year: 2008&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 31&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 30&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.1&lt;br /&gt;hwy: 27&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 28&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 27&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.1&lt;br /&gt;hwy: 25&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.1&lt;br /&gt;hwy: 25&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.1&lt;br /&gt;hwy: 25&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.2&lt;br /&gt;hwy: 23&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.3&lt;br /&gt;hwy: 20&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.3&lt;br /&gt;hwy: 15&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.3&lt;br /&gt;hwy: 20&#34;,&#34;year: 2008&lt;br /&gt;displ: 6.0&lt;br /&gt;hwy: 17&#34;,&#34;year: 2008&lt;br /&gt;displ: 6.2&lt;br /&gt;hwy: 26&#34;,&#34;year: 2008&lt;br /&gt;displ: 6.2&lt;br /&gt;hwy: 25&#34;,&#34;year: 2008&lt;br /&gt;displ: 7.0&lt;br /&gt;hwy: 24&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.3&lt;br /&gt;hwy: 19&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.3&lt;br /&gt;hwy: 14&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.4&lt;br /&gt;hwy: 30&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.5&lt;br /&gt;hwy: 29&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.6&lt;br /&gt;hwy: 26&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.3&lt;br /&gt;hwy: 24&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.3&lt;br /&gt;hwy: 24&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.3&lt;br /&gt;hwy: 17&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.8&lt;br /&gt;hwy: 23&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 23&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.7&lt;br /&gt;hwy: 19&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.7&lt;br /&gt;hwy: 18&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 19&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 19&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 12&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 17&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 12&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 17&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.7&lt;br /&gt;hwy: 18&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 16&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 12&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 17&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 17&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 16&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 12&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.7&lt;br /&gt;hwy: 17&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.4&lt;br /&gt;hwy: 18&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 19&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.6&lt;br /&gt;hwy: 19&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.6&lt;br /&gt;hwy: 17&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.4&lt;br /&gt;hwy: 17&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 26&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 24&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.6&lt;br /&gt;hwy: 23&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.6&lt;br /&gt;hwy: 22&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.4&lt;br /&gt;hwy: 20&#34;,&#34;year: 2008&lt;br /&gt;displ: 1.8&lt;br /&gt;hwy: 34&#34;,&#34;year: 2008&lt;br /&gt;displ: 1.8&lt;br /&gt;hwy: 36&#34;,&#34;year: 2008&lt;br /&gt;displ: 1.8&lt;br /&gt;hwy: 36&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 29&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.4&lt;br /&gt;hwy: 30&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.4&lt;br /&gt;hwy: 31&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.3&lt;br /&gt;hwy: 28&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 28&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 27&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.7&lt;br /&gt;hwy: 24&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.7&lt;br /&gt;hwy: 24&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.7&lt;br /&gt;hwy: 24&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.0&lt;br /&gt;hwy: 22&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.7&lt;br /&gt;hwy: 19&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 12&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 19&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.7&lt;br /&gt;hwy: 18&#34;,&#34;year: 2008&lt;br /&gt;displ: 6.1&lt;br /&gt;hwy: 14&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.2&lt;br /&gt;hwy: 18&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.4&lt;br /&gt;hwy: 18&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.4&lt;br /&gt;hwy: 18&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 19&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.6&lt;br /&gt;hwy: 19&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 31&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 32&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.5&lt;br /&gt;hwy: 27&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.5&lt;br /&gt;hwy: 26&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.5&lt;br /&gt;hwy: 25&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 20&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.6&lt;br /&gt;hwy: 18&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.8&lt;br /&gt;hwy: 28&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.3&lt;br /&gt;hwy: 25&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 27&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 25&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 26&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 23&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 25&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 27&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 25&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 27&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 20&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.7&lt;br /&gt;hwy: 17&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.4&lt;br /&gt;hwy: 31&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.4&lt;br /&gt;hwy: 31&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.5&lt;br /&gt;hwy: 28&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.4&lt;br /&gt;hwy: 31&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.4&lt;br /&gt;hwy: 31&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.3&lt;br /&gt;hwy: 27&#34;,&#34;year: 2008&lt;br /&gt;displ: 1.8&lt;br /&gt;hwy: 37&#34;,&#34;year: 2008&lt;br /&gt;displ: 1.8&lt;br /&gt;hwy: 35&#34;,&#34;year: 2008&lt;br /&gt;displ: 5.7&lt;br /&gt;hwy: 18&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.7&lt;br /&gt;hwy: 22&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 18&#34;,&#34;year: 2008&lt;br /&gt;displ: 4.0&lt;br /&gt;hwy: 20&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 29&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 29&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 29&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 29&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 29&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 29&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 28&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.5&lt;br /&gt;hwy: 29&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 28&#34;,&#34;year: 2008&lt;br /&gt;displ: 2.0&lt;br /&gt;hwy: 29&#34;,&#34;year: 2008&lt;br /&gt;displ: 3.6&lt;br /&gt;hwy: 26&#34;],&#34;type&#34;:&#34;scatter&#34;,&#34;mode&#34;:&#34;markers&#34;,&#34;marker&#34;:{&#34;autocolorscale&#34;:false,&#34;color&#34;:&#34;rgba(0,191,196,1)&#34;,&#34;opacity&#34;:1,&#34;size&#34;:5.6692913385826778,&#34;symbol&#34;:&#34;circle&#34;,&#34;line&#34;:{&#34;width&#34;:1.8897637795275593,&#34;color&#34;:&#34;rgba(0,191,196,1)&#34;}},&#34;hoveron&#34;:&#34;points&#34;,&#34;name&#34;:&#34;2008&#34;,&#34;legendgroup&#34;:&#34;2008&#34;,&#34;showlegend&#34;:true,&#34;xaxis&#34;:&#34;x&#34;,&#34;yaxis&#34;:&#34;y&#34;,&#34;hoverinfo&#34;:&#34;text&#34;,&#34;frame&#34;:null}],&#34;layout&#34;:{&#34;margin&#34;:{&#34;t&#34;:23.305936073059364,&#34;r&#34;:7.3059360730593621,&#34;b&#34;:37.260273972602747,&#34;l&#34;:37.260273972602747},&#34;plot_bgcolor&#34;:&#34;rgba(235,235,235,1)&#34;,&#34;paper_bgcolor&#34;:&#34;rgba(255,255,255,1)&#34;,&#34;font&#34;:{&#34;color&#34;:&#34;rgba(0,0,0,1)&#34;,&#34;family&#34;:&#34;&#34;,&#34;size&#34;:14.611872146118724},&#34;xaxis&#34;:{&#34;domain&#34;:[0,1],&#34;automargin&#34;:true,&#34;type&#34;:&#34;linear&#34;,&#34;autorange&#34;:false,&#34;range&#34;:[1.3300000000000001,7.2699999999999996],&#34;tickmode&#34;:&#34;array&#34;,&#34;ticktext&#34;:[&#34;2&#34;,&#34;3&#34;,&#34;4&#34;,&#34;5&#34;,&#34;6&#34;,&#34;7&#34;],&#34;tickvals&#34;:[2,3,4,5,6,7],&#34;categoryorder&#34;:&#34;array&#34;,&#34;categoryarray&#34;:[&#34;2&#34;,&#34;3&#34;,&#34;4&#34;,&#34;5&#34;,&#34;6&#34;,&#34;7&#34;],&#34;nticks&#34;:null,&#34;ticks&#34;:&#34;outside&#34;,&#34;tickcolor&#34;:&#34;rgba(51,51,51,1)&#34;,&#34;ticklen&#34;:3.6529680365296811,&#34;tickwidth&#34;:0.66417600664176002,&#34;showticklabels&#34;:true,&#34;tickfont&#34;:{&#34;color&#34;:&#34;rgba(77,77,77,1)&#34;,&#34;family&#34;:&#34;&#34;,&#34;size&#34;:11.68949771689498},&#34;tickangle&#34;:-0,&#34;showline&#34;:false,&#34;linecolor&#34;:null,&#34;linewidth&#34;:0,&#34;showgrid&#34;:true,&#34;gridcolor&#34;:&#34;rgba(255,255,255,1)&#34;,&#34;gridwidth&#34;:0.66417600664176002,&#34;zeroline&#34;:false,&#34;anchor&#34;:&#34;y&#34;,&#34;title&#34;:{&#34;text&#34;:&#34;displ&#34;,&#34;font&#34;:{&#34;color&#34;:&#34;rgba(0,0,0,1)&#34;,&#34;family&#34;:&#34;&#34;,&#34;size&#34;:14.611872146118724}},&#34;hoverformat&#34;:&#34;.2f&#34;},&#34;yaxis&#34;:{&#34;domain&#34;:[0,1],&#34;automargin&#34;:true,&#34;type&#34;:&#34;linear&#34;,&#34;autorange&#34;:false,&#34;range&#34;:[10.4,45.600000000000001],&#34;tickmode&#34;:&#34;array&#34;,&#34;ticktext&#34;:[&#34;20&#34;,&#34;30&#34;,&#34;40&#34;],&#34;tickvals&#34;:[20,30,40],&#34;categoryorder&#34;:&#34;array&#34;,&#34;categoryarray&#34;:[&#34;20&#34;,&#34;30&#34;,&#34;40&#34;],&#34;nticks&#34;:null,&#34;ticks&#34;:&#34;outside&#34;,&#34;tickcolor&#34;:&#34;rgba(51,51,51,1)&#34;,&#34;ticklen&#34;:3.6529680365296811,&#34;tickwidth&#34;:0.66417600664176002,&#34;showticklabels&#34;:true,&#34;tickfont&#34;:{&#34;color&#34;:&#34;rgba(77,77,77,1)&#34;,&#34;family&#34;:&#34;&#34;,&#34;size&#34;:11.68949771689498},&#34;tickangle&#34;:-0,&#34;showline&#34;:false,&#34;linecolor&#34;:null,&#34;linewidth&#34;:0,&#34;showgrid&#34;:true,&#34;gridcolor&#34;:&#34;rgba(255,255,255,1)&#34;,&#34;gridwidth&#34;:0.66417600664176002,&#34;zeroline&#34;:false,&#34;anchor&#34;:&#34;x&#34;,&#34;title&#34;:{&#34;text&#34;:&#34;hwy&#34;,&#34;font&#34;:{&#34;color&#34;:&#34;rgba(0,0,0,1)&#34;,&#34;family&#34;:&#34;&#34;,&#34;size&#34;:14.611872146118724}},&#34;hoverformat&#34;:&#34;.2f&#34;},&#34;shapes&#34;:[],&#34;showlegend&#34;:true,&#34;legend&#34;:{&#34;bgcolor&#34;:&#34;rgba(255,255,255,1)&#34;,&#34;bordercolor&#34;:&#34;transparent&#34;,&#34;borderwidth&#34;:1.8897637795275593,&#34;font&#34;:{&#34;color&#34;:&#34;rgba(0,0,0,1)&#34;,&#34;family&#34;:&#34;&#34;,&#34;size&#34;:11.68949771689498},&#34;title&#34;:{&#34;text&#34;:&#34;year&#34;,&#34;font&#34;:{&#34;color&#34;:&#34;rgba(0,0,0,1)&#34;,&#34;family&#34;:&#34;&#34;,&#34;size&#34;:14.611872146118724}}},&#34;hovermode&#34;:&#34;closest&#34;,&#34;barmode&#34;:&#34;relative&#34;},&#34;config&#34;:{&#34;doubleClick&#34;:&#34;reset&#34;,&#34;modeBarButtonsToAdd&#34;:[&#34;hoverclosest&#34;,&#34;hovercompare&#34;],&#34;showSendToCloud&#34;:false},&#34;source&#34;:&#34;A&#34;,&#34;attrs&#34;:{&#34;aece6d48ddbc&#34;:{&#34;colour&#34;:{},&#34;x&#34;:{},&#34;y&#34;:{},&#34;type&#34;:&#34;scatter&#34;}},&#34;cur_data&#34;:&#34;aece6d48ddbc&#34;,&#34;visdat&#34;:{&#34;aece6d48ddbc&#34;:[&#34;function (y) &#34;,&#34;x&#34;]},&#34;highlight&#34;:{&#34;on&#34;:&#34;plotly_click&#34;,&#34;persistent&#34;:false,&#34;dynamic&#34;:false,&#34;selectize&#34;:false,&#34;opacityDim&#34;:0.20000000000000001,&#34;selected&#34;:{&#34;opacity&#34;:1},&#34;debounce&#34;:0},&#34;shinyEvents&#34;:[&#34;plotly_hover&#34;,&#34;plotly_click&#34;,&#34;plotly_selected&#34;,&#34;plotly_relayout&#34;,&#34;plotly_brushed&#34;,&#34;plotly_brushing&#34;,&#34;plotly_clickannotation&#34;,&#34;plotly_doubleclick&#34;,&#34;plotly_deselect&#34;,&#34;plotly_afterplot&#34;,&#34;plotly_sunburstclick&#34;],&#34;base_url&#34;:&#34;https://plot.ly&#34;},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;You can now hover over a point to display more information about that point. There is also the possibility to zoom in and out, to download the plot, to select some observations, etc. More information about &lt;code&gt;{plotly}&lt;/code&gt; for R can be found &lt;a href=&#34;https://plotly.com/r/&#34; target=&#34;_blank&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;combine-plots-with-patchwork&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Combine plots with &lt;code&gt;{patchwork}&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;There are several ways to combine plots made in &lt;code&gt;{ggplot2}&lt;/code&gt;. In my opinion, the most convenient way is with the &lt;code&gt;{patchwork}&lt;/code&gt; package using symbols such as &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt; and parentheses.&lt;/p&gt;
&lt;p&gt;We first need to create some plots and save them:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p_a &amp;lt;- ggplot(dat) +
  aes(x = displ, y = hwy) +
  geom_point()

p_b &amp;lt;- ggplot(dat) +
  aes(x = hwy) +
  geom_histogram()

p_c &amp;lt;- ggplot(dat) +
  aes(x = drv, y = hwy) +
  geom_boxplot()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that we have 3 plots saved in our environment, we can combine them. To have plots &lt;strong&gt;next to each other&lt;/strong&gt; simply use the &lt;code&gt;+&lt;/code&gt; symbol:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(patchwork)
p_a + p_b + p_c&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-57-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;To display them &lt;strong&gt;above each other&lt;/strong&gt; simply use the &lt;code&gt;/&lt;/code&gt; symbol:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p_a / p_b / p_c&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-58-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;And finally, to combine them &lt;strong&gt;above and next&lt;/strong&gt; to each other, mix &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt; and parentheses:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p_a + p_b / p_c&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-59-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;(p_a + p_b) / p_c&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-59-2.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;See more ways to combine plots with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;grid.arrange()&lt;/code&gt; from the &lt;code&gt;{gridExtra}&lt;/code&gt; package&lt;/li&gt;
&lt;li&gt;&lt;code&gt;plot_grid()&lt;/code&gt; from the &lt;code&gt;{cowplot}&lt;/code&gt; package&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;flip-coordinates&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Flip coordinates&lt;/h3&gt;
&lt;p&gt;Flipping coordinates of your plot is useful to create horizontal boxplots, or when labels of a variable are so long that they overlap each other on the x-axis. See with and without flipping coordinates below:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# without flipping coordinates
p1 &amp;lt;- ggplot(dat) +
  aes(x = class, y = hwy) +
  geom_boxplot()

# with flipping coordinates
p2 &amp;lt;- ggplot(dat) +
  aes(x = class, y = hwy) +
  geom_boxplot() +
  coord_flip()

library(patchwork)
p1 + p2 # left: without flipping, right: with flipping&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-60-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;This can be done with many types of plot, not only with boxplots. For instance, if a categorical variable has many levels or the labels are long, it is usually best to flip the coordinates for a better visual:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = class) +
  geom_bar() +
  coord_flip()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-61-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;save-plot&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Save plot&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;ggsave()&lt;/code&gt; function will save the most recent plot in your current &lt;a href=&#34;https://statsandr.com/blog/how-to-import-an-excel-file-in-rstudio/#r-working-directory&#34;&gt;working directory&lt;/a&gt; unless you specify a path to another folder:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = displ, y = hwy) +
  geom_point()

ggsave(&amp;quot;plot1.pdf&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can also specify the width, height and resolution (&lt;code&gt;dpi&lt;/code&gt;) as follows:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggsave(&amp;quot;plot1.pdf&amp;quot;,
  width = 12,
  height = 12,
  units = &amp;quot;cm&amp;quot;,
  dpi = 300
)&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;managing-dates&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Managing dates&lt;/h3&gt;
&lt;p&gt;If the time variable in your dataset is in date format, the &lt;code&gt;{ggplot2}&lt;/code&gt; package recognizes the date format and automatically uses a specific type for the axis ticks.&lt;/p&gt;
&lt;p&gt;There is no time variable with a date format in our dataset, so let’s create a new variable of this type thanks to the &lt;code&gt;as.Date()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat$date &amp;lt;- as.Date(&amp;quot;2020-08-21&amp;quot;) - 0:(nrow(dat) - 1)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;See the first 6 observations of this date variable and its class:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;head(dat$date)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;2020-08-21&amp;quot; &amp;quot;2020-08-20&amp;quot; &amp;quot;2020-08-19&amp;quot; &amp;quot;2020-08-18&amp;quot; &amp;quot;2020-08-17&amp;quot;
## [6] &amp;quot;2020-08-16&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;str(dat$date)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##  Date[1:234], format: &amp;quot;2020-08-21&amp;quot; &amp;quot;2020-08-20&amp;quot; &amp;quot;2020-08-19&amp;quot; &amp;quot;2020-08-18&amp;quot; &amp;quot;2020-08-17&amp;quot; ...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The new variable &lt;code&gt;date&lt;/code&gt; is correctly specified in a date format.&lt;/p&gt;
&lt;p&gt;Most of the time, with a time variable, we want to create a line plot with the date on the X-axis and another continuous variable on the Y-axis, like the following plot for example:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p &amp;lt;- ggplot(dat) +
  aes(x = date, y = hwy) +
  geom_line()
p&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-66-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;As soon as the time variable is recognized as a date, we can use the &lt;code&gt;scale_x_date()&lt;/code&gt; layer to change the format displayed on the X-axis. The following table shows the most frequent date formats:&lt;/p&gt;
&lt;div class=&#34;float&#34;&gt;
&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/date%20formats%20in%20R.png&#34; style=&#34;width:100.0%&#34; alt=&#34;Source: www.statmethods.net&#34; /&gt;
&lt;div class=&#34;figcaption&#34;&gt;Source: www.statmethods.net&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;Run &lt;code&gt;?strptime()&lt;/code&gt; to see many more date formats available in R.&lt;/p&gt;
&lt;p&gt;For this example, let’s add the year in addition to the unabbreviated month:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + scale_x_date(date_labels = &amp;quot;%B %Y&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-67-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;It also possible to control the breaks to display on the X-axis with the &lt;code&gt;date_breaks&lt;/code&gt; argument. For this example, let’s say we want to display the day as number and the abbreviated month for each interval of 10 days:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + scale_x_date(date_breaks = &amp;quot;10 days&amp;quot;, date_labels = &amp;quot;%d %b&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-68-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;If labels displayed on the X-axis are unreadable because they overlap each other, you can rotate them with the &lt;code&gt;theme()&lt;/code&gt; layer and the &lt;code&gt;angle&lt;/code&gt; argument:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p + scale_x_date(date_breaks = &amp;quot;10 days&amp;quot;, date_labels = &amp;quot;%d %b&amp;quot;) +
  theme(axis.text.x = element_text(angle = 60, hjust = 1))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-69-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;highlight-data-with-gghighlight&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Highlight data with &lt;code&gt;{gghighlight}&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;{gghighlight}&lt;/code&gt; package allows, as its name suggests, to highlight some data directly on your ggplot. The highlighted data (that you define) are shown in bright color and the rest in gray.&lt;/p&gt;
&lt;p&gt;Below example of how it works for a scatter plot, boxplot, barplot and histogram.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(gghighlight)

# scatter plot
ggplot(mpg, aes(x = displ, y = hwy, color = cyl)) +
  geom_point() +
  gghighlight(cyl == &amp;quot;8&amp;quot;) +
  theme(legend.position = &amp;quot;none&amp;quot;) # remove legend&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-70-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# boxplot
ggplot(dat) +
  aes(x = drv, y = hwy, fill = drv) +
  geom_boxplot() +
  gghighlight(drv %in% c(&amp;quot;r&amp;quot;, &amp;quot;4&amp;quot;)) +
  theme(legend.position = &amp;quot;none&amp;quot;) # remove legend&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-70-2.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# barplot
ggplot(dat) +
  aes(x = drv, fill = drv) +
  geom_bar() +
  gghighlight(drv == &amp;quot;f&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-70-3.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;As you can see, the &lt;code&gt;gghighlight()&lt;/code&gt; layer accepts different types of conditions, but also several of them at the same time:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# histogram
ggplot(dat) +
  aes(x = hwy, fill = year) +
  geom_histogram() +
  gghighlight(displ &amp;gt; 2 &amp;amp; year == &amp;quot;1999&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-71-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;tip&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Tip&lt;/h1&gt;
&lt;p&gt;I recently learned a tip very useful when drawing plots with &lt;code&gt;{ggplot2}&lt;/code&gt;. If like me, you often comment and uncomment some lines of code in your plot, you know that you cannot transform the last line into a comment without removing the &lt;code&gt;+&lt;/code&gt; sign in the line just above.&lt;/p&gt;
&lt;p&gt;Adding a line &lt;code&gt;NULL&lt;/code&gt; at the end of your plots will avoid an &lt;a href=&#34;https://statsandr.com/blog/top-10-errors-in-r/#forgetting-the-sign-in-ggplot2&#34;&gt;error&lt;/a&gt; if you forget to remove the &lt;code&gt;+&lt;/code&gt; sign in the last line of your code. See with this basic example:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = date, y = hwy) +
  geom_line() + # I do not have to remove the + sign
  # theme_minimal() + # this line is a comment
  NULL # adding this line doesn&amp;#39;t change anything to the plot&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-08-21-graphics-in-r-with-ggplot2_files/figure-html/unnamed-chunk-72-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;This trick saves me a lot of time as I do not need to worry about making sure to remove the last &lt;code&gt;+&lt;/code&gt; sign after commenting some lines of code in my plots.&lt;/p&gt;
&lt;p&gt;If you find this trick useful, you may like these other &lt;a href=&#34;https://statsandr.com/blog/tips-and-tricks-in-rstudio-and-r-markdown/&#34;&gt;tips and tricks in RStudio and R Markdown&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;to-go-further&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;To go further&lt;/h1&gt;
&lt;p&gt;By now you have seen that &lt;code&gt;{ggplot2}&lt;/code&gt; is a very powerful and complete package to create plots in R. This article illustrated only the tip of the iceberg, and you will find many tutorials on how to create more advanced plots and visualizations with &lt;code&gt;{ggplot2}&lt;/code&gt; online. If you want to learn more than what is described in the present article, I highly recommend starting with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the chapters &lt;a href=&#34;https://r4ds.had.co.nz/data-visualisation.html&#34; target=&#34;_blank&#34;&gt;Data visualisation&lt;/a&gt; and &lt;a href=&#34;https://r4ds.had.co.nz/graphics-for-communication.html&#34; target=&#34;_blank&#34;&gt;Graphics for communication&lt;/a&gt; from the book &lt;a href=&#34;https://r4ds.had.co.nz/&#34; target=&#34;_blank&#34;&gt;R for Data Science&lt;/a&gt; from Garrett Grolemund and Hadley Wickham&lt;/li&gt;
&lt;li&gt;the book &lt;a href=&#34;https://ggplot2-book.org/&#34; target=&#34;_blank&#34;&gt;ggplot2: Elegant Graphics for Data Analysis&lt;/a&gt; from Hadley Wickham&lt;/li&gt;
&lt;li&gt;the book &lt;a href=&#34;https://r-graphics.org/&#34; target=&#34;_blank&#34;&gt;R Graphics Cookbook&lt;/a&gt; from Winston Chang&lt;/li&gt;
&lt;li&gt;the &lt;a href=&#34;https://exts.ggplot2.tidyverse.org/gallery/&#34; target=&#34;_blank&#34;&gt;ggplot2 extensions guide&lt;/a&gt; which lists many of the packages that extend &lt;code&gt;{ggplot2}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;the &lt;a href=&#34;https://statsandr.com/blog/files/ggplot2-cheatsheet.pdf&#34;&gt;&lt;code&gt;{ggplot2}&lt;/code&gt; cheat sheet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;this &lt;a href=&#34;https://pkg.garrickadenbuie.com/gentle-ggplot2/#1&#34;&gt;presentation&lt;/a&gt; by Garrick Aden-Buie&lt;/li&gt;
&lt;li&gt;a detailed tutorial by &lt;a href=&#34;https://cedricscherer.netlify.app/2019/08/05/a-ggplot2-tutorial-for-beautiful-plotting-in-r/&#34;&gt;Cédric Scherer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;conclusion&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;Thanks for reading.&lt;/p&gt;
&lt;p&gt;I hope this article helped you to create your first plots with the &lt;code&gt;{ggplot2}&lt;/code&gt; package. As a reminder, for simple graphs, it is sometimes easier to draw them via the &lt;a href=&#34;https://statsandr.com/blog/rstudio-addins-or-how-to-make-your-coding-life-easier/#esquisse&#34;&gt;{esquisse} addin&lt;/a&gt;. After some time, you will quickly learn how to create them by yourselves and in no time you will be able to build complex and sophisticated data visualizations.&lt;/p&gt;
&lt;p&gt;As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;footnotes footnotes-end-of-document&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;Use the &lt;code&gt;geom_jitter()&lt;/code&gt; layer with caution because, although it makes a plot more revealing at large scales, it also makes it slightly less accurate at small scales since some randomness is added to the points.&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn2&#34;&gt;&lt;p&gt;Code inspired from Claire Della Vedova (&lt;a href=&#34;https://delladata.fr/raincloud-plot/&#34; target=&#34;_blank&#34;&gt;DellaData&lt;/a&gt;) and &lt;a href=&#34;https://www.cedricscherer.com/2021/06/06/visualizing-distributions-with-raincloud-plots-and-how-to-create-them-with-ggplot2/&#34; target=&#34;_blank&#34;&gt;Cédric Scherer&lt;/a&gt;. Thanks to both of them for this nice plot!&lt;a href=&#34;#fnref2&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn3&#34;&gt;&lt;p&gt;There are (at the time of writing) 26 shapes accepted in the &lt;code&gt;shape&lt;/code&gt; argument. See this &lt;a href=&#34;https://ggplot2.tidyverse.org/reference/aes_linetype_size_shape.html&#34; target=&#34;_blank&#34;&gt;documentation&lt;/a&gt; for all available shapes.&lt;a href=&#34;#fnref3&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Correlation coefficient and correlation test in R</title>
      <link>https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/</link>
      <pubDate>Thu, 28 May 2020 00:00:00 +0000</pubDate>
      
      <guid>https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/</guid>
      <description>

&lt;div id=&#34;TOC&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#introduction&#34; id=&#34;toc-introduction&#34;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#data&#34; id=&#34;toc-data&#34;&gt;Data&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#correlation-coefficient&#34; id=&#34;toc-correlation-coefficient&#34;&gt;Correlation coefficient&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#between-two-variables&#34; id=&#34;toc-between-two-variables&#34;&gt;Between two variables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#correlation-matrix-correlations-for-all-variables&#34; id=&#34;toc-correlation-matrix-correlations-for-all-variables&#34;&gt;Correlation matrix: correlations for all variables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#interpretation-of-a-correlation-coefficient&#34; id=&#34;toc-interpretation-of-a-correlation-coefficient&#34;&gt;Interpretation of a correlation coefficient&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#visualizations&#34; id=&#34;toc-visualizations&#34;&gt;Visualizations&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#a-scatterplot-for-2-variables&#34; id=&#34;toc-a-scatterplot-for-2-variables&#34;&gt;A scatterplot for 2 variables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#scatterplots-for-several-pairs-of-variables&#34; id=&#34;toc-scatterplots-for-several-pairs-of-variables&#34;&gt;Scatterplots for several pairs of variables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#another-simple-correlation-matrix&#34; id=&#34;toc-another-simple-correlation-matrix&#34;&gt;Another simple correlation matrix&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#correlation-test&#34; id=&#34;toc-correlation-test&#34;&gt;Correlation test&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#for-2-variables&#34; id=&#34;toc-for-2-variables&#34;&gt;For 2 variables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#for-several-pairs-of-variables&#34; id=&#34;toc-for-several-pairs-of-variables&#34;&gt;For several pairs of variables&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#combination-of-correlation-coefficients-and-correlation-tests&#34; id=&#34;toc-combination-of-correlation-coefficients-and-correlation-tests&#34;&gt;Combination of correlation coefficients and correlation tests&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#correlograms&#34; id=&#34;toc-correlograms&#34;&gt;Correlograms&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#correlation-does-not-imply-causation&#34; id=&#34;toc-correlation-does-not-imply-causation&#34;&gt;Correlation does not imply causation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#conclusion&#34; id=&#34;toc-conclusion&#34;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#references&#34; id=&#34;toc-references&#34;&gt;References&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-05-28-correlation-coefficient-and-correlation-test-in-r_files/correlation-coefficient-and-correlation-test-in-r.jpeg&#34; style=&#34;width:100.0%&#34; /&gt;&lt;/p&gt;
&lt;div id=&#34;introduction&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;Correlations between variables play an important role in a &lt;a href=&#34;https://statsandr.com/tags/descriptive-statistics/&#34;&gt;descriptive analysis&lt;/a&gt;. A correlation measures the &lt;strong&gt;relationship between two variables&lt;/strong&gt;, that is, how they are linked to each other. In this sense, a correlation allows to know which variables evolve in the same direction, which ones evolve in the opposite direction, and which ones are independent.&lt;/p&gt;
&lt;p&gt;In this article, I show how to compute &lt;strong&gt;correlation coefficients&lt;/strong&gt;, how to perform &lt;strong&gt;correlation tests&lt;/strong&gt; and how to &lt;strong&gt;visualize relationships&lt;/strong&gt; between variables in R.&lt;/p&gt;
&lt;p&gt;Correlation is usually computed on two &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#quantitative&#34;&gt;quantitative&lt;/a&gt; variables, but it can also be computed on two &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#ordinal&#34;&gt;qualitative ordinal&lt;/a&gt; variables.&lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; See the &lt;a href=&#34;https://statsandr.com/blog/chi-square-test-of-independence-in-r/&#34;&gt;Chi-square test of independence&lt;/a&gt; if you need to study the relationship between two &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#nominal&#34;&gt;qualitative nominal&lt;/a&gt; variables.&lt;/p&gt;
&lt;p&gt;If you need to &lt;em&gt;quantify&lt;/em&gt; the relationship between two variables, I refer you to the article about &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/&#34;&gt;linear regression&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;data&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Data&lt;/h1&gt;
&lt;p&gt;In this article, we use the &lt;code&gt;mtcars&lt;/code&gt; dataset (loaded by default in R):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# display first 5 observations
head(mtcars, 5)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The variables &lt;code&gt;vs&lt;/code&gt; and &lt;code&gt;am&lt;/code&gt; are categorical variables, so they are removed for this article:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# remove vs and am variables
library(tidyverse)
dat &amp;lt;- mtcars %&amp;gt;%
  select(-vs, -am)

# display 5 first obs. of new dataset
head(dat, 5)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##                    mpg cyl disp  hp drat    wt  qsec gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02    3    2&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;correlation-coefficient&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Correlation coefficient&lt;/h1&gt;
&lt;div id=&#34;between-two-variables&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Between two variables&lt;/h2&gt;
&lt;p&gt;The correlation between 2 variables is found with the &lt;code&gt;cor()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;Suppose we want to compute the correlation between horsepower (&lt;code&gt;hp&lt;/code&gt;) and miles per gallon (&lt;code&gt;mpg&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Pearson correlation between 2 variables
cor(dat$hp, dat$mpg)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] -0.7761684&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that the correlation between variables &lt;em&gt;X&lt;/em&gt; and &lt;em&gt;Y&lt;/em&gt; is equal to the correlation between variables &lt;em&gt;Y&lt;/em&gt; and &lt;em&gt;X&lt;/em&gt; so the order of the variables in the &lt;code&gt;cor()&lt;/code&gt; function does not matter.&lt;/p&gt;
&lt;p&gt;The Pearson correlation is computed by default with the &lt;code&gt;cor()&lt;/code&gt; function. If you want to compute the Spearman correlation, add the argument &lt;code&gt;method = &#34;spearman&#34;&lt;/code&gt; to the &lt;code&gt;cor()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Spearman correlation between 2 variables
cor(dat$hp, dat$mpg,
  method = &amp;quot;spearman&amp;quot;
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] -0.8946646&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The most common correlation methods (Run &lt;code&gt;?cor&lt;/code&gt; for more information about the different methods available in the &lt;code&gt;cor()&lt;/code&gt; function) are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Pearson&lt;/strong&gt; correlation is often used for &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#continuous&#34;&gt;quantitative continuous&lt;/a&gt; variables that have a linear relationship&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Spearman&lt;/strong&gt; correlation (which is actually similar to Pearson but based on the ranked values for each variable rather than on the raw data) is often used to evaluate relationships involving at least one &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#ordinal&#34;&gt;qualitative ordinal&lt;/a&gt; variable or two quantitative variables if the link is partially linear&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Kendall’s tau-b&lt;/strong&gt; which is computed from the number of concordant and discordant pairs is often used for qualitative ordinal variables&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that there exists the &lt;em&gt;point-biserial correlation&lt;/em&gt; (which can be used to measure the association between a continuous variable and a nominal variable of two levels), but this correlation is not covered here.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;correlation-matrix-correlations-for-all-variables&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Correlation matrix: correlations for all variables&lt;/h2&gt;
&lt;p&gt;Suppose now that we want to compute correlations for several pairs of variables. We can easily do so for all possible pairs of variables in the dataset, again with the &lt;code&gt;cor()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# correlation for all variables
round(cor(dat),
  digits = 2 # rounded to 2 decimals
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##        mpg   cyl  disp    hp  drat    wt  qsec  gear  carb
## mpg   1.00 -0.85 -0.85 -0.78  0.68 -0.87  0.42  0.48 -0.55
## cyl  -0.85  1.00  0.90  0.83 -0.70  0.78 -0.59 -0.49  0.53
## disp -0.85  0.90  1.00  0.79 -0.71  0.89 -0.43 -0.56  0.39
## hp   -0.78  0.83  0.79  1.00 -0.45  0.66 -0.71 -0.13  0.75
## drat  0.68 -0.70 -0.71 -0.45  1.00 -0.71  0.09  0.70 -0.09
## wt   -0.87  0.78  0.89  0.66 -0.71  1.00 -0.17 -0.58  0.43
## qsec  0.42 -0.59 -0.43 -0.71  0.09 -0.17  1.00 -0.21 -0.66
## gear  0.48 -0.49 -0.56 -0.13  0.70 -0.58 -0.21  1.00  0.27
## carb -0.55  0.53  0.39  0.75 -0.09  0.43 -0.66  0.27  1.00&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This correlation matrix gives an overview of the correlations for all combinations of two variables.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;interpretation-of-a-correlation-coefficient&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Interpretation of a correlation coefficient&lt;/h2&gt;
&lt;p&gt;First of all, correlation ranges from &lt;strong&gt;-1 to 1&lt;/strong&gt;. It gives us an indication on two things:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;The direction of the relationship between the 2 variables&lt;/li&gt;
&lt;li&gt;The strength of the relationship between the 2 variables&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Regarding the direction of the relationship: On the one hand, a &lt;strong&gt;negative correlation&lt;/strong&gt; implies that the two variables under consideration vary in &lt;strong&gt;opposite directions&lt;/strong&gt;, that is, if a variable increases the other decreases and vice versa. On the other hand, a &lt;strong&gt;positive correlation&lt;/strong&gt; implies that the two variables under consideration vary in the &lt;strong&gt;same direction&lt;/strong&gt;, i.e., if a variable increases the other one increases and if one decreases the other one decreases as well.&lt;/p&gt;
&lt;p&gt;Regarding the strength of the relationship: The &lt;strong&gt;more extreme&lt;/strong&gt; the correlation coefficient (the closer to -1 or 1), the &lt;strong&gt;stronger the relationship&lt;/strong&gt;. This also means that a &lt;strong&gt;correlation close to 0&lt;/strong&gt; indicates that the two variables are &lt;strong&gt;independent&lt;/strong&gt;, that is, as one variable increases, there is no tendency in the other variable to either decrease or increase.&lt;/p&gt;
&lt;p&gt;As an illustration, the Pearson correlation between horsepower (&lt;code&gt;hp&lt;/code&gt;) and miles per gallon (&lt;code&gt;mpg&lt;/code&gt;) found above is -0.78, meaning that the 2 variables vary in opposite direction. This makes sense, cars with more horsepower tend to consume more fuel (and thus have a lower mileage per gallon). On the contrary, from the correlation matrix we see that the correlation between miles per gallon (&lt;code&gt;mpg&lt;/code&gt;) and the time to drive 1/4 of a mile (&lt;code&gt;qsec&lt;/code&gt;) is 0.42, meaning that fast cars (low &lt;code&gt;qsec&lt;/code&gt;) tend to have a worse mileage per gallon (low &lt;code&gt;mpg&lt;/code&gt;). This again makes sense as fast cars tend to consume more fuel.&lt;/p&gt;
&lt;p&gt;Note that it is a good practice to visualize the type of the relationship between the two variables &lt;em&gt;before&lt;/em&gt; interpreting the correlation coefficients. The reason is that the correlation coefficient could be biased due to an &lt;a href=&#34;https://statsandr.com/blog/outliers-detection-in-r/&#34;&gt;outlier&lt;/a&gt; or due to the type of link between the two variables.&lt;/p&gt;
&lt;p&gt;For instance, see the two Pearson correlation coefficients (denoted by &lt;code&gt;R&lt;/code&gt; in the following plots) when the outlier is excluded and included:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-05-28-correlation-coefficient-and-correlation-test-in-r_files/figure-html/unnamed-chunk-6-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The Pearson correlation coefficient changes drastically due to a single point, and thus the interpretation. It goes from a negative correlation coefficient, indicating a negative relationship between the 2 variables, to a positive coefficient, indicating a positive relationship. We would have missed this insight if we had not visualized the data in a scatterplot (see how to draw a scatterplot in this &lt;a href=&#34;https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/#visualizations&#34;&gt;section&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;A correlation coefficient may also miss a non-linear link between two variables:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-05-28-correlation-coefficient-and-correlation-test-in-r_files/figure-html/unnamed-chunk-7-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The Pearson correlation coefficient is equal to 0, indicating no relationship between the two variables, because it measures the &lt;strong&gt;linear&lt;/strong&gt; relationship and it is clear from the plot that the link is non-linear.&lt;/p&gt;
&lt;p&gt;So to recap, it is a good practice to visualize the data via a scatterplot before interpreting a correlation coefficient (it does not tell the whole story) and see how the correlation coefficient changes when using the parametric (Pearson) or nonparametric version (Spearman or Kendall’s tau-b).&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;visualizations&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Visualizations&lt;/h1&gt;
&lt;p&gt;The correlation matrix presented above is not easily interpretable, especially when the dataset is composed of many variables. In the following sections, we present some alternatives to the correlation matrix for better readability.&lt;/p&gt;
&lt;div id=&#34;a-scatterplot-for-2-variables&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;A scatterplot for 2 variables&lt;/h2&gt;
&lt;p&gt;A good way to visualize a correlation between 2 variables is to draw a scatterplot of the two variables of interest. Suppose we want to examine the relationship between horsepower (&lt;code&gt;hp&lt;/code&gt;) and miles per gallon (&lt;code&gt;mpg&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# scatterplot
library(ggplot2)

ggplot(dat) +
  aes(x = hp, y = mpg) +
  geom_point(colour = &amp;quot;#0c4c8a&amp;quot;) +
  theme_minimal()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-05-28-correlation-coefficient-and-correlation-test-in-r_files/figure-html/unnamed-chunk-8-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;If you are unfamiliar with the &lt;a href=&#34;https://statsandr.com/blog/graphics-in-r-with-ggplot2/&#34;&gt;&lt;code&gt;{ggplot2}&lt;/code&gt; package&lt;/a&gt;, you can draw the scatterplot using the &lt;code&gt;plot()&lt;/code&gt; function from R base graphics:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;plot(dat$hp, dat$mpg)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-05-28-correlation-coefficient-and-correlation-test-in-r_files/figure-html/unnamed-chunk-9-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;or use the &lt;a href=&#34;https://statsandr.com/blog/rstudio-addins-or-how-to-make-your-coding-life-easier/#esquisse&#34;&gt;esquisse addin&lt;/a&gt; to easily draw plots using the &lt;code&gt;{ggplot2}&lt;/code&gt; package.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;scatterplots-for-several-pairs-of-variables&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Scatterplots for several pairs of variables&lt;/h2&gt;
&lt;p&gt;Suppose that instead of visualizing the relationship between only 2 variables, we want to visualize the relationship for several pairs of variables. This is possible thanks to the &lt;code&gt;pair()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;For this illustration, we focus only on miles per gallon (&lt;code&gt;mpg&lt;/code&gt;), horsepower (&lt;code&gt;hp&lt;/code&gt;) and weight (&lt;code&gt;wt&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# multiple scatterplots
pairs(dat[, c(&amp;quot;mpg&amp;quot;, &amp;quot;hp&amp;quot;, &amp;quot;wt&amp;quot;)])&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-05-28-correlation-coefficient-and-correlation-test-in-r_files/figure-html/unnamed-chunk-10-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The figure indicates that weight (&lt;code&gt;wt&lt;/code&gt;) and horsepower (&lt;code&gt;hp&lt;/code&gt;) are positively correlated, whereas miles per gallon (&lt;code&gt;mpg&lt;/code&gt;) seems to be negatively correlated with horsepower (&lt;code&gt;hp&lt;/code&gt;) and weight (&lt;code&gt;wt&lt;/code&gt;).&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;another-simple-correlation-matrix&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Another simple correlation matrix&lt;/h2&gt;
&lt;p&gt;This version of the correlation matrix presents the correlation coefficients in a slightly more readable way, i.e., by coloring the coefficients based on their sign. Applied to our dataset, we have:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# improved correlation matrix
library(corrplot)

corrplot(cor(dat),
  method = &amp;quot;number&amp;quot;,
  type = &amp;quot;upper&amp;quot; # show only upper side
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-05-28-correlation-coefficient-and-correlation-test-in-r_files/figure-html/unnamed-chunk-11-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;correlation-test&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Correlation test&lt;/h1&gt;
&lt;div id=&#34;for-2-variables&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;For 2 variables&lt;/h2&gt;
&lt;p&gt;Unlike a correlation matrix which indicates the correlation coefficients between some pairs of variables in the &lt;a href=&#34;https://statsandr.com/blog/what-is-the-difference-between-population-and-sample/&#34;&gt;sample&lt;/a&gt;, a correlation test is used to test whether the correlation (denoted &lt;span class=&#34;math inline&#34;&gt;\(\rho\)&lt;/span&gt;) between 2 variables is significantly different from 0 or not in the &lt;a href=&#34;https://statsandr.com/blog/what-is-the-difference-between-population-and-sample/&#34;&gt;population&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Actually, a correlation coefficient different from 0 in the sample does not mean that the correlation is &lt;strong&gt;significantly&lt;/strong&gt; different from 0 in the population. This needs to be tested with a &lt;a href=&#34;https://statsandr.com/blog/hypothesis-test-by-hand/&#34;&gt;hypothesis test&lt;/a&gt;—and known as the correlation test.&lt;/p&gt;
&lt;p&gt;The null and alternative hypothesis for the correlation test are as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(H_0\)&lt;/span&gt;: &lt;span class=&#34;math inline&#34;&gt;\(\rho = 0\)&lt;/span&gt; (meaning that there is no linear relationship between the two variables)&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(H_1\)&lt;/span&gt;: &lt;span class=&#34;math inline&#34;&gt;\(\rho \ne 0\)&lt;/span&gt; (meaning that there is a linear relationship between the two variables)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Via this correlation test, what we are actually testing is whether:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the sample contains sufficient evidence to reject the null hypothesis and conclude that the correlation coefficient does not equal 0, so the relationship exists in the population.&lt;/li&gt;
&lt;li&gt;or on the contrary, the sample does not contain enough evidence that the correlation coefficient does not equal 0, so in this case we do not reject the null hypothesis of no relationship between the variables in the population.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that there are 2 assumptions for this test to be valid:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Independence of the data&lt;/li&gt;
&lt;li&gt;For small sample sizes (usually &lt;span class=&#34;math inline&#34;&gt;\(n &amp;lt; 30\)&lt;/span&gt;), the two variables should follow a &lt;a href=&#34;https://statsandr.com/blog/do-my-data-follow-a-normal-distribution-a-note-on-the-most-widely-used-distribution-and-how-to-test-for-normality-in-r/&#34;&gt;normal distribution&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Suppose that we want to test whether the rear axle ratio (&lt;code&gt;drat&lt;/code&gt;) is correlated with the time to drive a quarter of a mile (&lt;code&gt;qsec&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Pearson correlation test
test &amp;lt;- cor.test(dat$drat, dat$qsec)
test&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## 	Pearson&amp;#39;s product-moment correlation
## 
## data:  dat$drat and dat$qsec
## t = 0.50164, df = 30, p-value = 0.6196
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.265947  0.426340
## sample estimates:
##        cor 
## 0.09120476&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;em&gt;p&lt;/em&gt;-value of the correlation test between these 2 variables is 0.62. At the 5% significance level, we do not reject the null hypothesis of no correlation. We therefore conclude that we do not reject the hypothesis that there is no linear relationship between the 2 variables.&lt;a href=&#34;#fn2&#34; class=&#34;footnote-ref&#34; id=&#34;fnref2&#34;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This test proves that even if the correlation coefficient is different from 0 (the correlation is 0.09 in the sample), it is actually not significantly different from 0 in the population.&lt;/p&gt;
&lt;p&gt;Note that the &lt;em&gt;p&lt;/em&gt;-value of a correlation test is based on the correlation coefficient &lt;strong&gt;and&lt;/strong&gt; the sample size. The larger the sample size and the more extreme the correlation (closer to -1 or 1), the more likely the null hypothesis of no correlation will be rejected.&lt;/p&gt;
&lt;p&gt;With a small sample size, it is thus possible to obtain a &lt;em&gt;relatively&lt;/em&gt; large correlation in the sample (based on the correlation coefficient), but still find a correlation not significantly different from 0 in the population (based on the correlation test). For this reason, it is recommended to always perform a correlation test before interpreting a correlation coefficient to avoid flawed conclusions.&lt;/p&gt;
&lt;!-- A nice and easy way to report results of a correlation test in R is with the `report()` function from the `{report}` package: --&gt;
&lt;!-- As you can see, the function interprets the test (together with the correlation coefficient and the *p*-value) for you. --&gt;
&lt;!-- Note that the `report()` function can be used for other analyses. See more examples in the package&#39;s [documentation](https://easystats.github.io/report/){target=&#34;_blank&#34;}. See also more [tips and tricks in R](/blog/tips-and-tricks-in-rstudio-and-r-markdown/) if you find this one useful. --&gt;
&lt;/div&gt;
&lt;div id=&#34;for-several-pairs-of-variables&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;For several pairs of variables&lt;/h2&gt;
&lt;p&gt;Similar to the correlation matrix used to compute correlation for several pairs of variables, the &lt;code&gt;rcorr()&lt;/code&gt; function (from the &lt;code&gt;{Hmisc}&lt;/code&gt; package) allows to compute &lt;em&gt;p&lt;/em&gt;-values of the correlation test for several pairs of variables at once. Applied to our dataset, we have:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# correlation tests for whole dataset
library(Hmisc)
res &amp;lt;- rcorr(as.matrix(dat)) # rcorr() accepts matrices only

# display p-values (rounded to 3 decimals)
round(res$P, 3)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##        mpg   cyl  disp    hp  drat    wt  qsec  gear  carb
## mpg     NA 0.000 0.000 0.000 0.000 0.000 0.017 0.005 0.001
## cyl  0.000    NA 0.000 0.000 0.000 0.000 0.000 0.004 0.002
## disp 0.000 0.000    NA 0.000 0.000 0.000 0.013 0.001 0.025
## hp   0.000 0.000 0.000    NA 0.010 0.000 0.000 0.493 0.000
## drat 0.000 0.000 0.000 0.010    NA 0.000 0.620 0.000 0.621
## wt   0.000 0.000 0.000 0.000 0.000    NA 0.339 0.000 0.015
## qsec 0.017 0.000 0.013 0.000 0.620 0.339    NA 0.243 0.000
## gear 0.005 0.004 0.001 0.493 0.000 0.000 0.243    NA 0.129
## carb 0.001 0.002 0.025 0.000 0.621 0.015 0.000 0.129    NA&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Only correlations with &lt;em&gt;p&lt;/em&gt;-values smaller than the significance level (usually &lt;span class=&#34;math inline&#34;&gt;\(\alpha = 0.05\)&lt;/span&gt;) should be interpreted.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;combination-of-correlation-coefficients-and-correlation-tests&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Combination of correlation coefficients and correlation tests&lt;/h1&gt;
&lt;p&gt;Now that we covered the concepts of correlation coefficients and correlation tests, let see if we can combine the two concepts.&lt;/p&gt;
&lt;p&gt;If you need to do this for a few pairs of variables, I recommend using the &lt;code&gt;ggscatterstats()&lt;/code&gt; function from the &lt;code&gt;{ggstatsplot}&lt;/code&gt; package. Let’s see it in practice with one pair of variables—&lt;code&gt;wt&lt;/code&gt; and &lt;code&gt;mpg&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;## plot with statistical results
library(ggstatsplot)

ggscatterstats(
  data = dat,
  x = wt,
  y = mpg,
  bf.message = FALSE,
  marginal = FALSE # remove histograms
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-05-28-correlation-coefficient-and-correlation-test-in-r_files/figure-html/unnamed-chunk-15-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Based on the result of the test, we conclude that there is a negative correlation between the weight and the number of miles per gallon (&lt;span class=&#34;math inline&#34;&gt;\(r = - 0.87\)&lt;/span&gt;, &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value &amp;lt; 0.001).&lt;/p&gt;
&lt;p&gt;If you need to do it for many pairs of variables, I recommend using the the &lt;code&gt;correlation&lt;/code&gt; function from the &lt;a href=&#34;https://easystats.github.io/correlation/&#34; target=&#34;_blank&#34;&gt;easystats &lt;code&gt;{correlation}&lt;/code&gt; package&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This function allows to combine correlation coefficients and correlation tests for &lt;em&gt;several pairs&lt;/em&gt; of variables, all in a single table (thanks to &lt;a href=&#34;https://github.com/AntoineSoetewey/statsandr/issues/8&#34; target=&#34;_blank&#34;&gt;krzysiektr&lt;/a&gt; for pointing it out to me):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(correlation)

correlation::correlation(dat,
  include_factors = TRUE, method = &amp;quot;auto&amp;quot;
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # Correlation Matrix (auto-method)
## 
## Parameter1 | Parameter2 |     r |         95% CI | t(30) |         p
## --------------------------------------------------------------------
## mpg        |        cyl | -0.85 | [-0.93, -0.72] | -8.92 | &amp;lt; .001***
## mpg        |       disp | -0.85 | [-0.92, -0.71] | -8.75 | &amp;lt; .001***
## mpg        |         hp | -0.78 | [-0.89, -0.59] | -6.74 | &amp;lt; .001***
## mpg        |       drat |  0.68 | [ 0.44,  0.83] |  5.10 | &amp;lt; .001***
## mpg        |         wt | -0.87 | [-0.93, -0.74] | -9.56 | &amp;lt; .001***
## mpg        |       qsec |  0.42 | [ 0.08,  0.67] |  2.53 | 0.137    
## mpg        |       gear |  0.48 | [ 0.16,  0.71] |  3.00 | 0.065    
## mpg        |       carb | -0.55 | [-0.75, -0.25] | -3.62 | 0.016*   
## cyl        |       disp |  0.90 | [ 0.81,  0.95] | 11.45 | &amp;lt; .001***
## cyl        |         hp |  0.83 | [ 0.68,  0.92] |  8.23 | &amp;lt; .001***
## cyl        |       drat | -0.70 | [-0.84, -0.46] | -5.37 | &amp;lt; .001***
## cyl        |         wt |  0.78 | [ 0.60,  0.89] |  6.88 | &amp;lt; .001***
## cyl        |       qsec | -0.59 | [-0.78, -0.31] | -4.02 | 0.007**  
## cyl        |       gear | -0.49 | [-0.72, -0.17] | -3.10 | 0.054    
## cyl        |       carb |  0.53 | [ 0.22,  0.74] |  3.40 | 0.027*   
## disp       |         hp |  0.79 | [ 0.61,  0.89] |  7.08 | &amp;lt; .001***
## disp       |       drat | -0.71 | [-0.85, -0.48] | -5.53 | &amp;lt; .001***
## disp       |         wt |  0.89 | [ 0.78,  0.94] | 10.58 | &amp;lt; .001***
## disp       |       qsec | -0.43 | [-0.68, -0.10] | -2.64 | 0.131    
## disp       |       gear | -0.56 | [-0.76, -0.26] | -3.66 | 0.015*   
## disp       |       carb |  0.39 | [ 0.05,  0.65] |  2.35 | 0.177    
## hp         |       drat | -0.45 | [-0.69, -0.12] | -2.75 | 0.110    
## hp         |         wt |  0.66 | [ 0.40,  0.82] |  4.80 | &amp;lt; .001***
## hp         |       qsec | -0.71 | [-0.85, -0.48] | -5.49 | &amp;lt; .001***
## hp         |       gear | -0.13 | [-0.45,  0.23] | -0.69 | &amp;gt; .999   
## hp         |       carb |  0.75 | [ 0.54,  0.87] |  6.21 | &amp;lt; .001***
## drat       |         wt | -0.71 | [-0.85, -0.48] | -5.56 | &amp;lt; .001***
## drat       |       qsec |  0.09 | [-0.27,  0.43] |  0.50 | &amp;gt; .999   
## drat       |       gear |  0.70 | [ 0.46,  0.84] |  5.36 | &amp;lt; .001***
## drat       |       carb | -0.09 | [-0.43,  0.27] | -0.50 | &amp;gt; .999   
## wt         |       qsec | -0.17 | [-0.49,  0.19] | -0.97 | &amp;gt; .999   
## wt         |       gear | -0.58 | [-0.77, -0.29] | -3.93 | 0.008**  
## wt         |       carb |  0.43 | [ 0.09,  0.68] |  2.59 | 0.132    
## qsec       |       gear | -0.21 | [-0.52,  0.15] | -1.19 | &amp;gt; .999   
## qsec       |       carb | -0.66 | [-0.82, -0.40] | -4.76 | &amp;lt; .001***
## gear       |       carb |  0.27 | [-0.08,  0.57] |  1.56 | 0.774    
## 
## p-value adjustment method: Holm (1979)
## Observations: 32&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see, it gives, among other useful information, the correlation coefficients (column &lt;code&gt;r&lt;/code&gt;) and the result of the correlation test (column &lt;code&gt;95% CI&lt;/code&gt; for the confidence interval or &lt;code&gt;p&lt;/code&gt; for the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value) for all pairs of variables.&lt;/p&gt;
&lt;div id=&#34;correlograms&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Correlograms&lt;/h2&gt;
&lt;p&gt;The table above is very useful and informative, but let see if it is possible to combine the concepts of correlation coefficients and correlations test in one single visualization. A visualization that would be easy to read and interpret.&lt;/p&gt;
&lt;p&gt;Ideally, we would like to have a concise overview of correlations between all possible pairs of variables present in a dataset, with a clear distinction for correlations that are significantly different from 0.&lt;/p&gt;
&lt;p&gt;The figure below, known as a &lt;a href=&#34;https://statsandr.com/blog/correlogram-in-r-how-to-highlight-the-most-correlated-variables-in-a-dataset/#correlogram&#34;&gt;correlogram&lt;/a&gt; and adapted from the &lt;code&gt;corrplot()&lt;/code&gt; function, does precisely this:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# do not edit
corrplot2 &amp;lt;- function(data,
                      method = &amp;quot;pearson&amp;quot;,
                      sig.level = 0.05,
                      order = &amp;quot;original&amp;quot;,
                      diag = FALSE,
                      type = &amp;quot;upper&amp;quot;,
                      tl.srt = 90,
                      number.font = 1,
                      number.cex = 1,
                      mar = c(0, 0, 0, 0)) {
  library(corrplot)
  data_incomplete &amp;lt;- data
  data &amp;lt;- data[complete.cases(data), ]
  mat &amp;lt;- cor(data, method = method)
  cor.mtest &amp;lt;- function(mat, method) {
    mat &amp;lt;- as.matrix(mat)
    n &amp;lt;- ncol(mat)
    p.mat &amp;lt;- matrix(NA, n, n)
    diag(p.mat) &amp;lt;- 0
    for (i in 1:(n - 1)) {
      for (j in (i + 1):n) {
        tmp &amp;lt;- cor.test(mat[, i], mat[, j], method = method)
        p.mat[i, j] &amp;lt;- p.mat[j, i] &amp;lt;- tmp$p.value
      }
    }
    colnames(p.mat) &amp;lt;- rownames(p.mat) &amp;lt;- colnames(mat)
    p.mat
  }
  p.mat &amp;lt;- cor.mtest(data, method = method)
  col &amp;lt;- colorRampPalette(c(&amp;quot;#BB4444&amp;quot;, &amp;quot;#EE9988&amp;quot;, &amp;quot;#FFFFFF&amp;quot;, &amp;quot;#77AADD&amp;quot;, &amp;quot;#4477AA&amp;quot;))
  corrplot(mat,
    method = &amp;quot;color&amp;quot;, col = col(200), number.font = number.font,
    mar = mar, number.cex = number.cex,
    type = type, order = order,
    addCoef.col = &amp;quot;black&amp;quot;, # add correlation coefficient
    tl.col = &amp;quot;black&amp;quot;, tl.srt = tl.srt, # rotation of text labels
    # combine with significance level
    p.mat = p.mat, sig.level = sig.level, insig = &amp;quot;blank&amp;quot;,
    # hide correlation coefficients on the diagonal
    diag = diag
  )
}

# edit from here
corrplot2(
  data = dat,
  method = &amp;quot;pearson&amp;quot;,
  sig.level = 0.05,
  order = &amp;quot;original&amp;quot;,
  diag = FALSE,
  type = &amp;quot;upper&amp;quot;,
  tl.srt = 75
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-05-28-correlation-coefficient-and-correlation-test-in-r_files/figure-html/unnamed-chunk-17-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The correlogram shows correlation coefficients for all pairs of variables (with more intense colors for more extreme correlations), and correlations not significantly different from 0 are represented by a white box.&lt;/p&gt;
&lt;p&gt;To learn more about this plot and the code used, I invite you to read the article entitled “&lt;a href=&#34;https://statsandr.com/blog/correlogram-in-r-how-to-highlight-the-most-correlated-variables-in-a-dataset/&#34;&gt;Correlogram in R: how to highlight the most correlated variables in a dataset&lt;/a&gt;”.&lt;/p&gt;
&lt;p&gt;For those of you who are still not completely satisfied, I recently found two alternatives—one with the &lt;code&gt;ggpairs()&lt;/code&gt; function from the &lt;code&gt;{GGally}&lt;/code&gt; package and one with the &lt;code&gt;ggcormat()&lt;/code&gt; function from the &lt;code&gt;{ggstatsplot}&lt;/code&gt; package.&lt;/p&gt;
&lt;p&gt;The two functions are illustrated with the variables &lt;code&gt;mpg&lt;/code&gt;, &lt;code&gt;hp&lt;/code&gt; and &lt;code&gt;wt&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(GGally)

ggpairs(dat[, c(&amp;quot;mpg&amp;quot;, &amp;quot;hp&amp;quot;, &amp;quot;wt&amp;quot;)])&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-05-28-correlation-coefficient-and-correlation-test-in-r_files/figure-html/unnamed-chunk-18-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The plot above combines correlation coefficients, correlation tests (via the asterisks next to the coefficients&lt;a href=&#34;#fn3&#34; class=&#34;footnote-ref&#34; id=&#34;fnref3&#34;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;) and scatterplots for all possible pairs of variables present in a dataset.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(ggstatsplot)

ggcorrmat(
  data = dat[, c(&amp;quot;mpg&amp;quot;, &amp;quot;hp&amp;quot;, &amp;quot;wt&amp;quot;)],
  type = &amp;quot;parametric&amp;quot;, # parametric for Pearson, nonparametric for Spearman&amp;#39;s correlation
  colors = c(&amp;quot;darkred&amp;quot;, &amp;quot;white&amp;quot;, &amp;quot;steelblue&amp;quot;) # change default colors
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/2020-05-28-correlation-coefficient-and-correlation-test-in-r_files/figure-html/unnamed-chunk-19-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The plot above also shows the correlation coefficients and if any, the non-significant correlations (by default at the 5% significance level with the Holm adjustment method) are shown by a big cross on the correlation coefficients.&lt;/p&gt;
&lt;p&gt;The advantage of these two alternatives compared to the first one is that it is directly available within a package, so you do not need to run the code of the function first in order to draw the correlogram.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;correlation-does-not-imply-causation&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Correlation does not imply causation&lt;/h1&gt;
&lt;p&gt;I am pretty sure you have already heard the statement “Correlation does not imply causation” in statistics. An article about correlation would not be complete without discussing about causation.&lt;/p&gt;
&lt;p&gt;A non-zero correlation between two variables does not necessarily mean that there is a cause and effect relationship between these two variables!&lt;/p&gt;
&lt;p&gt;Indeed, a significant correlation between two variables means that changes in one variable are associated (positively or negatively) with changes in the other variable. Nonetheless, a significant correlation &lt;em&gt;does not&lt;/em&gt; indicate that variations in one variable &lt;em&gt;cause&lt;/em&gt; the variations in the other variable.&lt;/p&gt;
&lt;p&gt;A non-zero correlation between X and Y can appear in several cases:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;X causes Y&lt;/li&gt;
&lt;li&gt;Y causes X&lt;/li&gt;
&lt;li&gt;a third variable causes X and Y&lt;/li&gt;
&lt;li&gt;a combination of these three reasons&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Sometimes it is quite clear that there is a causal relationship between two variables. Take for example the correlation between the price of a consumer product such as milk and its consumption. It is quite obvious that there is a causal link between the two: if the price of milk increases, it is expected that its consumption will decrease.&lt;/p&gt;
&lt;p&gt;However, this causal link is not always present even if the correlation is significant. &lt;span class=&#34;citation&#34;&gt;Maurage et al. (&lt;a href=&#34;#ref-maurage2013does&#34;&gt;2013&lt;/a&gt;)&lt;/span&gt; showed that, although there is a positive and significant correlation between chocolate consumption and the number of Nobel laureates, this correlation comes from the fact that a third variable, Gross Domestic Product (GDP), causes chocolate consumption and the number of Nobel laureates. They found that countries with higher GDP tend to have a higher level of chocolate consumption and scientific research (leading to more Nobel laureates).&lt;/p&gt;
&lt;p&gt;This example shows that one must be very cautious when interpreting correlations and avoid over-interpreting a correlation as a causal relationship.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;conclusion&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;Thanks for reading.&lt;/p&gt;
&lt;p&gt;I hope this article helped you to compute correlation coefficients and perform correlation tests in R. If you would like to learn how to compute the coefficients by hand, see this &lt;a href=&#34;https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/&#34;&gt;step-by-step tutorial&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;(Note that this article is available for download on my &lt;a href=&#34;https://statsandr.gumroad.com/&#34;&gt;Gumroad page&lt;/a&gt;.)&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;references&#34; class=&#34;section level1 unnumbered&#34;&gt;
&lt;h1&gt;References&lt;/h1&gt;
&lt;div id=&#34;refs&#34; class=&#34;references csl-bib-body hanging-indent&#34;&gt;
&lt;div id=&#34;ref-maurage2013does&#34; class=&#34;csl-entry&#34;&gt;
Maurage, Pierre, Alexandre Heeren, and Mauro Pesenti. 2013. &lt;span&gt;“Does Chocolate Consumption Really Boost Nobel Award Chances? The Peril of over-Interpreting Correlations in Health Studies.”&lt;/span&gt; &lt;em&gt;The Journal of Nutrition&lt;/em&gt; 143 (6): 931–33.
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;footnotes footnotes-end-of-document&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;It is true that there is the point-biserial correlation which can be used with a nominal variable (consisting of two factors). Nonetheless, this type of correlation is much less known and usually not covered in introductory statistics classes; with one continuous and one nominal variable, it is much more frequent to learn about the &lt;a href=&#34;https://statsandr.com/blog/student-s-t-test-in-r-and-by-hand-how-to-compare-two-groups-under-different-scenarios/&#34;&gt;Student’s t-test&lt;/a&gt; (for a nominal variable with 2 groups) or &lt;a href=&#34;https://statsandr.com/blog/anova-in-r/&#34;&gt;ANOVA&lt;/a&gt; (for a nominal variable with 3 or more groups). More information about choosing the most appropriate measure of association depending on the type of variable can be found in this &lt;a href=&#34;https://journals.sagepub.com/doi/pdf/10.1177/8756479308317006&#34; target=&#34;_blank&#34;&gt;article&lt;/a&gt;.&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn2&#34;&gt;&lt;p&gt;It is important to remember that we tested for a &lt;em&gt;linear&lt;/em&gt; relationship between the two variables since we used the Pearson’s correlation. It may be the case that there is a relationship between the two variables in the population, but this relation may not be linear.&lt;a href=&#34;#fnref2&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn3&#34;&gt;&lt;p&gt;One asterisk means that the coefficient is significant at the 5% level, 2 is at the 1% significance level, and 3 is at the 0.1% significance level. This is usually the case in R; the more asterisks, the more it is significant.&lt;a href=&#34;#fnref3&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Correlogram in R: how to highlight the most correlated variables in a dataset</title>
      <link>https://statsandr.com/blog/correlogram-in-r-how-to-highlight-the-most-correlated-variables-in-a-dataset/</link>
      <pubDate>Sat, 22 Feb 2020 00:00:00 +0000</pubDate>
      
      <guid>https://statsandr.com/blog/correlogram-in-r-how-to-highlight-the-most-correlated-variables-in-a-dataset/</guid>
      <description>

&lt;div id=&#34;TOC&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#introduction&#34; id=&#34;toc-introduction&#34;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#correlation-matrix&#34; id=&#34;toc-correlation-matrix&#34;&gt;Correlation matrix&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#correlogram&#34; id=&#34;toc-correlogram&#34;&gt;Correlogram&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#correlation-test&#34; id=&#34;toc-correlation-test&#34;&gt;Correlation test&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#code&#34; id=&#34;toc-code&#34;&gt;Code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#ggstatsplot-package&#34; id=&#34;toc-ggstatsplot-package&#34;&gt;&lt;code&gt;{ggstatsplot}&lt;/code&gt; package&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#lares-package&#34; id=&#34;toc-lares-package&#34;&gt;&lt;code&gt;{lares}&lt;/code&gt; package&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#all-possible-correlations&#34; id=&#34;toc-all-possible-correlations&#34;&gt;All possible correlations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#correlation-of-one-variable-against-all-others&#34; id=&#34;toc-correlation-of-one-variable-against-all-others&#34;&gt;Correlation of one variable against all others&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#conclusion&#34; id=&#34;toc-conclusion&#34;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#references&#34; id=&#34;toc-references&#34;&gt;References&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/correlogram-in-r-how-to-highlight-correlations-between-variables_files/correlogram-in-r-how-to-highlight-correlations-between-variables.jpeg&#34; style=&#34;width:100.0%&#34; /&gt;&lt;/p&gt;
&lt;div id=&#34;introduction&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;&lt;a href=&#34;https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/&#34;&gt;Correlation&lt;/a&gt;, often computed as part of &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/&#34;&gt;descriptive statistics&lt;/a&gt;, is a statistical tool used to study the relationship between two variables, that is, whether and how strongly couples of variables are associated.&lt;/p&gt;
&lt;p&gt;Correlations are measured between 2 variables at a time. Therefore, for datasets with many variables, computing correlations can become quite cumbersome and time consuming.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;correlation-matrix&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Correlation matrix&lt;/h1&gt;
&lt;p&gt;A solution to this problem is to compute correlations and display them in a correlation matrix, which shows correlation coefficients for all possible combinations of two variables in the dataset.&lt;/p&gt;
&lt;p&gt;For example, below is the correlation matrix for the dataset &lt;code&gt;mtcars&lt;/code&gt; (which, as described by the help documentation of R, comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles).&lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; For this article, we include only the &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#continuous&#34;&gt;continuous&lt;/a&gt; variables.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat &amp;lt;- mtcars[, c(1, 3:7)]
round(cor(dat), 2)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##        mpg  disp    hp  drat    wt  qsec
## mpg   1.00 -0.85 -0.78  0.68 -0.87  0.42
## disp -0.85  1.00  0.79 -0.71  0.89 -0.43
## hp   -0.78  0.79  1.00 -0.45  0.66 -0.71
## drat  0.68 -0.71 -0.45  1.00 -0.71  0.09
## wt   -0.87  0.89  0.66 -0.71  1.00 -0.17
## qsec  0.42 -0.43 -0.71  0.09 -0.17  1.00&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Even after rounding the correlation coefficients to 2 digits, you will conceive that this correlation matrix is not easily and quickly interpretable.&lt;/p&gt;
&lt;p&gt;If you are using &lt;a href=&#34;https://statsandr.com/blog/getting-started-in-r-markdown/&#34;&gt;R Markdown&lt;/a&gt;, you can use the &lt;code&gt;pander()&lt;/code&gt; function from the &lt;code&gt;{pander}&lt;/code&gt; package to make it slightly more readable, but still, we must admit that this table is not optimal when it comes to visualizing correlations between several variables of a dataset, especially for large datasets.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;correlogram&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Correlogram&lt;/h1&gt;
&lt;p&gt;To tackle this issue and make it much more insightful, let’s transform the correlation matrix into a correlation plot. A correlation plot (also referred as a correlogram or corrgram in &lt;span class=&#34;citation&#34;&gt;Friendly (&lt;a href=&#34;#ref-friendly2002corrgrams&#34;&gt;2002&lt;/a&gt;)&lt;/span&gt;) allows to highlight the variables that are most (positively and negatively) correlated. Below an example with the same dataset presented above:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/correlogram-in-r-how-to-highlight-correlations-between-variables_files/figure-html/unnamed-chunk-2-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The correlogram represents the correlations for all pairs of variables. Positive correlations are displayed in blue and negative correlations in red. The intensity of the color is proportional to the correlation coefficient so the stronger the correlation (i.e., the closer to -1 or 1), the darker the boxes. The color legend on the right hand side of the correlogram shows the correlation coefficients and the corresponding colors.&lt;/p&gt;
&lt;p&gt;As a reminder, a negative correlation implies that the two variables under consideration vary in opposite directions, that is, if one variable increases the other decreases and vice versa. A positive correlation implies that the two variables under consideration vary in the same direction, that is, if one variable increases the other increases and if one variable decreases the other decreases as well. Furthermore, the stronger the correlation, the stronger the association between the two variables.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;correlation-test&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Correlation test&lt;/h1&gt;
&lt;p&gt;Finally, a white box in the correlogram indicates that the correlation is not significantly different from 0 at the specified significance level (in this example, at &lt;span class=&#34;math inline&#34;&gt;\(\alpha = 5\)&lt;/span&gt;%) for the couple of variables. A correlation not significantly different from 0 means that there is &lt;strong&gt;no linear&lt;/strong&gt; relationship between the two variables considered in the &lt;a href=&#34;https://statsandr.com/blog/what-is-the-difference-between-population-and-sample/&#34;&gt;population&lt;/a&gt; (there could be another kind of association, but not linear).&lt;/p&gt;
&lt;p&gt;To determine whether a specific correlation coefficient is significantly different from 0, a &lt;a href=&#34;https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/&#34;&gt;correlation test&lt;/a&gt; has been performed. Remind that the null and alternative hypotheses of this test are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(H_0\)&lt;/span&gt;: &lt;span class=&#34;math inline&#34;&gt;\(\rho = 0\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(H_1\)&lt;/span&gt;: &lt;span class=&#34;math inline&#34;&gt;\(\rho \ne 0\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;where &lt;span class=&#34;math inline&#34;&gt;\(\rho\)&lt;/span&gt; is denotes the correlation. The correlation test is based on two factors: the number of observations and the correlation coefficient. The more observations and the stronger the correlation between 2 variables, the more likely it is to reject the null hypothesis of no correlation between these 2 variables.&lt;/p&gt;
&lt;p&gt;In the context of our example, the correlogram above shows that the variables &lt;code&gt;wt&lt;/code&gt; (weight) and &lt;code&gt;hp&lt;/code&gt; (horsepower) are positively correlated, while the variables &lt;code&gt;mpg&lt;/code&gt; (miles per gallon) and &lt;code&gt;wt&lt;/code&gt; (weight) are negatively correlated (both correlations make sense if we think about it). Furthermore, the variables &lt;code&gt;wt&lt;/code&gt; and &lt;code&gt;qsec&lt;/code&gt; are not correlated (indicated by a white box). Even if the correlation coefficient is -0.17 between the 2 variables, the correlation test has shown that we cannot reject the hypothesis of no correlation in the population. This is the reason the box for these two variable is white.&lt;/p&gt;
&lt;p&gt;Although this correlogram presents exactly the same information than the correlation matrix, the correlogram presents a visual representation of the correlation matrix, allowing to quickly scan through it to see which variables are correlated and which are not.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;code&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Code&lt;/h1&gt;
&lt;p&gt;For those interested to draw this correlogram with their own data, here is the code of the function I adapted based on the &lt;code&gt;corrplot()&lt;/code&gt; function from the &lt;code&gt;{corrplot}&lt;/code&gt; package (thanks again to all contributors of this package):&lt;/p&gt;
&lt;script src=&#34;https://gist.github.com/AntoineSoetewey/1fc0fe939336a8b8085e1872e045b48f.js&#34;&gt;&lt;/script&gt;
&lt;p&gt;The main arguments in the &lt;code&gt;corrplot2()&lt;/code&gt; function are the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;data&lt;/code&gt;: name of your dataset&lt;/li&gt;
&lt;li&gt;&lt;code&gt;method&lt;/code&gt;: the correlation method to be computed, one of “pearson” (default), “kendall”, or “spearman”. As a rule of thumb, if your dataset contains &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#continuous&#34;&gt;quantitative continuous&lt;/a&gt; variables that have a linear relationship, you can keep the Pearson method. If you have &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#ordinal&#34;&gt;qualitative ordinal&lt;/a&gt; variables or quantitative variables with a partially linear link, the Spearman method is more appropriate&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sig.level&lt;/code&gt;: the significance level for the correlation test, default is 0.05&lt;/li&gt;
&lt;li&gt;&lt;code&gt;order&lt;/code&gt;: order of the variables, one of “original” (default), “AOE” (angular order of the eigenvectors), “FPC” (first principal component order), “hclust” (hierarchical clustering order), “alphabet” (alphabetical order)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;diag&lt;/code&gt;: display the correlation coefficients on the diagonal? The default is &lt;code&gt;FALSE&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;type&lt;/code&gt;: display the entire correlation matrix or simply the upper/lower part, one of “upper” (default), “lower”, “full”&lt;/li&gt;
&lt;li&gt;&lt;code&gt;tl.srt&lt;/code&gt;: rotation of the variable labels&lt;/li&gt;
&lt;li&gt;(note that missing values in the dataset are automatically removed)&lt;/li&gt;
&lt;/ul&gt;
&lt;!-- You can also play with the arguments of the `corrplot2` function and see the results thanks to this [R Shiny app](https://antoinesoetewey.shinyapps.io/correlogram/){target=&#34;_blank&#34;}. --&gt;
&lt;/div&gt;
&lt;div id=&#34;ggstatsplot-package&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;&lt;code&gt;{ggstatsplot}&lt;/code&gt; package&lt;/h1&gt;
&lt;p&gt;An alternative to the correlogram presented above is possible with the &lt;code&gt;ggcorrmat()&lt;/code&gt; function from the &lt;code&gt;{ggstatsplot}&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# load package
library(ggstatsplot)

# correlogram
ggstatsplot::ggcorrmat(
  data = dat,
  type = &amp;quot;parametric&amp;quot;, # parametric for Pearson, nonparametric for Spearman&amp;#39;s correlation
  colors = c(&amp;quot;darkred&amp;quot;, &amp;quot;white&amp;quot;, &amp;quot;steelblue&amp;quot;) # change default colors
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/correlogram-in-r-how-to-highlight-correlations-between-variables_files/figure-html/unnamed-chunk-3-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;In this correlogram, the non-significant correlations (by default at the 5% significance level with the Holm adjustment method) are shown by a cross on the correlation coefficients.&lt;/p&gt;
&lt;p&gt;The advantage of this alternative compared to the previous one is that it is directly available within a package, so you do not need to run the code of the function first in order to draw the correlogram.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;lares-package&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;&lt;code&gt;{lares}&lt;/code&gt; package&lt;/h1&gt;
&lt;p&gt;Thanks to this article, I discovered the &lt;code&gt;{lares}&lt;/code&gt; package which has really nice features regarding plotting correlations. Another advantage of this package is that it can be used to compute correlations with numerical, logical, categorical and date variables.&lt;/p&gt;
&lt;p&gt;See more information about the package in this &lt;a href=&#34;https://datascienceplus.com/find-insights-with-ranked-cross-correlations/&#34; target=&#34;_blank&#34;&gt;article&lt;/a&gt;.&lt;/p&gt;
&lt;div id=&#34;all-possible-correlations&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;All possible correlations&lt;/h2&gt;
&lt;p&gt;Use the &lt;code&gt;corr_cross()&lt;/code&gt; function if you want to compute all correlations and return the highest and significant ones in a plot:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# devtools::install_github(&amp;quot;laresbernardo/lares&amp;quot;)
library(lares)

corr_cross(dat, # name of dataset
  max_pvalue = 0.05, # display only significant correlations (at 5% level)
  top = 10 # display top 10 couples of variables (by correlation coefficient)
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/correlogram-in-r-how-to-highlight-correlations-between-variables_files/figure-html/unnamed-chunk-4-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Negative correlations are represented in red and positive correlations in blue.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;correlation-of-one-variable-against-all-others&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Correlation of one variable against all others&lt;/h2&gt;
&lt;p&gt;Use the &lt;code&gt;corr_var()&lt;/code&gt; function if you want to focus on the correlation of one variable against all others, and return the highest ones in a plot:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;corr_var(dat, # name of dataset
  mpg, # name of variable to focus on
  top = 5 # display top 5 correlations
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/correlogram-in-r-how-to-highlight-correlations-between-variables_files/figure-html/unnamed-chunk-5-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;conclusion&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;Thanks for reading.&lt;/p&gt;
&lt;p&gt;I hope this article will help you to visualize correlations between variables in a dataset and to make correlation matrices more insightful and more appealing.&lt;/p&gt;
&lt;p&gt;If you want to learn more about this topic, see:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;how to &lt;a href=&#34;https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/&#34;&gt;compute correlation coefficients and perform correlation tests in R&lt;/a&gt;, or&lt;/li&gt;
&lt;li&gt;how to &lt;a href=&#34;https://statsandr.com/blog/pearson-spearman-kendall-correlation-by-hand/&#34;&gt;compute correlation coefficients by hand&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;references&#34; class=&#34;section level1 unnumbered&#34;&gt;
&lt;h1&gt;References&lt;/h1&gt;
&lt;div id=&#34;refs&#34; class=&#34;references csl-bib-body hanging-indent&#34;&gt;
&lt;div id=&#34;ref-friendly2002corrgrams&#34; class=&#34;csl-entry&#34;&gt;
Friendly, Michael. 2002. &lt;span&gt;“Corrgrams: Exploratory Displays for Correlation Matrices.”&lt;/span&gt; &lt;em&gt;The American Statistician&lt;/em&gt; 56 (4): 316–24.
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;footnotes footnotes-end-of-document&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;The dataset &lt;code&gt;mtcars&lt;/code&gt; is preloaded in R by default, so there is no need to import it into R. Check the article “&lt;a href=&#34;https://statsandr.com/blog/how-to-import-an-excel-file-in-rstudio/&#34;&gt;How to import an Excel file in R&lt;/a&gt;” if you need help in importing your own dataset.&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Descriptive statistics in R</title>
      <link>https://statsandr.com/blog/descriptive-statistics-in-r/</link>
      <pubDate>Wed, 22 Jan 2020 00:00:00 +0000</pubDate>
      
      <guid>https://statsandr.com/blog/descriptive-statistics-in-r/</guid>
      <description>

&lt;div id=&#34;TOC&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#introduction&#34; id=&#34;toc-introduction&#34;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#data&#34; id=&#34;toc-data&#34;&gt;Data&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#minimum-and-maximum&#34; id=&#34;toc-minimum-and-maximum&#34;&gt;Minimum and maximum&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#range&#34; id=&#34;toc-range&#34;&gt;Range&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#mean&#34; id=&#34;toc-mean&#34;&gt;Mean&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#median&#34; id=&#34;toc-median&#34;&gt;Median&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#first-and-third-quartile&#34; id=&#34;toc-first-and-third-quartile&#34;&gt;First and third quartile&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#other-quantiles&#34; id=&#34;toc-other-quantiles&#34;&gt;Other quantiles&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#interquartile-range&#34; id=&#34;toc-interquartile-range&#34;&gt;Interquartile range&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#standard-deviation-and-variance&#34; id=&#34;toc-standard-deviation-and-variance&#34;&gt;Standard deviation and variance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#summary&#34; id=&#34;toc-summary&#34;&gt;Summary&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#coefficient-of-variation&#34; id=&#34;toc-coefficient-of-variation&#34;&gt;Coefficient of variation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#mode&#34; id=&#34;toc-mode&#34;&gt;Mode&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#correlation&#34; id=&#34;toc-correlation&#34;&gt;Correlation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#contingency-table&#34; id=&#34;toc-contingency-table&#34;&gt;Contingency table&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#mosaic-plot&#34; id=&#34;toc-mosaic-plot&#34;&gt;Mosaic plot&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#barplot&#34; id=&#34;toc-barplot&#34;&gt;Barplot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#histogram&#34; id=&#34;toc-histogram&#34;&gt;Histogram&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#boxplot&#34; id=&#34;toc-boxplot&#34;&gt;Boxplot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#dotplot&#34; id=&#34;toc-dotplot&#34;&gt;Dotplot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#scatterplot&#34; id=&#34;toc-scatterplot&#34;&gt;Scatterplot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#line-plot&#34; id=&#34;toc-line-plot&#34;&gt;Line plot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#qq-plot&#34; id=&#34;toc-qq-plot&#34;&gt;QQ-plot&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#for-a-single-variable&#34; id=&#34;toc-for-a-single-variable&#34;&gt;For a single variable&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#by-groups&#34; id=&#34;toc-by-groups&#34;&gt;By groups&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#density-plot&#34; id=&#34;toc-density-plot&#34;&gt;Density plot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#correlation-plot&#34; id=&#34;toc-correlation-plot&#34;&gt;Correlation plot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#advanced-descriptive-statistics&#34; id=&#34;toc-advanced-descriptive-statistics&#34;&gt;Advanced descriptive statistics&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#summarytools-package&#34; id=&#34;toc-summarytools-package&#34;&gt;&lt;code&gt;{summarytools}&lt;/code&gt; package&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#frequency-tables-with-freq&#34; id=&#34;toc-frequency-tables-with-freq&#34;&gt;Frequency tables with &lt;code&gt;freq()&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#cross-tabulations-with-ctable&#34; id=&#34;toc-cross-tabulations-with-ctable&#34;&gt;Cross-tabulations with &lt;code&gt;ctable()&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#descriptive-statistics-with-descr&#34; id=&#34;toc-descriptive-statistics-with-descr&#34;&gt;Descriptive statistics with &lt;code&gt;descr()&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#data-frame-summaries-with-dfsummary&#34; id=&#34;toc-data-frame-summaries-with-dfsummary&#34;&gt;Data frame summaries with &lt;code&gt;dfSummary()&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#describeby-from-the-psych-package&#34; id=&#34;toc-describeby-from-the-psych-package&#34;&gt;&lt;code&gt;describeBy()&lt;/code&gt; from the &lt;code&gt;{psych}&lt;/code&gt; package&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#aggregate-function&#34; id=&#34;toc-aggregate-function&#34;&gt;&lt;code&gt;aggregate()&lt;/code&gt; function&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#summaryby-from-doby&#34; id=&#34;toc-summaryby-from-doby&#34;&gt;&lt;code&gt;summaryBy()&lt;/code&gt; from &lt;code&gt;{doBy}&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#group_by-and-summarise-from-dplyr&#34; id=&#34;toc-group_by-and-summarise-from-dplyr&#34;&gt;&lt;code&gt;group_by()&lt;/code&gt; and &lt;code&gt;summarise()&lt;/code&gt; from &lt;code&gt;{dplyr}&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#conclusion&#34; id=&#34;toc-conclusion&#34;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/descriptive-statistics-in-r.jpeg&#34; style=&#34;width:100.0%&#34; /&gt;&lt;/p&gt;
&lt;div id=&#34;introduction&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;This article explains how to compute the main descriptive statistics in R and how to present them graphically. To learn more about the reasoning behind each descriptive statistics, how to compute them by hand and how to interpret them, read the article “&lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand/&#34;&gt;Descriptive statistics by hand&lt;/a&gt;”.&lt;/p&gt;
&lt;p&gt;To briefly recap what have been said in that article, descriptive statistics (in the broad sense of the term) is a branch of statistics aiming at summarizing, describing and presenting a series of values or a dataset. Descriptive statistics is often the first step and an important part in any statistical analysis. It allows to check the quality of the data and it helps to “understand” the data by having a clear overview of it. If well presented, descriptive statistics is already a good starting point for further analyses. There exists many measures to summarize a dataset. They are divided into two types:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;location measures and&lt;/li&gt;
&lt;li&gt;dispersion measures&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Location measures give an understanding about the central tendency of the data, whereas dispersion measures give an understanding about the spread of the data. In this article, we focus only on the implementation in R of the most common descriptive statistics and their visualizations (when deemed appropriate). See online or in the &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand/&#34;&gt;above mentioned article&lt;/a&gt; for more information about the purpose and usage of each measure.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;data&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Data&lt;/h1&gt;
&lt;p&gt;We use the dataset &lt;code&gt;iris&lt;/code&gt; throughout the article. This dataset is imported by default in R, you only need to load it by running &lt;code&gt;iris&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat &amp;lt;- iris # load the iris dataset and renamed it dat&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Below a preview of this dataset and its structure:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;head(dat) # first 6 observations&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;str(dat) # structure of dataset&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## &amp;#39;data.frame&amp;#39;:	150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels &amp;quot;setosa&amp;quot;,&amp;quot;versicolor&amp;quot;,..: 1 1 1 1 1 1 1 1 1 1 ...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The dataset contains 150 observations and 5 variables, representing the length and width of the sepal and petal and the species of 150 flowers. Length and width of the sepal and petal are numeric variables and the species is a factor with 3 levels (indicated by &lt;code&gt;num&lt;/code&gt; and &lt;code&gt;Factor w/ 3 levels&lt;/code&gt; after the name of the variables). See the &lt;a href=&#34;https://statsandr.com/blog/data-types-in-r/&#34;&gt;different variables types in R&lt;/a&gt; if you need a refresh.&lt;/p&gt;
&lt;p&gt;Regarding plots, we present the default graphs and the graphs from the well-known &lt;code&gt;{ggplot2}&lt;/code&gt; package. Graphs from the &lt;code&gt;{ggplot2}&lt;/code&gt; package usually have a better look but it requires more advanced coding skills (see the article “&lt;a href=&#34;https://statsandr.com/blog/graphics-in-r-with-ggplot2/&#34;&gt;Graphics in R with ggplot2&lt;/a&gt;” to learn more). If you need to publish or share your graphs, I suggest using &lt;code&gt;{ggplot2}&lt;/code&gt; if you can, otherwise the default graphics will do the job.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Tip:&lt;/em&gt; I recently discovered the ggplot2 builder from the &lt;code&gt;{esquisse}&lt;/code&gt; addins. See how you can easily &lt;a href=&#34;https://statsandr.com/blog/rstudio-addins-or-how-to-make-your-coding-life-easier/&#34;&gt;draw graphs from the &lt;code&gt;{ggplot2}&lt;/code&gt; package&lt;/a&gt; without having to code it yourself.&lt;/p&gt;
&lt;p&gt;All plots displayed in this article can be customized. For instance, it is possible to edit the title, x and y-axis labels, color, etc. However, customizing plots is beyond the scope of this article so all plots are presented without any customization. Interested readers will find numerous resources online.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;minimum-and-maximum&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Minimum and maximum&lt;/h1&gt;
&lt;p&gt;Minimum and maximum can be found thanks to the &lt;code&gt;min()&lt;/code&gt; and &lt;code&gt;max()&lt;/code&gt; functions:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;min(dat$Sepal.Length)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 4.3&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;max(dat$Sepal.Length)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 7.9&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Alternatively the &lt;code&gt;range()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;rng &amp;lt;- range(dat$Sepal.Length)
rng&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 4.3 7.9&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;gives you the minimum and maximum directly. Note that the output of the &lt;code&gt;range()&lt;/code&gt; function is actually an object containing the minimum and maximum (in that order). This means you can actually access the minimum with:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;rng[1] # rng = name of the object specified above&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 4.3&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and the maximum with:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;rng[2]&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 7.9&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This reminds us that, in R, there are often several ways to arrive at the same result. The method that uses the shortest piece of code is usually preferred as a shorter piece of code is less prone to coding errors and more readable.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;range&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Range&lt;/h1&gt;
&lt;p&gt;The range can then be easily computed, as you have guessed, by subtracting the minimum from the maximum:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;max(dat$Sepal.Length) - min(dat$Sepal.Length)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 3.6&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To my knowledge, there is no default function to compute the range. However, if you are familiar with writing functions in R
&lt;!-- (if not, see this article on [how to write a function in R](/blog/xxx/)) --&gt;
, you can create your own function to compute the range:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;range2 &amp;lt;- function(x) {
  range &amp;lt;- max(x) - min(x)
  return(range)
}

range2(dat$Sepal.Length)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 3.6&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which is equivalent than &lt;span class=&#34;math inline&#34;&gt;\(max - min\)&lt;/span&gt; presented above.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;mean&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Mean&lt;/h1&gt;
&lt;p&gt;The mean can be computed with the &lt;code&gt;mean()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;mean(dat$Sepal.Length)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 5.843333&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Tips:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;if there is at least one missing value in your dataset, use &lt;code&gt;mean(dat$Sepal.Length, na.rm = TRUE)&lt;/code&gt; to compute the mean with the NA excluded. This argument can be used for most functions presented in this article, not only the mean&lt;/li&gt;
&lt;li&gt;for a truncated mean, use &lt;code&gt;mean(dat$Sepal.Length, trim = 0.10)&lt;/code&gt; and change the &lt;code&gt;trim&lt;/code&gt; argument to your needs&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;median&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Median&lt;/h1&gt;
&lt;p&gt;The median can be computed thanks to the &lt;code&gt;median()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;median(dat$Sepal.Length)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 5.8&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or with the &lt;code&gt;quantile()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;quantile(dat$Sepal.Length, 0.5)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 50% 
## 5.8&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;since the quantile of order 0.5 (&lt;span class=&#34;math inline&#34;&gt;\(q_{0.5}\)&lt;/span&gt;) corresponds to the median.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;first-and-third-quartile&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;First and third quartile&lt;/h1&gt;
&lt;p&gt;As the median, the first and third quartiles can be computed thanks to the &lt;code&gt;quantile()&lt;/code&gt; function and by setting the second argument to 0.25 or 0.75:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;quantile(dat$Sepal.Length, 0.25) # first quartile&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 25% 
## 5.1&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;quantile(dat$Sepal.Length, 0.75) # third quartile&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 75% 
## 6.4&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You may have seen that the results above are slightly different than the results you would have found if you compute the first and third quartiles &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand/&#34;&gt;by hand&lt;/a&gt;. It is normal, there are many methods to compute them (R actually has 7 methods to compute the quantiles!). However, the methods presented here and in the article “&lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand/&#34;&gt;descriptive statistics by hand&lt;/a&gt;” are the easiest and most “standard” ones. Furthermore, results do not dramatically change between the two methods.&lt;/p&gt;
&lt;div id=&#34;other-quantiles&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Other quantiles&lt;/h2&gt;
&lt;p&gt;As you have guessed, any quantile can also be computed with the &lt;code&gt;quantile()&lt;/code&gt; function. For instance, the &lt;span class=&#34;math inline&#34;&gt;\(4^{th}\)&lt;/span&gt; decile or the &lt;span class=&#34;math inline&#34;&gt;\(98^{th}\)&lt;/span&gt; percentile:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;quantile(dat$Sepal.Length, 0.4) # 4th decile&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 40% 
## 5.6&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;quantile(dat$Sepal.Length, 0.98) # 98th percentile&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 98% 
## 7.7&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;interquartile-range&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Interquartile range&lt;/h1&gt;
&lt;p&gt;The interquartile range (i.e., the difference between the first and third quartile) can be computed with the &lt;code&gt;IQR()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;IQR(dat$Sepal.Length)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 1.3&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or alternatively with the &lt;code&gt;quantile()&lt;/code&gt; function again:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;quantile(dat$Sepal.Length, 0.75) - quantile(dat$Sepal.Length, 0.25)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 75% 
## 1.3&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As mentioned earlier, when possible it is usually recommended to use the shortest piece of code to arrive at the result. For this reason, the &lt;code&gt;IQR()&lt;/code&gt; function is preferred to compute the interquartile range.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;standard-deviation-and-variance&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Standard deviation and variance&lt;/h1&gt;
&lt;p&gt;The standard deviation and the variance is computed with the &lt;code&gt;sd()&lt;/code&gt; and &lt;code&gt;var()&lt;/code&gt; functions:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;sd(dat$Sepal.Length) # standard deviation&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.8280661&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;var(dat$Sepal.Length) # variance&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.6856935&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Remember from the article &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand/&#34;&gt;descriptive statistics by hand&lt;/a&gt; that the standard deviation and the variance are different whether we compute it for a sample or a population (see the &lt;a href=&#34;https://statsandr.com/blog/what-is-the-difference-between-population-and-sample/&#34;&gt;difference between sample and population&lt;/a&gt;). In R, the standard deviation and the variance are computed as if the data represent a sample (so the denominator is &lt;span class=&#34;math inline&#34;&gt;\(n - 1\)&lt;/span&gt;, where &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; is the number of observations). To my knowledge, there is no function by default in R that computes the standard deviation or variance for a population.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Tip:&lt;/em&gt; to compute the standard deviation (or variance) of multiple variables at the same time, use &lt;code&gt;lapply()&lt;/code&gt; with the appropriate statistics as second argument:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;lapply(dat[, 1:4], sd)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## $Sepal.Length
## [1] 0.8280661
## 
## $Sepal.Width
## [1] 0.4358663
## 
## $Petal.Length
## [1] 1.765298
## 
## $Petal.Width
## [1] 0.7622377&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The command &lt;code&gt;dat[, 1:4]&lt;/code&gt; selects the variables 1 to 4 as the fifth variable is a &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#qualitative&#34;&gt;qualitative variable&lt;/a&gt; and the standard deviation cannot be computed on such type of variable. See a recap of the different &lt;a href=&#34;https://statsandr.com/blog/data-types-in-r/&#34;&gt;data types in R&lt;/a&gt; if needed.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;summary&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Summary&lt;/h1&gt;
&lt;p&gt;You can compute the minimum, &lt;span class=&#34;math inline&#34;&gt;\(1^{st}\)&lt;/span&gt; quartile, median, mean, &lt;span class=&#34;math inline&#34;&gt;\(3^{rd}\)&lt;/span&gt; quartile and the maximum for all numeric variables of a dataset at once using &lt;code&gt;summary()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;summary(dat)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Tip:&lt;/em&gt; if you need these descriptive statistics by group use the &lt;code&gt;by()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;by(dat, dat$Species, summary)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## dat$Species: setosa
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.300   Min.   :1.000   Min.   :0.100  
##  1st Qu.:4.800   1st Qu.:3.200   1st Qu.:1.400   1st Qu.:0.200  
##  Median :5.000   Median :3.400   Median :1.500   Median :0.200  
##  Mean   :5.006   Mean   :3.428   Mean   :1.462   Mean   :0.246  
##  3rd Qu.:5.200   3rd Qu.:3.675   3rd Qu.:1.575   3rd Qu.:0.300  
##  Max.   :5.800   Max.   :4.400   Max.   :1.900   Max.   :0.600  
##        Species  
##  setosa    :50  
##  versicolor: 0  
##  virginica : 0  
##                 
##                 
##                 
## ------------------------------------------------------------ 
## dat$Species: versicolor
##   Sepal.Length    Sepal.Width     Petal.Length   Petal.Width          Species  
##  Min.   :4.900   Min.   :2.000   Min.   :3.00   Min.   :1.000   setosa    : 0  
##  1st Qu.:5.600   1st Qu.:2.525   1st Qu.:4.00   1st Qu.:1.200   versicolor:50  
##  Median :5.900   Median :2.800   Median :4.35   Median :1.300   virginica : 0  
##  Mean   :5.936   Mean   :2.770   Mean   :4.26   Mean   :1.326                  
##  3rd Qu.:6.300   3rd Qu.:3.000   3rd Qu.:4.60   3rd Qu.:1.500                  
##  Max.   :7.000   Max.   :3.400   Max.   :5.10   Max.   :1.800                  
## ------------------------------------------------------------ 
## dat$Species: virginica
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.900   Min.   :2.200   Min.   :4.500   Min.   :1.400  
##  1st Qu.:6.225   1st Qu.:2.800   1st Qu.:5.100   1st Qu.:1.800  
##  Median :6.500   Median :3.000   Median :5.550   Median :2.000  
##  Mean   :6.588   Mean   :2.974   Mean   :5.552   Mean   :2.026  
##  3rd Qu.:6.900   3rd Qu.:3.175   3rd Qu.:5.875   3rd Qu.:2.300  
##  Max.   :7.900   Max.   :3.800   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    : 0  
##  versicolor: 0  
##  virginica :50  
##                 
##                 
## &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where the arguments are the name of the dataset, the grouping variable and the summary function. Follow this order, or specify the name of the arguments if you do not follow this order.&lt;/p&gt;
&lt;p&gt;If you need more descriptive statistics, use &lt;code&gt;stat.desc()&lt;/code&gt; from the package &lt;code&gt;{pastecs}&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(pastecs)
stat.desc(dat)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##              Sepal.Length  Sepal.Width Petal.Length  Petal.Width Species
## nbr.val      150.00000000 150.00000000  150.0000000 150.00000000      NA
## nbr.null       0.00000000   0.00000000    0.0000000   0.00000000      NA
## nbr.na         0.00000000   0.00000000    0.0000000   0.00000000      NA
## min            4.30000000   2.00000000    1.0000000   0.10000000      NA
## max            7.90000000   4.40000000    6.9000000   2.50000000      NA
## range          3.60000000   2.40000000    5.9000000   2.40000000      NA
## sum          876.50000000 458.60000000  563.7000000 179.90000000      NA
## median         5.80000000   3.00000000    4.3500000   1.30000000      NA
## mean           5.84333333   3.05733333    3.7580000   1.19933333      NA
## SE.mean        0.06761132   0.03558833    0.1441360   0.06223645      NA
## CI.mean.0.95   0.13360085   0.07032302    0.2848146   0.12298004      NA
## var            0.68569351   0.18997942    3.1162779   0.58100626      NA
## std.dev        0.82806613   0.43586628    1.7652982   0.76223767      NA
## coef.var       0.14171126   0.14256420    0.4697441   0.63555114      NA&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can have even more statistics (i.e., skewness, kurtosis and &lt;a href=&#34;https://statsandr.com/blog/do-my-data-follow-a-normal-distribution-a-note-on-the-most-widely-used-distribution-and-how-to-test-for-normality-in-r/&#34;&gt;normality test&lt;/a&gt;) by adding the argument &lt;code&gt;norm = TRUE&lt;/code&gt; in the previous function. Note that the variable &lt;code&gt;Species&lt;/code&gt; is not numeric, so descriptive statistics cannot be computed for this variable and NA are displayed.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;coefficient-of-variation&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Coefficient of variation&lt;/h1&gt;
&lt;p&gt;The coefficient of variation can be found with &lt;code&gt;stat.desc()&lt;/code&gt; (see the line &lt;code&gt;coef.var&lt;/code&gt; in the table above) or by computing manually (remember that the coefficient of variation is the standard deviation divided by the mean):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;sd(dat$Sepal.Length) / mean(dat$Sepal.Length)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.1417113&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;mode&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Mode&lt;/h1&gt;
&lt;p&gt;To my knowledge there is no function to find the mode of a variable. However, we can easily find it thanks to the functions &lt;code&gt;table()&lt;/code&gt; and &lt;code&gt;sort()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;tab &amp;lt;- table(dat$Sepal.Length) # number of occurrences for each unique value
sort(tab, decreasing = TRUE) # sort highest to lowest&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
##   5 5.1 6.3 5.7 6.7 5.5 5.8 6.4 4.9 5.4 5.6   6 6.1 4.8 6.5 4.6 5.2 6.2 6.9 7.7 
##  10   9   9   8   8   7   7   7   6   6   6   6   6   5   5   4   4   4   4   4 
## 4.4 5.9 6.8 7.2 4.7 6.6 4.3 4.5 5.3   7 7.1 7.3 7.4 7.6 7.9 
##   3   3   3   3   2   2   1   1   1   1   1   1   1   1   1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;table()&lt;/code&gt; gives the number of occurrences for each unique value, then &lt;code&gt;sort()&lt;/code&gt; with the argument &lt;code&gt;decreasing = TRUE&lt;/code&gt; displays the number of occurrences from highest to lowest. The mode of the variable &lt;code&gt;Sepal.Length&lt;/code&gt; is thus 5. This code to find the mode can also be applied to qualitative variables such as &lt;code&gt;Species&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;sort(table(dat$Species), decreasing = TRUE)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
##     setosa versicolor  virginica 
##         50         50         50&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;summary(dat$Species)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##     setosa versicolor  virginica 
##         50         50         50&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;correlation&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Correlation&lt;/h1&gt;
&lt;p&gt;Another descriptive statistics is the correlation coefficient.&lt;/p&gt;
&lt;p&gt;The correlation measures the &lt;em&gt;linear&lt;/em&gt; relationship between two variables, and it can be computed with the &lt;code&gt;cor()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;cor(dat$Sepal.Length, dat$Sepal.Width)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] -0.1175698&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Computing correlation in R and interpreting the results deserve a detailed explanation, so I wrote an article covering &lt;a href=&#34;https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/&#34;&gt;correlation and correlation test&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;contingency-table&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Contingency table&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;table()&lt;/code&gt; introduced above can also be used on two qualitative variables to create a contingency table. The dataset &lt;code&gt;iris&lt;/code&gt; has only one qualitative variable so we create a new qualitative variable just for this example. We create the variable &lt;code&gt;size&lt;/code&gt; which corresponds to &lt;code&gt;small&lt;/code&gt; if the length of the petal is smaller than the median of all flowers, &lt;code&gt;big&lt;/code&gt; otherwise:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat$size &amp;lt;- ifelse(dat$Sepal.Length &amp;lt; median(dat$Sepal.Length),
  &amp;quot;small&amp;quot;, &amp;quot;big&amp;quot;
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here is a recap of the occurrences by size:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;table(dat$size)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
##   big small 
##    77    73&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We now create a contingency table of the two variables &lt;code&gt;Species&lt;/code&gt; and &lt;code&gt;size&lt;/code&gt; with the &lt;code&gt;table()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;table(dat$Species, dat$size)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##             
##              big small
##   setosa       1    49
##   versicolor  29    21
##   virginica   47     3&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or with the &lt;code&gt;xtabs()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;xtabs(~ dat$Species + dat$size)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##             dat$size
## dat$Species  big small
##   setosa       1    49
##   versicolor  29    21
##   virginica   47     3&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The contingency table gives the number of cases in each subgroup. For instance, there is only one big setosa flower, while there are 49 small setosa flowers in the dataset.&lt;/p&gt;
&lt;p&gt;To go further, we can see from the table that setosa flowers seem to be smaller in size than virginica flowers. In order to check whether size is significantly associated with species, we could perform a Chi-square test of independence since both variables are categorical variables. See how to do this test &lt;a href=&#34;https://statsandr.com/blog/chi-square-test-of-independence-by-hand/&#34;&gt;by hand&lt;/a&gt; and &lt;a href=&#34;https://statsandr.com/blog/chi-square-test-of-independence-in-r/&#34;&gt;in R&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Note that &lt;code&gt;Species&lt;/code&gt; are in rows and &lt;code&gt;size&lt;/code&gt; in column because we specified &lt;code&gt;Species&lt;/code&gt; and then &lt;code&gt;size&lt;/code&gt; in &lt;code&gt;table()&lt;/code&gt;. Change the order if you want to switch the two variables.&lt;/p&gt;
&lt;p&gt;Instead of having the frequencies (i.e.. the number of cases) you can also have the relative frequencies (i.e., proportions) in each subgroup by adding the &lt;code&gt;table()&lt;/code&gt; function inside the &lt;code&gt;prop.table()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;prop.table(table(dat$Species, dat$size))&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##             
##                      big       small
##   setosa     0.006666667 0.326666667
##   versicolor 0.193333333 0.140000000
##   virginica  0.313333333 0.020000000&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that you can also compute the percentages by row or by column by adding a second argument to the &lt;code&gt;prop.table()&lt;/code&gt; function: &lt;code&gt;1&lt;/code&gt; for row, or &lt;code&gt;2&lt;/code&gt; for column:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# percentages by row:
round(prop.table(table(dat$Species, dat$size), 1), 2) # round to 2 digits with round()&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##             
##               big small
##   setosa     0.02  0.98
##   versicolor 0.58  0.42
##   virginica  0.94  0.06&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# percentages by column:
round(prop.table(table(dat$Species, dat$size), 2), 2) # round to 2 digits with round()&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##             
##               big small
##   setosa     0.01  0.67
##   versicolor 0.38  0.29
##   virginica  0.61  0.04&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;See the section on &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/#cross-tabulations-with-ctable&#34;&gt;advanced descriptive statistics&lt;/a&gt; for more advanced contingency tables.&lt;/p&gt;
&lt;div id=&#34;mosaic-plot&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Mosaic plot&lt;/h2&gt;
&lt;p&gt;A mosaic plot allows to visualize a contingency table of two qualitative variables:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;mosaicplot(table(dat$Species, dat$size),
  color = TRUE,
  xlab = &amp;quot;Species&amp;quot;, # label for x-axis
  ylab = &amp;quot;Size&amp;quot; # label for y-axis
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-32-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The mosaic plot shows that, for our sample, the proportion of big and small flowers is clearly different between the three species. In particular, the virginica species is the biggest, and the setosa species is the smallest of the three species (in terms of sepal length since the variable &lt;code&gt;size&lt;/code&gt; is based on the variable &lt;code&gt;Sepal.Length&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;For your information, a mosaic plot can also be done via the &lt;code&gt;mosaic()&lt;/code&gt; function from the &lt;code&gt;{vcd}&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(vcd)

mosaic(~ Species + size,
  data = dat,
  direction = c(&amp;quot;v&amp;quot;, &amp;quot;h&amp;quot;)
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-33-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;barplot&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Barplot&lt;/h1&gt;
&lt;p&gt;Barplots can only be done on qualitative variables (see the difference with a quantitative variable &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/&#34;&gt;here&lt;/a&gt;). A barplot is a tool to visualize the distribution of a qualitative variable. We draw a barplot of the qualitative variable &lt;code&gt;size&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;barplot(table(dat$size)) # table() is mandatory&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-34-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;You can also draw a barplot of the relative frequencies instead of the frequencies by adding &lt;code&gt;prop.table()&lt;/code&gt; as we did earlier:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;barplot(prop.table(table(dat$size)))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-35-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;{ggplot2}&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(ggplot2) # needed each time you open RStudio
# The package ggplot2 must be installed first

ggplot(dat) +
  aes(x = size) +
  geom_bar()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-36-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;histogram&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Histogram&lt;/h1&gt;
&lt;p&gt;A histogram gives an idea about the distribution of a quantitative variable. The idea is to break the range of values into intervals and count how many observations fall into each interval. Histograms are a bit similar to barplots, but histograms are used for quantitative variables whereas barplots are used for qualitative variables. To draw a histogram in R, use &lt;code&gt;hist()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;hist(dat$Sepal.Length)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-37-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Add the arguments &lt;code&gt;breaks =&lt;/code&gt; inside the &lt;code&gt;hist()&lt;/code&gt; function if you want to change the number of bins. A rule of thumb (known as the square-root rule) is that the number of bins should be the rounded value of the square root of the number of observations. The dataset includes 150 observations so in this case the number of bins can be set to 12.&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;{ggplot2}&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = Sepal.Length) +
  geom_histogram()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-38-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;By default, the number of bins is 30. You can change this value with &lt;code&gt;geom_histogram(bins = 12)&lt;/code&gt; for instance.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;boxplot&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Boxplot&lt;/h1&gt;
&lt;p&gt;Boxplots are really useful in descriptive statistics and are often underused (mostly because it is not well understood by the public). A boxplot graphically represents the distribution of a quantitative variable by visually displaying five common location summary (minimum, median, first/third quartiles and maximum) and any observation that was classified as a suspected &lt;a href=&#34;https://statsandr.com/blog/outliers-detection-in-r/&#34;&gt;outlier&lt;/a&gt; using the interquartile range (IQR) criterion.&lt;/p&gt;
&lt;p&gt;The IQR criterion means that all observations above &lt;span class=&#34;math inline&#34;&gt;\(q_{0.75} + 1.5 \cdot IQR\)&lt;/span&gt; or below &lt;span class=&#34;math inline&#34;&gt;\(q_{0.25} - 1.5 \cdot IQR\)&lt;/span&gt; (where &lt;span class=&#34;math inline&#34;&gt;\(q_{0.25}\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(q_{0.75}\)&lt;/span&gt; correspond to first and third quartile respectively) are considered as potential outliers by R. The minimum and maximum in the boxplot are represented without these suspected outliers.&lt;/p&gt;
&lt;p&gt;Seeing all these information on the same plot help to have a good first overview of the dispersion and the location of the data. Before drawing a boxplot of our data, see below a graph explaining the information present on a boxplot:&lt;/p&gt;
&lt;div class=&#34;float&#34;&gt;
&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/how-to-interpret-boxplot.png&#34; style=&#34;width:100.0%&#34; alt=&#34;How to interpret a boxplot? Source: LFSAB1105&#34; /&gt;
&lt;div class=&#34;figcaption&#34;&gt;How to interpret a boxplot? Source: LFSAB1105&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now an example with our dataset:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;boxplot(dat$Sepal.Length)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-39-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Boxplots are even more informative when presented side-by-side for comparing and contrasting distributions from two or more groups. For instance, we compare the length of the sepal across the different species:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;boxplot(dat$Sepal.Length ~ dat$Species)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-40-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;{ggplot2}&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = Species, y = Sepal.Length) +
  geom_boxplot()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-41-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;dotplot&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Dotplot&lt;/h1&gt;
&lt;p&gt;A dotplot is more or less similar than a boxplot, except that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;observations are represented as points&lt;/li&gt;
&lt;li&gt;it does not easily tell us about the median, first and third quartiles.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(lattice)

dotplot(dat$Sepal.Length ~ dat$Species)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-42-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;{ggplot2}&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = Species, y = Sepal.Length) +
  geom_dotplot(binaxis = &amp;quot;y&amp;quot;, stackdir = &amp;quot;center&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-43-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The advantage of using &lt;code&gt;{ggplot2}&lt;/code&gt; over &lt;code&gt;{lattice}&lt;/code&gt; for this plot is that we can easily see the mode.&lt;/p&gt;
&lt;p&gt;Note that a dotplot is particularly useful when there are a limited number of observations, whereas a boxplot is more appropriate with large datasets.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;scatterplot&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Scatterplot&lt;/h1&gt;
&lt;p&gt;Scatterplots allow to check whether there is a potential link between two quantitative variables. For this reason, scatterplots are often used to visualize a potential &lt;a href=&#34;https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/&#34;&gt;correlation&lt;/a&gt; between two variables. For instance, when drawing a scatterplot of the length of the sepal and the length of the petal:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;plot(dat$Sepal.Length, dat$Petal.Length)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-44-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;There seems to be a positive association between the two variables.&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;{ggplot2}&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = Sepal.Length, y = Petal.Length) +
  geom_point()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-45-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Like boxplots, scatterplots are even more informative when differentiating the points according to a factor, in this case the species:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = Sepal.Length, y = Petal.Length, colour = Species) +
  geom_point() +
  scale_color_hue()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-46-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;line-plot&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Line plot&lt;/h1&gt;
&lt;p&gt;Line plots, particularly useful in time series or finance, can be created by adding the &lt;code&gt;type = &#34;l&#34;&lt;/code&gt; argument in the &lt;code&gt;plot()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;plot(dat$Sepal.Length,
  type = &amp;quot;l&amp;quot;
) # &amp;quot;l&amp;quot; for line&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-47-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;qq-plot&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;QQ-plot&lt;/h1&gt;
&lt;div id=&#34;for-a-single-variable&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;For a single variable&lt;/h2&gt;
&lt;p&gt;In order to check the normality assumption of a variable (normality means that the data follow a normal distribution, also known as a Gaussian distribution), we usually use histograms and/or QQ-plots.&lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; See an article discussing about the &lt;a href=&#34;https://statsandr.com/blog/do-my-data-follow-a-normal-distribution-a-note-on-the-most-widely-used-distribution-and-how-to-test-for-normality-in-r/&#34;&gt;normal distribution and how to evaluate the normality assumption in R&lt;/a&gt; if you need a refresh on that subject.&lt;/p&gt;
&lt;p&gt;Histograms have been presented earlier, so here is how to draw a QQ-plot:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Draw points on the qq-plot:
qqnorm(dat$Sepal.Length)
# Draw the reference line:
qqline(dat$Sepal.Length)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-48-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Or a QQ-plot with confidence bands with the &lt;code&gt;qqPlot()&lt;/code&gt; function from the &lt;code&gt;{car}&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(car) # package must be installed first
qqPlot(dat$Sepal.Length)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-49-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;## [1] 132 118&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If points are close to the reference line (sometimes referred as Henry’s line) and within the confidence bands, the normality assumption can be considered as met. The bigger the deviation between the points and the reference line and the more they lie outside the confidence bands, the less likely that the normality condition is met. The variable &lt;code&gt;Sepal.Length&lt;/code&gt; does not seem to follow a normal distribution because several points lie outside the confidence bands. When facing a non-normal distribution, the first step is usually to apply the logarithm transformation on the data and recheck to see whether the log-transformed data are normally distributed. Applying the logarithm transformation can be done with the &lt;code&gt;log()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;{ggpubr}&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(ggpubr)
ggqqplot(dat$Sepal.Length)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-50-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;by-groups&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;By groups&lt;/h2&gt;
&lt;p&gt;For some &lt;a href=&#34;https://statsandr.com/blog/what-statistical-test-should-i-do/&#34;&gt;statistical tests&lt;/a&gt;, the normality assumption is required in all groups. One solution is to draw a QQ-plot for each group by manually splitting the dataset into different groups and then draw a QQ-plot for each subset of the data (with the methods shown above). Another (easier) solution is to draw a QQ-plot for each group automatically with the argument &lt;code&gt;groups =&lt;/code&gt; in the function &lt;code&gt;qqPlot()&lt;/code&gt; from the &lt;code&gt;{car}&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;qqPlot(dat$Sepal.Length, groups = dat$size)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-51-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;{ggplot2}&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;qplot(
  sample = Sepal.Length, data = dat,
  col = size, shape = size
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-52-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;It is also possible to differentiate groups by only shape or color. For this, remove one of the argument &lt;code&gt;col&lt;/code&gt; or &lt;code&gt;shape&lt;/code&gt; in the &lt;code&gt;qplot()&lt;/code&gt; function above.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;density-plot&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Density plot&lt;/h1&gt;
&lt;p&gt;Density plot is a smoothed version of the histogram and is used in the same concept, that is, to represent the distribution of a numeric variable. The functions &lt;code&gt;plot()&lt;/code&gt; and &lt;code&gt;density()&lt;/code&gt; are used together to draw a density plot:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;plot(density(dat$Sepal.Length))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-53-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;{ggplot2}&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = Sepal.Length) +
  geom_density()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-in-r_files/figure-html/unnamed-chunk-54-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;correlation-plot&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Correlation plot&lt;/h1&gt;
&lt;p&gt;The last type of descriptive plot is a correlation plot, also called a correlogram. This type of graph is more complex than the ones presented above, so it is detailed in a separate article. See &lt;a href=&#34;https://statsandr.com/blog/correlogram-in-r-how-to-highlight-the-most-correlated-variables-in-a-dataset/&#34;&gt;how to draw a correlogram to highlight the most correlated variables in a dataset&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;advanced-descriptive-statistics&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Advanced descriptive statistics&lt;/h1&gt;
&lt;p&gt;We covered the main functions to compute the most common and basic descriptive statistics. There are, however, many more functions and packages to perform more advanced descriptive statistics in R. In this section, I present some of them with applications to our dataset.&lt;/p&gt;
&lt;div id=&#34;summarytools-package&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;&lt;code&gt;{summarytools}&lt;/code&gt; package&lt;/h2&gt;
&lt;p&gt;One package for descriptive statistics I often use for my projects in R is the &lt;a href=&#34;https://cran.r-project.org/web/packages/summarytools/index.html&#34; target=&#34;_blank&#34;&gt;&lt;code&gt;{summarytools}&lt;/code&gt;&lt;/a&gt; package. The package is centered around 4 functions:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;code&gt;freq()&lt;/code&gt; for frequencies tables&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ctable()&lt;/code&gt; for cross-tabulations&lt;/li&gt;
&lt;li&gt;&lt;code&gt;descr()&lt;/code&gt; for descriptive statistics&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dfSummary()&lt;/code&gt; for dataframe summaries&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A combination of these 4 functions is usually more than enough for most descriptive analyses. Moreover, the package has been built with &lt;a href=&#34;https://statsandr.com/blog/getting-started-in-r-markdown/&#34;&gt;R Markdown&lt;/a&gt; in mind, meaning that outputs render well in HTML reports. And for non-English speakers, built-in translations exist for French, Portuguese, Spanish, Russian and Turkish.&lt;/p&gt;
&lt;p&gt;I illustrate each of the 4 functions in the following sections. Outputs that follow display much better in R Markdown reports, but in this article I limit myself to the raw outputs as the goal is to show how the functions work, not how to make them render well. See the setup settings in the &lt;a href=&#34;https://cran.r-project.org/web/packages/summarytools/vignettes/introduction.html&#34; target=&#34;_blank&#34;&gt;vignette&lt;/a&gt; of the package if you want to print the outputs in a nice way in R Markdown.&lt;a href=&#34;#fn2&#34; class=&#34;footnote-ref&#34; id=&#34;fnref2&#34;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div id=&#34;frequency-tables-with-freq&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Frequency tables with &lt;code&gt;freq()&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;freq()&lt;/code&gt; function produces frequency tables with frequencies, proportions, as well as missing data information.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(summarytools)
freq(dat$Species)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Frequencies  
## dat$Species  
## Type: Factor  
## 
##                    Freq   % Valid   % Valid Cum.   % Total   % Total Cum.
## ---------------- ------ --------- -------------- --------- --------------
##           setosa     50     33.33          33.33     33.33          33.33
##       versicolor     50     33.33          66.67     33.33          66.67
##        virginica     50     33.33         100.00     33.33         100.00
##             &amp;lt;NA&amp;gt;      0                               0.00         100.00
##            Total    150    100.00         100.00    100.00         100.00&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you do not need information about missing values, add the &lt;code&gt;report.nas = FALSE&lt;/code&gt; argument:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;freq(dat$Species,
  report.nas = FALSE # remove NA information
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Frequencies  
## dat$Species  
## Type: Factor  
## 
##                    Freq        %   % Cum.
## ---------------- ------ -------- --------
##           setosa     50    33.33    33.33
##       versicolor     50    33.33    66.67
##        virginica     50    33.33   100.00
##            Total    150   100.00   100.00&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And for a minimalist output with only counts and proportions:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;freq(dat$Species,
  report.nas = FALSE, # remove NA information
  totals = FALSE, # remove totals
  cumul = FALSE, # remove cumuls
  headings = FALSE # remove headings
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
##                    Freq       %
## ---------------- ------ -------
##           setosa     50   33.33
##       versicolor     50   33.33
##        virginica     50   33.33&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;cross-tabulations-with-ctable&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Cross-tabulations with &lt;code&gt;ctable()&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;ctable()&lt;/code&gt; function produces cross-tabulations (also known as contingency tables) for pairs of categorical variables. Using the two categorical variables in our dataset:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ctable(
  x = dat$Species,
  y = dat$size
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Cross-Tabulation, Row Proportions  
## Species * size  
## Data Frame: dat  
## 
## ------------ ------ ------------ ------------ --------------
##                size          big        small          Total
##      Species                                                
##       setosa           1 ( 2.0%)   49 (98.0%)    50 (100.0%)
##   versicolor          29 (58.0%)   21 (42.0%)    50 (100.0%)
##    virginica          47 (94.0%)    3 ( 6.0%)    50 (100.0%)
##        Total          77 (51.3%)   73 (48.7%)   150 (100.0%)
## ------------ ------ ------------ ------------ --------------&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Row proportions are shown by default. To display column or total proportions, add the &lt;code&gt;prop = &#34;c&#34;&lt;/code&gt; or &lt;code&gt;prop = &#34;t&#34;&lt;/code&gt; arguments, respectively:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ctable(
  x = dat$Species,
  y = dat$size,
  prop = &amp;quot;t&amp;quot; # total proportions
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Cross-Tabulation, Total Proportions  
## Species * size  
## Data Frame: dat  
## 
## ------------ ------ ------------ ------------ --------------
##                size          big        small          Total
##      Species                                                
##       setosa           1 ( 0.7%)   49 (32.7%)    50 ( 33.3%)
##   versicolor          29 (19.3%)   21 (14.0%)    50 ( 33.3%)
##    virginica          47 (31.3%)    3 ( 2.0%)    50 ( 33.3%)
##        Total          77 (51.3%)   73 (48.7%)   150 (100.0%)
## ------------ ------ ------------ ------------ --------------&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To remove proportions altogether, add the argument &lt;code&gt;prop = &#34;n&#34;&lt;/code&gt;. Furthermore, to display only the bare minimum, add the &lt;code&gt;totals = FALSE&lt;/code&gt; and &lt;code&gt;headings = FALSE&lt;/code&gt; arguments:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ctable(
  x = dat$Species,
  y = dat$size,
  prop = &amp;quot;n&amp;quot;, # remove proportions
  totals = FALSE, # remove totals
  headings = FALSE # remove headings
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## ------------ ------ ----- -------
##                size   big   small
##      Species                     
##       setosa            1      49
##   versicolor           29      21
##    virginica           47       3
## ------------ ------ ----- -------&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is equivalent than &lt;code&gt;table(dat$Species, dat$size)&lt;/code&gt; and &lt;code&gt;xtabs(~ dat$Species + dat$size)&lt;/code&gt; performed in the section on &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/#contingency-table&#34;&gt;contingency tables&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To display results of the &lt;a href=&#34;https://statsandr.com/blog/chi-square-test-of-independence-in-r/&#34;&gt;Chi-square test of independence&lt;/a&gt;, add the &lt;code&gt;chisq = TRUE&lt;/code&gt; argument:&lt;a href=&#34;#fn3&#34; class=&#34;footnote-ref&#34; id=&#34;fnref3&#34;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ctable(
  x = dat$Species,
  y = dat$size,
  chisq = TRUE, # display results of Chi-square test of independence
  headings = FALSE # remove headings
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## ------------ ------ ------------ ------------ --------------
##                size          big        small          Total
##      Species                                                
##       setosa           1 ( 2.0%)   49 (98.0%)    50 (100.0%)
##   versicolor          29 (58.0%)   21 (42.0%)    50 (100.0%)
##    virginica          47 (94.0%)    3 ( 6.0%)    50 (100.0%)
##        Total          77 (51.3%)   73 (48.7%)   150 (100.0%)
## ------------ ------ ------------ ------------ --------------
## 
## ----------------------------
##  Chi.squared   df   p.value 
## ------------- ---- ---------
##    86.0345     2       0    
## ----------------------------&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;em&gt;p&lt;/em&gt;-value is close to 0 so we reject the null hypothesis of independence between the two variables. In our context, this indicates that species and size are dependent and that there is a significant relationship between the two variables.&lt;/p&gt;
&lt;p&gt;It is also possible to create a contingency table for each level of a third categorical variable thanks to the combination of the &lt;code&gt;stby()&lt;/code&gt; and &lt;code&gt;ctable()&lt;/code&gt; functions. There are only 2 categorical variables in our dataset, so let’s use the &lt;code&gt;tabacco&lt;/code&gt; dataset which has 4 categorical variables (i.e., gender, age group, smoker, diseased). For this example, we would like to create a contingency table of the variables &lt;code&gt;smoker&lt;/code&gt; and &lt;code&gt;diseased&lt;/code&gt;, and this for each &lt;code&gt;gender&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;stby(
  list(
    x = tobacco$smoker, # smoker and diseased
    y = tobacco$diseased
  ),
  INDICES = tobacco$gender, # for each gender
  FUN = ctable # ctable for cross-tabulation
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Cross-Tabulation, Row Proportions  
## smoker * diseased  
## Data Frame: tobacco  
## Group: gender = F  
## 
## -------- ---------- ------------- ------------- --------------
##            diseased           Yes            No          Total
##   smoker                                                      
##      Yes               62 (42.2%)    85 (57.8%)   147 (100.0%)
##       No               49 (14.3%)   293 (85.7%)   342 (100.0%)
##    Total              111 (22.7%)   378 (77.3%)   489 (100.0%)
## -------- ---------- ------------- ------------- --------------
## 
## Group: gender = M  
## 
## -------- ---------- ------------- ------------- --------------
##            diseased           Yes            No          Total
##   smoker                                                      
##      Yes               63 (44.1%)    80 (55.9%)   143 (100.0%)
##       No               47 (13.6%)   299 (86.4%)   346 (100.0%)
##    Total              110 (22.5%)   379 (77.5%)   489 (100.0%)
## -------- ---------- ------------- ------------- --------------&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;descriptive-statistics-with-descr&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Descriptive statistics with &lt;code&gt;descr()&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;descr()&lt;/code&gt; function produces descriptive (univariate) statistics with common central tendency statistics and measures of dispersion. (See the &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand/#location-versus-dispersion-measures&#34;&gt;difference between a measure of central tendency and dispersion&lt;/a&gt; if you need a reminder.)&lt;/p&gt;
&lt;p&gt;A major advantage of this function is that it accepts single vectors as well as data frames. If a data frame is provided, all non-numerical columns are ignored so you do not have to remove them yourself before running the function.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;descr()&lt;/code&gt; function allows to display:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;only a selection of descriptive statistics of your choice, with the &lt;code&gt;stats = c(&#34;mean&#34;, &#34;sd&#34;)&lt;/code&gt; argument for mean and standard deviation for example&lt;/li&gt;
&lt;li&gt;the minimum, first quartile, median, third quartile and maximum with &lt;code&gt;stats = &#34;fivenum&#34;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;the most common descriptive statistics (mean, standard deviation, minimum, median, maximum, number and percentage of valid observations), with &lt;code&gt;stats = &#34;common&#34;&lt;/code&gt;:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;descr(dat,
  headings = FALSE, # remove headings
  stats = &amp;quot;common&amp;quot; # most common descriptive statistics
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
##                   Petal.Length   Petal.Width   Sepal.Length   Sepal.Width
## --------------- -------------- ------------- -------------- -------------
##            Mean           3.76          1.20           5.84          3.06
##         Std.Dev           1.77          0.76           0.83          0.44
##             Min           1.00          0.10           4.30          2.00
##          Median           4.35          1.30           5.80          3.00
##             Max           6.90          2.50           7.90          4.40
##         N.Valid         150.00        150.00         150.00        150.00
##               N         150.00        150.00         150.00        150.00
##       Pct.Valid         100.00        100.00         100.00        100.00&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Tip:&lt;/em&gt; if you have a large number of variables, add the &lt;code&gt;transpose = TRUE&lt;/code&gt; argument for a better display.&lt;/p&gt;
&lt;p&gt;In order to compute these descriptive statistics by group (e.g., &lt;code&gt;Species&lt;/code&gt; in our dataset), use the &lt;code&gt;descr()&lt;/code&gt; function in combination with the &lt;code&gt;stby()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;stby(
  data = dat,
  INDICES = dat$Species, # by Species
  FUN = descr, # descriptive statistics
  stats = &amp;quot;common&amp;quot; # most common descr. stats
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Descriptive Statistics  
## dat  
## Group: Species = setosa  
## N: 50  
## 
##                   Petal.Length   Petal.Width   Sepal.Length   Sepal.Width
## --------------- -------------- ------------- -------------- -------------
##            Mean           1.46          0.25           5.01          3.43
##         Std.Dev           0.17          0.11           0.35          0.38
##             Min           1.00          0.10           4.30          2.30
##          Median           1.50          0.20           5.00          3.40
##             Max           1.90          0.60           5.80          4.40
##         N.Valid          50.00         50.00          50.00         50.00
##               N          50.00         50.00          50.00         50.00
##       Pct.Valid         100.00        100.00         100.00        100.00
## 
## Group: Species = versicolor  
## N: 50  
## 
##                   Petal.Length   Petal.Width   Sepal.Length   Sepal.Width
## --------------- -------------- ------------- -------------- -------------
##            Mean           4.26          1.33           5.94          2.77
##         Std.Dev           0.47          0.20           0.52          0.31
##             Min           3.00          1.00           4.90          2.00
##          Median           4.35          1.30           5.90          2.80
##             Max           5.10          1.80           7.00          3.40
##         N.Valid          50.00         50.00          50.00         50.00
##               N          50.00         50.00          50.00         50.00
##       Pct.Valid         100.00        100.00         100.00        100.00
## 
## Group: Species = virginica  
## N: 50  
## 
##                   Petal.Length   Petal.Width   Sepal.Length   Sepal.Width
## --------------- -------------- ------------- -------------- -------------
##            Mean           5.55          2.03           6.59          2.97
##         Std.Dev           0.55          0.27           0.64          0.32
##             Min           4.50          1.40           4.90          2.20
##          Median           5.55          2.00           6.50          3.00
##             Max           6.90          2.50           7.90          3.80
##         N.Valid          50.00         50.00          50.00         50.00
##               N          50.00         50.00          50.00         50.00
##       Pct.Valid         100.00        100.00         100.00        100.00&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;data-frame-summaries-with-dfsummary&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Data frame summaries with &lt;code&gt;dfSummary()&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;dfSummary()&lt;/code&gt; function generates a summary table with statistics, frequencies and graphs for all variables in a dataset. The information shown depends on the type of the variables (character, factor, numeric, date) and also varies according to the number of distinct values.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dfSummary(dat)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Data Frame Summary  
## dat  
## Dimensions: 150 x 6  
## Duplicates: 1  
## 
## -----------------------------------------------------------------------------------------------------------
## No   Variable       Stats / Values          Freqs (% of Valid)   Graph                 Valid      Missing  
## ---- -------------- ----------------------- -------------------- --------------------- ---------- ---------
## 1    Sepal.Length   Mean (sd) : 5.8 (0.8)   35 distinct values     . . : :             150        0        
##      [numeric]      min &amp;lt; med &amp;lt; max:                               : : : :             (100.0%)   (0.0%)   
##                     4.3 &amp;lt; 5.8 &amp;lt; 7.9                                : : : : :                               
##                     IQR (CV) : 1.3 (0.1)                           : : : : :                               
##                                                                  : : : : : : : :                           
## 
## 2    Sepal.Width    Mean (sd) : 3.1 (0.4)   23 distinct values           :             150        0        
##      [numeric]      min &amp;lt; med &amp;lt; max:                                     :             (100.0%)   (0.0%)   
##                     2 &amp;lt; 3 &amp;lt; 4.4                                        . :                                 
##                     IQR (CV) : 0.5 (0.1)                             : : : :                               
##                                                                  . . : : : : : :                           
## 
## 3    Petal.Length   Mean (sd) : 3.8 (1.8)   43 distinct values   :                     150        0        
##      [numeric]      min &amp;lt; med &amp;lt; max:                             :         . :         (100.0%)   (0.0%)   
##                     1 &amp;lt; 4.3 &amp;lt; 6.9                                :         : : .                           
##                     IQR (CV) : 3.5 (0.5)                         : :       : : : .                         
##                                                                  : :   . : : : : : .                       
## 
## 4    Petal.Width    Mean (sd) : 1.2 (0.8)   22 distinct values   :                     150        0        
##      [numeric]      min &amp;lt; med &amp;lt; max:                             :                     (100.0%)   (0.0%)   
##                     0.1 &amp;lt; 1.3 &amp;lt; 2.5                              :       . .   :                           
##                     IQR (CV) : 1.5 (0.6)                         :       : :   :   .                       
##                                                                  : :   : : : . : : :                       
## 
## 5    Species        1. setosa               50 (33.3%)           IIIIII                150        0        
##      [factor]       2. versicolor           50 (33.3%)           IIIIII                (100.0%)   (0.0%)   
##                     3. virginica            50 (33.3%)           IIIIII                                    
## 
## 6    size           1. big                  77 (51.3%)           IIIIIIIIII            150        0        
##      [character]    2. small                73 (48.7%)           IIIIIIIII             (100.0%)   (0.0%)   
## -----------------------------------------------------------------------------------------------------------&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;describeby-from-the-psych-package&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;&lt;code&gt;describeBy()&lt;/code&gt; from the &lt;code&gt;{psych}&lt;/code&gt; package&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;describeBy()&lt;/code&gt; function from the &lt;code&gt;{psych}&lt;/code&gt; package allows to report several summary statistics (i.e., number of valid cases, mean, standard deviation, median, trimmed mean, mad: median absolute deviation (from the median), minimum, maximum, range, skewness and kurtosis) by a grouping variable.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(psych)
describeBy(
  dat,
  dat$Species # grouping variable
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
##  Descriptive statistics by group 
## group: setosa
##              vars  n mean   sd median trimmed  mad min max range  skew kurtosis
## Sepal.Length    1 50 5.01 0.35    5.0    5.00 0.30 4.3 5.8   1.5  0.11    -0.45
## Sepal.Width     2 50 3.43 0.38    3.4    3.42 0.37 2.3 4.4   2.1  0.04     0.60
## Petal.Length    3 50 1.46 0.17    1.5    1.46 0.15 1.0 1.9   0.9  0.10     0.65
## Petal.Width     4 50 0.25 0.11    0.2    0.24 0.00 0.1 0.6   0.5  1.18     1.26
## Species         5 50 1.00 0.00    1.0    1.00 0.00 1.0 1.0   0.0   NaN      NaN
## size            6 50 1.98 0.14    2.0    2.00 0.00 1.0 2.0   1.0 -6.65    43.12
##                se
## Sepal.Length 0.05
## Sepal.Width  0.05
## Petal.Length 0.02
## Petal.Width  0.01
## Species      0.00
## size         0.02
## ------------------------------------------------------------ 
## group: versicolor
##              vars  n mean   sd median trimmed  mad min max range  skew kurtosis
## Sepal.Length    1 50 5.94 0.52   5.90    5.94 0.52 4.9 7.0   2.1  0.10    -0.69
## Sepal.Width     2 50 2.77 0.31   2.80    2.78 0.30 2.0 3.4   1.4 -0.34    -0.55
## Petal.Length    3 50 4.26 0.47   4.35    4.29 0.52 3.0 5.1   2.1 -0.57    -0.19
## Petal.Width     4 50 1.33 0.20   1.30    1.32 0.22 1.0 1.8   0.8 -0.03    -0.59
## Species         5 50 2.00 0.00   2.00    2.00 0.00 2.0 2.0   0.0   NaN      NaN
## size            6 50 1.42 0.50   1.00    1.40 0.00 1.0 2.0   1.0  0.31    -1.94
##                se
## Sepal.Length 0.07
## Sepal.Width  0.04
## Petal.Length 0.07
## Petal.Width  0.03
## Species      0.00
## size         0.07
## ------------------------------------------------------------ 
## group: virginica
##              vars  n mean   sd median trimmed  mad min max range  skew kurtosis
## Sepal.Length    1 50 6.59 0.64   6.50    6.57 0.59 4.9 7.9   3.0  0.11    -0.20
## Sepal.Width     2 50 2.97 0.32   3.00    2.96 0.30 2.2 3.8   1.6  0.34     0.38
## Petal.Length    3 50 5.55 0.55   5.55    5.51 0.67 4.5 6.9   2.4  0.52    -0.37
## Petal.Width     4 50 2.03 0.27   2.00    2.03 0.30 1.4 2.5   1.1 -0.12    -0.75
## Species         5 50 3.00 0.00   3.00    3.00 0.00 3.0 3.0   0.0   NaN      NaN
## size            6 50 1.06 0.24   1.00    1.00 0.00 1.0 2.0   1.0  3.59    11.15
##                se
## Sepal.Length 0.09
## Sepal.Width  0.05
## Petal.Length 0.08
## Petal.Width  0.04
## Species      0.00
## size         0.03&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;aggregate-function&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;&lt;code&gt;aggregate()&lt;/code&gt; function&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;aggregate()&lt;/code&gt; function allows to split the data into subsets and then to compute summary statistics for each. For instance, if we want to compute the mean for the variables &lt;code&gt;Sepal.Length&lt;/code&gt; and &lt;code&gt;Sepal.Width&lt;/code&gt; by &lt;code&gt;Species&lt;/code&gt; and &lt;code&gt;Size&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;aggregate(cbind(Sepal.Length, Sepal.Width) ~ Species + size,
  data = dat,
  mean
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##      Species  size Sepal.Length Sepal.Width
## 1     setosa   big     5.800000    4.000000
## 2 versicolor   big     6.282759    2.868966
## 3  virginica   big     6.663830    2.997872
## 4     setosa small     4.989796    3.416327
## 5 versicolor small     5.457143    2.633333
## 6  virginica small     5.400000    2.600000&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;summaryby-from-doby&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;&lt;code&gt;summaryBy()&lt;/code&gt; from &lt;code&gt;{doBy}&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;An alternative is the &lt;code&gt;summaryBy()&lt;/code&gt; function from the &lt;code&gt;{doBy}&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# summary statistics by group
library(doBy)
summaryBy(Sepal.Length + Sepal.Width ~ Species,
  data = dat,
  FUN = summary
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##      Species Sepal.Length.Min. Sepal.Length.1st Qu. Sepal.Length.Median
## 1     setosa               4.3                4.800                 5.0
## 2 versicolor               4.9                5.600                 5.9
## 3  virginica               4.9                6.225                 6.5
##   Sepal.Length.Mean Sepal.Length.3rd Qu. Sepal.Length.Max. Sepal.Width.Min.
## 1             5.006                  5.2               5.8              2.3
## 2             5.936                  6.3               7.0              2.0
## 3             6.588                  6.9               7.9              2.2
##   Sepal.Width.1st Qu. Sepal.Width.Median Sepal.Width.Mean Sepal.Width.3rd Qu.
## 1               3.200                3.4            3.428               3.675
## 2               2.525                2.8            2.770               3.000
## 3               2.800                3.0            2.974               3.175
##   Sepal.Width.Max.
## 1              4.4
## 2              3.4
## 3              3.8&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you are interested in some specific descriptive statistics, you can easily specify them via the &lt;code&gt;FUN&lt;/code&gt; argument:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;summaryBy(Sepal.Length + Sepal.Width ~ Species,
  data = dat,
  FUN = c(mean, var)
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##      Species Sepal.Length.mean Sepal.Width.mean Sepal.Length.var
## 1     setosa             5.006            3.428        0.1242490
## 2 versicolor             5.936            2.770        0.2664327
## 3  virginica             6.588            2.974        0.4043429
##   Sepal.Width.var
## 1      0.14368980
## 2      0.09846939
## 3      0.10400408&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;group_by-and-summarise-from-dplyr&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;&lt;code&gt;group_by()&lt;/code&gt; and &lt;code&gt;summarise()&lt;/code&gt; from &lt;code&gt;{dplyr}&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;Another alternative is with the &lt;code&gt;summarise()&lt;/code&gt; and &lt;code&gt;group_by()&lt;/code&gt; functions from the &lt;code&gt;{dplyr}&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(dplyr)

group_by(dat, Species) %&amp;gt;%
  summarise(
    mean = mean(Sepal.Length, na.rm = TRUE),
    sd = sd(Sepal.Length, na.rm = TRUE)
  )&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 3 × 3
##   Species     mean    sd
##   &amp;lt;fct&amp;gt;      &amp;lt;dbl&amp;gt; &amp;lt;dbl&amp;gt;
## 1 setosa      5.01 0.352
## 2 versicolor  5.94 0.516
## 3 virginica   6.59 0.636&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;conclusion&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;Thanks for reading.&lt;/p&gt;
&lt;p&gt;I hope this article helped you to do descriptive statistics in R. If you would like to do the same by hand or understand what these statistics represent, I invite you to read the article “&lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand/&#34;&gt;Descriptive statistics by hand&lt;/a&gt;”.&lt;/p&gt;
&lt;p&gt;As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;(Note that this article is available for download on my &lt;a href=&#34;https://statsandr.gumroad.com/&#34;&gt;Gumroad page&lt;/a&gt;.)&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;footnotes footnotes-end-of-document&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;Normality tests such as Shapiro-Wilk or Kolmogorov-Smirnov tests can also be used to test whether the data follow a normal distribution or not. However, in practice, normality tests are often considered as too conservative in the sense that for large sample size, a small deviation from the normality may cause the normality condition to be violated. For this reason, it is often the case that the normality condition is verified based on a combination of visual inspections (with histograms and QQ-plots) and formal test (Shapiro-Wilk test for instance).&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn2&#34;&gt;&lt;p&gt;Note that the &lt;code&gt;plain.ascii&lt;/code&gt; and &lt;code&gt;style&lt;/code&gt; arguments are needed for this package. In our examples, these arguments are added in the settings of each chunk so they are not visible.&lt;a href=&#34;#fnref2&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn3&#34;&gt;&lt;p&gt;Note that it is also possible to compute odds ratio and risk ratio. See the vignette of the package for more information on this matter as these ratios are beyond the scope of this article.&lt;a href=&#34;#fnref3&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Descriptive statistics by hand</title>
      <link>https://statsandr.com/blog/descriptive-statistics-by-hand/</link>
      <pubDate>Sun, 19 Jan 2020 00:00:00 +0000</pubDate>
      
      <guid>https://statsandr.com/blog/descriptive-statistics-by-hand/</guid>
      <description>

&lt;div id=&#34;TOC&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#introduction&#34; id=&#34;toc-introduction&#34;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#location-versus-dispersion-measures&#34; id=&#34;toc-location-versus-dispersion-measures&#34;&gt;Location versus dispersion measures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#location&#34; id=&#34;toc-location&#34;&gt;Location&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#minimum-and-maximum&#34; id=&#34;toc-minimum-and-maximum&#34;&gt;Minimum and maximum&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#mean&#34; id=&#34;toc-mean&#34;&gt;Mean&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#median&#34; id=&#34;toc-median&#34;&gt;Median&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#odd-number-of-observations&#34; id=&#34;toc-odd-number-of-observations&#34;&gt;Odd number of observations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#even-number-of-observations&#34; id=&#34;toc-even-number-of-observations&#34;&gt;Even number of observations&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#mean-vs.-median&#34; id=&#34;toc-mean-vs.-median&#34;&gt;Mean vs. median&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#st-and-3rd-quartiles&#34; id=&#34;toc-st-and-3rd-quartiles&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(1^{st}\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(3^{rd}\)&lt;/span&gt; quartiles&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#q_0.25-q_0.75-and-q_0.5&#34; id=&#34;toc-q_0.25-q_0.75-and-q_0.5&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(q_{0.25}\)&lt;/span&gt;, &lt;span class=&#34;math inline&#34;&gt;\(q_{0.75}\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(q_{0.5}\)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#a-note-on-deciles-and-percentiles&#34; id=&#34;toc-a-note-on-deciles-and-percentiles&#34;&gt;A note on deciles and percentiles&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#mode&#34; id=&#34;toc-mode&#34;&gt;Mode&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#quantitative-variables&#34; id=&#34;toc-quantitative-variables&#34;&gt;Quantitative variables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#qualitative-variables&#34; id=&#34;toc-qualitative-variables&#34;&gt;Qualitative variables&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#dispersion&#34; id=&#34;toc-dispersion&#34;&gt;Dispersion&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#range&#34; id=&#34;toc-range&#34;&gt;Range&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#standard-deviation&#34; id=&#34;toc-standard-deviation&#34;&gt;Standard deviation&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#standard-deviation-for-a-population&#34; id=&#34;toc-standard-deviation-for-a-population&#34;&gt;Standard deviation for a population&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#standard-deviation-for-a-sample&#34; id=&#34;toc-standard-deviation-for-a-sample&#34;&gt;Standard deviation for a sample&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#variance&#34; id=&#34;toc-variance&#34;&gt;Variance&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#variance-for-a-population&#34; id=&#34;toc-variance-for-a-population&#34;&gt;Variance for a population&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#variance-for-a-sample&#34; id=&#34;toc-variance-for-a-sample&#34;&gt;Variance for a sample&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#standard-deviation-vs.-variance&#34; id=&#34;toc-standard-deviation-vs.-variance&#34;&gt;Standard deviation vs. variance&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#notations&#34; id=&#34;toc-notations&#34;&gt;Notations&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#interquartile-range&#34; id=&#34;toc-interquartile-range&#34;&gt;Interquartile range&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#coefficient-of-variation&#34; id=&#34;toc-coefficient-of-variation&#34;&gt;Coefficient of variation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#coefficient-of-variation-vs.-standard-deviation&#34; id=&#34;toc-coefficient-of-variation-vs.-standard-deviation&#34;&gt;Coefficient of variation vs. standard deviation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#conclusion&#34; id=&#34;toc-conclusion&#34;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand_files/descriptive-statistics-by-hand.jpeg&#34; style=&#34;width:100.0%&#34; /&gt;&lt;/p&gt;
&lt;div id=&#34;introduction&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;This article explains how to compute the main descriptive statistics by hand and how to interpret them. To learn how to compute these measures in R, read the article “&lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/&#34;&gt;Descriptive statistics in R&lt;/a&gt;”.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Descriptive statistics&lt;/strong&gt; (in the broad sense of the term) is a branch of statistics aiming at &lt;strong&gt;summarizing, describing and presenting a series of values or a dataset&lt;/strong&gt;. Long series of values without any preparation or without any summary measures are often not informative due to the difficulty of recognizing any pattern in the data. Below an example with the height (in cm) of a population of 100 adults:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;188.7&lt;/em&gt;, &lt;em&gt;169.4&lt;/em&gt;, &lt;em&gt;178.6&lt;/em&gt;, &lt;em&gt;181.3&lt;/em&gt;, &lt;em&gt;179&lt;/em&gt;, &lt;em&gt;173.9&lt;/em&gt;, &lt;em&gt;190.1&lt;/em&gt;, &lt;em&gt;174.1&lt;/em&gt;, &lt;em&gt;195.2&lt;/em&gt;, &lt;em&gt;174.4&lt;/em&gt;, &lt;em&gt;188&lt;/em&gt;, &lt;em&gt;197.9&lt;/em&gt;, &lt;em&gt;161.1&lt;/em&gt;, &lt;em&gt;172.2&lt;/em&gt;, &lt;em&gt;173.7&lt;/em&gt;, &lt;em&gt;181.4&lt;/em&gt;, &lt;em&gt;172.2&lt;/em&gt;, &lt;em&gt;148.4&lt;/em&gt;, &lt;em&gt;150.6&lt;/em&gt;, &lt;em&gt;188.2&lt;/em&gt;, &lt;em&gt;171.9&lt;/em&gt;, &lt;em&gt;157.2&lt;/em&gt;, &lt;em&gt;173.3&lt;/em&gt;, &lt;em&gt;187.1&lt;/em&gt;, &lt;em&gt;194&lt;/em&gt;, &lt;em&gt;170.7&lt;/em&gt;, &lt;em&gt;172.4&lt;/em&gt;, &lt;em&gt;157.4&lt;/em&gt;, &lt;em&gt;179.6&lt;/em&gt;, &lt;em&gt;168.6&lt;/em&gt;, &lt;em&gt;179.6&lt;/em&gt;, &lt;em&gt;182&lt;/em&gt;, &lt;em&gt;185.4&lt;/em&gt;, &lt;em&gt;168.9&lt;/em&gt;, &lt;em&gt;180&lt;/em&gt;, &lt;em&gt;157.8&lt;/em&gt;, &lt;em&gt;167.2&lt;/em&gt;, &lt;em&gt;166.5&lt;/em&gt;, &lt;em&gt;150.9&lt;/em&gt;, &lt;em&gt;175.4&lt;/em&gt;, &lt;em&gt;177.1&lt;/em&gt;, &lt;em&gt;171.4&lt;/em&gt;, &lt;em&gt;182.6&lt;/em&gt;, &lt;em&gt;167.7&lt;/em&gt;, &lt;em&gt;161.3&lt;/em&gt;, &lt;em&gt;179.3&lt;/em&gt;, &lt;em&gt;166.9&lt;/em&gt;, &lt;em&gt;189.4&lt;/em&gt;, &lt;em&gt;170.7&lt;/em&gt;, &lt;em&gt;181.6&lt;/em&gt;, &lt;em&gt;178.2&lt;/em&gt;, &lt;em&gt;167.2&lt;/em&gt;, &lt;em&gt;190.8&lt;/em&gt;, &lt;em&gt;181.4&lt;/em&gt;, &lt;em&gt;175.9&lt;/em&gt;, &lt;em&gt;177.8&lt;/em&gt;, &lt;em&gt;181.8&lt;/em&gt;, &lt;em&gt;175.9&lt;/em&gt;, &lt;em&gt;145.1&lt;/em&gt;, &lt;em&gt;177.8&lt;/em&gt;, &lt;em&gt;171.3&lt;/em&gt;, &lt;em&gt;176.9&lt;/em&gt;, &lt;em&gt;180.8&lt;/em&gt;, &lt;em&gt;189&lt;/em&gt;, &lt;em&gt;167.7&lt;/em&gt;, &lt;em&gt;188&lt;/em&gt;, &lt;em&gt;178.4&lt;/em&gt;, &lt;em&gt;185.4&lt;/em&gt;, &lt;em&gt;184.2&lt;/em&gt;, &lt;em&gt;182.2&lt;/em&gt;, &lt;em&gt;164.6&lt;/em&gt;, &lt;em&gt;174.1&lt;/em&gt;, &lt;em&gt;181.2&lt;/em&gt;, &lt;em&gt;165.5&lt;/em&gt;, &lt;em&gt;169.6&lt;/em&gt;, &lt;em&gt;180.8&lt;/em&gt;, &lt;em&gt;182.7&lt;/em&gt;, &lt;em&gt;179.6&lt;/em&gt;, &lt;em&gt;166.1&lt;/em&gt;, &lt;em&gt;164&lt;/em&gt;, &lt;em&gt;190.1&lt;/em&gt;, &lt;em&gt;177.6&lt;/em&gt;, &lt;em&gt;175.9&lt;/em&gt;, &lt;em&gt;173.8&lt;/em&gt;, &lt;em&gt;163.1&lt;/em&gt;, &lt;em&gt;181.1&lt;/em&gt;, &lt;em&gt;172.8&lt;/em&gt;, &lt;em&gt;173.2&lt;/em&gt;, &lt;em&gt;184.3&lt;/em&gt;, &lt;em&gt;183.2&lt;/em&gt;, &lt;em&gt;188.9&lt;/em&gt;, &lt;em&gt;170.2&lt;/em&gt;, &lt;em&gt;181.5&lt;/em&gt;, &lt;em&gt;188.9&lt;/em&gt;, &lt;em&gt;163.9&lt;/em&gt;, &lt;em&gt;166.4&lt;/em&gt;, &lt;em&gt;163.7&lt;/em&gt;, &lt;em&gt;160.4&lt;/em&gt;, &lt;em&gt;175.8&lt;/em&gt; and &lt;em&gt;181.5&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Facing this series, it is hard (not to say impossible) for anyone to understand the data and have a clear view of the size of these adults in a reasonable amount of time. Descriptive statistics allow to summarize, and thus have a better overview of the data. Of course, by summarizing data through one or several measures, some information will inevitably be lost. However, in many cases it is generally better to lose some information but in return gain an overview.&lt;/p&gt;
&lt;p&gt;Descriptive statistics is often the first step and an important part in any statistical analysis. It allows to check the quality of the data by detecting potential &lt;a href=&#34;https://statsandr.com/blog/outliers-detection-in-r/&#34;&gt;outliers&lt;/a&gt; (i.e., data points that appear to be separated from the rest of the data), collection or encoding errors. It also helps to “understand” the data and if well presented, descriptive statistics is already a good starting point for further analyses.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;location-versus-dispersion-measures&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Location versus dispersion measures&lt;/h1&gt;
&lt;p&gt;Several different measures (called statistics if we are analyzing a sample) are used to summarize the data. Some of them give an understanding about the &lt;strong&gt;location&lt;/strong&gt; of the data, others give and understanding about the &lt;strong&gt;dispersion&lt;/strong&gt; of the data. In practice, both types of measures are often used together in order to summarize the data in the most concise but complete way.&lt;/p&gt;
&lt;p&gt;We illustrate this point with the graph below, representing the height (in cm) of 100 persons divided into two groups (50 persons in each group):&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand_files/figure-html/unnamed-chunk-2-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The black line corresponds to the mean. The mean height (in cm) is similar in both groups. However, it is clear that the dispersion of heights are very different in the two groups. For this reason, location or dispersion measures are often not enough if presented individually and it is a good practice to present several statistics from both types of measures.&lt;/p&gt;
&lt;p&gt;In the following sections, we detail the most common location and dispersion measures and illustrate them with examples. Note that for the sake of simplicity, we consider only series of values (i.e., univariate data) and not bivariate or multivariate data, and we do not consider the case of series grouped in classes.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;location&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Location&lt;/h1&gt;
&lt;p&gt;Location measures allow to see “where” the data are located, around which values. In other words, location measures give an understanding on &lt;strong&gt;what is the central tendency&lt;/strong&gt;, the “position” of the data as a whole. It includes the following statistics (others exist but we focus on the most common ones):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;minimum&lt;/li&gt;
&lt;li&gt;maximum&lt;/li&gt;
&lt;li&gt;mean&lt;/li&gt;
&lt;li&gt;median&lt;/li&gt;
&lt;li&gt;first quartile&lt;/li&gt;
&lt;li&gt;third quartile&lt;/li&gt;
&lt;li&gt;mode&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We detail and compute by hand each of them in the following sections.&lt;/p&gt;
&lt;div id=&#34;minimum-and-maximum&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Minimum and maximum&lt;/h2&gt;
&lt;p&gt;Minimum (&lt;span class=&#34;math inline&#34;&gt;\(min\)&lt;/span&gt;) and maximum (&lt;span class=&#34;math inline&#34;&gt;\(max\)&lt;/span&gt;) are simply the lowest and largest values, respectively. Given the height (in cm) of a sample of 6 adults:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;188.7&lt;/em&gt;, &lt;em&gt;169.4&lt;/em&gt;, &lt;em&gt;178.6&lt;/em&gt;, &lt;em&gt;181.3&lt;/em&gt;, &lt;em&gt;179&lt;/em&gt; and &lt;em&gt;173.9&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The minimum is 169.4 cm and the maximum is 188.7 cm. These two basic statistics give a clear idea about the size of the smallest and tallest of these 6 adults.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;mean&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Mean&lt;/h2&gt;
&lt;p&gt;The mean, also known as average, is probably the most common statistics. It gives an idea on what is the average value, that is, the central value of the data or in other words the center of gravity:&lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div style=&#34;text-align:center&#34;&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand_files/mean.png&#34; alt=&#34;Mean. Source: LFSAB1105 at UCLouvain&#34; style=&#34;width: 80%;&#34;&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;The mean is found by summing all values and dividing the total by the number of observations (denoted &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt;):&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[mean = \bar{x} = \frac{\text{sum of all values}}{\text{number of values}} = \frac{1}{n}\sum^{n}_{i = 1} x_i\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Given our sample of 6 adults presented above, the mean is:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\bar{x} = \frac{188.7 + 169.4 + 178.6}{6}\]&lt;/span&gt;
&lt;span class=&#34;math display&#34;&gt;\[\frac{+ 181.3 + 179 + 173.9}{6}\\ = 178.4833\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In conclusion, the mean size, that is, the average size of our sample of 6 adults is 178.48 cm (rounded to 2 decimals).&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;median&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Median&lt;/h2&gt;
&lt;p&gt;The median is another measure of location so it also gives an idea about the central tendency of the data. The interpretation of the median is that there are as many observations below as above the median. In other words, 50% of the observations lie below the median, and 50% of the observations lie above the median. Below a visual representation of the median:&lt;/p&gt;
&lt;div style=&#34;text-align: center;&#34;&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand_files/median.png&#34; alt=&#34;Median. Source: LFSAB1105 at UCLouvain&#34; style=&#34;width: 80%;&#34;&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;The easiest way to compute the median is by first sorting the data from lowest to highest (i.e., in ascending order) then take the middle point as the median. From the sorted values, for an odd number of observations, the middle point is easy to find: it is the value with as many observations below as above. Still from the sorted values, for an even number of observations, the middle point is exactly between the two middle values. Formally, after sorting, the median is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;if &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; (number of observations) is odd: &lt;span class=&#34;math display&#34;&gt;\[med(x) = x_{\frac{n+1}{2}}\]&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;if &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; is even: &lt;span class=&#34;math display&#34;&gt;\[med(x) = \frac{1}{2}\big(x_{\frac{n}{2}} + x_{\frac{n}{2} + 1}\big)\]&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;where the subscript of &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt; denotes the numbering of the sorted data. The formulas look harder than they really are, so let’s see with two concrete examples.&lt;/p&gt;
&lt;div id=&#34;odd-number-of-observations&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Odd number of observations&lt;/h3&gt;
&lt;p&gt;Given the height of a sample of 7 adults taken from the 100 adults presented in the introduction:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;188.9&lt;/em&gt;, &lt;em&gt;163.9&lt;/em&gt;, &lt;em&gt;166.4&lt;/em&gt;, &lt;em&gt;163.7&lt;/em&gt;, &lt;em&gt;160.4&lt;/em&gt;, &lt;em&gt;175.8&lt;/em&gt; and &lt;em&gt;181.5&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;We first sort the order from lowest to highest:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;160.4&lt;/em&gt;, &lt;em&gt;163.7&lt;/em&gt;, &lt;em&gt;163.9&lt;/em&gt;, &lt;em&gt;166.4&lt;/em&gt;, &lt;em&gt;175.8&lt;/em&gt;, &lt;em&gt;181.5&lt;/em&gt; and &lt;em&gt;188.9&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Given that the number of observations &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; is odd (since &lt;span class=&#34;math inline&#34;&gt;\(n = 7\)&lt;/span&gt;), the median is&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[med(x) = x_\frac{7 + 1}{2} = x_4\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;So we take the fourth value from the sorted values, which corresponds to 166.4. In conclusion, the median size of these 7 adults is 166.4 cm. As you can see, there are 3 observations below 166.4 and 3 observations above 166.4 cm.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;even-number-of-observations&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Even number of observations&lt;/h3&gt;
&lt;p&gt;Now let’s see when the number of observations is even, which is slightly more complicated than when the number of observations is odd. Given the height of a sample of 6 adults:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;188.7&lt;/em&gt;, &lt;em&gt;169.4&lt;/em&gt;, &lt;em&gt;178.6&lt;/em&gt;, &lt;em&gt;181.3&lt;/em&gt;, &lt;em&gt;179&lt;/em&gt; and &lt;em&gt;173.9&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;We sort the values in ascending order:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;169.4&lt;/em&gt;, &lt;em&gt;173.9&lt;/em&gt;, &lt;em&gt;178.6&lt;/em&gt;, &lt;em&gt;179&lt;/em&gt;, &lt;em&gt;181.3&lt;/em&gt; and &lt;em&gt;188.7&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Given that the number of observations &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; is even (since &lt;span class=&#34;math inline&#34;&gt;\(n = 6\)&lt;/span&gt;), the median is&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[med(x) = \frac{1}{2}\big(x_{\frac{6}{2}} + x_{\frac{6}{2} + 1}\big) = \frac{1}{2}\big(x_{3} + x_{4}\big)\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;So we sum the third and fourth values from the sorted values and divide the total by 2 (which is equivalent than taking the mean of these two middle values):&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\frac{1}{2}(178.6 + 179) = 178.8\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In conclusion, the median size of these 6 adults is 178.8 cm. Again, remark that there are as many observations below as above 178.8 cm.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;mean-vs.-median&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Mean vs. median&lt;/h2&gt;
&lt;p&gt;Although the mean and median are often relatively close to each other (in particular when the distribution is symmetric) they should not be confused since they both have advantages and disadvantages in different contexts. Besides the fact that almost everyone knows (or at least have heard about) the mean, it has the advantage that it gives a unique picture for each different series of data. However, it has the disadvantage that the mean is sensible to &lt;a href=&#34;https://statsandr.com/blog/outliers-detection-in-r/&#34;&gt;outliers&lt;/a&gt; (i.e., extreme values). On the other hand, the advantage of the median is that it is resistant to outliers and the inconvenient is that it may be the exact same value for very different series of data (so not unique to the data).&lt;/p&gt;
&lt;p&gt;To illustrate the “sensible to outlier” argument, consider 3 friends in a bar comparing their salaries. Their salaries are &lt;em&gt;1800&lt;/em&gt;, &lt;em&gt;2000&lt;/em&gt; and &lt;em&gt;2100&lt;/em&gt;€, for an average (mean) salary of &lt;em&gt;1967&lt;/em&gt;€. A friend of them (who happens to be friend with Bill Gates as well) joins them in the bar. Their salaries are now &lt;em&gt;1800&lt;/em&gt;, &lt;em&gt;2000&lt;/em&gt;, &lt;em&gt;2100&lt;/em&gt; and &lt;em&gt;1000000&lt;/em&gt;€. The average salary of the 4 friends is now &lt;em&gt;251475&lt;/em&gt;€, compared to &lt;em&gt;1967&lt;/em&gt;€ without the rich friend. Although it is statistically correct to say that the mean salary of the 4 friends is &lt;em&gt;251475&lt;/em&gt;€, you will concede that this measure does not represent a fair image of the salaries of the 4 friends, as 3 of them earn much less than the mean salary. As we have just seen, the mean is sensible to outliers. (&lt;em&gt;Note:&lt;/em&gt; this example also shows how a large majority of citizens earn less than the mean salary reported in the news. For the french-speaking readers, see this &lt;a href=&#34;https://www.youtube.com/watch?v=uIx2xvdwIIo&#34;&gt;video&lt;/a&gt; for more information.)&lt;/p&gt;
&lt;p&gt;On the other hand, if we report the medians, we see that the median salary of the 3 first friends is &lt;em&gt;2000&lt;/em&gt;€, and the median salary of the 4 friends is &lt;em&gt;2050&lt;/em&gt;€. As you can see with this example, the median is not sensible to outliers and for series with such extreme value(s), the median is more appropriate compared to the mean as it often gives a better representation of the data.&lt;/p&gt;
&lt;p&gt;Given the previous example, one may then choose to always use the median instead of the mean. However, the median has it own inconvenient which the mean does not have: the median is less unique and less specific to its underlying data than the mean. Consider the following data, representing the grades of 5 students taking a statistics and economics exam:&lt;/p&gt;
&lt;table style=&#34;width:51%;&#34;&gt;
&lt;colgroup&gt;
&lt;col width=&#34;16%&#34; /&gt;
&lt;col width=&#34;16%&#34; /&gt;
&lt;col width=&#34;18%&#34; /&gt;
&lt;/colgroup&gt;
&lt;thead&gt;
&lt;tr class=&#34;header&#34;&gt;
&lt;th align=&#34;center&#34;&gt;studentID&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;economics&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;statistics&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;center&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;10&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;center&#34;&gt;2&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;10&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;center&#34;&gt;3&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;10&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;center&#34;&gt;4&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;18&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;center&#34;&gt;5&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;20&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;11&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;br&gt;
The median of the grades is the same in economics and statistics (median = &lt;em&gt;10&lt;/em&gt;). Therefore, had we computed only the medians, we could have concluded that the students performed as well in economics as in statistics. However, although the medians are exactly the same for both classes, it is clear that students performed better in economics than in statistics (compare both grades for each student to see for yourself). In fact, the mean of the grades in economics is &lt;em&gt;13.6&lt;/em&gt; and the mean of the grades in statistics is &lt;em&gt;8.6&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;What we have just shown here is that the median is based only on one single value, the middle value, or on the two middle values if there are an even number of observations, while the mean is based on all values (and thus includes more information). The median is therefore not sensible to outliers, but it is also not unique (i.e., not specific) to different series of data, whereas the mean is much more likely to be different and unique for different series of data. This difference in terms of specificity and uniqueness between the two measures may make the mean more useful for data with no outlier.&lt;/p&gt;
&lt;p&gt;In conclusion, depending on the context and the data, it is often more interesting to report the mean or the median, or both. As a last remark regarding the comparison between the two most important location measures, note that when the mean and median are equal, the distribution of your data can often be considered to follow a &lt;a href=&#34;https://statsandr.com/blog/do-my-data-follow-a-normal-distribution-a-note-on-the-most-widely-used-distribution-and-how-to-test-for-normality-in-r/&#34;&gt;normal distribution&lt;/a&gt; (also referred as a Gaussian distribution).&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;st-and-3rd-quartiles&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;&lt;span class=&#34;math inline&#34;&gt;\(1^{st}\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(3^{rd}\)&lt;/span&gt; quartiles&lt;/h2&gt;
&lt;p&gt;The first and third quartiles are similar to the median in the sense that they also divide the observations into two parts, except that these parts are not equal. Remind that the median divides the data into two equal parts (with 50% of the observations below and 50% above the median).&lt;/p&gt;
&lt;p&gt;The first quartile cuts the observations such that there are 25% of the observations &lt;strong&gt;below&lt;/strong&gt; and thus 75% &lt;strong&gt;above&lt;/strong&gt; the first quartile. The third quartile, as you have guessed by now, represents the value with 75% of the observations below it and thus 25% of the observations above it. There exists several methods to compute the first and third quartile (which sometimes give slight differences, R for instance uses a different method), but here is I believe the easiest one when computing these statistics by hand:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;sort the data in ascending order&lt;/li&gt;
&lt;li&gt;compute &lt;span class=&#34;math inline&#34;&gt;\(0.25 \cdot n\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(0.75 \cdot n\)&lt;/span&gt; (i.e., 0.25 and 0.75 times the number of observations)&lt;/li&gt;
&lt;li&gt;round up these two numbers to the next whole number&lt;/li&gt;
&lt;li&gt;these two numbers represent the rank of the first and third quartile (in the sorted series), respectively&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The steps are the same for both an odd and even number of observations. Here is an example with the following series, representing the height in cm of 9 adults:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;170.2&lt;/em&gt;, &lt;em&gt;181.5&lt;/em&gt;, &lt;em&gt;188.9&lt;/em&gt;, &lt;em&gt;163.9&lt;/em&gt;, &lt;em&gt;166.4&lt;/em&gt;, &lt;em&gt;163.7&lt;/em&gt;, &lt;em&gt;160.4&lt;/em&gt;, &lt;em&gt;175.8&lt;/em&gt; and &lt;em&gt;181.5&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;We first order from lowest to highest:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;160.4&lt;/em&gt;, &lt;em&gt;163.7&lt;/em&gt;, &lt;em&gt;163.9&lt;/em&gt;, &lt;em&gt;166.4&lt;/em&gt;, &lt;em&gt;170.2&lt;/em&gt;, &lt;em&gt;175.8&lt;/em&gt;, &lt;em&gt;181.5&lt;/em&gt;, &lt;em&gt;181.5&lt;/em&gt; and &lt;em&gt;188.9&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;There are 9 observations so &lt;span class=&#34;math display&#34;&gt;\[0.25 \cdot 9 = 2.25\]&lt;/span&gt; and &lt;span class=&#34;math display&#34;&gt;\[0.75 \cdot 9 = 6.75\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Rounding up to the whole number gives 3 and 7, which represent the rank of the first and third quartiles, respectively. Therefore, the first quartile is 163.9 cm and the third quartile is 181.5 cm.&lt;/p&gt;
&lt;p&gt;In conclusion, 25% of adults are less than 163.9 cm tall (and thus 75% of them are more than 163.9 cm tall), while 75% of adults are less than 181.5 cm tall (and thus 25% of them are more than 181.5 cm tall).&lt;/p&gt;
&lt;div id=&#34;q_0.25-q_0.75-and-q_0.5&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;&lt;span class=&#34;math inline&#34;&gt;\(q_{0.25}\)&lt;/span&gt;, &lt;span class=&#34;math inline&#34;&gt;\(q_{0.75}\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(q_{0.5}\)&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;Note that the first quartile is denoted &lt;span class=&#34;math inline&#34;&gt;\(q_{0.25}\)&lt;/span&gt; and the third quartile is denoted &lt;span class=&#34;math inline&#34;&gt;\(q_{0.75}\)&lt;/span&gt; (where &lt;span class=&#34;math inline&#34;&gt;\(q\)&lt;/span&gt; stands for quartile). As you can see, the median is actually the second quartile and for this reason it is also sometimes denoted &lt;span class=&#34;math inline&#34;&gt;\(q_{0.5}\)&lt;/span&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;a-note-on-deciles-and-percentiles&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;A note on deciles and percentiles&lt;/h3&gt;
&lt;p&gt;Deciles and percentiles are similar to quartiles except that they cuts the data in 10 and 100 equal parts. For instance, the &lt;span class=&#34;math inline&#34;&gt;\(4^{th}\)&lt;/span&gt; decile (&lt;span class=&#34;math inline&#34;&gt;\(q_{0.4}\)&lt;/span&gt;) is the value such that there are 40% of the observations below it and thus 60% of the observations above it.&lt;/p&gt;
&lt;p&gt;Percentiles follow the same logic. For example, the &lt;span class=&#34;math inline&#34;&gt;\(98^{th}\)&lt;/span&gt; percentile (&lt;span class=&#34;math inline&#34;&gt;\(q_{0.98}\)&lt;/span&gt;, also sometimes denoted &lt;span class=&#34;math inline&#34;&gt;\(P98\)&lt;/span&gt;) is the value such that there are 98% of the observations below it and thus 2% of the observations above it. Percentiles are often used for the weight and height of babies, giving precise information to the parents about where their child stands compared to other children of the same age.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;mode&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Mode&lt;/h2&gt;
&lt;p&gt;The mode of a series is the value that appears most often. In other words, it is the value that has the highest number of occurrences.&lt;/p&gt;
&lt;p&gt;Unlike some descriptive statistics that can only be computed for quantitative variables (the mean for instance), the mode can be computed for quantitative &lt;strong&gt;and&lt;/strong&gt; qualitative variables (see a recap of the different &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/&#34;&gt;types of variables&lt;/a&gt; if you do not remember the difference).&lt;/p&gt;
&lt;div id=&#34;quantitative-variables&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Quantitative variables&lt;/h3&gt;
&lt;p&gt;Given the height of 9 adults:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;170&lt;/em&gt;, &lt;em&gt;168&lt;/em&gt;, &lt;em&gt;171&lt;/em&gt;, &lt;em&gt;170&lt;/em&gt;, &lt;em&gt;182&lt;/em&gt;, &lt;em&gt;165&lt;/em&gt;, &lt;em&gt;170&lt;/em&gt;, &lt;em&gt;189&lt;/em&gt; and &lt;em&gt;167&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The mode is 170 because it is the most common value with 3 occurrences. All other values appear only once.&lt;/p&gt;
&lt;p&gt;Note that it is possible that a series has no mode or more than one mode:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the series &lt;em&gt;4&lt;/em&gt;, &lt;em&gt;7&lt;/em&gt;, &lt;em&gt;2&lt;/em&gt; and &lt;em&gt;10&lt;/em&gt; has no mode,&lt;/li&gt;
&lt;li&gt;the series &lt;em&gt;4&lt;/em&gt;, &lt;em&gt;2&lt;/em&gt;, &lt;em&gt;2&lt;/em&gt;, &lt;em&gt;8&lt;/em&gt;, &lt;em&gt;11&lt;/em&gt; and &lt;em&gt;11&lt;/em&gt; has two modes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Data with two modes are often called bimodal and data with more than two modes are often called multimodal, as opposed to series with one mode which are referred as unimodal.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;qualitative-variables&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Qualitative variables&lt;/h3&gt;
&lt;p&gt;Given the eye color of the 9 adults presented above:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;brown&lt;/em&gt;, &lt;em&gt;brown&lt;/em&gt;, &lt;em&gt;brown&lt;/em&gt;, &lt;em&gt;brown&lt;/em&gt;, &lt;em&gt;blue&lt;/em&gt;, &lt;em&gt;blue&lt;/em&gt;, &lt;em&gt;blue&lt;/em&gt;, &lt;em&gt;brown&lt;/em&gt; and &lt;em&gt;green&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Brown is the most frequent color, so the mode is brown.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;dispersion&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Dispersion&lt;/h1&gt;
&lt;p&gt;All previous descriptive statistics helps to get a sense of the location and position of the data. We now present the most common dispersion measures, which help to get a sense of the &lt;strong&gt;dispersion and the variability&lt;/strong&gt; of the data (to which extent a distribution is squeezed or stretched):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;range&lt;/li&gt;
&lt;li&gt;standard deviation&lt;/li&gt;
&lt;li&gt;variance&lt;/li&gt;
&lt;li&gt;interquartile range&lt;/li&gt;
&lt;li&gt;coefficient of variation&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As for location measures, we detail and compute by hand each of these statistics one by one.&lt;/p&gt;
&lt;div id=&#34;range&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Range&lt;/h2&gt;
&lt;p&gt;The range is the difference between the maximum and the minimum value:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[range = max - min\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Given the height (in cm) of our sample of 6 adults:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;188.7&lt;/em&gt;, &lt;em&gt;169.4&lt;/em&gt;, &lt;em&gt;178.6&lt;/em&gt;, &lt;em&gt;181.3&lt;/em&gt;, &lt;em&gt;179&lt;/em&gt; and &lt;em&gt;173.9&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The range is 188.7 &lt;span class=&#34;math inline&#34;&gt;\(-\)&lt;/span&gt; 169.4 &lt;span class=&#34;math inline&#34;&gt;\(=\)&lt;/span&gt; 19.3 cm. The advantage of the range is that it is extremely easy to compute it and it gives a precise idea about the “length” of the data. The disadvantage is that it relies on the two most extreme values only, so it is highly sensible to &lt;a href=&#34;https://statsandr.com/blog/outliers-detection-in-r/&#34;&gt;outliers&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;standard-deviation&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Standard deviation&lt;/h2&gt;
&lt;p&gt;The standard deviation is the most common dispersion measure in statistics. Like the mean for the location measures, if we have to present one statistics which summarizes the spread of the data, it is usually the standard deviation.&lt;/p&gt;
&lt;p&gt;As its name suggests, the standard deviation tells what is the “normal” deviation of the data. It actually computes the &lt;strong&gt;mean deviation from the global mean&lt;/strong&gt;. The larger the standard deviation, the more scattered the data are. On the contrary, the smaller the standard deviation, the more the data are centered around the mean. Below a visual representation of the standard deviation:&lt;/p&gt;
&lt;div style=&#34;text-align: center;&#34;&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand_files/standard-deviation.png&#34; alt=&#34;Standard deviation. Source: LFSAB1105 at UCLouvain&#34; style=&#34;width: 80%;&#34;&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;The standard deviation is a bit more complex than the previous statistics in the sense that there are two formulas depending on whether we face a sample or a population. A population includes all members from a specified group, all possible outcomes or measurements that are of interest. A sample consists of some observations drawn from the population, so a part or a subset of the population. For instance, the population may be “&lt;strong&gt;all&lt;/strong&gt; people living in Belgium” and the sample may be “&lt;strong&gt;some&lt;/strong&gt; people living in Belgium”. Read this article on &lt;a href=&#34;https://statsandr.com/blog/what-is-the-difference-between-population-and-sample/&#34;&gt;the difference between population and sample&lt;/a&gt; if you want to learn more.&lt;/p&gt;
&lt;div id=&#34;standard-deviation-for-a-population&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Standard deviation for a population&lt;/h3&gt;
&lt;p&gt;The standard deviation for a population, denoted &lt;span class=&#34;math inline&#34;&gt;\(\sigma\)&lt;/span&gt;, is:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\sigma = \sqrt{\frac{1}{n}\sum^n_{i = 1}(x_i - \mu)^2}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;As you can see from the formula, the standard deviation is actually the mean deviation of the data from the global mean &lt;span class=&#34;math inline&#34;&gt;\(\mu\)&lt;/span&gt;. Note the square for the difference between the observations (&lt;span class=&#34;math inline&#34;&gt;\(x_i\)&lt;/span&gt;) and the mean (&lt;span class=&#34;math inline&#34;&gt;\(\mu\)&lt;/span&gt;) to avoid that negative differences are compensated by positive differences.&lt;/p&gt;
&lt;p&gt;For the sake of easiness, imagine a population of only 3 adults (the steps are the same with a large population, the computation is just longer). Below their heights (in cm):&lt;/p&gt;
&lt;p&gt;&lt;em&gt;160.4&lt;/em&gt;, &lt;em&gt;175.8&lt;/em&gt; and &lt;em&gt;181.5&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The mean is 172.6 (rounded to 1 decimal). The standard deviation is thus:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\sigma = \sqrt{\frac{1}{3} \big[(160.4 - 172.6)^2 \\ + (175.8 - 172.6)^2 \\ + (181.5 - 172.6)^2 \big]}\]&lt;/span&gt;
&lt;span class=&#34;math display&#34;&gt;\[\sigma = 8.91\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In conclusion, the standard deviation for the heights of these 3 adults is 8.91 cm. This means that, on average, the height of the adults in this population deviates from the mean by 8.91 cm.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;standard-deviation-for-a-sample&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Standard deviation for a sample&lt;/h3&gt;
&lt;p&gt;The standard deviation for a sample is similar to the standard deviation for a population except that we divide by &lt;span class=&#34;math inline&#34;&gt;\(n -1\)&lt;/span&gt; instead of &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; and it is denoted &lt;span class=&#34;math inline&#34;&gt;\(s\)&lt;/span&gt;:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[s = \sqrt{\frac{1}{n-1}\sum^n_{i = 1}(x_i - \bar{x})^2}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Imagine now that the 3 adults presented in the previous section is a sample instead of a population:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;160.4&lt;/em&gt;, &lt;em&gt;175.8&lt;/em&gt; and &lt;em&gt;181.5&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The mean is still 172.6 (rounded to 1 decimal) since the mean is the same whether it is a population or a sample. The standard deviation is now:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[s = \sqrt{\frac{1}{3 - 1} \big[(160.4 - 172.6)^2 \\ + (175.8 - 172.6)^2 \\ + (181.5 - 172.6)^2 \big]}\]&lt;/span&gt;
&lt;span class=&#34;math display&#34;&gt;\[s = 10.92\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In conclusion, the standard deviation for the heights of these 3 adults is 10.92 cm. The interpretation is the same than for a population.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;variance&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Variance&lt;/h2&gt;
&lt;p&gt;The variance is simply the square of the standard deviation. Put it another way, the standard deviation is the square root of the variance. We also distinguish between the variance for a population and for a sample in the next sections.&lt;/p&gt;
&lt;div id=&#34;variance-for-a-population&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Variance for a population&lt;/h3&gt;
&lt;p&gt;The variance for a population, denoted &lt;span class=&#34;math inline&#34;&gt;\(\sigma^2\)&lt;/span&gt;, is:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\sigma^2 = \frac{1}{n}\sum^n_{i = 1}(x_i - \mu)^2\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;As you can see, the formula for variance is the same than for standard deviation, except that the square root is removed for the variance. Remember the heights of our population of 3 adults:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;160.4&lt;/em&gt;, &lt;em&gt;175.8&lt;/em&gt; and &lt;em&gt;181.5&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The standard deviation was 8.91 cm, so the variance of the height of these adults is &lt;span class=&#34;math inline&#34;&gt;\(8.91^2 = 79.39\)&lt;/span&gt; &lt;span class=&#34;math inline&#34;&gt;\(cm^2\)&lt;/span&gt; (see below why the unit of a variance is &lt;span class=&#34;math inline&#34;&gt;\(unit^2\)&lt;/span&gt;).&lt;/p&gt;
&lt;p&gt;If you did not know the standard deviation of the population and needed to compute the variance of the population by hand, here is how:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\sigma^2 = \frac{1}{3} \big[(160.4 - 172.6)^2 \\ + (175.8 - 172.6)^2 \\ + (181.5 - 172.6)^2 \big] \\ = 79.43\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;(The difference with the above result is due to rounding.)&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;variance-for-a-sample&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Variance for a sample&lt;/h3&gt;
&lt;p&gt;Again, the variance for a sample is similar to the variance for a population except that we divide by &lt;span class=&#34;math inline&#34;&gt;\(n - 1\)&lt;/span&gt; instead of &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; and it is denoted &lt;span class=&#34;math inline&#34;&gt;\(s^2\)&lt;/span&gt;:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[s^2 = \frac{1}{n-1}\sum^n_{i = 1}(x_i - \bar{x})^2\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Imagine again that the 3 adults in the previous section is a sample instead of a population:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;160.4&lt;/em&gt;, &lt;em&gt;175.8&lt;/em&gt; and &lt;em&gt;181.5&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The standard deviation for this sample was 10.92 cm, so the variance of the height of these adults is &lt;span class=&#34;math inline&#34;&gt;\(10.92^2 = 119.25\)&lt;/span&gt; &lt;span class=&#34;math inline&#34;&gt;\(cm^2\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;If you did not know the standard deviation of the sample and needed to compute the variance of the sample by hand, here is how:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[s^2 = \frac{1}{3 - 1} \big[(160.4 - 172.6)^2 \\ + (175.8 - 172.6)^2 \\ + (181.5 - 172.6)^2 \big] \\ = 119.15\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;(The difference with the above result is due to rounding.)&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;standard-deviation-vs.-variance&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Standard deviation vs. variance&lt;/h2&gt;
&lt;p&gt;Standard deviation and variance are often used interchangeably and both quantify the spread of a given dataset by measuring how far the observations are from their mean. However, the standard deviation can be more easily interpreted because the unit for the standard deviation is the same than the unit of measurement of the data (while it is the &lt;span class=&#34;math inline&#34;&gt;\(unit^2\)&lt;/span&gt; for the variance).&lt;/p&gt;
&lt;p&gt;Following our example of adult heights in cm, the standard deviation is measured in cm while the variance is measured in &lt;span class=&#34;math inline&#34;&gt;\(cm^2\)&lt;/span&gt;. The fact that the standard deviation keeps the same unit than the initial unit of measurement makes it more interpretable and thus more often used in practice.&lt;/p&gt;
&lt;div id=&#34;notations&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Notations&lt;/h3&gt;
&lt;p&gt;For completeness, below a table showing the different notations for variance and standard deviation in case of population and sample:&lt;/p&gt;
&lt;center&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr class=&#34;header&#34;&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th align=&#34;center&#34;&gt;Population&lt;/th&gt;
&lt;th align=&#34;right&#34;&gt;Sample&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td&gt;Standard deviation&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(\sigma\)&lt;/span&gt;&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(s\)&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td&gt;Variance&lt;/td&gt;
&lt;td align=&#34;center&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(\sigma^2\)&lt;/span&gt;&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(s^2\)&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;interquartile-range&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Interquartile range&lt;/h2&gt;
&lt;p&gt;Remember the first &lt;span class=&#34;math inline&#34;&gt;\(q_{0.25}\)&lt;/span&gt; and third quartile &lt;span class=&#34;math inline&#34;&gt;\(q_{0.75}\)&lt;/span&gt; presented earlier (see this &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand/#st-and-3rd-quartiles&#34;&gt;section&lt;/a&gt;). The interquartile range is another measure of dispersion of the data, using the quartiles. It is the difference between the third and first quartile:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[IQR = q_{0.75} - q_{0.25}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Considering the height of the 9 adults presented in the section about the first and third quartile:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;170.2&lt;/em&gt;, &lt;em&gt;181.5&lt;/em&gt;, &lt;em&gt;188.9&lt;/em&gt;, &lt;em&gt;163.9&lt;/em&gt;, &lt;em&gt;166.4&lt;/em&gt;, &lt;em&gt;163.7&lt;/em&gt;, &lt;em&gt;160.4&lt;/em&gt;, &lt;em&gt;175.8&lt;/em&gt; and &lt;em&gt;181.5&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The first quartile was 163.9 cm and the third quartile was 181.5 cm. The IQR is thus:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[IQR = 181.5 - 163.9 = 17.6\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In conclusion, the interquartile range is 17.6 cm. The interquartile range is actually the range (since it is the difference between a higher and a lower value) of the middle data. The graph below may help to understand better the IQR and the quartiles:&lt;/p&gt;
&lt;div style=&#34;text-align: center;&#34;&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/descriptive-statistics-by-hand_files/IQR-quartiles.png&#34; alt=&#34;IQR, first and third quartile. Source: LFSAB1105 at UCLouvain&#34; style=&#34;width: 80%;&#34;&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;coefficient-of-variation&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Coefficient of variation&lt;/h2&gt;
&lt;p&gt;The last dispersion measure is the coefficient of variation. The coefficient of variation, denoted &lt;span class=&#34;math inline&#34;&gt;\(CV\)&lt;/span&gt;, is the standard deviation divided by the mean. Formally:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[CV = \frac{s}{\bar{x}}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Consider the height of a sample of 4 adults:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;163.7&lt;/em&gt;, &lt;em&gt;160.4&lt;/em&gt;, &lt;em&gt;175.8&lt;/em&gt; and &lt;em&gt;181.5&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The mean is &lt;span class=&#34;math inline&#34;&gt;\(\bar{x} =\)&lt;/span&gt; 170.35 cm and the standard deviation is &lt;span class=&#34;math inline&#34;&gt;\(s =\)&lt;/span&gt; 9.95 cm. (Find the same values as an exercise!) The coefficient of variation is&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[CV = \frac{9.95 \text{ cm}}{170.35 \text{ cm}} = 0.058 = 5.8\%\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In conclusion, the coefficient of variation is 5.8%. Note that, as a rule of thumb, a coefficient of variation greater than 15% usually means that the data are &lt;strong&gt;heterogeneous&lt;/strong&gt; while a coefficient of variation equal to or less than 15% means that the data are &lt;strong&gt;homogeneous&lt;/strong&gt;. Given that the coefficient of variation equals 5.8% in this case, we can conclude that these 4 adults are homogeneous in terms of height.&lt;/p&gt;
&lt;p&gt;Note that the coefficient of variation for a population follows the same formula, except that notations for the mean and the standard deviation differ:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[CV = \frac{\sigma}{\mu}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The interpretation is also the same as for a sample.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;coefficient-of-variation-vs.-standard-deviation&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Coefficient of variation vs. standard deviation&lt;/h2&gt;
&lt;p&gt;Although the coefficient of variation is rather unknown to the public, it is, in fact, worth presenting when making descriptive statistics.&lt;/p&gt;
&lt;p&gt;The standard deviation should always be understood in the context of the mean of the data and is dependent on its unit. The standard deviation has the advantage that it tells by how far on average the data is from the mean in terms of unit in which the data has been measured. Standard deviation is useful when considering variables with same units and approximately same means. However, standard deviation becomes less useful when comparing variables with different units or widely different means. For instance, a variable with a standard deviation of 10 cm cannot be compared to a variable with a standard deviation of 12€ to conclude which one of the two is the most dispersed.&lt;/p&gt;
&lt;p&gt;The coefficient of variation is a ratio of two statistics with the same units. It has thus no unit and is independent of the unit in which the data has been measured. Being unit-free, coefficients of variation computed on datasets or variables with different units or widely different means can be compared to conclude, in fine, which data or variables is more (or less) dispersed. For instance, consider a sample of 10 women with their heights in cm and their salaries in €. We cannot compare the dispersion of their weights with the dispersion of their salaries because it is not measured on the same unit/scale. Now suppose that the coefficients of variation are 0.032 and 0.061 respectively for the height and the salary. Based on that, we can conclude that, relative to their respective average, their salaries vary more than their heights for these women. This is the case because the coefficient of variation is larger for the salary compared to the coefficient variation for the height, and a coefficient of variation has no unit (it is a ratio).&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;conclusion&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;Remember that descriptive statistics are useful to describe and present a series of observations in a concise and informative way. There are two families of descriptive statistics: location and dispersion measures. Location measures give information about the position of the data, whereas dispersion measures give information about the variability of the data.&lt;/p&gt;
&lt;p&gt;We showed how to compute the most common descriptive statistics by hand with concrete examples. We also discussed differences between some measures and when it is more appropriate to use one or the other depending on the context and the data at hand.&lt;/p&gt;
&lt;p&gt;This concludes a relatively long article, thanks for reading! If you would like to learn how to compute these measures in R, read the article “&lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/&#34;&gt;Descriptive statistics in R&lt;/a&gt;”. A book I recommend for further reading is “&lt;a href=&#34;https://capitaloneshopping.com/p/statistics/VWP5CXD9KP&#34; target=&#34;_blank&#34;&gt;The Art of Statistics&lt;/a&gt;” by David Spiegelhalter. It is a great book for beginners in statistics and covers a wide range of topics.&lt;/p&gt;
&lt;p&gt;As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;footnotes footnotes-end-of-document&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;This image and the following ones are taken from the LFSAB1105 course syllabus at UCLouvain.&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
    </item>
    
  </channel>
</rss>