title: “Analisis Deret Waktu (AR, MA, ARMA, ARIMA) - Indonesia”
author: “Afrodita E. Pendong”
date: “2026-11-19”
output:
html_document:
theme: cosmo
geometry: margin=1.5cm
# 1. Memuat Paket
library(readxl)
library(dplyr)
library(tidyr)
library(stringr)
library(forecast)
library(ggplot2)
library(knitr)
# 2. Memuat & Memfilter Data WDI Khusus Indonesia
df_raw <- read_excel("C:/Users/User/Downloads/Data WDI 15 Negara.xlsx", sheet = 1)
col_neg <- names(df_raw)[str_detect(tolower(names(df_raw)), "country|negara")][1]
df_indo <- df_raw %>% filter(tolower(as.character(.data[[col_neg]])) == "indonesia")
kolom_thn <- names(df_indo)[str_detect(names(df_indo), "^[0-9]{4}")]
if (length(kolom_thn) > 3) {
col_ind <- names(df_indo)[str_detect(tolower(names(df_indo)), "series|indicator|indikator")][1]
if (!is.na(col_ind)) df_indo <- df_indo %>% filter(.data[[col_ind]] == unique(.data[[col_ind]])[1])
df_clean <- df_indo %>% select(all_of(kolom_thn)) %>%
pivot_longer(everything(), names_to = "Tahun", values_to = "Nilai") %>%
transmute(Tahun = as.numeric(str_sub(Tahun, 1, 4)), Nilai = suppressWarnings(as.numeric(as.character(Nilai)))) %>%
filter(!is.na(Nilai)) %>% arrange(Tahun)
} else {
col_wkt <- names(df_indo)[str_detect(tolower(names(df_indo)), "year|tahun")][1]
col_nil <- tail(names(df_indo)[sapply(df_indo, is.numeric) & names(df_indo) != col_wkt], 1)
df_clean <- df_indo %>% transmute(Tahun = as.numeric(str_sub(as.character(.data[[col_wkt]]), 1, 4)),
Nilai = suppressWarnings(as.numeric(as.character(.data[[col_nil]])))) %>% filter(!is.na(Nilai)) %>% arrange(Tahun)
}
# 3. Uji Stasioneritas & Plot ACF/PACF
ts_indo <- ts(df_clean$Nilai, start = min(df_clean$Tahun), frequency = 1)
d_opt <- ndiffs(ts_indo, test = "adf")
ts_diff <- if (d_opt > 0) diff(ts_indo, differences = d_opt) else ts_indo
cat(sprintf("Observasi: %d (%d-%d) | Rekomendasi Differencing (d) via ADF: %d\n",
length(ts_indo), min(df_clean$Tahun), max(df_clean$Tahun), d_opt))
## Observasi: 25 (2000-2024) | Rekomendasi Differencing (d) via ADF: 2
par(mfrow = c(1, 2), mar = c(3, 3, 2, 1))
acf(ts_diff, main = paste0("ACF (d=", d_opt, ")"))
pacf(ts_diff, main = paste0("PACF (d=", d_opt, ")"))
# 4. Estimasi Model AR, MA, ARMA, ARIMA & Perbandingan AIC/BIC
p <- 1; q <- 1
models <- list(
"AR(1)" = Arima(ts_indo, order = c(p, 0, 0)),
"MA(1)" = Arima(ts_indo, order = c(0, 0, q)),
"ARMA(1,1)" = Arima(ts_indo, order = c(p, 0, q)),
"ARIMA(p,d,q)" = Arima(ts_indo, order = c(p, d_opt, q)),
"Auto-ARIMA" = auto.arima(ts_indo, seasonal = FALSE)
)
kable(data.frame(Model = names(models), AIC = sapply(models, AIC), BIC = sapply(models, BIC)) %>% arrange(AIC),
digits = 2, row.names = FALSE, caption = "Perbandingan Model (AIC & BIC Terkecil = Terbaik)")
| Model | AIC | BIC |
|---|---|---|
| Auto-ARIMA | 76.90 | 79.17 |
| ARIMA(p,d,q) | 78.35 | 81.75 |
| ARMA(1,1) | 86.80 | 91.68 |
| AR(1) | 90.67 | 94.33 |
| MA(1) | 125.88 | 129.54 |
# 5. Koefisien Model Terbaik & Prediksi 5 Tahun ke Depan
best_model <- models[["Auto-ARIMA"]]
print(coef(best_model))
## ma1
## -0.6766024
fc <- forecast(best_model, h = 5)
autoplot(fc) + labs(title = paste("Proyeksi Indonesia -", as.character(best_model)), x = "Tahun", y = "Nilai") + theme_minimal()