Categorical generalization of the binary 2x2 swap algorithm to
matrices of categorical data. This function is a convenience wrapper
around nullcat() with method = "swapcat".
Usage
swapcat(
x,
n_iter = 1000L,
output = c("category", "index"),
swaps = "auto",
row_groups = NULL,
col_groups = NULL,
wt_row = NULL,
wt_col = NULL,
seed = NULL
)Arguments
- x
A matrix of categorical data, encoded as integers. Values should represent category or stratum membership for each cell.
- n_iter
Number of iterations. Default is 1000. Larger values yield more thorough mixing. Ignored for non-sequential methods. Minimum burn-in times can be estimated with
suggest_n_iter(). Whenrow_groupsorcol_groupsis used,n_iteriterations are run separately on every block. The number needed can be more or fewer than for the full matrix, so estimate it by passing the same groups tosuggest_n_iter().- output
Character indicating type of result to return:
"category"(default) returns randomized matrix"index"returns an index matrix describing where original entries (a.k.a. "tokens") moved. Useful mainly for testing, and for applications likequantize()that care about token tracking in addition to generic integer categories.
- swaps
Character string controlling the direction of token movement. Only used when method is
"curvecat","swapcat", or"tswapcat". Affects the result only whenoutput = "index", otherwise it only affects computation speed. Options include:"vertical": Tokens move between rows (stay within columns)."
horizontal": Tokens move between columns (stay within rows)."alternating": Tokens move in both dimensions, alternating between vertical and horizontal swaps. Provides full 2D mixing without preserving either row or column token sets."auto"(default): Foroutput = "category", automatically selects the fastest option based on matrix dimensions. Foroutput = "index", defaults to"alternating"for full mixing.
- row_groups, col_groups
Optional vectors assigning each row (for
row_groups) or column (forcol_groups) ofxto a group. Labels can be integers, characters, or factor levels. When supplied, the matrix is split into blocks (every combination of a row group and a column group) and each block is randomized independently withmethod, so values are only ever exchanged within a block. For algorithms that fix row and column margins, this additionally fixes the category counts of every row group within each column (and every column group within each row): for example, grouping sites into regions gives a null model in which each region keeps its own species pool. Rows or columns that are alone in their group cannot be changed by fixed-margin algorithms. To randomize within a different grouping in each replicate (e.g. random spatial tilings fromtile_groups()), pass a groups matrix tonullcat_batch()orquantize_batch().- wt_row, wt_col
Deprecated. Experimental pairwise weight matrices previously used to bias which pairs of rows or columns exchange values. But weighting pair selection changes only how quickly a randomization mixes, not the null distribution it converges to; the only lasting effect of a weight matrix is to prevent exchanges between rows (or columns) that are not connected, directly or through intermediaries, by nonzero weights. Weight matrices are therefore now converted to
row_groups/col_groupsgiven by the connected components of their nonzero entries, with a warning. Userow_groups/col_groupsinstead.- seed
Integer used to seed random number generator, for reproducibility.
Value
A matrix of the same dimensions as x, either randomized
categorical values (when output = "category") or an integer index
matrix describing the permutation of entries (when output = "index").
Details
The swapcat algorithm attempts random 2x2 swaps of the form:
where \(a\) and \(b\) are distinct categories. These swaps
preserve the multiset of categories in each row and column.
With only two categories present, swapcat() reduces to the
behavior of the standard binary swap algorithm.
References
Gotelli, N. J. (2000). Null model analysis of species co-occurrence patterns. Ecology, 81(9), 2606–2621.
See also
curvecat() for an algorithm that produces equivalent results with
better computational efficiency.
