-
Notifications
You must be signed in to change notification settings - Fork 0
/
McNemarextest.asv
238 lines (224 loc) · 7.95 KB
/
McNemarextest.asv
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
function McNemarextest(v,t,alpha)
%MCNEMAREXTEST McNemar's Exact Test.
% MCNEMAREXTEST, performs the conditional as well as the Chi-squared
% corrected for discontinuity McNemar's exact test for two dependent
% (correlated) samples that can occur in matched-pair studies with a
% dichotomous (yes-no) response. Dependent samples can also occur when
% the same subject is measured at two different times. It tests the null
% hypothesis of marginal homogeneity. This implies that rows totals are
% equal to the corresponding column totals. So, since the a- and the
% d-value on both sides of the equations cancel, then b = c. This is the
% basis of the proposed test by McNemar (1947). The McNemar test tests the
% null hypothesis that conditional on b + c, b has a binomial (b + c, 1/2)
% distribution.
%
% According to the next 2x2 table design,
%
% Sample 1
% --------------
% Y N
% --------------
% Y a b r1=a+b
% Sample 2
% N c d r2=c+d
% --------------
% c1=a+c c2=b+d n=c1+c2
%
% (Y = yes; N = no)
%
% The proper way to test the null hypothesis is to apply the one-sample
% binomial test. If there is no association between b and c values, then
% the probability is 0.5 that the sample 1 and sample 2 pair falls in the
% upper-right cell and 0.5 that it falls in the lower-left cell, given
% that the pair falls off the main diagonal.
%
% Syntax: function McNemarextest(v,t,alpha)
%
% Inputs:
% v - data vector defined by the observed frequency cells [a,b,c,d].
% t - desired test [t = 1, one-tail; t = 2, two-tail (default)].
% alpha - significance level (default = 0.05).
%
% Output:
% A table with the proportion of success for the dependent samples
% and the P-value.
%
% Example: From the example on Table 10.5 given by Hollander and Wolfe (1999), in a
% matched-pair study we are interested to testing by the McNemar's exact
% one-sided test the null hypothesis that the success for the dependent
% samples (sample 1 and sample 2) are equal. The data are given as follow.
%
% Sample 1
% ---------------------
% Y N
% ---------------------
% Y 26 15
% Sample 2
% N 7 37
% ---------------------
%
% v = [26,15,7,37];
%
% Calling on Matlab the function:
% McNemarextest(v,1,0.05)
%
% Answer is:
%
% Table for the McNemar's exact test.
% -------------------------------------------
% Proportion of success
% --------------------------------
% Sample 1 Sample 2 P
% -------------------------------------------
% 0.3882 0.4824 0.066900
% -------------------------------------------
% For a selected one-saided test.
% With a given significance of: 0.050
% The test results not significative.
%
% Table for the McNemar's test by the Chi-squared and
% corrected for discontinuity.
% -----------------------------------------------------------
% Proportion of success
% --------------------------------
% Sample 1 Sample 2 Chi2 P
% -----------------------------------------------------------
% 0.3882 0.4824 2.2273 0.135593
% -----------------------------------------------------------
% For a selected one-saided test.
% With a given significance of: 0.050
% The test results not significative.
%
% Created by A. Trujillo-Ortiz, R. Hernandez-Walls and A. Castro-Perez
% Facultad de Ciencias Marinas
% Universidad Autonoma de Baja California
% Apdo. Postal 453
% Ensenada, Baja California
% Mexico.
% Copyright (C) November 5, 2004.
%
% $$Authors thank the valuable to-improve comments on the m-file review given
% by I.Y., dated 2006-05-23. Modified lines are 138-143$$
%
% To cite this file, this would be an appropriate format:
% Trujillo-Ortiz, A., R. Hernandez-Walls and A. Castro-Perez. (2004).
% McNemarextest:McNemar's Exact Probability Test. A MATLAB file. [WWW document].
% URL http://www.mathworks.com/matlabcentral/fileexchange/6297
%
% References:
%
% Hollander, M. and Wolfe, D.A. (1999), Nonparametric Statistical Methods (2nd ed.).
% NY: John Wiley & Sons. p. 468-470.
% McNemar, Q. (1947), Note of the sampling error of the difference between
% correlated proportions or percentages. Psychometrika, 12:153-157.
%
if length(v) ~= 4
error('Warning:Vector must have four data. The a,b,c,d entry for the 2x2 table.');
return;
end
if ~all(isfinite(v(:))) || ~all(isnumeric(v(:)))
error('Warning:All X values must be numeric and finite')
elseif ~isequal(v(:),round(v(:)))
error('Warning:X data matrix values must be whole numbers')
end
if nargin < 3
alpha = 0.05; %(default)
elseif (length(alpha)>1)
error('Requires a scalar alpha value.');
elseif ((alpha <= 0) || (alpha >= 1))
error('Requires 0 < alpha < 1.');
end
if nargin < 2
t = 2; %two-tailed test
end
%Observed frequency cells definition
a = v(1);
b = v(2);
c = v(3);
d = v(4);
%Marginal totals calculation
r1 = a+b;
r2 = c+d;
c1 = a+c;
c2 = b+d;
n = c1+c2;
%Interested sample proportions
P1 = c1/n;
P2 = r1/n;
%One-sample Binomial distribution procedure
p = 0.5;
m = b+c;
x = max(b,c);
warning off
P = 0;
for i = x:m
P = P+(p.^i)*(1-p).^(m-i)*nchoosek(m,i);
end
warning on
%Kind hypothesis selection
if t == 1
P = P;
else t = 2;
P = 2*P;
end
disp(' ')
disp('Table for the McNemar''s exact test as conditional.')
fprintf('-------------------------------------------\n');
disp(' Proportion of success ');
fprintf('--------------------------------\n');
disp(' Sample 1 Sample 2 P ');
fprintf('-------------------------------------------\n');
fprintf(' %10.4f %10.4f %8.6f\n',[P1,P2,P].');
fprintf('-------------------------------------------\n');
if t = 1
disp('For a selected one-saided test.')
fprintf('With a given significance of: %.3f\n', alpha);
if P > alpha
disp('The test results not significative.')
else P < alpha;
disp('The test results significative.')
end
else t = 2;
disp('For a selected two-saided test.')
fprintf('With a given significance of: %.3f\n', alpha/2);
if P > alpha/2
disp('The test results not significative.')
else P < alpha/2;
disp('The test results significative.')
end
end
disp(' ')
N = v(2)-v(3);
D = v(2)+v(3);
X2 = N^2/D;
X2c = (abs(N)-1)^2/D;
p = 1-chi2cdf(X2c,1);
disp(' ')
disp('Table for the McNemar''s test by the Chi-squared and')
disp('corrected for discontinuity.')
fprintf('-----------------------------------------------------------\n');
disp(' Proportion of success ');
fprintf('--------------------------------\n');
disp(' Sample 1 Sample 2 Chi2 P ');
fprintf('-----------------------------------------------------------\n');
fprintf(' %10.4f %10.4f %10.4f %5.6f\n',[P1,P2,X2c,p].');
fprintf('-----------------------------------------------------------\n');
if t == 1
disp('For a selected one-saided test.')
fprintf('With a given significance of: %.3f\n', alpha);
if P > alpha
disp('The test results not significative.');
else P < alpha
disp('The test results significative.');
end
else t = 2;
disp('For a selected two-saided test.')
fprintf('With a given significance of: %.3f\n', alpha/2);
if P > alpha/2
disp('The test results not significative')
else P < alpha/2;
disp('The test results significative')
end
end
end