1

我目前没有解决此问题的方法,因此无论多么麻烦,只要我的代码再次运行,我都迫切希望解决此问题...

我想用以下方法将 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

非常感谢您的帮助!

4

1 回答 1

0

这不是最直观的错误消息,但我以前使用此功能曾经历过。你实际上必须传递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 包(v0.3.0)于 2020 年 9 月 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
响应变量的特征向量。

但是,如果你这样做,你仍然会得到一个错误:

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.
于 2020-09-28T11:50:03.537 回答