Integrating Python Forecasting with R’s Tidyverse

Wait 5 sec.

[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.In this article, we executed a successful integration of a non-standard Python forecasting model into the R Tidyverse/Tidymodels framework, primarily leveraging the reticulate package.1. The Objective (The Challenge)The goal was to utilize a powerful, custom Python model (nnetsauce‘s MTS wrapper around a cyb$BoosterRegressor) and integrate its outputs—predictions and prediction intervals—into the R ecosystem, allowing for consistent data manipulation with dplyr and high-quality visualization using ggplot2 (and interactive plotly).The challenge was that this custom Python model is not natively recognized by R’s standard forecasting packages like modeltime.2. The Integration MechanismThe integration was achieved through the following steps:reticulate Bridge: We used the reticulate package to seamlessly call the Python model, pass R data (tbl_train), fit the model (regr$fit()), and execute the forecast (regr$predict()).Data Conversion: The raw forecast output (predictions and confidence intervals) from Python was carefully extracted and converted into a standard R tibble (forecast_data_tbl).Tidymodels Simulation (Initial Attempt): We attempted to force this tibble into a modeltime structure by using dummy models and calibration, which failed due to rigid internal validation checks in plot_modeltime_forecast().Tidyverse Final Solution: We abandoned the rigid modeltime visualization functions and adopted the core Tidyverse approach:Data Manipulation: We used dplyr and tidyr::pivot_longer() to restructure the forecast data into the long format, which is optimal for ggplot2.Visualization: We used pure ggplot2 and plotly to manually draw the lines, ribbons, and apply the desired STOXX aesthetic, successfully bypassing all package incompatibility errors.3. Key FindingsThe process highlighted a critical statistical finding:Metric vs. Visual Discrepancy: The Python model’s point forecast quality was excellent (low MAPE), but its internal prediction interval calculation (type_pi="gaussian") was severely flawed (high Out-of-Band Error).Solution: To ensure the visualization reflected the low MAPE score, we calculated the true MAPE on the test set and manually generated a realistic, MAPE-based confidence interval to be used in the final plot.In essence, we created a robust R-centric pipeline for post-processing, validation, and visualization of forecasts generated by a custom Python machine learning model.library(tidyverse)library(tidyquant)library(reticulate)library(tidymodels)library(timetk)library(modeltime)library(plotly)# nnetsauce_env ortamını kullanmasını söyleyinuse_python("C:/Users/selcu/AppData/Local/r-miniconda/envs/nnetsauce_env/python.exe", required = TRUE) # Import Python packagesnp