首页 文章

缺少 Value - 有马模型

提问于
浏览
2

我有关于产品销售的每日时间序列,我的系列从2016年1月1日至2017年8月31日开始 .

考虑到它是一个为期六天的星期(我的星期一星期一开始,星期六结束)星期日没有数据,我知道在运行Arima模型之前我需要先填写缺失值 . 这是我需要帮助的地方:我已经读过我可以用 na.approxNA 填充缺失值,但我不知道该怎么做 .

你可以在这里看到我的系列:

https://drive.google.com/file/d/0BzIf8XvzKOGWSm1ucUdYUVhfVGs/view?usp=sharing

如您所见,周日没有数据 . 我需要知道如何填充缺失值以运行Arima模型并能够预测2017年的剩余时间 .

1 回答

  • 5

    这有三种方法:

    library(lubridate)
    library(xts)
    library(dplyr)
    library(forecast)
    
    df$Date = mdy(df$Date)
    

    Removing Sundays:

    ts_no_sunday = df %>%
      filter(wday(df$Date) != 1) %>%
      {xts(.$Units, .$Date)}
    
    plot(ts_no_sunday)
    
    no_sunday_arima = auto.arima(ts_no_sunday)
    
    plot(forecast(no_sunday_arima, h = 10))
    

    enter image description here

    enter image description here

    Replace Sundays with NAs:

    ts_sunday = df %>%
      mutate(Units = replace(Units, which(wday(df$Date) == 1), NA)) %>%
      {xts(.$Units, .$Date)}
    
    plot(ts_sunday)
    
    sunday_arima = auto.arima(ts_sunday)
    
    plot(forecast(sunday_arima, h = 10))
    

    enter image description here

    enter image description here

    Interpolate Sundays:

    ts_interp = df %>%
      mutate(Units = replace(Units, which(wday(df$Date) == 1), NA),
             Units = na.approx(Units)) %>%
      {xts(.$Units, .$Date)}
    
    plot(ts_interp)
    
    interp_arima = auto.arima(ts_interp)
    
    plot(forecast(interp_arima, h = 10))
    

    enter image description here

    enter image description here

    Notes:

    可以看出,他们产生了不同的预测 . 这是因为第一个时间序列是不规则的,第二个是具有缺失值的常规时间序列,第三个是具有内插数据的常规时间序列 . 在我看来,处理缺失值的更好方法是在拟合ARIMA之前进行插值,因为ARIMA假定时间序列是规则间隔的 . 但是,这也取决于您的“缺失”数据点是否实际丢失,而不是停止活动 . 前者应该用插值处理,而对于后者你可能最好去除星期日并将时间序列视为星期日不存在 .

    请参阅How to handle nonexistent or missing data?上的此讨论,并在Using the R forecast package with missing values and/or irregular time series上查看

相关问题