Angela Rojas
Angela Rojas

Reputation: 1

Error When Calculating survey_ratio Using Averages

I want to obtain the income gap between women and men from a complex sample using the previously declared object 'enoesvyset' (created as_survey_design with strata = estrato, weights = peso, id = upm, nest=TRUE). However, in the following code, it appears that the result does not take into account the survey design or sample weights because it calculates the income average by groups without utilizing survey_mean or similar functions, even though the value is later computed with survey_ratio.

enoesvyset %>%
  # Aggregation
  group_by(across(all_of(!!agregaciones[[ag]]))) %>%
  summarise(
    ing1 = mean(ingreso[sex == 1], na.rm = TRUE), # Average income for men
    ing2 = mean(ingreso[sex == 2], na.rm = TRUE), # Average income for women
    valor = round(survey_ratio(ing2, ing1, vartype = c("cv"), na.rm = TRUE), 4)
  ) %>%
  ungroup()

Calculating incomes with survey_mean first and then including these results within survey_ratio is not possible, as it results in the error "invalid type (list) for variable 'SRVYR_TEMP_NUM'."

It is essential to use tidy language as this code is within a loop. When calculating survey_mean for each group separately and then dividing these values to obtain the ratio, I cannot obtain the coefficient of variation considering the survey design.

It is crucial that the result takes into account the sample weights of the complex sample and that the coefficient of variation is obtained considering the survey design. How can this be resolved?

Upvotes: 0

Views: 43

Answers (1)

Thomas Lumley
Thomas Lumley

Reputation: 2765

The first two inputs to survey_ratio must be variables, not means.

Upvotes: 1

Related Questions