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)

它看起来像这样: 相互叠加的预测

我正在寻找更像这样的东西,除了刻面,这样我就不必纠结于轴刻度和标签等:

library(gridExtra)

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

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

grid.arrange(arimaPlot, tslmPlot)

在此处输入图像描述

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

4

1 回答 1

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)

于 2021-07-08T18:47:33.187 回答