<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/demystifying-life-expectancy-calculations-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/05/31/life-expectancy"> free range statistics - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Life expectancy and death rates</h2>
<p>A chance comment on Twitter about historical increases in life expectancy (in the USA) with fairly constant death rates got me wondering about the exact calculations of these things so I did some Googling. Warning on what follows – I am strictly an amateur in demographics, and was deliberately working things out from basics as a learning exercise, so there are possibly better ways to do some of the things below.</p>
<p>According to Wikipedia which is always good at this sort of thing, <a rel="nofollow" href="https://en.wikipedia.org/wiki/Life_expectancy" target="_blank">life expectancy can be calculated two ways</a>:</p>
<ul>
<li>by observing a cohort until they are all dead, and taking the average age. Obviously this only works with historical data. This is referred to as “cohort life expectancy at birth”.</li>
<li>by taking a set of death rates by age bracket, and calculating the average age of death of a hypothetical cohort of people who move through life with those death rates. This is called “period life expectancy at birth” and is the method used for reporting by national statistics offices.</li>
</ul>
<p>By convention, period life expectancy applies <em>today’s</em> death rates to an infant born today. Hence, there’s no estimation of those death rates changing; we assume that by the time this infant is 60 years old, the death rate of 60 year olds in 2078 will be the same as now. This has some rather obvious flaws, but it’s also easy to see why that approach is convenient, particularly for standardising data across countries. It means that the reported life expectancies aren’t really the best estimates of how long a baby born today will live conditional on standard expectations about civilization continuing at all, because we actually expect death rates to continue to decline (although some days I wonder). But it does mean that “life expectancy” is very clearly defined and with minimum discretion needed in the calculation. So long as we remember that period life expectancy is really a summary statistic of today’s death rates by age, not really how long people are expected to live but a sort of hypothetical life length, we are ok.</p>
<p>Life expectancy can go up while crude death rates are also going up (or vice versa) because of changing age composition in a population. All the age-specific death rates (and hence any age-adjusted death rate) might be going down, but if more and more people are in the higher-rate age brackets the overall crude death rate might be increasing. We can see this happening for Japan in the image below:</p>
<p><img width="100%" src="http://freerangestats.io/img/0123-life-death-csp.svg"></p>
<p>Here’s the R code that grabbed the data for that image and built it. ggplot2 afficianados might be interested in how I’ve used <code>geom_blank</code> to increase the scales a bit beyond the defaults, which were too narrow and had my text banging against the axes (a common problem). It’s a bit of a hack but it works nicely:</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>frs</span><span>)</span><span> </span><span># if necessary, install with devtools::install_github(&quot;ellisp/frs-r-package/pkg&quot;)
</span><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># Use WDIsearch to find the indicators we want
# View(WDIsearch(&quot;life&quot;))
</span><span>
</span><span>death_rate</span><span> </span><span>&lt;-</span><span> </span><span>WDI</span><span>(</span><span>indicator</span><span> </span><span>=</span><span> </span><span>&quot;SP.DYN.CDRT.IN&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>2100</span><span>)</span><span>
</span><span>life_exp</span><span> </span><span>&lt;-</span><span> </span><span>WDI</span><span>(</span><span>indicator</span><span> </span><span>=</span><span> </span><span>&quot;SP.DYN.LE00.IN&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>2100</span><span>)</span><span>
</span><span>selected_countries</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;United Arab Emirates&quot;</span><span>,</span><span> </span><span>&quot;Suriname&quot;</span><span>,</span><span> </span><span>&quot;Georgia&quot;</span><span>,</span><span> </span><span>&quot;Barbados&quot;</span><span>,</span><span> </span><span>&quot;Kiribati&quot;</span><span>,</span><span> </span><span>&quot;Moldova&quot;</span><span>,</span><span> </span><span>&quot;Ghana&quot;</span><span>,</span><span> </span><span>&quot;Japan&quot;</span><span>,</span><span> </span><span>&quot;New Zealand&quot;</span><span>)</span><span>
</span><span># connected scatter plot:
</span><span>death_rate</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>country</span><span> </span><span>%in%</span><span> </span><span>selected_countries</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>inner_join</span><span>(</span><span>life_exp</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;iso2c&quot;</span><span>,</span><span> </span><span>&quot;country&quot;</span><span>,</span><span> </span><span>&quot;year&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span># take out NAs which confuse the geom_text call later:
</span><span> </span><span>filter</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>SP.DYN.LE00.IN</span><span>))</span><span> </span><span>%>%</span><span>
</span><span># order country by life expectancy so graphic facets make some sense:
</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>SP.DYN.LE00.IN</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>SP.DYN.CDRT.IN</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>SP.DYN.LE00.IN</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_path</span><span>()</span><span> </span><span>+</span><span>
</span><span># add labels of the year at max and min points
</span><span> </span><span>geom_text</span><span>(</span><span>aes</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>year</span><span> </span><span>%in%</span><span> </span><span>range</span><span>(</span><span>year</span><span>),</span><span> </span><span>year</span><span>,</span><span> </span><span>&quot;&quot;</span><span>)),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey30&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</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>scales</span><span> </span><span>=</span><span> </span><span>&quot;free&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span># these next two geom_blank calls are a hack to make sure enough space is given for the labels which
</span><span> </span><span># otherwise crash into the axes, and can't be easily controlled with scale limits:
</span><span> </span><span>geom_blank</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>SP.DYN.CDRT.IN</span><span> </span><span>*</span><span> </span><span>1.1</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>SP.DYN.LE00.IN</span><span> </span><span>*</span><span> </span><span>1.05</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_blank</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>SP.DYN.CDRT.IN</span><span> </span><span>*</span><span> </span><span>0.9</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>SP.DYN.LE00.IN</span><span> </span><span>*</span><span> </span><span>0.95</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>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Crude death rate (per 1,000 people)&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Life Expectancy&quot;</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>ggtitle</span><span>(</span><span>&quot;Differing relationship of crude death rate and life expectancy&quot;</span><span>,</span><span>
</span><span>&quot;Countries with aging populations (eg Georgia, Japan, in the below) can experience increases in both simultaneously.&quot;</span><span>)</span></code></pre>
</figure>
<h2>Calculating life expectancy</h2>
<p>To be sure I understood how it worked, I had a go at estimating some life expectancies myself. I started with some <a rel="nofollow" href="https://www.ined.fr/en/everything_about_population/data/france/deaths-causes-mortality/mortality-rates-sex-age/" target="_blank">French death rates per age group</a> from 2015, because these were the most convenient to hand. Death rates are reported as deaths per 1,000 people per year. Here’s how they look when you convert them into the probability of surviving the year at any particular age:</p>
<p><img width="100%" src="http://freerangestats.io/img/0123-french-rates.svg"></p>
<p>Here’s how that was drawn. The <code>french_death_rates_2015</code> object <a rel="nofollow" href="https://github.com/ellisp/frs-r-package/blob/master/pkg/R/death_rates.R" target="_blank">comes from the <code>frs</code> package</a> where I store miscellaneous things to help out with this blog.</p>
<figure>
<pre><code data-lang="r"><span>french_death_rates_2015</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>sex</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>-</span><span>age</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>age</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>(</span><span>1000</span><span> </span><span>-</span><span> </span><span>value</span><span>)</span><span> </span><span>/</span><span> </span><span>1000</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>sex</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>x</span><span> </span><span>=</span><span> </span><span>&quot;Age&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Probability of surviving to the next year&quot;</span><span>,</span><span> </span><span>colour</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;https://www.ined.fr/en/everything_about_population/data/france/deaths-causes-mortality/mortality-rates-sex-age/&quot;</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>0.8</span><span>,</span><span> </span><span>1</span><span>),</span><span> </span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>100</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;French death rates in 2015&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>c</span><span>(</span><span>0.6</span><span>,</span><span> </span><span>0.6</span><span>))</span></code></pre>
</figure>
<p>To actually convert these probabilities into a life expectancy, we need to estimate the proportion of our hypothetical population that will die at each age. There’s lots of different ways you might do this but the one that came to mind to me was:</p>
<ul>
<li>Create interpolated death rates for each integer age (because typically death rates are given for a bracket of ages, not every single age)</li>
<li>Estimate the proportion still alive at any age, starting with 1 at birth and 0 at some arbitrary end point (I chose 150 which seems reasonable). This is the cumulative product of the yearly survival rates, which are of course 1 – death rate (where death rate has been converted to a probability rather than a factor out of 1,000).</li>
<li>Estimate the difference between those proportions for each year, which gives you the proportion of the total population that died in that year.</li>
<li>Take the average of all of our ages, weighted by the proportion of the population that died each year.</li>
</ul>
<p>Now that I write it up this seems a bit more involved than I would have thought. It’s possible there’s a simpler way. Anyway, here’s the function that implements my approach</p>
<figure>
<pre><code data-lang="r"><span>#' @param age vector of ages in years
#' @param rate death rate in deaths per 1000 people alive at the given age. Must be the same length as age.
#' @param data optionally, a data frame or matrix where the first column is used as age and the second column as rate
#' @examples
#' life_expectancy(data = french_death_rates_2015[ , c(&quot;age&quot;, &quot;female&quot;)])
#' life_expectancy(age = french_death_rates_2015$age, rate = french_death_rates_2015$male)
</span><span>life_expectancy</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>age</span><span> </span><span>=</span><span> </span><span>data</span><span>[,</span><span> </span><span>1</span><span>],</span><span> </span><span>rate</span><span> </span><span>=</span><span> </span><span>data</span><span>[</span><span> </span><span>,</span><span> </span><span>2</span><span>],</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>NULL</span><span>){</span><span>
</span><span>if</span><span>(</span><span>length</span><span>(</span><span>age</span><span>)</span><span> </span><span>!=</span><span> </span><span>length</span><span>(</span><span>rate</span><span>)</span><span> </span><span>|</span><span> </span><span>!</span><span>class</span><span>(</span><span>age</span><span>)</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;numeric&quot;</span><span>,</span><span> </span><span>&quot;integer&quot;</span><span>)</span><span> </span><span>|</span><span> </span><span>!</span><span>class</span><span>(</span><span>rate</span><span>)</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;numeric&quot;</span><span>,</span><span> </span><span>&quot;integer&quot;</span><span>)){</span><span>
</span><span>stop</span><span>(</span><span>&quot;age and rate should be integer or numeric vectors of the same length&quot;</span><span>)</span><span>
</span><span>}</span><span>
</span><span>if</span><span>(</span><span>max</span><span>(</span><span>rate</span><span>)</span><span> </span><span>!=</span><span> </span><span>1000</span><span>)</span><span> </span><span>{</span><span>
</span><span>stop</span><span>(</span><span>&quot;The highest value of rate should be exactly 1000, indicating the age at which everyone is guaranteed to die at or before.&quot;</span><span>)</span><span>
</span><span>}</span><span>
</span><span># interpolated version so we have a death rate for every age:
</span><span> </span><span>dr</span><span> </span><span>&lt;-</span><span> </span><span>stats</span><span>::</span><span>approx</span><span>(</span><span>age</span><span>,</span><span> </span><span>rate</span><span>,</span><span> </span><span>xout</span><span> </span><span>=</span><span> </span><span>0</span><span>:</span><span>max</span><span>(</span><span>age</span><span>))</span><span>
</span><span>prop_alive</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>cumprod</span><span>((</span><span>1000</span><span> </span><span>-</span><span> </span><span>dr</span><span>$</span><span>y</span><span>)</span><span> </span><span>/</span><span> </span><span>1000</span><span>))</span><span>
</span><span>deaths</span><span> </span><span>&lt;-</span><span> </span><span>-</span><span>diff</span><span>(</span><span>prop_alive</span><span>)</span><span>
</span><span>return</span><span>(</span><span>sum</span><span>(</span><span>deaths</span><span> </span><span>*</span><span> </span><span>0</span><span>:</span><span>max</span><span>(</span><span>age</span><span>)))</span><span>
</span><span>}</span></code></pre>
</figure>
<p>With my original data it gives plausible results: 85.3 for females and 79.1 for males. These differ minutely from the <a rel="nofollow" href="http://www.worldlifeexpectancy.com/france-life-expectancy" target="_blank">published figures</a> for France in 2015 of 85.4 and 79.4; I imagine the difference is in how the age brackets are treated. I made some fairly cavalier simplifications in using the middle year of the age bracket as a reference point and interpolating between those points, which on reflection will cause some small problems when there are rapid changes in mortality (the most likely being from the age 0-1 bracket to the age 2-5 bracket).</p>
<h2>Impact on life expectancy of changing a single age groups death rate</h2>
<p>It’s interesting to play with how changing death rates at a particular part of the life cycle change the life expectancy calculation. Infant mortality is the biggest driver. The intuition behind this is that everyone who is born alive gets a chance to survive the first year, so an improvement here impacts on the biggest part of our population. If you improve the odds of surviving from 110 to 111 it has minimal impact on life expectancy because most people are already dead at that point.</p>
<p>So here’s what happens if we make infant mortality (ie death per thousand in the first year of life) arbitrarily small or large, using French males in 2015 as a reference point. The blue dot represents the current actual death rate in first year of life and overall life expectancy; the rest of the line shows what happens for hypothetical different death rates (which for France with its low infant mortality, historically and internationally speaking, means higher ones):</p>
<p><img width="100%" src="http://freerangestats.io/img/0123-0.svg"></p>
<p>Obviously, if we say 1,000 out of 1,000 people die in the first year, our life expectancy becomes zero. More realistically, if death rates in the first year went up to 250 out 1,000 (which would be around the worst current day level, but well within historical ranges), life expectancy comes down from 79 to around 50, <em>despite</em> death rates at all other ages staying the same as in 2015 France.</p>
<p>On the other hand, what if we make a spike in deaths at age 18, perhaps due to a strange disease or social custom that makes this a uniquely hazardous age (the the hazard going down to normal levels at age 19). Even if the entire population dies at this age, the life expectancy is still 17 or so; and improvements in mortality rates for 18-year olds accordingly have less relative impact on life expectancy than was the case when we “improved” infant mortality:</p>
<p><img width="100%" src="http://freerangestats.io/img/0123-17.5.svg"></p>
<p>For the older population, the issue is more marked again:</p>
<p><img width="100%" src="http://freerangestats.io/img/0123-85.svg"></p>
<p>Finally, consider the case where a medical advance guarantees a uniform yearly survival rate for anyone who reaches 85 until they turn 150:</p>
<p><img width="100%" src="http://freerangestats.io/img/0123-85-over.svg"></p>
<p>Even if we make that survival rate 0 (ie all 85 year olds are guaranteed to reach 150), life expectancy only gets up to about 91.</p>
<p>The code for those simulations is below. It’s a bit repetitive, but with the fiddles I wanted to labels and so on and with limited future re-use expected, it didn’t seem worth writing a function to do this job.</p>
<figure>
<pre><code data-lang="r"><span>m</span><span> </span><span>&lt;-</span><span> </span><span>french_death_rates_2015</span><span>[</span><span> </span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;age&quot;</span><span>,</span><span> </span><span>&quot;male&quot;</span><span>)]</span><span>
</span><span>the_age</span><span> </span><span>&lt;-</span><span> </span><span>which</span><span>(</span><span>m</span><span>$</span><span>age</span><span> </span><span>==</span><span> </span><span>0</span><span>)</span><span> </span><span># row in the data frame for this age of interest
</span><span>current_level</span><span> </span><span>&lt;-</span><span> </span><span>life_expectancy</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>m</span><span>)</span><span>
</span><span>data.frame</span><span>(</span><span>
</span><span>dr</span><span> </span><span>=</span><span> </span><span>0</span><span>:</span><span>1000</span><span>,</span><span>
</span><span>le</span><span> </span><span>=</span><span> </span><span>sapply</span><span>(</span><span>0</span><span>:</span><span>1000</span><span>,</span><span> </span><span>function</span><span>(</span><span>i</span><span>){</span><span>
</span><span>m</span><span>[</span><span>1</span><span>,</span><span> </span><span>2</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>i</span><span> </span><span># m is modified only within the environment of this function
</span><span> </span><span>life_expectancy</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>m</span><span>)</span><span> </span><span>})</span><span>
</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>dr</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>le</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>annotate</span><span>(</span><span>&quot;point&quot;</span><span>,</span><span> </span><span>french_death_rates_2015</span><span>[</span><span>the_age</span><span>,</span><span> </span><span>&quot;male&quot;</span><span>],</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>current_level</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Impact of different infant mortality rates on life expectancy&quot;</span><span>,</span><span>
</span><span>&quot;Keeping death rates at all other ages the same, at levels for French males in 2015&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;Deaths in first year of life per thousand live births&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Life expectancy at birth for overall cohort&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ylim</span><span>(</span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>100</span><span>))</span><span>
</span><span>the_age</span><span> </span><span>&lt;-</span><span> </span><span>which</span><span>(</span><span>m</span><span>$</span><span>age</span><span> </span><span>==</span><span> </span><span>&quot;17.5&quot;</span><span>)</span><span>
</span><span>data.frame</span><span>(</span><span>
</span><span>dr</span><span> </span><span>=</span><span> </span><span>0</span><span>:</span><span>1000</span><span>,</span><span>
</span><span>le</span><span> </span><span>=</span><span> </span><span>sapply</span><span>(</span><span>0</span><span>:</span><span>1000</span><span>,</span><span> </span><span>function</span><span>(</span><span>i</span><span>){</span><span>
</span><span>m</span><span>[</span><span>the_age</span><span>,</span><span> </span><span>2</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>i</span><span>
</span><span>m</span><span> </span><span>&lt;-</span><span> </span><span>rbind</span><span>(</span><span>m</span><span>,</span><span> </span><span>data.frame</span><span>(</span><span>age</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>16.5</span><span>,</span><span> </span><span>18.5</span><span>),</span><span> </span><span>male</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.3</span><span>,</span><span> </span><span>0.3</span><span>)))</span><span>
</span><span>life_expectancy</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>m</span><span>)</span><span> </span><span>})</span><span>
</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>dr</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>le</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>annotate</span><span>(</span><span>&quot;point&quot;</span><span>,</span><span> </span><span>french_death_rates_2015</span><span>[</span><span>the_age</span><span>,</span><span> </span><span>&quot;male&quot;</span><span>],</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>current_level</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Impact of different death rates in early adulthood on life expectancy&quot;</span><span>,</span><span>
</span><span>&quot;Keeping death rates at all other ages the same, at levels for French males in 2015&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;Deaths at age 17.5 per thousand people living to 16.5&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Life expectancy at birth for overall cohort&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ylim</span><span>(</span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>100</span><span>))</span><span>
</span><span>the_age</span><span> </span><span>&lt;-</span><span> </span><span>which</span><span>(</span><span>m</span><span>$</span><span>age</span><span> </span><span>==</span><span> </span><span>&quot;85&quot;</span><span>)</span><span>
</span><span>data.frame</span><span>(</span><span>
</span><span>dr</span><span> </span><span>=</span><span> </span><span>0</span><span>:</span><span>1000</span><span>,</span><span>
</span><span>le</span><span> </span><span>=</span><span> </span><span>sapply</span><span>(</span><span>0</span><span>:</span><span>1000</span><span>,</span><span> </span><span>function</span><span>(</span><span>i</span><span>){</span><span>
</span><span>m</span><span>[</span><span>the_age</span><span>,</span><span> </span><span>2</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>i</span><span>
</span><span>m</span><span> </span><span>&lt;-</span><span> </span><span>rbind</span><span>(</span><span>m</span><span>,</span><span> </span><span>data.frame</span><span>(</span><span>age</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>84</span><span>,</span><span> </span><span>86</span><span>),</span><span> </span><span>male</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>78</span><span>,</span><span> </span><span>78</span><span>)))</span><span>
</span><span>life_expectancy</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>m</span><span>)</span><span> </span><span>})</span><span>
</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>dr</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>le</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>annotate</span><span>(</span><span>&quot;point&quot;</span><span>,</span><span> </span><span>french_death_rates_2015</span><span>[</span><span>the_age</span><span>,</span><span> </span><span>&quot;male&quot;</span><span>],</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>current_level</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Impact of different death rates in old age on life expectancy&quot;</span><span>,</span><span>
</span><span>&quot;Keeping death rates at all other ages the same, at levels for French males in 2015&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;Deaths at age 85 per thousand people living to 84&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Life expectancy at birth for overall cohort&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ylim</span><span>(</span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>100</span><span>))</span><span>
</span><span>the_age</span><span> </span><span>&lt;-</span><span> </span><span>which</span><span>(</span><span>m</span><span>$</span><span>age</span><span> </span><span>==</span><span> </span><span>&quot;85&quot;</span><span>)</span><span>
</span><span>mr</span><span> </span><span>&lt;-</span><span> </span><span>nrow</span><span>(</span><span>m</span><span>)</span><span> </span><span>-</span><span> </span><span>1</span><span>
</span><span>data.frame</span><span>(</span><span>
</span><span>dr</span><span> </span><span>=</span><span> </span><span>0</span><span>:</span><span>1000</span><span>,</span><span>
</span><span>le</span><span> </span><span>=</span><span> </span><span>sapply</span><span>(</span><span>0</span><span>:</span><span>1000</span><span>,</span><span> </span><span>function</span><span>(</span><span>i</span><span>){</span><span>
</span><span>m</span><span>[</span><span>the_age</span><span>:</span><span>mr</span><span>,</span><span> </span><span>2</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>i</span><span>
</span><span>life_expectancy</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>m</span><span>)</span><span> </span><span>})</span><span>
</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>dr</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>le</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>annotate</span><span>(</span><span>&quot;point&quot;</span><span>,</span><span> </span><span>french_death_rates_2015</span><span>[</span><span>the_age</span><span>,</span><span> </span><span>&quot;male&quot;</span><span>],</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>current_level</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Impact of different death rates in old age on life expectancy&quot;</span><span>,</span><span>
</span><span>&quot;Keeping death rates at all other ages the same, at levels for French males in 2015&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;Deaths at age 85 and all older ages until 150 (when all die), per thousand people living to 84&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Life expectancy at birth for overall cohort&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ylim</span><span>(</span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>100</span><span>))</span></code></pre>
</figure>
<p>Hmm, ok, interesting. I have some more thoughts about the arithmetic of demography, but they can come in a subsequent post.</p>
	<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/05/31/life-expectancy"> 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/82dvlmTEzT4" height="1"></a></div>