Repository navigation
Expand file tree
/
Copy path22_Demographic.R
More file actions
43 lines (35 loc) · 1.46 KB
/
Copy path22_Demographic.R
File metadata and controls
43 lines (35 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
################################################################################
#
# RiskExtrapolation
#
# Modelling demographic differences
#
################################################################################
#######################################
# ESTIMATE SECOND_STAGE MODEL WITH AGE
#######################################
#-------------------------
# Prepare age data
#-------------------------
#----- Add average age of death for each age group of each city
# Detailed info about age groups
agegrs <- mutate(metadf, agelow = as.numeric(substring(agegroup, 1, 2)),
agehigh = as.numeric(substring(agegroup, 3))) |>
subset(select = c(city_code, agegroup, agelow, agehigh))
# Extract all ages represented in each age group
ages <- subset(agegrs, !is.na(agehigh)) |>
reframe(age = agelow:agehigh, .by = c(city_code, agegroup))
agesdemo <- subset(metadata_age, !is.na(agehigh)) |>
reframe(age = age:agehigh, deathrate, .by = c(city_code, age))
# Compute weighted average age
ages <- merge(ages, agesdemo, all.x = T) |>
summarise(age = weighted.mean(age, deathrate), .by = c(city_code, agegroup))
# Life expectancy for oldest age group
oldage <- subset(agegrs, is.na(agehigh)) |>
merge(metadata_age, by.x = c("city_code", "agelow"),
by.y = c("city_code", "age")) |>
mutate(age = agelow + lifexp)
ages <- rbind(ages, oldage[, c("city_code", "agegroup", "age")]) |>
arrange(city_code, agegroup)
# Merge with metadata
metadf <- merge(metadf, ages)