
Conduct hypothesis tests for 1 and 2 samples (means & proportions) using normal and \(t\)-distributions.
Compare theory-based (CLT) testing with simulation/resampling methods.
Implement hypothesis tests in R using t.test(), prop.test(), and manual formulas.
Evaluate the assumptions, validity conditions, and limitations of theory-based inference.
Define null and alternative hypotheses
Set significance level
Choose a test statistic
Find the null model
Calculate observed test statistic and associated \(p\)-value
Make a decision
Normal population



Any population



Population (binary)



Scroll down
Your friend claims that they have psychic abilities and can predict the outcome of coin flips before they happen. You decide to test them by flipping a coin 100 times, and count the number of times they guessed right.
\[\hat{p} \sim N \left(p_0, \frac{p_0 (1-p_0)}{n} \right)\]
\[\hat{p} \sim N \left(0.5, \frac{0.5\times0.5}{100} \right)\]

There is not enough evidence, at \(10\%\) significance level, to suggest that your friend’s guesses were better than random guessing

When relying on the CLT, we usually use a standardized version of the test statistic;
For example, instead of using \(\hat{p} \sim N\left(p_0, \frac{p_0(1-p_0)}{n}\right)\), we use the z-score: \[Z = \frac{\hat{p} - p_0}{\sqrt{\frac{p_0(1-p_0)}{n}}}\sim N(0,1)\]
The advantage of doing this is that we can use the same process for the proportion, mean, difference in proportion, and difference in means.
Scroll down
Your friend claims that they have psychic abilities and can predict the outcome of coin flips before they happen. You decide to test them by flipping a coin 100 times, and count the number of times they guessed right.
\[Z = \frac{\hat{p} - p_0}{\sqrt{\frac{p_0(1-p_0)}{n}}}\]
\[Z \sim N \left(0, 1\right)\]

There is not enough evidence, at \(10\%\) significance level, to suggest that your friend’s guesses were better than random guessing
We can use prop.test() to perform the hypothesis test interactively:
X-squared = 1: The \(Z\)-statistic is \(Z = \sqrt{\text{X-squared}} = \sqrt{1} = 1.0\).p-value = 0.1587: Matches our manual calculation pnorm(1, lower.tail = FALSE).About \(10\%\) of the general population is left-handed, yet left-handers show up more often among elite fencers, boxers, and tennis players — a pattern some researchers attribute to the element of surprise a left-handed opponent brings against right-handed-dominated competition. A sports analyst samples \(n = 200\) players from a professional tennis ranking list and finds that \(28\) of them play left-handed. Is there evidence that left-handedness is over-represented among these professional players, compared to the general population rate of \(10\%\)?
\[H_0: p = 0.10 \quad \text{vs.} \quad H_A: p > 0.10, \qquad \alpha = 5\%\]
Given: \(x = 28\) left-handed players out of \(n = 200\) sampled, testing against \(p_0 = 0.10\). Fill in the blanks below to walk through the computation.
The sample proportion is the number of successes divided by the sample size: \(\hat{p} = x/n\).
Under \(H_0\), the standard error uses the hypothesized proportion \(p_0\), not \(\hat{p}\): \(SE = \sqrt{p_0(1-p_0)/n}\).
The \(Z\) test statistic standardizes the distance between \(\hat{p}\) and \(p_0\): \(Z = (\hat{p} - p_0)/SE\).
Since \(H_A: p > p_0\), the \(p\)-value is the area to the right of your observed \(Z\) under the standard normal curve: pnorm(z, lower.tail = FALSE).
Now confirm your hand calculation using prop.test().
prop.test() needs the number of successes (x), the sample size (n), and the hypothesized proportion (p).
Since \(H_A: p > 0.10\) is one-sided in the “greater” direction, set alternative = "greater".
Full call: prop.test(x = 28, n = 200, p = 0.10, alternative = "greater", correct = FALSE).
Scroll down
Alice, the coffee shop owner, wants to see if her new marketing campaign increased her average daily latte sales, which were 50 before the campaign. After 25 days, the average sales increased to 55 lattes per day, with a sample standard deviation of 8.
\[T \sim t_{n-1}\]
Assumptions and conditions:
\[T = \frac{55 - 50}{\frac{8}{\sqrt{25}}} = 3.125\]

There is sufficient evidence, at \(5\%\) significance level, to conclude that the average daily latte sales have increased after the new marketing campaign.
The \(25\) daily sales figures are stored in the latte_data data frame (column sales).
res_latte$statistic: The calculated \(T\)-statistic (\(T = 3.125\)).res_latte$parameter: Degrees of freedom (\(df = n - 1 = 24\)).res_latte$p.value: \(p\)-value for testing if daily sales increased.A manufacturer advertises that its new wireless earbuds last, on average, \(8\) hours per charge under continuous use. A tech reviewer is skeptical of the claim and tests a sample of \(n = 20\) units, measuring how long each lasts before dying. The sample mean is \(\bar{x} = 7.5\) hours with a sample standard deviation of \(s = 1.1\) hours. Is there evidence that the manufacturer is overstating the battery life?
\[H_0: \mu = 8 \quad \text{vs.} \quad H_A: \mu < 8, \qquad \alpha = 5\%\]
Given: \(n = 20\), \(\bar{x} = 7.5\), \(s = 1.1\), testing against \(\mu_0 = 8\).
For a one-sample \(t\)-test, the degrees of freedom are \(n - 1\).
The standard error of the sample mean is \(s / \sqrt{n}\).
The \(T\) statistic is \(T = (\bar{x} - \mu_0) / SE\).
Since \(H_A: \mu < \mu_0\), the \(p\)-value is the area to the left of your observed \(T\) under a \(t_{df}\) distribution: pt(t_stat, df) (the default is the lower tail).
The reviewer’s \(20\) battery-life measurements are stored in the battery_life vector.
t.test() needs the sample vector and the hypothesized mean (mu).
Since \(H_A: \mu < 8\), set alternative = "less".
Full call: t.test(battery_life, mu = 8, alternative = "less").
Scroll down
Example from: https://online.stat.psu.edu/stat415/lesson/9/9.4 (Pennsylvania State University)
Via a telephone poll, Time magazine asked 800 adult Americans:
“Should the federal tax on cigarettes be raised to pay for health care reform?”
The results of the survey were:
Is there sufficient evidence at the \(\alpha = 0.05\) to conclude that the two populations differ significantly with respect to their opinions?
The hypotheses are: \[H_0: p_1 - p_2 = 0\quad \text{vs.} \quad H_A: p_1 - p_2 \neq 0\]
\(p_1\): proportion of the non-smoker population who reply “yes”
\(p_2\): proportion of the smoker population who reply “yes”
where \(\hat{p}\) is the overall sample proportion, i.e., \(\hat{p} = \frac{\# Successes}{\# Total}\) (considering both groups).
\[Z \sim N \left(0, 1\right)\]
Observed proportions:
Non-smokers: \(\hat{p}_1 = \frac{351}{605} = 0.58\)
Smokers: \(\hat{p}_2 = \frac{41}{195} = 0.21\)
Overall sample proportion: \(\hat{p} = \frac{351 + 41}{605 + 195} = 0.49\)
Observed test statistic:
\[ Z = \frac{0.58 - 0.21}{\sqrt{0.49\times 0.51\left( \frac{1}{605} + \frac{1}{195}\right)}} = 8.99\]

There is sufficient evidence at the \(5\%\) significance level to conclude that the two populations differ with respect to their opinions concerning imposing a federal tax to help pay for health care reform.
For two independent proportions (Non-smokers: 351/605 vs Smokers: 41/195):
X-squared = 80.82: Taking \(\sqrt{80.82} = 8.99\), which is our \(Z\)-statistic!res_2prop$estimate: \(\hat{p}_1 = 0.580\) (non-smokers) and \(\hat{p}_2 = 0.210\) (smokers).A nonprofit is A/B testing two subject lines for its fall fundraising email. Subject line A is sent to \(500\) recipients and opened by \(40\) of them. Subject line B is sent to a different \(500\) recipients and opened by \(65\). Is there evidence that the two subject lines have different open rates?
\[H_0: p_1 - p_2 = 0 \quad \text{vs.} \quad H_A: p_1 - p_2 \neq 0, \qquad \alpha = 5\%\]
\(\hat{p}_1 = x_1/n_1\) and \(\hat{p}_2 = x_2/n_2\).
The pooled proportion combines the successes from both samples: \(\hat{p} = (x_1 + x_2)/(n_1 + n_2)\).
\(SE = \sqrt{\hat{p}(1-\hat{p})\left(\frac{1}{n_1} + \frac{1}{n_2}\right)}\), using the pooled proportion, not \(\hat{p}_1\) or \(\hat{p}_2\) individually.
\(Z = (\hat{p}_1 - \hat{p}_2)/SE\).
Since \(H_A\) is two-sided (\(\neq\)), the p-value is twice the upper-tail area: 2 * pnorm(abs(z), lower.tail = FALSE).
Confirm your hand calculation using prop.test().
Pass both counts as a vector to x and both sample sizes as a vector to n, in the same order: x = c(x1, x2), n = c(n1, n2).
Since \(H_A\) is two-sided, set alternative = "two.sided".
Full call: prop.test(x = c(40, 65), n = c(500, 500), alternative = "two.sided", correct = FALSE).
To discuss the difference in means, we need to consider the relationship between the two groups.
If the two groups are independent, we use the Welch’s two-sample t-test.
If the two groups are dependent, we use the paired t-test.
We want to see if people tend to marry later in life in the US compared to Canada.
We want to compare the red cells count in healthy people and people with leukemia.
We want to compare how much money Apple users are willing to spend on a new phone compared to Samsung users.



We want to see if a new drug is effective in reducing blood pressure. We measure the blood pressure before the treatment and after the treatment.
We want to see if married people have similar IQ levels.
We want to compare the weight of twins at birth.


Scroll down
A researcher wants to investigate whether there’s a difference in the average daily screen time between teenagers in urban and rural areas.
The researcher randomly samples 20 teenagers from urban areas and 25 teenagers from rural areas. They ask each teenager to report their average daily screen time (in hours) over the past week.
The results of the survey were:
| Group | Sample size | Sample mean | Std Dev |
|---|---|---|---|
| Urban | 20 | 6.2 hours | 1.5 hours |
| Rural | 25 | 5.5 hours | 1.2 hours |
The hypotheses are: \[H_0: \mu_1 - \mu_2 = 0\quad \text{vs.} \quad H_A: \mu_1 - \mu_2 \neq 0\]
\(\mu_1\): average screen time for teenagers in urban areas
\(\mu_2\): average screen time for teenagers in rural areas
\[T \sim t_k\] where k is \[k = \frac{ \left(\color{red}{\frac{S^2_1}{n_1}} + \color{blue}{\frac{S^2_2}{n_2}}\right)^2 }{ \color{red}{\frac{S_1^4}{n_1^2(n_1-1)}} + \color{blue}{\frac{S_2^4}{n_2^2(n_2-1)}} } \]
If both \(x_1\) and \(x_2\) follow the Normal model, there is no restriction on the sample sizes \(n_1\) and \(n_2\).
If \(x_1\) and \(x_2\) are non-Normal or follow an unknown distribution, we need reasonably large sample sizes to validate the Normal approximation by the CLT as well as the use of the t-model.
Observed test statistic:
\[T = \frac{\left(6.2 - 5.5\right) - 0}{\sqrt{\frac{1.5^2}{20} + \frac{1.2^2}{25}}} \approx 1.6973\]
Degrees of freedom (\(k\)): \[k = 35.97\]

There is sufficient evidence to conclude that teenagers in urban and rural areas have different average daily screen times.
The survey data is stored in the screen_data data frame (columns screen_time and area).
t.test() Output:
screen_time ~ area: Formula syntax (response ~ group).res_screen$parameter: Welch’s degrees of freedom (\(df \approx 35.97\)).An instructor teaches two sections of the same course, one fully online and one in-person, and wants to know whether students in the two formats spend a different amount of time studying each week. A survey of \(n_1 = 18\) online students finds a mean of \(\bar{x}_1 = 6.8\) hours with \(s_1 = 2.1\) hours; a survey of \(n_2 = 22\) in-person students finds a mean of \(\bar{x}_2 = 5.9\) hours with \(s_2 = 1.6\) hours.
\[H_0: \mu_1 - \mu_2 = 0 \quad \text{vs.} \quad H_A: \mu_1 - \mu_2 \neq 0, \qquad \alpha = 5\%\]
The Welch-Satterthwaite degrees of freedom formula is tedious to compute by hand, so it’s provided for you below in df.
\(SE = \sqrt{s_1^2/n_1 + s_2^2/n_2}\).
\(T = (\bar{x}_1 - \bar{x}_2)/SE\).
Since \(H_A\) is two-sided, the p-value is twice the upper-tail area under a \(t_{df}\) distribution: 2 * pt(abs(t_stat), df, lower.tail = FALSE).
The survey data is stored in the study_data data frame (columns hours and format).
Use formula syntax response ~ group, i.e. hours ~ format.
Since \(H_A\) is two-sided, set alternative = "two.sided".
Full call: t.test(hours ~ format, data = study_data, alternative = "two.sided").

Scroll down
A fitness instructor wants to evaluate the effectiveness of a new 8-week training program designed to improve participants’ resting heart rate (RHR). They believe the program will lower RHR.
The instructor recruits 12 participants and measures their RHR (in beats per minute) before starting the program and again after completing the 8-week program.
The data is collected as follows:
| Statistic | Before | After | Difference |
|---|---|---|---|
| Mean | 73.42 | 72.62 | 0.8 |
| Std Dev | 5.16 | 5.23 | 1.07 |
| n | 12 | 12 | 12 |
The hypotheses are: \[H_0: \mu_1 - \mu_2 = 0\quad \text{vs.} \quad H_A: \mu_1 - \mu_2 > 0\]
\(\mu_1\): average RHR before the program
\(\mu_2\): average RHR after the program
The test statistic we will use is: \[T = \frac{\bar{d} - \Delta_0}{\frac{s_d}{\sqrt{n}}}\]
where:
\[T \sim t_{n-1}\]
Assumptions and conditions for validity of using the t-model:
Observed test statistic:
\[T = \frac{0.8 - 0}{\frac{1.07}{\sqrt{12}}} \approx 2.59\]

There is not enough evidence, at \(\alpha = 1\%\), to conclude that the 8-week training program decreases participants’ average resting heart rate.
Participant resting heart rates are stored in the rhr_data data frame (columns before and after).
paired = TRUE tells t.test() to compute \(d_i = x_{\text{before}, i} - x_{\text{after}, i}\) first.res_rhr$estimate: Mean of the within-pair differences (\(\bar{d}\)).A UX researcher wants to know whether noise-cancelling headphones improve typing speed in a noisy open-plan office. The same \(15\) participants type a standardized passage twice in a simulated noisy environment – once without headphones and once with noise-cancelling headphones (order randomized to avoid practice effects). The difference in typing speed (with minus without, in words per minute) has a sample mean of \(\bar{d} = 4.2\) WPM and a sample standard deviation of \(s_d = 6.5\) WPM.
\[H_0: \mu_1 - \mu_2 = 0 \quad \text{vs.} \quad H_A: \mu_1 - \mu_2 > 0, \qquad \alpha = 5\%\]
For a paired \(t\)-test, the degrees of freedom are \(n - 1\), where \(n\) is the number of pairs (participants), not individual measurements.
\(T = \bar{d} / (s_d/\sqrt{n})\).
Since \(H_A: \mu_1 - \mu_2 > 0\), the p-value is the area to the right of your observed \(T\): pt(t_stat, df, lower.tail = FALSE).
Each participant’s typing speed (WPM) with and without headphones is stored in the with_wpm and without_wpm vectors.
Pass the two vectors in the same order as your hypotheses: with_wpm, then without_wpm.
Since this is a paired design, set paired = TRUE.
Since \(H_A: \mu_1 - \mu_2 > 0\), set alternative = "greater".
Full call: t.test(with_wpm, without_wpm, paired = TRUE, alternative = "greater").
Traditional theory based approach
Simulation approach
© 2024 Rodolfo Lourenzutti, Melissa Lee, Marie Auger-Méthé – Material Licensed under CC By-SA 4.0