Generate Toroidal Shift Permutation Indices
Source:R/C_resampling_function.R
generate_toroidal_permutations.RdShifts spatial coordinates in a toroidal (wrap-around) manner, perfectly preserving spatial autocorrelation structure. This is useful for permutation testing when you want to break cross-type coordination while preserving within-type spatial patterns.
Value
Matrix of permutation indices (n_cells x n_permu). Each column contains a permutation of row indices that can be used to reorder cells.
Details
The toroidal shift works by:
Applying a random shift to all coordinates (wrapping at boundaries)
Matching cells based on their new positions to original positions
This preserves ALL spatial autocorrelation within each cell type because the relative positions of cells are unchanged - only their absolute positions are shifted.
Examples
if (FALSE) { # \dontrun{
# Create example location data
loc_data <- data.frame(
x = runif(100, 0, 10),
y = runif(100, 0, 10),
cell_ID = paste0("cell_", 1:100)
)
# Generate 100 toroidal permutations
perm_matrix <- generate_toroidal_permutations(loc_data, n_permu = 100)
# Apply first permutation
permuted_cells <- loc_data$cell_ID[perm_matrix[, 1]]
} # }