从 mable 中提取模型描述

Extract Model Description from a 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 仍然是一个列表对象。我认为这是因为大理石的构造方式及其结构应该如何。

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

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

这最终解决了我的问题:

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

对于以后会遇到这种情况的任何其他人,我已经粘贴了一个适用于我的最新版本 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 package (v0.3.0)

于 2020-09-07 创建