Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • R/r-mortality-tables
1 result
Select Git revision
Show changes
Commits on Source (4)
......@@ -12,3 +12,5 @@ PossibleTables.txt
^inst/extdata/Austria_Population_Observation.csv$
^revdep$
^cran-comments\.md$
^README\.Rmd$
^CRAN-SUBMISSION$
Version: 2.0.5
Date: 2023-10-27 08:19:24 UTC
SHA: 63a17dc3789e97fa12908206ea1d96e068fbdbb7
......@@ -3,8 +3,8 @@ Type: Package
Version: 2.0.5
Date: 2023-10-27
Title: A Framework for Various Types of Mortality / Life Tables
Authors@R: c(person("Reinhold", "Kainhofer", role=c("aut", "cre"), email="reinhold@kainhofer.com"))
Author: Reinhold Kainhofer [aut, cre]
Authors@R: c(person("Reinhold", "Kainhofer", role=c("aut", "cre", "cph"), email="reinhold@kainhofer.com"))
Author: Reinhold Kainhofer [aut, cre, cph]
Maintainer: Reinhold Kainhofer <reinhold@kainhofer.com>
URL: https://gitlab.open-tools.net/R/r-mortality-tables
BugReports: https://gitlab.open-tools.net/R/r-mortality-tables/-/issues
......@@ -25,7 +25,7 @@ Suggests:
tidyverse,
reshape2,
rmarkdown
Description: Classes to implement and plot cohort life tables
Description: Classes to implement, analyze and plot cohort life tables
for actuarial calculations. Birth-year dependent cohort mortality
tables using a yearly trend to extrapolate from a base year are implemented,
as well as period life table, cohort life tables using an age shift, and
......
# MortalityTables 2.0.5 (27.10.2023):
* Fix incorrect ages in Austrian population mortality
* Update README with examples
# MortalityTables 2.0.4 (19.10.2023):
* Add new Austrian population mortality tables (2020/22)
# MortalityTables 2.0.3 (24.8.2021):
* Fix vignette builds
# MortalityTables 2.0.2 (12.12.2020):
* Add/fix some sample tables
* gracefully handle missing suggested packages
* First version of vignette about international tables
# MortalityTables 2.0.1 (4.9.2020):
* Fix performance issues with mortalityTables.list and mortalityTables.load
# MortalityTables 2.0 (27.8.2020):
* Add various convenience functions to derive tables from data
* implement dimensional information
* extend plotting capabilities (more flexible)
* Add more international tables / datasets
# MortalityTables 1.0 (30.3.2018):
* Initial Submission to CRAN
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(tidyverse)
library(reshape2)
library(pracma)
```
# MortalityTables
<!-- badges: start -->
<!-- badges: end -->
The goal of MortalityTables is to provide generic base classes and functions to
handle all kinds of actuarial actuarial mortality tables (period and cohort life tables).
Cohort and static life tables are implemented, observed data can be used, and
existing life tables can be blended or extrapolated to derive new tables.
Furthermore, plotting functions are provided for reports and publications.
## Installation
You can install the development version of MortalityTables from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("kainhofer/MortalityTables")
```
## About the package
The MortalityTables package provides the `mortalityTable` base class and
some derived classes to handle different types of mortality tables (also
called life tables), mainly
used for life insurance. Additionally it provides a plot function to compare
multiple life tables either directly using the absolute mortalities in
log-linear plots or using relative mortalities as percentages of a given
reference table.
### Types of Life Tables
Provided types of mortality tables are:
* Base class
: Class `mortalityTable`
* Period life table
: Class `mortalityTable.period(ages, deathProbs, ..., baseYear=2000)`
: Death probabilities observed / predicted for one observation year;
No dependency on the bith year is assumed.
* Cohort life table using age-specific trends
: Class `mortalityTable.trendProjection`
: Death probabilities of a given base year are projected into the future
using age-specific trends $\lambda_x$. The death probability of an $x$-year old in year
`baseYear + n` is calculated as:
$$q_x^{(baseYear+n)} = q_x^{(baseYear)} \cdot e^{-n\cdot\lambda_x}$$
: Consequently, the death probabilities for a person born in year `YOB` can be calculated as
$$q_x^{YOB} = q_x^{(base)} \cdot e^{-(YOB+x-baseYear)\cdot \lambda_x}$$
* Cohort life table approximation using age shift
: Class `mortalityTable.ageShift`
: Death probabilities for cohort $YOB$ are obtained by using death probabilities
for cohort $X$ and modifying the technical age with a birth-year dependent shift:
$$q_x^{YOB} = q_{x+shift(YOB)}^{(base)}$$
<!-- * Observed life table -->
<!-- : Class `mortalityTable.observed` -->
<!-- : Death probabilities observed during several years. The probabilities are -->
<!-- stored as a matrix with observation year and age as dimensions. -->
* Mixed life table
: Class `mortalityTable.mixed`
: Arithmetic mean of two life tables with given weights. This approach is
often used to generate unisex life tables by mixing male and female
mortalities with given weights (e.g. 70:30 or 40:60)
* Cohort life table using age-specific improvement factors
: Class `mortalityTable.improvementFactors`
: Project base life table using age-specific improvement factors.
* Pension table
: Class `pensionTable`
: Four states: active, early retirement / invalidity, old-age pension, death (with optional widow)
: All slots describe the corresponding transition probabilities by a
: `mortalityTable`-derived object.
## Loading the MortalityTables package
```{R loading}
library("MortalityTables")
```
## Provided Data Sets
The package provides several real-life life tables published by census bureaus
and actuarial associations around the world. You can use the function
`mortalityTables.list` to list all available datasets (if no argument is given)
or all datasets that match the given pattern (wildcard character is *). You can
then use `mortalityTables.load` to load either one single data set or all
datasets that match the pattern.
```{r ListLoadTables}
# list all datasets for Austria
mortalityTables.list("Austria_*")
# Load the German annuity table DAV 2004-R
mortalityTables.load("Germany_Annuities_DAV2004R")
# Load all Austrian data sets
mortalityTables.load("Austria_*")
```
## Cohort and Period Mortality Data
Cohort mortality vectors (for a given birth year) or period death probabilities (for a given observation year) can be extracted with the functions `periodDeathProbabilities()` and `deathProbabilities()`:
```{r deathProbabilities}
mortalityTables.load("Austria_Annuities")
deathProbabilities(AVOe2005R.male, YOB = 1977, ages = 35:50)
deathProbabilities(AVOe2005R.male, YOB = 2023, ages = 35:50)
periodDeathProbabilities(AVOe2005R.male, Period = 2023, ages = 35:50)
```
If the mortality table is a cohort table, the trend is used to calculate the death probabilities for the given cohort or calendar year. If the table is a static life table, the period and cohort life tables will be identical. If the table is an observed table (i.e. observed death probabilities for each age and year), the data is extracted from the matrix' rows/columns or diagonals. In all cases, the user does not have use different methods for different underlying tables.
## Plotting and Comparing Mortality Data
There are two plotting functions using ggplot: `plotMortalityTables()` and `plotMortalityTableComparisons()` to plot the absolute and relative mortalities. For absolute mortalities, the `q(x)` axis employs a log10-scale. The returned plot is a normal ggplot2 object, so all features provided by ggplot2 can be adde to the plots.
```{r Plotting, fig.width=8.5, fig.height=5}
mortalityTables.load("Austria_Annuities")
plotMortalityTables(AVOe2005R.male, AVOe2005R.male.unloaded, AVOe1996R.male, EROM.G1950.male,
YOB = 1977, ages = 0:99, legend.position = c(0.5, 0.65))
plotMortalityTableComparisons(AVOe2005R.male, AVOe2005R.male.unloaded, AVOe1996R.male, EROM.G1950.male,
YOB = 1977, ages = 0:99, legend.position = c(0.5, 0.65))
plotMortalityTrend(AVOe2005R.male, AVOe2005R.male.unloaded, AVOe1996R.male, AVOe1996R.male, EROM.G1950.male)
```
## Further information
For further information on how to use the package, see the ["Using the MortalityTables Package"](https://cran.r-project.org/package=MortalityTables/vignettes/using-the-mortalityTables-package.html) vignette.
<!-- README.md is generated from README.Rmd. Please edit that file -->
# MortalityTables
## Author: Reinhold Kainhofer, reinhold@kainhofer.com
R package implementing actuarial mortality tables (period and cohort life tables)
<!-- badges: start -->
<!-- badges: end -->
The goal of MortalityTables is to provide generic base classes and
functions to handle all kinds of actuarial actuarial mortality tables
(period and cohort life tables). Cohort and static life tables are
implemented, observed data can be used, and existing life tables can be
blended or extrapolated to derive new tables.
Furthermore, plotting functions are provided for reports and
publications.
## Installation
You can install the development version of MortalityTables from
[GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("kainhofer/MortalityTables")
```
## About the package
The MortalityTables package provides the `mortalityTable` base class and
some derived classes to handle different types of mortality tables (also
called life tables), mainly
used for life insurance. Additionally it provides a plot function to compare
multiple life tables either directly using the absolute mortalities in
log-linear plots or using relative mortalities as percentages of a given
reference table.
some derived classes to handle different types of mortality tables (also
called life tables), mainly used for life insurance. Additionally it
provides a plot function to compare multiple life tables either directly
using the absolute mortalities in log-linear plots or using relative
mortalities as percentages of a given reference table.
### Types of Life Tables
Provided types of mortality tables are:
* Base class
: Class `mortalityTable`
* Period life table
: Class `mortalityTable.period(ages, deathProbs, ..., baseYear=2000)`
: Death probabilities observed / predicted for one observation year;
No dependency on the bith year is assumed.
* Cohort life table using age-specific trends
: Class `mortalityTable.trendProjection`
: Death probabilities of a given base year are projected into the future
using age-specific trends $\lambda_x$. The death probability of an $x$-year old in year
`baseYear + n` is calculated as:
$$q_x^{(baseYear+n)} = q_x^{(baseYear)} \cdot e^{-n\cdot\lambda_x}$$
: Consequently, the death probabilities for a person born in year `YOB` can be calculated as
$$q_x^{YOB} = q_x^{(base)} \cdot e^{-(YOB+x-baseYear)\cdot \lambda_x}$$
* Cohort life table approximation using age shift
: Class `mortalityTable.ageShift`
: Death probabilities for cohort $YOB$ are obtained by using death probabilities
for cohort $X$ and modifying the technical age with a birth-year dependent shift:
$$q_x^{YOB} = q_{x+shift(YOB)}^{(base)}$$
<!-- * Observed life table -->
<!-- : Class `mortalityTable.observed` -->
<!-- : Death probabilities observed during several years. The probabilities are -->
<!-- stored as a matrix with observation year and age as dimensions. -->
* Mixed life table
: Class `mortalityTable.mixed`
: Arithmetic mean of two life tables with given weights. This approach is
often used to generate unisex life tables by mixing male and female
mortalities with given weights (e.g. 70:30 or 40:60)
* Cohort life table using age-specific improvement factors
: Class `mortalityTable.improvementFactors`
: Project base life table using age-specific improvement factors.
* Pension table
: Class `pensionTable`
: Four states: active, early retirement / invalidity, old-age pension, death (with optional widow)
: All slots describe the corresponding transition probabilities by a
: `mortalityTable`-derived object.
- Base class
Class `mortalityTable`
- Period life table
Class `mortalityTable.period(ages, deathProbs, ..., baseYear=2000)`
Death probabilities observed / predicted for one observation year;
No dependency on the bith year is assumed.
- Cohort life table using age-specific trends
Class `mortalityTable.trendProjection`
Death probabilities of a given base year are projected into the
future using age-specific trends $\lambda_x$. The death probability
of an $x$-year old in year `baseYear + n` is calculated as:
$$q_x^{(baseYear+n)} = q_x^{(baseYear)} \cdot e^{-n\cdot\lambda_x}$$
Consequently, the death probabilities for a person born in year
`YOB` can be calculated as
$$q_x^{YOB} = q_x^{(base)} \cdot e^{-(YOB+x-baseYear)\cdot \lambda_x}$$
- Cohort life table approximation using age shift
Class `mortalityTable.ageShift`
Death probabilities for cohort $YOB$ are obtained by using death
probabilities for cohort $X$ and modifying the technical age with a
birth-year dependent shift:
$$q_x^{YOB} = q_{x+shift(YOB)}^{(base)}$$
<!-- * Observed life table -->
<!-- : Class `mortalityTable.observed` -->
<!-- : Death probabilities observed during several years. The probabilities are -->
<!-- stored as a matrix with observation year and age as dimensions. -->
- Mixed life table
Class `mortalityTable.mixed`
Arithmetic mean of two life tables with given weights. This approach
is often used to generate unisex life tables by mixing male and
female mortalities with given weights (e.g. 70:30 or 40:60)
- Cohort life table using age-specific improvement factors
Class `mortalityTable.improvementFactors`
Project base life table using age-specific improvement factors.
- Pension table
Class `pensionTable`
Four states: active, early retirement / invalidity, old-age pension,
death (with optional widow)
All slots describe the corresponding transition probabilities by a
`mortalityTable`-derived object.
## Loading the MortalityTables package
```
``` r
library("MortalityTables")
```
## Provided Data Sets
The package provides several real-life life tables published by census bureaus
and actuarial associations around the world. You can use the function
`mortalityTables.list` to list all available datasets (if no argument is given)
or all datasets that match the given pattern (wildcard character is *). You can
then use `mortalityTables.load` to load either one single data set or all
datasets that match the pattern.
```
# list all available data sets
mortalityTables.list()
The package provides several real-life life tables published by census
bureaus and actuarial associations around the world. You can use the
function `mortalityTables.list` to list all available datasets (if no
argument is given) or all datasets that match the given pattern
(wildcard character is \*). You can then use `mortalityTables.load` to
load either one single data set or all datasets that match the pattern.
``` r
# list all datasets for Austria
mortalityTables.list("Austria_*")
#> [1] "Austria_Annuities" "Austria_Annuities_AVOe1996R"
#> [3] "Austria_Annuities_AVOe2005R" "Austria_Annuities_EROMF"
#> [5] "Austria_Annuities_RR67" "Austria_Census"
#> [7] "Austria_Endowments_ADSt2426_2Lives" "Austria_PopulationForecast"
#> [9] "Austria_PopulationMCMC" "Austria_PopulationObserved"
#> [11] "Austria_VUGesamtbestand_2012-16"
# Load the German annuity table DAV 2004-R
mortalityTables.load("Germany_Annuities_DAV2004R")
# Load all Austrian data sets
mortalityTables.load("Austria_*")
#> Lade nötiges Paket: MortalityLaws
#> Warning: Paket 'MortalityLaws' wurde unter R Version 4.2.3 erstellt
```
## Cohort and Period Mortality Data
Cohort mortality vectors (for a given birth year) or period death
probabilities (for a given observation year) can be extracted with the
functions `periodDeathProbabilities()` and `deathProbabilities()`:
``` r
mortalityTables.load("Austria_Annuities")
deathProbabilities(AVOe2005R.male, YOB = 1977, ages = 35:50)
#> [1] 0.0006467352 0.0006741228 0.0007202125 0.0007820113 0.0008524437
#> [6] 0.0009260103 0.0009794564 0.0010272832 0.0010731228 0.0011227093
#> [11] 0.0011784509 0.0012409740 0.0013080864 0.0013817843 0.0014633494
#> [16] 0.0015513107
deathProbabilities(AVOe2005R.male, YOB = 2023, ages = 35:50)
#> [1] 0.0001941029 0.0002041420 0.0002200821 0.0002411616 0.0002653197
#> [6] 0.0002909132 0.0003106056 0.0003288675 0.0003468281 0.0003663459
#> [11] 0.0003882554 0.0004128296 0.0004394048 0.0004687102 0.0005012581
#> [16] 0.0005366270
periodDeathProbabilities(AVOe2005R.male, Period = 2023, ages = 35:50)
#> [1] 0.0004718782 0.0005066172 0.0005573996 0.0006231746 0.0006993210
#> [6] 0.0007819197 0.0008511112 0.0009184673 0.0009869854 0.0010620137
#> [11] 0.0011462722 0.0012409740 0.0013445246 0.0014595263 0.0015880515
#> [16] 0.0017292761
```
If the mortality table is a cohort table, the trend is used to calculate
the death probabilities for the given cohort or calendar year. If the
table is a static life table, the period and cohort life tables will be
identical. If the table is an observed table (i.e. observed death
probabilities for each age and year), the data is extracted from the
matrix’ rows/columns or diagonals. In all cases, the user does not have
use different methods for different underlying tables.
## Plotting and Comparing Mortality Data
There are two plotting functions using ggplot: `plotMortalityTables()`
and `plotMortalityTableComparisons()` to plot the absolute and relative
mortalities. For absolute mortalities, the `q(x)` axis employs a
log10-scale. The returned plot is a normal ggplot2 object, so all
features provided by ggplot2 can be adde to the plots.
``` r
mortalityTables.load("Austria_Annuities")
plotMortalityTables(AVOe2005R.male, AVOe2005R.male.unloaded, AVOe1996R.male, EROM.G1950.male,
YOB = 1977, ages = 0:99, legend.position = c(0.5, 0.65))
```
<img src="man/figures/README-Plotting-1.png" width="100%" />
``` r
plotMortalityTableComparisons(AVOe2005R.male, AVOe2005R.male.unloaded, AVOe1996R.male, EROM.G1950.male,
YOB = 1977, ages = 0:99, legend.position = c(0.5, 0.65))
```
<img src="man/figures/README-Plotting-2.png" width="100%" />
``` r
plotMortalityTrend(AVOe2005R.male, AVOe2005R.male.unloaded, AVOe1996R.male, AVOe1996R.male, EROM.G1950.male)
```
<img src="man/figures/README-Plotting-3.png" width="100%" />
## Further information
For further information on how to use the package, see the "Using the MortalityTables Package" vignette.
## Changelog
* Version 1.0 (30.3.2018):
* Initial Submission to CRAN
* Version 2.0 (27.8.2020):
* Add various convenience functions to derive tables from data
* implement dimensional information
* extend plotting capabilities (more flexible)
* Add more international tables / datasets
* Version 2.0.1 (4.9.2020):
* Fix performance issues with mortalityTables.list and mortalityTables.load
* Version 2.0.2 (12.12.2020):
* Add/fix some sample tables
* gracefully handle missing suggested packages
* First version of vignette about international tables
* Version 2.0.3 (24.8.2021):
* Fix vignette builds
* Version 2.0.4 (19.10.2023):
* Add new Austrian population mortality tables (2020/22)
* Version 2.0.5 (27.10.2023):
* Fix incorrect ages in Austrian population mortality
For further information on how to use the package, see the [“Using the
MortalityTables
Package”](https://cran.r-project.org/package=MortalityTables/vignettes/using-the-mortalityTables-package.html)
vignette.
## R CMD check results ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── MortalityTables 2.0.5 ────
Duration: 3m 53.3s
0 errors ✔ | 0 warnings ✔ | 0 notes ✔
This is a patch version to correct wrong vector index offsets in Austrian mortality data.
Alter,Ausscheidewahrscheinlichkeit,Erlebenserwartung
0,0.1153804,55.50
1,0.0161876,62.74
2,0.0063539,63.76
3,0.0040476,64.15
4,0.0031575,64.40
5,0.0024232,64.59
6,0.001985,64.74
7,0.0017193,64.85
8,0.0015589,64.95
9,0.0014901,65.04
10,0.0014109,65.13
11,0.0013298,65.20
12,0.0013087,65.28
13,0.0014047,65.35
14,0.0016319,65.42
15,0.0019293,65.50
16,0.0023253,65.60
17,0.0028059,65.72
18,0.0033625,65.85
19,0.0038743,66.02
20,0.0042756,66.20
21,0.0044987,66.40
22,0.0045681,66.60
23,0.0045042,66.81
24,0.0044381,67.00
25,0.004382,67.20
26,0.0043309,67.38
27,0.004225,67.56
28,0.0041172,67.73
29,0.0040336,67.90
30,0.0040395,68.06
31,0.0040808,68.21
32,0.0040732,68.36
33,0.0040873,68.51
34,0.0041419,68.66
35,0.0042512,68.80
36,0.0044507,68.95
37,0.0046461,69.09
38,0.0048289,69.24
39,0.0050569,69.39
40,0.0053455,69.55
41,0.0056943,69.71
42,0.0060547,69.87
43,0.0063984,70.04
44,0.0067627,70.22
45,0.0072395,70.39
46,0.0077412,70.58
47,0.0082417,70.77
48,0.0088218,70.97
49,0.0095107,71.17
50,0.010311,71.39
51,0.0110594,71.61
52,0.0118956,71.84
53,0.0129607,72.08
54,0.0141932,72.33
55,0.0154771,72.59
56,0.0168074,72.87
57,0.0182973,73.16
58,0.0198897,73.46
59,0.0216718,73.77
60,0.0236395,74.10
61,0.0257388,74.44
62,0.0281273,74.80
63,0.0307722,75.17
64,0.0336925,75.55
65,0.0369152,75.96
66,0.0406539,76.38
67,0.0447188,76.82
68,0.0488101,77.28
69,0.0530999,77.75
70,0.0580827,78.24
71,0.0639447,78.75
72,0.0703689,79.28
73,0.0771813,79.83
74,0.0851501,80.40
75,0.0939023,81.00
76,0.1022245,81.62
77,0.1102224,82.26
78,0.119587,82.91
79,0.130578,83.58
80,0.1419805,84.27
81,0.1538578,84.97
82,0.1673561,85.69
83,0.182352,86.44
84,0.1977081,87.20
85,0.2128398,87.99
86,0.2279162,88.80
87,0.2432851,89.63
88,0.2552892,90.47
89,0.2671834,91.32
90,0.2845555,92.17
91,0.2998259,93.03
92,0.3146066,93.90
93,0.3296905,94.77
94,0.3451082,95.65
95,0.3609957,96.51
96,0.3701296,97.37
97,0.3917524,98.18
98,0.4067815,98.93
99,0.4285685,99.57
100,1,100.00
stopifnot(require(methods), require(utils), require(MortalityTables)) # MortalityTable classes; new; Excel reader
#' German Life Tables for (pure) endowments, loaded and unloaded
#' - ADSt 1924/26: German population mortality tables
"Germany_Endowments"
######################################################
## DAV 2008T Aggregat / Smoker / Non-Smoker
######################################################
ADSt192426.data = utils::read.csv(
system.file("extdata", "Germany_Endowments_ADSt1924-26_M.csv",
package = "MortalityTables"),
col.names = c(
"age", "qx", "Ex"));
ADSt192426.male = mortalityTable.period(
name = "ADSt 1924/26 male",
ages = ADSt192426.data$age,
deathProbs = ADSt192426.data$qx,
data = list(
dim = list(sex = "m", collar = "Aggregat", type = "Population", data = "unloaded", year = "1924")
)
)
rm(ADSt192426.data)
# plot(ADSt192426.male)
......@@ -6,7 +6,7 @@
\alias{MortalityTables-package}
\title{Provide life table classes for life insurance purposes}
\description{
Classes to implement and plot cohort life tables for actuarial calculations. In particular, birth-year dependent mortality tables using a yearly trend to extrapolate from a base year are implemented, as well as period life table, cohort life tables using an age shift, and merged life tables.
Classes to implement, analyze and plot cohort life tables for actuarial calculations. Birth-year dependent cohort mortality tables using a yearly trend to extrapolate from a base year are implemented, as well as period life table, cohort life tables using an age shift, and merged life tables. Additionally, several data sets from various countries are included to provide widely-used tables out of the box.
}
\seealso{
Useful links:
......@@ -17,6 +17,6 @@ Useful links:
}
\author{
\strong{Maintainer}: Reinhold Kainhofer \email{reinhold@kainhofer.com}
\strong{Maintainer}: Reinhold Kainhofer \email{reinhold@kainhofer.com} [copyright holder]
}
man/figures/README-Plotting-1.png

13 KiB

man/figures/README-Plotting-2.png

12.4 KiB

man/figures/README-Plotting-3.png

11.4 KiB