R/RcppExports.R

Defines functions mat2list comb_names comb_index choosecpp sri_mat shuffle_checkerboard shuffle_rowwise

Documented in mat2list shuffle_checkerboard shuffle_rowwise sri_mat

# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#' Randomize entries in matrix while keeping row sums fixed
#'
#' This is a utility function and as such probably not of much interest for a
#' typical user.
#'
#' @param inmat numeric or integer matrix, 0/1 matrix
#' @return a randomized matrix with the same dimensions as \code{inmat}, where
#' the sum of values within a row remains constant
#' @details The returned matrix will not have column and row names even if the
#'          input matrix had them.
#' @author Christof Neumann
#' @examples
#' xdata <- matrix(ncol = 8, nrow = 5)
#' xdata[] <- sample(c(0, 1), length(xdata), TRUE)
#' res <- testingtravis:::shuffle_rowwise(xdata)
#' # row sums are constant
#' rowSums(xdata) == rowSums(res)
#' # column sums are not (although some may be, by chance)
#' colSums(xdata) == colSums(res)
#' @useDynLib testingtravis
shuffle_rowwise <- function(inmat) {
    .Call(`_testingtravis_shuffle_rowwise`, inmat)
}

#' Randomize entries in matrix while keeping row and column sums fixed
#'
#' This is a utility function and as such probably not of much interest for a
#' typical user.
#'
#' @param inmat numeric or integer matrix, 0/1 matrix
#' @param swaps numeric, the number of (attempted/trial) swaps to perform
#'        (default is \code{1})
#' @param checkerboards_only logical, should the random selection of trial
#'        matrices to be swapped be constrained to checkerboards (default is
#'        \code{TRUE})
#' @return a randomized matrix with the same dimensions as \code{inmat}, where
#' row and column sums are constant
#' @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 \code{checkerboards_only =
#'          TRUE}, then the random selection is constrained to existing
#'          checkerboards. In other words, if \code{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 \code{swaps}. One rule of thumb
#'          is to set it to double the number of 1's in the matrix (i.e.
#'          \code{sum(inmat) * 2}).
#'
#'          The returned matrix will not have column and row names even if the
#'          input matrix had them.
#' @author Christof Neumann
#' @examples
#' 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)
shuffle_checkerboard <- function(inmat, swaps = 1L, checkerboards_only = TRUE) {
    .Call(`_testingtravis_shuffle_checkerboard`, inmat, swaps, checkerboards_only)
}

#' Simple ratio index
#'
#' This is a utility function and as such probably not of much interest for a
#' typical user.
#'
#' @param inmat integer matrix, rows represent observations/events and columns
#'        individuals/units
#' @return a square matrix with simple ratio index for pairs of individuals /
#'         units
#' @author Christof Neumann
sri_mat <- function(inmat) {
    .Call(`_testingtravis_sri_mat`, inmat)
}

choosecpp <- function(n, k) {
    .Call(`_testingtravis_choosecpp`, n, k)
}

comb_index <- function(n_units, comb) {
    .Call(`_testingtravis_comb_index`, n_units, comb)
}

comb_names <- function(elements, comb) {
    .Call(`_testingtravis_comb_names`, elements, comb)
}

#' Turn matrix into list
#'
#' @param inmat numeric or integer matrix, 0/1 matrix. Must have column names!
#' @return a list where each item represents a row in \code{inmat} and is a
#'         character vector with the column names for which a row value is 1
#' @details The function will fail if the input matrix does not have column
#'          names or if it is a not a \code{matrix}, but for example a
#'          \code{data.frame}.
#' @author Christof Neumann
#' @export
mat2list <- function(inmat) {
    .Call(`_testingtravis_mat2list`, inmat)
}
gobbios/testingtravis documentation built on July 26, 2020, 7:22 a.m.