I am trying to use lime to add ML explanations in the output of a package I am developing. My solution uses a gradient boosting model from library gbm. This type of model is not supported by lime as is, so I would need to add a gbm method to generics: -model_type -predict_model
if I do this manually, running my code line by line, I have no problem.
However, once lime is included in a function and I try to run it, it
looks like the function cannot assign the "gbm" method. .
Would anybody have an idea about how to approach this?
I have also tried using setMethod, but then I would get a message saying the environment is locked
I train my model outside the package function:
lime_model<-gbm(train_target ~. ,
data = train,
distribution="bernoulli",
n.trees = N_trees,
interaction.depth = Int.depth,
shrinkage=Learn_rate,
n.minobsinnode=Min.obs.node)
then pass it to the function:
explanation<-
lime_gbm(train_data=train,test_data=test,model=lime_model,Conf,
n_trees=N_trees)
defined as:
#' @param train_data a dataframe containing the data to train the model on
#' @param test_data a dataframe containing the data to test test the model on
#' @param Conf a config file to extract general variable names
#' @param n_trees number of trees in the model
#' @param model the model to be explained
#'
#' @import dplyr
#' @import lime
#'
#' @return list
#' @export
lime_gbm<-function(train_data,test_data,model,Conf, n_trees){
if(!is.data.frame(train_data) | !is.data.frame(test_data)){
stop("data must be a data.frame")
}
if(!is.numeric(n_trees)){
stop("model parameters must be numeric")
}
if(!class(model)=="gbm"){
stop("model must be of class gbm")
}
fit.gbm=model
#create model type for gbm
model_type.gbm <- function(x,...){
return("classification")
}
#setMethod("model_type","gbm",function(x,...) "classification")
predict_model.gbm <- function(x, newdata, type="prob",...) {
res <- predict.gbm(x,newdata = newdata, type="response",
n.trees=n_trees)
res <-as.data.frame(res)
colnames(res)<-"predictions"#c("Yes","No")
return(res)
}
#setMethod("predict_model","gbm",function(x,...) as.data.frame(x))
explainer <- lime::lime(train_data,
fit.gbm,
bin_continous=T,
quantile_bins=F)
explanation <- lime::explain(test_data,
explainer,
n.trees=n_trees,
n_labels=1,
#model_type="classification",
n_features=ncol(test_data),
n_permutations = 10,#500,
feature_select='lasso_path',
dist_fun="manhattan")
return(explanation)
}
The code as is gives the error message: "Error: The class of model must have a model_type method. See ? model_type to get an overview of models supported out of the box"
or, if I remove ".gbm" when assigning the method and remove # in front of
#setMethod()
:
Error in setMethod("model_type", "gbm", function(x, ...) "classification") : the environment ‘SiteViewModelling’ is locked; cannot assign methods for function ‘model_type’