mitchelloharawild / fable.prophet

fable extension for the prophet forecasting procedure
https://pkg.mitchelloharawild.com/fable.prophet
55 stars 8 forks source link

Error: Problem with `mutate()` input `prophet`. x y should be a univariate time series #25

Closed edgBR closed 3 years ago

edgBR commented 4 years ago

Dear Mitchell,

I am trying to forecast using a fable.prophet model. My data looks as follows:

processed_data |   |   |   |   |   |   |   |   |   |   |   |   |   |  
-- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | --
#   A tsibble: 379 x 27 [7D] |   |   |   |   |   |   |   |   |   |   |   |   |  
#   Key:      machine, counter,   db_src [1] |   |   |   |   |   |   |   |   |   |  
machine counter snsr_dt    db_src snsr_val_clean index.num  year year.iso  half quarter month month.xts month.lbl   day    wday wday.xts wday.lbl  mday
<chr>               <dbl> <date>      <dbl>          <dbl>     <int> <int>    <int> <int>   <int> <int>     <int> <ord>     <int> <int>    <int> <ord>    <int> |   |  
1 xxxxxxxxxxxxx            71 2012-10-01      1        2000    1.35e9    2012     2012     2         4    10         9 October       1       2        1 Monday       1 |  
2 xxxxxxxxxxxxx            71 2012-10-08      1        2208    1.35e9    2012     2012     2         4    10         9 October       8       2        1 Monday       8 |  
3 xxxxxxxxxxxxx            71 2012-10-15      1        2376    1.35e9    2012     2012     2         4    10         9 October      15       2        1 Monday      15 |  
4 xxxxxxxxxxxxx            71 2012-10-22      1        2543    1.35e9    2012     2012     2         4    10         9 October      22       2        1 Monday      22 |  
5 xxxxxxxxxxxxx            71 2012-10-29      1        2711    1.35e9    2012     2012     2         4    10         9 October      29       2        1 Monday      29 |  
6 xxxxxxxxxxxxx            71 2012-11-05      1        2867    1.35e9    2012     2012     2         4    11        10 November      5       2        1 Monday       5 |  
7 xxxxxxxxxxxxx            71 2012-11-12      1        3035    1.35e9    2012     2012     2         4    11        10 November     12       2        1 Monday      12 |  
8 xxxxxxxxxxxxx            71 2012-11-19      1        3202    1.35e9    2012     2012     2         4    11        10 November     19       2        1 Monday      19 |  
9 xxxxxxxxxxxxx            71 2012-11-26      1        3354    1.35e9    2012     2012     2         4    11        10 November     26       2        1 Monday      26 |  
#   … with 369 more rows, and 9 more variables: qday <int>, yday   <int>, mweek <int>, week <int>, week.iso <int>, week2   <int>, week3 <int>, week4 <int>, mday7 <int>

The data has no NAs, and no duplicates. When fitting a model everything works perfectly:

> test_models
# A mable: 1 x 4
# Key:     machine, counter, db_src [1]
  machine counter db_src prophet_additive
  <chr>               <dbl>  <dbl>          <model>
1 xxxxxxxxxxxxx            71      1        <prophet>
> test_models %>% fabletools::augment()
# A tsibble: 379 x 8 [7D
# Key:       machine, counter, db_src, .model [1]
   machine               counter db_src .model           snsr_dt    snsr_val_clean .fitted .resid
   <chr>               <dbl>  <dbl> <chr>            <date>              <dbl>   <dbl>  <dbl>
 1 xxxxxxxxxxxxx            71      1 prophet_additive 2012-10-01           2000   1686.   314.
 2 xxxxxxxxxxxxx            71      1 prophet_additive 2012-10-08           2208   1766.   442.
 3 xxxxxxxxxxxxx            71      1 prophet_additive 2012-10-15           2376   1952.   424.
 4 xxxxxxxxxxxxx            71      1 prophet_additive 2012-10-22           2543   2054.   489.
 5 xxxxxxxxxxxxx            71      1 prophet_additive 2012-10-29           2711   2340.   371.

This is exactly the same issue that I had: https://github.com/mitchelloharawild/fable.prophet/issues/24

But now I can not even reproduce the example data


> library(tsibble)
> library(dplyr)
> #> 
> #> Attaching package: 'dplyr'
> #> The following objects are masked from 'package:stats':
> #> 
> #>     filter, lag
> #> The following objects are masked from 'package:base':
> #> 
> #>     intersect, setdiff, setequal, union
> aus_turnover <- tsibbledata::aus_retail %>% 
+     summarise(Turnover = sum(Turnover))
> library(fable.prophet)
> #> Loading required package: Rcpp
> #> Loading required package: fabletools
> fit <- aus_turnover %>% 
+     model(
+         prophet = prophet(Turnover ~ season("year", 4, type = "multiplicative"))
+     )
> 
> fit %>% forecast(h=1)
Error: Problem with `mutate()` input `prophet`.
x y should be a univariate time series
ℹ Input `prophet` is `(function (object, ...) ...`.

Last error an trace as follows:

rlang::last_error() <error/dplyr_error> 
Problem with `mutate()` input `prophet`. x y should be a univariate time series 
ℹ Input `prophet` is `(function (object, ...) ...`. 
Backtrace:   
9. fabletools::forecast(., h = 1)  
11. dplyr::mutate_at(...)  
13. dplyr:::mutate.data.frame(.tbl, !!!funs)  
14. dplyr:::mutate_cols(.data, ...) 
Run `rlang::last_trace()` to see the full context.
> rlang::last_trace()
<error/dplyr_error>
Problem with `mutate()` input `prophet`.
x y should be a univariate time series
ℹ Input `prophet` is `(function (object, ...) ...`.
Backtrace:
     █
  1. └─fit %>% forecast(h = 1)
  2.   ├─base::withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
  3.   └─base::eval(quote(`_fseq`(`_lhs`)), env, env)
  4.     └─base::eval(quote(`_fseq`(`_lhs`)), env, env)
  5.       └─`_fseq`(`_lhs`)
  6.         └─magrittr::freduce(value, `_function_list`)
  7.           ├─base::withVisible(function_list[[k]](value))
  8.           └─function_list[[k]](value)
  9.             ├─fabletools::forecast(., h = 1)
 10.             └─fabletools:::forecast.mdl_df(., h = 1)
 11.               └─dplyr::mutate_at(...)
 12.                 ├─dplyr::mutate(.tbl, !!!funs)
 13.                 └─dplyr:::mutate.data.frame(.tbl, !!!funs)
 14.                   └─dplyr:::mutate_cols(.data, ...)
<parent: error/simpleError>
y should be a univariate time series

And sessionInfo:

R version 4.0.0 (2020-04-24)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/atlas-base/atlas/libblas.so.3.0
LAPACK: /usr/lib/atlas-base/atlas/liblapack.so.3.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] feather_0.3.5         xts_0.12-0            zoo_1.8-8             DescTools_0.99.35     data.table_1.12.8     qs_0.23.2             optparse_1.6.6       
 [8] furrr_0.1.0           future_1.17.0         imputeTS_3.0          timetk_2.0.0          feasts_0.1.3          forecast_8.12         fable.prophet_0.1.0  
[15] Rcpp_1.0.4.6          fable_0.2.1.9000      fabletools_0.2.0.9000 tsibble_0.9.1         forcats_0.5.0         stringr_1.4.0         dplyr_1.0.0          
[22] purrr_0.3.4           readr_1.3.1           tidyr_1.1.0           tibble_3.0.1          ggplot2_3.3.2         tidyverse_1.3.0       aws.s3_0.3.21        
[29] drake_7.12.2          tictoc_1.0            ConfigParser_1.0.0    R6_2.4.1              ini_0.3.1             DBI_1.1.0             odbc_1.2.2           
[36] lubridate_1.7.9      

loaded via a namespace (and not attached):
 [1] colorspace_1.4-1      ellipsis_0.3.1        class_7.3-17          base64enc_0.1-3       fs_1.4.1              rstudioapi_0.11       listenv_0.8.0        
 [8] farver_2.0.3          getopt_1.20.3         bit64_0.9-7           mvtnorm_1.1-0         prodlim_2019.11.13    fansi_0.4.1           xml2_1.3.2           
[15] codetools_0.2-16      splines_4.0.0         jsonlite_1.7.0        packrat_0.5.0         broom_0.5.6           anytime_0.3.8         dbplyr_1.4.3         
[22] compiler_4.0.0        httr_1.4.1            backports_1.1.8       assertthat_0.2.1      Matrix_1.2-18         cli_2.0.2             prettyunits_1.1.1    
[29] tools_4.0.0           igraph_1.2.5          gtable_0.3.0          glue_1.4.1            cellranger_1.1.0      fracdiff_1.5-1        vctrs_0.3.1          
[36] urca_1.3-0            nlme_3.1-147          progressr_0.6.0       lmtest_0.9-37         timeDate_3043.102     gower_0.2.2           globals_0.12.5       
[43] rvest_0.3.5           tsibbledata_0.2.0     lifecycle_0.2.0       MASS_7.3-51.6         scales_1.1.1          ipred_0.9-9           aws.ec2metadata_0.2.0
[50] hms_0.5.3             parallel_4.0.0        expm_0.999-4          quantmod_0.4.17       curl_4.3              aws.signature_0.5.2   rpart_4.1-15         
[57] stringi_1.4.6         tseries_0.10-47       TTR_0.24.0            filelock_1.0.2        boot_1.3-25           lava_1.6.7            storr_1.2.1          
[64] rlang_0.4.6           pkgconfig_2.0.3       distributional_0.1.0  lattice_0.20-41       stinepack_1.4         recipes_0.1.13        bit_1.1-15.2         
[71] tidyselect_1.1.0      magrittr_1.5          generics_0.0.2        base64url_1.4         txtq_0.2.0            pillar_1.4.4          haven_2.2.0          
[78] withr_2.2.0           survival_3.1-12       nnet_7.3-14           future.apply_1.5.0    modelr_0.1.7          crayon_1.3.4          utf8_1.1.4           
[85] RApiSerialize_0.1.0   progress_1.2.2        grid_4.0.0            readxl_1.3.1          blob_1.2.1            reprex_0.3.0          digest_0.6.25        
[92] stringfish_0.12.1     munsell_0.5.0         quadprog_1.5-8  

Any hints?

davidtedfordholt commented 3 years ago

Could you update fabletools to 0.2.1 or 0.2.1.9000? I just ran the example with R 4.0.2, dplyr_1.0.2 and fable_0.2.1.9000 and it forecasted. Also worked with fabletools_0.2.1.9000.

edgBR commented 3 years ago

Working now with fable 0.3