auditor icon indicating copy to clipboard operation
auditor copied to clipboard

`plot_acf` error with >2 objects provided

Open michkam89 opened this issue 2 years ago • 1 comments

Hi there, I think I found a minor bug in plot_acf() in section creating a dataframe for ggplot.

When providing 3 objects to plot the error occurs:

Error in `f()`:
! Insufficient values in manual scale. 3 needed but only 2 provided.

I think this is connected with the fact that labels column is character not factor and later colours <- rev(theme_drwhy_colors(nlevels(df$label))) returns only 2 colors and the scale is manual and there are three labels. nlevels() returns zero.

Here's my reprex:

library(recipes)
library(workflows)
library(parsnip)
library(DALEX)
library(DALEXtra)

data <- DALEX::titanic_imputed

data$survived <- as.factor(data$survived)

rec <- recipe(survived ~ ., data = data) %>%
  step_normalize(fare)

model_rpart <- decision_tree(tree_depth = 25) %>%
  set_engine("rpart") %>%
  set_mode("classification")

model_lr <- logistic_reg() %>%
  set_engine("glm") |>
  set_mode("classification")

model_svm_ker <- svm_linear() |>
  set_engine("kernlab") |>
  set_mode("classification")

my_models <- list(
  rpart = model_rpart, linear = model_lr, svm = model_svm_ker
)

wflows <- my_models |>
  purrr::map(~ workflow() %>%
        add_recipe(rec) %>%
        add_model(.x))

models_fitted <- wflows |> purrr::map(~ fit(.x, data = data))
#>  Setting default kernel parameters
labels = list(rpart = "rpart", linear = "glm", svm = "svm linear")

explainers <- models_fitted |>
  purrr::map2(
    labels,
    ~ DALEXtra::explain_tidymodels(
      .x,
      data = titanic_imputed,
      y = titanic_imputed$survived,
      label = .y
))
#> Preparation of a new explainer is initiated
#>   -> model label       :  rpart 
#>   -> data              :  2207  rows  8  cols 
#>   -> target variable   :  2207  values 
#>   -> predict function  :  yhat.workflow  will be used (  default  )
#>   -> predicted values  :  No value for predict function target column. (  default  )
#>   -> model_info        :  package tidymodels , ver. 0.1.4 , task classification (  default  ) 
#>   -> predicted values  :  numerical, min =  0.05555556 , mean =  0.3221568 , max =  0.9267399  
#>   -> residual function :  difference between y and yhat (  default  )
#>   -> residuals         :  numerical, min =  -0.9267399 , mean =  1.858834e-17 , max =  0.9444444  
#>   A new explainer has been created!  
#> Preparation of a new explainer is initiated
#>   -> model label       :  glm 
#>   -> data              :  2207  rows  8  cols 
#>   -> target variable   :  2207  values 
#>   -> predict function  :  yhat.workflow  will be used (  default  )
#>   -> predicted values  :  No value for predict function target column. (  default  )
#>   -> model_info        :  package tidymodels , ver. 0.1.4 , task classification (  default  ) 
#>   -> predicted values  :  numerical, min =  0.008128381 , mean =  0.3221568 , max =  0.9731431  
#>   -> residual function :  difference between y and yhat (  default  )
#>   -> residuals         :  numerical, min =  -0.9628583 , mean =  -2.569726e-10 , max =  0.9663346  
#>   A new explainer has been created!  
#> Preparation of a new explainer is initiated
#>   -> model label       :  svm linear 
#>   -> data              :  2207  rows  8  cols 
#>   -> target variable   :  2207  values 
#>   -> predict function  :  yhat.workflow  will be used (  default  )
#>   -> predicted values  :  No value for predict function target column. (  default  )
#>   -> model_info        :  package tidymodels , ver. 0.1.4 , task classification (  default  ) 
#>   -> predicted values  :  numerical, min =  0.1870084 , mean =  0.3221698 , max =  0.7237496  
#>   -> residual function :  difference between y and yhat (  default  )
#>   -> residuals         :  numerical, min =  -0.7236129 , mean =  -1.301819e-05 , max =  0.8128629  
#>   A new explainer has been created!


# debuging auditor
mrs <- purrr::map(explainers, ~ auditor::model_residual(.x))
do.call(auditor::plot_acf, args = unname(mrs))
#> Error in `f()`:
#> ! Insufficient values in manual scale. 3 needed but only 2 provided.

michkam89 avatar Apr 28 '22 06:04 michkam89

btw I already have fix for this, I will propose a PR if you wish.

michkam89 avatar Apr 28 '22 06:04 michkam89