简体   繁体   English

从 mable 中提取模型描述

[英]Extract Model Description from a mable

I have a mable object that is like so:我有一个像这样的 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]>

I just want the models descriptions so I can place them in a plot.我只想要模型描述,以便我可以将它们放在图中。 So I ran the following code:所以我运行了以下代码:

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)

This still gives me back a tibble where model_desc is still a list object.这仍然给了我一个小标题,其中 model_desc 仍然是一个列表对象。 I think this is because of how a mable is constructed and how its structure is supposed to be.我认为这是因为 mable 的构造方式以及它的结构应该如何。

** UPDATE ** I solved the problem by doing the following: ** 更新 ** 我通过执行以下操作解决了这个问题:

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

For anybody else who will encounter this going forward, I have pasted a solution that works for me with the latest versions of 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>

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

This ended up solving my issue:这最终解决了我的问题:

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

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

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