mitchelloharawild / fable.prophet

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

Unable to forecast using fable.prophet #24

Closed edgBR closed 4 years ago

edgBR commented 4 years ago

Dear colleagues,

I am trying to forecast using fable prophet as follows:

> models %>% forecast(h=1)
Error: Problem with `mutate()` input `prophet`.
x y should be a univariate time series
ℹ Input `prophet` is `(function (object, ...) ...`.
Run `rlang::last_error()` to see where the error occurred.

Last error:


> 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.

Last trace:

> 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. └─models %>% 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

Model taining:

  ts_models <- input_data %>% model(prophet_multiplicative = fable.prophet::prophet(snsr_val_clean ~ season("week", 2, type = "multiplicative") + 
                                                                             season("month", 2, type = "multiplicative")))

Model details:


> models %>% filter(a=='XXXXXXXXXX') %>% components()
# A dable:               4,512 x 12 [7D]
# Key:                   a,b,c, .model [12]
# Prophet Decomposition: snsr_val_clean = trend * (1 + multiplicative_terms) + additive_terms + .resid
   a b c  .model  snsr_dt    snsr_val_clean additive_terms multiplicative_terms trend  week   month .resid
   <chr>               <dbl>  <dbl> <chr>   <date>              <dbl>          <dbl>                <dbl> <dbl> <dbl>   <dbl>  <dbl>
 1 XXXXXXXXXXX            70      1 prophet 2012-10-01            226           13.3                    0  227.  12.9  0.428  -14.6 
 2 XXXXXXXXXXX           70      1 prophet 2012-10-08            235           12.9                    0  229.  12.9 -0.0111  -6.58
 3 XXXXXXXXXXX           70      1 prophet 2012-10-15            235           13.1                    0  230.  12.9  0.198   -8.25
 4 XXXXXXXXXXX           70      1 prophet 2012-10-22            235           12.3                    0  232.  12.9 -0.603   -8.91
 5 XXXXXXXXXXX           70      1 prophet 2012-10-29            235           13.2                    0  233.  12.9  0.308  -11.3 
 6 XXXXXXXXXXX           70      1 prophet 2012-11-05            236           13.0                    0  235.  12.9  0.0714 -11.5 
 7 XXXXXXXXXXX           70      1 prophet 2012-11-12            236           13.1                    0  236.  12.9  0.233  -13.1 
 8 XXXXXXXXXXX           70      1 prophet 2012-11-19            236           12.5                    0  237.  12.9 -0.416  -13.9 
 9 XXXXXXXXXXX           70      1 prophet 2012-11-26            239           12.8                    0  239.  12.9 -0.0786 -12.7 
10 XXXXXXXXXXX            70      1 prophet 2012-12-03            239           13.2                    0  240.  12.9  0.299  -14.6 
# … w

Any hints?

mitchelloharawild commented 4 years ago

I cannot reproduce this issue with a sample dataset:


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)
#> # A fable: 1 x 4 [1M]
#> # Key:     .model [1]
#>   .model     Month     Turnover  .mean
#>   <chr>      <mth>       <dist>  <dbl>
#> 1 prophet 2019 Jan sample[1000] 53572.

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

edgBR commented 4 years ago

Hi @mitchelloharawild

Your example work perfectly, I have noticed that I have some null model within my models$prophet. Could it be this the issue?

BR /E  

edgBR commented 4 years ago

Hi again,

Unfortunately I can not provide a reproducible example as my data is confidential. I will try to make non restricted version and I will attach it to the reprex.

BR /E

edgBR commented 4 years ago

Hi @mitchelloharawild,

After a reset of my tidyverts environment everything start to work again.

BR /E