mardi 22 novembre 2022

Draw sample from "binned" distribution in R

Suppose I have a vector of counts binned into classes i=1...k, i.e.

v = c(n_1, n_2, ... n_k)

for a total N = Sum n_i. I want to sample n < N without replacement. The naive approach to this is to use v to create a vector vnew with n1 1's, n2 2's, ... nk k's, and then apply

s1 = sample(vnew, n)
t1 = table(s1)

and then re-bin.

Surely there is a simpler way to do this in R. It's basically down-sampling, but with respect to a single distribution and a specified sample size (rather than two distributions).




Aucun commentaire:

Enregistrer un commentaire