<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/understanding-the-limitations-of-group-level-inequality-data-by-ellis2013nz/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://freerangestats.info/blog/2018/10/07/inequality-groups"> free range statistics - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A critical tweet the other day <a rel="nofollow" href="https://twitter.com/nicolas_sommet/status/1045315275825041409" target="_blank">by Nicolas Sommet</a> reminded me of the book <a rel="nofollow" href="https://en.wikipedia.org/wiki/The_Spirit_Level_(book)" target="_blank">The Spirit Level: Why Equal Societies Almost Always Do Better”</a>. That book argues that:</p>
<blockquote>
<p>“…there are ‘pernicious effects that inequality has on societies: eroding trust, increasing anxiety and illness, (and) encouraging excessive consumption’. It claims that for each of eleven different health and social problems: physical health, mental health, drug abuse, education, imprisonment, obesity, social mobility, trust and community life, violence, teenage pregnancies, and child well-being, outcomes are significantly worse in more unequal countries, whether rich or poor.”<br>
<em>Wikipedia</em></p>
</blockquote>
<p>In his tweet, Sommet pointed out (not claiming to be the first to do so – it’s a common critique) problems with the argument in <em>The Spirit Level</em>:</p>
<ul>
<li>cherry picking – some countries are excluded from the analysis with (he says) inadequate justification, in ways that make the conclusions seem stronger</li>
<li>failure to reproduce results when more countries are included</li>
<li>meta-analyses suggest the results are trivial</li>
</ul>
<p>Other criticisms have argued the book has a reliance of simple bivariate scatter plots rather than more sophisticated methods that are based on formal statistical tests while controlling for other variables. I have a further methodological criticism myself, but I’ll come to that later in the post.</p>
<h2>The country level data</h2>
<p>Before we go further – and I don’t want to focus in detail on the book itself as I read it some years ago and haven’t had time to refresh that (although I did buy a new, electronic copy) – let’s look at some data ourselves. Rather than pick a single year, or an artificial subset of countries, I’m going to use all years and countries for which data are available in the World Bank’s World Development Indicators.</p>
<p>I’m going to choose life expectancy as a typical response variable representing health outcomes. Because life expectancy is estimated <a rel="nofollow" href="http://freerangestats.info/blog/2018/05/31/life-expectancy" target="_blank">algorithmically from the death rates for all age groups at a point in time</a>, it’s a pretty good, standardised single dimensional indicator of health standards at their crudest. For example, it fully encompasses infant mortality as part of its calculation; and it is standardised and comparable across countries of differing demographic profiles because it is based on idea of a hypothetical person born today, suffering as they grew older all the death rates by age group that apply today.</p>
<p>Let’s start with the genuinely and obviously strong relationship between a country’s gross national income per capita and life expectancy:</p>
<p><img src="https://i1.wp.com/freerangestats.io/img/0136-income-life.gif?w=100%25" data-recalc-dims="1"></p>
<p>Note: all the code for today’s post is at the bottom of the post, to avoid breaking up the flow of the text; the animations were produced using <a rel="nofollow" href="https://github.com/thomasp85/gganimate" target="_blank">the amazing <code>gganimate</code> package</a>. Income is on a logarithmic scale, but other variables in this post are untransformed. If you’re wondering, the purple country that has volatile and low measures of life expectancy is Rwanda, reflecting the horrors in that country in the 1990s.</p>
<p>The strength and size of the relationship between income and life expectancy seems pretty constant over time, in terms of the gently curving slope on that chart, but interestingly the data gradually gain “height” on the chart over time. That is, there is an increasing intercept in the generalized additive model that is implicit behind the curve, shown in the line gradually climbing a few years in average life expectancy over the period of the data. If life expectancy was improving over time purely because of increasing income, we’d expect to see the line stay where it is but dots move upwards and rightwards along it in recent years; instead, in addition to the countries’ dots moving diagnoally up along the line, we see the line itself moving vertically. This suggests that there is something improving life expectancy beyond the increasing average incomes in countries. Investigating this further would be out of the scope of this blog post.</p>
<p>Second, let’s look at the main subject at issue today – the country-level relationship between inequality and life expectancy:</p>
<p><img src="https://i0.wp.com/freerangestats.io/img/0136-inequality-life.gif?w=100%25" data-recalc-dims="1"></p>
<p>The data are sparser and noisier in this case, but there are still 20 or so years with a good collection of data. Generally, the relationship is negative, as argued by <em>The Spirit Level</em> and similar advocates of the importance of inequality. That is, countries with a higher Gini coefficient for income (more inequality) have slightly lower life expectancies.</p>
<p>However, there’s a problem here before we can conclude a relationship between the variables, which is that the two explanatory variables we’ve considered so far are themselves correlated. The next animation shows the relationship between inequality and income over time:</p>
<p><img src="https://i0.wp.com/freerangestats.io/img/0136-income-inequality.gif?w=100%25" data-recalc-dims="1"></p>
<p>We can see that in the years where there are enough data for substantive conclusions, richer countries tend to have lower inequality. There are certainly complex historical and economic reasons for this which I don’t have time to go into here; suffice it to say that it is <em>not</em> because countries follow a <a rel="nofollow" href="https://en.wikipedia.org/wiki/Kuznets_curve" target="_blank">Kuznets curve</a> for any deterministic reason; the evidence accumulated since Kuznets’ time is strongly against any such explanation.</p>
<p>Because the impact of income on life expectancy is uncontroversial, reasonably direct, and obvious to the eye it makes sense to control for it before trying to identify the impact of country-level inequality on the same response variable. We can do this by first fitting a model predicting life expectancy on the basis of income (I chose a gently curving generalized additive model with a different curve allowed for each year); and then using the <em>residuals</em> from that model as the response variable to compare to inequality. This is essentially the process behind a <a rel="nofollow" href="https://en.wikipedia.org/wiki/Partial_regression_plot" target="_blank">partial regression plot</a>, although more normally used in linear regression than in the non-linear context here. When we do this we get the following:</p>
<p><img src="https://i2.wp.com/freerangestats.io/img/0136-inequality-unexplained-life.gif?w=100%25" data-recalc-dims="1"></p>
<p>The relationship between inequality and “unexplained life expectancy” (ie the residuals from our earlier model on income) is very weak and unstable. Whatever relationship there is in country-level data between inequality and life expectancy outcomes, it is clearly <em>much</em> weaker than the relationship with income, particularly when we consider the two variables together and control for income before we look for the inequality effect.</p>
<h2>A variant of the ecological fallacy?</h2>
<p>So far, the points are with the critics of <em>The Spirit Level</em>. But I wanted to look at an <em>additional</em> potential problem to those highlighted by Sommet in his tweet, which is a problem with using country-level data for individual-level inferences. If we look at <a rel="nofollow" href="https://inequality.org/facts/inequality-and-health/" target="_blank">Inequality.org</a> we see the argument that</p>
<blockquote>
<p>“By greater inequality, epidemiologists … don’t just mean poverty. Poor health and poverty do go hand-in-hand. But high levels of inequality, the epidemiological research shows, negatively affect the health of even the affluent, mainly because, researchers contend, inequality reduces social cohesion, a dynamic that leads to more stress, fear, and insecurity for everyone.”<br>
<em>Inequality.org</em></p>
</blockquote>
<p>It is clear that the argument is not just that unequal societies have more poor people, and poor people have poorer health, but that individuals with the same income are worse off surrounded by inequality than in an equal society. The evidence provided in favour is mostly or entirely in the form of cross-sectional country-level scatter plots.</p>
<p>Unfortunately I’m pretty sure there is a statistical fallacy in using country-level data for this sort of argument. It’s a variant of the ecological fallacy. An ecological fallacy is a mistaken inference about individuals from group level data. The classic form is:</p>
<blockquote>
<p>Imagine we could survey animals how much they worried about wolves. In the areas with relatively high numbers of wolves, concerns will be higher than in the areas with few or no wolves. A scatter plot of “percentage of wolves in area” by “percentage of animals worried by wolves” would show a strongly positive relationship. But it would be very wrong to infer from this that wolves are more worried by wolves than are other animals; it is the presence of wolves that makes other individuals worried about them, creating a group level positive relationship.</p>
</blockquote>
<p>In this contrived example the fallacy seems obvious, but unfortunately people fall for it fairly frequently. A particular trap is analysis of census data aggregated by area (often the only form available to researchers for confidentiality reasons), where the fallacy is often present in exactly this form, albeit with less obviously nonsensical content.</p>
<p>In our own case, there’s a twist on the most common ecological fallacy, as per my wolves example.</p>
<ul>
<li>In the wolves example, a genuine group-level effect (being in an area with a lot of wolves directly impacts on your fear of wolves) is mistaken for the individual level (being a wolf makes you fear wolves).</li>
<li>In the case of inequality, I suspect that the genuine effect is overwhelmingly at the individual level (<strong>being poor is bad for health outcomes; the poorer you are the more important the effect; and unequal societies have more very poor people given average income</strong>) and this has been mistaken for a group effect (being in an unequal society is directly bad for your health).</li>
</ul>
<p>To understand this, I’ll show how easy it is to simulate data where the only impact on health comes from being a poor individual, but we see a country level effect that <em>appears</em> to be inequality impacting on life expectancy, even when controlling for income</p>
<h3>Identical average income, two levels of inequality, simple relationship of income to health</h3>
<p>Consider two countries – Equaltopia and Unevenland – with identical mean income of $20,000 per year but very different inequality of income, as measured by Gini coefficient. The income distribution looks like this:</p>
<p><img width="100%" src="http://freerangestats.io/img/0136-sim-density.svg"></p>
<p>… or in a Lorenz curve, sometimes preferred in inequality discussions, like this:</p>
<p><img width="100%" src="http://freerangestats.io/img/0136-sim-lorenz.svg"></p>
<p>The diagonal line represents perfect equality where everyone has the same income, and the gap between that and the line which actually shows the cumulative proportion of income received by the cumulative proportion of people forms part of the definition of the Gini coefficient. Equaltopia and Unevenland have very different income distributions, but are within plausible ranges.</p>
<p>Now, I allocate each individual in Equaltopia and Unevenland a lifespan that is a random variable but which depends on their individual income. If their individual income is less than $10,000 per year the mean life expectancy is 50 years and standard deviation of 7 years; if above, it is 70 years and standard deviation of 10 years (this simplistic model will be made more realistic later on). If we draw a scatter plot at the individual level relating income and lifespan we see this:</p>
<p><img width="100%" src="http://freerangestats.io/img/0136-sim-income-life.svg"></p>
<p>Because Unevenland has a small number of very rich people but the same mean income as Equaltopia, statistically speaking it has to have a large number of poor people. Resulting from this, a higher proportion of the population fall below the “good health” threshhold of $10,000. We see the results starkly in this simple summary table of the average results:</p>
<table>
<thead>
<tr>
<th>country</th>
<th>Inequality</th>
<th>Mean income</th>
<th>Mean life expectancy</th>
</tr>
</thead>
<tbody>
<tr>
<td>Equaltopia</td>
<td>0.23</td>
<td>20000</td>
<td>68.4</td>
</tr>
<tr>
<td>Unevenland</td>
<td>0.63</td>
<td>20000</td>
<td>61.1</td>
</tr>
</tbody>
</table>
<p>So, in this simple example, we see how a more unequal country will have poorer health outcomes than an equal country, <em>even though there is no direct impact of inequality; only an individual income effect where poorer people have poorer health</em>.</p>
<h3>Varying average incomes, varying inequality, complex relationship of income to health</h3>
<p>The illusionary country-level inequality effect comes about because there is a diminishing marginal impact of an individual’s income on their health. This is realistic – adding $10,000 to the income of a millionaire has virtually no impact on their life expectancy, while it could change the life for the better of a homeless person. However, it’s not as stark as a simple cut-off point beyond which health is improved but can get no better.</p>
<p>To make the simulation more realistic, I change the relationship between income and mean life expectancy so it looks like this:</p>
<p><img width="100%" src="http://freerangestats.io/img/0136-sim-curve.svg"></p>
<p>To further extend the simulation, I created countries with a range of mean incomes and inequality of income, simulated individuals’ incomes within each country, and then simulated individual lifespans based on the curve above. Then when we aggregate the data back to country-level we see the following:</p>
<p><img width="100%" src="http://freerangestats.io/img/0136-sim-pairs.svg"></p>
<p>This is actually <em>very</em> similar to what the real life country-level data look like, except that if anything the illusionary inequality impact on life expectancy is even stronger in my simulated data than in the actual data. We see</p>
<ul>
<li>a strong relationship between a country’s average income and life expectancy;</li>
<li>a weak (if any) relationship between income and inequality; and</li>
<li>a medium-strength relationship between country inequality and average life expectancy… despite the fact that there is no such group level effect; income impacts on life expectancy, and inequality does not in this simulated data other than via creating larger numbers of poor people.</li>
</ul>
<p>Importantly, we can’t correct for this problem by fitting a model that “controls for” mean income. Inequality will still come out as a statistically signficant predictor of lower life expectancy in such a model, for the same reason as Unevenland had a lower life expectancy as Equaltopia despite identical mean income and life span being determined purely by individual income.</p>
<h2>What does it all mean?</h2>
<p>I find the reasons given for a potential direct impact of country-level inequality on individuals’ health plausible, but the evidence for them is weak.</p>
<p>Because of the reasons given here and demonstrated in the simulations above, country-level evidence by itself is simply inadmissable; there is no way that country-level averages and inequality measures can demonstrate a group-level impact of inequality. Only mixed individual and country level data, comparing rich and poor people in equal societies with rich and poor people in unequal societies, could do this. A study along these lines could be done with harmonised income and health microdata. It would be difficult but not impossible, and if it has been done it doesn’t get the attention that the much simpler cross-country analysis gets.</p>
<p>However, the country level data <em>can and does</em> suggest that <em>if</em> there is a direct impact of inequality on health outcomes, it is much weaker than the impact of country-level income.</p>
<p>But all the above is of little importance for policy purposes because it is clear and uncontroversial that individual income is very strongly related to individual health outcomes. So it isn’t necessary to argue for a country-level inequality effect to argue that inequality is indirectly important. Inequality means more poor people below a given poverty threshold, for a given average income.</p>
<p>A very basic result in welfare economics is that:</p>
<ul>
<li>given diminishing marginal returns for more income for rich people…</li>
<li>better overall life outcomes can always be achieved by redistributing income from rich to poor and reducing inequality…</li>
<li>if it can be done without reducing overall mean income (which possibility is of course contested, but outside today’s scope).</li>
</ul>
<h2>Code</h2>
<figure>
<pre><code data-lang="r"><span># to install gganimate:
</span><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>'thomasp85/gganimate'</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ineq</span><span>)</span><span>
</span><span>library</span><span>(</span><span>gganimate</span><span>)</span><span>
</span><span>library</span><span>(</span><span>GGally</span><span>)</span><span>
</span><span>library</span><span>(</span><span>WDI</span><span>)</span><span>
</span><span>library</span><span>(</span><span>MASS</span><span>)</span><span>
</span><span>library</span><span>(</span><span>mgcv</span><span>)</span><span>
</span><span>library</span><span>(</span><span>knitr</span><span>)</span><span>
</span><span>#===================Real data=================================
# Find the indicators we need with commands like:
# WDIsearch(&quot;gini&quot;) %>% View
# This gives us:
# - GINI index (World Bank estimate)
# - GNI per capita (constant 2010 US$)
# - Life expectancy at birth, total (years)
</span><span>
</span><span># Download data:
</span><span>country_data_orig</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;SI.POV.GINI&quot;</span><span>,</span><span> </span><span>&quot;NY.GNP.PCAP.KD&quot;</span><span>,</span><span> </span><span>&quot;SP.DYN.LE00.IN&quot;</span><span>),</span><span> </span><span>country</span><span> </span><span>=</span><span> </span><span>&quot;all&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>2020</span><span>)</span><span>
</span><span>country_data</span><span> </span><span>&lt;-</span><span> </span><span>country_data_orig</span><span> </span><span>%>%</span><span>
</span><span>as_tibble</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>inequality</span><span> </span><span>=</span><span>SI.POV.GINI</span><span>,</span><span> </span><span>income</span><span> </span><span>=</span><span> </span><span>NY.GNP.PCAP.KD</span><span>,</span><span> </span><span>life</span><span> </span><span>=</span><span> </span><span>SP.DYN.LE00.IN</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>log_income</span><span> </span><span>=</span><span> </span><span>log</span><span>(</span><span>income</span><span>),</span><span>
</span><span>log_life</span><span> </span><span>=</span><span> </span><span>log</span><span>(</span><span>life</span><span>),</span><span>
</span><span>log_inequality</span><span> </span><span>=</span><span> </span><span>log</span><span>(</span><span>inequality</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>iso2c</span><span> </span><span>!=</span><span> </span><span>&quot;&quot;</span><span> </span><span>&amp;</span><span> </span><span>!</span><span>is.na</span><span>(</span><span>income</span><span>)</span><span> </span><span>&amp;</span><span> </span><span>!</span><span>is.na</span><span>(</span><span>life</span><span>))</span><span> </span><span>%>%</span><span>
</span><span># limit ourselves to years with at least five observations (otherwsie 1983 has just 1):
</span><span> </span><span>group_by</span><span>(</span><span>year</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>sum</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>inequality</span><span>))</span><span> </span><span>></span><span> </span><span>5</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>year_factor</span><span> </span><span>=</span><span> </span><span>as.factor</span><span>(</span><span>year</span><span>),</span><span>
</span><span>year</span><span> </span><span>=</span><span> </span><span>as.integer</span><span>(</span><span>year</span><span>))</span><span>
</span><span># All three variables (and log of income) in one recent year
</span><span>country_data</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>1</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>income</span><span>,</span><span> </span><span>log_income</span><span>,</span><span> </span><span>inequality</span><span>,</span><span> </span><span>life</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggpairs</span><span>()</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Relationship between three key development indicators&quot;</span><span>,</span><span>
</span><span>paste</span><span>(</span><span>&quot;Income, life expectancy, and inequality (Gini coefficient) in&quot;</span><span>,</span><span> </span><span>max</span><span>(</span><span>country_data</span><span>$</span><span>year</span><span>)</span><span> </span><span>-</span><span> </span><span>1</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: World Bank, World Development Indicators&quot;</span><span>)</span><span>
</span><span>#-----------------------draw animations--------------
# life expectancy and income, all years (animation)
</span><span>
</span><span>a</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>country_data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>income</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>life</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_log10</span><span>(</span><span>&quot;Gross National Income per capita, 2000 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>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>2.5</span><span>,</span><span> </span><span>aes</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_smooth</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;gam&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;A positive relationship between country-level income and life expectancy&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>'Relationship holds in all years for which data are available\nYear: {frame_time}'</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>'Life expectancy'</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: World Bank, World Development Indicators&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>transition_time</span><span>(</span><span>year</span><span>)</span><span> </span><span>+</span><span>
</span><span>ease_aes</span><span>(</span><span>'linear'</span><span>)</span><span> </span><span>+</span><span>
</span><span>enter_fade</span><span>()</span><span> </span><span>+</span><span> </span><span>exit_shrink</span><span>()</span><span> </span><span>+</span><span>
</span><span>coord_cartesian</span><span>(</span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>25</span><span>,</span><span> </span><span>90</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;none&quot;</span><span>)</span><span>
</span><span># reality check - who has that big up-and-down in life expectancy?
</span><span>country_data</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>volatility</span><span> </span><span>=</span><span> </span><span>sd</span><span>(</span><span>life</span><span>,</span><span> </span><span>na.rm</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>desc</span><span>(</span><span>volatility</span><span>))</span><span>
</span><span>a</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>country_data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>inequality</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>life</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_log10</span><span>(</span><span>&quot;Inequality (Gini coefficient)&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>2.5</span><span>,</span><span> </span><span>aes</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_smooth</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;gam&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;A weaker negative relationship between inequality and life expectancy&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>'Relationship holds in years for which substantial data are available\nYear: {frame_time}'</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>'Life expectancy'</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: World Bank, World Development Indicators&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>transition_time</span><span>(</span><span>year</span><span>)</span><span> </span><span>+</span><span>
</span><span>ease_aes</span><span>(</span><span>'linear'</span><span>)</span><span> </span><span>+</span><span>
</span><span>enter_fade</span><span>()</span><span> </span><span>+</span><span> </span><span>exit_shrink</span><span>()</span><span> </span><span>+</span><span>
</span><span>coord_cartesian</span><span>(</span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>25</span><span>,</span><span> </span><span>90</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;none&quot;</span><span>)</span><span>
</span><span>a</span><span>3</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>country_data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>income</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>inequality</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_log10</span><span>(</span><span>&quot;Gross National Income per capita, 2000 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>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>2.5</span><span>,</span><span> </span><span>aes</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_smooth</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;gam&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;A variable relationship between income and inequality&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>'Relationship is negative in years for which substantial data are available\nYear: {frame_time}'</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>'Inequality (Gini coefficient)'</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: World Bank, World Development Indicators&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>transition_time</span><span>(</span><span>year</span><span>)</span><span> </span><span>+</span><span>
</span><span>ease_aes</span><span>(</span><span>'linear'</span><span>)</span><span> </span><span>+</span><span>
</span><span>enter_fade</span><span>()</span><span> </span><span>+</span><span> </span><span>exit_shrink</span><span>()</span><span> </span><span>+</span><span>
</span><span>coord_cartesian</span><span>(</span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>15</span><span>,</span><span> </span><span>70</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;none&quot;</span><span>)</span><span>
</span><span># save animations. </span><span>nf</span><span> </span><span>&lt;-</span><span> </span><span>500</span><span>
</span><span>animate</span><span>(</span><span>a</span><span>1</span><span>,</span><span> </span><span>nframes</span><span> </span><span>=</span><span> </span><span>nf</span><span>,</span><span> </span><span>fps</span><span> </span><span>=</span><span> </span><span>nf</span><span> </span><span>/</span><span> </span><span>10</span><span>,</span><span> </span><span>renderer</span><span> </span><span>=</span><span> </span><span>gifski_renderer</span><span>(</span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;0136-income-life.gif&quot;</span><span>),</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>500</span><span>,</span><span> </span><span>height</span><span> </span><span>=</span><span> </span><span>450</span><span>)</span><span>
</span><span>animate</span><span>(</span><span>a</span><span>2</span><span>,</span><span> </span><span>nframes</span><span> </span><span>=</span><span> </span><span>nf</span><span>,</span><span> </span><span>fps</span><span> </span><span>=</span><span> </span><span>nf</span><span> </span><span>/</span><span> </span><span>10</span><span>,</span><span> </span><span>renderer</span><span> </span><span>=</span><span> </span><span>gifski_renderer</span><span>(</span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;0136-inequality-life.gif&quot;</span><span>),</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>500</span><span>,</span><span> </span><span>height</span><span> </span><span>=</span><span> </span><span>450</span><span>)</span><span>
</span><span>animate</span><span>(</span><span>a</span><span>3</span><span>,</span><span> </span><span>nframes</span><span> </span><span>=</span><span> </span><span>nf</span><span>,</span><span> </span><span>fps</span><span> </span><span>=</span><span> </span><span>nf</span><span> </span><span>/</span><span> </span><span>10</span><span>,</span><span> </span><span>renderer</span><span> </span><span>=</span><span> </span><span>gifski_renderer</span><span>(</span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;0136-income-inequality.gif&quot;</span><span>),</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>500</span><span>,</span><span> </span><span>height</span><span> </span><span>=</span><span> </span><span>450</span><span>)</span><span>
</span><span>#-------------------------&quot;unexplained&quot; life expectancy-----------------
</span><span>
</span><span>model1</span><span> </span><span>&lt;-</span><span> </span><span>lm</span><span>(</span><span>life</span><span> </span><span>~</span><span> </span><span>year_factor</span><span> </span><span>*</span><span> </span><span>log_income</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>country_data</span><span>)</span><span>
</span><span>par</span><span>(</span><span>mfrow</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2</span><span>,</span><span> </span><span>2</span><span>),</span><span> </span><span>bty</span><span> </span><span>=</span><span> </span><span>&quot;l&quot;</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>model1</span><span>)</span><span>
</span><span>model2</span><span> </span><span>&lt;-</span><span> </span><span>gam</span><span>(</span><span>life</span><span> </span><span>~</span><span> </span><span>year_factor</span><span> </span><span>+</span><span> </span><span>s</span><span>(</span><span>log_income</span><span>,</span><span> </span><span>year_factor</span><span>,</span><span> </span><span>bs</span><span> </span><span>=</span><span> </span><span>&quot;fs&quot;</span><span>),</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>country_data</span><span>)</span><span>
</span><span>summary</span><span>(</span><span>model2</span><span>)</span><span> </span><span># R squared of 0.76
</span><span>summary</span><span>(</span><span>model1</span><span>)</span><span> </span><span># R squared of 0.72
</span><span>
</span><span>country_data</span><span>$</span><span>unexplained_life</span><span> </span><span>&lt;-</span><span> </span><span>residuals</span><span>(</span><span>model2</span><span>)</span><span>
</span><span>a</span><span>4</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>country_data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>inequality</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>unexplained_life</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_log10</span><span>(</span><span>&quot;Inequality (Gini coefficient)&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>2.5</span><span>,</span><span> </span><span>aes</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_smooth</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;gam&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Life expectancy after controlling for income, compared to inequality&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>'No clear relationship between inequality and residual life expectancy\nYear: {frame_time}'</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>'Unexplained life expectancy\n(residuals after modelling life expectancy on GNI per capita with a GAM)'</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: World Bank, World Development Indicators&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>transition_time</span><span>(</span><span>year</span><span>)</span><span> </span><span>+</span><span>
</span><span>ease_aes</span><span>(</span><span>'linear'</span><span>)</span><span> </span><span>+</span><span>
</span><span>enter_fade</span><span>()</span><span> </span><span>+</span><span> </span><span>exit_shrink</span><span>()</span><span> </span><span>+</span><span>
</span><span>coord_cartesian</span><span>(</span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>-25</span><span>,</span><span> </span><span>25</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;none&quot;</span><span>)</span><span>
</span><span>animate</span><span>(</span><span>a</span><span>4</span><span>,</span><span> </span><span>nframes</span><span> </span><span>=</span><span> </span><span>nf</span><span>,</span><span> </span><span>fps</span><span> </span><span>=</span><span> </span><span>nf</span><span> </span><span>/</span><span> </span><span>10</span><span>,</span><span> </span><span>renderer</span><span> </span><span>=</span><span> </span><span>gifski_renderer</span><span>(</span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;0136-inequality-unexplained-life.gif&quot;</span><span>),</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>500</span><span>,</span><span> </span><span>height</span><span> </span><span>=</span><span> </span><span>450</span><span>)</span><span>
</span><span>#======================Simulated data====================================
</span><span>#' Function to generate a log normal distribution that is vaguely income-like
#' #' @details Generates a log normal distribution of mean \code{mu} and inequality that will depend on \code{sd}.
#' @param n sample size
#' @param mu mean of the log-normal distribution
#' @param sd standard deviation of the underlying normal distribution
</span><span>rinc</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>1000</span><span>,</span><span> </span><span>mu</span><span> </span><span>=</span><span> </span><span>20000</span><span>,</span><span> </span><span>sd</span><span> </span><span>=</span><span> </span><span>1</span><span>){</span><span>
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>exp</span><span>(</span><span>rnorm</span><span>(</span><span>n</span><span>,</span><span> </span><span>mean</span><span> </span><span>=</span><span> </span><span>log</span><span>(</span><span>mu</span><span>),</span><span> </span><span>sd</span><span> </span><span>=</span><span> </span><span>sd</span><span>))</span><span>
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>x</span><span> </span><span>-</span><span> </span><span>mean</span><span>(</span><span>x</span><span>)</span><span> </span><span>+</span><span> </span><span>mu</span><span>
</span><span>return</span><span>(</span><span>x</span><span>)</span><span>
</span><span>}</span><span>
</span><span>#--------------------Simple two-country situation--------------------------
</span><span>n</span><span> </span><span>&lt;-</span><span> </span><span>1000</span><span>
</span><span>x</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>rinc</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>n</span><span>,</span><span> </span><span>sd</span><span> </span><span>=</span><span> </span><span>0.8</span><span>)</span><span>
</span><span>x</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>rinc</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>n</span><span>,</span><span> </span><span>sd</span><span> </span><span>=</span><span> </span><span>0.4</span><span>)</span><span>
</span><span>data1</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>
</span><span>income</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>x</span><span>1</span><span>,</span><span> </span><span>x</span><span>2</span><span>),</span><span>
</span><span>country</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;Unevenland&quot;</span><span>,</span><span> </span><span>&quot;Equaltopia&quot;</span><span>),</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>n</span><span>)</span><span>
</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>lifespan</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>income</span><span> </span><span></span><span> </span><span>10000</span><span>,</span><span> </span><span>rnorm</span><span>(</span><span>n</span><span>(),</span><span> </span><span>50</span><span>,</span><span> </span><span>7</span><span>),</span><span> </span><span>rnorm</span><span>(</span><span>n</span><span>(),</span><span> </span><span>70</span><span>,</span><span> </span><span>10</span><span>)))</span><span>
</span><span>ginis</span><span> </span><span>&lt;-</span><span> </span><span>data1</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>ineq</span><span> </span><span>=</span><span> </span><span>Gini</span><span>(</span><span>income</span><span>))</span><span>
</span><span>data1a</span><span> </span><span>&lt;-</span><span> </span><span>data1</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>ginis</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;country&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>country</span><span>,</span><span> </span><span>&quot;: Gini = &quot;</span><span>,</span><span> </span><span>round</span><span>(</span><span>ineq</span><span>,</span><span> </span><span>2</span><span>)))</span><span>
</span><span>#density plots
</span><span>data1a</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>income</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>label</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_density</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>NA</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</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>fill</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Two simulated income distributions with identical mean income&quot;</span><span>,</span><span>
</span><span>&quot;Statistical density plots&quot;</span><span>)</span><span>
</span><span># lorenz curves
</span><span>data1a</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>arrange</span><span>(</span><span>income</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>cum_income</span><span> </span><span>=</span><span> </span><span>cumsum</span><span>(</span><span>income</span><span>),</span><span>
</span><span>cum_inc_prop</span><span> </span><span>=</span><span> </span><span>cum_income</span><span> </span><span>/</span><span> </span><span>max</span><span>(</span><span>cum_income</span><span>),</span><span>
</span><span>inc_level</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>n</span><span>()</span><span> </span><span>/</span><span> </span><span>n</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>inc_level</span><span>,</span><span> </span><span>y</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>label</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</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>geom_abline</span><span>(</span><span>slope</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_ribbon</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>inc_level</span><span>,</span><span> </span><span>ymin</span><span> </span><span>=</span><span> </span><span>cum_inc_prop</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>inc_level</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>coord_equal</span><span>()</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Two simulated income distributions with identical mean income&quot;</span><span>,</span><span>
</span><span>&quot;Lorenz curve plots; shaded area shows the gap to pure equality.&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;Cumulated proportion of people, poorest to richest&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Cumulative income received&quot;</span><span>,</span><span>
</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>data1</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>income</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lifespan</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_point</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>scale_x_continuous</span><span>(</span><span>&quot;Individual income&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>y</span><span> </span><span>=</span><span> </span><span>&quot;Individual life span&quot;</span><span>,</span><span>
</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;Country&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Relationship between life expectancy and income&quot;</span><span>,</span><span>
</span><span>&quot;Simulated data, simplified relationship where $10k per year is a threshold for health benefits&quot;</span><span>)</span><span>
</span><span># aggregate table:	</span><span>data1</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>Inequality</span><span> </span><span>=</span><span> </span><span>round</span><span>(</span><span>Gini</span><span>(</span><span>income</span><span>),</span><span> </span><span>2</span><span>),</span><span>
</span><span>`Mean income`</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>income</span><span>),</span><span>
</span><span>`Mean life expectancy`</span><span> </span><span>=</span><span> </span><span>round</span><span>(</span><span>mean</span><span>(</span><span>lifespan</span><span>),</span><span> </span><span>1</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>kable</span><span>()</span><span>
</span><span>#-------------------------Multi country situation---------------------------
</span><span>
</span><span># a more sophisticated relationship of income to life expectancy:
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>0</span><span> </span><span>:</span><span>100000</span><span>
</span><span>y</span><span> </span><span>&lt;-</span><span> </span><span>50</span><span> </span><span>+</span><span> </span><span>x</span><span> </span><span>^</span><span> </span><span>0.5</span><span> </span><span>/</span><span> </span><span>7</span><span>
</span><span># visual demo of relationship:
</span><span>par</span><span>(</span><span>family</span><span> </span><span>=</span><span> </span><span>main_font</span><span>,</span><span> </span><span>font.main</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>bty</span><span> </span><span>=</span><span> </span><span>&quot;l&quot;</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;l&quot;</span><span>,</span><span> </span><span>xlab</span><span> </span><span>=</span><span> </span><span>&quot;Income&quot;</span><span>,</span><span> </span><span>ylab</span><span> </span><span>=</span><span> </span><span>&quot;Average life span&quot;</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Simulated dimininishing marginal impact of income on mean life expectancy&quot;</span><span>,</span><span>
</span><span>xaxt</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>)</span><span>
</span><span>marks</span><span> </span><span>&lt;-</span><span> </span><span>seq</span><span>(</span><span>from</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>to</span><span> </span><span>=</span><span> </span><span>100000</span><span>,</span><span> </span><span>length.out</span><span> </span><span>=</span><span> </span><span>6</span><span>)</span><span>
</span><span>axis</span><span>(</span><span>1</span><span>,</span><span>at</span><span> </span><span>=</span><span> </span><span>marks</span><span>,</span><span>labels</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;$&quot;</span><span>,</span><span> </span><span>format</span><span>(</span><span>marks</span><span>,</span><span> </span><span>scientific</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>big.mark</span><span> </span><span>=</span><span> </span><span>&quot;,&quot;</span><span>)))</span><span>
</span><span>grid</span><span>()</span><span>
</span><span>data2</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>country</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>paste</span><span>(</span><span>&quot;Country&quot;</span><span>,</span><span> </span><span>1</span><span>:</span><span>100</span><span>),</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>n</span><span>),</span><span>
</span><span>country_mean</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>rnorm</span><span>(</span><span>100</span><span>,</span><span> </span><span>20000</span><span>,</span><span> </span><span>5000</span><span>),</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>n</span><span>),</span><span>
</span><span>country_sd</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>runif</span><span>(</span><span>100</span><span>,</span><span> </span><span>0.1</span><span>,</span><span> </span><span>0.9</span><span>),</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>n</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>mutate</span><span>(</span><span>income</span><span> </span><span>=</span><span> </span><span>rinc</span><span>(</span><span>n</span><span>(),</span><span> </span><span>mu</span><span> </span><span>=</span><span> </span><span>unique</span><span>(</span><span>country_mean</span><span>),</span><span> </span><span>sd</span><span> </span><span>=</span><span> </span><span>unique</span><span>(</span><span>country_sd</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>lifespan</span><span> </span><span>=</span><span> </span><span>rnorm</span><span>(</span><span>n</span><span>(),</span><span> </span><span>50</span><span> </span><span>+</span><span> </span><span>pmax</span><span>(</span><span>0</span><span>,</span><span> </span><span>income</span><span>)</span><span> </span><span>^</span><span> </span><span>0.5</span><span> </span><span>/</span><span> </span><span>7</span><span>,</span><span> </span><span>sd</span><span> </span><span>=</span><span> </span><span>5</span><span>))</span><span>
</span><span>data3</span><span> </span><span>&lt;-</span><span> </span><span>data2</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>mean_income</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>income</span><span>),</span><span>
</span><span>inequality</span><span> </span><span>=</span><span> </span><span>Gini</span><span>(</span><span>income</span><span>),</span><span>
</span><span>life_exp</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>lifespan</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>data3</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>-</span><span>country</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggpairs</span><span>()</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Relationship between three key development indicators&quot;</span><span>,</span><span>
</span><span>&quot;Inequality appears to have a country-level impact on life expectancy, even though the relationship is purely\nvia individual income.&quot;</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: Simulated data where individual income impacts on life expectancy, but country level inequality has no direct effect.&quot;</span><span>)</span><span>
</span><span># model with scaled data to make it easier to compare the two coefficients
</span><span>mod</span><span> </span><span>&lt;-</span><span> </span><span>lm</span><span>(</span><span>life_exp</span><span> </span><span>~</span><span> </span><span>mean_income</span><span> </span><span>+</span><span> </span><span>inequality</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>as.data.frame</span><span>(</span><span>scale</span><span>(</span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>data3</span><span>,</span><span> </span><span>-</span><span>country</span><span>))))</span><span>
</span><span>anova</span><span>(</span><span>mod</span><span>)</span><span>
</span><span>summary</span><span>(</span><span>mod</span><span>)</span><span>
</span><span>confint</span><span>(</span><span>mod</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://freerangestats.info/blog/2018/10/07/inequality-groups"> free range statistics - 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/PWnFpax1huQ" height="1"></a></div>