我有一个带有频率权重的调查数据集(freq_wgt)。调查设计是多阶段整群抽样。如果遵循数据集。
sector <- c(1,2,1,2,1,2,1,2,1,2,2,2,1,1,2,1,2,1,2,2)
sex <- c(2,1,2,2,2,1,2,1,2,1,1,1,1,2,2,1,2,1,2,2)
Group <- c(1,2,3,3, 2,1,1,2,3,3,2,1,1,3,3,1,3,1,2,2)
freq_wgt <- c(2,4,5,6,3,4,5,3,2,5,6,7,5,4,3,5,7,8,9,1)
df <- data.frame(sector, sex, group, freq_wgt)
df$sector <- factor(df$sector, levels = c(1,2), labels = c("Rural", "Urban"))
df$sex <- factor(df$sex, levels = c(1,2), labels = c("Male", "Female"))
df$group <- factor(df$group, levels = c(1,2,3), labels = c("STs", "SCs", "Others"))
我想在应用频率权重后生成以下类型的多层表(频率和列/行百分比)。
mytable <- ftable(xtabs(~ sector + sex + group, data= df))
print(mytable)
注意:我从Hmisc包中找到了wtd.table函数,但不适合,因为它只生成一个阶段的strata表。提前谢谢。
答案 0 :(得分:0)
下面的设计假设是简单的随机抽样。您需要查看技术文档和/或?svydesign
,了解如何将其纳入多阶段整群抽样。
library(survey)
my_design <- svydesign( ~ 1 , data = df , weights = ~ freq_wgt )
svytable( ~ sector + sex + group , my_design )
svyby( ~ sector , ~ sex + group , my_design , svymean )
svyby( ~ sex , ~ sector + group , my_design , svymean )
svyby( ~ group , ~ sector + sex , my_design , svymean )