Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shap plots #5

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
S3method(print,mlcov_data)
export(MLCovSearch)
export(generate_residualsplots)
export(generate_shap_summary_plot)
import(SHAPforxgboost)
import(ggplot2)
import(xgboost)
70 changes: 29 additions & 41 deletions R/cov_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,60 +199,48 @@ MLCovSearch <- function(tab, list_pop_param, cov_continuous, cov_factors, seed =
}
}

# Initialize an empty list to store the SHAP summary plots
shap_plots <- list()

# Create a function to generate SHAP summary plots
generate_shap_summary_plot <- function(xgb_model, X_train, param_name) {
shap_values <- SHAPforxgboost::shap.values(xgb_model = xgb_model, X_train = X_train)
shap_long <- SHAPforxgboost::shap.prep(xgb_model = xgb_model, X_train = X_train)
p <- SHAPforxgboost::shap.plot.summary(shap_long)
p <- p + ggplot2::ggtitle(param_name)

return(p)
}
# Initialize an empty list to store the SHAP summary data and seed information
shap_data <- list()
shap_seed <- list()

# Interpretation of Selected covariates Beeswarm Plots
for (i in list_pop_param) {
y_xgb <- log(dat_XGB[, i])

if (is.na(result_ML[i, 1]) == FALSE){
list_cov <- strsplit(gsub(" ", "", result_ML[i, 1]), ",")
x.selected_final <- as.matrix(dat_XGB %>% dplyr::select(dplyr::all_of(list_cov[[1]])))

if (length(list_cov[[1]]) != 0 ) {
xgb.mod_final <- xgboost::xgboost(
data = x.selected_final,
label = y_xgb,
nrounds = 200,
objective = "reg:squarederror",
verbose = 0
)

# Generate SHAP summary plot for the current parameter
shap_plot <- generate_shap_summary_plot(
xgb_model = xgb.mod_final,
X_train = x.selected_final,
param_name = i
)
shap_plots[[i]] <- shap_plot
}

if (is.na(result_ML[i, 1]) == FALSE) {
list_cov <- strsplit(gsub(" ", "", result_ML[i, 1]), ",")
x.selected_final <-
as.matrix(dat_XGB %>% dplyr::select(dplyr::all_of(list_cov[[1]])))

if (length(list_cov[[1]]) != 0) {
xgb.mod_final <- xgboost::xgboost(
data = x.selected_final,
label = y_xgb,
nrounds = 200,
objective = "reg:squarederror",
verbose = 0
)

# Generate SHAP summary plot for the current parameter
shap_values <- SHAPforxgboost::shap.values(xgb_model = xgb.mod_final, X_train = x.selected_final)
shap_long <- SHAPforxgboost::shap.prep(xgb_model = xgb.mod_final, X_train = x.selected_final)

# Store shap data and seed
shap_data[[i]] <- list(shap_values = shap_values, shap_long = shap_long)
shap_seed[[i]] <- .Random.seed

}
}
}

combined_plots <- gridExtra::marrangeGrob(grobs = shap_plots,nrow = length(shap_plots),ncol = 1)




# Return the result_ML table and the SHAP plots for each parameter
return(
list(
result_ML = result_ML,
result_5folds = result_5folds,
shap_plots = shap_plots,
list_pop_param = list_pop_param,
dat_XGB = dat_XGB
shap_data = shap_data,
shap_seed = shap_seed
) %>% structure(class = "mlcov_data")
)
}
Expand Down
71 changes: 71 additions & 0 deletions R/generate_shap_summary_plot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#' Generate SHAP Summary Plots
#'
#' This function generates SHAP summary plots for the XGBoost model.
#' @inheritParams SHAPforxgboost::shap.plot.summary
#' @param data A list containing required data frames and results.
#' @param title A character string to customize the title
#' @param title.position A numeric value from 0.0-1.0 to adjust the alignment of the title
#' @param ylab A character string to customize the y-axis
#' @param xlab A character string to customize the x-axis
#' @return A list of ggplot objects, each representing a SHAP summary plot for a different parameter.
#'
#' @examples
#' # Assuming 'data' is a list with necessary components
#' \dontrun{
#' plots <- generate_shap_summary_plot(data, title = "Custom Title", y.inter = 0.25, ...)
#' }
#'
#' @import xgboost
#' @import ggplot2
#' @import SHAPforxgboost
#'
#' @export
#'
generate_shap_summary_plot <- function(data,
x_bound = NULL,
dilute = FALSE,
scientific = FALSE,
my_format = NULL,
min_color_bound = "#FFCC33",
max_color_bound = "#6600CC",
kind = c("sina", "bar"),
title = NULL,
title.position = 0,
ylab = NULL,
xlab = NULL)
{
# Initialize an empty list to store the SHAP summary plots
shap_plots <- list()

for (i in data$list_pop_param) {
if (!is.null(data$shap_data[[i]])) {
# Set to the seed used when making the data
set.seed(data$shap_seed[[i]])

shap_values <- data$shap_data[[i]]$shap_values
shap_long <- data$shap_data[[i]]$shap_long

# Generate summary plot
p <-
SHAPforxgboost::shap.plot.summary(
shap_long,
x_bound = x_bound,
dilute = dilute,
scientific = scientific,
my_format = my_format,
min_color_bound = min_color_bound,
max_color_bound = max_color_bound,
kind = kind
)
p <- p +
ggplot2::ggtitle(ifelse(is.null(title), i, title)) +
ggplot2::labs(y = ifelse(is.null(ylab), "SHAP value (impact on model output)", ylab),
x = ifelse(is.null(xlab), "", xlab)) +
ggplot2::theme(plot.title = ggplot2::element_text(hjust = title.position))
shap_plots[[i]] <- p
}

}

return(shap_plots)
}
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ print(result)

```

Generate SHAP plots:

```
generate_shap_summary_plot(
result,
x_bound = NULL,
dilute = FALSE,
scientific = FALSE,
my_format = NULL,
title = NULL,
title.position = 0.5,
ylab = NULL,
xlab = NULL)
```

Generate residual plots:

Cl
Expand Down
70 changes: 70 additions & 0 deletions man/generate_shap_summary_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading