marginTable: Compute margin of a table faster

marginTableR Documentation

Compute margin of a table faster

Description

Computes the margin of a contingency table given as an array, by summing out over the dimensions not specified.

Usage

marginTable(x, margin = NULL, order = TRUE) 
marginMatrix(x, margin, dim = NULL, incols = FALSE, order = FALSE)

Arguments

x

a numeric array

margin

integer vector giving margin to be calculated (1 for rows, etc.)

order

logical - should indices of output be ordered as in the vector margin? Defaults to TRUE for marginTable, FALSE for marginMatrix.

dim

Integer vector containing dimensions of variables. Assumed all binary if not specified.

incols

Logical specifying whether not the distributions are stored as the columns in the matrix; assumed to be rows by default.

Details

With order = TRUE this is the same as the base function margin.table(), but faster.

With order = FALSE the function is even faster, but the indices in the margin are returned in their original order, regardless of the way they are specified in margin.

propTable() returns a renormalized contingency table whose entries sum to 1. It is equivalent to prop.table(), but faster.

Value

The relevant marginal table. The class of x is copied to the output table, except in the summation case.

Note

Original functions are margin.table and prop.table.

Examples


m <- matrix(1:4, 2)
marginTable(m, 1)
marginTable(m, 2)

propTable(m, 2)

# 3-way example
m <- array(1:8, rep(2,3))
marginTable(m, c(2,3))
marginTable(m, c(3,2))
marginTable(m, c(3,2), order=FALSE)

#' set.seed(2314)
# set of 10 2x2x2 probability distributions
x = rdirichlet(10, rep(1,8))

marginMatrix(x, c(1,3))
marginMatrix(t(x), c(1,3), incols=TRUE)


rje documentation built on Nov. 12, 2022, 9:06 a.m.