shuffle_checkerboard: Randomize entries in matrix while keeping row and column sums...

Description Usage Arguments Details Value Author(s) Examples

View source: R/RcppExports.R

Description

This is a utility function and as such probably not of much interest for a typical user.

Usage

1
shuffle_checkerboard(inmat, swaps = 1L, checkerboards_only = TRUE)

Arguments

inmat

numeric or integer matrix, 0/1 matrix

swaps

numeric, the number of (attempted/trial) swaps to perform (default is 1)

checkerboards_only

logical, should the random selection of trial matrices to be swapped be constrained to checkerboards (default is TRUE)

Details

The algorithm starts by selecting a random submatrix with 2 rows and 2 columns (trial matrix). An actual swap is performed if this selected submatrix is a checkerboard. If checkerboards_only = TRUE, then the random selection is constrained to existing checkerboards. In other words, if checkerboards_only = FALSE a lot of swaps will not result in an actual swap and hence the final matrix will be 'less' randomized.

There is no accepted value to set swaps. One rule of thumb is to set it to double the number of 1's in the matrix (i.e. sum(inmat) * 2).

The returned matrix will not have column and row names even if the input matrix had them.

Value

a randomized matrix with the same dimensions as inmat, where row and column sums are constant

Author(s)

Christof Neumann

Examples

1
2
3
4
5
6
7
xdata <- matrix(ncol = 8, nrow = 5)
xdata[] <- sample(c(0, 1), length(xdata), TRUE)
res <- testingtravis:::shuffle_checkerboard(xdata, swaps = sum(xdata) * 2)
# row sums are constant
rowSums(xdata) == rowSums(res)
# column sums are constant too
colSums(xdata) == colSums(res)

gobbios/testingtravis documentation built on July 26, 2020, 7:22 a.m.