r语言 - 无法将同胞转换为寓言



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

我想用强迫一个寓言对象

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包(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参数应该是一个字符向量:

响应
响应变量的字符向量。

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

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.

相关内容

  • 没有找到相关文章

最新更新