1
$\begingroup$

I would like to draw sample from a population but I want my sampling probability to be different for each individual. For example, suppose I will take a sample of two from a population of three which is made up of c("A","B","C"). I want to define sampling probabilities for each of these differently(say 0.6, 0.2, 0.2, respectively). How can I define such a sampling structure in R? I want my sampling to be without replacement.

| cite | improve this question | | | | |
$\endgroup$
1
$\begingroup$

If you want an off-the-shelf solution, sample method in R is suitable for you. The first argument, x is your array, the second argument n is number of samples to choose. replace argument is FALSE in your case, which is also the default behavior (for the version (i.e. 3.5.3) in the link). And, you'll give your probability vector to the prob argument.

Edit: Stick with the R implementation, described above. For the theory behind, I've found this slightly complicated article. There are various others in internet, even for the simple special case $n=2$.

Here I'm leaving my previous incorrect procedure (which is only correct for the first sample) for future readers to save them from a mistake, if by any chance they read this: (Thanks @StasK)

Generate a random number in $[0,1]$, and then map it to the cumulative distribution of your elements (e.g. elements in $[0,0.6)$ maps to $A$, $[0.6,0.8)$ maps to $B$, and so on ...). After picking up your element, remove it from corresponding probability and element arrays, and then normalize your remaining probability vector such that it sums up to $1$; then sample one more element and so on.

| cite | improve this answer | | | | |
$\endgroup$
  • 1
    $\begingroup$ The second paragraph gives an incorrect procedure. Sampling one element with unequal probabilities is easy. Sampling more than one without replacement is incredibly difficult to do right. You have to rescale the probabilities in weird ways to make sure that your ultimate unit selection probability matches your target. library(sample) does it right, so there is no reason to try and invent a circular propulsion device. $\endgroup$ – StasK Apr 1 '19 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.