Based on a population specification create weights for the sample dataset to represent the required population.
Usage
srv_add_weights(
data,
margins = NULL,
lower = -Inf,
upper = Inf,
strict = TRUE,
name = "weight"
)Arguments
- data
A survey data to be weighted.
- margins
Named list of population proportions; if
NULL, usesgenpop_margins. Each element should sum to 1 exactly.- lower
A numeric with lower bound of weights.
- upper
A numeric upper bound of weights.
- strict
Logical; if
TRUE, iteratively enforce trimming bounds (default = TRUE). See survey::trimWeights for details.- name
Column name for the weights in the output dataframe.
Value
An input data.frame with an extra column called name and an
attribute that contains efficiency. Check examples.
Examples
library(dplyr)
data("ds_newsletter_new", package = "g6tr.survey")
ds_newsletter_new |>
filter(!is.na(s_age)) |>
srv_add_weights()
#> # A tibble: 1,020 × 9
#> response_id s_age s_gender s_race s_hhincome s_region s_ethnicity
#> <chr> <fct> <fct> <fct> <fct> <fct> <fct>
#> 1 R_3pg4VsSRrUWP4fs 18-30 Male White alone $100,000 … Northea… Not Hispan…
#> 2 R_2b2kowiL0yFLFQU 31-45 Male White alone $100,000 … West Not Hispan…
#> 3 R_0IIq4pRUtMFFDKV 18-30 Female Black or Af… Less than… West Not Hispan…
#> 4 R_28RM3tVIFAuE81D 18-30 Female White alone $50,000 -… South Not Hispan…
#> 5 R_2qC02WrbuSzydyt 46-64 Male Black or Af… $50,000 -… South Not Hispan…
#> 6 R_W3tDgxNut4jFy5r 18-30 Female White alone $25,000 -… South Hispanic o…
#> 7 R_2bI9DnVhxiDsqZ2 18-30 Female White alone $25,000 -… Northea… Hispanic o…
#> 8 R_2DV73Qng91iYuE2 18-30 Female White alone $50,000 -… West Hispanic o…
#> 9 R_1eRjhzUYGv7YVlK 31-45 Male Black or Af… $25,000 -… South Hispanic o…
#> 10 R_26fHTrKGGamXvCH 31-45 Male Black or Af… $25,000 -… South Hispanic o…
#> # ℹ 1,010 more rows
#> # ℹ 2 more variables: s_generation <fct>, weight <dbl>
## Create with custom weights
custom_margins <- list(
s_gender = c(Male = 0.48, Female = 0.52),
s_age = c(
"18-30" = 0.24,
"31-45" = 0.24,
"46-64" = 0.35,
"65+" = 0.17
),
s_hhincome = c(
"Less than $25,000" = 0.25,
"$25,000 - less than $50,000" = 0.25,
"$50,000 - less than $100,000" = 0.3,
"$100,000 or more" = 0.2
)
)
ds_newsletter_new |>
filter(!is.na(s_age)) |>
srv_add_weights(margins = custom_margins)
#> # A tibble: 1,020 × 9
#> response_id s_age s_gender s_race s_hhincome s_region s_ethnicity
#> <chr> <fct> <fct> <fct> <fct> <fct> <fct>
#> 1 R_3pg4VsSRrUWP4fs 18-30 Male White alone $100,000 … Northea… Not Hispan…
#> 2 R_2b2kowiL0yFLFQU 31-45 Male White alone $100,000 … West Not Hispan…
#> 3 R_0IIq4pRUtMFFDKV 18-30 Female Black or Af… Less than… West Not Hispan…
#> 4 R_28RM3tVIFAuE81D 18-30 Female White alone $50,000 -… South Not Hispan…
#> 5 R_2qC02WrbuSzydyt 46-64 Male Black or Af… $50,000 -… South Not Hispan…
#> 6 R_W3tDgxNut4jFy5r 18-30 Female White alone $25,000 -… South Hispanic o…
#> 7 R_2bI9DnVhxiDsqZ2 18-30 Female White alone $25,000 -… Northea… Hispanic o…
#> 8 R_2DV73Qng91iYuE2 18-30 Female White alone $50,000 -… West Hispanic o…
#> 9 R_1eRjhzUYGv7YVlK 31-45 Male Black or Af… $25,000 -… South Hispanic o…
#> 10 R_26fHTrKGGamXvCH 31-45 Male Black or Af… $25,000 -… South Hispanic o…
#> # ℹ 1,010 more rows
#> # ℹ 2 more variables: s_generation <fct>, weight <dbl>