library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.1 v dplyr 1.0.6
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
##
## recode
## The following object is masked from 'package:purrr':
##
## some
library(tseries)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(astsa)
library(foreign)
library(timsac)
library(vars)
## Loading required package: MASS
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
## Loading required package: strucchange
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: sandwich
##
## Attaching package: 'strucchange'
## The following object is masked from 'package:stringr':
##
## boundary
## Loading required package: urca
## Loading required package: lmtest
library(lmtest)
library(mFilter)
library(dynlm)
library(nlme)
##
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
##
## collapse
library(lmtest)
library(broom)
library(kableExtra)
##
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
##
## group_rows
library(knitr)
library(MASS)
library(parallel)
library(car)
library(mlogit)
## Loading required package: dfidx
##
## Attaching package: 'dfidx'
## The following object is masked from 'package:MASS':
##
## select
## The following object is masked from 'package:stats':
##
## filter
library(dplyr)
library(tidyr)
library(forecast)
##
## Attaching package: 'forecast'
## The following object is masked from 'package:nlme':
##
## getResponse
## The following object is masked from 'package:astsa':
##
## gas
#install.packages('fpp2', dependencies = TRUE)
library(fpp2)
## -- Attaching packages ---------------------------------------------- fpp2 2.4 --
## v fma 2.4 v expsmooth 2.3
## -- Conflicts ------------------------------------------------- fpp2_conflicts --
## x forecast::getResponse() masks nlme::getResponse()
## x car::some() masks purrr::some()
##
## Attaching package: 'fpp2'
## The following object is masked from 'package:astsa':
##
## oil
#Note que los modelos SARIMA especificados como ARIMA(p, d, q)(P, D, Q) m son modelos ARIMA con un gran numero de restricciones.
#donde (P, D, Q)m denota la parte estacional del modelo.
#Procedimiento general
#Importar los datos de la libreria fpp2
data(euretail)
euretail
## Qtr1 Qtr2 Qtr3 Qtr4
## 1996 89.13 89.52 89.88 90.12
## 1997 89.19 89.78 90.03 90.38
## 1998 90.27 90.77 91.85 92.51
## 1999 92.21 92.52 93.62 94.15
## 2000 94.69 95.34 96.04 96.30
## 2001 94.83 95.14 95.86 95.83
## 2002 95.73 96.36 96.89 97.01
## 2003 96.66 97.76 97.83 97.76
## 2004 98.17 98.55 99.31 99.44
## 2005 99.43 99.84 100.32 100.40
## 2006 99.88 100.19 100.75 101.01
## 2007 100.84 101.34 101.94 102.10
## 2008 101.56 101.48 101.13 100.34
## 2009 98.93 98.31 97.67 97.44
## 2010 96.53 96.56 96.51 96.70
## 2011 95.88 95.84 95.79 95.94
#Comando para saber cuantas diferenciaciones se requieren para un SARIMA
nsdiffs(euretail)
## [1] 1
#Comando para saber cuantas diferenciaciones se requieren para un SARIMA
ndiffs(euretail)
## [1] 2
#Realiza la grafica de serie de tiempo
plot.ts(euretail, main="Euretail")
# Correlograma de Función de Autocorrelación Simple.
acf(euretail)
# Correlograma de Función de Autocorrelación Simple.
pacf(euretail)
#Solicitar el mejor modelo de ARIMA
#La funcion auto.arima() calcula el mejor modelo ARIMA(p, d, q) de acuerdo a diferentes criterios: AIC, AICC o BIC value.
model=auto.arima(euretail,stepwise=FALSE,approximation=FALSE)
#Resumen del Modelo SARIMA
summary(model)
## Series: euretail
## ARIMA(0,1,3)(0,1,1)[4]
##
## Coefficients:
## ma1 ma2 ma3 sma1
## 0.2630 0.3694 0.4200 -0.6636
## s.e. 0.1237 0.1255 0.1294 0.1545
##
## sigma^2 estimated as 0.156: log likelihood=-28.63
## AIC=67.26 AICc=68.39 BIC=77.65
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set -0.02965298 0.3661147 0.2787802 -0.02795377 0.2885545 0.2267735
## ACF1
## Training set 0.006455781
residuals=resid(model)
plot(residuals, main="Residuals", col="Blue")
adf.test(residuals)
##
## Augmented Dickey-Fuller Test
##
## data: residuals
## Dickey-Fuller = -3.8579, Lag order = 3, p-value = 0.02164
## alternative hypothesis: stationary
#SARIMA(0,1,3)(0,1,1)[4]
#Pronóstico del modelo
forecast(model,h=10)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2012 Q1 95.17620 94.67006 95.68233 94.40214 95.95026
## 2012 Q2 95.23809 94.42273 96.05345 93.99111 96.48507
## 2012 Q3 95.32444 94.16364 96.48523 93.54915 97.09972
## 2012 Q4 95.33633 93.77859 96.89408 92.95397 97.71870
## 2013 Q1 94.56087 92.58895 96.53279 91.54508 97.57666
## 2013 Q2 94.57178 92.23497 96.90859 90.99794 98.14562
## 2013 Q3 94.56911 91.88684 97.25138 90.46694 98.67129
## 2013 Q4 94.58101 91.56075 97.60126 89.96193 99.20009
## 2014 Q1 93.80554 90.40685 97.20423 88.60769 99.00339
## 2014 Q2 93.81646 90.05855 97.57436 88.06923 99.56368
ggseasonplot(euretail, main="Plot SARIMA")