Search code examples
rforecast

Extract Accuracy measures for a VAR model


I am modeling with vars and forecast packages and with data set Canada.So I trying to extract accurancy errors ( ME,RMSE,MAE,MPE,MAPE,MASE,ACF1) in separate data.frame. I am trying to do this but despite this I can't put accurancy errors into one data frame.

# LIBRARY AND DATA SET

library(vars)
library(forecast)
        data("Canada")

# VAR MODELING
            trainingdata <- window(Canada, end=c(1998,4))
            testdata <- window(Canada, start=c(1999,1))
            v <- VAR(trainingdata, p=2)
            p <- predict(v, n.ahead=8)
            res <- residuals(v)
            fits <- fitted(v)

# EXTRACTING ACCURANCY ERRORS <--------------------------------------
            for(i in 1:4)
            {
              fc <- structure(list(mean=p$fcst[[i]][,"fcst"], x=trainingdata[,i],
                                   fitted=c(NA,NA,fits[,i])),class="forecast")
              print(accuracy(fc,testdata[,i]))
            }

I trying with this but I don't have success.

#  Example
for(i in 1:4)
            {
              fc <- structure(list(mean=p$fcst[[i]][,"fcst"], x=trainingdata[,i],
                                   fitted=c(NA,NA,fits[,i])),class="forecast")
              VAR_ACCURANCY<-rbind(print(accuracy(fc,testdata[,i])))
            }

So can anybody help how to resolve this problem and to get table like pic below ?

enter image description here


Solution

  • my_acc <- list()
    
                for(i in 1:4)
    
                {
                  fc <- structure(list(mean=p$fcst[[i]][,"fcst"], x=trainingdata[,i],
                                       fitted=c(NA,NA,fits[,i])),class="forecast")
                   my_acc[[i]] <- accuracy(fc,testdata[,i])
                }
    
    
    my_acc  <-     do.call(rbind,my_acc)
    
                            ME      RMSE       MAE           MPE       MAPE      MASE        ACF1 Theil's U
    Training set  1.536303e-15 0.3346096 0.2653946 -1.288309e-05 0.02817360 0.1237121 0.154055381        NA
    Test set     -1.058358e-01 0.8585455 0.7385238 -1.114099e-02 0.07694492 0.3442584 0.565511711  1.359761
    Training set  7.681616e-16 0.6173540 0.4876141 -2.294727e-04 0.11991245 0.3021549 0.008074408        NA
    Test set      3.782751e+00 4.3178199 3.7827506  9.073918e-01 0.90739184 2.3440190 0.717658769  6.114329
    Training set -2.304469e-15 0.7516211 0.5716939 -2.286906e-04 0.13180088 0.1336219 0.040611671        NA
    Test set     -1.653742e+00 2.1006680 1.6537419 -3.525009e-01 0.35250086 0.3865286 0.478272541  3.187979
    Training set  1.199728e-17 0.2414005 0.1875786 -6.410195e-02 1.97545990 0.1896330 0.053361109        NA
    Test set      6.161591e-01 0.7629915 0.6528118  8.345465e+00 8.87898322 0.6599614 0.551628239  2.684020