Reputation: 11
Using the code below and nycflight13 data as example, I am unable to pass filter to autolayer. First autoplot code works; second returns error object 'temp' not found. temp is not being passed into autolayer.
library(tidyverse)
library(tidyselect)
library(ggplot2)
library(ggplotify)
library(broom)
library(forecast)
library(fpp3)
library(fable)
library(tsibble)
library(fabletools)
library(tidyr)
weather <- nycflights13::weather %>%
select(origin, time_hour, temp, humid, precip)
weather_tsbl <- as_tsibble(weather, key = origin, index = time_hour)
##this works
weather_tsbl %>%
filter(origin == "EWR") %>%
autoplot(temp)
##this code temp does not get piped into autolayer Error: obect 'temp' not found
weather_tsbl %>%
filter(origin == "EWR") %>%
autoplot(temp)+
autolayer(meanf(temp, h=20), series="Mean", PI=FALSE)
Tried group_map( ... ) or setting everything in ( ). Why doesn't it pass through? Also tried |> to pipe as well... Eventually want to plot mean, niave, and drift layers of another tsibble I have which is encountering same issue... can't pass the filter into autolayer.
Upvotes: 1
Views: 74
Reputation: 66500
autoplot()
is great for initial exploration, but I find it much easier to troubleshoot and make changes if I prepare the data and modeling first, then feed it into a ggplot I specify. For this, I incorporated some code from the fable vignette, which shows examples of generating multiple models, and this SO question re a sliding window mean model.
weather_tsbl %>%
filter(origin == "EWR") %>%
model(mean = MEAN(temp),
mean_window = MEAN(temp ~ window(size = 20))) %>%
augment() %>%
ggplot(aes(time_hour, temp)) +
geom_line() +
geom_line(aes(y = .fitted, color = .model))
Upvotes: 1