Performs bin-wise spatial resampling to preserve local spatial structure while breaking cross-type coordination. This is used for permutation testing in CoPro to generate null distributions that account for spatial autocorrelation.
Arguments
- location_data
Data frame with columns: x, y, cell_ID. Optionally can include pre-computed x_bin, y_bin columns.
- num_bins_x
Number of bins in the x direction (default: 10). Larger values preserve more local structure but may have sparse bins.
- num_bins_y
Number of bins in the y direction (default: 10).
- match_quantile
Logical. If TRUE, matches cells between original and target tiles based on their relative (quantile) positions within each tile. This better preserves within-tile spatial structure. Default: FALSE.
Value
Data frame with resampled cell assignments. The x, y coordinates remain fixed, but cell_ID is shuffled according to the bin-wise resampling.
Details
The algorithm:
Divides the spatial domain into a grid of bins
Creates a random mapping (shuffle) between bins
For each original bin, samples cells from the mapped target bin
If target bin has insufficient cells, expands to neighboring bins
When match_quantile = TRUE, step 3 uses quantile-based matching instead of
random sampling. This matches cells based on their relative x/y positions
within each tile (using rank quantiles), better preserving within-tile
spatial autocorrelation structure.
This preserves spatial autocorrelation within cell types while breaking the cross-type coordination that would indicate true co-progression.
Examples
if (FALSE) { # \dontrun{
# Create example data
loc_data <- data.frame(
x = runif(100, 0, 10),
y = runif(100, 0, 10),
cell_ID = paste0("cell_", 1:100)
)
# Check bin distribution first
diagnose_bin_distribution(loc_data, num_bins_x = 5, num_bins_y = 5)
# Perform spatial resampling (random within tile)
resampled <- resample_spatial(loc_data, num_bins_x = 5, num_bins_y = 5)
# Perform spatial resampling (quantile-matched within tile)
resampled_matched <- resample_spatial(loc_data, num_bins_x = 5, num_bins_y = 5,
match_quantile = TRUE)
} # }