#############################################
## Rework mortality risk using Gompertz Model
#############################################
library(ggplot2); theme_set(theme_bw())
library(flexsurv) # For pgompertz
library(survival) # For fitting
library(readxl)
library(janitor)
# Read in Raw Lifetable Data
<- read.table("gompertz-mortality/data/mltper_1x1.txt",skip=2,header=T,as.is=TRUE)
deaths.male <- read.table("gompertz-mortality/data/fltper_1x1.txt",skip=2,header=T,as.is=TRUE)
deaths.female
<- readxl::read_xlsx("gompertz-mortality/data/un-data_2022-07-31/MLT_UN2011_130_1y_complete.xlsx")
un_
<- un_ %>%
un select(Age = age, Family, Sex, Type, mx = mx1, qx = qx1, ax = ax1, lx = lx1, dx = dx1, Lx = Lx1, Tx = Tx1, ex = ex1) %>%
as_tibble()
# Make Some Edits
which(deaths.male$Age=="110+"),]$Age <- 110
deaths.male[which(deaths.female$Age=="110+"),]$Age <- 110
deaths.female[$Age <- as.numeric(deaths.male$Age)
deaths.male$Age <- as.numeric(deaths.female$Age)
deaths.female
# Isolate each file to 2010 deaths only
.2010 <- subset(deaths.male,Year==2010); head(deaths.male.2010)
deaths.male.2010 <- subset(deaths.female,Year==2010); head(deaths.female.2010)
deaths.female
# Generate the CDF ## CDF == 1-Survival = 1 - lx/10)
.2010$cdf <- with(deaths.male.2010,1-lx/100000)
deaths.male.2010$cdf <- with(deaths.female.2010,1-lx/100000)
deaths.female
# Expand to full data frame
<- data.frame(Age=rep(deaths.male.2010$Age, deaths.male.2010$dx))
deaths.male.long $Death <- 1
deaths.male.longhead(deaths.male.long)
<- data.frame(Age=rep(deaths.female.2010$Age, deaths.female.2010$dx))
deaths.female.long $Death <- 1
deaths.female.longhead(deaths.female.long)
= data.frame(
parameters Age = 0:110,
male.shape=rep(NA,111),
male.rate=rep(NA,111),
female.shape=rep(NA,111),
female.rate=rep(NA,111)
)
for(age in 0:109)
{print(age)
<- with(deaths.male.long[deaths.male.long$Age >= age,], Surv(Age, Death, origin=age))
surv.data <- flexsurvreg(surv.data ~ 1, dist="gompertz")
surv.model
$male.shape[age+1] <- surv.model$coefficients[1]
parameters$male.rate[age+1] <- exp(surv.model$coefficients[2])
parameters
<- with(deaths.female.long[deaths.female.long$Age >= age,], Surv(Age, Death, origin=age))
surv.data <- flexsurvreg(surv.data ~ 1, dist="gompertz")
surv.model
$female.shape[age+1] <- surv.model$coefficients[1]
parameters$female.rate[age+1] <- exp(surv.model$coefficients[2])
parameters
}
<- parameters[1:110,]
parameters
#write.csv(parameters, "gompertz-mortality.csv", row.names=FALSE)
# Let's check the post 40 fit.
<- deaths.male.long[deaths.male.long$Age >= 40,]
surv.data
hist(surv.data$Age, freq=FALSE, main="Census Data from 2010", xlab="Male Deaths Starting at 40", breaks=20)
curve(dgompertz(x-40, parameters$male.shape[41], parameters$male.rate[41]) , add=TRUE, col='red')
# Let's check the post 90 fit.
<- deaths.male.long[deaths.male.long$Age >= 90,]
surv.data
hist(surv.data$Age, freq=FALSE, main="Census Data from 2010", xlab="Male Deaths Starting at 90", breaks=20)
curve(dgompertz(x-90, parameters$male.shape[91], parameters$male.rate[91]) , add=TRUE, col='red')
Modeling Mortality Using a Gompertz Model
Introduction
This document outlines how to model mortality using a gompertz model fit to life table data.