Bagged Neural Networks: Will Bayrou’s Fell Affect the STOXX 600 Index?

[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.

The French government is planning to hold a confidence vote on Prime Minister François Bayrou’s fiscal plan.

If a coalition of opposition parties votes against the government, as is widely expected, Bayrou will have to submit his resignation to French President Emmanuel Macron.

This could cause a fall to the middle band for the STOXX Europe 600 index, according to the chart built by bagged neural networks via nnet.

Source code:

library(tidyverse)
library(tidymodels)
library(timetk)
library(modeltime)
library(tidyquant)
library(baguette)

#STXE 600 (^STOXX)
df_stoxx <-  
  tq_get("^STOXX") %>% 
  select(date, close) %>% 
  drop_na()

#Splitting the Data 
splits <- 
  time_series_split(
    df_stoxx,
    assess     = "1 month",
    cumulative = TRUE
  )

df_train <- training(splits)
df_test <- testing(splits)


#Recipe
rec_nnet <- 
  recipe(close ~ ., data = df_train) %>% 
  step_timeseries_signature(date) %>% 
  step_rm(date) %>% 
  step_dummy(all_nominal_predictors(), one_hot = TRUE) %>% 
  step_zv(all_predictors()) %>% 
  step_normalize(all_numeric_predictors())

#Model
mod_nnet <- 
  bag_mlp(penalty = tune(), 
          hidden_units = tune(),
          epochs = tune()) %>% 
  set_engine("nnet") %>% 
  set_mode("regression") 


#Hyperparameter Tuning
mod_param <- extract_parameter_set_dials(mod_nnet)

set.seed(1234)
model_tbl <- 
  mod_param %>% 
  grid_random(size = 10) %>%
  create_model_grid(
    f_model_spec = bag_mlp,
    engine_name  = "nnet",
    mode         = "regression"
  )

#Extracting the model list
model_list <- model_tbl$.models

#Workflowsets
model_wfset <- 
  workflow_set(
    preproc = list(rec_nnet),
    models = model_list, 
    cross = TRUE
  )

#Fitting Using Parallel Backend
model_parallel_tbl <- 
  model_wfset %>%
  modeltime_fit_workflowset(
    data    = df_train,
    control = control_fit_workflowset(
      verbose   = TRUE,
      allow_par = TRUE
    )
  )



#Accuracy 
model_parallel_tbl %>% 
  modeltime_calibrate(new_data = df_test) %>% 
  modeltime_accuracy() %>%
  table_modeltime_accuracy()



#Calibration to the test set for the best model
calibration_tbl <- 
  model_parallel_tbl %>%
  filter(.model_desc == "RECIPE_BAG_MLP_9") %>% 
  modeltime_calibrate(df_test)



#Prediction Intervals
calibration_tbl %>% 
  modeltime_forecast(new_data = df_test, 
                     actual_data = df_test) %>%
  plot_modeltime_forecast(.interactive = FALSE,
                          .legend_show = FALSE,
                          .line_size = 1.5,
                          .color_lab = "",
                          .title = "STOXX EUROPE 600") +
  labs(subtitle = "<span style = 'color:dimgrey;'>Predictive Intervals</span> of the <span style = 'color:red;'>Deep Learning Model</span>") + 
  scale_y_continuous(labels = scales::label_currency(prefix = "€", 
                                                     suffix = "")) +
  scale_x_date(labels = scales::label_date("%b %d"),
               date_breaks = "4 days") +
  theme_minimal(base_family = "Roboto Slab", base_size = 16) +
  theme(plot.subtitle = ggtext::element_markdown(face = "bold"),
        plot.title = element_text(face = "bold"),
        plot.background = element_rect(fill = "azure", color = "azure"),
        panel.background = element_rect(fill = "snow", color = "snow"),
        axis.text = element_text(face = "bold"),
        axis.text.x = element_text(angle = 45, 
                                   hjust = 1, 
                                   vjust = 1),
        legend.position = "none")
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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)