Skip to content

Commit 39b86a9

Browse files
committed
Update mortality-with-age to latest HMD data, add CSV output
1 parent 99c5f89 commit 39b86a9

File tree

7 files changed

+332
-1144
lines changed

7 files changed

+332
-1144
lines changed

README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,17 @@ for convenience.
100100

101101
### `mortality.org/`
102102

103-
This folder is mostly empty as per the
104-
[Human Mortality Database User Agreement](https://www.mortality.org/Public/UserAgreement.php)
105-
which requests that copies of data downloaded are not passed to other users.
103+
This folder is mostly empty as per the [Human Mortality Database User Agreement](https://www.mortality.org/Public/UserAgreement.php) which requests that copies of data downloaded are not passed to other users.
106104

107105
It is used in `mortality-with-age.R` via the `read_life_table_hmd()` and
108106
`read_population_hmd()` convenience functions in `init.R` which expect this
109-
folder to be populated. You will need to
110-
[download](https://www.mortality.org/cgi-bin/hmd/hmd_download.php) the
111-
‘Period data’, ‘Population estimates (January 1)’ and ‘Life Tables - Both
112-
Sexes’, and then move the `Population` and `bltper_1x1` folders into this folder
113-
for the script to work.
114-
115-
`codelist.csv` translates between the HMD-specific country codes and the ISO
116-
equivalents (eg GBR_NP instead of GBR for the UK), and may need updating if more
117-
countries are added and you wish to include them.
107+
folder to be populated. You will need to [download](https://www.mortality.org/cgi-bin/hmd/hmd_download.php) the ‘Period data’, ‘Population estimates (January 1)’ and ‘Life Tables - Both Sexes’, and then move the `Population` and `bltper_1x1` folders into this folder for the script to work.
108+
109+
`codelist.csv` translates between the HMD-specific country codes and the ISO equivalents (eg GBR_NP instead of GBR for the UK), and may need updating if more countries are added and you wish to include them.
110+
111+
Citation:
112+
113+
>HMD. Human Mortality Database. Max Planck Institute for Demographic Research (Germany), University of California, Berkeley (USA), and French Institute for Demographic Studies (France). Available at [www.mortality.org](https://www.mortality.org/).
118114
119115
### `un/`
120116

code/init.R

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ download_data <-
5353
unzip(dest_file, exdir = dest_dir)
5454
}
5555
}
56+
}
57+
58+
read_hmd <- function(filename, columns, col_widths) {
59+
# Make a tibble to return
60+
read_fwf(
61+
filename,
62+
fwf_widths(
63+
col_widths,
64+
columns
65+
),
66+
skip = 3,
67+
na = c(".")
68+
) %>%
69+
select(-'deleteme') %>%
70+
# This is a kludge which needs fixing - HMD uses eg 1973- and 1973+ to
71+
# denote populations before and after a border change. This causes
72+
# issues so currently I'm just dropping them...
73+
mutate(year = as.numeric(year)) %>%
74+
drop_na(year)
5675
}
5776

5877
read_life_table_hmd <-
@@ -69,17 +88,13 @@ read_life_table_hmd <-
6988
)
7089

7190
# Make a tibble to return
72-
read_fwf(
91+
read_hmd(
7392
filename,
74-
fwf_widths(
75-
c(9,9,1,12),
76-
# deleteme catches the + in 110+ at the end of the life tables, which
77-
# thankfully is in its own column
78-
c('year', 'age', 'deleteme', 'risk')
79-
),
80-
skip = 3
81-
) %>%
82-
select(-'deleteme')
93+
# deleteme catches the + in 110+ at the end of the life tables, which
94+
# thankfully is in its own column
95+
c('year', 'age', 'deleteme', 'risk'),
96+
c(9,9,1,12)
97+
)
8398
}
8499

85100
read_population_hmd <-
@@ -95,21 +110,12 @@ read_population_hmd <-
95110
file_suffix
96111
)
97112

98-
# Make a tibble to return
99-
read_fwf(
100-
filename,
101-
fwf_widths(
102-
c(9,9,1,20,16,16),
113+
read_hmd(
114+
filename,
103115
# deleteme catches the + in 110+ at the end of the life tables, which
104116
# thankfully is in its own column
105117
# 'population' is called 'total' in the source data
106-
c('year', 'age', 'deleteme', 'female', 'male', 'population')
107-
),
108-
skip = 3
109-
) %>%
110-
select(-'deleteme') %>%
111-
# This is a kludge which needs fixing - HMD uses eg 1973- and 1973+ to
112-
# denote populations before and after a border change. This causes issues
113-
# so currently I'm just dropping them...
114-
drop_na(year)
118+
columns = c('year', 'age', 'deleteme', 'female', 'male', 'population'),
119+
col_widths = c(9,9,1,20,16,16)
120+
)
115121
}

code/mortality-with-age.R

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
source('init.R')
66

7-
#' ## Read in data
7+
#' ## Data source
88
#'
99
#' We’re using data from the
1010
#' [Human Mortality Database](https://www.mortality.org/),
@@ -13,6 +13,13 @@ source('init.R')
1313
#' ‘high-income countries’ in that database, averaged over the five most recent
1414
#' years of data.
1515
#'
16+
#' ### Citation
17+
#'
18+
#' HMD. Human Mortality Database. Max Planck Institute for Demographic Research
19+
#' (Germany), University of California, Berkeley (USA), and French Institute for
20+
#' Demographic Studies (France). Available at
21+
#' [www.mortality.org](https://www.mortality.org/).
22+
#'
1623
#+ read_data
1724

1825
# Load a list of the countries available in the HMD
@@ -37,7 +44,7 @@ for(country in countries$country_hmd) {
3744
# This is a kludge - we want the five most recent years, but top_n
3845
# selects from all values, not just unique ones, so only selects the
3946
# most recent year because there are 111 values (0-110+) for each year.
40-
# Should really make a top_n_unique function buy this will do...
47+
# Should really make a top_n_unique function but this will do...
4148
top_n(5*111, year) %>%
4249
mutate(country = country)
4350
)
@@ -177,3 +184,5 @@ avg_lt %>%
177184
kable() %>%
178185
kable_styling()
179186

187+
# Save full table as a CSV
188+
avg_lt %>% write_csv(file.path(output_dir, 'mortality-with-age.csv'))

code/spin-reports.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ github_repo <- 'https://github.com/ajsteele/ageless/'
1414
github_file_prepend <- 'blob/master/'
1515
github_url <- paste0(github_repo, github_file_prepend)
1616

17+
pretty_long_date <- function(date = Sys.Date()) {
18+
day <- as.integer(format(date, "%d"))
19+
20+
# determine ordinal suffix
21+
ordinal <-
22+
ifelse(day %% 10 == 1 & day != 11, "st",
23+
ifelse(day %% 10 == 2 & day != 12, "nd",
24+
ifelse(day %% 10 == 3 & day != 13, "rd", "th")))
25+
26+
# return formatted string
27+
paste0(
28+
day, ordinal, # day-th
29+
" ",
30+
format(date, "%B %Y") # monthname YYYY
31+
)
32+
}
33+
1734
pretty_spin <- function(
1835
filename, output_dir = '../output/', addendum = TRUE, log_file = TRUE
1936
) {
@@ -49,6 +66,12 @@ pretty_spin <- function(
4966
c(
5067
"#' ## About this report",
5168
"#'",
69+
paste0(
70+
"#' _This report was last updated on ",
71+
pretty_long_date(),
72+
"._"
73+
),
74+
"#'",
5275
"#' This report was generated from an R script which performs the",
5376
"#' underlying calculations. The code and console output have been",
5477
"#' removed to make it easier to read. If you want to see the code,",

output/mortality-with-age.csv

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
age,risk,odds_cum
2+
0,0.004612868498928659,1
3+
1,3.5316464009159837e-4,0.9953871315010714
4+
2,2.1833711909038537e-4,0.995035595963023
5+
3,1.7074296147145506e-4,0.994818342757608
6+
4,1.41278910731745e-4,0.9946484845276395
7+
5,1.2322200234267315e-4,0.9945079616731844
8+
6,1.1224599467681787e-4,0.9943854164108012
9+
7,1.0463970234104559e-4,0.9942738006306441
10+
8,1.025665720015746e-4,0.9941697601161006
11+
9,9.748304758897413e-5,0.9940677915318178
12+
10,1.0056056116078554e-4,0.9939708867739893
13+
11,1.080292306585877e-4,0.9938709325038377
14+
12,1.2280376318818512e-4,0.9937635653916255
15+
13,1.5027417176893555e-4,0.993641527486076
16+
14,1.9078645599066518e-4,0.9934922088284979
17+
15,2.4327879437949603e-4,0.9933026639709212
18+
16,3.146517879579433e-4,0.9930610144963764
19+
17,3.9343499106978615e-4,0.9927485460726138
20+
18,5.122371352938935e-4,0.9923579640572551
21+
19,5.790111425084664e-4,0.9918496414565603
22+
20,6.312769429425175e-4,0.9912753494624639
23+
21,6.97930101492033e-4,0.990649580190241
24+
22,7.406703349395003e-4,0.9899581760281958
25+
23,7.885771027995985e-4,0.9892249433743809
26+
24,8.404968272523658e-4,0.9884448632345176
27+
25,8.992238864561824e-4,0.9876140784630552
28+
26,9.468011404120613e-4,0.9867259922931008
29+
27,0.0010125942320030482,0.9857917589983235
30+
28,0.0010818911554559005,0.9847935519492056
31+
29,0.0011572066136466695,0.9837281125154018
32+
30,0.001251420397577094,0.9825897358375688
33+
31,0.001305989747579358,0.9813601029996918
34+
32,0.0013763551511263264,0.9800784567664907
35+
33,0.001455405271788976,0.9787295207340123
36+
34,0.0015082848828246401,0.9773050726298804
37+
35,0.0015837332734974917,0.9758310181629251
38+
36,0.0016356376505703104,0.9742855621101495
39+
37,0.0017090314685849336,0.9726919839623551
40+
38,0.0017622070867878148,0.9710296227525231
41+
39,0.0018419683414618627,0.9693184674698277
42+
40,0.0019463720903420962,0.967533013539954
43+
41,0.0020156758477514014,0.9656498342859152
44+
42,0.002105532417628064,0.9637033972375599
45+
43,0.0022244352950344243,0.9616742884936981
46+
44,0.002352564118387724,0.9595351062640455
47+
45,0.002539505526740598,0.9572777384027154
48+
46,0.002724939484518705,0.9548467262954159
49+
47,0.0029553671038350035,0.9522448267492701
50+
48,0.0032137090544761345,0.9494305937134982
51+
49,0.003521299084909059,0.9463794000178845
52+
50,0.0038806126030630104,0.9430469151026247
53+
51,0.004194111066521029,0.9393873153585978
54+
52,0.004542184569995156,0.9354474206235028
55+
53,0.004919547549834611,0.931198445783505
56+
54,0.005347403170835847,0.9266173707511409
57+
55,0.0058727926020417385,0.9216623740846348
58+
56,0.006376288143575408,0.9162496421125302
59+
57,0.006888853580074014,0.9104073703829729
60+
58,0.007433482115730971,0.9041357073101846
61+
59,0.008090475364830154,0.8974148306997005
62+
60,0.00886976728189027,0.8901543181198913
63+
61,0.00960933464912037,0.8822588564731981
64+
62,0.0103860716380214,0.8737809358741969
65+
63,0.011119088240797655,0.8647057844782702
66+
64,0.01196283613440747,0.8550910445583281
67+
65,0.012648547739694396,0.8448617305122774
68+
66,0.013387526005866884,0.8341754565804521
69+
67,0.014175868749012286,0.8230079109620254
70+
68,0.015041595179901002,0.8113410588368289
71+
69,0.01645352602913997,0.7991371950769731
72+
70,0.018017334484986557,0.7859885704369202
73+
71,0.019808726979957274,0.7718271514619818
74+
72,0.021898681574681358,0.7565382381429533
75+
73,0.02394756745847366,0.7399710481667903
76+
74,0.026355488812933075,0.7222505415734986
77+
75,0.02895866613295038,0.7032152755049235
78+
76,0.03165648959795186,0.6828510991219856
79+
77,0.034471549169679365,0.6612344304056805
80+
78,0.03805061612419631,0.6384406552252662
81+
79,0.04213219048821815,0.6141475949352092
82+
80,0.04733499426830912,0.588272211477518
83+
81,0.05323182544197273,0.560426349719024
84+
82,0.059825282323406635,0.5305938320976991
85+
83,0.0670308042414164,0.49885090629339596
86+
84,0.0751644780458086,0.46541252884799017
87+
85,0.0845760108902049,0.4304300390411512
88+
86,0.09491511441944345,0.39402598337173544
89+
87,0.10693353931120954,0.35662696207577343
90+
88,0.1204363506546374,0.31849157880720647
91+
89,0.13499793135053065,0.28013361534143266
92+
90,0.15203466281063638,0.242316156768594
93+
91,0.16977297278250084,0.2054757015807115
94+
92,0.18937348218739414,0.17059148088878412
95+
93,0.21077309752622483,0.13828597812137078
96+
94,0.23328419091398295,0.10913901416828568
97+
95,0.25721850964948934,0.08367860755088745
98+
96,0.28408163612850934,0.06215492082710367
99+
97,0.31256362223983813,0.0444978492251021
100+
98,0.3423136758090914,0.030589440289422013
101+
99,0.3729629661317349,0.020118256543007244
102+
100,0.40417606256251715,0.012614891909328081
103+
101,0.437351858463736,0.007516254567764103
104+
102,0.47163339081091304,0.004229006663865929
105+
103,0.5074400348003333,0.0022344659112248934
106+
104,0.5430960056988635,0.001100608451472775
107+
105,0.5778653615240843,5.028723976394995e-4
108+
106,0.6108251743668208,2.1227985777706705e-4
109+
107,0.6439552679757108,8.261397663582614e-5
110+
108,0.6754769336444885,2.941427117276361e-5
111+
109,0.7051713212811662,9.545609475597775e-6
112+
110,0.7330875384496346,2.814319429256472e-6

0 commit comments

Comments
 (0)