Skip to contents

[Deprecated] Based on a population specification create weights for the sample dataset to represent the required population. General population specification can be achieved with with srv_genpop_margins()

Usage

srv_weight(
  data,
  sample.margins = list(~s_gender, ~s_age, ~s_hhincome, ~s_race, ~s_region, ~s_ethnicity),
  population.margins = srv_genpop_margins(1000),
  lower = -Inf,
  upper = Inf,
  strict = TRUE
)

srv_genpop_margins(N)

Arguments

data

A survey data to be weighted.

sample.margins

A list containing columns to be used for weighting - they need to exist in the design data and need to be provided in population.margins.

population.margins

A result of srv_genpop_margins().

lower

A numeric with lower bound of weights.

upper

A numeric upper bound of weights.

strict

The reapportionment of the 'trimmings' from the weights can push other weights over the limits. If trim=TRUE the function calls itself recursively to prevent this. See survey::trimWeights for details.

N

A numeric with a number of respondents to reflect in general population margins.

Value

An input data.frame with an extra column called weights and an attribute that contains efficiency. Check examples.

Examples


library(dplyr)
data("ds_newsletter_new", package = "g6tr.survey")

head(ds_newsletter_new)
#> # A tibble: 6 × 8
#>   response_id s_age s_gender s_race s_hhincome s_region s_ethnicity s_generation
#>   <chr>       <fct> <fct>    <fct>  <fct>      <fct>    <fct>       <fct>       
#> 1 R_3pg4VsSR… 18-30 Male     White… $100,000 … Northea… Not Hispan… Millennials 
#> 2 R_2b2kowiL… 31-45 Male     White… $100,000 … West     Not Hispan… Gen X       
#> 3 R_0IIq4pRU… 18-30 Female   Black… Less than… West     Not Hispan… Millennials 
#> 4 R_28RM3tVI… 18-30 Female   White… $50,000 -… South    Not Hispan… Gen Z       
#> 5 R_2qC02Wrb… 46-64 Male     Black… $50,000 -… South    Not Hispan… Gen X       
#> 6 R_W3tDgxNu… 18-30 Female   White… $25,000 -… South    Hispanic o… Millennials 
ds_newsletter_weights_1020 <-
  srv_weight(
    ds_newsletter_new %>%
    filter(!is.na(s_age)),
    sample.margins = list(~s_gender, ~s_age, ~s_hhincome, ~s_race, ~s_region, ~s_ethnicity),
    population.margins = srv_genpop_margins(1020)
  )
#> Warning: `srv_weight()` was deprecated in g6tr.survey 0.4.0.
#> ℹ Please use `srv_add_weights()` instead.

attr(ds_newsletter_weights_1020, "efficiency")
#> [1] 82.1431

round(sum(ds_newsletter_weights_1020$weights),3) == 1020
#> [1] TRUE


ds_newsletter_weights_1020 %>%
  group_by(s_age) %>%
  summarise_at(vars(weights), sum) %>%
  left_join(srv_genpop_margins(1020)[[2]]) %>%
  rename(population_frequency = Freq)
#> Joining with `by = join_by(s_age)`
#> # A tibble: 4 × 3
#>   s_age weights population_frequency
#>   <chr>   <dbl>                <dbl>
#> 1 18-30    244.                 245.
#> 2 31-45    245.                 245.
#> 3 46-64    357.                 357 
#> 4 65+      174.                 173.

ds_newsletter_weights_500 <-
  srv_weight(
    ds_newsletter_new %>%
      filter(!is.na(s_age)),
    sample.margins = list(~s_gender, ~s_age, ~s_hhincome, ~s_race, ~s_region, ~s_ethnicity),
    population.margins = srv_genpop_margins(500)
  )

round(sum(ds_newsletter_weights_500$weights), 3) == 500
#> [1] TRUE

ds_newsletter_weights_500 %>%
  group_by(s_hhincome) %>%
  summarise_at(vars(weights), sum) %>%
  left_join(srv_genpop_margins(500)[[3]]) %>%
  rename(population_frequency = Freq)
#> Joining with `by = join_by(s_hhincome)`
#> # A tibble: 4 × 3
#>   s_hhincome                   weights population_frequency
#>   <chr>                          <dbl>                <dbl>
#> 1 Less than $25,000              125.                   125
#> 2 $25,000 - less than $50,000    125.                   125
#> 3 $50,000 - less than $100,000   150.                   150
#> 4 $100,000 or more                99.9                  100
  
# It is possible to use different set of population margins

srv_genpop_margins_adj <- list(
  s_gender_dist =
    data.frame(
      s_gender = c("Male", "Female"),
      Freq = 200 * c(0.48, 0.52)
    ),
  s_age_dist =
    data.frame(
      s_age = c("18-30", "31-45", "46-64", "65+"),
      Freq = 200 * c(0.24, 0.24, 0.35, 0.17)
    )
)
  
ds_newsletter_weights_200 <-
  srv_weight(
    ds_newsletter_new %>%
      filter(!is.na(s_age)),
    sample.margins = list(~s_gender, ~s_age),
    population.margins = srv_genpop_margins_adj
  )