Learn R Programming

bartXViz (version 1.0.8)

plot.ExplainBART: A Function for Visualizing the Shapley Values of BART Models

Description

The plot.ExplainBART function provides various visualization methods for Shapley values. It is designed to visualize ExplainBART class objects, which contain Shapley values computed from models estimated using the bart function from the dbarts or the wbart/gbart functions from BART. The values and format used in the graph are determined based on the input parameters.

Usage

# S3 method for ExplainBART
plot(
  x,
  average = NULL,
  type = NULL,
  num_post = NULL,
  plot.flag = TRUE,
  adjust = FALSE,
  probs = 0.95,
  title = NULL,
  ...
)

Value

The plot is returned based on the specified option.:

out

If average is "obs" or "post", a bar plot or summary plot is generated based on the selected averaging criterion. If average is `"both"`, a boxplot is displayed to show the distribution of Shapley values computed using both criteria. If adjust is TRUE, the adjusted Shapley values are displayed. If num_post is specified, a bar plot or summary plot for the selected posterior sample is generated.

Arguments

x

An ExplainBART class object containing the Shapley values of the BART model.

average

Input the reference value for calculating the mean of the object's phi list. "obs" represents abind the average based on observations (#post by #variable), while "post" represents the average based on posterior samples (#obs by #variable). If "both" is entered, calculations are performed based on both observation and posterior sample criteria. If no value is specified, "both" is used as the default.

type

"bar" represents a bar chart that includes the average contribution of each variable, while "bee" represents a summary plot, allowing you to determine the graph's format.

num_post

To check the contribution of variables for a single posterior sample, enter a value within the number of posterior samples.

plot.flag

If average = "obs", the quantile interval of each variable's is provided by default.

adjust

The default value is FALSE. Enter TRUE to check the Shapley values adjusted based on the model's average contribution.

probs

Enter the probability for the quantile interval. The default value is 0.95.

title

The title of the plot, with a default value of NULL.

...

Additional arguments to be passed

Examples

Run this code
# \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