Open snvv opened 4 days ago
The custom predict function must have a signature of function(object, newdata, type)
. This is the R standard for predict functions. By conforming to the R standard, the ale
package works automatically for many model types; only those (like randomForestSRC
) that don't conform to the standard need custom predict functions to bring them into conformity.
So, the corrected custom predict function is simply
pred_fun <- function(object, newdata, type = pred_type) {
as.numeric(predict(object, newdata)$predicted)
}
In fact, the error message pointed to this: Please refer to the help documentation for 'ale' on how to create a custom predict function for non-standard models.
If you consult help(ale) you will find
The requirements for this custom function are:
* It must take three required arguments and nothing else:
* object: a model
* newdata: a dataframe or compatible table type
* type: a string; it should usually be specified as type = pred_type
These argument names are according to the R convention for the generic stats::predict function.
One little note: the pred_type = 'raw'
in the ale()
call is unnecessary as it does nothing.
Here is the full corrected code:
library(randomForestSRC)
library(ale)
mod1 <- rfsrc(mpg ~ ., data = mtcars, importance = TRUE)
pred_fun <- function(object, newdata, type = pred_type) {
as.numeric(predict(object, newdata)$predicted)
}
rf_ale <- ale(
mtcars, mod1,
y_col = "mpg",
pred_fun = pred_fun,
parallel = 14
)
rf_ale$plots$wt
Created on 2024-09-25 with reprex v2.1.1
Hello, I am encountering an issue with my custom function for
randomForestSRC
. Below is a representative example of the code I am working with:My custom prediction function is as follows:
However, I am receiving the following error:
I have attempted several variations but have not been able to resolve the issue.
Any assistance would be greatly appreciated.
Thank you!