Skip to contents

Rescale a non-negative weight vector so that its total is approximately scale, then round to integers for use with fitting routines that require integer case weights.

Usage

as_caseweights(w, scale = 10000L)

Arguments

w

A numeric vector of non-negative weights.

scale

Target total for the rescaled integer case weights.

Value

An integer vector of case weights on the same scale as w.

Examples

as_caseweights(c(1, 2, 3), scale = 60L)
#> [1] 10 20 30
as_caseweights(c(0.2, 0.3, 0.5))
#> [1] 2000 3000 5000

data(kenya, package = "ineqTrees")

kenya_model_data <- kenya[
  stats::complete.cases(kenya[, "wealth", drop = FALSE]),
  "wealth",
  drop = FALSE
]

set.seed(20260512)
kenya_model_data <- kenya_model_data[
  sample.int(nrow(kenya_model_data), 800L),
  ,
  drop = FALSE
]

wealth_quintile <- cut(
  kenya_model_data$wealth,
  breaks = stats::quantile(
    kenya_model_data$wealth,
    probs = seq(0, 1, by = 0.2),
    na.rm = TRUE
  ),
  include.lowest = TRUE
)

quintile_weights <- as.numeric(table(wealth_quintile))
as_caseweights(quintile_weights, scale = 1000L)
#> [1] 200 200 200 200 200