Title: | Bayesian Projection of Life Expectancy |
---|---|
Description: | Making probabilistic projections of life expectancy for all countries of the world, using a Bayesian hierarchical model <doi:10.1007/s13524-012-0193-x>. Subnational projections are also supported. |
Authors: | Hana Sevcikova, Adrian Raftery, Jennifer Chunn |
Maintainer: | Hana Sevcikova <[email protected]> |
License: | GPL-3 | file LICENSE |
Version: | 5.2-0 |
Built: | 2024-11-13 05:29:53 UTC |
Source: | https://github.com/cran/bayesLife |
Collection of functions for making probabilistic projections of the life expectancy for all countries of the world, using a Bayesian hierarchical model and the United Nations demographic time series. Projections on a subnational level are also supported.
The projection follows a method developed by Chunn et al (2010, 2013). It uses historical data provided by the United Nations to simulate a posterior distribution of the life expectancy for all countries in the world simultaneously.
The package is implemented in a similar way as the bayesTFR package and thus, many functions have their equivalents in bayesTFR. The main functions of the bayesLife package are:
run.e0.mcmc: Runs a Markov Chain Monte Carlo (MCMC) simulation for one or more chains, possibly in parallel. It results in a posterior sample of the MCMC parameters. Existing simulation runs can be resumed using continue.e0.mcmc.
e0.predict: Using the posterior parameter samples it derives posterior trajectories of the life expectancy for all countries.
e0.jmale.predict: Given existing outputs of e0.predict for female life expectancy, this function estimates and predicts a joint male life expectancy as described in Raftery et al (2014).
e0.predict.subnat: Given existing projections on national level, it generates projections on a subnational level, for both, female and male (Sevcikova and Raftery 2021).
A number of functions analyzing results are included in the package:
e0.trajectories.plot: Shows the posterior trajectories for a given country, including their median and given probability intervals.
e0.trajectories.table: Shows the posterior trajectories for a given country in a tabular form.
e0.map and e0.map.gvis: Show a world map of life expectancy for a given projection period.
e0.DLcurve.plot: Shows the posterior curves of the double logistic function used in the simulation, including their median and given probability intervals.
e0.partraces.plot and e0.partraces.cs.plot: Plot the MCMC traces of country-independent parameters and country-specific parameters, respectively.
e0.pardensity.plot and e0.pardensity.cs.plot: Plot the posterior density of the MCMCs for country-independent parameters and country-specific parameters, respectively.
summary.bayesLife.mcmc.set: Summary function for the MCMC results.
summary.bayesLife.prediction: Summary function for the prediction results.
For MCMC diagnostics, function e0.coda.list.mcmc creates an object of type “mcmc.list” that can be used with the coda package. Furthermore, function e0.diagnose analyzes the MCMCs using the Raftery diagnostics implemented in the coda package and gives information about parameters that did not converge. Function e0.dl.coverage computes a goodness of fit of the double logistic function.
Existing simulation results can be accessed using the get.e0.mcmc function. An existing prediction can be accessed via get.e0.prediction. Existing predictions on a subnational level can be accessed via get.rege0.prediction.
For a table with countries included in the mcmc or prediction object, the function get.countries.table can be used in the same way as in bayesTFR.
Historical data are taken from one of the packages wpp2019 (default), wpp2017, wpp2015, wpp2012 or wpp2010, depending on users settings. For more recent data, package wpp2022 can be installed from Github (@PPgp).
There is a directory ex-data
shipped with the package which contains results from an example simulation, containing one chain with 60 iterations. The Example section below shows how these results were created. These data are used in Example sections throughout the manual. The user can either reproduce the data in her/his local directory, or use the ones from the package.
Hana Sevcikova, Adrian Raftery, Jennifer Chunn
Maintainer: Hana Sevcikova <[email protected]>
J. L. Chunn, A. E. Raftery, P. Gerland, H. Sevcikova (2013): Bayesian Probabilistic Projections of Life Expectancy for All Countries. Demography 50(3):777-801. <doi:10.1007/s13524-012-0193-x>
A. E. Raftery, N. Li, H. Sevcikova, P. Gerland, G. K. Heilig (2012). Bayesian probabilistic population projections for all countries. Proceedings of the National Academy of Sciences 109:13915-13921.
A. E. Raftery, N. Lalic, P. Gerland (2014). Joint Probabilistic Projection of Female and Male Life Expectancy. Demographic Research, 30:795-822.
H. Sevcikova, A. E. Raftery (2021). Probabilistic Projection of Subnational Life Expectancy. Journal of Official Statistics, , Vol. 37, no. 3, 591-610.
## Not run: sim.dir <- tempfile() m <- run.e0.mcmc(sex = 'F', nr.chains = 1, iter = 60, seed = 1, thin = 1, output.dir = sim.dir, verbose = TRUE) pred <- e0.predict(m, burnin = 30, verbose = TRUE) summary(pred, country = "Canada") unlink(sim.dir, recursive = TRUE) ## End(Not run)
## Not run: sim.dir <- tempfile() m <- run.e0.mcmc(sex = 'F', nr.chains = 1, iter = 60, seed = 1, thin = 1, output.dir = sim.dir, verbose = TRUE) pred <- e0.predict(m, burnin = 30, verbose = TRUE) summary(pred, country = "Canada") unlink(sim.dir, recursive = TRUE) ## End(Not run)
MCMC simulation object bayesLife.mcmc
containing information about one MCMC chain. A set of such objects belonging to the same simulation together with a bayesLife.mcmc.meta
object constitute a bayesLife.mcmc.set
object.
An object bayesLife.mcmc
points to a place on disk (element output.dir
) where MCMC results from all iterations are stored. They can be retrieved to the memory using get.e0.mcmc(...)
.
The object is in standard cases not to be manipulated by itself, but rather as part of a bayesLife.mcmc.set
object.
A bayesLife.mcmc
object contains parameters of the Bayesian hierarchical model, more specifically, their initial values (all names with the suffix .ini
) and values from the last iteration. These are: Triangle/Triangle.ini, lambda/lambda.ini
- world parameters, containing four values each. They correspond to model parameters and
, respectively.
k/k.ini, z/z.ini, omega/omega.ini, lambda.k/lambda.k.ini,
lambda.z/lambda.z.ini
- world parameters, containing one value each. They correspond to model parameters ,
,
,
, and
, respectively.
Triangle.c
- country-specific parameter with four values for each country, i.e. an
matrix where
is the number of countries.
k.c, z.c
- country-specific parameters and
(1d arrays of length
).
Furthermore, the object contains components:
iter |
Total number of iterations the simulation was started with. |
finished.iter |
Number of iterations that were finished. Results from the last finished iteration are stored in the parameters above. |
length |
Length of the MCMC stored on disk. It differs from |
thin |
Thinning interval used when simulating the MCMCs. |
id |
Identifier of this chain. |
output.dir |
Subdirectory (relative to |
traces |
This is a placeholder for keeping whole parameter traces in the memory. If the processing operates in a low memory mode, it will be 0. It can be filled in using the function |
traces.burnin |
Burnin used to retrieve the traces, i.e. how many stored iterations are missing from the beginning in the |
rng.state |
State of the random number generator at the end of the last finished interation. |
meta |
Object of class |
Hana Sevcikova
run.e0.mcmc
, get.e0.mcmc
, bayesLife.mcmc.set
, bayesLife.mcmc.meta
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") # loads traces from one chain m <- get.e0.mcmc(sim.dir, low.memory = FALSE, burnin = 40, chain.ids = 1) # should have 20 rows, since 60 iterations in total minus 40 burnin dim(e0.mcmc(m, 1)$traces) summary(m)
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") # loads traces from one chain m <- get.e0.mcmc(sim.dir, low.memory = FALSE, burnin = 40, chain.ids = 1) # should have 20 rows, since 60 iterations in total minus 40 burnin dim(e0.mcmc(m, 1)$traces) summary(m)
Simulation meta object bayesLife.mcmc.meta
used by all chains of the same MCMC simulation.
It contains information that is common to all chains. It is a part of a bayesLife.mcmc.set
object.
The object is in standard cases not to be manipulated by itself, but rather as part of a bayesLife.mcmc.set
object.
A bayesLife.mcmc.meta
object stores values of the various input arguments
of the run.e0.mcmc
function. These are sex
, nr.chains
,
start.year
, present.year
, wpp.year
, my.e0.file
, compression.type
.
Furthermore, it contains components:
e0.matrix.all |
A |
e0.matrix |
Like |
d.ct |
A difference e0 matrix of size |
loessSD |
Matrix of the same dimension as |
nr.countries |
Number of countries included in the e0 matrices. |
nr.countries.estimation |
Number of countries included in the MCMC estimation. It must be smaller or equal to |
Tc.index |
A list with one element per country. For each country, it contains the index within |
regions |
List of arrays of length |
regionsDT |
Like |
output.dir |
Directory for storing simulation output. |
mcmc.options |
List of various options used in the estimation. See |
country.bounds |
List of country-specific bounds of the various parameter priors, as constructed from the |
suppl.data |
If supplemental data were used in the simulation (i.e. start year was set prior to 1950), this is a list containing information about the additional data. It has the following components of the same form as described above, but related only to the additional data: |
Hana Sevcikova
run.e0.mcmc
, get.e0.mcmc
, e0mcmc.options
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") m <- get.e0.mcmc(sim.dir) summary(m, meta.only = TRUE) names(m$meta)
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") m <- get.e0.mcmc(sim.dir) summary(m, meta.only = TRUE) names(m$meta)
Functions for computing residuals from the observed life expectancy and MCMC estimation, and fitting a local polynomial regression.
compute.residuals(sim.dir, burnin = 1000) compute.loess(sim.dir = NULL, burnin = 1000, residuals = NULL)
compute.residuals(sim.dir, burnin = 1000) compute.loess(sim.dir = NULL, burnin = 1000, residuals = NULL)
sim.dir |
Directory with the MCMC estimation. In |
burnin |
Number of (unthinned) iterations to be discarded. In |
residuals |
Residuals can be computed outside of the |
The Bayesian hierarchical model for life expectancy uses a lowess curve as a multiplier of the variance. The dataset is stored in the package as the loess_sd
dataset. These functions can be used to re-compute this loess_sd
dataset. In such a case, the simulation should be run with the argument constant.variance = TRUE
(in run.e0.mcmc
).
The residuals are computed for each country as the absolute differences between the observed life expectancy increases and the mean of the estimated double logistic function at the corresponding life expectancy level.
compute.residuals
returns a data frame with columns ‘x’ (life expectancy levels) and ‘y’ (absolute residuals).
compute.loess
also returns a data frame with columns ‘x’ and ‘y’, where ‘x’ is the same as before (with added a minimum and maximum) and ‘y’ is the local polynomial fit with constant tails.
Hana Sevcikova
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") resid <- compute.residuals(sim.dir, burnin = 30) lws <- compute.loess(residuals = resid) # plot residuals and loess plot(resid$x, resid$y, ylim = c(0, 4)) lines(lws$x, lws$y, col = "red")
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") resid <- compute.residuals(sim.dir, burnin = 30) lws <- compute.loess(residuals = resid) # plot residuals and loess plot(resid$x, resid$y, ylim = c(0, 4)) lines(lws$x, lws$y, col = "red")
Converts trajectories of the life expectancy stored in a binary format into two CSV files of a UN-specific format.
convert.e0.trajectories(dir = file.path(getwd(), "bayesLife.output"), n = 1000, output.dir = NULL, verbose = FALSE)
convert.e0.trajectories(dir = file.path(getwd(), "bayesLife.output"), n = 1000, output.dir = NULL, verbose = FALSE)
dir |
Directory containing the prediction object. It should correspond to the |
n |
Number of trajectories to be stored. It can be either a single number or the word “all” in which case all available trajectories are converted. |
output.dir |
Directory in which the resulting files will be stored. If |
verbose |
Logical switching log messages on and off. |
The function creates two files per sex. One is called “ascii_trajectories.csv”, it is a comma-separated table with the following columns:
“LocID”: country code
“Period”: prediction interval, e.g. 2015-2020
“Year”: middle year of the prediction interval
“Trajectory”: identifier of the trajectory
“e0”: life expectancy
The second file is called “ascii_trajectories_wide.csv”, it is also a comma-separated table and it contains the same information as above but in a ‘transposed’ format. I.e. the data for one country are ordered in columns, thus, there is one column per country. The country columns are ordered alphabetically.
If n
is smaller than the total number of trajectories, the trajectories are selected using equal spacing.
This function is automatically called from the e0.predict
function, therefore in standard cases it will not be needed to call it directly. However, it can be useful for example, if different number of trajectories are to be converted, without having to re-run the prediction.
Hana Sevcikova
write.e0.projection.summary
, e0.predict
## Not run: sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") pred.dir <- file.path(getwd(), "exampleLEpred") # stores 10 trajectories out of 30 (60-30) into # exampleLEpred/predictions/ascii_trajectories.csv (for female) # and exampleLEpred/predictions/joint_male/ascii_trajectories.csv (for male) e0.predict(sim.dir = sim.dir, output.dir = pred.dir, burnin = 30, save.as.ascii = 10, verbose = TRUE) # stores all 30 trajectories into the current directory convert.e0.trajectories(dir = pred.dir, n = "all", output.dir = ".", verbose = TRUE) # Note: If the output.dir argument in e0.predict is omitted, # call convert.e0.trajectories with dir = sim.dir ## End(Not run)
## Not run: sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") pred.dir <- file.path(getwd(), "exampleLEpred") # stores 10 trajectories out of 30 (60-30) into # exampleLEpred/predictions/ascii_trajectories.csv (for female) # and exampleLEpred/predictions/joint_male/ascii_trajectories.csv (for male) e0.predict(sim.dir = sim.dir, output.dir = pred.dir, burnin = 30, save.as.ascii = 10, verbose = TRUE) # stores all 30 trajectories into the current directory convert.e0.trajectories(dir = pred.dir, n = "all", output.dir = ".", verbose = TRUE) # Note: If the output.dir argument in e0.predict is omitted, # call convert.e0.trajectories with dir = sim.dir ## End(Not run)
The functions convert MCMC traces (simulated using run.e0.mcmc
) into objects that can be used with the coda package.
e0.coda.list.mcmc(mcmc.list = NULL, country = NULL, chain.ids = NULL, sim.dir = file.path(getwd(), "bayesLife.output"), par.names = NULL, par.names.cs = NULL, low.memory = FALSE, ...) ## S3 method for class 'bayesLife.mcmc' coda.mcmc(mcmc, country = NULL, par.names = NULL, par.names.cs = NULL, ...)
e0.coda.list.mcmc(mcmc.list = NULL, country = NULL, chain.ids = NULL, sim.dir = file.path(getwd(), "bayesLife.output"), par.names = NULL, par.names.cs = NULL, low.memory = FALSE, ...) ## S3 method for class 'bayesLife.mcmc' coda.mcmc(mcmc, country = NULL, par.names = NULL, par.names.cs = NULL, ...)
mcmc.list |
List of |
mcmc |
Object of class |
country |
Country name or code. It is used in connection with the |
chain.ids |
Vector of chain identifiers. By default, all chains available in the |
sim.dir |
Directory with the MCMC simulation results. Only used if |
par.names |
Names of country-independent parameters to be included. |
par.names.cs |
Names of country-specific parameters to be included. The argument |
low.memory |
Logical indicating if the function should run in a memory-efficient mode. |
... |
Additional arguments passed to the coda's |
The function e0.coda.list.mcmc
returns an object of class “mcmc.list”. The function coda.mcmc
returns an object of class “mcmc”, both defined in the coda package.
Hana Sevcikova
e0.partraces.plot
for plotting the MCMC traces and summary.bayesLife.mcmc.set
.
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") coda.list <- e0.coda.list.mcmc(sim.dir = sim.dir, country = "France", burnin = 30) summary(coda.list)
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") coda.list <- e0.coda.list.mcmc(sim.dir = sim.dir, country = "France", burnin = 30) summary(coda.list)
Function e0.diagnose
runs convergence diagnostics of existing MCMCs, using the raftery.diag
function from the coda package.
e0.diagnose(sim.dir, thin = 225, burnin = 10000, express = FALSE, country.sampling.prop = NULL, keep.thin.mcmc = FALSE, verbose = TRUE)
e0.diagnose(sim.dir, thin = 225, burnin = 10000, express = FALSE, country.sampling.prop = NULL, keep.thin.mcmc = FALSE, verbose = TRUE)
sim.dir |
Directory with the MCMC simulation results. |
thin |
Thinning interval. |
burnin |
Number of iterations to be discarded from the beginning of the parameter traces. |
express |
Logical. If |
country.sampling.prop |
Proportion of countries that are included in the diagnostics. If it is |
keep.thin.mcmc |
Logical. If |
verbose |
Logical switching log messages on and off. |
The function invokes the e0.raftery.diag
function separately for country-independent parameters and for country-specific parameters. It results in two possible states: red, i.e. it did not converge, and green, i.e. it converged.
The resulting object is stored in
‘{sim.dir}/diagnostics/bayesLife.convergence_{thin}_{burnin}.rda’ and can be accessed using the function get.e0.convergence
.
Function has.mcmc.converged
from the bayesTFR package can be used to check if the existing diagnostics converged.
e0.diagnose
returns an object of class bayesLife.convergence
with components:
result |
Table containing all not-converged parameters. Its columns include ‘Total iterations needed’ and ‘Remaining iterations’. |
lresult.country.independent |
Number of rows in |
country.independent |
Result of |
country.specific |
Result of |
iter.needed |
Number of additional iterations suggested in order to achieve convergence. |
iter.total |
Total number of iterations of the original unthinned set of chains. |
use.nr.traj |
Suggestion for number of trajectories in generating predictions. |
burnin |
Burnin used. |
thin |
Thinning interval used. |
status |
Vector of character strings containing the result status. Possible values: ‘green’, ‘red’. |
mcmc.set |
Object of class |
thin.mcmc |
If |
express |
Value of the input argument |
nr.countries |
Vector with elements |
Hana Sevcikova, Adrian Raftery
e0.raftery.diag
, raftery.diag
, summary.bayesLife.convergence
, get.e0.convergence
, create.thinned.e0.mcmc
The function computes coverage, i.e. the ratio of observed data fitted within the given probability intervals of the predictive posterior distribution of the double logistic function, as well as the root mean square error of the simulation.
e0.dl.coverage(sim.dir, pi = c(80, 90, 95), burnin = 10000, verbose = TRUE)
e0.dl.coverage(sim.dir, pi = c(80, 90, 95), burnin = 10000, verbose = TRUE)
sim.dir |
Directory with the MCMC simulation results. If a prediction and its corresponding thinned mcmcs are available in the simulation directory, those are taken for assessing the goodness of fit. |
pi |
Probability interval. It can be a single number or an array. |
burnin |
Burnin. Only relevant if |
verbose |
Logical switching log messages on and off. |
List with the same components as tfr.dl.coverage
.
To see the fit visually per country, use e0.DLcurve.plot(..., predictive.distr=TRUE,...)
.
Hana Sevcikova
## Not run: sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") e0 <- get.e0.mcmc(sim.dir) # Note that this simulation is a toy example and thus has not converged. gof <- e0.dl.coverage(sim.dir) gof$country.coverage e0.DLcurve.plot(e0, country=608, predictive.distr=TRUE, pi=c(80, 90, 95)) ## End(Not run)
## Not run: sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") e0 <- get.e0.mcmc(sim.dir) # Note that this simulation is a toy example and thus has not converged. gof <- e0.dl.coverage(sim.dir) gof$country.coverage e0.DLcurve.plot(e0, country=608, predictive.distr=TRUE, pi=c(80, 90, 95)) ## End(Not run)
The functions plot the posterior distribution of the double logistic function used in the simulation, including their median and given probability intervals.
e0.DLcurve.plot(mcmc.list, country, burnin = NULL, pi = 80, e0.lim = NULL, nr.curves = 20, predictive.distr = FALSE, ylim = NULL, xlab = "e(0)", ylab = "5-year gains", main = NULL, show.legend = TRUE, col = c('black', 'red', "#00000020"), ...) e0.DLcurve.plot.all(mcmc.list = NULL, sim.dir = NULL, output.dir = file.path(getwd(), "DLcurves"), output.type = "png", burnin = NULL, verbose = FALSE, ...) e0.parDL.plot(mcmc.set, country = NULL, burnin = NULL, lty = 2, ann = TRUE, ...) e0.world.dlcurves(x, mcmc.list, burnin = NULL, ...) e0.country.dlcurves(x, mcmc.list, country, burnin = NULL, ...)
e0.DLcurve.plot(mcmc.list, country, burnin = NULL, pi = 80, e0.lim = NULL, nr.curves = 20, predictive.distr = FALSE, ylim = NULL, xlab = "e(0)", ylab = "5-year gains", main = NULL, show.legend = TRUE, col = c('black', 'red', "#00000020"), ...) e0.DLcurve.plot.all(mcmc.list = NULL, sim.dir = NULL, output.dir = file.path(getwd(), "DLcurves"), output.type = "png", burnin = NULL, verbose = FALSE, ...) e0.parDL.plot(mcmc.set, country = NULL, burnin = NULL, lty = 2, ann = TRUE, ...) e0.world.dlcurves(x, mcmc.list, burnin = NULL, ...) e0.country.dlcurves(x, mcmc.list, country, burnin = NULL, ...)
mcmc.list |
List of |
mcmc.set |
Object of class |
country |
Name or numerical code of a country. It can also be given as ISO-2 or ISO-3 characters. |
burnin |
Number of iterations to be discarded from the beginning of parameter traces. |
pi |
Probability interval. It can be a single number or an array. |
e0.lim |
It can be a tuple of the minimum and maximum life expectancy to be shown in the plot. If |
nr.curves |
Number of curves to be plotted. If |
predictive.distr |
Logical. If |
ylim , xlab , ylab , main , lty
|
Graphical parameters passed to the |
show.legend |
Logical determining if the legend should be shown. |
col |
Vector of colors in this order: 1. observed data points, 2. quantiles, 3. trajectories |
... |
Additional graphical parameters. In addition, any arguments from |
sim.dir |
Directory with the simulation results. Only relevant, if |
output.dir |
Directory into which resulting graphs are stored. |
output.type |
Type of the resulting files. It can be “png”, “pdf”, “jpeg”, “bmp”, “tiff”, or “postscript”. |
verbose |
Logical switching log messages on and off. |
x |
e0 values for which the double logistic should be computed. |
ann |
Logical if parameters should be annotated. |
e0.DLcurve.plot
plots double logistic curves for the given country. e0.DLcurve.plot.all
creates such plots for all countries and stores them in output.dir
. Parameters passed to the double logistic function are either thinned traces created by the e0.predict
function (if mcmc.list
is an object of class bayesLife.prediction
), or they are selected by equal spacing from the MCMC traces. In the former case, burnin
is set automatically; in the latter case, burnin
defaults to 0 since such object has already been “burned”. If nr.curves
is smaller than 2000, the median and probability intervals are computed on a sample of 2000 equally spaced data points, otherwise on all plotted curves.
Function e0.parDL.plot
draws the means of the DL parameters as vertical and horizontal lines. The lines are added to the current graphical device and annotated if ann
is TRUE
. If country is NULL
, the mean of world parameters are drawn.
Function e0.world.dlcurves
returns the DL curves of the hierarchical distribution. Function e0.country.dlcurves
returns DL curves for a given country. If mcmc.list
is a prediction object, burnin
should not be given, as such object has already been “burned”.
e0.world.dlcurves
and e0.country.dlcurves
return a matrix of size where
is the number of trajectories and
is the number of values of
.
Hana Sevcikova
## Not run: sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") mcmc.set <- get.e0.mcmc(sim.dir = sim.dir) e0.DLcurve.plot(mcmc.set, country = "Japan", burnin = 40) e0.parDL.plot(mcmc.set, "Japan") # add the median of the hierarchical DL curves x <- seq(40, 90, length = 100) world <- e0.world.dlcurves(x, mcmc.set, burnin = 40) qw <- apply(world, 2, median) lines(x, qw, col = 'blue') ## End(Not run)
## Not run: sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") mcmc.set <- get.e0.mcmc(sim.dir = sim.dir) e0.DLcurve.plot(mcmc.set, country = "Japan", burnin = 40) e0.parDL.plot(mcmc.set, "Japan") # add the median of the hierarchical DL curves x <- seq(40, 90, length = 100) world <- e0.world.dlcurves(x, mcmc.set, burnin = 40) qw <- apply(world, 2, median) lines(x, qw, col = 'blue') ## End(Not run)
The functions plot the posterior distribution of the gaps between female and male life expectancy, modeled and predicted using a model described in Lalic (2011) and Raftery, Lalic & Gerland (2014). This can be done for one country (e0.gap.plot
) or for all countries (e0.gap.plot.all
).
e0.gap.plot(e0.pred, country, e0.pred2 = NULL, pi = c(80, 95), nr.traj = 0, xlim = NULL, ylim = NULL, type = "b", xlab = "Year", ylab = "Gap in life expectancy", main = NULL, show.legend = TRUE, ...) e0.gap.plot.all(e0.pred, output.dir = file.path(getwd(), "e0gaps"), output.type = "png", verbose = FALSE, ...)
e0.gap.plot(e0.pred, country, e0.pred2 = NULL, pi = c(80, 95), nr.traj = 0, xlim = NULL, ylim = NULL, type = "b", xlab = "Year", ylab = "Gap in life expectancy", main = NULL, show.legend = TRUE, ...) e0.gap.plot.all(e0.pred, output.dir = file.path(getwd(), "e0gaps"), output.type = "png", verbose = FALSE, ...)
e0.pred |
Object of class |
country |
Name or numerical code of a country. It can also be given as ISO-2 or ISO-3 characters. |
e0.pred2 |
Object of class |
pi |
Probability interval. It can be a single number or an array. |
nr.traj |
Number of trajectories to be plotted. |
xlim , ylim , type , xlab , ylab , main
|
Graphical parameters passed to the |
show.legend |
Logical controlling whether the legend should be drawn. |
output.dir |
Directory into which resulting graphs are stored. |
output.type |
Type of the resulting files. It can be “png”, “pdf”, “jpeg”, “bmp”, “tiff”, or “postscript”. |
verbose |
Logical switching log messages on and off. |
... |
Additional graphical parameters. In addition, for |
Hana Sevcikova
Lalic, N. (2011). Master's thesis at the Department of Statistics, University of Washington.
A. E. Raftery, N. Lalic, P. Gerland (2014). Joint Probabilistic Projection of Female and Male Life Expectancy. Demographic Research, 30:795-822.
e0.joint.plot
, e0.jmale.estimate
, e0.jmale.predict
, get.e0.jmale.prediction
# See example for e0.jmale.predict
# See example for e0.jmale.predict
The function estimates the joint female-male model of life expectancy, as described in Raftery et al. (2014, 2012) and Lalic (2011). It consist of two equations with t-distributed errors, see Details below.
e0.jmale.estimate(mcmc.set, countries.index = NULL, estDof.eq1 = TRUE, start.eq1 = list(dof = 2), max.e0.eq1 = 83, estDof.eq2 = TRUE, start.eq2 = list(dof = 2), constant.gap.eq2 = TRUE, include.suppl.gap = FALSE, my.e0.file = NULL, my.locations.file = NULL, verbose = FALSE)
e0.jmale.estimate(mcmc.set, countries.index = NULL, estDof.eq1 = TRUE, start.eq1 = list(dof = 2), max.e0.eq1 = 83, estDof.eq2 = TRUE, start.eq2 = list(dof = 2), constant.gap.eq2 = TRUE, include.suppl.gap = FALSE, my.e0.file = NULL, my.locations.file = NULL, verbose = FALSE)
mcmc.set |
Object of class |
countries.index |
Index of countries (within the mcmc.set object) to be included in the estimation. By default, all countries included in the estimation of |
estDof.eq1 , estDof.eq2
|
Logical, controlling whether the degrees of freedom of the first and second equation, respectively, should be estimated. If it is |
start.eq1 , start.eq2
|
Argument |
max.e0.eq1 |
Maximum female life expectancy of records included in the estimation of the first equation (parameter |
constant.gap.eq2 |
Logical. If |
include.suppl.gap |
If |
my.e0.file |
File name containing user-specified male time series for one or more countries. The function replaces the corresponding country data from the WPP dataset by values in this file. Only columns are replaced that match column names of the WPP dataset. |
my.locations.file |
File name containing user-specified locations if different from the default |
verbose |
Logical switching log messages on and off. If |
The joint female-male life expectancy model is a model for estimating gaps between female and male life expectancy. It consists of two parts, see Equation (1) in Raftery et al. (2012):
1. If , then
where is iid
.
2. If , then
where is iid
.
Here, is the time and
is the country index.
is the gap for country
at time
and
is the female life expectancy for country
at time
.
can be set in the
max.e0.eq1
argument.
Using the tlm
function of the hett package, the function estimates the coefficients (
) and
, as well as paramteres
(
) and optionally the degrees of freedom
(
). If
constant.gap.eq2
is TRUE
, is set to 1 and
is iid
.
The mcmc.set
object should be a bayesLife.mcmc.set
object obtained from a simulation of a female life expectancy. Note that since only the observed data and no MCMC results are used in this estimation, the mcmc.set
object can be obtained from a toy simulation such as in the example below. The function extracts observed data from this object and treats them as . For the male historical time series, the function takes the male WPP dataset (
e0M
) from the same wpp package as the female data, and possibly partly replaces the male dataset by any user-specified data given in my.e0.file
.
List with the components, eq1
and eq2
, each containing estimation results from the first and second equation, respectively. These are:
coefficients |
Estimated coefficients |
sigma |
Parameter |
dof |
Degrees of freedom |
Hana Sevcikova
A. E. Raftery, N. Lalic, P. Gerland (2014). Joint Probabilistic Projection of Female and Male Life Expectancy. Demographic Research, 30:795-822.
A. E. Raftery, N. Li, H. Sevcikova , P. Gerland, G. K. Heilig (2012). Bayesian probabilistic population projections for all countries. Proceedings of the National Academy of Sciences 109:13915-13921.
Lalic, N. (2011). Master's thesis at the Department of Statistics, University of Washington.
## Not run: sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") m <- get.e0.mcmc(sim.dir) fit <- e0.jmale.estimate(m, verbose = TRUE) ## End(Not run)
## Not run: sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") m <- get.e0.mcmc(sim.dir) fit <- e0.jmale.estimate(m, verbose = TRUE) ## End(Not run)
Prediction of the joint female-male model of life expectancy, as described in Raftery et al. (2014, 2012) and Lalic (2011).
e0.jmale.predict(e0.pred, estimates = NULL, gap.lim = c(0, 18), max.e0.eq1.pred = 86, my.e0.file = NULL, my.locations.file = NULL, save.as.ascii = 1000, resample.outrange = TRUE, verbose = TRUE, ...)
e0.jmale.predict(e0.pred, estimates = NULL, gap.lim = c(0, 18), max.e0.eq1.pred = 86, my.e0.file = NULL, my.locations.file = NULL, save.as.ascii = 1000, resample.outrange = TRUE, verbose = TRUE, ...)
e0.pred |
Object of class |
estimates |
List of the same structure as returned by |
gap.lim |
Vector of length two giving the minimum and maximum bounds for the female-male life expectancy gaps. See argument |
max.e0.eq1.pred |
Maximum female life expectancy for male projections handled by the first equation (parameter |
my.e0.file |
File name containing user-specified male time series for one or more countries. The function replaces the corresponding country data from the WPP dataset by values in this file. Only columns are replaced that match column names of the WPP dataset. |
my.locations.file |
File name containing user-specified locations if different from the default |
save.as.ascii |
Either a number determining how many trajectories should be converted into an ASCII file, or “all” in which case all trajectories are converted. It should be set to 0, if no conversion is desired. |
resample.outrange |
Logical indicating if values outside of the allowed gap range given by |
verbose |
Logical switching log messages on and off. |
... |
Further arguments passed to |
If no estimates
are given, the function invokes an estimation by calling e0.jmale.estimate
. Using those estimates, the male life expectancy is projected forward in time (as a function of a female-male gap), using the female predictions from e0.pred
. The initial male data point is extracted from the male WPP dataset (e0M
) and possibly partly replaced by any user-specified data given in my.e0.file
.
The resulting trajectory files are stored in a subdirectory of the female prediction directory, called ‘joint_male’. Furthermore, an object of class bayesLife.prediction
is created and added to e0.pred
as a component called joint.male
.
The predicted gaps can be viewed using the e0.gap.plot
function.
Updated e0.pred
object where a new component was added, called joint.male
. It is also an object of class bayesLife.prediction
and it contains results from this prediction.
Hana Sevcikova
A. E. Raftery, N. Lalic, P. Gerland (2014). Joint Probabilistic Projection of Female and Male Life Expectancy. Demographic Research, 30:795-822.
A. E. Raftery, N. Li, H. Sevcikova , P. Gerland, G. K. Heilig (2012). Bayesian probabilistic population projections for all countries. Proceedings of the National Academy of Sciences 109:13915-13921.
Lalic, N. (2011). Master's thesis at the Department of Statistics, University of Washington.
e0.jmale.estimate
, get.e0.jmale.prediction
, e0.predict
, e0.gap.plot
## Not run: sim.dir <- tempfile() m <- run.e0.mcmc(sex = 'F', nr.chains = 1, iter = 30, thin = 1, output.dir = sim.dir) pred <- e0.predict(m, burnin = 15, verbose = FALSE, save.as.ascii = 0, predict.jmale = FALSE) both.pred <- e0.jmale.predict(pred) e0.trajectories.plot(both.pred, 'Guatemala') # Female e0.trajectories.plot(get.e0.jmale.prediction(both.pred), 'Guatemala') # Male # Marginal distribution of the sex-specific projections e0.trajectories.plot(both.pred, 'GTM', both.sexes = TRUE, pi = 80) # Plotting the gaps e0.gap.plot(both.pred, 'GTM') # Joint distribution of the sex-specific projections e0.joint.plot(both.pred, 'Guatemala', pi = 80, years = c(2013, 2043, 2093)) unlink(sim.dir, recursive = TRUE) ## End(Not run)
## Not run: sim.dir <- tempfile() m <- run.e0.mcmc(sex = 'F', nr.chains = 1, iter = 30, thin = 1, output.dir = sim.dir) pred <- e0.predict(m, burnin = 15, verbose = FALSE, save.as.ascii = 0, predict.jmale = FALSE) both.pred <- e0.jmale.predict(pred) e0.trajectories.plot(both.pred, 'Guatemala') # Female e0.trajectories.plot(get.e0.jmale.prediction(both.pred), 'Guatemala') # Male # Marginal distribution of the sex-specific projections e0.trajectories.plot(both.pred, 'GTM', both.sexes = TRUE, pi = 80) # Plotting the gaps e0.gap.plot(both.pred, 'GTM') # Joint distribution of the sex-specific projections e0.joint.plot(both.pred, 'Guatemala', pi = 80, years = c(2013, 2043, 2093)) unlink(sim.dir, recursive = TRUE) ## End(Not run)
The functions plot the joint posterior distribution of female and male life expectancy, modeled and predicted using the joint model described in Lalic (2011) and Raftery, Lalic & Gerland (2014). This can be done for one country (e0.joint.plot
) or for all countries (e0.joint.plot.all
).
e0.joint.plot(e0.pred, country, pi = 95, years, nr.points = 500, obs.pch = 17, obs.cex=1, xlim = NULL, ylim = NULL, xlab = "Female life expectancy", ylab = "Male life expectancy", main = NULL, col = NULL, show.legend = TRUE, add = FALSE, ...) e0.joint.plot.all(e0.pred, output.dir = file.path(getwd(), "e0joint"), output.type = "png", verbose = FALSE, ...)
e0.joint.plot(e0.pred, country, pi = 95, years, nr.points = 500, obs.pch = 17, obs.cex=1, xlim = NULL, ylim = NULL, xlab = "Female life expectancy", ylab = "Male life expectancy", main = NULL, col = NULL, show.legend = TRUE, add = FALSE, ...) e0.joint.plot.all(e0.pred, output.dir = file.path(getwd(), "e0joint"), output.type = "png", verbose = FALSE, ...)
e0.pred |
Object of class |
country |
Name or numerical code of a country. It can also be given as ISO-2 or ISO-3 characters. |
pi |
Probability interval. It can be a single number or an array. |
years |
Array of future years for which to plot the distribution. |
nr.points |
Number of points shown in the plot for each year. |
obs.pch , obs.cex
|
Graphical parameters used for displaying observed data or data without variation. |
xlim , ylim , xlab , ylab , main
|
Graphical parameters passed to the |
col |
Array of colors, one for each year. |
show.legend |
Logical controlling whether the legend should be drawn. |
add |
Logical controlling whether the distribution should be plotted into a new graphic device ( |
output.dir |
Directory into which resulting graphs are stored. |
output.type |
Type of the resulting files. It can be “png”, “pdf”, “jpeg”, “bmp”, “tiff”, or “postscript”. |
verbose |
Logical switching log messages on and off. |
... |
Additional graphical parameters passed to the |
Hana Sevcikova, Adrian Raftery
Lalic, N. (2011). Master's thesis at the Department of Statistics, University of Washington.
A. E. Raftery, N. Lalic, P. Gerland (2014). Joint Probabilistic Projection of Female and Male Life Expectancy. Demographic Research, 30:795-822.
e0.gap.plot
, e0.trajectories.plot
, e0.jmale.predict
# See example for e0.jmale.predict
# See example for e0.jmale.predict
Generates a world map of the life expectancy for given quantile and projection or estimation period, using different techniques: e0.map
and e0.map.all
use rworldmap, e0.ggmap
uses ggplot2, and e0.map.gvis
creates an interactive map via GoogleVis.
e0.map(pred, ...) e0.ggmap(pred, ...) e0.map.all(pred, output.dir, output.type = "png", e0.range = NULL, nr.cats = 50, same.scale = TRUE, quantile = 0.5, file.prefix = "e0wrldmap_", ...) get.e0.map.parameters(pred, e0.range = NULL, nr.cats = 50, same.scale = TRUE, quantile = 0.5, ...) e0.map.gvis(pred, ...)
e0.map(pred, ...) e0.ggmap(pred, ...) e0.map.all(pred, output.dir, output.type = "png", e0.range = NULL, nr.cats = 50, same.scale = TRUE, quantile = 0.5, file.prefix = "e0wrldmap_", ...) get.e0.map.parameters(pred, e0.range = NULL, nr.cats = 50, same.scale = TRUE, quantile = 0.5, ...) e0.map.gvis(pred, ...)
pred |
Object of class |
output.dir |
Directory into which resulting maps are stored. |
output.type |
Type of the resulting files. It can be “png”, “pdf”, “jpeg”, “bmp”, “tiff”, or “postscript”. |
e0.range |
Range of the life expectancy to be displayed. It is of the form |
nr.cats |
Number of color categories. |
same.scale |
Logical controlling if maps for all years of this prediction object should be on the same color scale. |
quantile |
Quantile for which the map should be generated. It must be equal to one of the values in |
file.prefix |
Prefix for file names. |
... |
In |
e0.map
creates a single map for the given time period and quantile. e0.map.all
generates a sequence of maps, namely one for each projection period. If the package fields is installed, a color bar legend at the botom of the map is created.
Function get.e0.map.parameters
can be used in combination with e0.map
. (Note that get.e0.map.parameters
is called from inside of e0.map.all
.) It sets breakpoints for the color scheme using quantiles of a fitted gamma distribution.
Function e0.ggmap
is similar to e0.map
, but used the ggplot2 package in combination with the geom_sf
function.
Function e0.map.gvis
creates an interactive map using the googleVis package and opens it in an internet browser. It also generates a table of the mapped values that can be sorted by columns interactively in the browser.
By default, both e0.map
, e0.ggmap
and e0.map.gvis
produce maps of life expectancy. Alternatively, the functions can be used to plot country-specific MCMC parameters into a world map. They are given by the argument par.name
. One can pass any value from e0.parameter.names.cs.extended()
.
get.e0.map.parameters
returns a list with elements:
pred |
The object of class |
quantile |
Value of the argument |
catMethod |
If the argument |
numCats |
Number of categories. |
coulourPalette |
Subset of the rainbow palette, starting from dark blue and ending at red. |
... |
Additional arguments passed to the function. |
Hana Sevcikova, Adrian Raftery
## Not run: sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") pred <- get.e0.prediction(sim.dir = sim.dir) # Using ggplot2 e0.ggmap(pred, same.scale = TRUE) e0.ggmap(pred, year = 2100, same.scale = TRUE) # Using rworldmap # Uses heat colors and seven categories by default e0.map(pred) # Uses more colors with more suitable categorization params <- get.e0.map.parameters(pred) do.call('e0.map', params) # Another projection year on the same scale do.call('e0.map', c(list(year = 2043), params)) # Interactive map (requires Flash) e0.map.gvis(pred, year = 2043) ## End(Not run)
## Not run: sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") pred <- get.e0.prediction(sim.dir = sim.dir) # Using ggplot2 e0.ggmap(pred, same.scale = TRUE) e0.ggmap(pred, year = 2100, same.scale = TRUE) # Using rworldmap # Uses heat colors and seven categories by default e0.map(pred) # Uses more colors with more suitable categorization params <- get.e0.map.parameters(pred) do.call('e0.map', params) # Another projection year on the same scale do.call('e0.map', c(list(year = 2043), params)) # Interactive map (requires Flash) e0.map.gvis(pred, year = 2043) ## End(Not run)
These functions are to be used by expert analysts. They allow to change the projection medians either to specific values, including the WPP values, or shift the medians by a given constant or a factor.
e0.median.set(sim.dir, country, values, years = NULL, joint.male = FALSE) e0.median.shift(sim.dir, country, reset = FALSE, shift = 0, from = NULL, to = NULL, joint.male = FALSE) e0.median.adjust.jmale(sim.dir, countries, factors = c(1.2, 1.1)) e0.median.reset(sim.dir, countries = NULL, joint.male = FALSE) e0.shift.prediction.to.wpp(sim.dir, joint.male = FALSE, ...)
e0.median.set(sim.dir, country, values, years = NULL, joint.male = FALSE) e0.median.shift(sim.dir, country, reset = FALSE, shift = 0, from = NULL, to = NULL, joint.male = FALSE) e0.median.adjust.jmale(sim.dir, countries, factors = c(1.2, 1.1)) e0.median.reset(sim.dir, countries = NULL, joint.male = FALSE) e0.shift.prediction.to.wpp(sim.dir, joint.male = FALSE, ...)
sim.dir |
Directory containing the prediction object. |
country |
Name or numerical code of a country. |
countries |
Vector of country names or codes. In the |
values |
Array of the new median values. |
years |
Numeric vector giving years which |
joint.male |
Logical. If |
reset |
Logical. If |
shift |
Constant by which the medians should be offset. It is not used if |
from |
Year from which the offset/reset should start. By default, it starts at the first prediction period. |
to |
Year until which the offset/reset should be done. By default, it is set to the last prediction period. |
factors |
It should be a vector where each element corresponds to one time period. The adjustment of male medians is done as |
... |
Additional arguments passed to the underlying adjustment function. It can be |
The function e0.median.set
can be used to set the medians of the given country to specific values. Function e0.median.shift
can be used to offset the medians by a specific constant, or to reset the medians to their original BHM values. Function e0.median.adjust.jmale
adjusts male medians using factors that can expand or shrink the female-male gap.
Functione0.shift.prediction.to.wpp
shifts the projected medians so that they correspond to the values found in the e0Fproj
(joint.male = FALSE
) or e0Mproj
(joint.male = TRUE
) datasets of the wpp package that either corresponds to the package used for the simulation itself or is given by the wpp.year
argument. If using wpp2022, the dataset name is automatically adjusted depending if it is an annual or a 5-year simulation.
Function e0.median.reset
resets medians of the given countries to the original values. By default it deletes adjustments for all countries.
In all cases, if a median is modified, the corresponding offset is stored in the prediction object (element median.shift
). All functions write the updated prediction object back to disk. All functions in the package that use trajectories and trajectory statistics use the median.shift
values to offset the results correspondingly, i.e. trajectories are shifted the same way as the medians.
All functions return an updated object of class bayesLife.prediction
.
Hana Sevcikova
Functions for accessing names of the MCMC parameters, either country-independent or country-specific.
e0.parameter.names(...) e0.parameter.names.cs(...) e0.parameter.names.extended(...) e0.parameter.names.cs.extended(country.code = NULL, ...)
e0.parameter.names(...) e0.parameter.names.cs(...) e0.parameter.names.extended(...) e0.parameter.names.cs.extended(country.code = NULL, ...)
country.code |
Country code. If it is given, the country-specific parameter names contain the suffix ‘_c |
... |
List of options containing elements |
e0.parameter.names
returns names of the world parameters.e0.parameter.names.cs
returns names of the country-specific parameters.e0.parameter.names.extended
returns names of all world parameters in their extended format. I.e. parameters ‘Triangle’ and ‘lambda’ have the suffix ‘_1’, ‘_2’, ‘_3’, and ‘_4’. e0.parameter.names.cs.extended
returns names of all country-specific parameters in their extended format. I.e. parameters ‘Triangle.c’ and ‘lambda.c’ are in their extended format with the suffix ‘_1’, ‘_2’ and ‘_3’.
Hana Sevcikova
e0.parameter.names() e0.parameter.names.extended() e0.parameter.names.cs() e0.parameter.names.cs.extended()
e0.parameter.names() e0.parameter.names.extended() e0.parameter.names.cs() e0.parameter.names.cs.extended()
Functions for plotting density of the posterior distribution of the MCMC parameters.
e0.pardensity.plot(mcmc.list = NULL, sim.dir = file.path(getwd(), "bayesLife.output"), chain.ids = NULL, par.names = NULL, burnin = NULL, dev.ncol = 5, low.memory = TRUE, ...) e0.pardensity.cs.plot(country, mcmc.list = NULL, sim.dir = file.path(getwd(), "bayesLife.output"), chain.ids = NULL, par.names = NULL, burnin = NULL, dev.ncol = 3, low.memory = TRUE, ...)
e0.pardensity.plot(mcmc.list = NULL, sim.dir = file.path(getwd(), "bayesLife.output"), chain.ids = NULL, par.names = NULL, burnin = NULL, dev.ncol = 5, low.memory = TRUE, ...) e0.pardensity.cs.plot(country, mcmc.list = NULL, sim.dir = file.path(getwd(), "bayesLife.output"), chain.ids = NULL, par.names = NULL, burnin = NULL, dev.ncol = 3, low.memory = TRUE, ...)
country |
Name or numerical code of a country. |
mcmc.list |
List of |
sim.dir |
Directory with the MCMC simulation results. It is only used if |
chain.ids |
List of MCMC identifiers to be plotted. If it is |
par.names |
Names of parameters for which density should be plotted. By default all country-independent parameters are plotted if used within |
burnin |
Number of iterations to be discarded from the beginning of each chain. |
dev.ncol |
Number of columns for the graphics device. If the number of parameters is smaller than |
low.memory |
Logical indicating if the processing should run in a memory-efficient mode. |
... |
Further arguments passed to the |
The functions plot the density of the posterior distribution either for country-independent parameters (e0.pardensity.plot
) or for country-specific parameters (e0.pardensity.cs.plot
), one graph per parameter. One can restrict it to specific chains by setting the chain.ids
argument and to specific parameters by setting the par.names
argument.
If mcmc.list
is an object of class bayesLife.prediction
, thinned traces are used instead of the full chains. In such a case, burnin
and chain.ids
cannot be modified - their value is set to the one used when the thinned traces were created, namely when running e0.predict
.
Hana Sevcikova
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") e0.pardensity.plot(sim.dir = sim.dir, burnin = 10) e0.pardensity.cs.plot(country = "Ireland", sim.dir = sim.dir, burnin = 10)
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") e0.pardensity.plot(sim.dir = sim.dir, burnin = 10) e0.pardensity.cs.plot(country = "Ireland", sim.dir = sim.dir, burnin = 10)
Functions for plotting the MCMC parameter traces.
e0.partraces.plot(mcmc.list = NULL, sim.dir = file.path(getwd(), "bayesLife.output"), chain.ids = NULL, par.names = NULL, nr.points = NULL, dev.ncol = 5, low.memory = TRUE, ...) e0.partraces.cs.plot(country, mcmc.list = NULL, sim.dir = file.path(getwd(), "bayesLife.output"), chain.ids = NULL, par.names = NULL, nr.points = NULL, dev.ncol = 3, low.memory = TRUE, ...)
e0.partraces.plot(mcmc.list = NULL, sim.dir = file.path(getwd(), "bayesLife.output"), chain.ids = NULL, par.names = NULL, nr.points = NULL, dev.ncol = 5, low.memory = TRUE, ...) e0.partraces.cs.plot(country, mcmc.list = NULL, sim.dir = file.path(getwd(), "bayesLife.output"), chain.ids = NULL, par.names = NULL, nr.points = NULL, dev.ncol = 3, low.memory = TRUE, ...)
country |
Name or numerical code of a country. It can also be given as ISO-2 or ISO-3 characters. |
mcmc.list |
List of |
sim.dir |
Directory with the MCMC simulation results. It is only used if |
chain.ids |
List of MCMC identifiers to be plotted. If it is |
par.names |
Names of parameters for which traces should be plotted. By default all country-independent parameters are plotted if used within |
nr.points |
Number of points to be plotted. If |
dev.ncol |
Number of column for the graphics device. If the number of parameters is smaller than |
low.memory |
Logical indicating if the processing should run in a low-memory mode. If it is |
... |
Additional graphical arguments. It can also contain the arguments |
The functions plot MCMC traces either for country-independent parameters (e0.partraces.plot
) or for country-specific parameters (e0.partraces.cs.plot
), one graph per parameter. One can restrict it to specific chains by setting the chain.ids
argument, and to specific parameters by setting the par.names
argument.
Hana Sevcikova
e0.coda.list.mcmc
and get.e0.parameter.traces
for retrieving the raw values of traces.
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") e0.partraces.plot(sim.dir = sim.dir) e0.partraces.cs.plot(country = "IRL", sim.dir = sim.dir)
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") e0.partraces.plot(sim.dir = sim.dir) e0.partraces.cs.plot(country = "IRL", sim.dir = sim.dir)
Using the posterior parameter samples simulated by run.e0.mcmc
the function generates posterior trajectories for the life expectancy for all countries of the world.
e0.predict(mcmc.set = NULL, end.year = 2100, sim.dir = file.path(getwd(), "bayesLife.output"), replace.output = FALSE, predict.jmale = TRUE, nr.traj = NULL, thin = NULL, burnin = 10000, use.diagnostics = FALSE, save.as.ascii = 0, start.year = NULL, output.dir = NULL, low.memory = TRUE, ignore.last.observed = FALSE, seed = NULL, verbose = TRUE, ...)
e0.predict(mcmc.set = NULL, end.year = 2100, sim.dir = file.path(getwd(), "bayesLife.output"), replace.output = FALSE, predict.jmale = TRUE, nr.traj = NULL, thin = NULL, burnin = 10000, use.diagnostics = FALSE, save.as.ascii = 0, start.year = NULL, output.dir = NULL, low.memory = TRUE, ignore.last.observed = FALSE, seed = NULL, verbose = TRUE, ...)
mcmc.set |
Object of class |
end.year |
End year of the prediction. |
sim.dir |
Directory with the MCMC simulation results. It should equal to the |
replace.output |
Logical. If |
predict.jmale |
Logical controlling if a joint female-male prediciton should be performed. This is done only if the underlying mcmcs in |
nr.traj |
Number of trajectories to be generated. If |
thin |
Thinning interval used for determining the number of trajectories. Only relevant, if |
burnin |
Number of iterations to be discarded from the beginning of the parameter traces. |
use.diagnostics |
Logical determining if an existing convergence diagnostics should be used for choosing the values of |
save.as.ascii |
Either a number determining how many trajectories should be converted into an ASCII file, or “all” in which case all trajectories are converted. It should be set to 0, if no conversion is desired (default). |
start.year |
This argument should be only used if the start year of the prediction is before or at the present year of the MCMC run (see Details below). By default the prediction starts in the next time period after the present year (passed to |
output.dir |
Directory into which the resulting prediction object and the trajectories are stored. If it is |
low.memory |
Logical indicating if the prediction should run in a low-memory mode. If it is |
ignore.last.observed |
Logical. By default, the prediction (or imputation) for each country starts one time period after the last observed data point for that country defined by the “last.observed” column in the data. If this argument is set to |
seed |
Seed of the random number generator. If |
verbose |
Logical switching log messages on and off. |
... |
Additional arguments passed to the |
The trajectories are generated using the double logistic function (Chunn et al. 2013). Parameter samples simulated via run.e0.mcmc
are used from all chains, from which the given burnin was discarded. They are evenly thinned to match nr.traj
or using the thin
argument. Such thinned parameter traces, collapsed into one chain, if they do not already exist, are stored on disk into the sub-directory ‘thinned_mcmc_t_b’ where t is the value of thin
and b the value of burnin
(see create.thinned.e0.mcmc
).
The projection is run for all missing values before the present year, if any. Medians over the trajectories are used as imputed values and the trajectories are discarded. The process then continues by projecting the future values where all generated trajectories are kept.
A special case is when the argument start.year
is given that is smaller or equal the present year. In such a case, imputed missing values before present year are treated as ordinary predictions (trajectories are kept). All historical data between start year and present year are used as projections.
The resulting prediction object is saved into ‘{output.dir}/predictions’. Trajectories for all countries are saved into the same directory in a binary format, one file per country. At the end of the projection, if save.as.ascii
is larger than 0, the function converts the given number of trajectories into a CSV file of a UN-specific format. They are selected by equal spacing (see function convert.e0.trajectories
for more details on the conversion). In addition, two summary files are created: one in a user-friendly format, the other using a UN-specific coding of the variants and time (see write.e0.projection.summary
for more details).
Object of class bayesLife.prediction
which is a list containing components:
quantiles |
A |
traj.mean.sd |
A |
nr.traj |
Number of trajectories. |
e0.matrix.reconstructed |
Matrix containing imputed e0 values on spots where the original e0 matrix has missing values, i.e. between the last observed data point and the present year. |
output.directory |
Directory where trajectories corresponding to this prediction are stored. |
nr.projections |
Number of projections. |
burnin |
Burnin used for this prediction. |
end.year |
The end year of this prediction. |
start.year |
The |
mcmc.set |
Object of class |
joint.male |
If |
Hana Sevcikova, using code from Jennifer Chunn
J. L. Chunn, A. E. Raftery, P. Gerland, H. Sevcikova (2013): Bayesian Probabilistic Projections of Life Expectancy for All Countries. Demography 50(3):777-801. <doi:10.1007/s13524-012-0193-x>
run.e0.mcmc
, e0.jmale.predict
, create.thinned.e0.mcmc
, convert.e0.trajectories
,
get.e0.prediction
, summary.bayesLife.prediction
## Not run: m <- run.e0.mcmc(nr.chains = 1, iter = 50, thin = 1, verbose = TRUE) pred <- e0.predict(m, burnin = 25, verbose = TRUE) summary(pred, country = "Portugal") # names and codes of countries included head(get.countries.table(pred, iso = TRUE)) ## End(Not run)
## Not run: m <- run.e0.mcmc(nr.chains = 1, iter = 50, thin = 1, verbose = TRUE) pred <- e0.predict(m, burnin = 25, verbose = TRUE) summary(pred, country = "Portugal") # names and codes of countries included head(get.countries.table(pred, iso = TRUE)) ## End(Not run)
Using the posterior parameter samples the function generates posterior trajectories of the life expectancy for given countries or regions. It is intended to be used after running run.e0.mcmc.extra
, but it can be also used for purposes of testing specific settings on one or a few countries.
e0.predict.extra(sim.dir = file.path(getwd(), 'bayesLife.output'), prediction.dir = sim.dir, countries = NULL, save.as.ascii = 1000, verbose = TRUE, ...)
e0.predict.extra(sim.dir = file.path(getwd(), 'bayesLife.output'), prediction.dir = sim.dir, countries = NULL, save.as.ascii = 1000, verbose = TRUE, ...)
sim.dir |
Directory with the MCMC simulation results. |
prediction.dir |
Directory where the prediction object and the trajectories are stored. |
countries |
Vector of country codes for which the prediction should be made. If it is |
save.as.ascii |
Either a number determining how many trajectories should be converted into an ascii file, or “all” in which case all trajectories are converted. It should be set to 0, if no converions is desired. Note that the convertion is done on all countries. |
verbose |
Logical switching log messages on and off. |
... |
Additional arguments passed to a joint female-male prediction. |
In order to use this function, a prediction object must exist, i.e. the function e0.predict
must have been processed prior to using this function.
Trajectories for given countries or regions are generated and stored in binary format along with other countries (in prediction.dir
). The existing prediction object is updated and stored in the same directory. If save.as.ascii
is larger than zero, trajectories of ALL countries are converted to an ascii format.
If the prediction object contains joint male projections, these are also created for the given countries.
Updated object of class bayesLife.prediction
.
Hana Sevcikova
Generates posterior trajectories of the life expectancy at birth (e0) for subregions of given countries, for female and male.
e0.predict.subnat(countries, my.e0.file, sim.dir = file.path(getwd(), "bayesLife.output"), method = c("ar1", "shift", "scale"), predict.jmale = FALSE, my.e0M.file = NULL, end.year = 2100, start.year = NULL, output.dir = NULL, annual = NULL, nr.traj = NULL, seed = NULL, ar.pars = NULL, save.as.ascii = 0, verbose = TRUE, jmale.estimates = NULL, ...) e0.jmale.predict.subnat(e0.pred, estimates = NULL, gap.lim = c(0,18), max.e0.eq1.pred = 86, my.e0.file = NULL, save.as.ascii = 0, verbose = TRUE) subnat.gap.estimates(annual = FALSE)
e0.predict.subnat(countries, my.e0.file, sim.dir = file.path(getwd(), "bayesLife.output"), method = c("ar1", "shift", "scale"), predict.jmale = FALSE, my.e0M.file = NULL, end.year = 2100, start.year = NULL, output.dir = NULL, annual = NULL, nr.traj = NULL, seed = NULL, ar.pars = NULL, save.as.ascii = 0, verbose = TRUE, jmale.estimates = NULL, ...) e0.jmale.predict.subnat(e0.pred, estimates = NULL, gap.lim = c(0,18), max.e0.eq1.pred = 86, my.e0.file = NULL, save.as.ascii = 0, verbose = TRUE) subnat.gap.estimates(annual = FALSE)
countries |
Vector of numerical country codes or country names. |
my.e0.file |
Tab-separated ASCII file containing the subnational e0 data. In |
sim.dir |
Simulation directory with the national projections generated using |
method |
Method to use for the projections, see the reference paper. |
predict.jmale |
Logical determining if male projections should be generated as well. If |
my.e0M.file |
Tab-separated ASCII file containing the subnational male e0 data. |
end.year |
End year of the projections. |
start.year |
Start year of the projections. By default, projections start at the same time point as the national projections. |
output.dir |
Directory into which the resulting prediction objects and the trajectories are stored. See below for details. |
annual |
Logical indicating if the subnational projection should be on an annual scale or a 5-year scale. By default,
the scale is matched to the national simulation given by |
nr.traj |
Number of trajectories to be generated. If |
seed |
Seed of the random number generator. If |
ar.pars |
Named vector containing the parameter estimates for the AR(1) method (i.e. if |
save.as.ascii |
Either a number determining how many trajectories should be converted into an ASCII file, or “all” in which case all trajectories are converted. By default no conversion is performed. |
verbose |
Logical switching log messages on and off. |
jmale.estimates , estimates
|
List with estimates for the female-male gap model. The default values are retrieved using the function |
... |
Additional arguments passed to |
e0.pred |
Object of class |
gap.lim , max.e0.eq1.pred
|
The same meaning as in |
The e0.predict.subnat
function implements the methodology described in Sevcikova and Raftery (2021). Given a set of national bayesLife projections, it applies one of the methods (AR(1), Shift or Scale) to each national trajectory and each subregion of given countries which yields subnational e0 projections.
The file on subnational data passed into my.e0.file
and my.e0M.file
has to have a column “country_code” with numerical values corresponding to countries given in the argument countries
, and column “reg_code” giving the numerical identifier of each subregion. Column “name” should be used for subregion name, and column “country_name” for country name. An optional column “include_code” can be used to eliminate entries from processing. Entries with values of 1 or 2 will be included, all others will be ignored. Column “last.observed” can be used to define which time period contains the last observed data point (given as integer, e.g. year in the middle of the time period). Remaining columns define the time periods, e.g. “2000-2005”, “2005-2010” for a 5-year simulation, or “2020”, “2021” for an annual simulation. The package contains an example of such dataset, see Example below.
The default AR(1) parameters for the “ar1” method were designed for a 5-year simulation, see Sevcikova & Raftery (2021) for more detail. These are . If an annual AR(1) process is desired, we use the following conversion for the autoregressive parameter
and the
and
parameters:
. The
parameter stays the same for both processes. Thus, the annual parameters are
c(rho = 0.9898, U = 82.5, a = 0.01, b = -0.0032)
. Note that if the ar.pars
argument is specified by the user, it is assumed that the parameters have been scaled appropriately and thus, no conversion takes place.
Argument output.dir
gives a location on disk where results of the function should be stored. If it is NULL
(default), results are stored in the same directory as the national projections. In both cases a subdirectory called “subnat_method
” is created in which each country has its own subfolder with the country code in its name. Each such subfolder contains the same type of outputs as in the national case generated using e0.predict
, most importantly a directory “predictions” with trajectories for each region.
If the argument predict.jmale
is TRUE
, the e0.predict.subnat
invokes the e0.jmale.predict.subnat
function for each country. However, one can call the e0.jmale.predict.subnat
function explicitly. It applies the female-male gap model to regions of one country. See e0.jmale.predict
for more detail on the model. The default covariates of the model are not estimated on the fly. They were estimated externally using subnational data for about 30 countries and can be viewed using subnat.gap.estimates()
, either for estimates derived from 5-year data (default) or annual data (annual = TRUE
).
Function e0.predict.subnat
returns a list of objects of class bayesLife.prediction
. The name of each element includes its country code. Not all elements of the class bayesLife.prediction
are available. For example, no mcmc.set
is attached to these objects. Thus, not all functions that work with bayesLife.prediction
can be applied to these results.
Function e0.jmale.predict.subnat
returns an object of class bayesLife.prediction
which updates the input e0.pred
object by adding a new component called joint.male
. This component is also an object of class bayesLife.prediction
and it contains results of the male projections.
Even though the resulting object contains subnational results, the names of its elements are the same as in the national case. This allows to apply the same functions on both objects (subnational and national). However, it means that sometimes the meaning of the elements or function arguments does not match the subnational context. For example, various functions expect the argument country
. When a subnational object is passed to such a function, country
means a subregion.
Hana Sevcikova
H. Sevcikova, A. E. Raftery (2021). Probabilistic Projection of Subnational Life Expectancy. Journal of Official Statistics, Vol. 37, no. 3, 591-610.
get.rege0.prediction
, e0.predict
, e0.jmale.predict
# View the example data my.sube0.file <- file.path(find.package("bayesLife"), 'extdata', 'subnational_e0_template.txt') sube0 <- read.delim(my.sube0.file, check.names = FALSE) head(sube0) # Directory with national projections (contains 30 trajectories for each country) nat.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") # Subnational projections for Australia and Canada, # including the joint female-male gap model subnat.dir <- tempfile() preds <- e0.predict.subnat(c(36, 124), my.e0.file = my.sube0.file, sim.dir = nat.dir, output.dir = subnat.dir, start.year = 2018) names(preds) get.countries.table(preds[["36"]]) summary(preds[["36"]], "Queensland") e0.trajectories.plot(preds[["36"]], "Queensland") # plot subnational and national e0 in one plot nat.pred <- get.e0.prediction(nat.dir) e0.trajectories.plot(preds[["36"]], 4, pi = 80) e0.trajectories.plot(nat.pred, "Australia", add = TRUE, col = rep("darkgreen", 5), nr.traj = 0, show.legend = FALSE) legend("top", c("regional e0", "national e0"), col = c("red", "darkgreen"), lty = 1, bty = 'n') # Add male projection to Canada, # using (wrongly) female data only for demonstration predCan <- e0.jmale.predict.subnat(preds[["124"]], my.e0.file = my.sube0.file) # retrieve male prediction object predCanMale <- get.rege0.prediction(subnat.dir, 124, joint.male = TRUE) # the same works using predCanMale <- get.e0.jmale.prediction(predCan) # Retrieve female and male trajectories trajsF.Alberta <- get.e0.trajectories(predCan, "Alberta") trajsM.Alberta <- get.e0.trajectories(predCanMale, "Alberta") # summary of differences summary(t(trajsF.Alberta - trajsM.Alberta)) # cleanup unlink(subnat.dir) # See more examples in ?get.rege0.prediction
# View the example data my.sube0.file <- file.path(find.package("bayesLife"), 'extdata', 'subnational_e0_template.txt') sube0 <- read.delim(my.sube0.file, check.names = FALSE) head(sube0) # Directory with national projections (contains 30 trajectories for each country) nat.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") # Subnational projections for Australia and Canada, # including the joint female-male gap model subnat.dir <- tempfile() preds <- e0.predict.subnat(c(36, 124), my.e0.file = my.sube0.file, sim.dir = nat.dir, output.dir = subnat.dir, start.year = 2018) names(preds) get.countries.table(preds[["36"]]) summary(preds[["36"]], "Queensland") e0.trajectories.plot(preds[["36"]], "Queensland") # plot subnational and national e0 in one plot nat.pred <- get.e0.prediction(nat.dir) e0.trajectories.plot(preds[["36"]], 4, pi = 80) e0.trajectories.plot(nat.pred, "Australia", add = TRUE, col = rep("darkgreen", 5), nr.traj = 0, show.legend = FALSE) legend("top", c("regional e0", "national e0"), col = c("red", "darkgreen"), lty = 1, bty = 'n') # Add male projection to Canada, # using (wrongly) female data only for demonstration predCan <- e0.jmale.predict.subnat(preds[["124"]], my.e0.file = my.sube0.file) # retrieve male prediction object predCanMale <- get.rege0.prediction(subnat.dir, 124, joint.male = TRUE) # the same works using predCanMale <- get.e0.jmale.prediction(predCan) # Retrieve female and male trajectories trajsF.Alberta <- get.e0.trajectories(predCan, "Alberta") trajsM.Alberta <- get.e0.trajectories(predCanMale, "Alberta") # summary of differences summary(t(trajsF.Alberta - trajsM.Alberta)) # cleanup unlink(subnat.dir) # See more examples in ?get.rege0.prediction
The function computes the Raftery diagnostics for each parameter in the same way as tfr.raftery.diag
of the bayesTFR package.
e0.raftery.diag(mcmc = NULL, sim.dir = file.path(getwd(), "bayesLife.output"), burnin = 0, country = NULL, par.names = NULL, par.names.cs = NULL, country.sampling.prop = 1, verbose = TRUE, ...)
e0.raftery.diag(mcmc = NULL, sim.dir = file.path(getwd(), "bayesLife.output"), burnin = 0, country = NULL, par.names = NULL, par.names.cs = NULL, country.sampling.prop = 1, verbose = TRUE, ...)
mcmc |
A |
sim.dir |
Directory with the MCMC simulation results. Only used if |
burnin |
Burnin. |
country |
Name or code of a country. If it is given, only country-specific parameters parameters of that country are considered. |
par.names |
Names of country-independent parameters for which the Raftery diagnostics should be computed. By default all parameters are used. |
par.names.cs |
Names of country-specific parameters for which the Raftery diagnostics should be computed. By default all parameters are used. |
country.sampling.prop |
Proportion of countries that are included in the diagnostics. It should be between 0 and 1. If it is smaller than 1, the countries are randomly sampled. It is only relevant if |
verbose |
Logical switching log messages on and off. |
... |
Additional arguments passed to the |
See tfr.raftery.diag
for details. This function is called from e0.diagnose
.
Hana Sevcikova, Adrian Raftery
tfr.raftery.diag
, raftery.diag
, e0.diagnose
The functions plot/tabulate the posterior distribution of trajectories of the life expectancy for a given country, or for all countries, including their median and given probability intervals.
e0.trajectories.plot(e0.pred, country, pi = c(80, 95), both.sexes = FALSE, nr.traj = NULL, adjusted.only = TRUE, typical.trajectory = FALSE, xlim = NULL, ylim = NULL, type = "b", xlab = "Year", ylab = "Life expectancy at birth", main = NULL, lwd = c(2, 2, 2, 2, 1), col = c('black', 'green', 'red', 'red', '#00000020'), col2 = c('gray39', 'greenyellow', 'hotpink', 'hotpink', '#00000020'), pch = c(1, 2), show.legend = TRUE, add = FALSE, ...) e0.trajectories.plot.all(e0.pred, output.dir = file.path(getwd(), 'e0trajectories'), output.type = "png", verbose = FALSE, ...) e0.trajectories.table(e0.pred, country, pi = c(80, 95), both.sexes = FALSE, ...)
e0.trajectories.plot(e0.pred, country, pi = c(80, 95), both.sexes = FALSE, nr.traj = NULL, adjusted.only = TRUE, typical.trajectory = FALSE, xlim = NULL, ylim = NULL, type = "b", xlab = "Year", ylab = "Life expectancy at birth", main = NULL, lwd = c(2, 2, 2, 2, 1), col = c('black', 'green', 'red', 'red', '#00000020'), col2 = c('gray39', 'greenyellow', 'hotpink', 'hotpink', '#00000020'), pch = c(1, 2), show.legend = TRUE, add = FALSE, ...) e0.trajectories.plot.all(e0.pred, output.dir = file.path(getwd(), 'e0trajectories'), output.type = "png", verbose = FALSE, ...) e0.trajectories.table(e0.pred, country, pi = c(80, 95), both.sexes = FALSE, ...)
e0.pred |
Object of class |
country |
Name or numerical code of a country. It can also be given as ISO-2 or ISO-3 characters. |
pi |
Probability interval. It can be a single number or an array. If |
both.sexes |
Logical or the character “A”. If |
nr.traj |
Number of trajectories to be plotted. If |
adjusted.only |
Logical. By default, if the projection median is adjusted using e.g. |
typical.trajectory |
Logical. If |
xlim , ylim , type , xlab , ylab , main
|
Graphical parameters passed to the |
lwd , col , col2
|
Vector of five elements giving the line width and color for: 1. observed data, 2. imputed missing data, 3. median, 4. quantiles, 5. trajectories. |
pch |
Vector of two elements specifying plotting symbols for the observed and imputed data, respectively. It is not used if |
show.legend |
Logical controlling whether the legend should be drawn. |
add |
Logical controlling whether the trajectories should be plotted into a new graphic device ( |
... |
Additional graphical parameters. In addition, for |
output.dir |
Directory into which resulting graphs are stored. |
output.type |
Type of the resulting files. It can be “png”, “pdf”, “jpeg”, “bmp”, “tiff”, or “postscript”. |
verbose |
Logical switching log messages on and off. |
e0.trajectories.plot
plots posterior distribution of trajectories of life expectancy for a given country. e0.trajectories.table
gives the same output in a tabular format. e0.trajectories.plot.all
creates a set of such graphs (one per country) that are stored in output.dir
.
The median and given probability intervals are computed using all available trajectories. Thus, nr.traj
does not influence those values - it is used only to control the number of trajectories plotted.
Hana Sevcikova
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") pred <- get.e0.prediction(sim.dir) e0.trajectories.table(pred, country = "Japan", pi = c(80, 95)) e0.trajectories.plot(pred, country = "Japan", pi = c(80, 95)) # plot multiple countries into one plot e0.trajectories.plot(pred, "JP", col = rep("green", 5), nr.traj = 0, pi = c(80), show.legend = FALSE, main = "") e0.trajectories.plot(pred, "USA", col = rep("blue", 5), add = TRUE, nr.traj = 0, pi = c(80), show.legend = FALSE) legend("topleft", legend = c("Japan", "USA"), col = c("green", "blue"), lty = 1, bty = "n")
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") pred <- get.e0.prediction(sim.dir) e0.trajectories.table(pred, country = "Japan", pi = c(80, 95)) e0.trajectories.plot(pred, country = "Japan", pi = c(80, 95)) # plot multiple countries into one plot e0.trajectories.plot(pred, "JP", col = rep("green", 5), nr.traj = 0, pi = c(80), show.legend = FALSE, main = "") e0.trajectories.plot(pred, "USA", col = rep("blue", 5), add = TRUE, nr.traj = 0, pi = c(80), show.legend = FALSE) legend("topleft", legend = c("Japan", "USA"), col = c("green", "blue"), lty = 1, bty = "n")
Setting, retrieving and updating global options.
using.bayesLife() e0options() e0mcmc.options(..., annual = FALSE) e0pred.options(...) e0mcmc.dlpriors.options(prior.choice = "B", annual = FALSE, un.constraints = FALSE) get.DLpriors(prior.choice = NULL, annual = FALSE) data(DLpriors)
using.bayesLife() e0options() e0mcmc.options(..., annual = FALSE) e0pred.options(...) e0mcmc.dlpriors.options(prior.choice = "B", annual = FALSE, un.constraints = FALSE) get.DLpriors(prior.choice = NULL, annual = FALSE) data(DLpriors)
... |
Arguments in |
annual |
Logical indicating if the options are for an annual simulation ( |
prior.choice |
A character indicating for which combination of the upper bound on the |
un.constraints |
Logical indicating if constraints on the lower bounds of the |
Function using.bayesLife
sets all global options to their default values. Function e0options
is used to get all options as a named list.
The global options are divided into three main categories, namely options used for MCMC estimations in a 5-year simulation, in a 1-year simulation, and options used for predictions. To set or retrieve options of the first two categories, use e0mcmc.options
and use the argument annual
to distinguish between them (see section MCMC Options below), while the third category is controlled by e0pred.options
(see section Prediction Options below).
Many options are in the form of a list and it is possible to overwrite only single elements of the list. However, if an option is a vector, all elements of the vector have to be defined when updating (see Example).
The dataset DLpriors
contains four sets of parameters a
, delta
and tau
(see section MCMC Options below) estimated for different combinations of the upper limit on the z
parameter (i.e. maximum 5-year increase of e0; column “Uz”) and the upper bound of the sum of (column “Sa”; set in the
sumTriangle.lim
option which is interpreted as the value of e0 for which the transition is completed; see below for more detail). A get.DLpriors()
call retrieves all available combinations. Function e0mcmc.dlpriors.options
can be used to change the default option B (i.e. the upper limit on z
being 0.653 and the transition being completed at e0 of 86). Use the column “option” from DLpriors
to select the desired combination. In addition, setting the argument un.constraints
to TRUE
will set the lower limit on the parameters (
Triangle
, Triangle.c
) to the same values as the UN used for WPP 2022. Note that the DLpriors
dataset corresponds to parameter values designed for a 5-year simulation. Use get.DLprior(annual = TRUE)
to see the equivalents for an annual simulation where various values are divided by five.
e0options
returns a list of all global options.e0mcmc.options
, when called with no argument other than annual
, it returns a list of options related to the MCMC estimation. The annual
argument determines if the values correspond to an annual or 5-year simulation.e0pred.options
, when called with no argument, it returns a list of options related to the prediction.
For both, e0mcmc.options
and e0pred.options
, when a specific option is queried, it returns the value of that option. When an option is set, a list of the previous values of all MCMC/prediction options is returned invisibly. get.DLpriors
returns the content of the DLpriors
dataset. e0mcmc.dlpriors.options
overwrites various values and like e0mcmc.options
, it returns a list of the previous values of all MCMC options invisibly.
vector of the , ... ,
parameters, which are the prior means of the world-level parameters (
, ...,
,
,
).
vector of the , ... ,
parameters,
which are the prior standard deviations of the world-level parameters (
, ...,
,
,
).
vector of the , ... ,
parameters, which is the square root rate of the prior Gamma distribution of the world-level parameters (
, ...,
,
,
).
list with elements:
list with elements:
initial values for , ...,
. If not
NULL
, then each element should be of the same length as the number of MCMC chains. If it is NULL
, the initial values are equally spaced between ini.low
and ini.up
for the respective parameter. By default in the estimation, if there is just one chain, the initial value is the middle point of the interval.
vectors of length four. They are the lower and upper bounds for initial values of , ...,
. An
-th item is only used if
ini$Ti
is NULL
.
vectors of length four. They are the lower and upper bounds for the prior (truncated normal) distribution of , ...,
.
vector of length four defining the slice width for MCMC slice sampling for the four parameters, , ...,
.
lists with elements:
vector of initial values for (
). Its length (if not
NULL
) should correspond to the number of MCMC chains. By default, the initial values are equally spaced between ini.low
and ini.up
. In case of one chain, the initial value is by default the middle point of the interval.
single value giving the lower and upper bounds for initial values of (
). It is only used if
ini
is NULL
. Regarding defaults for the parameter, see Note below.
single value giving the lower and upper bounds for the prior (truncated normal) distribution of (
). Regarding defaults for the
parameter, see Note below.
single value giving the slice width for MCMC slice sampling of the parameter (not available for
).
list with elements:
list with elements:
initial values for , ...,
. Each element should be of the same length as the number of MCMC chains. If it is
NULL
, the initial values are equally spaced between ini.low
and ini.up
of the respective parameter. By default, if there is just one chain, the value is the middle point of the interval.
vectors of length four. They are the lower and upper bounds for initial values of , ...,
. An
-th item is only used if
ini$Ti
is NULL
.
vector of length four defining the slice width for MCMC slice sampling for the four parameters, , ...,
.
lists with elements:
vector of initial values for (
). Its length (if not
NULL
) should correspond to the number of MCMC chains. By default, the initial values are equally spaced between ini.low
and ini.up
. In case of one chain, the initial value is by default the middle point of the interval.
single value giving the lower and upper bounds for initial values of (
). It is only used if
ini
is NULL
.
single value giving the slice width for MCMC slice sampling of the parameter (not available for
).
list with elements:
vector of initial values for . Its length (if not
NULL
) should correspond to the number of MCMC chains. By default, the initial values are equally spaced between ini.low
and ini.up
. In case of one chain, the initial value is by default the middle point of the interval.
single value giving the lower and upper bounds for initial values of . It is only used if
ini
is NULL
.
list with elements:
list with elements:
vectors of size four. They correspond to the means and standard deviations, respectively, for the initial values of the country-specific parameters , ...,
which are drawn from a truncated normal distribution with bounds defined by
prior.low
and prior.up
.
vectors of length four. They are the lower and upper bounds for the prior (truncated normal) distribution of country-specific , ...,
.
vector of length four defining the slice width for MCMC slice sampling of the country-specific , ...,
.
list with elements:
named vector of length two, called “mean” and “sd”. The elements correspond to the means and standard deviations, respectively, for the initial values of the country-specific parameters (
) which are drawn from a normal distribution truncated between
prior.low
and prior.up
.
single values giving the lower and upper bounds for the prior (truncated normal) distribution of country-specific (
). Regarding defaults for
, see Note below.
single value giving the slice width for MCMC slice sampling of the (
) parameter.
the shape parameter of the Gamma distributions of all parameters is
nu/2
.
values of the parameters and
of the double logistic function.
lower and upper limits for the sum of the parameters. MCMC proposals that are outside of this limit are rejected. It is applied to both, the world parameters as well as the country specific parameters. The sum of
can be interpreted as the level of e0 at which the transition is completed and is followed by an e0 increase with a constant rate
z
.
named vector where names are the world parameters and values are the number of sub-parameters. For example, has 4 sub-parameters, while
and
are both just one parameter.
named vector where names are the country-specific parameters and values are the number of sub-parameters.
ranges for determining outliers in the historical data. If outliers=c(x, y)
then any increase in life expectancy smaller than x
or larger than y
is considered as an outlier and removed from the estimation.
buffer size (in number of [thinned] iterations) for keeping data in the memory. The smaller the buffer.size
the more often will the process access the hard disk and thus, the slower the run. On the other hand, the smaller the buffer.size
the less data will be lost in case of failure.
list containing a configuration for an ‘automatic’ run. All items in this list must be integer values. The option is only used if the argument iter
in run.e0.mcmc
is set to ‘auto’ (see description of argument iter
in run.e0.mcmc
). The list contains the following elements:
gives the number of iterations in the first chunk of the MCMC simulation.
gives the number of iterations in the following chunks.
gives the number of chains in all chunks of the MCMC simulation.
used in the convergence diagnostics following each chunk.
controls the maximum number of chunks.
This option allows to overwrite some of the prior parameters for specific countries. If it is not NULL
it should be a data frame with an obligatory column ‘country_code’. Each row then corresponds to one country. Other columns can be ‘k.c.prior.low’, ‘k.c.prior.up’, ‘z.c.prior.low’, ‘z.c.prior.up’, ‘Triangle_.c.prior.low’ and ‘Triangle_
.c.prior.up’ where
can be an integer from 1 to 4.
Parameter determines the asymptote in gains in life expectancy. The following text gives an explanation for the choice of upper limits on
-related parameters:
The pace of improvement and the asymptotic limit in future gains in female life expectancy vary for each projected trajectory, but ultimately is informed and constrained by the finding that the rate of increase of maximum female life expectancy over the past 150 year has been highly linear (2a, 2b) (i.e., about 2.4 years per decade), albeit at slightly lower pace once the leading countries started to exceed 75 years of female life expectancy at birth in the 1960s (3) (about 2.26 years of gains per decade). By assuming that the asymptotic average rate of increase in life expectancy is nonnegative, life expectancy is assumed to continually increase (on average), and no limit is imposed to life expectancy in the foreseeable future. The increase in maximum female life span among countries with highest life expectancy and reliable data on very old age provide further guidance on future rate of progress which has also been increasingly linear at least since the 1970s (4a-4c) (about 1.25 years per decade for countries like Sweden and Norway), and is used to inform the asymptotic average rate of increase in female life expectancy used in the 2012 WPP Revision. To set the posterior median to an annual gain of 0.125 year (or 5-year gain of 0.625 in this context) the upper bound value of 0.653 is used for the world prior () and country-specific prior (
) as default values in the estimation of the double-logistic parameters.
Hana Sevcikova, Patrick Gerland contributed to the documentation.
(1) J. L. Chunn, A. E. Raftery, P. Gerland, H. Sevcikova (2013): Bayesian Probabilistic Projections of Life Expectancy for All Countries. Demography 50(3):777-801. <doi:10.1007/s13524-012-0193-x>
(2a) Oeppen J, and J.W. Vaupel (2002) Broken limits to life expectancy. Science 296:1029-1031.
(2b) Vaupel, J.W. and K.G.V. Kistowski. 2005. Broken Limits to Life Expectancy. Ageing Horizons (3):6-13.
(3) Vallin, J., and F. Mesle (2009). The Segmented Trend Line of Highest Life Expectancies. Population and Development Review, 35(1), 159-187. doi:10.1111/j.1728-4457.2009.00264.x
(4a) Wilmoth, J. R., L. J. Deegan, H. Lundstrom, and S. Horiuchi (2000). Increase of maximum life-span in Sweden, 1861-1999. Science, 289(5488), 2366-2368.
(4b) Wilmoth, J. R. and J-M. Robine. (2003). The world trend in maximum life span, in: J. R. Carey and S. Tuljapurkar (eds.), Life Span: Evolutionary, Ecological, and Demographic Perspectives, supplement to vol. 29, Population and Development Review, pp. 239-257.
(4c) Wilmoth, J. R. and N. Ouellette (2012). Maximum human lifespan: Will the records be unbroken?, Paper presented at the European Population Conference, Stockholm, Sweden, 13-16 June.
e0mcmc.options("z", "Triangle") # Set new z$ini.up and Triangle$prior.up # Modifying single elements of the z-list and Triangle-list. # However, Triangle$prior.up is a vector and needs all four values. e0mcmc.options(z = list(ini.up = 0.8), Triangle = list(prior.up = rep(120, 4))) e0mcmc.options("z", "Triangle") # revert to defaults using.bayesLife() e0mcmc.options("z", "Triangle") # options for an annual simulation e0mcmc.options("z", "sumTriangle.lim", annual = TRUE) # modify using a different set from DLpriors get.DLpriors(annual = TRUE) # view the DLpriors dataset e0mcmc.dlpriors.options("C", annual = TRUE) # use C option # upper bounds for z correspond to values from DLpriors divided by 5 e0mcmc.options("z", "sumTriangle.lim", annual = TRUE) # set the UN's Triangle lower bound constraints e0mcmc.dlpriors.options(NULL, annual = TRUE, un.constraints = TRUE) e0mcmc.options("Triangle", "Triangle.c", annual = TRUE) # prior.low is modified
e0mcmc.options("z", "Triangle") # Set new z$ini.up and Triangle$prior.up # Modifying single elements of the z-list and Triangle-list. # However, Triangle$prior.up is a vector and needs all four values. e0mcmc.options(z = list(ini.up = 0.8), Triangle = list(prior.up = rep(120, 4))) e0mcmc.options("z", "Triangle") # revert to defaults using.bayesLife() e0mcmc.options("z", "Triangle") # options for an annual simulation e0mcmc.options("z", "sumTriangle.lim", annual = TRUE) # modify using a different set from DLpriors get.DLpriors(annual = TRUE) # view the DLpriors dataset e0mcmc.dlpriors.options("C", annual = TRUE) # use C option # upper bounds for z correspond to values from DLpriors divided by 5 e0mcmc.options("z", "sumTriangle.lim", annual = TRUE) # set the UN's Triangle lower bound constraints e0mcmc.dlpriors.options(NULL, annual = TRUE, un.constraints = TRUE) e0mcmc.options("Triangle", "Triangle.c", annual = TRUE) # prior.low is modified
The functions load objects of class bayesLife.convergence
from disk that were created using the function e0.diagnose
.
get.e0.convergence(sim.dir = file.path(getwd(), "bayesLife.output"), thin = 225, burnin = 10000) get.e0.convergence.all(sim.dir = file.path(getwd(), "bayesLife.output"))
get.e0.convergence(sim.dir = file.path(getwd(), "bayesLife.output"), thin = 225, burnin = 10000) get.e0.convergence.all(sim.dir = file.path(getwd(), "bayesLife.output"))
sim.dir |
Simulation directory used for computing the diagnostics. |
thin |
Thinning interval used with this diagnostics. |
burnin |
Burnin used for computing the diagnostics. |
Function get.e0.convergence
loads an object of class bayesLife.convergence
for the specific thin
and burnin
. Function get.e0.convergence.all
loads all bayesLife.convergence
objects available in sim.dir
.
get.e0.convergence
returns an object of class bayesLife.convergence
; get.e0.convergence.all
returns a list of objects of class bayesLife.convergence
.
Hana Sevcikova
e0.diagnose
, summary.bayesLife.convergence
.
The function get.e0.mcmc
retrieves results of an MCMC simulation and creates an object of class bayesLife.mcmc.set
. Function has.e0.mcmc
checks the existence of such results. Function e0.mcmc
extracts a single chain, and e0.mcmc.list
extracts several or all chains from the simulation results.
get.e0.mcmc(sim.dir = file.path(getwd(), "bayesLife.output"), chain.ids = NULL, low.memory = TRUE, burnin = 0, verbose = FALSE) has.e0.mcmc(sim.dir) e0.mcmc(mcmc.set, chain.id = 1) e0.mcmc.list(mcmc.set, chain.ids = NULL)
get.e0.mcmc(sim.dir = file.path(getwd(), "bayesLife.output"), chain.ids = NULL, low.memory = TRUE, burnin = 0, verbose = FALSE) has.e0.mcmc(sim.dir) e0.mcmc(mcmc.set, chain.id = 1) e0.mcmc.list(mcmc.set, chain.ids = NULL)
sim.dir |
Directory where the simulation results are stored. |
chain.ids |
Chain identifiers in case only specific chains should be included in the resulting object. By default, all available chains are included. |
low.memory |
If |
burnin |
Burnin used for loading traces. Only relevant, if |
verbose |
Logical switching log messages on and off. |
chain.id |
Chain identifier. |
mcmc.set |
Object of class |
get.e0.mcmc
returns an object of class bayesLife.mcmc.set
. has.e0.mcmc
returns a logical value.
e0.mcmc
returns an object of class bayesLife.mcmc
, and e0.mcmc.list
returns a list of bayesLife.mcmc
objects.
Hana Sevcikova
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") m <- get.e0.mcmc(sim.dir) summary(m) # summary of the world parameters for a single chain # (the same as above since there is only one chain in this toy example) summary(e0.mcmc.list(m)[[1]], par.names.cs = NULL) # the same as summary(e0.mcmc(m, chain.id = 1), par.names.cs = NULL)
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") m <- get.e0.mcmc(sim.dir) summary(m) # summary of the world parameters for a single chain # (the same as above since there is only one chain in this toy example) summary(e0.mcmc.list(m)[[1]], par.names.cs = NULL) # the same as summary(e0.mcmc(m, chain.id = 1), par.names.cs = NULL)
Functions for accessing traces of the MCMC parameters, either country-independent or country-specific.
get.e0.parameter.traces(mcmc.list, par.names = NULL, burnin = 0, thinning.index = NULL, thin = NULL) get.e0.parameter.traces.cs(mcmc.list, country.obj, par.names = NULL, burnin = 0, thinning.index = NULL, thin = NULL)
get.e0.parameter.traces(mcmc.list, par.names = NULL, burnin = 0, thinning.index = NULL, thin = NULL) get.e0.parameter.traces.cs(mcmc.list, country.obj, par.names = NULL, burnin = 0, thinning.index = NULL, thin = NULL)
mcmc.list |
List of |
country.obj |
Country object list (see |
par.names |
Names of country-independent parameters (in case of |
burnin |
Burn-in indicating how many iterations should be removed from the beginning of each chain. |
thinning.index |
Index of the traces for thinning. If it is |
thin |
Alternative to |
Both functions return a matrix with columns being the parameters and rows being the MCMC values, attached to one another in case of multiple chains. get.e0.parameter.traces
returns country-independent parameters, get.e0.parameter.traces.cs
returns country-specific parameters.
Hana Sevcikova
e0.coda.list.mcmc
for another way of retrieving parameter traces.
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") m <- get.e0.mcmc(sim.dir) e0.values <- get.e0.parameter.traces(m$mcmc.list, burnin = 10, par.names = "z") hist(e0.values, main = colnames(e0.values)) e0.values.cs <- get.e0.parameter.traces.cs(m$mcmc.list, get.country.object("Canada", meta = m$meta), burnin = 10, par.names = "z.c") hist(e0.values.cs, main = colnames(e0.values.cs))
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") m <- get.e0.mcmc(sim.dir) e0.values <- get.e0.parameter.traces(m$mcmc.list, burnin = 10, par.names = "z") hist(e0.values, main = colnames(e0.values)) e0.values.cs <- get.e0.parameter.traces.cs(m$mcmc.list, get.country.object("Canada", meta = m$meta), burnin = 10, par.names = "z.c") hist(e0.values.cs, main = colnames(e0.values.cs))
Function get.e0.prediction
retrieves results of a prediction and creates an object of class bayesLife.prediction
. Function has.e0.prediction
checks an existence of such results. Analogously, functions get.e0.jmale.prediction
and has.e0.jmale.prediction
retrieve and check an existence of male predictions from a given female prediction object.
get.e0.prediction(mcmc = NULL, sim.dir = NULL, joint.male = FALSE, mcmc.dir = NULL) has.e0.prediction(mcmc = NULL, sim.dir = NULL) get.e0.jmale.prediction(e0.pred) has.e0.jmale.prediction(e0.pred)
get.e0.prediction(mcmc = NULL, sim.dir = NULL, joint.male = FALSE, mcmc.dir = NULL) has.e0.prediction(mcmc = NULL, sim.dir = NULL) get.e0.jmale.prediction(e0.pred) has.e0.jmale.prediction(e0.pred)
mcmc |
Object of class |
sim.dir |
Directory where the prediction is stored. It should correspond to the value of the |
joint.male |
Logical. If |
mcmc.dir |
Optional argument to be used only in a special case when the mcmc object contained in the prediction object was estimated in different directory than in the one to which it points to (for example due to moving or renaming the original directory). The argument causes that the mcmc is redirected to the given directory. |
e0.pred |
Object of class |
If mcmc
is not NULL
, the search directory is set to mcmc$meta$output.dir
. This approach assumes that the prediction was stored in the same directory as the MCMC simulation, i.e. the output.dir
argument of the e0.predict
function was set to NULL
. If it is not the case, the argument mcmc.dir
should be used.
Function get.e0.jmale.prediction
extracts male projections from the e0.pred
objects (which should be a female prediction object), if the male prediction was generated using the e0.jmale.predict
function. has.e0.jmale.prediction
checks if such male prediction was generated.
Functions has.e0.prediction
and has.e0.jmale.prediction
return a logical indicating if a prediction exists.
Functions get.e0.prediction
and get.e0.jmale.prediction
return an
object of class bayesLife.prediction
.
Hana Sevcikova
bayesLife.prediction
, e0.predict
, summary.bayesLife.prediction
, e0.jmale.predict
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") pred <- get.e0.prediction(sim.dir = sim.dir) # female prediction summary summary(pred, country = "Canada") ## Not run: # male prediction summary # (works only if a joint male prediction exists - not the case in this toy example) summary(get.e0.jmale.prediction(pred), country = "Canada") ## End(Not run)
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") pred <- get.e0.prediction(sim.dir = sim.dir) # female prediction summary summary(pred, country = "Canada") ## Not run: # male prediction summary # (works only if a joint male prediction exists - not the case in this toy example) summary(get.e0.jmale.prediction(pred), country = "Canada") ## End(Not run)
Function for accessing trajectories of the life expectancy.
get.e0.trajectories(e0.pred, country)
get.e0.trajectories(e0.pred, country)
e0.pred |
Object of class |
country |
Name or numerical code of a country. It can also be given as ISO-2 or ISO-3 characters. |
The function loads trajectories of life expectancy for the given country from disk and returns it as a matrix.
Array of size the number of projection periods (including the present year) times the number of trajectories. The row names correspond to the mid-years of the prediction periods.
Hana Sevcikova
bayesLife.prediction
, get.e0.prediction
, e0.trajectories.table
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") pred <- get.e0.prediction(sim.dir=sim.dir) get.e0.trajectories(pred, "Germany")
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") pred <- get.e0.prediction(sim.dir=sim.dir) get.e0.trajectories(pred, "Germany")
Retrieve subnational (regional) prediction results produced by e0.predict.subnat
, either for one country or for all available countries.
get.rege0.prediction(sim.dir, country = NULL, method = "ar1", joint.male = FALSE)
get.rege0.prediction(sim.dir, country = NULL, method = "ar1", joint.male = FALSE)
sim.dir |
Simulation directory of the subnational predictions. It corresponds to the argument |
country |
Numerical country code. If it is not given, all available subnational predictions are retrieved. |
method |
Method used for generating the projections. It corresponds to the |
joint.male |
Logical. If |
Predictions for country are assumed to be stored in “
sim.dir
/subnat_method
/c”.
If argument country
is given, the function returns an object of class bayesLife.prediction
. If it is NULL
, it returns a list of such objects. Names of the list items are the country codes.
# Subnational example data my.sube0.file <- file.path(find.package("bayesLife"), 'extdata', 'subnational_e0_template.txt') sube0 <- read.delim(my.sube0.file, check.names = FALSE) countries <- unique(sube0[, c("country_code", "country_name")]) # Directory with national projections (contains 30 trajectories for each country) nat.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") # Subnational projections for all three countries, # including male projections where female # data is used only for demonstration purposes # (my.e0M.file should contain male e0). subnat.dir <- tempfile() e0.predict.subnat(countries$country_code, my.e0.file = my.sube0.file, sim.dir = nat.dir, output.dir = subnat.dir, start.year = 2013, predict.jmale = TRUE, my.e0M.file = my.sube0.file) # Retrieve results for all countries preds <- get.rege0.prediction(subnat.dir) names(preds) # View tables of subregions for each country for(i in 1:nrow(countries)) { cat("\n\n", countries$country_name[i], "\n") print(get.countries.table(preds[[as.character(countries$country_code[i])]])) } # Quantiles for individual subregions for female e0.trajectories.table(preds[["36"]], "Victoria") # Retrieve results for one country (Canada) pred <- get.rege0.prediction(subnat.dir, 124) e0.trajectories.plot(pred, "Quebec", both.sexes = TRUE) # Retrieve only male results predM <- get.rege0.prediction(subnat.dir, 124, joint.male = TRUE) e0.trajectories.table(predM, "Quebec") # cleanup unlink(subnat.dir) # See more examples in ?e0.predict.subnat
# Subnational example data my.sube0.file <- file.path(find.package("bayesLife"), 'extdata', 'subnational_e0_template.txt') sube0 <- read.delim(my.sube0.file, check.names = FALSE) countries <- unique(sube0[, c("country_code", "country_name")]) # Directory with national projections (contains 30 trajectories for each country) nat.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") # Subnational projections for all three countries, # including male projections where female # data is used only for demonstration purposes # (my.e0M.file should contain male e0). subnat.dir <- tempfile() e0.predict.subnat(countries$country_code, my.e0.file = my.sube0.file, sim.dir = nat.dir, output.dir = subnat.dir, start.year = 2013, predict.jmale = TRUE, my.e0M.file = my.sube0.file) # Retrieve results for all countries preds <- get.rege0.prediction(subnat.dir) names(preds) # View tables of subregions for each country for(i in 1:nrow(countries)) { cat("\n\n", countries$country_name[i], "\n") print(get.countries.table(preds[[as.character(countries$country_code[i])]])) } # Quantiles for individual subregions for female e0.trajectories.table(preds[["36"]], "Victoria") # Retrieve results for one country (Canada) pred <- get.rege0.prediction(subnat.dir, 124) e0.trajectories.plot(pred, "Quebec", both.sexes = TRUE) # Retrieve only male results predM <- get.rege0.prediction(subnat.dir, 124, joint.male = TRUE) e0.trajectories.table(predM, "Quebec") # cleanup unlink(subnat.dir) # See more examples in ?e0.predict.subnat
The function get.thinned.e0.mcmc
accesses
a thinned and burned version of the given MCMC set. create.thinned.e0.mcmc
creates such set.
get.thinned.e0.mcmc(mcmc.set, thin = 1, burnin = 0) create.thinned.e0.mcmc(mcmc.set, thin = 1, burnin = 0, output.dir = NULL, verbose = TRUE)
get.thinned.e0.mcmc(mcmc.set, thin = 1, burnin = 0) create.thinned.e0.mcmc(mcmc.set, thin = 1, burnin = 0, output.dir = NULL, verbose = TRUE)
mcmc.set |
Object of class |
thin , burnin
|
Thinning interval and burnin used for creating or identifying the thinned object. |
output.dir |
Directory for storing the thinned object. By default it is stored into the same directory as |
verbose |
Logical switching log messages on and off. |
The function create.thinned.e0.mcmc
is called from e0.predict
and thus, the resulting object contains exactly the same MCMCs used for generating projections.
The thinning is done as follows: The given burnin
is removed from the beginning of each chain in the original MCMC set. Then each chain is thinned by thin
using equal spacing and all chains are collapsed into one single chain per parameter. They are stored in output.dir
under the name ‘thinned_mcmc_t_b’ where t is the value of thin
and b the value of burnin
.
Both functions return an object of class bayesLife.mcmc.set
. get.thinned.e0.mcmc
returns NULL
if such object does not exist.
Hana Sevcikova
bayesLife.mcmc.set
, e0.predict
## Not run: sim.dir <- tempfile() m <- run.e0.mcmc(nr.chains = 2, iter = 60, thin = 2, output.dir = sim.dir, verbose = TRUE) pr <- e0.predict(m, burnin = 40, predict.jmale = FALSE) # creates thinned MCMCs mb <- get.thinned.e0.mcmc(m, thin = 2, burnin = 40) summary(mb, meta.only = TRUE) # length 20 = 2chains x (60-40)iters./2thin # the same chain as summary(pr$mcmc.set, meta.only = TRUE) unlink(sim.dir, recursive=TRUE) ## End(Not run)
## Not run: sim.dir <- tempfile() m <- run.e0.mcmc(nr.chains = 2, iter = 60, thin = 2, output.dir = sim.dir, verbose = TRUE) pr <- e0.predict(m, burnin = 40, predict.jmale = FALSE) # creates thinned MCMCs mb <- get.thinned.e0.mcmc(m, thin = 2, burnin = 40) summary(mb, meta.only = TRUE) # length 20 = 2chains x (60-40)iters./2thin # the same chain as summary(pr$mcmc.set, meta.only = TRUE) unlink(sim.dir, recursive=TRUE) ## End(Not run)
Datasets containing codes that determine which countries are to be included into a simulation or/and projections.
data(include_2022) data(include_2019) data(include_2017) data(include_2015) data(include_2012) data(include_2010)
data(include_2022) data(include_2019) data(include_2017) data(include_2015) data(include_2012) data(include_2010)
Data frames containing one record per country or region. It has the following variables:
Name of country or region. Not used.
Numerical Location Code (3-digit codes following ISO 3166-1 numeric standard) - see https://en.wikipedia.org/wiki/ISO_3166-1_numeric.
Entries for which include_code=2
are included in MCMC simulations (i.e. estimation of the model parameters). Entries for which include_code
is 1 or 2 are included in the prediction.
In a simulation, an include_*
dataset is selected that corresponds to the given wpp.year
passed to the function run.e0.mcmc
. It is merged with an e0
dataset from the corresponding wpp package using the country_code
column. Thus, the country entries in this dataset should correspond to entries in the e0F
(e0M
) dataset.
The package contains also a dataset called ‘my_e0_template’ (in ‘extdata’ directory) which is a template for user-specified e0 time series. It has the same structure as the e0
dataset, except that most of the columns are optional. The only required column is country_code
(see description of the argument my.e0.file
in run.e0.mcmc
).
In all three datasets, countries affected by AIDS are not included in the estimation, i.e. the include_code
is set to 3.
Data provided by the United Nations Population Division.
data(include_2019) head(include_2019) # select AIDS countries subset(include_2019, include_code == 3)
data(include_2019) head(include_2019) # select AIDS countries subset(include_2019, include_code == 3)
Runs (or continues running) MCMCs for simulating the life expectancy for all countries of the world, using a Bayesian hierarchical model.
run.e0.mcmc(sex = c("Female", "Male"), nr.chains = 3, iter = 160000, output.dir = file.path(getwd(), "bayesLife.output"), thin = 10, replace.output = FALSE, annual = FALSE, start.year = 1873, present.year = 2020, wpp.year = 2019, my.e0.file = NULL, my.locations.file = NULL, constant.variance = FALSE, seed = NULL, parallel = FALSE, nr.nodes = nr.chains, compression.type = 'None', verbose = FALSE, verbose.iter = 100, mcmc.options = NULL, ...) continue.e0.mcmc(iter, chain.ids = NULL, output.dir = file.path(getwd(), "bayesLife.output"), parallel = FALSE, nr.nodes = NULL, auto.conf = NULL, verbose = FALSE, verbose.iter = 10, ...)
run.e0.mcmc(sex = c("Female", "Male"), nr.chains = 3, iter = 160000, output.dir = file.path(getwd(), "bayesLife.output"), thin = 10, replace.output = FALSE, annual = FALSE, start.year = 1873, present.year = 2020, wpp.year = 2019, my.e0.file = NULL, my.locations.file = NULL, constant.variance = FALSE, seed = NULL, parallel = FALSE, nr.nodes = nr.chains, compression.type = 'None', verbose = FALSE, verbose.iter = 100, mcmc.options = NULL, ...) continue.e0.mcmc(iter, chain.ids = NULL, output.dir = file.path(getwd(), "bayesLife.output"), parallel = FALSE, nr.nodes = NULL, auto.conf = NULL, verbose = FALSE, verbose.iter = 10, ...)
sex |
Sex for which to run the simulation. |
nr.chains |
Number of MCMC chains to run. |
iter |
Number of iterations to run in each chain. In addition to a single value, it can have the value ‘auto’ for an automatic assessment of the convergence. In such a case, the function runs for the number of iterations given in the global option |
output.dir |
Directory which the simulation output should be written into. |
thin |
Thinning interval between consecutive observations to be stored on disk. |
replace.output |
If |
annual |
If |
start.year |
Start year for using historical data. |
present.year |
End year for using historical data. |
wpp.year |
Year for which WPP data is used. The functions loads a package called wpp |
my.e0.file |
File name containing user-specified e0 time series for one or more countries. See Details below. |
my.locations.file |
File name containing user-specified locations. See Details below. |
constant.variance |
Logical indicating if the model should be estimated using constant variance. It should only be used if the standard deviation lowess is to be analysed, see |
seed |
Seed of the random number generator. If |
parallel |
Logical determining if the simulation should run multiple chains in parallel. If it is |
nr.nodes |
Relevant only if |
compression.type |
One of ‘None’, ‘gz’, ‘xz’, ‘bz’, determining type of a compression of the MCMC files. |
verbose |
Logical switching log messages on and off. |
verbose.iter |
Integer determining how often (in number of iterations) log messages are outputted during the estimation. |
mcmc.options |
List of options that overwrites global MCMC options as defined in |
auto.conf |
In |
... |
Additional parameters to be passed to the function |
chain.ids |
Array of chain identifiers that should be resumed. If it is |
The function run.e0.mcmc
uses a set of global options (for priors, initial values etc.), possibly modified by the mcmc.options
argument. One can also modify these options using e0mcmc.options
. Call e0mcmc.options()
for the full set of options. Function continue.e0.mcmc
inherits its set of options from the corresponding run.e0.mcmc
call.
The function run.e0.mcmc
creates an object of class bayesLife.mcmc.meta
and stores it in output.dir
. It launches nr.chains
MCMCs, either sequentially or in parallel. Parameter traces of each chain are stored as (possibly compressed) ASCII files in a subdirectory of output.dir
, called mc
x where x is the identifier of that chain. There is one file per parameter, named after the parameter with the suffix “.txt”, possibly followed by a compression suffix if compression.type
is given. Country-specific parameters have the suffix _country
c where c is the country code. In addition to the trace files, each mc
x directory contains the object bayesLife.mcmc
in binary format. All chain-specific files are written into disk after the first, last and each -th (thinned) iteration, where
is given by the global option
buffer.size
.
Using the function continue.e0.mcmc
one can continue simulating an existing MCMCs by iter
iterations for either all or selected chains. The global options used for generating the existing MCMCs will be used. Only the auto.conf
option can be overwritten by passing the new value as an argument.
The function loads observed data (further denoted as WPP dataset), depending on the specified sex, from the e0F
(e0M
) and e0F_supplemental
(e0M_supplemental
) datasets in a wpp package where
is the
wpp.year
. It is then merged with the include
dataset that corresponds to the same wpp.year
. The argument my.e0.file
can be used to overwrite those default data. Such a file can include a subset of countries contained in the WPP dataset, as well as a set of new countries. In the former case,
the function replaces the corresponding country data from the WPP dataset with values in this file. Only columns are replaced that match column names of the WPP dataset, and in addition, columns ‘last.observed’ and ‘include_code’ are used, if present. Countries are merged with WPP using the column ‘country_code’. In addition, in order the countries to be included in the simulation, in both cases (whether they are included in the WPP dataset or not), they must be contained in the table of locations (UNlocations
). In addition, their corresponding ‘include_code’ must be set to 2. If the column ‘include_code’ is present in my.e0.file
, its value overwrites the default include code, unless is -1.
If annual
is TRUE
the default WPP dataset is not used and the my.e0.file
argument must provide the dataset to be used for estimation. Its time-related columns should be single years.
The default UN table of locations mentioned above can be overwritten/extended by using a file passed as the my.locations.file
argument. Such a file must have the same structure as the UNlocations
dataset. Entries in this file will overwrite corresponding entries in UNlocations
matched by the column ‘country_code’. If there is no such entry in the default dataset, it will be appended. This option of appending new locations is especially useful in cases when my.e0.file
contains new countries/regions that are not included in UNlocations
. In such a case, one must provide a my.locations.file
with a definition of those countries/regions.
For simulation of the hyperparameters of the Bayesian hierarchical model, all countries are used that are included in the WPP dataset, possibly complemented by the my.e0.file
, that have include_code
equal to 2. The hyperparameters are used to simulate country-specific parameters, which is done for all countries with include_code
equal 1 or 2. The following values of include_code
in my.e0.file
are recognized: -1 (do not overwrite the default include code), 0 (ignore), 1 (include in prediction but not estimation), 2 (include in both, estimation and prediction). Thus, the set of countries included in the estimation and prediction can be fully specified by the user.
Optionally, my.e0.file
can contain a column called last.observed
containing the year of the last observation for each country. In such a case, the code would ignore any data after that time point. Furthermore, the function e0.predict
fills in the missing values using the median of the BHM procedure (stored in e0.matrix.reconstructed
of the bayesLife.prediction
object). For last.observed
values that are below a middle year of a time interval (computed as
) the last valid data point is the time interval
, whereas for values larger equal a middle year, the data point in
is valid.
The package contains a dataset called ‘my_e0_template’ (in ‘extdata’ directory) which is a template for user-specified my.e0.file
.
An object of class bayesLife.mcmc.set
which is a list with two components:
meta |
An object of class |
mcmc.list |
A list of objects of class |
Hana Sevcikova, Patrick Gerland contributed to the documentation.
J. L. Chunn, A. E. Raftery, P. Gerland, H. Sevcikova (2013): Bayesian Probabilistic Projections of Life Expectancy for All Countries. Demography 50(3):777-801. <doi:10.1007/s13524-012-0193-x>
get.e0.mcmc
, summary.bayesLife.mcmc.set
, e0mcmc.options
, e0.predict
.
## Not run: m <- run.e0.mcmc(nr.chains = 1, iter = 5, thin = 1, verbose = TRUE) summary(m) m <- continue.e0.mcmc(iter = 5, verbose = TRUE) summary(m) ## End(Not run)
## Not run: m <- run.e0.mcmc(nr.chains = 1, iter = 5, thin = 1, verbose = TRUE) summary(m) m <- continue.e0.mcmc(iter = 5, verbose = TRUE) summary(m) ## End(Not run)
Run MCMC for extra countries, areas or regions. It uses the posterior distribution of model hyperparameters from an existing simulation to generate country-specific parameters.
run.e0.mcmc.extra(sim.dir = file.path(getwd(), "bayesLife.output"), countries = NULL, my.e0.file = NULL, iter = NULL, thin = 1, burnin = 0, parallel = FALSE, nr.nodes = NULL, my.locations.file = NULL, country.overwrites = NULL, verbose = FALSE, verbose.iter = 100, ...)
run.e0.mcmc.extra(sim.dir = file.path(getwd(), "bayesLife.output"), countries = NULL, my.e0.file = NULL, iter = NULL, thin = 1, burnin = 0, parallel = FALSE, nr.nodes = NULL, my.locations.file = NULL, country.overwrites = NULL, verbose = FALSE, verbose.iter = 100, ...)
sim.dir |
Directory with an existing simulation. |
countries |
Vector of country codes. These include codes of areas and regions (see column |
my.e0.file |
File name containing user-specified time series of life expectancy for countries for which the simulation should run (see Details below). |
iter |
Number of iterations to be used for sampling from the posterior distribution of the hyperparameters. By default, the number of (possibly thinned) iterations used in the existing simulation is taken. |
thin |
Thinning interval for sampling from the posterior distribution of the hyperparameters. |
burnin |
Number of iterations discarded before sampling from the posterior distribution of the hyperparameters. |
parallel |
Logical determining if the simulation should run multiple chains in parallel. |
nr.nodes |
Relevant only if |
my.locations.file |
File name containing user-specified locations. See Details below. |
country.overwrites |
This argument allows to overwrite some of the prior parameters for specific countries, stored in the global option of the same name, see |
verbose |
Logical switching log messages on and off. |
verbose.iter |
Integer determining how often (in number of iterations) log messages are outputted during the estimation. |
... |
Additional parameters to be passed to the function |
The function can be used to make predictions for countries, areas or regions (further denoted as ‘countries’) that were not included in the MCMC estimation (invoked by run.e0.mcmc
). It creates MCMC traces for country-specific parameters. The purpose of this function is to have country-specific parameters available in order to be able to generate projections for additional countries or their aggregations, without having to re-run the often time-expensive MCMC simulation.
The set of countries to be considered by this function can be given either by their codes, using the argument countries
, in which case the countries must be included in the UN WPP e0
dataset. Or, it can be given by a user-specific file, using the argument my.e0.file
. The function considers a union of both arguments. The function will ignore all countries that were used in the existing MCMC simulation for estimating the hyperparameters. Countries that already own country-specific parameters (e.g. because they were included in my.e0.file
passed to run.e0.mcmc
) get their parameters recomputed. Note that all countries must be included in the UNlocations
dataset, but unlike in run.e0.mcmc
, their include_code
is ignored. As in the case of run.e0.mcmc
, the default dataset of locations UNlocations
can be overwritten using a file of the same structure as UNlocations
passed via the my.locations.file
argument. This file should be especially used, if e0 is simulated for new locations that are not included in UNlocations
.
An object of class bayesLife.mcmc.set
.
If there is an existing projection for the directory sim.dir
, use e0.predict.extra
to obtain projections for the extra countries used in this function.
Hana Sevcikova
## Not run: m <- run.e0.mcmc(nr.chains = 1, iter = 20, thin = 1, verbose = TRUE) m <- run.e0.mcmc.extra(countries = c(908,924), burnin = 10, verbose = TRUE) summary(m, country = 924) pred <- e0.predict(burnin = 10, verbose = TRUE) summary(pred, country = 908) ## End(Not run)
## Not run: m <- run.e0.mcmc(nr.chains = 1, iter = 20, thin = 1, verbose = TRUE) m <- run.e0.mcmc.extra(countries = c(908,924), burnin = 10, verbose = TRUE) summary(m, country = 924) pred <- e0.predict(burnin = 10, verbose = TRUE) summary(pred, country = 908) ## End(Not run)
Summary of an object of class bayesLife.convergence
created using the e0.diagnose
function. It gives an overview about parameters that did not converge.
## S3 method for class 'bayesLife.convergence' summary(object, expand = FALSE, ...)
## S3 method for class 'bayesLife.convergence' summary(object, expand = FALSE, ...)
object |
Object of class |
expand |
By default, the function does not show parameters for each country for which there was no convergence, if the status is ‘red’. This argument can switch that option on. |
... |
Not used. |
Hana Sevcikova
Summary of an object bayesLife.mcmc.set
or bayesLife.mcmc
, computed via run.e0.mcmc
. It can be obtained either for all countries or for a specific country, and either for all parameters or for specific parameters. The function uses the summary.mcmc
function of the coda package.
## S3 method for class 'bayesLife.mcmc.set' summary(object, country = NULL, chain.id = NULL, par.names = NULL, par.names.cs = NULL, meta.only = FALSE, thin = 1, burnin = 0, ...) ## S3 method for class 'bayesLife.mcmc' summary(object, country = NULL, par.names = NULL, par.names.cs = NULL, thin = 1, burnin = 0, ...)
## S3 method for class 'bayesLife.mcmc.set' summary(object, country = NULL, chain.id = NULL, par.names = NULL, par.names.cs = NULL, meta.only = FALSE, thin = 1, burnin = 0, ...) ## S3 method for class 'bayesLife.mcmc' summary(object, country = NULL, par.names = NULL, par.names.cs = NULL, thin = 1, burnin = 0, ...)
object |
Object of class |
country |
Country name or code if a country-specific summary is desired. It can also be given as ISO-2 or ISO-3 characters. |
chain.id |
Identifiers of MCMC chains. By default, all chains are considered. |
par.names |
Country independent parameters to be included in the summary. Run |
par.names.cs |
Country-specific parameters to be included in the summary. Run |
meta.only |
Logical. If it is |
thin |
Thinning interval. Only used if larger than the |
burnin |
Number of iterations to be discarded from the beginning of each chain before computing the summary. |
... |
Additional arguments passed to the |
Hana Sevcikova
bayesLife.mcmc.set
, summary.mcmc
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") m <- get.e0.mcmc(sim.dir) summary(m, country="Czechia", burnin=20) # names and codes of countries included head(get.countries.table(m, iso = TRUE)) # using an ISO code summary(m, country="MG", burnin=20)
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") m <- get.e0.mcmc(sim.dir) summary(m, country="Czechia", burnin=20) # names and codes of countries included head(get.countries.table(m, iso = TRUE)) # using an ISO code summary(m, country="MG", burnin=20)
Country-specific summary of an object of class bayesLife.prediction
, created using the function e0.predict
. The summary contains the mean, standard deviation and several commonly used quantiles of the simulated trajectories.
## S3 method for class 'bayesLife.prediction' summary(object, country = NULL, compact = TRUE, ...)
## S3 method for class 'bayesLife.prediction' summary(object, country = NULL, compact = TRUE, ...)
object |
Object of class |
country |
Country name or code. |
compact |
Logical switching between a smaller and larger number of displayed quantiles. |
... |
Not used. |
Hana Sevcikova
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") pred <- get.e0.prediction(sim.dir=sim.dir) summary(pred, country="Iceland") # names and codes of countries included tail(get.countries.table(pred, iso = TRUE), 20) # using an ISO code summary(pred, country="CHE")
sim.dir <- file.path(find.package("bayesLife"), "ex-data", "bayesLife.output") pred <- get.e0.prediction(sim.dir=sim.dir) summary(pred, country="Iceland") # names and codes of countries included tail(get.countries.table(pred, iso = TRUE), 20) # using an ISO code summary(pred, country="CHE")
The function creates two files containing projection summaries, such as the median, the lower and upper bound of the 80 and 90% probability intervals, respectively, and the constant variant. One file is in a user-friendly format, whereas the other is in a UN-specific format with internal coding of the time and the variants.
write.e0.projection.summary(dir = file.path(getwd(), "bayesLife.output"), output.dir = NULL, revision = NULL, adjusted = FALSE)
write.e0.projection.summary(dir = file.path(getwd(), "bayesLife.output"), output.dir = NULL, revision = NULL, adjusted = FALSE)
dir |
Directory containing the prediction object. It should correspond to the |
output.dir |
Directory in which the resulting file will be stored. If |
revision |
UN revision number. If |
adjusted |
Logical. By default the function writes summary using the original BHM projections. If the projection medians are adjusted (using e.g. |
The first file that the function creates is called ‘projection_summary_user_friendly.csv’, it is a comma-separated table with the following columns:
“country_name”: country name
“country_code”: country code
“variant”: name of the variant, such as “median”, “lower 80”, “upper 80”, “lower 95”, “upper 95”, “constant”
period1: e.g. “2010-2015”: life expectancy for the first time period
period2: e.g. “2015-2020”: life expectancy for the second time period
... further columns with life expectancy projections
The second file, called ‘projection_summary.csv’, also comma-separated table, contains the same information as above in a UN-specific format:
“RevID”: revision number, passed to the function as an argument;
“VarID”: variant identifier, extracted from the UN_variants
dataset in the bayesTFR package;
“LocID”: country code;
“TimeID”: time identifier, extracted from the UN_time
dataset in the bayesTFR package;
“e0”: the life expectancy for this variant, location and time period.
If the simulation directory contains joint male predictions, summary files for those are created as well. In such a case, if output.dir
is given, separate subdirectories for female and male are created.
This function is automatically called from the e0.predict
and e0.jmale.predict
functions, therefore in standard cases it will not be needed to call it directly.
Hana Sevcikova
convert.e0.trajectories
, e0.predict