Last active
July 6, 2021 21:06
-
-
Save madilk/d93ba3dc62a52eae08b2e675e6cb11e2 to your computer and use it in GitHub Desktop.
modeltime r package for time series forecasts - google analytics data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# R TIPS ---- | |
# TIP 040 | Introduction to Modeltime: In Under 10-Minutes ---- | |
#By Matt Dancho, modeltime package creator | |
#R Tips newsletter by Matt Dancho: https://mailchi.mp/business-science/r-tips-newsletter | |
# LIBRARIES ---- | |
#installing packages from git | |
#install.packages("devtools") | |
library(devtools) | |
library(usethis) | |
#install_github("tidymodels/tidymodels") | |
#install.packages("modeltime") | |
#install.packages("tiymodels") | |
#install_version("modeltime") | |
library(modeltime) | |
library(tidymodels) | |
library(tidyverse) | |
library(timetk) | |
library(lubridate) | |
# DATA ---- | |
#install.packages("googleAnalyticsR") | |
library(googleAnalyticsR) | |
#install.packages("ellipsis") | |
ga_auth(json_file="C:\\folder-where-you-place-your-json-file\\service-key.json") | |
#the Google Analytics view id | |
viewID <- 12345678 | |
data <- google_analytics( | |
viewID, | |
date_range=c("2019-01-01","2021-07-05"), | |
dimensions=c("date"), | |
metrics=c("sessions"), | |
anti_sample=TRUE | |
) | |
#backup data to xlsx | |
write_xlsx(data,path="C:\\Users\\folder-where-you-want-to-write-file-to\\data.xlsx", | |
col_names = TRUE, | |
format_headers = TRUE, | |
use_zip64 = FALSE) | |
summary(data) | |
View(data) | |
#plot time series data | |
ga_data %>% plot_time_series(date,sessions) | |
#split data into training /test | |
splits <- time_series_split( | |
ga_data, | |
assess = "6 months", | |
cumulative = TRUE | |
) | |
#plot training and test ts | |
splits %>% | |
tk_time_series_cv_plan() %>% | |
plot_time_series_cv_plan(date, sessions) | |
# FORECAST ---- | |
# * AUTO ARIMA ---- | |
library(prophet) | |
?prophet() | |
library(forecast) | |
model_arima <- arima_reg() %>% | |
set_engine("auto_arima") %>% | |
fit(sessions ~ date, training(splits)) | |
model_arima | |
# * Prophet ---- | |
model_prophet <- prophet_reg( | |
mode="regression", | |
seasonality_weekly = "auto", | |
seasonality_yearly = "auto", | |
seasonality_daily = "auto" | |
) %>% | |
set_engine("prophet") %>% | |
fit(sessions ~ date, training(splits)) | |
model_prophet | |
#AUTO ETS Model | |
model_ets <- exp_smoothing()%>% | |
set_engine("ets") %>% | |
fit(sessions ~ date,training(splits)) | |
model_ets | |
#Snaive model | |
model_snaive <- naive_reg()%>% | |
set_engine("snaive")%>% | |
fit( | |
sessions~date,training(splits)) | |
model_snaive | |
# MODELTIME COMPARE ---- | |
# * Modeltime Table ---- | |
model_tbl <- modeltime_table( | |
model_arima, | |
model_prophet, | |
model_ets, | |
model_snaive | |
) | |
# * Calibrate ---- | |
calib_tbl <- model_tbl %>% | |
modeltime_calibrate(testing(splits)) | |
# * Accuracy ---- | |
calib_tbl %>% modeltime_accuracy() | |
# * Test Set Visualization ---- | |
calib_tbl %>% | |
modeltime_forecast( | |
new_data = testing(splits), | |
actual_data = data, | |
conf_interval = 0 | |
) %>% | |
plot_modeltime_forecast() | |
# * Forecast Future ---- | |
future_forecast_tbl <- calib_tbl %>% | |
modeltime_refit(data) %>% | |
modeltime_forecast( | |
h = "3 months", | |
actual_data = data, | |
conf_interval = 0 | |
) | |
#plot forecast | |
future_forecast_tbl %>% | |
plot_modeltime_forecast() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment