【问题标题】:How to deal with NA's in timeseries in R?如何在 R 中处理时间序列中的 NA?
【发布时间】:2020-03-25 01:51:31
【问题描述】:

我正在尝试构建一个 ARIMA 模型来预测办公室的入住率。数据中有一些 NA,这些日期是国定假日,这意味着没有人在办公室,因此没有数据。如何处理这些 NA 值来构建 ARIMA 模型?

不适用示例:

    2019-04-19 09:00:00      12.878788
    2019-04-19 10:00:00      19.848485
    2019-04-19 11:00:00      21.969697
    2019-04-19 12:00:00      11.212121
    2019-04-19 13:00:00      14.090909
    2019-04-19 14:00:00      16.363636
    2019-04-19 15:00:00      22.727273
    2019-04-19 16:00:00       7.727273
    2019-04-22 09:00:00             NA
    2019-04-22 10:00:00             NA
    2019-04-22 11:00:00             NA
    2019-04-22 12:00:00             NA
    2019-04-22 13:00:00             NA
    2019-04-22 14:00:00             NA
    2019-04-22 15:00:00             NA
    2019-04-22 16:00:00             NA
    2019-04-23 09:00:00      23.636364
    2019-04-23 10:00:00      49.545455
    2019-04-23 11:00:00      57.575758
    2019-04-23 12:00:00      48.030303
    2019-04-23 13:00:00      45.151515
    2019-04-23 14:00:00      35.606061
    2019-04-23 15:00:00      25.151515
    2019-04-23 16:00:00       8.333333

我尝试使用此代码:

    plot(stl(ts, na.action = na.omit))

但是我收到了这个错误:

    Error in na.omit.ts(as.ts(x)) : time series contains internal NAs

【问题讨论】:

  • 我想建议你把它移到CrossValidated
  • 看看this
  • 你可以考虑使用插值吗?

标签: r time-series na forecasting arima


【解决方案1】:

R 中的 ARIMA 模型可以毫无问题地处理 NA。 STL 分解不处理 NA,这是您的错误的来源。

如果您想做 STL,您可以使用 forecast 包中的 mstl,它会为您估算缺失值。

library(forecast)
library(ggplot2)
USAccDeaths[20:23] <- NA
USAccDeaths %>%
  mstl(s.window="periodic") %>%
  autoplot()


USAccDeaths %>%
  auto.arima() %>%
  forecast(h=24) %>%
  autoplot()

reprex package (v0.3.0) 于 2019 年 11 月 30 日创建

【讨论】:

    猜你喜欢
    • 2017-04-10
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    相关资源
    最近更新 更多