Cargar paquetes
library(readxl);library(ggplot2);library(forecast);library(tseries);library(TSA);library(ggfortify);library(sarima);library(changepoint);library(strucchange);library(gridExtra)
## Registered S3 method overwritten by 'xts':
## method from
## as.zoo.xts zoo
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Registered S3 methods overwritten by 'forecast':
## method from
## fitted.fracdiff fracdiff
## residuals.fracdiff fracdiff
## Registered S3 methods overwritten by 'TSA':
## method from
## fitted.Arima forecast
## plot.Arima forecast
##
## Attaching package: 'TSA'
## The following objects are masked from 'package:stats':
##
## acf, arima
## The following object is masked from 'package:utils':
##
## tar
## Registered S3 methods overwritten by 'ggfortify':
## method from
## autoplot.Arima forecast
## autoplot.acf forecast
## autoplot.ar forecast
## autoplot.bats forecast
## autoplot.decomposed.ts forecast
## autoplot.ets forecast
## autoplot.forecast forecast
## autoplot.stl forecast
## autoplot.ts forecast
## fitted.ar forecast
## fortify.ts forecast
## residuals.ar forecast
## Loading required package: FitAR
## Loading required package: lattice
## Loading required package: leaps
## Loading required package: ltsa
## Loading required package: bestglm
##
## Attaching package: 'FitAR'
## The following object is masked from 'package:forecast':
##
## BoxCox
## Loading required package: stats4
##
## Attaching package: 'sarima'
## The following object is masked from 'package:TSA':
##
## periodogram
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Successfully loaded changepoint package version 2.2.2
## NOTE: Predefined penalty values changed in version 2.2. Previous penalty values with a postfix 1 i.e. SIC1 are now without i.e. SIC and previous penalties without a postfix i.e. SIC are now with a postfix 0 i.e. SIC0. See NEWS and help files for further details.
## Loading required package: sandwich
Cargar la base de datos
Serie <- read_excel("Serie.xlsx")
head(Serie)
Generar un objeto tipo serie de tiempo
serie <- ts(Serie$Y, start=c(2000,01), frequency=12)
head(serie)
## Jan Feb Mar Apr May Jun
## 2000 92705.00 71760.50 53025.35 41789.74 35040.82 29477.58
Visualizar la serie de tiempo
autoplot(serie)

Identificación de un patrón estacional
ggseasonplot(serie)

ggseasonplot(serie, polar=TRUE)

autoplot(decompose(serie))
## Warning: attributes are not identical across measure variables;
## they will be dropped
## Warning: Removed 24 rows containing missing values (geom_path).

autoplot(cpt.meanvar(serie))

autoplot(breakpoints(serie~1))

ggfreqplot(serie, frequency=12)

ggmonthplot(serie)

gglagplot(serie)

gglagchull(serie)

ggtsdisplay(serie)

eacf(serie)
## AR/MA
## 0 1 2 3 4 5 6 7 8 9 10 11 12 13
## 0 x x o o x x x x o o x x x x
## 1 o x o o o o o x o o o x o o
## 2 x o o o o o o o o o o x x o
## 3 x x o o o o o o o o o x x x
## 4 x x x o o o o o o o o x x o
## 5 x x x x o o o o o o o x x x
## 6 x x x o x o o o o o o x x x
## 7 o x x x x o o o o o o x o o
modelo <- auto.arima(serie)
summary(modelo)
## Series: serie
## ARIMA(2,1,0)(1,1,2)[12]
##
## Coefficients:
## ar1 ar2 sar1 sma1 sma2
## -0.2663 -0.1955 0.8866 -1.2403 0.3399
## s.e. 0.0731 0.0755 0.2898 0.3014 0.1142
##
## sigma^2 estimated as 14294539: log likelihood=-1804.27
## AIC=3620.53 AICc=3621 BIC=3639.92
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 177.1083 3606.665 2707.72 0.05353533 10.93431 0.660297
## ACF1
## Training set 0.0008697126
Detección de datos atipicos
detectAO(modelo)
## [,1]
## ind 61.000000
## lambda2 -4.873222
detectIO(modelo)
## [,1] [,2]
## ind 61.000000 85.000000
## lambda1 -4.039939 -3.852838
autoplot(modelo)

fitted(modelo)
## Jan Feb Mar Apr May Jun Jul
## 2000 92651.477 71752.787 53036.135 41807.692 35061.243 29499.684 26949.609
## 2001 72169.373 51429.304 36265.209 31619.809 29769.140 24861.302 24073.260
## 2002 71994.027 49267.585 38591.953 33043.454 24276.150 25139.658 22766.656
## 2003 66925.579 45666.620 36235.405 28090.376 25986.031 23086.795 22760.460
## 2004 57031.532 39405.359 35828.378 29591.299 32479.494 24277.482 28761.202
## 2005 58653.403 36692.003 29160.763 25477.481 24226.124 19627.807 22139.428
## 2006 50745.305 32188.174 35862.016 24560.074 26805.522 24669.563 17664.240
## 2007 49832.718 34713.150 27015.231 25564.956 21247.362 18356.996 17920.799
## 2008 37753.402 35634.545 31375.831 30424.071 27425.902 23743.931 22145.428
## 2009 40938.559 41534.391 36040.802 34483.394 32582.885 23373.516 23236.298
## 2010 37481.581 39320.291 35326.795 27494.819 24263.442 18821.214 24709.431
## 2011 45933.465 39872.013 32339.592 28031.497 21234.969 19348.464 17530.991
## 2012 41415.512 37501.186 28321.680 25888.380 21407.476 20162.400 22426.193
## 2013 41113.319 35269.118 27511.555 22980.811 22819.987 16016.051 19928.338
## 2014 39782.481 26816.373 24333.935 19182.085 15328.839 15394.300 23570.635
## 2015 34090.280 32410.997 24072.301 19616.120 21296.514 25534.617 25945.220
## 2016 36301.952 32753.135 29575.514 30884.135 26841.966 23217.941 23082.387
## Aug Sep Oct Nov Dec
## 2000 24946.325 24276.926 21571.189 20244.182 19720.092
## 2001 23371.662 20835.539 17764.726 16874.134 17311.307
## 2002 20936.615 18562.936 15380.822 17036.247 15153.212
## 2003 18439.395 14579.194 14710.810 11447.987 15482.890
## 2004 21832.137 20887.230 22222.644 20174.054 19375.723
## 2005 15937.776 14257.581 15084.435 8907.089 11410.615
## 2006 19043.163 19380.738 22006.895 18378.356 23467.977
## 2007 19484.840 17641.647 15529.645 22277.025 21569.001
## 2008 21781.830 25476.608 22627.287 18607.863 23782.611
## 2009 21692.522 16740.313 17592.306 19409.682 18814.012
## 2010 22676.742 22138.471 23023.719 23018.057 20225.372
## 2011 14292.827 14148.832 19954.407 17822.182 16334.254
## 2012 18682.398 24038.525 20109.848 25167.949 19508.316
## 2013 18585.722 25274.322 19119.917 20570.952 19826.441
## 2014 24877.201 20087.988 25043.527 21745.267 18818.125
## 2015 21332.298 21623.039 19737.468 19262.825 16800.645
## 2016 24569.793
pronosticos <- forecast(modelo, level=c(50), h=24)
autoplot(pronosticos)
