Home > FMAToolbox > General > BartlettTest.m

BartlettTest

PURPOSE ^

BartlettTest - Test if k groups of samples have equal variances (homogeneity of variances).

SYNOPSIS ^

function [h,p,T] = BartlettTest(data,alpha)

DESCRIPTION ^

BartlettTest - Test if k groups of samples have equal variances (homogeneity of variances).

  This test assumes that all populations follow a Gaussian distribution.

  USAGE

    [h,p,t] = BartlettTest(data,alpha)

    data           Nx2 matrix of (observation,group) pairs
    alpha          optional significance level (default = 0.05)

    h              test result (1 = reject null hypothesis, 0 = accept)
    p              p value
    t              test statistics

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [h,p,T] = BartlettTest(data,alpha)
0002 
0003 %BartlettTest - Test if k groups of samples have equal variances (homogeneity of variances).
0004 %
0005 %  This test assumes that all populations follow a Gaussian distribution.
0006 %
0007 %  USAGE
0008 %
0009 %    [h,p,t] = BartlettTest(data,alpha)
0010 %
0011 %    data           Nx2 matrix of (observation,group) pairs
0012 %    alpha          optional significance level (default = 0.05)
0013 %
0014 %    h              test result (1 = reject null hypothesis, 0 = accept)
0015 %    p              p value
0016 %    t              test statistics
0017 
0018 % Copyright (C) 2004-2011 by Michaƫl Zugaro
0019 %
0020 % This program is free software; you can redistribute it and/or modify
0021 % it under the terms of the GNU General Public License as published by
0022 % the Free Software Foundation; either version 3 of the License, or
0023 % (at your option) any later version.
0024 
0025 % Significance level
0026 if nargin < 2,
0027     alpha = 0.05;
0028 end
0029 
0030 % Number of groups
0031 k = max(data(:,2));
0032 for i = 1:k,
0033     group = data(:,2) == i;
0034     % Number of samples in this group
0035     n(i) = sum(group);
0036     % Variance for this group
0037     s2(i) = var(data(group,1));
0038 end
0039 % Total number of samples
0040 N = sum(n);
0041 % Pooled variance
0042 S2 = sum((n-1).*s2)/(N-k);
0043 
0044 % Test statistics (biased)
0045 t = (N-k)*log(S2)-sum((n-1).*log(s2));
0046 % Bias correction
0047 C = 1+(1/(3*(k-1)))*(sum(1./(n-1))-1/(N-k));
0048 % Test statistics (unbiased)
0049 T = t/C;
0050 
0051 % Chi square at alpha with (k-1) degrees of freedom
0052 p = 1 - chi2cdf(T,k-1);
0053 
0054 h = p < alpha;
0055 
0056 disp(['Bartlett test: Variances: ' num2str(s2)]);
0057 
0058 if h,
0059     message = '+++ Two or more variances are significantly different';
0060 else
0061     message = '--- Variances are not significantly different';
0062 end
0063 message = ['Bartlett test: ' message ' (p='  num2str(p) ', T=' num2str(T) ', N=' int2str(N) ')'];
0064 disp(message);

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005