-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatsFunction.m
More file actions
111 lines (68 loc) · 3.02 KB
/
Copy pathstatsFunction.m
File metadata and controls
111 lines (68 loc) · 3.02 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
function [contTable,chiResults,fisherExtract,McNemResult] = statsFunction(group1,group2,label1,label2,tailNr,mcNemar)
% this function receives 2 groups you want to compare as input, generates contingency table, conducts chi square of
% independence. As output it returns chi square and critical value together
% with the text stating significance of the results. It also returns
% ficherExtract results
% for tail number, add 1 for right-tailed fisher's exact test and add 2 for
% two-tailed fisher's exact test
% mcNemar == 1, means you want to conduct mcNemar test
% label 1 : one you expect to be higher (if you have directional hypothesis
% contingency table
accuracyLabels = {'correct', 'wrong'};
groupNames = {'group1','group2'};
IV = categorical(group1,{label1,label2},groupNames);
DV = categorical(group2,[1, 0], accuracyLabels);
%conTable = [x,y];
[contTable, chi2, pValue] = crosstab(IV, DV);
% calculate critical chi
alpha = 0.05;
df = 1; % nr of groups - 1
criticalChi = chi2inv((1-alpha),df); % p is percentile, 1 is df
% compare the criticalChi with chi2
if chi2 > criticalChi
disp('chi result : groups are significantly different');
else
disp('chi result : groups are not significanly different-chi');
end
% create the table
chiResults = table(chi2,criticalChi,pValue,'VariableNames',{'chisquare','criticalChi','pvalue'});
%fisher's exact test
%check whether there are zeros in the table
if any(contTable(:) == 0)
disp('Zeros found in the table, adjusting counts.'); % add constant to avoid zeros - haldane-anscombe correction
contTable = contTable + 1;
end
if tailNr == 1
[h,p,stats] = fishertest(contTable,'Tail','right','Alpha',0.05);
elseif tailNr == 2
[h,p,stats] = fishertest(contTable,'Tail','both','Alpha',0.05);
end
oddsRatio = stats.OddsRatio;
confInterval = stats.ConfidenceInterval;
if h == 1
disp('Fisher Result : groups are significantly different');
elseif h == 0
disp('Fisher Result : groups are not significantly different');
end
fisherExtract = table(h,p,oddsRatio,confInterval(1),confInterval(2),'VariableNames',{'h','p','oddsRatio','lowerLimit','upperLimit'});
if mcNemar == 1
% make contTable useful for the function
% a = correct in both tests
% b = correct in the second wrong in the first
% c = correct in the first, incorrect in the second
% d = incorrect in both
controlCorrect = contTable(1,1);
controlIncorrect = contTable(1,2);
criticalCorrect = contTable(2,1);
criticalIncorrect = contTable(2,2);
a = min(controlCorrect,criticalCorrect); %correct in both (get the minumum of corrects)
b = max((criticalCorrect-controlCorrect),0); % correct in the second, wrong in the first
c= max((controlCorrect - criticalCorrect),0); % correct in the first and wrong in the second, if negative turns zero
d = min(controlIncorrect,criticalIncorrect); % number that wrong in both
vector = [a,b,c,d];
%mcnemar(vector,alpha);
%McNemarextest(vector,2,alpha);
[midpValue] = mcNemEce(vector,alpha);
McNemResult = {vector,midpValue};
end
end