-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmcomptest.m
206 lines (203 loc) · 7.63 KB
/
tmcomptest.m
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
function tmcomptest(X,alpha)
%TMCOMPTEST Tukey's HSD multiple comparisons test among proportions.
% A statistical analysis used for count data is to compare the proportions
% of a binary outcome of interest for two or more groups.
% This m-file it is an implemented alternative to the Marascuillo's test and
% identifies significant pairwise contrasts that this last one does not.
% If the previously Chi-square test results in a p-value smaller to an alpha-
% value, the results are deemed significant, the null hypothesis that all
% proportions are equal is rejected, and it is concluded that there is a
% significant difference between at least two of the proportions.
% We can look at the pairwise comparisons obtained out of the Tukey's honest
% significant difference test empoyed. You will observe the comparisons
% identified as significant. We can compare up to 20 proportions.
%
% Syntax: tmcomptest(X,alpha)
%
% Input:
% X - data matrix size rx2. Rows=number of groups. Columnn 1=number of
% successes or interested events. Column 2=number of trials.
% alpha - significance level (default = 0.05)
%
% Output:
% - Complete Tukey's HSD multiple comparisons test among proportions
% table
%
% Example: From Zar (1999), after a significant Chi-square test of 4 group
% proportions (P = 0.03), we are interested to analyse which pair or pairs
% of groups have significantly different proportions with a significance of
% 0.05. Data table is as follows:
%
% Group
% -----------------------------
% 1 2 3 4
% -----------------------------
% Successes (events of interest) 32 43 16 9
% Fails 55 65 64 16
% -----------------------------
% Trials 87 108 80 25
% -----------------------------
% Proportion (p) 0.3678 0.3981 0.2000 0.3600
% -----------------------------
%
% Data vector is:
% X=[32 87;43 108;16 80;9 25];
%
% Calling on Matlab the function:
% mcomptest(X)
%
% Answer is:
%
% Group proportions of the interested events are:
% -------------------------------------------
% Group Proportions
% -------------------------------------------
% 1 0.3678
% 2 0.3981
% 3 0.2000
% 4 0.3600
% -------------------------------------------
%
% Tukey's multiple comparisions test among proportions, k = 4
% -------------------------------------------------------------------------
% Comparision Difference* SE q qc Decision
% -------------------------------------------------------------------------
% 4 3 10.3574 4.603 2.250 3.633 NS
% 4 2 1.9932 4.458 0.447 3.633 NS
% 4 1 0.2396 4.559 0.053 3.633 NS
% 3 2 12.3507 2.980 4.145 3.633 S
% 3 1 10.5970 3.128 3.387 3.633 NS
% 2 1 1.7536 2.911 0.602 3.633 NS
% -------------------------------------------------------------------------
% With a given significance level of: 0.05
% The multiple comparisions can be significant (S) or not significant (NS).
% * After asin transformation of proportions.
%
% Created by A. Trujillo-Ortiz, R. Hernandez-Walls, K. Barba-Rojo and
% A. Castro-Perez
% Facultad de Ciencias Marinas
% Universidad Autonoma de Baja California
% Apdo. Postal 453
% Ensenada, Baja California
% Mexico.
%
% Copyright. July 4, 2007.
%
% To cite this file, this would be an appropriate format:
% Trujillo-Ortiz, A., R. Hernandez-Walls, K. Barba-Rojo and A. Castro-Perez.
% (2007). tmpomptest:Tukey's HSD multiple comparisons test among proportions.
% A MATLAB file. [WWW document]. URL http://www.mathworks.com/matlabcentral/
% fileexchange/loadFile.do?objectId=15499
%
% References:
% Zar, J. H. (1999), Biostatistical Analysis (2nd ed.).
% NJ: Prentice-Hall, Englewood Cliffs. p.564.
%
switch nargin
case{2}
if isempty(X) == false && isempty(alpha) == false
if (alpha <= 0 || alpha >= 1)
fprintf('Warning: Significance level error; must be 0 < alpha < 1 \n');
return;
end
end
case{1}
alpha = 0.05;
otherwise
error('Requires at least one input argument.');
end
s = X(:,1); %number of successes
t = X(:,2); %number of trials
is = fix(s)==s;
it = fix(t)==t;
if (sum(is) ~= length(s)) | (sum(it) ~= length(t)),
error('Data is not in integer format. Please, check it.');
return;
end
f = t-s;
if any(f<0)
error('Number of events must be less than the number of trials. Check it.');
end
k = length(X);
if k < 2,
error('Requires at least two groups.');
return;
end
if k > 20,
error('Cannot compare more than 20 proportions.');
return,
end
%arcsin transform of each proportion as follows
p = [];
for i = 1:k
x = 0.5*(asin(sqrt(s(i)/(t(i)+1)))+asin(sqrt((s(i)+1)/(t(i)+1))))*180/pi;
p = [p x];
end
e = s;
CO=[];dpx=[];se=[];
for i = k:-1:2
for s = i-1:-1:1
if s ~= i
Co = [i s];
CO = [CO;Co];
difp = abs(p(i)-p(s));
dpx = [dpx;difp];
sep = sqrt((410.35/(t(i)+0.5))+(410.35/(t(s)+0.5)));
se = [se;sep];
end
end
end
q = dpx./se;
%Tukey's critical values up to 20 groups for infinity degrees of freedom for
%alpha-value=0.01, 0.05 and 0.10
if alpha == 0.01;
c = [3.643 4.120 4.403 4.603 4.757 4.882 4.987 5.078 5.157 5.227 5.290 ...
5.348 5.400 5.448 5.493 5.535 5.574 5.611 5.645];
elseif alpha == 0.05;
c = [2.772 3.314 3.633 3.858 4.030 4.170 4.286 4.387 4.474 4.552 4.622 ...
4.685 4.743 4.796 4.845 4.891 4.934 4.974 5.012];
else alpha == 0.10;
c = [2.326 2.902 3.240 3.478 3.661 3.808 3.931 4.037 4.129 4.211 4.285 ...
4.351 4.412 4.468 4.519 4.568 4.612 4.654 4.694];
end
v = k-1;
qc = c(v);
c = size(CO);
c = c(1);
Ds = [];
for i = 1:c
if (q(i) >= qc);
ds = ' S';
else (q(i) < qc);
ds = 'NS';
end;
Ds = [Ds;ds];
end;
warning ('off');
qc = [ones(size(q))]*qc;
%qc = [ones(size(q),1)]*qc;
warning ('on');
ss = 1:k
ps = e./t
disp(' ');
disp('Group proportions of the interested events are:');
disp('-------------------------------------------');
disp('Group Proportions');
disp('-------------------------------------------');
fprintf(' %d %17.4f\n',[ss',ps].');
disp('-------------------------------------------');
disp(' ');
fprintf('Tukey''s multiple comparisions test among proportions, k = %.i\n', k);
disp('-------------------------------------------------------------------------');
disp(' Comparision Difference* SE q qc Decision');
disp('-------------------------------------------------------------------------');
for ml=1:k*((k-1)/2)
fprintf(' %d %d %11.4f %6.3f %4.3f %4.3f %s\n', ...
CO(ml,1),CO(ml,2),dpx(ml),se(ml),q(ml),qc(ml,:),Ds(ml,:));
end;
disp('-------------------------------------------------------------------------');
fprintf('With a given significance level of:% 3.2f\n', alpha);
disp('The multiple comparisions can be significant (S) or not significant (NS).');
disp('* After asin transformation of proportions.');
return,