Skip to contents

Adjusts a table A so its row and column sums match prescribed targets while preserving cross-product ratios (the RAS/IPF algorithm) – TDA's mpfit. A must be non-negative with strictly positive row and column sums, as must the target row_sums/col_sums, and their totals should agree (TDA warns but does not refuse otherwise).

Usage

tda_mpfit(
  A,
  row_sums,
  col_sums,
  max_iter = 100,
  eps = 1e-06,
  dir = tempfile("tda")
)

Arguments

A

a non-negative numeric matrix (n x m).

row_sums

target row sums, length n.

col_sums

target column sums, length m.

max_iter

maximum number of iterations.

eps

convergence tolerance on the maximum deviation between the fitted and target sums.

dir

working directory.

Value

A list: B, the fitted table; iterations, how many were used; accuracy, the final deviation reached.

Examples

tda_mpfit(matrix(c(10, 20, 30, 40), 2, 2, byrow = TRUE),
         row_sums = c(45, 55), col_sums = c(40, 60))
#> $B
#>          [,1]     [,2]
#> [1,] 15.61072 29.38928
#> [2,] 24.38928 30.61072
#> 
#> $iterations
#> [1] 5
#> 
#> $accuracy
#> [1] 1.21766e-08
#>