These functions perform life table calculations, to add selected life table parameters from others, using established demographic relationships.
gen_lx_from_qx(dt, id_cols, assert_na = T)
gen_qx_from_lx(dt, id_cols, assert_na = T)
gen_dx_from_lx(dt, id_cols, assert_na = T)
gen_nLx(dt, id_cols, assert_na = T)
gen_Tx(dt, id_cols, assert_na = T)
gen_ex(dt, assert_na = T)
[data.table()
]
Life table(s). Includes columns 'age_start',
'age_end', and all id_cols
. Additionally:
gen_lx_from_qx
requires 'qx'
gen_qx_from_lx
requires 'lx'
gen_dx_from_lx
requires 'lx'
gen_nLx
requires 'lx', 'ax', 'dx'
gen_Tx
requires 'nLx'
gen_ex
requires 'lx', 'Tx'
[character()
]
Columns that uniquely identify each row
of dt
. Must include 'age_start' and 'age_end'.
[logical()
]
Whether to check for NA values in the
generated variable.
dt
with column added for new life table parameter. Modifies
data.tables in place.
Parameter definitions:
For an age interval x to x+n:
mx = mortality rate (deaths / person-years)
qx = probability of death, conditional on survival to x-th birthday
ax = average years lived of those who died in the age interval
dx = number (or proportion of cohort) who died in the age interval
lx = number (or proportion of cohort) alive at the beginning of the age interval
ex = life expectancy, expected value for years lived after age x
Tx = total person-years lived beyond age x
nLx = total person-years lived in interval
gen_lx_from_qx: Use probability of death (qx) to get the proportion of survivors in the beginning of an age group (lx). l0 = 1; lx for ages > 0 is lx for previous age group times the probability of surviving the previous age group.
gen_qx_from_lx: Use survival proportion (lx) at age x and x+n to get
probability of death between x and x+n (qx). This relationship is an
algebraic equivalent to the gen_lx_from_qx
relationship as described.
Terminal age qx is set to 1.
gen_dx_from_lx: Calculate the proportion dying between age x and x+n (dx) as the difference between the proportion surviving (lx) at age x and the proportion surviving at age x+n. In the terminal age group all survivors die in the age group, so dx = lx.
gen_nLx: nLx is calculated as the sum of years lived by survivors and years lived by those who die between ages x and x+n. The first component is n times l(x+n). The second component is ax * dx. For terminal ages, assume that average person years lived equals number at the start of the age group divided by the mortality rate.
gen_Tx: Person-years lived above age x (Tx) is the cumulative sum of person-years lived by people in each age interval above age x (nLx). For the terminal age group, Tx = nLx.
gen_ex: Life expectancy (ex) at age x is based on person-years lived above the age group (Tx) and proportion of people alive at the start of age group (lx): ex = Tx / lx.
dt <- data.table::data.table(
sex = rep("both", 4),
age_start = c(0, 5, 10, 15),
age_end = c(5, 10, 15, Inf),
age_length = c(5, 5, 5, Inf),
mx = c(0.1, 0.2, 0.3, 0.4),
ax = c(2.5, 2.5, 2.5, 2.5)
)
dt[, qx := mx_ax_to_qx(mx, ax, age_length)]
#> sex age_start age_end age_length mx ax qx
#> <char> <num> <num> <num> <num> <num> <num>
#> 1: both 0 5 5 0.1 2.5 0.4000000
#> 2: both 5 10 5 0.2 2.5 0.6666667
#> 3: both 10 15 5 0.3 2.5 0.8571429
#> 4: both 15 Inf Inf 0.4 2.5 1.0000000
gen_lx_from_qx(dt, id_cols = c("sex", "age_start", "age_end"))
gen_dx_from_lx(dt, id_cols = c("sex", "age_start", "age_end"))
gen_nLx(dt, id_cols = c("sex", "age_start", "age_end"))
gen_Tx(dt, id_cols = c("sex", "age_start", "age_end"))
gen_ex(dt)
gen_qx_from_lx(dt, id_cols = c("sex", "age_start", "age_end"))