Paired Sample T-test in R

A paired sample t-test is used to compare the means of two related groups to determine whether there is a significant difference between them. In R, you can use the t.test() function to perform a paired sample t-test.

Here’s how to conduct a paired sample t-test in R:

  1. Import your data into R. You can use read.csv() for reading CSV files, or input your data directly as vectors.
  2. Use the t.test() function with the paired argument set to TRUE.

Here’s an example:


# Sample data
group1 <- c(12, 9, 14, 10, 8, 13, 11)
group2 <- c(15, 12, 16, 11, 10, 14, 13)

# Paired sample t-test
result <- t.test(group1, group2, paired = TRUE)

# Print the result
print(result)

Output

> # Print the result
> print(result)

Paired t-test

data: group1 and group2
t = -6.4807, df = 6, p-value = 0.0006413
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-2.755133 -1.244867
sample estimates:
mean of the differences 
-2

In this example, group1 and group2 are two related groups of data. The t.test() function performs a paired sample t-test on these groups by setting the paired argument to TRUE. The result of the test is then printed, showing the t-value, degrees of freedom, p-value, and confidence interval, among other information.

To interpret the results, look at the p-value. If the p-value is less than your chosen significance level (commonly 0.05), you can reject the null hypothesis and conclude that there is a significant difference between the means of the two groups.

Example – Sleep Data

Here’s another example of a Paired Sample T-test in R, this time using sleep data to determine if there’s a significant difference between sleep quality before and after a sleep intervention.

# Load the required packages
install.packages("datasets")
library(datasets)

# Use the sleep dataset available in R
data(sleep)

# The sleep dataset contains information on 10 individuals' sleep 
# quality before and after a sleep intervention
# The 'extra' column represents the difference in sleep quality (hours)
# The 'group' column indicates whether the data is 
# pre-intervention (1) or post-intervention (2)
# The 'ID' column represents the individual identifier

# Preview the dataset
head(sleep)

# Calculate the mean difference in sleep quality
mean_difference <- tapply(sleep$extra, sleep$group, mean)
mean_difference

# Conduct a Paired Sample T-test to check if there is a significant 
# difference in sleep quality before and after the intervention
paired_t_test <- t.test(
sleep$extra[sleep$group == 1], sleep$extra[sleep$group == 2], 
paired = TRUE)

paired_t_test

In this example, we use the built-in “sleep” dataset in R, which contains information on 10 individuals’ sleep quality before and after a sleep intervention. The Paired Sample T-test is conducted to determine if there’s a significant difference in sleep quality between the two groups (pre-intervention and post-intervention). The results of the test will show the t-statistic, degrees of freedom, p-value, and confidence interval, which can help you determine if the intervention had a significant effect on sleep quality.

Output

> paired_t_test

Paired t-test

data: sleep$extra[sleep$group == 1] and sleep$extra[sleep$group == 2]
t = -4.0621, df = 9, p-value = 0.002833
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-2.4598858 -0.7001142
sample estimates:
mean of the differences 
-1.58

Example 3 – Student Test Scores

There’s another example of a Paired Sample T-test in R, this time using a hypothetical dataset of student test scores before and after a tutoring program.

# Create a hypothetical dataset of student test scores
student_id <- 1:20
before_tutoring <- c(65, 75, 80, 60, 85, 77, 90, 
68, 50, 82, 79, 84, 87, 73, 70, 60, 55, 69, 76, 80)
after_tutoring <- c(75, 82, 88, 65, 92, 81, 95, 72, 
58, 87, 85, 90, 92, 78, 74, 66, 63, 72, 82, 86)

# Combine the data into a data frame
test_scores <- data.frame(student_id, before_tutoring, after_tutoring)

# Preview the dataset
head(test_scores)

# Calculate the mean difference in test scores
mean_difference <- mean(
test_scores$after_tutoring - test_scores$before_tutoring)
mean_difference

# Conduct a Paired Sample T-test to check if there is a 
# significant difference in test scores before and 
# after the tutoring program
paired_t_test <- t.test(
test_scores$before_tutoring, test_scores$after_tutoring, paired = TRUE)
paired_t_test

Output

> paired_t_test

Paired t-test

data: test_scores$before_tutoring and test_scores$after_tutoring
t = -15.397, df = 19, p-value = 3.475e-12
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-6.702046 -5.097954
sample estimates:
mean of the differences 
-5.9

Two sample T-test in R

Gamma Distribution in R Programming