#' Mean Absolute Scaled Error(MASEs)
#'
#' This function calculates and returns list of two dataframes,
#' where the first data frame contains MASEs for the given data, diferent horizons and methods,
#' the second one contains ranked list of the methods according to MASEs.
#' Also the function plots MASEs for different hirizons and methods.
#'
#' @aliases calculateMASEs
#' @param frame A data frame containing columns "seies_id, actual", "forecast", "method", and "horizon".
#' @param frame2 A data frame containing observations of each time series (containing columns named "series_id" and "value").
#' @param sort logical. If TRUE the resulting list of MASEs dataframe and ranked dataframe of MASEs sorting by average value.
#' @return \code{calculateMASEs} function calculates and returns list of two dataframes,
#' where the first data frame contains MASEs for the given data, diferent horizons and methods,
#' the second one contains ranked dataframe of the methods according to MASEs.
#' Also the function plots MASEs for different hirizons and methods.
#' @author Sai Van Cuong, Maixm Shcherbakov and Andrey Davydenko
#' @seealso \code{\link{calculateAvgRelMAEs}}, \code{\link{calculateGMAPEs}}, \code{\link{calculateGMRAEs}}, \code{\link{calculateMAD_MEAN_ratio}},
#' \code{\link{calculateMAEs}}, \code{\link{calculateMAPEs}}, \code{\link{calculateMdAPEs}}, \code{\link{calculateMPEs}}, \code{\link{calculateMSEs}},
#' \code{\link{calculatePB_MAEs}}, \code{\link{calculateRMSEs}}, \code{\link{calculateSMAPEs}}, \code{\link{calculateSMdAPEs}}.
#' @references Andrey Davydenko, Robert Fildes (2015) Volume title: \emph{Forecast Error Measures: Critical Review and Practical Recommendations}. \url{https://www.researchgate.net/publication/284947381_Forecast_Error_Measures_Critical_Review_and_Practical_Recommendations}.
#' @references Chao Chen, Jamie Twycross, Jonathan M. Garibaldi (2017) Volume title: \emph{A new accuracy measure based on bounded relative error for time series forecasting}. \url{http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0174202}.
#' @references MV Shcherbakov, A Brebels, NL Shcherbakova (2013) Volume title: \emph{Information Technologies in Modern Industry, Education & Society}. \url{https://www.researchgate.net/publication/281718517_A_survey_of_forecast_error_measures}.
#' \url{http://eva.fcea.edu.uy/pluginfile.php/109034/mod_resource/content/0/2006_Hyndman_Predicc.pdf}.
#' @keywords dataframe
#' @examples
#' calculateMASEs(frame = FORAYearForecast, frame2 = FORAYearSeries)
#' calculateMASEs(frame = FORAYearForecast, frame2 = FORAYearSeries, sort = TRUE)
#'
#' @export
calculateMASEs <- function(frame, frame2, sort = FALSE){
out <-matrix(NA, nrow = length(unique(frame$method)), ncol = length(unique(frame$horizon)))
methodlist <- list()
horizonlist <- list()
MASElist <- list()
MASE <- c()
df3 = data.frame(out)
colnames(df3) <- paste("horizon = ", 1:length(unique(frame$horizon)), sep ="")
rownames(df3) <- unique(frame$method)
ranks = data.frame(out)
colnames(ranks) <- paste("horizon = ", 1:length(unique(frame$horizon)), sep ="")
rownames(ranks) <- unique(frame$method)
outlist <- list()
df2 <- list()
NAIVE_MAE <- c()
df <- list()
df2 <- list()
for(k in unique(frame2$series_id)){
df[[k]] <- subset(frame, series_id == k)
df2 <- subset(frame2, series_id ==k)
NAIVE_MAE[k] <- mean(abs(diff(df2$value)))
df[[k]] <- cbind(df[[k]], " NAIVE_MAE " = rep(NAIVE_MAE[k], length(unique(frame$horizon))*length(unique(frame$method))))
}
df = do.call(rbind, df)
for(j in as.vector(unique(df$horizon))){
for(i in as.vector(unique(df$method))){
df3[i, j] <- mean(abs((subset(df, method == i & horizon == j)$actual - subset(df, method == i & horizon == j)$forecast)/subset(df, method == i & horizon == j)$` NAIVE_MAE `), na.rm=TRUE)
}
}
for (k in 1:length(unique(frame$horizon))){
ranks[,k] <- rank(df3[, k])
}
averagerank <- rowMeans(ranks, na.rm =TRUE)
averageMASE <- rowMeans(df3, na.rm =TRUE)
ranks <- cbind(ranks, "average rank" = averagerank)
df3 <- cbind(df3, " average MASE" = averageMASE)
for(m in 1:length(unique(frame$method))){
MASElist[[m]] <- unname(df3[m, 1:length(unique(frame$horizon))])
methodlist[[m]] <- rep(as.vector(unique(frame$method))[m],length(unique(frame$horizon)))
horizonlist[[m]]<- as.vector(unique(frame$horizon))
}
MASE1 <- Reduce(c, MASElist)
MASE <- Reduce(c, MASE1)
horizon <- Reduce(c, horizonlist)
method = Reduce(c, methodlist)
df4 <- data.frame(MASE, horizon, method )
# plots MASEs frame
gp1 <- ggplot2::ggplot(df4, ggplot2::aes(x=horizon, y=MASE, group=method,color=method, shape=method))+
ggplot2::scale_shape_manual(values=1:nlevels(df4$method)) +
ggplot2::labs(title = "MASE for different horizons and methods") +
ggplot2::geom_line() +
ggplot2::geom_point(size=3)+
ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5))
print(gp1)
outlist <- list("MASE" = df3,"rank" =ranks)
if(sort == FALSE){
return(outlist)
}else{
frame1 <-df3[order(df3$` average MASE`),]
frame2 <- ranks[order(ranks$`average rank`),]
outlist <- list("MASE" = frame1,"rank" = frame2)
return(outlist)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.