Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

explain() seems to fail if X is a tibble (two different errors) #20

Closed
ltd-pa opened this issue Apr 8, 2021 · 1 comment
Closed

explain() seems to fail if X is a tibble (two different errors) #20

ltd-pa opened this issue Apr 8, 2021 · 1 comment

Comments

@ltd-pa
Copy link

ltd-pa commented Apr 8, 2021

Apologies in advance if this is related to the package configuration on my machine - but explain() seems to fail for me when 'X' is a tibble. Here is an example that is reproducible on my machine:

# Load required packages
library(fastshap)  # for fast (approximate) Shapley values
library(ranger)    # for fast random forest algorithm
library(dplyr)
library(tibble)

# Simulate training data
trn <- gen_friedman(200, seed = 101)
X <- subset(trn, select = -y)  # feature columns only

# Fit a random forest
set.seed(102)
rfo <- ranger(y ~ ., data =  trn)

# Prediction wrapper
pfun <- function(object, newdata) {
  predict(object, data = newdata)$predictions
}

# Succeeds when X is a dataframe
shap <- explain(rfo, X = X, pred_wrapper = pfun, nsim = 1)

# Fails when X is a tibble (with error "Subscript `O` is a matrix, the data `X[O]` must have size 1.")
shap <- explain(rfo, X = X %>% tibble(), pred_wrapper = pfun, nsim = 1)

# Still succeeds if we add a factor predictor
trn$fac_pred <- sample(c("A", "B", "C"), 200, TRUE) %>% factor()
X <- subset(trn, select = -y)  # feature columns only
set.seed(102)
rfo <- ranger(y ~ ., data =  trn)
shap <- explain(rfo, X = X, pred_wrapper = pfun, nsim = 1)

# But fails in a new way with tibbles (with error "Error: Can't combine `x1` <double> and `fac_pred` <factor<5eb8e>>.")
shap <- explain(rfo, X = X %>% tibble(), pred_wrapper = pfun, nsim = 1)

@bgreenwell
Copy link
Owner

Thanks @ltd-pa , should be fixed in 0.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants