-
Notifications
You must be signed in to change notification settings - Fork 17
Description
When trying to use fastshap with a cv.glmnet model, specifying exact argument to fastshap::explain() causes the function to fail, no matter what the supplied value of exact is. The error returned is not very helpful.
Here's the documentation on exact (emphasis mine):
Logical indicating whether to compute exact Shapley values. Currently only available for stats::lm(), xgboost::xgboost(), and lightgbm::lightgbm() objects. Default is FALSE. Note that setting exact = TRUE will return explanations for each of the stats::terms() in an stats::lm() object. Default is FALSE.
Based on this, I would expect the following behavior when calling fastshap::explain() with an explicit exact argument:
fastshap::explain(..., exact = FALSE)should produce the default behavior (i.e., same as calling without explicitly settingexact), since the documentation states the default value forexactis FALSE.fastshap::explain(..., exact = TRUE)should:- produce exact Shapley values for stats::lm(), xgboost::xgboost(), and lightgbm::lightgbm() objects
- fail with an informative error otherwise (e.g., "You have set
exact = TRUEfor an object type that does not support this functionality, please see ?fastshap::explain() for more details")
Instead, both cases yielded this error:
Error in `colnames<-`(`*tmp*`, value = feature_names) :
attempt to set 'colnames' on an object with less than two dimensions
The function only works when exact is left unspecified.
Here's a reprex (kudos to @DavZim for the example this is based on):
library(fastshap)
library(AmesHousing)
library(glmnet)
data <- AmesHousing::make_ames()
y <- data$Sale_Price
X <- as.matrix(data[, c("Lot_Frontage", "Lot_Area", "Year_Sold", "Year_Built")])
glm <- cv.glmnet(X, y)
pred_wrapper_fun <- function(mdl, newdata) as.numeric(predict(mdl, newdata, s = glm$lambda.min))
# This works:
expl_glm <- fastshap::explain(
object = glm,
X = X,
pred_wrapper = pred_wrapper_fun
)
# This doesn't work:
expl_glm <- fastshap::explain(
object = glm,
X = X,
pred_wrapper = pred_wrapper_fun,
exact = FALSE
)
# Error in `colnames<-`(`*tmp*`, value = feature_names) :
# attempt to set 'colnames' on an object with less than two dimensions
# This also doesn't work:
expl_glm <- fastshap::explain(
object = glm,
X = X,
pred_wrapper = pred_wrapper_fun,
exact = TRUE
)
# Error in `colnames<-`(`*tmp*`, value = feature_names) :
# attempt to set 'colnames' on an object with less than two dimensions
# This also doesn't work:
expl_glm <- fastshap::explain(
object = glm,
X = X,
pred_wrapper = pred_wrapper_fun,
exact = NULL
)
# Error in `colnames<-`(`*tmp*`, value = feature_names) :
# attempt to set 'colnames' on an object with less than two dimensions
I am not sure whether this happens with other types of models for which exact = TRUE is not supported.