ModelOriented / iBreakDown

Break Down with interactions for local explanations (SHAP, BreakDown, iBreakDown)
https://ModelOriented.github.io/iBreakDown/
GNU General Public License v3.0
81 stars 15 forks source link

Error when explaining random forest model #38

Closed GerardMeester closed 4 years ago

GerardMeester commented 5 years ago

I get the following error when using iBreadkDown in combination with a Random Forest model: _Error in yhat.default(x, data) : (list) object cannot be coerced to type 'double'__

library(tidyverse) library(DALEX) library(iBreakDown) library(randomForest)

setwd("~/2019 Diabetes")

Diabetes <- read_csv("diabetes.csv")

Diabetes <- Diabetes %>% mutate(Outcome = as.factor(Outcome))

set.seed(57974)

RF_Fit <- rand_forest(mode = "classification", mtry = 5, trees = 100) %>% set_engine("randomForest") %>% fit(Outcome ~ ., data = Diabetes)

And explaining the model cases, with iBreakdown

Explainer_Rf_Diabetes <- DALEX::explain(RF_Fit, data = Diabetes[, 1:8], Y = Diabetes == "1")

person1 <- Diabetes %>% slice(1) %>% select(-Outcome)

Cp_Rf1 <- break_down(Explainer_Rf_Diabetes, new_observation = person1)

plot(Cp_Rf1)

pbiecek commented 5 years ago

In the following chunk

Explainer_Rf_Diabetes <- DALEX::explain(RF_Fit,
data = Diabetes[, 1:8],
Y = Diabetes == "1")

it looks like there should be Diabetes$Outcome == "1"

GerardMeester commented 5 years ago

This sounds logical, I changed it to Diabetes$Outcome, but I still get this error.

NB: I forgot to copy library(parsnip) at the start of the code.

GerardMeester commented 5 years ago

Thanks Przemyslaw! I changed the code, also added "library(parsnip)" at the start, but I still get the same error.

library(tidyverse) library(DALEX) library(iBreakDown) library(randomForest) library(parsnip)

setwd("~/2019 Diabetes")

Diabetes <- read_csv("diabetes.csv")

Diabetes <- Diabetes %>% mutate(Outcome = as.factor(Outcome))

set.seed(57974)

RF_Fit <- rand_forest(mode = "classification", mtry = 5, trees = 100) %>% set_engine("randomForest") %>% fit(Outcome ~ ., data = Diabetes)

En nu uitleggen met iBreakdown

Explainer_Rf_Diabetes <- DALEX::explain(RF_Fit, data = Diabetes[, 1:8], Y = Diabetes$Outcome == "1")

person1 <- Diabetes %>% slice(1) %>% select(-Outcome)

Cp_Rf1 <- break_down(Explainer_Rf_Diabetes, new_observation = person1)

plot(Cp_Rf1)

pbiecek commented 5 years ago

I cannot reproduce this problem without data, but maybe try to use lowercase y as argument in explain (instead of Y)

I see that I need to work on better error handling in explain

GerardMeester commented 5 years ago

I hereby send you the diabetes dataset. Thanks for your swift answer!

I have tried using lower case y, instead of Y in the explain, but it did not help.

On Fri, 7 Jun 2019 at 06:57, Przemysław Biecek notifications@github.com wrote:

I cannot reproduce this problem without data, but maybe try to use lowercase y as argument in explain (instead of Y)

I see that I need to work on better error handling in explain

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ModelOriented/iBreakDown/issues/38?email_source=notifications&email_token=ADS42NUNYOKSEL2X6XTUQBLPZHTFBA5CNFSM4HU7KAK2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXE24SA#issuecomment-499756616, or mute the thread https://github.com/notifications/unsubscribe-auth/ADS42NRCWSQ24DXRPSKSTMTPZHTFBANCNFSM4HU7KAKQ .

pbiecek commented 4 years ago

after all these changes in iBreakDown, is this issue still unsolved?

GerardMeester commented 4 years ago

If I have the time, I will have a look at it today. I switched to shapley plots, because I ran in to many issues with iBreakDown, although your books and online articles are detailed and good.

On Sat, 15 Feb 2020 at 02:02, Przemysław Biecek notifications@github.com wrote:

after all these changes in iBreakDown, is this issue still unsolved?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ModelOriented/iBreakDown/issues/38?email_source=notifications&email_token=ADS42NWTDVWKVWZVJFQCN53RC45LJA5CNFSM4HU7KAK2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEL25ISY#issuecomment-586536011, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADS42NVIKJDXM3TWS655SXDRC45LJANCNFSM4HU7KAKQ .

-- Met vriendelijke groet,

Warm regards,

Gerard Meester

[T] +31 10 - 59 10 895 [M] +31 6 - 15 94 90 11

hbaniecki commented 4 years ago

Above code works for me. Session info:

R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=Polish_Poland.1250  LC_CTYPE=Polish_Poland.1250   
[3] LC_MONETARY=Polish_Poland.1250 LC_NUMERIC=C                  
[5] LC_TIME=Polish_Poland.1250    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] parsnip_0.0.5       randomForest_4.6-14 iBreakDown_1.1.0   
 [4] DALEX_1.0           forcats_0.4.0       stringr_1.4.0      
 [7] dplyr_0.8.5         purrr_0.3.3         readr_1.3.1        
[10] tidyr_1.0.2         tibble_2.1.3        ggplot2_3.3.0      
[13] tidyverse_1.2.1    

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4       cellranger_1.1.0 pillar_1.4.3     compiler_3.6.0  
 [5] tools_3.6.0      digest_0.6.25    jsonlite_1.6.1   lubridate_1.7.4 
 [9] lifecycle_0.2.0  gtable_0.3.0     nlme_3.1-139     lattice_0.20-38 
[13] pkgconfig_2.0.3  rlang_0.4.5      cli_2.0.2        rstudioapi_0.11 
[17] haven_2.1.1      withr_2.1.2      xml2_1.2.5       httr_1.4.1      
[21] generics_0.0.2   vctrs_0.2.4      hms_0.5.3        grid_3.6.0      
[25] tidyselect_1.0.0 glue_1.3.2       R6_2.4.1         fansi_0.4.1     
[29] readxl_1.3.1     farver_2.0.3     modelr_0.1.4     magrittr_1.5    
[33] scales_1.1.0     backports_1.1.5  rvest_0.3.3      assertthat_0.2.1
[37] colorspace_1.4-1 labeling_0.3     stringi_1.4.6    munsell_0.5.0   
[41] broom_0.5.2      crayon_1.3.4

Closing outdated issue.