This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE
parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
library(quantmod)
## Warning: package 'quantmod' was built under R version 4.4.3
## Cargando paquete requerido: xts
## Warning: package 'xts' was built under R version 4.4.3
## Cargando paquete requerido: zoo
## Warning: package 'zoo' was built under R version 4.4.3
##
## Adjuntando el paquete: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Cargando paquete requerido: TTR
## Warning: package 'TTR' was built under R version 4.4.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
# Descargar datos de Apple desde Yahoo Finance
getSymbols("AAPL", src = "yahoo", from = "2020-01-01", to = "2025-01-01")
## [1] "AAPL"
# Ver primeros datos
head(AAPL)
## AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2020-01-02 74.0600 75.1500 73.7975 75.0875 135480400 72.53851
## 2020-01-03 74.2875 75.1450 74.1250 74.3575 146322800 71.83330
## 2020-01-06 73.4475 74.9900 73.1875 74.9500 118387200 72.40568
## 2020-01-07 74.9600 75.2250 74.3700 74.5975 108872000 72.06516
## 2020-01-08 74.2900 76.1100 74.2900 75.7975 132079200 73.22441
## 2020-01-09 76.8100 77.6075 76.5500 77.4075 170108400 74.77977
# Guardar en CSV
write.csv(AAPL, file = "AAPL_data.csv")
packages = c('corrplot', 'ggpubr', 'plotly', 'tidyverse')
for(p in packages){library
if(!require(p, character.only = T)){
install.packages(p)
}
library(p, character.only = T)
}
## Cargando paquete requerido: corrplot
## Warning: package 'corrplot' was built under R version 4.4.3
## corrplot 0.95 loaded
## Cargando paquete requerido: ggpubr
## Warning: package 'ggpubr' was built under R version 4.4.3
## Cargando paquete requerido: ggplot2
## Cargando paquete requerido: plotly
## Warning: package 'plotly' was built under R version 4.4.3
##
## Adjuntando el paquete: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
## Cargando paquete requerido: tidyverse
## Warning: package 'tidyverse' was built under R version 4.4.3
## Warning: package 'tidyr' was built under R version 4.4.3
## Warning: package 'readr' was built under R version 4.4.3
## Warning: package 'purrr' was built under R version 4.4.3
## Warning: package 'dplyr' was built under R version 4.4.3
## Warning: package 'forcats' was built under R version 4.4.3
## Warning: package 'lubridate' was built under R version 4.4.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.4 ✔ tibble 3.2.1
## ✔ purrr 1.0.4 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::first() masks xts::first()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks xts::last()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
install.packages("quantmod")
## Warning: package 'quantmod' is in use and will not be installed
library(quantmod)
# Descargar datos de Apple desde Yahoo Finance
getSymbols("ETNB", src = "yahoo", from = "2020-01-01", to = "2025-01-01")
## [1] "ETNB"
# Ver primeros datos
head(ETNB)
## ETNB.Open ETNB.High ETNB.Low ETNB.Close ETNB.Volume ETNB.Adjusted
## 2020-01-02 27.24 27.24 24.39 25.10 45400 25.10
## 2020-01-03 24.75 28.99 24.31 26.83 137600 26.83
## 2020-01-06 26.18 27.89 25.01 27.13 79200 27.13
## 2020-01-07 26.86 27.25 25.87 27.12 63500 27.12
## 2020-01-08 26.96 27.43 25.25 25.76 70800 25.76
## 2020-01-09 25.91 26.98 25.27 25.62 97000 25.62
# Guardar en CSV
write.csv(ETNB, file = "ETNB_data.csv")
library(quantmod)
# Descargar datos de Apple desde Yahoo Finance
getSymbols("AMZN", src = "yahoo", from = "2020-01-01", to = "2025-01-01")
## [1] "AMZN"
# Ver primeros datos
head(AMZN)
## AMZN.Open AMZN.High AMZN.Low AMZN.Close AMZN.Volume AMZN.Adjusted
## 2020-01-02 93.7500 94.9005 93.2075 94.9005 80580000 94.9005
## 2020-01-03 93.2250 94.3100 93.2250 93.7485 75288000 93.7485
## 2020-01-06 93.0000 95.1845 93.0000 95.1440 81236000 95.1440
## 2020-01-07 95.2250 95.6945 94.6020 95.3430 80898000 95.3430
## 2020-01-08 94.9020 95.5500 94.3220 94.5985 70160000 94.5985
## 2020-01-09 95.4945 95.8910 94.7900 95.0525 63346000 95.0525
# Guardar en CSV
write.csv(AMZN, file = "AMZN_data.csv")
library(quantmod)
# Descargar datos de Apple desde Yahoo Finance
getSymbols("GOOGL", src = "yahoo", from = "2020-01-01", to = "2025-01-01")
## [1] "GOOGL"
# Ver primeros datos
head(GOOGL)
## GOOGL.Open GOOGL.High GOOGL.Low GOOGL.Close GOOGL.Volume
## 2020-01-02 67.4205 68.4340 67.3245 68.4340 27278000
## 2020-01-03 67.4000 68.6875 67.3660 68.0760 23408000
## 2020-01-06 67.5815 69.9160 67.5500 69.8905 46768000
## 2020-01-07 70.0230 70.1750 69.5780 69.7555 34330000
## 2020-01-08 69.7410 70.5925 69.6315 70.2520 35314000
## 2020-01-09 71.0965 71.4340 70.5105 70.9895 33200000
## GOOGL.Adjusted
## 2020-01-02 67.96523
## 2020-01-03 67.60969
## 2020-01-06 69.41177
## 2020-01-07 69.27769
## 2020-01-08 69.77079
## 2020-01-09 70.50323
# Guardar en CSV
write.csv(GOOGL, file = "GOOGL_data.csv")
library(quantmod)
# Descargar datos de Apple desde Yahoo Finance
getSymbols("GOOGL", src = "yahoo", from = "2020-01-01", to = "2025-01-01")
## [1] "GOOGL"
# Ver primeros datos
head(GOOGL)
## GOOGL.Open GOOGL.High GOOGL.Low GOOGL.Close GOOGL.Volume
## 2020-01-02 67.4205 68.4340 67.3245 68.4340 27278000
## 2020-01-03 67.4000 68.6875 67.3660 68.0760 23408000
## 2020-01-06 67.5815 69.9160 67.5500 69.8905 46768000
## 2020-01-07 70.0230 70.1750 69.5780 69.7555 34330000
## 2020-01-08 69.7410 70.5925 69.6315 70.2520 35314000
## 2020-01-09 71.0965 71.4340 70.5105 70.9895 33200000
## GOOGL.Adjusted
## 2020-01-02 67.96523
## 2020-01-03 67.60969
## 2020-01-06 69.41175
## 2020-01-07 69.27769
## 2020-01-08 69.77078
## 2020-01-09 70.50323
# Guardar en CSV
write.csv(GOOGL, file = "GOOGL_data.csv")
library(quantmod)
# Descargar datos de Apple desde Yahoo Finance
getSymbols("NFLX", src = "yahoo", from = "2020-01-01", to = "2025-01-01")
## [1] "NFLX"
# Ver primeros datos
head(NFLX)
## NFLX.Open NFLX.High NFLX.Low NFLX.Close NFLX.Volume NFLX.Adjusted
## 2020-01-02 326.10 329.98 324.78 329.81 4485800 329.81
## 2020-01-03 326.78 329.86 325.53 325.90 3806900 325.90
## 2020-01-06 323.12 336.36 321.20 335.83 5663100 335.83
## 2020-01-07 336.47 336.70 330.30 330.75 4703200 330.75
## 2020-01-08 331.49 342.70 331.05 339.26 7104500 339.26
## 2020-01-09 342.00 343.42 334.61 335.66 4709300 335.66
# Guardar en CSV
write.csv(NFLX, file = "NFLX_data.csv")
library(quantmod)
# Descargar datos de Apple desde Yahoo Finance
getSymbols("TSLA", src = "yahoo", from = "2020-01-01", to = "2025-01-01")
## [1] "TSLA"
# Ver primeros datos
head(TSLA)
## TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume TSLA.Adjusted
## 2020-01-02 28.30000 28.71333 28.11400 28.68400 142981500 28.68400
## 2020-01-03 29.36667 30.26667 29.12800 29.53400 266677500 29.53400
## 2020-01-06 29.36467 30.10400 29.33333 30.10267 151995000 30.10267
## 2020-01-07 30.76000 31.44200 30.22400 31.27067 268231500 31.27067
## 2020-01-08 31.58000 33.23267 31.21533 32.80933 467164500 32.80933
## 2020-01-09 33.14000 33.25333 31.52467 32.08933 426606000 32.08933
# Guardar en CSV
write.csv(TSLA, file = "TSLA_data.csv")
library(quantmod)
# Descargar datos de Apple desde Yahoo Finance
getSymbols("DIS", src = "yahoo", from = "2020-01-01", to = "2025-01-01")
## [1] "DIS"
# Ver primeros datos
head(DIS)
## DIS.Open DIS.High DIS.Low DIS.Close DIS.Volume DIS.Adjusted
## 2020-01-02 145.29 148.20 145.10 148.20 9502100 145.7699
## 2020-01-03 146.40 147.90 146.05 146.50 7320200 144.0978
## 2020-01-06 145.54 146.03 144.31 145.65 8262500 143.2617
## 2020-01-07 145.99 146.87 145.42 145.70 6906500 143.3109
## 2020-01-08 145.49 146.13 144.82 145.40 6984200 143.0158
## 2020-01-09 146.47 146.63 144.61 144.83 6664200 142.4552
# Guardar en CSV
write.csv(DIS, file = "DIS_data.csv")
library(quantmod)
# Descargar datos de Apple desde Yahoo Finance
getSymbols("V", src = "yahoo", from = "2020-01-01", to = "2025-01-01")
## [1] "V"
# Ver primeros datos
head(V)
## V.Open V.High V.Low V.Close V.Volume V.Adjusted
## 2020-01-02 189.00 191.14 188.72 191.12 8733000 183.5491
## 2020-01-03 188.41 190.96 187.92 189.60 4899700 182.0893
## 2020-01-06 188.00 189.21 187.16 189.19 10109500 181.6955
## 2020-01-07 189.59 190.10 188.60 188.69 4392300 181.2153
## 2020-01-08 189.49 192.50 188.80 191.92 5712000 184.3174
## 2020-01-09 193.00 193.92 192.52 193.25 6748000 185.5948
# Guardar en CSV
write.csv(V, file = "V_data.csv")
library(quantmod)
# Descargar datos de Apple desde Yahoo Finance
getSymbols("JPM", src = "yahoo", from = "2020-01-01", to = "2025-01-01")
## [1] "JPM"
# Ver primeros datos
head(JPM)
## JPM.Open JPM.High JPM.Low JPM.Close JPM.Volume JPM.Adjusted
## 2020-01-02 139.79 141.10 139.26 141.09 10803700 120.1547
## 2020-01-03 137.50 139.23 137.08 138.34 10386800 118.5691
## 2020-01-06 136.56 138.27 136.50 138.23 10259000 118.4748
## 2020-01-07 137.28 137.86 135.82 135.88 10531300 116.4607
## 2020-01-08 135.70 137.58 135.60 136.94 9695300 117.3692
## 2020-01-09 138.05 138.23 137.04 137.44 9469000 117.7977
# Guardar en CSV
write.csv(JPM, file = "JPM_data.csv")
`
library(quantmod)
library(dplyr)
symbols <- c("AAPL", "MSFT", "AMZN", "GOOGL", "TSLA", "JPM", "V", "DIS", "NFLX", "ETNB")
# Descargar datos
for(sym in symbols){
getSymbols(sym, src = "yahoo", from = "2020-01-01", to = "2025-01-01", auto.assign = TRUE)
}
# Extraer precios de cierre y unir en un xts
close_xts <- Cl(get(symbols[1]))
for(sym in symbols[-1]){
close_xts <- merge(close_xts, Cl(get(sym)))
}
# Cambiar nombres de columnas a los símbolos
colnames(close_xts) <- symbols
# Convertir a data.frame y agregar columna de fecha
close_prices <- data.frame(date = index(close_xts), coredata(close_xts))
# Mostrar primeras filas
head(close_prices)
## date AAPL MSFT AMZN GOOGL TSLA JPM V DIS
## 1 2020-01-02 75.0875 160.62 94.9005 68.4340 28.68400 141.09 191.12 148.20
## 2 2020-01-03 74.3575 158.62 93.7485 68.0760 29.53400 138.34 189.60 146.50
## 3 2020-01-06 74.9500 159.03 95.1440 69.8905 30.10267 138.23 189.19 145.65
## 4 2020-01-07 74.5975 157.58 95.3430 69.7555 31.27067 135.88 188.69 145.70
## 5 2020-01-08 75.7975 160.09 94.5985 70.2520 32.80933 136.94 191.92 145.40
## 6 2020-01-09 77.4075 162.09 95.0525 70.9895 32.08933 137.44 193.25 144.83
## NFLX ETNB
## 1 329.81 25.10
## 2 325.90 26.83
## 3 335.83 27.13
## 4 330.75 27.12
## 5 339.26 25.76
## 6 335.66 25.62
library(tidyr)
library(dplyr)
library(ggplot2)
AAPL_plot <- ggplot(data = close_prices, aes(x = AAPL)) +
geom_histogram(bins = 20, color = "black", fill = "light blue") +
labs(title = "Histograma de AAPL")
ETNB_plot <- ggplot(data = close_prices, aes(x = ETNB)) +
geom_histogram(bins = 20, color = "black", fill = "light blue") +
labs(title = "Histograma de ETNB")
MSFT_plot <- ggplot(data = close_prices, aes(x = MSFT)) +
geom_histogram(bins = 20, color = "black", fill = "light blue") +
labs(title = "Histograma de MSFT")
AMZN_plot <- ggplot(data = close_prices, aes(x = AMZN)) +
geom_histogram(bins = 20, color = "black", fill = "light blue") +
labs(title = "Histograma de AMZN")
GOOGL_plot <- ggplot(data = close_prices, aes(x = GOOGL)) +
geom_histogram(bins = 20, color = "black", fill = "light blue") +
labs(title = "Histograma de GOOGL")
TSLA_plot <- ggplot(data = close_prices, aes(x = TSLA)) +
geom_histogram(bins = 20, color = "black", fill = "light blue") +
labs(title = "Histograma de TSLA")
JPM_plot <- ggplot(data = close_prices, aes(x = JPM)) +
geom_histogram(bins = 20, color = "black", fill = "light blue") +
labs(title = "Histograma de JPM")
V_plot <- ggplot(data = close_prices, aes(x = V)) +
geom_histogram(bins = 20, color = "black", fill = "light blue") +
labs(title = "Histograma de V")
DIS_plot <- ggplot(data = close_prices, aes(x = DIS)) +
geom_histogram(bins = 20, color = "black", fill = "light blue") +
labs(title = "Histograma de DIS")
NFLX_plot <- ggplot(data = close_prices, aes(x = NFLX)) +
geom_histogram(bins = 20, color = "black", fill = "light blue") +
labs(title = "Histograma de NFLX")
library(ggpubr)
ggarrange(AAPL_plot, MSFT_plot, AMZN_plot, GOOGL_plot, TSLA_plot, JPM_plot,
V_plot, DIS_plot, NFLX_plot, ETNB_plot,
ncol = 3, nrow = 3)
## $`1`
##
## $`2`
##
## attr(,"class")
## [1] "list" "ggarrange"
Analisis: En primer lugar, podemos encontrar que AAPL tiene una distribución simetrica, concentrada principalmente entre 100 y 200, luciendo asi como una distribución normal con ligera dispersion hacia los extremos.
MSFT es similar a AAPL con un centro en 250-350, su forma es bastante equilibrada y sin colas demasiado largas.
AMZN por su parte es mas dispersa, con valores entre 80 y 200, teniendo asi una distribucion con varios picos. Lo que puede indicar periodos de cambios fuerte en eo precio.
GOOGL es relativamente uniforme entre 80 y 160, con ligeras caidas a los extremos, mostrando asi intervalos con diferentes niveles de frecuencia.
TSLA tiene mucha dispersion, entre 0 y 500, con asimetria positiva, indicando un sesgo hacia la derecha, a su vez que la mayoria de precios estan entre 100 y 300, con datos alcanzando hasta 500.
JPM tiene una distribucion en torno de 100 y 200, concentrada y simetrica
V tiene valores entre 150 y 300, agrupados en forma de campana, lo que indica cierta estabilidad.
DIS tiene una distribucion sesgada hacia la derecha, mas concentrada entre 100 y 150, con cola hacia arriba, reflejando posibles caidas de precio.
NFLX es muy dispersa, con una asimetria positivamente fuerte y una mayoria de datos entre 200-400, pero con valores de hasta 700
ENTB por su parte se mantiene entre 5 y 10, teniendo asi un mucho menor precio absuluto, mayor asimetria y menor estabilidad.
pairs(close_prices[, 2:11], main = "Pairs plot de precios de cierre")
Interpretacion: El grafico de correlacion muestra que los precios de cierre de las acciones estan fuertemente correlacionadas entre si, principalmente las provenientes de grandes empresas tegnologicas, como AAPL, MSFT, AMZN, GOOGL, NFLX Y TSLA. lo que indica que tienen una tendencia a moverse en la misma direccion en el mercado. Las financieras por su parte, tambien siguen una tendencia con menor variabilidad, mientras que ETNB se comporta de manera independiente y volatil, con precio mucho mas bajos y una dispersion distinta, contrastando con la estabilidad de los demas.
pairs(close_prices[, 2:11], upper.panel = NULL)
El grafico muestra que la mayoria de las acciones analizadas tienen relaciones lineales positivamente fuertes, especialmente entre las grandes tegnologias, este estilo del grafico lo hace mas claro, mostrando como las grandes tegnologicas destacan, lo que es evidente en la forma en que sus precios de cierre se mueven de forma conjunta en respuesta a factores de mercado comunes. JPM Y V tambien demuestran correlaciones positivas, aunque con menor pendiente y dispersion mas controlada. Por otro lado, al igual que en el grafico anterior, el grafico de dispersion triangular destaca la poca relacion de ETNB con el resto, confirmando su baja correlacion y su comportamiento independiente.
pairs(close_prices[, 2:11], lower.panel = NULL)
El analisis se mantiene igual que el anterior debido a que el cambio es principalmente de estilo y modo de lectura, con datos similares.
panel.cor <- function(x, y, digits=2, prefix="", cex.cor, ...) {
usr <- par("usr")
on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- abs(cor(x, y, use="complete.obs"))
txt <- format(c(r, 0.123456789), digits=digits)[1]
txt <- paste0(prefix, txt)
if(missing(cex.cor)) cex.cor <- 0.8 / strwidth(txt)
text(0.5, 0.5, txt, cex = cex.cor * (1 + r) / 2)
}
pairs(close_prices[, 2:11],
upper.panel = panel.cor,
main = "Pairs plot con correlaciones (precios de cierre)")
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
Interpretacion: Con el añadido del coeficiente de variacion, es posible ver de mejor manera, como las grandes tegonologias como APPL, MSFT, AMZN, GOOGL, NFLX Y TSLA presentan distribuciones con mayor dispersion y correlaciones fuertes entre si (con r superiores a 0.8), lo que evidencia que sus precuos de cierre tienen la tendencia a moverse en la misma dirección frente a factores de mercado comunes; en contraste, las financieras como JPM y V exhiben menor volatibilidad y tambien correlaciones altas entre ellas (r = 0.9), reflejando mayor estabilidad, mientras que por su lado, ETNB se distingue por su asimetria positiva, alta variabilidad relativa y correlaciones muy bajas con el resto (r < 0,2).