life_table_prep: Prepare death data for use with life_table()

life_table_prepR Documentation

Prepare death data for use with life_table()

Description

Processes individual-level death data to create a standardized data table. This table is collapsed/aggregated by age bin and optionally, by demographics, for use with life_table.

Usage

life_table_prep(
  ph.data,
  cuts = c(0, 1, 5, 10, 15, 18, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85),
  dobvar = "date_of_birth",
  dodvar = "date_of_death",
  group_by = NULL
)

Arguments

ph.data

a data.table or data.frame. Must contain individual-level death data with the date of birth, date of death, and any demographics which you want to use to aggregate the resulting table.

The default value is ph.data = NULL.

cuts

integer vector of any length greater than 1 (typically of length ~ 20). It specifies the cut-points for the age groupings to be created in the data. Each number represents the beginning of an interval and the final cut extends through to the maximum age observed. For example, when cuts = c(0, 5, 10, 20), the data will be grouped into ages [0,5), [5,10), [10,20), and [20, infinity).

The default is cuts= c(0, 1, 5, 10, 15, 18, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85), which creates the standard age groupings used by WA DOH.

dobvar

character vector of length one identifying a column with the decedent's date of birth. The referenced column must be of class 'date' or class 'character' in the format "YYYY-MM-DD" or "YYYY/MM/DD."

The default is dobvar = "date_of_birth", which is the dob variable available via rads::get_data_death().

dodvar

character vector of length one identifying a column with the decedent's date of death. The referenced column must be of class 'date' or class 'character' in the format "YYYY-MM-DD" or "YYYY/MM/DD."

The default is dodvar = "date_of_death", which is the dod variable available via rads::get_data_death().

group_by

a character vector of indeterminate length. This is used to specify all the variables by which you want to group (a.k.a. stratify) the results. For example, if you specified group_by = c('chi_sex', 'chi_race_6'), the results would be grouped by each combination of sex and race. If you leave it blank (i.e., group_by = NULL), it will only provide the death counts and death fractions by age group described by cuts.

The default is group_by = NULL

Details

Note that population data (from get_population) must be merged onto the returned data.table before running it through life_table.

Value

a data.table with deaths aggregated by any demographics specified in the group_by argument, as well as ages (age group), deaths (deaths per demographic group and age group), and fraction (the mean fraction of the age interval lived by those who died in that interval).

Examples


 # create data set ----
set.seed(98104)

deaths <- data.table::data.table(
  date_of_death = as.Date("2020-01-01") + sample(0:365, 10000, replace = TRUE),
  race_eth = rep_len(c("AIAN", "Asian", "Black", "Hispanic", "NHPI", "White"),
  length.out = 10000),
  gender = sample(c('Male', 'Female'), 10000, replace = TRUE),
  year = 2020
)
# Calculate a date of birth based on a maximum age of 120 years (~43800 days)
deaths[, date_of_birth := date_of_death - sample(1:43800, 10000, replace = TRUE)]

 # process with life_table_prep ----
 output1 <- life_table_prep(ph.data = deaths)
 head(output1)

 # process with life_table_prep using group_by argument----
 output2 <- life_table_prep(ph.data = deaths, group_by = c('gender', 'race_eth'))
 head(output2)



PHSKC-APDE/rads documentation built on April 14, 2025, 10:47 a.m.