Learn R Programming

SurveyStat (version 1.0.3)

rake_weights: Rake survey weights to match population targets

Description

This function implements simple raking (iterative proportional fitting) to adjust survey weights to match known population marginal totals. Assumes two-dimensional raking for simplicity.

Usage

rake_weights(data, population_targets, weight_col = "weight")

Value

A data.frame with raked weights

Arguments

data

A data.frame containing survey data

population_targets

Named list with population totals for each variable

weight_col

Character string specifying initial weight column name

Examples

Run this code
# Assuming we have gender and education population totals
targets <- list(
  gender = c(Male = 1000000, Female = 1050000),
  education = c(HighSchool = 800000, Bachelor = 900000, Graduate = 350000)
)
data <- data.frame(
  gender = c("Male", "Female", "Male", "Female", "Male"), 
  education = c("HighSchool", "Bachelor", "Bachelor", "HighSchool", "Graduate"),
  weight = c(1, 1, 1, 1, 1)
)
raked_data <- rake_weights(data, targets, "weight")

Run the code above in your browser using DataLab