Statistics · Topic 12 of 13
Z Tests
Theory
While t-tests are used to compare the means of numerical data, a z-test (specifically a 2-sample test for equality of proportions) is used to analyse categorical data to compare the proportions of two different groups.
1. Performing the Test in R Studio
In R Studio, the command used to perform this test is prop.test(x = c(a, b), n = c(n1, n2)).
In this formula, x represents the number of "successes" (the specific event of interest) for group A and group B, and n represents the total number of observations (the sample size) for group A and group B.
2. Formulating Hypotheses for Z-Tests (The "Proportion" Trap)
Massive Exam Trap:
Just as t-tests require the word "mean", z-test hypotheses must explicitly use the word proportion (or percentage).
- Null Hypothesis (H0): There is no difference in the proportion of [Variable] between [Group A] and [Group B].
- Alternative Hypothesis (H1): There is a difference in the proportion of [Variable] between [Group A] and [Group B].
3. Interpreting the Output
- The p-value: If p < 0.05, you reject the null hypothesis (there is a statistically significant difference in the proportions). If p ≥ 0.05, you fail to reject the null hypothesis (there is not enough evidence to suggest a difference).
- The Confidence Interval: This gives the 95% confidence interval for the true difference between the two proportions.
- If the interval contains zero (goes from a negative number to a positive number), zero is a plausible value for the difference, so there is not enough evidence of a difference between the proportions.
- If the interval does not contain zero, it provides further evidence that a statistical difference between the proportions truly exists.
Worked examples
Example 1
Example 1: Generating the R Studio Code
A driving instructor wants to compare the first-time pass rates of pupils learning in a manual car versus an automatic car.
- Out of 150 pupils who learned in a manual car, 82 passed first time.
- Out of 120 pupils who learned in an automatic car, 78 passed first time.
State the exact R Studio command required to perform a 2-sample test for equality of proportions for this data.
prop.test(x = c(82, 78), n = c(150, 120))
Example 2
Example 2: Writing Hypotheses in Context
A quality control manager is investigating whether there is a difference in the defect rate between two manufacturing machines. Machine A produced 14 defective items out of 500. Machine B produced 22 defective items out of 450. State the appropriate null and alternative hypotheses for this investigation.
H0: There is no difference in the proportion of defective items produced by Machine A and Machine B.
H1: There is a difference in the proportion of defective items produced by Machine A and Machine B.
Example 3
Example 3: Interpreting a Z-Test (Failing to Reject H0)
A market researcher surveys a sample of adults and a sample of teenagers to see if they prefer shopping online or in-store. They run a z-test to compare the proportion who prefer shopping online. The software generates a p-value of 0.214 and a 95% confidence interval for the difference in proportions of [-0.125, 0.042].
- (a) Interpret the p-value and the result of the hypothesis test in context.
- (b) Explain how the confidence interval supports this conclusion.
(a) Since the p-value (0.214) is greater than 0.05, we fail to reject the null hypothesis. There is not enough evidence to suggest a statistically significant difference between the proportion of adults and teenagers who prefer shopping online.
(b) We can be 95% confident that the true difference in the proportions lies between -0.125 and 0.042. Because this interval contains zero, zero is a plausible value for the difference, so there is not enough evidence of a difference between the two proportions.
Example 4
Example 4: Interpreting a Z-Test (Rejecting H0)
A local council wants to know if there is a difference in the adoption of smart energy meters between City X and City Y.
- In City X, 312 out of 500 surveyed households have a smart meter.
- In City Y, 245 out of 550 surveyed households have a smart meter.
The council runs a 2-sample test for equality of proportions. The p-value is 0.00012 and the 95% confidence interval is [0.115, 0.242].
- (a) State whether the council should reject or fail to reject the null hypothesis, and state their conclusion in context.
- (b) Use the confidence interval to justify why this was the correct decision.
(a) Since the p-value (0.00012) is less than 0.05, the council should reject the null hypothesis. There is statistically significant evidence to suggest that there is a difference in the proportion of households with a smart energy meter in City X compared to City Y.
(b) We can be 95% confident that the true difference in the proportions lies between 0.115 and 0.242. Because this interval does not contain zero, it provides further statistical evidence that a true difference in the proportions exists.