Facet a fabletools autoplot 按模型预测寓言

Facet a fabletools autoplot of a fable forecast by model

有什么方法可以在寓言中使用自动绘图,但要根据模型对其进行分面?下面的代码生成了一个漂亮的小图表,但将预测叠加在彼此之上。

library(tidyverse)
library(tsibble)
library(feasts)
library(fable)
library(fabletools)

tourism_melb <- tourism %>%
    filter(Region == "Melbourne") %>% 
    filter(Purpose == "Business") %>% 
    select(-Region, -State, -Purpose)

fableModels <- tourism_melb %>% 
    model(arima = ARIMA(), tslm = TSLM(Trips ~ trend()), mean = MEAN(window = 4))

forecasts <- fableModels %>% 
    forecast(h = "2 years")

forecasts %>% autoplot(tourism_melb)

看起来像这样:

我正在寻找更像这样的东西,除了多面体,这样我就不必处理轴刻度和标签等问题:

library(gridExtra)

arimaPlot <- forecasts %>% 
    filter(.model == "arima") %>% 
    autoplot(tourism_melb)

tslmPlot <- forecasts %>% 
    filter(.model == "tslm") %>% 
    autoplot(tourism_melb)

grid.arrange(arimaPlot, tslmPlot)

这似乎是很常见的事情,但我知道这些自动绘图是非常快速和肮脏的,所以我不知道它是否具有该功能。我查看了 fatabletools github,但找不到任何东西。

也许这就是您要找的。作为 autoplot returns 一个 ggplot 对象,您可以简单地添加 facet_wrap(~.model, ncol = 1)

library(tidyverse)
library(tsibble)
library(feasts)
library(fable)
library(fabletools)

tourism_melb <- tourism %>%
  filter(Region == "Melbourne") %>% 
  filter(Purpose == "Business") %>% 
  select(-Region, -State, -Purpose)

fableModels <- tourism_melb %>% 
  model(arima = ARIMA(), tslm = TSLM(Trips ~ trend()), mean = MEAN(window = 4))

forecasts <- fableModels %>% 
  forecast(h = "2 years")

forecasts %>% autoplot(tourism_melb) + facet_wrap(~.model, ncol = 1)