具有多项选择和权重变量的交叉表

Crosstab with multiple choice and weight variable

我正在学习使用优秀的 "expss" R 包。

我需要知道是否可以使用此包在多项选择变量和分类变量之间进行权变table,考虑权重变量

这个数据框中的分类变量是"sex",权重变量是"survey_weight":

demo <- tribble(
~dummy1, ~dummy2, ~dummy3, ~survey_weight, ~sex,
      1,       0,       0,          1.5,  "male",
      1,       1,       0,          1.5,  "female",
      1,       1,       1,           .5,  "female",
      0,       1,       1,          1.5,  "male",
      1,       1,       1,           .5,  "male",
      0,       0,       1,           .5,  "male",
)
demo 

我需要根据回答问题的总受访者而不是总答复来计算百分比。

提前致谢!

也许我们可以使用 cro_cpct

library(expss)
calculate(demo, cro_cpct(list(dummy1, dummy2, dummy3), weight = survey_weight, sex))
#                                 
# |              |    sex |      |
# |              | female | male |
# | ------------ | ------ | ---- |
# |            0 |        | 50.0 |
# |            1 |    100 | 50.0 |
# | #Total cases |      2 |  4.0 |
# |            0 |        | 50.0 |
# |            1 |    100 | 50.0 |
# | #Total cases |      2 |  4.0 |
# |            0 |     75 | 37.5 |
# |            1 |     25 | 62.5 |
# | #Total cases |      2 |  4.0 |
library(expss)
demo = text_to_columns('
 dummy1   dummy2   dummy3  survey_weight  sex
      1        0        0            1.5  male
      1        1        0            1.5  female
      1        1        1             .5  female
      0        1        1            1.5  male
      1        1        1             .5  male
      0        0        1             .5  male
')


demo %>% 
    tab_cells(mdset(dummy1 %to% dummy3)) %>%  # 'mdset' designate that with have multiple dichotomy set
    tab_cols(sex) %>%  # columns
    tab_weight(survey_weight) %>% # weight
    tab_stat_cpct() %>% # statistic
    tab_pivot() 

# |              |    sex |      |
# |              | female | male |
# | ------------ | ------ | ---- |
# |       dummy1 |    100 | 50.0 |
# |       dummy2 |    100 | 50.0 |
# |       dummy3 |     25 | 62.5 |
# | #Total cases |      2 |  4.0 |

# shorter notation with the same result
calc_cro_cpct(demo, mdset(dummy1 %to% dummy3), sex, weight = survey_weight)