Explaining Time-Series Forecasts with Sensitivity Analysis (ahead::dynrmf and external regressors)
R-bloggers 2026-03-29
[This article was first published on T. Moudiki's Webpage - R, 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.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Following the post on exact Shapley values for time series explainability, this post illustrates an example of how to use sensitivity analysis to explain time-series forecasts, based on the ahead::dynrmf model and external regressors. What is sensitivity analysis in this context? It’s about evaluating the impact of changes in the external regressors on the time-series forecast.
The post uses the ahead::dynrmf_sensi function to compute the sensitivities, and the ahead::plot_dynrmf_sensitivity function to plot the results.
First, install the package:
devtools::install_github("Techtonique/ahead")Then, run the following code:
# devtools::install_github("Techtonique/ahead")# install.packages(c("fpp2", "e1071", "patchwork"))library(ahead)library(fpp2)library(patchwork)library(e1071)#' # Example 1: US Consumption vs Incomesensitivity_results_auto <- ahead::dynrmf_sensi(y = fpp2::uschange[, "Consumption"],xreg = fpp2::uschange[, "Income"],h = 10)plot1 <- ahead::plot_dynrmf_sensitivity(sensitivity_results_auto, title = "Sensitivity of Consumption to Income (Ridge)", y_label = "Effect (ΔConsumption / ΔIncome)")#' # Example 1: US Consumption vs Incomesensitivity_results_auto_svm <- ahead::dynrmf_sensi( y = fpp2::uschange[, "Consumption"], xreg = fpp2::uschange[, "Income"], h = 10, fit_func = e1071::svm # additional parameter passed to ahead::dynrmf)plot2 <- ahead::plot_dynrmf_sensitivity(sensitivity_results_auto_svm, title = "Sensitivity of Consumption to Income (SVM)", y_label = "Effect (ΔConsumption / ΔIncome)") # Example 2: TV Advertising vs Insurance Quotessensitivity_results_tv <- ahead::dynrmf_sensi( y = fpp2::insurance[, "Quotes"], xreg = fpp2::insurance[, "TV.advert"], h = 8 )plot3 <- ahead::plot_dynrmf_sensitivity(sensitivity_results_tv, title = "Sensitivity of Insurance Quotes to TV Advertising (Ridge)", y_label = "Effect (ΔQuotes / ΔTV.advert)")sensitivity_results_tv_svm <- ahead::dynrmf_sensi( y = fpp2::insurance[, "Quotes"], xreg = fpp2::insurance[, "TV.advert"], h = 8, fit_func = e1071::svm # additional parameter passed to ahead::dynrmf)plot4 <- ahead::plot_dynrmf_sensitivity(sensitivity_results_tv_svm, title = "Sensitivity of Insurance Quotes to TV Advertising (SVM)", y_label = "Effect (ΔQuotes / ΔTV.advert)")(plot1+plot2)(plot3+plot4)

To leave a comment for the author, please follow the link and comment on their blog: T. Moudiki's Webpage - R.
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.