<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/estimating-gini-coefficient-when-we-only-have-mean-income-by-decile-by-ellis2013nz/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2017/08/19/quantiles-gini"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Income inequality data</h2>
<p>Ideally the Gini coefficient to estimate inequality is based on original household survey data with hundreds or thousands of data points. Often this data isn’t available due to access restrictions from privacy or other concerns, and all that is published is some kind of aggregate measure. Some aggregations include the income at the 80th percentile divided by that at the 20th (or 90 and 10); the number of people at the top of the distribution whose combined income is equal to that of everyone else; or the income of the top 1% as a percentage of all income. I wrote a little more about this in <a rel="nofollow" href="http://ellisp.github.io/blog/2015/09/12/inequality-stats-distributions" target="_blank">one of my earliest blog posts</a>.</p>
<p>One way aggregated data are sometimes presented is as the mean income in each decile or quintile. This is not the same as the actual quantile values themselves, which are the <em>boundary</em> between categories. The 20th percentile is the value of the 20/100th person when they are lined up in increasing order, whereas the mean income of the first quintile is the mean of all the incomes of a “bin” of everyone from 0/100 to 20/100, when lined up in order.</p>
<p>To explore estimating Gini coefficients from this type of binned data I used data from the wonderful <a rel="nofollow" href="https://www.gc.cuny.edu/CUNY_GC/media/LISCenter/brankoData/LaknerMilanovic2013WorldPanelIncomeDistributionLMWPIDDescription.pdf" target="_blank">Lakner-Milanovic World Panel Income Distribution database</a>, which is available for free download. This useful collection contains the mean income by decile bin of many countries from 1985 onwards – the result of some careful and doubtless very tedious work with household surveys from around the world. This is an amazing dataset, and amongst other purposes it can be used (as <a rel="nofollow" href="https://en.wikipedia.org/wiki/Branko_Milanovi%C4%87" target="_blank">Milanovic and co-authors have pioneered</a> dating back to his World Bank days) in combination with population numbers to estimate “global inequality”, treating everyone on the planet as part of a single economic community regardless of national boundaries. But that’s for another day.</p>
<p>Here’s R code to download the data (in Stata format) and grab the first ten values, which happen to represent Angloa in 1995. These particular data are based on consumption, which in poorer economies is often more sensible to measure than income:</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>foreign</span><span>)</span><span> </span><span># for importing Stata files
</span><span>library</span><span>(</span><span>actuar</span><span>)</span><span> </span><span># for access to the Burr distribution
</span><span>library</span><span>(</span><span>acid</span><span>)</span><span>
</span><span>library</span><span>(</span><span>forcats</span><span>)</span><span>
</span><span># Data described at https://www.gc.cuny.edu/CUNY_GC/media/LISCenter/brankoData/LaknerMilanovic2013WorldPanelIncomeDistributionLMWPIDDescription.pdf
# The database has been created specifically for the
# paper “Global Income Distribution: From the Fall of the Berlin Wall to the Great Recession”,
# World Bank Policy Research Working Paper No. 6719, December 2013, published also in World
# Bank Economic Review (electronically available from 12 August 2015). </span><span>download.file</span><span>(</span><span>&quot;https://wfs.gc.cuny.edu/njohnson/www/BrankoData/LM_WPID_web.dta&quot;</span><span>,</span><span> </span><span>mode</span><span> </span><span>=</span><span> </span><span>&quot;wb&quot;</span><span>,</span><span> </span><span>destfile</span><span> </span><span>=</span><span> </span><span>&quot;LM_WPID_web.dta&quot;</span><span>)</span><span>
</span><span>wpid</span><span> </span><span>&lt;-</span><span> </span><span>read.dta</span><span>(</span><span>&quot;LM_WPID_web.dta&quot;</span><span>)</span><span>
</span><span># inc_con whether survey is income or consumption
# group income decline group 1 to 10
# RRinc is average per capita income of the decile in 2005 PPP
</span><span>
</span><span># the first 10 rows are Angola in 1995, so let's experiment with them
</span><span>angola</span><span> </span><span>&lt;-</span><span> </span><span>wpid</span><span>[</span><span>1</span><span>:</span><span>10</span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;RRinc&quot;</span><span>,</span><span> </span><span>&quot;group&quot;</span><span>)]</span></code></pre>
</figure>
<p>Here’s the resulting 10 numbers. N</p>
<p><img width="100%" src="http://ellisp.github.io/img/0107-angola-quantiles.svg"></p>
<p>And this is the Lorenz curve:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0107-angola-lorenz.svg"></p>
<p>Those graphics were drawn with this code:</p>
<figure>
<pre><code data-lang="r"><span>ggplot</span><span>(</span><span>angola</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>group</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>RRinc</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Mean consumption by decile in Angola in 1995&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Annual consumption for each decile group&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>dollar</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;Decile group&quot;</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>10</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: Lakner/Milanovic World Panel Income Distribution data&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>panel.grid.minor</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>())</span><span>
</span><span>angola</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>group</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>cum_inc_prop</span><span> </span><span>=</span><span> </span><span>cumsum</span><span>(</span><span>RRinc</span><span>)</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>RRinc</span><span>),</span><span>
</span><span>pop_prop</span><span> </span><span>=</span><span> </span><span>group</span><span> </span><span>/</span><span> </span><span>max</span><span>(</span><span>group</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>pop_prop</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>cum_inc_prop</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_ribbon</span><span>(</span><span>aes</span><span>(</span><span>ymax</span><span> </span><span>=</span><span> </span><span>pop_prop</span><span>,</span><span> </span><span>ymin</span><span> </span><span>=</span><span> </span><span>cum_inc_prop</span><span>),</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.2</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_abline</span><span>(</span><span>intercept</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>slope</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Cumulative proportion of population&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Cumulative proportion of consumption&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: Lakner/Milanovic World Panel Income Distribution data&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Inequality in Angola in 1995&quot;</span><span>,</span><span>
</span><span>&quot;Lorenz curve based on binned decile mean consumption&quot;</span><span>)</span></code></pre>
</figure>
<h2>Calculating Gini directly from deciles?</h2>
<p>Now, I could just treat these 10 deciles as a sample of 10 representative people (each observation after all represents exactly 10% of the population) and calculate the Gini coefficient directly. But my hunch was that this would underestimate inequality, because of the straight lines in the Lorenz curve above which are a simplification of the real, more curved, reality.</p>
<p>To investigate this issue, I started by creating a known population of 10,000 income observations from a <a rel="nofollow" href="https://en.wikipedia.org/wiki/Burr_distribution" target="_blank">Burr distribution</a>, which is a flexible, continuous non-negative distribution often used to model income. That looks like this:</p>
<figure>
<pre><code data-lang="r"><span>population</span><span> </span><span>&lt;-</span><span> </span><span>rburr</span><span>(</span><span>10000</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>3</span><span>,</span><span> </span><span>3</span><span>)</span><span>
</span><span>par</span><span>(</span><span>bty</span><span> </span><span>=</span><span> </span><span>&quot;l&quot;</span><span>,</span><span> </span><span>font.main</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>density</span><span>(</span><span>population</span><span>),</span><span> </span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Burr(1,3,3) distribution&quot;</span><span>)</span></code></pre>
</figure>
<p><img width="100%" src="http://ellisp.github.io/img/0107-burr.svg"></p>
<p>Then I divided the data up into between 2 and 100 bins, took the means of the bins, and calculated the Gini coefficient of the bins. Doing this for 10 bins is the equivalent of calculating a Gini coefficient directly from decile data such as in the Lakner-Milanovic dataset. I got this result, which shows, that when you have the means of 10 bins, you are underestimating inequality slightly:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0107-ginis-from-deciles.svg"></p>
<p>Here’s the code for that little simulation. I make myself a little function to bin data and return the mean values of the bins in a tidy data frame, which I’ll need for later use too:</p>
<figure>
<pre><code data-lang="r"><span>#' Quantile averages
#' #' Mean value in binned groups
#' #' @param y numeric vector to provide summary statistics on
#' @param len number of bins to calculate means for
#' @details this is different from producing the actual quantiles; it is the mean value of y within each bin.
</span><span>bin_avs</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>y</span><span>,</span><span> </span><span>len</span><span> </span><span>=</span><span> </span><span>10</span><span>){</span><span>
</span><span># argument checks:
</span><span> </span><span>if</span><span>(</span><span>class</span><span>(</span><span>y</span><span>)</span><span> </span><span>!=</span><span> </span><span>&quot;numeric&quot;</span><span>){</span><span>stop</span><span>(</span><span>&quot;y should be numeric&quot;</span><span>)</span><span> </span><span>}</span><span>
</span><span>if</span><span>(</span><span>length</span><span>(</span><span>y</span><span>)</span><span> </span><span></span><span> </span><span>len</span><span>){</span><span>stop</span><span>(</span><span>&quot;y should be longer than len&quot;</span><span>)}</span><span>
</span><span># calculation:
</span><span> </span><span>y</span><span> </span><span>&lt;-</span><span> </span><span>sort</span><span>(</span><span>y</span><span>)</span><span>
</span><span>bins</span><span> </span><span>&lt;-</span><span> </span><span>cut</span><span>(</span><span>y</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>quantile</span><span>(</span><span>y</span><span>,</span><span> </span><span>probs</span><span> </span><span>=</span><span> </span><span>seq</span><span>(</span><span>0</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>length.out</span><span> </span><span>=</span><span> </span><span>len</span><span> </span><span>+</span><span> </span><span>1</span><span>)))</span><span>
</span><span>tmp</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>bin_number</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>len</span><span>,</span><span>
</span><span>bin_breaks</span><span> </span><span>=</span><span> </span><span>levels</span><span>(</span><span>bins</span><span>),</span><span>
</span><span>mean</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>tapply</span><span>(</span><span>y</span><span>,</span><span> </span><span>bins</span><span>,</span><span> </span><span>mean</span><span>)))</span><span>
</span><span>return</span><span>(</span><span>tmp</span><span>)</span><span>
</span><span>}</span><span>
</span><span>ginis</span><span> </span><span>&lt;-</span><span> </span><span>numeric</span><span>(</span><span>99</span><span>)</span><span>
</span><span>for</span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>99</span><span>){</span><span>
</span><span>ginis</span><span>[</span><span>i</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>weighted.gini</span><span>(</span><span>bin_avs</span><span>(</span><span>population</span><span>,</span><span> </span><span>len</span><span> </span><span>=</span><span> </span><span>i</span><span> </span><span>+</span><span> </span><span>1</span><span>)</span><span>$</span><span>mean</span><span>)</span><span>$</span><span>Gini</span><span>
</span><span>}</span><span>
</span><span>ginis_df</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>
</span><span>number_bins</span><span> </span><span>=</span><span> </span><span>2</span><span>:</span><span>100</span><span>,</span><span>
</span><span>gini</span><span> </span><span>=</span><span> </span><span>ginis</span><span>
</span><span>)</span><span>
</span><span>ginis_df</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>number_bins</span><span> </span><span></span><span> </span><span>11</span><span> </span><span>|</span><span> </span><span>round</span><span>(</span><span>number_bins</span><span> </span><span>/</span><span> </span><span>10</span><span>)</span><span> </span><span>==</span><span> </span><span>number_bins</span><span> </span><span>/</span><span> </span><span>10</span><span>,</span><span> </span><span>number_bins</span><span>,</span><span> </span><span>&quot;&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>number_bins</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>gini</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_text</span><span>(</span><span>aes</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>label</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Number of bins&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Gini coefficient estimated from means within bins&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Estimating Gini coefficient from binned mean values of a Burr distribution population&quot;</span><span>,</span><span>
</span><span>paste0</span><span>(</span><span>&quot;Correct Gini is &quot;</span><span>,</span><span> </span><span>round</span><span>(</span><span>weighted.gini</span><span>(</span><span>population</span><span>)</span><span>$</span><span>Gini</span><span>,</span><span> </span><span>3</span><span>),</span><span> </span><span>&quot;. Around 25 bins needed for a really good estimate.&quot;</span><span>))</span></code></pre>
</figure>
<h2>A better method for Gini from deciles?</h2>
<p>Maybe I should have stopped there; after all, there is hardly any difference between 0.32 and 0.34; probably much less than the sampling error from the survey. But I wanted to explore if there were a better way. The method I chose was to:</p>
<ul>
<li>choose a log-normal distribution that would generate (close to) the 10 decile averages we have;</li>
<li>simulate individual-level data from that distribution; and</li>
<li>estimate the Gini coefficient from that simulated data.</li>
</ul>
<p>I also tried this with a Burr distribution but the results were very unstable. The log-normal approach was quite good at generating data with means of 10 bins very similar to the original data, and gave plausible values of Gini coefficient just slightly higher than when calculated directly of the bins’ means.</p>
<p>Here’s how I did that:</p>
<figure>
<pre><code data-lang="r"><span># algorithm will be iterative
# 1. assume the 10 binned means represent the following quantiles: 0.05, 0.15, 0.25 ... 0.65, 0.75, 0.85, 0.95
# 2. pick the best lognormal distribution that fits those 10 quantile values. # Treat as a non-linear optimisation problem and solve with `optim()`.
# 3. generate data from that distribution and work out what the actual quantiles are
# 4. repeat 2, with these actual quantiles
</span><span>
</span><span>n</span><span> </span><span>&lt;-</span><span> </span><span>10000</span><span>
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>angola</span><span>$</span><span>RRinc</span><span>
</span><span>fn2</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>params</span><span>)</span><span> </span><span>{</span><span>
</span><span>sum</span><span>((</span><span>x</span><span> </span><span>-</span><span> </span><span>qlnorm</span><span>(</span><span>p</span><span>,</span><span> </span><span>params</span><span>[</span><span>1</span><span>],</span><span> </span><span>params</span><span>[</span><span>2</span><span>]))</span><span> </span><span>^</span><span> </span><span>2</span><span> </span><span>/</span><span> </span><span>x</span><span>)</span><span>
</span><span>}</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>seq</span><span>(</span><span>0.05</span><span>,</span><span> </span><span>0.95</span><span>,</span><span> </span><span>length.out</span><span> </span><span>=</span><span> </span><span>10</span><span>)</span><span>
</span><span>fit2</span><span> </span><span>&lt;-</span><span> </span><span>optim</span><span>(</span><span>c</span><span>(</span><span>1</span><span>,</span><span>1</span><span>),</span><span> </span><span>fn2</span><span>)</span><span>
</span><span>simulated2</span><span> </span><span>&lt;-</span><span> </span><span>rlnorm</span><span>(</span><span>n</span><span>,</span><span> </span><span>fit2</span><span>$</span><span>par</span><span>[</span><span>1</span><span>],</span><span> </span><span>fit2</span><span>$</span><span>par</span><span>[</span><span>2</span><span>])</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>plnorm</span><span>(</span><span>bin_avs</span><span>(</span><span>simulated2</span><span>)</span><span>$</span><span>mean</span><span>,</span><span> </span><span>fit2</span><span>$</span><span>par</span><span>[</span><span>1</span><span>],</span><span> </span><span>fit2</span><span>$</span><span>par</span><span>[</span><span>2</span><span>])</span><span>
</span><span>fit2</span><span> </span><span>&lt;-</span><span> </span><span>optim</span><span>(</span><span>c</span><span>(</span><span>1</span><span>,</span><span>1</span><span>),</span><span> </span><span>fn2</span><span>)</span><span>
</span><span>simulated2</span><span> </span><span>&lt;-</span><span> </span><span>rlnorm</span><span>(</span><span>n</span><span>,</span><span> </span><span>fit2</span><span>$</span><span>par</span><span>[</span><span>1</span><span>],</span><span> </span><span>fit2</span><span>$</span><span>par</span><span>[</span><span>2</span><span>])</span></code></pre>
</figure>
<p>And here are the results. The first table shows the means of the bins in my simulated log-normal population (<code>mean</code>) compared to the original data for Angola’s actual deciles in 1995 (<code>x</code>). The next two values, 0.415 and 0.402, are the Gini coefficents from the simulated and original data respectively:</p>
<div>
<pre><code>> cbind(bin_avs(simulated2), x)
   bin_number         bin_breaks      mean    x
1           1         (40.6,222]  165.0098  174
2           2          (222,308]  266.9120  287
3           3          (308,393]  350.3674  373
4           4          (393,487]  438.9447  450
5           5          (487,589]  536.5547  538
6           6          (589,719]  650.7210  653
7           7          (719,887]  795.9326  785
8           8     (887,1.13e+03] 1000.8614  967
9           9 (1.13e+03,1.6e+03] 1328.3872 1303
10         10  (1.6e+03,1.3e+04] 2438.4041 2528
> weighted.gini(simulated2)$Gini
          [,1]
[1,] 0.4145321
> 
> # compare to the value estimated directly from the data:
> weighted.gini(x)$Gini
         [,1]
[1,] 0.401936
</code></pre>
</div>
<p>As would be expected from my earlier simulation, the Gini coefficient from the estimated underlying log-normal distribtuion is verr slightly higher than that calculated directly from the means of the decile bins.</p>
<h2>Applying this method to the Lakner-Milanovic inequality data</h2>
<p>I rolled up this approach into a function to convert means of deciles into Gini coefficients and applied it to all the countries and years in the World Panel Income Distribution data. Here are the results, first over time:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0107-all-countries-ginis.svg"></p>
<p>.. and then as a snapshot</p>
<p><img width="100%" src="http://ellisp.github.io/img/0107-all-countries-snapshot.svg"></p>
<p>Neither of these is great as a polished data visualisation, but it’s difficult data to present in a static snapshot, and will do for these illustrative purposes.</p>
<p>Here’s the code for that function (which depends on the previously defined ) and drawing those charts. Drawing on the convenience of Hadley Wickham’s <code>dplyr</code> and <code>ggplot2</code> it’s easy to do this on the fly and in the below I calculate the Gini coefficients twice, once for each chart. Technically this is wasteful, but with modern computers this isn’t a big deal even though there is quite a bit of computationally intensive stuff going on under the hood; the code below only takes a minute or so to run.</p>
<figure>
<pre><code data-lang="r"><span>#' Convert data that is means of deciles into a Gini coefficient
#' #' @param x vector of 10 numbers, representing mean income (or whatever) for 10 deciles
#' @param n number of simulated values of the underlying log-normal distribution to generate
#' @details returns an estimate of Gini coefficient that is less biased than calculating it
#' directly from the deciles, which would be slightly biased downwards.
</span><span>deciles_to_gini</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>1000</span><span>){</span><span>
</span><span>fn</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>params</span><span>)</span><span> </span><span>{</span><span>
</span><span>sum</span><span>((</span><span>x</span><span> </span><span>-</span><span> </span><span>qlnorm</span><span>(</span><span>p</span><span>,</span><span> </span><span>params</span><span>[</span><span>1</span><span>],</span><span> </span><span>params</span><span>[</span><span>2</span><span>]))</span><span> </span><span>^</span><span> </span><span>2</span><span> </span><span>/</span><span> </span><span>x</span><span>)</span><span>
</span><span>}</span><span>
</span><span># starting estimate of p based on binned means and parameters
</span><span> </span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>seq</span><span>(</span><span>0.05</span><span>,</span><span> </span><span>0.95</span><span>,</span><span> </span><span>length.out</span><span> </span><span>=</span><span> </span><span>10</span><span>)</span><span>
</span><span>fit</span><span> </span><span>&lt;-</span><span> </span><span>optim</span><span>(</span><span>c</span><span>(</span><span>1</span><span>,</span><span>1</span><span>),</span><span> </span><span>fn</span><span>)</span><span>
</span><span># calculate a better value of p
</span><span> </span><span>simulated</span><span> </span><span>&lt;-</span><span> </span><span>rlnorm</span><span>(</span><span>n</span><span>,</span><span> </span><span>fit</span><span>$</span><span>par</span><span>[</span><span>1</span><span>],</span><span> </span><span>fit</span><span>$</span><span>par</span><span>[</span><span>2</span><span>])</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>plnorm</span><span>(</span><span>bin_avs</span><span>(</span><span>simulated</span><span>)</span><span>$</span><span>mean</span><span>,</span><span> </span><span>fit</span><span>$</span><span>par</span><span>[</span><span>1</span><span>],</span><span> </span><span>fit</span><span>$</span><span>par</span><span>[</span><span>2</span><span>])</span><span>
</span><span># new fit with the better p
</span><span> </span><span>fit</span><span> </span><span>&lt;-</span><span> </span><span>optim</span><span>(</span><span>c</span><span>(</span><span>1</span><span>,</span><span>1</span><span>),</span><span> </span><span>fn</span><span>)</span><span>
</span><span>simulated</span><span> </span><span>&lt;-</span><span> </span><span>rlnorm</span><span>(</span><span>n</span><span>,</span><span> </span><span>fit</span><span>$</span><span>par</span><span>[</span><span>1</span><span>],</span><span> </span><span>fit</span><span>$</span><span>par</span><span>[</span><span>2</span><span>])</span><span>
</span><span>output</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>par</span><span> </span><span>=</span><span> </span><span>fit</span><span>$</span><span>par</span><span>)</span><span>
</span><span>output</span><span>$</span><span>Gini</span><span> </span><span>&lt;-</span><span> </span><span>as.numeric</span><span>(</span><span>weighted.gini</span><span>(</span><span>simulated</span><span>)</span><span>$</span><span>Gini</span><span>)</span><span>
</span><span>return</span><span>(</span><span>output</span><span>)</span><span>
</span><span>}</span><span>
</span><span># example usage:
</span><span>deciles_to_gini</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>wpid</span><span>[</span><span>61</span><span>:</span><span>70</span><span>,</span><span> </span><span>]</span><span>$</span><span>RRinc</span><span>)</span><span>
</span><span>deciles_to_gini</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>wpid</span><span>[</span><span>171</span><span>:</span><span>180</span><span>,</span><span> </span><span>]</span><span>$</span><span>RRinc</span><span>)</span><span>
</span><span># draw some graphs:
</span><span>wpid</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>country</span><span> </span><span>!=</span><span> </span><span>&quot;Switzerland&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>inc_con</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>inc_con</span><span> </span><span>==</span><span> </span><span>&quot;C&quot;</span><span>,</span><span> </span><span>&quot;Consumption&quot;</span><span>,</span><span> </span><span>&quot;Income&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>region</span><span>,</span><span> </span><span>country</span><span>,</span><span> </span><span>contcod</span><span>,</span><span> </span><span>year</span><span>,</span><span> </span><span>inc_con</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Gini</span><span> </span><span>=</span><span> </span><span>deciles_to_gini</span><span>(</span><span>RRinc</span><span>)</span><span>$</span><span>Gini</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>year</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Gini</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>contcod</span><span>,</span><span> </span><span>linetype</span><span> </span><span>=</span><span> </span><span>inc_con</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>region</span><span>)</span><span> </span><span>+</span><span>
</span><span>guides</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Inequality over time&quot;</span><span>,</span><span>
</span><span>&quot;Gini coefficients estimated from decile data&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>linetype</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: Lakner/Milanovic World Panel Income Distribution data&quot;</span><span>)</span><span> </span><span>wpid</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>country</span><span> </span><span>!=</span><span> </span><span>&quot;Switzerland&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>inc_con</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>inc_con</span><span> </span><span>==</span><span> </span><span>&quot;C&quot;</span><span>,</span><span> </span><span>&quot;Consumption&quot;</span><span>,</span><span> </span><span>&quot;Income&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>region</span><span>,</span><span> </span><span>country</span><span>,</span><span> </span><span>contcod</span><span>,</span><span> </span><span>year</span><span>,</span><span> </span><span>inc_con</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Gini</span><span> </span><span>=</span><span> </span><span>deciles_to_gini</span><span>(</span><span>RRinc</span><span>)</span><span>$</span><span>Gini</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>country</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>year</span><span> </span><span>==</span><span> </span><span>max</span><span>(</span><span>year</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>country</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>country</span><span>,</span><span> </span><span>Gini</span><span>),</span><span>
</span><span>region</span><span> </span><span>=</span><span> </span><span>fct_lump</span><span>(</span><span>region</span><span>,</span><span> </span><span>5</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Gini</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>country</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>inc_con</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>contcod</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>region</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>,</span><span> </span><span>nrow</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Gini coefficient&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: Lakner-Milanovic World Panel Income Distribution&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Inequality by country&quot;</span><span>,</span><span>
</span><span>&quot;Most recent year available up to 2008; Gini coefficients are estimated from decile mean income.&quot;</span><span>)</span></code></pre>
</figure>
<p>There we go – deciles to Gini fun with world inequality data!</p>
<figure>
<pre><code data-lang="r"><span># cleanup
</span><span>unlink</span><span>(</span><span>&quot;LM_WPID_web.dta&quot;</span><span>)</span></code></pre>
</figure>
	<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://ellisp.github.io/blog/2017/08/19/quantiles-gini"> Peter's stats stuff - R</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/wkEuhrbCLgw" height="1"></a></div>