【问题标题】:Convert forecast time produced by arima into standard date time将 arima 生成的预测时间转换为标准日期时间
【发布时间】:2017-12-17 14:43:26
【问题描述】:

我已经使用 ARIMA 进行了预测:

预测结果如下:

    Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
15552001       18.80470 18.52527 19.08413 18.37735 19.23206
15555601       18.94718 18.63358 19.26078 18.46758 19.42679
15559201       18.86774 18.53623 19.19924 18.36074 19.37473
15562801       18.84979 18.46603 19.23355 18.26288 19.43670
15566401       18.91506 18.52182 19.30830 18.31366 19.51646
15570001       18.86569 18.46538 19.26599 18.25347 19.47791
15573601       18.86727 18.45311 19.28143 18.23387 19.50067
15577201       18.89555 18.47853 19.31258 18.25777 19.53334
15580801       18.86784 18.44715 19.28854 18.22444 19.51125
15584401       18.87419 18.44782 19.30056 18.22212 19.52627
15588001       18.88546 18.45692 19.31399 18.23007 19.54085
15591601       18.87097 18.43914 19.30280 18.21054 19.53139
15595201       18.87679 18.44097 19.31262 18.21025 19.54334
15598801       18.88064 18.44225 19.31904 18.21018 19.55111
15602401       18.87349 18.43165 19.31533 18.19775 19.54923
15606001       18.87755 18.43210 19.32300 18.19630 19.55880
15609601       18.87844 18.43003 19.32685 18.19265 19.56423
15613201       18.87510 18.42320 19.32701 18.18397 19.56623
15616801       18.87759 18.42224 19.33293 18.18120 19.57397
15620401       18.87748 18.41899 19.33597 18.17627 19.57868
15624001       18.87602 18.41411 19.33793 18.16959 19.58245
15627601       18.87742 18.41219 19.34264 18.16592 19.58891

我想将此点转换为人类可读的日期时间。我该怎么做?

我的初始数据框的纪元时间为 row.names,例如:

         y

1484337600 19.22819
1484341200 19.28906
1484344800 19.28228
1484348400 19.21669
1484352000 19.32759
1484355600 19.21833
1484359200 19.20626
1484362800 19.28737
1484366400 19.20651
1484370000 19.18424

我使用的这个初始数据集:

xts_dataframe <- xts(x = dataframe$y, order.by = as.POSIXct(as.numeric(row.names(dataframe)), origin="1970-01-01"))



fit <- auto.arima(ts_dataframe ,trace = TRUE,stepwise = FALSE)  #411 
  fc <- forecast(fit,h=24)
  fc <- as.POSIXct(as.numeric(fc$mean), origin = '1970-01-01')
  print(fc)

它现在给出这样的输出:

[1] "1969-12-31 16:00:18 PST" "1969-12-31 16:00:18 PST"
  [3] "1969-12-31 16:00:18 PST" "1969-12-31 16:00:18 PST"
  [5] "1969-12-31 16:00:18 PST" "1969-12-31 16:00:18 PST"
  [7] "1969-12-31 16:00:18 PST" "1969-12-31 16:00:18 PST"
  [9] "1969-12-31 16:00:18 PST" "1969-12-31 16:00:18 PST"
 [11] "1969-12-31 16:00:18 PST" "1969-12-31 16:00:18 PST"
 [13] "1969-12-31 16:00:18 PST" "1969-12-31 16:00:18 PST"
 [15] "1969-12-31 16:00:18 PST" "1969-12-31 16:00:18 PST"
 [17] "1969-12-31 16:00:18 PST" "1969-12-31 16:00:18 PST"
 [19] "1969-12-31 16:00:18 PST" "1969-12-31 16:00:18 PST"
 [21] "1969-12-31 16:00:18 PST" "1969-12-31 16:00:18 PST"
 [23] "1969-12-31 16:00:18 PST" "1969-12-31 16:00:18 PST"

没有预测结果。

问题-2:如果我没有对预测结果中的点进行任何更改:

 fit <- auto.arima(ts_dataframe ,trace = TRUE,stepwise = FALSE)
    fc <- forecast(fit,h=24)
    print(fc)

我得到了我在顶部发布的结果。在这个纪元时间是 1970 年,但它应该是在我原始数据框中的最后一个纪元时间之后。请帮忙。

【问题讨论】:

    标签: r time-series xts zoo arima


    【解决方案1】:

    我希望这就是您的数据的样子

    > head(data)
           epoch        y
    1 1484341200 19.28906
    2 1484344800 19.28228
    3 1484348400 19.21669
    4 1484352000 19.32759
    5 1484355600 19.21833
    6 1484359200 19.20626
    
    > data$epoch <- as.POSIXct(data$epoch, origin = '1970-01-01')
    
    > head(data)
                    epoch        y
    1 2017-01-14 02:30:00 19.28906
    2 2017-01-14 03:30:00 19.28228
    3 2017-01-14 04:30:00 19.21669
    4 2017-01-14 05:30:00 19.32759
    5 2017-01-14 06:30:00 19.21833
    6 2017-01-14 07:30:00 19.20626
    
    > xts_dataframe <- xts(data$y, order.by = data$epoch)
    > fit <- auto.arima(xts_dataframe ,trace = TRUE,stepwise = FALSE)
    > fc <- forecast(fit,h=24)
    
    > fc <- as.data.frame(fc)
    > time1 <- as.data.frame(seq(tail(data$epoch,1)+60, length.out = nrow(fc), by = "30 min"))
    
    > output <- as.data.frame(cbind(time1[,1],fc))
    > print(output)
    

    【讨论】:

    • fit
    • output &lt;- as.POSIXct( fc$mean, origin = '1970-01-01') 。这将以日期时间格式给出点预测值。如果您希望 Lo80、Lo95、Hi80、HI95 采用日期时间格式,请将 fc$mean 替换为 fc$lower[,1] 等等。
    • 所以,我已经尝试使用您的代码:fc forecast_data - > as.POSIXct -> as.POSIXct.default 执行停止
    • origin 只用单引号。
    • 我得到的预测数据已经发布了。您是要我应用 ARIMA 的初始数据集吗?
    【解决方案2】:

    您可以查看新的sweep 包。这是描述该过程的recent post。以下输入基于您的数据框:

    • data 是您的数据框,其中包含日期或日期时间列和值列
    • freq 是您的频率,start 是您的 ts 对象的起点
    • h 是您对未来的预测期

    这是一般过程。

    library(forecast)
    library(sweep)
    library(timekit)
    
    # Make ts with tk_ts()
    data_ts <- tk_ts(data, freq = freq, start = start)
    
    # auto.arima()
    fit <- auto.arima(data_ts)
    
    # Make forecast
    fcast <- forecast(fit, h = h)
    
    # Use sw_sweep with timekit_idx = TRUE
    sw_sweep(fcast, timekit_idx = T)
    

    【讨论】:

    • 感谢您的回复。你能告诉我如何在 data_ts
    • 你能告诉我是否可以在这个 tk_ts() 方法中应用每小时频率?
    • ts 系统使用以频率和开始指定的正则化时间序列。除了漂亮的印刷外,开始并不重要,但频率很重要。我会查看您的数据并尝试找出您的基期。对于月度数据,您需要设置 freq = 12,因为它每年都会重复。如果以 1 小时的频率收集数据,我会设置 freq = 24start = 1。如果间隔 5 分钟,我会设置 freq = 60 / 5,因为每小时有 12 次观察。
    • 我发现 tk_methods 的其他变体可用于每小时分钟等频率。我正在使用这样的东西: tk_xts_dataframe
    • 但我想问的一件事是,即使我使用的是 final
    猜你喜欢
    • 2022-01-26
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多