fastshap icon indicating copy to clipboard operation
fastshap copied to clipboard

Missing object 'phis'

Open T7963 opened this issue 1 year ago • 6 comments

I'm trying to use fastshap for the analysis of the prediction of an elastic neural network with 3 outputs. The wrapper function is designed for one of the outputs and generating a numeric vector in the correct way. However, following prediction the error message "object 'phis' not found" is reported. The enviroment is: R4.1.1, Keras3, Tensorflow 2.17, module shap imported with reticulate::py_install("shap")

explainer_Y1 <- fastshap::explain( loaded_model, baseline = baseline, X = x_train_reshaped_2d, pred_wrapper = function(object, newdata) predict_wrapper(object, newdata, "Y1"), nsim = 300, adjust = TRUE ) I modified adjust and baseline as well. Every possible combination fails.

T7963 avatar Sep 16 '24 15:09 T7963

Sorry for the delay @T7963, I will try to take a look. Do you have a reprex I can run on my end?

bgreenwell avatar Nov 22 '24 00:11 bgreenwell

I have this same problem. Any thoughts?

ssefick avatar Jul 17 '25 02:07 ssefick

Thanks @ssefick. It would help to have a reproducible example to run on my end. Can you add one? Using the reprex package is helpful.

bgreenwell avatar Jul 17 '25 11:07 bgreenwell

I'll see if I can replicate the issue with an example dataset.

The actual problem is with data I can not share.

Thanks for helping!

ssefick avatar Jul 17 '25 17:07 ssefick

No problem, sounds good! Thanks for trying to put one together.

bgreenwell avatar Jul 17 '25 20:07 bgreenwell

I could replicate the issue using the example data. It occurs mainly when using feature_names argument. Another observation, no phis error when adjust=FALSE, even with one feature.

library(fastshap)

head(t1 <- titanic_mice[[1L]])
#>   survived pclass   age    sex sibsp parch
#> 1      yes      1 29.00 female     0     0
#> 2      yes      1  0.92   male     1     2
#> 3       no      1  2.00 female     1     2
#> 4       no      1 30.00   male     1     2
#> 5       no      1 25.00 female     1     2
#> 6      yes      1 48.00   male     0     0

library(ranger)

# for reproducibility
set.seed(2053)  
(rfo <- ranger(survived ~ ., data = t1, probability = TRUE))
#> Ranger result
#> 
#> Call:
#>  ranger(survived ~ ., data = t1, probability = TRUE) 
#> 
#> Type:                             Probability estimation 
#> Number of trees:                  500 
#> Sample size:                      1309 
#> Number of independent variables:  5 
#> Mtry:                             2 
#> Target node size:                 10 
#> Variable importance mode:         none 
#> Splitrule:                        gini 
#> OOB prediction error (Brier s.):  0.1337358

# prediction wrapper
pfun <- function(object, newdata) {  
  unname(predict(object, data = newdata)$predictions[, "yes"])
}

# features only
X <- subset(t1, select = -survived)  

# for reproducibility
set.seed(2129)  
ex.t1 <- explain(rfo, 
                 X = X, 
                 pred_wrapper = pfun, 
                 nsim = 25, 
                 adjust = TRUE,
                 shap_only = FALSE)

set.seed(2130)  
ex.t2 <- explain(rfo, 
                 X = X, 
                 feature_names = "age",
                 pred_wrapper = pfun, 
                 nsim = 25, 
                 adjust = TRUE,
                 shap_only = FALSE)
#> Error in explain.default(rfo, X = X, feature_names = "age", pred_wrapper = pfun, : object 'phis' not found

set.seed(2131)  
ex.t2 <- explain(rfo, 
                 X = X, 
                 feature_names = c("age", "sex"),
                 pred_wrapper = pfun, 
                 nsim = 25, 
                 adjust = TRUE,
                 shap_only = FALSE)


library(doParallel)
#> Loading required package: foreach
#> Loading required package: iterators
#> Loading required package: parallel

cl <- makeCluster(1L)
registerDoParallel(cores = cl)  
set.seed(5038)
ex.t3 <- explain(rfo, 
                 X = X, 
                 feature_names = "age",
                 pred_wrapper = pfun, 
                 nsim = 25, 
                 adjust = TRUE,
                 shap_only = FALSE, 
                 parallel = TRUE)
#> Error in explain.default(rfo, X = X, feature_names = "age", pred_wrapper = pfun, : object 'phis' not found
stopCluster(cl)

cl <- makeCluster(2L)
registerDoParallel(cores = cl)  
set.seed(5038)
ex.t4 <- explain(rfo, 
                 X = X, 
                 feature_names = c("age", "sex"),
                 pred_wrapper = pfun, 
                 nsim = 25, 
                 adjust = TRUE,
                 shap_only = FALSE, 
                 parallel = TRUE)
stopCluster(cl)

Created on 2025-12-04 with reprex v2.1.0

ahomoudi avatar Dec 04 '25 06:12 ahomoudi