Paired-samples t-test in R

Sometimes you collect data before and after an intervention – for example, stress levels before and after a mindfulness training session. Or you work with pairs, twins, or repeated measures. In such cases, you use the paired-samples t-test, also called the paired t-test.


What does the paired t-test examine?

Definition:
The paired-samples t-test examines whether two related measurements (e.g., before and after) differ significantly in their means.
Null hypothesis: $H_0: \mu_{\text{diff}} = 0$
Alternative hypothesis: $H_1: \mu_{\text{diff}} e 0$ (two-tailed)

The mean of the differences is analyzed: For each person, the difference between the two time points is calculated, and then it is tested whether this differs from zero on average.


Example: Stress before and after a training session

Suppose you are studying 20 people who participated in a stress-management training session. You measure their stress level before and after the session on a scale from 1–10.

# Simulate example data
set.seed(123)
stress_vorher <- rnorm(20, mean = 7, sd = 1)
stress_nachher <- stress_vorher - rnorm(20, mean = 0.8, sd = 0.5)

daten <- data.frame(stress_vorher, stress_nachher)

Performing the test in R

# Conducting a Paired t-Test
t.test(daten$stress_vorher, daten$stress_nachher, paired = TRUE)

Important: paired = TRUE indicates that the two variables are paired observations.


Example output (abbreviated)

Paired t-test

data: daten$stress_vorher and daten$stress_nachher
t = 5.201, df = 19, p-value = 5.13e-05
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
0.662 1.391
mean of the differences: 1.027

Interpreting the results

StatisticMeaning
t = 5.201Test statistic – how much the mean of the differences deviates from 0
df = 19Degrees of freedom: number of pairs minus 1
p-value = 0.0000513Very small p-value → the difference is significant
Confidence interval [0.66, 1.39]With 95% confidence, the true difference lies within this range
mean of differences = 1.027On average, stress was 1.03 points lower after the training

Conclusion: Stress levels decreased significantly after the training.


Visualizing the differences

# Display the difference as a histogram
diff <- daten$stress_vorher - daten$stress_nachher
hist(diff, main = "Difference: stress before - after", col = "lightgray",
xlab = "Difference values", breaks = 8)

# or boxplot
boxplot(daten$stress_vorher, daten$stress_nachher, names = c("Before", "After"),
main = "Stress level before and after training", ylab = "Stress (1-10)",
col = c("lightblue", "lightgreen"))

Summary

StepWhat you do
1Prepare two paired measurements (e.g. before/after)
2Run t.test(x, y, paired = TRUE)
3Interpret the p-value, confidence interval, and mean difference
4Optional: visualize with a histogram or boxplot

Q&A for reflection

Question 1: When do you use the paired-samples t-test?
Answer: When you have two paired measurements for each case, e.g. pre–post data or repeated measures.

Question 2: What does the test specifically assess?
Answer: Whether the mean of the differences differs significantly from zero.

Question 3: How do you run it in R?
Answer: With t.test(x, y, paired = TRUE)

Question 4: What does a p-value of 0.00005 mean?
Answer: It is extremely unlikely to obtain such a difference by chance. The result is highly significant.

Alles klar?

Ich hoffe, der Beitrag war für dich soweit verständlich. Wenn du weitere Fragen hast, nutze bitte hier die Möglichkeit, eine Frage an mich zu stellen!

Stelle Dominik eine Frage