weigh: Weigh survey participants

View source: R/weigh.R

weighR Documentation

Weigh survey participants

Description

Applies weights to participants in a contact_survey object. Weights are always multiplied into an existing weight column (or one is created with value 1), making multiple calls composable.

The behaviour depends on the combination of arguments:

target = NULL

Numeric column: multiply weight by column values directly.

Unnamed target + groups

Map column values to groups, assign target[g] / n_in_group per participant.

Named target

Names match column values, assign target[val] / n_with_val per participant.

Data frame target

Post-stratify against population data (expanded to single-year ages via pop_age()).

Usage

weigh(survey, by, target = NULL, groups = NULL, ...)

Arguments

survey

a survey() object (must have been processed by assign_age_groups() if using data frame target)

by

column name in the participant data to weigh by

target

target weights: NULL for direct numeric weighting, an unnamed numeric vector (with groups), a named numeric vector, or a data frame with columns lower.age.limit and population

groups

a list of value sets mapping column values to groups (used with unnamed target vector); must be the same length as target

...

further arguments passed to pop_age() when target is a data frame

Value

the survey object with updated participant weights

Examples

data(polymod)
# Direct numeric weighting
if ("survey_weight" %in% names(polymod$participants)) {
  polymod |> weigh("survey_weight")
}

# Dayofweek weighting with groups (POLYMOD uses 0 = Sunday, 6 = Saturday)
polymod |>
  weigh("dayofweek", target = c(5, 2), groups = list(1:5, c(0, 6)))


socialmixr documentation built on April 29, 2026, 9:07 a.m.