Code covered by the BSD License  

Highlights from
ManHaenORtest

from ManHaenORtest by Antonio Trujillo-Ortiz
Mantel-Haenszel odds ratio inference for k strata of 2x2 tables.

ManHaenORtest(X,t,alpha)
function ManHaenORtest(X,t,alpha)
%MANHAENORTEST Mantel-Haenszel's Odds Ratio Test.
%   MANHAENORTEST, performs an inference on odds ratio for k strata of 2x2 tables.
%   Mantel and Haenszel (1959) proposed an approximate test of the null hypothesis
%   which specifies that within each stratum, the success probabilities are equal,
%
%        Ho: 1_p_1 = 1_p_2, 2_p_1 = 2_p_2, . . ., k_p_1 = k_p_2.
%
%   This is the same to test that there is a common odds ratio and it is equal to 1,
%
%        Ho: OR_1 = OR_2 = . . . = OR_k = 1.
%
%   An odds ratio (OR) is defined as,
%
%        OR_i = (i_p_1/(1-i_p_1))/(i_p_2/(1-i_p_2));  i = 1,2,...,k.
%
%   The null hypothesis allows for the common success probabilities to differ from 
%   stratum to stratum.
%
%   Each stratum must be a 2x2 table design such as,
%
%                  S       F
%                --------------
%     Sample-1     a       b      r1=a+b
%        
%     Sample-2     c       d      r2=c+d
%                --------------
%                c1=a+c  c2=b+d  n=c1+c2
%
%   (S=success; F=failure)
%
%   Here, we use the approximate conditional procedure. The given marginal totals 
%   are fixed and the random variable for each stratum ai has a hypergeometric
%   distribution.
%
%             P(ai = x) = (r1i_C_x)*(r2i_C_c1i-x)/(ni_C_c1i).
%
%   Syntax: function ManHaenORtest(X,t,alpha) 
%      
%   Inputs:
%         X - data matrix (size kx4) of the observed frequency
%             cells (a,b,c,d) for each stratum.
%         t - desired test [t = 1, one-tail; t = 2, two-tail (default)].
%     alpha - significance level (default = 0.05).
%
%   Output:
%         A table with:
%         - Percentage of success of samples on each stratum.
%         A table with:
%         - Mantel-Haenszel statistic, number of strata and P-value.
%
%   Example: From the example 10.5 given by Hollander and Wolfe (1999) for 19 2x2
%            strata we are interested to test if the studied strata have a common odds
%            ratio that is 1. The formed table is given as follows.
%
%                       -------------------------------
%                                   Frequency cells
%                               ----------------------- 
%                       Stratum   a     b     c     d
%                       -------------------------------
%                         1       4     9    12    34
%                         2       4     6    34    33
%                         3       7     2     6     7
%                         4       5     5    59    56
%                         5       7     7    22    69
%                         6       5     6    41    80
%                         7       3     6     8    72
%                         8       4     8     1    20
%                         9       7     2    77    38
%                        10       4     6    20    70
%                        11       1     8    16    76
%                        12       4    10    10    91
%                        13       9    18    27   118
%                        14       3     5    35    45
%                        15       9     5    69    20
%                        16       2     3     3    12
%                        17       6     1    45    31
%                        18      14    10    12    70
%                        19      15    15    43   129
%                       -------------------------------
%
%   Data matrix must be:
%      X=[4 9 12 34;4 6 34 33;7 2 6 7;5 5 59 56;7 7 22 69;5 6 41 80;3 6 8 72;4 8 1 20;
%      7 2 77 38;4 6 20 70;1 8 16 76;4 10 10 91;9 18 27 118;3 5 35 45;9 5 69 20;
%      2 3 3 12;6 1 45 31;14 10 12 70;15 15 43 129];
%   
%                                       
%   Calling on Matlab the function: 
%             ManHaenORtest(X,1,0.05)
%
%   Answer is:
%
%   Table of the percentage of success of samples
%   on each stratum.
%   ----------------------------------------------
%                                 Sample  
%                  -------------------------------
%     Stratum             1                  2
%   ----------------------------------------------
%         1             30.769             26.087
%         2             40.000             50.746
%         3             77.778             46.154
%         4             50.000             51.304
%         5             50.000             24.176
%         6             45.455             33.884
%         7             33.333             10.000
%         8             33.333              4.762
%         9             77.778             66.957
%        10             40.000             22.222
%        11             11.111             17.391
%        12             28.571              9.901
%        13             33.333             18.621
%        14             37.500             43.750
%        15             64.286             77.528
%        16             40.000             20.000
%        17             85.714             59.211
%        18             58.333             14.634
%        19             50.000             25.000
%   ----------------------------------------------
% 
%   Table for the Mantel-Haenszel's odds ratio test.
%   ------------------------------------------------
%     MH statistic     Number of strata       P  
%   ------------------------------------------------
%        5.0984               19           0.0000
%   ------------------------------------------------
%   For a selected one-saided test.
%   With a given significance of: 0.050
%   If P-value >= alpha, the alternatives H1: i_p_1 > i_p_2
%   result not significatives. Otherwise, they result significatives.
%
%   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.
%              atrujo@uabc.mx
%              And the special collaboration of the post-graduate students of the 2004:2
%              Multivariate Statistics Course: Laura Rodriguez-Cardozo, Norma Alicia Ramos-Delgado,
%              and Rene Garcia-Sanchez. 
%   Copyright (C) November 3, 2004.
%
%  To cite this file, this would be an appropriate format:
%  Trujillo-Ortiz, A., R. Hernandez-Walls, A. Castro-Perez, L. Rodriguez-Cardozo, 
%    N.A. Ramos-Delgado and R. Garcia-Sanchez. (2004). ManHaenORtest:Mantel-Haenszel's
%    Odds Ratio Test. A MATLAB file. [WWW document]. URL http://www.mathworks.com/
%    matlabcentral/fileexchange/loadFile.do?objectId=6200
%
%   References:
% 
%   Hollander, M. and Wolfe, D.A. (1999), Nonparametric Statistical Methods (2nd ed.).
%         NY: John Wiley & Sons. p. 484-488.
%   Mantel, N. and Haenszel, W. (1959), Statistical aspects of the analysis of data 
%         from retrospective studies of disease. J. Natl. Cancer Inst., 22:719-748.
%

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;

[xr xc] = size(X);

if xc ~= 4,
    error('Matrix must have four columns. The a,b,c,d entry for each stratum.');
    return;
end;

r1s = [X(:,1)+X(:,2)];  %vector of marginal total rows-1
r2s = [X(:,3)+X(:,4)];  %vector of marginal total rows-2
c1s = [X(:,1)+X(:,3)];  %vector of marginal total columns-1
c2s = [X(:,2)+X(:,4)];  %vector of marginal total columns-2
ns = [X(:,1)+X(:,2)+X(:,3)+X(:,4)];   %vector of total of tables 2x2
Eo = (r1s.*c1s)./ns;  %vector of null means
Vo = (r1s.*c1s.*r2s.*c2s)./((ns.^2).*(ns-1));  %vector of null variances
MH = (sum(X(:,1))-sum(Eo))/sqrt(sum(Vo));  %Mantel-Haenszel statistic

if t == 1;
    P = 1-normcdf(abs(MH));  %approximate normal distribution for an one-sided test
else t == 2;
    MH = MH^2;
    P = 1-chi2cdf(MH^2,1);  %approximate Chi-square distribution for a two-sided test
end;

pa = X(:,1)./r1s*100;  %percentage of success of sample-1
pc = X(:,3)./r2s*100;  %percentage of success of sample-2

S=[];
for i = 1:xr
    S=[S;i];
end;

disp(' ')
disp('Table of the percentage of success of samples')
disp('on each stratum.')
fprintf('----------------------------------------------\n');
disp('                              Sample  '); 
fprintf('               -------------------------------\n');
disp('  Stratum             1                  2'); 
fprintf('----------------------------------------------\n');
fprintf('%7.i       %12.3f       %12.3f\n',[S,pa,pc].');
fprintf('----------------------------------------------\n');
disp(' ')
disp('Table for the Mantel-Haenszel''s odds ratio test.')
fprintf('------------------------------------------------\n');
disp('  MH statistic     Number of strata       P  '); 
fprintf('------------------------------------------------\n');
fprintf(' %10.4f       %10.i       %10.4f\n',[MH,xr,P].');
fprintf('------------------------------------------------\n');
if t == 1;
    disp('For a selected one-saided test.')
    fprintf('With a given significance of: %.3f\n', alpha);
    if MH < 0.0;
        disp('If P-value >= alpha, the alternatives H1: i_p_1 < i_p_2')
        disp('result not significatives. Otherwise, they result significatives.')
    else MH > 0.0;
        disp('If P-value >= alpha, the alternatives H1: i_p_1 > i_p_2')
        disp('result not significatives. Otherwise, they result significatives.')
    end;
else t == 2;
    disp('For a selected two-saided test.')
    fprintf('With a given significance of: %.3f\n', alpha);
    disp('If P-value >= alpha, the alternatives H1: i_p_1 ne i_p_2')
    disp('result not significatives. Otherwise, they result significatives.')
end;
disp(' ')

return;

Contact us at files@mathworks.com