尝试拟合 ARIMA 模型时 xy.coords 出错,请告知

Posted

技术标签:

【中文标题】尝试拟合 ARIMA 模型时 xy.coords 出错,请告知【英文标题】:Error in xy.coords when trying to fit ARIMA model, please advise 【发布时间】:2021-10-06 21:34:29 【问题描述】:

我希望您能帮助我解决我在尝试为我正在进行的学校项目拟合 ARIMA 模型时遇到的问题。

我使用的数据显示了从 2019 年到 2021 年的每周销售数据。我的目标是根据这些数据对 2021 年剩余时间进行预测。由于我的数据集由每周数据组成,并且基于 ACF 和 PACF 图的季节性似乎每年发生一次,因此我将 sarima() 函数中的“S =”参数设置为 52。问题是每次我尝试运行模型,我不断收到错误消息,我想不出任何方法来摆脱它。

我尝试将相同的代码与“S = 52”的数据营环境中的其他数据集一起使用,并且模型运行没有问题。我希望有人可以就如何处理这个问题给我一些建议。谢谢!

附言

如果“S =”参数设置为低于 35,则模型将运行。 (以防万一这些信息可能会有所帮助)

####Install Packages####

library(tidyverse)
library(zoo)
library(xts)
library(lubridate)
library(astsa)
library(tseries)
library(forecast)

######Load and inspect the data########
unit_sales <- structure(list(Date = c("30/03/2019", "06/04/2019", "13/04/2019", 
                        "20/04/2019", "27/04/2019", "04/05/2019", "11/05/2019", "18/05/2019", 
                        "25/05/2019", "01/06/2019", "08/06/2019", "15/06/2019", "22/06/2019", 
                        "29/06/2019", "06/07/2019", "13/07/2019", "20/07/2019", "27/07/2019", 
                        "03/08/2019", "10/08/2019", "17/08/2019", "24/08/2019", "31/08/2019", 
                        "07/09/2019", "14/09/2019", "21/09/2019", "28/09/2019", "05/10/2019", 
                        "12/10/2019", "19/10/2019", "26/10/2019", "02/11/2019", "09/11/2019", 
                        "16/11/2019", "23/11/2019", "30/11/2019", "07/12/2019", "14/12/2019", 
                        "21/12/2019", "28/12/2019", "04/01/2020", "11/01/2020", "18/01/2020", 
                        "25/01/2020", "01/02/2020", "08/02/2020", "15/02/2020", "22/02/2020", 
                        "29/02/2020", "07/03/2020", "14/03/2020", "21/03/2020", "28/03/2020", 
                        "04/04/2020", "11/04/2020", "18/04/2020", "25/04/2020", "02/05/2020", 
                        "09/05/2020", "16/05/2020", "23/05/2020", "30/05/2020", "06/06/2020", 
                        "13/06/2020", "20/06/2020", "27/06/2020", "04/07/2020", "11/07/2020", 
                        "18/07/2020", "25/07/2020", "01/08/2020", "08/08/2020", "15/08/2020", 
                        "22/08/2020", "29/08/2020", "05/09/2020", "12/09/2020", "19/09/2020", 
                        "26/09/2020", "03/10/2020", "10/10/2020", "17/10/2020", "24/10/2020", 
                        "31/10/2020", "07/11/2020", "14/11/2020", "21/11/2020", "28/11/2020", 
                        "05/12/2020", "12/12/2020", "19/12/2020", "26/12/2020", "02/01/2021", 
                        "09/01/2021", "16/01/2021", "23/01/2021", "30/01/2021", "06/02/2021", 
                        "13/02/2021", "20/02/2021", "27/02/2021", "06/03/2021", "13/03/2021", 
                        "20/03/2021", "27/03/2021"), Units = c(967053.4, 633226.9, 523264, 
                                                               473914.2, 418087.5, 504342.2, 477819, 415650, 406972.3, 429791.4, 
                                                               441724.4, 453221.8, 402005.8, 414993.4, 381457.2, 391218.7, 486925.9, 
                                                               409791.8, 399217.9, 409210, 478121.2, 495549.1, 503918.3, 535949.5, 
                                                               517450.4, 523036.8, 616456.9, 665979.3, 705201.5, 700168.1, 763538.8, 
                                                               875501.2, 886586.6, 967806, 1094195, 1285950.5, 1450436.1, 1592162.8, 
                                                               2038160.5, 1676988.8, 1026193.7, 820405.5, 738643.9, 669657.6, 
                                                               720287.7, 673194.1, 754102.5, 639532, 680413.6, 710702, 711722.8, 
                                                               834036.8, 427817.2, 505849.6, 441047.4, 439411, 487634.1, 594594.8, 
                                                               548796.7, 565682, 528275.2, 448092, 467780.1, 544160.3, 538275.8, 
                                                               485055.5, 592097.3, 537514.3, 493381.9, 445280.8, 448111.2, 419263.4, 
                                                               457125.7, 561169.6, 704575.3, 656423.1, 653751.3, 622937.7, 718022.8, 
                                                               768901.9, 793443, 814604.2, 876269.3, 982921.8, 1064920.7, 1201494.4, 
                                                               1337374.9, 1619595.8, 1734773.8, 1624071, 1777832.3, 1648201.9, 
                                                               1106253.8, 940141.1, 796129.1, 853392.9, 932059.1, 905990.4, 
                                                               981188.6, 907823.9, 956098.8, 1003966.7, 1331125.5, 805593.6, 
                                                               799486.2)), class = "data.frame", row.names = c(NA, -105L))

####Convert date column to date format
unit_sales$Date <- as.Date(unit_sales$Date, format ="%d/%m/%Y" )

###Convert to xts object
unit_sales_xts <-  xts(unit_sales, unit_sales$Date)

periodicity(unit_sales_xts)

###Convert to ts object
unit_sales_vector <- unit_sales$Units

unit_sales_ts <- ts(unit_sales_vector, start = decimal_date(as.Date("2019-03-30")), frequency = 52)

###Plot data
ts.plot(unit_sales_ts)

###Make data stationary and plot it
ts.plot(diff(log(unit_sales_ts)))

###Plot ACF and PACF
pacf_plot <- pacf(diff(log(unit_sales_ts)), lag.max = 105)
acf_plot <- acf(diff(log(unit_sales_ts)), lag.max = 105)

###Test if data is stationary
adf.test(diff(log(unit_sales_ts)))

###Fit ARIMA model
sarima(unit_sales_ts, p = 1, d = 1, q = 0)
sarima.for(unit_sales_ts, n.ahead = 39, 1,1,0)

**###Fit Seasona ARIMA model - THIS IS WHERE THE ERROR OCCURS -**
sarima(unit_sales_ts, p = 1, d = 1, q = 0, P = 0, D = 1, Q = 0, S = 52)

###Forecast using the above model
sarima.for(unit_sales_ts,n.ahead = 39, p = 1, d = 1, q = 0, P = 0, D = 1, Q = 0, S = 52)

【问题讨论】:

【参考方案1】:

我测试了你的代码并得到了同样的错误,所以我阅读了 astsa::sarima() 的实现,发现了这两行,关于季节性和你的数据的使用:

alag <- max(10 + sqrt(num), 3 * S)

nlag <- ifelse(S < 7, 20, 3 * S)

在不阅读整个实现的情况下,我推断,包创建者假设参数是季节大小的 3 倍才能正常工作。当使用 S = 52 时,105 观察不是你的情况。现在,如果这是一个错误,或者只是没有很好地记录或在代码中正确处理,我不能告诉你。我不知道哪个版本的包 datacamp 运行以及包本身的更新历史是什么。但我们可以假设两行中至少有一条会导致错误,因为 S 的 35 中的所有值都会导致相同的错误。

一种解决方法是将函数的实现代码打印到控制台(只需编写“astsa::sarima”并按回车,但不带“),复制它以修改行(我尝试使用 2 *而不是 3 *) 并将其分配给您自己的函数名称。然后代码运行。您也可以在 datacamp 环境中尝试打印并与本地安装进行比较。

【讨论】:

非常感谢您的帮助,我自己永远也想不通。我会回到我的项目并尝试实施您的建议。希望你有一个美好的一天! :)

以上是关于尝试拟合 ARIMA 模型时 xy.coords 出错,请告知的主要内容,如果未能解决你的问题,请参考以下文章

解释 ARIMA 模型的预测

auto.arima() 函数中的错误

模块“statsmodels.tsa.api”没有属性“arima_model”

ARIMa--时间序列模型

时间序列笔记-季节性ARIMA模型(一)

时间序列2拟合检验和预测#R