Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

R_starbucks y microsoft.R 4.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  1. #Importando datos de precios de cierre de Starbucks y Microsoft
  2. library(readr)
  3. sbux.df <- read_csv("A CURSO SERIES TEMPORALES (NUEVO)/Clases Nuevas/Starbuck/sbuxPrices.csv")
  4. View(sbux.df)
  5. sbux.ts = ts(data=sbux.df$Adj.Close, frequency = 12,
  6. start=c(1993,3), end=c(2008,3))
  7. class(sbux.ts)
  8. msft.df <- read_csv("A CURSO SERIES TEMPORALES (NUEVO)/Clases Nuevas/Starbuck/msftPrices.csv")
  9. View(sbux.df)
  10. msft.ts = ts(data=msft.df$Adj.Close, frequency = 12,
  11. start=c(1993,3), end=c(2008,3))
  12. #Fechas y frecuencia de la serie
  13. sbux.ts
  14. start(sbux.ts)
  15. end(sbux.ts)
  16. frequency(sbux.ts)
  17. #Subconjunto de la serie de tiempo
  18. tmp = sbux.ts[1:5]
  19. class(tmp)
  20. tmp = window(sbux.ts, start=c(1993, 3), end=c(1993,8))
  21. class(tmp)
  22. #Combinando dos series (dos columnas)
  23. sbuxmsft.ts = cbind(sbux.ts, msft.ts)
  24. class(sbuxmsft.ts)
  25. #mts: multiple time series
  26. #Seleccionando las primeras 5 filas:
  27. window(sbuxmsft.ts, start=c(1993, 3), end=c(1993,7))
  28. #Plot objeto ts
  29. plot(sbux.ts, col="blue", lwd=2, ylab="Adjusted close",
  30. main="Monthly closing price of SBUX")
  31. #Dibujar un subconjunto (Acercar)
  32. plot(window(sbux.ts, start=c(2000,3), end=c(2008,3)),
  33. ylab="Adjusted close",col="blue", lwd=2,
  34. main="Monthly closing price of SBUX")
  35. #Plot para múltiples columnas
  36. #En gráficos diferentes
  37. plot(sbuxmsft.ts)
  38. #En el mismo gráfico
  39. plot(sbuxmsft.ts, plot.type="single",
  40. main="Monthly closing prices on SBUX and MSFT",
  41. ylab="Adjusted close price",
  42. col=c("blue", "red"), lty=1:2)
  43. legend(1994, 35, legend=c("SBUX","MSFT"), col=c("blue", "red"),
  44. lty=1:2)
  45. ################### zoo
  46. library(zoo)
  47. #Fecha
  48. td = seq(as.Date("1993/3/1"), as.Date("2008/3/1"), "months")
  49. class(td)
  50. head(td)
  51. #Alternativa
  52. td2 = as.Date(sbux.df$Date, format="%m/%d/%Y")
  53. head(td2)
  54. #Combinando el índice de tiempo a las dos series de precios
  55. sbux.z = zoo(x=sbux.df$Adj.Close, order.by=td)
  56. msft.z = zoo(x=msft.df$Adj.Close, order.by=td)
  57. class(sbux.z)
  58. str(sbux.z)
  59. head(sbux.z)
  60. #Extrayendo el indice de tiempo y los datos
  61. index(sbux.z)
  62. coredata(sbux.z)
  63. #Start and End
  64. start(sbux.z)
  65. end(sbux.z)
  66. #Ventaja de zoo: extraer subconjunto indexando con las fechas
  67. sbux.z[as.Date(c("2000/3/1", "2003/3/1"))]
  68. #window() también funciona
  69. window(sbux.z, start=as.Date("2000/3/1"), end=as.Date("2003/3/1"))
  70. #Combinando dos series
  71. sbuxmsft.z = cbind(sbux.z, msft.z)
  72. class(sbuxmsft.z)
  73. head(sbuxmsft.z)
  74. #Plot
  75. plot(sbux.z, col="blue", lty=1, lwd=2, ylim=c(0,50),main="Monthly closing prices of SBUX and MFST",
  76. ylab="Adjusted close price")
  77. lines(msft.z, col="red", lty=2, lwd=2)
  78. legend(x="topleft", legend=c("SBUX","MSFT"), col=c("blue","red"),
  79. lty=1:2)
  80. #Alternativa, las dos a la vez
  81. plot(sbuxmsft.z, plot.type="single", col=c("blue","red"), lty=1:2,
  82. lwd=2,main="Monthly closing prices of SBUX and MFST",
  83. ylab="Adjusted close price")
  84. legend(x="topleft", legend=c("SBUX","MSFT"), col=c("blue","red"),
  85. lty=1:2)
  86. #Importar datos directamente como objeto zoo
  87. sbux.z2 = read.zoo("A CURSO SERIES TEMPORALES (NUEVO)/Clases Nuevas/Starbuck/sbuxPrices.csv",
  88. format="%m/%d/%Y", sep=",", header=T)
  89. #Importar datos de Yahoo Finance
  90. library(tseries)
  91. SBUX.z = get.hist.quote(instrument="sbux", start="1993-03-01",
  92. end="2020-06-01", quote="AdjClose",
  93. provider="yahoo", origin="1970-01-01",
  94. compression="d", retclass="zoo")
  95. View(SBUX.z)
  96. MSFT.z = get.hist.quote(instrument="msft", start="1993-03-01",
  97. end="2020-06-01", quote="AdjClose",
  98. provider="yahoo", origin="1970-01-01",
  99. compression="d", retclass="zoo")
  100. #Plot
  101. plot(cbind(SBUX.z,MSFT.z), plot.type="single", col=c("blue","red"), lty=1:2,
  102. lwd=2,main="Monthly closing prices of SBUX and MFST",
  103. ylab="Adjusted close price")
  104. legend(x="topleft", legend=c("SBUX","MSFT"), col=c("blue","red"),
  105. lty=1:2)
  106. ###################################################################
  107. #Libería dygraphs
  108. install.packages("dygraphs")
  109. library(dygraphs)
  110. dygraph(SBUX.z, "Monthly closing prices of SBUX")
  111. dygraph(cbind(SBUX.z,MSFT.z), "Monthly closing prices of SBUX and MFST")
  112. #############################################################3 Datos diarios
  113. #Generamos datos aleatorios
  114. datos <- rnorm(78, 0, 10)
  115. fechas <- seq(as.Date("2020-03-06"), as.Date("2020-05-22"), by = "day")
  116. as.numeric(format(fechas[1], "%j"))
  117. miserie.ts<-ts(datos,start=c(2016,66), frequency=365)
  118. plot(miserie.ts)
  119. library(zoo)
  120. miserie.z=zoo(datos, fechas)
  121. plot(miserie.z)
  122. dygraph(miserie.z)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...