# NOT RUN {
library(recipes)
library(dplyr)
library(rlang)
library(ggplot2, quietly = TRUE)
set.seed(5522)
sim_dat <- data.frame(x1 = (20:100) / 10)
n <- nrow(sim_dat)
sim_dat$y1 <- sin(sim_dat$x1) + rnorm(n, sd = 0.1)
sim_dat$y2 <- cos(sim_dat$x1) + rnorm(n, sd = 0.1)
sim_dat$x2 <- runif(n)
sim_dat$x3 <- rnorm(n)
rec <- recipe(y1 + y2 ~ x1 + x2 + x3, data = sim_dat) %>%
step_window(starts_with("y"), size = 7, statistic = "median",
names = paste0("med_7pt_", 1:2),
role = "outcome") %>%
step_window(starts_with("y"),
names = paste0("mean_3pt_", 1:2),
role = "outcome")
rec <- prep(rec, training = sim_dat)
# If you aren't sure how to set the names, see which variables are selected
# and the order that they are selected:
terms_select(info = summary(rec), terms = quos(starts_with("y")))
smoothed_dat <- bake(rec, sim_dat, everything())
ggplot(data = sim_dat, aes(x = x1, y = y1)) +
geom_point() +
geom_line(data = smoothed_dat, aes(y = med_7pt_1)) +
geom_line(data = smoothed_dat, aes(y = mean_3pt_1), col = "red") +
theme_bw()
tidy(rec, number = 1)
tidy(rec, number = 2)
# If you want to replace the selected variables with the rolling statistic
# don't set `names`
sim_dat$original <- sim_dat$y1
rec <- recipe(y1 + y2 + original ~ x1 + x2 + x3, data = sim_dat) %>%
step_window(starts_with("y"))
rec <- prep(rec, training = sim_dat)
smoothed_dat <- bake(rec, sim_dat, everything())
ggplot(smoothed_dat, aes(x = original, y = y1)) +
geom_point() +
theme_bw()
# }
Run the code above in your browser using DataLab