【发布时间】:2017-11-29 15:05:08
【问题描述】:
如何对数据框而不是行索引进行采样?
具体来说,我不确定如何更改 bs 函数中的 indices 参数以选择构成因子级别的多行,而不是仅选择单个行索引。对于上下文,我将函数 bs 与 boot 包中的 boot 函数结合使用来引导置信区间。
函数bs 允许boot 函数使用indices 参数对数据帧进行采样。
bs <- function(data, indices) {
d <- data[indices,] # allows boot function to select sample
shares <- aggregate(d$PASVINT3W, by=list(d$Prod), FUN = sum)
shares <- shares[1:4 , ]
names(shares) <- c("Prod", "sum.prob")
shares <- shares$sum.prob/sum(shares$sum.prob)
return(shares)
}
然后boot实际进行采样。
作为一个简化的示例,我有变量 type1,其中每两行分组,即 1、1、2、2、3、3。我想对这些分组进行抽样,而不是对单个行进行抽样。
device geslacht leeftijd type1
1 mob 0 53 1
2 tab 1 64 1
3 pc 1 50 2
4 tab 0 75 2
5 mob 1 54 3
6 pc 1 58 3
7 pc 1 57 4
8 pc 0 68 4
9 pc 0 66 5
10 mob 0 45 5
11 tab 1 77 6
12 mob 1 16 6
【问题讨论】: