Forecast accuracy: no MASE with two vectors as arguments

早过忘川 提交于 2019-11-30 20:16:03

The MASE requires the historical data to compute the scaling factor. It is not computed from the future data as in the answer by @FBE. So if you don't pass the historical data to accuracy(), the MASE cannot be computed. For example,

> library(forecast)
> fcast <- snaive(window(USAccDeaths,end=1977.99))
> accuracy(fcast$mean,USAccDeaths)
         ME        RMSE         MAE         MPE        MAPE        ACF1 
225.1666667 341.1639391 259.5000000   2.4692164   2.8505546   0.3086626 
  Theil's U 
  0.4474491 

But if you pass the whole fcast object (which includes the historical data), you get

> accuracy(fcast,USAccDeaths)
         ME        RMSE         MAE         MPE        MAPE        MASE 
225.1666667 341.1639391 259.5000000   2.4692164   2.8505546   0.5387310 
       ACF1   Theil's U 
  0.3086626   0.4474491 

The paper on MASE clearly explains how to find it (even for non time-series data)

computeMASE <- function(forecast,train,test,period){

  # forecast - forecasted values
  # train - data used for forecasting .. used to find scaling factor
  # test - actual data used for finding MASE.. same length as forecast
  # period - in case of seasonal data.. if not, use 1

  forecast <- as.vector(forecast)
  train <- as.vector(train)
  test <- as.vector(test)

  n <- length(train)
  scalingFactor <- sum(abs(train[(period+1):n] - train[1:(n-period)])) / (n-period)

  et <- abs(test-forecast)
  qt <- et/scalingFactor
  meanMASE <- mean(qt)
  return(meanMASE)
}

To help myself a little bit, I created a function to calculate the MASE, as described by Hyndman et al in "Another look at measures of forecast accuracy" (2006).

calculateMASE <- function(f,y) { # f = vector with forecasts, y = vector with actuals
    if(length(f)!=length(y)){ stop("Vector length is not equal") }
    n <- length(f)
    return(mean(abs((y - f) / ((1/(n-1)) * sum(abs(y[2:n]-y[1:n-1]))))))
}

For reference, see:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!