## Importing mortality data from the USA available on the Human Mortality Database (HMD):
data(USA)
## Selecting the log mortality rate of the 2010 total population ranging from 0 to 90 years old
USA2010 = USA[USA$Year == 2010,]
x = 0:90
Ex = USA2010$Ex.Total[x+1]
Dx = USA2010$Dx.Total[x+1]
y = log(Dx/Ex)
## Fitting HP model
fit = hp(x = x, Ex = Ex, Dx = Dx, model = "lognormal",
m = c(NA, 0.08, rep(NA, 6)),
v = c(NA, 1e-4, rep(NA, 6)))
## Plotting all the available types of plot:
plot_chain(fit, type = "trace")
plot_chain(fit, type = "acf")
plot_chain(fit, type = "density")
## Fitting DLM
fit = dlm(y, M = 100)
plot_chain(fit, param = "sigma2", type = "trace")
plot_chain(fit, param = "mu[10]", type = "acf")
## Selecting all theta1 indexed with 1 in first digit
plot_chain(fit, param = "theta1[1", type = "density")
## Plotting all parameters indexed by age 10 and age 11
plot_chain(fit, param = c("[10]", "[11]"))
Run the code above in your browser using DataLab