NorskRegnesentral / shapr

Explaining the output of machine learning models with more accurately estimated Shapley values
https://norskregnesentral.github.io/shapr/
Other
138 stars 32 forks source link

Error for glm and gam models #301

Closed JensWahl closed 1 year ago

JensWahl commented 1 year ago

On devel branch using a glm model gives an error due to being of class glm and lm.

library(data.table)
library(shapr)
# Data 
n = 100
beta = c(-0.5, 0.5)
x_train = data.table(matrix(rnorm(n*2), ncol = 2))
dt = data.table(y=y, x_train)
x_test = x_train[1:10, ]
y = rpois(n, exp(as.matrix(x_train) %*% beta))
p = mean(y)

# Model
model = glm(y ~ ., data = dt, family = poisson)

explanation <- explain(
  x_train,
  x_test,
  model = model,
  approach = "empirical",
  prediction_zero = p,
)

Gives the error message

Error in get(paste0("get_model_specs.", model_class0)) : 
  first argument has length > 1
In addition: Warning message:
In if (native_func_available) { :
  the condition has length > 1 and only the first element will be used

which comes from this line in get_feature_specs:

https://github.com/NorskRegnesentral/shapr/blob/f3081da8ec0436489b373f122d00dce7d907c426/R/model_setup_R.R#L27

as a glm model is of class lm and glm.

This will happen with GAMs as well, because they are in addition of class gam.