Ejercicio ARIMA con Google (GOOG)

Ejecutando las librerías

library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(tseries)
library(timeSeries)
## Loading required package: timeDate
## 
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
## 
##     time<-
library(forecast)
library(xts)
library(ggplot2)

1. IDENTIFICACIÒN

GOOG <- getSymbols('GOOG', src='yahoo', from = as.Date("2015-01-01"),to=as.Date("2023-08-04"), auto.assign = FALSE)

Graficando serie

chartSeries(GOOG, name="GOOG", subset="last 6 months", theme=chartTheme("white"))

Datos de yahoo finance:

data_GOOG <- data.frame(GOOG, tiempo = as.Date(rownames(data.frame(GOOG))))
head(data_GOOG)
##            GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume GOOG.Adjusted
## 2015-01-02  26.37808  26.49077 26.13325   26.16865    28951268      26.16865
## 2015-01-05  26.09137  26.14472 25.58276   25.62315    41196796      25.62315
## 2015-01-06  25.67950  25.73809 24.98391   25.02928    57998800      25.02928
## 2015-01-07  25.28059  25.29276 24.91410   24.98640    41301082      24.98640
## 2015-01-08  24.83133  25.10507 24.48278   25.06518    67071641      25.06518
## 2015-01-09  25.16890  25.17688 24.67176   24.74058    41427428      24.74058
##                tiempo
## 2015-01-02 2015-01-02
## 2015-01-05 2015-01-05
## 2015-01-06 2015-01-06
## 2015-01-07 2015-01-07
## 2015-01-08 2015-01-08
## 2015-01-09 2015-01-09
attach(data_GOOG)

Separando la serie close:

base1 = data.frame(tiempo, GOOG.Close)
names (base1) = c("tiempo","GOOG")
base1 <- na.omit(base1) #eliminando datos ominitidos "NA"
#base1
head(base1, n = 10)
##        tiempo     GOOG
## 1  2015-01-02 26.16865
## 2  2015-01-05 25.62315
## 3  2015-01-06 25.02928
## 4  2015-01-07 24.98640
## 5  2015-01-08 25.06518
## 6  2015-01-09 24.74058
## 7  2015-01-12 24.56007
## 8  2015-01-13 24.74107
## 9  2015-01-14 24.97493
## 10 2015-01-15 25.02081

Graficando la serie

GOOG_ma = ts(na.omit(base1$GOOG), frequency=30)
decomp = stl(GOOG_ma, s.window="periodic")
deseasonal_base1 <- seasadj(decomp)
plot(decomp)

ggplot(base1, aes(x = tiempo, y =  GOOG)) + geom_line() +  labs(title = "Cotizaciones de Google", x = "Fecha", y = "Precio por acciòn")

SEGUNDA ETAPA: Análisis y diferenciación de la serie temporal

Análisis de estacionariedad

adf.test(GOOG_ma, alternative = "stationary")
## 
##  Augmented Dickey-Fuller Test
## 
## data:  GOOG_ma
## Dickey-Fuller = -1.9886, Lag order = 12, p-value = 0.5831
## alternative hypothesis: stationary

No hay estacionalidad ya que el p-value es mayor a 0.05 se esta aceptando H0 por ende se acepta la no Estacionalidad

TERCERA ETAPA: Ajuste de un modelo ARIMA.

Ajustamos el modelo.

auto.arima(deseasonal_base1, seasonal=FALSE)
## Series: deseasonal_base1 
## ARIMA(1,1,1) with drift 
## 
## Coefficients:
##          ar1      ma1   drift
##       0.7727  -0.8132  0.0481
## s.e.  0.1441   0.1327  0.0262
## 
## sigma^2 = 2.193:  log likelihood = -3911.72
## AIC=7831.45   AICc=7831.47   BIC=7854.16

CUARTA ETAPA: Predicción

modeloarima<-auto.arima(deseasonal_base1, seasonal=FALSE)
modeloarima
## Series: deseasonal_base1 
## ARIMA(1,1,1) with drift 
## 
## Coefficients:
##          ar1      ma1   drift
##       0.7727  -0.8132  0.0481
## s.e.  0.1441   0.1327  0.0262
## 
## sigma^2 = 2.193:  log likelihood = -3911.72
## AIC=7831.45   AICc=7831.47   BIC=7854.16
tsdisplay(residuals(modeloarima), lag.max=10, main='(1,1,1) Model Residuals')

QUINTA ETAPA

prediccion <- forecast(modeloarima, h=300)
plot(prediccion)

Los datos proyectados para los siguientes 300 días son:

tail(prediccion$mean,300)
## Time Series:
## Start = c(73, 2) 
## End = c(83, 1) 
## Frequency = 30 
##   [1] 128.9059 128.9444 128.9852 129.0276 129.0713 129.1160 129.1614 129.2075
##   [9] 129.2541 129.3010 129.3482 129.3956 129.4432 129.4909 129.5387 129.5865
##  [17] 129.6345 129.6824 129.7305 129.7785 129.8266 129.8746 129.9227 129.9708
##  [25] 130.0189 130.0670 130.1152 130.1633 130.2114 130.2595 130.3077 130.3558
##  [33] 130.4039 130.4521 130.5002 130.5483 130.5965 130.6446 130.6927 130.7409
##  [41] 130.7890 130.8371 130.8853 130.9334 130.9815 131.0297 131.0778 131.1259
##  [49] 131.1741 131.2222 131.2703 131.3185 131.3666 131.4147 131.4629 131.5110
##  [57] 131.5591 131.6073 131.6554 131.7035 131.7517 131.7998 131.8479 131.8961
##  [65] 131.9442 131.9923 132.0405 132.0886 132.1367 132.1849 132.2330 132.2811
##  [73] 132.3293 132.3774 132.4255 132.4737 132.5218 132.5699 132.6181 132.6662
##  [81] 132.7143 132.7625 132.8106 132.8587 132.9069 132.9550 133.0031 133.0513
##  [89] 133.0994 133.1475 133.1957 133.2438 133.2919 133.3401 133.3882 133.4363
##  [97] 133.4845 133.5326 133.5807 133.6289 133.6770 133.7251 133.7733 133.8214
## [105] 133.8695 133.9177 133.9658 134.0139 134.0621 134.1102 134.1583 134.2065
## [113] 134.2546 134.3027 134.3509 134.3990 134.4471 134.4953 134.5434 134.5915
## [121] 134.6397 134.6878 134.7359 134.7841 134.8322 134.8803 134.9285 134.9766
## [129] 135.0247 135.0729 135.1210 135.1691 135.2173 135.2654 135.3135 135.3617
## [137] 135.4098 135.4579 135.5061 135.5542 135.6023 135.6505 135.6986 135.7467
## [145] 135.7949 135.8430 135.8911 135.9393 135.9874 136.0355 136.0837 136.1318
## [153] 136.1799 136.2281 136.2762 136.3243 136.3725 136.4206 136.4687 136.5169
## [161] 136.5650 136.6131 136.6613 136.7094 136.7575 136.8057 136.8538 136.9019
## [169] 136.9501 136.9982 137.0463 137.0945 137.1426 137.1907 137.2389 137.2870
## [177] 137.3351 137.3833 137.4314 137.4795 137.5277 137.5758 137.6239 137.6721
## [185] 137.7202 137.7683 137.8165 137.8646 137.9127 137.9609 138.0090 138.0571
## [193] 138.1053 138.1534 138.2015 138.2497 138.2978 138.3459 138.3941 138.4422
## [201] 138.4903 138.5385 138.5866 138.6347 138.6829 138.7310 138.7791 138.8273
## [209] 138.8754 138.9235 138.9717 139.0198 139.0679 139.1161 139.1642 139.2123
## [217] 139.2605 139.3086 139.3567 139.4049 139.4530 139.5011 139.5493 139.5974
## [225] 139.6455 139.6937 139.7418 139.7899 139.8381 139.8862 139.9343 139.9825
## [233] 140.0306 140.0787 140.1269 140.1750 140.2231 140.2713 140.3194 140.3675
## [241] 140.4157 140.4638 140.5119 140.5601 140.6082 140.6563 140.7045 140.7526
## [249] 140.8007 140.8489 140.8970 140.9451 140.9933 141.0414 141.0895 141.1377
## [257] 141.1858 141.2339 141.2821 141.3302 141.3783 141.4265 141.4746 141.5227
## [265] 141.5709 141.6190 141.6671 141.7153 141.7634 141.8115 141.8597 141.9078
## [273] 141.9559 142.0041 142.0522 142.1003 142.1485 142.1966 142.2448 142.2929
## [281] 142.3410 142.3892 142.4373 142.4854 142.5336 142.5817 142.6298 142.6780
## [289] 142.7261 142.7742 142.8224 142.8705 142.9186 142.9668 143.0149 143.0630
## [297] 143.1112 143.1593 143.2074 143.2556