View source: R/MeasureAccuracy.R
MeasureAccuracy | R Documentation |
R function to estimate different measures of accuracy.
the sum of squared errors (SSE) for the mortality rates:
\sum_{x}^{} \sum_{t} \left( qxt1 - qxt2 \right)^{2}
where qxt1 is the real mortality rates qxt_crude
, and qxt2 is the adjusted mortality rates qxt_aju
.
The mean squared errors (MSE) for the mortality rates:
\frac{1}{n}\sum_{x} \sum_{t} \left( qxt1 - qxt2 \right)^2 = \frac{1}{n} SSE
where qxt1 is the real mortality rates qxt_crude
, and qxt2 is the adjusted mortality rates qxt_aju
.
The mean absolute errors (MAE) for the mortality rates:
\frac{1}{n}\sum_{x} \sum_{t} \left| qx1 - qxt2 \right|
.
where qxt1 is the real mortality rates qxt_crude
, and qxt2 is the adjusted mortality rates qxt_aju
.
The mean absolute percentage error (MAPE) for the mortality rates:
\frac{1}{n}\sum_{x} \sum_{t}\left| \frac{\left(qxt1 - qxt2\right) }{qxt2} \right|
where qxt1 is the real mortality rates qxt_crude
, and qxt2 is the adjusted mortality rates qxt_aju
.
You only have to provide the real value, the fitted or forecasted value for your mortality rates and the measure of accuracy chosen.
However, the function is constructed to provide the real value and the fitted or forecasted value of your independent variable.
These variables must have the same dimensions to be compared.
MeasureAccuracy(
measure = c("SSE", "MSE", "MAE", "MAPE", "All"),
qxt_crude,
qxt_aju,
wxt
)
measure |
choose the non-penalized measure of accuracy that you want to use; c(" |
qxt_crude |
corresponds to the crude mortality rates. These crude rate are directly obtained by dividing the number of registered deaths by the number of those initially exposed to the risk for age x, period t and in each region i. |
qxt_aju |
adjusted mortality rates using a specific mode. |
wxt |
weights of the mortality rates or data provided. |
An object with class "MoA"
including the value of the measure of accuracy for the data provided.
Atance, D., Debón, A., & Navarro, E. (2020). A comparison of forecasting mortality models using resampling methods. Mathematics, 8(9), 1550.
fitLCmulti
, forecast.fitLCmulti
,
multipopulation_cv
.
#The example takes more than 5 seconds because it includes
#several fitting and forecasting process and hence all
#the process is included in donttest
#To show how the function works, we need to provide fitted or forecasted data and the real data.
#In this case, we employ the following data of the library:
SpainRegions
library(gnm)
library(forecast)
ages <- c(0, 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90)
#In this case, we fit for males providing the lxt
multiplicative_Spainmales <- fitLCmulti(model = "multiplicative",
qxt = SpainRegions$qx_male,
periods = c(1991:2020),
ages = c(ages),
nPop = 18,
lxt = SpainRegions$lx_male)
multiplicative_Spainmales
plot(multiplicative_Spainmales)
#Once, we have the fitted data, we will obtain different measures of accuracy
#for the first population.
#We need to obtain wxt (weight of the mortality rates or data provided) using a
library(StMoMo)
wxt_1pop <- genWeightMat(ages = ages, years = c(1991:2020), clip = 0)
##########################
#SSE#
##########################
SSE_multSpmales <- MeasureAccuracy(measure = "SSE",
qxt_crude = multiplicative_Spainmales$qxt.crude$pop1,
qxt_aju = multiplicative_Spainmales$qxt.fitted$pop1,
wxt = wxt_1pop)
SSE_multSpmales
##########################
#MSE#
##########################
MSE_multSpmales <- MeasureAccuracy(measure = "MSE",
qxt_crude = multiplicative_Spainmales$qxt.crude$pop1,
qxt_aju = multiplicative_Spainmales$qxt.fitted$pop1,
wxt = wxt_1pop)
MSE_multSpmales
##########################
#MAE#
##########################
MAE_multSpmales <- MeasureAccuracy(measure = "MSE",
qxt_crude = multiplicative_Spainmales$qxt.crude$pop1,
qxt_aju = multiplicative_Spainmales$qxt.fitted$pop1,
wxt = wxt_1pop)
MAE_multSpmales
##########################
#MAPE#
##########################
MAPE_multSpmales <- MeasureAccuracy(measure = "MSE",
qxt_crude = multiplicative_Spainmales$qxt.crude$pop1,
qxt_aju = multiplicative_Spainmales$qxt.fitted$pop1,
wxt = wxt_1pop)
MAPE_multSpmales
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.