Learning and Forecasting of Age–Specific Period Mortality via B–Spline Processes with Locally–Adaptive Dynamic Coefficients
This repository is associated with the article "Learning and Forecasting of Age–Specific Period Mortality via B–Spline Processes with Locally–Adaptive Dynamic Coefficients" and aims at providing detailed materials and codes to implement the BSP model presented in the article and to fully reproduce the results in Section 4.
In order to obtain all the results from the paper, run main.R. A step-by-step explanation on how to implement and fit the model is given in the notebook application-countries/BSP_stepbystep.Rmd.
A general overview of the organization of the repository is provided below.
main.Rcontains the call to all the scripts necessary to reproduce the results in Section 4 of the paper;datacontains the data as downloaded from the Human Mortality Database;sourcecontains all theRfunctions required to process the data, fit the model, and make forecasts:data_processing.Rcontains the preprocessing of the data for the countries analyzed in the article;data_processing_extra.Rcontains the preprocessing of the data for additional countries;setup.Rcontains some setting of the model, such as the spline knots and the kernel function;BSP.Rcontains the code to implement and fit the BSP model;BSP_forecast.Rcontains the code to implement and fit the BSP forecasting model;helper_fun.Rcontains useful functions to post-process results and create plot-friendly dataframes.
application-countriescontains reproducible code for the application to US, UK, Sweden, and Italy, as described in detail in Section 4 of the paper;outputall the outputs generated by any of the previous scripts will be save in this folder (for example, the fileITA_fit.Rdatais the result of the scripts run inBSP_stepbystep.Rmd);
It is possible to include the model comparison for the additional countries France (FRA), Denmark (DNK), and Czech Republic (CZE) by first running /application-countries/model-comparison/extra/main_extra.R and then setting flag_extra_countries <- TRUE before running /application-countries/model-comparison/results.R.
In order to include the comparison with the plain nGP model and the Kalman forecast, it is necessary to run the respective scripts in /application-countries/model-comparison/ and to set flag_NGP_Kalman <- TRUE before running /application-countries/model-comparison/results.R.
In case the parallelization is an issue on your machine, you can run the R-scripts on a single core. For the experiments related to the BSP model (reporting _BSP in the naming of the file), it is enough to set parallel=FALSE in the argument of the function bsp.fit or rolling_uq. For the files implementing the other models used for the comparison (see scripts in /application-countries/model-comparison/), you have to change the command mclapply to lapply.
Note: for those who want to inspect the implementation of the functions in source, they should keep in mind that the notation of the code uses letter U referring to parameters
The analyses are performed using R version 4.0.3 and KFAS package version 1.4.6. Note that, although a seed is set at the beginning of each script, the final output reported in the tables and figures may be subject to slight variations depending on which version of the R packages has been used in the implementation of the code and whether the code is executed with the default parallelization or not. However, the magnitude of these minor variations is negligible and does not affect the final conclusions.