我需要根据来自更广泛人群的四个人口特征的边际分布对样本中的观察结果进行加权。我目前正在使用该软件包anesrake
来执行此操作。
人口信息存储在targets
. 这是一个包含 4 个元素的列表 - 我想根据每个受访者属性对样本进行加权的一个数字向量。每个元素的行名代表不同的类别。我targets
在这里创建:
quota_age <- c(0.30, 0.33, 0.37)
quota_race <- c(0.62, 0.12, 0.17, 0.5, 0.3)
quota_gender <- c(0.52, 0.48)
quota_ed <- c(0.41, 0.29, 0.19, 0.11)
names(quota_age) <- c("18 to 34", "35 to 54", "55+")
names(quota_race) <- c("White non-Hispanic", "Black non-Hispanic", "Hispanic", "Asian", "Other")
names(quota_gender) <- c("Female", "Male")
names(quota_ed) <- c("HS or less", "Some college", "Bachelors", "Advanced")
targets <- list(quota_age, quota_race, quota_gender, quota_ed)
调查文件 ( m1b
) 是一个数据框,其中包含人口统计信息和每个受访者的唯一 ID(此处链接到谷歌表)。这是前几个obs:
> head(m1b)
ResponseId quota_ed quota_age quota_gender quota_race
1 R_3McITJbfcFuwc9x Some college 18 to 34 Female White non-Hispanic
2 R_2q3oeAbZgCZ5YcZ Bachelors 55+ Female White non-Hispanic
3 R_YSVccSQ1xJ6zuDv Advanced 35 to 54 Female White non-Hispanic
4 R_DubbKu7uJicbpQd Some college 35 to 54 Male White non-Hispanic
5 R_5zj5CNu598lCwRX Bachelors 55+ Male Other
6 R_21mPGFS7kHX2ELm Advanced 55+ Female White non-Hispanic
使用该anesrake
程序包,我想构建一个名为的新变量weight
,我可以在以后的分析中使用它来解释总体和样本边际分布之间的差异。
但是当我这样调用anesrake
函数时(pctlim
参数非常小以至于夸大了我的观点):
library(anesrake)
raking <- anesrake(inputter = targets,
dataframe = m1b,
caseid = m1b$ResponseId,
choosemethod = "total",
type = "pctlim",
pctlim = 0.0000001)
我收到以下错误:
Error in selecthighestpcts(discrep1, inputter, pctlim) :
No variables are off by more than 0.00001 percent using the method you have chosen, either weighting is
unnecessary or a smaller pre-raking limit should be chosen.
尽管这在客观上是不正确的。例如,考虑 quota_ed 目标:
> targets[[4]]
HS or less Some college Bachelors Advanced
0.41 0.29 0.19 0.11
> wpct(m1b$quota_ed)
Advanced Bachelors HS or less Some college
0.1614583 0.3645833 0.1666667 0.3072917
任何关于我做错了什么的想法将不胜感激。请参阅此链接到 RBloggers 帖子,了解我要模拟的例程。