tidyverts / fabletools

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

augment method for mdl_df error with aggregated keys: STRING_ELT() can only be applied to a 'character vector', not a 'NULL' #236

Closed tanner-ellison closed 4 years ago

tanner-ellison commented 4 years ago

augment.mdl_df is throwing the following error introduced with 0.2.0 (https://fabletools.tidyverts.org/news/index.html#fabletools-020) when augmenting a mable with multiple aggregrate keys:

tsibbledata::olympic_running %>% 
aggregate_key(Length * Sex, Time = mean(Time)) %>%
model(naive = NAIVE(Time)) %>% 
augment()

Error in vec_group_loc(x) : Error in vec_group_loc(x) : STRING_ELT() can only be applied to a 'character vector', not a 'NULL'

I have traced it down to the augment.mdl_df function definition, specifically this code as shown here: https://github.com/tidyverts/fabletools/blob/master/R/broom.R

x <- gather(x, ".model", ".fit", !!!syms(mable_vars(x)))

I see the following breaking change but not sure how to resolve this: " The row order of wide to long mable operations (such as accuracy()) has changed (due to shift to pivot_longer() from gather()). Model column name values are now nested within key values, rather than key values nested in model name values."

mitchelloharawild commented 4 years ago

Thanks, should work fine now. The issue was that tidyr::gather() does not support vctrs.

library(fable)
#> Warning: package 'fable' was built under R version 3.6.3
#> Loading required package: fabletools
tsibbledata::olympic_running %>% 
  aggregate_key(Length * Sex, Time = mean(Time)) %>%
  model(naive = NAIVE(Time)) %>% 
  augment()
#> # A tsibble: 605 x 7 [4Y]
#> # Key:       Length, Sex [24]
#>    Length Sex          .model  Year  Time .fitted .resid
#>    <int>  <chr>        <chr>  <int> <dbl>   <dbl>  <dbl>
#>  1 100    <aggregated> naive   1896  12      NA   NA    
#>  2 100    <aggregated> naive   1900  11      12   -1    
#>  3 100    <aggregated> naive   1904  11      11    0    
#>  4 100    <aggregated> naive   1908  10.8    11   -0.200
#>  5 100    <aggregated> naive   1912  10.8    10.8  0    
#>  6 100    <aggregated> naive   1916  NA      10.8 NA    
#>  7 100    <aggregated> naive   1920  10.8    10.8  0    
#>  8 100    <aggregated> naive   1924  10.6    10.8 -0.2  
#>  9 100    <aggregated> naive   1928  11.5    10.6  0.9  
#> 10 100    <aggregated> naive   1932  11.1    11.5 -0.400
#> # … with 595 more rows

Created on 2020-06-26 by the reprex package (v0.3.0)

tanner-ellison commented 4 years ago

Awesome! Thank you for the quick fix. Love this package btw. keep up the good work!