R调查后分层:与调查功能作斗争
R survey poststratification: struggling with the survey function
我是新来的,也是 R 的新手。我想知道我是否正确使用了 R 调查包来对我的数据进行 postStratify。下面你可以看到我的数据框(df)的数据结构。
utype | gender | age | regzeit | finanz | sfeld | sindex |
---|---|---|---|---|---|---|
pri | female | 23 | ja | s | ARG | 5 |
sta | male | 23 | nein | f | ARG | -7 |
sta | female | 21 | ja | ARG | 11 | |
pri | male | 28 | ja | t | ARG | 1 |
我已经为“性别”变量对女性进行了过采样,为“utype”变量对学生进行了过采样,现在想针对人口中的分布进行调整。我的 n=383 被过度采样到 n = 477 我的 n=383 样本的预期分布是:
utype | male | female | Sum |
---|---|---|---|
pri | 54 | 68 | 122 |
sta | 128 | 133 | 261 |
Sum | 187 | 196 | 383 |
design <- svydesign(id = ~utype+gender, data = df)
警告信息: 在 svydesign.default(id = ~utype + gender, data = df) 中: 假设概率相等,未提供权重或概率
pop.types <- data.frame(utype=c("sta","pri"), Freq=c(261,122))
designp <- postStratify(design, ~utype, pop.types)
postStratify(design, ~utype, pop.types)
svymean(~sindex, design)
平均 |东南
指数 0.48008 | 0.0192
svymean(~sindex, designp)
意思是|东南
指数 0.47692 | 0
我现在的问题是下面的代码是否正确以及我如何在我的代码中对变量 utype 和 gender 进行 postStratify,或者我是否必须 运行 两次 postStratify 命令。由于警告消息,我特别担心加权样本中的标准误差为零。以及 Freq 值是否适合我在这里尝试执行的操作?
我一直想弄清楚的最后一件事是如何获得“sindex”的 svymean、svyhist 或 svyboxplot 函数,但仅限于 utype == pri 的观察,所以基本上是按组。这应该全部应用于加权 sindex 值。
我希望我遵守所有规则。非常感谢!
您不想post-分层两次(那样会给您带来倾斜)。你想 post-stratify 一次,使用 post-stratum 变量,它是你的两个变量性别的组合,就像你的 2x2 table 一样。也就是说,
designp <- update(designp, combined_var = interaction(gender,utype))
您现在指定一个 pop.types=
参数,该参数对于这个新变量的四个水平中的每一个都具有所需的频率。
只有您列出的四个观察结果,您最终会得到零标准误差,因为在 post-层中没有任何变化。