Skip to content

Error in mutate() when using forcats::fct_reorder() in eSHAP_plot() #1

@zhonghua723

Description

@zhonghua723

Hi, When running the eSHAP_plot() function, I encounter an error in the mutate() step where forcats::fct_reorder(feature, mean_phi) fails. The error message indicates that lvls_reorder() expects idx to contain one integer for each level of f, but this condition is not met.

rlang::last_trace()
<error/dplyr:::mutate_error>
Error in `mutate()`:In argument: `feature = forcats::fct_reorder(feature,
  mean_phi)`.
Caused by error in `lvls_reorder()`:
! `idx` must contain one integer for each level of `f`
---
Backtrace:1. ├─explainer::eSHAP_plot(...)
  2. │ └─... %>% ggplot(aes(x = feature, y = Phi, color = f_val))
  3. ├─ggplot2::ggplot(., aes(x = feature, y = Phi, color = f_val))
  4. ├─dplyr::mutate(., feature = forcats::fct_reorder(feature, mean_phi))
  5. ├─dplyr:::mutate.data.frame(., feature = forcats::fct_reorder(feature, mean_phi))
  6. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
  7. │   ├─base::withCallingHandlers(...)
  8. │   └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
  9. │     └─mask$eval_all_mutate(quo)
 10. │       └─dplyr (local) eval()
 11. └─forcats::fct_reorder(feature, mean_phi)
 12.   └─forcats::lvls_reorder(f, order(summary, decreasing = .desc))
  • code
mydata <- read.csv('train.csv')
library(tidyverse)
mydata <- mydata %>%
  mutate(across(where(is.character), as.factor)) %>%
  mutate(across(where(is.numeric), as.numeric)) %>%
  mutate(Disease = as.factor(Disease))
# Create a classification task
maintask <- mlr3::TaskClassif$new(
  id = "my_classification_task",
  backend = mydata,
  target = 'Disease',
  positive = '1'
)

# Split the dataset for training
splits <- mlr3::partition(maintask)

# Create a ranger learner for classification
mylrn <- mlr3::lrn("classif.ranger", predict_type = "prob")

# Train the learner on the training set
mylrn$train(maintask, splits$train)

# Generate SHAP values and plot
SHAP_output <- eSHAP_plot(
  task = maintask,
  trained_model = mylrn,
  splits = splits,
  sample.size = 30,
  seed = seed
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions