giuseppec / iml

iml: interpretable machine learning R package
https://giuseppec.github.io/iml/
Other
491 stars 88 forks source link

Bug in handing over `type` to `predict.fun`? #97

Open ugroempi opened 4 years ago

ugroempi commented 4 years ago

Hi Christoph,

I may have stumbled on a bug: the documentation for Predictor claims that the type is handed to predict.fun, if both are specified. However, I am trying to use a predict.fun (for a random forest from the randomForest package) with a type argument that does not have a default, and I get the error message

Error in (function (object, newdata, type) : argument "type" is missing, with no default

If I use a default, this default is always used, i.e. changing the type argument in Predictor$new does not change the prediction behavior. I have to hardcode different predictor functions for different types in order to get them to work with iml.

Best, Ulrike

christophM commented 4 years ago

Thanks for reporting this issue. Could you add a small reproducible example?

ugroempi commented 4 years ago

Here is a small example that illustrates the issue (assuming iml and randomForest are loaded).

set.seed(71)
iris.rf <- randomForest(Species ~ ., data=iris)
predictfun <-
  function(model, newdata, type="prob"){
    ### probability for second class
  hilf <- predict(model, newdata, type="prob")[,2]
  if (type=="prob") return(hilf)
  if (type=="logit") {
    hilf <- log(hilf/(1-hilf))
    hilf[hilf==Inf] <- 999
    hilf[hilf==-Inf] <- -999
    return(hilf)
  }
  else return(predict(model, newdata, type=type))
}
predictfun(iris.rf, iris, type="logit")
model <- Predictor$new(iris.rf, iris, predict.fun = predictfun, type="logit")
model$predict(iris)