Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aspina7 opened this issue Feb 6, 2022 · 4 comments
Open

Comments

@aspina7
Copy link

aspina7 commented Feb 6, 2022

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::cbind somewhere. (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
Copy link
Contributor

TimTaylor commented Feb 7, 2022

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
Copy link
Contributor

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
Copy link
Author

aspina7 commented Apr 25, 2022

@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
Copy link
Contributor

TimTaylor commented Apr 25, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants