R 随机抽样组,然后在这些抽样组内进行抽样
我的数据集包含几个组,每个组可以有不同数量的唯一观测值。我按组进行了一些计算(在下面的代码中进行了简化),得出了每个组的汇总值。接下来,出于引导的目的,我想:R 随机抽样组,然后在这些抽样组内进行抽样,r,random,grouping,sampling,resampling,R,Random,Grouping,Sampling,Resampling,我的数据集包含几个组,每个组可以有不同数量的唯一观测值。我按组进行了一些计算(在下面的代码中进行了简化),得出了每个组的汇总值。接下来,出于引导的目的,我想: 随机抽样替换组(抽样组数量=等于原始数据集中不同组的数量) 在这些抽样组中,随机抽样观察值并进行替换(每组抽样观察值的数量=等于原始数据集中该组中唯一观察值的数量) 我的数据设置的简化版本(data1): 例如,基于上面的数据集示例:原始数据集中有4个组,因此我想用替换的方法对4个组进行采样(例如采样的组=组4,3,3,1),然后对这4个
# Calculate group value
y.group <- tapply(data1$y,data1$group,mean)
# Step 1. Sample groups, with replacement:
sampled.group <- sample(1:length(unique(data1$group)),replace=T)
# Step 2. Sample within groups, with replacement
data2 <- data.frame(data1 %>%
group_by(group) %>% # for each group
sample_frac(1, replace = TRUE) %>%
ungroup)
#计算组值
y、 组别%
解组)
显然,上面完整的代码并没有达到我想要的效果,因为在步骤2中,步骤1中的采样组被忽略,因为它只使用原始组变量(我知道这一点)。我尝试使用步骤1来解决这个问题,并尝试生成一个新的数据帧,该数据帧仅包含采样组的观察值(如果一个组被多次采样,则可能会出现重复),然后将步骤2应用于该新数据帧,但我无法实现这一点
我想我只是走错了路或者想得太多了。希望你能给我一些关于如何进行的建议
编辑:在等待任何可能的解决方案时,我自己继续回答这个问题,最后得出以下结论:
total.result <- c()
for (j in 1:length(unique(data1$group))){
sampled.group <- sample(1:length(unique(data1$group)),size=1,replace=T)
group.result <- sample_n(data1[data1$group==sampled.group,],
size=length(unique(data1$id[data1$group==sampled.group])),replace=T)
total.result <- rbind(total.result,group.result)
}
total.result我想这就是你要找的。让我们从可复制格式的数据开始:
data1不清楚预期的输出<代码>采样。步骤1中的组
没有与步骤2的任何引用。如果您需要基于sampled.groupdata1%>%过滤器(!group%在%sampled.group中)
进行过滤。另外,y.group
如果要生成仅包含采样组的数据集data1%%>%过滤器(组%在%sampled.group中)%%>%groupby(组)%%>%sample\u frac(1,replace=TRUE)%%>%ungroup
预期输出应该是一个数据帧,其中包含随机采样组的采样观察值(使用替换)。例如,基于上面的数据集示例:原始数据集中有4个组,因此我想使用替换对4个组进行采样(例如采样的组=组4,3,3,1),然后对这4个组中的观察值/行进行采样(组4中的4个ID(例如5007、5007、5006、5009);组3中的2个ID(两次,因为组3被采样了两次),以及组1中的3个ID,都带有替换),并将采样的行一起返回到一个数据帧中(4+2+2+3=11行)。@akrun请参阅我的上述评论(也添加到帖子中)。我尝试了您的建议,这有助于筛选抽样的组,但作为下一步,我希望在这些组中随机抽样,替换的行数等于组的大小,但也考虑到某些组可以多次抽样(因此,我想对这些组重复多次采样过程,例如,一个有3行的组被采样两次,输出中应该有2*3=6个采样行。)y.group
用于以后的计算,可以忽略(为混淆道歉)。太好了,这很有效!在发布问题后,我自己继续提问,并找到了另一个问题(我将其添加到了开场白中),但你的问题似乎更直截了当,因此我将接受它作为答案!@CJPW1987太好了-很高兴我能提供帮助。
total.result <- c()
for (j in 1:length(unique(data1$group))){
sampled.group <- sample(1:length(unique(data1$group)),size=1,replace=T)
group.result <- sample_n(data1[data1$group==sampled.group,],
size=length(unique(data1$id[data1$group==sampled.group])),replace=T)
total.result <- rbind(total.result,group.result)
}
data1
#> id group y
#> 1 1001 1 10
#> 2 1002 1 15
#> 3 1003 1 3
#> 4 3002 2 24
#> 5 3003 2 15
#> 6 3005 2 37
#> 7 3006 2 32
#> 8 3007 2 11
#> 9 4001 3 12
#> 10 4002 3 15
#> 11 5006 4 7
#> 12 5007 4 9
#> 13 5009 4 22
#> 14 5010 4 19
set.seed(69)
split_dfs <- split(data1, data1$group)
sampled_group_dfs <- split_dfs[sample(length(split_dfs), replace = TRUE)]
all_sampled <- lapply(sampled_group_dfs, function(x) x[sample(nrow(x), replace = TRUE), ])
result <- do.call(rbind, all_sampled)
result
#> id group y
#> 4.14 5010 4 19
#> 4.14.1 5010 4 19
#> 4.11 5006 4 7
#> 4.13 5009 4 22
#> 1.3 1003 1 3
#> 1.3.1 1003 1 3
#> 1.2 1002 1 15
#> 3.9 4001 3 12
#> 3.9.1 4001 3 12
#> 2.5 3003 2 15
#> 2.5.1 3003 2 15
#> 2.6 3005 2 37
#> 2.7 3006 2 32
#> 2.5.2 3003 2 15