0

I'm trying to use the "lime" package to interpret a Random Forest model with the "import85" dataset, but when I run the explain command it returns an error:

library(lime)
library(caret)

data("imports85", package = "randomForest")

imp85 <- imports85[,-2] 
imp85 <- imp85[complete.cases(imp85), ] 
imp85[] <- lapply(imp85, function(x) if (is.factor(x)) x[, drop=TRUE] else x)
stopifnot(require(randomForest)) 

NROW(imp85)*0.7
idx <- sample(1:NROW(imp85),135)
test <- imp85[idx, c(1:4, 6:25)]
train <- imp85[-idx, c(1:4, 6:25)]
resp <- imp85[[5]][-idx]

model <- train(train, resp, method = 'rf')
explainer <- lime(train, model)

explanation <- explain(test, explainer, n_labels = 1, n_features = 2) 

Error in predict.randomForest(modelFit, newdata, type = "prob") :
Type of predictors in new data do not match that of the training data.

How can I solve it?

EDIT 1: I tried to force the factor variable levels to be the same for both train and test datasets, but it doesn't work

0 Answers0