Search code examples
rprobability

Probability of x elements chosen from the same group


can anyone please help me with the following problem and tell me how to solve the problem by using R?

I have two groups (groupM and groupF) with 9 elements each (M1:M9 and F1:F9).

I want to randomly sample 4 elements from these groups:

#Make the sample reproducible
set.seed(11)

#two groups with 
groupM <- c("M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8", "M9", "M10")
groupF <- c("F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10")

groupAll <- c(groupM, groupF)

sample(groupAll, 4, replace = FALSE)

What is the likelihood that all 4 elements are chosen from the same group? How to I calculate this probability and how do I solve this problem by using R?


Solution

  • As @Bas explained this is a hypergeometric distribution: and hence could be computed as:

    dhyper(4,9,9,4) * 2
    [1] 0.08235294
    

    or simply:

     2* choose(9, 4) / choose(18, 4)
     [1] 0.08235294
    

    For the simulation Part:

    mean(replicate(40000,mean(sample(x, 4))%in%1:2))
    [1] 0.0829