Skip to content

Instantly share code, notes, and snippets.

@MJacobs1985
Created February 21, 2022 13:38
Show Gist options
  • Select an option

  • Save MJacobs1985/7af02f3af3967be96b25db9f7d4ab59e to your computer and use it in GitHub Desktop.

Select an option

Save MJacobs1985/7af02f3af3967be96b25db9f7d4ab59e to your computer and use it in GitHub Desktop.
df_nest<-day%>%
dplyr::select(date_interval,location_identification, egg_count)%>%
group_by(location_identification)%>%
filter(!location_identification%in%c(4))%>%
nest()
df_ts<-df_nest%>%mutate(data.ts = map(.x = data,
.f = tk_ts,
select = -date_interval,
start = 2021,
freq = 12))
df_ts_fit<-df_ts%>%mutate(fit.ets = map(data.ts, ets))
df_ts_fit %>%
mutate(tidy = map(fit.ets, sw_tidy)) %>%
unnest(tidy) %>%
spread(key = location_identification, value = estimate)
df_ts_fit %>%
mutate(glance = map(fit.ets, sw_glance)) %>%
unnest(glance)
augment_fit_ets <- df_ts_fit %>%
mutate(augment = map(fit.ets,
sw_augment,
timetk_idx = TRUE,
rename_index = "date")) %>%
unnest(augment)
augment_fit_ets %>%
ggplot(aes(x = date, y = .resid, group = location_identification)) +
geom_hline(yintercept = 0, color = "grey40") +
geom_line() +
geom_smooth(method = "loess") +
labs(title = "Eggs produced By Location",
subtitle = "ETS Model Residuals", x = "") +
theme_bw() +
facet_wrap(~ location_identification, scale = "free", ncol = 3)
df_ts_fit%>%
mutate(decomp = map(fit.ets,
sw_tidy_decomp,
timetk_idx = TRUE, rename_index = "date")) %>%
unnest(decomp)
df_ts_fcast <- df_ts_fit%>%
mutate(fcast.ets = map(fit.ets, forecast, h = 12)); df_ts_fcast
df_ts_fcast_tidy <- df_ts_fcast %>%
mutate(sweep = map(fcast.ets, sw_sweep, fitted = FALSE, timetk_idx = TRUE)) %>%
unnest(sweep); df_ts_fcast_tidy
df_ts_fcast_tidy %>%
ggplot(aes(x = index,
y = value,
color = key,
group = location_identification)) +
geom_ribbon(aes(ymin = lo.95, ymax = hi.95),
fill = "#D5DBFF", color = NA, size = 0) +
geom_ribbon(aes(ymin = lo.80, ymax = hi.80, fill = key),
fill = "#596DD5", color = NA, size = 0, alpha = 0.8) +
geom_line() +
labs(title = "Eggs produced By Location",
subtitle = "ETS Model Forecasts",
x = "", y = "Units") +
facet_wrap(~location_identification,
scales = "free",
ncol = 3) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment