#The example takes more than 5 seconds because they include
#several cross-validation methods and hence all the processes are included in "donttest".
# \donttest{
#We present a cross-validation method for spanish male regions using:
ages <- c(0, 1, 5, 10, 15, 20, 25, 30, 35, 40,
45, 50, 55, 60, 65, 70, 75, 80, 85, 90)
library(gnm)
library(forecast)
library(StMoMo)
#1. FIXED-ORIGIN -- using the ACFM nahead + trainset1 = periods;
#fixed_train_origin = TRUE (defualt value)
ho_Spainmales_addit <- multipopulation_cv(qxt = SpainRegions$qx_male,
model = c("ACFM"),
periods = c(1991:2020), ages = c(ages),
nPop = 18, lxt = SpainRegions$lx_male,
nahead = 5,
trainset1 = 25,
ktmethod = c("arimapdq"),
measures = c("SSE"))
ho_Spainmales_addit
#Once, we have run the function we can check the result in different ways:
ho_Spainmales_addit$meas_ages
ho_Spainmales_addit$meas_periodsfut
ho_Spainmales_addit$meas_pop
ho_Spainmales_addit$meas_total
#2. Let's continue with a RO-recalibration,
#(fixed_train_origin = TRUE (defualt value))
#where we have implemented three main CV techniques:
#2.1. Leave-One-Out-Cross-Validation (LOOCV) RO-recalibration when nahead = 1;
#(independently the number of periods blocked for the first train set; trainset1"
loocv_Spainmales_addit <- multipopulation_cv(qxt = SpainRegions$qx_male,
model = c("additive"),
periods = c(1991:2020), ages = c(ages),
nPop = 18, lxt = SpainRegions$lx_male,
nahead = 1, trainset1 = 10,
ktmethod = c("arimapdq"),
measures = c("SSE"))
loocv_Spainmales_addit
#Once, we have run the function we can check the result in different ways:
loocv_Spainmales_addit$meas_ages
loocv_Spainmales_addit$meas_periodsfut
loocv_Spainmales_addit$meas_pop
loocv_Spainmales_addit$meas_total
#2.2. K-Fold-CV RO-recalibration when nahead = trainset1
kfoldcv_Spainmales_addit <- multipopulation_cv(qxt = SpainRegions$qx_male,
model = c("additive"),
periods = c(1991:2020), ages = c(ages),
nPop = 18, lxt = SpainRegions$lx_male,
nahead = 5, trainset1 = 5,
ktmethod = c("arimapdq"),
measures = c("SSE"))
kfoldcv_Spainmales_addit
#Once, we have run the function we can check the result in different ways:
kfoldcv_Spainmales_addit$meas_ages
kfoldcv_Spainmales_addit$meas_periodsfut
kfoldcv_Spainmales_addit$meas_pop
kfoldcv_Spainmales_addit$meas_total
#2.3. standard time-series CV
cv_Spainmales_addit <- multipopulation_cv(qxt = SpainRegions$qx_male,
model = c("additive"),
periods = c(1991:2020), ages = c(ages),
nPop = 18, lxt = SpainRegions$lx_male,
nahead = 5, trainset1 = 10,
fixed_train_origin = TRUE,
ktmethod = c("arimapdq"),
measures = c("SSE"))
cv_Spainmales_addit
#Once, we have run the function we can check the result in different ways:
cv_Spainmales_addit$meas_ages
cv_Spainmales_addit$meas_periodsfut
cv_Spainmales_addit$meas_pop
cv_Spainmales_addit$meas_total
#3. RW-evaluation (fixed_train_origin = c(FALSE, "add_remove1"))
#3.1. fixed_train_origin = TRUE (The default value)
#In this case, the previous processes (Fixed-Origin or RO-recalibration)
#3.2. fixed_train_origin = FALSE
#where the origin in the training set is moved "nahead" period ahead in every iteration.
#This process allows to test the forecasting accuracy of "nahead" periods ahead
#keeping constant the size of the training and test set. As an example, we present
#three methods
#3.2.1. LOOCV
loocv_Spainmales_addit_rw <- multipopulation_cv(qxt = SpainRegions$qx_male,
model = c("additive"),
periods = c(1991:2020), ages = c(ages),
nPop = 18, lxt = SpainRegions$lx_male,
nahead = 1, trainset1 = 10,
fixed_train_origin = FALSE,
ktmethod = c("arimapdq"),
measures = c("SSE"))
loocv_Spainmales_addit_rw
#Once, we have run the function we can check the result in different ways:
loocv_Spainmales_addit_rw$meas_ages
loocv_Spainmales_addit_rw$meas_periodsfut
loocv_Spainmales_addit_rw$meas_pop
loocv_Spainmales_addit_rw$meas_total
#3.2.2. K-Fold-CV
kfoldcv_Spainmales_addit_rw <- multipopulation_cv(qxt = SpainRegions$qx_male,
model = c("additive"),
periods = c(1991:2020), ages = c(ages),
nPop = 18, lxt = SpainRegions$lx_male,
nahead = 5, trainset1 = 5,
fixed_train_origin = FALSE,
ktmethod = c("arimapdq"),
measures = c("SSE"))
kfoldcv_Spainmales_addit_rw
#Once, we have run the function we can check the result in different ways:
kfoldcv_Spainmales_addit$meas_ages
kfoldcv_Spainmales_addit$meas_periodsfut
kfoldcv_Spainmales_addit$meas_pop
kfoldcv_Spainmales_addit$meas_total
#3.2.3. standard time-series CV
cv_Spainmales_addit_rw <- multipopulation_cv(qxt = SpainRegions$qx_male,
model = c("additive"),
periods = c(1991:2020), ages = c(ages),
nPop = 18, lxt = SpainRegions$lx_male,
nahead = 5, trainset1 = 10,
fixed_train_origin = FALSE,
ktmethod = c("arimapdq"),
measures = c("SSE"))
cv_Spainmales_addit_rw
#Once, we have run the function we can check the result in different ways:
cv_Spainmales_addit_rw$meas_ages
cv_Spainmales_addit_rw$meas_periodsfut
cv_Spainmales_addit_rw$meas_pop
cv_Spainmales_addit_rw$meas_total
#3.3 RW-evaluation (fixed_train_origin = c("add_remove1"))
#where the origin in the training set is moved 1 period ahead in every iteration,
#adding the next period and removing the last period in the trainset.
#This process allows to test the forecasting accuracy of "nahead" periods ahead
#modifying the origin in the training set by 1.
#When "nahead" = 1 --- we will have a loocv equally as in the previous process,
#while using a different value of 1 for "nahead" we will test the forecasting
#accuracy of the model in "nahead" periods:
cv_Spainmales_addit_rw1 <- multipopulation_cv(qxt = SpainRegions$qx_male,
model = c("additive"),
periods = c(1991:2020), ages = c(ages),
nPop = 18, lxt = SpainRegions$lx_male,
nahead = 5, trainset1 = 15,
fixed_train_origin = "add_remove1",
ktmethod = c("arimapdq"),
measures = c("SSE"))
cv_Spainmales_addit_rw1
#Once, we have run the function we can check the result in different ways:
cv_Spainmales_addit_rw1$meas_ages
cv_Spainmales_addit_rw1$meas_periodsfut
cv_Spainmales_addit_rw1$meas_pop
cv_Spainmales_addit_rw1$meas_total
# }
Run the code above in your browser using DataLab