Search code examples
rimlr-ale

What is causing iml's FeatureEffects to throw an error on a caret trained model?


I'm trying to use the iml package in TIBCO's R (TERR 6.1.0) and I am getting an error from FeatureEffects$new that I cannot make sense of. I am training a random forest using caret, then I am squeezing the model into a Predictor object as required by iml. And finally I'm trying to compute FeatureEffects and it throws this error:

Error in diag(dists.cumulated) : Can only replace the diagonal of a matrix

I have a short code snippet from the documentation below and my version of it beneath. The only difference is that I am using caret to train my model but this is explicitly mentioned as supported in the documentation. Does anyone have experience with caret and iml and has a clue what is going on? Needless to say that the code from the documentation throws no such error...

Code snippet from iml documentation:

library("rpart")
data("Boston", package = "MASS")
rf <- rpart(medv ~ ., data = Boston)
# Squeeze the model into a Predictor object
mod <- Predictor$new(rf, data = Boston)
# Compute the accumulated local effects for all features
eff <- FeatureEffects$new(mod, method="ale")

Adopted code snippet using my caret based model:

# Train a random forest using caret:
mtry <- 1:max(1,floor(ncol(Boston)*0.9))
tunegrid <- expand.grid(.mtry = mtry)
cntrl<-trainControl(method = "oob", number=5, search='grid', verboseIter=TRUE, savePredictions=TRUE)
rf.model <- caret::train(Boston[, names(Boston) != "medv", drop=FALSE], Boston[, names(Boston) == "medv"], method="rf", ntree=250, metric="RMSE", trControl=cntrl, tuneGrid=tunegrid, nodesize=floor(nrow(Boston)*0.8), importance=TRUE)
# Squeeze the model into a Predictor object
mod <- Predictor$new(rf.model, data=Boston)
# Compute ale effects for all features
eff <- FeatureEffects$new(mod, method="ale")

Solution

  • This problem occurs only when using TIBCO's Enterprise Runtime for R (TERR) or as it is recently called "Spotfire Enterprise Runtime for R version 6.1.0". I did not pay attention when posting this question and accidentally used TERR inside RStudio w/o noticing it. Apologies!

    When testing the corrections I used open source R 4.3.0 and it is running just fine. I will leave the question in here for the benefit of other TERR users but I will correct the question to not say R but TERR.