Last active
May 15, 2017 06:40
-
-
Save FlxVctr/4f0ad0e225620f1aa5b33143e4f7197d to your computer and use it in GitHub Desktop.
An independent t-test with a possibility to visually confirm normal distribution of values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import pandas as pd | |
from scipy.stats import ttest_ind | |
p_criterium = 0.01 # adjust for desired p-value | |
data_1 = [0,2,4,1,3,4,5,6,5,4,3,2] # insert array-like data | |
data_2 = [1,3,4,123,1234,3,2,3,4] # same here | |
data_1 = pd.Series(data_1) | |
data_2 = pd.Series(data_2) | |
mean_1 = data_1.mean() | |
mean_2 = data_2.mean() | |
f, axarr = plt.subplots(2, sharex=True) | |
axarr[0].hist(data_1, bins=100, histtype='step') | |
axarr[0].set_title('data_1') | |
axarr[0].axvline(mean_1, color='r') | |
axarr[0].set_ylabel('n') | |
axarr[0].annotate(xy=(mean_1, 0), s=mean_1) | |
axarr[1].hist(mean_2, bins=100, histtype='step') | |
axarr[1].set_title('data_2') | |
axarr[1].axvline(mean_2, color='r') | |
axarr[1].set_xlabel('values') | |
axarr[1].set_ylabel('n') | |
axarr[1].annotate(xy=(mean_2, 0), s=mean_2) | |
print("Please check visually for normal distribution:") | |
plt.show() | |
t, p = ttest_ind(data_1, data_2, equal_var=False) | |
print("mean 1: ", mean_1) | |
print("mean 2: ", mean_2) | |
print("t = ", t, ", p = ", p) | |
if p <= p_criterium: | |
print("Yay! p <= ", p_criterium) | |
else: | |
print("Nay :( p > ", p_criterium) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment