From e8bc098129f8ba726e03526535ae2a22883fa0f8 Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer <reinhold@kainhofer.com> Date: Sun, 10 Sep 2017 20:59:56 +0000 Subject: [PATCH] Add analytic mortality distributions - mortalityTable.deMoivre(omega) - mortalityTable.MakehamGompertz(A, B, c) - mortalityTable.Weibull(k, n) --- DESCRIPTION | 3 + NAMESPACE | 6 ++ R/mortalityTable.MakehamGompertz.R | 65 +++++++++++++++++++++ R/mortalityTable.Weibull.R | 48 +++++++++++++++ R/mortalityTable.deMoivre.R | 42 +++++++++++++ man/mortalityTable.MakehamGompertz-class.Rd | 38 ++++++++++++ man/mortalityTable.Weibull-class.Rd | 32 ++++++++++ man/mortalityTable.deMoivre-class.Rd | 27 +++++++++ 8 files changed, 261 insertions(+) create mode 100644 R/mortalityTable.MakehamGompertz.R create mode 100644 R/mortalityTable.Weibull.R create mode 100644 R/mortalityTable.deMoivre.R create mode 100644 man/mortalityTable.MakehamGompertz-class.Rd create mode 100644 man/mortalityTable.Weibull-class.Rd create mode 100644 man/mortalityTable.deMoivre-class.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 68fd0dd..e42b6b4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -44,6 +44,9 @@ Collate: 'makeQxDataFrame.R' 'mortalityComparisonTable.R' 'mortalityImprovement.R' + 'mortalityTable.MakehamGompertz.R' + 'mortalityTable.Weibull.R' + 'mortalityTable.deMoivre.R' 'periodDeathProbabilities.R' 'mortalityTable.jointLives.R' 'mortalityTables.list.R' diff --git a/NAMESPACE b/NAMESPACE index 38e64ed..698b724 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,7 +6,10 @@ export(generateAgeShift) export(makeQxDataFrame) export(mortalityComparisonTable) export(mortalityTable) +export(mortalityTable.MakehamGompertz) +export(mortalityTable.Weibull) export(mortalityTable.ageShift) +export(mortalityTable.deMoivre) export(mortalityTable.improvementFactors) export(mortalityTable.joined) export(mortalityTable.jointLives) @@ -26,7 +29,10 @@ export(plotMortalityTableComparisons) export(plotMortalityTables) export(plotMortalityTrend) exportClasses(mortalityTable) +exportClasses(mortalityTable.MakehamGompertz) +exportClasses(mortalityTable.Weibull) exportClasses(mortalityTable.ageShift) +exportClasses(mortalityTable.deMoivre) exportClasses(mortalityTable.improvementFactors) exportClasses(mortalityTable.joined) exportClasses(mortalityTable.jointLives) diff --git a/R/mortalityTable.MakehamGompertz.R b/R/mortalityTable.MakehamGompertz.R new file mode 100644 index 0000000..7089136 --- /dev/null +++ b/R/mortalityTable.MakehamGompertz.R @@ -0,0 +1,65 @@ +#' @include mortalityTable.period.R +NULL + +#' Class mortalityTable.MakehamGompertz - Mortality table with Makeham-Gompertz's law +#' +#' A period life table following Makeham and Gompertz's law of a mortality rate +#' $\mu$ increasing exponentially with age $x$ ($\mu_{x+t} = A + B c^{(x+t)}$). +#' The only required slots are the parameters $A$, $B$ and $c$, all probabilities +#' are calculated from them, for technical reasons a maximum age of 120 is +#' technically assumed. Optionally, a name and loading can be passed +#' (inherited from \code{\link{mortalityTable}}). +#' +#' @slot A Parameter A of the Makeham-Gompertz distribution +#' @slot B Parameter B of the Makeham-Gompertz distribution +#' @slot c Parameter c of the Makeham-Gompertz distribution +#' @slot omega Maximum age (default: 150) +#' +#' @examples +#' # A Gompertz mortality, roughtly approximating the Austrian annuitants 2017 +#' gmp = mortalityTable.MakehamGompertz(A = 0, B = 0.00001, c = 1.11) +#' mortalityTables.load("Austria_Annuities_AVOe2005R") +#' plot(gmp, AVOe2005R.male, Period=2017) +#' +#' # A Makeham-Gompertz mortality, approximating the Austrian annuitants 2017 +#' mg = mortalityTable.MakehamGompertz(A = 0.0002, B = 0.00001, c = 1.11) +#' plot(mg, gmp, AVOe2005R.male, Period=2017) +#' +#' @export mortalityTable.MakehamGompertz +#' @exportClass mortalityTable.MakehamGompertz +mortalityTable.MakehamGompertz = setClass( + "mortalityTable.MakehamGompertz", + slots = list( + A = "numeric", + B = "numeric", + c = "numeric", + omega = "numeric" + ), + prototype = list( + A = 0, + B = 1, + c = 1, + omega = 120 + ), + contains = "mortalityTable.period" +) + +setMethod("initialize", "mortalityTable.MakehamGompertz", function(.Object, A = 0, B = 1, c = 1, omega = 120, name = NULL, ...) { + if (missing(name) || is.null(name)) { + if (A == 0) { + name = paste("Gompertz mortality, B=", B, ", c=", c, collapse = "") + } else if (B == 0 || c == 1) { + name = paste("Exponential mortality, mu=", A + B * c, collapse = "") + } else { + name = paste("Makeham-Gompertz mortality, A=", A, ", B=", B, ", c=", c, collapse = "") + } + } + ages = 0:omega + if (c == 1 || B == 0) { + deathProbs = 1 - exp(-A) + } else { + deathProbs = 1 - exp(-A) * exp(B/log(c) * (c^ages - c^(ages + 1))) + } + callNextMethod(.Object, A = A, B = B, c = c, omega = omega, name = name, ages = ages, deathProbs = deathProbs, ...) +}) + diff --git a/R/mortalityTable.Weibull.R b/R/mortalityTable.Weibull.R new file mode 100644 index 0000000..8c7f666 --- /dev/null +++ b/R/mortalityTable.Weibull.R @@ -0,0 +1,48 @@ +#' @include mortalityTable.period.R +NULL + +#' Class mortalityTable.Weibull - Mortality table with Weibull's law +#' +#' A period life table following Weibulls's law of a mortality rate +#' $\mu$ increasing as a power of $t$ ($\mu_{x+t} = k * (x+t)^n$). +#' The only required slots are the parameters $k>0$ and $n>0$, all probabilities +#' are calculated from them, for technical reasons a maximum age of 150 is +#' technically assumed. Optionally, a name and loading can be passed +#' (inherited from \code{\link{mortalityTable}}). +#' +#' @slot k Parameter k of the Weibull distribution +#' @slot n Parameter n of the Weibull distribution +#' @slot omega Maximum age (default: 120) +#' +#' @examples +#' # A Weibull mortality +#' wbl = mortalityTable.Weibull(k = 0.0000000001, n = 4.8) +#' mortalityTables.load("Austria_Annuities_AVOe2005R") +#' plot(wbl, AVOe2005R.male, Period=2017, ylim = c(0.00005, 1)) +#' +#' @export mortalityTable.Weibull +#' @exportClass mortalityTable.Weibull +mortalityTable.Weibull = setClass( + "mortalityTable.Weibull", + slots = list( + k = "numeric", + n = "numeric", + omega = "numeric" + ), + prototype = list( + k = 0.005, + n = 5, + omega = 120 + ), + contains = "mortalityTable.period" +) + +setMethod("initialize", "mortalityTable.Weibull", function(.Object, k = 1, n = 1, omega = 120, name = NULL, ...) { + if (missing(name) || is.null(name)) { + name = paste("Weibull mortality, k=", k, ", n=", n, collapse = "") + } + ages = 0:omega + deathProbs = 1 - exp(-k / (n + 1) * ((ages + 1)^(n + 1) - ages^(n + 1))) + callNextMethod(.Object, k = k, n = n, omega = omega, name = name, ages = ages, deathProbs = deathProbs, ...) +}) + diff --git a/R/mortalityTable.deMoivre.R b/R/mortalityTable.deMoivre.R new file mode 100644 index 0000000..7de638e --- /dev/null +++ b/R/mortalityTable.deMoivre.R @@ -0,0 +1,42 @@ +#' @include mortalityTable.period.R +NULL + +#' Class mortalityTable.deMoivre - Mortality table with de Moivre's law +#' +#' A period life table with maximum age omega dn the time of death identically +#' distributed on the interval [0, omega]. The only required slot is the maximum +#' age \code{omega}, all probabilities are calculated from it. +#' Optionally, a name and loading can be passed (inherited from +#' \code{\link{mortalityTable}}). +#' +#' @slot omega Maximum age +#' +#' @examples +#' mm = mortalityTable.deMoivre(100) +#' plot(mm, +#' mortalityTable.deMoivre(90), +#' mortalityTable.deMoivre(50)) +#' +#' @export mortalityTable.deMoivre +#' @exportClass mortalityTable.deMoivre +mortalityTable.deMoivre = setClass( + "mortalityTable.deMoivre", + slots = list( + omega = "numeric" + ), + prototype = list( + omega = 100 + ), + contains = "mortalityTable.period" +) + +setMethod("initialize", "mortalityTable.deMoivre", function(.Object, omega = 100, name = NULL, ...) { + if (missing(name) || is.null(name)) { + name = paste("de Moivre mortality, omega=", omega) + } + callNextMethod(.Object, omega = omega, name = name, ages = 0:omega, deathProbs = c(1 / (omega - 0:(omega - 1)), 1), ...) +}) + + + + diff --git a/man/mortalityTable.MakehamGompertz-class.Rd b/man/mortalityTable.MakehamGompertz-class.Rd new file mode 100644 index 0000000..31a3b92 --- /dev/null +++ b/man/mortalityTable.MakehamGompertz-class.Rd @@ -0,0 +1,38 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/mortalityTable.MakehamGompertz.R +\docType{class} +\name{mortalityTable.MakehamGompertz-class} +\alias{mortalityTable.MakehamGompertz-class} +\alias{mortalityTable.MakehamGompertz} +\title{Class mortalityTable.MakehamGompertz - Mortality table with Makeham-Gompertz's law} +\description{ +A period life table following Makeham and Gompertz's law of a mortality rate +$\mu$ increasing exponentially with age $x$ ($\mu_{x+t} = A + B c^{(x+t)}$). +The only required slots are the parameters $A$, $B$ and $c$, all probabilities +are calculated from them, for technical reasons a maximum age of 120 is +technically assumed. Optionally, a name and loading can be passed +(inherited from \code{\link{mortalityTable}}). +} +\section{Slots}{ + +\describe{ +\item{\code{A}}{Parameter A of the Makeham-Gompertz distribution} + +\item{\code{B}}{Parameter B of the Makeham-Gompertz distribution} + +\item{\code{c}}{Parameter c of the Makeham-Gompertz distribution} + +\item{\code{omega}}{Maximum age (default: 150)} +}} + +\examples{ +# A Gompertz mortality, roughtly approximating the Austrian annuitants 2017 +gmp = mortalityTable.MakehamGompertz(A = 0, B = 0.00001, c = 1.11) +mortalityTables.load("Austria_Annuities_AVOe2005R") +plot(gmp, AVOe2005R.male, Period=2017) + +# A Makeham-Gompertz mortality, approximating the Austrian annuitants 2017 +mg = mortalityTable.MakehamGompertz(A = 0.0002, B = 0.00001, c = 1.11) +plot(mg, gmp, AVOe2005R.male, Period=2017) + +} diff --git a/man/mortalityTable.Weibull-class.Rd b/man/mortalityTable.Weibull-class.Rd new file mode 100644 index 0000000..88ac6ca --- /dev/null +++ b/man/mortalityTable.Weibull-class.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/mortalityTable.Weibull.R +\docType{class} +\name{mortalityTable.Weibull-class} +\alias{mortalityTable.Weibull-class} +\alias{mortalityTable.Weibull} +\title{Class mortalityTable.Weibull - Mortality table with Weibull's law} +\description{ +A period life table following Weibulls's law of a mortality rate +$\mu$ increasing as a power of $t$ ($\mu_{x+t} = k * (x+t)^n$). +The only required slots are the parameters $k>0$ and $n>0$, all probabilities +are calculated from them, for technical reasons a maximum age of 150 is +technically assumed. Optionally, a name and loading can be passed +(inherited from \code{\link{mortalityTable}}). +} +\section{Slots}{ + +\describe{ +\item{\code{k}}{Parameter k of the Weibull distribution} + +\item{\code{n}}{Parameter n of the Weibull distribution} + +\item{\code{omega}}{Maximum age (default: 120)} +}} + +\examples{ +# A Weibull mortality +wbl = mortalityTable.Weibull(k = 0.0000000001, n = 4.8) +mortalityTables.load("Austria_Annuities_AVOe2005R") +plot(wbl, AVOe2005R.male, Period=2017, ylim = c(0.00005, 1)) + +} diff --git a/man/mortalityTable.deMoivre-class.Rd b/man/mortalityTable.deMoivre-class.Rd new file mode 100644 index 0000000..060015d --- /dev/null +++ b/man/mortalityTable.deMoivre-class.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/mortalityTable.deMoivre.R +\docType{class} +\name{mortalityTable.deMoivre-class} +\alias{mortalityTable.deMoivre-class} +\alias{mortalityTable.deMoivre} +\title{Class mortalityTable.deMoivre - Mortality table with de Moivre's law} +\description{ +A period life table with maximum age omega dn the time of death identically +distributed on the interval [0, omega]. The only required slot is the maximum +age \code{omega}, all probabilities are calculated from it. +Optionally, a name and loading can be passed (inherited from +\code{\link{mortalityTable}}). +} +\section{Slots}{ + +\describe{ +\item{\code{omega}}{Maximum age} +}} + +\examples{ +mm = mortalityTable.deMoivre(100) +plot(mm, + mortalityTable.deMoivre(90), + mortalityTable.deMoivre(50)) + +} -- GitLab