Each pass rescales within one margin, which breaks the previous margin, which is why this iterates rather than solving once. It converges when the margins are consistent (a common total) and no level with a positive target is empty in the sample; both conditions are checked rather than left to diverge silently.
Details
Formula: repeat over margins A, B, ...:
w_i <- w_i m^A_h / sum_{j in h} w_j for every level h, until
every margin matches its target.
References
Deming, W. E. & Stephan, F. F. (1940). On a least squares adjustment of a sampled frequency table when the expected marginal totals are known. Annals of Mathematical Statistics 11(4):427-444. doi:10.1214/aoms/1177731829 .
Examples
y <- c(10, 12, 9, 15, 11, 14)
w <- rep(1, 6)
sex <- c("f", "m", "f", "m", "f", "m"); age <- c("y", "y", "o", "o", "y", "o")
margins <- list(list(labels = sex, targets = c(f = 300, m = 300)),
list(labels = age, targets = c(y = 250, o = 350)))
r <- Raklng(y, w, margins)
c(r$estimate, r$max_margin_error)
#> [1] 1.186462e+01 4.547474e-13
