library(forecast)
## Warning: package 'forecast' was built under R version 4.4.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(ggplot2)
library(tseries)
## Warning: package 'tseries' was built under R version 4.4.3
data <- read.csv("D:/Semester 4/Pengantar Analisis Deret Waktu/Inflow BI.csv",header=TRUE)
data <- data[ , !grepl("^X(\\.|$)", names(data))]
data <- data[rowSums(is.na(data)) != ncol(data), ]
ts_data <- ts(data$Indonesia, start = c(2014, 1), end = c(2025,4),frequency=12)
ts_training_range <- window(ts_data,start = c(2014,1),end = c(2023,12))
ts_test_range <- window(ts_data, start = c(2024, 1), end = c(2025, 4))
K <- 6
fourier_terms_all <- fourier(ts_data,K=K)
dummy_vars <- data[, c("AT1", "AT2", "AT3", "AT4", "AFT1", "AFT2", "AFT3", "AFT4")]
xreg_all <- cbind(fourier_terms_all, dummy_vars)
n_test <- length(ts_test_range)
n_train <- length(ts_training_range)
x_reg_training <- xreg_all[1:n_train,]
x_reg_training <- as.matrix(sapply(x_reg_training, as.numeric))
x_reg_test <- xreg_all[(n_train+1):(n_train+n_test),]
x_reg_test <- as.matrix(sapply(x_reg_test, as.numeric))
fit <- auto.arima(ts_training_range, xreg = x_reg_training, seasonal = FALSE)
fc <- forecast(fit, xreg = x_reg_test, h = n_test)
autoplot(fc) +
autolayer(ts_test_range, series = "Aktual", color = "red") +
ggtitle("Forecast Inflow BI (ARIMA + Fourier + Dummy Idul Fitri)") +
xlab("Tahun") + ylab("Inflow") +
theme_minimal()
pred_vs_actual <- data.frame(
Tahun = time(ts_test_range),
Aktual = as.numeric(ts_test_range),
Prediksi = as.numeric(fc$mean)
)
ggplot(pred_vs_actual, aes(x = Tahun)) +
geom_line(aes(y = Aktual, color = "Aktual"), size = 1.2) +
geom_line(aes(y = Prediksi, color = "Prediksi"), size = 1.2, linetype = "dashed") +
scale_color_manual(values = c("Aktual" = "red", "Prediksi" = "blue")) +
ggtitle("Perbandingan Prediksi vs Aktual (2024–2025) (data test)") +
xlab("Tahun") + ylab("Inflow BI") +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.
accuracy(fc, ts_test_range)
## ME RMSE MAE MPE MAPE MASE
## Training set 697.8787 8045.335 5927.289 -0.2795564 12.66037 0.5514597
## Test set 1379.4761 9710.190 7795.344 -7.0126524 17.84432 0.7252588
## ACF1 Theil's U
## Training set -0.06658917 NA
## Test set 0.11637381 0.1907084
Data forecast model full
fc
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Jan 2024 88222.98 76871.48 99574.48 70862.361 105583.60
## Feb 2024 45309.45 33750.79 56868.12 27632.006 62986.90
## Mar 2024 40638.09 28875.92 52400.27 22649.396 58626.79
## Apr 2024 88943.17 76980.94 100905.40 70648.519 107237.82
## May 2024 63670.22 51511.23 75829.21 45074.652 82265.79
## Jun 2024 37432.59 25079.97 49785.21 18540.892 56324.29
## Jul 2024 48008.05 35464.79 60551.30 28824.796 67191.30
## Aug 2024 43561.80 30830.76 56292.84 24091.352 63032.24
## Sep 2024 42559.20 29643.11 55475.30 22805.743 62312.67
## Oct 2024 41984.72 28886.18 55083.25 21952.238 62017.20
## Nov 2024 40923.00 27644.53 54201.47 20615.337 61230.67
## Dec 2024 26247.20 12791.21 39703.20 5668.034 46826.38
## Jan 2025 88222.98 74591.77 101854.19 67375.842 109070.12
## Feb 2025 45309.45 31505.25 59113.66 24197.745 66421.16
## Mar 2025 25440.25 11465.20 39415.31 4067.251 46813.26
## Apr 2025 93330.88 79187.03 107474.72 71699.736 114962.02
summary(fit)
## Series: ts_training_range
## Regression with ARIMA(0,1,1) errors
##
## Coefficients:
## ma1 S1-12 C1-12 S2-12 C2-12 S3-12 C3-12
## -0.8081 2568.949 4396.828 9207.3064 1067.926 7198.5512 -3081.9249
## s.e. 0.0583 1042.542 1210.604 989.7304 1043.770 975.3356 979.5855
## S4-12 C4-12 S5-12 C5-12 C6-12 AT1
## 6187.6712 -7721.6912 3669.0464 -6907.5954 -5947.3017 77843.457
## s.e. 978.5025 959.0988 963.2136 950.8662 671.0497 4773.268
## AT2 AT3 AT4 AFT1 AFT2 AFT3
## 52516.958 24177.090 -15197.84 8028.001 21691.958 30235.114
## s.e. 5826.968 5757.976 4777.99 4757.014 5881.427 5753.763
## AFT4
## 56904.668
## s.e. 4759.408
##
## sigma^2 = 78457480: log likelihood = -1240.03
## AIC=2522.06 AICc=2531.59 BIC=2580.42
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 697.8787 8045.335 5927.289 -0.2795564 12.66037 0.5514597
## ACF1
## Training set -0.06658917
checkresiduals(fit)
##
## Ljung-Box test
##
## data: Residuals from Regression with ARIMA(0,1,1) errors
## Q* = 12.411, df = 23, p-value = 0.9636
##
## Model df: 1. Total lags used: 24
Model Reduced
xreg_all_reduced <- xreg_all[, !(colnames(xreg_all) %in% c("C2-12", "AFT1"))]
x_reg_training_reduced <- xreg_all_reduced[1:n_train,]
x_reg_training_reduced <- as.matrix(sapply(x_reg_training_reduced, as.numeric))
x_reg_test_reduced <- xreg_all_reduced[(n_train+1):(n_train+n_test),]
x_reg_test_reduced <- as.matrix(sapply(x_reg_test_reduced, as.numeric))
fit_reduced <- auto.arima(ts_training_range, xreg = x_reg_training_reduced, seasonal = FALSE)
fc_reduced <- forecast(fit_reduced, xreg = x_reg_test_reduced, h = n_test)
summary(fit_reduced)
## Series: ts_training_range
## Regression with ARIMA(0,1,1) errors
##
## Coefficients:
## ma1 S1-12 C1-12 S2-12 S3-12 C3-12 S4-12
## -0.8081 2387.872 4105.114 9454.8392 7056.1424 -3048.704 6195.7630
## s.e. 0.0585 1054.343 1172.709 995.2513 988.9863 995.195 997.0017
## C4-12 S5-12 C5-12 C6-12 AT1 AT2
## -7744.778 3715.7709 -6993.6813 -5877.2165 77646.770 53329.827
## s.e. 977.062 980.9548 967.9323 682.8242 4683.485 5805.711
## AT3 AT4 AFT2 AFT3 AFT4
## 23824.942 -14712.718 22079.143 29917.896 56722.871
## s.e. 5854.428 4724.641 5831.388 5851.681 4760.607
##
## sigma^2 = 79843955: log likelihood = -1242.26
## AIC=2522.52 AICc=2530.2 BIC=2575.33
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 700.8207 8197.682 5962.466 -0.2015137 12.65962 0.5547325
## ACF1
## Training set -0.0506701
Ljung Box
checkresiduals(fit_reduced)
##
## Ljung-Box test
##
## data: Residuals from Regression with ARIMA(0,1,1) errors
## Q* = 12.319, df = 23, p-value = 0.9652
##
## Model df: 1. Total lags used: 24
Forecast reduced
fc_reduced
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Jan 2024 87706.73 76255.36 99158.09 70193.380 105220.07
## Feb 2024 45955.54 34295.13 57615.95 28122.479 63788.60
## Mar 2024 41863.44 29997.66 53729.22 23716.294 60010.58
## Apr 2024 90431.64 78363.99 102499.29 71975.758 108887.52
## May 2024 63454.55 51188.35 75720.76 44695.012 82214.09
## Jun 2024 36998.77 24537.18 49460.37 17940.414 56057.13
## Jul 2024 48267.12 35613.16 60921.08 28914.552 67619.69
## Aug 2024 45046.69 32203.23 57890.14 25404.318 64689.06
## Sep 2024 43768.44 30738.25 56798.63 23840.481 63696.39
## Oct 2024 42610.64 29396.36 55824.93 22401.135 62820.15
## Nov 2024 40383.16 26987.31 53779.01 19895.969 60870.35
## Dec 2024 25124.23 11549.24 38699.23 4363.074 45885.39
## Jan 2025 87706.73 73954.93 101458.52 66675.166 108738.28
## Feb 2025 45955.54 32029.18 59881.90 24657.012 67254.07
## Mar 2025 27150.72 13051.96 41249.48 5588.529 48712.91
## Apr 2025 93824.68 79555.61 108093.76 72002.017 115647.35
Accuracy full vs reduced
akurasifull <- accuracy(fc,ts_test_range)
akurasireduced <- accuracy(fc_reduced,ts_test_range)
akurasifull
## ME RMSE MAE MPE MAPE MASE
## Training set 697.8787 8045.335 5927.289 -0.2795564 12.66037 0.5514597
## Test set 1379.4761 9710.190 7795.344 -7.0126524 17.84432 0.7252588
## ACF1 Theil's U
## Training set -0.06658917 NA
## Test set 0.11637381 0.1907084
akurasireduced
## ME RMSE MAE MPE MAPE MASE
## Training set 700.8207 8197.682 5962.466 -0.2015137 12.65962 0.5547325
## Test set 976.6913 9621.998 7939.730 -7.8943581 18.38797 0.7386921
## ACF1 Theil's U
## Training set -0.05067010 NA
## Test set 0.08864385 0.1875357