#training random forest model
set.seed(8431)
cvforest.train <- train(x = train[, c(3:122, 124:138)], y = train[, 123],
method = "cforest", metric = "RMSE",
trControl = trainControl(method = "cv", number = 5),
controls = cforest_unbiased(ntree = 1000, minsplit = 5, minbucket = 5)
)
cvforest.model.train<-cvforest.train$finalModel #final model object
#calculating shapley values
X <- train[, c(3:122, 124:138)]
y <- train[, 123]
predictor <- iml::Predictor$new(cvforest.model.train, data = X, y = y)
shapley_vals <- iml::Shapley$new(predictor)
Using the code above, I fit a random forest model using the caret and party packages in R. Now I want to calculate Shapley values for this model. However, when I create the 'shapley_vals' object using the code above, I find the 'results' and 'fit' options to be NULL. I'm not sure what steps I'm missing. Any advice on what to do next?
It's difficult to help without example data. Here's an example of how to do it using the
irisdataset:The
x.interestargument here specifies the instance for which you want to compute the Shapley values. These results are for the first instance in the test dataset, but you can change this to any instance. Let me know if this helps.