来自具有R中指定概率的分组数据帧的样本 [英] Sample from a grouped dataframe with specified probabilities in R

查看:63
本文介绍了来自具有R中指定概率的分组数据帧的样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面,我首先将我的data.frame( d )按两个分类变量分组。首先,通过性别(两级;男/女)。其次,按部门教育,工业,非政府组织,私人,公共)。然后,我想从每个部门级别进行抽样,并得出以下概率: c(.2,.3,.3,.1,。 1)性别,并遵循概率 c(.4,.6)

Below, I first group my data.frame (d) by two categorical variables. First, by gender (2-levels; M/F). Second, by sector (Education, Industry, NGO, Private, Public). Then, I want to sample from each level of sector with the following probabilities: c(.2, .3, .3, .1, .1), and gender by following probabilities c(.4, .6).

我正在使用下面的代码来实现我的目标而没有成功?有解决办法吗?

I'm using the code below to achieve my goal without success? Is there a fix for that?

如果我的代码总体上符合我的正确描述,请发表评论吗?

Would you please comment if generally my code does what I describe correctly?

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, .3, .3, .1, .1))) # `Error:  incorrect number of probabilities`


推荐答案

slice_sample 并不能完全满足您的要求,因此我建议您使用 splitstackshape 来完成这项工作。

Well slice_sample won't do exactly what you want so I recommend you use splitstackshape to do the job. Install and load as necessary

# install.packages("splitstackshape")
library(splitstackshape)

指定比例表的方法比较短,但是我会从所需的样本总数中有条不紊地进行说明。在这种情况下,我们将使 n = 100 ,然后为各种因子水平指定百分比。

There are shorter faster ways to specify the proportions table but I'll do it methodically starting from the total sample wanted, which in this case we'll make n = 100 then we'll specify the percentages for the various factor levels.

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 ,首先是我们要处理的两列的向量,然后是组和想要的数量,我们将从上面的百分比中计算得出...

Then we call the function stratified with first a vector of the two columns we're operating on, then a vector of groups and the number wanted which we'll calculate from the percentages above...

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)
              )

我们取回了我们请求的随机选择的数量

We get back the randomly selected quantities we requested

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

这篇关于来自具有R中指定概率的分组数据帧的样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆