Learn R Programming

NNMoMo (version 0.1.0)

NNMoMoData: Create NNMoMoData Object from Demogdata Object or HMD Datasets

Description

Utility function to construct an object of class NNMoMoData from one or multiple mortality datasets of class demogdata. Each dataset is converted into a tabular representation with mortality rates and population counts by age, sex, and year, suitable for fitting a neural network using fit.NNMoMo.

Usage

NNMoMoData(...)

Value

An object of class NNMoMoData containing the following columns:

  • country: Country label from the input demogdata object.

  • sex: Sex of the population ("female" or "male").

  • year: Calendar year.

  • rate_*: Mortality rates by age (e.g., rate_0, rate_1, ...).

  • pop_*: Population counts by age (e.g., pop_0, pop_1, ...).

Arguments

...

One or more objects of class demogdata. Each input must contain mortality rates and population data for both sexes ("female", "male"), structured as in the demography package. If no arguments are provided, the function will prompt the user for Human Mortality Database (HMD) login credentials (email and password) and automatically download a predefined list of countries using hmd.mx. The list of countries is the same used by Scognamiglio, S. (2022). Calibrating the Lee-Carter and the Poisson Lee-Carter models via neural networks. ASTIN Bulletin: The Journal of the IAA, 52(2), 519-561. Cambridge University Press. tools:::Rd_expr_doi("10.1017/asb.2022.5")

Details

This function converts the age-by-year matrices of mortality rates and populations into long-format rows, one row per sex-year combination. Each row stores the full vector of rates and populations across all ages.

If any input is not of class demogdata, the function stops with an error.

If no inputs are provided, the function connects to the Human Mortality Database (HMD) using user-supplied credentials and downloads a predefined set of countries (AUS, AUT, BEL, BGR, BLR, CAN, CHE, CZE, DEUTE, DEUTW, DNK, ESP, EST, FIN, FRATNP, GBRTENW, GBR_NIR, GBR_SCO, GRC, HUN, IRL, ISL, ISR, ITA, JPN, LTU, LUX, LVA, NLD, NOR, NZL_NM, POL, PRT, RUS, SVK, SVN, SWE, TWN, UKR, USA). It might take a few minutes to download all of them.

A demogdata object can also be created from scratch or imported manually from the Human Mortality Database using hmd.mx.

The label of the demogdata object must be specified, as all subsequent operations rely on this label.

Examples

Run this code
# Example 1: Create from an existing demogdata object
demography_obj <- demography::demogdata(
  data = matrix(runif(10*5), nrow = 10),
  pop = matrix(runif(10*5, 1000, 2000), nrow = 10),
  ages = 50:59,
  years = 2000:2004,
  type = "mortality",
  name = "male",
  label = "France"
)
nn_data <- NNMoMoData(demography_obj)
head(nn_data)

if (FALSE) {
# Example 2: Automatic download from HMD if no arguments are provided
# (will prompt for email and password, requires HMD access)
nn_data_auto <- NNMoMoData()
head(nn_data_auto)
}

Run the code above in your browser using DataLab