From 80fe2b685745b9fbb519a80dfe28a3fe38f2e019 Mon Sep 17 00:00:00 2001
From: Reinhold Kainhofer <reinhold@kainhofer.com>
Date: Wed, 1 Aug 2018 01:55:23 +0200
Subject: [PATCH] New function: mT.switchover - transition from one table to
 another either at fixed age or by the given weights

---
 R/utilityFunctions.R | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/R/utilityFunctions.R b/R/utilityFunctions.R
index 940eae5..6269857 100644
--- a/R/utilityFunctions.R
+++ b/R/utilityFunctions.R
@@ -334,6 +334,28 @@ pT.getSubTable = function(table, subtable = "qx") {
         NULL
 }
 
+#' @export
+mT.switchover = function(table, to, at, weights = NULL) {
+    if (is.array(table)) {
+        return(array(
+            lapply(table, mT.switchover, to = to, at = at, weights = weights),
+            dim = dim(table), dimnames = dimnames(table))
+        )
+    } else if (is.list(table)) {
+        return(lapply(table, mT.switchover, to = to, at = at, weights = weights))
+    }
+    if (!is(table, "mortalityTable"))
+        stop("First argument must be a mortalityTable or a list of mortalityTable objects.")
+
+    if (is.null(weights)) {
+        ags.table = ages(table)
+        ags.to = ages(to)
+        weights = 1 * (ags.to >= at)
+    }
+    table@deathProbs = table@deathProbs * (1 - weights) + to@deathProbs * weights
+    table
+}
+
 
 
 #' @exportMethod mT.round
-- 
GitLab