# Load the necessary libraries library(shapviz) library(ggplot2) library(xgboost) # Read Data dt <- read.csv(file.choose(), row.names = 1) View(dt) head(dt) # Data Preparation x <- dt[, 1:15] y <- dt[, 16] # Convert data to the DMatrix format required by XGBoost dtrain <- xgb.DMatrix(data.matrix(x), label = y) # Train the XGBoost model fit <- xgb.train( params = list(learning_rate = 0.1, objective = "binary:logistic"), data = dtrain, nrounds = 65L ) # Generate SHAP explanation object shp <- shapviz(fit, X_pred = data.matrix(x), X = x) # Generate a standard SHAP feature importance plot sv_importance(shp) + theme_bw() + theme( panel.grid.major = element_blank(), panel.grid.minor = element_blank() ) # Generate SHAP feature importance plots in the form of bar charts sv_importance(shp, kind = "bar") + theme_bw() + theme( panel.grid.major = element_blank(), panel.grid.minor = element_blank() ) # Generate SHAP feature importance plots in the form of swarm plots and apply custom color schemes sv_importance(shp, kind = "beeswarm") + theme_bw() + scale_color_gradientn(colors = c("#560B6A", "#FBB91E")) + theme( legend.title = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line() )