【问题标题】:Facet a fabletools autoplot of a fable forecast by modelFacet a fabletools 按模型预测的寓言自动绘图
【发布时间】:2021-09-19 05:26:55
【问题描述】:

有什么方法可以将自动绘图与寓言一起使用,但要通过模型来刻画它?下面的代码生成了一个漂亮的小图表,但将预测叠加在一起。

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)

这似乎是很常见的事情,但我知道这些的自动绘图意味着非常快速和肮脏,所以我不知道它是否有这个功能。我浏览了 fabletools github,但找不到任何东西。

【问题讨论】:

    标签: r fable fabletools


    【解决方案1】:

    也许这就是你要找的。 autoplot 返回一个 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)
    

    【讨论】:

    • 你是个传奇!我什至没有想过它会返回一个 ggplot 对象。我知道会有一个比我想的更简单/更直接的解决方案。谢谢大家!