Setup

Load the relevant libraries.

# rm(list = ls())
# .rs.restartR()


# data manipulation
library("plyr")
library("tidyverse")
library("magrittr")
library("data.table")
library("lubridate")
library("sqldf")


# time series specific packages
library("timetk")
library("zoo")
library("tibbletime")


# modeling
library("fpp2")
library("prophet")
library("caret")
library("randomForest")
library("xgboost")
library("h2o")
library("keras")
# use_session_with_seed(123456789) # setting the seed to obtain reproducible results
# see https://keras.rstudio.com/articles/faq.html#how-can-i-obtain-reproducible-results-using-keras-during-development and https://cran.r-project.org/web/packages/keras/vignettes/faq.html
# can also re-enable gpu and parallel processing by using:  use_session_with_seed(42, disable_gpu = FALSE, disable_parallel_cpu = FALSE)



# other
library("geosphere")          # specific for distance calculations from lat-lon pairs
library("naniar")             # inspecting missing data
library("rlang")              # building functions
library("recipes")            # used in Keras modeling to design matrices
library("rsample")            # rolling samples for validation stats
library("tfruns")             # used in Keras modeling for trainin runs
library("stringr")            # string manipulation
library("ggplot2")            # viz
library("sweep")              # more easily pull out model statistics
library("yardstick")          # easily calculate accuracy stats
library("doParallel")         # parallel processing

Session Info.

sessionInfo()
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS High Sierra 10.13.6
## 
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] parallel  stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] doParallel_1.0.14   iterators_1.0.10    foreach_1.4.4      
##  [4] yardstick_0.0.2     sweep_0.2.1.1       tfruns_1.4         
##  [7] rsample_0.0.3       recipes_0.1.4       rlang_0.3.0.1      
## [10] naniar_0.4.1        geosphere_1.5-7     keras_2.2.4        
## [13] h2o_3.20.0.8        xgboost_0.71.2      randomForest_4.6-14
## [16] caret_6.0-81        lattice_0.20-38     prophet_0.3.0.1    
## [19] Rcpp_1.0.0          fpp2_2.3            expsmooth_2.3      
## [22] fma_2.3             forecast_8.4        tibbletime_0.1.1   
## [25] zoo_1.8-4           timetk_0.1.1.1      sqldf_0.4-11       
## [28] RSQLite_2.1.1       gsubfn_0.7          proto_1.0.0        
## [31] lubridate_1.7.4     data.table_1.11.8   magrittr_1.5       
## [34] forcats_0.3.0       stringr_1.3.1       dplyr_0.7.8        
## [37] purrr_0.2.5         readr_1.2.1         tidyr_0.8.2        
## [40] tibble_1.4.2        ggplot2_3.1.0       tidyverse_1.2.1    
## [43] plyr_1.8.4         
## 
## loaded via a namespace (and not attached):
##  [1] colorspace_1.3-2   class_7.3-14       visdat_0.5.1      
##  [4] rprojroot_1.3-2    base64enc_0.1-3    rstudioapi_0.8    
##  [7] rstan_2.18.2       bit64_0.9-7        prodlim_2018.04.18
## [10] xml2_1.2.0         codetools_0.2-15   splines_3.5.1     
## [13] knitr_1.20         zeallot_0.1.0      jsonlite_1.5      
## [16] pROC_1.13.0        broom_0.5.0        compiler_3.5.1    
## [19] httr_1.3.1         backports_1.1.2    assertthat_0.2.0  
## [22] Matrix_1.2-15      lazyeval_0.2.1     cli_1.0.1         
## [25] htmltools_0.3.6    prettyunits_1.0.2  tools_3.5.1       
## [28] bindrcpp_0.2.2     gtable_0.2.0       glue_1.3.0        
## [31] reshape2_1.4.3     cellranger_1.1.0   fracdiff_1.4-2    
## [34] urca_1.3-0         debugme_1.1.0      nlme_3.1-137      
## [37] lmtest_0.9-36      timeDate_3043.102  gower_0.1.2       
## [40] ps_1.2.1           rvest_0.3.2        MASS_7.3-51.1     
## [43] scales_1.0.0       ipred_0.9-8        hms_0.4.2         
## [46] inline_0.3.15      yaml_2.2.0         quantmod_0.4-13   
## [49] curl_3.2           reticulate_1.10    memoise_1.1.0     
## [52] gridExtra_2.3      loo_2.0.0          StanHeaders_2.18.0
## [55] uroot_2.0-9        rpart_4.1-13       stringi_1.2.4     
## [58] tensorflow_1.10    tseries_0.10-46    TTR_0.23-4        
## [61] pkgbuild_1.0.2     lava_1.6.4         chron_2.3-53      
## [64] bitops_1.0-6       pkgconfig_2.0.2    matrixStats_0.54.0
## [67] evaluate_0.12      bindr_0.1.1        bit_1.1-14        
## [70] processx_3.2.0     tidyselect_0.2.5   R6_2.3.0          
## [73] generics_0.0.2     DBI_1.0.0          whisker_0.3-2     
## [76] pillar_1.3.0       haven_2.0.0        withr_2.1.2       
## [79] xts_0.11-2         sp_1.3-1           RCurl_1.95-4.11   
## [82] survival_2.43-3    nnet_7.3-12        modelr_0.1.2      
## [85] crayon_1.3.4       rmarkdown_1.10     grid_3.5.1        
## [88] readxl_1.1.0       blob_1.1.1         callr_3.0.0       
## [91] ModelMetrics_1.2.2 digest_0.6.18      stats4_3.5.1      
## [94] munsell_0.5.0      tcltk_3.5.1        quadprog_1.5-5

Setup the root directory.

Setting wd as the working directory.

wd <- getwd()

wd
## [1] "/Users/mdturse/Desktop/Analytics/Chicago_El_Divvy"

Modeling

NOTE: period_train, period_test, skip_span, train_data, DV_train_data, and add_trn_val_test, time.Rf.corr_no, and time.Xgbtree.corr_yes are the outputs produced in Step 02 and Step 03

period_train <-
  readRDS(paste0(wd,
                 "/Data/Interim/",
                 "period_train.Rds"
                 )
          )

period_test <-
  readRDS(paste0(wd,
                 "/Data/Interim/",
                 "period_test.Rds"
                 )
          )

skip_span <-
  readRDS(paste0(wd,
                 "/Data/Interim/",
                 "skip_span.Rds"
                 )
          )

train_data <-
  readRDS(paste0(wd,
                 "/Models/",
                 "train_data.Rds"
                 )
          )

DV_train_data <-
  readRDS(paste0(wd,
                 "/Models/",
                 "DV_train_data.Rds"
                 )
          )

add_trn_val_test <-
  readRDS(paste0(wd,
                 "/Data/Interim/",
                 "add_trn_val_test.Rds"
                 )
          )

time.Rf.corr_no <-
  readRDS(paste0(wd,
                 "/Models/",
                 "time.Rf.corr_no.Rds"
                 )
          )

time.Xgbtree.corr_yes <-
  readRDS(paste0(wd,
                 "/Models/",
                 "time.Xgbtree.corr_yes.Rds"
                 )
          )

Setup for Models Not in caret

This is based on the example shown here, and is needed for the time-series-ish models, which are not currently part of the caret modeling process.

Create the rolling-origin resamples to be used for measuring forecast accuracy. Rolling samples will use 2 years of data do predict the next 14 days.

roll_rs <-
  # DV_train_data %>% 
  train_data %>% 
  map(~ rolling_origin(.x,
                       initial = period_train,
                       assess = period_test,
                       cumulative = FALSE,
                       skip = skip_span
                       )
      )
## Warning: `list_len()` is soft-deprecated as of rlang 0.2.0.
## Please use `new_list()` instead
## This warning is displayed once per session.
message("DV_train_data")
## DV_train_data
DV_train_data %>% 
  map(~ nrow(.x)
      )
## $`40600`
## [1] 905
## 
## $`41140`
## [1] 905
## 
## $`40120`
## [1] 905
## 
## $`40910`
## [1] 875
## 
## $`40380`
## [1] 905
## 
## $`41660`
## [1] 905
message("roll_rs")
## roll_rs
roll_rs %>% 
  map(~ nrow(.x)
      )
## $`40600`
## [1] 13
## 
## $`41140`
## [1] 13
## 
## $`40120`
## [1] 13
## 
## $`40910`
## [1] 10
## 
## $`40380`
## [1] 13
## 
## $`41660`
## [1] 13
names(roll_rs$`40600`)
## [1] "splits" "id"
roll_rs %>% 
  map(~ length(.x$splits)
      )
## $`40600`
## [1] 13
## 
## $`41140`
## [1] 13
## 
## $`40120`
## [1] 13
## 
## $`40910`
## [1] 10
## 
## $`40380`
## [1] 13
## 
## $`41660`
## [1] 13
# roll_rs$`40600`$splits[[1]]
# train_data %>% 
#   map(~ summarise(.x,
#                   min_d = min(el_date),
#                   max_d = max(el_date),
#                   days = n()
#                   )
#       )

# rm(period_train, period_test, skip_span)

For plotting, let’s index each split by the first day of the assessment set.

get_date <-
  function(x)
    min(assessment(x)$el_date
        )


roll_rs <-
  pmap(.l = list(a = roll_rs),
       .f = function(a) {
         data = a
         
         splits = data$splits %>% 
           map(get_date)
         
         data$start_date = do.call("c", splits)
         
         return(data)
         }
       )

names(roll_rs$`40600`)
## [1] "splits"     "id"         "start_date"
length(roll_rs$`40600`$start_date)
## [1] 13
head(roll_rs$`40600`$start_date, 20)
##  [1] "2015-02-27" "2015-03-08" "2015-03-17" "2015-03-26" "2015-04-04"
##  [6] "2015-04-13" "2015-04-22" "2015-05-01" "2015-05-10" "2015-05-19"
## [11] "2015-05-28" "2015-06-06" "2015-06-15"
rm(get_date)

Model Setup (forecast::auto.arima and prophet::prophet)

Here, we use forecast::auto.arima to produce create an arima model. We also try prophet::prophet to create a model based on trend, seasonality, and holidays, and that is a bit like a general additive model. More info can be found here and here.

First, I create the function for the basic ARIMA model.

fit_model_arima <-
  function(x, ...) {
    data = x %>% 
      analysis() %>% 
      # Since the first day changes over resamples, adjust it based on the first date value in the data frame
      tk_ts(select = el_rides,
            start = .$el_date[[1]] %>% lubridate::year(),
            freq = 7,
            silent = TRUE
            )
    
    fit = auto.arima(data, ...)
    
    return(fit)
    }

Next I create the function to run an ARIMA model with external regressors (external regressors include fourier transformations, and other regressors identified by using the random forest and xgboost models used above).

fit_model_arima_xreg <-
  function(x, ...) {
    data = 
      x %>% 
      analysis() %>% 
      mutate(holiday_binary = if_else(holiday == FALSE,
                                      0,
                                      1
                                      )
             )
    
    # weekly frequency
    ts_7 =
      data %>% 
      tk_ts(select = el_rides,
            start = .$el_date[[1]] %>% lubridate::year(),
            freq = 7,
            silent = TRUE
            )
    
    # yearly frequency
    ts_365 =
      data %>% 
      tk_ts(select = el_rides,
            start = .$el_date[[1]] %>% lubridate::year(),
            # freq = 365.25,
            freq = 365,
            silent = TRUE
            )
    
    # use a fourier transformation to capture daily seasonality and choose K programatically
    bestfit = list(aicc = Inf)
    for(K in seq(7)
        ) {
      n = nrow(assessment(x)
               )
      
      ts_365_fourier = fourier(ts_365,
                               K = K
                               )
      
      ts_365_fourier_future = fourier(ts_365,
                                      K = K,
                                      h = n
                                      )
      
      fit = auto.arima(ts_7,
                       xreg = cbind(ts_365_fourier,
                                    # additional variables identified as "important" with Random Forest and XGBTree models
                                    data$holiday_binary,
                                    data$year,
                                    data$half,
                                    data$quarter,
                                    data$month,
                                    data$mweek,
                                    data$wday.lbl,
                                    data$el_rides_l07,
                                    data$el_rides_l14,
                                    data$el_rides_l21,
                                    data$el_rides_l28,
                                    data$el_rides_ma07,
                                    data$el_rides_ma14,
                                    data$el_rides_ma21,
                                    data$el_rides_ma28
                                    )#,
                       # seasonal = FALSE
                       )
      
      if(fit[["aicc"]] < bestfit[["aicc"]]) {
        bestfit = fit
        bestK = K
        bestts_365_fourier_future = ts_365_fourier_future
      }
      
      return(list(best_fit = bestfit,
                  best_k = bestK,
                  best_ts_365_fourier_future = bestts_365_fourier_future
                  )
             )
    }
    
    return()
    }

Now I create the function to run the basic prophet model (based just on el_rides.

# prophet
fit_model_prophet <-
  function(x, ...) {
    x %>% 
      analysis() %>% 
      select(el_date,
             el_rides
             ) %>% 
      rename(ds = el_date,
             y = el_rides
             ) %>% 
      prophet(...)
  }

Run Models

Arima-Based Model

Need to review this: http://www.business-science.io/code-tools/2018/04/08/introducing-anomalize.html for anomaly detection.

Can also use any other modeling methods (e.g., prophet), and then use the outlier detection method twitter used (Generalized ESD) on the remainder. See: https://www.rdocumentation.org/packages/EnvStats/versions/2.3.0/topics/rosnerTest for the algorithm used outside of AnomalyDetection::AnomalyDetectionTs.

Here I run the basic forecast::auto.arima model.

# user   system  elapsed 
# 1001.467   21.414 1036.904
# ~ 17 min
message("arima")
## arima
start <- proc.time()
models <-
  pmap(.l = list(a = roll_rs),
       .f = function(a) {
         
         splits_a = a$splits %>%
           map(fit_model_arima)
         
         a$arima = splits_a
         
         return(a)
         }
       )

time.arima <- proc.time() - start
time.arima
##     user   system  elapsed 
##  995.351   14.265 1144.603

Here I run the forecast::auto.arima model with external regressors.

# user  system elapsed 
# 626.467  26.937 673.471
# ~ 11 min
message("arima_xreg")
## arima_xreg
start <- proc.time()
models <-
  pmap(.l = list(a = models),
       .f = function(a) {
         
         splits_a_xreg = a$splits %>% 
           map(fit_model_arima_xreg)
         
         a$arima_xreg = splits_a_xreg
         
         return(a)
         }
       )

time.arima_xreg <- proc.time() - start
time.arima_xreg
##    user  system elapsed 
## 653.077  25.525 719.484

Here I run the basic prophet::prophet model

# user  system elapsed 
#  12.007   1.344  13.901
message("prophet")
## prophet
start <- proc.time()
models <-
  pmap(.l = list(a = models),
       .f = function(a) {
         
         splits_p = a$splits %>%
           map(~ fit_model_prophet(.x,
                                   
                                   )
               )
         
         a$prophet = splits_p
         
         return(a)
         }
       )
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -13.7404
## Optimization terminated normally: 
##   Convergence detected: absolute parameter change was below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -19.2862
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -17.8852
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -19.7988
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -15.9496
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -22.0054
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -17.9319
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -18.0652
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -18.196
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -22.3152
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -21.3384
## Optimization terminated normally: 
##   Convergence detected: absolute parameter change was below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -23.0606
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -21.4911
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -13.6547
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -10.3925
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -21.3698
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -8.87054
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -26.3523
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -19.4382
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -10.692
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -24.9817
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -21.7715
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -30.5333
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -12.7263
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -30.5257
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -16.0536
## Optimization terminated normally: 
##   Convergence detected: absolute parameter change was below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -21.8228
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -22.2602
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -25.7048
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -27.7389
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -22.301
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -27.7156
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -22.5187
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -33.9924
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -24.9654
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -28.9799
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -28.0826
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -31.9632
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -26.5334
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -54.4437
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -44.2642
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -37.6462
## Optimization terminated normally: 
##   Convergence detected: absolute parameter change was below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -40.0443
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -72.6364
## Optimization terminated normally: 
##   Convergence detected: absolute parameter change was below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -50.0799
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -9.60659
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -14.2861
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -10.2214
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -18.7365
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -42.3573
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -40.6643
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -43.8898
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -41.157
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -36.4829
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -43.6537
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -37.6811
## Optimization terminated normally: 
##   Convergence detected: absolute parameter change was below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -45.8237
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -40.2807
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -47.7146
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -39.0395
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -46.1692
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -39.6705
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -15.2607
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -21.472
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -17.1367
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -22.7222
## Optimization terminated normally: 
##   Convergence detected: absolute parameter change was below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -14.8147
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -21.3628
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -15.6542
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -17.5156
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -14.6518
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -17.7027
## Optimization terminated normally: 
##   Convergence detected: absolute parameter change was below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -22.0569
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -20.7763
## Optimization terminated normally: 
##   Convergence detected: absolute parameter change was below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -20.9008
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
time.prophet <- proc.time() - start
time.prophet
##    user  system elapsed 
##  10.408   0.777  11.447

Here I run the prophet::prophet model that includes regressors for holidays.

# create the holiday_dates
holiday_dates <-
  add_trn_val_test %>% 
  map(~ select(.x,
               holiday_name,
               el_date
               ) %>% 
        filter(holiday_name != "--Not_Holiday--") %>% 
        rename(holiday = holiday_name,
               ds = el_date
               )
      )

holiday_dates <- bind_rows(holiday_dates) %>% 
  select(holiday,
         ds
         ) %>% 
  distinct() %>% 
  arrange(ds)


# user  system elapsed 
#  35.327   3.401  47.577
message("prophet_hol")
## prophet_hol
start <- proc.time()
models <-
  pmap(.l = list(a = models),
       .f = function(a) {
         
         splits_p_hol = a$splits %>%
           map(~ fit_model_prophet(.x,
                                   holidays = holiday_dates
                                   )
               )
           
         a$prophet_hol <- splits_p_hol
         
         return(a)
         }
       )
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -13.7404
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -19.2862
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -17.8852
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -19.7988
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -15.9496
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -22.0054
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -17.9319
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -18.0652
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -18.196
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -22.3152
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -21.3384
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -23.0606
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -21.4911
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -13.6547
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -10.3925
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -21.3698
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -8.87054
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -26.3523
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -19.4382
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -10.692
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -24.9817
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -21.7715
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -30.5333
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -12.7263
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -30.5257
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -16.0536
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -21.8228
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -22.2602
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -25.7048
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -27.7389
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -22.301
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -27.7156
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -22.5187
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -33.9924
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -24.9654
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -28.9799
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -28.0826
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -31.9632
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -26.5334
## Optimization terminated normally: 
##   Convergence detected: absolute parameter change was below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -54.4437
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -44.2642
## Optimization terminated normally: 
##   Convergence detected: absolute parameter change was below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -37.6462
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -40.0443
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -72.6364
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -50.0799
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -9.60659
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -14.2861
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -10.2214
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -18.7365
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -42.3573
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -40.6643
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -43.8898
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -41.157
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -36.4829
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -43.6537
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -37.6811
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -45.8237
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -40.2807
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -47.7146
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -39.0395
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -46.1692
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -39.6705
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -15.2607
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -21.472
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -17.1367
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -22.7222
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -14.8147
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -21.3628
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -15.6542
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -17.5156
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -14.6518
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -17.7027
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -22.0569
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -20.7763
## Optimization terminated normally: 
##   Convergence detected: absolute parameter change was below tolerance
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -20.9008
## Optimization terminated normally: 
##   Convergence detected: relative gradient magnitude is below tolerance
time.prophet_hol <- proc.time() - start
time.prophet_hol
##    user  system elapsed 
##  23.725   1.602  25.434
# rm(holiday_dates)
run_times <- list(arima = as.list(time.arima),
                  arima_xreg = as.list(time.arima_xreg),
                  prophet = as.list(time.prophet),
                  prophet_hol = as.list(time.prophet_hol)
                  )

run_times
## $arima
## $arima$user.self
## [1] 995.351
## 
## $arima$sys.self
## [1] 14.265
## 
## $arima$elapsed
## [1] 1144.603
## 
## $arima$user.child
## [1] 0
## 
## $arima$sys.child
## [1] 0
## 
## 
## $arima_xreg
## $arima_xreg$user.self
## [1] 653.077
## 
## $arima_xreg$sys.self
## [1] 25.525
## 
## $arima_xreg$elapsed
## [1] 719.484
## 
## $arima_xreg$user.child
## [1] 0
## 
## $arima_xreg$sys.child
## [1] 0
## 
## 
## $prophet
## $prophet$user.self
## [1] 10.408
## 
## $prophet$sys.self
## [1] 0.777
## 
## $prophet$elapsed
## [1] 11.447
## 
## $prophet$user.child
## [1] 0
## 
## $prophet$sys.child
## [1] 0
## 
## 
## $prophet_hol
## $prophet_hol$user.self
## [1] 23.725
## 
## $prophet_hol$sys.self
## [1] 1.602
## 
## $prophet_hol$elapsed
## [1] 25.434
## 
## $prophet_hol$user.child
## [1] 0
## 
## $prophet_hol$sys.child
## [1] 0
# saving is done to avoid having to run the models again
saveRDS(models,
        paste0(wd,
               "/Models/",
               "models.Rds"
               )
        )

# models <-
#   readRDS(paste0(wd,
#                  "/Data_Processed/",
#                  "models.Rds"
#                  )
#           )

Create the Prophet forecasts and plots - these will be used for measuring model accuracy (below). Here I creat the future prophet dataset.

# Dataframe for future dates
prophet.future <-
  pmap(.l = list(a = models),
       .f = function(a) {
         splits_p_future =
           a$prophet %>% 
           map(make_future_dataframe,
               periods = 365
               )
         
         splits_p_hol_future =
           a$prophet_hol %>% 
           map(make_future_dataframe,
               periods = 365
               )
         
         a$prophet.future <- splits_p_future
         a$prophet_hol.future <- splits_p_hol_future
         
         return(a)
         }
       )

length(prophet.future$`40600`$prophet.future)
## [1] 13
length(prophet.future$`40600`$prophet_hol.future)
## [1] 13

And now I can create the prohpet forecasts. First, the basic prophet forecast.

# user  system elapsed 
# 220.704  21.132 242.524
# ~ 4 min
start <- proc.time()
prophet.forecast <-
  pmap(.l = list(a = prophet.future),
       .f = function(a) {
         splits_p_m =
           a$prophet
         
         splits_p_future =
           a$prophet.future
         
         splits_p_forecast =
           pmap(.l = list(b = splits_p_m,
                          c = splits_p_future
                          ),
                .f = function(b, c) {
                  predict(b, c) %>% 
                    # if_else is needed to prevent any negative predictions
                    mutate(yhat_zero_floor = if_else(yhat < 0,
                                                     0,
                                                     yhat
                                                     )
                           )
                  }
                )
         
         a$prophet.forecast = splits_p_forecast
         
         return(a)
         }
       )

time.prophet.forecast <- proc.time() - start
time.prophet.forecast
##    user  system elapsed 
## 243.423  21.534 282.165

And now the prophet forecast including holidays.

# user  system elapsed 
# 792.629  78.698 881.271
# ~ 15 min
start <- proc.time()
prophet.forecast <-
  pmap(.l = list(a = prophet.forecast),
       .f = function(a) {
         splits_p_hol_m =
           a$prophet_hol
         
         splits_p_hol_future =
           a$prophet_hol.future
         
         splits_p_hol_forecast =
           pmap(.l = list(b = splits_p_hol_m,
                          c = splits_p_hol_future
                          ),
                .f = function(b, c) {
                  predict(b, c) %>% 
                    # if_else is needed to prevent any negative predictions
                    mutate(yhat_zero_floor = if_else(yhat < 0,
                                                     0,
                                                     yhat
                                                     )
                           )
                  }
                )
         
         a$prophet_hol.forecast = splits_p_hol_forecast
         
         return(a)
         }
       )

time.prophet_hol.forecast <- proc.time() - start
time.prophet_hol.forecast
##    user  system elapsed 
## 833.379  79.933 960.309

Now I simply update the run_times dataset with the relevant prophet info, and the relvant info from the random forest and xgboost models.

run_times[5:6] <-
  list(prophet.forecast = as.list(time.prophet.forecast),
       prophet_hol.forecast = as.list(time.prophet_hol.forecast)
       )

names(run_times)[5:6] <- c("prophet.forecast", "prophet_hol.forecast")

run_times[7:8] <-
  list(rf_corr_no = as.list(time.Rf.corr_no),
       xgbtree_corr_yes = as.list(time.Xgbtree.corr_yes)
       )
  
names(run_times)[7:8] <- c("rf_corr_no", "xgbtree_corr_yes")


str(run_times)
## List of 8
##  $ arima               :List of 5
##   ..$ user.self : num 995
##   ..$ sys.self  : num 14.3
##   ..$ elapsed   : num 1145
##   ..$ user.child: num 0
##   ..$ sys.child : num 0
##  $ arima_xreg          :List of 5
##   ..$ user.self : num 653
##   ..$ sys.self  : num 25.5
##   ..$ elapsed   : num 719
##   ..$ user.child: num 0
##   ..$ sys.child : num 0
##  $ prophet             :List of 5
##   ..$ user.self : num 10.4
##   ..$ sys.self  : num 0.777
##   ..$ elapsed   : num 11.4
##   ..$ user.child: num 0
##   ..$ sys.child : num 0
##  $ prophet_hol         :List of 5
##   ..$ user.self : num 23.7
##   ..$ sys.self  : num 1.6
##   ..$ elapsed   : num 25.4
##   ..$ user.child: num 0
##   ..$ sys.child : num 0
##  $ prophet.forecast    :List of 5
##   ..$ user.self : num 243
##   ..$ sys.self  : num 21.5
##   ..$ elapsed   : num 282
##   ..$ user.child: num 0
##   ..$ sys.child : num 0
##  $ prophet_hol.forecast:List of 5
##   ..$ user.self : num 833
##   ..$ sys.self  : num 79.9
##   ..$ elapsed   : num 960
##   ..$ user.child: num 0
##   ..$ sys.child : num 0
##  $ rf_corr_no          :List of 5
##   ..$ user.self : num 56.2
##   ..$ sys.self  : num 2.88
##   ..$ elapsed   : num 471
##   ..$ user.child: num 0
##   ..$ sys.child : num 0
##  $ xgbtree_corr_yes    :List of 5
##   ..$ user.self : num 10.6
##   ..$ sys.self  : num 2.53
##   ..$ elapsed   : num 215
##   ..$ user.child: num 0
##   ..$ sys.child : num 0
# saving is done to avoid having to run the forecasts again
saveRDS(run_times,
        paste0(wd,
               "/Models/",
               "run_times.Rds"
               )
        )

# run_times <-
#   readRDS(paste0(wd,
#                  "/Models/",
#                  "run_times.Rds"
#                  )
#           )


rm(time.Rf.corr_no, time.Xgbtree.corr_yes)

And now I can create the prophet plots.

prophet.plots <-
  pmap(.l = list(a = prophet.forecast),
       .f = function(a) {
         splits_p_m =
           a$prophet
         
         splits_p_hol_m =
           a$prophet_hol
         
         splits_p_forecast =
           a$prophet.forecast
         
         splits_p_hol_forecast =
           a$prophet_hol.forecast
         
         
         splits_p_plots =
           pmap(.l = list(b = splits_p_m,
                          c = splits_p_forecast
                          ),
                .f = function(b, c) {
                  plot(b, c)
                  }
                )
         
         splits_p_hol_plots =
           pmap(.l = list(b = splits_p_hol_m,
                          c = splits_p_hol_forecast
                          ),
                .f = function(b, c) {
                  plot(b, c)
                  }
                )
         
         
         a$prophet.plots <- splits_p_plots
         a$prophet_hol.plots <- splits_p_hol_plots
         
         return(a)
         }
       )

# saving is done to avoid having to run the forecasts again
saveRDS(prophet.plots,
        paste0(wd,
               "/Models/",
               "prophet.plots.Rds"
               )
        )

# prophet.plots <-
#   readRDS(paste0(wd,
#                  "/Models/",
#                  "prophet.plots.Rds"
#                  )
#           )


names(prophet.plots)
## [1] "40600" "41140" "40120" "40910" "40380" "41660"
names(prophet.plots$`40600`)
##  [1] "splits"               "id"                   "start_date"          
##  [4] "arima"                "arima_xreg"           "prophet"             
##  [7] "prophet_hol"          "prophet.future"       "prophet_hol.future"  
## [10] "prophet.forecast"     "prophet_hol.forecast" "prophet.plots"       
## [13] "prophet_hol.plots"
length(prophet.plots$`40600`$prophet.plots)
## [1] 13
length(prophet.plots$`40600`$prophet_hol.plots)
## [1] 13
message("prophet")
## prophet
pmap(.l = list(a = prophet.plots
               ),
     .f = function(a, b) {
       dat = a
       
       dat$prophet.plots[[10]]
       }
     )
## $`40600`

## 
## $`41140`

## 
## $`40120`

## 
## $`40910`

## 
## $`40380`

## 
## $`41660`

message("prophet_hol")
## prophet_hol
pmap(.l = list(a = prophet.plots
               ),
     .f = function(a, b) {
       dat = a
       
       dat$prophet_hol.plots[[10]]
       
       }
     )
## $`40600`

## 
## $`41140`

## 
## $`40120`

## 
## $`40910`

## 
## $`40380`

## 
## $`41660`