#|output: false
suppressPackageStartupMessages({
<- c("quantmod", "tidyverse", "PerformanceAnalytics", "scales", "readxl","forecast","tseries","ggfortify")
packages for(pkg in packages) {
if(!require(pkg, character.only = TRUE)) {
install.packages(pkg)
library(pkg, character.only = TRUE)
}
} })
Analisis de Estacionariedad
Carga de librerías
Lectura de datos desde Excel
#Índice de Servicios y bienes de consumo no básico
<-read_excel("MXSE04.xlsx")
SE04head(SE04)
# A tibble: 6 × 8
`Exchange Date` Close Net `%Chg` Open Low High Volume
<dttm> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2013-05-24 00:00:00 482. NA NA 502. 476. 502. 14125787
2 2013-05-31 00:00:00 502. 19.9 0.0413 483. 482. 502. 11190811
3 2013-06-07 00:00:00 486. -15.3 -0.0305 497. 482. 497. 7969728
4 2013-06-14 00:00:00 483. -3.40 -0.00699 491. 481. 491. 7567039
5 2013-06-21 00:00:00 460. -22.5 -0.0467 477. 457. 477. 22474036
6 2013-06-28 00:00:00 470. 10.1 0.0219 456. 455. 471. 26258090
Convertir los datos a objeto tipo “ts”
<- as.Date("2013-05-24")
fecha_inicio
<-ts(SE04,start = c(2013,
SE.tsas.integer(format(fecha_inicio, "%U"))),
frequency = 52)
head(SE.ts)
Time Series:
Start = c(2013, 20)
End = c(2013, 25)
Frequency = 52
Exchange Date Close Net %Chg Open Low High Volume
2013.365 1369353600 481.67 NA NA 501.73 475.96 502.04 14125787
2013.385 1369958400 501.56 19.89 0.041293832 482.81 481.80 501.56 11190811
2013.404 1370563200 486.25 -15.31 -0.030524763 496.83 481.75 496.85 7969728
2013.423 1371168000 482.85 -3.40 -0.006992288 490.80 480.66 491.33 7567039
2013.442 1371772800 460.32 -22.53 -0.046660454 477.43 456.89 477.47 22474036
2013.462 1372377600 470.41 10.09 0.021919534 455.78 454.56 470.80 26258090
tail(SE.ts)
Time Series:
Start = c(2023, 16)
End = c(2023, 21)
Frequency = 52
Exchange Date Close Net %Chg Open Low High Volume
2023.288 1681430400 839.94 28.95 0.035697111 812.92 800.08 846.27 21114935
2023.308 1682035200 832.94 -7.00 -0.008333929 842.47 828.76 848.59 28778263
2023.327 1682640000 854.53 21.59 0.025920234 833.08 802.35 855.91 28310945
2023.346 1683244800 866.76 12.23 0.014311961 853.41 845.24 873.99 32124695
2023.365 1683849600 887.95 21.19 0.024447367 866.74 852.31 891.44 35143711
2023.385 1684454400 883.87 -4.08 -0.004594853 888.81 868.11 903.48 18437493
Gráfica de los datos
autoplot(SE.ts)
Eliminar “Exchange Date”
.2<-SE.ts[,-1] SE.ts
Gráfica
autoplot(SE.ts.2)
Eliminar “Volume”
.2<-SE.ts.2[,-7]
SE.tsautoplot(SE.ts.2)
Selección sólo del precio de cierre
<-Cl(SE.ts)
SE.clautoplot(SE.cl, ts.colour="gold", ts.linetype="solid", size=1)
Descomposición de la serie
autoplot(stl(SE.cl, s.window="periodic"),
ts.colour="khaki4", size=0.7)
Descomposición del índice por semanas
boxplot(SE.cl~cycle(SE.cl), col="goldenrod")
Transformación logarítmica del índice
<-log(SE.cl)
lseautoplot(lse,ts.colour="gold", ts.linetype="solid", size=1)
¿Es estacionaria?
Función de Autocorrelación
acf(lse)
Pruebas de Dickey-Fuller y de Phillips-Perron
adf.test(lse)
Augmented Dickey-Fuller Test
data: lse
Dickey-Fuller = -2.3535, Lag order = 8, p-value = 0.4287
alternative hypothesis: stationary
pp.test(lse)
Phillips-Perron Unit Root Test
data: lse
Dickey-Fuller Z(alpha) = -8.0662, Truncation lag parameter = 6, p-value
= 0.6598
alternative hypothesis: stationary
Primeras diferencias
<-diff(lse)
d1.lseautoplot(d1.lse,ts.colour="coral", ts.linetype="solid", size=1)
acf(d1.lse)
adf.test(d1.lse)
Warning in adf.test(d1.lse): p-value smaller than printed p-value
Augmented Dickey-Fuller Test
data: d1.lse
Dickey-Fuller = -6.268, Lag order = 8, p-value = 0.01
alternative hypothesis: stationary
pp.test(d1.lse)
Warning in pp.test(d1.lse): p-value smaller than printed p-value
Phillips-Perron Unit Root Test
data: d1.lse
Dickey-Fuller Z(alpha) = -548.52, Truncation lag parameter = 6, p-value
= 0.01
alternative hypothesis: stationary