?hmsidwR::gho_lifetables
Appendix A — Life Tables, Markov Chain and APIs
A.1 Life Tables and Life Expectancy
Back in the 1700s the Swiss mathematician and physicist Daniel Bernoulli (1700  1782) developed the use of a life table model by differentiating life tables based on specific causes of death.^{1}
Originally made by the English scientist John Graunt (16201674), for the analysis of the mortality of the population of London and the impact of different diseases. Life tables contain fundamental statistics for the calculation of probabilities of deaths and the computation of life expectancy at birth and at different ages.
A.1.1 Life tables components
More recent life tables are standardized to be used for a population of 100 000 at age 0.
The probability of survival is given by: p_x=1q_x
Let’s start constructing a life table
The Global Life Tables are included in the hmsidwR package as gho_lifetables
dataset. This dataset has been released by the WHO, and contains various indicators.
The construction of the Global Life Tables takes consideration of agespecific mortality patterns, which is the main improvements made on life tables construction since the first set of model life tables published by the United Nations in 1955, see^{2} for more information about a detailed procedure.
To have a look at the package documentation for this dataset, use:
The indicator of interest for rebuilding a life table are:

lx
 number of people left alive at age x age
These two key elements are crucial for building the life tables.
lx < hmsidwR::gho_lifetables %>%
distinct() %>%
filter(
indicator == "lx",
year == "2019"
)
The probability of survival is calculated as follow:
p_x = \frac{l_x}{l_{x+1}}
px < lx_f$lx / lag(lx_f$lx)
data.frame(
x,
lx = round(lx_f),
dx = round(c(diff(lx_f$lx), 0)),
px,
qx = 1  lead(px),
Lx = c(
(lx_f$lx[1] + (lx_f$lx[2])) / 2,
5 * (lx_f$lx[1] + lead(lx_f$lx[1])) / 2
)
) %>%
head()
#> x lx dx px qx Lx
#> 1 <1 100000 2584 NA 0.025843406 98707.83
#> 2 0104 97416 915 0.9741566 0.009393050 484790.72
#> 3 0509 96501 366 0.9906069 0.003790526 481588.68
#> 4 1014 96135 249 0.9962095 0.002588600 480052.07
#> 5 1519 95886 305 0.9974114 0.003185671 478666.28
#> 6 2024 95581 399 0.9968143 0.004179281 476903.98
hmsidwR::gho_lifetables %>%
distinct() %>%
mutate(indicator = gsub(" .*", "", indicator)) %>%
filter(
indicator == "nLx",
year == "2019",
sex == "female"
) %>%
head()
#> # A tibble: 6 × 5
#> indicator year age sex value
#> <chr> <dbl> <ord> <chr> <dbl>
#> 1 nLx 2019 <1 female 97857.
#> 2 nLx 2019 0104 female 387467.
#> 3 nLx 2019 0509 female 481589.
#> 4 nLx 2019 1014 female 480052.
#> 5 nLx 2019 1519 female 478666.
#> 6 nLx 2019 2024 female 476904.
A.1.2 Life expectancy
Life expectancy is the expected number of years a person will live, based on current age and prevailing mortality rates. There are several methods to calculate life expectancy, but one common approach is to use the actuarial life table, which is a statistical table that provides the mortality rates for a population at different ages. The following steps can be used to calculate life expectancy using a life table:
Identify the relevant mortality rates for the population and time period of interest. Calculate the probability of surviving to each age, given the mortality rates. Multiply the probability of surviving to each age by the remaining life expectancy at that age to obtain the expected number of years of life remaining at each age. Sum the expected number of years of life remaining at each age to obtain the total life expectancy. Note that life expectancy is a statistical estimate and can be influenced by many factors, such as lifestyle, health, and environmental factors, so actual individual life expectancy can vary widely.
Here are some key references for calculating life expectancy:
United Nations World Population Prospects  The UN provides detailed life tables and population data, including life expectancy, for countries and regions around the world.
Centers for Disease Control and Prevention (CDC)  The CDC provides life tables for the United States, as well as information on how life expectancy is calculated and factors that affect it.
World Health Organization (WHO)  The WHO provides information on global health and life expectancy, including data and reports on trends in life expectancy and mortality.
Actuarial Science textbooks  Books such as “Actuarial Mathematics” by Bowers, Gerber, Hickman, Jones, and Nesbitt, or “An Introduction to Actuarial Mathematics” by Michel Millar, provide comprehensive coverage of the methods and mathematics used in calculating life expectancy.
Journal articles  Articles in actuarial and demographic journals, such as the North American Actuarial Journal or Demographic Research, often provide indepth coverage of the latest research and methods for calculating life expectancy.
A.2 Markov Chain
A Markov chain is a stochastic model describing a sequence of possible events in which the probability of each event depends only on the state attained in the previous event. The state space of a Markov chain is the set of all possible states of the system. The transition probabilities are the probabilities of moving from one state to another. The transition matrix is a square matrix that describes the transition probabilities between states.
The code for this replication of the Markov Chain is from Dobrow  Bayesian analysis of infectious diseases book (chapter4). It shows clearly how to create a Markov chain and calculate the transition probabilities.
set.seed(000)
markov < function(init, mat, n, labels) {
if (missing(labels)) labels < 1:length(init)
simlist < numeric(n + 1)
states < 1:length(init)
simlist[1] < sample(states, 1, prob = init)
for (i in 2:(n + 1)) {
simlist[i] < sample(states, 1, prob = mat[simlist[i  1], ])
}
labels[simlist]
}
markov(init = init, mat = P, n = 100)
#> [1] 1 1 1 2 1 1 2 1 2 1 1 1 1 2 2 1 1 2 1 1 2 1 1 2 2 2 2 2 2 1 1 1 2 2 2 1 2
#> [38] 1 1 2 2 1 2 1 2 1 2 2 2 1 2 2 1 1 1 1 1 1 2 1 1 2 2 2 2 1 1 1 2 2 1 1 2 2
#> [75] 2 2 1 2 2 1 2 2 1 1 1 2 2 1 1 1 1 1 1 2 1 2 1 1 1 2 1
# Define the number of transitions
n_transitions < 10000
# Simulate Markov chain
simulated_chain < markov(init, P, n_transitions)
# Calculate the transition probabilities
transition_counts < table(
simulated_chain[1],
simulated_chain[(n_transitions + 1)]
)
transition_probabilities < transition_counts / rowSums(transition_counts)
transition_probabilities
#>
#> 1 2
#> 1 0.5140224 0.4859776
#> 2 0.4844249 0.5155751
# Calculate prior probabilities
prior_probabilities < table(simulated_chain[n_transitions]) / n_transitions
# Calculate posterior probabilities
posterior_probabilities < transition_probabilities %*% prior_probabilities
# Print prior probabilities
print("Prior probabilities:")
#> [1] "Prior probabilities:"
print(prior_probabilities)
#>
#> 1 2
#> 0.4992 0.5008
# Print posterior probabilities
print("Posterior probabilities:")
#> [1] "Posterior probabilities:"
print(posterior_probabilities)
#>
#> [,1]
#> 1 0.4999776
#> 2 0.5000249
A.3 Collecting Data with APIs
Collecting data to use in a research analysis involves a selection of sources and methods to use for optimizing computational time when downloading and reading the files. Once data is set and ready to use a further step is required to make the data suitable for the selected model. The source of data is an important variable. Generally, data can be downloaded by using an API (application programming interface) which allow the user to get access to data directly from source, with the use of specified backend computations. There are alternatives at using an API; data can be obtained by downloading it directly into the computer,or loaded through library packages. Usually, available files are provided under various forms such as delimited type of files, .csv, .xls, .json, and other types. Here is an example of how to use an API for downloading a file directly onto your computer.
A.3.1 Download Data with APIs
A.3.1.1 IHME Data APIs

SDG Query Input:
 GetGoal
 GetIndicator
 GetTarget
 GetLocation
 GetAgeGroup
 GetScenario
 GetSexSDG

Query Input for Results:
 GetResultsByTarget
 GetResultsByIndicator
 GetResultsByLocation
 GetResultsByYear
Use the function get_data
to download data from the IHME API. The function requires the following arguments:
url = "https://api.healthdata.org/sdg/v1
key = "YOURKEY"
sdg = "GetResultsByLocation?location_id=86&indicator_id=1002&year=2019"
Build the query to download results by adding specification of the data.
GetResultsByLocation?location_id=86&indicator_id=1002&year=2019? location_id & indicator_id ...
data < get_data(
url = "https://api.healthdata.org/sdg/v1",
key = YourKeyHere,
sdg = "GetResultsByLocation?location_id=86&indicator_id=1002&year=2019"
)
data
“Life Table  an Overview  ScienceDirect Topics,” n.d., https://www.sciencedirect.com/topics/medicineanddentistry/lifetable.↩︎
“Modified Logit Life Table System: Principles, Empirical Validation, and Application: Population Studies: Vol 57, No 2,” n.d., https://www.tandfonline.com/doi/abs/10.1080/0032472032000097083.↩︎