Data

The data used for this assignment is from Denmark. The following two lines of code imports the data used for this assignment.

path <- file.path('C:/Users/Rafae/downloads')
path.fire <- file.path(path, "DNK.txt")

Question A

The aim of the following code is to display the force of mortality of an (x) year old in the years 1932, 1972 and 2012.

The fourty-year intervals allows for the observation of the force of mortality over a great time frame. The formula for the force of mortality is: \[\mu_x = -log(1 - q_x)\]

DNKpop <- read.table(path.fire, header = TRUE)
Mydata <- DNKpop[,c(1,2,3,4,6,7)]

Lifetable_2012 <- subset(Mydata,Year==2012)
force_of_mortality_2012 <- -log(1-Lifetable_2012[,4])
force_of_mortality_2012 <- force_of_mortality_2012[-length(force_of_mortality_2012)]

Lifetable_1972 <- subset(Mydata,Year==1972)
force_of_mortality_1972 <- -log(1-Lifetable_1972[,4])
force_of_mortality_1972 <- force_of_mortality_1972[-length(force_of_mortality_1972)]

Lifetable_1932 <- subset(Mydata,Year==1932)
force_of_mortality_1932 <- -log(1-Lifetable_1932[,4])
force_of_mortality_1932 <- force_of_mortality_1932[-length(force_of_mortality_1932)]

ages <- c(0:109)

Question B

The following code computes the force of mortality for the ages 2, 32, 62 and 70 throughout the period 1835:2016.

Lifetable_Age_2 <- subset(Mydata,Age==2)
force_of_mortality_Age_2 <- -log(1-Lifetable_Age_2[,4])

Lifetable_Age_32 <- subset(Mydata,Age==32)
force_of_mortality_Age_32 <- -log(1-Lifetable_Age_32[,4])

Lifetable_Age_62 <- subset(Mydata,Age==62)
force_of_mortality_Age_62 <- -log(1-Lifetable_Age_62[,4])

Lifetable_Age_70 <- subset(Mydata,Age==70)
force_of_mortality_Age_70 <- -log(1-Lifetable_Age_70[,4])

years <- c(1835:2016)

Question C

The following lines of code determine the survival function of a newborn, using data from 1932, 1972 and 2012.

As a result of medical advances since 1932, the probability of survival for a newborn has sharply increased, as seen in figure 3.

Lifetable_survival_2012 <- subset(Mydata,Year==2012)
px_2012 <- 1 - Lifetable_survival_2012[,4]
Survival_function_2012 <- cumprod(px_2012)

Lifetable_survival_1972 <- subset(Mydata,Year==1972)
px_1972  <- 1 - Lifetable_survival_1972[,4]
Survival_function_1972 <- cumprod(px_1972)

Lifetable_survival_1932 <- subset(Mydata,Year==1932)
px_1932 <- 1 - Lifetable_survival_1932[,4]
Survival_function_1932 <- cumprod(px_1932)

Question D

In the following section, a comparison is drawn between the female and male mortality rates in the years 1932, 1972 and 2012. Note that the natural logarithm of the mortality rates are taken.

path <- file.path('C:/Users/Rafae/downloads')
path.fire <- file.path(path, "DNK.txt")
path.fire2 <- file.path(path, "DNK_males.txt")
path.fire3 <- file.path(path, "DNK_females.txt")

DNKpop_male <- read.table(path.fire2, header = TRUE)
DNKpop_female <- read.table(path.fire3, header = TRUE)
Mydata_male <- DNKpop_male[,c(1,2,3,4,6,7)]
Mydata_female <- DNKpop_female[,c(1,2,3,4,6,7)]


Lifetable_2012_male <- subset(Mydata_male,Year==2012)
mortality_table_2012_male <- Lifetable_2012_male[,4]

Lifetable_1972_male <- subset(Mydata_male,Year==1972)
mortality_table_1972_male <- Lifetable_1972_male[,4]

Lifetable_1932_male <- subset(Mydata_male,Year==1932)
mortality_table_1932_male <- Lifetable_1932_male[,4]

plot(c(ages,110), log(mortality_table_2012_male), type = 'l', lwd = 2, col = "blue", xlab = "Age x", ylab = "log(qx)", main = "Male log of mortality rate by age")

lines(c(ages,110),log(mortality_table_1972_male),col="red")
lines(c(ages,110),log(mortality_table_1932_male),col="green")

labels <- c("2012", "1972","1932")
legend("topleft", inset = .02,title = "Legend",labels,col=c("blue","red","green"), lty = c(1,1,1))

Lifetable_2012_female <- subset(Mydata_female,Year==2012)
mortality_table_2012_female <- Lifetable_2012_female[,4]

Lifetable_1972_female <- subset(Mydata_female,Year==1972)
mortality_table_1972_female <- Lifetable_1972_female[,4]

Lifetable_1932_female <- subset(Mydata_female,Year==1932)
mortality_table_1932_female <- Lifetable_1932_female[,4]