-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtimeseries-moving-average.Rmd
240 lines (181 loc) · 5.08 KB
/
timeseries-moving-average.Rmd
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
---
title: "Visualizing Time Series in R<br><br>Using tidyverse and tidyquant"
subtitle: "Daily Stock Prices and Moving Averages"
author: "StatistikinDD"
date: "Created: `r Sys.Date()`"
output:
xaringan::moon_reader:
chakra: libs/remark-latest.min.js
lib_dir: libs
css: ["libs/_css/xaringan-themer.css", "libs/_css/my_css.css"]
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
slideNumberFormat: "%current%"
ratio: 16:9
---
```{r setup, include = FALSE}
options(htmltools.dir.version = FALSE)
# knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, comment = "")
knitr::opts_chunk$set(echo = FALSE, comment = "")
library(knitr)
library(ggplot2)
theme_set(theme_gray(base_size = 16))
```
# The tidyquant package
* Authors: Matt Dancho, Davis Vaughan
* Maintainer: Matt Dancho
* Check out https://www.business-science.io/
** A Bridge between Time Series specific packages and the Tidyverse**
--
```{r, out.height = "75%", out.width = "75%", fig.align = "center"}
knitr::include_graphics("libs/_Images/tidyquant-tidyverse.png")
```
Source: Youtube video, see *Introduction to tidyquant* vignette
`help(package = "tidyquant")`
---
# Getting Amazon Stock Prices
```{r get-data, echo = TRUE, message = FALSE}
library(tidyverse)
library(tidyquant)
AMZN <- tq_get("AMZN", from = "2015-01-01", to = "2021-05-31")
head(AMZN) %>% kable()
```
---
# A First Line Plot: Amazon Stock Prices
.pull-left[
```{r first-plot, echo = TRUE, eval = FALSE}
AMZN %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line()
```
]
.pull-right[
```{r, ref.label = "first-plot"}
```
]
---
# Some Finetuning
.pull-left[
```{r finetuning, echo = TRUE, eval = FALSE}
theme_set(ggthemes::theme_wsj()) #<<
AMZN %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line() +
scale_y_continuous( #<<
labels = scales::label_dollar(), #<<
position = "right") + #<<
labs(title = "Amazon Stock Price",
x = "")
```
]
.pull-right[
```{r, ref.label = "finetuning"}
```
]
---
# Zooming in: From 2020-01-01, into Covid
.pull-left[
```{r zoom-in, echo = TRUE, eval = FALSE}
AMZN %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line() +
scale_y_continuous(
labels = scales::label_dollar(),
position = "right") +
coord_x_date( #<<
xlim = c("2020-01-01", "2021-05-31"), #<<
ylim = c(1500, 3500)) + #<<
labs(title = "Amazon Stock Price",
x = "")
```
]
.pull-right[
```{r, ref.label = "zoom-in"}
```
]
---
# Adding A 30 Day Moving Average
.pull-left[
```{r sma-30, echo = TRUE, eval = FALSE}
AMZN %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line() +
geom_ma(ma_fun = SMA, n = 30, size = 1.2) + #<<
scale_y_continuous(
labels = scales::label_dollar(),
position = "right") +
coord_x_date(
xlim = c("2020-01-01", "2021-05-31"),
ylim = c(1500, 3500)) +
labs(title = "Amazon Stock Price",
x = "")
```
]
.pull-right[
```{r, ref.label = "sma-30"}
```
]
---
# Adding A 90 Day Moving Average
.pull-left[
```{r sma-90, echo = TRUE, eval = FALSE}
AMZN %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line() +
geom_ma(ma_fun = SMA, n = 30, size = 1.2) + #<<
geom_ma(ma_fun = SMA, n = 90, #<<
color = "green", size = 1.2) + #<<
scale_y_continuous(
labels = scales::label_dollar(),
position = "right") +
coord_x_date(
xlim = c("2020-01-01", "2021-05-31"),
ylim = c(1500, 3500)) +
labs(title = "Amazon Stock Price",
x = "")
```
]
.pull-right[
```{r, ref.label = "sma-90"}
```
]
---
# Adding A Custom Legend
.pull-left[
```{r legend, echo = TRUE, eval = FALSE}
AMZN %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line(aes(color = "Daily")) + #<<
scale_y_continuous(
labels = scales::label_dollar(),
position = "right") +
coord_x_date(xlim = c("2020-01-01", "2021-05-31"),
ylim = c(1500, 3500)) +
labs(title = "Amazon Stock Price", x = "",
subtitle = "Daily Adjusted / \nSimple Moving Averages (SMA)",
caption = "Created using R, tidyverse, \n ggthemes, and tidyquant") +
geom_ma(ma_fun = SMA, n = 30,
aes(color = "30 Day SMA")) + #<<
geom_ma(ma_fun = SMA, n = 90,
aes(color = "90 Day SMA")) + #<<
scale_color_manual(name = "", #<<
values = c("Daily" = "black", #<<
"30 Day SMA" = "blue", #<<
"90 Day SMA" = "green")) #<<
```
]
.pull-right[
```{r, ref.label = "legend"}
```
]
---
class: center, middle
# Thanks!
### Youtube: StatistikinDD
### Twitter: @StatistikinDD
### github: fjodor
Slides created via the R package [**xaringan**](https://github.com/yihui/xaringan).
The chakra comes from [remark.js](https://remarkjs.com), [**knitr**](https://yihui.org/knitr), and [R Markdown](https://rmarkdown.rstudio.com).
Thanks to **Yihui Xie** for *{knitr}* and *{xaringan}* and **Garrick Aden-Buie** for *{xaringanthemer}*.