当在Forecast()和ar()中传递参数时,R中的预测误差 [英] Forecasting error in R when passing around arguments in forecast() and ar()

查看:88
本文介绍了当在Forecast()和ar()中传递参数时,R中的预测误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试使用Rob Hyndman的预测库从较小的函数组成函数时,如下所示:

> library('forecast')
> arf <- function(data, ...) forecast(ar(data, order.max=1, method="ols"), ...)

尝试插入某些数据时出现错误:

> arf(ts(1:100, start=c(2000,1), frequency=4))
Error in ts(x, frequency = 1, start = 1) : object is not a matrix

但是,直接使用arf主体可以完美地发挥作用:

> forecast(ar(ts(1:100, start=c(2000,1), frequency=4), order.max=1,method="ols"))
        Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2025 Q1            101   101   101   101   101
2025 Q2            102   102   102   102   102
2025 Q3            103   103   103   103   103
2025 Q4            104   104   104   104   104
2026 Q1            105   105   105   105   105
2026 Q2            106   106   106   106   106
2026 Q3            107   107   107   107   107
2026 Q4            108   108   108   108   108
2027 Q1            109   109   109   109   109
2027 Q2            110   110   110   110   110

为什么arf无法正常工作?

解决方案

这是forecast.ar()中的问题(不是错误).所有forecast.xxx()函数都尝试存储用于估计时间序列模型的数据,这是绘图和精度计算所必需的.但是,ar()不返回数据,因此forecast.ar()尝试在调用环境或父环境中查找数据.当您直接调用forecast(ar(...))时,该函数设法查找数据,但是arf()将调用放置到ar()的更深一层,这使forecast很难确定正在使用什么数据.

我可以修改该函数以使其看起来更难于读取数据(即,也可以在祖父母环境中查看),但是构造仍会失败,因为predict.ar()(stats包的一部分)会导致类似的错误.如果ar()返回数据会更好,但ar()stats包的一部分,而我对此无能为力.

有几种可能的解决方案.

  1. 您可以将ar替换为Arima:

    arf <- function(dat, ...) forecast(Arima(dat, order=c(1,0,0)), ...)
    

    如果数据是固定的,那应该返回相同的模型(尽管参数估计会略有不同).由于时间序列是非平稳的,因此不会为您的问题中的示例返回相同的答案.

  2. 如果您想使用比AR(1)更通用的ARIMA模型,则可以改用auto.arima().

    arf <- function(dat, ...) forecast(auto.arima(dat, ...)
    

  3. (基于@agstudy的建议).解决方法是确保数据存储在ar对象中

    :

    arf <- function(dat, ...) 
    {
      object <- ar(dat, order.max=1, method="ols")
      object$x <- dat
      forecast(object,...)
    }
    

When trying to compose a function from smaller ones using Rob Hyndman's forecast library, like so:

> library('forecast')
> arf <- function(data, ...) forecast(ar(data, order.max=1, method="ols"), ...)

I get an error when trying to plug in some data:

> arf(ts(1:100, start=c(2000,1), frequency=4))
Error in ts(x, frequency = 1, start = 1) : object is not a matrix

However, using the body of arf directly works perfectly:

> forecast(ar(ts(1:100, start=c(2000,1), frequency=4), order.max=1,method="ols"))
        Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2025 Q1            101   101   101   101   101
2025 Q2            102   102   102   102   102
2025 Q3            103   103   103   103   103
2025 Q4            104   104   104   104   104
2026 Q1            105   105   105   105   105
2026 Q2            106   106   106   106   106
2026 Q3            107   107   107   107   107
2026 Q4            108   108   108   108   108
2027 Q1            109   109   109   109   109
2027 Q2            110   110   110   110   110

Why does arf not work as it should?

解决方案

This is a problem (not really a bug) in forecast.ar(). All forecast.xxx() functions try to store the data used to estimate the time series model as it is required for plots and accuracy calculations. However, ar() does not return the data, so forecast.ar() attempts to find the data in the calling environment, or in the parent environment. When you call forecast(ar(...)) directly, the function manages to find the data, but arf() places the call to ar() one level deeper making it that much harder for forecast to figure out what data was being used.

I can modify the function to make it look harder for the data (i.e., look in the grandparent environment also), but the construction will still fail because predict.ar() (part of the stats package) will then cause a similar error. It would be much better if ar() returned the data, but ar() is part of the stats package and I have no control over it.

There are several possible solutions.

  1. You could replace ar with Arima:

    arf <- function(dat, ...) forecast(Arima(dat, order=c(1,0,0)), ...)
    

    That should return the same model if the data are stationary (although the parameter estimates will be slightly different). It won't return the same answer for the example in your question because the time series is non-stationary.

  2. You could use auto.arima() instead if you were willing to use more general ARIMA models than AR(1).

    arf <- function(dat, ...) forecast(auto.arima(dat, ...)
    

  3. (Based on a suggestion from @agstudy). A workaround is to ensure the data is stored within the ar object:

    arf <- function(dat, ...) 
    {
      object <- ar(dat, order.max=1, method="ols")
      object$x <- dat
      forecast(object,...)
    }
    

这篇关于当在Forecast()和ar()中传递参数时,R中的预测误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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