balance_matrix {cuadramelo}R Documentation

Balance matrix

Description

Balances a matrix so that the columns and/or rows add up to a certain vector.

Usage

balance_matrix(Y, col_totals = NULL, row_totals = NULL, allow_negative = TRUE)

Arguments

Y

Matrix to be balanced.

col_totals

(optional) Desired sum of columns.

row_totals

(optional) Desired sum of rows.

allow_negative

Are negative entries in the balanced matrix allowed?

Details

Balancing is done according to the criteria of minimum sum of squares.

If neither col_totals nor row_totals is given, the same matrix will be returned. If only one of them is given, only that axis will be balanced.

Value

The balanced matrix.

Examples

set.seed(2)
Y <- rnorm(3*5) |> matrix(3,5) |> round(3)
v <- c( 0.876, -1.078, 3.452, 0.261, 1.349)
h <- c(-1.851, 0.243, 6.468)
X1 <- balance_matrix(Y, v, h)
Y
X1
h
rowSums(X1)
v
colSums(X1)
X3 <- balance_matrix(Y, col_totals = v)
v
colSums(X3)
X4 <- balance_matrix(Y, row_totals = h)
h
rowSums(X4)

[Package cuadramelo version 1.0.0 Index]