View source: R/explain_bartMachine.R
Explain.bartMachine | R Documentation |
bartMachine
This function is used to calculate the contribution of each variable
in the Bayesian Additive Regression Trees (BART) model using permutation.
It is used to compute the Shapley values of models estimated
using the bartMachine
function from the bartMachine.
## S3 method for class 'bartMachine'
Explain(
object,
feature_names = NULL,
X = NULL,
nsim = 1,
pred_wrapper = NULL,
newdata = NULL,
parallel = FALSE,
...
)
object |
A BART model (Bayesian Additive Regression Tree) estimated
using the |
feature_names |
The name of the variable for which you want to check the contribution.
The default value is set to |
X |
The dataset containing all independent variables used as input when estimating the BART model. Categorical or character variables must not contain an underscore ("_") in their values or labels. |
nsim |
The number of Monte Carlo repetitions used for estimating each Shapley value is set to |
pred_wrapper |
A function used to estimate the predicted values of the model. |
newdata |
New data containing the variables included in the model.
This is used when checking the contribution of newly input data using the model.
The default value is set to |
parallel |
The default value is set to |
... |
Additional arguments to be passed |
An object of class ExplainbartMachine
with the following components :
phis |
A list containing the Shapley values for each variable. |
newdata |
The data used to check the contribution of variables. If a variable has categories, categorical variables are one-hot encoded. |
fnull |
The expected value of the model's predictions. |
fx |
The prediction value for each observation. |
factor_names |
The name of the categorical variable. If the data contains only continuous or dummy variables, it is set to |
## Friedman data
set.seed(2025)
n <- 200
p <- 5
X <- data.frame(matrix(runif(n * p), ncol = p))
y <- 10 * sin(pi* X[ ,1] * X[,2]) +20 * (X[,3] -.5)^2 + 10 * X[ ,4] + 5 * X[,5] + rnorm(n)
## Using the bartMachine
model <- bartMachine::bartMachine(X, y, seed = 2025, num_iterations_after_burn_in =200 )
## prediction wrapper function
pfun <- function (object, newdata) {
bartMachine::bart_machine_get_posterior(object,newdata) $ y_hat_posterior_samples
}
## Calculate shapley values
model_exp <- Explain ( model, X = X, pred_wrapper = pfun )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.