1. Learn
  2. /
  3. Courses
  4. /
  5. Life Insurance Products Valuation in R

Connected

Exercise

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]))

Instructions

100 XP
  • Explore life_table. Print out the first 6 rows using head() and compute the range() (docs) of the year variable.
  • Complete the code with proper use of subset() on life_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.