Learn R Programming

bartXViz (version 1.0.8)

decision_plot: Decision Plot

Description

The decision_plot function is a graph that visualizes how individual features contribute to a model's prediction for a specific observation using Shapley values. It can be used to visualize one or multiple observations.

Usage

decision_plot(
  object,
  obs_num = NULL,
  title = NULL,
  geo.unit = NULL,
  geo.id = NULL,
  bar_default = TRUE
)

Value

plot_out

The decision plot for one or multiple observations specified in obs_num.

Arguments

object

Enter the name of the object that contains the model's contributions and results obtained using the Explain function.

obs_num

single or multiple observation numbers

title

plot title

geo.unit

The name of the stratum variable in the BARP model as a character.

geo.id

Enter a single value of the stratum variable as a character.

bar_default

bar_default is an option for adjusting the legend's color scale to fit the window length, and its default value is set to TRUE. If plots fail to render in LaTeX documents, it is recommended to set this option to FALSE.

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)

## BART model
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 )

# Single observation 
decision_plot(model_exp, obs_num=1 )

#Multiple observation 
decision_plot(model_exp, obs_num=10:40 )
# }

Run the code above in your browser using DataLab