Introduction
When you collect survey data, your sample’s demographic distributions often differ from those of a target population (e.g., the U.S. general population). To correct for these differences, we apply survey weights so that analyses on the weighted data better reflect the intended population.
This vignette shows how to use the
srv_add_weights() function from
the g6tr.survey package to:
Use default population margins (e.g., from the U.S. Census).
Supply custom margins via a simple named list interface.
Apply raking to match marginal distributions.
Optionally trim extreme weights.
Inspect the resulting weights and weighting efficiency.
Installation
If you haven’t already
installed g6tr.survey, do so via:
install.packages("g6tr.survey")Data
For the use case presentation let us use the demographic data from
2020 March Newsletter data that is distributed within
g6tr.survey package.
library(dplyr)
data('ds_newsletter_new', package = 'g6tr.survey')
# Inspect the first few rows
head(ds_newsletter_new)
[38;5;246m# A tibble: 6 × 8
[39m
response_id s_age s_gender s_race s_hhincome s_region s_ethnicity s_generation
[3m
[38;5;246m<chr>
[39m
[23m
[3m
[38;5;246m<fct>
[39m
[23m
[3m
[38;5;246m<fct>
[39m
[23m
[3m
[38;5;246m<fct>
[39m
[23m
[3m
[38;5;246m<fct>
[39m
[23m
[3m
[38;5;246m<fct>
[39m
[23m
[3m
[38;5;246m<fct>
[39m
[23m
[3m
[38;5;246m<fct>
[39m
[23m
[38;5;250m1
[39m R_3pg4VsSRrUWP4fs 18-30 Male White alone $100,000 or more Northeast Not Hispanic or Latino Millennials
[38;5;250m2
[39m R_2b2kowiL0yFLFQU 31-45 Male White alone $100,000 or more West Not Hispanic or Latino Gen X
[38;5;250m3
[39m R_0IIq4pRUtMFFDKV 18-30 Female Black or African American alone Less than $25,000 West Not Hispanic or Latino Millennials
[38;5;250m4
[39m R_28RM3tVIFAuE81D 18-30 Female White alone $50,000 - less than $100,000 South Not Hispanic or Latino Gen Z
[38;5;250m5
[39m R_2qC02WrbuSzydyt 46-64 Male Black or African American alone $50,000 - less than $100,000 South Not Hispanic or Latino Gen X
[38;5;250m6
[39m R_W3tDgxNut4jFy5r 18-30 Female White alone $25,000 - less than $50,000 South Hispanic or Latino Millennials
Ensure that any demographic variables you weight on have no missing
values—srv_add_weights() will error if NAs
exist.
Using Default Population Margins
The package provides a default margins
object genpop_margins, containing U.S.
general population proportions for:
Gender (
s_gender)Age group (
s_age)Household income (
s_hhincome)Race (
s_race)Region (
s_region)Ethnicity (
s_ethnicity)
You can apply these directly:
genpop_margins
[1m
[33mError
[39m:
[22m
[33m!
[39m object 'genpop_margins' not found
# Weight to default population margins
weighted_default <- srv_add_weights(data = ds_newsletter_new, margins = NULL)
[1m
[33mError
[39m in `srv_add_weights()`:
[22m
[33m!
[39m could not find function "srv_add_weights"
## Try to remove NAs
weighted_default <-
ds_newsletter_new |>
filter(!is.na(s_age)) |>
srv_add_weights( margins = NULL)
[1m
[33mError
[39m in `srv_add_weights()`:
[22m
[33m!
[39m could not find function "srv_add_weights"
# Check the new weights column and efficiency
head(weighted_default$weights)
[1m
[33mError
[39m:
[22m
[33m!
[39m object 'weighted_default' not found
attr(weighted_default, "efficiency")
[1m
[33mError
[39m:
[22m
[33m!
[39m object 'weighted_default' not found
Verify that the weighted distributions now match the defaults. For example, check gender:
weighted_default %>%
group_by(s_gender) %>%
summarize(total = sum(weights)) %>%
mutate(prop = total / sum(total))
[1m
[33mError
[39m:
[22m
[33m!
[39m object 'weighted_default' not found
genpop_margins$s_gender
[1m
[33mError
[39m:
[22m
[33m!
[39m object 'genpop_margins' not found
These proportions should align
with genpop_margins$s_gender.
Supplying Custom Margins
You can override the defaults by providing your own margins as a named list. Each element name must match a column, and each value is a named numeric vector of proportions (summing to 1).
# Define a custom margin list
my_margins <- list(
s_gender = c(Male = 0.60, Female = 0.40),
s_age = c(
"18-30" = 0.20,
"31-45" = 0.30,
"46-64" = 0.30,
"65+" = 0.20
)
)
# Apply custom margins
weighted_custom <- ds_newsletter_new |>
filter(!is.na(s_age)) |>
srv_add_weights(margins = my_margins)
[1m
[33mError
[39m in `srv_add_weights()`:
[22m
[33m!
[39m could not find function "srv_add_weights"
# Inspect weighted age distribution
weighted_custom |>
count(s_age, wt = weights) |>
mutate(prop = n / sum(n))
[1m
[33mError
[39m:
[22m
[33m!
[39m object 'weighted_custom' not found
Trimming Extreme Weights
Large or very small weights can inflate variance.
Use lower, upper, and strict to
cap weights:
# Trim weights to range [0.5, 3]
weighted_trim <-
ds_newsletter_new |>
filter(!is.na(s_age)) |>
srv_add_weights(lower = 0.5,
upper = 3.0,
strict = TRUE)
[1m
[33mError
[39m in `srv_add_weights()`:
[22m
[33m!
[39m could not find function "srv_add_weights"
# Check weight summary
summary(weighted_trim$weight)
[1m
[33mError
[39m:
[22m
[33m!
[39m object 'weighted_trim' not found
# Efficiency after trimming
attr(weighted_trim$weight, "efficiency")
[1m
[33mError
[39m:
[22m
[33m!
[39m object 'weighted_trim' not found
Interpreting Weighting Efficiency
The attribute "efficiency" reports Kish’s
efficiency (effective n / actual n). For instance:
[1m
[33mError
[39m:
[22m
[33m!
[39m object 'weighted_trim' not found
An efficiency of 0.85 means the weighted data retains ~85% of the statistical power of the unweighted sample.
Conclusion
The srv_add_weights() function provides
a straightforward, flexible interface for applying survey weights:
Default margins via
genpop_margins.Custom margins with a named list.
Raking for post‑stratification.
Optional trimming to control weight extremes.
Efficiency diagnostics to assess variance inflation.
This approach ensures your weighted analyses reflect the intended population structure with full transparency and control.