sbw_weights: Fit stable balancing weights for a two-arm study (design...

View source: R/sbw_weights.R

sbw_weightsR Documentation

Fit stable balancing weights for a two-arm study (design stage)

Description

Formula-based front end to get_sbws_for_study(): balances the covariates named in balance (factors dummy-coded automatically) to the pooled-arm mean, exactly (imbalance tolerance fixed at zero), matching the paper's SBW specification. Intended to be called before unblinding, using only baseline covariates and the randomization assignment.

Usage

sbw_weights(balance, data, treatment)

Arguments

balance

One-sided formula naming the covariates to balance, e.g. ~ age + sex + bmi + region.

data

Data frame containing the balance covariates and treatment.

treatment

Treatment assignment: either a column name in data, unquoted (treatment = arm) or quoted (treatment = "arm"), or a vector. Accepts numeric 0/1, logical, or a two-level factor/character vector. For a factor, the second level is treated (coded 1); for a character vector, the second in alphabetical order. Use a factor with explicit levels to control this.

Value

An object of class sbw_fit with elements weights, n_clipped, max_abs_clipped (see get_sbws_for_study()), balance (the formula), treatment_name, treatment_levels, treatment (recoded 0/1), X (the balance design matrix), data, n, and call.

Examples

set.seed(1)
n = 100
trial_baseline = data.frame(
  age = rnorm(n, 50, 10),
  region = sample(c("N", "S"), n, replace = TRUE),
  arm = rbinom(n, 1, 0.5)
)
sbw = sbw_weights(~ age + region, data = trial_baseline, treatment = arm)
sbw
summary(sbw)

sbwadjust documentation built on Oct. 10, 2026, 5:08 p.m.