๐Ÿ“ˆ Understanding t-Test and Z-Test: Comparing Means in Statistics

๐ŸŒŸ Introduction

In data analysis and inferential statistics, we often want to test hypotheses about population means โ€” for example:

  • Do two groups differ significantly in their average income?
  • Does a new fertilizer increase crop yield compared to the previous one?
  • Are the exam scores of two classes statistically different?

To answer such questions, we use statistical hypothesis testing, and two of the most commonly used tools are the t-test and z-test.

Though both serve similar purposes โ€” testing differences in means โ€” they differ in sample size, data variance knowledge, and underlying assumptions.


๐ŸŽฏ What Are Hypothesis Tests?

Hypothesis testing helps us make decisions about a population based on sample data.

We start with:

  • Null Hypothesis (Hโ‚€): No difference or effect exists.
  • Alternative Hypothesis (Hโ‚): There is a significant difference or effect.

We then calculate a test statistic (like t or z) to determine whether to reject Hโ‚€.


โš–๏ธ Difference Between t-Test and Z-Test

Featuret-TestZ-Test
Population standard deviation (ฯƒ)UnknownKnown
Sample size (n)Small (n < 30)Large (n โ‰ฅ 30)
Distribution usedt-distributionNormal (z) distribution
Use caseCompare means when ฯƒ unknownCompare means when ฯƒ known
ExampleComparing two classroom averagesComparing a sample mean to population mean

๐Ÿ’ก Tip: When in doubt and ฯƒ is unknown (which is common in real life), use the t-test.


๐Ÿงฎ The Formulas

Z-Test Formula:

Where:

  • xฬ„: Your sample mean
  • ฮผ: The known population mean
  • ฯƒ: The known population standard deviation
  • n: Your sample size

t-Test Formula:

Where:

  • s = sample standard deviation (since ฯƒ is unknown)

๐Ÿ“Š Types of t-Tests

TypeUsed WhenExample
One-sample t-testComparing a sample mean to a known population meanIs the average yield from a new crop variety = 40 kg/acre?
Independent two-sample t-testComparing means of two independent groupsDo male and female students differ in average scores?
Paired sample t-testComparing two related groups (before-after)Did training improve employee productivity?

The t-Test

In the real world, you rarely know the population standard deviation (ฯƒ). You only have your sample to work with. The t-test is the modern solution to this problem, using the sample standard deviation (s) instead.

When to Use a t-test:

  1. Your sample size isย small (typically n < 30).
  2. Youย do not knowย the population standard deviation (ฯƒ).
  3. Your data is (approximately) normally distributed.

The t-test accounts for the extra uncertainty introduced by estimating ฯƒ from the sample. This makes its distribution (the t-distribution) slightly wider and flatter than the normal Z-distribution.

The t-Test Statistic Formula (One-Sample):
t = (xฬ„ - ฮผ) / (s / โˆšn)

  • s: The sample standard deviation

Solved Example: The New Drug Trial

A pharmaceutical company claims its new drug reduces cholesterol by an average of 30 points (the known population mean for the old drug). A small clinical trial with 15 patients uses the new drug. The sample mean reduction is 35 points with a sample standard deviation of 8 points. Test if the new drug is more effective at ฮฑ = 0.05.

Step 1: State the Hypotheses

  • Hโ‚€:ย The new drug is not more effective. ฮผ = 30 points.
  • Hโ‚:ย The new drug is more effective. ฮผ > 30 points. (One-tailed test)

Step 2: Calculate the t-Test Statistic
We have:

  • xฬ„ย = 35 points
  • ฮผย = 30 points
  • sย = 8 points
  • nย = 15

t = (35 - 30) / (8 / โˆš15) = (5) / (8 / 3.873) = 5 / 2.066 โ‰ˆ 2.420

Step 3: Find the Critical Value and Make a Decision
We need the critical value from the t-distribution table.

  • Degrees of Freedom (df):ย df = n - 1 = 15 - 1 = 14
  • Significance Level (ฮฑ):ย 0.05 for a one-tailed test.

The critical t-value for df=14 and ฮฑ=0.05 (one-tailed) is 1.761.

Decision Rule: If our t-statistic is greater than the critical value, we reject the null hypothesis.

  • Our t (2.420) > Critical t (1.761)

Step 4: Conclusion
We reject the null hypothesis. There is sufficient evidence to conclude that the new drug leads to a greater average reduction in cholesterol than the old drug.


๐Ÿ“˜ Example 1: One-Sample Z-Test

Scenario:
A tea factory claims that the average weight of tea packets is 250g.
A consumer group randomly samples 36 packets and finds the mean weight = 245g, with ฯƒ = 12g.
Test at 5% significance if the claim is valid.

Step 1:
Hโ‚€: ฮผ = 250
Hโ‚: ฮผ โ‰  250

Step 2:
Given:

Step 3: Compute Z-statistic

Step 4:
At ฮฑ = 0.05 (two-tailed), critical Z = ยฑ1.96

Step 5:
Since |โˆ’2.5| > 1.96 โ†’ Reject Hโ‚€

โœ… Conclusion: The mean weight differs significantly from 250g; the claim is not valid.


๐Ÿ“— Example 2: One-Sample t-Test

Scenario:
A farmer believes the average yield of his farm is 60 kg per acre.
From a sample of 16 plots, the mean yield is 58 kg, and the sample standard deviation is 4 kg.
Test the farmerโ€™s claim at 5% significance.

Step 1:
Hโ‚€: ฮผ = 60
Hโ‚: ฮผ โ‰  60

Step 2:
Given:
xฬ„=58, ฮผ=60, s=4, n=16

Step 3: Compute t-statistic

Step 4:
Degrees of freedom (df) = n โˆ’ 1 = 15
From t-table at ฮฑ = 0.05, two-tailed, critical t = ยฑ2.131

Step 5:
|โˆ’2| < 2.131 โ†’ Fail to reject Hโ‚€

โœ… Conclusion: The farmerโ€™s claim is valid; thereโ€™s no significant difference from 60 kg/acre.


๐Ÿ“˜ Example 3: Independent Two-Sample t-Test

Scenario:
You want to know if two fertilizers (A and B) yield different average outputs.

FertilizerMean Yield (kg/acre)Sample SizeStd. Dev.
A48105
B52124

Test at 5% significance.

Step 1:
Hโ‚€: ฮผโ‚ = ฮผโ‚‚
Hโ‚: ฮผโ‚ โ‰  ฮผโ‚‚

Step 2:

Step 3:
df โ‰ˆ nโ‚ + nโ‚‚ โˆ’ 2 = 20
Critical t = ยฑ2.086

Step 4:
|โˆ’2.07| < 2.086 โ†’ Fail to reject Hโ‚€

โœ… Conclusion: There is no significant difference between Fertilizer A and B yields.


๐Ÿ“˜ Example 3: Paired Sample t-Test

Scenario: A coach wants to know whether a 4-week training program improved athletesโ€™ sprint times. Eight athletesโ€™ 100-m times (in seconds) were recorded before and after the program.

Data

AthleteBefore (s)After (s)
18588
27882
39093
47275
58890
67679
79597
88082

(Here โ€œAfter โˆ’ Beforeโ€ is positive when the score increased โ€” use whichever direction is meaningful for your context.)

Step 1 โ€” State hypotheses

Weโ€™ll test whether the training changed (here increased) the scores.

Weโ€™ll use a two-tailed test at ฮฑ=0.05.

Step 2 โ€” Compute differences and summary statistics

Compute differences diโ€‹=Afterโˆ’Before:

d = [3, 4, 3, 3, 2, 3, 2, 2]

Now compute the sample standard deviation of the differences sd

Deviations from mean and squared deviations:

Sample variance of differences:

Sample standard deviation:

Standard error of the mean difference:

Step 3 โ€” Compute the t statistic

Step 4 โ€” Decision (compare with critical value)

Since computed t=11.0t = 11.0t=11.0 is much larger than 2.365, we reject Hโ‚€ โ€‹.

Conclusion: There is strong statistical evidence that the training program changed the athletesโ€™ scores (here, the mean score increased by 2.75 units). The result is highly significant (p โ‰ช 0.01).

Step 5 โ€” Effect size (optional but useful)

which is an extremely large effect โ€” indicating a large practical change as well as statistical significance.

Assumptions reminder

Paired t-test assumes:

  1. The differences did_idiโ€‹ are a random sample and observations are paired/related.
  2. The distribution of differences is approximately normal (for small nnn). With n=8n=8n=8 itโ€™s good to check a histogram or normality test; with larger nnn the t-test is robust.

Short interpretation

In this example the average improvement after training was 2.75 units (SD of differences = 0.71). The paired t-test yields t(7)=11.00, p<0.001. We therefore reject the null hypothesis and conclude the training produced a statistically significant improvement โ€” also a very large practical effect (Cohenโ€™s d โ‰ˆ 3.9d).


The Z-Test

The Z-test is the older, more straightforward test. It’s used when you have a clear, stable understanding of the “noise” in the entire population.

When to Use a Z-test:

  1. Your sample size isย large (typically n > 30).
  2. You know theย population standard deviation (ฯƒ).
  3. Your data is (approximately) normally distributed.

The Z-Test Statistic Formula:
The formula varies slightly depending on what you’re comparing.

  • One-Sample Z-testย (Comparing a sample mean to a population mean):
    Z = (xฬ„ - ฮผ) / (ฯƒ / โˆšn)
    • xฬ„: Your sample mean
    • ฮผ: The known population mean
    • ฯƒ: The known population standard deviation
    • n: Your sample size

Solved Example: The Soda Bottling Plant

A soda bottling plant claims its bottles are filled with 500 ml of liquid. The population standard deviation is known from the machinery specs to be 2 ml. A quality inspector takes a random sample of 35 bottles and finds an average fill of 499.2 ml. Test if the machine is under-filling at a significance level of 0.05.

Step 1: State the Hypotheses

  • Null Hypothesis (Hโ‚€):ย The machine is not under-filling. ฮผ = 500 ml.
  • Alternative Hypothesis (Hโ‚):ย The machine is under-filling. ฮผ < 500 ml. (This is aย one-tailed test)

Step 2: Calculate the Z-Test Statistic
We have:

  • xฬ„ย = 499.2 ml
  • ฮผย = 500 ml
  • ฯƒย = 2 ml
  • nย = 35

Z = (499.2 - 500) / (2 / โˆš35) = (-0.8) / (2 / 5.916) = (-0.8) / 0.338 โ‰ˆ -2.366

Step 3: Find the Critical Value and Make a Decision
For a one-tailed test at ฮฑ = 0.05, the critical Z-value from the table is -1.645.

Decision Rule: If our Z-statistic is less than the critical value, we reject the null hypothesis.

  • Our Z (-2.366) < Critical Z (-1.645)

Step 4: Conclusion
We reject the null hypothesis. There is sufficient evidence at the 0.05 significance level to conclude that the machine is under-filling the bottles.


๐Ÿ“Š Decision Rules Summary

TestConditionDecision Rule
Z-Testฯƒ known, n โ‰ฅ 30Reject Hโ‚€ if
t-Testฯƒ unknown, n < 30Reject Hโ‚€ if

Pro Tip:ย When your sample size is very large (n > 100), the t-distribution becomes almost identical to the normal Z-distribution. So, for large samples, even if you don’t know ฯƒ, you can often use a Z-test as a close approximation. However, statistical software will almost always use a t-test by default when ฯƒ is unknown.


๐Ÿง  Interpretation Tips

  • A large |t| or |Z| value โ†’ stronger evidence against Hโ‚€.
  • Always check p-values (probability of observing results if Hโ‚€ true).
  • Smaller p-value (< 0.05) โ†’ significant difference.
  • Report results as: โ€œThe mean yield was significantly higher for Fertilizer B (t(20)=2.07, p<0.05).โ€

Summary: Your Decision Flowchart

  1. Start by asking:ย What am I comparing?
    • One sample mean to a population mean? -> Use aย one-sampleย test.
    • Two independent group means? -> Use anย independent two-sampleย test.
    • The same group at two different times? -> Use aย paired sampleย test.
  2. For any of these, ask:ย Do I know the population standard deviation (ฯƒ)?
    • YESย -> Use aย Z-test.
    • NOย -> Use aย t-test. (This will be the case 99% of the time).

By understanding the “why” behind these tests, you can confidently choose the right tool to determine if the differences you see are real or just a trick of the light. Now go forth and test your hypotheses


๐Ÿงฎ Tools for Performing Tests

  • Excel: =T.TEST(range1, range2, tails, type)
  • Python: scipy.stats.ttest_ind() or ttest_rel()
  • R: t.test()
  • SPSS / Minitab / RStudio โ€“ user-friendly for non-coders

๐Ÿ“š Further Reading

Leave a comment

It’s time2analytics

Welcome to time2analytics.com, your one-stop destination for exploring the fascinating world of analytics, technology, and statistical techniques. Whether you’re a data enthusiast, professional, or curious learner, this blog offers practical insights, trends, and tools to simplify complex concepts and turn data into actionable knowledge. Join us to stay ahead in the ever-evolving landscape of analytics and technology, where every post empowers you to think critically, act decisively, and innovate confidently. The future of decision-making starts hereโ€”letโ€™s embrace it together!

Let’s connect