reconverse / trending

Coherent interface for fitting trends
http://www.reconverse.org/trending/
Other
8 stars 1 forks source link

trending::predict() issue with {tsibble} v.1.1.1 and dplyr::bind_cols #19

Open aspina7 opened 2 years ago

aspina7 commented 2 years ago

hey sorry for delayed post - but as discussed over on the epirhandbook issue with the new release of tsibble forces the use of dplyr::bind_cols, which clashes with trending::predict() which I am guessing has a base::cbindsomewhere. (see reprex below)

Not sure if there is an easy catch you could include that depending on the class you use one or the other? Alternatively there is the tsbox package for converting between timeseries classes? Hope this is clear and thanks!

  library(rio)
  library(tsibble)
#> Warning: Paket 'tsibble' wurde unter R Version 4.1.2 erstellt
#> 
#> Attache Paket: 'tsibble'
#> Die folgenden Objekte sind maskiert von 'package:base':
#> 
#>     intersect, setdiff, union
  library(imputeTS)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
  library(dplyr)
#> 
#> Attache Paket: 'dplyr'
#> Die folgenden Objekte sind maskiert von 'package:stats':
#> 
#>     filter, lag
#> Die folgenden Objekte sind maskiert von 'package:base':
#> 
#>     intersect, setdiff, setequal, union
  library(stringr)
  library(forecast)
  library(trending)

  # import the campylobacter counts (lifted from {surveillance})
  counts <- rio::import("https://github.com/epirhandbook/Epi_R_handbook/raw/master/data/time_series/campylobacter_germany.xlsx")

  ## ensure the date column is in the appropriate format
  counts$date <- as.Date(counts$date)

  ## create a calendar week variable 
  ## fitting ISO definitons of weeks starting on a monday
  counts <- counts %>% 
    mutate(epiweek = yearweek(date, week_start = 1)) 

  ## define time series object 
  counts <- tsibble(counts, index = epiweek)

  ## create a variable with missings instead of weeks with reporting issues
  counts <- counts %>% 
    mutate(case_miss = if_else(
      ## if epiweek contains 52, 53, 1 or 2
      str_detect(epiweek, "W51|W52|W53|W01|W02"), 
      ## then set to missing 
      NA_real_, 
      ## otherwise keep the value in case
      case
    ))

  ## alternatively interpolate missings by linear trend 
  ## between two nearest adjacent points
  counts <- counts %>% 
    mutate(case_int = imputeTS::na_interpolation(case_miss)
    )

  ## add in fourier terms using the epiweek and case_int variabless
  counts$fourier <- select(counts, epiweek, case_int) %>% 
    fourier(K = 1)

  ## define the model you want to fit (negative binomial) 
  model <- glm_nb_model(
    ## set number of cases as outcome of interest
    case_int ~
      ## use epiweek to account for the trend
      epiweek +
      ## use the fourier terms to account for seasonality
      fourier)

  ## fit your model using the counts dataset
  fitted_model <- trending::fit(model, counts)

  ## calculate confidence intervals and prediction intervals 
  observed <- predict(fitted_model, simulate_pi = FALSE)
#> Error: Not supported for tsibble.
#> i Please use `dplyr::bind_cols()` instead.

Created on 2022-02-06 by the reprex package (v2.0.0)

TimTaylor commented 2 years ago

Hi @aspina7. Just a heads up that this actually already works on the current master branch. However, I can't give an anticipated release for this as it's part of a refactoring effort that also necessitates changes to {i2extras} and {trendeval} and I'm currently time poor.

@thibautjombart I'm leaving this issue open as, although this already works on the current master, we may still need to call as.data.frame on other data.frame but not data.frame inputs such as {data.table}.

thibautjombart commented 2 years ago

Thanks @TimTaylor for your reply. I think we should not rush the re-structuring of i2extras / trendeval, and should hopefully have some time dedicated on this as part of the epiverse initiative.

aspina7 commented 2 years ago

@TimTaylor sorry - this hit me during exam period and then I totally forgot about it! Thanks for the reply and good to know master should work...

Any thoughts on when this might hit CRAN?

TimTaylor commented 2 years ago

Hi @aspina7

Any thoughts on when this might hit CRAN?

Not at the moment. But will give you a poke when I've a better idea of timings.