DiscoverR-bloggersLinear Model with Feature Engineering: Silver Prices Surge
Linear Model with Feature Engineering: Silver Prices Surge

Linear Model with Feature Engineering: Silver Prices Surge

Update: 2025-09-01
Share

Description




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


Silver prices have reached a 14-year high amid growing expectations that the US Federal Reserve (FED) will cut interest rates this month.





According to the machine learning model, the bands are down, and the price is above the upper band, indicating anomalous price levels.





<figure><button class="lightbox-trigger" type="button">
<svg fill="none" height="12" viewBox="0 0 12 12" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" fill="#fff">
</svg>
</button></figure>



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")


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.
Continue reading: Linear Model with Feature Engineering: Silver Prices Surge
Comments 
loading
00:00
00:00
x

0.5x

0.8x

1.0x

1.25x

1.5x

2.0x

3.0x

Sleep Timer

Off

End of Episode

5 Minutes

10 Minutes

15 Minutes

30 Minutes

45 Minutes

60 Minutes

120 Minutes

Linear Model with Feature Engineering: Silver Prices Surge

Linear Model with Feature Engineering: Silver Prices Surge

Selcuk Disci