tsibble icon indicating copy to clipboard operation
tsibble copied to clipboard

FR : a clean generic aggregation method

Open arnaud-feldmann opened this issue 3 years ago • 0 comments

Hi, despite the adding of .start and .end for fill_gaps, there is still no clean way to aggregate.

I'd like something like that to work on compatible tsibbles :

agreg_tsibble <- function(x,f=sum,unit) {
  
  f <- match.fun(f)
  ind <- index(x)
  key <- key(x)
  
  dates_lims <- pull(x,as.character(ind)) %>%
  {c(min(.,na.rm = TRUE),max(.,na.rm=TRUE))} %>%
  as_date %>%
  {c(floor_date(.[1L],unit),ceiling_date(.[2L],unit,change_on_boundary = TRUE))}
  
  x %>% 
  fill_gaps(.start = dates_lims[1L],
            .end = dates_lims[2L]) %>% 
  index_by(quarter = ~ yearquarter(.)) %>%
  group_by_key() %>%
  summarize(across(! (!!ind),f))

}

There is some things that forbids that :

  • incompatibility between dates formats in .start and .end (cf my other issue https://github.com/tidyverts/tsibble/issues/267 )
  • ceiling_date isn't the right max limit, it should be something like ceiling_date(.[2L],unit,change_on_boundary = TRUE)-"hf interval" but I don't know how to substract tsibble objects of class interval

It would be a very great feature to add, because right now it's way more complicated to aggregate than in stats::ts.

arnaud-feldmann avatar Nov 02 '21 17:11 arnaud-feldmann