| shapley | R Documentation |
Computes Weighted Mean SHAP ratios (WMSHAP) and confidence intervals to assess feature
importance across a collection of models (e.g., an H2O grid/AutoML leaderboard or
base-learners of an ensemble). Instead of reporting SHAP contributions for a single model,
this function summarizes feature importance across multiple models and weights each model
by a chosen performance metric.
Currently, only models trained by the h2o machine learning platform,
autoEnsemble, and the HMDA R packages are supported.
shapley(
models,
newdata,
plot = TRUE,
performance_metric = "r2",
standardize_performance_metric = FALSE,
performance_type = "xval",
minimum_performance = 0,
method = "mean",
cutoff = 0.01,
top_n_features = NULL,
n_models = 10,
sample_size = NULL
)
models |
An H2O AutoML object, H2O grid object, |
newdata |
An |
plot |
Logical. If |
performance_metric |
Character. Performance metric used to weight models.
Options are |
standardize_performance_metric |
Logical. If |
performance_type |
Character. Specify which performance metric performance estimate to use:
|
minimum_performance |
Numeric. Specify the minimum performance metric
for a model to be included in calculating WMSHAP.
Models below this threshold receive
zero weight and are excluded. The default is |
method |
Character. Specify the method for selecting important features
based on their WMSHAP. The default is
|
cutoff |
Numeric. Cutoff applied by |
top_n_features |
Integer or |
n_models |
Integer. Minimum number of models that must meet the performance threshold
for WMSHAP and CI computation. Use |
sample_size |
Integer. Number of rows in |
The function works as follows:
For each model, SHAP contributions are computed on newdata.
For each model, feature-level absolute SHAP contributions are aggregated and converted to a ratio (share of total absolute SHAP across features).
Models are weighted by a performance metric (e.g., "r2" for regression or
"auc" / "aucpr" for classification).
The weighted mean SHAP ratio (WMSHAP) is computed for each feature, along with an confidence interval across models.
An object of class "shapley" (a named list) containing:
Character vector of model IDs originally supplied or extracted.
Character vector of model IDs included after filtering by performance.
Data frame of excluded models and their performance.
Numeric vector of model weights (performance metrics) for included models.
Data frame of row-level SHAP contributions merged across models.
Data frame of feature-level WMSHAP means and confidence intervals.
Character vector of selected important features.
List of per-feature absolute contribution summaries by model.
A ggplot-like object returned by h2o.shap_summary_plot()
used for the WMSHAP (“wmshap”) style plot.
A ggplot object (bar plot) if plot = TRUE, otherwise NULL.
E. F. Haghish
## Not run:
# load the required libraries for building the base-learners and the ensemble models
library(h2o) #shapley supports h2o models
library(shapley)
# initiate the h2o server
h2o.init(ignore_config = TRUE, nthreads = 2, bind_to_localhost = FALSE, insecure = TRUE)
# upload data to h2o cloud
prostate_path <- system.file("extdata", "prostate.csv", package = "h2o")
prostate <- h2o.importFile(path = prostate_path, header = TRUE)
set.seed(10)
### H2O provides 2 types of grid search for tuning the models, which are
### AutoML and Grid. Below, I demonstrate how weighted mean shapley values
### can be computed for both types.
#######################################################
### PREPARE AutoML Grid (takes a couple of minutes)
#######################################################
# run AutoML to tune various models (GBM) for 60 seconds
y <- "CAPSULE"
prostate[,y] <- as.factor(prostate[,y]) #convert to factor for classification
aml <- h2o.automl(y = y, training_frame = prostate, max_runtime_secs = 120,
include_algos=c("GBM"),
# this setting ensures the models are comparable for building a meta learner
seed = 2023, nfolds = 10,
keep_cross_validation_predictions = TRUE)
### call 'shapley' function to compute the weighted mean and weighted confidence intervals
### of SHAP values across all trained models.
### Note that the 'newdata' should be the testing dataset!
result <- shapley(models = aml, newdata = prostate, performance_metric = "aucpr", plot = TRUE)
#######################################################
### PREPARE H2O Grid (takes a couple of minutes)
#######################################################
# make sure equal number of "nfolds" is specified for different grids
grid <- h2o.grid(algorithm = "gbm", y = y, training_frame = prostate,
hyper_params = list(ntrees = seq(1,50,1)),
grid_id = "ensemble_grid",
# this setting ensures the models are comparable for building a meta learner
seed = 2023, fold_assignment = "Modulo", nfolds = 10,
keep_cross_validation_predictions = TRUE)
result2 <- shapley(models = grid, newdata = prostate, performance_metric = "aucpr", plot = TRUE)
#######################################################
### PREPARE autoEnsemble STACKED ENSEMBLE MODEL
#######################################################
### get the models' IDs from the AutoML and grid searches.
### this is all that is needed before building the ensemble,
### i.e., to specify the model IDs that should be evaluated.
library(autoEnsemble)
ids <- c(h2o.get_ids(aml), h2o.get_ids(grid))
autoSearch <- ensemble(models = ids, training_frame = prostate, strategy = "search")
result3 <- shapley(models = autoSearch, newdata = prostate,
performance_metric = "aucpr", plot = TRUE)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.