suppressMessages({
  library(modeltime)
  library(timetk)   
  library(lubridate)
  library(tidyverse)
  library(data.table)
  library(tidymodels)
  library(modeltime.ensemble)
})
options(repr.plot.width = 12, repr.plot.height = 8)
knitr::opts_chunk$set(echo = TRUE)
train <- read.csv("~/Desktop/train.csv")
train$date= as.Date(train$date, "%m/%d/%Y %H:%M")
train=train %>% 
  group_by(date) %>% 
  summarise_each(funs(sum))
train %>% head(3)
## # A tibble: 3 x 5
##   date        user session visitors views
##   <date>     <int>   <int>    <int> <int>
## 1 2018-09-09   281     266       73  1826
## 2 2018-09-10   264     247       51  2092
## 3 2018-09-11   329     310       58  1998

Time series Plot

## Using date_var: date

Arima model

## frequency = 7 observations per 1 week
## parsnip model object
## 
## Fit time:  1.8s 
## Series: outcome 
## ARIMA(1,0,2)(0,1,2)[7] with drift 
## 
## Coefficients:
##          ar1      ma1      ma2     sma1    sma2   drift
##       0.8674  -0.2586  -0.1947  -0.8988  0.0589  3.4389
## s.e.  0.0423   0.0599   0.0520   0.0400  0.0370  0.9404
## 
## sigma^2 estimated as 66722:  log likelihood=-5049.95
## AIC=10113.89   AICc=10114.05   BIC=10145.98

Prophet model

## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## parsnip model object
## 
## Fit time:  2.8s 
## PROPHET Model
## - growth: 'linear'
## - n.changepoints: 25
## - changepoint.range: 0.8
## - yearly.seasonality: 'TRUE'
## - weekly.seasonality: 'auto'
## - daily.seasonality: 'auto'
## - seasonality.mode: 'additive'
## - changepoint.prior.scale: 0.05
## - seasonality.prior.scale: 10
## - holidays.prior.scale: 10
## - logistic_cap: NULL
## - logistic_floor: NULL
## - extra_regressors: 0

Elastic Net

Sub-Model Evaluation

## # Modeltime Table
## # A tibble: 2 x 3
##   .model_id .model   .model_desc                      
##       <int> <list>   <chr>                            
## 1         1 <fit[+]> ARIMA(1,0,2)(0,1,2)[7] WITH DRIFT
## 2         2 <fit[+]> PROPHET
Accuracy Table
.model_id .model_desc .type mae mape mase smape rmse rsq
1 ARIMA(1,0,2)(0,1,2)[7] WITH DRIFT Test 516.49 22.06 0.94 19.51 683.77 0.42
2 PROPHET Test 686.46 31.91 1.25 26.86 817.22 0.54
## Warning: Expecting the following names to be in the data frame: .conf_hi, .conf_lo. 
## Proceeding with '.conf_interval_show = FALSE' to visualize the forecast without confidence intervals.
## Alternatively, try using `modeltime_calibrate()` before forecasting to add confidence intervals.

## ── Modeltime Ensemble ───────────────────────────────────────────
## Ensemble of 2 Models (MEAN)
## 
## # Modeltime Table
## # A tibble: 2 x 3
##   .model_id .model   .model_desc                      
##       <int> <list>   <chr>                            
## 1         1 <fit[+]> ARIMA(1,0,2)(0,1,2)[7] WITH DRIFT
## 2         2 <fit[+]> PROPHET
## ── Modeltime Ensemble ───────────────────────────────────────────
## Ensemble of 2 Models (MEAN)
## 
## # Modeltime Table
## # A tibble: 2 x 3
##   .model_id .model   .model_desc                      
##       <int> <list>   <chr>                            
## 1         1 <fit[+]> ARIMA(1,0,2)(0,1,2)[7] WITH DRIFT
## 2         2 <fit[+]> PROPHET
## # Modeltime Table
## # A tibble: 2 x 3
##   .model_id .model         .model_desc                
##       <int> <list>         <chr>                      
## 1         1 <ensemble [2]> ENSEMBLE (MEAN): 2 MODELS  
## 2         2 <ensemble [2]> ENSEMBLE (MEDIAN): 2 MODELS
Accuracy Table
.model_id .model_desc .type mae mape mase smape rmse rsq
1 ENSEMBLE (MEAN): 2 MODELS Test 583.7 26.24 1.07 22.78 726.15 0.46
2 ENSEMBLE (MEDIAN): 2 MODELS Test 583.7 26.24 1.07 22.78 726.15 0.46
ensemble_models_tbl %>%
    modeltime_forecast(
        new_data    = testing(splits),
        actual_data = train
    ) %>%
    plot_modeltime_forecast(.interactive =FALSE)
## Warning: Expecting the following names to be in the data frame: .conf_hi, .conf_lo. 
## Proceeding with '.conf_interval_show = FALSE' to visualize the forecast without confidence intervals.
## Alternatively, try using `modeltime_calibrate()` before forecasting to add confidence intervals.

ensemble_models_tbl  %>%
    modeltime_forecast(
        new_data    = testing(splits),
        actual_data = train
    ) %>%
    plot_modeltime_forecast(.interactive =FALSE)
## Warning: Expecting the following names to be in the data frame: .conf_hi, .conf_lo. 
## Proceeding with '.conf_interval_show = FALSE' to visualize the forecast without confidence intervals.
## Alternatively, try using `modeltime_calibrate()` before forecasting to add confidence intervals.

Predict next two month

ensemble_models_tbl  %>%
  # Remove ARIMA model with low accuracy
  filter(.model_id != 3) %>%
  
  # Refit and Forecast Forward
  modeltime_refit(train) %>%
  modeltime_forecast(h = "2 months", actual_data = train) %>%
  plot_modeltime_forecast(.interactive = FALSE)
## frequency = 7 observations per 1 week
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## frequency = 7 observations per 1 week
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Warning: Expecting the following names to be in the data frame: .conf_hi, .conf_lo. 
## Proceeding with '.conf_interval_show = FALSE' to visualize the forecast without confidence intervals.
## Alternatively, try using `modeltime_calibrate()` before forecasting to add confidence intervals.

predict<-ensemble_models_tbl  %>%
  # Remove ARIMA model with low accuracy
  filter(.model_id != 3) %>%
  
  # Refit and Forecast Forward
  modeltime_refit(train) %>%
  modeltime_forecast(h = "2 months", actual_data = train) 
## frequency = 7 observations per 1 week
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## frequency = 7 observations per 1 week
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
write.csv(predict,"~/Desktop/predict.csv", row.names = FALSE)