0

我有一个像这样的 mable 对象:

models
# A mable: 1 x 3
  ets           arima                     nnetar             
  <model>       <model>                   <model>            
1 <ETS(M,Ad,M)> <ARIMA(2,1,2)(0,0,2)[12]> <NNAR(14,1,10)[12]>

我只想要模型描述,这样我就可以将它们放在一个情节中。所以我运行了以下代码:

model_desc <- models %>% 
  gather() %>% 
  select(key, value) %>% 
  set_names("model","model_desc") %>%
  mutate(model_desc_char = model_desc %>% as.character())
  as_tibble() %>%
  select(model, model_desc)

这仍然给了我一个模型,其中 model_desc 仍然是一个列表对象。我认为这是因为 mable 是如何构造的,以及它的结构应该如何。

** 更新 ** 我通过执行以下操作解决了这个问题:

model_desc <- models %>% 
  as_tibble() %>%
  gather() %>%
  mutate(model_desc = print(value)) %>%
  select(key, model_desc) %>%
  set_names("model", "model_desc")
4

2 回答 2

1

对于以后会遇到这种情况的其他人,我已经粘贴了一个适用于我最新版本的 fable/fabletools 的解决方案。

library(fable)
#> Loading required package: fabletools
library(tsibble)
library(tsibbledata)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:tsibble':
#> 
#>     interval
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)

aus_retail %>%
  filter(
    State %in% c("New South Wales", "Victoria"),
    Industry == "Department stores"
  ) %>% 
  model(
    ets = ETS(box_cox(Turnover, 0.3)),
    arima = ARIMA(log(Turnover)),
    snaive = SNAIVE(Turnover)
  ) %>%
  pivot_longer(cols = -c(State, Industry),
               names_to = "model_type",
               values_to = "model_specifics_mdl") %>%
  mutate(model_specifics = format(model_specifics_mdl)) %>%
  select(-model_specifics_mdl)
#> # A tibble: 6 x 4
#>   State           Industry          model_type model_specifics          
#>   <chr>           <chr>             <chr>      <chr>                    
#> 1 New South Wales Department stores ets        <ETS(A,Ad,A)>            
#> 2 New South Wales Department stores arima      <ARIMA(2,1,1)(2,1,1)[12]>
#> 3 New South Wales Department stores snaive     <SNAIVE>                 
#> 4 Victoria        Department stores ets        <ETS(A,A,A)>             
#> 5 Victoria        Department stores arima      <ARIMA(2,1,1)(1,1,2)[12]>
#> 6 Victoria        Department stores snaive     <SNAIVE>

reprex 包(v0.3.0)于 2020 年 9 月 7 日创建

于 2020-09-07T15:54:24.647 回答
0

这最终解决了我的问题:

model_desc <- models %>% 
  as_tibble() %>%
  gather() %>%
  mutate(model_desc = print(value)) %>%
  select(key, model_desc) %>%
  set_names("model", "model_desc")
于 2020-01-13T19:11:07.217 回答