install.packages('quantmod') # librerÃa para hacer scraping
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
install.packages('tseries')
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
install.packages('timeSeries')
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
install.packages('forecast')
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
install.packages('xts')
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
install.packages('ggplot2')
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
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)
GOOG <- getSymbols('GOOG', src='yahoo', from = as.Date("2019-01-01"),to=as.Date("2023-08-04"), auto.assign = FALSE)
Graficando la 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
## 2019-01-02 50.8285 52.6160 50.7855 52.2925 30652000 52.2925
## 2019-01-03 52.0500 52.8490 50.7035 50.8030 36822000 50.8030
## 2019-01-04 51.6295 53.5420 51.3709 53.5355 41878000 53.5355
## 2019-01-07 53.5750 53.7000 52.7380 53.4195 39638000 53.4195
## 2019-01-08 53.8055 54.2280 53.0265 53.8140 35298000 53.8140
## 2019-01-09 54.0825 54.1315 53.3200 53.7330 23986000 53.7330
## tiempo
## 2019-01-02 2019-01-02
## 2019-01-03 2019-01-03
## 2019-01-04 2019-01-04
## 2019-01-07 2019-01-07
## 2019-01-08 2019-01-08
## 2019-01-09 2019-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 2019-01-02 52.2925
## 2 2019-01-03 50.8030
## 3 2019-01-04 53.5355
## 4 2019-01-07 53.4195
## 5 2019-01-08 53.8140
## 6 2019-01-09 53.7330
## 7 2019-01-10 53.5165
## 8 2019-01-11 52.8595
## 9 2019-01-14 52.2345
## 10 2019-01-15 53.8575
Graficando la serie
ggplot(base1, aes(x = tiempo, y = GOOG)) + geom_line() + labs(title = "Cotizaciones de Google", x = "Fecha", y = "Precios por accion")
GOOG_ma = ts(na.omit(base1$GOOG), frequency=30)
decomp = stl(GOOG_ma, s.window="periodic")
deseasonal_base1 <- seasadj(decomp)
plot(decomp)
adf.test(GOOG_ma, alternative = "stationary")
##
## Augmented Dickey-Fuller Test
##
## data: GOOG_ma
## Dickey-Fuller = -1.4928, Lag order = 10, p-value = 0.793
## alternative hypothesis: stationary
p-value = 0.793 > 0.05 NRHo no existe estacionariedad
auto.arima(deseasonal_base1, seasonal=FALSE)
## Series: deseasonal_base1
## ARIMA(0,1,0)
##
## sigma^2 = 3.737: log likelihood = -2398.07
## AIC=4798.14 AICc=4798.14 BIC=4803.19