-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_analysis_ipo_24.2.py
2105 lines (1986 loc) · 126 KB
/
data_analysis_ipo_24.2.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import math
import numpy as np
import pandas as pd
import pingouin as pg
import statsmodels.api as sm
import statsmodels.formula.api as smf
from scipy import stats
from statsmodels.stats.power import FTestAnovaPower
from scipy.stats import ttest_ind, spearmanr, friedmanchisquare, rankdata, norm, anderson
from scipy.special import gammaln
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import seaborn as sns
import scikit_posthocs as sp
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.preprocessing import StandardScaler, LabelEncoder
def convertTuple(tup):
stringing = ''
for item in tup:
stringing = stringing + str(item)
return stringing
def most_frequent(x):
return x.mode().iloc[0] if not x.mode().empty else None
def compute_power(pop1, pop2, alpha, num_simulations=10000):
t_statistics = np.zeros(num_simulations)
n1 = len(pop1)
mean1 = np.mean(pop1)
var1 = np.var(pop1)
n2 = len(pop2)
mean2 = np.mean(pop2)
var2 = np.var(pop2)
for i in range(num_simulations):
sample1 = np.random.normal(mean1, np.sqrt(var1), n1)
sample2 = np.random.normal(mean2, np.sqrt(var2), n2)
_, t_statistic = ttest_ind(sample1, sample2, equal_var=False)
t_statistics[i] = t_statistic
critical_value = np.percentile(t_statistics, 100 - alpha / 2)
power = np.mean(t_statistics > critical_value)
return power
def test_nemenyi(data):
nemenyi = NemenyiTestPostHoc(data)
meanRanks, pValues = nemenyi.do()
return meanRanks, pValues
class NemenyiTestPostHoc():
def __init__(self, data):
self._noOfGroups = data.shape[0]
self._noOfSamples = data.shape[1]
self._data = data
def do(self):
dataAsRanks = np.full(self._data.shape, np.nan)
for i in range(self._noOfSamples):
dataAsRanks[:, i] = rankdata(self._data[:, i])
meansOfRanksOfDependentSamples = np.mean(dataAsRanks, 1)
qValues = self._compareStatisticsOfAllPairs(meansOfRanksOfDependentSamples)
pValues = self._calculatePValues(qValues)
return qValues, pValues
def _compareStatisticsOfAllPairs(self, meansOfRanks):
noOfMeansOfRanks = len(meansOfRanks)
compareResults = np.zeros((noOfMeansOfRanks-1, noOfMeansOfRanks))
for i in range(noOfMeansOfRanks-1):
for j in range(i+1, noOfMeansOfRanks):
compareResults[i][j] = self._compareStatisticsOfSinglePair((meansOfRanks[i], meansOfRanks[j]))
return compareResults
def _compareStatisticsOfSinglePair(self, meansOfRanksPair):
diff = abs(meansOfRanksPair[0] - meansOfRanksPair[1])
qval = diff / np.sqrt(self._noOfGroups * (self._noOfGroups + 1) / (6 * self._noOfSamples))
return qval * np.sqrt(2)
def _calculatePValues(self, qValues):
for qRow in qValues:
for i in range(len(qRow)):
qRow[i] = self._ptukey(qRow[i], 1, self._noOfGroups, np.inf)
return 1 - qValues
def _wprob(self, w, rr, cc):
nleg = 12
ihalf = 6
C1 = -30
C2 = -50
C3 = 60
M_1_SQRT_2PI = 1 / np.sqrt(2 * np.pi)
bb = 8
wlar = 3
wincr1 = 2
wincr2 = 3
xleg = [
0.981560634246719250690549090149,
0.904117256370474856678465866119,
0.769902674194304687036893833213,
0.587317954286617447296702418941,
0.367831498998180193752691536644,
0.125233408511468915472441369464
]
aleg = [
0.047175336386511827194615961485,
0.106939325995318430960254718194,
0.160078328543346226334652529543,
0.203167426723065921749064455810,
0.233492536538354808760849898925,
0.249147045813402785000562436043
]
qsqz = w * 0.5
if qsqz >= bb:
return 1.0
# find (f(w/2) - 1) ^ cc
# (first term in integral of hartley's form).
pr_w = 2 * norm.cdf(qsqz) - 1
if pr_w >= np.exp(C2 / cc):
pr_w = pr_w ** cc
else:
pr_w = 0.0
# if w is large then the second component of the
# integral is small, so fewer intervals are needed.
wincr = wincr1 if w > wlar else wincr2
# find the integral of second term of hartley's form
# for the integral of the range for equal-length
# intervals using legendre quadrature. limits of
# integration are from (w/2, 8). two or three
# equal-length intervals are used.
# blb and bub are lower and upper limits of integration.
blb = qsqz
binc = (bb - qsqz) / wincr
bub = blb + binc
einsum = 0.0
# integrate over each interval
cc1 = cc - 1.0
for wi in range(1, wincr + 1):
elsum = 0.0
a = 0.5 * (bub + blb)
# legendre quadrature with order = nleg
b = 0.5 * (bub - blb)
for jj in range(1, nleg + 1):
if (ihalf < jj):
j = (nleg - jj) + 1
xx = xleg[j-1]
else:
j = jj
xx = -xleg[j-1]
c = b * xx
ac = a + c
# if exp(-qexpo/2) < 9e-14
# then doesn't contribute to integral
qexpo = ac * ac
if qexpo > C3:
break
pplus = 2 * norm.cdf(ac)
pminus = 2 * norm.cdf(ac, w)
# if rinsum ^ (cc-1) < 9e-14, */
# then doesn't contribute to integral */
rinsum = (pplus * 0.5) - (pminus * 0.5)
if (rinsum >= np.exp(C1 / cc1)):
rinsum = (aleg[j-1] * np.exp(-(0.5 * qexpo))) * (rinsum ** cc1)
elsum += rinsum
elsum *= (((2.0 * b) * cc) * M_1_SQRT_2PI)
einsum += elsum
blb = bub
bub += binc
# if pr_w ^ rr < 9e-14, then return 0
pr_w += einsum
if pr_w <= np.exp(C1 / rr):
return 0
pr_w = pr_w ** rr
if (pr_w >= 1):
return 1
return pr_w
def _ptukey(self, q, rr, cc, df):
M_LN2 = 0.69314718055994530942
nlegq = 16
ihalfq = 8
eps1 = -30.0
eps2 = 1.0e-14
dhaf = 100.0
dquar = 800.0
deigh = 5000.0
dlarg = 25000.0
ulen1 = 1.0
ulen2 = 0.5
ulen3 = 0.25
ulen4 = 0.125
xlegq = [
0.989400934991649932596154173450,
0.944575023073232576077988415535,
0.865631202387831743880467897712,
0.755404408355003033895101194847,
0.617876244402643748446671764049,
0.458016777657227386342419442984,
0.281603550779258913230460501460,
0.950125098376374401853193354250e-1
]
alegq = [
0.271524594117540948517805724560e-1,
0.622535239386478928628438369944e-1,
0.951585116824927848099251076022e-1,
0.124628971255533872052476282192,
0.149595988816576732081501730547,
0.169156519395002538189312079030,
0.182603415044923588866763667969,
0.189450610455068496285396723208
]
if q <= 0:
return 0
if (df < 2) or (rr < 1) or (cc < 2):
return float('nan')
if np.isfinite(q) is False:
return 1
if df > dlarg:
return self._wprob(q, rr, cc)
# in fact we don't need the code below and majority of variables:
# calculate leading constant
f2 = df * 0.5
f2lf = ((f2 * np.log(df)) - (df * M_LN2)) - gammaln(f2)
f21 = f2 - 1.0
# integral is divided into unit, half-unit, quarter-unit, or
# eighth-unit length intervals depending on the value of the
# degrees of freedom.
ff4 = df * 0.25
if df <= dhaf:
ulen = ulen1
elif df <= dquar:
ulen = ulen2
elif df <= deigh:
ulen = ulen3
else:
ulen = ulen4
f2lf += np.log(ulen)
ans = 0.0
for i in range(1, 51):
otsum = 0.0
# legendre quadrature with order = nlegq
# nodes (stored in xlegq) are symmetric around zero.
twa1 = (2*i - 1) * ulen
for jj in range(1, nlegq + 1):
if (ihalfq < jj):
j = jj - ihalfq - 1
t1 = (f2lf + (f21 * np.log(twa1 + (xlegq[j] * ulen)))) - (((xlegq[j] * ulen) + twa1) * ff4)
else:
j = jj - 1
t1 = (f2lf + (f21 * np.log(twa1 - (xlegq[j] * ulen)))) + (((xlegq[j] * ulen) - twa1) * ff4)
# if exp(t1) < 9e-14, then doesn't contribute to integral
if t1 >= eps1:
if ihalfq < jj:
qsqz = q * np.sqrt(((xlegq[j] * ulen) + twa1) * 0.5)
else:
qsqz = q * np.sqrt(((-(xlegq[j] * ulen)) + twa1) * 0.5)
wprb = self._wprob(qsqz, rr, cc)
rotsum = (wprb * alegq[j]) * np.exp(t1)
otsum += rotsum
# if integral for interval i < 1e-14, then stop.
# However, in order to avoid small area under left tail,
# at least 1 / ulen intervals are calculated.
if (i * ulen >= 1.0) and (otsum <= eps2):
break
ans += otsum
return min(1, ans)
# FLAG FOR PLOTS
want_figures = False
want_qq = False
want_xy_plots = False
# FLAG FOR MIXED MODELS
want_lm = False
# FLAG FOR CORRELATION
want_correlation = False
# FLAG FOR STATS
want_stats = False
mpl.rcParams['figure.dpi'] = 300
#plt.rcParams.update({'font.size': 22})
dataset = pd.read_csv('datasets/complete_bi_NH.csv')
#dataset = pd.read_csv('datasets/complete_bi_NH_meno_il_primo.csv')
clean_dataset = pd.DataFrame()
variables = ['Signed_error', 'Unsigned_error', 'Head_rotation', 'Head_distance']
var_names = ['Signed error', 'Unsigned error', 'Head rotation', 'Head distance']
individual_vars = ['Age', 'MacroCause', 'Experience DX', 'Experience SX',
'Threshold w/o 500 DX', 'Threshold w/o 1000 DX',
'Threshold w/o 2000 DX', 'Threshold w/o 4000 DX',
'Threshold w/o 500 SX', 'Threshold w/o 1000 SX',
'Threshold w/o 2000 SX', 'Threshold w/o 4000 SX', 'Threshold w/ 500 DX',
'Threshold w/ 1000 DX', 'Threshold w/ 2000 DX', 'Threshold w/ 4000 DX',
'Threshold w/ 500 SX', 'Threshold w/ 1000 SX', 'Threshold w/ 2000 SX',
'Threshold w/ 4000 SX']
# REMOVE OUTLIERS WRT UNSIGNED ERROR
conditions = ['ICSX_NOICDX', 'ICSX_ICDX', 'NOICSX_ICDX', 'PASX_NOPADX', 'PASX_PADX',
'NOPASX_NOPADX', 'NOPASX_PADX']
results_list = []
for condition in conditions:
one_condition_dataset = dataset[dataset['Condition'] == condition]
condition_participants = one_condition_dataset['Participant'].unique()
count_participants = 0
for participant in condition_participants:
one_condition_one_participant = one_condition_dataset[one_condition_dataset['Participant'] == participant]
if len(one_condition_one_participant) != 65:
count_participants += 1
result = {'Condition': condition,
'Participant': participant,
'Number of answers': len(one_condition_one_participant)}
results_list.append(result)
mean_unsigned_error = one_condition_dataset['Unsigned_error'].mean()
std_unsigned_error = one_condition_dataset['Unsigned_error'].std()
threshold = mean_unsigned_error + 3 * std_unsigned_error
no_outliers_dataset = one_condition_dataset[one_condition_dataset['Unsigned_error'] <= threshold]
result = {'Condition': condition,
'Original Number of participants': len(one_condition_dataset['Participant'].unique()),
'Original Number of answers': len(one_condition_dataset),
'Number of participants not complete': count_participants,
'Outliers threshold': threshold,
'Filtered Number of participants': len(no_outliers_dataset['Participant'].unique()),
'Filtered Number of answers': len(no_outliers_dataset),
'Percentage outliers': (len(one_condition_dataset) - len(
no_outliers_dataset)) / len(one_condition_dataset) * 100}
results_list.append(result)
clean_dataset = pd.concat([clean_dataset, no_outliers_dataset], ignore_index=True)
results_df = pd.DataFrame(results_list)
results_df.to_csv('filtering_results.csv', index=False)
print("Original Dataset Shape:", len(dataset))
print("Filtered Dataset Shape:", len(clean_dataset))
for individual_var in individual_vars:
#print("Individual variable: ", individual_var)
levels_individual_var = clean_dataset[individual_var].unique()
#print('Levels: ', len(levels_individual_var))
# Filter dataset for each individual variable level
for var_level in levels_individual_var:
#print('Level: ', var_level)
one_level_dataset = clean_dataset[clean_dataset[individual_var] == var_level]
if individual_var == 'Age':
ages = []
for participant in clean_dataset['Participant'].unique():
ages.append(clean_dataset[clean_dataset['Participant'] == participant][individual_var].unique())
print(np.mean(ages))
print(np.std(ages))
# LOG TRANSFORM ALL DATA
min_values = []
min_values_IC = []
min_values_PA = []
for var in variables:
print('VARIABLE', var)
IC_data = clean_dataset[(clean_dataset['Condition'] == 'NOICSX_ICDX') | (
clean_dataset['Condition'] == 'ICSX_ICDX') | (
clean_dataset['Condition'] == 'ICSX_NOICDX')]
PA_data = clean_dataset[(clean_dataset['Condition'] == 'NOPASX_PADX') | (
clean_dataset['Condition'] == 'PASX_PADX') | (
clean_dataset['Condition'] == 'NOPASX_NOPADX') | (
clean_dataset['Condition'] == 'PASX_NOPADX')]
min_values.append(min(clean_dataset[var]))
print('min_value', min_values[-1])
min_values_IC.append(min(IC_data[var]))
min_values_PA.append(min(PA_data[var]))
shifted_data = [x - min_values[-1] + 1 for x in clean_dataset[var]]
log_data = [np.log(x) for x in shifted_data]
clean_dataset[var] = log_data
# INTRODUCE POPULATION COLUMN: HA or CI
clean_dataset['Group'] = clean_dataset['Group'].str.contains('CI', case=False, regex=True)
clean_dataset['Group'] = clean_dataset['Group'].map({True: 'CI', False: 'HA'})
# RENAME COLUMNS
clean_dataset.rename(columns={'Threshold w/o 500 DX': 'Threshold_wo_500_DX',
'Threshold w/o 1000 DX': 'Threshold_wo_1000_DX',
'Threshold w/o 2000 DX': 'Threshold_wo_2000_DX',
'Threshold w/o 4000 DX': 'Threshold_wo_4000_DX',
'Threshold w/o 500 SX': 'Threshold_wo_500_SX',
'Threshold w/o 1000 SX': 'Threshold_wo_1000_SX',
'Threshold w/o 2000 SX': 'Threshold_wo_2000_SX',
'Threshold w/o 4000 SX': 'Threshold_wo_4000_SX',
'Threshold w/ 500 DX': 'Threshold_w_500_DX',
'Threshold w/ 1000 DX': 'Threshold_w_1000_DX',
'Threshold w/ 2000 DX': 'Threshold_w_2000_DX',
'Threshold w/ 4000 DX': 'Threshold_w_4000_DX',
'Threshold w/ 500 SX': 'Threshold_w_500_SX',
'Threshold w/ 1000 SX': 'Threshold_w_1000_SX',
'Threshold w/ 2000 SX': 'Threshold_w_2000_SX',
'Threshold w/ 4000 SX': 'Threshold_w_4000_SX',
'Experience DX': 'Experience_DX', 'Experience SX': 'Experience_SX'}, inplace=True)
# AGGREGATE DATA OF TARGETS WITH MEDIANS
clean_dataset = clean_dataset.groupby(['Target', 'Participant', 'Condition'], as_index=False).agg(
{'Signed_error': 'median', 'Unsigned_error': 'median', 'Head_rotation': 'median', 'Head_distance': 'median',
'Age': 'median', 'Experience_DX': 'median', 'Experience_SX': 'median',
'Threshold_wo_500_DX': 'median', 'Threshold_wo_1000_DX': 'median', 'Threshold_wo_2000_DX': 'median',
'Threshold_wo_4000_DX': 'median', 'Threshold_wo_500_SX': 'median', 'Threshold_wo_1000_SX': 'median',
'Threshold_wo_2000_SX': 'median', 'Threshold_wo_4000_SX': 'median', 'Threshold_w_500_DX': 'median',
'Threshold_w_1000_DX': 'median', 'Threshold_w_2000_DX': 'median', 'Threshold_w_4000_DX': 'median',
'Threshold_w_500_SX': 'median', 'Threshold_w_1000_SX': 'median', 'Threshold_w_2000_SX': 'median',
'Threshold_w_4000_SX': 'median', 'MacroCause': most_frequent})
participants = clean_dataset['Participant'].unique()
data_medians = {'Condition': [], 'Variable': [], 'Participant': [], 'Median': [], 'Target': [],
'Age': [], 'MacroCause': [], 'Experience_DX': [], 'Experience_SX': [],
'Threshold_wo_500_DX': [], 'Threshold_wo_1000_DX': [],
'Threshold_wo_2000_DX': [], 'Threshold_wo_4000_DX': [],
'Threshold_wo_500_SX': [], 'Threshold_wo_1000_SX': [],
'Threshold_wo_2000_SX': [], 'Threshold_wo_4000_SX': [], 'Threshold_w_500_DX': [],
'Threshold_w_1000_DX': [], 'Threshold_w_2000_DX': [], 'Threshold_w_4000_DX': [],
'Threshold_w_500_SX': [], 'Threshold_w_1000_SX': [], 'Threshold_w_2000_SX': [],
'Threshold_w_4000_SX': []}
dataset_medians = pd.DataFrame(data_medians)
for participant in participants:
one_participant_dataset = clean_dataset[clean_dataset['Participant'] == participant]
for n, var in enumerate(variables):
for m, condition in enumerate(one_participant_dataset['Condition'].unique()):
one_condition_one_participant = one_participant_dataset[
one_participant_dataset['Condition'] == condition]
right_half_alpha = np.asarray([np.mean(one_condition_one_participant['Threshold_wo_500_DX'].unique()),
np.mean(one_condition_one_participant['Threshold_wo_1000_DX'].unique()),
np.mean(one_condition_one_participant['Threshold_wo_2000_DX'].unique()),
np.mean(one_condition_one_participant['Threshold_wo_4000_DX'].unique())])
left_half_alpha = np.asarray([np.mean(one_condition_one_participant['Threshold_wo_500_SX'].unique()),
np.mean(one_condition_one_participant['Threshold_wo_1000_SX'].unique()),
np.mean(one_condition_one_participant['Threshold_wo_2000_SX'].unique()),
np.mean(one_condition_one_participant['Threshold_wo_4000_SX'].unique())])
cosine_sim = cosine_similarity(right_half_alpha.reshape(1, -1), left_half_alpha.reshape(1, -1))
cosine_dist_without = np.round(1 - cosine_sim[0][0], 4)
right_half_with = np.asarray([np.mean(one_condition_one_participant['Threshold_w_500_DX'].unique()),
np.mean(one_condition_one_participant['Threshold_w_1000_DX'].unique()),
np.mean(one_condition_one_participant['Threshold_w_2000_DX'].unique()),
np.mean(one_condition_one_participant['Threshold_w_4000_DX'].unique())])
left_half_with = np.asarray([np.mean(one_condition_one_participant['Threshold_w_500_SX'].unique()),
np.mean(one_condition_one_participant['Threshold_w_1000_SX'].unique()),
np.mean(one_condition_one_participant['Threshold_w_2000_SX'].unique()),
np.mean(one_condition_one_participant['Threshold_w_4000_SX'].unique())])
cosine_sim = cosine_similarity(right_half_with.reshape(1, -1), left_half_with.reshape(1, -1))
cosine_dist_with = np.round(1 - cosine_sim[0][0], 4)
for target in one_condition_one_participant['Target'].unique():
one_participant_one_target = one_condition_one_participant[
one_condition_one_participant['Target'] == target]
median_var = np.median(one_participant_one_target[var])
new_row = {'Condition': [condition], 'Variable': [var], 'Participant': [participant],
'Median': [median_var], 'Target': [target],
'Age': [np.mean(one_participant_one_target['Age'].unique())],
'MacroCause': [one_participant_one_target['MacroCause'].unique()[0]],
'Experience_DX': [np.mean(one_participant_one_target['Experience_DX'].unique())],
'Experience_SX': [np.mean(one_participant_one_target['Experience_SX'].unique())],
'Threshold_wo_500_DX': [np.mean(one_participant_one_target['Threshold_wo_500_DX'].unique())],
'Threshold_wo_1000_DX': [np.mean(one_participant_one_target['Threshold_wo_1000_DX'].unique())],
'Threshold_wo_2000_DX': [np.mean(one_participant_one_target['Threshold_wo_2000_DX'].unique())],
'Threshold_wo_4000_DX': [np.mean(one_participant_one_target['Threshold_wo_4000_DX'].unique())],
'Threshold_wo_500_SX': [np.mean(one_participant_one_target['Threshold_wo_500_SX'].unique())],
'Threshold_wo_1000_SX': [np.mean(one_participant_one_target['Threshold_wo_1000_SX'].unique())],
'Threshold_wo_2000_SX': [np.mean(one_participant_one_target['Threshold_wo_2000_SX'].unique())],
'Threshold_wo_4000_SX': [np.mean(one_participant_one_target['Threshold_wo_4000_SX'].unique())],
'Threshold_w_500_DX': [np.mean(one_participant_one_target['Threshold_w_500_DX'].unique())],
'Threshold_w_1000_DX': [np.mean(one_participant_one_target['Threshold_w_1000_DX'].unique())],
'Threshold_w_2000_DX': [np.mean(one_participant_one_target['Threshold_w_2000_DX'].unique())],
'Threshold_w_4000_DX': [np.mean(one_participant_one_target['Threshold_w_4000_DX'].unique())],
'Threshold_w_500_SX': [np.mean(one_participant_one_target['Threshold_w_500_SX'].unique())],
'Threshold_w_1000_SX': [np.mean(one_participant_one_target['Threshold_w_1000_SX'].unique())],
'Threshold_w_2000_SX': [np.mean(one_participant_one_target['Threshold_w_2000_SX'].unique())],
'Threshold_w_4000_SX': [np.mean(one_participant_one_target['Threshold_w_4000_SX'].unique())]}
new_row = pd.DataFrame(new_row)
dataset_medians = pd.concat([dataset_medians, new_row], ignore_index=True)
# CORRELATION ANALYSIS PER CONDITION PER PARTICIPANT
results_list = []
for condition in conditions:
heatmap_data_condition = clean_dataset[clean_dataset['Condition'] == condition]
if want_xy_plots:
fig, ax = plt.subplots()
ax.scatter(heatmap_data_condition['Head_distance'], heatmap_data_condition['Signed_error'])
fig.savefig(condition + '_HD_SE.png')
plt.close()
fig, ax = plt.subplots()
ax.scatter(heatmap_data_condition['Head_distance'], heatmap_data_condition['Unsigned_error'])
fig.savefig(condition + '_HD_UE.png')
plt.close()
fig, ax = plt.subplots()
ax.scatter(heatmap_data_condition['Head_rotation'], heatmap_data_condition['Signed_error'])
fig.savefig(condition + '_HR_SE.png')
plt.close()
fig, ax = plt.subplots()
ax.scatter(heatmap_data_condition['Head_rotation'], heatmap_data_condition['Unsigned_error'])
fig.savefig(condition + '_HR_UE.png')
plt.close()
fig, ax = plt.subplots()
ax.scatter(heatmap_data_condition['Head_rotation'], heatmap_data_condition['Head_distance'])
fig.savefig(condition + '_HR_HD.png')
plt.close()
fig, ax = plt.subplots()
ax.scatter(heatmap_data_condition['Age'], heatmap_data_condition['Unsigned_error'])
fig.savefig(condition + '_Age_UE.png')
plt.close()
fig, ax = plt.subplots()
ax.scatter(heatmap_data_condition['Age'], heatmap_data_condition['Head_distance'])
fig.savefig(condition + '_Age_HD.png')
plt.close()
fig, ax = plt.subplots()
ax.scatter(heatmap_data_condition['Age'], heatmap_data_condition['Head_rotation'])
fig.savefig(condition + '_Age_HR.png')
plt.close()
fig, ax = plt.subplots()
ax.scatter(heatmap_data_condition['Age'], heatmap_data_condition['Signed_error'])
fig.savefig(condition + '_Age_SE.png')
plt.close()
fig, ax = plt.subplots()
ax.scatter(heatmap_data_condition['Unsigned_error'], heatmap_data_condition['Signed_error'])
fig.savefig(condition + '_UE_SE.png')
plt.close()
correlation_coefficient, p_value = stats.pearsonr(heatmap_data_condition['Signed_error'],
heatmap_data_condition['Head_rotation'])
if p_value < 0.05:
result = {'Condition': condition,
'Test': 'Pearson Signed error - Head rotation',
'Correlation Coefficient': correlation_coefficient,
'P-value': p_value}
results_list.append(result)
correlation_coefficient, p_value = stats.pearsonr(heatmap_data_condition['Unsigned_error'],
heatmap_data_condition['Head_distance'])
if p_value < 0.05:
result = {'Condition': condition,
'Test': 'Pearson Unsigned error - Head distance',
'Correlation Coefficient': correlation_coefficient,
'P-value': p_value}
results_list.append(result)
correlation_coefficient, p_value = stats.pearsonr(heatmap_data_condition['Unsigned_error'],
heatmap_data_condition['Experience_DX'])
if p_value < 0.05:
result = {'Condition': condition,
'Test': 'Pearson Unsigned error - Experience_DX',
'Correlation Coefficient': correlation_coefficient,
'P-value': p_value}
results_list.append(result)
correlation_coefficient, p_value = stats.pearsonr(heatmap_data_condition['Unsigned_error'],
heatmap_data_condition['Experience_SX'])
if p_value < 0.05:
result = {'Condition': condition,
'Test': 'Pearson Unsigned error - Experience_SX',
'Correlation Coefficient': correlation_coefficient,
'P-value': p_value}
results_list.append(result)
correlation_coefficient, p_value = stats.pearsonr(heatmap_data_condition['Unsigned_error'],
heatmap_data_condition['Age'])
if p_value < 0.05:
result = {'Condition': condition,
'Test': 'Pearson Unsigned error - Age',
'Correlation Coefficient': correlation_coefficient,
'P-value': p_value}
results_list.append(result)
for participant in heatmap_data_condition['Participant'].unique():
heatmap_target = heatmap_data_condition[heatmap_data_condition['Participant'] == participant]
correlation_coefficient, p_value = stats.pearsonr(heatmap_target['Signed_error'],
heatmap_target['Head_rotation'])
if p_value < 0.05:
result = {'Condition': condition,
'Participant': participant,
'Test': 'Pearson Signed error - Head rotation',
'Correlation Coefficient': correlation_coefficient,
'P-value': p_value}
results_list.append(result)
correlation_coefficient, p_value = stats.pearsonr(heatmap_target['Unsigned_error'],
heatmap_target['Head_distance'])
if p_value < 0.05:
result = {'Condition': condition,
'Participant': participant,
'Test': 'Pearson Unsigned error - Head distance',
'Correlation Coefficient': correlation_coefficient,
'P-value': p_value}
results_list.append(result)
if condition in ['NOPASX_PADX', 'PASX_NOPADX']:
for participant in heatmap_data_condition['Participant'].unique():
thresh_4k_L_wo = np.mean(heatmap_data_condition[
heatmap_data_condition['Participant'] == participant]['Threshold_wo_4000_SX'])
thresh_4k_L_w = np.mean(heatmap_data_condition[
heatmap_data_condition['Participant'] == participant]['Threshold_w_4000_SX'])
thresh_4k_R_wo = np.mean(heatmap_data_condition[
heatmap_data_condition['Participant'] == participant]['Threshold_wo_4000_DX'])
thresh_4k_R_w = np.mean(heatmap_data_condition[
heatmap_data_condition['Participant'] == participant]['Threshold_w_4000_DX'])
error = np.exp(heatmap_data_condition[
heatmap_data_condition['Participant'] == participant]['Unsigned_error'] + min_values[n] - 1)
#print(condition)
#print('Subject')
#print(participant)
#print('Thresholds')
#print(thresh_4k_L_wo)
#print(thresh_4k_L_w)
#print(thresh_4k_R_wo)
#print(thresh_4k_R_w)
#print('Median error')
#print(np.median(error))
results_df = pd.DataFrame(results_list)
results_df.to_csv('correlation_results.csv', index=False)
# LINEAR MIXED MODELS ANALYSIS
if want_lm:
label_encoder = LabelEncoder()
#clean_dataset['Condition'] = label_encoder.fit_transform(clean_dataset['Condition'])
#clean_dataset['MacroCause'] = label_encoder.fit_transform(clean_dataset['MacroCause'])
#clean_dataset['Group'] = label_encoder.fit_transform(clean_dataset['Group'])
individual_vars = ['Age', 'MacroCause', 'Experience_DX', 'Experience_SX',
'Threshold_wo_500_DX', 'Threshold_wo_1000_DX', 'Threshold_wo_2000_DX', 'Threshold_wo_4000_DX',
'Threshold_wo_500_SX', 'Threshold_wo_1000_SX', 'Threshold_wo_2000_SX', 'Threshold_wo_4000_SX',
'Threshold_w_500_DX', 'Threshold_w_1000_DX', 'Threshold_w_2000_DX', 'Threshold_w_4000_DX',
'Threshold_w_500_SX', 'Threshold_w_1000_SX', 'Threshold_w_2000_SX', 'Threshold_w_4000_SX']
combined_results_list = []
mixed_dataset = clean_dataset
mixed_dataset['condition1'] = mixed_dataset.apply(lambda row: row['Condition'] if row['Group'] == 'CI' else 0, axis=1)
mixed_dataset['condition2'] = mixed_dataset.apply(lambda row: row['Condition'] if row['Group'] == 'HA' else 0, axis=1)
mixed_dataset['MacroCause'] = label_encoder.fit_transform(mixed_dataset['MacroCause'])
mixed_dataset['condition1'] = mixed_dataset['condition1'].astype('category')
mixed_dataset['condition2'] = mixed_dataset['condition2'].astype('category')
mixed_dataset['Group'] = mixed_dataset['Group'].astype('category')
for var in variables:
formula = f"{var} ~ Group * Condition"
title = formula
try:
model = smf.mixedlm(formula, data=mixed_dataset, groups=mixed_dataset['Participant'])
result = model.fit()
summary_df = result.summary().tables[1].to_html()
summary_df = pd.read_html(summary_df, header=0, index_col=0)[0]
summary_df['Title'] = title
significant_rows = summary_df[abs(summary_df['P>|z|']) < 0.05]
combined_results_list.append(significant_rows)
except np.linalg.LinAlgError as e:
print(f"Error for {title}: {e}")
for individual_var in individual_vars:
formula = f"{var} ~ Group * condition1 * condition2 + {individual_var}"
title = formula
#formula = f"{var} ~ Group + Condition + Target + {individual_var}"
#formula = f"{var} ~ Condition + Target + Age + MacroCause + Experience_DX + Experience_SX + Threshold_wo_500_DX + Threshold_wo_1000_DX + Threshold_wo_2000_DX + Threshold_wo_4000_DX + Threshold_wo_500_SX + Threshold_wo_1000_SX + Threshold_wo_2000_SX + Threshold_wo_4000_SX + Threshold_w_500_DX + Threshold_w_1000_DX + Threshold_w_2000_DX + Threshold_w_4000_DX + Threshold_w_500_SX + Threshold_w_1000_SX + Threshold_w_2000_SX + Threshold_w_4000_SX"
try:
model = sm.MixedLM.from_formula(formula,
data=mixed_dataset,
groups=mixed_dataset['Participant'])
result = model.fit(method='nm', maxiter=1000)
summary_df = result.summary().tables[1].to_html()
summary_df = pd.read_html(summary_df, header=0, index_col=0)[0]
summary_df['Title'] = title
significant_rows = summary_df[abs(summary_df['P>|z|']) < 0.05]
combined_results_list.append(significant_rows)
except np.linalg.LinAlgError as e:
print(f"Error for {title}: {e}")
mixed_results = pd.concat(combined_results_list)
mixed_results.to_csv(f'mixed_results.csv')
# GENERAL LISTS FOR PLOTS, BOXPLOTS, AND STATISTICS
targets = [-90., -75., -60., -45., -30., -15., 0., 15., 30., 45., 60., 75., 90.]
conditions_ON = ['PASX_PADX', 'ICSX_ICDX']
conditions_L = ['PASX_NOPADX', 'ICSX_NOICDX']
conditions_R = ['NOPASX_PADX', 'NOICSX_ICDX']
conditions_CI = ['ICSX_NOICDX', 'ICSX_ICDX', 'NOICSX_ICDX']
conditions_HA = ['PASX_NOPADX', 'PASX_PADX', 'NOPASX_NOPADX', 'NOPASX_PADX']
colors_ON = ['y', 'g']
colors_all = ['b', 'y', 'r', 'c', 'g', 'grey', 'm']
colors_plot = colors_all * len(targets)
y_labels = ['Signed error [°]', 'Unsigned error [°]', 'Head rotation [°]', 'Head distance [m]']
titles = ['CI L', 'CI ON', 'CI R', 'HA L', 'HA ON', 'NO HA', 'HA R']
patterns = ['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*']
# CORRELATION ANALYSIS
if want_correlation:
for nn, condition in enumerate(conditions):
heatmap_data_condition = clean_dataset[(clean_dataset['Condition'] == condition)].copy()
heatmap_data_condition.rename(columns={'Signed_error': 'Signed error', 'Head_rotation': 'Head rotation',
'Unsigned_error': 'Unsigned error', 'Head_distance': 'Head distance',
'Experience_DX': 'Experience R', 'Experience_SX': 'Experience L',
'Threshold_w_500_DX': 'T500 w/ R', 'Threshold_w_1000_DX': 'T1000 w/ R',
'Threshold_w_2000_DX': 'T2000 w/ R', 'Threshold_w_4000_DX': 'T4000 w/ R',
'Threshold_w_500_SX': 'T500 w/ L', 'Threshold_w_1000_SX': 'T1000 w/ L',
'Threshold_w_2000_SX': 'T2000 w/ L', 'Threshold_w_4000_SX': 'T4000 w/ L',
'Threshold_wo_500_DX': 'T500 w/o R', 'Threshold_wo_1000_DX': 'T1000 w/o R',
'Threshold_wo_2000_DX': 'T2000 w/o R', 'Threshold_wo_4000_DX': 'T4000 w/o R',
'Threshold_wo_500_SX': 'T500 w/o L', 'Threshold_wo_1000_SX': 'T1000 w/o L',
'Threshold_wo_2000_SX': 'T2000 w/o L', 'Threshold_wo_4000_SX': 'T4000 w/o L'},
inplace=True)
for thr in ['T500 w/ R', 'T1000 w/ R', 'T2000 w/ R', 'T4000 w/ R',
'T500 w/ L', 'T1000 w/ L', 'T2000 w/ L', 'T4000 w/ L',
'T500 w/o R', 'T1000 w/o R', 'T2000 w/o R', 'T4000 w/o R',
'T500 w/o L', 'T1000 w/o L', 'T2000 w/o L', 'T4000 w/o L',
'Experience R', 'Experience L']:
min_value = heatmap_data_condition[thr].min()
max_value = heatmap_data_condition[thr].max()
heatmap_data_condition[thr] = (heatmap_data_condition[thr] - min_value) / (max_value - min_value)
# Calculate median for each combination of 'Target' and 'Participant'
right_half_alpha = np.asarray([np.mean(one_condition_one_participant['Threshold_wo_500_DX'].unique()),
np.mean(one_condition_one_participant['Threshold_wo_1000_DX'].unique()),
np.mean(one_condition_one_participant['Threshold_wo_2000_DX'].unique()),
np.mean(one_condition_one_participant['Threshold_wo_4000_DX'].unique())])
left_half_alpha = np.asarray([np.mean(one_condition_one_participant['Threshold_wo_500_SX'].unique()),
np.mean(one_condition_one_participant['Threshold_wo_1000_SX'].unique()),
np.mean(one_condition_one_participant['Threshold_wo_2000_SX'].unique()),
np.mean(one_condition_one_participant['Threshold_wo_4000_SX'].unique())])
cosine_sim = cosine_similarity(right_half_alpha.reshape(1, -1), left_half_alpha.reshape(1, -1))
cosine_dist_without = np.round(1 - cosine_sim[0][0], 4)
grouped_df = heatmap_data_condition.groupby(['Target', 'Participant'], as_index=False).agg(
{'Signed error': 'median', 'Unsigned error': 'median', 'Head rotation': 'median', 'Head distance': 'median',
'Condition': most_frequent, 'Age': 'median', 'Experience R': 'median', 'Experience L': 'median',
'T500 w/o R': 'median', 'T1000 w/o R': 'median', 'T2000 w/o R': 'median', 'T4000 w/o R': 'median',
'T500 w/o L': 'median', 'T1000 w/o L': 'median', 'T2000 w/o L': 'median', 'T4000 w/o L': 'median',
'T500 w/ R': 'median', 'T1000 w/ R': 'median', 'T2000 w/ R': 'median', 'T4000 w/ R': 'median',
'T500 w/ L': 'median', 'T1000 w/ L': 'median', 'T2000 w/ L': 'median', 'T4000 w/ L': 'median'})
#if condition in conditions_CI:
# grouped_df.drop(columns=['Participant', 'Target', 'Condition', 'T500 w/o L', 'T1000 w/o L', 'T2000 w/o L', 'T4000 w/o L', 'T500 w/o R', 'T1000 w/o R', 'T2000 w/o R', 'T4000 w/o R'], inplace=True)
# grouped_df = grouped_df[['Unsigned error', 'Head distance', 'Signed error', 'Head rotation', 'Age', 'Experience L', 'Experience R', 'T500 w/ L', 'T1000 w/ L', 'T2000 w/ L', 'T4000 w/ L', 'T500 w/ R', 'T1000 w/ R', 'T2000 w/ R', 'T4000 w/ R']]
#else:
# grouped_df.drop(columns=['Participant', 'Target', 'Condition'], inplace=True)
# grouped_df = grouped_df[['Unsigned error', 'Head distance', 'Signed error', 'Head rotation', 'Age', 'Experience L', 'Experience R', 'T500 w/o L', 'T1000 w/o L', 'T2000 w/o L', 'T4000 w/o L', 'T500 w/ L', 'T1000 w/ L', 'T2000 w/ L', 'T4000 w/ L', 'T500 w/o R', 'T1000 w/o R', 'T2000 w/o R', 'T4000 w/o R', 'T500 w/ R', 'T1000 w/ R', 'T2000 w/ R', 'T4000 w/ R']]
grouped_df.drop(columns=['Participant', 'Target', 'Condition',
'T500 w/ R', 'T1000 w/ R', 'T2000 w/ R', 'T4000 w/ R',
'T500 w/ L', 'T1000 w/ L', 'T2000 w/ L', 'T4000 w/ L',
'T500 w/o R', 'T1000 w/o R', 'T2000 w/o R', 'T4000 w/o R',
'T500 w/o L', 'T1000 w/o L', 'T2000 w/o L', 'T4000 w/o L',
'Experience R', 'Experience L'], inplace=True)
grouped_df = grouped_df[['Unsigned error', 'Head distance', 'Signed error', 'Head rotation', 'Age']]
plt.figure(figsize = (28, 28))
#if condition in conditions_CI:
# plt.figure(figsize = (28, 28))
#else:
# plt.figure(figsize = (42, 28))
#sns.set(font_scale=5)
#plt.tight_layout()
correlation_matrix, p_value_matrix = spearmanr(grouped_df)
correlation_df = pd.DataFrame(correlation_matrix[:4, :], index=grouped_df.columns[:4],
columns=grouped_df.columns)
p_value_df = pd.DataFrame(p_value_matrix[:4, :], index=grouped_df.columns[:4], columns=grouped_df.columns)
bright_colormap = LinearSegmentedColormap.from_list("bright_rd_bu_r", ["#ffcccc", "#ff6666", "#cc0000", "#0000cc", "#6666ff", "#ccccff"])
g = sns.heatmap(correlation_df, cmap="RdBu_r", linewidths=.5, fmt=".2f", annot=False, annot_kws={"size": 20},
cbar=False)
g.set_title(titles[nn], fontsize=100)
for i in range(len(correlation_df)):
for j in range(len(correlation_df.columns)):
corr_value = correlation_df.iloc[i, j]
p_value = p_value_df.iloc[i, j]
if corr_value > -0.1:
color = 'black'
else:
color = 'white'
if p_value < 0.05:
annotation_text = f"{corr_value:.2f}* \n p = {p_value:.2f}"
else:
annotation_text = f"{corr_value:.2f} \n p = {p_value:.2f}"
if i == j:
annotation_text = ""
g.text(j + 0.5, i + 0.5, annotation_text, ha='center', va='center', color=color, fontsize=80)
sns.set_theme(style='white')
#g = sns.heatmap(grouped_df.corr(method='spearman'), cmap = "RdBu_r", linewidths = .5, fmt=".2f", annot = True, annot_kws={"size": 20})
g.tick_params(axis='both', labelsize=80)
g.set_xticklabels(['UE', 'HD', 'SE', 'HR', 'Age'],
rotation=0, ha='right')
g.set_yticklabels(['UE', 'HD', 'SE', 'HR'], rotation=0, ha='right')
plt.savefig('heatmap_' + condition + '.png')
plt.close()
mpl.rcParams['axes.prop_cycle'] = mpl.rcParamsDefault['axes.prop_cycle']
mpl.rcParams['figure.dpi'] = 300
if want_stats:
# CSVS FOR STATS
text_median_name = 'STATS_MEDIANS_boxplot.txt'
text_median_path = text_median_name
text_median_anova = open(text_median_path, 'w')
anova_median_txt = []
text_file_angles_name = 'STATS_ANGLES_boxplot.txt'
text_file_angles_path = text_file_angles_name
text_file_angles = open(text_file_angles_path, 'w')
anova_data_angles_txt = []
text_file_diff_angles_name = 'STATS_DIFFERENT_ANGLE_boxplot.txt'
text_file_diff_angles_path = text_file_diff_angles_name
text_file_diff_angles = open(text_file_diff_angles_path, 'w')
anova_data_diff_angles_txt = []
# BOXPLOTS AND STATISTICS FOR EACH CONDITION WRT TARGETS
for n, var in enumerate(variables):
data_for_all_targets = []
data_for_all_targets_CI_HA = []
for ncond, condition in enumerate(conditions):
#for condition in conditions:
filtered_data_condition = dataset_medians[(dataset_medians['Condition'] == condition) &
(dataset_medians['Variable'] == var)]
if want_qq:
residual = filtered_data_condition['Median'] - np.mean(filtered_data_condition['Median'])
qqplot = sm.qqplot(residual, line='q')
name_qq = 'Q-Q ' + var_names[n] + ' ' + titles[ncond]
plt.title(name_qq)
plt.savefig(name_qq + '.png')
plt.close()
if want_stats:
group_data = [
filtered_data_condition[
filtered_data_condition['Target'] == group][
'Median'].values for group in filtered_data_condition['Target'].unique()]
k = len(group_data)
nn = np.sum([len(group) for group in group_data])
df_between = k - 1
df_within = nn - k
bonfi = 0.05 / math.comb(k, 2)
anova_data_diff_angles_txt.append(
'STATS FOR VARIABLE ' + var + ' in condition '+ condition + ' between Targets')
anova_data_diff_angles_txt.append('\n')
anova_data_diff_angles_txt.append('BONFERRONI P-CORRECTED VALUE ' + str(bonfi))
anova_data_diff_angles_txt.append('\n')
anova_data_diff_angles_txt.append('CRONBACH ALPHA FOR VARIABLE ' + var)
anova_data_diff_angles_txt.append('\n')
cronbach = pg.cronbach_alpha(data=filtered_data_condition[[
'Median', 'Participant', 'Age', 'Experience_DX', 'Experience_SX', 'Threshold_wo_500_DX',
'Threshold_wo_1000_DX', 'Threshold_wo_2000_DX', 'Threshold_wo_4000_DX', 'Threshold_wo_500_SX',
'Threshold_wo_1000_SX', 'Threshold_wo_2000_SX', 'Threshold_wo_4000_SX', 'Threshold_w_500_DX',
'Threshold_w_1000_DX', 'Threshold_w_2000_DX', 'Threshold_w_4000_DX', 'Threshold_w_500_SX',
'Threshold_w_1000_SX', 'Threshold_w_2000_SX', 'Threshold_w_4000_SX']], scores='Median',
subject='Participant', ci=0.95)
anova_data_diff_angles_txt.append(convertTuple(cronbach))
anova_data_diff_angles_txt.append('\n')
anova_data_diff_angles_txt.append('NORMALITY CHECK FOR VARIABLE ' + var + ' BETWEEN Targets')
anova_data_diff_angles_txt.append('\n')
normi_data = filtered_data_condition.copy()
for targi in filtered_data_condition['Target'].unique():
residual = filtered_data_condition[
filtered_data_condition['Target'] == targi]['Median'] - np.mean(
filtered_data_condition[filtered_data_condition['Target'] == targi]['Median'])
#normi_data[normi_data['Target'] == targi]['Median'] = residual
normi_data.loc[normi_data['Target'] == targi, 'Median'] = residual
normi = pg.normality(data=normi_data, dv='Median', group='Target')
anova_data_diff_angles_txt.append(normi.to_string())
anova_data_diff_angles_txt.append('\n')
true_count = list(normi['normal']).count(True)
false_count = list(normi['normal']).count(False)
if false_count > true_count:
#if False in list(normi['normal']):
anova_data_diff_angles_txt.append('FRIEDMAN TEST FOR VARIABLE ' + var + ' BETWEEN Targets')
anova_data_diff_angles_txt.append('\n')
#fried = pg.friedman(data=filtered_data_condition, dv='Median', subject='Participant', within='Target')
intersection_parts = filtered_data_condition[
filtered_data_condition['Target'] == targets[0]]['Participant']
for tar in targets:
parties = filtered_data_condition[filtered_data_condition['Target'] == tar]['Participant']
intersection_parts = list(set(intersection_parts).intersection(parties))
data = []
sample_size = 0
for tar in targets:
data_target = filtered_data_condition[filtered_data_condition['Target'] == tar]
data_to_append = np.asarray(
data_target[data_target['Participant'].isin(intersection_parts)]['Median'])
sample_size += len(data_to_append)
data.append(data_to_append)
fried = friedmanchisquare(*data)
result_str = "F = {}, pval = {})".format(fried.statistic, fried.pvalue)
anova_data_diff_angles_txt.append('NUMEROSITY: ' + str(len(intersection_parts)))
anova_data_diff_angles_txt.append('\n')
anova_data_diff_angles_txt.append(result_str)
anova_data_diff_angles_txt.append('\n')
w = fried.statistic / (sample_size * (len(data[-1])-1))
anova_data_diff_angles_txt.append('EFFECT SIZE: ' + str(w))
anova_data_diff_angles_txt.append('\n')
pval = fried.pvalue
if pval < 0.05:
anova_data_diff_angles_txt.append('NEMENYI TEST FOR VARIABLE ' + var + ' BETWEEN Targets')
anova_data_diff_angles_txt.append('\n')
data = np.asarray(data)
pairwise = sp.posthoc_nemenyi_friedman(data.T)
anova_data_diff_angles_txt.append(pairwise.to_string())
anova_data_diff_angles_txt.append('\n')
meanRanks, pValues = test_nemenyi(data)
anova_data_diff_angles_txt.append('Nemenyi mean ranks ')
anova_data_diff_angles_txt.append(np.array2string(meanRanks, formatter={'float_kind':lambda x: "%.2f" % x}))
anova_data_diff_angles_txt.append('p-values ')
anova_data_diff_angles_txt.append(np.array2string(pValues, formatter={'float_kind':lambda x: "%.4f" % x}))
anova_data_diff_angles_txt.append('\n')
efx = pg.compute_effsize(data, paired=True, eftype='hedges')
anova_data_diff_angles_txt.append('EFFECT SIZE')
anova_data_diff_angles_txt.append(str(efx))
anova_data_diff_angles_txt.append('\n')
else:
anova_data_diff_angles_txt.append('SPHERICITY CHECK FOR VARIABLE ' + var + ' CONDITION ' + condition)
spher = pg.sphericity(filtered_data_condition, dv='Median', subject='Participant', within=['Target'])
result_str = "Sphericity = {}, W = {}, chi2 = {}, dof = {}, pval = {})".format(spher.spher, spher.W,
spher.chi2, spher.dof,
spher.pval)
anova_data_diff_angles_txt.append('\n')
anova_data_diff_angles_txt.append(result_str)
anova_data_diff_angles_txt.append('\n')
anova_data_diff_angles_txt.append(
'HOMOSCEDASTICITY CHECK FOR VARIABLE ' + var + ' CONDITION ' + condition)
anova_data_diff_angles_txt.append('\n')
homo = pg.homoscedasticity(data=filtered_data_condition, dv='Median', group='Target')
anova_data_diff_angles_txt.append(homo.to_string())
anova_data_diff_angles_txt.append('\n')
anova_data_diff_angles_txt.append('LEVENE DEGREES OF FREEDOM BETWEEN ' + str(df_between))
anova_data_diff_angles_txt.append('\n')
anova_data_diff_angles_txt.append('LEVENE DEGREES OF FREEDOM WITHIN ' + str(df_within))
anova_data_diff_angles_txt.append('\n')
if spher.spher:
anova_data_diff_angles_txt.append('RM ANOVA FOR VARIABLE ' + var)
anova_data_diff_angles_txt.append('\n')
aov = pg.rm_anova(data=filtered_data_condition, dv='Median', within='Target',
subject='Participant', detailed=True, effsize='np2')
anova_data_diff_angles_txt.append(aov.to_string())
anova_data_diff_angles_txt.append('\n')
alpha = 0.05
effect_size = aov['np2']
anova_data_diff_angles_txt.append('EFFECT SIZE ' + str(effect_size))
anova_data_diff_angles_txt.append('\n')
nobs = len(filtered_data_condition)
power_analysis = FTestAnovaPower().solve_power(effect_size=effect_size, nobs=nobs, alpha=alpha,
k_groups=len(
filtered_data_condition['Target'].unique()))
anova_data_diff_angles_txt.append('ANOVA POWER ' + str(power_analysis[0]))
anova_data_diff_angles_txt.append('\n')
if aov['p-unc'][0] < 0.05:
anova_data_diff_angles_txt.append('PAIRWISE TEST FOR VARIABLE ' + var + ' BETWEEN Targets')
anova_data_diff_angles_txt.append('\n')
pairwise = pg.pairwise_tests(data=filtered_data_condition, dv='Median', between='Target',
subject='Participant', parametric=True, marginal=True,
alpha=0.05, alternative='two-sided', padjust='bonf',
effsize='hedges', correction='auto', return_desc=True,
interaction=True, within_first=True)
anova_data_diff_angles_txt.append(pairwise.to_string())
anova_data_diff_angles_txt.append('\n')
else:
anova_data_diff_angles_txt.append('GREENHOUSE RM ANOVA FOR VARIABLE ' + var)