1. Создание фрейма данных FTS c 3 интервалами (80%, 90%, 95%) для demo

library(Mcomp)
library(forecast)
ts <- M3_monthly_FTS <- read.csv("~/GitHub/forvision_data/M3_monthly_TSTS.csv")
fs <- M3_monthly_FTS <- read.csv("~/GitHub/forvision_data/M3_monthly_FTS.csv")
ts <- subset(ts, series_id == "M302")
fs <- subset(fs, series_id == "M302"& method == "NAIVE2")
fit <- auto.arima(M3[[1702]]$x)
predicted <- forecast(fit, h=M3[[1702]]$h, level=c(80,90, 95))
df <- as.data.frame(predicted)
names(df) <- c("forecast", "lo80", "hi80", "lo90", "hi90", "lo95", "hi95")
fs$method <- "ARIMA"
fs$forecast <- df$forecast
fs <- cbind(fs, df[, 2:7])
head(fs)
##        series_id category method forecast horizon timestamp
## 130033      M302    MICRO  ARIMA 1535.058       1   1993-10
## 130034      M302    MICRO  ARIMA 1723.306       2   1993-11
## 130035      M302    MICRO  ARIMA 1259.472       3   1993-12
## 130036      M302    MICRO  ARIMA 1662.239       4   1994-01
## 130037      M302    MICRO  ARIMA 1382.579       5   1994-02
## 130038      M302    MICRO  ARIMA 1736.022       6   1994-03
##        origin_timestamp      lo80     hi80       lo90     hi90      lo95
## 130033          1993-09 298.23209 2771.883  -52.39078 3122.506 -356.5038
## 130034          1993-09 458.14247 2988.470   99.48609 3347.127 -211.5948
## 130035          1993-09 -33.40953 2552.353 -399.92336 2918.867 -717.8194
## 130036          1993-09 342.22239 2982.255  -31.98395 3356.462 -356.5521
## 130037          1993-09  35.97354 2729.184 -345.77033 3110.928 -676.8762
## 130038          1993-09 363.34347 3108.701  -25.79195 3497.837 -363.3089
##            hi95
## 130033 3426.619
## 130034 3658.207
## 130035 3236.763
## 130036 3681.030
## 130037 3442.034
## 130038 3835.354
head(fs)
##        series_id category method forecast horizon timestamp
## 130033      M302    MICRO  ARIMA 1535.058       1   1993-10
## 130034      M302    MICRO  ARIMA 1723.306       2   1993-11
## 130035      M302    MICRO  ARIMA 1259.472       3   1993-12
## 130036      M302    MICRO  ARIMA 1662.239       4   1994-01
## 130037      M302    MICRO  ARIMA 1382.579       5   1994-02
## 130038      M302    MICRO  ARIMA 1736.022       6   1994-03
##        origin_timestamp      lo80     hi80       lo90     hi90      lo95
## 130033          1993-09 298.23209 2771.883  -52.39078 3122.506 -356.5038
## 130034          1993-09 458.14247 2988.470   99.48609 3347.127 -211.5948
## 130035          1993-09 -33.40953 2552.353 -399.92336 2918.867 -717.8194
## 130036          1993-09 342.22239 2982.255  -31.98395 3356.462 -356.5521
## 130037          1993-09  35.97354 2729.184 -345.77033 3110.928 -676.8762
## 130038          1993-09 363.34347 3108.701  -25.79195 3497.837 -363.3089
##            hi95
## 130033 3426.619
## 130034 3658.207
## 130035 3236.763
## 130036 3681.030
## 130037 3442.034
## 130038 3835.354

2. plotFanChart и 2 интервала (80% и 90%)

  • Выбирать данные с PIs 80% and 90%
col <- c("series_id", "method", "forecast", "horizon", "timestamp", "origin_timestamp", "lo80", "hi80", "lo90", "hi90")
fs_2 <- fs[, col]
head(fs_2)
##        series_id method forecast horizon timestamp origin_timestamp
## 130033      M302  ARIMA 1535.058       1   1993-10          1993-09
## 130034      M302  ARIMA 1723.306       2   1993-11          1993-09
## 130035      M302  ARIMA 1259.472       3   1993-12          1993-09
## 130036      M302  ARIMA 1662.239       4   1994-01          1993-09
## 130037      M302  ARIMA 1382.579       5   1994-02          1993-09
## 130038      M302  ARIMA 1736.022       6   1994-03          1993-09
##             lo80     hi80       lo90     hi90
## 130033 298.23209 2771.883  -52.39078 3122.506
## 130034 458.14247 2988.470   99.48609 3347.127
## 130035 -33.40953 2552.353 -399.92336 2918.867
## 130036 342.22239 2982.255  -31.98395 3356.462
## 130037  35.97354 2729.184 -345.77033 3110.928
## 130038 363.34347 3108.701  -25.79195 3497.837
  • Pplot fanchart:
library(forvision)
plotFanChart(ts = ts, fs = fs_2, id = "M302", origin = "1993-09", m = "ARIMA")

Error in plotFanChart(ts = ts, fs = fs_2, id = “M1”, origin = “1994-02”, : Check the column names of input data frame ts. The input data ts needed in the form of a data frame containing columns named ‘series_id’, value, and ‘timestamp_dbo’.

  • У нас нету колонки ‘timestamp_dbo’, поэтому надо создать:
library(zoo)
fs_2$timestamp_dbo <- as.yearmon(fs_2$timestamp, format = '%Y-%m')
ts$timestamp_dbo <- as.yearmon(ts$timestamp, format = '%Y-%m')
head(fs_2)
##        series_id method forecast horizon timestamp origin_timestamp
## 130033      M302  ARIMA 1535.058       1   1993-10          1993-09
## 130034      M302  ARIMA 1723.306       2   1993-11          1993-09
## 130035      M302  ARIMA 1259.472       3   1993-12          1993-09
## 130036      M302  ARIMA 1662.239       4   1994-01          1993-09
## 130037      M302  ARIMA 1382.579       5   1994-02          1993-09
## 130038      M302  ARIMA 1736.022       6   1994-03          1993-09
##             lo80     hi80       lo90     hi90 timestamp_dbo
## 130033 298.23209 2771.883  -52.39078 3122.506      Oct 1993
## 130034 458.14247 2988.470   99.48609 3347.127      Nov 1993
## 130035 -33.40953 2552.353 -399.92336 2918.867      Dec 1993
## 130036 342.22239 2982.255  -31.98395 3356.462      Jan 1994
## 130037  35.97354 2729.184 -345.77033 3110.928      Feb 1994
## 130038 363.34347 3108.701  -25.79195 3497.837      Mar 1994
  • Plot fan chart:
library(forvision)
plotFanChart(ts = ts, fs = fs_2, id = "M302", origin = "1993-09", m = "ARIMA")

3. plotFanChart и 3 интервала (80%, 90%, and 95% )

  • Выбирать данные с PIs 80%, 90% and 95%
col <- c("series_id", "method", "forecast", "horizon", "timestamp", "origin_timestamp", "lo80", "hi80", "lo90", "hi90", "lo95", "hi95")
fs_3 <- fs[, col]
head(fs_3)
##        series_id method forecast horizon timestamp origin_timestamp
## 130033      M302  ARIMA 1535.058       1   1993-10          1993-09
## 130034      M302  ARIMA 1723.306       2   1993-11          1993-09
## 130035      M302  ARIMA 1259.472       3   1993-12          1993-09
## 130036      M302  ARIMA 1662.239       4   1994-01          1993-09
## 130037      M302  ARIMA 1382.579       5   1994-02          1993-09
## 130038      M302  ARIMA 1736.022       6   1994-03          1993-09
##             lo80     hi80       lo90     hi90      lo95     hi95
## 130033 298.23209 2771.883  -52.39078 3122.506 -356.5038 3426.619
## 130034 458.14247 2988.470   99.48609 3347.127 -211.5948 3658.207
## 130035 -33.40953 2552.353 -399.92336 2918.867 -717.8194 3236.763
## 130036 342.22239 2982.255  -31.98395 3356.462 -356.5521 3681.030
## 130037  35.97354 2729.184 -345.77033 3110.928 -676.8762 3442.034
## 130038 363.34347 3108.701  -25.79195 3497.837 -363.3089 3835.354
  • Создать колонку timestamp_dbo и построить fan chart
library(forvision)
fs_3$timestamp_dbo <- as.yearmon(fs_3$timestamp, format = '%Y-%m')
plotFanChart(ts = ts, fs = fs_3, id = "M302", origin = "1993-09", m = "ARIMA")