Mortality rates over time
Cynthia downloads the most up-to-date mortality data for Belgium from the Human Mortality Database (HMD). This data set is preloaded as life_table
.
In this exercise, you will use two common R functions. with()
(docs) allows you to evaluate an R expression in a local environment constructed from a data frame. This avoids the need of repeatedly typing life_table$
to extract columns. For instance, the log mortality rates of 18-year-olds over the years can be extracted using:
with(life_table, log(qx[age == 18]))
with()
is particularly handy in combination with subset()
(docs). For instance, the log mortality rate of 18-year-olds from the life table of year 1999 can be returned using:
with(subset(life_table, year == 1999), log(qx[age == 18]))
This exercise is part of the course
Life Insurance Products Valuation in R
Exercise instructions
- Explore
life_table
. Print out the first 6 rows usinghead()
and compute therange()
(docs) of theyear
variable. - Complete the code with proper use of
subset()
onlife_table
such that the mortality rates of an 18-year-old female throughout the years are plotted. - Again make use of
subset()
to select the life table of 1950 and plot the mortality rate curve in that year.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Explore life_table
___
___
# Plot the logarithm of the female mortality rates for (18) by year
with(___(___, ___),
plot(year, log(qx),
type = "l", main = "Log mortality rates (Belgium, females, 18-year-old)",
xlab = "Year t", ylab = expression(paste("Log mortality rate ", log(q[18])))))
# Plot the logarithm of the female mortality rates in the year 1950 by age
with(___(___, ___),
plot(age, log(qx),
type = "l", main = "Log mortality rates (Belgium, females, 1950)",
xlab = "Age x", ylab = expression(paste("Log mortality rate ", log(q[x])))))