# NOT RUN {
library(recipes)
library(tidyverse)
library(tidyquant)
library(timetk)
# Training Data
FB_tbl <- FANG %>%
filter(symbol == "FB") %>%
select(symbol, date, adjusted)
# New Data - Make some fake new data next 90 time stamps
new_data <- FB_tbl %>%
tail(90) %>%
mutate(date = date %>% tk_make_future_timeseries(length_out = 90))
# ---- PERIOD ----
# Create a recipe object with a step_smooth()
rec_smooth_period <- recipe(adjusted ~ ., data = FB_tbl) %>%
step_smooth(adjusted, period = 30)
# Bake the recipe object - Applies the Loess Transformation
training_data_baked <- bake(prep(rec_smooth_period), FB_tbl)
# "Period" Effect on New Data
new_data_baked <- bake(prep(rec_smooth_period), new_data)
# Smoother's fit on new data is very similar because
# 30 days are used in the new data regardless of the new data being 90 days
training_data_baked %>%
ggplot(aes(date, adjusted)) +
geom_line() +
geom_line(color = "red", data = new_data_baked)
# ---- SPAN ----
# Create a recipe object with a step_smooth
rec_smooth_span <- recipe(adjusted ~ ., data = FB_tbl) %>%
step_smooth(adjusted, span = 0.03)
# Bake the recipe object - Applies the Loess Transformation
training_data_baked <- bake(prep(rec_smooth_span), FB_tbl)
# "Period" Effect on New Data
new_data_baked <- bake(prep(rec_smooth_span), new_data)
# Smoother's fit is not the same using span because new data is only 90 days
# and 0.03 x 90 = 2.7 days
training_data_baked %>%
ggplot(aes(date, adjusted)) +
geom_line() +
geom_line(color = "red", data = new_data_baked)
# ---- NEW COLUMNS ----
# Use the `names` argument to create new columns instead of overwriting existing
rec_smooth_names <- recipe(adjusted ~ ., data = FB_tbl) %>%
step_smooth(adjusted, period = 30, names = "adjusted_smooth_30") %>%
step_smooth(adjusted, period = 180, names = "adjusted_smooth_180") %>%
step_smooth(adjusted, span = 0.75, names = "long_term_trend")
bake(prep(rec_smooth_names), FB_tbl) %>%
ggplot(aes(date, adjusted)) +
geom_line(alpha = 0.5) +
geom_line(aes(y = adjusted_smooth_30), color = "red", size = 1) +
geom_line(aes(y = adjusted_smooth_180), color = "blue", size = 1) +
geom_line(aes(y = long_term_trend), color = "orange", size = 1)
# }
Run the code above in your browser using DataLab