Skip to contents

This function computes sample weights based on gender for each wave to achieve a target gender balance in the population. It assumes a binary gender classification where 1 represents male and 0 represents female.

Usage

margot_compute_gender_weights_by_wave(
  data,
  male_col = "male",
  wave_col = "wave",
  target_male_prop = 0.5
)

Arguments

data

A data frame containing the gender and wave information.

male_col

Character string specifying the name of the column in `data` that indicates male gender (1 for male, 0 for female). Default is "male".

wave_col

Character string specifying the name of the column in `data` that indicates the wave or group for weighting. Default is "wave".

target_male_prop

Numeric value between 0 and 1 specifying the target proportion of males in the population. Default is 0.5 (50% male).

Value

A numeric vector of sample weights. Each weight corresponds to a row in the input data frame.

Details

The function requires the `dplyr` package and calculates weights that, when applied, will adjust the sample within each wave to match the specified target gender proportion. It upweights the underrepresented gender and downweights the overrepresented gender within each wave. The function will return an error if `target_male_prop` is not between 0 and 1 or if the gender column does not contain binary values. Missing values in the gender or wave column will be excluded from weight calculations.

Examples

# Load the dplyr package
library(dplyr)

# Create a sample dataset
dat <- data.frame(
  id = 1:100,
  male = sample(c(0, 1), 100, replace = TRUE, prob = c(0.7, 0.3)),
  wave = rep(1:2, each = 50)
)

# Compute weights by wave
weights <- margot_compute_gender_weights_by_wave(
  dat,
  male_col = "male",
  wave_col = "wave",
  target_male_prop = 0.5
)

# Check weight distribution by wave
table(round(weights, 3))
#> 
#> 0.714 0.781 1.389 1.667 
#>    35    32    18    15