1. Memuat Paket dan Data China

library(dplyr)
library(tidyr)
library(forecast)
library(ggplot2)
library(knitr)

file_path <- "P_Data_Extract_From_World_Development_Indicators.xlsx"

if (file.exists(file_path) && requireNamespace("readxl", quietly = TRUE)) {
  raw_df <- readxl::read_excel(file_path, sheet = "Data")
  china_long <- raw_df %>%
    filter(`Country Name` == "China") %>%
    pivot_longer(
      cols = matches("\\[YR\\d{4}\\]"),
      names_to = "Kolom_Tahun",
      values_to = "Nilai_Raw"
    ) %>%
    mutate(
      Tahun = as.integer(gsub(".*\\[YR(\\d{4})\\]", "\\1", Kolom_Tahun)),
      Nilai = suppressWarnings(as.numeric(Nilai_Raw))
    ) %>%
    select(`Series Code`, `Series Name`, Tahun, Nilai) %>%
    arrange(`Series Code`, Tahun)
} else {
  tahun_vec <- 2000:2024
  china_long <- bind_rows(
    data.frame(
      `Series Code` = "NE.GDI.FTOT.ZS",
      `Series Name` = "Gross fixed capital formation (% of GDP)",
      Tahun = tahun_vec,
      Nilai = c(32.647179, 33.459882, 34.987864, 38.084681, 39.350537, 39.270329, 38.521573,
                37.670987, 38.803081, 43.409767, 43.520182, 43.515391, 43.873708, 44.075543,
                43.380347, 41.616671, 41.080580, 41.418490, 42.380376, 42.250349, 41.849411,
                41.352120, 41.232932, 40.454435, 39.651933),
      check.names = FALSE
    ),
    data.frame(
      `Series Code` = "SL.AGR.EMPL.ZS",
      `Series Name` = "Employment in agriculture (% of total employment) (modeled ILO estimate)",
      Tahun = tahun_vec,
      Nilai = c(50.039561, 50.000008, 49.999993, 49.099489, 46.900252, 44.800196, 42.600488,
                40.800044, 39.600071, 38.099885, 36.700610, 34.799791, 33.600592, 31.400286,
                29.500473, 28.300472, 27.422125, 26.683583, 25.751495, 24.721990, 23.599860,
                22.868778, 24.080109, 22.800877, 22.216498),
      check.names = FALSE
    ),
    data.frame(
      `Series Code` = "FR.INR.DPST",
      `Series Name` = "Deposit interest rate (%)",
      Tahun = tahun_vec,
      Nilai = c(2.25, 2.25, 1.98, 1.98, 2.25, 2.25, 2.52,
                4.14, 2.25, 2.25, 2.75, 3.50, 3.00, 3.00,
                2.75, 1.50, 1.50, 1.50, 1.50, 1.50, 1.50,
                1.50, 1.50, 1.50, 1.50),
      check.names = FALSE
    ),
    data.frame(
      `Series Code` = "NY.GDP.TOTL.RT.ZS",
      `Series Name` = "Total natural resources rents (% of GDP)",
      Tahun = tahun_vec,
      Nilai = c(2.697740, 2.592377, 1.956296, 2.017907, 4.724876, 5.102773, 5.521198,
                6.327879, 9.648414, 3.917821, 6.286726, 7.685203, 4.088493, 2.983072,
                2.310135, 1.174767, 1.052474, 1.314238, 1.425184, 1.266914, 0.863775,
                1.707565, NA, NA, NA),
      check.names = FALSE
    )
  )
}

ringkasan_china <- china_long %>%
  filter(!is.na(Nilai)) %>%
  group_by(`Series Code`, `Series Name`) %>%
  summarise(
    N = n(),
    Tahun_Mulai = min(Tahun),
    Tahun_Akhir = max(Tahun),
    Rata_Rata = round(mean(Nilai), 4),
    Median = round(median(Nilai), 4),
    Std_Dev = round(sd(Nilai), 4),
    Min = round(min(Nilai), 4),
    Max = round(max(Nilai), 4),
    .groups = "drop"
  )

kable(ringkasan_china, caption = "Ringkasan Statistik Data China")
Ringkasan Statistik Data China
Series Code Series Name N Tahun_Mulai Tahun_Akhir Rata_Rata Median Std_Dev Min Max
FR.INR.DPST Deposit interest rate (%) 25 2000 2024 2.1648 2.2500 0.7194 1.5000 4.1400
NE.GDI.FTOT.ZS Gross fixed capital formation (% of GDP) 25 2000 2024 40.3143 41.2329 3.1371 32.6472 44.0755
NY.GDP.TOTL.RT.ZS Total natural resources rents (% of GDP) 22 2000 2021 3.4848 2.6451 2.4212 0.8638 9.6484
SL.AGR.EMPL.ZS Employment in agriculture (% of total employment) (modeled ILO estimate) 25 2000 2024 34.6555 33.6006 9.8978 22.2165 50.0396

2. Visualisasi Indikator China

china_long %>%
  filter(!is.na(Nilai)) %>%
  ggplot(aes(x = Tahun, y = Nilai)) +
  geom_line(color = "#1f77b4", linewidth = 1) +
  geom_point(color = "#1f77b4", size = 1.8) +
  facet_wrap(~ `Series Name`, scales = "free_y", ncol = 2) +
  labs(
    title = "Indikator Deret Waktu China (2000–2024)",
    x = "Tahun",
    y = "Nilai"
  ) +
  theme_minimal()

3. Stasioneritas & Pembagian Data Train/Test

target_code <- "NE.GDI.FTOT.ZS"

df_sub <- china_long %>%
  filter(`Series Code` == target_code, !is.na(Nilai))

nama_indikator <- unique(df_sub$`Series Name`)
ts_china <- ts(df_sub$Nilai, start = min(df_sub$Tahun), frequency = 1)

cat("Indikator terpilih                 :", nama_indikator, "\n")
## Indikator terpilih                 : Gross fixed capital formation (% of GDP)
cat("Jumlah differencing disarankan (d) :", ndiffs(ts_china), "\n")
## Jumlah differencing disarankan (d) : 1
cat("Ljung-Box p-value (lag 5)          :", round(Box.test(ts_china, lag = 5, type = "Ljung-Box")$p.value, 4), "\n")
## Ljung-Box p-value (lag 5)          : 0
par(mfrow = c(1, 2))
acf(ts_china, main = "ACF (Data Level)")
pacf(ts_china, main = "PACF (Data Level)")

par(mfrow = c(1, 1))

n_obs <- length(ts_china)
n_train <- floor(0.8 * n_obs)
n_test <- n_obs - n_train

train_ts <- window(ts_china, end = time(ts_china)[n_train])
test_ts  <- window(ts_china, start = time(ts_china)[n_train + 1])

4. Pemodelan AR, MA, ARMA, dan Model Lainnya

mod_ar    <- Arima(train_ts, order = c(1, 0, 0))
mod_ma    <- Arima(train_ts, order = c(0, 0, 1))
mod_arma  <- Arima(train_ts, order = c(1, 0, 1))
mod_arima <- Arima(train_ts, order = c(1, 1, 1))
mod_auto  <- auto.arima(train_ts, seasonal = FALSE)
mod_holt  <- holt(train_ts, h = n_test)
mod_ets   <- ets(train_ts)

fc_ar    <- forecast(mod_ar, h = n_test)
fc_ma    <- forecast(mod_ma, h = n_test)
fc_arma  <- forecast(mod_arma, h = n_test)
fc_arima <- forecast(mod_arima, h = n_test)
fc_auto  <- forecast(mod_auto, h = n_test)
fc_ets   <- forecast(mod_ets, h = n_test)

summary(mod_ar)
## Series: train_ts 
## ARIMA(1,0,0) with non-zero mean 
## 
## Coefficients:
##          ar1     mean
##       0.9380  38.5304
## s.e.  0.0674   3.4696
## 
## sigma^2 = 2.463:  log likelihood = -37.4
## AIC=80.8   AICc=82.3   BIC=83.79
## 
## Training set error measures:
##                     ME     RMSE      MAE      MPE     MAPE    MASE      ACF1
## Training set 0.4680784 1.488971 1.046264 1.094838 2.651876 1.03419 0.2446508
summary(mod_ma)
## Series: train_ts 
## ARIMA(0,0,1) with non-zero mean 
## 
## Coefficients:
##          ma1     mean
##       0.8553  40.0079
## s.e.  0.1283   0.8612
## 
## sigma^2 = 5.005:  log likelihood = -44.09
## AIC=94.17   AICc=95.67   BIC=97.16
## 
## Training set error measures:
##                   ME     RMSE      MAE        MPE    MAPE     MASE      ACF1
## Training set 0.13061 2.122352 1.605593 -0.1156417 4.15102 1.587064 0.4156517
summary(mod_arma)
## Series: train_ts 
## ARIMA(1,0,1) with non-zero mean 
## 
## Coefficients:
##          ar1     ma1     mean
##       0.8831  0.5011  38.8322
## s.e.  0.1086  0.1873   2.9071
## 
## sigma^2 = 2.021:  log likelihood = -35.06
## AIC=78.11   AICc=80.78   BIC=82.09
## 
## Training set error measures:
##                     ME     RMSE       MAE       MPE     MAPE     MASE
## Training set 0.3166194 1.310568 0.9428653 0.7212279 2.365605 0.931984
##                     ACF1
## Training set -0.09318985

5. Evaluasi Model & Grafik Prediksi

tabel_metrik <- bind_rows(
  data.frame(
    Model = "AR(1)",
    AIC = round(AIC(mod_ar), 2),
    RMSE = round(accuracy(fc_ar, test_ts)["Test set", "RMSE"], 4),
    MAE  = round(accuracy(fc_ar, test_ts)["Test set", "MAE"], 4),
    MAPE = round(accuracy(fc_ar, test_ts)["Test set", "MAPE"], 4)
  ),
  data.frame(
    Model = "MA(1)",
    AIC = round(AIC(mod_ma), 2),
    RMSE = round(accuracy(fc_ma, test_ts)["Test set", "RMSE"], 4),
    MAE  = round(accuracy(fc_ma, test_ts)["Test set", "MAE"], 4),
    MAPE = round(accuracy(fc_ma, test_ts)["Test set", "MAPE"], 4)
  ),
  data.frame(
    Model = "ARMA(1,1)",
    AIC = round(AIC(mod_arma), 2),
    RMSE = round(accuracy(fc_arma, test_ts)["Test set", "RMSE"], 4),
    MAE  = round(accuracy(fc_arma, test_ts)["Test set", "MAE"], 4),
    MAPE = round(accuracy(fc_arma, test_ts)["Test set", "MAPE"], 4)
  ),
  data.frame(
    Model = "ARIMA(1,1,1)",
    AIC = round(AIC(mod_arima), 2),
    RMSE = round(accuracy(fc_arima, test_ts)["Test set", "RMSE"], 4),
    MAE  = round(accuracy(fc_arima, test_ts)["Test set", "MAE"], 4),
    MAPE = round(accuracy(fc_arima, test_ts)["Test set", "MAPE"], 4)
  ),
  data.frame(
    Model = "Auto ARIMA",
    AIC = round(AIC(mod_auto), 2),
    RMSE = round(accuracy(fc_auto, test_ts)["Test set", "RMSE"], 4),
    MAE  = round(accuracy(fc_auto, test_ts)["Test set", "MAE"], 4),
    MAPE = round(accuracy(fc_auto, test_ts)["Test set", "MAPE"], 4)
  ),
  data.frame(
    Model = "Holt's Trend",
    AIC = round(AIC(mod_holt$model), 2),
    RMSE = round(accuracy(mod_holt, test_ts)["Test set", "RMSE"], 4),
    MAE  = round(accuracy(mod_holt, test_ts)["Test set", "MAE"], 4),
    MAPE = round(accuracy(mod_holt, test_ts)["Test set", "MAPE"], 4)
  ),
  data.frame(
    Model = "ETS",
    AIC = round(AIC(mod_ets), 2),
    RMSE = round(accuracy(fc_ets, test_ts)["Test set", "RMSE"], 4),
    MAE  = round(accuracy(fc_ets, test_ts)["Test set", "MAE"], 4),
    MAPE = round(accuracy(fc_ets, test_ts)["Test set", "MAPE"], 4)
  )
)

kable(tabel_metrik, caption = paste("Evaluasi Model pada Data Test —", nama_indikator))
Evaluasi Model pada Data Test — Gross fixed capital formation (% of GDP)
Model AIC RMSE MAE MAPE
AR(1) 80.80 0.8688 0.7046 1.7464
MA(1) 94.17 0.9463 0.8581 2.0836
ARMA(1,1) 78.11 0.4621 0.3234 0.8052
ARIMA(1,1,1) 71.27 1.3337 1.0915 2.7049
Auto ARIMA 69.30 1.3396 1.0955 2.7147
Holt’s Trend 83.24 3.2687 2.9089 7.1820
ETS 81.27 1.5479 1.3422 3.3181
thn_test <- as.numeric(time(test_ts))

df_plot <- bind_rows(
  data.frame(Tahun = as.numeric(time(ts_china)), Nilai = as.numeric(ts_china), Model = "Aktual"),
  data.frame(Tahun = thn_test, Nilai = as.numeric(fc_ar$mean),    Model = "AR(1)"),
  data.frame(Tahun = thn_test, Nilai = as.numeric(fc_ma$mean),    Model = "MA(1)"),
  data.frame(Tahun = thn_test, Nilai = as.numeric(fc_arma$mean),  Model = "ARMA(1,1)"),
  data.frame(Tahun = thn_test, Nilai = as.numeric(fc_arima$mean), Model = "ARIMA(1,1,1)"),
  data.frame(Tahun = thn_test, Nilai = as.numeric(fc_auto$mean),  Model = "Auto ARIMA"),
  data.frame(Tahun = thn_test, Nilai = as.numeric(mod_holt$mean), Model = "Holt's Trend")
)

ggplot(df_plot, aes(x = Tahun, y = Nilai, color = Model, linetype = Model)) +
  geom_line(linewidth = 0.9) +
  geom_point(size = 2) +
  labs(
    title = paste("Perbandingan Prediksi Model —", nama_indikator),
    subtitle = "China (Data Test)",
    x = "Tahun",
    y = "Nilai"
  ) +
  theme_minimal()

fit_full <- Arima(ts_china, order = c(1, 0, 1))
fc_full  <- forecast(fit_full, h = 5)
print(fc_full)
##      Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## 2025       39.38956 37.77558 41.00354 36.92119 41.85792
## 2026       39.29540 36.53417 42.05663 35.07246 43.51834
## 2027       39.21172 35.80749 42.61596 34.00539 44.41806
## 2028       39.13736 35.30069 42.97402 33.26968 45.00503
## 2029       39.07127 34.92482 43.21772 32.72982 45.41272
autoplot(fc_full) +
  labs(
    title = paste("Forecast 5 Tahun ke Depan (ARMA(1,1)) —", nama_indikator),
    x = "Tahun",
    y = "Nilai"
  ) +
  theme_minimal()