Skip to main content

3  The sign test

The sign test is the oldest test in this book. John Arbuthnot used its logic in 1710 for 82 years of London christenings (Chapter 1). It needs almost no assumptions. It uses only the direction of each difference, and it ignores the size.

3.1 The problem

A dermatologist wants to compare two ointments, A and B, for sunburn. Each of 45 volunteers gets ointment A on one arm and ointment B on the other arm. A coin toss decides which arm gets A. After one hour in sunlight, the dermatologist records which arm is less red.

a_better <- 22
b_better <- 18
equal    <- 5
n_total  <- a_better + b_better + equal
n_total
[1] 45

In 22 people the arm with A is less red. In 18 people the arm with B is less red. In 5 people there is no difference.

Is one ointment better than the other, or can a 22 to 18 split come from chance?

The design is paired. Each person gets both ointments, so each person is their own control. Skin type and time in the sun affect both arms equally. The outcome is not a number. For each person we know only which arm is less red. This is nominal data with two categories, and a mean redness score does not exist.

3.2 Intuitive solution

The 5 people with equal arms give no information about which ointment is better, so we leave them out. That leaves 40 people.

Assume the two ointments are equally good. Then, for each of the 40 people, “A is better” and “B is better” are equally likely. Each person is a toss of a fair coin. We toss 40 fair coins, count the heads, and repeat this 10,000 times.

set.seed(1710)
n <- a_better + b_better
heads <- rbinom(10000, size = n, prob = 0.5)
mean(heads >= a_better | heads <= n - a_better)
[1] 0.6278
Show code
tibble(heads = heads) |>
  count(heads) |>
  mutate(extreme = heads >= a_better | heads <= n - a_better) |>
  ggplot(aes(heads, n, fill = extreme)) +
  geom_col(width = 0.9) +
  scale_fill_manual(values = c(`FALSE` = "grey75", `TRUE` = "firebrick"), guide = "none") +
  labs(x = "number of heads in 40 tosses", y = "number of repeats")
Figure 3.1: Number of heads in 40 tosses of a fair coin, repeated 10,000 times. The red bars are the results at least as far from 20 as the observed 22 (22 or more, or 18 or fewer).

A split of 22 to 18, or a more uneven one, occurs in 63% of the repeats. A fair coin makes a result like ours most of the time. The data give no evidence that one ointment is better.

3.3 Formal solution

Let \(a_i\) and \(b_i\) be the redness of the arm with A and the arm with B for person \(i\). Let \(d_i = a_i - b_i\). Only the sign of \(d_i\) enters the test.

The null hypothesis is that a nonzero \(d_i\) is equally likely to be positive or negative: \(P(D > 0) = P(D < 0)\). Then each nonzero \(d_i\) is positive with probability 1/2. We drop the pairs with \(d_i = 0\). Let \(n\) be the number of nonzero pairs and let \(C\) be the number of pairs with \(d_i > 0\). Under \(H_0\),

\[ C \sim \text{Binomial}(n, 1/2). \]

The assumptions are few. The pairs must be independent of each other, and positive and negative differences must be equally likely under \(H_0\). The test makes no assumption about the shape of the distribution of \(D\).

For the ointments, we count the people for whom B is better, so a positive \(d_i\) means that the arm with A is redder. Then \(n = 40\) and \(C = 18\). The binomial distribution is symmetric when \(p = 1/2\), so counting the other direction gives the same p-value.

3.3.1 The exact test

The two-sided p-value is the probability of a split at least as uneven as ours:

\[ p = P(C \le 18) + P(C \ge 22). \]

binom.test() calculates this sum exactly.

bt <- binom.test(b_better, n, p = 0.5)
bt

    Exact binomial test

data:  b_better and n
number of successes = 18, number of trials = 40, p-value = 0.6358
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
 0.2925884 0.6150932
sample estimates:
probability of success 
                  0.45 

Read the output line by line:

  • number of successes = 18, number of trials = 40: \(C\) and \(n\).
  • p-value: the exact two-sided p-value, 0.636. It is close to the 0.628 from the coin simulation.
  • alternative hypothesis: true probability of success is not equal to 0.5: the test is two-sided.
  • 95 percent confidence interval: the interval for the probability that B is better, 0.293 to 0.615. It contains 0.5.
  • probability of success: 18/40 = 0.45.

We do not reject \(H_0\). The data do not show that one ointment is better than the other.

3.3.2 The normal approximation

Before computers, a binomial sum with many terms was slow to calculate. For large \(n\), the binomial distribution is close to a normal distribution. De Moivre found this in 1733 (Chapter 1). Under \(H_0\), \(C\) has mean \(n/2\) and variance \(n/4\). So

\[ Z = \frac{C - n/2}{\sqrt{n/4}} \]

is approximately standard normal. Use this approximation when \(n \ge 20\).

\(C\) is a whole number, but the normal curve is continuous. To get a better approximation, we use the continuity correction. The probability of one whole number \(k\) is the normal area from \(k - 1/2\) to \(k + 1/2\). For example, \(P(C = 5) \approx P(4.5 < Y < 5.5)\), where \(Y\) is the normal variable. So \(P(C \le 18)\) becomes the normal area below 18.5.

Show code
sd_c <- sqrt(n / 4)
tibble(k = 8:32, p = dbinom(k, n, 0.5)) |>
  ggplot(aes(k, p)) +
  geom_col(aes(fill = k <= b_better), width = 1, colour = "white") +
  stat_function(fun = \(x) dnorm(x, n / 2, sd_c), geom = "area",
                xlim = c(8, b_better + 0.5), fill = "firebrick", alpha = 0.25) +
  stat_function(fun = \(x) dnorm(x, n / 2, sd_c), colour = "firebrick", linewidth = 1) +
  geom_vline(xintercept = b_better + 0.5, linetype = "dashed", colour = "grey40") +
  scale_fill_manual(values = c(`FALSE` = "grey80", `TRUE` = "grey55"), guide = "none") +
  labs(x = "C, the number of pairs where B is better", y = "probability")
Figure 3.2: Binomial(40, 1/2) probabilities (grey bars) and the normal curve with mean 20 and standard deviation \(\sqrt{10}\) (red). The shaded area below 18.5 is the continuity-corrected approximation to P(C ≤ 18).

The two-sided p-value with the continuity correction is

\[ p = \begin{cases} 2\left[1 - \Phi\!\left(\dfrac{C - n/2 - 1/2}{\sqrt{n/4}}\right)\right] & \text{if } C > n/2, \\[2ex] 2\,\Phi\!\left(\dfrac{C - n/2 + 1/2}{\sqrt{n/4}}\right) & \text{if } C < n/2, \\[2ex] 1 & \text{if } C = n/2, \end{cases} \]

where \(\Phi\) is the standard normal cumulative distribution function. At significance level \(\alpha\), you reject \(H_0\) if

\[ C > \frac{n}{2} + \frac{1}{2} + z_{1-\alpha/2}\sqrt{\frac{n}{4}} \quad\text{or}\quad C < \frac{n}{2} - \frac{1}{2} - z_{1-\alpha/2}\sqrt{\frac{n}{4}}. \]

We calculate the p-value by hand. Here \(C = 18 < n/2 = 20\), so we use the second line.

z <- (b_better - n / 2 + 0.5) / sqrt(n / 4)
p_normal <- 2 * pnorm(z)
c(z = z, p = p_normal)
         z          p 
-0.4743416  0.6352563 

The R function prop.test() gives the same result. It reports \(Z^2\) as a chi-square statistic with 1 degree of freedom.

pt <- prop.test(b_better, n, p = 0.5, correct = TRUE)
pt

    1-sample proportions test with continuity correction

data:  b_better out of n, null probability 0.5
X-squared = 0.225, df = 1, p-value = 0.6353
alternative hypothesis: true p is not equal to 0.5
95 percent confidence interval:
 0.2960304 0.6134103
sample estimates:
   p 
0.45 

The X-squared statistic is 0.225, which is \(z^2 = (-0.4743)^2\). The p-value is 0.6353, the same as our calculation by hand. The exact p-value was 0.6358. With \(n = 40\), the approximation is close to the exact value.

The rejection limits for \(\alpha = 0.05\) are:

n / 2 + c(-1, 1) * (0.5 + qnorm(0.975) * sqrt(n / 4))
[1] 13.30205 26.69795

\(C\) must be below 13.3 or above 26.7 for a significant result. So, out of 40 people, 13 or fewer, or 27 or more, must favour one ointment. Our count of 18 is far inside these limits.

3.4 Sample size and the p-value

Each ointment won in about half of the pairs, 22 for A and 18 for B. This is a 55 to 45 split. Keep this split fixed and increase the number of people.

sizes <- tibble(n = c(40, 100, 400, 1000, 2000)) |>
  mutate(a_better = 0.55 * n,
         p_value  = map2_dbl(a_better, n, \(k, m) binom.test(k, m, p = 0.5)$p.value))
sizes
# A tibble: 5 × 3
      n a_better    p_value
  <dbl>    <dbl>      <dbl>
1    40       22 0.636     
2   100       55 0.368     
3   400      220 0.0510    
4  1000      550 0.00173   
5  2000     1100 0.00000846
Show code
m_grid <- seq(20, 2000, by = 20)
tibble(m = m_grid,
       p = map_dbl(m_grid, \(m) binom.test(round(0.55 * m), m, p = 0.5)$p.value)) |>
  ggplot(aes(m, p)) +
  geom_hline(yintercept = 0.05, linetype = "dashed", colour = "grey40") +
  geom_line(colour = "steelblue", linewidth = 1) +
  scale_y_log10() +
  labs(x = "number of nonzero pairs", y = "p-value (log scale)")
Figure 3.3: Exact sign-test p-value for a 55 to 45 split, as the number of nonzero pairs increases. The dashed line is p = 0.05. The y axis is on a log scale.

With 40 pairs the p-value is 0.64. With 400 pairs it is 0.051, near the 0.05 line. With 1,000 pairs it is 0.0017, and with 2,000 pairs it is 8.5^{-6}.

The proportion is the same in every row. Only the sample size changes. The test asks if the split is larger than the noise at that sample size. The noise in the proportion \(C/n\) has standard deviation \(\sqrt{1/(4n)}\), so it decreases as \(n\) increases.

A large sample can make a small difference significant, and a small sample can miss a real one. The p-value does not give the size of the effect, so a report must give the proportion with it.

3.5 More paired comparisons

3.5.1 Airtel or Jio?

The same problem comes up in a hostel. A new student asks you which network gives faster data in the hostel, Airtel or Jio. You ask 45 students who used both. Of these, 22 say Airtel was faster, 18 say Jio was faster, and 5 cannot tell.

The numbers are the same as for the ointments, so the test is the same. Each student compares the two networks, so the design is paired. The answer is a preference, which is nominal data.

We drop the 5 ties and test \(C =\) 22 against Binomial(40, 1/2). The p-value is 0.636, and we cannot say that one network is faster. The result describes what the students report. A speed test on each network measures the speed itself.

3.5.2 When the sign test is weak

The sign test uses only the direction of each difference. Suppose the arm with A is a little less red in 22 people, and the arm with B is much less red in 18 people. The sign test gives the same p-value as before, because the counts do not change. When the differences are numbers, a test that also uses their size can detect smaller effects. The paired t-test (Chapter 4) uses the values of the differences. The signed-rank test (Chapter 5) uses the ranks of the differences.

3.6 Summary

  • The sign test uses only the direction of each paired difference, and drops the ties.
  • The number of positive differences is Binomial(\(n\), 1/2) under \(H_0\), so the exact p-value comes from binom.test().
  • The normal approximation with a continuity correction is close to the exact p-value for \(n \ge 20\), and prop.test() calculates it.
  • The same proportion gives a smaller p-value with a larger sample, so a report needs the proportion together with the p-value.