-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanova.py
76 lines (53 loc) · 1.74 KB
/
anova.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
import numpy as np
import scipy.special as special
def FPvalue( *args):
""" Return F an p value
"""
df_btwn, df_within = __degree_of_freedom_( *args)
mss_btwn = __ss_between_( *args) / float( df_btwn)
mss_within = __ss_within_( *args) / float( df_within)
F = mss_btwn / mss_within
P = special.fdtrc( df_btwn, df_within, F)
return( F, P)
def EffectSize( *args):
""" Return the eta squared as the effect size for ANOVA
"""
return( float( __ss_between_( *args) / __ss_total_( *args)))
def __concentrate_( *args):
""" Concentrate input list-like arrays
"""
v = list( map( np.asarray, args))
vec = np.hstack( np.concatenate( v))
return( vec)
def __ss_total_( *args):
""" Return total of sum of square
"""
vec = __concentrate_( *args)
ss_total = sum( (vec - np.mean( vec)) **2)
return( ss_total)
def __ss_between_( *args):
""" Return between-subject sum of squares
"""
# grand mean
grand_mean = np.mean( __concentrate_( *args))
ss_btwn = 0
for a in args:
ss_btwn += ( len(a) * ( np.mean( a) - grand_mean) **2)
return( ss_btwn)
def __ss_within_( *args):
"""Return within-subject sum of squares
"""
return( __ss_total_( *args) - __ss_between_( *args))
def __degree_of_freedom_( *args):
"""Return degree of freedom
Output-
Between-subject dof, within-subject dof
"""
args = list( map( np.asarray, args))
# number of groups minus 1
df_btwn = len( args) - 1
# total number of samples minus number of groups
df_within = len( __concentrate_( *args)) - df_btwn - 1
return( df_btwn, df_within)
if __name__ == "__main__":
print( 'Sorry, you can not use it in this way.')