# ══════════════════════════════════════════════════ # T10 · Caso práctico Macro — IPC de España (INE) # Abre primero mSeriesTemporales.Rproj en RStudio # Datos: data/ipc_mensual_espana.RData (INE, serie IPC290751) # ══════════════════════════════════════════════════ library(tidyverse) library(forecast) library(seasonal) pausa <- function(msg = "\n [Pulsa ENTER para continuar...]") { if (interactive()) { cat(msg); invisible(readline()) } } # ── 1. CARGA Y PREPARACION ──────────────────────── load("data/ipc_mensual_espana.RData") ipc <- ipc_mensual_espana ipc$fecha <- as.Date(ipc$fecha) ipc_ts <- ts(ipc$ipc, start = c(2002, 1), frequency = 12) m_x13 <- seas(ipc_ts) ipc_sa <- final(m_x13) pausa("\n [Datos cargados y desestacionalizados. Pulsa ENTER...]") # ── 2. ALISADO EXPONENCIAL SIMPLE (SES) ─────────── m_ses <- ses(ipc_ts, h = 12) cat(sprintf("SES: alfa=%.4f (pegado al limite superior: no hay tendencia que modelizar)\n", m_ses$model$par["alpha"])) plot(m_ses, main = "SES sobre el IPC: prediccion plana") pausa() # ── 3. EQUIVALENCIA SES = ARIMA(0,1,1) ──────────── m_arima011 <- Arima(ipc_ts, order = c(0, 1, 1)) theta_ses <- unname(m_ses$model$par["alpha"]) - 1 theta_arima <- unname(coef(m_arima011)["ma1"]) cat(sprintf("theta implicito por el SES (alfa-1) = %.4f\n", theta_ses)) cat(sprintf("theta del ARIMA(0,1,1) ajustado directamente = %.4f\n", theta_arima)) pausa() # ── 4. HOLT SOBRE LA SERIE DESESTACIONALIZADA ───── m_holt <- holt(ipc_sa, h = 12) cat(sprintf("Holt (sobre IPC desestacionalizado): alfa=%.4f beta=%.4f\n", m_holt$model$par["alpha"], m_holt$model$par["beta"])) plot(m_holt, main = "Holt sobre el IPC desestacionalizado") pausa() # ── 5. HOLT-WINTERS ADITIVO SOBRE LA SERIE ORIGINAL ─ m_hw <- hw(ipc_ts, h = 12, seasonal = "additive") cat(sprintf("Holt-Winters aditivo: alfa=%.4f beta=%.4f gamma=%.4f\n", m_hw$model$par["alpha"], m_hw$model$par["beta"], m_hw$model$par["gamma"])) plot(m_hw, main = "Holt-Winters aditivo sobre el IPC") pausa() # ── 6. SELECCION AUTOMATICA CON ets() ───────────── m_ets <- ets(ipc_ts) cat("Modelo ETS seleccionado por AICc:\n") print(m_ets$method) cat(sprintf("AICc ETS = %.2f | AICc HW aditivo = %.2f\n", m_ets$aicc, m_hw$model$aicc)) pausa() # ── 7. BACKTEST: SES, HOLT, HW, ETS Y EL SARIMA DEL CAPITULO 6 ─ n <- length(ipc_ts) train <- window(ipc_ts, end = time(ipc_ts)[n - 12]) test <- window(ipc_ts, start = time(ipc_ts)[n - 11]) f_ses <- ses(train, h = 12)$mean f_holt <- holt(train, h = 12)$mean f_hw <- hw(train, h = 12, seasonal = "additive")$mean f_ets <- forecast(ets(train), h = 12)$mean f_sarima <- forecast(Arima(train, order = c(0, 1, 1), seasonal = list(order = c(0, 1, 1), period = 12)), h = 12)$mean recm <- function(e) sqrt(mean(e^2)) resultado <- tibble( modelo = c("SES", "Holt", "Holt-Winters", "ETS", "SARIMA (Cap.6)"), RECM = c(recm(test - f_ses), recm(test - f_holt), recm(test - f_hw), recm(test - f_ets), recm(test - f_sarima)) ) |> arrange(RECM) print(resultado) cat(sprintf("\nMejor modelo en el backtest a 12 meses: %s\n", resultado$modelo[1])) # === FIN Caso Práctico Macro — Tema 10 (IPC, alisado exponencial y ETS) ===