Concept:
-
A paired samples t-test is used to compare the means of two samples when each observation in one sample can be paired with an observation in the other sample.
Background:
- Suppose we want to know whether a certain study program significantly impacts student performance on a particular exam. To test this, we have 15 students in a class take a pre-test. Then, we have each of the students participate in the study program for two weeks. Then, the students retake a test of similar difficulty.
To compare the difference between the mean scores on the first and second test, we use a paired samples t-test because for each student their first test score can be paired with their second test score.
1. Data from excel or given arrays:
image.png
2.Given arrays:
group3 = [88, 82, 84, 93, 75, 78, 84, 87, 95, 91, 83, 89, 77, 68, 91]
group4 = [91, 84, 88, 90, 79, 80, 88, 90, 90, 96, 88, 89, 81, 74, 92]
3. Do the test:
#pip install scipy
#pip install openpyxl
import pandas as pd
import numpy as np
# read the data from excel
group1 = np.array(pd.read_excel('C:/Users/Mr.R/Desktop/excels/zm.xlsx', sheet_name='1', usecols='C'))
group2 = np.array(pd.read_excel('C:/Users/Mr.R/Desktop/excels/zm.xlsx', sheet_name='1', usecols='D'))
print(group1, group2)
import scipy.stats as stats
#perform the paired samples t-test
paired_samples_t_test1_2 = stats.ttest_rel(group1, group2)
print("\n paired_samples_t_test1_2 result :\n", paired_samples_t_test1_2)
#or two arrays
group3 = [88, 82, 84, 93, 75, 78, 84, 87, 95, 91, 83, 89, 77, 68, 91]
group4 = [91, 84, 88, 90, 79, 80, 88, 90, 90, 96, 88, 89, 81, 74, 92]
paired_samples_t_test_3_4 = stats.ttest_rel(group3, group4)
print("\n Here is paired_samples_t_test_3_4 result:\n", paired_samples_t_test_3_4)
# Interpret the results.
# In this example, the paired samples t-test uses the following null and alternative hypotheses:
# H0: The mean pre-test and post-test scores are equal
# HA:The mean pre-test and post-test scores are not equal
# Since the p-value (0.0101) is less than 0.05, we reject the null hypothesis.
# We have sufficient evidence to say that the true mean test score is different
# for group3 and group4.
test results:
image.png
5. Interpret the results:
In this example, the paired samples t-test uses the following null and alternative hypotheses:
- H0: The mean pre-test and post-test scores are equal
- HA:The mean pre-test and post-test scores are not equal
-
Since the p-value (0.0101) is less than 0.05, we reject the null hypothesis
. We have sufficient evidence to say that the true mean test score is different for group3 and group4
.
网友评论