<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/world-income-inequality-and-murder/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://staff.math.su.se/hoehle/blog/2018/07/09/gini.html"> Theory meets practice...</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Abstract:</h2>
<p>We follow up on last weeks post on using Gapminder data to study the world’s income distribution. In order to assess the inequality of the distribution we compute the Gini coefficient for the world’s income distribution by Monte Carlo approximation and visualize the result as a time series. Furthermore, we animate the association between Gini coefficient and homicide rate per country using the new version of <code>gganimate</code>.</p>
<p><center><br>
<img src="https://i0.wp.com/staff.math.su.se/hoehle/blog/figure/source/2018-07-09-gini/MURDERGINIPLOT-1.png?w=456" data-recalc-dims="1"><br>
</center></p>
<p> <a rel="nofollow" href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank"><img alt="Creative Commons License" src="https://i1.wp.com/i.creativecommons.org/l/by-sa/4.0/88x31.png?w=456&ssl=1" data-recalc-dims="1"></a> This work is licensed under a <a rel="nofollow" href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank">Creative Commons Attribution-ShareAlike 4.0 International License</a>. The markdown+Rknitr source code of this blog is available under a <a rel="nofollow" href="https://www.gnu.org/licenses/gpl-3.0.html" target="_blank">GNU General Public License (GPL v3)</a> license from github.</p>
<h2>Introduction</h2>
<p>One of the main messages of the Chapter ‘The Gap Instinct’ of the book <a rel="nofollow" href="https://www.gapminder.org/factfulness/" target="_blank"><em>Factfulness</em></a> is that there is no justification of the ‘we’ and ‘them’ classification of countries anymore, because they have converged towards the same levels in key indicators such as life expectancy, child mortality, births per female. The difference between countries is, hence, not as many imagine it to be: there is less inequality and no real gap. While reading, I became curious about the following: <strong>what if countries became more equal, but simultaneously inequality within countries became bigger?</strong> This was also indirectly a Disqus comment by F. Weidemann to the post <a rel="nofollow" href="http://staff.math.su.se/hoehle/blog/2018/07/02/factfulness.html" target="_blank"><em>Factfulness: Building Gapminder Income Mountains</em></a>. Aim of the present post is to investigate this hypothesis using the Gapminder data. Furthermore, we use the country specific Gini coefficients to investigate the association with the number of homicides in the country.</p>
<h2>Gini coefficient</h2>
<p>There are different ways to measure income inequality, both in terms of which response you consider and which statistical summary you compute for it. Not going into the details of these discussion we use the GDP/capita in <a rel="nofollow" href="https://en.wikipedia.org/wiki/Purchasing_power_parity" target="_blank">Purchasing Power Parities (PPP)</a> measured in so called international dollars (fixed prices 2011). In other words, comparison between years and countries are possible, because the response is adjusted for inflation and differences in price of living.</p>
<p>The <a rel="nofollow" href="https://en.wikipedia.org/wiki/Gini_coefficient" target="_blank"><strong>Gini coefficient</strong></a> is a statistical measure to quantify inequality. In what follows we shall focus on computing the Gini coefficient for a continuous probability distribution with a known probability density function. Let the probability density function of the non-negative continuous income distribution be defined by <span>\(f\)</span>, then the Gini coefficient is given as <strong>half the relative mean difference</strong>:</p>
<p><span>\[<br>
G<br>
= \frac{1}{2\mu}\int_0^\infty \int_0^\infty |x-y| \> f(x) \> f(y) \><br>
dx\> dy, \quad\text{where}\quad \mu = \int_{0}^\infty x\cdot f(x) dx.<br>
\]</span></p>
<p>Depending on <span>\(f\)</span> it might be possible to <a rel="nofollow" href="https://en.wikipedia.org/wiki/Gini_coefficient#Continuous_probability_distribution" target="_blank">solve these integrals analytically</a>, however, a straightforward computational approach is to use Monte Carlo sampling – as we shall see shortly. Personally, I find the above relative mean difference presentation of the Gini index much more intuitive than the area argument using the Lorenz curve. From the eqution it also becomes clear that the Gini coefficient is invariant to multiplicative changes in the income: if everybody increases their income by factor <span>\(k>0\)</span> then the Gini coefficient remains the same, because <span>\(|k x – k y| = k | x – y|\)</span> and <span>\(E(k \cdot X) = k \mu\)</span>.</p>
<p>The above formula indirectly also states how to compute the Gini coefficient for a discrete sample of size <span>\(n\)</span> and with incomes <span>\(x_1,\ldots, x_n\)</span>: <span>\[<br>
G = \frac{\sum_{i=1}^n \sum_{j=1}^n |x_i –<br>
x_j| \frac{1}{n} \frac{1}{n}}{2 \sum_{i=1}^n x_i \frac{1}{n}} =<br>
\frac{\sum_{i=1}^n \sum_{j=1}^n |x_i – x_j|}{2 n \sum_{j=1}^n x_j}.<br>
\]</span></p>
<h4>Approximating the integral by Monte Carlo</h4>
<p>If one is able to easily sample from <span>\(f\)</span> then can instead of solving the integral analytically use <span>\(k\)</span> pairs <span>\((x,y)\)</span> both drawn at random from <span>\(f\)</span> to approximate the double integral:</p>
<p><span>\[<br>
G \approx \frac{1}{2\mu K} \sum_{k=1}^K |x_k – y_k|, \quad\text{where}\quad<br>
x_k \stackrel{\text{iid}}{\sim} f \text{ and } y_k \stackrel{\text{iid}}{\sim} f,<br>
\]</span> where for our mixture model <span>\[<br>
\mu = \sum_{i=1}^{192} w_i \> E(X_i) = \sum_{i=1}^{192} w_i \exp\left(\mu_i + \frac{1}{2}\sigma_i^2\right).<br>
\]</span> This allows us to compute <span>\(G\)</span> even in case of a complex <span>\(f\)</span> such as the log-normal mixture distribution. As always, the larger <span>\(K\)</span> is, the better the Monte Carlo approximation is.</p>
<div>
<pre><code>##Precision of Monte Carlo approx is controlled by the number of samples
n &lt;-<span> </span><span>1e6</span>

##Compute Gini index of world income per year
gini_year &lt;-<span> </span>gm <span>%>%</span><span> </span><span>group_by</span>(year) <span>%>%</span><span> </span><span>do</span>({
  x &lt;-<span> </span><span>rmix</span>(n, <span>meanlog=</span>.<span>$</span>meanlog, <span>sdlog=</span> .<span>$</span>sdlog, <span>w=</span>.<span>$</span>w)
  y &lt;-<span> </span><span>rmix</span>(n, <span>meanlog=</span>.<span>$</span>meanlog, <span>sdlog=</span> .<span>$</span>sdlog, <span>w=</span>.<span>$</span>w)
  int &lt;-<span> </span><span>mean</span>( <span>abs</span>(x<span>-</span>y) )
  mu &lt;-<span> </span><span>sum</span>(<span>exp</span>( .<span>$</span>meanlog <span>+</span><span> </span><span>1</span><span>/</span><span>2</span> <span>*</span><span> </span>.<span>$</span>sdlog<span>^</span><span>2</span>) <span>*</span><span> </span>.<span>$</span>w)
  <span>data.frame</span>(<span>gini_all_mc=</span><span>1</span><span>/</span>(<span>2</span><span>*</span>mu)<span>*</span>int,
             <span>country_gini=</span><span>gini</span>(.<span>$</span>gdp<span>*</span>.<span>$</span>population))
}) <span>%>%</span>
<span> </span><span>rename</span>(<span>`</span><span>World Population GDP/capita</span><span>`</span>=gini_all_mc, <span>`</span><span>Country GDP/capita</span><span>`</span>=country_gini)

##Convert to long format
gini_ts &lt;-<span> </span>gini_year <span>%>%</span><span> </span><span>gather</span>(<span>key=</span><span>&quot;type&quot;</span>, <span>value=</span><span>&quot;gini&quot;</span>, <span>-</span>year)</code></pre>
</div>
<h3>Results</h3>
<p>We start by showing the country specific Gini coefficient per year since 1950 for a somewhat arbitrary selection of countries. The dashed black line shows the mean Gini coefficient each year over all 192 countries in the dataset.</p>
<p><img src="https://i1.wp.com/staff.math.su.se/hoehle/blog/figure/source/2018-07-09-gini/GINITSSELECTED-1.png?w=456" data-recalc-dims="1"></p>
<p>In addition, we now show the Monte Carlo computed Gini coefficient for the world’s income distribution given as a log-normal mixture with 192 components.</p>
<p><img src="https://i0.wp.com/staff.math.su.se/hoehle/blog/figure/source/2018-07-09-gini/PLOTGINI-1.png?w=456" data-recalc-dims="1"></p>
<p>We notice that the Gini coefficient taken over the 192 countries’ GDP/capita remains very stable over time. This, however, does not take the large differences in populations between countries into account. A fairer measure is thus the Gini coefficient for the world’s income distribution. We see that this Gini coefficient increased over time until peaking around 1990. From then on it has declined. However, the pre-1950 Gini coefficients are rather guesstimates as stated by Gapminder, hence, we zoom in on the period from 1970, because data are more reliable from this point on.</p>
<p><img src="https://i0.wp.com/staff.math.su.se/hoehle/blog/figure/source/2018-07-09-gini/PLOTGINIFROM1970-1.png?w=456" data-recalc-dims="1"></p>
<h2>Gini coefficient and Homicide Rate</h2>
<p>Finally, we end the post by illustrating the association between the Gini coefficient and the homicide rate per country using a 2D scatterplot over the years. The Gapminder data download page also contains <a rel="nofollow" href="https://docs.google.com/spreadsheet/pub?key=tZgPgT_sx3VdAuyDxEzenYA&output=xlsx" target="_blank">data</a> for this for the years 1950- 2005. Unfortunately, no data for more recent years are available from the Gapminder homepage, but the plot shown below is the situation in <code>r show_year</code> with a log-base-10 y-axis for the homicide rates. For each of the four Gapminder regional groups we also fit a simple linear regression line to the points of all countries within the region. Findings such as <span data-cites="fajnzylber_etal2002">Fajnzylber, Lederman, and Loayza (2002)</span> suggest that there is a strong positive correlation between Gini coefficient and homicide rate, but we see from the plot that there are regional differences and of course correlation is not causality. Explanations for this phenomena are <a rel="nofollow" href="https://www.theguardian.com/us-news/2017/dec/08/income-inequality-murder-homicide-rates" target="_blank">debated</a> and beyond the scope of this post.</p>
<p><img src="https://i0.wp.com/staff.math.su.se/hoehle/blog/figure/source/2018-07-09-gini/MURDERGINIPLOT-1.png?w=456" data-recalc-dims="1"></p>
<p>We extend the plots to all years 1950-2005. Unfortunately, not all countries are available every year – so we only plot the available countries each year. This means that many African countries are missing from the animation. An improvement would be to try some form of linear interpolation. Furthermore, for the sake of simplicity of illustration, we fix countries with a reported murder rate of zero in a given year (happens for example for Cyprus, Iceland, Fiji in some years) to 0.01 per 100,000 population. This can be nicely animated using the new version of the <a rel="nofollow" href="https://github.com/thomasp85/gganimate" target="_blank"><code>gganimate</code></a> pkg by <a rel="nofollow" href="https://twitter.com/thomasp85" target="_blank">Thomas Lin Pedersen</a>.</p>
<div>
<pre><code>## New version of gganimate. Not on CRAN yet.
## devtools::install_github('thomasp85/gganimate')
<span>require</span>(gganimate)

p &lt;-<span> </span><span>ggplot</span>(gm2_nozero, <span>aes</span>(<span>x=</span>gini, <span>y=</span>murder_rate,<span>size=</span>population, <span>color=</span>Region)) <span>+</span>
<span> </span><span>geom_point</span>() <span>+</span>
<span> </span><span>scale_x_continuous</span>(<span>labels=</span>scales<span>::</span>percent) <span>+</span>
<span> </span><span>scale_y_continuous</span>(<span>trans=</span><span>&quot;log10&quot;</span>,
                     <span>breaks =</span> <span>trans_breaks</span>(<span>&quot;log10&quot;</span>, <span>function</span>(x) <span>10</span><span>^</span>x,<span>n=</span><span>5</span>),
                     <span>labels =</span> <span>trans_format</span>(<span>&quot;log10&quot;</span>, <span>function</span>(x) <span>ifelse</span>(x<span></span><span>0</span>, <span>sprintf</span>(<span>paste0</span>(<span>&quot;%.&quot;</span>,<span>ifelse</span>(<span>is.na</span>(x),<span>&quot;0&quot;</span>,<span>round</span>(<span>abs</span>(x))),<span>&quot;f&quot;</span>),<span>10</span><span>^</span>x), <span>sprintf</span>(<span>&quot;%.0f&quot;</span>,<span>10</span><span>^</span>x)))) <span>+</span>
<span> </span><span>geom_smooth</span>(<span>se=</span><span>FALSE</span>, <span>method=</span><span>&quot;lm&quot;</span>, <span>formula=</span>y<span>~</span>x) <span>+</span>
<span> </span><span>geom_text</span>(<span>data=</span>gm2, <span>aes</span>(<span>x=</span>gini,<span>y=</span>murder_rate, <span>label=</span>country), <span>vjust=</span><span>-</span><span>0.9</span>, <span>show.legend=</span><span>FALSE</span>) <span>+</span>
<span> </span><span>ylab</span>(<span>&quot;Murder rate (per 100,000 population)&quot;</span>) <span>+</span>
<span> </span><span>xlab</span>(<span>&quot;Gini coefficient (in %)&quot;</span>) <span>+</span>
<span> </span><span>guides</span>(<span>size=</span><span>FALSE</span>) <span>+</span>
<span> </span><span>labs</span>(<span>title =</span> <span>'Year: {frame_time}'</span>) <span>+</span>
<span> </span><span>transition_time</span>(year) <span>+</span>
<span> </span><span>ease_aes</span>(<span>'linear'</span>)

<span>animate</span>(p, <span>nframes=</span><span>length</span>(<span>unique</span>(gm2<span>$</span>year)), <span>fps=</span><span>4</span>, <span>width=</span><span>800</span>, <span>height=</span><span>400</span>, <span>res=</span><span>100</span>)</code></pre>
</div>
<p><img src="https://i2.wp.com/staff.math.su.se/hoehle/blog/figure/source/2018-07-09-gini/ANIMATE-1.gif?w=456" data-recalc-dims="1"></p>
<h2>Discussion</h2>
<p>Based on the available Gapminder data we showed that in the last 25 years the Gini coefficient for the world’s income distribution has decreased. For several individual countries opposite dynamics are, however, observed. One particular concern is the share that the richest 1% have of the overall wealth: <a rel="nofollow" href="https://www.theguardian.com/inequality/2017/nov/14/worlds-richest-wealth-credit-suisse" target="_blank">more than 50%</a>.</p>
<h2>Literature</h2>
<div>
<div>
<p>Fajnzylber, P., D. Lederman, and N. Loayza. 2002. “Inequality and Violent Crime.” <em>Journal of Law and Economics</em> 45 (April). <a rel="nofollow" href="http://siteresources.worldbank.org/DEC/Resources/Crime&amp;Inequality.pdf" target="_blank">http://siteresources.worldbank.org/DEC/Resources/Crime&amp;Inequality.pdf</a>.</p>
</div>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://staff.math.su.se/hoehle/blog/2018/07/09/gini.html"> Theory meets practice...</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/L9eR75NYBYI" height="1"></a></div>