This journal includes LIME examples with a logistic regression model fit to the sine data.

library(caret)
library(cowplot)
library(lime)
library(limeaid)

Data

Prepare the sine data

# Prepare training and testing data
x_train = sine_data_train[c("x1", "x2", "x3")]
y_train = factor(sine_data_train$y)
x_test = sine_data_test[1:5, c("x1", "x2", "x3")]

Logistic Regression Models

Main Effects Only

Logistic regression model: y ~ x1 + x2 + x3

# Fit the model
lrm <- caret::train(
  as.factor(y) ~ x1 + x2 + x3,
  preProcess = "scale",
  data = sine_data_train,
  method = "glm",
  family = "binomial"
)

lrm$finalModel
## 
## Call:  NULL
## 
## Coefficients:
## (Intercept)           x1           x2           x3  
##     -0.1533      -2.2070       1.8365      -0.1240  
## 
## Degrees of Freedom: 499 Total (i.e. Null);  496 Residual
## Null Deviance:       692.9 
## Residual Deviance: 335.8     AIC: 343.8

All Interactions

Logistic regression model: y ~ x1 * x2 * x3

# Fit the model
lri <- caret::train(
  as.factor(y) ~ x1 * x2 * x3,
  preProcess = "scale",
  data = sine_data_train,
  method = "glm",
  family = "binomial"
)

lri$finalModel
## 
## Call:  NULL
## 
## Coefficients:
## (Intercept)           x1           x2           x3      `x1:x2`      `x1:x3`  
##    -0.25910     -2.25052      1.87303     -0.09646      0.24543      0.22155  
##     `x2:x3`   `x1:x2:x3`  
##    -0.17317      0.01177  
## 
## Degrees of Freedom: 499 Total (i.e. Null);  492 Residual
## Null Deviance:       692.9 
## Residual Deviance: 332.3     AIC: 348.3

Apply LIME

Apply LIME with 3 quantile bins, 4 quantile bins, and kernel density to both models

# Run apply_lime
resm <- apply_lime(
  train = x_train,
  test = x_test,
  model = lrm,
  label = "1",
  n_features = 2,
  sim_method = c('quantile_bins',
                 'kernel_density'),
  nbins = 3:4,
  return_perms = TRUE
)

# Run apply_lime
resi <- apply_lime(
  train = x_train,
  test = x_test,
  model = lri,
  label = "1",
  n_features = 2,
  sim_method = c('quantile_bins',
                 'kernel_density'),
  nbins = 3:4,
  return_perms = TRUE
)

Exlanation Visualizations

Plot the LIME explanation plot and my EOI plot for each prediction in the test data and each implementation of LIME

# Plot the explanation of interest
join_plots <- function(id, exp) {
  p1 <- lime::plot_features(exp$explain[id:(id+1),])
  p2 <- limeaid::plot_explain_scatter(exp$explain[id:(id+1),])
  plot_grid(p1, p2)
}

Main Effects Only

3 quantile bins

purrr::map(.x = seq(1, 9, 2), .f = join_plots, exp = resm)
## [[1]]

## 
## [[2]]

## 
## [[3]]

## 
## [[4]]

## 
## [[5]]

4 quantile bins

purrr::map(.x = seq(11, 19, 2), .f = join_plots, exp = resm)
## [[1]]

## 
## [[2]]

## 
## [[3]]

## 
## [[4]]

## 
## [[5]]

kernel density

purrr::map(.x = seq(21, 29, 2), .f = join_plots, exp = resm)
## [[1]]

## 
## [[2]]

## 
## [[3]]

## 
## [[4]]

## 
## [[5]]

All Interactions

3 quantile bins

purrr::map(.x = seq(1, 9, 2), .f = join_plots, exp = resi)
## [[1]]

## 
## [[2]]

## 
## [[3]]

## 
## [[4]]

## 
## [[5]]

4 quantile bins

purrr::map(.x = seq(11, 19, 2), .f = join_plots, exp = resi)
## [[1]]

## 
## [[2]]

## 
## [[3]]

## 
## [[4]]

## 
## [[5]]

kernel density

purrr::map(.x = seq(21, 29, 2), .f = join_plots, exp = resi)
## [[1]]

## 
## [[2]]

## 
## [[3]]

## 
## [[4]]

## 
## [[5]]