首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何提取具有多个不同条件的随机样本?

如何提取具有多个不同条件的随机样本?
EN

Stack Overflow用户
提问于 2015-07-26 21:02:50
回答 1查看 1.1K关注 0票数 0

我有一套跨国数据集,每个被调查者至少有一本日记。每个答复者的日记数量和日记填写日因国家而异。

例如,在一个国家,每个答复者只填写了一本日记(一半的答复者只在一个周末完成,而另一半的答复者仅在工作日完成)。在另一个国家,每个答复者填写了2份日记(一个周末-一个工作日),在另一个国家,每个人填写了7份日记(一周中每一天一份)。还有一些调查显示,一些受访者返回了2份日记,而另一些则返回了3份;还有一些调查中,每个人都返回了4份日记。数据如下所示:

代码语言:javascript
复制
country_id<-rep(1:4,c(8,8,14,10))
diarist_id<-c(11:18,rep(21:24,each=2),
              rep(31:32,each=7),
              rep(41:44,c(3,3,2,2)))
diary_id<-c(111:118,211,212,221,222,231,232,241,242,
            311:317,321:327,411,412,413,
            421,422,423,431,432,441,442)
weekend<-c(1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,
           0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,
           0,1,0,1,0,1,0,1,0)

dat<-data.frame(country_id,diarist_id,diary_id,weekend)

我试图从每个国家随机抽取一个“一人一本日记”的样本。但在国家一级,我需要大约29%的日记是周末日记。我怎样才能一组一组地画出这样一个条件随机样本?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-26 21:46:10

我想这就是你想要的。为了清晰起见,我选择了拆分示例;可能有一种方法可以不这样做就得到您想要的东西,但它并没有出现在我这里。

我要用data.table

代码语言:javascript
复制
set.seed(100)
library(data.table)
setDT(dat) #turn dat into a data.table (by reference)
country_n<-5 #how many observations you'd like per country

#split the data by weekend status
weekend.dat<-dat[weekend==T]
#we have to take care that there are actually enough
#  weekend observations in each country, so we take the
#  minimum of 29% of country_n (rounded) and the total
#  number of weekend observations in that country
weekend.sample<-
  weekend.dat[weekend.dat[,.I[sample(.N,min(round(.29*country_n),.N))],
                          by=country_id]$V1]

#repeat for the weekday sample, except take 71% this time
weekday.dat<-dat[weekend==F]
weekday.sample<-
  weekday.dat[weekday.dat[,.I[sample(.N,min(round(.71*country_n),.N))],
                          by=country_id]$V1]

#combine; setkey orders the data (as well as other
#  things that may be useful later on)
full.sample<-setkey(rbindlist(list(weekend.sample,weekday.sample)),
                    country_id,diarist_id,diary_id)

这是为我给定的随机种子生成的样本

代码语言:javascript
复制
> full.sample
    country_id diarist_id diary_id weekend
 1:          1         12      112       0
 2:          1         13      113       1
 3:          1         14      114       0
 4:          1         16      116       0
 5:          1         18      118       0
 6:          2         21      212       0
 7:          2         22      221       1
 8:          2         22      222       0
 9:          2         23      232       0
10:          2         24      242       0
11:          3         31      315       0
12:          3         31      316       0
13:          3         31      317       0
14:          3         32      321       1
15:          3         32      324       0
16:          4         41      411       1
17:          4         42      421       0
18:          4         42      423       0
19:          4         43      432       0
20:          4         44      442       0
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31641929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档