-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path01_White_replication.Rmd
2427 lines (1939 loc) · 78.1 KB
/
01_White_replication.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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Slowdown hypothesis"
output:
html_document:
df_print: paged
---
# Introduction
The aim of this notebook is to extend and update the analyses presented in [White (2002)](https://onlinelibrary.wiley.com/doi/pdf/10.1111/j.1728-4457.2002.00059.x).
The figures/analyses are as follows:
* Table 1: How well does a linear time trend in 21 high-income countries explain change in life exptectancy at birth between 1955 and 1991?
* Figure 1: Life expectancy at birth over time, unweighted averages of 21 high-income countries, 1955-96
* Table 2: Change in age-specific death rates between 1955 and 1991, unweighted averages for 21 high-income countries
* Figure 2: Annual change in life expectancy at birth, average of 21 high-income countries, 1955-96
* Figure 3: relationship between life expectancy at birth in 1995 and annual change in life expectancy in 21 high-income countries, 1955-96
* Table 3: Life expectancy at birth and difference from average $e_{0}$ in 21 high-income countries, 1955 to 1995
* Figure 4: Variance betwene life expectancies for 21 high-income countries, 1955-96
* Figure 5: Relationship between early (1955-75) and late (1975-95) gains in life expectancy for 21 high-income countries
All but one of the above can be replicated with the period life expectancy data alone
## Prereqs
```{r}
pacman::p_load(
tidyverse, HMDHFDplus, here,
ggrepel, kableExtra, openxlsx
)
dta_e0 <- read_rds(here("tidy_data","e0_period.rds"))
```
## First step: define countries
The high income countries were:
* Australia (AUS)
* Austria (AUT)
* Belgium (BEL)
* Canada (CAN)
* Denmark (DNK)
* Finland (FIN)
* France (FRATNP)
* Germany (West) (DEUTW)
* Greece (GRC)
* Ireland (IRL)
* Italy (ITA)
* Japan (JPN)
* Netherlands (NLD)
* New Zealand (NZL_NP)
* Norway (NOR)
* Portugal (PRT)
* Spain (ESP)
* Sweden (SWE)
* Switzerland (CHE)
* United Kingdom (GBR_NP)
* United States (USA)
```{r}
source(here("scripts", "country_definitions.R"))
```
# Replications
## Table 1
To do
## Figure 1
Life expectancy at birth over time, unweighted averages of 21 high-income countries, 1955-96
```{r}
fig_01_dta <- dta_e0 %>%
filter(code %in% high_income_countries) %>%
filter(sex == "total") %>%
filter(between(year, 1955, 1996)) %>%
group_by(year) %>%
summarise(mean_e0 = mean(e0, na.rm = T)) %>%
ungroup()
fig_01_dta %>%
ggplot(aes(x = year, y = mean_e0)) +
geom_point() +
scale_x_continuous(breaks = seq(1955, 2015, by = 5)) +
scale_y_continuous(breaks = seq(65, 85, by = 1)) +
stat_smooth(method = "lm", se = F, colour = "black", linetype = "dashed") +
labs(
x = "Year",
y = "Life expectancy in years",
title = "Mean life expectancy of 21 high income countries from 1955 to 1996",
subtitle = "Dashed line is linear regression trend line",
caption = "Unweighted average of 21 countries used in White (2002), replication of figure 1"
)
ggsave(here("figures", "pt1_white_orig.png"), height = 12, width = 20, units = "cm", dpi = 300)
fig_01_dta %>%
ggplot(aes(x = year, y = mean_e0)) +
geom_point() +
scale_x_continuous(breaks = seq(1955, 2015, by = 5)) +
scale_y_continuous(breaks = seq(65, 85, by = 1)) +
stat_smooth(method = "lm", se = F, colour = "black", linetype = "dashed") +
labs(
x = "Year",
y = "Life expectancy in years"
)
ggsave(here("figures", "pt1_white_orig_plain.png"), height = 12, width = 20, units = "cm", dpi = 300)
```
And what's the average improvement per year?
```{r}
fig_01_dta %>%
lm(mean_e0 ~ year, data = .) %>%
summary()
```
Around 0.211 years/year, not the 0.208 given in the paper. (So close enough given changes in data quality and HMD protocol changes)
```{r}
fig_01_dta %>%
lm(mean_e0 ~ I(year-1950), data = .) %>%
summary()
```
Now how has this continued?
```{r}
tmp <- dta_e0 %>%
filter(code %in% high_income_countries) %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
group_by(year) %>%
summarise(mean_e0 = mean(e0, na.rm = T)) %>%
ungroup()
tmp %>%
ggplot(aes(x = year, y = mean_e0)) +
geom_point() +
scale_x_continuous(breaks = seq(1955, 2015, by = 5)) +
scale_y_continuous(breaks = seq(65, 85, by = 1)) +
stat_smooth(method = "lm", se = F, colour = "black", linetype = "dashed") +
labs(
x = "Year",
y = "Life expectancy in years",
title = "Mean life expectancy of 21 high income countries from 1955 to 2016",
subtitle = "Dashed line is linear regression trend line",
caption = "Unweighted average of 21 countries used in White (2002), replication and updating of figure 1"
)
ggsave(here("figures", "pt1_white_update.png"), height = 12, width = 20, units = "cm", dpi = 300)
ggsave(here("figures", "e0_limits", "fig_1_a_all_highincome.png"), height = 12, width = 20, units = "cm", dpi = 300)
tmp %>%
ggplot(aes(x = year, y = mean_e0)) +
geom_point() +
scale_x_continuous(breaks = seq(1955, 2015, by = 5)) +
scale_y_continuous(breaks = seq(65, 85, by = 1)) +
stat_smooth(method = "lm", se = F, colour = "black", linetype = "dashed") +
labs(
x = "Year",
y = "Life expectancy in years"
)
ggsave(here("figures", "pt1_white_update_plain.png"), height = 12, width = 20, units = "cm", dpi = 300)
ggsave(here("figures", "e0_limits", "fig_1_a_all_highincome_plain.png"), height = 12, width = 20, units = "cm", dpi = 300)
```
The near perfect linearlity has continued beyond 1996.
What does this look like for the earlier and later period?
```{r}
tmp %>%
lm(mean_e0 ~ year, .) %>%
summary()
tmp %>%
filter(year < 1993) %>%
lm(mean_e0 ~ year, .) %>%
summary()
tmp %>%
filter(year >= 1993) %>%
lm(mean_e0 ~ year, .) %>%
summary()
```
The R-squared value is over 99% for all years, the earlier period, and the latter period.
The rate of annual increase actually increased rather than fell.
Let's do this for each decade
```{r}
dta_e0 %>%
filter(code %in% high_income_countries) %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
mutate(decade = cut(year, seq(1955, 2015, by = 10), include.lowest = TRUE)) %>%
group_by(year, decade) %>%
summarise(mean_e0 = mean(e0, na.rm = T)) %>%
group_by(decade) %>%
nest() %>%
filter(!is.na(decade)) %>%
mutate(mod = map(data, ~lm(mean_e0 ~ I(year - min(year)), data = .))) %>%
mutate(tdy_mod = map(mod, broom::tidy)) %>%
select(decade, tdy_mod) %>%
unnest()
```
So, there is no evidence in slowdown of the trend for the mean of high income countries.
* From 1955-1964 the average improvement was 0.204 years/year.
* From 1965-1974 the average improvement was 0.188 years/year.
* From 1975-1984 the average improvement was 0.248 years/year.
* From 1985-1994 the average improvement was 0.196 years/year.
* From 1995-2004 the average improvement was 0.245 years/year.
* From 2005-2014 the average improvement was 0.189 years/year.
By subtracting the minimum year in each decade, the intercepts now show the life expectancy at the start of the period, and so are more meaningful. This increased from 69 in 1955 to 80 in 2005.
Table of the above
```{r}
show_2dp <- function(x){format(round(x, 2), nsmall = 2)}
make_wide_table <- function(x = dta_e0,
country_selection,
sex_selection = c("total", "male", "female"),
period_selection = c(1955, 2016),
cut_selection = seq(1955, 2015, by = 10)
){
x %>%
filter(code %in% country_selection) %>%
filter(sex == sex_selection) %>%
filter(between(year, period_selection[1], period_selection[2])) %>%
mutate(decade = cut(year, cut_selection, include.lowest = TRUE)) %>%
group_by(year, decade) %>%
summarise(mean_e0 = mean(e0, na.rm = T)) %>%
group_by(decade) %>%
nest() %>%
filter(!is.na(decade)) %>%
mutate(mod = map(data, ~lm(mean_e0 ~ I(year - min(year)), data = .))) %>%
mutate(tdy_mod = map(mod, broom::tidy)) %>%
select(decade, tdy_mod) %>%
unnest() %>%
mutate(estimate = round(estimate, 2), lower = round(estimate - 2 * std.error, 2), upper = round(estimate + 2 * std.error, 2)) %>%
mutate(text = glue::glue(
"{show_2dp(estimate)} [{show_2dp(lower)} to {show_2dp(upper)}]"
)
) %>%
select(decade, term, text) %>%
spread(term, text) %>%
rename(
`Decade` = `decade`,
`Life expectancy at start of decade (+/- 2 SEs)` = `(Intercept)`,
`Mean gain in life expectancy per year (+/- 2 SEs)` = `I(year - min(year))`
)
}
save_format_dt <- function(wb, sheet_name, dta, cols = 1:3){
wb %>%
addWorksheet(sheetName = sheet_name)
wb %>% writeDataTable(sheet = sheet_name, x = dta)
wb %>% setColWidths(sheet = sheet_name, cols = cols, widths = "auto")
}
wb <- createWorkbook()
# wb %>% addWorksheet(sheetName = "Decadal gains, high income")
# wb %>% openxlsx::writeDataTable(sheet = "Decadal gains, high income",
# x = make_wide_table(country_selection = high_income_countries)
# )
# wb %>% addWorksheet(sheetName = "Decadal gains, all")
# wb %>% openxlsx::writeDataTable(sheet = "Decadal gains, all",
# x = make_wide_table(country_selection = all_distinct_countries)
# )
# wb %>% setColWidths(sheet = "Decadal gains, all", cols = 1:3, widths = "auto")
save_format_dt(wb = wb,
sheet_name = "Decadal gains, high income",
dta = make_wide_table(country_selection = high_income_countries)
)
save_format_dt(wb = wb,
sheet_name = "Decadal gains, high income, fem",
dta = make_wide_table(country_selection = high_income_countries, sex_selection = "female" )
)
save_format_dt(wb = wb,
sheet_name = "Decadal gains, high_income, mal",
dta = make_wide_table(country_selection = high_income_countries, sex_selection = "male")
)
save_format_dt(wb = wb,
sheet_name = "Decadal gains, all",
dta = make_wide_table(country_selection = all_distinct_countries)
)
save_format_dt(wb = wb,
sheet_name = "Decadal gains, all, fem",
dta = make_wide_table(country_selection = all_distinct_countries, sex_selection = "female" )
)
save_format_dt(wb = wb,
sheet_name = "Decadal gains, all, mal",
dta = make_wide_table(country_selection = all_distinct_countries, sex_selection = "male")
)
saveWorkbook(wb, file = here("tables", "decadal_summaries.xlsx"), overwrite = TRUE)
```
As above, all countries
```{r}
tmp <- dta_e0 %>%
filter(code %in% all_distinct_countries) %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
group_by(year) %>%
summarise(mean_e0 = mean(e0, na.rm = T)) %>%
ungroup()
tmp %>%
ggplot(aes(x = year, y = mean_e0)) +
geom_point() +
scale_x_continuous(breaks = seq(1955, 2015, by = 5)) +
scale_y_continuous(breaks = seq(65, 85, by = 1)) +
stat_smooth(method = "lm", se = F, colour = "black", linetype = "dashed") +
labs(
x = "Year",
y = "Life expectancy in years",
title = "Mean life expectancy of all HMD countries from 1955 to 2016",
subtitle = "Dashed line is linear regression trend line",
caption = "Unweighted average of all available countries, replication and updating of figure 1"
)
ggsave(here("figures", "e0_limits", "appendix", "fig_1_a_all_hmd.png"), height = 12, width = 20, units = "cm", dpi = 300)
tmp %>%
ggplot(aes(x = year, y = mean_e0)) +
geom_point() +
scale_x_continuous(breaks = seq(1955, 2015, by = 5)) +
scale_y_continuous(breaks = seq(65, 85, by = 1)) +
stat_smooth(method = "lm", se = F, colour = "black", linetype = "dashed") +
labs(
x = "Year",
y = "Life expectancy in years"
)
ggsave(here("figures", "e0_limits", "appendix", "fig_1_a_all_hmd_plain.png"), height = 12, width = 20, units = "cm", dpi = 300)
```
The fit is clearly less linear over time. This is likely due to changing composition of the countries over time, perhaps due to many Eastern European countries appearing post 1990ish, pulling the average down, but then increasing quickly thereafter.
Again, what's the $R^2$ overall and by decade?
```{r}
tmp %>%
lm(mean_e0 ~ year, data = .) %>%
summary()
```
Now the $R^2$ is 'only' around 0.98.
What does this look like for the earlier and later period?
```{r}
tmp %>%
lm(mean_e0 ~ year, .) %>%
summary()
tmp %>%
filter(year < 1993) %>%
lm(mean_e0 ~ year, .) %>%
summary()
tmp %>%
filter(year >= 1993) %>%
lm(mean_e0 ~ year, .) %>%
summary()
```
Since 1993, the improvement for all available countries has been almost perfect ($R^2$ > 0.99), with an average improvement of around 0.255 years/year. This is a general phemomena, not confined to historically rich nations.
And again, by decade
```{r}
dta_e0 %>%
filter(code %in% all_distinct_countries) %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
mutate(decade = cut(year, seq(1955, 2015, by = 10), include.lowest = TRUE)) %>%
group_by(year, decade) %>%
summarise(mean_e0 = mean(e0, na.rm = T)) %>%
group_by(decade) %>%
nest() %>%
filter(!is.na(decade)) %>%
mutate(mod = map(data, ~lm(mean_e0 ~ I(year - min(year)), data = .))) %>%
mutate(tdy_mod = map(mod, broom::tidy)) %>%
select(decade, tdy_mod) %>%
unnest()
```
So, there is no evidence in slowdown of the trend for the mean of all distinct countries.
* From 1955-1964 the average improvement was 0.220 years/year.
* From 1965-1974 the average improvement was 0.103 years/year.
* From 1975-1984 the average improvement was 0.164 years/year.
* From 1985-1994 the average improvement was 0.067 years/year.
* From 1995-2004 the average improvement was 0.228 years/year.
* From 2005-2014 the average improvement was 0.287 years/year.
The expansion of the range of countries in the HMD appears to have led to a slowdown from 1985-1994, but this is almost certainly compositional rather than genuine for any particular country. From 2005-2014 the average rate of improvement was faster than in the previous decadal period.
And as a more nicely formatted table
```{r}
dta_e0 %>%
filter(code %in% all_distinct_countries) %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
mutate(decade = cut(year, seq(1955, 2015, by = 10), include.lowest = TRUE)) %>%
group_by(year, decade) %>%
summarise(mean_e0 = mean(e0, na.rm = T)) %>%
group_by(decade) %>%
nest() %>%
filter(!is.na(decade)) %>%
mutate(mod = map(data, ~lm(mean_e0 ~ I(year - min(year)), data = .))) %>%
mutate(tdy_mod = map(mod, broom::tidy)) %>%
select(decade, tdy_mod) %>%
unnest() %>%
mutate(text = glue::glue(
"{show_2dp(estimate)} [{show_2dp(estimate - 2 * std.error)} to {show_2dp(estimate + 2 * std.error)}]"
)
) %>%
select(decade, term, text) %>%
spread(term, text) %>%
rename(
`Decade` = `decade`,
`Life expectancy at start of decade (+/- 2 SEs)` = `(Intercept)`,
`Mean gain in life expectancy per year (+/- 2 SEs)` = `I(year - min(year))`
)
```
Let's look at how the UK compares with this.
```{r}
tmp <- dta_e0 %>%
filter(code %in% high_income_countries) %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
group_by(year) %>%
summarise(mean_e0 = mean(e0, na.rm = T)) %>%
ungroup()
tmp %>%
ggplot(aes(x = year, y = mean_e0)) +
geom_point() +
stat_smooth(method = "lm", se = F, colour = "black", linetype = "dashed") +
scale_x_continuous(breaks = seq(1955, 2015, by = 5)) +
scale_y_continuous(breaks = seq(65, 85, by = 1)) +
geom_point(
aes(x = year, y = e0),
inherit.aes = F,
data = dta_e0 %>%
filter(sex == "total") %>%
filter(code == "GBR_NP") %>%
filter(between(year, 1955, 2016)) ,
colour = "darkred", shape = 1
) +
labs(
x = "Year",
y = "Life expectancy",
title = "Life expectancy over time for UK and high income average",
subtitle = "Red unfilled: UK; black filled: mean of 21 high income nations",
caption = "Black dashed line: linear regression of mean of 21 high income countries. Source: HMD"
)
ggsave(here("figures", "pt1_white_update_uk.png"), height = 12, width = 20, units = "cm", dpi = 300)
ggsave(here("figures", "e0_limits", "fig_01_b_highincomeuk.png"), height = 12, width = 20, units = "cm", dpi = 300)
tmp %>%
ggplot(aes(x = year, y = mean_e0)) +
geom_point() +
stat_smooth(method = "lm", se = F, colour = "black", linetype = "dashed") +
scale_x_continuous(breaks = seq(1955, 2015, by = 5)) +
scale_y_continuous(breaks = seq(65, 85, by = 1)) +
geom_point(
aes(x = year, y = e0),
inherit.aes = F,
data = dta_e0 %>%
filter(sex == "total") %>%
filter(code == "GBR_NP") %>%
filter(between(year, 1955, 2016)) ,
colour = "darkred", shape = 1
) +
labs(
x = "Year",
y = "Life expectancy"
)
ggsave(here("figures", "pt1_white_update_uk_plain.png"), height = 12, width = 20, units = "cm", dpi = 300)
ggsave(here("figures", "e0_limits", "fig_01_b_highincomeuk_plain.png"), height = 12, width = 20, units = "cm", dpi = 300)
```
So the UK fell below the rich country average in the late 1960s, and hasn't returned since.
It looked like the UK was starting to close the gap in the late 2000s, but it then fell back in recent years.
And the UK compared with all high income countries.
```{r}
tmp <- dta_e0 %>%
filter(code %in% all_distinct_countries) %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
group_by(year) %>%
summarise(mean_e0 = mean(e0, na.rm = T)) %>%
ungroup()
tmp %>%
ggplot(aes(x = year, y = mean_e0)) +
geom_point() +
stat_smooth(method = "lm", se = F, colour = "black", linetype = "dashed") +
geom_point(
aes(x = year, y = e0),
inherit.aes = F,
data = dta_e0 %>%
filter(sex == "total") %>%
filter(code == "GBR_NP") %>%
filter(between(year, 1955, 2016)) ,
colour = "darkred", shape = 17
) +
labs(
x = "Year",
y = "Life expectancy",
title = "Life expectancy over time for average of all available countries (black) and the UK (red)",
caption = "Black line is unweighted average of all countries available in HMD for each year."
)
ggsave(here("figures", "e0_limits", "appendix", "fig_01_b_allhmd.png"), height = 12, width = 20, units = "cm", dpi = 300)
tmp %>%
ggplot(aes(x = year, y = mean_e0)) +
geom_point() +
stat_smooth(method = "lm", se = F, colour = "black", linetype = "dashed") +
geom_point(
aes(x = year, y = e0),
inherit.aes = F,
data = dta_e0 %>%
filter(sex == "total") %>%
filter(code == "GBR_NP") %>%
filter(between(year, 1955, 2016)) ,
colour = "darkred", shape = 17
) +
labs(
x = "Year",
y = "Life expectancy"
)
ggsave(here("figures", "e0_limits", "appendix", "fig_01_b_allhmd_plain.png"), height = 12, width = 20, units = "cm", dpi = 300)
```
And what was the rate of improvement in UK only?
```{r}
dta_e0 %>%
filter(code== "GBR_NP") %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
mutate(decade = cut(year, seq(1955, 2015, by = 10), include.lowest = TRUE)) %>%
group_by(decade) %>%
nest() %>%
filter(!is.na(decade)) %>%
mutate(mod = map(data, ~lm(e0 ~ I(year - min(year)), data = .))) %>%
mutate(tdy_mod = map(mod, broom::tidy)) %>%
select(decade, tdy_mod) %>%
unnest()
```
And nicely formatted
```{r}
dta_e0 %>%
filter(code== "GBR_NP") %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
mutate(decade = cut(year, seq(1955, 2015, by = 10), include.lowest = TRUE)) %>%
group_by(decade) %>%
nest() %>%
filter(!is.na(decade)) %>%
mutate(mod = map(data, ~lm(e0 ~ I(year - min(year)), data = .))) %>%
mutate(tdy_mod = map(mod, broom::tidy)) %>%
select(decade, tdy_mod) %>%
unnest() %>%
mutate(text = glue::glue(
"{show_2dp(estimate)} [{show_2dp(estimate - 2 * std.error)} to {show_2dp(estimate + 2 * std.error)}]"
)
) %>%
select(decade, term, text) %>%
spread(term, text) %>%
rename(
`Decade` = `decade`,
`Life expectancy at start of decade (+/- 2 SEs)` = `(Intercept)`,
`Mean gain in life expectancy per year (+/- 2 SEs)` = `I(year - min(year))`
)
```
### Life expectancy for best country (labelled by this country)
A similar approach to White (2002) is presented in [Christenson 2009](https://linkinghub.elsevier.com/retrieve/pii/S0140673609614604) Their approach is to look at life expectancy for the top performing country, rather than the average of these. Let's now replicate this
```{r}
tmp <- dta_e0 %>%
filter(code %in% high_income_countries) %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
group_by(year) %>%
summarise(
max_e0 = max(e0, na.rm = T),
which_code = code[e0 == max(e0)]
) %>%
ungroup()
tmp %>%
ggplot(aes(x = year, y = max_e0)) +
geom_point() +
geom_text(
aes(x = year, y = max_e0, label = which_code),
inherit.aes = F,
data = tmp %>% filter(year %in% seq(1955, 2015, by = 5)),
nudge_y = 0.5
) +
stat_smooth(method = "lm", se = F, colour = "black", linetype = "dashed") +
geom_vline(xintercept = 1996)
```
Japan has remained the high income country with the highest life expectancy since the mid 1980s.
What's the linear rate of improvement for the best performing line compared with the average improvement line?
```{r}
tmp %>%
filter(between(year, 1955, 2016)) %>%
lm(max_e0 ~ I(year - min(year)), .) %>%
summary()
```
So, the rate of improvement has been about 0.198 years/year for the best performing line, compared with 0.191 years/ year for the average of the 21 countries.
Let's now plot the mean, upper and lower together.
```{r}
tmp <- dta_e0 %>%
filter(code %in% high_income_countries) %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
group_by(year) %>%
summarise(
max_e0 = max(e0, na.rm = T),
which_code_max = code[e0 == max(e0)],
min_e0 = min(e0, na.rm = T),
which_code_min = code[e0 == min(e0)],
mean_e0 = mean(e0, na.rm = T),
uk_e0 = e0[code == "GBR_NP"]
) %>%
ungroup()
tmp %>%
ggplot(aes(x = year)) +
geom_point(aes(y = max_e0), colour = "darkgreen") +
geom_text(
aes(x = year, y = max_e0, label = which_code_max),
inherit.aes = F,
colour = "darkgreen",
data = tmp %>% filter(year %in% seq(1955, 2015, by = 5)),
nudge_y = 0.5
) +
geom_point(aes(y = mean_e0)) +
geom_point(aes(y = min_e0), colour = "red") +
geom_point(aes(y = uk_e0), colour = "darkred", shape = 17) +
geom_text(
aes(x = year, y = min_e0, label = which_code_min),
inherit.aes = F,
colour = "red",
data = tmp %>% filter(year %in% seq(1955, 2015, by = 5)),
nudge_y = - 0.5
) +
stat_smooth(aes(y = max_e0), method = "lm", se = F, colour = "darkgreen", linetype = "dashed") +
stat_smooth(aes(y = min_e0), method = "lm", se = F, colour = "red", linetype = "dashed") +
stat_smooth(aes(y = mean_e0), method = "lm", se = F, colour = "black", linetype = "dashed") +
stat_smooth(aes(y = uk_e0), method = "lm", se = F, colour = "darkred", linetype = "dashed") +
labs(
x = "Year", y = "Life expectancy in years",
title = "Life expectancy trends for best performing, worst performing, average high income, and UK, 1955-2016",
subtitle = "Best performing: Green; Worst performing: Red; Average: Black; UK: Dark Red",
caption = "Unweighted average of 21 high income countries in White 2002. Linear trends as dashed lines"
)
ggsave(here("figures", "pt1_sandwich.png"), height = 12, width = 20, units = "cm", dpi = 300)
ggsave(here("figures", "e0_limits", "fig_1_c_sandwich.png"), height = 12, width = 20, units = "cm", dpi = 300)
tmp %>%
ggplot(aes(x = year)) +
geom_point(aes(y = max_e0), colour = "darkgreen") +
geom_text(
aes(x = year, y = max_e0, label = which_code_max),
inherit.aes = F,
colour = "darkgreen",
data = tmp %>% filter(year %in% seq(1955, 2015, by = 5)),
nudge_y = 0.5
) +
geom_point(aes(y = mean_e0)) +
geom_point(aes(y = min_e0), colour = "red") +
geom_point(aes(y = uk_e0), colour = "darkred", shape = 17) +
geom_text(
aes(x = year, y = min_e0, label = which_code_min),
inherit.aes = F,
colour = "red",
data = tmp %>% filter(year %in% seq(1955, 2015, by = 5)),
nudge_y = - 0.5
) +
stat_smooth(aes(y = max_e0), method = "lm", se = F, colour = "darkgreen", linetype = "dashed") +
stat_smooth(aes(y = min_e0), method = "lm", se = F, colour = "red", linetype = "dashed") +
stat_smooth(aes(y = mean_e0), method = "lm", se = F, colour = "black", linetype = "dashed") +
stat_smooth(aes(y = uk_e0), method = "lm", se = F, colour = "darkred", linetype = "dashed") +
labs(
x = "Year", y = "Life expectancy in years"
)
ggsave(here("figures", "pt1_sandwich_plain.png"), height = 12, width = 20, units = "cm", dpi = 300)
ggsave(here("figures", "e0_limits", "fig_1_c_sandwich_plain.png"), height = 12, width = 20, units = "cm", dpi = 300)
```
In the figure above, the red dots show the worst performing of the 21 high income countries, the green dots that for the best performing country. The black dots show the average (mean) for these countries, and the blue dots show the life expectancies for the UK. Every five years, the best and worst performing country is labelled.
Table summarising above
* Intercept: $e_0$ in 1955
* Slope: Average $e_0$ gain per year since
* Fit: $R^2$
For worst, best, mean
```{r}
summary_best_worst_mean_hi <-
dta_e0 %>%
filter(code %in% high_income_countries) %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
group_by(year) %>%
summarise(
max_e0 = max(e0, na.rm = T),
which_code_max = code[e0 == max(e0)],
min_e0 = min(e0, na.rm = T),
which_code_min = code[e0 == min(e0)],
mean_e0 = mean(e0, na.rm = T),
uk_e0 = e0[code == "GBR_NP"]
) %>%
ungroup() %>%
select(year, min_e0, max_e0, mean_e0) %>%
gather(-year, key = "popn", value = "e0") %>%
group_by(popn) %>%
nest() %>%
mutate(model = map(data, ~lm(e0 ~ year, data = .))) %>%
mutate(mdl_tidy = map(model, broom::tidy)) %>%
mutate(mdl_diag = map(model, broom::glance))
```
```{r, results='asif'}
pt1 <-
summary_best_worst_mean_hi %>%
select(popn, mdl_tidy) %>%
unnest(mdl_tidy) %>%
filter(term == "year")
pt2 <-
summary_best_worst_mean_hi %>%
select(popn, mdl_diag) %>%
unnest(mdl_diag) %>%
select(popn, r.squared, adj.r.squared)
pt3 <-
dta_e0 %>%
filter(code %in% high_income_countries) %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
group_by(year) %>%
summarise(
max_e0 = max(e0, na.rm = T),
which_code_max = code[e0 == max(e0)],
min_e0 = min(e0, na.rm = T),
which_code_min = code[e0 == min(e0)],
mean_e0 = mean(e0, na.rm = T),
uk_e0 = e0[code == "GBR_NP"]
) %>%
ungroup() %>%
select(year, min_e0, max_e0, mean_e0) %>%
filter(year == 1955) %>%
gather(-year, key = "popn", value = "e0_1955") %>%
select(-year)
pt1 %>% left_join(pt2) %>% left_join(pt3) %>%
select(-term) %>%
ungroup() %>%
mutate(popn = case_when(
popn == "min_e0" ~ "Lowest",
popn == "max_e0" ~ "Highest",
popn == "mean_e0" ~ "Average",
TRUE ~ NA_character_
)) %>%
rename(slope = estimate) %>%
select(popn, e0_1955, slope, everything(), -p.value) %>%
kable(digits = 3,
col.names = c("Population", "e0 in 1955", "Average annual gain", "SE", "t value", "R squared", "Adj. R Squared")) %>%
kable_styling()
```
Life expectancy for all countries
```{r}
summary_best_worst_mean_hi <-
dta_e0 %>%
filter(code %in% all_distinct_countries) %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
group_by(year) %>%
summarise(
max_e0 = max(e0, na.rm = T),
which_code_max = code[e0 == max(e0)],
min_e0 = min(e0, na.rm = T),
which_code_min = code[e0 == min(e0)],
mean_e0 = mean(e0, na.rm = T),
uk_e0 = e0[code == "GBR_NP"]
) %>%
ungroup() %>%
select(year, min_e0, max_e0, mean_e0) %>%
gather(-year, key = "popn", value = "e0") %>%
group_by(popn) %>%
nest() %>%
mutate(model = map(data, ~lm(e0 ~ year, data = .))) %>%
mutate(mdl_tidy = map(model, broom::tidy)) %>%
mutate(mdl_diag = map(model, broom::glance))
```
```{r, results = 'asis'}
pt1 <-
summary_best_worst_mean_hi %>%
select(popn, mdl_tidy) %>%
unnest(mdl_tidy) %>%
filter(term == "year")
pt2 <-
summary_best_worst_mean_hi %>%
select(popn, mdl_diag) %>%
unnest(mdl_diag) %>%
select(popn, r.squared, adj.r.squared)
pt3 <-
dta_e0 %>%
filter(code %in% all_distinct_countries) %>%
filter(sex == "total") %>%
filter(between(year, 1955, 2016)) %>%
group_by(year) %>%
summarise(
max_e0 = max(e0, na.rm = T),
which_code_max = code[e0 == max(e0)],
min_e0 = min(e0, na.rm = T),
which_code_min = code[e0 == min(e0)],
mean_e0 = mean(e0, na.rm = T),
uk_e0 = e0[code == "GBR_NP"]
) %>%
ungroup() %>%
select(year, min_e0, max_e0, mean_e0) %>%
filter(year == 1955) %>%
gather(-year, key = "popn", value = "e0_1955") %>%
select(-year)
pt1 %>% left_join(pt2) %>% left_join(pt3) %>%
select(-term) %>%
ungroup() %>%
mutate(popn = case_when(
popn == "min_e0" ~ "Lowest",
popn == "max_e0" ~ "Highest",
popn == "mean_e0" ~ "Average",
TRUE ~ NA_character_
)) %>%
rename(slope = estimate) %>%
select(popn, e0_1955, slope, everything(), -p.value) %>%
kable(digits = 3,
col.names = c("Population", "e0 in 1955", "Average annual gain", "SE", "t value", "R squared", "Adj. R Squared")) %>%
kable_styling()
```
Let's do the above, but for all countries
```{r}
all_countries_summary <-
dta_e0 %>%
filter(code %in% all_distinct_countries) %>%
mutate(high_income = ifelse(code %in% high_income_countries, "High Income", "Other")) %>%
filter(between(year, 1955, 2016)) %>%
mutate(start_year = year - min(year)) %>%
group_by(code, high_income, sex) %>%
nest() %>%
mutate(linmod = map(data, ~lm(e0 ~ start_year, data = .))) %>%
mutate(
tidied = map(linmod, broom::tidy),
glanced = map(linmod, broom::glance)
) %>%
select(code, sex, high_income, tidied, glanced) %>%
unnest(cols = c(tidied, glanced), names_sep = "_") %>%
select(code, high_income, sex,
term = tidied_term, estimate = tidied_estimate, SE = tidied_std.error, t_value = tidied_statistic,
r_squared = glanced_r.squared, adj_r_squared = glanced_adj.r.squared
) %>%
arrange(high_income, code, sex)
wb <- createWorkbook()
addWorksheet(wb, sheetName = "All Countries Summaries")
writeData(wb = wb, sheet = "All Countries Summaries", x = all_countries_summary)
openxlsx::saveWorkbook(wb, file = here("tables", "all_summaries.xlsx"), overwrite = TRUE)
all_countries_summary
```
```{r}
tmp %>%
lm(max_e0 ~ I(year - min(year)), .) %>%
summary()
tmp %>%
lm(min_e0 ~ I(year - min(year)), .) %>%