3

So I am trying to use LIME to understand predictions from a logit model in R. I know I don't 'need' to, but I am trying to illustrate what it does with a model that one can simply understand as a starting point for a presentation.

But I am having troubles getting this working. I am sure it is due to the model.predict aspect, but my few solutions have not worked.

Basically here is what I would like to do:

model.logit <- glm(formula = formula, data = build.dat, family = binomial(link = "logit"))


train.x <- build.dat[ , all.vars(formula[[3]])] 
test.x <- reject.dat[1:100, all.vars(formula[[3]])]

explainer <- lime(train.x, as_classifier(model.logit ), n_bins = 20, quantile_bins = TRUE)

explain.me <- lime::explain(test.x[2 , ], explainer, n_labels = 1, n_features = 8, n_permutations = 5000, 
                        feature_select = "forward_selection", type = "response" )

Now I get the error

Error in match.arg(type) :'arg' should be one of “link”, “response”, “terms”

But moving my 'type = "response"' within the 'lime' code does not fix it.

And I have tried creating a function 'predict_model.glm' with what I thought might correct this due to what I thought was going on when I was using randomForest and got it working:

predict_model.glm <- function(x, newdata, type = "response" ) {
res <- as.data.frame(c(predict(x, newdata = newdata, type = type), 1-predict(x, newdata = newdata, type = type)))

}

But this only seemed to add errors.

I am sure this is due to my missing what exactly the 'lime' aspect is looking for (thus my failure at correcting this with the 'predict_model.glm' function), but I cannot seem to find clarification anywhere.

Any help would be great, Thanks!

J Econ
  • 51
  • 1
  • So an update. I found this error was resolved if I directly called 'type = "response" ' inside the function 'predict_model.glm – J Econ Aug 14 '18 at 17:32
  • However, now the internally used glm.fit function does not seem to like my predictions and I get: – J Econ Aug 14 '18 at 17:32
  • Error in glm.fit(x = x_fit, y = y, weights = weights, family = gaussian()) : NAs in V(mu) . I have tried addressing this by changing the format of the output in the data frame within 'predict_model.glm' but cannot seem to fix this. And I do have a 'model_type.glm <- function(x,...) 'classification' call in there – J Econ Aug 14 '18 at 17:34

1 Answers1

1

You'll have to transform the output of predict within predict_model.glm. As a first step, I'd recomment printing type and one first row of the result of calling predict. Depending on the type passed in, the call to glm and the returned result will be different - ?predict_model hints at it: for 'raw', it's a single res, for 'prob', it's probabilities (or for really linear model: the numeric result).

Overall, as far as I (hopefully) understand your case, a function similar to that one might get you one step forward:

predict_model.glm <- function(x, newdata, type, ...) { 
  print(type)
  res <- predict(x, newdata);
  print(res[1])
  data.frame(Response = res)
}