簡體   English   中英

R:按列組應用Holt Winters來預測時間序列

[英]R: applying Holt Winters by group of columns to forecast time series

我有一個頻率= 7的時間序列數據如下:

combo_1_daily_mini <-   read.table(header=TRUE, text="
region_1    region_2    region_3    date    incidents
USA CA  San Francisco   1/1/15  37
USA CA  San Francisco   1/2/15  30
USA CA  San Francisco   1/3/15  31
USA CA  San Francisco   1/4/15  33
USA CA  San Francisco   1/5/15  28
USA CA  San Francisco   1/6/15  33
USA CA  San Francisco   1/7/15  39
USA PA  Pittsburg   1/1/15  38
USA PA  Pittsburg   1/2/15  35
USA PA  Pittsburg   1/3/15  37
USA PA  Pittsburg   1/4/15  33
USA PA  Pittsburg   1/5/15  30
USA PA  Pittsburg   1/6/15  33
USA PA  Pittsburg   1/7/15  25
Greece  Macedonia   Skopje  1/1/15  29
Greece  Macedonia   Skopje  1/2/15  37
Greece  Macedonia   Skopje  1/3/15  28
Greece  Macedonia   Skopje  1/4/15  38
Greece  Macedonia   Skopje  1/5/15  27
Greece  Macedonia   Skopje  1/6/15  38
Greece  Macedonia   Skopje  1/7/15  39
Italy   Trentino    Trento  1/1/15  35
Italy   Trentino    Trento  1/2/15  31
Italy   Trentino    Trento  1/3/15  34
Italy   Trentino    Trento  1/4/15  34
Italy   Trentino    Trento  1/5/15  26
Italy   Trentino    Trento  1/6/15  33
Italy   Trentino    Trento  1/7/15  27
", sep = "\t")

dput(trst,  control = "all")
structure(list(region_1 = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Greece", "Italy", "USA"), class = "factor"), 
region_2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L), .Label = c("CA", "Macedonia", "PA", "Trentino"
), class = "factor"), region_3 = structure(c(2L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Pittsburg", 
"San Francisco", "Skopje", "Trento"), class = "factor"), 
date = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L), .Label = c("1/1/15", "1/2/15", "1/3/15", "1/4/15", 
"1/5/15", "1/6/15", "1/7/15"), class = "factor"), incidents = c(37L, 
30L, 31L, 33L, 28L, 33L, 39L, 38L, 35L, 37L, 33L, 30L, 33L, 
25L, 29L, 37L, 28L, 38L, 27L, 38L, 39L, 35L, 31L, 34L, 34L, 
26L, 33L, 27L)), .Names = c("region_1", "region_2", "region_3", 
"date", "incidents"), class = "data.frame", row.names = c(NA, 
-28L))

region_1,region_2,region_3的每一組都有自己的季節性和趨勢。

我試圖根據歷史數據預測下一周的事故數量。 我有2015年1月1日至2015年6月30日為32個不同國家的6個月歷史數據。 每個國家/地區都有很多region_2和region_3。 我總共有32,356個獨特的region_1,region_2,region_3時間序列。

我有2個問題/問題:

  1. 問題 - 我面臨的問題是當我在by()函數中應用Holt Winters時,我收到警告並且我無法理解它們。 任何幫助理解它們都非常有幫助

以下是我的代碼:

ts_fun <- function(x){
  ts_y <- ts(x, frequency = 7)
}

hw_fun <- function(x){
    ts_y <- ts_fun(x)
    ts_h <- HoltWinters(ts_y) 
} 

combo_1_daily_mini$region_1 <- as.factor(combo_1_daily_mini$region_1)
combo_1_daily_mini$region_2 <- as.factor(combo_1_daily_mini$region_2)
combo_1_daily_mini$region_3 <- as.factor(combo_1_daily_mini$region_3)

combo_1_ts <- by(combo_1_daily_mini,list(combo_1_daily_mini$region_1,
                                     combo_1_daily_mini$region_2, 
                                     combo_1_daily_mini$region_3
                                     ),ts_fun)

combo_1_hw <- by(combo_1_daily_mini,list(combo_1_daily_mini$region_1,
                                     combo_1_daily_mini$region_2, 
                                     combo_1_daily_mini$region_3
                                     ),hw_fun)

警告信息:

1: In HoltWinters(ts_y) :
 optimization difficulties: ERROR: ABNORMAL_TERMINATION_IN_LNSRCH
2: In HoltWinters(ts_y) :
 optimization difficulties: ERROR: ABNORMAL_TERMINATION_IN_LNSRCH
3: In HoltWinters(ts_y) :
 optimization difficulties: ERROR: ABNORMAL_TERMINATION_IN_LNSRCH
4: In HoltWinters(ts_y) :
 optimization difficulties: ERROR: ABNORMAL_TERMINATION_IN_LNSRCH
  1. 問題 - 我通過多列正確應用函數的方式是什么? 有沒有更好的辦法? 我基本上希望通過region_1,region_2,region_3獲得下周的預測數字。 我計划使用以下代碼:

    nw_forecast < - forecast(combo_1_hw,7)

我可以應用Holt Winters功能,還可以預測每個region_1,region_2,region_3組合創建時間序列數據的時間。 此方法不可行,因為我的數據集中有32,356個唯一組合。

任何幫助表示贊賞謝謝

你可以看一下Hyndman集團的tsibble fable 寓言

library(tsibble)
library(fable)
combo_1_daily_mini %>%
  mutate(date = lubridate::mdy(date)) %>% 
  as_tsibble(index = date, key = c('region_1', 'region_2', 'region_3')) -> combo_1_daily_mini

combo_1_daily_mini %>% 
  model(
    ets = ETS(box_cox(incidents, 0.3))) %>%
  forecast %>% 
  autoplot(combo_1_daily_mini)

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM