简体   繁体   English

无法将 tsibble 转换为寓言

[英]Can't convert a tsibble to a fable

I do not have a workaround for this at the moment, so desperately looking to solve this issue, no matter how cumbersome as long as my code is working again...我目前没有解决方法,所以拼命地寻求解决这个问题,无论多么麻烦,只要我的代码再次工作......

I want to coerce a tsibble to a fable object with:我想将 tsibble 强制为寓言对象:

as_fable

Documentation says that this is possible:文档说这是可能的:

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

But when I specify the input parameter of this function I always get an error.但是当我指定这个函数的输入参数时,我总是得到一个错误。

Example:例子:

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"

Coercing to a fable leads to an error:强制使用寓言会导致错误:

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

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

Would be extremely grateful for any help!将非常感谢任何帮助!

It's not the most intuitive error message, but I have experienced this before with this function.这不是最直观的错误信息,但我之前使用过这个功能也遇到过这种情况。 You actually have to pass Debt to both arguments.您实际上必须将Debt传递给这两个参数。 I believe the error message references .mean because of an error thrown by an internal function.我相信错误消息引用.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.

Created on 2020-09-28 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2020 年 9 月 28 日创建

It also works if you don't quote the distribution variable.如果您不引用分布变量,它也有效。

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.

Note that in the documentation it specifies that the response argument should be a character vector:请注意,在文档中它指定response参数应该是一个字符向量:

response回复
The character vector of response variable(s).响应变量的特征向量。

However, if you do this you still get an error:但是,如果您这样做,您仍然会收到错误消息:

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

That error message is also unintuitive and somewhat conflicting.该错误消息也不直观且有些矛盾。 This is where I learned that you actually want to pass the distribution column to both arguments:这是我了解到您实际上想要将分布列传递给两个参数的地方:

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM