R/boxplotAPEs.R

#' Boxplot of absolute percentage errors for the given data from dataset
#'
#' This function returns box and whisker plots of absolute percentage errors (APE) for
#' the given data from dataset for diferent methods
#'
#'
#' @aliases boxplotAPEs
#' @param df A data frame containing columns "actual", "forecast", and "method".
#' @param useLogs logical. If useLogs==TRUE the resulting box and whisker plotted in logarithmic scales.
#' If FALSE the resulting box and whisker plotted in scales by default.
#' @param notch  logical. If the notch==TRUE, a notch is drawn in each side of the boxes.
#' @param limits The minimum and maximum values on the axis APE. If xlim is NULL, minimum and maximum values are calculated based on given data.
#' @param showOutliers If showOutliers is NA, the outliers are not drawn.
#' @param ... Other undocumented arguments.
#' @return \code{boxplotAPEs} function returns box and whisker plot of percentage errors for the given data
#' from dataset containing columns named "actual", "forecast" and "method" for different methods.
#' @author Andrey Davydenko, Maxim shcherbokov and Sai Van Cuong
#' @seealso \code{\link{boxplotPEs}}, \code{\link{plotPRD}}.
#' @references Rob J. Hyndman, Anne B. Koehler (2006) Volume title: "International Journal
#' of Forecasting".Chapter title: \emph{Another look at measures of forecast accuracy}.
#' Chapter pages : (p.679-688).\url{http://eva.fcea.edu.uy/pluginfile.php/109034/mod_resource/content/0/2006_Hyndman_Predicc.pdf}.
#' @keywords dataframe
#' @examples
#' boxplotAPEs(FORAYearForecast)
#' data1 <- FORAYearForecast[1:300,]
#' boxplotAPEs(data1)
#' boxplotAPEs(FORAYearForecast, notch = TRUE, limits = c(0, 0.5), showOutlier = NA)
#' @export
boxplotAPEs <- function (df, useLogs = FALSE, notch = FALSE, limits = NULL,
                         showOutliers = NULL,...){
  df["APE"] <- abs((df$actual - df$forecast)/df$actual)
  nam<- c("method", "APE")
  df1 <- df[nam]
  if(useLogs==FALSE) {
   gp <- ggplot2::ggplot(df1, ggplot2::aes(reorder(method, APE, fun = median), APE, colour = method)) + #sorting by median
     ggplot2::geom_boxplot(notch = notch, outlier.shape = showOutliers)+
     ggplot2::coord_flip() + # to flip the coordinate axes
     ggplot2::scale_x_discrete(name = "Method") + # to change the axis labels,
     ggplot2::stat_boxplot(geom='errorbar') +  #whiskers
     ggplot2::stat_summary(fun.y=mean, geom="point", size=2) + #dot for the mean
     ggplot2::coord_cartesian(ylim =  limits)+
     ggplot2::theme(axis.text.x=ggplot2::element_text(angle=60, hjust=1)) +
     ggplot2::ggtitle("Distribution of APE for defferent methods") +
     ggplot2::theme(legend.position="none")+
     ggplot2::theme(plot.title = element_text(hjust = 0.5))
  print(gp)
  }
  else{
    gp <- ggplot2::ggplot(df1, ggplot2::aes(reorder(method, APE, median), APE, colour = method)) + #sorting by median
      ggplot2::geom_boxplot(notch = notch, outlier.shape = showOutliers)+
      ggplot2::coord_flip() + # to flip the coordinate axes
      ggplot2::scale_x_discrete(name = "Method") + # to change the axis labels,
      ggplot2::stat_boxplot(geom='errorbar') +  #whiskers
      ggplot2::stat_summary(fun.y=mean, geom="point", size=2) + #dot for the mean
      ggplot2::coord_cartesian(ylim =  limits)+
      ggplot2::theme(axis.text.x=element_text(angle=60, hjust=1))+
      ggplot2::ggtitle("Distribution of log(APE) for defferent methods")+
      ggplot2::scale_y_log10() +
      ggplot2::theme(legend.position="none")+
      ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5))
    print(gp)
  }

}
svcuonghvktqs/FORA documentation built on May 20, 2019, 9:57 a.m.