# Load packages
library(tseries)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(TSA)
##
## Attaching package: 'TSA'
## The following objects are masked from 'package:stats':
##
## acf, arima
## The following object is masked from 'package:utils':
##
## tar
library(forecast)
## Registered S3 methods overwritten by 'forecast':
## method from
## fitted.Arima TSA
## plot.Arima TSA
set.seed(123) # Agar hasil replikasi konsisten
n <- 200
ar <- 0.7
ma <- -0.5
ts_arima <- arima.sim(model = list(order = c(1,1,1), ar = ar, ma = ma), n = n)
ts.plot(ts_arima, main = "Simulasi Data ARIMA(1,1,1)", ylab = "Value", col = "blue")
Data ini merupakan simulasi ARIMA dengan 1 autoregressive, 1
differencing, dan 1 moving average.
acf(ts_arima, main = "ACF Data Asli")
pacf(ts_arima, main = "PACF Data Asli")
adf.test(ts_arima)
##
## Augmented Dickey-Fuller Test
##
## data: ts_arima
## Dickey-Fuller = -2.449, Lag order = 5, p-value = 0.388
## alternative hypothesis: stationary
Hasil ADF Test: Nilai p-value > 0.05 → data belum stasioner, perlu dilakukan differencing.
diff1 <- diff(ts_arima)
acf(diff1, main = "ACF Data Setelah Differencing")
pacf(diff1, main = "PACF Data Setelah Differencing")
adf.test(diff1)
## Warning in adf.test(diff1): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: diff1
## Dickey-Fuller = -5.4572, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
Hasil ADF Test (differencing 1x): p-value < 0.05 → data sudah stasioner, analisis bisa dilanjutkan.
data.ts <- ts(diff1)
head(data.ts)
## Time Series:
## Start = 1
## End = 6
## Frequency = 1
## [1] -0.4362295 -1.1367886 -0.4798151 -1.2528876 -1.0929103 -1.0256309
acf(data.ts, main = "ACF Kandidat Model")
pacf(data.ts, main = "PACF Kandidat Model")
eacf(data.ts)
## AR/MA
## 0 1 2 3 4 5 6 7 8 9 10 11 12 13
## 0 x o o o o o o o o o o o o o
## 1 x o o o o o o o o o o o o o
## 2 x x o o o o o o o o o o o o
## 3 x x o o o o o o o o o o o o
## 4 x x o o o o o o o o o o o o
## 5 x o o o o o o o o o o o o o
## 6 x o o x o o o o o o o o o o
## 7 o x x x x o o o o o o o o o
auto_model <- auto.arima(data.ts)
auto_model
## Series: data.ts
## ARIMA(2,0,2) with zero mean
##
## Coefficients:
## ar1 ar2 ma1 ma2
## -0.1116 0.6336 0.3108 -0.6250
## s.e. 0.2175 0.1701 0.2294 0.2122
##
## sigma^2 = 0.8631: log likelihood = -267.28
## AIC=544.57 AICc=544.88 BIC=561.06
Model hasil auto.arima: ARIMA(2,0,2) Karena kita bekerja dengan data yang sudah di-difference, maka ini ekuivalen dengan ARIMA(2,1,2) untuk data asli.
m1 <- arima(data.ts, order = c(1,1,1), method = "ML")
m2 <- arima(data.ts, order = c(1,1,3), method = "ML")
m3 <- arima(data.ts, order = c(0,1,1), method = "ML")
m4 <- arima(data.ts, order = c(2,0,2), method = "ML")
# Cetak semua nilai AIC
cat("AIC Model (1,1,1):", AIC(m1), "\n")
## AIC Model (1,1,1): 553.1276
cat("AIC Model (1,1,3):", AIC(m2), "\n")
## AIC Model (1,1,3): 550.4942
cat("AIC Model (0,1,1):", AIC(m3), "\n")
## AIC Model (0,1,1): 556.2792
cat("AIC Model (2,0,2):", AIC(m4), "\n")
## AIC Model (2,0,2): 546.514