Skip to content

Commit

Permalink
finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
mdancho84 committed Jun 29, 2021
1 parent 423e7d6 commit e464820
Showing 1 changed file with 57 additions and 12 deletions.
69 changes: 57 additions & 12 deletions 040_modeltime_forecasting/040_modeltime_forecasting.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# R TIPS ----
# TIP 039 | Introduction to Modeltime: In 2-Minutes ----
# TIP 039 | Introduction to Modeltime: In 5-Minutes ----
#
# 👉 For Weekly R-Tips, Sign Up Here:
# https://mailchi.mp/business-science/r-tips-newsletter
Expand All @@ -19,45 +19,90 @@ bike_sharing_daily
data <- bike_sharing_daily %>%
select(dteday, cnt)

data

data %>% plot_time_series(dteday, cnt)

# TRAIN / TEST SPLITS ----

splits <- time_series_split(
data,
assess = "3 months",
cumulative = TRUE
)

splits %>%
tk_time_series_cv_plan() %>%
plot_time_series_cv_plan(dteday, cnt)

# FORECAST ----

# * ARIMA ---
model_arima <- arima_reg(seasonal_period = 52) %>%
# * AUTO ARIMA ---
model_arima <- arima_reg() %>%
set_engine("auto_arima") %>%
fit(cnt ~ dteday, data)
fit(cnt ~ dteday, training(splits))

model_arima

# * Prophet ----
model_prophet <- prophet_reg() %>%
model_prophet <- prophet_reg(
seasonality_yearly = TRUE
) %>%
set_engine("prophet") %>%
fit(cnt ~ dteday, data)
fit(cnt ~ dteday, training(splits))

model_prophet

# * Machine Learning - GLM ----
model_glmnet <- linear_reg(penalty = 0.01) %>%
set_engine("glmnet") %>%
fit(
cnt ~ month(dteday, label = TRUE) + wday(dteday, label = TRUE) + as.numeric(dteday),
data
cnt ~ wday(dteday, label = TRUE)
+ month(dteday, label = TRUE)
+ as.numeric(dteday),
training(splits)
)

model_glmnet


# MODELTIME COMPARE ----

# * Modeltime Table ----
model_tbl <- modeltime_table(
model_arima,
model_prophet,
model_glmnet
)

model_tbl %>%
# * Calibrate ----
calib_tbl <- model_tbl %>%
modeltime_calibrate(testing(splits))

# * Accuracy ----
calib_tbl %>% modeltime_accuracy()

# * Test Set Visualization ----
calib_tbl %>%
modeltime_forecast(
h = "1 year",
new_data = testing(splits),
actual_data = data
) %>%
plot_modeltime_forecast()

# * Forecast Future ----
future_forecast_tbl <- calib_tbl %>%
modeltime_refit(data) %>%
modeltime_forecast(
h = "3 months",
actual_data = data
)

future_forecast_tbl %>%
plot_modeltime_forecast()

# LEARNING MORE ----
# 1. FEATURE ENGINEERING - A LOT OF ROOM FOR IMPROVEMENT
# 2. MACHINE LEARNING ALGORIMTHMS | TIDYMODELS API
# 3. MODELTIME ECOSYSTEM | MODELTIME H2O, GLUONTS

# HIGH PERFORMANCE TIME SERIES COURSE:
# https://university.business-science.io/p/ds4b-203-r-high-performance-time-series-forecasting/

0 comments on commit e464820

Please sign in to comment.