简体   繁体   English

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

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

i would like to generate forecasts using auto.arima however i dont see future dates populated.我想使用 auto.arima 生成预测,但是我看不到未来的日期。 How can i get future forecasts with date.我怎样才能得到带有日期的未来预测。 I am having weekly data, want to generate forecasts upto Dec 2020我有每周数据,想要生成截至 2020 年 12 月的预测

i am using forecast package in R我在 R 中使用预测 package

fit <- auto.arima(zoo_ts)

fcast <- forecast(fit, h=83)

Need weekly forecast from july 2019 with dates having weekly interval.需要从 2019 年 7 月开始的每周预测,日期有每周间隔。 I am not providing any data.我不提供任何数据。 Can anyone share how to do it will be great任何人都可以分享如何做到这一点会很棒

The forecast package uses ts objects, which are not great for weekly data. forecast package 使用ts对象,这对于每周数据来说不是很好。 The time index is stored numerically in terms of years.时间索引以年数形式存储。 So 2019.5385 means week 28 of 2019 (as 28/52 = 0.5385).所以 2019.5385 表示 2019 年的第 28 周(如 28/52 = 0.5385)。

An alternative is to use the fable and tsibble packages.另一种方法是使用fabletsibble包。 Here is an example using weekly data.这是使用每周数据的示例。

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

The time index is stored in weeks here.时间索引以周为单位存储在这里。 This can be converted to a date using as.Date :这可以使用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

Created on 2019-10-16 by the reprex package (v0.3.0)reprex package (v0.3.0) 于 2019 年 10 月 16 日创建

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

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