Skip to contents

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).

Usage

SpaPheno_SHAP_waterfall_plot(shap_df, sample_id, top_n = 10)

Arguments

shap_df

A data.frame of SHAP results. Must include: sample, feature, and phi columns.

sample_id

A string. The ID of the sample to visualize (must match sample in shap_df).

top_n

Integer; number of top features to include. Default is 10.

Value

A ggplot2 object displaying the SHAP waterfall plot.

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.

Author

Bin Duan

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)

} # }