library(tidyverse)
library(fpp3)ARIMA
US Change
us_change |>
autoplot(Consumption)Vamos a utilizar la prueba unitroot_ndiffs() para ver si necesitamos diferenciar la serie para convertirla en estacionaria:
us_change |>
features(Consumption, unitroot_ndiffs)ndiffs nos dice 0. Por lo tanto, no se requieren diferencias y el orden d de nuestro modelo ARIMA sería 0; \(d = 0\).
google_stock <- gafa_stock %>%
filter(Symbol == "GOOG") %>%
mutate(day = row_number()) %>%
update_tsibble(index = day, regular = TRUE) %>%
mutate(diff_close = difference(Close))
google_2015 <- google_stock %>% filter(year(Date) == 2015)
google_2015 |>
autoplot(Close)google_2015 |>
gg_tsdisplay(Close, plot_type = "partial")Para encontrar el orden \(p\) y \(q\), vamos a utilizar las funciones de autocorrelación (ACF) y autocorrelación parcial (PACF):
us_change |>
gg_tsdisplay(Consumption, plot_type = "partial")La gráfica de la PACF sugiere un orden \(p = 3\), y la ACF sugiere \(q = 3\), o incluso \(q = 4\).
Para obtener el orden p nos fijamos en los pacf consecutivos que sean significativos. Para el orden de q es el mismo criterio pero se usa con acf.
Entonces, podríamos proponer los siguientes modelos ARIMA:
\[ ARIMA(p,d,q) : y_{t}^{\prime}=c+\phi_{1} y_{t-1}^{\prime}+\cdots+\phi_{p} y_{t-p}^{\prime}+\theta_{1} \varepsilon_{t-1}+\cdots+\theta_{q} \varepsilon_{t-q}+\varepsilon_{t} \\ ARIMA(3,0,3): y_t = c + \phi_{1} y_{t-1}+ \phi_{2} y_{t-2} + \phi_3 y_{t-3} + \theta_1\varepsilon_{t-1} +\theta_2\varepsilon_{t-2} + \theta_3 \varepsilon_{t-3} + \varepsilon_t \\ ARIMA(3,0,4): y_t = c + \phi_{1} y_{t-1}+ \phi_{2} y_{t-2} + \phi_3 y_{t-3} + \theta_1\varepsilon_{t-1} +\theta_2\varepsilon_{t-2} + \theta_3 \varepsilon_{t-3} + + \theta_4 \varepsilon_{t-4} + \varepsilon_t \\ ARIMA(3,0,0): y_t = c + \phi_{1} y_{t-1}+ \phi_{2} y_{t-2} + \phi_3 y_{t-3} + \varepsilon_t \\ ARIMA(0,0,3): y_t = c + \theta_1\varepsilon_{t-1} +\theta_2\varepsilon_{t-2} + \theta_3 \varepsilon_{t-3} + \varepsilon_t \\ ARIMA(0,0,4): y_t = c + \theta_1\varepsilon_{t-1} +\theta_2\varepsilon_{t-2} + \theta_3 \varepsilon_{t-3} + \theta_4 \varepsilon_{t-4} + \varepsilon_t \]
us_change_fit <- us_change |>
model(
arima_303 = ARIMA(Consumption ~ pdq(3,0,3) + PDQ(0,0,0)),
arima_304 = ARIMA(Consumption ~ pdq(3,0,4) + PDQ(0,0,0) + 1),
arima_300 = ARIMA(Consumption ~ pdq(3,0,0) + PDQ(0,0,0)),
arima_003 = ARIMA(Consumption ~ pdq(0,0,3) + PDQ(0,0,0)),
arima_004 = ARIMA(Consumption ~ pdq(0,0,4) + PDQ(0,0,0)),
arima_100 = ARIMA(Consumption ~ pdq(1,0,0) + PDQ(0,0,0)),
arima_001 = ARIMA(Consumption ~ pdq(0,0,1) + PDQ(0,0,0))
)
us_change_fitReportes de algunos modelos
us_change_fit |>
select(arima_303) |>
report()Series: Consumption
Model: ARIMA(3,0,3) w/ mean
Coefficients:
ar1 ar2 ar3 ma1 ma2 ma3 constant
0.4489 0.4091 -0.2963 -0.2369 -0.2913 0.4357 0.3247
s.e. 0.3300 0.2577 0.2663 0.3136 0.2328 0.1829 0.0364
sigma^2 estimated as 0.3352: log likelihood=-169.4
AIC=354.81 AICc=355.57 BIC=381.11
us_change_fit |>
select(arima_300) |>
report()Series: Consumption
Model: ARIMA(3,0,0) w/ mean
Coefficients:
ar1 ar2 ar3 constant
0.2027 0.1605 0.2252 0.3046
s.e. 0.0691 0.0697 0.0690 0.0400
sigma^2 estimated as 0.3332: log likelihood=-170.3
AIC=350.6 AICc=350.91 BIC=367.04
us_change_fit |>
select(arima_003) |>
report()Series: Consumption
Model: ARIMA(0,0,3) w/ mean
Coefficients:
ma1 ma2 ma3 constant
0.2148 0.2082 0.2766 0.7430
s.e. 0.0699 0.0697 0.0610 0.0692
sigma^2 estimated as 0.3376: log likelihood=-171.6
AIC=353.19 AICc=353.51 BIC=369.63
LA forma tradicional de elegir el orden p y q de un modelo ARIMA es a través de los criterios de información (AIC, AICc, BIC):
us_change_fit |>
glance() |>
arrange(AICc)Sin embargo, también podríamos escoger el modelo a partir de las métricas de error en el entrenamiento:
accuracy(us_change_fit) |>
arrange(MAE)Realizamos el diagnóstico de residuos para los dos modelos que parecen ajustarse mejor:
us_change_fit |>
select(arima_300) |>
gg_tsresiduals()us_change_fit |>
select(arima_304) |>
gg_tsresiduals()us_change_fit |>
select(arima_100) |>
gg_tsresiduals()us_change_fit |>
select(arima_001) |>
gg_tsresiduals()us_change_fit |>
augment() |>
features(.innov, ljung_box, lag = 10) |>
mutate(residuos_autocorrelacionados = if_else(lb_pvalue >=0.05, "No autocorrelación", "Autocorrelacionados"))tictoc::tic()
us_change |>
model(auto_arima = ARIMA(Consumption ~ PDQ(0,0,0))) |>
report()Series: Consumption
Model: ARIMA(1,0,3) w/ mean
Coefficients:
ar1 ma1 ma2 ma3 constant
0.5731 -0.3617 0.0925 0.1934 0.3160
s.e. 0.1503 0.1607 0.0787 0.0824 0.0371
sigma^2 estimated as 0.3334: log likelihood=-169.88
AIC=351.77 AICc=352.21 BIC=371.5
tictoc::toc()0.19 sec elapsed
tictoc::tic()
us_change |>
model(
auto_arima = ARIMA(Consumption ~ PDQ(0,0,0),
stepwise = FALSE, approximation = FALSE)
) |>
report()Series: Consumption
Model: ARIMA(3,0,0) w/ mean
Coefficients:
ar1 ar2 ar3 constant
0.2027 0.1605 0.2252 0.3046
s.e. 0.0691 0.0697 0.0690 0.0400
sigma^2 estimated as 0.3332: log likelihood=-170.3
AIC=350.6 AICc=350.91 BIC=367.04
tictoc::toc()1.22 sec elapsed
tictoc::tic()
us_change |>
model(
semi_auto_arima = ARIMA(Consumption ~ pdq(p = 1:3, d = 0, q = 0:2) + PDQ(0,0,0))
) |>
report()Series: Consumption
Model: ARIMA(3,0,0) w/ mean
Coefficients:
ar1 ar2 ar3 constant
0.2027 0.1605 0.2252 0.3046
s.e. 0.0691 0.0697 0.0690 0.0400
sigma^2 estimated as 0.3332: log likelihood=-170.3
AIC=350.6 AICc=350.91 BIC=367.04
tictoc::toc()0.15 sec elapsed
1:3[1] 1 2 3