Creating life tables using an R package, LifeTables

(https://cran.r-project.org/web/packages/LifeTables/LifeTables.pdf)

# some data to build the table
data(MLTobs)
?MLTobs

Example 1: Male life table

Provide nMx for male population. nmx=test.mx.m

# build the life table: male
#test.mx.m <- mlt.mx[,1]
#lt_male<-lt.mx(nmx=test.mx.m, sex="male")
test.mx.m <- mlt.mx[1:19,1] ## ages 0-85. THIS SHOULD BE REPLACED WITH REAL DATA. See what it looks like. 
    
    str(test.mx.m)
    View(test.mx.m)

lt_male<-lt.mx(nmx=test.mx.m, sex="male", age=c(0,1,seq(5,85,5)))

results<-as.data.frame(lt_male[[4]])%>%
    mutate(
        year=1930, 
        sex="Male",
        race="Total")

e0_male<-round(results$ex[1], 1)
imr_male<-round(1000*results$nqx[1])
adult_male<-round(1000*(results$lx[5]-results$lx[14])/results$lx[5])
kable(results, caption = "Life table, test, male", row.names = FALSE)%>%
    kable_styling(bootstrap_options = c("hover"), full_width = F)%>%
    column_spec(1, background = "#A4C7E1")%>%
    column_spec(3, background = "#A4C7E1")%>%
    column_spec(5, background = "#E7F1F7")%>%
    column_spec(7, background = "#E7F1F7")%>%
    column_spec(9, background = "#E7F1F7")%>%
    column_spec(11, background = "#CCCCCC")%>%
    column_spec(12, background = "#CCCCCC")%>%
    column_spec(13, background = "#CCCCCC")
Life table, test, male
Age nax nmx nqx npx ndx lx nLx Tx ex year sex race
0 0.229 0.0686 0.0651 0.9349 6515 100000 94978 5979868 59.80 1930 Male Total
1 1.458 0.0059 0.0233 0.9767 2180 93485 368398 5884891 62.95 1930 Male Total
5 2.500 0.0018 0.0090 0.9910 823 91305 454468 5516492 60.42 1930 Male Total
10 2.500 0.0015 0.0075 0.9925 681 90482 450708 5062025 55.95 1930 Male Total
15 2.500 0.0023 0.0112 0.9888 1009 89801 446482 4611318 51.35 1930 Male Total
20 2.500 0.0030 0.0150 0.9850 1330 88792 440635 4164835 46.91 1930 Male Total
25 2.500 0.0035 0.0172 0.9828 1505 87462 433548 3724200 42.58 1930 Male Total
30 2.500 0.0040 0.0196 0.9804 1685 85957 425572 3290652 38.28 1930 Male Total
35 2.500 0.0052 0.0258 0.9742 2175 84272 415922 2865080 34.00 1930 Male Total
40 2.500 0.0069 0.0338 0.9662 2777 82097 403542 2449158 29.83 1930 Male Total
45 2.500 0.0096 0.0470 0.9530 3729 79320 387278 2045615 25.79 1930 Male Total
50 2.500 0.0129 0.0627 0.9373 4738 75591 366110 1658338 21.94 1930 Male Total
55 2.500 0.0183 0.0875 0.9125 6199 70853 338768 1292228 18.24 1930 Male Total
60 2.500 0.0285 0.1329 0.8671 8592 64654 301790 953460 14.75 1930 Male Total
65 2.500 0.0420 0.1900 0.8100 10652 56062 253680 651670 11.62 1930 Male Total
70 2.500 0.0643 0.2770 0.7230 12578 45410 195605 397990 8.76 1930 Male Total
75 2.500 0.1020 0.4063 0.5937 13339 32832 130812 202385 6.16 1930 Male Total
80 2.500 0.1447 0.5313 0.4687 10357 19493 71572 71572 3.67 1930 Male Total
85 NA 0.2251 1.0000 0.0000 9136 9136 NA NA NA 1930 Male Total

Summary for male,
* Life expectancy at birth is 59.8
* Infant mortality rate is 65 per 1000
* Adult mortality rate (45q15) is 280 per 1000

Example 2: Female life table

Provide nMx for female population. nmx=test.mx.f

# some data to build the table
data(MLTobs)

# build the life table: female
#test.mx.f <- flt.mx[,1]
#lt_female<-lt.mx(nmx=test.mx.f, sex="female")
test.mx.f <- flt.mx[1:19,1] ## ages 0-85. THIS SHOULD BE REPLACED WITH REAL DATA
lt_female<-lt.mx(nmx=test.mx.f, sex="female", age=c(0,1,seq(5,85,5)))

results<-as.data.frame(lt_female[[4]])%>%
    mutate(
        year=1930, 
        sex="Female",
        race="Total")

e0_female<-round(results$ex[1], 1)
imr_female<-round(1000*results$nqx[1])
adult_female<-round(1000*(results$lx[5]-results$lx[14])/results$lx[5])
kable(results, caption = "Life table, test, female", row.names = FALSE)%>%
    kable_styling(bootstrap_options = c("hover"), full_width = F)%>%
    column_spec(1, background = "#A4C7E1")%>%
    column_spec(3, background = "#A4C7E1")%>%
    column_spec(5, background = "#E7F1F7")%>%
    column_spec(7, background = "#E7F1F7")%>%
    column_spec(9, background = "#E7F1F7")%>%
    column_spec(11, background = "#CCCCCC")%>%
    column_spec(12, background = "#CCCCCC")%>%
    column_spec(13, background = "#CCCCCC")
Life table, test, female
Age nax nmx nqx npx ndx lx nLx Tx ex year sex race
0 0.204 0.0538 0.0516 0.9484 5161 100000 95890 6343489 63.43 1930 Female Total
1 1.440 0.0050 0.0198 0.9802 1880 94839 374544 6247599 65.88 1930 Female Total
5 2.500 0.0016 0.0078 0.9922 727 92959 462978 5873055 63.18 1930 Female Total
10 2.500 0.0012 0.0060 0.9940 552 92232 459780 5410078 58.66 1930 Female Total
15 2.500 0.0019 0.0093 0.9907 848 91680 456280 4950298 54.00 1930 Female Total
20 2.500 0.0029 0.0142 0.9858 1290 90832 450935 4494018 49.48 1930 Female Total
25 2.500 0.0035 0.0172 0.9828 1536 89542 443870 4043082 45.15 1930 Female Total
30 2.500 0.0040 0.0196 0.9804 1721 88006 435728 3599212 40.90 1930 Female Total
35 2.500 0.0049 0.0241 0.9759 2076 86285 426235 3163485 36.66 1930 Female Total
40 2.500 0.0053 0.0260 0.9740 2186 84209 415580 2737250 32.51 1930 Female Total
45 2.500 0.0068 0.0337 0.9663 2762 82023 403210 2321670 28.31 1930 Female Total
50 2.500 0.0094 0.0459 0.9541 3639 79261 387208 1918460 24.20 1930 Female Total
55 2.500 0.0129 0.0627 0.9373 4740 75622 366260 1531252 20.25 1930 Female Total
60 2.500 0.0196 0.0934 0.9066 6619 70882 337862 1164992 16.44 1930 Female Total
65 2.500 0.0309 0.1435 0.8565 9219 64263 298268 827130 12.87 1930 Female Total
70 2.500 0.0494 0.2197 0.7803 12090 55044 244995 528862 9.61 1930 Female Total
75 2.500 0.0846 0.3493 0.6507 15005 42954 177258 283868 6.61 1930 Female Total
80 2.500 0.1243 0.4742 0.5258 13254 27949 106610 106610 3.81 1930 Female Total
85 NA 0.1992 1.0000 0.0000 14695 14695 NA NA NA 1930 Female Total

Summary for female,
* Life expectancy at birth is 63.4
* Infant mortality rate is 52 per 1000
* Adult mortality rate (45q15) is 227 per 1000