24  Two Sample t-test / Independent samples t-test

The independent samples t-test, also known as the two-sample t-test or Student’s t-test, is a statistical procedure used to determine if there is a significant difference between the means of two independent groups. This test is commonly used in situations where you want to compare the means from two different groups, such as two different treatments or conditions, to see if they differ from each other in a statistically significant way.

24.0.1 Assumptions

You would use an independent samples t-test under the following conditions:

  1. Independence of Samples: The two groups being compared must be independent, meaning the samples drawn from one group do not influence the samples from the other group.
  2. Normally Distributed Data: The data in the two groups should be roughly normally distributed.
  3. Equality of Variances: The variances of the two groups are assumed to be equal. If this assumption is significantly violated, a variation of the t-test, like Welch’s t-test, may be used instead.

24.0.2 Hypotheses

The hypotheses for an independent samples t-test are usually framed as follows:

  • Null Hypothesis (H₀): The means of the two groups are equal (\(\mu_1 = \mu_2\)).
  • Alternative Hypothesis (H₁): The means of the two groups are not equal (\(\mu_1 \neq \mu_2\)), which can be two-tailed, or one-tailed if the direction of the difference is specified.

24.0.3 Formula

The t-statistic is calculated using the following formula: \[ t = \frac{\bar{X}_1 - \bar{X}_2}{s_p \cdot \sqrt{\frac{1}{n_1} + \frac{1}{n_2}}} \] Where:

  • \(\bar{X}_1\) and \(\bar{X}_2\) are the sample means of groups 1 and 2, respectively.
  • \(n_1\) and \(n_2\) are the sample sizes of groups 1 and 2, respectively.
  • \(s_p\) is the pooled standard deviation of the two samples, calculated as: \[ s_p = \sqrt{\frac{(n_1 - 1) \cdot s_1^2 + (n_2 - 1) \cdot s_2^2}{n_1 + n_2 - 2}} \]
  • \(s_1^2\) and \(s_2^2\) are the variances of the two samples.

24.0.4 Calculating Degrees of Freedom

The degrees of freedom used in this table are \(n_1 + n_2 - 2\).

24.0.5 Interpretation

To decide whether to reject the null hypothesis, compare the calculated t-value to the critical t-value from the t-distribution at the desired significance level (\(\alpha\), often 0.05 for a 5% significance level). The decision rules are:

  • If the absolute value of the calculated t-value is greater than the critical t-value, reject the null hypothesis.
  • If the absolute value of the calculated t-value is less than or equal to the critical t-value, do not reject the null hypothesis.

This test allows researchers to understand whether different conditions have a statistically significant impact on the means of the groups being compared, providing crucial insights in fields such as medicine, psychology, and economics.

24.0.6 Two Samples T-Test Example problem

Suppose we want to determine if there is a significant difference in the average test scores between two classes. Class A has 5 students, and Class B has 5 students. Here are their test scores:

  • Class A: 85, 88, 90, 95, 78
  • Class B: 80, 83, 79, 92, 87

Hypotheses:

  • Null Hypothesis (H₀): \(\mu_1 = \mu_2\) (The means of both classes are equal)
  • Alternative Hypothesis (H₁): \(\mu_1 \neq \mu_2\) (The means of both classes are not equal)

We will use a significance level (\(\alpha\)) of 0.05.

To illustrate the mathematics behind the calculations performed for the independent samples t-test, let’s break down each step using the provided scores for Class A and Class B:

Calculate the means (\(\bar{X}_1\) and \(\bar{X}_2\)):

For Class A: \[ \bar{X}_1 = \frac{85 + 88 + 90 + 95 + 78}{5} = 87.2 \]

For Class B: \[ \bar{X}_2 = \frac{80 + 83 + 79 + 92 + 87}{5} = 84.2 \]

Calculate the sample variances (\(s_1^2\) and \(s_2^2\)):

For Class A: \[ s_1^2 = \frac{(85 - 87.2)^2 + (88 - 87.2)^2 + (90 - 87.2)^2 + ....}{4} \]

\[ s_1^2 = \frac{(-2.2)^2 + (0.8)^2 + (2.8)^2 + (7.8)^2 + (-9.2)^2}{4} \] \[ s_1^2 = \frac{4.84 + 0.64 + 7.84 + 60.84 + 84.64}{4} = 39.7 \]

For Class B: \[ s_2^2 = \frac{(80 - 84.2)^2 + (83 - 84.2)^2 + (79 - 84.2)^2 + ....}{4} \] \[ s_2^2 = \frac{(-4.2)^2 + (-1.2)^2 + (-5.2)^2 + (7.8)^2 + (2.8)^2}{4} \] \[ s_2^2 = \frac{17.64 + 1.44 + 27.04 + 60.84 + 7.84}{4} = 28.7 \]

Calculate the pooled variance (\(s_p^2\)):

\[ s_p^2 = \frac{(4 \times 39.7) + (4 \times 28.7)}{8} \] \[ s_p^2 = \frac{158.8 + 114.8}{8} = 34.2 \]

Calculate the t-statistic:

\[ t = \frac{87.2 - 84.2}{\sqrt{34.2} \cdot \sqrt{\frac{1}{5} + \frac{1}{5}}} \] \[ t = \frac{3}{\sqrt{34.2} \cdot \sqrt{\frac{2}{5}}} \] \[ t = \frac{3}{\sqrt{34.2} \cdot \sqrt{0.4}} \] \[ t = \frac{3}{5.85 \cdot 0.6325} = 0.8111 \]

Degrees of freedom:

\[ \text{df} = 5 + 5 - 2 = 8 \]

The critical t-value and p-value:

The t-value needs to be compared against the critical value from a t-distribution table for df = 8 and a two-tailed test with \(\alpha = 0.05\).
If t > 2.306, the null hypothesis is rejected.
In this case, t = 0.8111, so the null hypothesis is not rejected.

24.0.7 Two samples t-test calculation using Excel:

Download the Excel file link here

24.0.8 Two-Sample T-Test calculation using R and Python:

Two-Sample T-Test
# Two-sample t-test (Independent Samples)

# Test scores for two independent classes
class_a_scores <- c(85, 88, 90, 95, 78)
class_b_scores <- c(80, 83, 79, 92, 87)

# Perform independent t-test (equal variances assumed)
result <- t.test(class_a_scores, class_b_scores, var.equal = TRUE)

# Print all results neatly
cat(
  "Group A Mean          :", round(mean(class_a_scores), 2), "\n",
  "Group B Mean          :", round(mean(class_b_scores), 2), "\n",
  "t-Statistic           :", round(result$statistic, 4), "\n",
  "Degrees of Freedom    :", result$parameter, "\n",
  "P-value (two-tailed)  :", round(result$p.value, 5), "\n",
  "Confidence Interval   :", paste(round(result$conf.int, 2), collapse = " to "), "\n"
)
Group A Mean          : 87.2 
 Group B Mean          : 84.2 
 t-Statistic           : 0.8111 
 Degrees of Freedom    : 8 
 P-value (two-tailed)  : 0.44077 
 Confidence Interval   : -5.53 to 11.53 
# Interpret the result 
alpha <- 0.05
p <- result$p.value

  
if (p < alpha) {
  cat("(P-value)",  p, "<", alpha, 
      "(significance level / alpha).",  "\n",
      " Reject the null hypothesis: 
      There is a significant difference.\n")
} else {
  cat("(P-value)",  p, ">=", alpha, 
      "(significance level / alpha).", "\n",
      " Do not Reject the null hypothesis: 
      No significant difference.\n")
}
(P-value) 0.4407658 >= 0.05 (significance level / alpha). 
  Do not Reject the null hypothesis: 
      No significant difference.
Two-Sample T-Test
import numpy as np
from scipy.stats import ttest_ind

# Test scores for two independent classes
class_a_scores = np.array([85, 88, 90, 95, 78])
class_b_scores = np.array([80, 83, 79, 92, 87])

# Significance level
alpha = 0.05

# Perform independent samples t-test (equal variances assumed)
t_stat, p_value = ttest_ind(class_a_scores, class_b_scores, equal_var=True)

# Summary statistics
mean_a = np.mean(class_a_scores)
mean_b = np.mean(class_b_scores)
sd_a = np.std(class_a_scores, ddof=1)
sd_b = np.std(class_b_scores, ddof=1)
n_a, n_b = len(class_a_scores), len(class_b_scores)

# Degrees of freedom (for equal variances)
df = n_a + n_b - 2

# Display results neatly
print(f"""
Group A Mean         : {mean_a:.2f}
Group B Mean         : {mean_b:.2f}
Group A SD           : {sd_a:.2f}
Group B SD           : {sd_b:.2f}
t-Statistic          : {t_stat:.4f}
Degrees of Freedom   : {df}
P-value (two-tailed) : {p_value:.5f}
""")

Group A Mean         : 87.20
Group B Mean         : 84.20
Group A SD           : 6.30
Group B SD           : 5.36
t-Statistic          : 0.8111
Degrees of Freedom   : 8
P-value (two-tailed) : 0.44077
p = p_value

# Interpret the result
alpha = 0.05

if p < alpha:
    print(f"(P-value) {p:.4f} < {alpha} (sig level / alpha).")
    print("Reject the null hypothesis:", "\n", "There is a significant difference.\n")
else:
    print(f"(P-value) {p:.4f} >= {alpha} (significance level / alpha).")
    print("Do not reject the null hypothesis:", "\n", "No significant difference.\n")
(P-value) 0.4408 >= 0.05 (significance level / alpha).
Do not reject the null hypothesis: 
 No significant difference.

24.0.9 Example Research Articles on Independent samples t-test:

  1. Esra Emir et al. (2025) : 👉 Download Article
  2. William Norris et al. (2025) : 👉 Download Article
  3. Aynur Bozkurt Bostancı & Musa Pullu (2025) : 👉 Download Article
  4. Kassahun T. Gessesse & Peteti Premanandam (2024) : 👉Download Article