Linear model with function -engineering: Silver prices rise | R-Bloggers

Linear model with function -engineering: Silver prices rise | R-Bloggers

[This article was first published on DataGeeek, and kindly contributed to R-bloggers]. (You can report problems here about the content on this page)


Do you want to share your content on R-bloggers? Click here if you have a blog, or here If you don’t.

The silver prices have reached a highest height of 14 years in the midst of the growing expectations that the American Federal Reserve (FED) will lower interest rates this month.

According to the Machine Learning model, the tires are down and the price is above the top tire, which indicates anomal price levels.

Source code:

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

#Silver Futures
df_silver <- 
  tq_get("SI=F") %>% 
  select(date, close) %>% 
  filter(date >= last(date) - months(36)) %>% 
  drop_na()


#Splitting the data
df_split <- 
  df_silver %>% 
  time_series_split(assess = "30 days", 
                    cumulative = TRUE)

df_train <- 
  training(df_split)

df_test <- 
  testing(df_split)

# Turn the normal mean function into a rolling mean with a 5 row .period
mean_roll_5 <- slidify(mean, .period = 5, .align = "right")

#Preprocessing
rec_spec <- 
  recipe(close ~ ., data = df_train) %>% 
  step_timeseries_signature(date) %>% 
  step_mutate(slid_close = mean_roll_5(close)) %>% 
  step_impute_bag(slid_close) %>% 
  step_fourier(date, period = 365, K = 5) %>%
  step_rm(date) %>%
  step_dummy(all_nominal_predictors(), one_hot = TRUE) %>% 
  step_zv(all_predictors()) %>% 
  step_normalize(all_numeric_predictors())


#Model Specification
mod_spec <- 
  linear_reg() %>% 
  set_engine("lm")


#Training
wflow_fit <- 
  workflow() %>% 
  add_recipe(rec_spec) %>% 
  add_model(mod_spec) %>% 
  fit(df_train)

#Modeltime
df_modeltime <- 
  modeltime_table(wflow_fit)

#Calibrate the model to the testing set
calibration_tbl <- 
  df_modeltime %>%
  modeltime_calibrate(new_data = df_test)


#Accuracy of the finalized model
calibration_tbl %>%
  modeltime_accuracy(metric_set = metric_set(rmse, rsq, mape))

  
#Prediction Intervals
calibration_tbl %>%
  modeltime_forecast(
    new_data    = df_test,
    actual_data = df_test
  ) %>% 
  plot_modeltime_forecast(
    .interactive = FALSE,
    .line_size = 1.5
  )  +
  labs(title = "Silver Futures", 
       subtitle = "Predictive Intervals of ML Model Model", 
       y = "", x = "") + 
  scale_y_continuous(labels = scales::label_currency()) +
  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")


#Linear #model #function #engineering #Silver #prices #rise #RBloggers

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *