rake_survey: Rake survey

Description Usage Arguments Details Value Examples

View source: R/rake_survey.R

Description

Applies the raking algorithm to a survey using specified raking targets in order to obtain weights. Wrapper around the calibrate function from the survey package with argument calfun = "raking".

Usage

1
2
3
4
5
6
7
8
9
rake_survey(
  .data,
  pop_margins,
  base_weight = 1,
  scale_to_n = TRUE,
  epsilon = 5e-06,
  maxit = 100,
  ...
)

Arguments

.data

A data.frame containing the survey data

pop_margins

A list of tibbles giving the population margins for raking variables. create_raking_targets outputs such a list in the correct format.

base_weight

The survey's base weight variable, if applicable. Can be ignored if the survey data doesn't come with a base weight.

scale_to_n

If TRUE, scales the resulting weights so so that they sum to the number of observations in the survey.

epsilon

A numeric variable indicating the tolerance for the raking algorithm. When the proportions of the raking variables are within tolerance, the raking algorithm will stop.

maxit

Maximum number of iterations before stopping if raking has not converged.

...

Other arguments passed to survey::calibrate().

Details

The variables in .data must exactly match the variables in pop_margins both in name in the factor values. Additionally, they should contain no missing data.

Value

A vector of survey weights.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
library(dplyr)
# Prepare variables from the survey data for raking
dec13_excerpt_raking <- dec13_excerpt %>%
  mutate(rk_sex = sex,
         rk_recage = dk_to_na(recage, pattern = "DK/Ref"),
         rk_receduc = dk_to_na(receduc, pattern = "DK/Ref")) %>%
  impute_vars(.) %>%
  mutate(rk_sex_receduc = interaction(rk_sex, rk_receduc, sep = ":"))
# Prepare population marginal distributions for raking
# Here we will use the acs_2017_excerpt dataset included wih the package
targets <- create_raking_targets(acs_2017_excerpt,
                                 vars = c("sex", "recage", "receduc", "sex:receduc"),
                                 wt = "weight")

# Now that we have raking variables and population targets, we can create a raking weight
fake_weight <- rake_survey(dec13_excerpt_raking, targets)

pewresearch/pewmethods documentation built on March 27, 2020, 7:22 p.m.