Given a data.table with at least two of variables mx, ax, and qx, compute a complete life table. This is a helper function that combines many other functions in this package to calculate px, lx, dx, Tx, nLx, and ex.

lifetable(
  dt,
  id_cols,
  preserve_u5 = FALSE,
  assert_na = TRUE,
  format_long = FALSE
)

Arguments

dt

[data.table()]
Input life tables. Must include 2/3 of 'qx', 'mx', and 'ax', in addition to all id_cols.

id_cols

[character()]
Columns that uniquely identify each row of dt. Must include 'age_start' and 'age_end'.

preserve_u5

[logical()]
Whether to preserve under-5 qx estimates rather than recalculating them based on mx and ax.

assert_na

[logical()]
Whether to assert that there is no missingness.

format_long

[logical(1)]
Whether to format output with a 'life_table_parameter' column and a column for each summary statistic (rather than a column for each 'life_table_parameter' and summary statistic pair).

Value

[data.table()]

Input life table(s) with additional columns: px, lx, dx, Tx, nLx, ex. Or, if format_long is TRUE, additional columns 'life_table_parameter' and 'value' with data long on life table parameter.

Details

Note that while it typically takes estimation techniques to arrive at mx, ax, and qx from empirical data, the solution of the remaining life table parameters as compiled in this function is deterministic once mx, ax, and qx are specified. The steps of this solution are as follows:

  1. Calculate mx, ax, or qx if one of the three is missing

  2. Recalculate qx if all three of mx, ax, and qx are provided to confirm the three agree. See mx_qx_ax_conversions functions. Will not recalculate under-5 qx if preserve_u5 is true.

  3. Compute px from qx

  4. gen_lx_from_qx()

  5. gen_dx_from_lx()

  6. gen_nLx()

  7. gen_Tx()

  8. gen_ex()

  9. Replace terminal (age_end = Inf) ax with terminal ex

Examples

dt <- data.table::data.table(
  sex = rep("both", 4),
  age_start = c(0, 5, 10, 15),
  age_end = c(5, 10, 15, Inf),
  mx = c(0.1, 0.2, 0.3, 0.4),
  ax = c(2.5, 2.5, 2.5, 2.5)
)
dt <- lifetable(dt, id_cols = c("sex", "age_start", "age_end"))