Python-111 Welch’s t-test in Python 韦尔奇 t -检验 2021-06-30

  • Step 1: Data preparation in a excel table:
image.png

  • Step2: code and process:
#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

#find variance for each group
# As a rule of thumb, we can assume the populations have equal variances
# if the ratio of the larger sample variance to the smaller sample variance is less than 4:1.

print("\nVariances of the groups are:\nnp.var(group1):", np.var(group1), "\nnp.var(group2):", np.var(group2))

# calculate the ratio of large variance to smaller variance
if np.var(group1) > np.var(group2):
    ratio = np.var(group1)/np.var(group2)
else:
    ratio = np.var(group2)/np.var(group1)

#calculate whether can do two-sample t-test
if ratio < 4.0:
 print("\n Calculated large/small ratio is :", ratio)
 print("\n Yeah! The population variances are equal.\n Let us perform the two sample t-test!") # change the line
else:
    print("\n Calculated large/small ratio is :", ratio)
    print("\n The population variances are not equal,can not proceed the two sample t-test!\n Should perform Welch’s t-test!")

    # The ratio of the larger sample variance to the smaller sample variance
    # is 17.333333333333332/11.209876543209877 = 1.5462555066079293, which is less than 4.
    # This means we can assume that the population variances are equal.

    # so when it is right to do the 2-sample t-test then we do!
if ratio > 4.0:
    import scipy.stats as stats

# perform two sample t-test with equal variances
    two_sample_t_test_result = stats.ttest_ind(a=group1, b=group2, equal_var=False)
    print("\n Here is Welch’s t-test result is :\n", two_sample_t_test_result)
image.png

  • Step 3: interpretation of results
  • The test statistic turns out to be 2.60254071 and the corresponding p-value is 0.03049848.
  • Since this p-value is less than 0.05, we can reject the null hypothesis of the test and conclude that there is a statistically significant difference in mean scores between the two groups.

  • Notes:
  • Note that the two sample sizes in this example were equal, but Welch’s t-test still works even if the two sample sizes are not equal.

你可能感兴趣的:(Python-111 Welch’s t-test in Python 韦尔奇 t -检验 2021-06-30)