Compute SHAP Values for Spatial Transcriptomics Data
Source:R/compute_shap_spatial.R
compute_shap_spatial.Rd
This function computes SHAP (SHapley Additive exPlanations) values for spatial transcriptomics data using a predictive model trained on bulk RNA-seq data. It returns a long-format data frame with one row per sample-feature pair.
Arguments
- model
A fitted glmnet model object (e.g., from
cv.glmnet
) with named elements$model
and$lambda
.- X_bulk
A numeric matrix or data.frame of bulk RNA-seq gene expression (samples × features).
- y_bulk
A numeric vector of phenotype labels corresponding to
X_bulk
.- X_spatial
A numeric matrix or data.frame of spatial transcriptomics expression (samples × features).
Value
A data.frame of SHAP values for each sample-feature pair with columns:
feature
: Feature (gene) name.phi
: The SHAP value of that feature.sample
: The spatial sample identifier.feature.value
: Original feature expression value.
Details
Normalizes
X_bulk
by row-wise sum to ensure comparability across samples.Replaces
NA
andNaN
inX_spatial
with 0 to ensure model stability.Ensures feature alignment and column order consistency between bulk and spatial datasets.
Computes SHAP values using the
iml::Shapley
class for each spatial unit.
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)
} # }