Analisis Forecasting Data Time Series (Lanjutan)

Nidia Mindiyarti

2023-01-12

Persiapan Data

data <- read.csv("D:/R/Time Series/Data Revenue & Package 2019-2022.csv", header = TRUE)#data_test
data <- within(data, {
        berat <- NULL 
        revenue <- NULL
        })
head(data)
##      periode volume
## 1 2019-01-01   6977
## 2 2019-01-02  84942
## 3 2019-01-03  88100
## 4 2019-01-04  78848
## 5 2019-01-05  58016
## 6 2019-01-06  12541
data <- data %>% 
  mutate(date = ymd(periode))

range(data$date)
## [1] "2019-01-01" "2023-01-12"
colSums(is.na(data))
## periode  volume    date 
##       0       0       0

Membentuk Data Time Series

volume_ts %>% ts_plot()

Cek Dekomposisi Data

# Membagi Data Training dan Data Testing
data_train <- head(volume_ts, 1426)
data_test <- tail(volume_ts, length(volume_ts)-length(data_train))
#This will plot the time series
ts.plot(data_train, xlab="Periode", ylab="Jumlah Volume", main="Volume Harian Sicepat, 2019-2022")

# This will fit in a line
abline(reg=lm(data_train~time(data_train)))

# Dekomposisi Time Series
data_train%>% decompose() %>% autoplot()

Pemodelan

Holt’s Winters Exponential (Triple Exponential Smoothing)

# Pemodelan Holt's Winter Exponential
model_hoW <- HoltWinters(data_train)

# Evaluasi Model
model_how_forecast <- forecast(object = model_hoW, h = 43)

volume_ts %>% autoplot(series = "data_train")+
  autolayer(data_test, series = "data_test") +
  autolayer(model_how_forecast$mean, series = "forecast")

accuracy(model_how_forecast$mean, x = data_test)
##                 ME     RMSE      MAE       MPE     MAPE      ACF1 Theil's U
## Test set -107063.3 237543.4 200031.1 -37.38803 46.83678 0.3155771 0.3229196
# Model STLM
model_stlm <- stlm(y = data_train, s.window = 43, method = "ets")

model_stlm_forecast <- forecast(model_stlm, h = 43)

volume_ts %>% autoplot(series = "Data Training")+
  autolayer(data_test, series = "Data Testing") +
  autolayer(model_stlm_forecast$mean, series = "forecast")

accuracy(model_stlm_forecast$mean, x = data_test)
##                 ME     RMSE      MAE       MPE     MAPE      ACF1 Theil's U
## Test set -76642.86 232119.5 191845.3 -34.38522 46.03675 0.2836827 0.3581627

Model Terbaik

Hasil <- data.frame(
   Forecast  = model_how_forecast$mean,
   Volume_Real = data_test
)

Percentage <- with(Hasil, (abs(Forecast - Volume_Real)/Volume_Real)*100)
Fore_per_Act <- with(Hasil, (Forecast/Volume_Real))
Fore_per_Act <- round(Fore_per_Act, 2)
Percentage <- round(Percentage, 2)
#Periode <- data.frame(as.matrix(Percentage), date=time(Percentage))
#Periode <- Periode[,-1]
Hasil_Forecast <- data.frame(Hasil,Fore_per_Act, Percentage)
Hasil_Forecast
##     Forecast Volume_Real Fore_per_Act Percentage
## 1   630856.3      756184         0.83      16.57
## 2   682490.4      763572         0.89      10.62
## 3   501733.4      650713         0.77      22.89
## 4   669687.0      290657         2.30     130.40
## 5  1153810.0     1054729         1.09       9.39
## 6   829390.9      870612         0.95       4.73
## 7   840897.3      796187         1.06       5.62
## 8   723865.5      675026         1.07       7.24
## 9   907401.8      672990         1.35      34.83
## 10  791596.9      606972         1.30      30.42
## 11  563543.2      249059         2.26     126.27
## 12  955951.8     1358276         0.70      29.62
## 13  739814.8     1238655         0.60      40.27
## 14  833254.0      880731         0.95       5.39
## 15  665063.6      673929         0.99       1.32
## 16  767227.9      572577         1.34      34.00
## 17  661667.8      482464         1.37      37.14
## 18  405790.3      171566         2.37     136.52
## 19  882077.3      753151         1.17      17.12
## 20  664185.9      570820         1.16      16.36
## 21  734448.2      533562         1.38      37.65
## 22  597132.5      502364         1.19      18.86
## 23  716941.9      497000         1.44      44.25
## 24  627022.6      433156         1.45      44.76
## 25  470144.8      190734         2.46     146.49
## 26  763139.2      930048         0.82      17.95
## 27  737230.8      761210         0.97       3.15
## 28  864351.4      653428         1.32      32.28
## 29  673185.2      596132         1.13      12.93
## 30  757842.8      518991         1.46      46.02
## 31  701531.5      351566         2.00      99.54
## 32  495669.5      143666         3.45     245.02
## 33  898226.9     1021083         0.88      12.03
## 34  526456.0      857401         0.61      38.60
## 35 1124475.6      708520         1.59      58.71
## 36 1001567.2      655426         1.53      52.81
## 37  972889.7      600668         1.62      61.97
## 38  834092.4      505451         1.65      65.02
## 39  592669.3      196722         3.01     201.27
## 40  924922.1      852717         1.08       8.47
## 41  724631.5      709323         1.02       2.16
## 42  846835.5      654272         1.29      29.43
## 43  726995.1      616674         1.18      17.89