T Test (Students T Test) – Understanding the math and how it works (2024)

T Test (Students T Test) is a statistical significance test that is used to compare the means of two groups and determine if the difference in means is statistically significant.

In this one, you’ll understand when to use the T-Test, the different types of T-Test, math behind it, how to determine which test to choose in what situation and why, how to read from the t-tables, example situations and how to apply it in R and Python.

T Test (Students T Test) – Understanding the math and how it works (1)T Test (Students T Test) – Understanding the math and how it works. Photo by Parker Coffman.

Contents

  1. Introduction
  2. Story Behind the T-Test
  3. What are the three types of T-Tests
  4. How to perform a T-Test?
  5. T-Test Example Exercise
  6. How to implement t-test in R and Python?
  7. Conclusion
  8. Related Posts

Introduction

T Test is one of the foundational statistical tests. It is used to compare the means of two groups and determine if the difference is statistically significant. It is a very common test often used in data and statistical analysis.

So when to use the T-Test?

Let’s suppose, you have measurements from two different groups, say, you are measuring the marks scored by students from a class you attended special coaching versus those who did not. And you want to determine if the scores of those who attending the coaching is significantly higher than those who did not. You can use the T-Test to determine this.

However, depending on how the groups are chosen and how the measurements were made you will have to choose a different type of T-Test for different situations. You will see more such examples coming up.

By the end of this you will know:

  1. How the Student’s T Test got its name
  2. What is the T-Test
  3. Three Types of T-Tests
  4. How to pick the right T-Test to use in various situations
  5. Example Situations of when to use which test
  6. Formula of T-Statistic (in various situations)
  7. T Test Solved Exercise

Story Behind the T-Test

T-Test was first invented by ‘William Sealy Gosset’, when he was working at Guinness Brewery. The original goal was to select the best barley grains from small samples, when the population standard deviation was not known.

If Mr. Gossett invented it, why is it called Student’s T-Test instead of ‘Gosett’s T-Test’?

It’s said that Guinness didn’t want him to reveal some sort of industry secret which may be detrimental from a business point of view. However, they allowed him to publish it under a different name.

So he published his work in a paper title ‘Biometrika’ in 1908 under the pseudoname ‘Student’ and the name caught on. As a result this test is often referred to as ‘Student’s T Test’.

So, effectively this test has been around for more than a century and is in active use today.

This test assumes that the test statistic, which is often the ‘sample mean’, should follow a normal distribution. Let’s look at the types of T-Tests.

What are the three types of T-Tests?

    1. One Sample T-Test
      Tests the null hypothesis that the population mean is equal to a specified value `μ` based on a sample mean `x̄`.

      T Test (Students T Test) – Understanding the math and how it works (2)

      Input:
      Small numeric array (vector) and hypothesized population mean.

      Output:
      T-Statistic, P value.

      When to use (example):
      Test if the mean weight of adult male mice is 10g (population mean), given a numeric array of mouse weights.

    1. Two Sample Independent T-Test
      Tests the null hypothesis that two sample means x̄1 and x̄2 are equal. Compares the means of two numeric variables obtained from two independent groups.

      Input:
      Two numeric arrays which may or may not be of the same size. They belong to two different groups.

      Output:
      T-Statistic, P value.

      When to use (example):
      Test of the mean weight of mangoes from Farm A equals mean weight of mangoes from Farm B.

  1. Two Sample Dependent T-Test (aka Paired T-Test)
    Compare the means of two numeric variables of same size where the observations from the two variables are paired. Typically, it may be from the same entity before and after a treatment, where treatment could be showing a commercial and the measured value could be opinion score about a brand.

    Input:
    Two numeric arrays of same size and observations from the two samples are paired.

    Output:
    T-Statistic, P-Value

    When to use (example):
    Given the numeric ratings of same menu items from two different restaurants (rated by same connoisseur), determine which restaurant’s food tastes better?

How to perform a T Test?

The main part of performing the T-Test is in computing the T Statistic, which is the test statistic in this case.

The way you compute the t-statistic is different depending on the type of t-test. Let’s see how to compute it one by one.

1. One-Sample Student’s t-test:

You perform a one sample T Test when you want to test if the population mean is of a specified value μ given by a sample mean x̄.

Let me put it plainly: you have a sample of observations for which you know the mean (sample mean), and you want to test if the sample came from a population with given (hypothesized) mean.

Step 1: Define the Null and Alternate Hypothesis

H0 (Null Hypothesis): Sample Mean (x̄) = Hypothesized Population Mean(μ)

H1 (Alternate Hypothesis): Sample Mean (x̄) != Hypothesized Population Mean(μ)

Step 2: Compute the T statistic

Use the following formula to compute the t-statistic:

$$t = \frac{x̄ – μ}{s/\sqrt{n}}$$

where, x̄=sample mean, μ=population mean, n=sample size, s=sample standard error.

The only difference with the z-statistic formula is that instead of population standard deviation, it uses the sample’s standard error.

Step 3: Lookup T critical and check if computed T statistic falls in rejection region.

Based on the degrees of freedom (n-1) and the alpha level (typically 0.05), lookup the critical T value from the T-Table. If the absolute value of the computed T-statistic is greater than the critical T-value, that is, it falls in the rejection region, then we reject the null hypothesis that the population mean is of the specified value.

Alternately, you can check the output P-value. P-value is typically an output from the software as a result of the T Test. If the P-value is lesser that the significance level (typically 0.05), then reject the null hypothesis and conclude that the population mean is different from what is stated.

The t-critical score is dependent on:

1. Degrees of freedom, which in this case is sample size minus 1 (n-1).2. Chosen significance level (default: 0.05).

The shape of the T Distribution depends on the number of degrees of freedom (d.o.f), which is in turn dependent on the number of observations in the sample. It is simply (n-1). T distribution usually has fatter tail, which gets narrower as the degree of freedom increases.

As the number of observations (or d.o.f) increase, the T distribution becomes closer in shape to the standard normal distribution.

2. Independent Two-Sample T-Test:

When trying to compare two samples with a two-sample t-test, you can think of the t-test as a ratio of signal (sample means) to noise (sample variability).

$$t = \frac{signal}{noise} = \frac{difference \; in \; means}{sample \; variability} = \frac{\bar{x_1} – \bar{x_2}}{\sqrt{\frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}}}$$

where, x̄1, x̄2 = sample means, s_1^1, s_2^2 = sample variances and n_1, n_2 = sample sizes.

Once t-statistic is computed, we then compare it to the critical t-score for a given degrees of freedom df and significance level (α).

How to compute the degrees of freedom?

Since the sample sizes of the two samples can be unequal with different variances, determining the degrees of freedom is not as straight forward as you saw in 1-sample t-test.

The formula for calculating df was given by Welch-Satterthwaite as shown below.

$$df = \frac{\left(\frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}\right)^2}{\frac{1}{n_1-1}\left( \frac{s_1^2}{n1}\right)^2 + {\frac{1}{n_2-1}\left( \frac{s_2^2}{n2}\right)^2}}$$

If you have approximately same variances in each sample, though that may not always be the case, the above equation can be generalized as below:

$$df = n1 + n2 – 2$$

Once degree of freedom is calculated, for the given alpha level (say 0.05) you will lookup the T-table for the critical T value. Then, based on the T-critical, see of the computed T value falls in the rejection region. If it does, we reject the null hypothesis and understand that the means from the two samples are not the same.

Let’s solve an example problem.

T-Test Example Exercise

Problem Statement

A global fast food chain wants to venture into a new city by setting up a store in a popular mall in the city. It has shortlisted 2 popular locations A and B for the same and wants to choose one of them based on the number of foot falls per week.

The company has obtained the data for the same and wants to know which of the two locations to choose and if there is significant difference between the two.

Here’s how the footfalls per day (in 1000’s) looks like for 10 randomly chosen days:

Solution

First, let’s compare the sample means.

$$\bar{x}_{Loc A} = 121.72$$

$$\bar{x}_{Loc B} = 112.84$$

x̄_LocA – x̄_LocB = 8.88

Looks like, Location A gets 8.88k more footfalls on an average over a period of 10 days.

However, the average number of footfalls is in these locations in much more and tend to vary on a day to day basis. So, the question remains: Is an additional avg footfall of 8.88k measured over 10 days, enough to say that Loc A receives different footfalls than Loc B? Is it statistically significant?

How to find this out?

Let’s begin by framing the null and alternate hypothesis.

Step 0: Identify the type of T Test.

Well, here are the facts: There are two samples. Each sample contains the number of footfalls but since both are random samples the observations are not paired. That is, the number of footfalls of 116.2 in location A does not correspond to 110.0 in location B because they may not be captured on the same day. So, the observations are not paired, as a result, the T-Test to perform is the Two Sample Independent T Test.

Step 1: Frame the null and alternate hypothesis
Null Hypothesis H0: x̄_LocA = x̄_LocB

Alternate Hypothesis H1: x̄_LocA != x̄_LocB

Step 2: Degrees of freedom
Since this is a 2 sample t-Test, the degrees of freedom = 10 + 10 – 2 = 18

Ideally we should use Welch-Satterthwaite’s formula. But for simplicity of manual calculation, I am using this one for now.

Step 3: Compute the t-Statistic

Let’s start by computing the variance
Variance of Loc_A = 80.508
Variance of Loc_B = 95.584

$$t = \frac{\bar{x_1} – \bar{x_2}}{\sqrt{\frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}}} = \frac{8.88}{\sqrt{\frac{80.51}{10} + \frac{95.58}{10}}} = 2.1161$$

Step 4: Lookup the critical value from t-table

If the computed T-statistic falls in the rejection

Since its a two-tailed test with 18 degrees of freedom. And assuming a default 95% confidence, the critical value from the t-table is 2.101

In our case, the t-statistic (2.1161) > t-critical (2.101)

This means, the t-statistic does falls in the rejection zone and so, we reject the null hypothesis and conclude that the means are in fact different.

How to implement t-test in R and Python?

Below is a reproducible code to implement the t-test for the problem we just discussed.

How to do T Test In R?

df <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/footfalls.csv")t.test(df[, 'Loc_A'], df[, 'Loc_B'], paired=FALSE)#> Welch Two Sample t-test#> data: df$Loc_A and df$Loc_B#> t = 2.1161, df = 17.869, p-value = 0.04864#> alternative hypothesis: true difference in means is not equal to 0#> 95 percent confidence interval:#> 0.05916631 17.70083369#> sample estimates:#> mean of x mean of y #> 121.72 112.84

# Compute P Value (area to the right and left extremities of the computed T-Statistic)2 * pt(-2.1161, 18)#> 0.048532

How to do T Test In Python?

from scipy.stats import ttest_indimport pandas as pddf = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/footfalls.csv")tscore, pvalue = ttest_ind(df.Loc_A, df.Loc_B)print("t Statistic: ", tscore) print("P Value: ", pvalue)#> t Statistic: 2.1161#> P Value: 0.0485
# Compute P Value explicitly (Python)from scipy.stats import tt_dist = t(18)2 * t_dist.cdf(-2.1161)#> 0.048532

Conclusion

We have seen a lot of details both T-Test and the different types. Next, we will go through each type of T-Test in more detail with worked out examples. Stay tuned.

T Test (Students T Test) – Understanding the math and how it works (2024)

FAQs

How does a student's t-test work? ›

Student's t-test is a statistical test used to test whether the difference between the response of two groups is statistically significant or not. It is any statistical hypothesis test in which the test statistic follows a Student's t-distribution under the null hypothesis.

What is the t-test in math? ›

Mathematically, the t-test takes a sample from each of the two sets and establishes the problem statement. It assumes a null hypothesis that the two means are equal. Using the formulas, values are calculated and compared against the standard values. The assumed null hypothesis is accepted or rejected accordingly.

How do you solve a student t-test? ›

The t-score formula for an independent t-test is: t equals the mean of population 1 minus the mean of population 2 divided by the product of the pooled standard deviation and the square root of one over the sample size of sample 1 plus one over the sample size of sample 2.

What is the student's t-test result? ›

The Student's t test is used to compare the means between two groups, whereas ANOVA is used to compare the means among three or more groups. In ANOVA, first gets a common P value. A significant P value of the ANOVA test indicates for at least one pair, between which the mean difference was statistically significant.

How to understand t-test results? ›

We can work out the chances of the result we have obtained happening by chance. If a p-value reported from a t test is less than 0.05, then that result is said to be statistically significant. If a p-value is greater than 0.05, then the result is insignificant.

What is the student's t-test mainly concerned with? ›

2 The t-Test. The t-test (also sometimes called the Student t-test) is used to determine the significance of the difference between the means of two sets of data. In essence, the test compares the difference in means relative to the observed random variations in each set.

What is the t-test in layman's terms? ›

[1] In simple terms, a Student's t-test is a ratio that quantifies how significant the difference is between the 'means' of two groups while taking their variance or distribution into account.

What is the t-score in math? ›

The t-score formula is: t = x ― − μ S n , where x ― is the sample mean, μ is the population mean, S is the standard deviation of the sample, and n is the sample size.

What is the T in math? ›

The Latin letter t. In mathematics, the symbol 't' is often used in equations as a variable to represent time.

What is a good t test value? ›

Generally, a t-statistic of 2 or higher is considered to be statistically significant. However, the exact value of the t-statistic that is considered to be statistically significant will depend on the sample size and the level of confidence desired.

How do you find the student's t value? ›

The Student t -distribution is the distribution of the t -statistic given by t=¯x−μs√n t = x ¯ − μ s n where ¯x is the sample mean, μ is the population mean, s is the sample standard deviation and n is the sample size.

What are the requirements for a student t-test? ›

Most parametric tests start with the basic assumption on the distribution of populations. The conditions required to conduct the t-test include the measured values in ratio scale or interval scale, simple random extraction, normal distribution of data, appropriate sample size, and hom*ogeneity of variance.

What is the t-test for grades? ›

The t-test is used to compare two sets of scores in order to determine whether the average score (called the mean score) of the first set is significantly higher/lower than the second set.

What is a significant student's t-test? ›

The Student's t-test is a powerful and widely used tool for comparing the means of two groups to determine if they are significantly different from each other. Developed by William Sealy Gosset in 1908 under the pseudonym “Student,” this test has become a fundamental part of the statistical toolkit.

What is student's t-distribution generally used to test? ›

The Student's t distribution plays a role in a number of widely used statistical analyses, including Student's t test for assessing the statistical significance of the difference between two sample means, the construction of confidence intervals for the difference between two population means, and in linear regression ...

References

Top Articles
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 5997

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.