◆ The Blueprint
The five steps

Every hypothesis test follows the same five steps: state the hypotheses, check the validity conditions, compute the test statistic, compute the p-value, and state the conclusion. Tests differ only in the statistic and its null distribution, the distribution of the statistic when H\(_0\) is true.

The test statistic

A standardized statistic measures how far the statistic is from the null mean (the mean of its null distribution), in null SDs (the standard deviation of that distribution). When H\(_0\) is true, \(\hat{p}\) has mean \(\pi_0\) and standard deviation \(\sqrt{\pi_0(1-\pi_0)/n}\).

$$\text{standardized statistic} = \dfrac{\text{statistic} - \text{null mean}}{\text{null SD}}$$
$$Z = \frac{\hat{p} - \pi_0}{\sqrt{\pi_0(1-\pi_0)/n}}, \qquad \hat{p} = \frac{k}{n}$$
Validity conditions

The standard normal is a good approximation to the null distribution when \(n\pi_0 \geq 10\) and \(n(1-\pi_0) \geq 10\): at least 10 expected successes and 10 expected failures. When the conditions fail, use the simulated p-value.

The simulation

Each simulated sample draws \(k^* \sim \text{Binomial}(n, \pi_0)\), the number of successes in \(n\) trials when H\(_0\) is true, and computes \(Z^*\) from it with the same standard error. The simulated p-value is the fraction of the \(Z^*\) values at least as extreme as the observed \(Z\), in the direction of H\(_a\).

Why the two p-values differ

The count \(k\) is a whole number, so the simulated null distribution is a set of separate bars. The simulated p-value counts the whole bar at the observed \(k\). The normal curve's area starts at the middle of that bar and covers about half of it. At the starting values, samples with exactly 30 or exactly 20 successes have probability 0.084 together, and half of that is most of the gap between the theory p-value, 0.157, and the simulated p-value, near 0.20. R's prop.test() moves \(k\) half a success toward \(n\pi_0\) by default (the continuity correction) for this reason, which gives 0.2031. The course's Z-tests, and the p-values on these tabs, use correct = FALSE.

Strength of evidence

p > 0.10: little or weak evidence against H\(_0\).

0.05 < p ≤ 0.10: moderate evidence.

0.01 < p ≤ 0.05: strong evidence.

p ≤ 0.01: very strong evidence.

In R

2 * (1 - pnorm(abs(z))) gives the two-tailed p-value, 1 - pnorm(z) the right-tailed, and pnorm(z) the left-tailed. prop.test(30, 50, p = 0.5, correct = FALSE) runs the whole test; its X-squared is \(Z^2\).

◆ The Blueprint
The test statistic
$$t = \frac{\bar{x} - \mu_0}{s/\sqrt{n}}, \qquad df = n - 1$$
The t distribution and the standard normal

The standard error \(s/\sqrt{n}\) uses the sample standard deviation \(s\) in place of the unknown \(\sigma\). That extra uncertainty gives the null distribution heavier tails than the standard normal: the t distribution with \(n - 1\) degrees of freedom. As \(n\) grows, \(s\) gets closer to \(\sigma\) and the t curve approaches the standard normal. A Z-test for a mean needs \(\sigma\) known, which is rare.

Validity conditions

The data are roughly symmetric, or \(n \geq 20\) and the data are not heavily skewed. Tintle et al. use \(n \geq 20\) as the guideline; the traditional central limit theorem threshold is \(n \geq 30\).

Tintle, N. et al. Introduction to Statistical Investigations (ISI).

The simulation

Each simulated sample draws \(n\) values from a normal population with mean \(\mu_0\) and standard deviation \(s\), then computes \(\bar{x}^*\), \(s^*\), and \(t^*\). The simulated p-value is the fraction of the \(t^*\) values at least as extreme as the observed \(t\). Because it draws normal data, the simulation matches the t curve up to simulation noise for every sample size it runs (up to 1,000); it cannot show what skewed data would do.

In R

2 * (1 - pt(abs(t), n - 1)) gives the two-tailed p-value from summary statistics. With the raw data, t.test(x, mu = 100) runs the whole test.

◆ The Blueprint
Why pool under H₀

H\(_0\) says \(\pi_1 = \pi_2\). If the two proportions are equal, the best estimate of that common proportion uses the data from both groups: the pooled proportion \(\hat{p}\). The standard error uses \(\hat{p}\) because the test statistic is computed as if H\(_0\) were true. A confidence interval for \(\pi_1 - \pi_2\) does not assume H\(_0\), so it uses each group's own proportion.

$$\hat{p} = \frac{k_1 + k_2}{n_1 + n_2}, \qquad Z = \frac{\hat{p}_1 - \hat{p}_2}{\sqrt{\hat{p}(1 - \hat{p})\left(\frac{1}{n_1} + \frac{1}{n_2}\right)}}$$
Validity conditions

Each group needs at least 10 expected successes and 10 expected failures under H\(_0\): \(n_1\hat{p}\), \(n_1(1-\hat{p})\), \(n_2\hat{p}\), and \(n_2(1-\hat{p})\) are all at least 10, with \(\hat{p}\) the pooled proportion.

Independence

The two samples must be independent. When subjects are matched or measured twice, analyze the paired differences instead.

The simulation

Each simulated pair of samples draws \(k_1^* \sim \text{Binomial}(n_1, \hat{p})\) and \(k_2^* \sim \text{Binomial}(n_2, \hat{p})\) from the pooled proportion, then computes \(Z^*\) with its own pooled proportion. A pair with no successes, or no failures, in both groups has a pooled proportion of 0 or 1, so its standard error is 0 and \(Z^*\) is undefined, so that pair is dropped.

In R

prop.test(c(60, 54), c(100, 120), correct = FALSE) runs the test; its X-squared is \(Z^2\). From summaries, 2 * (1 - pnorm(abs(z))) gives the two-tailed p-value.

◆ The Blueprint
The test statistic

SE is the standard error of \(\bar{x}_1 - \bar{x}_2\).

$$t = \frac{\bar{x}_1 - \bar{x}_2}{SE}, \qquad SE = \sqrt{\frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}}$$
Welch degrees of freedom

Welch's formula sets the degrees of freedom from both groups' variances and sizes. It is not a whole number in general.

$$df = \frac{\left(\frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}\right)^2}{\frac{\left(s_1^2/n_1\right)^2}{n_1 - 1} + \frac{\left(s_2^2/n_2\right)^2}{n_2 - 1}}$$
Welch and pooled

The pooled t-test assumes \(\sigma_1 = \sigma_2\) and uses one pooled variance with \(n_1 + n_2 - 2\) degrees of freedom. Welch's test drops that assumption and is what R's t.test() runs by default. The conservative \(df = \min(n_1 - 1, n_2 - 1)\) gives a slightly larger p-value than Welch's.

Validity conditions

Each group has at least 20 observations and is not heavily skewed, or is roughly symmetric. The traditional central limit theorem threshold is \(n \geq 30\) per group.

Tintle, N. et al. Introduction to Statistical Investigations (ISI).

The simulation

H\(_0\) says the two population means are equal, so each simulated pair of samples draws \(n_1\) values from a normal population with mean 0 and standard deviation \(s_1\), and \(n_2\) values with mean 0 and standard deviation \(s_2\). Each pair gives one Welch \(t^*\). Because it draws normal data, the simulation shows how well Welch's t curve fits when the populations are normal; it cannot show what skewed data would do.

In R

t.test(x1, x2) runs Welch's test on raw data; var.equal = TRUE runs the pooled test. From summaries, 2 * (1 - pt(abs(t), df)) with the Welch df gives the two-tailed p-value.

◆ The Blueprint
The paired t-test

Each pair gives one difference \(x_{i,1} - x_{i,2}\). The test is a one-sample t-test on the \(n\) differences, with \(\bar{x}_{\text{paired}}\) and \(s_{\text{paired}}\) their mean and standard deviation.

$$t = \frac{\bar{x}_{\text{paired}} - 0}{s_{\text{paired}}/\sqrt{n}}, \qquad df = n - 1$$
Why pairing helps

Pairing removes the variation between subjects. Each measurement includes the subject's own typical level, and subtracting one measurement from the other cancels it, so the differences show only the change within each subject. The standard deviation of the differences is then often much smaller than either group's standard deviation, which lowers the standard error and strengthens the evidence.

Paired or two-sample

Use the paired test when observations come in natural pairs: before and after measurements on the same subject, matched subjects, or repeated measures. Use the two-sample test when the groups are independent.

Validity conditions

At least 20 pairs and differences that are not heavily skewed, or roughly symmetric differences. The traditional central limit theorem threshold is \(n \geq 30\).

Tintle, N. et al. Introduction to Statistical Investigations (ISI).

The simulation

H\(_0\) says \(\mu_{\text{paired}} = 0\), so each simulated sample draws \(n\) differences from a normal population with mean 0 and standard deviation \(s_{\text{paired}}\), then computes \(t^*\). The simulated p-value is the fraction of the \(t^*\) values at least as extreme as the observed \(t\). Because it draws normal data, the simulation matches the t curve up to simulation noise for every sample size it runs (up to 1,000); it cannot show what skewed data would do.

In R

t.test(second, first, paired = TRUE) on raw data, or t.test(d, mu = 0) on the differences. From summaries, 2 * (1 - pt(abs(t), n - 1)).

The R functions, one by one
CallWhat it returns
pnorm(z)The area under the standard normal to the left of z. Right-tailed p-value: 1 - pnorm(z); two-tailed: 2 * (1 - pnorm(abs(z))).
pt(t, df)The area under the t curve with df degrees of freedom to the left of t. The tails work as for pnorm().
prop.test(k, n, p, correct = FALSE)The one- or two-proportion Z-test. X-squared is \(Z^2\).
t.test(x, mu = 100)
t.test(x1, x2)
t.test(x1, x2, paired = TRUE)
The one-sample, Welch two-sample, and paired t-tests on raw data. alternative = "greater" or "less" runs a one-tailed test.
rbinom()
rnorm()
replicate()
The simulation: draw samples as if H\(_0\) were true and compute the statistic for each.
ht_in_R.R