regrake

regrake provides an interface for regularized raking in R.

This more general formulation of the weighting problem, following Barratt et al. (2021)'s approach, enables more flexible functional forms in adherence to population targets, meaningful regularization, and ultimately more expressive and efficient survey weights.

Installation

# From r-universe (recommended — includes pre-built binaries)
install.packages("regrake", repos = "https://andytimm.r-universe.dev")

# Or from GitHub
remotes::install_github("andytimm/regrake")

Quick start

library(regrake)

set.seed(604)
n <- 500

sample_data <- data.frame(
  sex = sample(c("F", "M"), n, replace = TRUE, prob = c(0.55, 0.45)),
  age_group = sample(c("18-34", "35-54", "55+"), n, replace = TRUE,
                     prob = c(0.40, 0.35, 0.25)),
  income = rnorm(n, mean = 58000, sd = 14000)
)

# autumn-style target table: variable / level / target
pop_targets <- data.frame(
  variable = c("sex", "sex", "age_group", "age_group", "age_group", "income"),
  level = c("F", "M", "18-34", "35-54", "55+", "mean"),
  target = c(0.51, 0.49, 0.30, 0.40, 0.30, 62000)
)

fit <- regrake(
  data = sample_data,
  formula = ~ rr_exact(sex + age_group) + rr_mean(income),
  population_data = pop_targets,
  pop_type = "proportions",
  regularizer = "entropy",
  bounds = c(0.3, 3)
)

fit
head(fit$weights)
fit$balance

Formula interface

Constraint helpers include:

Variables sharing a constraint type can be combined with + inside the wrapper (for example rr_exact(sex + age_group)). Interactions use : (for example rr_l2(sex:age_group)).

Population target formats

regrake() supports:

Output

A fitted object contains:

For more

Status

1.0.0 released. Available on r-universe.

License

Apache License 2.0.