R/mortify_cohort.R

Defines functions mortify_cohort

mortify_cohort <- function(filtered_data, current_year) {
  for (x in unique(filtered_data$age_group)) {
    for (y in unique(filtered_data$race)) {
      for (z in unique(filtered_data$sex)) {
        mortality_cohort <-
          filtered_data %>%
          filter(age_group == x) %>%
          filter(race == y) %>%
          filter(sex == z) %>%
          arrange(theta_mortality)

        mortality_length <- nrow(education_cohort)

        cohort_mortality <-
          mortality_data %>%
          filter(Year == current_year) %>%
          filter(Age_Group == x) %>%
          filter(Race == y) %>%
          filter(Sex == z) %$%
          multiply_by(Deaths / Population, mortality_length) %>%
          rep(current_year, .)

        if(length(cohort_mortality) > mortality_length) {
          cohort_mortality <- cohort_mortality[1:mortality_length]
        }

        if(length(cohort_mortality) < mortality_length) {
          cohort_mortality <-
            length(cohort_mortality) %>%
            subtract(mortality_length, .) %>%
            rep(cohort_mortality[1], .) %>%
            c(cohort_mortality)
        }

        mortality_cohort$date_of_death <- cohort_mortality

        filtered_data %<>%
          merge.data.frame(mortality_cohort, all.y = TRUE)
      }
    }
  }
}
schifferl/LagSelectionBias documentation built on May 29, 2019, 3:38 p.m.