Conforming forecast intervals of XGBOOST -Model: Bitcoin Peaks in the middle of Coinbase’s income struggle | R-Bloggers

Conforming forecast intervals of XGBOOST -Model: Bitcoin Peaks in the middle of Coinbase’s income struggle | R-Bloggers

2 minutes, 26 seconds Read

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

In the second quarter, Coinbase did not meet the expectations of Wall Street. This decrease took place next to lower market volatility, despite the BTC prices that record heights reached according to Kaiko Research.

The graph below indicates that the market priced the aforementioned from August. We can observe a negative decoupling between the worldwide stock prices of Bitcoin and Coinbase.

Source code:

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


#Coinbase Global
df_coin <- 
  tq_get("COIN") %>% 
  select(date, Coinbase = close)

#Bitcoin
df_btc <- 
  tq_get("BTC-USD") %>% 
  select(date, Bitcoin = close)

#Merging the datasets
df_merged <- 
  df_coin %>% 
  left_join(df_btc) %>% 
  drop_na() %>% 
  filter(date >= last(date) - months(36)) %>% 
  pivot_longer(-date,
               names_to = "id",
               values_to = "close") %>% 
  mutate(id = as_factor(id)) 


#Split Data
splits <- 
  time_series_split(
  df_merged,
  assess     = "15 days",
  cumulative = TRUE
)

#Create & Fit Forecasting Models

#Recipe
recipe_ml <- 
  recipe(close ~ ., training(splits)) %>%
  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 & Workflow
model_xgb <- 
  boost_tree("regression") %>%
  set_engine("xgboost")


wflw_fit_xgb <- 
  workflow() %>%
  add_model(model_xgb) %>%
  add_recipe(recipe_ml) %>%
  fit(training(splits))


#Adding fitted models to a Model Table
models_tbl <- modeltime_table(
  wflw_fit_xgb
)


#Calibrating the model to a testing set
calibration_tbl <- 
  models_tbl %>%
  modeltime_calibrate(
    new_data = testing(splits), 
    id       = "id"
  )


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


#Conformal Split Method
#https://business-science.github.io/modeltime/articles/modeltime-conformal-prediction.html

forecast_tbl <- 
  calibration_tbl %>%
  modeltime_forecast(
    new_data      = testing(splits),
    actual_data   = df_merged %>% filter(date >= as.Date("2025-07-23")),
    conf_interval = 0.95,
    conf_method   = "conformal_split", # Split Conformal Method
    conf_by_id    = TRUE, # TRUE = local CI by ID, FALSE = global CI
    keep_data     = TRUE
  )

#Plotting prediction intervals
forecast_tbl %>%
  group_by(id) %>%
  plot_modeltime_forecast(
    .facet_ncol  = 1, 
    .line_size = 1.5,
    .interactive = FALSE
  ) +
  labs(title = "Conformal Prediction Intervals of XGBoost Model", 
       y = "", 
       x = "") + 
  scale_y_continuous(labels = scales::label_currency()) +
  scale_x_date(labels = scales::label_date("%b %d"),
               date_breaks = "4 days") +
  theme_tq(base_family = "Roboto Slab", base_size = 16) +
  theme(plot.title = ggtext::element_markdown(face = "bold", 
                                              hjust = 0.5,
                                              size = 18),
        strip.text = element_text(face = "bold"),
        legend.position = "none")


#Conforming #forecast #intervals #XGBOOST #Model #Bitcoin #Peaks #middle #Coinbases #income #struggle #RBloggers

Similar Posts

Leave a Reply

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