SHAP Waterfall Plot for a Single Sample
Source:R/SpaPheno_SHAP_waterfall_plot.R
SpaPheno_SHAP_waterfall_plot.Rd
Generate a SHAP waterfall plot to visualize the most influential features contributing to a model's prediction for a given sample. Features are ordered by the magnitude of their SHAP values, and color-coded by direction (positive/negative).
Details
- Filters SHAP values for the selected sample_id
.
- Selects top top_n
features based on absolute SHAP value.
- Plots bars horizontally using ggplot2
, colored by contribution sign.
Examples
if (FALSE) { # \dontrun{
data("osmFISH_metadata_cellType")
data("osmFISH_bulk_decon")
data("osmFISH_bulk_pheno")
data("osmFISH_bulk_coordinate")
PhenoResult <- SpatialPhenoMap(
bulk_decon = osmFISH_bulk_decon,
bulk_pheno = osmFISH_bulk_pheno,
family = "binomial",
coord = osmFISH_bulk_coordinate,
resolution = "single_cell",
sample_information_cellType = osmFISH_metadata_cellType
)
pred_result <- PhenoResult$pred_score
phenoPlus <- row.names(pred_result[pred_result$label %in% "phenotype+", ])
model <- PhenoResult$model
X <- as.data.frame(PhenoResult$cell_type_distribution[phenoPlus, ])
# This step took a very long time
shap_test_plus <- compute_shap_spatial(
model = model,
X_bulk = as.data.frame(osmFISH_bulk_decon),
y_bulk = osmFISH_bulk_pheno,
X_spatial = X)
SpaPheno_SHAP_waterfall_plot(shap_test_plus, sample_id = "cell_3477", top_n = 10)
} # }