1 Flipping coins again

Say you are flipping a fair coin twice. You know you can end up with Heads or Tails on any given toss. The sample space would then be \(S = \left\{ HH, HT, TH, TT \right\}\), i.e., the sample size is \(n=4\). The probability of getting exactly one Tails is \(\dfrac{2}{4} = 0.5\) and so is the probability of one Heads. What if we flipped the coin ten times? What would be the probability of exactly three Tails in ten flips of the coin? We could spell out the full sample space and then calculate the needed probability but that would be tedious. It would be easier to use a formula that does this calculation for us.

\[\begin{eqnarray*} P\left[X \text{ successes}\right] = \binom{n}{X}p^{X}\left(1 - p\right)^{n-X} \\ \text{where } \binom{n}{x} = \dfrac{n!}{X!(n-X)!} \text{ and } \\ n! = n \times (n-1) \times (n-2) \times \cdots \times 2 \times 1 \end{eqnarray*}\]

Let us define Success as a Tails resulting on the flip, and X being 3 Tails in ten flips of the coin (i.e., \(n=10\)). Let us also assume that it is a fair coin so that the probability of Tails on any single flip of the coin is \(p=0.5\). Plugging these values into the equation will yield:

\[\begin{eqnarray*} P\left[3 \text{ successes}\right] = \binom{10}{3}0.5^{3}\left(1 - 0.5 \right)^{10-3} \\ = \dfrac{10 \times 9 \times 8 \times \cdots \times 1}{\left(3 \times 2 \times 1 \right) \left(7 \times 6 \times \cdots \times 1 \right)} \left(0.5 \right)^{3} \left(0.5 \right)^{7} \\ = \dfrac{10 \times 9 \times 8 }{\left(3 \times 2 \times 1 \right)} \left(0.5 \right)^{3} \left(0.5 \right)^{7} \\ = \left(10 \times 3 \times 4 \right)\left(0.5 \right)^{3} \left(0.5 \right)^{7} \\ = \left(120\right) \left(0.125 \right) \left(0.0078125 \right) \\ = 0.1171875 \end{eqnarray*}\]

Using R as a calculator we could solve the above formula as follows:

n = 10
x = 3
p = 0.5
p.3tails = choose(n, x) * (p)^(x) * (1 - p)^{n - x}
p.3tails
## [1] 0.1171875

What about three or more Tails? We could calculate the the probability of exactly three Tails, four Tails, and so on through ten Tails, add these up, and we would have our answer … all using the formula above. An R function, rbinom(), can do these calculations for us quite easily. Below we do so for the example above.

set.seed(13579)
n = 10000000
x = rbinom(n, 10, p=0.5)
tab.x = table(x); tab.x
## x
##       0       1       2       3       4       5       6       7       8 
##    9736   97255  438778 1169791 2051968 2462822 2050665 1171457  439391 
##       9      10 
##   98260    9877
ptab.x = (tab.x / n); ptab.x
## x
##         0         1         2         3         4         5         6 
## 0.0009736 0.0097255 0.0438778 0.1169791 0.2051968 0.2462822 0.2050665 
##         7         8         9        10 
## 0.1171457 0.0439391 0.0098260 0.0009877
plot(ptab.x, ylab="Probability", xlab="No. of Tails", xlim=c(0, 10), ylim=c(0, 0.25), col="firebrick") 

Note: If it is a fair coin then half the flips result in Tails … this is the most likely outcome. Say you flip the coin 10 times and you end up with only 1 Tails. Could this by chance? Sure, because we can see that this could happen with some non-zero probability, just as we could also end up with no Tails or even with ten Tails! Strange outcomes can result, even if they occur very rarely, and by chance alone. As a result, we have to generate some rules that can guide us in determining when to suspect that something is wrong because of an unusual result and when to determine that the result isn’t very unusual after all.

Before we move on, however, let us see how we might calculate the probability of three or more Tails.

p.3plus = sum(ptab.x[c(4, 5, 6, 7, 8, 9, 10, 11)])
p.3plus
## [1] 0.9454231

Note how we did this. We asked R to add the probability of 3 Tails (which is the fourth element in ptab.x), 4 Tails, 5 Tails, and so on through to 10 Tails (the eleventh element in ptab.x). We could do this more succinctly:

p.3plus = sum(ptab.x[4:11])
p.3plus
## [1] 0.9454231

What about the probability of two or fewer Tails?

p.2less = sum(ptab.x[1:3])
p.2less
## [1] 0.0545769

Note that this is just \(0.0009736 + 0.0097255 + 0.0438778\).

What about the probability of more than two Tails? This should be just 1 minus the probability of two or fewer Tails. That is, 1 - 0.0545769, i.e., 0.9454231, exactly the same result we got when we calculated p.3plus.

2 - Generating Null Distributions of Proportions and Testing the Handedness of Toads

We will begin with the toad example.The series of commands below will accomplish the following. We first create a sample size of 10 million observations (here toads). We then use the rbinom() command to generate binomial probabilities of observing, in this sample, 0, 1, 2, 3, …, 17, 18 right-handed toads, under the assumption that right-handed toads occur with probability = 0.5. We then construct a frequency table that we call tab.x, and then a relative frequency table that we have named proptab.x. The last command plots these relative frequencies.

n = 10000000
x = rbinom(n, 18, p=0.5)
tab.x = table(x)
data.frame(tab.x)
##     x    Freq
## 1   0      47
## 2   1     701
## 3   2    5862
## 4   3   31060
## 5   4  116916
## 6   5  327377
## 7   6  707325
## 8   7 1212644
## 9   8 1671183
## 10  9 1854748
## 11 10 1666455
## 12 11 1215088
## 13 12  708835
## 14 13  327249
## 15 14  117335
## 16 15   30720
## 17 16    5761
## 18 17     654
## 19 18      40
proptab.x = (table(x)/n)
data.frame(proptab.x)
##     x      Freq
## 1   0 0.0000047
## 2   1 0.0000701
## 3   2 0.0005862
## 4   3 0.0031060
## 5   4 0.0116916
## 6   5 0.0327377
## 7   6 0.0707325
## 8   7 0.1212644
## 9   8 0.1671183
## 10  9 0.1854748
## 11 10 0.1666455
## 12 11 0.1215088
## 13 12 0.0708835
## 14 13 0.0327249
## 15 14 0.0117335
## 16 15 0.0030720
## 17 16 0.0005761
## 18 17 0.0000654
## 19 18 0.0000040
plot(proptab.x, ylab="Probability", xlab="No. of Right-Handed Toads", xlim=c(0,18), ylim=c(0,0.25), col="lightblue") 

Notice that the plot peaks at 9 right-handed toads because that is what we said with the p=0.5 switch – half the toads sampled should be right-handed.

Now, assume we sampled 18 toads and exactly 14 turned out to be right-handed. This means in our sample we have 0.7777778 right-handed toads. This could happen by chance of course but if the TRUE distribution is an equal chance of right-handed and left-handed toads then we should have ended up with only 9 right-handed toads.

So we set about asking: What would be the probability of getting 14 or more toads if in the population right-handed toads occur with \(p=0.5\)? We could calculate this from proptab.x as \(0.0116489 + 0.0031092 + ... + 0.0000052\) but this would be inefficient. Instead, the code below will do this very calculation for us.

sum(proptab.x[15:19])
## [1] 0.015451

Because we have a two-tailed test we would have to allow for seeing 4 or fewer right-handed toads as well. Given the symmetry of the distribution this means doubling up the sum we arrived at above. That is, the probability of seeing either 14 or more right-handed toads or 4 or fewer right-handed toads if \(H_0\) were true is 0.030902

Had we decided before conducting the test to Reject \(H_0\) if the \(P-value\) were \(\leq 0.05\) then we would have to reject the belief that right-handed and left-handed toads occur with equal frequency in the population. Why this decision? Because 0.030902 is less than \(0.05\)

What if we had decided to Reject \(H_0\) if the \(P-value\) were \(\leq 0.01\)? Why then we would be unable to reject the belief that right-handed and left-handed toads occur with equal frequency in the population.

3 - What if \(H_0\) is Not Rejected?

Here we take up the case of a situation when \(H_0\) is not rejected, and we do so with data on mud plantains wherein left- and right-handed individuals are believe to occur in a 1:3 ratio. A sample of 27 mud plantains from a particular crossing (see Example 6.4 on page 161) yielded 6 left-handed flowers. Do these data support the belief that the crossing should yield left- and right-handed flowers in a 1:3 ratio? We start by setting forth the hypotheses and our decision rule.

\(H_0: \text{ Left-handed and right-handed offspring occur with a 1:3 ratio } (p=0.25)\)
\(H_A: \text{ Left-handed and right-handed offspring do not occur with a 1:3 ratio } (p \neq 0.25)\)

Let us decide to Reject \(H_0\) if the \(P-value \leq 0.05\)

Let us generate the distribution to be expected if \(H_0\) were true (called the Null distribution).

n = 10000000
x = rbinom(n, 27, 0.25)
tab.x = table(x); tab.x
## x
##       0       1       2       3       4       5       6       7       8 
##    4203   38174  164757  458241  915914 1406633 1717147 1720324 1433226 
##       9      10      11      12      13      14      15      16      17 
## 1008893  604062  311877  138713   53227   17839    5145    1262     307 
##      18      19      20 
##      48       7       1
proptab.x = (table(x)/n); proptab.x
## x
##         0         1         2         3         4         5         6 
## 0.0004203 0.0038174 0.0164757 0.0458241 0.0915914 0.1406633 0.1717147 
##         7         8         9        10        11        12        13 
## 0.1720324 0.1433226 0.1008893 0.0604062 0.0311877 0.0138713 0.0053227 
##        14        15        16        17        18        19        20 
## 0.0017839 0.0005145 0.0001262 0.0000307 0.0000048 0.0000007 0.0000001
plot(proptab.x, ylab="Probability", xlab="No. of Right-Handed Flowers", xlim=c(0,27), ylim=c(0,0.20), col="lightblue") 

If \(H_0\) were true we should have seen 6.75 left-handed flowers. Now, what is the probability of seeing the 6 left-handed flowers that we saw in this sample?

sum(proptab.x[1:7])
## [1] 0.4705069

Since it is a two-sided test we would have to double this probability to get 0.9410138 and this \(P-value\) is far greater than \(0.05\). Hence we fail to reject \(H_0\). That is, the sample data provide insufficient evidence to conclude that left- and right-handed flowers occur with something other than a 1:3 ratio.

Caution is warranted of course. Why? Because \(H_0\) could still be false but somehow our sample gave us the wrong result by chance alone. Perhaps had we drawn 100 flowers at random our conclusion would have been to reject \(H_0\). As a result, whenever we fail to reject \(H_0\) we will conclude that the data provide evidence that supports \(H_0\).

4 - One-Sided Tests

Assume that a study is designed to test whether daughters resemble their fathers. Participants are shown one photo of a girl and photos of two adult men, and then asked to guess which of the two men is the girl’s father. If there is no resemblance in the population then the probability of the participant selecting the actual father should at most be equal to a guess – 50:50, i.e., \(p=0.5\). If there is a resemblance then the probability of picking the actual father should be greater than 50:50 (i.e., \(p > 0.5\)).

We will begin by setting up the hypotheses and our decision rule.

\(H_0: \text{ Participants pick the actual father correctly at most half the time } (p \leq 0.5)\)
\(H_A: \text{ Participants pick the actual father correctly more than half the time } (p > 0.5)\)

Let us decide to reject \(H_0\) if the \(P-value \leq 0.05\). Now we generate the Null distribution

n = 10000000
x = rbinom(n, 18, 0.5)
tab.x = table(x); tab.x
## x
##       0       1       2       3       4       5       6       7       8 
##      27     667    5919   31557  116284  327510  708959 1214288 1669348 
##       9      10      11      12      13      14      15      16      17 
## 1853762 1667428 1214330  708182  327589  116251   31378    5784     692 
##      18 
##      45
proptab.x = (table(x)/n); proptab.x
## x
##         0         1         2         3         4         5         6 
## 0.0000027 0.0000667 0.0005919 0.0031557 0.0116284 0.0327510 0.0708959 
##         7         8         9        10        11        12        13 
## 0.1214288 0.1669348 0.1853762 0.1667428 0.1214330 0.0708182 0.0327589 
##        14        15        16        17        18 
## 0.0116251 0.0031378 0.0005784 0.0000692 0.0000045
plot(proptab.x, ylab="Probability", xlab="No. of Correct Guesses", xlim=c(0,18), ylim=c(0,0.20), col="cornflowerblue") 

Now assume in a sample of 18 participants some 13 successfully guessed the actual father. Does this suggest that there is a resemblance? We can start by calculating how likely it would be, if \(H_0\) were true, for 13 or more participants to correctly identify the actual father.

sum(proptab.x[14:19])
## [1] 0.0481739

This turns out to be 0.0481739 and is less than 0.05. Hence we can reject \(H_0\); the data provide sufficient evidence to conclude that daughters do indeed resemble their fathers.

As a rule, remember that one-sided hypothesis tests make it very easy to reject \(H_0\) and hence open us up to making mistakes more often than do two-sided tests. Note also that one-sided and two-sided hypotheses and tests are also referred to as one-tailed and two-tailed hypotheses and tests.

5 - Some Worked Examples

5.1 - Problem 11

Out of 18 of the largest mammal species in the Americas 16 were found to be smaller on islands than on the mainland. Do these data suggest that large mammals are likely to differ in size between islands and the mainland in a particular direction? That is, for example, that mammals will be smaller on islands? Use the conventional significance levels of \(\alpha = 0.05\)

I’ll generate the Null distribution under the following hypotheses: \(H_0: \text{ Large mammals occur as often on islands as on the mainland } (p = 0.5)\)
\(H_A: \text{ Large mammals do not occur as often on islands as on the mainland } (p \neq 0.5)\)
Set \(\alpha = 0.05\)
We know that if \(H_0\) were true we should see the same proportion of larger/smaller mammals on the islands as on the mainland (i.e., \(p=0.50\)). The sample proportion is: \(\hat{p}=\dfrac{16}{18} = 0.8888\)

The code below will generate, tabulate, and plot the Null distribution (i.e., when \(H_0\) is true)

n = 10000000
x = rbinom(n, 18, 0.5)
tab.x = table(x); tab.x
## x
##       0       1       2       3       4       5       6       7       8 
##      38     659    5784   30933  116725  326459  706476 1213998 1669868 
##       9      10      11      12      13      14      15      16      17 
## 1856846 1670441 1212826  707511  327084  116667   31151    5810     690 
##      18 
##      34
proptab.x = (table(x)/n); proptab.x
## x
##         0         1         2         3         4         5         6 
## 0.0000038 0.0000659 0.0005784 0.0030933 0.0116725 0.0326459 0.0706476 
##         7         8         9        10        11        12        13 
## 0.1213998 0.1669868 0.1856846 0.1670441 0.1212826 0.0707511 0.0327084 
##        14        15        16        17        18 
## 0.0116667 0.0031151 0.0005810 0.0000690 0.0000034
plot(proptab.x, ylab="Probability", xlab="No. of Larger Mammals Guesses", xlim=c(0,18), ylim=c(0,0.20), col="cornflowerblue") 

How likely is it then that we would have seen 16 larger mammal species on the mainland? { r 8} sum(proptab.x[17:19])

Since it is a two-tailed test we will have to double this to get the \(P-value:\) 0.0013068 and this tells us that the resulting \(P-value\) is quite a bit less than \(\alpha = 0.05\). Hence we reject \(H_0\). The data suggest that large mammal species tend to be larger on the mainland than on islands.

5.2 - Problem 14

\(H_0: \text{ Subjects pick the mother at most half the time } (p \leq 0.5)\)
\(H_A: \text{ Subjects pick the mother more than half the time } (p > 0.5)\)
Let \(\alpha = 0.05\)

This is a one-tailed test.

The test statistic would be the proportion of correct picks seen in the sample.

n = 10000000
x = rbinom(n, 18, 0.5)
tab.x = table(x); tab.x
## x
##       0       1       2       3       4       5       6       7       8 
##      41     675    5832   31257  116544  326685  709283 1214306 1669349 
##       9      10      11      12      13      14      15      16      17 
## 1855073 1668646 1213197  709375  325930  116419   30888    5793     661 
##      18 
##      46
proptab.x = (table(x)/n); proptab.x
## x
##         0         1         2         3         4         5         6 
## 0.0000041 0.0000675 0.0005832 0.0031257 0.0116544 0.0326685 0.0709283 
##         7         8         9        10        11        12        13 
## 0.1214306 0.1669349 0.1855073 0.1668646 0.1213197 0.0709375 0.0325930 
##        14        15        16        17        18 
## 0.0116419 0.0030888 0.0005793 0.0000661 0.0000046
sum(proptab.x[8:19])
## [1] 0.8809683

If \(H_0\) were true we would see 7 or more correct picks in a sample of 18, by chance alone, with a \(P-value\) of 0.8809683. Since this is \(> 0.05\) we fail to reject \(H_0\). That is, the data provide insufficient evidence to conclude that subjects pick the mother at most half the time (i.e., that sons resemble their mothers).