tidyverts / fabletools

General fable features useful for extension packages
http://fabletools.tidyverts.org/
89 stars 31 forks source link

Cannot use `select()` to drop irrelevant column from a `mable` #120

Closed AlanFeder closed 5 years ago

AlanFeder commented 5 years ago

Hi,

I am using fable to fit a few different models.

However, when I then try to use select() to drop the to_keep column (now that it is irrelevant, I get an error that states:

Error: A mable must contain at least one model. To remove all models, first convert to a tibble with `as_tibble()`.

I can drop the column by just setting it to NULL, but I prefer to use select().

If I want to turn it into a tibble, I will lose the ability to run forecast() on the object.


Here is a reprex, with a slightly different problem, but running into an identical issue:

library(tsibble)
library(fable, quietly = TRUE)
library(tsibbledata)
library(dplyr, warn.conflicts = FALSE)

g <- gafa_stock %>%
  model(mean = MEAN(Volume))
g
#> # A mable: 4 x 2
#> # Key:     Symbol [4]
#>   Symbol mean   
#>   <chr>  <model>
#> 1 AAPL   <MEAN> 
#> 2 AMZN   <MEAN> 
#> 3 FB     <MEAN> 
#> 4 GOOG   <MEAN>

g <- gafa_stock %>%
  model(mean = MEAN(Volume)) %>%
  mutate(to_keep = (Symbol != "FB"))
g
#> # A mable: 4 x 3
#> # Key:     Symbol [4]
#>   Symbol mean    to_keep
#>   <chr>  <model> <lgl>  
#> 1 AAPL   <MEAN>  TRUE   
#> 2 AMZN   <MEAN>  TRUE   
#> 3 FB     <MEAN>  FALSE  
#> 4 GOOG   <MEAN>  TRUE

g <- gafa_stock %>%
  model(mean = MEAN(Volume)) %>%
  mutate(to_keep = (Symbol != "FB")) %>%
  filter(to_keep) 
g
#> # A mable: 3 x 3
#> # Key:     Symbol [3]
#>   Symbol mean    to_keep
#>   <chr>  <model> <lgl>  
#> 1 AAPL   <MEAN>  TRUE   
#> 2 AMZN   <MEAN>  TRUE   
#> 3 GOOG   <MEAN>  TRUE

g <- gafa_stock %>%
  model(mean = MEAN(Volume)) %>%
  mutate(to_keep = (Symbol != "FB")) %>%
  filter(to_keep) %>%
  select(-to_keep)
#> A mable must contain at least one model. To remove all models, first
#> convert to a tibble with `as_tibble()`.

g
#> # A mable: 3 x 3
#> # Key:     Symbol [3]
#>   Symbol mean    to_keep
#>   <chr>  <model> <lgl>  
#> 1 AAPL   <MEAN>  TRUE   
#> 2 AMZN   <MEAN>  TRUE   
#> 3 GOOG   <MEAN>  TRUE
g$to_keep <- NULL
g
#> # A mable: 3 x 2
#> # Key:     Symbol [3]
#>   Symbol mean   
#>   <chr>  <model>
#> 1 AAPL   <MEAN> 
#> 2 AMZN   <MEAN> 
#> 3 GOOG   <MEAN>

Created on 2019-09-16 by the reprex package (v0.3.0)

mitchelloharawild commented 5 years ago

Thanks for the clear bug report. Should be fixed now.

library(tsibble)
library(fable, quietly = TRUE)
library(tsibbledata)
library(dplyr, warn.conflicts = FALSE)

gafa_stock %>%
  model(mean = MEAN(Volume)) %>%
  mutate(to_keep = (Symbol != "FB")) %>%
  filter(to_keep) %>%
  select(-to_keep)
#> # A mable: 3 x 2
#> # Key:     Symbol [3]
#>   Symbol mean   
#>   <chr>  <model>
#> 1 AAPL   <MEAN> 
#> 2 AMZN   <MEAN> 
#> 3 GOOG   <MEAN>

Created on 2019-09-17 by the reprex package (v0.3.0)