-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathANowoa.py
511 lines (312 loc) · 12.9 KB
/
ANowoa.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# -*- coding: utf-8 -*-
#!/usr/bin/python
# Author: Niam Moltta
# UY - 2017
# License: MIT
# One way or another...
# One and Two ways ANOVA conducting with Python
# %matplotlib inline
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import matplotlib.axes
pd.set_option("display.width", 100)
import matplotlib.pylab as plt
import matplotlib
import re
from statsmodels.compat import urlopen
import numpy as np
np.set_printoptions(precision=5, suppress=True)
import seaborn
from statsmodels.formula.api import ols
from statsmodels.graphics.api import interaction_plot, abline_plot
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.stats.anova import anova_lm as lm
from scipy import stats
import matplotlib.cm as cm
warnings.filterwarnings('ignore')
print (' ')
print (' ')
print (' Welcome to ANowoa.py')
print (' -- by Niam Moltta --')
print (' ~~/\//V\ ')
print (' ')
print (' ')
print (' ')
print ("Application: Analysis of Variance (ANOVA).\n\nINSTRUCTIONS:\n\n- Make sure that the .csv file is in the same folder of this script.\n- To start, enter the name of the file without 'quotes' and ending with .csv\n Example: scores.csv\n- Enter 'ya' to select number of ways again.\n- Enter 'ya' to quit.\n- Select file, select columns to analyze by group.\n- Returns Analysis of Variance between two or more group means.\n- Returns Degrees of Freedom, Sum of Squares, Mean Square.\n- Returns F-value and p-value.\n- Returns Eta squared and Omega squared for effect size.\n- Returns scatter graph of selected variables.\n")
fhand = raw_input('Enter .csv file name: ')
filecsv = str(fhand)
if filecsv == (''):
print(' ')
print ('Ciao, human!')
print(' ')
exit()
'''
elif re.findall('^http.*$', filecsv):
try:
url = urlopen(filecsv)
dataframe = pd.read_table(url)
last = re.findall('^http.*/([a-z].+[a-z])$', filecsv)
dataframe.to_csv(str(last))
# possible conflict: S(proa) X(socio) E(educa) M(sexo)
except:
'''
data = pd.read_csv(filecsv)
print (' ')
frame = pd.DataFrame(data)
coolist = frame.columns
columns = np.asarray(coolist)
while True:
ways = raw_input('Enter number of ways to conduct the ANOVA 1/2: ')
print (' ')
hm = str(ways)
if (hm == '') | (hm == '0'):
break
elif hm == ('ya'):
break
elif hm == ('1'):
print ('Columns in', re.findall('(.+?).csv', filecsv), 'are:\n')
print (columns)
print (' ')
hand1 = raw_input('Enter first column header: ')
print (' ')
if (hand1 == 'ya') | (hand1 == ''):
print (' ')
continue
handg = raw_input('Enter "by group" column header: ')
print (' ')
column1 = str(hand1)
column2 = str(handg)
# ONE WAY ANOVA:
print (' ')
grps = pd.unique(data[column2].values)
d_data = {grp:data[column1][data[column2] == grp] for grp in grps}
k = len(pd.unique(data[column2]))
N = len(data.values)
n = data.groupby(data[column2]).size()
print ('Number of conditions:')
print ('k =', k)
print (' ')
print ('Conditions times participants:')
print ('N =', N)
print (' ')
print ('Participants in each condition:')
print ('n =', n)
DFbetween = k - 1
DFwithin = N - k
DFtotal = N - 1
print (' ')
print ('Degrees of freedom:')
print ('DFbetween =', DFbetween)
print ('DFwithin =', DFwithin)
print ('DFtotal =', DFtotal)
print (' ')
SSbetween = (sum(data.groupby(data[column2]).sum()[column1]**2)/n)-(data[column1].sum()**2)/N
print ('Sum of Squares:')
print ('SSbetween =', SSbetween)
print (' ')
Y2 = sum([value**2 for value in data[column1].values])
SSwithin = Y2 - sum(data.groupby(data[column2]).sum()[column1]**2)/n
SStotal = Y2 - (data[column1].sum()**2)/N
print ('SSwithin =', SSwithin)
print (' ')
print ('SStotal =', SStotal)
print (' ')
MSbetween = SSbetween/DFbetween
MSwithin = SSwithin/DFwithin
print ('Mean Square:')
print ('MSbetween =', MSbetween)
print (' ')
print ('MSwithin =', MSwithin)
print (' ')
F = MSbetween / MSwithin
p = stats.f.sf(F, DFbetween, DFwithin)
print 'F =', F
print ' '
print 'p =', p
print ' '
effsize = SSbetween/SStotal # eta-squared
print 'Effect size:'
print ' '
print 'eta-squared =', effsize
print ' '
om_sqrd = ((SSbetween-(DFbetween*MSwithin))/(SStotal+MSwithin))
print 'Omega squared =', om_sqrd
print ' '
print 'Drawing scatter plot...'
print ' '
arr = np.asarray(sorted(data[column1]))
arrG = np.asarray(sorted(data[column2]))
name = str(column1)+' / '+str(column2)
warnings.filterwarnings('ignore')
fig1 = plt.scatter(arr, arrG, label=name)
plt.title(name)
plt.ylim((min(arrG)-1, max(arrG)+1))
plt.xlabel(column1)
plt.ylabel(column2)
plt.show(fig1)
elif hm == '2':
print 'Columns in', re.findall('(.+?).csv', filecsv), 'are:\n'
print columns
print ' '
hand0 = raw_input('Enter "by Group A" column header: ')
print ' '
if hand0 == 'ya':
print ' '
continue
elif hand0 == '':
break
hand1 = raw_input('Enter "by Group B", or hit Return to continue: ')
if hand1 == '':
print ' '
hand2 = raw_input('Enter independent variable "X" column header: ')
print ' '
hand3 = raw_input('Enter dependent variable "Y" column header: ')
print ' '
#M = str(hand1)
E = str(hand0)
X = str(hand2)
S = str(hand3)
# TWO WAY ANOVA:
print ' '
print 'Drawing preview of data...'
groups = data.groupby(data[E])
colors = ['blue', 'red', 'yellow', 'green', 'purple', 'brown', 'orange', 'silver','magenta','cyan','black','white']
if len(groups) == 2:
X = data[X]
Y = data[S]
s = 100
plt.figure(figsize=(8,6))
groups = data.groupby(data[E])
for key, group in groups: # ERROR (working on it)
interaction_plot(X, group, np.log(Y+1), colors=['r','b'], markers=['D','^'], ms=10, ax=plt.gca())
plt.show() #?
else:
fig, ax = plt.subplots(figsize=(8,6))
s = 100
for key, group in groups: # ERROR (working on it)
group.plot(ax=ax, kind='scatter', x=X, y=S, label=key, color=colors[key-1], alpha=0.3, s=s)
nomen = 'variable "'+str(E)+'" is represented by colors'
nomenclature = nomen
plt.title(nomenclature)
plt.xlabel(X);
plt.ylabel(S);
plt.show()
print ' '
print '--------------------------------'
XoY = stats.ttest_ind(data[X], data[S])
print 'T test for X and Y (ind):\n'
print 't-statistic=', XoY[0], '\n\np-value=', XoY[1]
print ' '
print '--------------------------------'
XyY = stats.ttest_rel(data[X], data[S])
print 'T test for X and Y (rel):\n'
print 't-statistic=', XyY[0], '\n\np-value=', XyY[1]
print ' '
print '--------------------------------'
xyy = stats.ttest_1samp(data[X], data[S])
print 'T test for X and Y (1samp)\nReady as "xyy"'
print ' '
print '--------------------------------'
Y = data[S]
Group = data[E]
X = data[X]
# f-test
formula = 'np.log(Y+1) ~ C(Group) * C(X)'
print 'Formula ready:', formula
print ' '
model = ols(formula, data=data).fit()
print 'MODEL SUMMARY:'
print ' '
print model.summary()
print ' '
#aov_table = lm(model, typ=2) # ERROR Singular matrix
#print 'ANALYSIS OF VARIANCE (ANOVA) TABLE:'
#print ' '
#print aov_table
print ' '
print ' Drawing INTERACTION PLOT...'
print ' '
sumofsq = ols('np.log(Y+1) ~ C(Group, Sum) * C(X, Sum)', data=data).fit()
print ' '
print ' Sum of Squares'
print lm(sumofsq)
print ' '
print ' Type 2'
print lm(sumofsq, typ=2)
print ' '
print ' Type 3'
print lm(sumofsq, typ=3)
print ' '
# 3 ways visualization:
else:
print ' '
hand2 = raw_input('Enter independent variable "X" column header: ')
print ' '
hand3 = raw_input('Enter dependent variable "Y" column header: ')
print ' '
M = str(hand1)
E = str(hand0)
X = str(hand2)
S = str(hand3)
# THREE WAY ANOVA:
print ' '
print 'Drawing preview of data...'
print ' '
print 'Calculating ANOVA...'
groups = data.groupby([str(E), str(M)])
colors = ['blue', 'red', 'yellow', 'green', 'purple', 'brown', 'orange', 'silver','magenta','cyan','black','white']
symbols = ['o','d','^', 'h', 's', 'p', 's', 'v', '>','x','D','8','+']
fig, ax = plt.subplots(figsize=(8,6))
if len(groups) < 5:
s = 10**2
else:
s = 81
print ' '
for values, group in groups: # ERROR (working on it)
i, j = values
group.plot(ax=ax, kind='scatter', x=X, y=S, label=values, color=colors[i-1], alpha=0.3, s=s, marker=symbols[j-1], edgecolors='black')
nomen = 'variable "'+str(E)+'" is represented by colors\nvariable "'+str(M)+'" is represented by figures'
nomenclature = nomen
plt.title(nomenclature)
plt.xlabel(X);
plt.ylabel(S);
plt.show()
print ' '
print '--------------------------------'
XoY = stats.ttest_ind(data[X], data[S])
print 'T test for X and Y (ind):\n'
print 't-statistic=', XoY[0], '\n\np-value=', XoY[1]
print ' '
print '--------------------------------'
XyY = stats.ttest_rel(data[X], data[S])
print 'T test for X and Y (rel):\n'
print 't-statistic=', XyY[0], '\n\np-value=', XyY[1]
print ' '
print '--------------------------------'
xyy = stats.ttest_1samp(data[X], data[S])
print 'T test for X and Y (1samp)\nReady as "xyy"'
print ' '
print '--------------------------------'
GroupA = data[E]
Y = data[S]
GroupB = data[M]
X = data[X]
formula = 'Y ~ C(X) + C(GroupA) * C(GroupB)'
print 'Formula ready:', formula
print ' '
model = ols(formula, data=data).fit()
print 'MODEL SUMMARY:'
print ' '
print model.summary()
print ' '
aov_table = lm(model, typ=2)
print 'ANALYSIS OF VARIANCE (ANOVA) TABLE:'
print ' '
print aov_table
print ' '
print ' '
print 'Hasta la vista, human.'
print ' '
exit()