-
Notifications
You must be signed in to change notification settings - Fork 1
/
02_Mx_calc.Rmd
848 lines (674 loc) · 20.4 KB
/
02_Mx_calc.Rmd
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
---
title: "Age Specific Mortality Rate Trends "
output: html_notebook
---
# Intro
This doc will describe some of the average trends at specific ages from 21 high income countries.
It is inspired by [Christensen 2010](https://www.thelancet.com/journals/lancet/article/PIIS0140-6736(09)61460-4/fulltext), which showed
* Probability of dying in next 12 months
* At age 80 and age 90
* Males and females
* Selected countries
* England & Wales
* France
* East Germany
* West Germany
* Japan
* Sweden
* USA
However, as per the figures in [White 2002](https://onlinelibrary.wiley.com/doi/pdf/10.1111/j.1728-4457.2002.00059.x)), the average of 21 high income countries will be used instead, as well as the best-performing country at these different age groups.
Additionally, the 12 month mortality probabilities at the following ages will also be calculated:
* 0-1 years
* 40 years
The aim will be to determine how strongly the (log of these) trends are correlated.
# Pre reqs
```{r}
pacman::p_load(
tidyverse, HMDHFDplus,
ggrepel, plotly
)
dta_Mx <- read_rds("tidy_data/Mx_data.rds")
```
Define countries
```{r}
source("scripts/country_definitions.R")
```
# Replication of Fig 2 of Christenson et al 2010
```{r}
dta_Mx %>%
filter(sex != "total") %>%
filter(age %in% c(80, 90)) %>%
filter(
code %in% c("GBRTENW", "FRATNP", "DEUTE", "DEUTW", "JPN", "SWE", "USA")
) %>%
filter(between(year, 1950, 2003)) %>%
ggplot(
aes(x = year, y = Mx, colour = code, group = code)
) +
geom_line() +
facet_grid(age ~ sex)
```
And how has this developed since?
```{r}
dta_Mx %>%
filter(sex != "total") %>%
filter(age %in% c(80, 90)) %>%
filter(
code %in% c("GBRTENW", "FRATNP", "DEUTE", "DEUTW", "JPN", "SWE", "USA")
) %>%
filter(between(year, 1950, 2017)) %>%
ggplot(
aes(x = year, y = Mx, colour = code, group = code)
) +
geom_line() +
facet_grid(age ~ sex)
```
It's interesting that life expectancies have at age 80 have converged, for males especially.
And that they've levelled off in Japan over the 2000s, while continued to improve in the USA.
In England/Wales they Mx at age 90 seem to have increased for females especially in recent years.
Let's look at this for the average of the 21 countries
Now let's do this for the average of the 21 high income countries
```{r}
dta_Mx %>%
filter(sex != "total") %>%
filter(age %in% c(80, 90)) %>%
filter(code %in% high_income_countries) %>%
filter(between(year, 1950, 2016)) %>%
group_by(year, age, sex) %>%
summarise(mean_Mx = mean(Mx, na.rm = T)) %>%
ungroup() %>%
ggplot(aes(x = year, y = mean_Mx, colour = sex)) +
facet_wrap(~age) +
geom_point()
```
Let's now use log rather than linear
```{r}
dta_Mx %>%
filter(sex != "total") %>%
filter(age %in% c(80, 90)) %>%
filter(code %in% high_income_countries) %>%
filter(between(year, 1950, 2016)) %>%
group_by(year, age, sex) %>%
summarise(mean_Mx = mean(Mx, na.rm = T)) %>%
ungroup() %>%
ggplot(aes(x = year, y = mean_Mx, colour = sex)) +
facet_wrap(~age) +
geom_point() +
scale_y_log10()
```
So, the trends on % reductions have been more continuous for age 90 than age 80, and more continuous for females than males.
Let's now do this for a couple of additional age groups
```{r}
dta_Mx %>%
filter(sex != "total") %>%
filter(age %in% c(0, 30, 80, 90)) %>%
filter(code %in% high_income_countries) %>%
filter(between(year, 1950, 2016)) %>%
group_by(year, age, sex) %>%
summarise(mean_Mx = mean(Mx, na.rm = T)) %>%
ungroup() %>%
ggplot(aes(x = year, y = mean_Mx, colour = sex)) +
facet_wrap(~age) +
geom_point() +
scale_y_log10()
```
Now let's estimate the correlation between these trends
```{r}
dta_trnd <- dta_Mx %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
filter(age %in% c(0, 30, 80, 90)) %>%
group_by(year, age) %>%
summarise(mean_Mx = mean(Mx, na.rm = T)) %>%
ungroup() %>%
mutate(log_mean_Mx = log(mean_Mx, 10))
dta_trnd %>%
select(-mean_Mx) %>%
mutate(age = paste0("age_", age)) %>%
spread(age, log_mean_Mx) %>%
select(-year) %>%
cor()
```
So, as expected, % improvements in infancy are more strongly correlated with those at age 80 and 90 than at age 30, and % changes at age 90 are less strongly correlated than with those at other ages.
Let's do this as a heatmap for all ages
```{r}
dta_trnd <- dta_Mx %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
filter(age <= 109) %>%
group_by(year, age) %>%
summarise(mean_Mx = mean(Mx, na.rm = T)) %>%
ungroup() %>%
mutate(log_mean_Mx = log(mean_Mx, 10))
tmp <- dta_trnd %>%
select(-mean_Mx) %>%
spread(age, log_mean_Mx) %>%
select(-year) %>%
cor()
cor_df <- tmp %>%
as_tibble() %>%
mutate(from_age = rownames(tmp)) %>%
gather(key="to_age", value = "value", -from_age) %>%
mutate(from_age = as.numeric(from_age), to_age = as.numeric(to_age))
cor_df %>%
filter(from_age <= 100, to_age <= 100) %>%
ggplot(aes(x = from_age, y = to_age, fill = value)) +
geom_tile() +
scale_fill_viridis_c() +
scale_x_continuous(breaks = seq(0, 100, by = 10)) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_equal()
```
Definitely a 'wow' figure!
Interesting that the trends that apply at older ages don't apply at the oldest ages (from around age 96 onwards)
Let's see how different it looks by gender.
```{r}
dta_trnd <- dta_Mx %>%
filter(sex != "total") %>%
filter(between(year, 1955, 2016)) %>%
filter(age <= 109) %>%
group_by(sex, year, age) %>%
summarise(mean_Mx = mean(Mx, na.rm = T)) %>%
ungroup() %>%
mutate(log_mean_Mx = log(mean_Mx, 10)) %>%
group_by(sex) %>%
nest()
cors_df <- dta_trnd %>%
mutate(cors = map(
data,
function(X) {
X %>%
select(-mean_Mx) %>%
spread(age, log_mean_Mx) %>%
select(-year) %>%
cor()
}
)
) %>%
mutate(cor_df = map(
cors,
function(X){
X %>%
as_tibble() %>%
mutate(from_age = rownames(X)) %>%
gather(key = "to_age", value = "value", -from_age) %>%
mutate(from_age = as.numeric(from_age), to_age = as.numeric(to_age))
}
)
) %>%
select(sex, cor_df) %>%
unnest()
cors_df %>%
filter(from_age <= 100, to_age <= 100) %>%
ggplot(aes(x = from_age, y = to_age, fill = value)) +
geom_tile() +
scale_fill_viridis_c() +
scale_x_continuous(breaks = seq(0, 100, by = 10)) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_equal() +
facet_wrap(~sex)
```
Again, beautiful, staggering, and awesome. This is like seeing the genome of population health improvement being mapped. The gender differences in the differences in correlation are very apparent.
Let's extend this to a few few select countries:
* France
* Sweden?
* UK
* USA
* Spain (Identified as most compatible with Lee-Carter modelling approach)
* Japan
```{r}
dta_trnd <- dta_Mx %>%
filter(sex != "total") %>%
filter(code %in% c("FRATNP", "SWE", "GBR_NP", "USA", "ESP", "JPN")) %>%
filter(between(year, 1955, 2016)) %>%
filter(age <= 109) %>%
group_by(code, sex, year, age) %>%
summarise(mean_Mx = mean(Mx, na.rm = T)) %>%
ungroup() %>%
mutate(log_mean_Mx = log(mean_Mx + 0.00001, 10)) %>% # Correction for Sweden
group_by(sex, code) %>%
nest()
cors_df <- dta_trnd %>%
mutate(cors = map(
data,
function(X) {
X %>%
select(-mean_Mx) %>%
spread(age, log_mean_Mx) %>%
select(-year) %>%
cor()
}
)
) %>%
mutate(cor_df = map(
cors,
function(X){
X %>%
as_tibble() %>%
mutate(from_age = rownames(X)) %>%
gather(key = "to_age", value = "value", -from_age) %>%
mutate(from_age = as.numeric(from_age), to_age = as.numeric(to_age))
}
)
) %>%
select(sex, code, cor_df) %>%
unnest()
cors_df %>%
filter(from_age <= 100, to_age <= 100) %>%
ggplot(aes(x = from_age, y = to_age, fill = value)) +
geom_tile() +
scale_fill_viridis_c(limits = c(-1,1)) +
scale_x_continuous(breaks = seq(0, 100, by = 10)) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_equal() +
facet_grid(sex ~ code)
```
Let's do this just for the UK nations
```{r}
dta_trnd <- dta_Mx %>%
filter(sex != "total") %>%
filter(code %in% c("GBRTENW", "GBR_SCO", "GBR_NIR")) %>%
filter(between(year, 1955, 2016)) %>%
filter(age <= 109) %>%
group_by(code, sex, year, age) %>%
summarise(mean_Mx = mean(Mx, na.rm = T)) %>%
ungroup() %>%
mutate(log_mean_Mx = log(mean_Mx + 0.00001, 10)) %>% # Correction for Sweden
group_by(sex, code) %>%
nest()
cors_df <- dta_trnd %>%
mutate(cors = map(
data,
function(X) {
X %>%
select(-mean_Mx) %>%
spread(age, log_mean_Mx) %>%
select(-year) %>%
cor()
}
)
) %>%
mutate(cor_df = map(
cors,
function(X){
X %>%
as_tibble() %>%
mutate(from_age = rownames(X)) %>%
gather(key = "to_age", value = "value", -from_age) %>%
mutate(from_age = as.numeric(from_age), to_age = as.numeric(to_age))
}
)
) %>%
select(sex, code, cor_df) %>%
unnest()
cors_df %>%
filter(from_age <= 100, to_age <= 100) %>%
ggplot(aes(x = from_age, y = to_age, fill = value)) +
geom_tile() +
scale_fill_viridis_c(limits = c(-1,1)) +
scale_x_continuous(breaks = seq(0, 100, by = 10)) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_equal() +
facet_grid(sex ~code) +
labs(x = "Age", y = "Age", title = "Correlations between rates of change in log mortality risk age different ages",
subtitle = "Trends from 1955-2016. England & Wales, Scotland, Northern Ireland",
caption = "Source: Human Mortality Database. Available from https://github.com/JonMinton/Life_expectancy_limits/")
ggsave("figures/trend_correlation_heatmap.png", height = 20, width = 25, units = "cm", dpi = 300)
```
Although plotly doesn't convert faceting too easily, can I use ggplotly for hovering over values for Scotland males alone?
```{r}
p <- cors_df %>%
filter(from_age <= 100, to_age <= 100) %>%
filter(sex == "male") %>%
filter(code == "GBR_SCO") %>%
ggplot(aes(x = from_age, y = to_age, fill = value)) +
geom_tile() +
scale_fill_viridis_c() +
scale_x_continuous(breaks = seq(0, 100, by = 10)) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_equal()
p <- cors_df %>%
filter(from_age <= 100, to_age <= 100) %>%
ggplot(aes(x = from_age, y = to_age, fill = value)) +
geom_tile() +
scale_fill_viridis_c() +
scale_x_continuous(breaks = seq(0, 100, by = 10)) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_equal() +
facet_grid(sex ~code)
plotly::ggplotly(p)
```
ggplotly now works with faceted plots! (Though the number of observations looks like it causes things to struggle!)
For Scotland and England & Wales (independently), how have the correlations changed over time?
England/Wales first:
```{r}
dta_trnd <- dta_Mx %>%
filter(sex != "total") %>%
filter(code %in% c("GBRTENW")) %>%
filter(between(year, 1950, 2010)) %>%
filter(age <= 109) %>%
mutate(
decade = cut(year, breaks = seq(1950, 2010, by = 10), labels = c("1950s", "1960s", "1970s", "1980s", "1990s", "2000s"), include.lowest = TRUE)
) %>%
group_by(sex, decade, year, age) %>%
summarise(mean_Mx = mean(Mx, na.rm = T)) %>%
ungroup() %>%
mutate(log_mean_Mx = log(mean_Mx, 10)) %>% # Correction for Sweden
group_by(sex, decade) %>%
nest()
cors_df <- dta_trnd %>%
mutate(cors = map(
data,
function(X) {
X %>%
select(-mean_Mx) %>%
spread(age, log_mean_Mx) %>%
select(-year) %>%
cor()
}
)
) %>%
mutate(cor_df = map(
cors,
function(X){
X %>%
as_tibble() %>%
mutate(from_age = rownames(X)) %>%
gather(key = "to_age", value = "value", -from_age) %>%
mutate(from_age = as.numeric(from_age), to_age = as.numeric(to_age))
}
)
) %>%
select(sex, decade, cor_df) %>%
unnest()
cors_df %>%
filter(from_age <= 100, to_age <= 100) %>%
ggplot(aes(x = from_age, y = to_age, fill = value)) +
geom_tile() +
scale_fill_viridis_c() +
scale_x_continuous(breaks = seq(0, 100, by = 10)) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_equal() +
facet_grid(sex ~ decade)
```
Now for Scotland.
```{r}
dta_trnd <- dta_Mx %>%
filter(sex != "total") %>%
filter(code %in% c("GBR_SCO")) %>%
filter(between(year, 1950, 2010)) %>%
filter(age <= 109) %>%
mutate(
decade = cut(year, breaks = seq(1950, 2010, by = 10), labels = c("1950s", "1960s", "1970s", "1980s", "1990s", "2000s"), include.lowest = TRUE)
) %>%
group_by(sex, decade, year, age) %>%
summarise(mean_Mx = mean(Mx, na.rm = T)) %>%
ungroup() %>%
mutate(log_mean_Mx = log(mean_Mx + 0.00001, 10)) %>% # Correction for Sweden
group_by(sex, decade) %>%
nest()
cors_df <- dta_trnd %>%
mutate(cors = map(
data,
function(X) {
X %>%
select(-mean_Mx) %>%
spread(age, log_mean_Mx) %>%
select(-year) %>%
cor()
}
)
) %>%
mutate(cor_df = map(
cors,
function(X){
X %>%
as_tibble() %>%
mutate(from_age = rownames(X)) %>%
gather(key = "to_age", value = "value", -from_age) %>%
mutate(from_age = as.numeric(from_age), to_age = as.numeric(to_age))
}
)
) %>%
select(sex, decade, cor_df) %>%
unnest()
cors_df %>%
filter(from_age <= 100, to_age <= 100) %>%
ggplot(aes(x = from_age, y = to_age, fill = value)) +
geom_tile() +
scale_fill_viridis_c() +
scale_x_continuous(breaks = seq(0, 100, by = 10)) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_equal() +
facet_grid(sex ~ decade)
```
Northern Ireland
```{r}
dta_trnd <- dta_Mx %>%
filter(sex != "total") %>%
filter(code %in% c("GBR_NIR")) %>%
filter(between(year, 1950, 2010)) %>%
filter(age <= 109) %>%
mutate(
decade = cut(year, breaks = seq(1950, 2010, by = 10), labels = c("1950s", "1960s", "1970s", "1980s", "1990s", "2000s"), include.lowest = TRUE)
) %>%
group_by(sex, decade, year, age) %>%
summarise(mean_Mx = mean(Mx, na.rm = T)) %>%
ungroup() %>%
mutate(log_mean_Mx = log(mean_Mx + 0.00001, 10)) %>% # Correction for Sweden
group_by(sex, decade) %>%
nest()
cors_df <- dta_trnd %>%
mutate(cors = map(
data,
function(X) {
X %>%
select(-mean_Mx) %>%
spread(age, log_mean_Mx) %>%
select(-year) %>%
cor()
}
)
) %>%
mutate(cor_df = map(
cors,
function(X){
X %>%
as_tibble() %>%
mutate(from_age = rownames(X)) %>%
gather(key = "to_age", value = "value", -from_age) %>%
mutate(from_age = as.numeric(from_age), to_age = as.numeric(to_age))
}
)
) %>%
select(sex, decade, cor_df) %>%
unnest()
cors_df %>%
filter(from_age <= 100, to_age <= 100) %>%
ggplot(aes(x = from_age, y = to_age, fill = value)) +
geom_tile() +
scale_fill_viridis_c() +
scale_x_continuous(breaks = seq(0, 100, by = 10)) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_equal() +
facet_grid(sex ~ decade)
```
```{r}
dta_trnd <- dta_Mx %>%
filter(sex != "total") %>%
filter(code %in% c("GBRTENW")) %>%
filter(between(year, 1955, 2015)) %>%
filter(age <= 109) %>%
mutate(
decade = cut(year, breaks = seq(1955, 2015, by = 10), include.lowest = TRUE)
) %>%
group_by(sex, decade, year, age) %>%
summarise(mean_Mx = mean(Mx, na.rm = T)) %>%
ungroup() %>%
mutate(log_mean_Mx = log(mean_Mx, 10)) %>% # Correction for Sweden
group_by(sex, decade) %>%
nest()
cors_df <- dta_trnd %>%
mutate(cors = map(
data,
function(X) {
X %>%
select(-mean_Mx) %>%
spread(age, log_mean_Mx) %>%
select(-year) %>%
cor()
}
)
) %>%
mutate(cor_df = map(
cors,
function(X){
X %>%
as_tibble() %>%
mutate(from_age = rownames(X)) %>%
gather(key = "to_age", value = "value", -from_age) %>%
mutate(from_age = as.numeric(from_age), to_age = as.numeric(to_age))
}
)
) %>%
select(sex, decade, cor_df) %>%
unnest()
cors_df %>%
filter(from_age <= 100, to_age <= 100) %>%
ggplot(aes(x = from_age, y = to_age, fill = value)) +
geom_tile() +
scale_fill_viridis_c() +
scale_x_continuous(breaks = seq(0, 100, by = 10)) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_equal() +
facet_grid(sex ~ decade) +
labs(
x = "Age", y = "Age",
title = "Correlation between trends at indivdual ages, England & Wales, by decade"
)
```
Now for Scotland.
```{r}
dta_trnd <- dta_Mx %>%
filter(sex != "total") %>%
filter(code %in% c("GBR_SCO")) %>%
filter(between(year, 1955, 2015)) %>%
filter(age <= 109) %>%
mutate(
decade = cut(year, breaks = seq(1955, 2015, by = 10), include.lowest = TRUE)
) %>%
group_by(sex, decade, year, age) %>%
summarise(mean_Mx = mean(Mx, na.rm = T)) %>%
ungroup() %>%
mutate(log_mean_Mx = log(mean_Mx + 0.00001, 10)) %>% # Correction for Sweden
group_by(sex, decade) %>%
nest()
cors_df <- dta_trnd %>%
mutate(cors = map(
data,
function(X) {
X %>%
select(-mean_Mx) %>%
spread(age, log_mean_Mx) %>%
select(-year) %>%
cor()
}
)
) %>%
mutate(cor_df = map(
cors,
function(X){
X %>%
as_tibble() %>%
mutate(from_age = rownames(X)) %>%
gather(key = "to_age", value = "value", -from_age) %>%
mutate(from_age = as.numeric(from_age), to_age = as.numeric(to_age))
}
)
) %>%
select(sex, decade, cor_df) %>%
unnest()
cors_df %>%
filter(from_age <= 100, to_age <= 100) %>%
ggplot(aes(x = from_age, y = to_age, fill = value)) +
geom_tile() +
scale_fill_viridis_c() +
scale_x_continuous(breaks = seq(0, 100, by = 10)) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_equal() +
facet_grid(sex ~ decade)
```
## Analysis/results for aqmen conference presentation
Populations:
* France,
* UK
* USA
* Japan
Time period:
* 1970 to last available year
Analyses:
* Individual plots
* Correlations between broad age groups
* Dendrogram of age correlations
```{r}
dta_Mx %>%
filter(code %in% c("FRATNP", "GBR_NP", "USA", "JPN")) %>%
filter(year >= 1970) %>%
filter(age <= 109) %>%
filter(sex != "total") %>%
group_by(code, sex) %>%
nest()
```
Let's look at hclust examples
It seems from this that the datastructure required should have the following:
* rownames: the individual ages
* columns: rate in different years
Let's start with a single population: USA males
```{r}
dta_Mx %>%
filter(code == "USA") %>%
filter(sex == "male") %>%
filter(year >= 1970) %>%
filter(age <= 109) %>%
mutate(lnMx = log(Mx + 0.00001, 10)) %>%
select(-code, -sex, -Mx) %>%
spread(year, lnMx) -> tmp
nms <- tmp$age
tmp$age <- NULL
rownames(tmp) <- nms
tmp
hc <- hclust(dist(tmp), "ave")
ggdendro::ggdendrogram(hc, rotate = TRUE, leaf_labels = TRUE) + coord_flip()
```
This has been a bit too much of a drain on my cognitive resources for now. I'm going to stop and reevaluate when I've got more focus. It does seem to identify two broad clusters, and for these clusters to be as expected, split between working age and retiremnt age, with age 0 with post retirement age.
Let's quickly try PCA on the same
```{r}
pc <- prcomp(tmp, scale = TRUE, center = TRUE)
pc_df <- tibble(age = rownames(pc$x), pc1 = pc$x[,1], pc2 = pc$x[,2], pc3 = pc$x[,3])
ggplot(pc_df, aes(x = pc1, y = pc2, label = age)) +
geom_text()
ggplot(pc_df, aes(x = pc2, y = pc3, label = age)) +
geom_text()
```
```{r}
dta_Mx %>%
filter(code == "USA") %>%
filter(sex == "female") %>%
filter(year >= 1970) %>%
filter(age <= 109) %>%
mutate(lnMx = log(Mx + 0.00001, 10)) %>%
select(-code, -sex, -Mx) %>%
spread(year, lnMx) -> tmp2
nms <- tmp2$age
tmp2$age <- NULL
rownames(tmp2) <- nms
tmp2
hc2 <- hclust(dist(tmp2), "ave")
ggdendro::ggdendrogram(hc2, rotate = TRUE, leaf_labels = TRUE) + coord_flip()
pc2 <- prcomp(tmp2, scale = TRUE, center = TRUE)
pc2_df <- pc2$x[,1:3] %>% as.tibble() %>% mutate(age = rownames(pc2$x))
ggplot(pc2_df, aes(x = PC1, y = PC2, label = age)) +
geom_text()
ggplot(pc2_df, aes(x = PC2, y = PC3, label = age)) +
geom_text()
```