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

Allow list of mortalityTables objects in the mT.* utility functions

parent fc41147e
No related branches found
No related tags found
No related merge requests found
...@@ -76,8 +76,11 @@ fitExpExtrapolation = function(data, idx, up = TRUE, verbose = FALSE) { ...@@ -76,8 +76,11 @@ fitExpExtrapolation = function(data, idx, up = TRUE, verbose = FALSE) {
#' @export #' @export
mT.setName = function(table, name = table@name) { mT.setName = function(table, name = table@name) {
if (is.list(table)) {
return(lapply(table, mT.setName, name = name))
}
if (!is(table, "mortalityTable")) if (!is(table, "mortalityTable"))
stop("First argument must be a mortalityTable.") stop("First argument must be a mortalityTable or a list of mortalityTable objects.")
table@name = name table@name = name
table table
...@@ -87,8 +90,11 @@ mT.setName = function(table, name = table@name) { ...@@ -87,8 +90,11 @@ mT.setName = function(table, name = table@name) {
#' @export #' @export
mT.fillAges = function(table, neededAges, fill = 0) { mT.fillAges = function(table, neededAges, fill = 0) {
if (is.list(table)) {
return(lapply(table, mT.fillAges, neededAges = neededAges, fill = fill))
}
if (!is(table, "mortalityTable")) if (!is(table, "mortalityTable"))
stop("First argument must be a mortalityTable.") stop("First argument must be a mortalityTable or a list of mortalityTable objects.")
existingAges = ages(table) existingAges = ages(table)
if (.hasSlot(table, "ages")) if (.hasSlot(table, "ages"))
...@@ -110,6 +116,12 @@ mT.fillAges = function(table, neededAges, fill = 0) { ...@@ -110,6 +116,12 @@ mT.fillAges = function(table, neededAges, fill = 0) {
#' @export #' @export
mT.scaleProbs = function(table, factor = 1.0, name.postfix = "scaled", name = paste(table@name, name.postfix)) { mT.scaleProbs = function(table, factor = 1.0, name.postfix = "scaled", name = paste(table@name, name.postfix)) {
if (is.list(table)) {
return(lapply(table, mT.scaleProbs, factor = factor, name.postfix = name.postfix, name = name))
}
if (!is(table, "mortalityTable"))
stop("First argument must be a mortalityTable or a list of mortalityTable objects.")
table@deathProbs = factor * table@deathProbs table@deathProbs = factor * table@deathProbs
if (!is.null(name)) { if (!is.null(name)) {
table@name = name table@name = name
...@@ -120,8 +132,11 @@ mT.scaleProbs = function(table, factor = 1.0, name.postfix = "scaled", name = pa ...@@ -120,8 +132,11 @@ mT.scaleProbs = function(table, factor = 1.0, name.postfix = "scaled", name = pa
#' @export #' @export
mT.setTrend = function(table, trend, trendages = ages(table), baseYear = table@baseYear, dampingFunction = identity) { mT.setTrend = function(table, trend, trendages = ages(table), baseYear = table@baseYear, dampingFunction = identity) {
if (is.list(table)) {
return(lapply(table, mT.setTrend, trend = trend, trendages = trendages, baseYear = baseYear, dampingFunction = dampingFunction))
}
if (!is(table, "mortalityTable")) if (!is(table, "mortalityTable"))
stop("First argument must be a mortalityTable.") stop("First argument must be a mortalityTable or a list of mortalityTable objects.")
t = mortalityTable.trendProjection( t = mortalityTable.trendProjection(
table, table,
...@@ -139,8 +154,11 @@ mT.addTrend = mT.setTrend ...@@ -139,8 +154,11 @@ mT.addTrend = mT.setTrend
#' @export #' @export
mT.extrapolateTrendExp = function(table, idx, up = TRUE) { mT.extrapolateTrendExp = function(table, idx, up = TRUE) {
if (is.list(table)) {
return(lapply(table, mT.extrapolateTrendExp, idx = idx, up = up))
}
if (!is(table, "mortalityTable")) if (!is(table, "mortalityTable"))
stop("First argument must be a mortalityTable.") stop("First argument must be a mortalityTable or a list of mortalityTable objects.")
if (.hasSlot(table, "trend") && !is.null(table@trend)) if (.hasSlot(table, "trend") && !is.null(table@trend))
table@trend = fitExpExtrapolation(table@trend, idx = idx,up = up) table@trend = fitExpExtrapolation(table@trend, idx = idx,up = up)
...@@ -152,8 +170,11 @@ mT.extrapolateTrendExp = function(table, idx, up = TRUE) { ...@@ -152,8 +170,11 @@ mT.extrapolateTrendExp = function(table, idx, up = TRUE) {
#' @export #' @export
mT.translate = function(table, baseYear, name = table@name) { mT.translate = function(table, baseYear, name = table@name) {
if (is.list(table)) {
return(lapply(table, mT.translate, baseYear = baseYear, name = name))
}
if (!is(table, "mortalityTable")) if (!is(table, "mortalityTable"))
stop("First argument must be a mortalityTable.") stop("First argument must be a mortalityTable or a list of mortalityTable objects.")
table@deathProbs = periodDeathProbabilities(table, Period = baseYear) table@deathProbs = periodDeathProbabilities(table, Period = baseYear)
table@baseYear = baseYear table@baseYear = baseYear
...@@ -164,8 +185,11 @@ mT.translate = function(table, baseYear, name = table@name) { ...@@ -164,8 +185,11 @@ mT.translate = function(table, baseYear, name = table@name) {
#' @export #' @export
mT.extrapolateProbsExp = function(table, age, up = TRUE) { mT.extrapolateProbsExp = function(table, age, up = TRUE) {
if (is.list(table)) {
return(lapply(table, mT.extrapolateProbsExp, age = age, up = up))
}
if (!is(table, "mortalityTable")) if (!is(table, "mortalityTable"))
stop("First argument must be a mortalityTable.") stop("First argument must be a mortalityTable or a list of mortalityTable objects.")
if (.hasSlot(table, "deathProbs")) { if (.hasSlot(table, "deathProbs")) {
idx = match(age, ages(table)) idx = match(age, ages(table))
...@@ -222,8 +246,11 @@ mT.fitExtrapolationLaw = function(table, method = "LF2", law = "HP", ...@@ -222,8 +246,11 @@ mT.fitExtrapolationLaw = function(table, method = "LF2", law = "HP",
#' @export #' @export
mT.setDimInfo = function(table, ..., append = TRUE) { mT.setDimInfo = function(table, ..., append = TRUE) {
if (is.list(table)) {
return(lapply(table, mT.setDimInfo, ..., append = append))
}
if (!is(table, "mortalityTable")) if (!is(table, "mortalityTable"))
stop("First argument must be a mortalityTable.") stop("First argument must be a mortalityTable or a list of mortalityTable objects.")
if (append) { if (append) {
table@data$dim[names(list(...))] = list(...) table@data$dim[names(list(...))] = list(...)
......
...@@ -67,8 +67,11 @@ ...@@ -67,8 +67,11 @@
#' @import scales #' @import scales
#' @export #' @export
whittaker.mortalityTable = function(table, lambda = 10, d = 2, name.postfix = ", smoothed", ..., weights = NULL, log = TRUE) { whittaker.mortalityTable = function(table, lambda = 10, d = 2, name.postfix = ", smoothed", ..., weights = NULL, log = TRUE) {
if (is.list(table)) {
return(lapply(table, whittaker.mortalityTable, lambda = lambda, d = d, name.postfix = name.postfix, ..., weights = weights, log = log))
}
if (!is(table, "mortalityTable")) { if (!is(table, "mortalityTable")) {
stop("Table object must be an instance of mortalityTable in whittaker.mortalityTable.") stop("Table object must be an instance (or list of instances) of mortalityTable in whittaker.mortalityTable.")
} }
# append the postfix to the table name to distinguish it from the original (raw) table # append the postfix to the table name to distinguish it from the original (raw) table
if (!is.null(name.postfix)) { if (!is.null(name.postfix)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment