Skip to main content

2  Data, p-values and confidence intervals

Most tests in this book ask if a difference in the data is larger than chance alone can make. The choice of test depends on the type of data. The answer comes as a p-value and a confidence interval.

2.1 The problem

The Lecture 02 warm-up quiz showed plots of systolic blood pressure (SBP) for two groups of patients and asked one question.

Is the systolic blood pressure statistically different between the two groups?

The quiz showed only plots, so we have no real data for it. Here we simulate a small trial of the same type. The data are simulated. We randomise 30 patients, 15 to a new drug and 15 to a placebo. After 8 weeks we measure SBP in mmHg.

set.seed(1)
n <- 15
sbp <- tibble(
  group = factor(rep(c("Placebo", "Drug"), each = n), levels = c("Placebo", "Drug")),
  sbp   = round(c(rnorm(n, mean = 140, sd = 12), rnorm(n, mean = 131, sd = 12)))
)
sbp |> group_by(group) |> summarise(n = n(), mean = mean(sbp), sd = sd(sbp))
# A tibble: 2 × 4
  group       n  mean    sd
  <fct>   <int> <dbl> <dbl>
1 Placebo    15  141.  12.3
2 Drug       15  132.  10.3

The mean SBP is 141.1 mmHg in the placebo group and 131.6 mmHg in the drug group. The drug group is lower by 9.5 mmHg. The standard deviations are 12.3 and 10.3 mmHg, so the spread inside each group is larger than the gap between them.

Show code
ggplot(sbp, aes(group, sbp, colour = group)) +
  geom_jitter(width = 0.08, size = 2, alpha = 0.8) +
  stat_summary(fun = mean, geom = "crossbar", width = 0.4, colour = "black") +
  scale_colour_manual(values = c(Placebo = "grey50", Drug = "steelblue"), guide = "none") +
  labs(x = NULL, y = "SBP at 8 weeks (mmHg)")
Figure 2.1: Simulated SBP after 8 weeks for 15 patients on placebo and 15 on the drug. Each point is one patient, and the black bar is the group mean.

2.2 Intuitive solution

Assume that the drug does nothing. Then the labels “Drug” and “Placebo” carry no information. Each patient has the same SBP whatever label we give them. If we shuffle the labels at random, we get a difference that chance alone made.

We shuffle the 30 labels 10,000 times. Each time, we calculate the difference in means. Then we count how often the shuffled difference is at least as large as the observed one, in either direction.

set.seed(801)
shuffled <- replicate(10000, {
  g <- sample(sbp$group)
  mean(sbp$sbp[g == "Drug"]) - mean(sbp$sbp[g == "Placebo"])
})
mean(abs(shuffled) >= abs(obs_diff))
[1] 0.0292
Show code
tibble(d = shuffled) |>
  ggplot(aes(d, fill = abs(d) >= abs(obs_diff))) +
  geom_histogram(binwidth = 1, boundary = 0, colour = "white") +
  geom_vline(xintercept = c(obs_diff, -obs_diff), colour = "firebrick", linetype = "dashed") +
  scale_fill_manual(values = c(`FALSE` = "grey75", `TRUE` = "firebrick"), guide = "none") +
  labs(x = "difference in means after shuffling (mmHg)", y = "number of shuffles")
Figure 2.2: Differences in mean SBP (Drug minus Placebo) after 10,000 random shuffles of the group labels. The red lines mark the observed difference and its mirror image. The red bars are the shuffles at least as extreme as the data.

In 2.9% of the shuffles, the gap is as large as 9.5 mmHg. If the drug does nothing, a result like ours is rare. This proportion is a p-value. We calculated it with no formula and no assumption about the shape of the data.

2.3 Formal solution

A formal test does the same job as the shuffle. It replaces the 10,000 shuffles with a known distribution. The choice of test depends on the type of data, on the design and on the assumptions that you accept.

2.3.1 Types of data

Type What you can do Examples
Nominal Count categories. There is no order. Sex, hair colour, blood group, preferred mobile network
Ordinal Rank the categories. The gaps between them are not equal. Disease grade I to IV, pain as mild, moderate or severe, visual acuity 20/20 or 20/30
Cardinal, interval scale Add and subtract. Zero is arbitrary. Temperature in °C, calendar year
Cardinal, ratio scale Add, subtract, multiply and divide. Zero means “none”. SBP, weight, height, hospital stay in days

Cardinal data are numbers with a fixed unit. On an interval scale, differences have a meaning, but ratios do not. A day at 30 °C is not twice as hot as a day at 15 °C, because 0 °C is not “no heat”. On a ratio scale, ratios also have a meaning. An SBP of 160 mmHg is twice a pressure of 80 mmHg.

Our SBP data are on a ratio scale. We can calculate a mean and a standard deviation. For ordinal data, a mean is not meaningful, so we use ranks or medians. For nominal data, we can only count.

A categorical variable with two levels is a Bernoulli variable. The number of “yes” answers in \(n\) independent trials is binomial. With more than two levels, the counts are multinomial. Chapter 3 uses the binomial.

2.3.2 Paired and unpaired designs

In a paired design, each measurement in one group has a partner in the other group. Examples are the same patient before and after treatment, the left and right arm of one person, and twins. In an unpaired design, the two groups contain different people.

Our trial is unpaired. Each patient gets either the drug or the placebo. A paired design removes the variation between people, because each person acts as their own control. A paired test uses the differences inside each pair. An unpaired test compares the two groups as a whole. If you use an unpaired test on paired data, you throw away this advantage.

2.3.3 Parametric and non-parametric tests

A parametric test assumes that the data come from a family of distributions with a small number of parameters. The t-test (Chapter 4) assumes a normal distribution with a mean and a standard deviation. A non-parametric test makes weaker assumptions. The sign test uses only the direction of each difference. Rank tests use only the order of the values. The label shuffle above is also non-parametric.

When its assumptions are true, a parametric test uses more of the information in the data. When they are false, a non-parametric test can give a more reliable answer. A plot of the data shows which case applies, so draw the data before you choose a test.

2.3.4 The p-value

Our data are cardinal and unpaired. A two-sample t-test fits this design. Chapter 4 explains the test. Here we only read its p-value.

tt <- t.test(sbp ~ group, data = sbp)
tt$p.value
[1] 0.02892889

The t-test gives p = 0.029. The shuffle gave a close value, 0.029.

The null hypothesis \(H_0\) states that there is no effect. Here, \(H_0\) states that the mean SBP is the same with the drug and the placebo. The definition of the p-value is:

\[ p = P(\text{a result at least as extreme as the observed one} \mid H_0 \text{ is true}). \]

The p-value is a probability about the data. We calculate it on the assumption that \(H_0\) is true. So it cannot tell you the probability that \(H_0\) is true. All three statements below are incorrect:

  • “There is a 2.9% probability that the drug does nothing.”
  • “There is a 97.1% probability that the drug works.”
  • “There is a 2.9% probability that the result came from chance.”

A correct statement starts from \(H_0\). If the drug does nothing, a difference of 9.5 mmHg or more occurs in 2.9% of trials like ours.

We usually compare p with a threshold \(\alpha\), the significance level, and often \(\alpha = 0.05\). If \(p < \alpha\) we reject \(H_0\). A p-value above \(\alpha\) does not prove that \(H_0\) is true. It shows that the data do not give enough evidence against it.

When \(H_0\) is true, the p-value is equally likely to be any value between 0 and 1. So a test at \(\alpha = 0.05\) rejects a true \(H_0\) in 5% of experiments. We can check this by simulation. We run 10,000 trials in which the drug does nothing.

set.seed(42)
p_null <- replicate(10000, t.test(rnorm(15, 140, 12), rnorm(15, 140, 12))$p.value)
mean(p_null < 0.05)
[1] 0.0454
Show code
tibble(p = p_null) |>
  ggplot(aes(p, fill = p < 0.05)) +
  geom_histogram(binwidth = 0.05, boundary = 0, colour = "white") +
  scale_fill_manual(values = c(`FALSE` = "grey75", `TRUE` = "firebrick"), guide = "none") +
  labs(x = "p-value", y = "number of trials")
Figure 2.3: p-values from 10,000 simulated trials in which the drug has no effect. The distribution is flat. The red bar is the 5% of trials with p < 0.05.

In 4.5% of these trials, the p-value is below 0.05. Each of them is a false positive.

2.4 What a 95% confidence interval means

The t-test also gives a 95% confidence interval for the difference in means.

tt$conf.int
[1]  1.055643 18.011024
attr(,"conf.level")
[1] 0.95

R reports the difference as Placebo minus Drug, so the interval is from 1.1 to 18.0 mmHg. The interval does not contain 0. This agrees with \(p < 0.05\). A 95% interval excludes 0 exactly when the two-sided test rejects at \(\alpha = 0.05\).

The true difference is a fixed number, and a given interval either contains it or does not. The “95%” describes the method. If you repeat the experiment many times and calculate an interval each time, 95% of the intervals will contain the true value.

In a simulation, we know the true value. We made the data with a true difference of 9 mmHg. We repeat the trial 100 times and calculate the 95% interval each time.

set.seed(100)
true_diff <- 140 - 131
cis <- map_dfr(1:100, \(i) {
  ci_i <- t.test(rnorm(15, 140, 12), rnorm(15, 131, 12))$conf.int
  tibble(trial = i, lower = ci_i[1], upper = ci_i[2])
}) |>
  mutate(covers = lower <= true_diff & upper >= true_diff)
sum(cis$covers)
[1] 95
Show code
ggplot(cis, aes(y = trial, xmin = lower, xmax = upper, colour = covers)) +
  geom_vline(xintercept = true_diff, linetype = "dashed", colour = "grey40") +
  geom_vline(xintercept = 0, colour = "grey80") +
  geom_linerange() +
  scale_colour_manual(values = c(`TRUE` = "steelblue", `FALSE` = "firebrick"), guide = "none") +
  labs(x = "95% CI for Placebo minus Drug (mmHg)", y = "trial")
Figure 2.4: 95% confidence intervals for the difference in mean SBP from 100 simulated trials. The dashed line is the true difference of 9 mmHg. Red intervals do not contain it.

Of the 100 intervals, 95 contain the true difference and 5 do not. With a different seed, the count changes a little, but it stays near 95. The width of the intervals also changes from trial to trial, because each trial has its own sample standard deviation.

54 of the 100 intervals do not contain 0. In each of those trials the t-test rejects \(H_0\) at the 5% level. In the other 46 trials, the test does not reject \(H_0\), although the drug works, so a small trial can miss a real effect.

2.5 Summary

  • The data type sets the choice of test: you can only count nominal data, you can rank ordinal data, and you can average cardinal data.
  • A paired design uses each subject as their own control, and the test uses the differences inside each pair.
  • The p-value is the probability of data at least as extreme as yours if \(H_0\) is true. It is not the probability that \(H_0\) is true.
  • A 95% confidence interval comes from a method that contains the true value in 95% of repeated experiments.