-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHI_SQUARE_ANOVA_1.py
53 lines (39 loc) · 1.25 KB
/
CHI_SQUARE_ANOVA_1.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
import scipy.stats as stats
# Define null and alternative hypothesis
# For example, if we have three groups: A, B, and C
# H0: The means of all three groups are equal
# H1: At least one of the means is different from the others
# Select significance level
alpha = 0.05
# Sample data
A = [4,3,4]
B = [3,2,5]
C = [5,3,6]
# Calculate sum of all observations
T = sum(A) + sum(B) + sum(C)
# Calculate correction factor
nT = len(A) + len(B) + len(C)
CF = T**2 / nT
# Calculate sum of squares total
SST = sum([x**2 for x in A+B+C]) - CF
# Calculate sum of squares between columns
SSB = sum([sum(g)**2/len(g) for g in [A, B, C]]) - CF
# Calculate sum of squares within columns
SSW = SST - SSB
# Calculate mean of squares between groups
MSB = SSB / 2
# Calculate mean of squares within groups
MSW = SSW / 12
# Calculate F value
Fcal = MSB / MSW
# Calculate critical F value
dfnum = 2 # k - 1
dfden = 12 - 2 # nT - k
Fcrit = stats.f.ppf(q=1-alpha, dfn=dfnum, dfd=dfden)
print(Fcal)
print(Fcrit)
# Compare Fcal and Fcrit
if Fcal > Fcrit:
print("Reject null hypothesis. At least one of the means is different from the others.")
else:
print("Fail to reject null hypothesis. The means of all groups are equal.")