Skip to contents

Adjusts weights so that within each calibration variable the weighted sums match the supplied marginal targets. margins is a named list keyed by variable name; each entry is a named numeric vector mapping category values (as strings) to target totals.

Usage

morie_weights_rake(
  weights,
  df,
  margins,
  max_iter = 100,
  tol = 1e-06,
  bounds = NULL
)

Arguments

weights

Initial numeric weights (length n).

df

data.frame containing the calibration variables.

margins

Named list of named numeric vectors.

max_iter

Maximum IPF iterations (default 100).

tol

Convergence tolerance on max relative adjustment (default 1e-6).

bounds

Optional c(lo, hi) to clip the per-iteration multiplier.

Value

list with weights, converged, iterations, max_adjustment, diagnostics (from morie_weights_diagnostics).

Examples

set.seed(1)
df <- data.frame(g = rep(c("m", "f"), 15), r = rep(c("x", "y", "z"), 10))
res <- morie_weights_rake(rep(1, 30), df,
                          list(g = c(m = 50, f = 50), r = c(x = 30, y = 40, z = 30)))
res$converged
#> [1] TRUE