## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE, comment = "#>",
  fig.width = 7, fig.height = 5, fig.align = "center"
)
library(arimasel)
set.seed(2026)

## ----ts-eda, fig.height=6-----------------------------------------------------
eda <- ts_eda(gdp_ng)
print(eda)

## ----ts-features--------------------------------------------------------------
ts_features(inflation_ng)

## ----cp-sets------------------------------------------------------------------
cp_sets(p_set = 0:2, d_set = 0:1, q_set = 0:2)

## ----data-plots, fig.height=4-------------------------------------------------
data(gdp_ng)
data(exchange_ng)
data(inflation_ng)

oldpar <- par(mfrow = c(1, 3), mar = c(4, 4, 3, 1))
plot(gdp_ng,      main = "GDP Growth (%)",    ylab = "%")
plot(exchange_ng, main = "USD/NGN Rate",      ylab = "NGN")
plot(inflation_ng,main = "CPI Inflation (%)", ylab = "%")
par(oldpar)

## ----stationarity-------------------------------------------------------------
stationarity_test(gdp_ng)
d_rec <- suggest_d(gdp_ng)
cat("Recommended d:", d_rec, "\n")

## ----cart-arima---------------------------------------------------------------
result <- cart_arima(gdp_ng,
                     p_set     = 0:3,
                     d_set     = 0:1,
                     q_set     = 0:3,
                     criterion = "AIC",
                     top_n     = 10L)
print(result)

## ----summary------------------------------------------------------------------
summary(result)

## ----rerank-------------------------------------------------------------------
arima_table(result, criterion = "BIC", top_n = 5)

## ----weights------------------------------------------------------------------
ic_vals <- setNames(result$full_table$AIC,
                    result$full_table$Model)
arima_weights(ic_vals[1:6])

## ----vote---------------------------------------------------------------------
result$vote_table

## ----plot-criteria, fig.height=5----------------------------------------------
plot(result, type = "criteria", top_n = 8)

## ----plot-weights, fig.height=5-----------------------------------------------
plot(result, type = "weights", top_n = 8)

## ----plot-surface, fig.height=5-----------------------------------------------
plot(result, type = "surface", criterion = "AIC")

## ----plot-fitted, fig.height=5------------------------------------------------
plot(result, type = "fitted")

## ----diagnose-----------------------------------------------------------------
arima_diagnose(result, lags = 15)

## ----forecast-best, fig.height=5----------------------------------------------
fc <- arima_forecast(result, h = 8, level = c(80, 95), plot = TRUE)
cat("Point forecasts:\n")
round(fc$mean, 2)

## ----forecast-ensemble, fig.height=5------------------------------------------
fc_ens <- arima_forecast(result, h = 8, ensemble = TRUE,
                         top_k = 4L, plot = TRUE)

## ----exchange-----------------------------------------------------------------
res_ex <- cart_arima(exchange_ng,
                     p_set = 0:2, d_set = 1L, q_set = 0:2,
                     criterion = "BIC")
print(res_ex)
arima_forecast(res_ex, h = 12, plot = TRUE)

## ----compare, eval=requireNamespace("forecast", quietly=TRUE)-----------------
compare_arima(gdp_ng, p_set = 0:3, d_set = 0:1, q_set = 0:3,
              holdout = 5L)

## ----seasonal-strength--------------------------------------------------------
seasonal_strength(inflation_ng)$seasonal_strength
suggest_D(inflation_ng)

## ----seasonal-search----------------------------------------------------------
res_seas <- cart_arima(inflation_ng,
                       p_set = 0:1, d_set = 0:1, q_set = 0:1,
                       seasonal = list(P = 0:1, D = 0:1, Q = 0:1, period = 12),
                       criterion = "AIC", top_n = 8L)
print(res_seas)

## ----plot-seasonal, fig.height=5----------------------------------------------
plot(res_seas, type = "seasonal")

## ----xreg-demo----------------------------------------------------------------
set.seed(42)
trend <- as.numeric(time(gdp_ng)) - 1990
res_xreg <- cart_arima(gdp_ng, p_set = 0:2, d_set = 0:1, q_set = 0:2,
                       xreg = trend, criterion = "AIC")
fc_xreg <- arima_forecast(res_xreg, h = 5,
                          newxreg = matrix(max(trend) + 1:5, ncol = 1),
                          plot = FALSE)
round(fc_xreg$mean, 2)

## ----cv-demo------------------------------------------------------------------
cv <- arima_cv(result, h = 3, initial = floor(0.7 * result$n_obs))
print(cv)

## ----plot-cv, fig.height=5----------------------------------------------------
plot(cv, type = "rmse")

## ----smart-arima--------------------------------------------------------------
res_smart <- smart_arima(inflation_ng, p_set = 0:1, q_set = 0:1)
print(res_smart)

