Hypothesis Test for Comparing Two Means
Two Sample Z Test is used to test whether the means of two independent groups are significantly different when population standard deviations are known or when a large-sample z workflow is used. This guide explains the two sample z test for means with formula, assumptions, conditions, p-value, confidence interval, SPSS image output, Python charts, R validation charts and Excel workflow using G3 final grade data for the GP and MS school groups.
Google AdSense top placement reserved here
Quick Answer: Two Sample Z Test Result
The Two Sample Z Test was performed to compare the mean G3 final grade between two independent school groups: GP and MS. The GP group had n1 = 423 students and a mean G3 score of 12.577. The MS group had n2 = 226 students and a mean G3 score of 10.650.
The observed mean difference was GP − MS = 1.926. The z-test workflow used known standard deviations of σ1 = 3.000 and σ2 = 3.000. The standard error of the mean difference was SE = 0.247. The observed test statistic was z = 7.793, displayed on the chart as approximately z = 7.79. The two-tailed p-value was approximately 6.52 × 10-15, so it should be reported as p < .001. The 95% confidence interval for the mean difference was [1.442, 2.411].
Final conclusion: Since p < .001, reject the null hypothesis. The sample provides very strong evidence that the mean G3 final grade differs between GP and MS. Because the observed difference is positive, GP has a significantly higher mean final grade than MS.
Important note: A textbook two sample z test assumes known population standard deviations. In this worked example, the z-test output uses σ1 = 3.000 and σ2 = 3.000. If population standard deviations are unknown and estimated only from samples, a two-sample t test is usually the formal textbook choice.
Table of Contents
- What Is a Two Sample Z Test?
- When Should You Use a Two Sample Z Test?
- Null and Alternative Hypothesis
- Two Sample Z Test Formula
- Conditions and Assumptions
- Dataset and Variables Used
- Verified Results Summary
- SPSS Image Output and Interpretation
- Python Charts and Interpretation
- R Validation Charts and Interpretation
- Overall Image Interpretation
- How to Run the Test in SPSS, Python, R and Excel
- How to Report the Two Sample Z Test
- Common Mistakes
- Related Statistical Guides
- FAQs
What Is a Two Sample Z Test?
A Two Sample Z Test is a hypothesis test used to compare the means of two independent groups. It is also called a two-sample z-test, two sample mean z test, z test for two sample means or two sample z test for comparing two means.
The test compares two group means, usually written as x̄1 and x̄2. The null hypothesis says that the two population means are equal. The alternative hypothesis says that the two population means are different. The observed difference between the two sample means is divided by the standard error of the difference, producing a z statistic.
In this post, the outcome variable is G3 final grade. The grouping variable is school, with two independent groups: GP and MS. The research question is: Is the mean G3 final grade significantly different between GP and MS?
For background concepts, see Null and Alternative Hypothesis, P Value, Z Score, Standard Error, Standard Normal Distribution, Mean, Median and Mode and Descriptive Statistics.
When Should You Use a Two Sample Z Test?
Use a two sample z test when the goal is to compare the means of two independent groups and the population standard deviations are known. It is most suitable for continuous outcomes such as scores, marks, weights, times or measurements.
| Situation | Use Two Sample Z Test? | Reason |
|---|---|---|
| Comparing mean G3 final grade between GP and MS schools | Yes | The outcome is numeric and there are two independent groups. |
| Comparing average exam score between two schools with known σ values | Yes | The test compares two independent means using known standard deviations. |
| Comparing average delivery time between two warehouses | Yes | The outcome is continuous and the groups are independent. |
| Testing whether one mean differs from a benchmark | No | That is a one-sample mean question. Use a One Sample Z Test. |
| Comparing pass proportions between two schools | No | That is a proportion question. Use a Two Proportion Z Test or a related categorical test. |
| Population standard deviations are unknown and sample is small | Usually no | A two-sample t test is usually preferred when σ values are unknown. |
The easiest way to remember the difference is this: use One Sample Z Test for one mean, Two Sample Z Test for two independent means, One Proportion Z Test for one proportion and Two Proportion Z Test for two proportions.
Null and Alternative Hypothesis for Two Sample Z Test
The hypotheses compare the two population means. In this example, μGP is the population mean G3 final grade for GP school, and μMS is the population mean G3 final grade for MS school.
| Hypothesis | Symbolic form | Meaning in this example |
|---|---|---|
| Null hypothesis | H0: μGP = μMS | The two schools have the same population mean G3 final grade. |
| Alternative hypothesis | H1: μGP ≠ μMS | The two schools have different population mean G3 final grades. |
| Equivalent difference form | H0: μGP − μMS = 0 | There is no population mean difference between the schools. |
| Decision rule | Reject H0 if p-value < α | Using α = .05, reject H0 because p < .001. |
This example uses a two-tailed two sample z test because the alternative hypothesis says the school means are different. If the research question were specifically whether GP has a higher mean than MS, the alternative hypothesis would be H1: μGP > μMS. If the research question were whether GP has a lower mean than MS, the alternative hypothesis would be H1: μGP < μMS.
Two Sample Z Test Formula and Exact Calculation
The two sample z test formula for comparing two independent means is:
z = (x̄1 - x̄2) / sqrt[(σ1² / n1) + (σ2² / n2)]
where:
x̄1 = mean of group 1
x̄2 = mean of group 2
σ1 = known standard deviation of group 1
σ2 = known standard deviation of group 2
n1 = sample size of group 1
n2 = sample size of group 2For this worked example, GP is group 1 and MS is group 2.
| Component | Exact value | Explanation |
|---|---|---|
| Group 1 | GP | First school group. |
| Group 2 | MS | Second school group. |
| GP sample size | n1 = 423 | Total GP students. |
| MS sample size | n2 = 226 | Total MS students. |
| GP mean | x̄1 = 12.577 | Mean G3 final grade for GP. |
| MS mean | x̄2 = 10.650 | Mean G3 final grade for MS. |
| Known standard deviation 1 | σ1 = 3.000 | Known standard deviation used for GP. |
| Known standard deviation 2 | σ2 = 3.000 | Known standard deviation used for MS. |
| Mean difference | x̄1 − x̄2 = 1.926 | GP mean is 1.926 grade points higher than MS. |
| Standard error | SE = 0.247 | Standard error of the difference between means. |
| Z statistic | z = 7.793 | The observed difference is 7.793 standard errors above zero. |
| Two-tailed p-value | p < .001 | Reject the null hypothesis. |
| 95% CI for difference | [1.442, 2.411] | The interval is entirely above zero. |
x̄GP = 12.577
x̄MS = 10.650
Difference = x̄GP - x̄MS
Difference = 12.577 - 10.650
Difference = 1.926
SE = sqrt[(3.000² / 423) + (3.000² / 226)]
SE = 0.247
z = 1.926 / 0.247
z = 7.793The 95% confidence interval for the mean difference is calculated as:
95% CI = difference ± 1.96 × SE
95% CI = 1.926 ± 1.96 × 0.247
95% CI = [1.442, 2.411]The confidence interval is entirely above 0, which supports the same conclusion as the p-value. GP has a higher mean G3 final grade than MS. For more detail on interval interpretation, see Confidence Interval, Margin of Error and Standard Error.
Conditions and Assumptions for Two Sample Z Test
The two sample z test assumptions explain when the z statistic and p-value can be trusted. These conditions should be checked before reporting the result.
| Condition | How to check it | This example |
|---|---|---|
| Continuous outcome | The dependent variable should be numeric. | G3 final grade is numeric. |
| Two independent groups | Each case should belong to only one group. | Each student belongs to either GP or MS. |
| Independent observations | Each observation should represent a separate case. | Each row is treated as one student record. |
| Known population standard deviations | The textbook z test assumes σ1 and σ2 are known. | The workflow uses σ1 = 3.000 and σ2 = 3.000. |
| Approximately normal sampling distribution | The sampling distribution of the mean difference should be approximately normal. | The sample sizes are large: GP n = 423 and MS n = 226. |
| No paired or repeated measurements | The two samples should not be matched pairs. | GP and MS are independent school groups. |
If population standard deviations are not known, a two-sample t test is normally preferred. If the main concern is whether group variances are similar, use a variance test or assumption check such as Levene Test, Brown-Forsythe Test or Bartlett’s Test. For normality context, compare the distribution with Q Q Plot Normality Check, P P Plot Normality Check, Shapiro Wilk Test and Kolmogorov Smirnov Test.
Dataset and Variables Used
This worked example uses student performance data. The outcome variable is G3 final grade. The grouping variable is school, with two categories: GP and MS. The two sample z test compares the mean G3 final grade between these two schools.
| Item | Value | Explanation |
|---|---|---|
| Outcome variable | G3 | Final exam grade. |
| Outcome scale | 0 to 20 | G3 is measured as a numeric grade score. |
| Grouping variable | school | Two independent school groups. |
| Group 1 | GP | n = 423, mean G3 = 12.577. |
| Group 2 | MS | n = 226, mean G3 = 10.650. |
| Known standard deviations | σ1 = 3.000, σ2 = 3.000 | Values used in the z-test standard error. |
External dataset source: UCI Machine Learning Repository: Student Performance dataset.
Google AdSense middle placement reserved here
Verified SPSS, Python and R Results Summary
The SPSS image output, Python charts and R validation charts all support the same statistical conclusion. GP has a higher mean G3 final grade than MS. The mean difference is 1.926, the confidence interval is entirely above zero, and the observed z statistic is far beyond the usual two-tailed critical values of ±1.96.
| Statistic | Exact value | Interpretation |
|---|---|---|
| GP sample size | 423 | Total GP students. |
| MS sample size | 226 | Total MS students. |
| GP mean G3 | 12.577 | Average final grade in GP. |
| MS mean G3 | 10.650 | Average final grade in MS. |
| Mean difference | 1.926 | GP is 1.926 grade points higher than MS. |
| Standard error | 0.247 | Standard error of the mean difference. |
| Z statistic | 7.793 | The difference is far into the rejection region. |
| Two-tailed p-value | p < .001 | Reject the null hypothesis. |
| 95% confidence interval | [1.442, 2.411] | The interval is entirely above zero. |
Main interpretation: The two sample z test indicates that the mean final exam grade is significantly higher in GP than in MS. The result is statistically significant and practically meaningful because the estimated mean difference is about 1.93 grade points.
SPSS Image Output and Interpretation
The SPSS image output explains the Two Sample Z Test visually. These images show the G3 distribution by school, the group mean comparison, the confidence interval for the mean difference, the z statistic on the standard normal curve, the boxplot by school and subgroup context by sex and study time.
1. SPSS Image: G3 Distribution by School

This SPSS image shows the distribution of G3 final grades for the GP and MS school groups. It also marks the two group means. The GP mean is 12.577, and the MS mean is 10.650. The GP mean line is visibly to the right of the MS mean line, which means GP students have a higher average final grade.
The distribution view is important because it shows the raw grade pattern before the z statistic is calculated. The two schools overlap because both groups contain students with low, middle and high G3 grades. However, the center of the GP distribution is higher. The raw mean difference is 1.926 grade points.
This image supports the Two Sample Z Test because the main test question is whether this visible mean difference is large enough relative to its standard error to reject the null hypothesis. The later z curve confirms that the difference is statistically significant.
2. SPSS Image: Group Mean Comparison by School

This SPSS image focuses directly on the two group means. GP has a mean G3 score of 12.577, while MS has a mean G3 score of 10.650. The difference is 1.926 grade points in favor of GP.
This is the central comparison in the Two Sample Z Test. The null hypothesis states that the two population means are equal. The observed data show that the GP sample mean is clearly higher than the MS sample mean.
The chart gives a simple practical interpretation: GP students scored about 1.93 final grade points higher on average. The z test then determines whether this observed difference is too large to be explained by ordinary sampling variation.
3. SPSS Image: Confidence Interval for Mean Difference

This SPSS image shows the confidence interval for the difference in means. The observed mean difference is 1.926, and the 95% confidence interval is [1.442, 2.411]. The horizontal reference line for no mean difference is 0.
The entire confidence interval is above zero. This means that even after accounting for sampling uncertainty, the estimated population mean difference remains positive. In plain language, the GP mean is higher than the MS mean by a likely amount between about 1.44 and 2.41 grade points.
This image is one of the clearest pieces of evidence in the post. It supports the p-value decision because an interval entirely above zero corresponds to rejecting the null hypothesis of no mean difference at the 5% level.
4. SPSS Image: Z Statistic on the Standard Normal Curve

This SPSS image places the observed z statistic on the standard normal curve. The chart shows z = 7.79. For a two-tailed test at α = .05, the usual critical values are approximately -1.96 and +1.96.
The observed z statistic is far to the right of +1.96. This means the observed difference in means is far larger than what would normally be expected if the two population means were equal.
The curve explains why the p-value is extremely small. A z value near 7.79 is deep in the rejection region. Therefore, the test rejects H0: μGP = μMS.
5. SPSS Image: G3 Boxplot by School

This SPSS boxplot gives a distribution-based view of the school difference. The GP median is 13, while the MS median is 11. The GP interquartile range is approximately 11 to 14, while the MS interquartile range is approximately 9 to 13.
The boxplot supports the mean comparison because the GP distribution is generally shifted upward compared with the MS distribution. Both schools have low outliers, including values near 0, and both reach high values near 19. However, the center of the GP group is higher.
This image does not replace the Two Sample Z Test, but it provides valuable context. It shows that the significant mean difference is consistent with the overall shape and center of the distributions.
6. SPSS Image: Mean G3 by Sex Within School

This SPSS image provides subgroup context by sex within each school. Among female students, GP has n = 237 and mean G3 13.004, while MS has n = 146 and mean G3 11.034. Among male students, GP has n = 186 and mean G3 12.032, while MS has n = 80 and mean G3 9.950.
The chart shows that GP has a higher mean G3 than MS within both sex categories. The female subgroup difference is approximately 1.970 points, and the male subgroup difference is approximately 2.082 points.
This is descriptive context, not the formal two sample z test itself. The formal test compares the overall school means. However, the subgroup chart supports the interpretation that the GP advantage is visible within both female and male students.
7. SPSS Image: Mean G3 by Study Time Within School

This SPSS image shows mean G3 across study-time categories within each school. In study-time category 1, GP has n = 119 and mean G3 11.529, while MS has n = 93 and mean G3 9.968. In category 2, GP has n = 206 and mean G3 12.733, while MS has n = 99 and mean G3 10.758. In category 3, GP has n = 71 and mean G3 13.563, while MS has n = 26 and mean G3 12.308. In category 4, GP has n = 27 and mean G3 13.407, while MS has n = 8 and mean G3 11.875.
The chart shows that GP has a higher mean G3 than MS in every study-time category. The differences are approximately 1.562, 1.975, 1.256 and 1.532 grade points for categories 1 through 4.
This chart is descriptive context. The formal Two Sample Z Test compares the overall GP and MS means. A separate model would be needed to formally adjust for study time, but this image shows that the school difference is visible across the study-time groups.
Python Charts and Interpretation
The Python charts validate the same Two Sample Z Test result using a programmatic workflow. They confirm the group distributions, mean comparison, confidence interval, z statistic, boxplot and subgroup patterns.
1. Python Chart: G3 Distribution by School

The Python distribution chart confirms the same school pattern shown in the SPSS image. GP has a mean G3 score of 12.577, and MS has a mean G3 score of 10.650. The GP mean line appears to the right of the MS mean line.
The chart shows that both distributions overlap, but the center of the GP distribution is higher. This is why the mean difference is positive. The raw difference between the two means is 1.926 grade points.
This Python chart is useful because it validates that the difference is visible in the data distribution, not only in a final test table. It supports the interpretation that GP students have a higher average final grade than MS students.
2. Python Chart: Group Mean Comparison by School

The Python group-mean chart shows GP mean = 12.577 and MS mean = 10.650. The visual gap between the bars is the main effect tested by the Two Sample Z Test.
The chart confirms that GP is higher than MS by 1.926 grade points. Because the standard error of the difference is only about 0.247, this difference becomes a very large standardized z statistic.
The Python chart therefore validates the same conclusion as SPSS: the school means are not equal in the sample, and the formal z test confirms that the difference is statistically significant.
3. Python Chart: Confidence Interval for Mean Difference

The Python confidence interval chart confirms that the mean difference is 1.926, with a 95% confidence interval of [1.442, 2.411]. The no-difference value is 0.
The entire interval is above zero. This means the estimated mean difference remains positive after sampling uncertainty is included. The population mean G3 for GP is likely higher than the population mean G3 for MS.
The confidence interval also gives practical size. It suggests that GP is likely between about 1.44 and 2.41 grade points higher than MS on average.
4. Python Chart: Z Statistic on the Standard Normal Curve

The Python standard normal curve chart shows the observed z statistic at about 7.79. This is far beyond the usual two-tailed critical values of ±1.96.
The chart explains the p-value decision. If the null hypothesis of equal means were true, a z statistic this extreme would be extraordinarily unlikely. Therefore, the p-value is extremely small and should be reported as p < .001.
The Python chart confirms the formal decision: reject the null hypothesis and conclude that the GP and MS mean G3 scores are significantly different.
5. Python Chart: G3 Boxplot by School

The Python boxplot confirms the distribution pattern. GP has a higher center than MS, with GP median 13 and MS median 11. The GP middle 50% of scores is approximately 11 to 14, while the MS middle 50% is approximately 9 to 13.
The boxplot shows that GP performance is generally shifted upward compared with MS. Both schools include low outliers, and both have high scores near the top of the scale. However, the central tendency is higher for GP.
This chart supports the Two Sample Z Test interpretation because the mean difference is consistent with the distribution-level comparison.
6. Python Chart: Mean G3 by Sex Within School

The Python sex-within-school chart confirms the subgroup pattern. For female students, GP has mean G3 13.004 and MS has mean G3 11.034. For male students, GP has mean G3 12.032 and MS has mean G3 9.950.
In both sex groups, GP has a higher average G3 score than MS. This supports the descriptive interpretation that the GP advantage is not limited to only one sex category.
The formal test still compares the overall school means. This subgroup chart is a context chart that helps explain the overall result.
7. Python Chart: Mean G3 by Study Time Within School

The Python study-time chart confirms that GP has a higher mean G3 than MS across all four study-time categories. The exact category means are: category 1, GP = 11.529 and MS = 9.968; category 2, GP = 12.733 and MS = 10.758; category 3, GP = 13.563 and MS = 12.308; category 4, GP = 13.407 and MS = 11.875.
This descriptive chart shows that the overall school difference appears across study-time categories. GP remains higher than MS in each category.
The chart is not an adjusted hypothesis test, but it provides important context. A regression or ANOVA-style model would be needed to formally adjust for study time.
R Validation Charts and Interpretation
The R charts provide another independent validation of the same Two Sample Z Test result. R confirms the distribution, mean comparison, confidence interval, z curve, boxplot and subgroup patterns.
1. R Chart: G3 Distribution by School

The R distribution chart confirms the same result shown by SPSS and Python. The GP distribution has a higher center than the MS distribution. The group means are 12.577 for GP and 10.650 for MS.
The mean lines show that the average final grade is higher in GP. The difference between the group means is 1.926 grade points.
Because the R chart matches the SPSS and Python chart, it validates that the result is not a software-specific artifact.
2. R Chart: Group Mean Comparison by School

The R group-mean chart confirms the same mean values: GP = 12.577 and MS = 10.650. The bar for GP is clearly higher than the bar for MS.
This chart directly supports the hypothesis-test comparison. The null hypothesis says the two school means are equal, but the observed means differ by 1.926 grade points.
The R chart confirms the same practical conclusion: GP has the higher average G3 final grade.
3. R Chart: Confidence Interval for Mean Difference

The R confidence interval chart confirms that the estimated mean difference is 1.926, with a 95% confidence interval of [1.442, 2.411].
The interval does not cross zero. This means the estimated GP mean is higher than the estimated MS mean even after accounting for sampling uncertainty.
The R confidence interval supports the same final decision: reject the null hypothesis of equal population means.
4. R Chart: Z Statistic on the Standard Normal Curve

The R standard normal curve chart validates the formal z-test decision. The observed z statistic is about 7.79, far beyond the usual critical values of ±1.96.
This means the observed mean difference is far outside the range expected under the null hypothesis of equal means. The p-value is extremely small.
Therefore, the R chart supports the same conclusion as SPSS and Python: reject H0: μGP = μMS.
5. R Chart: G3 Boxplot by School

The R boxplot confirms the same distribution pattern. The GP median is 13, and the MS median is 11. The GP middle range is higher than the MS middle range.
The boxplot helps readers see that the difference is not only a single mean value. The overall distribution of GP scores is shifted upward compared with MS.
Like the SPSS and Python boxplots, the R boxplot provides descriptive evidence that supports the significant mean difference.
6. R Chart: Mean G3 by Sex Within School

The R sex-within-school chart confirms that GP has a higher mean G3 than MS for both female and male students. Female students have mean G3 values of 13.004 in GP and 11.034 in MS. Male students have mean G3 values of 12.032 in GP and 9.950 in MS.
The chart supports the descriptive pattern that the GP advantage is visible across sex categories. It does not replace the overall two-sample test.
If the research question were about sex differences or interaction effects, a separate model would be required. Here, the chart helps interpret the overall school comparison.
7. R Chart: Mean G3 by Study Time Within School

The R study-time chart confirms that GP has a higher mean G3 than MS across all study-time categories. The exact category means are: study time 1, GP = 11.529 and MS = 9.968; study time 2, GP = 12.733 and MS = 10.758; study time 3, GP = 13.563 and MS = 12.308; study time 4, GP = 13.407 and MS = 11.875.
This validation chart shows that the GP advantage is not limited to only one study-time category. GP remains higher in every displayed category.
The formal Two Sample Z Test remains the overall comparison of school means. The study-time chart is supporting context.
Overall Interpretation of All SPSS, Python and R Images
All image sets tell the same statistical story. The distribution images show that GP has a higher center than MS. The mean comparison images show 12.577 versus 10.650. The confidence interval images show that the mean difference is 1.926 with a 95% confidence interval of [1.442, 2.411]. The z curve images show that z = 7.79 is far into the rejection region.
| Image type | Main message | How it supports the test |
|---|---|---|
| G3 distribution images | GP distribution is centered higher than MS | Shows the raw data context for the mean comparison. |
| Group mean comparison images | GP = 12.577 and MS = 10.650 | Shows the observed mean difference. |
| Confidence interval images | Difference = 1.926, 95% CI [1.442, 2.411] | Shows the difference is positive and does not include zero. |
| Z curve images | Observed z = 7.79 | Shows the result is far beyond ±1.96. |
| Boxplot images | GP median = 13 and MS median = 11 | Shows GP has a higher distribution center. |
| Sex subgroup images | GP is higher for both female and male students | Provides descriptive subgroup support. |
| Study-time subgroup images | GP is higher in all study-time categories | Provides descriptive context across study-time groups. |
The final decision is consistent across SPSS, Python and R: reject the null hypothesis. The mean G3 final grade is significantly different between GP and MS, and GP has the higher mean.
How to Run Two Sample Z Test in SPSS, Python, R and Excel
SPSS Method
SPSS can calculate the group means and sample sizes with descriptive statistics. The z statistic can then be computed using the known standard deviations and the two-sample z formula.
* Two Sample Z Test in SPSS.
* Compare mean G3 between GP and MS.
* Known standard deviations used: sigma_GP = 3.000, sigma_MS = 3.000.
MEANS TABLES=G3 BY school
/CELLS=COUNT MEAN STDDEV.
* Enter exact summary values from the output.
INPUT PROGRAM.
DATA LIST FREE /n_GP n_MS mean_GP mean_MS sigma_GP sigma_MS.
BEGIN DATA
423 226 12.576832 10.650442 3.000 3.000
END DATA.
END FILE.
END INPUT PROGRAM.
COMPUTE diff = mean_GP - mean_MS.
COMPUTE se_diff = SQRT((sigma_GP ** 2 / n_GP) + (sigma_MS ** 2 / n_MS)).
COMPUTE z_value = diff / se_diff.
COMPUTE p_value_two_tailed = 2 * (1 - CDF.NORMAL(ABS(z_value), 0, 1)).
COMPUTE ci_95_low = diff - 1.96 * se_diff.
COMPUTE ci_95_high = diff + 1.96 * se_diff.
EXECUTE.
FORMATS diff se_diff z_value p_value_two_tailed ci_95_low ci_95_high (F12.6).
LIST.Python Method
Python can calculate the Two Sample Z Test from the group sample sizes, group means and known standard deviations.
import math
# Exact values from the school mean comparison
n_gp = 423
n_ms = 226
mean_gp = 12.576832
mean_ms = 10.650442
sigma_gp = 3.000
sigma_ms = 3.000
alpha = 0.05
diff = mean_gp - mean_ms
se_diff = math.sqrt((sigma_gp**2 / n_gp) + (sigma_ms**2 / n_ms))
z_value = diff / se_diff
p_value_two_tailed = math.erfc(abs(z_value) / math.sqrt(2))
ci_low = diff - 1.96 * se_diff
ci_high = diff + 1.96 * se_diff
decision = "Reject H0" if p_value_two_tailed < alpha else "Fail to reject H0"
print("GP mean:", mean_gp)
print("MS mean:", mean_ms)
print("Mean difference:", diff)
print("SE:", se_diff)
print("z:", z_value)
print("p-value:", p_value_two_tailed)
print("95% CI:", ci_low, ci_high)
print("Decision:", decision)R Method
R can reproduce the same result using direct formula calculation.
n_gp <- 423
n_ms <- 226
mean_gp <- 12.576832
mean_ms <- 10.650442
sigma_gp <- 3.000
sigma_ms <- 3.000
alpha <- 0.05
diff <- mean_gp - mean_ms
se_diff <- sqrt((sigma_gp^2 / n_gp) + (sigma_ms^2 / n_ms))
z_value <- diff / se_diff
p_value_two_tailed <- 2 * (1 - pnorm(abs(z_value)))
ci_low <- diff - 1.96 * se_diff
ci_high <- diff + 1.96 * se_diff
decision <- ifelse(p_value_two_tailed < alpha, "Reject H0", "Fail to reject H0")
data.frame(
n_gp = n_gp,
n_ms = n_ms,
mean_gp = mean_gp,
mean_ms = mean_ms,
difference = diff,
se = se_diff,
z_value = z_value,
p_value = p_value_two_tailed,
ci_low = ci_low,
ci_high = ci_high,
decision = decision
)Excel Method
Excel can calculate the Two Sample Z Test using formulas. Enter the summary values into cells and compute the mean difference, standard error, z statistic, p-value and confidence interval.
| Excel item | Example formula | Purpose |
|---|---|---|
| GP mean | =12.576832 | Stores the GP sample mean. |
| MS mean | =10.650442 | Stores the MS sample mean. |
| Mean difference | =mean_GP_cell-mean_MS_cell | Calculates GP − MS. |
| Standard error | =SQRT((3^2/423)+(3^2/226)) | Calculates the standard error of the mean difference. |
| Z statistic | =difference_cell/se_cell | Calculates the z statistic. |
| Two-tailed p-value | =2*(1-NORM.S.DIST(ABS(z_cell),TRUE)) | Calculates the two-tailed p-value. |
| 95% CI lower | =difference_cell-1.96*se_cell | Lower confidence bound. |
| 95% CI upper | =difference_cell+1.96*se_cell | Upper confidence bound. |
How to Report the Two Sample Z Test
A complete report should include the two groups, sample sizes, group means, known standard deviations, mean difference, z statistic, p-value, confidence interval and decision.
APA-style report: A two sample z test was conducted to compare mean G3 final grade between GP and MS schools. GP had a higher mean G3 score (M = 12.577, n = 423) than MS (M = 10.650, n = 226). Using known standard deviations of σ1 = 3.000 and σ2 = 3.000, the mean difference was statistically significant, z = 7.793, p < .001, 95% CI for μGP − μMS [1.442, 2.411]. Therefore, the null hypothesis of equal means was rejected.
In plain language, GP students scored about 1.93 G3 grade points higher than MS students on average. The confidence interval suggests the population difference is likely between about 1.44 and 2.41 grade points.
Common Mistakes in Two Sample Z Test Interpretation
1. Using a one sample test for two groups
If there are two independent groups, use a two sample test. A One Sample Z Test compares one sample mean with one benchmark value.
2. Confusing a mean test with a proportion test
A Two Sample Z Test compares two means. If the outcome is pass/fail or yes/no, use a proportion test such as the Two Proportion Z Test.
3. Ignoring the known standard deviation condition
A formal z test assumes known population standard deviations. If the standard deviations are estimated from the sample, a two-sample t test is often the better textbook method.
4. Reporting p = .000
Do not write p = .000. If the p-value is extremely small, write p < .001.
5. Treating subgroup charts as adjusted hypothesis tests
The sex and study-time charts are descriptive context. They do not replace a formal adjusted regression model or stratified test.
6. Ignoring practical meaning
A statistically significant result should still be interpreted in original units. Here, the difference is about 1.93 final grade points, which is meaningful on a 0 to 20 grade scale.
7. Forgetting to check assumptions
Before reporting a two sample z test, check independence, outcome scale, group structure and the standard deviation condition. For distribution context, use Histogram Interpretation and Box Plot Interpretation.
FAQs About Two Sample Z Test
What is a Two Sample Z Test?
A Two Sample Z Test is a hypothesis test used to compare the means of two independent groups when population standard deviations are known or when a large-sample z workflow is used.
What is the Two Sample Z Test formula?
The formula is z = (x̄1 − x̄2) / sqrt[(σ1² / n1) + (σ2² / n2)].
What was the result in this example?
GP had mean G3 = 12.577 with n = 423, and MS had mean G3 = 10.650 with n = 226. The mean difference was 1.926, z = 7.793, p < .001 and 95% CI [1.442, 2.411].
When should I use a Two Sample Z Test?
Use it when comparing two independent means for a numeric outcome and when the population standard deviations are known.
What are the conditions for Two Sample Z Test?
The outcome should be continuous, the groups should be independent, observations should be independent, standard deviations should be known and the sampling distribution of the mean difference should be approximately normal.
What is the difference between One Sample Z Test and Two Sample Z Test?
One Sample Z Test compares one sample mean with one benchmark. Two Sample Z Test compares two independent sample means.
Is Two Sample Z Test the same as Two Proportion Z Test?
No. Two Sample Z Test compares two means. Two Proportion Z Test compares two proportions such as pass rates or yes/no rates.
Can I run a Two Sample Z Test in Excel?
Yes. Excel can calculate the mean difference, standard error, z statistic, p-value and confidence interval using formulas such as SQRT and NORM.S.DIST.
Should I use a t test instead of a z test?
Use a two-sample t test when the population standard deviations are unknown and must be estimated from the sample, especially in smaller samples.
How do I report a very small p-value?
Report very small p-values as p < .001 instead of p = .000.
Google AdSense bottom placement reserved here
