<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/inter-country-inequality-and-the-world-development-indicators-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/07/22/inter-country-inequality"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I recently read the high quality book <a rel="nofollow" href="http://www.hup.harvard.edu/catalog.php?isbn=9780674737136" target="_blank"><em>Global Inequality</em></a> by Branko Milanovic. When reading this sort of thing, I often find I can increase my engagement with a topic by playing around with the data myself. In this day and age, this is much more likely to be possible than a couple of decades back! I remember when I first studied development economics, typing into Lotus 123 the data from tables in the back of a <em>World Development Report</em>. These days to get the same data we just fire up R and get the data from the <code>WDI</code> package, which speaks directly to the World Bank’s World Development Indicators API.</p>
<h2>Re-creating three charts</h2>
<p>Chapter 4 of <em>Global Inequality</em> is entitled “Global Inequality in This Century and the Next”. It explores a range of issues to do with inter-country inequality and global inequality. The difference between the two is that “global” compares the income (or wealth, although there usually isn’t adequate data to do this) of all global citizens on an equal basis whereas “inter-country” uses the average income in each country and helps explore the idea of “citizenship rent” ie the bonus one gets from having been born in a wealthier place. I won’t try to summarise the discussion in this excellent chapter, I just recommend reading the original.</p>
<p>I set out to reproduce the first three figures in the chapter and had an interestingly partial success. That is, a partial success, that was partial for interesting reasons.</p>
<h3>Global income inequality among countries</h3>
<p>First, here is my version of Milanovic’s Figure 4.1. His original figure looks close to an extended version of what I have in just the left panel. This is based on purchasing-power parity (PPP) GDP per capita – that is,<br>
adjusted not just for exchange rates but for differing price levels in different countries. However, Milanovic used data back to 1960, and for more countries than I have available. Like mine, his data were drawn<br>
from the World Development Indicators. But in 2014, the World Development Indicators were updated with new PPP<br>
references. Because of <a rel="nofollow" href="http://databank.worldbank.org/data/download/WDIrevisions.xls" target="_blank">concerns about inaccuracy</a> the data were limited to 1990 onwards. It seems also at some time in the past few years<br>
the WDI were restricted to data about currently existing countries (eg excluding data from the USSR or Yugoslavia).</p>
<p>For these reasons, the left pane of my chart below differs from the original in <em>Global Inequality</em>. And to get the picture over a longer period, in the right pane I had to use a constant US dollar GDP series instead of a PPP one. I think this not only overstates inequality (because a dollar buys more in poor countries than rich ones), it hides some of the patterns; but it’s better than nothing.</p>
<p>If this were important, I’d definitely want to do something about the slowly-changing-dimension problem; that is, the people who have been excluded from counting because their countries have not been in continuous existence over the whole period in question. <em>What</em> to do about this could be a blog post (or a book) in itself…</p>
<p><img width="100%" src="http://ellisp.github.io/img/0105-gini-usd.svg"></p>
<p>However, the basic story is still supported even though absolute levels differ from my version to the one based on previous data. Reflecting income growth in China and India, there is a steep decline in the population-weighted Gini coefficient from 1990 or 2000 in my chart; in Milanovic’s original there was a gradual decline from 1980, accelerating from 2000. My unweighted Gini coefficients show similar patterns to his, with inequality declining from around 2000. Average incomes in Latin America, Eastern Europe and Africa failed to catch up with those in wealthier countries in the 1980-2000 period, but have been converging since 2000.</p>
<h3>Difference in the combined (population-weighted) growth rates</h3>
<p>The second chart I tried to reproduce was Figure 4.2, which shows differing growth rates between the advanced economies (treated as a single bloc) and the “principal emerging economies (excluding China)”. Milanovic defined this latter group as India, Brazil, Indonesia, South Africa and Vietnam; I’ve chosen a larger group. When the bar is above 0, the emerging economies have grown faster than the advanced economies.</p>
<p><img width="100%" src="http://ellisp.github.io/img/0105-growth-gap.svg"></p>
<p>The data for Vietnam available to me only started in the 1980s, and this alone leads to quite a difference in conclusion and interpretation between my chart above and Milanovic’s original. Vietnam was ravaged by war for decades until 1975. It’s a populous country and if its data were included in the chart above it would definitely drag <em>down</em> the “emerging” economies’ pre-1980 combined growth rate, and <em>up</em> their rate since 1980. Without Vietnam in the dataset, I see considerably more growth in the historical “emerging” economies than is present in the figure in the original book. Playing around with different combinations of the “principal” emerging economies didn’t make much difference. I chose the countries I did on the basis of population size and data availability.</p>
<p>As in the original chart, I still have a generally stronger relative performance by the (non-China) emerging economies from 2000 onwards than previously; but the trend over time isn’t as dramatic when Vietnam is excluded, more emerging economies are added in, and the updated data source used.</p>
<h3>Level of GDP per capita in 1970 and average subsequent growth</h3>
<p>Finally, I had good success in creating a substantively similar chart to Milanovic’s Figure 4.3, shown below:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0105-scatter.svg"></p>
<p>The descriptive/historical interpretation of my chart is identical to Milanovic’s. For Asian and Western countries, the countries with lower incomes in 1970 have successfully played catch-up, with higher average annual economic growth for poorer countries. For Eastern European, Latin American and African countries, this hasn’t been the case, with no relationship between income in 1970 and subsequent growth.</p>
<p>I have a little more information in my charts than was in the original. Point sizes now show population, which is important for appreciating the significance of India and China in the right hand panel in particular. I also include both weighted and unweighted regression lines; Milanovic discusses the importance of weighting by population in this sort of analysis but I think in the chart he has presented only the unweighted regression line (the substance of the conclusion stays the same).</p>
<p>If you’re wondering who the tiny “Asian” country is that had negative average GDP per capita growth over this 46 year period (!) it is Kiribati (pronounced <a rel="nofollow" href="http://www.pronouncenames.com/pronounce/Kiribati" target="_blank">K EE R ih b ae s</a> – unlike as spoken when it was mentioned once on the Gilmore Girls). Following Milanovic, I included Pacific countries in the “Asian” grouping. Of countries with data available, only Liberia, Democratic Republic of the Congo, Central African Republic, Madagascar, Niger had worse shrinkage in economic production per capita.</p>
<h2>Delving</h2>
<p>Here’s how I did this. First, here’s the code to load up R functionality for the whole session, set a colour palette I’m going to use a couple of times, explore the names of the various data collections available in the <code>WDI</code> interface, and download the datasets I need for GDP per capita (constant US dollars), GDP per capita (constant PPP US dollars) and population:</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>WDI</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ineq</span><span>)</span><span> </span><span># for Gini
</span><span>library</span><span>(</span><span>acid</span><span>)</span><span> </span><span># for weighted.gini
</span><span>library</span><span>(</span><span>forcats</span><span>)</span><span>
</span><span>library</span><span>(</span><span>RColorBrewer</span><span>)</span><span>
</span><span>library</span><span>(</span><span>directlabels</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggrepel</span><span>)</span><span>
</span><span>library</span><span>(</span><span>testthat</span><span>)</span><span>
</span><span>library</span><span>(</span><span>forecastHybrid</span><span>)</span><span>
</span><span>library</span><span>(</span><span>stringr</span><span>)</span><span> </span><span># for str_wrap
</span><span>
</span><span># Colour palette for making sure charts use the same colours:
</span><span>gini_palette</span><span> </span><span>&lt;-</span><span> </span><span>brewer.pal</span><span>(</span><span>3</span><span>,</span><span> </span><span>&quot;Set2&quot;</span><span>)</span><span>
</span><span>names</span><span>(</span><span>gini_palette</span><span>)[</span><span>c</span><span>(</span><span>1</span><span>,</span><span>2</span><span>)]</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;Unweighted&quot;</span><span>,</span><span> </span><span>&quot;Weighted&quot;</span><span>)</span><span>
</span><span>#==============download data===========
</span><span>tmp1</span><span> </span><span>&lt;-</span><span> </span><span>WDIsearch</span><span>(</span><span>&quot;GDP per capita&quot;</span><span>)</span><span>
</span><span>tmp2</span><span> </span><span>&lt;-</span><span> </span><span>WDIsearch</span><span>(</span><span>&quot;population&quot;</span><span>)</span><span>
</span><span># &quot;NY.GDP.PCAP.PP.KD&quot; &quot;GDP per capita, PPP (constant 2005 international $)&quot; # - this one sounds and looks like what Milanovic used but now it only goes to 1990
# In http://databank.worldbank.org/data/download/WDIrevisions.xls,
# it is stated that from May 2014 PPP data are onlyu provided back to 1990 due
# to concerns about the risk of inaccuracy the further back from the benchmark year
</span><span>
</span><span># alternatives:
# &quot;NY.GDP.PCAP.KD&quot; &quot;GDP per capita (constant 2000 US$)&quot; ie not adjusted for inter-country price differences
# &quot;GDP.PC.KD&quot; &quot;GDP per Capita, constant US$, millions&quot; - doesn't seem to exist
# &quot;NY.GDP.PCAP.CD&quot; &quot;GDP per capita (current US$)&quot; ie not corrected for inflation at all
# &quot;NY.GDP.PCAP.KN&quot; &quot;GDP per capita (constant LCU)&quot; ie local currency
# SPPOPTOTL Population, millions at least this is straightforward...
</span><span>
</span><span>gdp_ppp</span><span> </span><span>&lt;-</span><span> </span><span>WDI</span><span>(</span><span>indicator</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;NY.GDP.PCAP.PP.KD&quot;</span><span>,</span><span> </span><span>&quot;SP.POP.TOTL&quot;</span><span>),</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>1960</span><span>,</span><span> </span><span>end</span><span> </span><span>=</span><span> </span><span>2016</span><span>,</span><span> </span><span>extra</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>gdp_constant</span><span> </span><span>&lt;-</span><span> </span><span>WDI</span><span>(</span><span>indicator</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;NY.GDP.PCAP.KD&quot;</span><span>,</span><span> </span><span>&quot;SP.POP.TOTL&quot;</span><span>),</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>1960</span><span>,</span><span> </span><span>end</span><span> </span><span>=</span><span> </span><span>2016</span><span>,</span><span> </span><span>extra</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span></code></pre>
</figure>
<p>I wanted to get a feel for the data in its most basic presentation, so I drew myself a time series line chart to see if China’s economy grew as fast in the data as I expected it to:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0105-line-charts.svg"></p>
<figure>
<pre><code data-lang="r"><span>#============time series chart=================
</span><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>gdp_constant</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>gdp</span><span> </span><span>=</span><span> </span><span>NY.GDP.PCAP.KD</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>country</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;United States&quot;</span><span>,</span><span> </span><span>&quot;United Kingdom&quot;</span><span>,</span><span> </span><span>&quot;France&quot;</span><span>,</span><span> </span><span>&quot;China&quot;</span><span>,</span><span> </span><span>&quot;India&quot;</span><span>,</span><span> </span><span>&quot;Thailand&quot;</span><span>,</span><span> </span><span>&quot;South Africa&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>year</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>gdp</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>country</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>scale_y_log10</span><span>(</span><span>&quot;GDP per capita, constant US dollars&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>labs</span><span>(</span><span>x</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: World Development Indicators&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;GDP per capita over time, selected countries&quot;</span><span>)</span><span>
</span><span>direct.label</span><span>(</span><span>p</span><span>1</span><span>)</span></code></pre>
</figure>
<p>Next I had to sort a problem of which “countries” to include. The WDI data includes a number of regional aggregations, and also countries that are missing quite a bit of data.</p>
<p>I had thought that we might find the number of countries with GDP data (not the PPP data, which I knew started in 1990) might increase to 1990 and then be flat, but we see in the chart below that it’s more complicated than that:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0105-data-coverage.svg"></p>
<p>Here’s a chart of the countries that have at least <em>some</em> data, but not for every year from 1960 to 2016. As well as things I expected (Russian Federation and Germany don’t enter the dataset until relatively late, due to changing national boundaries), there are a few surprises; for example, New Zealand’s GDP per capita in this particular dataset doesn’t start until 1977:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0105-countries-coverage.svg"></p>
<p>Here’s the code for sorting through the country and regional issues:</p>
<figure>
<pre><code data-lang="r"><span>#============country coverage==========
# first we need to work out which &quot;countries&quot; are actually regional aggregations
</span><span>country_codes</span><span> </span><span>&lt;-</span><span> </span><span>gdp_ppp</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>iso2c</span><span>,</span><span> </span><span>country</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>distinct</span><span>()</span><span>
</span><span># everything begining with X except XK (Kosovo) is a grouping eg &quot;Euro area&quot;
# everything with a number in it is a a group eg T5 is South Asia (IDA and IBRD)
# ZF, ZG, ZQ, ZT, ZJ are regions
# OE is the OECD
# EU is the European Union
</span><span>
</span><span>codes</span><span> </span><span>&lt;-</span><span> </span><span>country_codes</span><span>$</span><span>iso2c</span><span>
</span><span>exes</span><span> </span><span>&lt;-</span><span> </span><span>codes</span><span>[</span><span>grepl</span><span>(</span><span>&quot;^X&quot;</span><span>,</span><span> </span><span>codes</span><span>)]</span><span>
</span><span>exes</span><span> </span><span>&lt;-</span><span> </span><span>exes</span><span>[</span><span>exes</span><span> </span><span>!=</span><span> </span><span>&quot;XK&quot;</span><span>]</span><span>
</span><span># regs has the iso2c codes for regional aggregfations:
</span><span>regs</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>exes</span><span>,</span><span> </span><span>codes</span><span>[</span><span>grepl</span><span>(</span><span>&quot;[0-9]&quot;</span><span>,</span><span> </span><span>codes</span><span>)],</span><span> </span><span>&quot;ZF&quot;</span><span>,</span><span> </span><span>&quot;ZG&quot;</span><span>,</span><span> </span><span>&quot;ZQ&quot;</span><span>,</span><span> </span><span>&quot;ZT&quot;</span><span>,</span><span> </span><span>&quot;ZJ&quot;</span><span>,</span><span> </span><span>&quot;OE&quot;</span><span>,</span><span> </span><span>&quot;EU&quot;</span><span>)</span><span>
</span><span># how many countries have data each year?
</span><span>gdp_constant</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>!</span><span>iso2c</span><span> </span><span>%in%</span><span> </span><span>regs</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>year</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>countries</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>NY.GDP.PCAP.KD</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>countries</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Variable GDP coverage in the World Development Indicators&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Number of countries with \nconstant US prices GDP data in the WDI&quot;</span><span>,</span><span> </span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: World Development Indicators&quot;</span><span>)</span><span>
</span><span>country_coverage</span><span> </span><span>&lt;-</span><span> </span><span>gdp_constant</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>!</span><span>iso2c</span><span> </span><span>%in%</span><span> </span><span>regs</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>summarise</span><span>(</span><span>prop_complete</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>NY.GDP.PCAP.KD</span><span>))</span><span> </span><span>/</span><span> </span><span>n</span><span>())</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>desc</span><span>(</span><span>prop_complete</span><span>))</span><span>
</span><span># some surprises here. For example New Zealand only has data from 1977 onwards
</span><span>country_coverage</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>prop_complete</span><span> </span><span>!=</span><span> </span><span>0</span><span> </span><span>&amp;</span><span> </span><span>prop_complete</span><span> </span><span>!=</span><span> </span><span>1</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>prop_complete</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>prop_complete</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>country</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;Countries with partial GDP data in the WDI&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;Percentage of years 1960-2016 with data on GDP in constant US dollars&quot;</span><span>,</span><span>
</span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>y</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: World Development Indicators&quot;</span><span>)</span></code></pre>
</figure>
<p>Here’s the code that draws the three main charts. The <code>tidyverse</code> makes it pretty easy to do this calculation of summary statistics like Gini or weighted Gini for arbitrary groupings and turn them into nice charts.</p>
<figure>
<pre><code data-lang="r"><span>#================Variant of figure 4.1 on Gini over time===========
# constant prices version, which we will then rbind onto the PPP version:
</span><span>tmp</span><span> </span><span>&lt;-</span><span> </span><span>gdp_constant</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>gdp</span><span> </span><span>=</span><span> </span><span>NY.GDP.PCAP.KD</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>var</span><span> </span><span>=</span><span> </span><span>&quot;USD exchange rates&quot;</span><span>)</span><span>
</span><span>gdp_ppp</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>gdp</span><span> </span><span>=</span><span> </span><span>NY.GDP.PCAP.PP.KD</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>var</span><span> </span><span>=</span><span> </span><span>&quot;Purchasing Power Parity&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>rbind</span><span>(</span><span>tmp</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>country</span><span> </span><span>%in%</span><span> </span><span>filter</span><span>(</span><span>country_coverage</span><span>,</span><span> </span><span>prop_complete</span><span> </span><span>==</span><span> </span><span>1</span><span>)</span><span>$</span><span>country</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># need to knock out the NA values in the PPP data:
</span><span> </span><span>filter</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>gdp</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>year</span><span>,</span><span> </span><span>var</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Unweighted</span><span> </span><span>=</span><span> </span><span>Gini</span><span>(</span><span>gdp</span><span>),</span><span>
</span><span>Weighted</span><span> </span><span>=</span><span> </span><span>weighted.gini</span><span>(</span><span>gdp</span><span>,</span><span> </span><span>SP.POP.TOTL</span><span>)</span><span>$</span><span>bcwGini</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>variable</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>-</span><span>year</span><span>,</span><span> </span><span>-</span><span>var</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>variable</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>variable</span><span>,</span><span> </span><span>-</span><span>value</span><span>,</span><span> </span><span>fun</span><span> </span><span>=</span><span> </span><span>last</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>value</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>variable</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>var</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;right&quot;</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;Gini or weighted Gini&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: World Development Indicators&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Inter-country inequality over time&quot;</span><span>,</span><span>
</span><span>&quot;GDP per capita, two different methods of comparing across countries
Only includes countries with full coverage of data 1960 to 2016
Inspired by Figure 4.1 from Milanovic, Global Inequality&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1960</span><span>,</span><span> </span><span>2020</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_colour_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>gini_palette</span><span>)</span><span> </span><span>#================Figure 4.2====================
</span><span>
</span><span># excluding Vietnam because they don't have any data now in WDI until the 1980s
# adding in some other countries by population - not clear why excluded from the original
</span><span>emerging</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;India&quot;</span><span>,</span><span> </span><span>&quot;Brazil&quot;</span><span>,</span><span> </span><span>&quot;Indonesia&quot;</span><span>,</span><span> </span><span>&quot;South Africa&quot;</span><span>,</span><span> </span><span>&quot;Pakistan&quot;</span><span>,</span><span> </span><span>&quot;Bangladesh&quot;</span><span>,</span><span> </span><span>&quot;Nigeria&quot;</span><span>,</span><span>
</span><span>&quot;Mexico&quot;</span><span>,</span><span> </span><span>&quot;Thailand&quot;</span><span>,</span><span> </span><span>&quot;Philippines&quot;</span><span>,</span><span> </span><span>&quot;Egypt, Arab Rep.&quot;</span><span>,</span><span> </span><span>&quot;Turkey&quot;</span><span>,</span><span> </span><span>&quot;Korea, Rep.&quot;</span><span>,</span><span>
</span><span>&quot;Iran, Islamic Rep.&quot;</span><span>,</span><span> </span><span>&quot;Myanmar&quot;</span><span>,</span><span> </span><span>&quot;Colombia&quot;</span><span>,</span><span> </span><span>&quot;Congo, Dem. Rep.&quot;</span><span>)</span><span>
</span><span>expect_equal</span><span>(</span><span>sum</span><span>(</span><span>!</span><span>emerging</span><span> </span><span>%in%</span><span> </span><span>gdp_constant</span><span>$</span><span>country</span><span>),</span><span> </span><span>0</span><span>)</span><span>
</span><span>gdp_constant</span><span> </span><span>%>%</span><span> </span><span>rename</span><span>(</span><span>gdp</span><span> </span><span>=</span><span> </span><span>NY.GDP.PCAP.KD</span><span>,</span><span>
</span><span>pop</span><span> </span><span>=</span><span> </span><span>SP.POP.TOTL</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>country</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;European Union&quot;</span><span>,</span><span> </span><span>&quot;United States&quot;</span><span>,</span><span> </span><span>&quot;Japan&quot;</span><span>,</span><span> </span><span>emerging</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>type</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>country</span><span> </span><span>%in%</span><span> </span><span>emerging</span><span>,</span><span> </span><span>&quot;emerging&quot;</span><span>,</span><span> </span><span>&quot;advanced&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>year</span><span>,</span><span> </span><span>type</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># needs to be false so bars don't draw when not present
</span><span> </span><span>summarise</span><span>(</span><span>gdp</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>gdp</span><span> </span><span>*</span><span> </span><span>pop</span><span>,</span><span> </span><span>na.rm</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>type</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>diffgdp</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>NA</span><span>,</span><span> </span><span>diff</span><span>(</span><span>gdp</span><span>)),</span><span>
</span><span>growth</span><span> </span><span>=</span><span> </span><span>diffgdp</span><span> </span><span>/</span><span> </span><span>(</span><span>gdp</span><span> </span><span>-</span><span> </span><span>diffgdp</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>year</span><span>,</span><span> </span><span>type</span><span>,</span><span> </span><span>growth</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>spread</span><span>(</span><span>type</span><span>,</span><span> </span><span>growth</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>difference</span><span> </span><span>=</span><span> </span><span>emerging</span><span> </span><span>-</span><span> </span><span>advanced</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>weight</span><span> </span><span>=</span><span> </span><span>difference</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>()</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;GDP growth gap between 'emerging' and 'advanced' countries over time&quot;</span><span>,</span><span>
</span><span>&quot;Excluding China for illustrative reasons, and Vietnam for data reasons.
Inspired by Figure 4.2 from Milanovic, Global Inequality&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Difference in population-weighted growth rate between emerging and advanced economies, in percentage points&quot;</span><span>,</span><span>
</span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>str_wrap</span><span>(</span><span>paste0</span><span>(</span><span>&quot;Emerging countries defined as &quot;</span><span>,</span><span> </span><span>paste</span><span>(</span><span>emerging</span><span>,</span><span> </span><span>collapse</span><span> </span><span>=</span><span> </span><span>&quot;, &quot;</span><span>)),</span><span>
</span><span>100</span><span>),</span><span>
</span><span>&quot;\n\nAdvanced countries defined as EU, USA and Japan&quot;</span><span>),</span><span> </span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: World Development Indicators&quot;</span><span>)</span><span>
</span><span># This is much more positive in the late 1960s and 1970s than in the original,
# and perhaps this is all because he had Vietnam (during the war...) and I don't
# have data.
</span><span>
</span><span>#====================Figure 4.3 scatter plots========
</span><span>end_year</span><span> </span><span>&lt;-</span><span> </span><span>2016</span><span>
</span><span>gdp_summary</span><span> </span><span>&lt;-</span><span> </span><span>gdp_constant</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>!</span><span>iso2c</span><span> </span><span>%in%</span><span> </span><span>regs</span><span> </span><span>&amp;</span><span> </span><span>year</span><span> </span><span>>=</span><span> </span><span>1970</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>gdp</span><span> </span><span>=</span><span> </span><span>NY.GDP.PCAP.KD</span><span>,</span><span>
</span><span>pop</span><span> </span><span>=</span><span> </span><span>SP.POP.TOTL</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>year</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>country_type</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>income</span><span> </span><span>==</span><span> </span><span>&quot;High income: OECD&quot;</span><span>,</span><span> </span><span>&quot;Western&quot;</span><span>,</span><span> </span><span>&quot;Other&quot;</span><span>),</span><span>
</span><span>country_type</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>region</span><span> </span><span>==</span><span> </span><span>&quot;East Asia &amp; Pacific (all income levels)&quot;</span><span> </span><span>|</span><span> </span><span>country</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;India&quot;</span><span>,</span><span> </span><span>&quot;Bangladesh&quot;</span><span>,</span><span> </span><span>&quot;Nepal&quot;</span><span>,</span><span> </span><span>&quot;Pakistan&quot;</span><span>),</span><span> </span><span>&quot;Asian&quot;</span><span>,</span><span> </span><span>country_type</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>iso2c</span><span>,</span><span> </span><span>country</span><span>,</span><span> </span><span>country_type</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>gdp1970</span><span> </span><span>=</span><span> </span><span>gdp</span><span>[</span><span>year</span><span> </span><span>==</span><span> </span><span>1970</span><span>],</span><span>
</span><span>growth</span><span> </span><span>=</span><span> </span><span>(</span><span>gdp</span><span>[</span><span>year</span><span> </span><span>==</span><span> </span><span>end_year</span><span>]</span><span> </span><span>/</span><span> </span><span>gdp1970</span><span>)</span><span> </span><span>^</span><span> </span><span>(</span><span>1</span><span> </span><span>/</span><span> </span><span>(</span><span>end_year</span><span> </span><span>-</span><span> </span><span>1970</span><span>))</span><span> </span><span>-</span><span> </span><span>1</span><span>,</span><span>
</span><span>pop1970</span><span> </span><span>=</span><span> </span><span>pop</span><span>[</span><span>year</span><span> </span><span>==</span><span> </span><span>1970</span><span>])</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>growth</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>plot_label</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>country_type</span><span> </span><span>==</span><span> </span><span>&quot;Other&quot;</span><span>,</span><span> </span><span>&quot;Excluding Asian and Western countries&quot;</span><span>,</span><span> </span><span>&quot;Asian and Western countries&quot;</span><span>))</span><span>
</span><span>gdp_summary</span><span>$</span><span>plot_label</span><span> </span><span>&lt;-</span><span> </span><span>relevel</span><span>(</span><span>factor</span><span>(</span><span>gdp_summary</span><span>$</span><span>plot_label</span><span>),</span><span> </span><span>&quot;Excluding Asian and Western countries&quot;</span><span>)</span><span>
</span><span>gdp_summary</span><span>$</span><span>country_type</span><span> </span><span>&lt;-</span><span> </span><span>fct_relevel</span><span>(</span><span>factor</span><span>(</span><span>gdp_summary</span><span>$</span><span>country_type</span><span>),</span><span> </span><span>c</span><span>(</span><span>&quot;Asian&quot;</span><span>,</span><span> </span><span>&quot;Western&quot;</span><span>))</span><span>
</span><span>gdp_summary</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>gdp1970</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>growth</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>plot_label</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;lm&quot;</span><span>,</span><span> </span><span>se</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey40&quot;</span><span>,</span><span> </span><span>linetype</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;lm&quot;</span><span>,</span><span> </span><span>se</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey40&quot;</span><span>,</span><span> </span><span>aes</span><span>(</span><span>weight</span><span> </span><span>=</span><span> </span><span>pop1970</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>aes</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>country_type</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>pop1970</span><span> </span><span>/</span><span> </span><span>10</span><span>^</span><span>6</span><span>),</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>aes</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>pop1970</span><span> </span><span>/</span><span> </span><span>10</span><span>^</span><span>6</span><span>),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>,</span><span> </span><span>shape</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span># next line would give labels for points but horribly cluttered plot
</span><span> </span><span># geom_text_repel(aes(label = iso2c)) +
</span><span> </span><span>scale_x_log10</span><span>(</span><span>&quot;\nGDP per capita in 1970\n\nDotted line is unweighted regression; solid line is population-weighted regression.\n&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>dollar</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1000</span><span>,</span><span> </span><span>5000</span><span>,</span><span> </span><span>20000</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>paste</span><span>(</span><span>&quot;Average growth between 1970 and&quot;</span><span>,</span><span> </span><span>end_year</span><span>),</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_size_area</span><span>(</span><span>&quot;Population\nin 1970 (m)&quot;</span><span>,</span><span> </span><span>max_size</span><span> </span><span>=</span><span> </span><span>10</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;Region&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: World Development Indicators&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;GDP in 1970 and subsequent growth&quot;</span><span>,</span><span>
</span><span>&quot;Constant prices, US dollars\nInspired by Figure 4.3 from Milanovic, Global Inequality&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;right&quot;</span><span>)</span><span>
</span><span># Note the pattern is the same as in Milanovich's graphic, even though </span><span>#</span><span> </span><span>the</span><span> </span><span>growth</span><span> </span><span>rates</span><span> </span><span>seem</span><span> </span><span>different</span></code></pre>
</figure>
<h2>Bonus – forecasting inter-country inequality</h2>
<p>An excursus in Milanovic’s book refers to some work by others forecasting trends in global inequality if current trends continue. I’m only working with inter-country data today, nothing that features the two individual-level global inequality, buit I was sufficiently interested in the idea to knock together my own very crude forecasts. I basically projected current country-level trends in GDP per capita and population growth with straightforward time series methods (a hybrid of Hyndman’s <code>auto.arima</code> and <code>ets</code> methods, as conveniently pulled together in the <code>forecastHybrid</code> package by David Shaub with minor help from myself). The result shows the expected continued decline in weighted inequality as China and India continue to “catch up”:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0105-gini-forecasts.svg"></p>
<p>Here’s the code for that forecasting exercise:</p>
<figure>
<pre><code data-lang="r"><span>#================forecasts of global gini=============
# this is only a taster for what is described in Excursus 4.1 in Milanovic's book
# because I only deal with inter-national inequality, and use very crude forecasts!
# but it comes up with a modestly similar result. I see a drop in inter-national inequaltiy
# of about 0.07 whereas they find a drop in global inequality of 0.04. These feel quite
# potentially consistent (because most forecasts for intranational inequaltiy are increases).
</span><span>
</span><span># could do the forecasts of Gini by forecasting GDP per capita, and using the population # forecasts from https://esa.un.org/unpd/wpp/Download/Standard/Population/
# but.... effort!...
# so let's just do our own forecasts
</span><span>
</span><span>#' Forecast gdp and pop for one country gdp_constant
#' and combines it with the historical data
#' context-specific function, only works in this session
</span><span>forecast_gdp_pop</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>the_country</span><span>,</span><span> </span><span>h</span><span> </span><span>=</span><span> </span><span>20</span><span>){</span><span>
</span><span># assumes presence in environment of a data.frame called gdp_constant
</span><span> </span><span>country_vars</span><span> </span><span>&lt;-</span><span> </span><span>gdp_constant</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>gdp</span><span> </span><span>=</span><span> </span><span>NY.GDP.PCAP.KD</span><span>,</span><span>
</span><span>pop</span><span> </span><span>=</span><span> </span><span>SP.POP.TOTL</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>year</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>country</span><span> </span><span>==</span><span> </span><span>the_country</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>gdp</span><span>,</span><span> </span><span>pop</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ts</span><span>(</span><span>start</span><span> </span><span>=</span><span> </span><span>1960</span><span>)</span><span> </span><span># a bit of fiddliness is needed because some countries are missing early GDP per person data.
</span><span> </span><span># maybe they should be knocked out altogether of course... but let's decide that
</span><span> </span><span># later and explicitly, this function shoudl be able to handle it either way
</span><span> </span><span>years_orig</span><span> </span><span>&lt;-</span><span> </span><span>time</span><span>(</span><span>country_vars</span><span>)</span><span>
</span><span>rows_include</span><span> </span><span>&lt;-</span><span> </span><span>which</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>country_vars</span><span>[</span><span> </span><span>,</span><span> </span><span>&quot;gdp&quot;</span><span>]))</span><span>
</span><span># some &quot;countries&quot; have virtually no observations, so don't even try to forecast them...
</span><span> </span><span>if</span><span>(</span><span>length</span><span>(</span><span>rows_include</span><span>)</span><span> </span><span>></span><span> </span><span>5</span><span>){</span><span> </span><span>country_vars</span><span> </span><span>&lt;-</span><span> </span><span>country_vars</span><span>[</span><span>rows_include</span><span>,</span><span> </span><span>]</span><span>
</span><span>mod_gdp</span><span> </span><span>&lt;-</span><span> </span><span>hybridModel</span><span>(</span><span>country_vars</span><span>[</span><span> </span><span>,</span><span> </span><span>&quot;gdp&quot;</span><span>],</span><span> </span><span>lambda</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>models</span><span> </span><span>=</span><span> </span><span>&quot;ae&quot;</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>mod_pop</span><span> </span><span>&lt;-</span><span> </span><span>hybridModel</span><span>(</span><span>country_vars</span><span>[</span><span> </span><span>,</span><span> </span><span>&quot;pop&quot;</span><span>],</span><span> </span><span>lambda</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>models</span><span> </span><span>=</span><span> </span><span>&quot;ae&quot;</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>fc_gdp</span><span> </span><span>&lt;-</span><span> </span><span>forecast</span><span>(</span><span>mod_gdp</span><span>,</span><span> </span><span>h</span><span> </span><span>=</span><span> </span><span>h</span><span>)</span><span>
</span><span>fc_pop</span><span> </span><span>&lt;-</span><span> </span><span>forecast</span><span>(</span><span>mod_pop</span><span>,</span><span> </span><span>h</span><span> </span><span>=</span><span> </span><span>h</span><span>)</span><span>
</span><span>years</span><span> </span><span>&lt;-</span><span> </span><span>years_orig</span><span>[</span><span>rows_include</span><span>]</span><span>
</span><span>years</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>years</span><span>,</span><span> </span><span>max</span><span>(</span><span>years</span><span> </span><span>+</span><span> </span><span>1</span><span>)</span><span>:</span><span>(</span><span>max</span><span>(</span><span>years</span><span>)</span><span> </span><span>+</span><span> </span><span>h</span><span>))</span><span>
</span><span>tmp</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>
</span><span>country</span><span> </span><span>=</span><span> </span><span>the_country</span><span>,</span><span>
</span><span>gdp_pp</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>fc_gdp</span><span>$</span><span>x</span><span>,</span><span> </span><span>fc_gdp</span><span>$</span><span>mean</span><span>),</span><span>
</span><span>pop</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>fc_pop</span><span>$</span><span>x</span><span>,</span><span> </span><span>fc_pop</span><span>$</span><span>mean</span><span>),</span><span>
</span><span>year</span><span> </span><span>=</span><span> </span><span>years</span><span>
</span><span>)}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>tmp</span><span> </span><span>&lt;-</span><span> </span><span>NULL</span><span>
</span><span>}</span><span>
</span><span>return</span><span>(</span><span>tmp</span><span>)</span><span>
</span><span>}</span><span>
</span><span># test cases. Is designed to work even with countries with some NA values,
# even though when doing it for real I decided to drop them.
</span><span>forecast_gdp_pop</span><span>(</span><span>&quot;Australia&quot;</span><span>,</span><span> </span><span>h</span><span> </span><span>=</span><span> </span><span>100</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>variable</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>-</span><span>year</span><span>,</span><span> </span><span>-</span><span>country</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>value</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>variable</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>ncol</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Crude forecast of GDP per person and population for Australia&quot;</span><span>)</span><span>
</span><span>forecast_gdp_pop</span><span>(</span><span>&quot;China&quot;</span><span>,</span><span> </span><span>h</span><span> </span><span>=</span><span> </span><span>5</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>variable</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>-</span><span>year</span><span>,</span><span> </span><span>-</span><span>country</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>value</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>variable</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>ncol</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Crude forecast of GDP per person and population for China&quot;</span><span>)</span><span>
</span><span>forecast_gdp_pop</span><span>(</span><span>&quot;Vietnam&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>variable</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>-</span><span>year</span><span>,</span><span> </span><span>-</span><span>country</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>value</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>variable</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>ncol</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span>
</span><span># vector of all the countries with complete data:
</span><span>all_countries</span><span> </span><span>&lt;-</span><span> </span><span>filter</span><span>(</span><span>country_coverage</span><span>,</span><span> </span><span>prop_complete</span><span> </span><span>==</span><span> </span><span>1</span><span>)</span><span>$</span><span>country</span><span>
</span><span># do the actual forecasts:
</span><span>all_forecasts</span><span> </span><span>&lt;-</span><span> </span><span>lapply</span><span>(</span><span>all_countries</span><span>,</span><span> </span><span>forecast_gdp_pop</span><span>)</span><span>
</span><span>all_forecasts_df</span><span> </span><span>&lt;-</span><span> </span><span>do.call</span><span>(</span><span>&quot;rbind&quot;</span><span>,</span><span> </span><span>all_forecasts</span><span>)</span><span>
</span><span># draw graphic:
</span><span>all_forecasts_df</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>year</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Unweighted</span><span> </span><span>=</span><span> </span><span>Gini</span><span>(</span><span>gdp_pp</span><span>),</span><span>
</span><span>Weighted</span><span> </span><span>=</span><span> </span><span>weighted.gini</span><span>(</span><span>gdp_pp</span><span>,</span><span> </span><span>pop</span><span>)</span><span>$</span><span>bcwGini</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>variable</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>-</span><span>year</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>variable</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>variable</span><span>,</span><span> </span><span>-</span><span>value</span><span>,</span><span> </span><span>fun</span><span> </span><span>=</span><span> </span><span>last</span><span>))</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>type</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>year</span><span> </span><span>&lt;=</span><span> </span><span>2016</span><span>,</span><span> </span><span>&quot;Actual&quot;</span><span>,</span><span> </span><span>&quot;Forecast&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>year</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>value</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>variable</span><span>,</span><span> </span><span>linetype</span><span> </span><span>=</span><span> </span><span>type</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;right&quot;</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>linetype</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;&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Gini or weighted Gini&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: World Development Indicators&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Inter-country inequality over time, including crude forecasts&quot;</span><span>,</span><span>
</span><span>&quot;GDP per capita, two different methods of comparing across countries
Only includes countries with complete data 1960 to 2013&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_colour_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>gini_palette</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/07/22/inter-country-inequality"> 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/s9jdb6r90v0" height="1"></a></div>