Integrated from simPIC (Chugh et al., 2024; bluster::neighborPurity). For each cell, evaluates the proportion of its k-nearest neighbors that share the same cluster or cell-type identity.
Value
A numeric vector of neighborhood purity scores (values from 0 to 1), with an attribute "mean_purity" containing the overall average.
Examples
data <- matrix(stats::rnorm(200), 20, 10)
cl <- factor(rep(c("A", "B"), each = 10))
dist_mat <- stats::dist(data)
calc_neighborhood_purity(data, cl)
#> [1] 0.4285714 0.4285714 0.6250000 0.7500000 1.0000000 0.5000000 1.0000000
#> [8] 0.6666667 0.6000000 0.4285714 0.6000000 0.5000000 0.7142857 0.6666667
#> [15] 0.8333333 0.6000000 0.8571429 0.4000000 0.2500000 0.6363636
#> attr(,"mean_purity")
#> [1] 0.6242587
