不能将一个 tsibble 转换成一个寓言

Can't convert a tsibble to a fable

目前我没有解决这个问题的方法,所以拼命想解决这个问题,无论多么麻烦只要我的代码再次工作...

我想用以下命令将一个 tsibble 强制转换为寓言对象:

as_fable

文档说这是可能的:

## S3 method for class 'tbl_ts'
as_fable(x, response, distribution, ...)

但是当我指定这个函数的输入参数时,我总是得到一个错误。

示例:

library(tsibbledata)
library(tsibble)
library(fable)
library(fabletools)

aus <- tsibbledata::hh_budget

fit <-  fabletools::model(aus, ARIMA = ARIMA(Debt))

fc_tsibble <- fit %>% 
              fabletools::forecast(., h = 2) %>%
              as_tibble(.) %>% 
              tsibble::as_tsibble(., key = c(Country, .model), index = Year)

fc_tsibble

# A tsibble: 8 x 5 [1Y]
# Key:       Country, .model [4]
  Country   .model  Year        Debt .mean
  <chr>     <chr>  <dbl>      <dist> <dbl>
1 Australia ARIMA   2017  N(215, 21)  215.
2 Australia ARIMA   2018  N(221, 63)  221.
3 Canada    ARIMA   2017   N(188, 7)  188.
4 Canada    ARIMA   2018  N(192, 21)  192.
5 Japan     ARIMA   2017 N(106, 3.8)  106.
6 Japan     ARIMA   2018 N(106, 7.6)  106.
7 USA       ARIMA   2017  N(109, 11)  109.
8 USA       ARIMA   2018  N(110, 29)  110.

class(fc_tsibble)

[1] "tbl_ts"     "tbl_df"     "tbl"        "data.frame"

强迫寓言导致错误:

as_fable(fc_tsibble, response = .mean, distribution = Debt)

Error in eval_tidy(enquo(response)) : object '.mean' not found

如有任何帮助,将不胜感激!

这不是最直观的错误消息,但我之前使用此功能遇到过这种情况。您实际上必须将 Debt 传递给两个参数。我认为错误消息引用 .mean 是因为内部函数抛出错误。

library(tsibbledata)
library(tsibble)
library(fable)
library(fabletools)

aus <- tsibbledata::hh_budget

fit <-  fabletools::model(aus, ARIMA = ARIMA(Debt))

fc_tsibble <- fit %>% 
  fabletools::forecast(., h = 2) %>%
  as_tibble(.) %>% 
  tsibble::as_tsibble(., key = c(Country, .model), index = Year)

fc_tsibble
#> # A tsibble: 8 x 5 [1Y]
#> # Key:       Country, .model [4]
#>   Country   .model  Year        Debt .mean
#>   <chr>     <chr>  <dbl>      <dist> <dbl>
#> 1 Australia ARIMA   2017  N(215, 21)  215.
#> 2 Australia ARIMA   2018  N(221, 63)  221.
#> 3 Canada    ARIMA   2017   N(188, 7)  188.
#> 4 Canada    ARIMA   2018  N(192, 21)  192.
#> 5 Japan     ARIMA   2017 N(106, 3.8)  106.
#> 6 Japan     ARIMA   2018 N(106, 7.6)  106.
#> 7 USA       ARIMA   2017  N(109, 11)  109.
#> 8 USA       ARIMA   2018  N(110, 29)  110.

fbl <- as_fable(fc_tsibble, response = "Debt", distribution = "Debt")

fbl
#> # A fable: 8 x 5 [1Y]
#> # Key:     Country, .model [4]
#>   Country   .model  Year        Debt .mean
#>   <chr>     <chr>  <dbl>      <dist> <dbl>
#> 1 Australia ARIMA   2017  N(215, 21)  215.
#> 2 Australia ARIMA   2018  N(221, 63)  221.
#> 3 Canada    ARIMA   2017   N(188, 7)  188.
#> 4 Canada    ARIMA   2018  N(192, 21)  192.
#> 5 Japan     ARIMA   2017 N(106, 3.8)  106.
#> 6 Japan     ARIMA   2018 N(106, 7.6)  106.
#> 7 USA       ARIMA   2017  N(109, 11)  109.
#> 8 USA       ARIMA   2018  N(110, 29)  110.

reprex package (v0.3.0)

于 2020-09-28 创建

如果您不引用分布变量,它也有效。

as_fable(fc_tsibble, response = "Debt", distribution = Debt)
#> # A fable: 8 x 5 [1Y]
#> # Key:     Country, .model [4]
#>   Country   .model  Year        Debt .mean
#>   <chr>     <chr>  <dbl>      <dist> <dbl>
#> 1 Australia ARIMA   2017  N(215, 21)  215.
#> 2 Australia ARIMA   2018  N(221, 63)  221.
#> 3 Canada    ARIMA   2017   N(188, 7)  188.
#> 4 Canada    ARIMA   2018  N(192, 21)  192.
#> 5 Japan     ARIMA   2017 N(106, 3.8)  106.
#> 6 Japan     ARIMA   2018 N(106, 7.6)  106.
#> 7 USA       ARIMA   2017  N(109, 11)  109.
#> 8 USA       ARIMA   2018  N(110, 29)  110.

请注意,在文档中它指定 response 参数应该是一个字符向量:

response
The character vector of response variable(s).

但是,如果您这样做,您仍然会收到错误消息:

as_fable(fc_tsibble, response = ".mean", distribution = Debt)
#> Error: `fbl[[chr_dist]]` must be a vector with type <distribution>.
#> Instead, it has type <distribution>.

该错误消息也不直观且有些矛盾。这是我了解到您实际上想将分布列传递给两个参数的地方:

as_fable(fc_tsibble, response = "Debt", distribution = Debt)
#> # A fable: 8 x 5 [1Y]
#> # Key:     Country, .model [4]
#>   Country   .model  Year        Debt .mean
#>   <chr>     <chr>  <dbl>      <dist> <dbl>
#> 1 Australia ARIMA   2017  N(215, 21)  215.
#> 2 Australia ARIMA   2018  N(221, 63)  221.
#> 3 Canada    ARIMA   2017   N(188, 7)  188.
#> 4 Canada    ARIMA   2018  N(192, 21)  192.
#> 5 Japan     ARIMA   2017 N(106, 3.8)  106.
#> 6 Japan     ARIMA   2018 N(106, 7.6)  106.
#> 7 USA       ARIMA   2017  N(109, 11)  109.
#> 8 USA       ARIMA   2018  N(110, 29)  110.

as_fable(fc_tsibble, response = "Debt", distribution = "Debt")
#> # A fable: 8 x 5 [1Y]
#> # Key:     Country, .model [4]
#>   Country   .model  Year        Debt .mean
#>   <chr>     <chr>  <dbl>      <dist> <dbl>
#> 1 Australia ARIMA   2017  N(215, 21)  215.
#> 2 Australia ARIMA   2018  N(221, 63)  221.
#> 3 Canada    ARIMA   2017   N(188, 7)  188.
#> 4 Canada    ARIMA   2018  N(192, 21)  192.
#> 5 Japan     ARIMA   2017 N(106, 3.8)  106.
#> 6 Japan     ARIMA   2018 N(106, 7.6)  106.
#> 7 USA       ARIMA   2017  N(109, 11)  109.
#> 8 USA       ARIMA   2018  N(110, 29)  110.