Error: ' "what" must be a function or character string ' with XGBoost
- Dominant language
- R
- Stars
- 503
- Forks
- 87
- PR merge metrics
- No merged PRs in 30d
Description
Hi
I have fitted an XGBoost model by transforming a data frame (with both features and target feature) to a dgCMatrix using the sparse.model.matrix function from the "Matrix" package. (cvd_incident is the target feature, complete_train_men is the data frame).
`complete_train_men_sparse = sparse.model.matrix(cvd_incident ~., data = complete_train_men)[,-1]
`
I recieve an error message when trying to create an ALE-plot using the FeatureEffect$new function. I tried following the tip for the pred.function in #29 with some modifications, as my model "boostmod_men" is trained on a dgCMatrix instead of an xgb.DMatrix.
```
pred.fun = function(model, newdata){
previous_na_action <- options('na.action') #store the current na.action
options(na.action='na.pass') #change the na.action
newData_x = sparse.model.matrix(cvd_incident ~., data=newdata)[,-1]
options(na.action=previous_na_action$na.action) #reset the na.action
results <- predict(model, newData_x)
return(results)
}
```
To verify that the function works correctly, I try using it to predict the test set and compare with previously computed values in 'pred_test_men':
```
test<- pred.fun(model = boostmod_men, newdata = complete_test_men)
head(test)
head(pred_test_men)
#the two prints the same values for the first 5 observations in test set, so it works fine
```
Now to where the problem arises:
```
mod <- Predictor$new(model= boostmod_men, data = complete_train_men, pred.fun(boostmod_men, complete_train_men))
eff = FeatureEffect$new(mod, feature = "age")
```
The first line runs without issues, but the second one gives the following error message
> Error in do.call(predict.fun, list(model, newdata = newdata)) :
> 'what' must be a function or character string
Do you know what causes this, and how to fix it?
Contributor guide
Research direction
Start by reading the Predictor$new and FeatureEffect$new entry points and compare the arguments passed in the report with the documented prediction-function API. Reproduce the error using the supplied sparse.model.matrix and pred.fun examples, then verify the expected function argument and confirm that FeatureEffect$new completes without the do.call error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100