繁体   English   中英

如何使用 R 中的 auto.arima 生成带有日期的未来预测?

[英]How to generate future forecasts with dates using auto.arima in R?

我想使用 auto.arima 生成预测,但是我看不到未来的日期。 我怎样才能得到带有日期的未来预测。 我有每周数据,想要生成截至 2020 年 12 月的预测

我在 R 中使用预测 package

fit <- auto.arima(zoo_ts)

fcast <- forecast(fit, h=83)

需要从 2019 年 7 月开始的每周预测,日期有每周间隔。 我不提供任何数据。 任何人都可以分享如何做到这一点会很棒

forecast package 使用ts对象,这对于每周数据来说不是很好。 时间索引以年数形式存储。 所以 2019.5385 表示 2019 年的第 28 周(如 28/52 = 0.5385)。

另一种方法是使用fabletsibble包。 这是使用每周数据的示例。

library(tsibble)
library(fable)
library(fpp3) # For the data

# Fit the model
fit <- us_gasoline %>% model(arima = ARIMA(Barrels))
# Produce forecasts
fcast <- forecast(fit, h = 83)
fcast
#> # A fable: 83 x 4 [1W]
#> # Key:     .model [1]
#>    .model     Week Barrels .distribution
#>    <chr>    <week>   <dbl> <dist>       
#>  1 arima  2017 W04    8.30 N(8.3, 0.072)
#>  2 arima  2017 W05    8.44 N(8.4, 0.077)
#>  3 arima  2017 W06    8.53 N(8.5, 0.082)
#>  4 arima  2017 W07    8.59 N(8.6, 0.086)
#>  5 arima  2017 W08    8.48 N(8.5, 0.091)
#>  6 arima  2017 W09    8.49 N(8.5, 0.096)
#>  7 arima  2017 W10    8.61 N(8.6, 0.101)
#>  8 arima  2017 W11    8.52 N(8.5, 0.106)
#>  9 arima  2017 W12    8.58 N(8.6, 0.111)
#> 10 arima  2017 W13    8.47 N(8.5, 0.115)
#> # … with 73 more rows

时间索引以周为单位存储在这里。 这可以使用as.Date转换为日期:

# Convert weekly index to a date
fcast %>% mutate(date = as.Date(Week))
#> # A fable: 83 x 5 [1W]
#> # Key:     .model [1]
#>    .model     Week Barrels .distribution date      
#>    <chr>    <week>   <dbl> <dist>        <date>    
#>  1 arima  2017 W04    8.30 N(8.3, 0.072) 2017-01-23
#>  2 arima  2017 W05    8.44 N(8.4, 0.077) 2017-01-30
#>  3 arima  2017 W06    8.53 N(8.5, 0.082) 2017-02-06
#>  4 arima  2017 W07    8.59 N(8.6, 0.086) 2017-02-13
#>  5 arima  2017 W08    8.48 N(8.5, 0.091) 2017-02-20
#>  6 arima  2017 W09    8.49 N(8.5, 0.096) 2017-02-27
#>  7 arima  2017 W10    8.61 N(8.6, 0.101) 2017-03-06
#>  8 arima  2017 W11    8.52 N(8.5, 0.106) 2017-03-13
#>  9 arima  2017 W12    8.58 N(8.6, 0.111) 2017-03-20
#> 10 arima  2017 W13    8.47 N(8.5, 0.115) 2017-03-27
#> # … with 73 more rows

reprex package (v0.3.0) 于 2019 年 10 月 16 日创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM