ihmeuw-demographics / hierarchyUtils

Demographics Related Utility Functions
https://ihmeuw-demographics.github.io/hierarchyUtils/
BSD 3-Clause "New" or "Revised" License
8 stars 3 forks source link

BUG: issue with missing age group in agg function #67

Closed hcomfo95 closed 3 years ago

hcomfo95 commented 3 years ago

Describe the bug The agg function produces this error when an age group is missing even though I have the argument missing_dt_severity = "none".

Error in colnamesInt(x, neworder, check_dups = FALSE) : argument specifying columns specify non existing column(s): cols[1]='age_group_id' In addition: Warning message: In [.data.table(result_dt, , :=(c(col_stem), NULL)) : Column 'age' does not exist to remove

It would be nice if the error was more informative because it makes it seem like the age columns are missing but they are not.

To Reproduce

dt <- data.table(age_group_id = c(7:14),
                 nid = rep(256619),
                 underlying_nid = rep(NA),
                 ihme_loc_id = rep("CAN"),
                 year_id = rep(2000),
                 sex = rep("both"),
                 births_reported = c(153, 17350, 59523, 101072, 96353, 45393, 7643, 278),
                 age_start = c(seq(10, 45, 5)),
                 age_end = c(seq(15, 50, 5)),
                 unique_identifier = rep("256619_NA_CAN_2000_both"))

age_specific_births_reported <- copy(dt)
gbd_year <- 2020

age_map <- mortdb::get_age_map(gbd_year = gbd_year, type = "all")

age_map_10_54 <- age_map[age_group_id == 169, c("age_group_years_start", "age_group_years_end")]
colnames(age_map_10_54) <- c("age_start", "age_end")

value_cols <- "births_reported"
id_cols <- names(age_specific_births_reported)[!names(age_specific_births_reported) %in% value_cols]

age_specific_agg_age_10_54 <- data.table()

for (i in unique(age_specific_births_reported$unique_identifier)) {

  temp <- age_specific_births_reported[unique_identifier == i, ]

  temp_agg <- hierarchyUtils::agg(
    dt = temp,
    id_cols = id_cols,
    value_cols = value_cols,
    col_stem = "age",
    col_type = "interval",
    mapping = age_map_10_54,
    missing_dt_severity = "none",
    present_agg_severity = "skip",
    overlapping_dt_severity = "stop"
  )

  age_specific_agg_age_10_54 <- rbind(age_specific_agg_age_10_54, temp_agg)

  temp_agg <- NULL

}