## Introduction

Paired t-test is used when data_1 and data_2 are from the same group of people or objects but at two different times.

In Python, we can use `scipy.stats.ttest_rel()`

to conduct paired sample t-test. The syntax is as follows.

scipy.stats.ttest_rel (data_1, data_2)

Where,

data_1: data collected at time point 1

data_2: data collected at time point 2

For example, you want to compare the difference between Exam 1 and Exam 2 for a same group of students. Here, Exam 1 is the data_1 and Exam 2 is the data_2.

## Step 1: Prepare the data for paired t-test

Suppose we would like to test whether students perform differently in Exam 1 and Exam 2. The following is the hypothetical data example for paired sample t-test.

Exam 1 | Exam 2 |
---|---|

65 | 75 |

70 | 71 |

75 | 90 |

80 | 98 |

68 | 65 |

95 | 99 |

The following generates a set of sample data for Exam 1 and Exam 2. Then, print them out.

```
# generate sample data of Exam 1
Exam1=[65,70,75,80,68,95]
# generate sample data of Exam 2
Exam2=[75,71,90,98,65,99]
# print out the sample data for Exam 1 and Exam 2
print(Exam1, Exam2)
```

[65, 70, 75, 80, 68, 95] [75, 71, 90, 98, 65, 99]

## Step 2: Use scipy.stats for paired t-test

We can use `scipy.stats.ttest_rel()`

to conduct the paired sample t-test. The following includes the Python code and the output from the function. The output includes t-statistic (-2.24) and p-valuem (0.076).

```
import scipy.stats
ttest_results=scipy.stats.ttest_rel(Exam1,Exam2)
print(ttest_results)
```

Ttest_relResult(statistic=-2.23606797749979, pvalue=0.0755868184216124)

## Step 3: Interpreatation of paired t-test output

The following is the null and alternative hypotheses for paired t-test.

*H*_{0}: Exam 1 and Exam 2 are not significantly different.

*H*_{1}: Exam 1 and Exam 2 are significantly different.

The p-value is 0.076, which is greater than 0.05. That means that we fail to reject the null hypothesis. Thus, it suggests that Exam 1 and Exam 2 scores are not significantly different from each other.

Further, we can also calculate the means of these two lists to see the mean difference. The following is the Python code to calculate the means. As we can see, Exam 1’s mean is 75.5, whereas Exam 2’s mean is 83. While the means look different, they are not statistically significantly different.

```
# define function to calculate means
def Average(lst):
return sum(lst) / len(lst)
# calculate means and print them out
print("Exam 1 average:")
print(Average(Exam1))
print("\n")
print("Exam 2 average:")
print(Average(Exam2))
```

Exam 1 average: 75.5 Exam 2 average: 83.0

## Step 4: Write the report for paired t-test

You can write out the following report for the paired t-test you just conducted.

The mean for Exam 1 is 75.5, whereas the mean for Exam 2 is 83. We conducted a paired sample test comparing Exam 1 and Exam 2. We found that

t= -2.34, andp-value = 0.076. Thus, we conclude that the difference between Exam 1 and Exam is not significant.