-
Notifications
You must be signed in to change notification settings - Fork 2
/
DepT-test.py
323 lines (287 loc) · 11.5 KB
/
DepT-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# -*- coding: utf-8 -*-
#!/usr/bin/python
# Author: Niam Moltta
# UY - 2017
# MIT License
# Two tailed T-test for differences of Means
import math
import numpy as np
import pandas as pd
from scipy.stats import norm
import scipy.stats as st
import matplotlib.pyplot as plt
import re
import seaborn
print ' '
print ' '
print ' '
print ' Welcome to DepT-test.py'
print ' --by Niam Moltta-- '
print ' ~~/\//V\ '
print ' '
print ' '
print ' '
print "Application: DIFFERENCES OF MEANS (TWO TAILED T-TEST).\n\nINSTRUCTIONS:\n\n- You must run this program in the same folder that contains your data.\n- Select file, select two columns, select alpha.\n- Returns Mean and Standard Deviation for the differences.\n- Returns t-statistic.\n- Returns p-value and t-value from the t-table.\n- Returns Cohen's D.\n- Returns Confidence Interval.\n- Returns acceptance/rejection of the null hypothesis.\n- The columns you choose must have the same length and numeric values.\n- It will ask you a lot if you are using columns from different files.\nPlease, be patient and answer.\n- The answer is important so that you can get the correct results.\n"
print ' '
while True:
fh = raw_input('Enter first .csv file name: ')
fhand = str(fh)
if (fh == 'ya') | (fh == ''):
print ' '
print 'Hasta la vista, baby'
print ' '
exit()
data = pd.read_csv(fhand, header=0)
frame = pd.DataFrame(data)
colist = frame.columns
columns = np.asarray(colist)
print ' '
print 'Columns in', re.findall('(.+?).csv', fhand), 'are:\n'
print columns
print ' '
print '--------------------------------------------------------'
hand = raw_input('Enter first column header: ')
print ' '
if (hand == 'ya') | (hand == ''):
print ' '
print 'Ciao, bambino!'
print ' '
exit()
fha = raw_input('Enter second .csv file name or "s" to continue: ') # in case column is in a different file
fhan = str(fha)
print ' '
if (fhan == 'ya') | (fhan == ''):
print ' '
print 'Hasta la vista, baby'
print ' '
exit()
elif fhan == 's':
hand2 = raw_input('Enter second column header: ')
print '--------------------------------------------------------'
print ' '
column1 = str(hand)
column2 = str(hand2)
frame['Difference'] = data[column1] - data[column2] #
Difs = frame['Difference'].values
SumDifs = sum(Difs)
nDifs = len(Difs)
AvDifs = float(SumDifs)/float(nDifs)
Avrg = float(AvDifs)
print 'Mean of differences =', Avrg
StdDevDifs = (Difs-Avrg)**2
print ' '
Var = sum(StdDevDifs)/(nDifs-1)
StdDev = math.sqrt(Var)
print 'Standard Deviation for the differences =', StdDev
print ' '
tstat = Avrg/(StdDev/math.sqrt(nDifs))
print 't-statistic =', tstat
print ' '
print '--------------------------------------------------------'
fh = raw_input('Enter alpha: ')
print '--------------------------------------------------------'
alpha = float(fh)
def t(alpha, gl):
return st.t.ppf(1-(alpha/2), gl) # alpha/2 because two tailed
gl = nDifs-1
tvalue = (t(alpha,gl))
print ' '
t = 1
if tstat < 0:
t = t*(-1)
else:
t = t*(1)
print 't-value =', tvalue*t
pvalue = st.t.sf(np.abs(tstat), nDifs-1)*2
print ' '
print 'p-value =', pvalue
print ' '
cohen = Avrg / StdDev
print "Cohen's d =", cohen
print ' '
rootn = math.sqrt(nDifs)
c = StdDev / rootn
CIa = Avrg - (tvalue*c)
CIb = Avrg + (tvalue*c)
print 'Confidence Interval = (',CIa,',',CIb,')'
print ' '
if (tstat > CIa) | (tstat < CIb):
print 'Ho = Reject'
elif (tstat < CIa) | (tstat > CIb):
print 'Ho = Fail to reject'
print ' '
else:
data2 = pd.read_csv(fhan, header=0)
frame2 = pd.DataFrame(data2)
colist2 = frame2.columns
columns2 = np.asarray(colist2)
print ' '
print 'Columns in', re.findall('(.+?).csv', fhan), 'are:\n'
print columns2
print ' '
hand2 = raw_input('Enter second column header: ')
print '--------------------------------------------------------'
print ' '
column1 = str(hand)
column2 = str(hand2)
frame['Difference'] = data[column1] - data2[column2] #
Difs = frame['Difference'].values
SumDifs = sum(Difs)
nDifs = len(Difs)
AvDifs = float(SumDifs)/float(nDifs)
Avrg = float(AvDifs)
print 'Mean of differences =', Avrg
StdDevDifs = (Difs-Avrg)**2
print ' '
Var = sum(StdDevDifs)/(nDifs-1)
StdDev = math.sqrt(Var)
print 'Standard Deviation for the differences =', StdDev
print ' '
tstat = Avrg/(StdDev/math.sqrt(nDifs))
print 't-statistic =', tstat
print ' '
print '--------------------------------------------------------'
fh = raw_input('Enter alpha: ')
print '--------------------------------------------------------'
alpha = float(fh)
def t(alpha, gl):
return st.t.ppf(1-(alpha/2), gl) # alpha/2 because two tailed
gl = nDifs-1
tvalue = (t(alpha,gl))
print ' '
t = 1
if tstat < 0:
t = t*(-1)
else:
t = t*(1)
print 't-value =', tvalue*t
pvalue = st.t.sf(np.abs(tstat), nDifs-1)*2
print ' '
print 'p-value =', pvalue
print ' '
cohen = Avrg / StdDev
print "Cohen's d =", cohen
print ' '
rootn = math.sqrt(nDifs)
c = StdDev / rootn
CIa = Avrg - (tvalue*c)
CIb = Avrg + (tvalue*c)
print 'Confidence Interval = (',CIa,',',CIb,')'
print ' '
if (tstat > CIa) | (tstat < CIb):
print 'Ho = Reject'
elif (tstat < CIa) | (tstat > CIb):
print 'Ho = Fail to reject'
print ' '
while True:
path = raw_input('Are you working on the same file? Enter "yes", or "no" to continue: ')
answer = str(path)
print ' '
if (answer == 'yes') | (answer == ''):
print 'KEYWORDS:\n\nEnter "plot" to see the columns behavior.\nEnter "graph" to see the Differences of Means distribution.\nEnter "next" to analyze another two columns in the same file.\nEnter "ya" to quit the program.\n\n'
user = raw_input('Enter keyword: ')
hands = str(user)
print ' '
if hands == 'graph':
Array = sorted(Difs)
Arr = np.asarray(Array)
pdf2 = st.norm.pdf(Arr, Avrg, tvalue)
altn = int(nDifs)
legen = ("n = "+str(altn))
fig2 = plt.plot(Arr, pdf2, label=legen)
plt.title("Differences of Means distribution")
plt.xlabel("Values")
altm = str(Avrg)
legenda = ("Mean =\n "+altm)
V1 = 0 - tvalue
V2 = 0 + tvalue
plt.axvline(x= 0, color='r', linestyle='dashed', label=legenda)
plt.axvline(x= V1, color ='g', linestyle='dashed', label=V1)
plt.axvline(x= V2, color = 'g', linestyle='dashed', label=V2)
score = ("t =\n"+str(tstat))
plt.axvline(x=tstat, color = 'purple', label=score)
print ' '
print ('To continue, you must save the figure and close it, or just close it. You can also zoom in it or move the graph to see it better, use the buttons.\n')
plt.legend()
plt.show(fig2)
print ' '
continue
elif hands == 'plot':
Array = sorted(Difs)
x = np.cumsum(data[column1])
y = np.cumsum(data[column2])
z = np.cumsum(Array)
plt.plot(x, 'b', label=column1)
plt.plot(y, 'g', label=column2)
plt.plot(z, 'r', label="Differences")
plt.title("Column behavior")
plt.xlabel("Values")
plt.ylabel("Frequency")
print ('To continue, you must save the figure and close it, or just close it. You can also zoom in it or move the graph to see it better, use the buttons.\n')
plt.legend()
plt.show()
print ' '
continue
elif hands == 'next':
break
break
elif (hands == 'ya') | (hands == ''):
print ' '
print 'Hasta la vista, baby.'
print ' '
exit()
elif answer == 'no':
print 'KEYWORDS:\n\nEnter "plot" to see the columns behavior.\nEnter "graph" to see the Differences of Means distribution.\nEnter "next" to analyze another two columns.\nEnter "ya" to quit the program.\n\n'
user = raw_input('Enter keyword: ')
hands = str(user)
print ' '
if hands == 'graph':
Array = sorted(Difs)
Arr = np.asarray(Array)
pdf2 = st.norm.pdf(Arr, Avrg, tvalue)
altn = int(nDifs)
legen = ("n = "+str(altn))
fig2 = plt.plot(Arr, pdf2, label=legen)
plt.title("Differences of Means distribution")
plt.xlabel("Values")
altm = str(Avrg)
legenda = ("Mean =\n "+altm)
V1 = 0 - tvalue
V2 = 0 + tvalue
plt.axvline(x= 0, color='r', linestyle='dashed', label=legenda)
plt.axvline(x= V1, color ='g', linestyle='dashed', label=V1)
plt.axvline(x= V2, color = 'g', linestyle='dashed', label=V2)
score = ("t =\n"+str(tstat))
plt.axvline(x=tstat, color = 'purple', label=score)
print ' '
print ('To continue, you must save the figure and close it, or just close it. You can also zoom in it or move the graph to see it better, use the buttons.\n')
plt.legend()
plt.show(fig2)
print ' '
continue
elif hands == 'plot':
Array = sorted(Difs)
x = np.cumsum(data[column1])
y = np.cumsum(data2[column2])
z = np.cumsum(Array)
plt.plot(x, 'b', label=column1)
plt.plot(y, 'g', label=column2)
plt.plot(z, 'r', label="Differences")
plt.title("Column behavior")
plt.xlabel("Values")
plt.ylabel("Frequency")
print ('To continue, you must save the figure and close it, or just close it. You can also zoom in it or move the graph to see it better, use the buttons.\n')
plt.legend()
plt.show()
print ' '
continue
elif hands == 'next':
break
elif (hands == 'ya') | (hands == ''):
print ' '
print 'Hasta la vista, baby.'
print ' '
exit()
print ' '
print 'Hasta la vista, baby.'
print ' '