Performs data preprocessing required to prepare the observed data for the model.
setup_model_obs(years_of_data = NULL, years_of_age = NULL, age_groups = NULL, spacing_of_historic_tb = 1, con_age_groups = NULL, aggregated = FALSE, historic = FALSE)
years_of_data | Numeric, the years modern case data to filter for. If not given all are returned. |
---|---|
years_of_age | Numeric, the years of age distributed cases to fit to. If not specified then no years are used. |
age_groups | Numeric, the numeric age groups to include in the observed data, defaults to |
spacing_of_historic_tb | Numeric, defaults to 1. Mod to use to identify years of data to use for. |
aggregated | Logical, defaults to |
historic | Logical, defaults to |
con_age_age_groups | Character string, age groups to include as observations. By default no age groups are included. Options
include |
A named list of observed data required by the model.
## Code setup_model_obs#> function(years_of_data = NULL, #> years_of_age = NULL, age_groups = NULL, #> spacing_of_historic_tb = 1, #> con_age_groups = NULL, #> aggregated = FALSE, #> historic = FALSE) { #> #> #> ## Extract historic Pulmonary TB cases #> historic_p_tb <- ModelTBBCGEngland::historic_cases %>% #> filter(year < 2000, year >= 1990) %>% #> select(time = year, value = pulmonary) %>% #> mutate(time = time - 1931) %>% #> filter((time - min(time)) %% spacing_of_historic_tb == 0) #> #> ## Extract age stratified UK born cases #> age_cases <- ModelTBBCGEngland::incidence %>% #> filter(ukborn == "UK Born") %>% #> select(-ukborn) %>% #> group_by(year, age_group) %>% #> summarise(value = sum(incidence, na.rm = T)) %>% #> ungroup %>% #> mutate(time = year - 1931) %>% #> arrange(time, age_group) %>% #> mutate(age = as.numeric(age_group) - 1) %>% #> select(time, age, value) %>% #> arrange(time, age) #> #> ## Extract UK born cases #> yearly_cases <- age_cases %>% #> group_by(time) %>% #> summarise(value = sum(value, na.rm = TRUE)) #> #> if (!is.null(years_of_data)) { #> yearly_cases <- yearly_cases %>% #> filter(time %in% (years_of_data - 1931)) #> } #> #> if (aggregated) { #> obs <- list( #> "YearlyInc" = yearly_cases #> ) #> }else{ #> obs <- list() #> } #> #> if (historic) { #> obs[["YearlyHistPInc"]] <- historic_p_tb #> } #> #> ## Filter age cases #> if (!is.null(years_of_age)) { #> #> age_cases <- age_cases %>% #> filter(time %in% (years_of_age - 1931)) #> #> if (!is.null(con_age_groups)) { #> if ("children" %in% con_age_groups) { #> YearlyChildInc <- age_cases %>% #> filter(age %in% c(0:2)) %>% #> group_by(time) %>% #> summarise(value = sum(value)) #> #> obs[["YearlyChildInc"]] <- YearlyChildInc #> } #> #> if ("adults" %in% con_age_groups) { #> YearlyAdultInc <- age_cases %>% #> filter(age %in% c(3:10)) %>% #> group_by(time) %>% #> summarise(value = sum(value)) #> #> obs[["YearlyAdultInc"]] <- YearlyAdultInc #> } #> #> if ("older adults" %in% con_age_groups) { #> YearlyOlderAdultInc <- age_cases %>% #> filter(age %in% c(11)) %>% #> group_by(time) %>% #> summarise(value = sum(value)) #> #> obs[["YearlyOlderAdultInc"]] <- YearlyOlderAdultInc #> } #> #> } #> if (!is.null(age_groups)) { #> age_cases <- age_cases %>% #> mutate(value = ifelse(!(age %in% age_groups), NA, value)) #> #> #> obs[["YearlyAgeInc"]] <- age_cases #> } #> #> #> } #> #> obs <- obs %>% #> map(drop_na) #> #> return(obs) #> } #> <environment: namespace:ModelTBBCGEngland>## Output setup_model_obs(years_of_age = 2000:2015, con_age_groups = c("children", "adults", "older adults"))#> $YearlyChildInc #> # A tibble: 16 x 2 #> time value #> <dbl> <int> #> 1 69 209 #> 2 70 229 #> 3 71 228 #> 4 72 179 #> 5 73 264 #> 6 74 247 #> 7 75 209 #> 8 76 290 #> 9 77 294 #> 10 78 257 #> 11 79 238 #> 12 80 234 #> 13 81 254 #> 14 82 195 #> 15 83 187 #> 16 84 162 #> #> $YearlyAdultInc #> # A tibble: 16 x 2 #> time value #> <dbl> <int> #> 1 69 1223 #> 2 70 1246 #> 3 71 1221 #> 4 72 1164 #> 5 73 1200 #> 6 74 1240 #> 7 75 1210 #> 8 76 1237 #> 9 77 1290 #> 10 78 1362 #> 11 79 1253 #> 12 80 1402 #> 13 81 1451 #> 14 82 1344 #> 15 83 1273 #> 16 84 1137 #> #> $YearlyOlderAdultInc #> # A tibble: 16 x 2 #> time value #> <dbl> <int> #> 1 69 371 #> 2 70 391 #> 3 71 384 #> 4 72 342 #> 5 73 312 #> 6 74 298 #> 7 75 295 #> 8 76 254 #> 9 77 271 #> 10 78 281 #> 11 79 309 #> 12 80 304 #> 13 81 282 #> 14 82 279 #> 15 83 286 #> 16 84 238 #>