使用 srvyr 对加权调查数据进行多重响应分析 [英] Multiple response analysis in weighted survey data using srvyr
问题描述
我正在尝试从加权调查数据集中分析多选题.我喜欢 srvyr
包,因为它允许我使用 dplyr 管道,但我找不到有关如何处理多重响应问题的参考资料.
I'm trying to analyse a multiple response question from a weighted survey dataset. I like the srvyr
package because it allows me to use the dplyr pipes, but I can't find the reference material on how to handle multiple response questions.
我有一个简单的数据集,查看不同的收入来源.以下是数据的示例
I have a simple dataset looking at different sources of income. Here's an example of how the data looks like
ID <- c(1,2,3,4,5,6,7,8,9,10)
rent_income <- c("Yes", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "No")
salary_income <- c( "No", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No")
other_income <- c( "No", "Yes", "No", "No", "No", "No", "Yes", "No", "No", "No")
survey_weights <- c(0.6, 1.2 , 1.1 , 0.7 , 2.4 , 1.1 , 0.3 , 0.6 , 1.0 , 0.8)
df<-data.frame(ID, rent_income, salary_income, other_income, survey_weights)
请注意,数据是完全组成的.使用 srvyr
如果首先必须创建一个调查对象
Note that the data is entirely made up. With srvyr
if first have to create a survey object
weighted_dataset <- df %>% as_survey_design(ids=ID, weights=survey_weights)
现在我想计算具有不同收入类型的样本的加权百分比.关于如何做到这一点的任何想法?在 Stata 中有一个名为 mr_tab 的函数.但是我在 R 中找不到类似的
Now I would want to calculate the weighted percentage of the sample that has different types of incomes. Any ideas on how to do that? In Stata there is a function called mr_tab . But I can't find a similar one in R
推荐答案
您可以通过 dplyr
和 使用方便的
R 包.group_by()
和变量选择语法>srvyr
You could use the convenient group_by()
and variable selection syntax available through the dplyr
and srvyr
R packages.
weighted_dataset %>%
# Organize the data into groups defined by each combination of the income variables
group_by_at(vars(ends_with("_income"))) %>%
# For categorical variables, this calculates estimates of percentages
summarize(Percent = survey_mean())
> # A tibble: 6 x 5
> rent_income salary_income other_income Percent Percent_se
> <fct> <fct> <fct> <dbl> <dbl>
> 1 No No No 1 0
> 2 No Yes No 0.769 0.265
> 3 No Yes Yes 0.231 0.265
> 4 Yes No No 1 0
> 5 Yes Yes No 0.6 0.312
> 6 Yes Yes Yes 0.40 0.312
这篇关于使用 srvyr 对加权调查数据进行多重响应分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!