PHSKC-APDE / rads

RADS Code
1 stars 1 forks source link

life expectancy is not calculated when 85+ is missing after life_table_prep #383

Open linsong-kc opened 1 month ago

linsong-kc commented 1 month ago

because of small number for a few census tracts, 5-year combined data do not have the 85+ age group. As a result, life expectancy is not calculated. There are methods to adjust for this.

dcolombara commented 4 days ago

The following code shows this issue:


# Set up ----
  rm(list=ls())
  library(data.table)
  library(rads)

# Create death data with no females >= 85 ----
  deaths <- get_data_death(year = 2016:2020,
                           cols = c('date_of_birth', 'date_of_death', 'chi_sex'))

  deaths[, age := calc_age(date_of_birth, date_of_death)]
  deaths[, date_of_death := fifelse(chi_sex == 'Female' & age >= 85, date_of_death - 365*(age-80), date_of_death)]
  deaths[, age := calc_age(date_of_birth, date_of_death)]
  deaths[, age := NULL]

# Use life_table_prep ----
  prepped = life_table_prep(ph.data = deaths,
                       group_by = 'chi_sex')

  prepped <- prepped[!is.na(chi_sex) & !is.na(ages)]

  setnames(prepped, "chi_sex", "gender")

# Get corresponding population ----
  pop <- get_population(kingco = T,
                        years = 2016:2020,
                        group_by = c('genders', 'ages'))
  pop <- pop[, .(gender, age, pop)]

  my.cuts <- c(0, 1, 5, 10, 15, 18, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85)
  pop[, ages := cut(age, my.cuts, right = F)] # create age intervals
  pop[, ages := gsub("\\[|\\]|\\)", "", ages)] # tidy
  pop[, ages := gsub("\\,", "-", ages)] #tidy
  pop[age >= max(my.cuts), ages := paste0(max(my.cuts), "+")] # tidy
  pop <- pop[, .(pop = sum(pop)), .(gender, ages)] #tidy

# Merge on corresponding population ----
  prepped <- merge(prepped,
                   pop,
                   by = c('gender', 'ages'),
                   all = T)

# Run life table ----
  lifetable <- life_table(ph.data = prepped,
                             group_by = 'gender')
# Review output ----
  prepped[gender == 'Female' & ages %in% c('80-85', '85+')]
  lifetable[gender == 'Female' & ages %in% c('80-85', '85+')]