Repository navigation
Expand file tree
/
Copy pathfriedman_wilcoxon.py
More file actions
91 lines (63 loc) · 2.38 KB
/
Copy pathfriedman_wilcoxon.py
File metadata and controls
91 lines (63 loc) · 2.38 KB
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
# guidance found here: https://stats.stackexchange.com/questions/467467/how-to-do-friedman-test-and-post-hoc-test-on-python
from scipy.stats import friedmanchisquare, wilcoxon
import numpy as np
import pandas as pd
import itertools
import matplotlib.pyplot as plt
# create df
nums = np.random.randint(0,100,(270))
labels = ["a","b","c"]*90
xyz = [1,2,3]*90
df = pd.DataFrame({'labels':labels, 'nums':nums, 'xyz':xyz})
#%%
# boxplot graph of data
labels = df['labels'].unique()
collection=[]
for label in labels:
test = df[df['labels']==label]
collection.append(df['nums'][df['labels']==label].to_list())
# make list
#for i in range(0,len(labels)):
# labels[i] = collection[i]
def boxplot_graph(data, labels):
fig, ax = plt.subplots()
ax.boxplot(x=data, labels=labels, showmeans = False)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.set_xlabel('Type',size=10)
ax.set_ylabel("Number", size=10)
ax.set_title('Title',size=12)
# annotate graph
#for x in zip()
#set size of graph
cmsize=1/2.54
fig.set_size_inches(30*cmsize, 15*cmsize)
return ax.boxplot(x=data, labels=labels, showmeans = False)
# annotate graph
#for x in zip()
#set size of graph
cmsize=1/2.54
fig.set_size_inches(30*cmsize, 15*cmsize)
bp = boxplot_graph(collection, labels=labels)
#%%
# convert labels to factor
df['labels_fct'] = pd.factorize(df['labels'])[0]
# drop categorical column
df1 = df.drop('labels', axis="columns")
# run the friedman test through the columns
f_test = friedmanchisquare(df1['labels_fct'],df1['nums'],df1['xyz'])
f_res = pd.DataFrame({'test':'Friedman','statistic':f_test[0],'pvalue':f_test[0]},index=[0])
# run pairwise wilcoxon
wilc_test = [wilcoxon(df1[i],df1[j]) for i,j in itertools.combinations(df1.columns,2)]
w_res = pd.DataFrame(wilc_test)
w_res['test'] = ["wilcoxon " + i+" vs "+j for i,j in itertools.combinations(df1.columns,2)]
# concat the results
res = pd.concat([f_res,w_res])
#%%
from scipy.stats import ks_2samp
# perform Kolmogorov-Smirnov test
ks_tst = ks_2samp(df['nums'][df['labels']=="a"], df['nums'][df['labels']=="b"])
#%%
from scipy import stats
# perform Kruskal–Wallis anova test
kw_tst = stats.kruskal(df['nums'][df['labels']=="a"], df['nums'][df['labels']=="b"])