This article describes how to create weights where different targets are used in different sub-groups (e.g., where the targets change over time, by country, or by segment). There are a number of approaches to doing this. Two of those approaches are:
- Create weights for each sub-sample and sum them.
- Create the weight using code.
Case study
To illustrate the approaches we consider a study that has four quarters of data, where the desire is to weight the data to:
- 50% Male and 50% Female in waves Qtr 1, 2, and 4
- in Qtr 3 only:
- 20% Male and aged under 45
- 20% Male and aged 45 or more
- 30% Female and aged under 45
- 30% Female and aged 45 or more
Approach 1: Create weights for each sub-sample and sum them
This approach involves three steps:
- A weight is created for each weighting structure.
- The weight variables are summed.
- The variable is set as a weight.
Step 1: Create a weight variable for each unique set of targets
In the case study, we have two sets of targets: gender, and, gender-by-age. We create weights as follows:
- Anything > Weight
- Select the adjustment variables for the first set of targets, including the sub-group variable as an adjustment variable. In the case study, this means selecting the gender and wave variables as adjustment variables.
- Enter targets for the sub-group(s) related to the targets, setting all other targets to 0. See the screenshot below. We could have merged together Qtr 1, Qtr 2, and Qtr 4, but would then have needed to enter targets of 150 and 150 for each sex.
- In the drop-down next to Make weights sum to select target values.
- Set Targets are to Population/Count.
- Press + New Weight.
We now need to create a weight for the second set of targets using similar steps as described above. In this case study, we need to use a third adjustment variable, Age. The setup of the screen is shown below. If we have more than two sets of targets, we then create more weights using this same approach.
Step 2: Sum the weight variables
- Select the 2 weight variables you have created in the Data Set tree.
- Press Calculation > Sum > Sum. Note that this approach only works because we have created weights where the targets in each sub-sample (i.e., quarters) are comparable. If we, say, had data by country, we would instead need to make sure that the sum of the targets for each country was proportional to the population sizes.
Step 3: Set the variable as a weight
Last, we select the newly-created variable and then check Usable as a weight in the object inspector.
Approach 2: Creating the weight via code
Code can also be used to create the weight variables. For example, to create the weight for the case study:
- Insert a new variable by hovering over the variables in the Data Set tree, and press + > Custom Code > R - Numeric.
- Paste in the code below, modifying it to suit your problem.
- Check Usable as a weight.
# Getting variables to be used in the weighting variables = data.frame(gender = Gender, ageByGender = factor(paste(Gender, age))) # Setting the targets targets = list("Qtr 1" = list(gender = rbind(c("Male", .50), c("Female", .50))), "Qtr 2" = list(gender = rbind(c("Male", .50), c("Female", .50))), "Qtr 3" = list(ageByGender = rbind(c("Male Under 45", .20), c("Female Under 45", .30), c("Male 45 or more", .20), c("Female 45 or more", .30))), "Qtr 4" = list(gender = rbind(c("Male", .50), c("Female", .50)))) # Output variable to store the results out = rep(NA, nrow(variables)) for (level in levels(wave)) # Looping through the waves { # Creating a filter variable f = level == wave & complete.cases(variables) # filtering variables tgts = targets[[level]] f.variables = variables[f, names(tgts), drop = FALSE]
# Calculating the weight out[f] = flipData::Calibrate(f.variables,tgts) } dynamic.weight = out
A few tips:
- If creating cell weights (i.e., with interlocking categories, such as gender within age) you need to create a composite variable. This is illustrated in the case study by ageByGender.
- The code above can be modified to deal with multiple variables by adding additional variables within each list function.
- It's a good idea to first write this in a normal calculation, rather than a variable, as it will make it easier to debug.
See Also
How to Configure A Weight from Variable(s)
Comments
0 comments
Article is closed for comments.