title: “Tugas ARMA”

author: “Grace Maria Alua (240611010050)”

date: “2026-11-19”

output: html_document


1. Persiapan Paket dan Pemuatan Data Jepang

Kode berikut memuat paket yang diperlukan untuk analisis deret waktu (time series), membaca file P_Data_Extract_From_World_Development_Indicators.xlsx langsung dari folder Downloads, serta memfilter seluruh indikator ekonomi dan demografi milik Jepang (Country Code == "JPN").

library(readxl)

library(dplyr)

library(tidyr)

library(tseries)

library(forecast)

library(vars)

library(knitr)



# Membaca file Excel langsung dari folder Downloads pengguna Windows

file_path <- file.path(Sys.getenv("USERPROFILE"), "Downloads", "P_Data_Extract_From_World_Development_Indicators.xlsx")

df_raw <- read_excel(file_path, sheet = "Data")



# Filter khusus negara Jepang (JPN)

df_jpn <- df_raw %>%

  filter(`Country Code` == "JPN")



# Mengubah format wide (tahun sebagai kolom) menjadi format long lalu wide berdasarkan tahun

df_long <- df_jpn %>%

  dplyr::select(Indikator = A, matches("\\[YR")) %>%

  pivot_longer(

    cols = matches("\\[YR"),

    names_to = "Tahun_Str",

    values_to = "Nilai_Str"

  ) %>%

  mutate(

    Tahun = as.integer(substr(Tahun_Str, 1, 4)),

    Nilai = suppressWarnings(as.numeric(Nilai_Str))

  ) %>%

  dplyr::select(Tahun, Indikator, Nilai) %>%

  arrange(Indikator, Tahun)



df_wide <- df_long %>%

  pivot_wider(names_from = Indikator, values_from = Nilai) %>%

  arrange(Tahun)



kable(df_wide, digits = 4, caption = "Data Deret Waktu Jepang (2001 - 2025)")
Data Deret Waktu Jepang (2001 - 2025)
Tahun Exports of goods and services (% of GDP) Foreign direct investment, net inflows (% of GDP) Imports of goods and services (% of GDP) Population growth (annual %)
2001 9.9313 0.1110 9.3459 0.2410
2002 10.6941 0.2722 9.4491 0.2325
2003 11.3308 0.1918 9.7442 0.2140
2004 12.6608 0.1523 10.7716 0.0337
2005 13.6894 0.1120 12.3016 0.0094
2006 15.4992 -0.0516 14.2157 0.0634
2007 17.1191 0.4677 15.3766 0.1149
2008 17.0551 0.4772 16.7187 0.0484
2009 12.3085 0.2291 11.8646 -0.0125
2010 14.7827 0.1280 13.4573 0.0180
2011 14.6645 -0.0135 15.3069 -0.1852
2012 14.2739 0.0086 15.9004 -0.1597
2013 15.6037 0.2020 17.9877 -0.1443
2014 17.1079 0.3962 19.6565 -0.1327
2015 17.0921 0.1158 17.6355 -0.1061
2016 15.7224 0.8014 14.9339 -0.0511
2017 17.2158 0.3732 16.4730 -0.0819
2018 17.9111 0.4906 17.8924 -0.1269
2019 17.0378 0.7618 17.3205 -0.1405
2020 15.1287 1.2061 15.3905 -0.2942
2021 17.4620 0.6703 17.9634 -0.4600
2022 20.6479 1.0826 24.2128 -0.4439
2023 21.0593 0.4563 22.5358 -0.4874
2024 21.9799 0.4505 22.8730 -0.4357
2025 NA 1.0328 NA -0.4921

2. Eksplorasi Data dan Uji Stasioneritas (ADF Test)

Model AR, MA, dan ARMA mensyaratkan data dalam kondisi stasioner. Jika data tidak stasioner pada tingkat level (\(p\text{-value} > 0,05\)), maka diperlukan proses differencing (\(d = 1\)) sehingga model berkembang menjadi ARIMA(\(p,d,q\)).

indikator_list <- unique(df_long$Indikator)



hasil_adf <- lapply(indikator_list, function(ind) {

  s <- df_long %>%

    filter(Indikator == ind, !is.na(Nilai)) %>%

    pull(Nilai)

  

  adf_lvl <- adf.test(s)

  adf_diff1 <- adf.test(diff(s))

  

  data.frame(

    Indikator = ind,

    N = length(s),

    Mean = round(mean(s), 4),

    SD = round(sd(s), 4),

    ADF_Level = round(adf_lvl$statistic, 4),

    P_Val_Level = round(adf_lvl$p.value, 4),

    ADF_Diff1 = round(adf_diff1$statistic, 4),

    P_Val_Diff1 = round(adf_diff1$p.value, 4),

    Kesimpulan = ifelse(adf_lvl$p.value <= 0.05, "Stasioner di Level", "Stasioner di 1st Difference (I(1))")

  )

}) %>% bind_rows()



kable(hasil_adf, row.names = FALSE, caption = "Statistik Deskriptif dan Uji Augmented Dickey-Fuller (ADF)")
Statistik Deskriptif dan Uji Augmented Dickey-Fuller (ADF)
Indikator N Mean SD ADF_Level P_Val_Level ADF_Diff1 P_Val_Diff1 Kesimpulan
Exports of goods and services (% of GDP) 24 15.7491 3.0855 -2.5113 0.3776 -2.7840 0.2737 Stasioner di 1st Difference (I(1))
Foreign direct investment, net inflows (% of GDP) 25 0.4050 0.3503 -2.4073 0.4172 -3.1978 0.1161 Stasioner di 1st Difference (I(1))
Imports of goods and services (% of GDP) 24 15.8053 4.0801 -2.4816 0.3889 -2.9456 0.2122 Stasioner di 1st Difference (I(1))
Population growth (annual %) 25 -0.1112 0.2226 -2.4659 0.3949 -2.9388 0.2147 Stasioner di 1st Difference (I(1))

Visualisasi Tren Historis & Plot ACF/PACF

par(mfrow = c(4, 3), mar = c(3, 3, 2.5, 1))

for (ind in indikator_list) {

  sub_df <- df_long %>% filter(Indikator == ind, !is.na(Nilai))

  ts_obj <- ts(sub_df$Nilai, start = min(sub_df$Tahun), frequency = 1)

  

  plot(ts_obj, main = paste("Tren:", substr(ind, 1, 25)), ylab = "%", xlab = "Tahun", col = "navy", lwd = 2)

  acf(diff(ts_obj), main = paste("ACF Diff1:", substr(ind, 1, 18)))

  pacf(diff(ts_obj), main = paste("PACF Diff1:", substr(ind, 1, 18)))

}

par(mfrow = c(1, 1))

3. Evaluasi Model AR, MA, ARMA, ARIMA, dan Holt’s Linear Trend

Untuk mengevaluasi performa prediksi tiap model secara objektif, data dibagi menjadi data latih (train) dan 4 tahun terakhir sebagai data uji (test). Estimasi menggunakan metode Maximum Likelihood (method = "ML") agar proses komputasi berjalan cepat dan stabil.

evaluasi_semua <- list()

model_terbaik_list <- list()



for (ind in indikator_list) {

  sub_df <- df_long %>% filter(Indikator == ind, !is.na(Nilai))

  n_total <- nrow(sub_df)

  n_test <- 4

  n_train <- n_total - n_test

  

  ts_train <- ts(sub_df$Nilai[1:n_train], start = sub_df$Tahun[1], frequency = 1)

  actual_test <- sub_df$Nilai[(n_train + 1):n_total]

  

  kandidat <- list(

    "AR(1)" = tryCatch(Arima(ts_train, order = c(1, 0, 0), method = "ML"), error = function(e) NULL),

    "MA(1)" = tryCatch(Arima(ts_train, order = c(0, 0, 1), method = "ML"), error = function(e) NULL),

    "ARMA(1,1)" = tryCatch(Arima(ts_train, order = c(1, 0, 1), method = "ML"), error = function(e) NULL),

    "ARIMA(1,1,0)" = tryCatch(Arima(ts_train, order = c(1, 1, 0), method = "ML"), error = function(e) NULL),

    "ARIMA(0,1,1)" = tryCatch(Arima(ts_train, order = c(0, 1, 1), method = "ML"), error = function(e) NULL),

    "ARIMA(1,1,1)" = tryCatch(Arima(ts_train, order = c(1, 1, 1), method = "ML"), error = function(e) NULL),

    "Holt Linear Trend" = tryCatch(holt(ts_train, h = n_test), error = function(e) NULL)

  )

  

  res_ind <- lapply(names(kandidat), function(nama_mod) {

    mod <- kandidat[[nama_mod]]

    if (is.null(mod)) return(NULL)

    

    fc <- forecast(mod, h = n_test)

    pred <- as.numeric(fc$mean)

    rmse_val <- sqrt(mean((actual_test - pred)^2))

    mae_val <- mean(abs(actual_test - pred))

    aic_val <- if (!is.null(mod$aic)) round(mod$aic, 2) else round(mod$model$aic, 2)

    bic_val <- if (!is.null(mod$bic)) round(mod$bic, 2) else round(mod$model$bic, 2)

    

    data.frame(

      Model = nama_mod,

      AIC = aic_val,

      BIC = bic_val,

      RMSE = round(rmse_val, 4),

      MAE = round(mae_val, 4)

    )

  }) %>% bind_rows() %>% arrange(RMSE)

  

  evaluasi_semua[[ind]] <- res_ind

  model_terbaik_list[[ind]] <- res_ind$Model[1]

}



for (ind in indikator_list) {

  cat("\n### Evaluasi Model:", ind, "\n")

  print(kable(evaluasi_semua[[ind]], row.names = FALSE))

}
## 
## ### Evaluasi Model: Exports of goods and services (% of GDP) 
## 
## 
## |Model             |   AIC|   BIC|   RMSE|    MAE|
## |:-----------------|-----:|-----:|------:|------:|
## |Holt Linear Trend | 88.17| 93.15| 4.3312| 4.1183|
## |ARIMA(1,1,1)      | 78.51| 81.35| 5.0820| 4.8192|
## |ARIMA(0,1,1)      | 76.80| 78.69| 5.2699| 4.9879|
## |ARIMA(1,1,0)      | 76.83| 78.72| 5.3364| 5.0573|
## |MA(1)             | 86.49| 89.47| 5.8464| 5.6221|
## |AR(1)             | 81.68| 84.66| 5.8465| 5.5522|
## |ARMA(1,1)         | 83.52| 87.51| 5.9670| 5.6895|
## 
## ### Evaluasi Model: Foreign direct investment, net inflows (% of GDP) 
## 
## 
## |Model             |   AIC|   BIC|   RMSE|    MAE|
## |:-----------------|-----:|-----:|------:|------:|
## |ARIMA(1,1,0)      |  9.05| 11.04| 0.2959| 0.2819|
## |ARIMA(0,1,1)      |  7.95|  9.94| 0.3043| 0.3022|
## |ARIMA(1,1,1)      |  9.95| 12.94| 0.3043| 0.3021|
## |Holt Linear Trend | 15.01| 20.23| 0.3291| 0.3022|
## |ARMA(1,1)         | 11.03| 15.21| 0.3462| 0.2968|
## |AR(1)             |  9.59| 12.72| 0.4383| 0.3289|
## |MA(1)             | 11.52| 14.65| 0.5119| 0.4162|
## 
## ### Evaluasi Model: Imports of goods and services (% of GDP) 
## 
## 
## |Model             |   AIC|   BIC|   RMSE|    MAE|
## |:-----------------|-----:|-----:|------:|------:|
## |Holt Linear Trend | 93.56| 98.54| 6.0965| 5.7040|
## |ARIMA(1,1,0)      | 81.85| 83.74| 7.0198| 6.6120|
## |ARIMA(1,1,1)      | 83.39| 86.23| 7.0581| 6.6671|
## |ARIMA(0,1,1)      | 81.79| 83.67| 7.1190| 6.7179|
## |AR(1)             | 87.34| 90.33| 7.4959| 7.0672|
## |MA(1)             | 93.53| 96.52| 7.8049| 7.4497|
## |ARMA(1,1)         | 88.43| 92.41| 7.8268| 7.4279|
## 
## ### Evaluasi Model: Population growth (annual %) 
## 
## 
## |Model             |    AIC|    BIC|   RMSE|    MAE|
## |:-----------------|------:|------:|------:|------:|
## |ARIMA(1,1,1)      | -36.10| -33.11| 0.0308| 0.0258|
## |ARIMA(0,1,1)      | -38.10| -36.11| 0.0344| 0.0254|
## |ARIMA(1,1,0)      | -38.09| -36.09| 0.0435| 0.0363|
## |AR(1)             | -35.26| -32.12| 0.0634| 0.0509|
## |ARMA(1,1)         | -34.27| -30.09| 0.0678| 0.0567|
## |Holt Linear Trend | -31.62| -26.39| 0.1209| 0.1175|
## |MA(1)             | -23.79| -20.65| 0.3795| 0.3638|

4. Peramalan (Forecasting) 5 Tahun ke Depan

Model dengan nilai RMSE terendah untuk masing-masing indikator dilatih kembali menggunakan seluruh data historis untuk memproyeksikan 5 periode ke depan.

par(mfrow = c(2, 2), mar = c(4, 4, 3, 1))



for (ind in indikator_list) {

  sub_df <- df_long %>% filter(Indikator == ind, !is.na(Nilai))

  ts_full <- ts(sub_df$Nilai, start = min(sub_df$Tahun), frequency = 1)

  terbaik <- model_terbaik_list[[ind]]

  

  fit_final <- switch(

    terbaik,

    "AR(1)" = Arima(ts_full, order = c(1, 0, 0), method = "ML"),

    "MA(1)" = Arima(ts_full, order = c(0, 0, 1), method = "ML"),

    "ARMA(1,1)" = Arima(ts_full, order = c(1, 0, 1), method = "ML"),

    "ARIMA(1,1,0)" = Arima(ts_full, order = c(1, 1, 0), method = "ML"),

    "ARIMA(0,1,1)" = Arima(ts_full, order = c(0, 1, 1), method = "ML"),

    "ARIMA(1,1,1)" = Arima(ts_full, order = c(1, 1, 1), method = "ML"),

    "Holt Linear Trend" = holt(ts_full, h = 5)

  )

  

  fc_final <- forecast(fit_final, h = 5)

  plot(

    fc_final,

    main = paste0(substr(ind, 1, 28), "\n(", terbaik, ")"),

    xlab = "Tahun",

    ylab = "Nilai (%)",

    col = "navy",

    lwd = 2

  )

  grid()

  

  cat("\n**Proyeksi 5 Periode ke Depan -", ind, "(Model:", terbaik, ")**\n")

  print(kable(as.data.frame(fc_final), digits = 4))

}
## 
## **Proyeksi 5 Periode ke Depan - Exports of goods and services (% of GDP) (Model: Holt Linear Trend )**
## 
## 
## |     | Point Forecast|   Lo 80|   Hi 80|   Lo 95|   Hi 95|
## |:----|--------------:|-------:|-------:|-------:|-------:|
## |2025 |        22.4256| 20.1826| 24.6686| 18.9953| 25.8559|
## |2026 |        22.9443| 20.0138| 25.8747| 18.4625| 27.4260|
## |2027 |        23.4629| 19.9780| 26.9479| 18.1331| 28.7927|
## |2028 |        23.9816| 20.0189| 27.9444| 17.9211| 30.0421|
## |2029 |        24.5003| 20.1114| 28.8892| 17.7881| 31.2125|
## 
## **Proyeksi 5 Periode ke Depan - Foreign direct investment, net inflows (% of GDP) (Model: ARIMA(1,1,0) )**
## 
## 
## |     | Point Forecast|  Lo 80|  Hi 80|   Lo 95|  Hi 95|
## |:----|--------------:|------:|------:|-------:|------:|
## |2026 |         0.7573| 0.3673| 1.1472|  0.1608| 1.3537|
## |2027 |         0.8876| 0.4468| 1.3284|  0.2135| 1.5618|
## |2028 |         0.8259| 0.2968| 1.3551|  0.0167| 1.6352|
## |2029 |         0.8551| 0.2693| 1.4410| -0.0409| 1.7511|
## |2030 |         0.8413| 0.1958| 1.4868| -0.1459| 1.8285|
## 
## **Proyeksi 5 Periode ke Depan - Imports of goods and services (% of GDP) (Model: Holt Linear Trend )**
## 
## 
## |     | Point Forecast|   Lo 80|   Hi 80|   Lo 95|   Hi 95|
## |:----|--------------:|-------:|-------:|-------:|-------:|
## |2025 |        21.9280| 19.1421| 24.7138| 17.6674| 26.1886|
## |2026 |        22.4121| 19.6262| 25.1979| 18.1515| 26.6726|
## |2027 |        22.8962| 20.1103| 25.6820| 18.6356| 27.1567|
## |2028 |        23.3803| 20.5944| 26.1661| 19.1197| 27.6408|
## |2029 |        23.8644| 21.0785| 26.6502| 19.6038| 28.1249|

## 
## **Proyeksi 5 Periode ke Depan - Population growth (annual %) (Model: ARIMA(1,1,1) )**
## 
## 
## |     | Point Forecast|   Lo 80|   Hi 80|   Lo 95|   Hi 95|
## |:----|--------------:|-------:|-------:|-------:|-------:|
## |2026 |        -0.4972| -0.6045| -0.3900| -0.6612| -0.3333|
## |2027 |        -0.4994| -0.6596| -0.3392| -0.7444| -0.2544|
## |2028 |        -0.5003| -0.7030| -0.2977| -0.8103| -0.1904|
## |2029 |        -0.5007| -0.7395| -0.2620| -0.8658| -0.1356|
## |2030 |        -0.5009| -0.7714| -0.2304| -0.9145| -0.0872|
par(mfrow = c(1, 1))

5. Model Multivariat: Vector Autoregression (VAR)

Untuk menganalisis keterkaitan dinamis antar keempat indikator ekonomi dan demografi Jepang secara simultan pada periode 2001–2024, digunakan model VAR(1) pada data first difference yang telah stasioner.

df_var_raw <- df_wide %>%

  filter(Tahun <= 2024) %>%

  dplyr::select(-Tahun)



# Differencing pertama agar seluruh variabel stasioner

df_var_diff <- as.data.frame(lapply(df_var_raw, diff))

colnames(df_var_diff) <- c("FDI_GDP", "Exports_GDP", "Imports_GDP", "Pop_Growth")



# Estimasi model VAR(1)

model_var <- VAR(df_var_diff, p = 1, type = "const")

summary(model_var)
## 
## VAR Estimation Results:
## ========================= 
## Endogenous variables: FDI_GDP, Exports_GDP, Imports_GDP, Pop_Growth 
## Deterministic variables: const 
## Sample size: 22 
## Log Likelihood: -44.622 
## Roots of the characteristic polynomial:
## 0.423 0.3812 0.3812 0.1688
## Call:
## VAR(y = df_var_diff, p = 1, type = "const")
## 
## 
## Estimation results for equation FDI_GDP: 
## ======================================== 
## FDI_GDP = FDI_GDP.l1 + Exports_GDP.l1 + Imports_GDP.l1 + Pop_Growth.l1 + const 
## 
##                Estimate Std. Error t value Pr(>|t|)
## FDI_GDP.l1       0.2626     0.5294   0.496    0.626
## Exports_GDP.l1  -0.1609     1.3882  -0.116    0.909
## Imports_GDP.l1  -0.2648     0.3913  -0.677    0.508
## Pop_Growth.l1   -3.3212     6.1002  -0.544    0.593
## const            0.4315     0.4774   0.904    0.379
## 
## 
## Residual standard error: 1.858 on 17 degrees of freedom
## Multiple R-Squared: 0.05045, Adjusted R-squared: -0.173 
## F-statistic: 0.2258 on 4 and 17 DF,  p-value: 0.9202 
## 
## 
## Estimation results for equation Exports_GDP: 
## ============================================ 
## Exports_GDP = FDI_GDP.l1 + Exports_GDP.l1 + Imports_GDP.l1 + Pop_Growth.l1 + const 
## 
##                Estimate Std. Error t value Pr(>|t|)  
## FDI_GDP.l1      0.02642    0.08777   0.301   0.7670  
## Exports_GDP.l1 -0.54376    0.23014  -2.363   0.0303 *
## Imports_GDP.l1 -0.02315    0.06487  -0.357   0.7256  
## Pop_Growth.l1   0.98653    1.01131   0.975   0.3430  
## const           0.04981    0.07915   0.629   0.5375  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.308 on 17 degrees of freedom
## Multiple R-Squared: 0.3102,  Adjusted R-squared: 0.1479 
## F-statistic: 1.911 on 4 and 17 DF,  p-value: 0.1549 
## 
## 
## Estimation results for equation Imports_GDP: 
## ============================================ 
## Imports_GDP = FDI_GDP.l1 + Exports_GDP.l1 + Imports_GDP.l1 + Pop_Growth.l1 + const 
## 
##                 Estimate Std. Error t value Pr(>|t|)
## FDI_GDP.l1       0.68950    0.66368   1.039    0.313
## Exports_GDP.l1  -0.09583    1.74015  -0.055    0.957
## Imports_GDP.l1  -0.48342    0.49051  -0.986    0.338
## Pop_Growth.l1  -10.23538    7.64676  -1.339    0.198
## const            0.21390    0.59848   0.357    0.725
## 
## 
## Residual standard error: 2.329 on 17 degrees of freedom
## Multiple R-Squared: 0.1425,  Adjusted R-squared: -0.05928 
## F-statistic: 0.7062 on 4 and 17 DF,  p-value: 0.5986 
## 
## 
## Estimation results for equation Pop_Growth: 
## =========================================== 
## Pop_Growth = FDI_GDP.l1 + Exports_GDP.l1 + Imports_GDP.l1 + Pop_Growth.l1 + const 
## 
##                Estimate Std. Error t value Pr(>|t|)  
## FDI_GDP.l1     -0.01826    0.02296  -0.795   0.4375  
## Exports_GDP.l1 -0.11203    0.06021  -1.861   0.0802 .
## Imports_GDP.l1  0.01249    0.01697   0.736   0.4717  
## Pop_Growth.l1   0.21130    0.26456   0.799   0.4355  
## const          -0.01987    0.02071  -0.960   0.3507  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.08058 on 17 degrees of freedom
## Multiple R-Squared: 0.1695,  Adjusted R-squared: -0.02593 
## F-statistic: 0.8673 on 4 and 17 DF,  p-value: 0.5034 
## 
## 
## 
## Covariance matrix of residuals:
##              FDI_GDP Exports_GDP Imports_GDP Pop_Growth
## FDI_GDP      3.45213   -0.093343    3.767686   0.028459
## Exports_GDP -0.09334    0.094878    0.008438   0.003566
## Imports_GDP  3.76769    0.008438    5.424435   0.001952
## Pop_Growth   0.02846    0.003566    0.001952   0.006493
## 
## Correlation matrix of residuals:
##             FDI_GDP Exports_GDP Imports_GDP Pop_Growth
## FDI_GDP      1.0000    -0.16310     0.87067     0.1901
## Exports_GDP -0.1631     1.00000     0.01176     0.1437
## Imports_GDP  0.8707     0.01176     1.00000     0.0104
## Pop_Growth   0.1901     0.14366     0.01040     1.0000