rfable-rtidyverts

Extract Model Description from a mable


I have a mable object that is like so:

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. I think this is because of how a mable is constructed and how its structure is supposed to be.

** 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")

Solution

  • 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)