Search code examples
rlapplyforecast

Apply lapply function on forecast package (accuracy and auto.arima)


I am foresting data set uschange from fpp2 package and function from the forecast package auto.arima. Because I forecasting several time series in the same time I used own function which make several projections simultaneously.

# 
library(fpp2) # data
library(dplyr)
library(forecast)

MY_DATA<-uschange[,1:4]

Trening_set<-subset(MY_DATA,start=1,end=150) # Training set
Test_set<-subset(MY_DATA,start=151,end=187) # Test set 20% of observations

# 1.Own functions for forecasting
FORECASTING_FUNCTION_ARIMA <- function(Z, hrz = 16) {
  timeseries <- msts(Z, start = 1970, seasonal.periods = 4)
  forecast <- auto.arima(timeseries)
  #ic = c("bic")
}
FORECASTING_LIST_ARIMA <- lapply(X = Trening_set, FORECASTING_FUNCTION_ARIMA)
ARIMA_MODELS_FORECAST<-lapply(FORECASTING_LIST_ARIMA, forecast,h=37)

In order to see accuracy of this models I used lapply function.So code and results you can see below:

# Accurancy test
ACCURANCY_ARIMA <- lapply(FORECASTING_LIST_ARIMA,  accuracy)

So next step should be how to use same function in order to produce same accuracy errors like in previous example but now with test set. I try with code below but something is wrong and I can't get good results.

 ACCURANCY_ARIMA1<-lapply(FORECASTING_LIST_ARIMA, accuracy(forecast(ARIMA_MODELS_ALL,h=37),x=Test_set))

If this function work properly output should look like this table below (numbers are only for illustration).

enter image description here

So can anybody help me how to fix this code line and get output similar like last pic above.


Solution

  • You can try :

    library(forecast)
    
    ACCURACY_ARIMA <- Map(function(x, y) accuracy(forecast(x, h = 37), 
            x = Test_set[, y]), FORECASTING_LIST_ARIMA, seq_len(ncol(Test_set)))
    
    ACCURACY_ARIMA
    #$Consumption
    #                   ME RMSE  MAE MPE MAPE MASE  ACF1 Theil's U
    #Training set  0.00082 0.61 0.45  62  192 0.66 0.018        NA
    #Test set     -0.44644 0.66 0.49 165  346 0.72 0.629      0.67
    
    #$Income
    #                   ME RMSE  MAE MPE MAPE MASE   ACF1 Theil's U
    #Training set  4.2e-15 0.86 0.60  40  163 0.63 -0.056        NA
    #Test set     -3.4e-01 1.18 0.69  20  212 0.72 -0.326      0.65
    
    #$Production
    #                  ME RMSE  MAE MPE MAPE MASE   ACF1 Theil's U
    #Training set  0.0094  1.3 0.88  36  114 0.59 -0.021        NA
    #Test set     -0.6538  1.8 1.05  18  124 0.71  0.771         1
    
    #$Savings
    #              ME RMSE  MAE MPE MAPE MASE   ACF1 Theil's U
    #Training set 1.2   12  8.1 111  176 0.65 -0.012        NA
    #Test set     2.5   19 11.9 101  101 0.96 -0.356      0.97