Last active
January 24, 2020 18:54
-
-
Save jonspring/b73c29fde10d1e59848adea8f28b28bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(fable) | |
library(gganimate) | |
set.seed(1) | |
df <- data.frame(x = 1:365, | |
y = cumsum(runif(365, min = -1))) | |
smoothy <- function(alpha1) { | |
df %>% | |
as_tsibble(index = x) %>% | |
# using tip from @mitchoharawild | |
# https://twitter.com/mitchoharawild/status/1220659030634434560 | |
model(ETS(y ~ trend(alpha = alpha1) + error("A") + season("N"))) %>% | |
augment() %>% | |
mutate(alpha = alpha1) %>% | |
select(x, y, alpha, .fitted) | |
} | |
my_seq <- seq(from = 0.7, to = 0.15, length.out = 25*8)^3 | |
animate( | |
c(my_seq, rev(my_seq)) %>% | |
map_dfr(smoothy, .id = "id") %>% | |
as_tibble() %>% | |
mutate(id = as.numeric(id)) %>% | |
ggplot(aes(x, .fitted, | |
group = id)) + | |
geom_line(aes(y = y), color = "red") + | |
geom_line(alpha = 1) + | |
geom_text(data = . %>% group_by(id) %>% slice(1) %>% | |
mutate(alpha_label = paste( | |
"Alpha:", scales::comma(alpha, accuracy = 0.0001))), | |
x = 70, y = -7, size = 10, aes(label = alpha_label)) + | |
annotate("segment", x = 0, xend = 225, y = -5, yend = -5) + | |
geom_point(data = . %>% group_by(id) %>% slice(1), | |
aes(x = alpha * 650), y = -5, | |
size = 3, shape = 21, fill = "gray92") + | |
transition_manual(id), | |
fps = 25, duration = 16, type = "cairo", width = 500, height = 300) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment