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

Use the mortalityTable's @data$dim list/data.frame as further dimensions/factors in makeQxDataFrame

parent 09a0d925
Branches
Tags
No related merge requests found
...@@ -24,42 +24,45 @@ makeQxDataFrame = function(..., YOB = 1972, Period = NA, reference = NULL) { ...@@ -24,42 +24,45 @@ makeQxDataFrame = function(..., YOB = 1972, Period = NA, reference = NULL) {
data = unlist(list(...)); data = unlist(list(...));
if (is.null(data)) return(data.frame(x = double(), y = double(), group = character())) if (is.null(data)) return(data.frame(x = double(), y = double(), group = character()))
names(data) = lapply(data, function(t) t@name);
reference_ages = NULL; reference_ages = NULL;
if (missing(Period)) { if (missing(Period)) {
# If reference is given, normalize all probabilities by that table! # If reference is given, normalize all probabilities by that table!
if (!missing(reference)) { if (!missing(reference) && !is.null(reference)) {
reference_ages = ages(reference); reference_ages = ages(reference);
reference = deathProbabilities(reference, YOB = YOB); reference = deathProbabilities(reference, YOB = YOB);
} }
data = lapply(data, function(t) { data = lapply(data, function(t) {
normalize_deathProbabilities( normalize_deathProbabilities(
cbind(x = ages(t), y = `names<-`(deathProbabilities(t, YOB = YOB), NULL)), if (is.data.frame(t@data$dim) || is.list(t@data$dim)) {
cbind(x = ages(t), y = `names<-`(deathProbabilities(t, YOB = YOB), NULL), group = t@name, as.data.frame(t@data$dim))
} else {
cbind(x = ages(t), y = `names<-`(deathProbabilities(t, YOB = YOB), NULL), group = t@name)
},
reference = reference, reference = reference,
referenceAges = reference_ages) referenceAges = reference_ages)
}); });
} else { } else {
if (!missing(reference)) { if (!missing(reference) && !is.null(reference)) {
reference_ages = ages(reference); reference_ages = ages(reference);
reference = periodDeathProbabilities(reference, Period = Period); reference = periodDeathProbabilities(reference, Period = Period);
} }
data = lapply(data, function(t) { data = lapply(data, function(t) {
normalize_deathProbabilities( normalize_deathProbabilities(
cbind(x = ages(t), y = periodDeathProbabilities(t, Period = Period)), ifelse(is.data.frame(t@data$dim) || is.list(t@data$dim),
cbind(x = ages(t), y = `names<-`(periodDeathProbabilities(t, Period = Period), NULL), group = t@name, as.data.frame(t@data$dim)),
cbind(x = ages(t), y = `names<-`(periodDeathProbabilities(t, Period = Period), NULL), group = t@name)
),
reference = reference, reference = reference,
referenceAges = reference_ages) referenceAges = reference_ages)
}); });
} }
list.names = names(data)
lns <- sapply(data, nrow)
data <- as.data.frame(do.call("rbind", data)) data <- as.data.frame(do.call("rbind", data))
data$group <- rep(list.names, lns)
data data
} }
normalize_deathProbabilities = function (data, reference = NULL, referenceAges = NULL) { normalize_deathProbabilities = function(data, reference = NULL, referenceAges = NULL) {
if (missing(reference) || missing(referenceAges) || is.null(reference) || is.null(referenceAges)) { if (missing(reference) || missing(referenceAges) || is.null(reference) || is.null(referenceAges)) {
return(data); return(data);
} }
...@@ -71,7 +74,7 @@ normalize_deathProbabilities = function (data, reference = NULL, referenceAges = ...@@ -71,7 +74,7 @@ normalize_deathProbabilities = function (data, reference = NULL, referenceAges =
# Find which ages in data do NOT exist in the reference ages (and are thus NOT normalized at all) # Find which ages in data do NOT exist in the reference ages (and are thus NOT normalized at all)
# Print a warning! # Print a warning!
missingrefs = setdiff(data[,"x"], referenceAges) missingrefs = setdiff(data[,"x"], referenceAges)
if (length(missingrefs)>0) { if (length(missingrefs) > 0) {
warning("Reference mortality table does not contain ages ", warning("Reference mortality table does not contain ages ",
missingrefs, missingrefs,
" required for normalization. These ages will not be normalized!") " required for normalization. These ages will not be normalized!")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment