library(tidyverse)
library(tidyquant)
library(timetk)
# Training Data
FB_tbl <- FANG %>%
filter(symbol == "FB") %>%
select(symbol, date, adjusted)
# ---- PERIOD ----
FB_tbl %>%
mutate(adjusted_30 = smooth_vec(adjusted, period = 30, degree = 2)) %>%
ggplot(aes(date, adjusted)) +
geom_line() +
geom_line(aes(y = adjusted_30), color = "red")
# ---- SPAN ----
FB_tbl %>%
mutate(adjusted_30 = smooth_vec(adjusted, span = 0.75, degree = 2)) %>%
ggplot(aes(date, adjusted)) +
geom_line() +
geom_line(aes(y = adjusted_30), color = "red")
# ---- Loess vs Moving Average ----
# - Loess: Using `degree = 0` to make less flexible. Comperable to a moving average.
FB_tbl %>%
mutate(
adjusted_loess_30 = smooth_vec(adjusted, period = 30, degree = 0),
adjusted_ma_30 = slidify_vec(adjusted, .period = 30,
.f = AVERAGE, .partial = TRUE)
) %>%
ggplot(aes(date, adjusted)) +
geom_line() +
geom_line(aes(y = adjusted_loess_30), color = "red") +
geom_line(aes(y = adjusted_ma_30), color = "blue") +
labs(title = "Loess vs Moving Average")
Run the code above in your browser using DataLab