Code covered by the BSD License  

Highlights from
FKtest

from FKtest by Antonio Trujillo-Ortiz
Fligner-Killeen test for homogeneity of variances.

FKtest(X,o)
function FKtest(X,o)
%FKTEST Fligner-Killeen test for homogeneity of variances.
%There are several tests for homogeneity of variances. Fligner and
% Killeen (1976) suggest ranking (i) the |x_ij - mean| as normal type 
% scores (a) by (0.5 + i/2(N+1)). Where i is the group and j the
% observation. The replacement of the mean by the median, a  modification,
% is an attempt to improve the robustness of the test. From these scores is
% formulated a statistic based on a Chi-squared or F distribution.
%
% According to Conover et al. (1981), the Fligner-Killeen test by median is
% one of the best tests to use on the basis of robustness and power. Also,
% by several simulations, the Type I error rate and power is slightly larger
% when F approximation was used than the Chi-squared approximation. Some of
% these tests are very sensitive to outliers, but Fligner test is not.
% Fligner test is the most robust against departures from normality.

%
% Syntax: function FKtest(X,o) 
%      
% Inputs:
%    X - Data matrix; Data=column 1, Group=column 2
%    o - By option median=1 (default) or mean=2
% Outputs:
%    Complete analysis of the homogeneity of variances test by a 
%    Chi-squared and F approximation
%
% Example: The manager of a supermarket chain wants to see if the 
% consumption in kilowatts of 4 stores between them are equal. He collects
% data at the end of each month for 6 months. He is interested to test the 
% homoscedasticity. The results are:
%
%                     -----------------------
%                              Store
%                     -----------------------
%                       A     B     C     D
%                     -----------------------
%                       65    64    60    62
%                       48    44    50    46
%                       66    70    65    68
%                       75    70    69    72
%                       70    68    69    67
%                       55    59    57    56
%                     -----------------------
%
% Data matrix must be:
% X = [65 1;48 1;66 1;75 1;70 1;55 1;64 2;44 2;70 2;70 2;68 2;59 2;
%    60 3;50 3;65 3;69 3;69 3;57 3;62 4;46 4;68 4;72 4;67 4;56 4];
%
% Calling on Matlab the function: 
%     FKtest(X)
%
% Answer is:
%
% Fligner-Killeen:med chi-squared = 0.1316
% df =  3
% p-value = 0.9878
% 
% Fligner-Killeen:med F = 0.0384
% df1 =  3, df2 = 20
% p-value = 0.9896
%
% Created by A. Trujillo-Ortiz, R. Hernandez-Walls and N. Castro-Castro
%            Facultad de Ciencias Marinas
%            Universidad Autonoma de Baja California
%            Apdo. Postal 453
%            Ensenada, Baja California
%            Mexico.
%            atrujo@uabc.mx
%
% Copyright. August 14, 2009.
%
% To cite this file, this would be an appropriate format:
% Trujillo-Ortiz, A., R. Hernandez-Walls and N. Castro-Castro. (2009).FKtest:
%   Fligner-Killeen test for homogeneity of variances. A MATLAB file. [WWW document].
%   URL http://www.mathworks.com/matlabcentral/fileexchange/25040
%
% References:
% Conover, W. J., Johnson, M. E. and Johnson, M. M. (1981), A Comparative
%     Study of Tests for Homogeneity of Variances, with Applications to the
%     Outer Continental Shelf Bidding Data. Technometrics, 23(4):351-361.
% Fligner, M. A. and Killeen, T. J. (1976), Distribution Free Two-Sample
%     Tests fos Scale. Journal of the American Statistical Association,
%     71:210-213.
%

if nargin == 1, %Fligner-Killeen median procedure by default
   o = 1;
end

n = length(X); %total size

k = max(X(:,2)); %number of groups

%Fligner-Killeen procedures
if o == 1,
    %by the median (med)
    md=[];Y=[];
    indice = X(:,2);
    for i = 1:k,
        Xe = find(indice==i);
        eval(['X' num2str(i) '=X(Xe,1);']);
        eval(['md' num2str(i) '=median(X' num2str(i) ');'])
        eval(['xmd= md' num2str(i) ';'])
        eval(['y= abs(X' num2str(i) ' - md' num2str(i) ');'])
        md=[md;xmd];Y=[Y;y];
    end
else
    %by the mean 
    m=[];Y=[];
    indice = X(:,2);
    for i = 1:k,
        Xe = find(indice==i);
        eval(['X' num2str(i) '=X(Xe,1);']);
        eval(['m' num2str(i) '=mean(X' num2str(i) ');'])
        eval(['xm= m' num2str(i) ';'])
        eval(['y= abs(X' num2str(i) ' - m' num2str(i) ');'])
        m=[m;xm];Y=[Y;y];
    end
end
    
Z = tiedrank(Y); %score ranking

a = norminv(.5+(Z/(2*(n+1)))); %score standard normal distibution

A = [a' X(:,2)];

M = mean(A(:,1)); %overall mean score

m=[];n=[];B=[];
indice=A(:,2);
for i=1:k
   Ae=find(indice==i);
   eval(['A' num2str(i) '=A(Ae,1);']);
   eval(['m' num2str(i) '=mean(A' num2str(i) ');'])
   eval(['n' num2str(i) '=length(A' num2str(i) ') ;'])
   eval(['xm= m' num2str(i) ';'])
   eval(['xn= n' num2str(i) ';'])
   eval(['b =(n' num2str(i) '*(m' num2str(i) ' - M).^2);']);
   m=[m;xm];n=[n;xn];B=[B;b];
end

n = sum(n);

V = var(A(:,1)); %overall variance score

X2 = sum(B)/V; %Fligner-Killeen statistic by the Chi-squared approximation

v = k-1; %statistic degrees of freedom

F = (X2/v)/((n-1-X2)/(n-k)); %Fligner-Killeen statistic by the F approximation

P1 = 1-chi2cdf(X2,v); %Chi-squared p-value

P2 = 1-fcdf(F,v,n-k); %F p-value

if o == 1,
    disp(' ')
    fprintf('Fligner-Killeen:med chi-squared = %3.4f\n', X2);
    fprintf('df = %2i\n', v);
    fprintf('p-value = %3.4f\n', P1);
    disp(' ')
    fprintf('Fligner-Killeen:med F = %3.4f\n', F);
    fprintf('df1 = %2i, df2 = %2i\n', v,n-k);
    fprintf('p-value = %3.4f\n', P2);
else
    disp(' ')
    fprintf('Fligner-Killeen:mean chi-squared = %3.4f\n', X2);
    fprintf('df = %2i\n', v);
    fprintf('p-value = %3.4f\n', P1);
    disp(' ')
    fprintf('Fligner-Killeen:mean F = %3.4f\n', F);
    fprintf('df1 = %2i, df2 = %2i\n', v,n-k);
    fprintf('p-value = %3.4f\n', P2);
end
disp(' ')

return

Contact us at files@mathworks.com