# \donttest{
## 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 dbarts
model <- dbarts::bart (X,y, keeptrees = TRUE, ndpost = 200)
# prediction wrapper function
pfun <- function (object, newdata) {
predict(object, newdata)
}
# Calculate shapley values
model_exp <- Explain ( model, X = X, pred_wrapper = pfun )
# Distribution of Shapley values (boxplot)
# computed based on observation and posterior sample criteria
plot(model_exp,average = "both" )
# Barplot based on observation criteria
plot(model_exp,average = "obs",type ="bar",probs = 0.95)
# Barplot based on posterior sample
plot(model_exp,average = "post",type ="bar" )
# Summary plot based on posterior sample
plot(model_exp,average = "post",type ="bees" )
# Summary plot of the 100th posterior sample
plot(model_exp,average = "post",type ="bees",num_post = 100)
# Barplot of the adjusted baseline
plot(model_exp, type ="bar", adjust= TRUE )
# }
Run the code above in your browser using DataLab