#' Precentage Better(MAE) for the given data from dataset(PB_MAEs)
#'
#' This function calculates and returns list of two dataframes,
#' where the first data frame contains PB_MAEs for the given data, diferent horizons and methods,
#' the second one contains ranked list of the methods according to PB_MAEs.
#' Also the function plots PB_MAEs for different hirizons and methods.
#'
#' @aliases calculatePB_MAEs
#' @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 "series_id" and "value").
#' @param sort logical. If TRUE the resulting list of MSEs dataframe and ranked dataframe of MSEs sorting by average value.
#' @return \code{calculatePB_MAEs} function calculates and returns list of two dataframes,
#' where the first data frame contains PB_MAEs for the given data, diferent horizons and methods,
#' the second one contains ranked dataframe of the methods according to PB_MAEs.
#' Also the function plots PB_MAEs 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{calculateMASEs}}, \code{\link{calculateMdAPEs}},
#' \code{\link{calculateMPEs}}, \code{\link{calculateMSEs}}, \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}.
#' @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}.
#' @keywords dataframe
#' @examples
#' calculatePB_MAEs(frame = FORAYearForecast, frame2 = FORAYearSeries)
#' calculatePB_MAEs(frame = FORAYearForecast, frame2 = FORAYearSeries, sort = TRUE)
#'
#' @export
calculatePB_MAEs <- function(frame, frame2, sort = FALSE){
out <-matrix(NA, nrow = length(unique(frame$method)), ncol = length(unique(frame$horizon)))
methodlist <- list()
horizonlist <- list()
PB_MAElist <- list()
PB_MAE <- 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()
ni <- c()
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] <- 100* mean(abs(subset(df, method == i & horizon == j)$actual - subset(df, method == i & horizon == j)$forecast) < subset(df, method == i & horizon == j)$` NAIVE_MAE `)
}
}
for (k in 1:length(unique(frame$horizon))){
ranks[,k] <- rank(df3[, k])
}
averagerank <- rowMeans(ranks, na.rm =TRUE)
averagePB <- rowMeans(df3, na.rm =TRUE)
ranks <- cbind(ranks, "average rank" = averagerank)
df3 <- cbind(df3, " average PB_MAE" = averagePB)
for(m in 1:length(unique(frame$method))){
PB_MAElist[[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))
}
PB_MAE1 <- Reduce(c, PB_MAElist)
PB_MAE <- Reduce(c, PB_MAE1)
horizon <- Reduce(c, horizonlist)
method = Reduce(c, methodlist)
df4 <- data.frame(PB_MAE, horizon, method )
# plot PB_MAEs frame
gp1 <- ggplot2::ggplot(df4, ggplot2::aes(x=horizon, y= PB_MAE, group=method,color=method, shape=method))+
ggplot2::scale_shape_manual(values=1:nlevels(df4$method)) +
ggplot2::labs(title = "PB_MAE 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("PB_MAE" = df3,"rank" =ranks)
if(sort == FALSE){
return(outlist)
}else{
frame1 <-df3[order(df3$` average PB_MAE`),]
frame11 <- ranks[order(ranks$`average rank`),]
outlist <- list("PB_MAE" = frame1,"rank" = frame11)
return(outlist)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.