title: “Tugas Arma”

author: “Ni Kadek Srisuryani 240611010035”

date: “2026-11-18”

output: html_document


library(readxl)

library(dplyr)

library(tidyr)

library(stringr)

library(zoo)

library(tseries)

library(forecast)

library(ggplot2)

library(knitr)
file_path <- "C:/Users/acer/OneDrive/Dokumen/Data indonesia pacific sri 1.xlsx"

raw_df <- read_excel(file_path, sheet = 1, col_types = "text")



country_col <- which(sapply(raw_df, function(col) any(grepl("Malaysia", col, ignore.case = TRUE))))[1]

if (is.na(country_col)) country_col <- 1

var_col <- country_col + 1

data_start_col <- var_col + 1



country_vec <- trimws(as.character(raw_df[[country_col]]))

country_vec[country_vec %in% c("", "NA", "NaN", "..", "-")] <- NA

country_vec <- na.locf(country_vec, na.rm = FALSE)



malaysia_rows <- which(grepl("Malaysia", country_vec, ignore.case = TRUE))

malaysia_sub <- raw_df[malaysia_rows, var_col:ncol(raw_df), drop = FALSE]

colnames(malaysia_sub)[1] <- "Variable"



malaysia_long <- malaysia_sub %>%

  mutate(

    Country = "Malaysia",

    Variable = trimws(as.character(Variable))

  ) %>%

  filter(!is.na(Variable) & Variable != "") %>%

  pivot_longer(

    cols = -c(Country, Variable),

    names_to = "Period",

    values_to = "Raw_Value"

  ) %>%

  mutate(

    Clean_Value = str_trim(str_remove_all(as.character(Raw_Value), "%|,")),

    Value = suppressWarnings(as.numeric(Clean_Value))

  )



kable(

  data.frame(Available_Indicators = unique(malaysia_long$Variable)),

  caption = "Available Indicators for Malaysia"

)
Available Indicators for Malaysia
Available_Indicators
Government expenditure on education, total (% of GDP)
Current health expenditure (% of GDP)
Life expectancy at birth, total (years)
GDP per capita (current US$)
selected_variable <- "Government expenditure on education, total (% of GDP)"



df_var <- malaysia_long %>%

  filter(grepl(selected_variable, Variable, fixed = TRUE)) %>%

  mutate(Time_Index = row_number()) %>%

  arrange(Time_Index)



if (nrow(df_var) == 0) {

  stop("Selected variable not found. Check 'Available_Indicators' table above.")

}



ts_clean <- na.interp(df_var$Value)

ts_mal <- ts(ts_clean, start = 2000, frequency = 1)



autoplot(ts_mal) +

  ggtitle(paste("Malaysia Time Series:", selected_variable)) +

  xlab("Year") +

  ylab("Value") +

  theme_minimal()

adf_initial <- tryCatch(

  adf.test(na.omit(ts_mal)),

  error = function(e) list(statistic = NA, p.value = 1, method = "ADF Test Failed (Series too short)")

)

print(adf_initial)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  na.omit(ts_mal)
## Dickey-Fuller = -4.0869, Lag order = 2, p-value = 0.02022
## alternative hypothesis: stationary
d_order <- ifelse(!is.na(adf_initial$p.value) && adf_initial$p.value < 0.05, 0, 1)



if (d_order == 1) {

  ts_stat <- diff(na.omit(ts_mal), differences = 1)

  adf_diff <- tryCatch(

    adf.test(ts_stat),

    error = function(e) list(statistic = NA, p.value = NA, method = "ADF Test on Differenced Series")

  )

  print(adf_diff)

} else {

  ts_stat <- na.omit(ts_mal)

}
max_lag <- max(1, floor(length(ts_stat) / 2) - 1)



par(mfrow = c(1, 2))

acf(ts_stat, lag.max = max_lag, main = "ACF Plot (MA Order / q)")

pacf(ts_stat, lag.max = max_lag, main = "PACF Plot (AR Order / p)")

par(mfrow = c(1, 1))
fit_safe_arima <- function(series, ord) {

  tryCatch(

    Arima(series, order = ord, method = "ML"),

    error = function(e) Arima(series, order = ord, method = "CSS")

  )

}



model_ar1 <- fit_safe_arima(ts_stat, c(1, 0, 0))

model_ma1 <- fit_safe_arima(ts_stat, c(0, 0, 1))

model_arma11 <- fit_safe_arima(ts_stat, c(1, 0, 1))

model_arima <- fit_safe_arima(ts_mal, c(1, d_order, 1))

model_auto <- auto.arima(ts_mal, trace = TRUE, stepwise = FALSE, approximation = FALSE)
## 
##  ARIMA(0,1,0)                    : 50.79021
##  ARIMA(0,1,0) with drift         : 51.65255
##  ARIMA(0,1,1)                    : 52.81049
##  ARIMA(0,1,1) with drift         : 53.31573
##  ARIMA(0,1,2)                    : 51.22729
##  ARIMA(0,1,2) with drift         : 53.33167
##  ARIMA(0,1,3)                    : 53.11108
##  ARIMA(0,1,3) with drift         : 55.98783
##  ARIMA(0,1,4)                    : Inf
##  ARIMA(0,1,4) with drift         : Inf
##  ARIMA(0,1,5)                    : Inf
##  ARIMA(0,1,5) with drift         : Inf
##  ARIMA(1,1,0)                    : 52.59873
##  ARIMA(1,1,0) with drift         : 52.9019
##  ARIMA(1,1,1)                    : 54.09104
##  ARIMA(1,1,1) with drift         : 54.99
##  ARIMA(1,1,2)                    : 53.60099
##  ARIMA(1,1,2) with drift         : 56.35909
##  ARIMA(1,1,3)                    : 56.33507
##  ARIMA(1,1,3) with drift         : 59.56984
##  ARIMA(1,1,4)                    : 58.43405
##  ARIMA(1,1,4) with drift         : Inf
##  ARIMA(2,1,0)                    : 53.23692
##  ARIMA(2,1,0) with drift         : 54.76826
##  ARIMA(2,1,1)                    : 56.14061
##  ARIMA(2,1,1) with drift         : 57.98996
##  ARIMA(2,1,2)                    : Inf
##  ARIMA(2,1,2) with drift         : Inf
##  ARIMA(2,1,3)                    : Inf
##  ARIMA(2,1,3) with drift         : Inf
##  ARIMA(3,1,0)                    : 56.13834
##  ARIMA(3,1,0) with drift         : 57.97792
##  ARIMA(3,1,1)                    : 58.41177
##  ARIMA(3,1,1) with drift         : Inf
##  ARIMA(3,1,2)                    : Inf
##  ARIMA(3,1,2) with drift         : Inf
##  ARIMA(4,1,0)                    : 58.51516
##  ARIMA(4,1,0) with drift         : 60.4761
##  ARIMA(4,1,1)                    : 59.95264
##  ARIMA(4,1,1) with drift         : Inf
##  ARIMA(5,1,0)                    : 57.15053
##  ARIMA(5,1,0) with drift         : 57.82799
## 
## 
## 
##  Best model: ARIMA(0,1,0)
model_ets <- ets(ts_mal)



summary(model_ar1)
## Series: series 
## ARIMA(1,0,0) with non-zero mean 
## 
## Coefficients:
##          ar1    mean
##       0.8918  5.1961
## s.e.  0.0963  0.9467
## 
## sigma^2 = 0.4657:  log likelihood = -25.67
## AIC=57.34   AICc=58.49   BIC=61
## 
## Training set error measures:
##                      ME      RMSE       MAE       MPE     MAPE     MASE
## Training set -0.1317415 0.6545734 0.4892661 -4.012698 9.716177 1.100454
##                    ACF1
## Training set -0.1590255
summary(model_ma1)
## Series: series 
## ARIMA(0,0,1) with non-zero mean 
## 
## Coefficients:
##          ma1    mean
##       0.5708  5.0140
## s.e.  0.1170  0.2856
## 
## sigma^2 = 0.9238:  log likelihood = -33.64
## AIC=73.27   AICc=74.42   BIC=76.93
## 
## Training set error measures:
##                       ME      RMSE       MAE       MPE     MAPE     MASE
## Training set -0.02930144 0.9218805 0.7250667 -4.401672 14.73135 1.630815
##                  ACF1
## Training set 0.305952
summary(model_arma11)
## Series: series 
## ARIMA(1,0,1) with non-zero mean 
## 
## Coefficients:
##          ar1      ma1    mean
##       0.9074  -0.0524  5.2188
## s.e.  0.1002   0.1734  1.0128
## 
## sigma^2 = 0.4841:  log likelihood = -25.63
## AIC=59.25   AICc=61.25   BIC=64.13
## 
## Training set error measures:
##                      ME      RMSE       MAE       MPE     MAPE     MASE
## Training set -0.1413718 0.6526987 0.4912079 -4.174671 9.767698 1.104822
##                    ACF1
## Training set -0.1107895
summary(model_arima)
## Series: series 
## ARIMA(1,0,1) with non-zero mean 
## 
## Coefficients:
##          ar1      ma1    mean
##       0.9074  -0.0524  5.2188
## s.e.  0.1002   0.1734  1.0128
## 
## sigma^2 = 0.4841:  log likelihood = -25.63
## AIC=59.25   AICc=61.25   BIC=64.13
## 
## Training set error measures:
##                      ME      RMSE       MAE       MPE     MAPE     MASE
## Training set -0.1413718 0.6526987 0.4912079 -4.174671 9.767698 1.104822
##                    ACF1
## Training set -0.1107895
comparison_table <- data.frame(

  Model = c(

    "AR(1)",

    "MA(1)",

    "ARMA(1,1)",

    paste0("ARIMA(1,", d_order, ",1)"),

    "Auto-ARIMA",

    "ETS"

  ),

  AIC = c(

    AIC(model_ar1),

    AIC(model_ma1),

    AIC(model_arma11),

    AIC(model_arima),

    AIC(model_auto),

    AIC(model_ets)

  ),

  BIC = c(

    BIC(model_ar1),

    BIC(model_ma1),

    BIC(model_arma11),

    BIC(model_arima),

    BIC(model_auto),

    BIC(model_ets)

  )

)



kable(comparison_table, digits = 4, caption = "Model Comparison Based on AIC and BIC")
Model Comparison Based on AIC and BIC
Model AIC BIC
AR(1) 57.3443 61.0010
MA(1) 73.2741 76.9308
ARMA(1,1) 59.2539 64.1294
ARIMA(1,0,1) 59.2539 64.1294
Auto-ARIMA 50.6084 51.7864
ETS 64.7432 68.3999
summary(model_auto)
## Series: ts_mal 
## ARIMA(0,1,0) 
## 
## sigma^2 = 0.4437:  log likelihood = -24.3
## AIC=50.61   AICc=50.79   BIC=51.79
## 
## Training set error measures:
##                     ME      RMSE       MAE       MPE     MAPE      MASE
## Training set -0.158487 0.6526775 0.4271191 -3.796766 8.481882 0.9606734
##                    ACF1
## Training set -0.2326272
checkresiduals(model_auto)

## 
##  Ljung-Box test
## 
## data:  Residuals from ARIMA(0,1,0)
## Q* = 8.1477, df = 5, p-value = 0.1483
## 
## Model df: 0.   Total lags used: 5
forecast_5_periods <- forecast(model_auto, h = 5)

kable(as.data.frame(forecast_5_periods), digits = 4, caption = "5-Period Ahead Forecast Results")
5-Period Ahead Forecast Results
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2025 3.5148 2.6611 4.3685 2.2092 4.8204
2026 3.5148 2.3075 4.7221 1.6684 5.3612
2027 3.5148 2.0362 4.9934 1.2534 5.7762
2028 3.5148 1.8074 5.2222 0.9036 6.1260
2029 3.5148 1.6059 5.4237 0.5954 6.4342
autoplot(forecast_5_periods) +

  ggtitle(paste("5-Period Projection - Malaysia:", selected_variable)) +

  xlab("Year") +

  ylab("Value") +

  theme_minimal()