使用预测中的precision()测量VAR精度 [英] Measuring VAR accuracy using accuracy() from forecast

查看:120
本文介绍了使用预测中的precision()测量VAR精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R中的vars包学习向量自回归模型.此包没有任何方法可以测量返回模型的准确性.

I'm trying to learn a vector autoregressive model using the vars package in R. This package doesn't have any way to measure the accuracy of the returned model.

具体来说,我想使用R中forecast包中accuracy函数中定义的MASE,以将VAR的预测与使用Arima模型在每个组件时间序列上的预测进行比较(我正在使用4个可能相关的时间系列). accuracy无法识别vars返回的varest对象.如何获得每个预测组件的MASE?我想同时计算样本内和样本外的准确性

Specifically, I want to use MASE as defined in the accuracy function from the forecast package in R to compare forecasting with VAR with forecasting using Arima models on each component time series (I'm using 4 possibly correlated time series). accuracy doesn't recognize the varest object returned by vars. How can I get the MASE for each forecasted component? I want to calculate both in-sample and out-of-sample accuracy

代码示例:

library(vars)
library(forecast)
data(Canada)
v<- VAR(window(Canada, end=c(1998,4)), p=2)
accuracy(v$varresult[[1]])

accuracy的参数是一个lm对象,并返回序列1的训练精度为:

The argument of accuracy is an lm object and returns the training accuracy for series 1 as:

                       ME      RMSE       MAE           MPE      MAPE       MASE
Training set 1.536303e-15 0.3346096 0.2653946 -1.288309e-05 0.0281736 0.03914555

我想使用类似的方法来获得样本外测试的准确性(并非完全如此,因为需要指定预测时间):

I want to get the out-of-sample test accuracy using something like (not exactly this, since the forecast period needs to be specified):

 accuracy(v$varresult[[1]], window(Canada[,1], start=c(1999,1)))

但是lm对象不支持此操作,并返回错误

but this is not supported for lm objects and returns the error

 Error in testaccuracy(f, x, test) : Unknown list structure

如果我直接按如下方式使用这些值,则不会获得MASE,它需要有关训练集的信息.因为使用的是值,而不是ts对象,而accuracy将直接与存储的时间匹配,所以这也容易出现一对一的错误:

And if I use the values directly as follows, I don't get the MASE, which needs information about the training set. This is also prone to off-by-one errors because values are used and not ts objects, for which accuracy will match the stored times directly:

 p<-predict(v, n.ahead=8)
 accuracy(p$fcst[[1]][,"fcst"],window(Canada[,1], start=c(1999,1)))

             ME      RMSE       MAE         MPE       MAPE      ACF1 Theil's U
Test set -0.1058358 0.8585455 0.7385238 -0.01114099 0.07694492 0.5655117  1.359761

理想情况下,我希望将其预测为:

Ideally, I should like to forecast it as:

fr<-forecast(v$varresult[[1]], h=8)

但这无法正常工作,因为它需要其他系列进行预测,并给出:

but this can't work because it needs the other series for the prediction, and gives:

Error in eval(expr, envir, enclos) : object 'datamat' not found

可以尝试复制forecast.Arima等功能并尝试编写forecast.varresult程序包,但是有没有更简单的方法?

I could try copying the functionality of forecast.Arima etc and try writing a forecast.varresult package, but is there a simpler way out?

推荐答案

为什么不尝试阅读文档.这是关于第一个参数f的说法:

Why don't you try reading the documentation. Here is what it says about the first argument, f:

"forecast"类的对象,或包含 预测.如果x为,它也可以与Arima,ets和lm对象一起使用. 省略-在这种情况下,将返回样本内准确性度量.

An object of class "forecast", or a numerical vector containing forecasts. It will also work with Arima, ets and lm objects if x is omitted – in which case in-sample accuracy measures are returned.

VAR不会返回"forecast"类的对象,但是您可以计算包含预测的数字矢量.

VAR does not return an object of class "forecast", but you can compute a numerical vector containing forecasts.

现在阅读第二个参数x.

Now read about the second argument, x.

一个可选数字矢量,其中包含与对象长度相同的实际值,或者 时间序列与f的时间重叠.

An optional numerical vector containing actual values of the same length as object, or a time series overlapping with the times of f.

好的,那很简单.只需在x中给出实际值,在f中给出预测值即可.

OK, that's pretty straightforward. Just give it the actual values in x and the forecast values in f.

但是,这不会为您提供MASE,因为它在帮助页面的下方进一步说明了"MASE计算是使用非季节性时间序列的样本内天真预测,季节性的样本内季节天真预测的MAE进行缩放的时间序列和样本中的非时间序列数据的均值预测."因此,没有历史数据就无法进行该计算,并且除非传递了预测"类的对象,否则它将不知道它们.

But that won't give you the MASE as further down the help page it explains that the "MASE calculation is scaled using MAE of in-sample naive forecasts for non-seasonal time series, in-sample seasonal naive forecasts for seasonal time series and in-sample mean forecasts for non-time series data." So it can't do that calculation without the historical data, and unless you are passing an object of class 'forecast' it won't know about them.

但是,诱骗它给出想要的东西并不难.这是执行此操作的一些代码:

However, it is not hard to trick it into giving what you want. Here is some code that does it:

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)
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]))
}

这篇关于使用预测中的precision()测量VAR精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆