## Importing mortality data from the USA available on the Human Mortality Database (HMD):
data(USA)
## Selecting the exposure and the death count of the year 2010, ranging from 0 to 90 years old:
USA2010 = USA[USA$Year == 2010,]
x = 0:100
Ex = USA2010$Ex.Male[x+1]
Dx = USA2010$Dx.Male[x+1]
y <- log(Dx/Ex)
fit <- dlm(y, M = 100)
## Applying the closing function with different methods:
close1 = dlm_close(fit, method = "plateau")
### Getting new data for the linear and gompertz methods:::
x2 = 101:110
Ex2 = USA2010$Ex.Male[x2+1]
Dx2 = USA2010$Dx.Male[x2+1]
y2 <- log(Dx2/Ex2)
close2 = dlm_close(fit, method = "linear",
new_data = y2)
#### Using the other functions available in the package with the 'ClosedDLM' object:
## qx estimation (See "?fitted" in the BayesMortalityPlus package for more options):
fitted(close2)
## life expectancy (See "?expectancy.DLM" for more options)
expectancy(close2, age = seq(0,120,by=20), graph = FALSE)
## plotting (See "?plot" in the BayesMortalityPlus package for more options):
plot(list(close1, close2, fit),
colors = c("red4","seagreen", "blue"),
labels = c("Plateau method","Linear method", "DLM fitted"),
plotData = FALSE)
Run the code above in your browser using DataLab