Paired T-Test
Calculator
Learn More
Paired T-Test
Definition
Paired T-Test is a statistical test used to compare two related/dependent samples to determine if there is a significant difference between their means. It's particularly useful when measurements are taken from the same subject before and after a treatment, or when subjects are matched pairs.
Formula
Test Statistic:
Degrees of freedom:
Confidence Intervals:
Two-sided confidence interval:
One-sided confidence intervals:
Where:
- = mean difference between paired observations
- = standard deviation of the differences
- = number of pairs
Key Assumptions
Practical Example
Testing the effectiveness of a weight loss program by measuring participants' weights before and after the program:
Given Data:
- Before weights (kg): 70, 75, 80, 85, 90
- After weights (kg): 68, 72, 77, 82, 87
- Differences (After - Before): -2, -3, -3, -3, -3
- (two-tailed test)
Hypotheses:
Null Hypothesis (): (no difference between before and after)
Alternative Hypothesis (): (there is a difference)
Step-by-Step Calculation:
- Calculate mean difference:
- Calculate standard deviation of differences:
- Degrees of freedom:
- Calculate t-statistic:
- Critical value:
- Confidence interval:
Conclusion:
, we reject the null hypothesis. There is sufficient evidence to conclude that the weight loss program resulted in a significant change in participants' weights (). We are 95% confident that the true mean difference lies between -3.2 and -2.4 kg.
Effect Size
Cohen's d for paired samples:
Interpretation guidelines:
Power Analysis
Required sample size (n) for desired power (1-β):
Where:
- = significance level
- = probability of Type II error
- = standard deviation of differences
- = minimum detectable difference
Decision Rules
Reject if:
- Two-sided test:
- Left-tailed test:
- Right-tailed test:
- Or if
Reporting Results
Standard format for scientific reporting:
Code Examples
1library(tidyverse)
2library(car)
3library(effsize)
4
5set.seed(42)
6n <- 30
7baseline <- rnorm(n, mean = 100, sd = 15)
8followup <- baseline + rnorm(n, mean = -5, sd = 5) # Average decrease of 5 units
9
10# Create data frame
11data <- tibble(
12 subject = 1:n,
13 baseline = baseline,
14 followup = followup,
15 difference = followup - baseline
16)
17
18# Basic summary
19summary_stats <- data %>%
20 summarise(
21 mean_diff = mean(difference),
22 sd_diff = sd(difference),
23 n = n()
24 )
25
26# Paired t-test
27t_test_result <- t.test(data$followup, data$baseline, paired = TRUE)
28
29# Effect size
30cohens_d <- mean(data$difference) / sd(data$difference)
31
32# Visualization
33ggplot(data) +
34 geom_point(aes(x = baseline, y = followup)) +
35 geom_abline(intercept = 0, slope = 1, linetype = "dashed") +
36 theme_minimal() +
37 labs(title = "Baseline vs Follow-up Measurements",
38 subtitle = paste("Mean difference:", round(mean(data$difference), 2)))
1import numpy as np
2from scipy import stats
3import matplotlib.pyplot as plt
4import seaborn as sns
5from statsmodels.stats.power import TTestPower
6
7# Generate example data
8np.random.seed(42)
9n = 30
10baseline = np.random.normal(100, 15, n)
11followup = baseline + np.random.normal(-5, 5, n)
12differences = followup - baseline
13
14# Basic statistics
15mean_diff = np.mean(differences)
16sd_diff = np.std(differences, ddof=1)
17se_diff = sd_diff / np.sqrt(n)
18
19# Paired t-test
20t_stat, p_value = stats.ttest_rel(followup, baseline)
21
22# Effect size
23cohens_d = mean_diff / sd_diff
24
25# Power analysis
26analysis = TTestPower()
27power = analysis.power(effect_size=cohens_d,
28 nobs=n,
29 alpha=0.05)
30
31# Visualization
32plt.figure(figsize=(12, 5))
33
34# Scatterplot
35plt.subplot(1, 2, 1)
36plt.scatter(baseline, followup)
37min_val = min(baseline.min(), followup.min())
38max_val = max(baseline.max(), followup.max())
39plt.plot([min_val, max_val], [min_val, max_val], '--', color='red')
40plt.xlabel('Baseline')
41plt.ylabel('Follow-up')
42plt.title('Baseline vs Follow-up')
43
44# Differences histogram
45plt.subplot(1, 2, 2)
46sns.histplot(differences, kde=True)
47plt.axvline(mean_diff, color='red', linestyle='--')
48plt.xlabel('Differences (Follow-up - Baseline)')
49plt.title('Distribution of Differences')
50
51plt.tight_layout()
52plt.show()
53
54print(f"Mean difference: {mean_diff:.2f}")
55print(f"Standard deviation of differences: {sd_diff:.2f}")
56print(f"t-statistic: {t_stat:.2f}")
57print(f"p-value: {p_value:.4f}")
58print(f"Cohen's d: {cohens_d:.2f}")
59print(f"Statistical Power: {power:.4f}")
Alternative Tests
Consider these alternatives when assumptions are violated:
- Wilcoxon Signed-Rank Test: When normality of differences is violated or data is ordinal
- Independent t-test: When samples are independent rather than paired
Related Calculators
One-Sample T-Test
Two-Sample T-Test (Unpaired)
T Table
One Way ANOVA Calculator
Help us improve
Found an error or have a suggestion? Let us know!