# How to calculate the chance of getting completely unbalanced groups? (with R)

I want to simulate or calculate probabilities of combinations of group membership for different sample sizes (e.g., n= 3, 4, 5, 10, or 100) for two groups (of the same sample size). Each outcome could be male/female and young/old. The population is 50% male and 50% young.

What is the probability of getting:

all male in Group 1 AND all female in Group 2

OR

all male in Group 2 AND all female in Group 1

OR

all young in Group 1 AND all old in Group 2

OR

all young in Group 2 AND all old in Group 1

I would also like to be able to include additional categories (e.g., eats their vegetables, doesn't eat their vegetables) and be able to choose the percent of the population that falls into one category or the other. The categories can be independent of each other, but it would be better if there was the ability to make membership dependent (e.g., females eat their vegetables 70% of the time and males 50% of the time). It would also be better if this was not limited to categories with 2 types (e.g., be able to do 4th, 5th, or 6th grader).

My goal is to calculate the probability of getting completely unbalanced groups for different combinations of confounds and I can't figure out how to do this with R. This is an expansion on this question, but the approach used there is slow for large sample sizes and sort of convoluted.

Edit:

Reworded question: There are two groups that are independent samples from the same population of gradeschoolers, call them treatment and control. The population consists of 50% males, 50% females, 25% each in 3rd-6th grade. There are equal number of males and females in each grade. Also 75% of females eat vegetables, while only 50% of males eat vegetables regardless of schoolgrade.

For different sample sizes I want to calculate the chance of getting

All males in one group (treatment/control) while there are all females in the other

OR

All students of the same grade in one group while the second group consists of all students of the same grade but different than the first. For example all 3rd graders in the treatment group and all 6th graders in the control group.

OR

All vegetable eaters in one group and all non-vegetable eaters in the second group.

• (1) Probabilities depends on counts, not proportions. (2) Probabilities require some kind of model for variation. What is your model? – whuber Dec 12 '13 at 19:30
• @whuber I'm not sure if I follow. For example in the simple case of n=2 per group sampled from a population that is exactly 50% male and 50% female. What is the probability of getting 2 males in group 1 and 2 females in group 2? It would be .5*.5*.5*.5=0.0625 or ~6% of the time. Right? Your question makes me think I may be severely confused. – Flask Dec 12 '13 at 19:54
• Are you saying that "groups" are independent samples of the same population? My confusion more generally stems from a lack of clear definition of "group," "category," "type", "unbalanced," and "confound" in this question, which appears to use at least some of these terms in non-standard ways. Perhaps you could ask an explicit, definite question to clarify your meanings. – whuber Dec 12 '13 at 19:59
• @whuber Yes, groups are independent samples from the same population. I will attempt to reword the question. – Flask Dec 12 '13 at 20:01
• I want to comment that I think this question is very very basic but I am confused on it for some reason. – Flask Dec 12 '13 at 21:06

Taking the reworded question as a starting point:

A) What is the chance of getting all males in one group (treatment/control) while there are all females in the other for different sample sizes?

With n=1, the chance of picking a male from the population is 0.5. With n=2, the chance that the first pick is a male is 0.5. The chance that the second pick is a male as wel is also 0.5. Consequently the chance of picking all males in a sample of 2 is 0.5 * 0.5, which is the same as 0.5^2.

To put it more generally: the chance of picking all males in a sample is 0.5^n (in which n = sample size). The same is true for the chance of picking all females: 0.5^n.

Now the chance of having all males in one group and all females in the other group is the product of these chances: (0.5^n) * (0.5^n). When you want to take a more general approach, the case where it doesn't matter which sex is in the control group as long as the the other group is composed of the other sex, than the formula is (0.5^(n-1)) * (0.5^n).

B) What is the chance that students in one group are from the same grade, while the student from the other group are all from one of the other grades?

Building on (A), the chances for the first group (=grade) seem quite simple to calculate. As the population is equally divided among the four groups, the chance of picking a student from a particular group is 0.25. So you would say that the chance is 0.25^n. However, this is not true. As it doe not matter from which group the first pick of a sample comes from, only the following picks do matter. The first pick determines from which group the following pick have to come. The chances that these following picks are from the same group are 0.25 for each pick. The chances of picking students from the same group is therefore 0.25^(n-1).

Now you want to know what the cahnces are that you pick students all from one of the other group. The chance for your first pick is 0.75. As the following picks have to be from the same group, the chance for each of these picks is 0.25. The chances of picking students for the second from another, but the same, group is 0.75 * (0.25^(n-1))

The chance of picking students from the same grade for the first group, while picking students from another, but the same, grade for the second group is therefore: (0.25^(n-1)) * (0.75 * (0.25^(n-1)))

C) What is the chance of having all vegetable eaters in one group and all non-vegetable eaters in the second group?

For this question you have to calculate the change of picking a (non)-vegetable eater first. The chance of picking a vegetable eater is 0.5 for males and 0.75 for females. Because the population is equally divided betwwen male and females, the chance of picking a vegetable eater is: (0.5 * 0.5) + (0.75 * 0.5) = 0.625. The chance of picking a non-vegetable eater is: 1 - 0.625 = 0.375

The chance of picking all vegetable eaters in the first group while in the same time picking all non-vegetable eaters for the second group is: (0.625^n) * (0.375^n)

You can calculate the answers for A, B & C in R with the following code:

nr <- as.data.frame(c(3, 4, 5, 10, 100))
names(nr) <- c("sample.size")
nr$A <- (0.5 ^ (nr$sample.size - 1)) * (0.5 ^ nr$sample.size) nr$B <- (0.5 ^ (nr$sample.size - 1)) * (0.75 * (0.25 ^ (nr$sample.size - 1)))
nr$C <- (0.625 ^ nr$sample.size) * (0.375 ^ nr$sample.size)  This will produce a dataframe with sample size in the first column and the probabilities for A, B & C in their respective columns. Why you should use n-1 instead of simply multiplying by 2: Consider the example of two group with n = 3 each. When you start selecting the first group, it does not matter which sex you select. So your chance of picking the right one is 1. However, the outcome of your first pick determines which outcomes of the following two selections you need for a same sex group. The chances of picking the same sex for both selections are 0.5. Consequently the chance of picking participants of the same sex for the first group is 1*0.5*0.5 = 1*(0.5^2) = 0.5^2. The general formula for this outcome is 0.5^(n-1). For your second group, you have only a chance of 0.5 for the first pick of selecting a participant of the other sex. Also for the second and third pick you have a chance of 0.5 for each. The calculation is therefore: 0.5*0.5*0.5 = 0.5^3. The general formula for this part is 0.5 ^ n. As a result, the chance of selecting the first group of the same sex and the other group of the other sex is (0.5^(n-1)) * (0.5^n). I did some testing with different variations of the formula. In your specific setup the formula (0.5^n) * (0.5^n) * 2 gives the same outcome. The caveat is in the individual chances. When you have for example a population with 40% males and 60% females, just multiplying with 2 will give you the wrong probability. Therefore working with n-1 is the right solution. When you add the following lines of code to the code above, you can see it action: nr$Aa <- (0.5 ^ nr$sample.size) * (0.5 ^ nr$sample.size) * 2
nr$Ab <- (0.4 ^ (nr$sample.size - 1)) * (0.6 ^ nr$sample.size) nr$Ac <- (0.4 ^ nr$sample.size) * (0.6 ^ nr$sample.size) * 2

• I think this is my point of confusion: Take the simple n=2 male/female scenario. The possibilities are two males in the treatment group and two females in the control group OR two females in the treatment group and two males in the control group. Your calculation is only for one of those cases. – Flask Dec 15 '13 at 19:09
• @Flask No, the calculation is applicable for both cases. For your second case the chance is (0.5^n) * (0.5^n) as wel. When you want to take a more general approach, the case it doesn't matter sex is in the control group as long as the other sex is in the other group, than the formula is (0.5^(n-1)) * (0.5^n). See my changes in my answer. – Jaap Dec 15 '13 at 21:27
• Can you explain why it is n-1 rather than simply multiplying by two? – Flask Dec 15 '13 at 23:29
• I added my answer to your question to my solution above. Is it clear? – Jaap Dec 16 '13 at 17:15
• Yes, thank you. This appears to answer my question. The last thing is that to calculate the probability of one of the scenarios occurring I would just sum the columns of your data frame correct? Also, to get to my ultimate purpose, what we could conclude from this is that the chance of getting a "completely confounded" pair of samples will scale linearly with the number of possible confounding factors. If there are 32 different categories like male/female and our n=3 we would predict 32*3.125=100% chance of having our result completely confounded along at least one dimension. – Flask Dec 16 '13 at 17:25