r - 如何在使用带有 model() 扩展的 fable 包时将外生变量添加到我的 ARIMA 模型估计中
问题描述
我正在尝试估计 100 个不同系列的 ARIMA 模型。所以我采用了fabletools::model()
方法和fable::ARIMA()
函数来完成这项工作。但我无法在模型估计中使用我的外生变量。
我的系列有 3 个不同的列,第一个标识第一个出口的 ID 标签,然后是 Date.Time 标签,最后是销售。除了这些变量之外,我还有代表一天中的小时和一周中的虚拟变量。
按照下面给出的代码,我将包含我的 endegounus 和 exegenous 变量的数据框转换为 tstibble。
ts_forecast <- df11 %>% select(-Date) %>%
mutate(ID = factor(ID)) %>% group_by(ID) %>% as_tsibble(index=Date.Time,key=ID)%>%tsibble::fill_gaps(Sales=0) %>%
fabletools::model(Arima = ARIMA(Sales,stepwise = TRUE,xreg=df12))
使用此代码,我尝试为使用 ID 因子标识的多个网点预测相同日期时间间隔的值。但是,该代码返回以下错误。
> Could not find an appropriate ARIMA model.
> This is likely because automatic selection does not select models with characteristic roots that may be numerically unstable.
> For more details, refer to https://otexts.com/fpp3/arima-r.html#plotting-the-characteristic-roots
销售额是我的内生目标变量,df12 包括代表小时和天的虚拟变量。一些商店不会在某些特定时间创造销售,因此它们代表 01:00 AM 的虚拟变量对于所有观察都可能为零。但是我不认为这会是一个问题,而 fable 使用逐步方法。我想,当代码看到带有 0 的变量时,它可以排除它们
我不确定是什么问题。我是否使用有问题的方式将 xreg 添加到模型中(在 ARIMA hep 页面中,它说 xreg= 就像以前的预测包一样)或者问题与我提到的第二个问题有关,包括所有观察的“0”。如果是第二个,则可能有一种解决方案可以排除所有具有恒定 0 值的变量。
如果你能帮助我,我会很高兴。
谢谢
解决方案
这是一个使用每小时行人计数数据的示例。
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tsibble)
library(fable)
#> Loading required package: fabletools
# tsibble with hourly data
df <- pedestrian %>%
mutate(dow = lubridate::wday(Date, label=TRUE))
# Training data
train <- df %>%
filter(Date <= "2015-01-31")
# Fit models
fit <- train %>%
model(arima = ARIMA(Count ~ season("day") + dow + pdq(2,0,0) + PDQ(0,0,0)))
# Forecast period
fcast_xregs <- df %>%
filter(Date > "2015-01-31", Date <= "2015-02-07")
# Forecasts
fit %>%
forecast(fcast_xregs)
#> # A fable: 504 x 8 [1h] <Australia/Melbourne>
#> # Key: Sensor, .model [3]
#> Sensor .model Date_Time Count .mean Date Time
#> <chr> <chr> <dttm> <dist> <dbl> <date> <int>
#> 1 Birra… arima 2015-02-01 00:00:00 N(-67, 174024) -67.1 2015-02-01 0
#> 2 Birra… arima 2015-02-01 01:00:00 N(-270, 250881) -270. 2015-02-01 1
#> 3 Birra… arima 2015-02-01 02:00:00 N(-286, 310672) -286. 2015-02-01 2
#> 4 Birra… arima 2015-02-01 03:00:00 N(-283, 351704) -283. 2015-02-01 3
#> 5 Birra… arima 2015-02-01 04:00:00 N(-264, 380588) -264. 2015-02-01 4
#> 6 Birra… arima 2015-02-01 05:00:00 N(-244, 4e+05) -244. 2015-02-01 5
#> 7 Birra… arima 2015-02-01 06:00:00 N(-137, 414993) -137. 2015-02-01 6
#> 8 Birra… arima 2015-02-01 07:00:00 N(93, 424929) 93.0 2015-02-01 7
#> 9 Birra… arima 2015-02-01 08:00:00 N(292, 431894) 292. 2015-02-01 8
#> 10 Birra… arima 2015-02-01 09:00:00 N(225, 436775) 225. 2015-02-01 9
#> # … with 494 more rows, and 1 more variable: dow <ord>
由reprex 包(v0.3.0)于 2020 年 10 月 9 日创建
笔记:
- 您不需要在 R 中创建虚拟变量。公式界面将适当地处理分类变量。
season("day")
特殊范围内将ARIMA
生成适当的季节性分类变量,相当于 23 小时虚拟变量。- 我指定了一个特定的 ARIMA 模型来节省计算时间。但是省略了
pdq
特殊的自动选择最优模型。 - 保持
PDQ(0,0,0)
特殊,因为在使用外生变量时不需要 ARIMA 模型来处理季节性。
推荐阅读
- html - 如何以角度解析嵌套的json
- java - 客户端无法使用自签名证书连接到 REST API
- rust - 如何通过 curl 使用 actix-from-data 的映射数组发出 POST 请求?
- asp.net - 如何用我在 iis 服务器上运行的 asp.net 中的自定义值替换缓存控制安全标头值中的私有值
- jquery - jQuery - 有多个总计的总计
- sprite-kit - 如何使用 SpriteKit 在圆上通过给定偏移创建点?
- c# - 以 xamarin 形式将对象放置在 TabbedPage 上的绝对位置
- python - 将列表的字典转换为字典中列表中元素的所有梳的字典列表的最pythonic方法?
- xcode - 无法初始化可可豆荚
- python-3.x - 将数字限制为特定的小数位