dandls / counterfactuals

counterfactuals: An R package for Counterfactual Explanation Methods
https://dandls.github.io/counterfactuals/
GNU Lesser General Public License v3.0
21 stars 4 forks source link

CounterfactualMethod printer broken? #37

Closed mb706 closed 4 months ago

mb706 commented 4 months ago

Following the steps from the intro vignette, but then printing the "CounterfactualMethod" object:

data(german, package = "rchallenge")
credit = german[, c("duration", "amount", "purpose", "age", "employment_duration", "housing", "number_credits", "credit_risk")]

set.seed(20210816)
rf = randomForest::randomForest(credit_risk ~ ., data = credit[-998L,])

predictor = iml::Predictor$new(rf, type = "prob")
x_interest = credit[998L, ]

moc_classif = MOCClassif$new(
  predictor, epsilon = 0, fixed_features = c("age", "employment_duration"),
  quiet = TRUE, termination_crit = "genstag", n_generations = 10L
)

moc_classif

gives the output

Counterfactual explanation method:  MOCClassif 
Parameters:
 - epsilon:  0 
 - fixed_features:  age employment_duration 
 - init_strategy:  icecurve 
 - k:  1 
 - lower:  
 - max_changed:  
 - mu:  20 
 - termination_crit:  genstag 
 - n_generations:  10 
 - p_mut:  0.73 
 - p_mut_gen:  0.5 
 - p_mut_use_orig:  0.4 
 - p_rec:  0.71 
 - p_rec_gen:  0.62 
 - upper:  - weights:  
andreash0 commented 4 months ago

Hi @mb706, thanks for the observations!

Regarding your points:

null_to_char <- function(x) {
  if (is.null(x)) {
    cat("NULL")
  } else {
    cat(x)
  }
}
mb706 commented 4 months ago

If the value is only ever a scalar or NULL you can also do cat(x %??% "NULL"), using %??% from checkmate.

I don't think you need a helper function here, but if you want to implement one, I would not write a function that outputs a value (using cat), but instead just converts to a string, e.g. to_string <- function(x) x %??% "NULL". It could also do other things, like paste(x, collapse = ", "), if it should be necessary somewhere.