You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 setting exact), since the documentation states the default value for exact is 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 = TRUE for 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_PriceX<- 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.
The text was updated successfully, but these errors were encountered:
nimirea
changed the title
explain() fails uninformatively if exact argument is specified, on glmnet model objectsexplain() fails uninformatively if exact argument is specified, on cv.glmnet model objects
Dec 6, 2023
When trying to use fastshap with a cv.glmnet model, specifying
exact
argument tofastshap::explain()
causes the function to fail, no matter what the supplied value ofexact
is. The error returned is not very helpful.Here's the documentation on
exact
(emphasis mine):Based on this, I would expect the following behavior when calling
fastshap::explain()
with an explicitexact
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 forexact
is FALSE.fastshap::explain(..., exact = TRUE)
should:exact = TRUE
for an object type that does not support this functionality, please see ?fastshap::explain() for more details")Instead, both cases yielded this error:
The function only works when
exact
is left unspecified.Here's a reprex (kudos to @DavZim for the example this is based on):
I am not sure whether this happens with other types of models for which
exact = TRUE
is not supported.The text was updated successfully, but these errors were encountered: