Description Usage Arguments See Also Examples
View source: R/do_BackTesting.R
Perform Out-of-Sample Testing of Mortality Forecasts Over One Time Period
| 1 2 3 | 
| data | A data.frame or a matrix containing mortality data 
with ages  | 
| data.B | A data.frame or a matrix containing mortality data for 
the benchmark population. This dataset is needed only in the coherent 
mortality models (e.g.  | 
| x | Numerical vector indicating the ages in input  | 
| y.fit | Years to be considered in fitting. | 
| y.for | Years to be forecast. | 
| data.in | Specify the type of input  | 
| models | One or several mortality models to be estimated. The following options are available: 
 | 
| level | Significance level of the confidence interval. | 
| jumpchoice | Method used for computation of jumpchoice. 
Possibilities:  | 
| verbose | A logical value. Set  | 
| ... | Arguments to be passed to or from other methods. | 
do.BBackTesting
evalAccuracy.BackTesting
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | library(magrittr)
x  <- 0:100            # Ages
y1 <- 1980:2000        # Training period
y2 <- 2001:2016        # Validation period
y  <- c(y1, y2)        # entire period
K <- "GBRTENW"         # Country to be analysed
# Mortality data for country: K
mx.country <- HMD_male$mx[[K]][paste(x), paste(y)] %>% replace.zeros
# Create a mortality data for a benchmark population
# to be used in "LiLee" and "OeppenC" models
average_mx <- function(w) prod(w, na.rm = TRUE)^(1/(length(w)))
Mx <- HMD_male$mx %>% 
  lapply(as.matrix) %>% 
  lapply(replace.zeros) %>% 
  simplify2array() %>% 
  apply(., 1:2, FUN = average_mx) %>% 
  as.data.frame()
mx.benchmark <- Mx[paste(x), paste(y)]
# Select various mortality models
MM <- c("MRWD", "LeeCarter", "LiLee", "HyndmanUllah", 
        "Oeppen", "OeppenC", "MEM5", "MEM6")
# Fit & Forecast the models 
B <- do.BackTesting(data = mx.country,
                    data.B = mx.benchmark,
                    x = x,
                    y.fit = y1, 
                    y.for = y2,
                    data.in = "mx",
                    models = MM)
# Compute accuracy measures for resulted life exectancies
A <- evalAccuracy(B, data.out = "ex")
A
# Rank the model's performance.
R <- do.Ranking(A)
R
R[, c(1:3, 20)]
# Visualize the results
plot(B, data.out = "ex", facet = "x", 
     which = c(0, 20, 40, 60, 75, 90))
plot(B, data.out = "mx", facet = "y", which = 2016) 
 | 
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.