我想出了一个简单的方法来做到这一点,虽然它绝对不是理想的,而且有超过 100 次模拟会有点累。
villages = split(d$malaria, d$village500)
positives = vector("list", 100)
for(i in 1:100) {
sampled = lapply(villages, sample, 20)
positives[[i]] = lapply(sampled, sum)
}
simulations = cbind(positives[[1]], positives[[2]], positives[[3]], positives[[4]], positives[[5]], positives[[6]], positives[[7]], positives[[8]], positives[[9]], positives[[10]], positives[[11]], positives[[12]], positives[[13]], positives[[14]], positives[[15]], positives[[16]], positives[[17]], positives[[18]], positives[[19]], positives[[20]], positives[[21]], positives[[22]], positives[[23]], positives[[24]], positives[[25]], positives[[26]], positives[[27]], positives[[28]], positives[[29]], positives[[30]], positives[[31]], positives[[32]], positives[[33]], positives[[34]], positives[[35]], positives[[36]], positives[[37]], positives[[38]], positives[[39]], positives[[40]], positives[[41]], positives[[42]], positives[[43]], positives[[44]], positives[[45]], positives[[46]], positives[[47]], positives[[48]], positives[[49]], positives[[50]], positives[[51]], positives[[52]], positives[[53]], positives[[54]], positives[[55]], positives[[56]], positives[[57]], positives[[58]], positives[[59]], positives[[60]], positives[[61]], positives[[62]], positives[[63]], positives[[64]], positives[[65]], positives[[66]], positives[[67]], positives[[68]], positives[[69]], positives[[70]], positives[[71]], positives[[72]], positives[[73]], positives[[74]], positives[[75]], positives[[76]], positives[[77]], positives[[78]], positives[[79]], positives[[80]], positives[[81]], positives[[82]], positives[[83]], positives[[84]], positives[[85]], positives[[86]], positives[[87]], positives[[88]], positives[[89]], positives[[90]], positives[[91]], positives[[92]], positives[[93]], positives[[94]], positives[[95]], positives[[96]], positives[[97]], positives[[98]], positives[[99]], positives[[100]])