Skip to content
Snippets Groups Projects
Commit 8165cd4b authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Improve ValuationTable class

- Add security loading to all tables
- Add getOmega and ages functions for all classes
parent bce68466
No related branches found
No related tags found
No related merge requests found
...@@ -4,8 +4,8 @@ library(ggplot2); ...@@ -4,8 +4,8 @@ library(ggplot2);
# (virtual) base class for valuation tables, contains only the name / ID # (virtual) base class for valuation tables, contains only the name / ID
valuationTable=setClass( valuationTable=setClass(
"valuationTable", "valuationTable",
slots=list(name="character", baseYear="numeric"), slots=list(name="character", baseYear="numeric", loading="numeric"),
prototype=list(name="Actuarial Valuation Table", baseYear=2000), prototype=list(name="Actuarial Valuation Table", baseYear=2000, loading=0),
contains="VIRTUAL" contains="VIRTUAL"
); );
...@@ -71,8 +71,8 @@ valuationTable.joined=setClass( ...@@ -71,8 +71,8 @@ valuationTable.joined=setClass(
# A cohort life table obtained by mixing two life tables with the given weights # A cohort life table obtained by mixing two life tables with the given weights
valuationTable.mixed=setClass( valuationTable.mixed=setClass(
"valuationTable.mixed", "valuationTable.mixed",
slots=c(table1="valuationTable", table2="valuationTable", weight1="numeric", weight2="numeric"), slots=c(table1="valuationTable", table2="valuationTable", weight1="numeric", weight2="numeric", loading="numeric"),
prototype=list(weight1=1/2, weight2=1/2), prototype=list(weight1=1/2, weight2=1/2, loading=0),
contains="valuationTable" contains="valuationTable"
); );
...@@ -83,22 +83,38 @@ setMethod("getOmega", "valuationTable.period", ...@@ -83,22 +83,38 @@ setMethod("getOmega", "valuationTable.period",
function (object) { function (object) {
max(object@ages,na.rm=TRUE); max(object@ages,na.rm=TRUE);
}) })
setMethod("getOmega", "valuationTable.mixed",
function (object) {
getOmega(object@table1);
})
setMethod("getOmega", "valuationTable.joined",
function (object) {
getOmega(object@table1);
})
setGeneric("ages", function(object, ...) standardGeneric("ages")); setGeneric("ages", function(object, ...) standardGeneric("ages"));
setMethod("ages", "valuationTable.period", setMethod("ages", "valuationTable.period",
function (object, ...) { function (object, ...) {
object@ages; object@ages;
}) })
setMethod("ages", "valuationTable.mixed",
function (object, ...) {
ages(object@table1);
})
setMethod("ages", "valuationTable.joined",
function (object, ...) {
ages(object@table1);
})
setGeneric("deathProbabilities", function(object, ..., YOB=1975) standardGeneric("deathProbabilities")); setGeneric("deathProbabilities", function(object, ..., YOB=1975) standardGeneric("deathProbabilities"));
setMethod("deathProbabilities", "valuationTable.period", setMethod("deathProbabilities", "valuationTable.period",
function(object, ..., YOB=1975) { function(object, ..., YOB=1975) {
object@deathProbs; object@deathProbs * (1+object@loading);
}) })
setMethod("deathProbabilities","valuationTable.ageShift", setMethod("deathProbabilities","valuationTable.ageShift",
function (object, ..., YOB=1975) { function (object, ..., YOB=1975) {
qx=object@deathProbs; qx=object@deathProbs * (1+object@loading);
shift.index=match(YOB, object@shifts, 0); shift.index=match(YOB, object@shifts, 0);
if (shift.index) {} if (shift.index) {}
# TODO # TODO
...@@ -107,7 +123,7 @@ setMethod("deathProbabilities","valuationTable.ageShift", ...@@ -107,7 +123,7 @@ setMethod("deathProbabilities","valuationTable.ageShift",
setMethod("deathProbabilities","valuationTable.trendProjection", setMethod("deathProbabilities","valuationTable.trendProjection",
function (object, ..., YOB=1975) { function (object, ..., YOB=1975) {
cat("deathProbabilities for valuationTable.trendProjection, YOB=", YOB, "\n") cat("deathProbabilities for valuationTable.trendProjection, YOB=", YOB, "\n")
qx=object@deathProbs; qx=object@deathProbs * (1+object@loading);
if (is.null(object@trend2) || length(object@trend2)<=1) { if (is.null(object@trend2) || length(object@trend2)<=1) {
ages=0:(length(qx)-1); ages=0:(length(qx)-1);
damping=sapply(ages, function (age) { object@dampingFunction(YOB+age-object@baseYear) }); damping=sapply(ages, function (age) { object@dampingFunction(YOB+age-object@baseYear) });
...@@ -122,13 +138,13 @@ cat("deathProbabilities for valuationTable.trendProjection, YOB=", YOB, "\n") ...@@ -122,13 +138,13 @@ cat("deathProbabilities for valuationTable.trendProjection, YOB=", YOB, "\n")
# data.frame(x=0:121, qx=deathProbabilities(AVOe2005R.unisex, YOB=1948)); # data.frame(x=0:121, qx=deathProbabilities(AVOe2005R.unisex, YOB=1948));
setMethod("deathProbabilities","valuationTable.improvementFactors", setMethod("deathProbabilities","valuationTable.improvementFactors",
function (object, ..., YOB=1975) { function (object, ..., YOB=1975) {
qx=object@deathProbs; qx=object@deathProbs * (1+object@loading);
(1-object@improvement)^(YOB+0:(length(qx)-1)-object@baseYear)*qx (1-object@improvement)^(YOB+0:(length(qx)-1)-object@baseYear)*qx
}) })
setMethod("deathProbabilities","valuationTable.mixed", setMethod("deathProbabilities","valuationTable.mixed",
function (object, ..., YOB=1975) { function (object, ..., YOB=1975) {
qx1=deathProbabilities(object@table1, ..., YOB); qx1=deathProbabilities(object@table1, ..., YOB) * (1+object@loading);
qx2=deathProbabilities(object@table2, ..., YOB); qx2=deathProbabilities(object@table2, ..., YOB) * (1+object@loading);
(object@weight1*qx1 + object@weight2*qx2)/(object@weight1 + object@weight2) (object@weight1*qx1 + object@weight2*qx2)/(object@weight1 + object@weight2)
}) })
...@@ -136,12 +152,12 @@ setMethod("deathProbabilities","valuationTable.mixed", ...@@ -136,12 +152,12 @@ setMethod("deathProbabilities","valuationTable.mixed",
setGeneric("periodDeathProbabilities", function(object, ...) standardGeneric("periodDeathProbabilities")); setGeneric("periodDeathProbabilities", function(object, ...) standardGeneric("periodDeathProbabilities"));
setMethod("periodDeathProbabilities", "valuationTable.period", setMethod("periodDeathProbabilities", "valuationTable.period",
function(object, ...) { function(object, ...) {
object@deathProbs; object@deathProbs * (1+object@loading);
}) })
setMethod("periodDeathProbabilities","valuationTable.ageShift", setMethod("periodDeathProbabilities","valuationTable.ageShift",
function (object, ..., Period=1975) { function (object, ..., Period=1975) {
# TODO # TODO
qx=object@deathProbs; qx=object@deathProbs * (1+object@loading);
shift.index=match(YOB, object@shifts, 0); shift.index=match(YOB, object@shifts, 0);
if (shift.index) {} if (shift.index) {}
# TODO # TODO
...@@ -149,7 +165,7 @@ setMethod("periodDeathProbabilities","valuationTable.ageShift", ...@@ -149,7 +165,7 @@ setMethod("periodDeathProbabilities","valuationTable.ageShift",
}) })
setMethod("periodDeathProbabilities","valuationTable.trendProjection", setMethod("periodDeathProbabilities","valuationTable.trendProjection",
function (object, ..., Period=1975) { function (object, ..., Period=1975) {
qx=object@deathProbs; qx=object@deathProbs * (1+object@loading);
if (is.null(object@trend2) || length(object@trend2)<=1) { if (is.null(object@trend2) || length(object@trend2)<=1) {
ages=0:(length(qx)-1); ages=0:(length(qx)-1);
damping=object@dampingFunction(Period-object@baseYear); damping=object@dampingFunction(Period-object@baseYear);
...@@ -165,13 +181,13 @@ setMethod("periodDeathProbabilities","valuationTable.trendProjection", ...@@ -165,13 +181,13 @@ setMethod("periodDeathProbabilities","valuationTable.trendProjection",
# data.frame(x=0:121, qx=deathProbabilities(AVOe2005R.unisex, YOB=1948)); # data.frame(x=0:121, qx=deathProbabilities(AVOe2005R.unisex, YOB=1948));
setMethod("periodDeathProbabilities","valuationTable.improvementFactors", setMethod("periodDeathProbabilities","valuationTable.improvementFactors",
function (object, ..., Period=1975) { function (object, ..., Period=1975) {
qx=object@deathProbs; qx=object@deathProbs * (1+object@loading);
(1-object@improvement)^(Period-object@baseYear)*qx (1-object@improvement)^(Period-object@baseYear)*qx
}) })
setMethod("periodDeathProbabilities","valuationTable.mixed", setMethod("periodDeathProbabilities","valuationTable.mixed",
function (object, ..., Period=1975) { function (object, ..., Period=1975) {
qx1=periodDeathProbabilities(object@table1, ..., Period=Period); qx1=periodDeathProbabilities(object@table1, ..., Period=Period) * (1+object@loading);
qx2=periodDeathProbabilities(object@table2, ..., Period=Period); qx2=periodDeathProbabilities(object@table2, ..., Period=Period) * (1+object@loading);
(object@weight1*qx1 + object@weight2*qx2)/(object@weight1 + object@weight2) (object@weight1*qx1 + object@weight2*qx2)/(object@weight1 + object@weight2)
}) })
......
...@@ -13,13 +13,13 @@ library("gdata") ...@@ -13,13 +13,13 @@ library("gdata")
############################################################################### ###############################################################################
a.vz.dataM=read.xls( a.vz.dataM=read.xls(
"Tafeln/A_Volkszaehlungen.xls", "Tables/A_Volkszaehlungen.xls",
sheet="Austria_M", sheet="Austria_M",
skip=2, skip=2,
header=TRUE header=TRUE
) )
a.vz.dataF=read.xls( a.vz.dataF=read.xls(
"Tafeln/A_Volkszaehlungen.xls", "Tables/A_Volkszaehlungen.xls",
sheet="Austria_F", sheet="Austria_F",
skip=2, skip=2,
header=TRUE header=TRUE
...@@ -27,7 +27,7 @@ a.vz.dataF=read.xls( ...@@ -27,7 +27,7 @@ a.vz.dataF=read.xls(
censtable = function(data, name, qslot, baseYear=1900) { censtable = function(data, name, qslot, baseYear=1900) {
qx=data[names(data)==qslot]; qx=data[names(data)==qslot];
ix=complete.cases(qx); ix=complete.cases(qx);
valuationTable.period(name=name, ages=data$x[ix], deathProbs=qx[ix,], baseYear=baseYear) valuationTable.period(name=name, ages=data$x[ix], deathProbs=qx[ix,], baseYear=baseYear)
} }
mort.AT.census.1869.male = censtable(a.vz.dataM, name="ÖVSt 1868/71 M", baseYear=1869, qslot="X1868.71"); mort.AT.census.1869.male = censtable(a.vz.dataM, name="ÖVSt 1868/71 M", baseYear=1869, qslot="X1868.71");
...@@ -94,5 +94,5 @@ rm(a.vz.dataM, a.vz.dataF, censtable) ...@@ -94,5 +94,5 @@ rm(a.vz.dataM, a.vz.dataF, censtable)
############################################################################### ###############################################################################
plotValuationTables(mort.AT.census.ALL.male, title="Vergleich österreichische Sterbetafeln, Männer", legend.position=c(1,0)) plotValuationTables(mort.AT.census.ALL.male, title="Vergleich österreichische SterbeTables, Männer", legend.position=c(1,0))
plotValuationTables(mort.AT.census.ALL.female, title="Vergleich österreichische Sterbetafeln, Frauen", legend.position=c(1,0)) plotValuationTables(mort.AT.census.ALL.female, title="Vergleich österreichische SterbeTables, Frauen", legend.position=c(1,0))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment