Análisis de acciones a través de yahoo finance - Serie de tiempo de Yamaha.

Primero cargaremos todos los paquetes que utilizaremos para el desarrollo de esta actividad.

require(ggplot2)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.6.3
require(plotly)
## Loading required package: plotly
## Warning: package 'plotly' was built under R version 3.6.3
## 
## Attaching package: '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
require(forecast)
## Loading required package: forecast
## Warning: package 'forecast' was built under R version 3.6.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
require(tseries)
## Loading required package: tseries
## Warning: package 'tseries' was built under R version 3.6.3
require(fGarch)
## Loading required package: fGarch
## Warning: package 'fGarch' was built under R version 3.6.3
## Loading required package: timeDate
## Warning: package 'timeDate' was built under R version 3.6.3
## Loading required package: timeSeries
## Warning: package 'timeSeries' was built under R version 3.6.3
## Loading required package: fBasics
## Warning: package 'fBasics' was built under R version 3.6.3

Ahora traemos la basede datos de yahoo finance

con=url("https://finance.yahoo.com")

yamaha= get.hist.quote(instrument="YAMCY", start="2015-01-01",quote="Close",compression = "m")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## time series ends   2020-10-01

Grafiquemos el comportamiento de esta serie de tiempo:

plot(yamaha)

Podemos decir que desde el año 2015 las acciones han tenido un comportamiento con tendencia al alza; sin embargo ha tenido picos decrecientes en diferentes años y el que presenta la pendiente más pronunciada es el 2020 claramente por los efectos del covid-19.

serie_yamaha=decompose(ts(yamaha,frequency = 12))
plot(serie_yamaha)

Después de relizar la descomposición de la serie de tiempo observamos lo siguiente: la aleatoridad no tiene marcado un comportamiento constante, por el contrario es muy notoria la variabilidad, igualmete la estacionalidad. por otro la tendencia es alcista y está bien definida con un comportamiento constante.

yamaha1=as.numeric(yamaha)
length(yamaha1)
## [1] 70
fecha=time(yamaha)
df1=data.frame(fecha,yamaha1)

fecha2=rep(fecha,2)
precios=c(yamaha1)
empresa=c(rep("yamaha1",length(yamaha1)))
df2=data.frame(fecha2,precios,empresa)
df2
##         fecha2 precios empresa
## 1   2015-01-01   14.94 yamaha1
## 2   2015-02-01   16.91 yamaha1
## 3   2015-03-01   17.60 yamaha1
## 4   2015-04-01   18.43 yamaha1
## 5   2015-05-01   20.83 yamaha1
## 6   2015-06-01   20.36 yamaha1
## 7   2015-07-01   23.24 yamaha1
## 8   2015-08-01   23.02 yamaha1
## 9   2015-09-01   21.88 yamaha1
## 10  2015-10-01   24.22 yamaha1
## 11  2015-11-01   25.41 yamaha1
## 12  2015-12-01   24.53 yamaha1
## 13  2016-01-01   22.73 yamaha1
## 14  2016-02-01   28.12 yamaha1
## 15  2016-03-01   29.78 yamaha1
## 16  2016-04-01   30.35 yamaha1
## 17  2016-05-01   30.00 yamaha1
## 18  2016-06-01   28.35 yamaha1
## 19  2016-07-01   27.76 yamaha1
## 20  2016-08-01   32.72 yamaha1
## 21  2016-09-01   32.65 yamaha1
## 22  2016-10-01   35.83 yamaha1
## 23  2016-11-01   30.57 yamaha1
## 24  2016-12-01   30.41 yamaha1
## 25  2017-01-01   30.75 yamaha1
## 26  2017-02-01   25.63 yamaha1
## 27  2017-03-01   27.75 yamaha1
## 28  2017-04-01   28.74 yamaha1
## 29  2017-05-01   32.15 yamaha1
## 30  2017-06-01   35.00 yamaha1
## 31  2017-07-01   35.65 yamaha1
## 32  2017-08-01   35.15 yamaha1
## 33  2017-09-01   37.55 yamaha1
## 34  2017-10-01   39.38 yamaha1
## 35  2017-11-01   37.37 yamaha1
## 36  2017-12-01   37.27 yamaha1
## 37  2018-01-01   41.97 yamaha1
## 38  2018-02-01   44.06 yamaha1
## 39  2018-03-01   44.26 yamaha1
## 40  2018-04-01   47.94 yamaha1
## 41  2018-05-01   52.27 yamaha1
## 42  2018-06-01   52.64 yamaha1
## 43  2018-07-01   47.09 yamaha1
## 44  2018-08-01   47.97 yamaha1
## 45  2018-09-01   52.76 yamaha1
## 46  2018-10-01   43.69 yamaha1
## 47  2018-11-01   42.50 yamaha1
## 48  2018-12-01   41.81 yamaha1
## 49  2019-01-01   43.89 yamaha1
## 50  2019-02-01   48.88 yamaha1
## 51  2019-03-01   50.18 yamaha1
## 52  2019-04-01   50.83 yamaha1
## 53  2019-05-01   44.92 yamaha1
## 54  2019-06-01   47.80 yamaha1
## 55  2019-07-01   46.98 yamaha1
## 56  2019-08-01   42.49 yamaha1
## 57  2019-09-01   45.24 yamaha1
## 58  2019-10-01   46.29 yamaha1
## 59  2019-11-01   55.32 yamaha1
## 60  2019-12-01   56.87 yamaha1
## 61  2020-01-01   52.35 yamaha1
## 62  2020-02-01   47.39 yamaha1
## 63  2020-03-01   38.54 yamaha1
## 64  2020-04-01   40.44 yamaha1
## 65  2020-05-01   49.15 yamaha1
## 66  2020-06-01   47.51 yamaha1
## 67  2020-07-01   46.26 yamaha1
## 68  2020-08-01   48.82 yamaha1
## 69  2020-09-01   48.17 yamaha1
## 70  2020-10-01   48.25 yamaha1
## 71  2015-01-01   14.94 yamaha1
## 72  2015-02-01   16.91 yamaha1
## 73  2015-03-01   17.60 yamaha1
## 74  2015-04-01   18.43 yamaha1
## 75  2015-05-01   20.83 yamaha1
## 76  2015-06-01   20.36 yamaha1
## 77  2015-07-01   23.24 yamaha1
## 78  2015-08-01   23.02 yamaha1
## 79  2015-09-01   21.88 yamaha1
## 80  2015-10-01   24.22 yamaha1
## 81  2015-11-01   25.41 yamaha1
## 82  2015-12-01   24.53 yamaha1
## 83  2016-01-01   22.73 yamaha1
## 84  2016-02-01   28.12 yamaha1
## 85  2016-03-01   29.78 yamaha1
## 86  2016-04-01   30.35 yamaha1
## 87  2016-05-01   30.00 yamaha1
## 88  2016-06-01   28.35 yamaha1
## 89  2016-07-01   27.76 yamaha1
## 90  2016-08-01   32.72 yamaha1
## 91  2016-09-01   32.65 yamaha1
## 92  2016-10-01   35.83 yamaha1
## 93  2016-11-01   30.57 yamaha1
## 94  2016-12-01   30.41 yamaha1
## 95  2017-01-01   30.75 yamaha1
## 96  2017-02-01   25.63 yamaha1
## 97  2017-03-01   27.75 yamaha1
## 98  2017-04-01   28.74 yamaha1
## 99  2017-05-01   32.15 yamaha1
## 100 2017-06-01   35.00 yamaha1
## 101 2017-07-01   35.65 yamaha1
## 102 2017-08-01   35.15 yamaha1
## 103 2017-09-01   37.55 yamaha1
## 104 2017-10-01   39.38 yamaha1
## 105 2017-11-01   37.37 yamaha1
## 106 2017-12-01   37.27 yamaha1
## 107 2018-01-01   41.97 yamaha1
## 108 2018-02-01   44.06 yamaha1
## 109 2018-03-01   44.26 yamaha1
## 110 2018-04-01   47.94 yamaha1
## 111 2018-05-01   52.27 yamaha1
## 112 2018-06-01   52.64 yamaha1
## 113 2018-07-01   47.09 yamaha1
## 114 2018-08-01   47.97 yamaha1
## 115 2018-09-01   52.76 yamaha1
## 116 2018-10-01   43.69 yamaha1
## 117 2018-11-01   42.50 yamaha1
## 118 2018-12-01   41.81 yamaha1
## 119 2019-01-01   43.89 yamaha1
## 120 2019-02-01   48.88 yamaha1
## 121 2019-03-01   50.18 yamaha1
## 122 2019-04-01   50.83 yamaha1
## 123 2019-05-01   44.92 yamaha1
## 124 2019-06-01   47.80 yamaha1
## 125 2019-07-01   46.98 yamaha1
## 126 2019-08-01   42.49 yamaha1
## 127 2019-09-01   45.24 yamaha1
## 128 2019-10-01   46.29 yamaha1
## 129 2019-11-01   55.32 yamaha1
## 130 2019-12-01   56.87 yamaha1
## 131 2020-01-01   52.35 yamaha1
## 132 2020-02-01   47.39 yamaha1
## 133 2020-03-01   38.54 yamaha1
## 134 2020-04-01   40.44 yamaha1
## 135 2020-05-01   49.15 yamaha1
## 136 2020-06-01   47.51 yamaha1
## 137 2020-07-01   46.26 yamaha1
## 138 2020-08-01   48.82 yamaha1
## 139 2020-09-01   48.17 yamaha1
## 140 2020-10-01   48.25 yamaha1

Ahora hagamos el gráfico dinámico.

g1=ggplot(df2,aes(x=fecha2,y=precios, col=empresa))+geom_line()+theme_bw()+geom_smooth(method= "gam")+labs(x="fecha",y="precio de la acción")+ggtitle("Precio mensual de la acción de Yamaha desde hace 5 años")
g1
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'

ggplotly(g1)
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'

Como hemos mencionado con antelación el precio de la acción en los últimos 5 años ha tenido un comportamiento constnte al alza