Package 'rollshap'

Title: Rolling Shapley Values
Description: Analytical computation of rolling and expanding Shapley values for time-series data. The 'rollshap' package decomposes the coefficient of determination (R-squared) of a linear regression into nonnegative contributions from each explanatory variable using the Shapley value from cooperative game theory (Shapley, 1953, <doi:10.1515/9781400881970-018>). For each window, the exact Shapley value is computed by fitting all subsets of the explanatory variables and averaging the marginal contribution to R-squared across all orderings, which returns an order-invariant attribution that sums to the full-model R-squared. Use cases include variable importance, factor attribution, and feature selection in time-series regression. The package supports rolling and expanding windows, weights, and handling of missing values via the min_obs, complete_obs, and na_restore arguments. The implementation uses the online and offline algorithms from the 'roll' package to compute rolling and expanding cross-products efficiently, with parallelism across columns and windows provided by 'RcppParallel'.
Authors: Jason Foster [aut, cre]
Maintainer: Jason Foster <[email protected]>
License: GPL (>= 2)
Version: 1.0.2
Built: 2026-11-20 00:13:14 UTC
Source: https://github.com/jasonjfoster/rollshap

Help Index


Rolling Shapley Values

Description

Analytical computation of rolling and expanding Shapley values for time-series data. The 'rollshap' package decomposes the coefficient of determination (R-squared) of a linear regression into nonnegative contributions from each explanatory variable using the Shapley value from cooperative game theory (Shapley, 1953, <doi:10.1515/9781400881970-018>). For each window, the exact Shapley value is computed by fitting all subsets of the explanatory variables and averaging the marginal contribution to R-squared across all orderings, which returns an order-invariant attribution that sums to the full-model R-squared. Use cases include variable importance, factor attribution, and feature selection in time-series regression. The package supports rolling and expanding windows, weights, and handling of missing values via the min_obs, complete_obs, and na_restore arguments. The implementation uses the online and offline algorithms from the 'roll' package to compute rolling and expanding cross-products efficiently, with parallelism across columns and windows provided by 'RcppParallel'.

Details

rollshap is a package that provides analytical computation of rolling and expanding Shapley values for time-series data.

Author(s)

Jason Foster [aut, cre]

References

Shapley, L.S. (1953). "A Value for n-Person Games." In Contributions to the Theory of Games, Volume II, edited by H.W. Kuhn and A.W. Tucker, 307-317. Princeton University Press. doi:10.1515/9781400881970-018


Rolling Shapley Values

Description

A function for computing the rolling and expanding Shapley values of time-series data.

Usage

roll_shap(x, y, width, weights = rep(1, width), intercept = TRUE,
  min_obs = width, complete_obs = TRUE, na_restore = FALSE,
  online = TRUE)

Arguments

x

vector or matrix. Rows are observations and columns are the independent variables.

y

vector or matrix. Rows are observations and columns are the dependent variables.

width

integer. Window size.

weights

vector. Weights for each observation within a window.

intercept

logical. Either TRUE to include or FALSE to remove the intercept.

min_obs

integer. Minimum number of observations required to have a value within a window, otherwise result is NA.

complete_obs

logical. If TRUE then rows containing any missing values are removed, if FALSE then pairwise is used.

na_restore

logical. Should missing values be restored?

online

logical. Process observations using an online algorithm.

Details

Every subset of the columns of x is fitted within each window, and the Shapley value of each variable is its marginal contribution to r-squared averaged over all orderings, so the attribution is order-invariant and sums to the r-squared of the full model. If intercept is TRUE then r-squared is centered, if FALSE then uncentered.

The number of subsets grows as 2^p in the number of variables p, so ncol(x) is limited to 20. complete_obs = FALSE is not supported, so a warning is given and the argument is ignored. The online algorithm is used for equal or exponential decay weights, otherwise the offline algorithm is used with a warning if online is TRUE.

Value

An object of the same class and dimension as x with the rolling and expanding Shapley values.

Examples

n <- 15
m <- 3
x <- matrix(rnorm(n * m), nrow = n, ncol = m)
y <- rnorm(n)
weights <- 0.9 ^ (n:1)

# rolling Shapley values with complete windows
roll_shap(x, y, width = 5)

# rolling Shapley values with partial windows
roll_shap(x, y, width = 5, min_obs = 1)

# expanding Shapley values with partial windows
roll_shap(x, y, width = n, min_obs = 1)

# expanding Shapley values with partial windows and weights
roll_shap(x, y, width = n, min_obs = 1, weights = weights)