Calibration weights via iterative proportional fitting (raking)
Source:R/sampling.R
morie_calibration_weights.RdAdjusts initial design weights so that weighted marginal totals match known population totals for each auxiliary variable.
Usage
morie_calibration_weights(
df,
aux_vars,
population_totals,
initial_weights = NULL,
max_iter = 50L,
tol = 1e-06
)Arguments
- df
A data frame.
- aux_vars
Character vector of categorical auxiliary variable names.
- population_totals
Named list:
"var_level"-> population count. Keys should be"varname_level"(e.g."gender_female").- initial_weights
Optional numeric vector of starting weights.
- max_iter
Maximum IPF iterations.
- tol
Convergence tolerance.
Examples
set.seed(1)
df <- data.frame(
region = sample(c("A", "B"), 100, TRUE),
sex = sample(c("M", "F"), 100, TRUE)
)
totals <- list(region_A = 60, region_B = 40, sex_M = 55, sex_F = 45)
morie_calibration_weights(df,
aux_vars = c("region", "sex"),
population_totals = totals
)
#> [1] 1.1634999 0.7424349 1.1634999 1.2830401 0.8187142 1.2830401 1.2830401
#> [8] 1.1634999 0.8187142 0.8187142 1.2830401 1.2830401 1.2830401 1.1634999
#> [15] 1.1634999 0.7424349 0.7424349 0.8187142 0.7424349 1.1634999 1.1634999
#> [22] 1.1634999 1.2830401 1.2830401 1.1634999 1.2830401 0.8187142 1.1634999
#> [29] 1.1634999 0.8187142 0.8187142 0.8187142 1.1634999 0.8187142 1.1634999
#> [36] 1.2830401 0.7424349 1.1634999 0.7424349 0.8187142 0.7424349 0.8187142
#> [43] 1.2830401 0.7424349 0.8187142 0.7424349 0.7424349 0.7424349 1.2830401
#> [50] 1.2830401 0.7424349 1.2830401 0.8187142 0.8187142 1.2830401 1.1634999
#> [57] 0.7424349 0.7424349 0.7424349 1.1634999 1.2830401 0.8187142 0.8187142
#> [64] 0.7424349 0.8187142 0.8187142 0.7424349 1.2830401 0.8187142 0.7424349
#> [71] 0.8187142 0.8187142 1.1634999 1.2830401 1.2830401 0.7424349 0.7424349
#> [78] 1.1634999 1.1634999 0.8187142 0.7424349 0.8187142 1.2830401 1.2830401
#> [85] 1.2830401 0.8187142 1.1634999 0.8187142 1.1634999 0.8187142 1.2830401
#> [92] 1.1634999 0.8187142 0.7424349 1.1634999 1.1634999 1.2830401 0.7424349
#> [99] 0.8187142 1.2830401