I would like to create something similar to the below plot (from Python) here.
The Python code is the following:
import xgboost
import shap
# load JS visualization code to notebook
shap.initjs()
# train XGBoost model
X,y = shap.datasets.boston()
model = xgboost.train({"learning_rate": 0.01}, xgboost.DMatrix(X, label=y), 100)
# explain the model's predictions using SHAP
# (same syntax works for LightGBM, CatBoost, scikit-learn and spark models)
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X)
It plots a single observations prediction based on Shapley values.
I think I have managed to re-create up to this point in R using:
library(MASS)
library(xgboost)
library(SHAPforxgboost)
data(Boston)
Boston
X_train <- Boston %>%
dplyr::select(-crim) %>%
as.matrix()
Y_train <- Boston %>%
dplyr::select(crim) %>%
as.matrix()
dtrain <- xgb.DMatrix(data = X_train, label = Y_train)
params <- list(
"eta" = 0.3,
"max_depth" = 6,
"colsample_bytree" = 1,
"min_child_weight" = 3,
"subsample"= 0.8,
"objective"="reg:linear",
"gamma" = 1,
"lambda" = 1,
"alpha" = 0,
"max_delta_step" = 0,
"colsample_bylevel" = 1,
"eval_metric"= "rmse",
"set.seed" = 176
)
watchlist <- list("train" = dtrain)
nround = 50
xgb.model <- xgb.train(params, dtrain, nround, watchlist)
shapValues <- shap.values(xgb_model = xgb.model, X_train = X_train)
shapForceData <- X_train %>%
data.frame(shapValues$shap_score)
singleShapObs <- data.frame(shapForceData[1, ]) # want to plot this data
Now I have a singleShapObs which contains the "real" values and the "Shapley" values (the variables which contain the .1 are the Shaply values. How can I plot something similar to the above Python example?
Note: This leads on from a previous question I had answered here. In that solution each geom_col is an observation. I basically want to extract one of those geom_cols and represent it in a similar fashion to the Python Shap package. (does not have to be 100% exactly the same as the Python plot)
