Ensembles of Models
[This article was first published on DataGeeek, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The BIST Technology index seems to have reached the upper band despite the ongoing political crisis in Turkey. But could it be continued?
Source code:
library(tidymodels)
library(tidyverse)
library(modeltime)
library(modeltime.ensemble)
library(timetk)
#BIST Technology Index
df_bist <-
read_csv("data/bist_tech.csv") %>%
janitor::clean_names() %>%
mutate(date = parse_date(date, "%m/%d/%Y")) %>%
select(date, value = price) %>%
slice_min(date, n = -1)
#Splitting
splits <-
time_series_split(df_bist,
assess = "3 months",
cumulative = TRUE)
#Recipe
recipe_spec <-
recipe(value ~ date, training(splits)) %>%
step_timeseries_signature(date) %>%
step_rm(matches("(.iso$)|(.xts$)")) %>%
step_normalize(matches("(index.num$)|(_year$)")) %>%
step_dummy(all_nominal()) %>%
step_fourier(date, K = 1, period = 365)
#Model 1 - Auto ARIMA
model_spec_arima <-
arima_reg() %>%
set_engine("auto_arima")
wflw_fit_arima <-
workflow() %>%
add_model(model_spec_arima) %>%
add_recipe(recipe_spec %>% step_rm(all_predictors(), -date)) %>%
fit(training(splits))
#Model 2 - Prophet
model_spec_prophet <-
prophet_reg() %>%
set_engine("prophet")
wflw_fit_prophet <-
workflow() %>%
add_model(model_spec_prophet) %>%
add_recipe(recipe_spec %>% step_rm(all_predictors(), -date)) %>%
fit(training(splits))
#Model 3 - Elastic Net
model_spec_glmnet <-
linear_reg(
mixture = 0.9,
penalty = 4.36e-6
) %>%
set_engine("glmnet")
wflw_fit_glmnet <-
workflow() %>%
add_model(model_spec_glmnet) %>%
add_recipe(recipe_spec %>% step_rm(date)) %>%
fit(training(splits))
#Modeltime Workflow for Ensemble Forecasting
df_models <-
modeltime_table(
wflw_fit_arima,
wflw_fit_prophet,
wflw_fit_glmnet
)
#Make an Ensemble
ensemble_fit <-
df_models %>%
ensemble_average(type = "mean")
#Calibration
calibration_tbl <-
modeltime_table(
ensemble_fit
) %>%
modeltime_calibrate(testing(splits))
#Accuracy
calibration_tbl %>%
modeltime_accuracy() %>%
table_modeltime_accuracy(
.interactive = FALSE
)
#Predictive intervals (95% Confidence Interval)
calibration_tbl %>%
modeltime_forecast(actual_data = df_bist %>%
filter(date >= last(date) - months(3)),
new_data = testing(splits)) %>%
plot_modeltime_forecast(.interactive = FALSE,
.legend_show = FALSE,
.line_size = 1.5,
.color_lab = "",
.title = "BIST Technology Index") +
labs(subtitle = "<span style = 'color:dimgrey;'>Predictive Intervals</span><br><span style = 'color:red;'>Ensemble Model</span>") +
scale_y_continuous(labels = scales::label_currency(prefix = "",suffix = "₺")) +
theme_minimal(base_family = "Roboto Slab", base_size = 20) +
theme(legend.position = "none",
plot.background = element_rect(fill = "azure",
color = "azure"),
plot.title = element_text(face = "bold"),
axis.text = element_text(face = "bold"),
#axis.text.x = element_text(angle = 60, hjust = 1, vjust = 1),
plot.subtitle = ggtext::element_markdown(face = "bold", size = 20))
To leave a comment for the author, please follow the link and comment on their blog: DataGeeek.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.