问题描述
下面,我首先通过两个分类变量将data.frame(d
)分组。首先,通过gender
(2级; M / F)。其次,通过sector
(教育,行业,非政府组织,私人,公共)。然后,我想从sector
的每个级别中抽取以下概率:c(.2,.3,.1,.1)
和gender
,并遵循概率c(.4,.6)
。
我正在使用下面的代码来实现我的目标而没有成功?有解决办法吗?
d <- read.csv('https://raw.githubusercontent.com/rnorouzian/d/master/su.csv')
library(tidyverse)
set.seed(1)
(out <- d %>%
group_by(gender,sector) %>%
slice_sample(n = 2,weight_by = c(.4,.6,.2,.1))) # `Error: incorrect number of probabilities`
解决方法
slice_sample
并不能完全满足您的要求,因此我建议您使用splitstackshape
来完成任务。根据需要安装并加载
# install.packages("splitstackshape")
library(splitstackshape)
有几种更短的方法来指定比例表,但我将从需要的总样本开始系统地进行操作,在这种情况下,我们将使n = 100
为指定各种表的百分比因素水平。
total_sample <- 100
M_percent <- .4
F_percent <- .6
Education_percent <- .2
Industry_percent <- .3
NGO_percent <- .3
Private_percent <- .1
Public_percent <- .1
然后我们调用函数stratified
,首先是我们要处理的两列的向量,然后是要根据上面的百分比计算出的组和想要的数目的向量... >
abc <-
stratified(indt = d,c("gender","sector"),c("F Education" = F_percent * Education_percent * total_sample,"M Education" = M_percent * Education_percent * total_sample,"F Industry" = F_percent * Industry_percent * total_sample,"M Industry" = M_percent * Industry_percent * total_sample,"F NGO" = F_percent * NGO_percent * total_sample,"M NGO" = M_percent * NGO_percent * total_sample,"F Private" = F_percent * Private_percent * total_sample,"M Private" = M_percent * Private_percent * total_sample,"F Public" = F_percent * Public_percent * total_sample,"M Public" = M_percent * Public_percent * total_sample)
)
我们取回了我们随机选择的数量
head(abc,20)
fake.name sector pretest state gender pre email phone
1: Correa Education 1254 TX F Medium Correa@...com xxx-xx-1886
2: Manzanares Education 1227 CA F Low Manzanares@...com xxx-xx-1539
3: el-Daoud Education 1409 CA F High el-Daoud@...com xxx-xx-9972
4: Engman Education 1436 CA F High Engman@...com xxx-xx-9446
5: el-Kaba Education 1305 NY F Medium el-Kaba@...com xxx-xx-7060
6: Herrera Education 1405 NY F High Herrera@...com xxx-xx-9146
7: el-Sham Education 1286 TX F Medium el-Sham@...com xxx-xx-4046
8: Harrison Education 1112 NY F Low Harrison@...com xxx-xx-3118
9: Zhu Education 1055 CA F Low Zhu@...com xxx-xx-6223
10: Deguzman Gransee Education 1312 TX F Medium Deguzman Gransee@...com xxx-xx-5676
11: Kearney Education 1303 NY F Medium Kearney@...com xxx-xx-5145
12: Hernandez Mendoza Education 1139 CA F Low Hernandez Mendoza@...com xxx-xx-9642
13: Barros Education 1416 NY M High Barros@...com xxx-xx-2455
14: Torres Education 1370 CA M High Torres@...com xxx-xx-2129
15: King Education 1346 CA M Medium King@...com xxx-xx-5351
16: Cabrera Education 1188 NY M Low Cabrera@...com xxx-xx-6349
17: Lee Education 1208 CA M Low Lee@...com xxx-xx-7713
18: Vernon Education 1216 TX M Low Vernon@...com xxx-xx-7649
19: Ripoll-Bunn Education 1419 TX M High Ripoll-Bunn@...com xxx-xx-8126
20: Ashby Education 1295 TX M Medium Ashby@...com xxx-xx-8416