Home > FMAToolbox > General > WatsonU2Test.m

WatsonU2Test

PURPOSE ^

WatsonU2Test - Test if two samples (circular data) have different means / variances.

SYNOPSIS ^

function [h,U2] = WatsonU2Test(group1,group2,alpha)

DESCRIPTION ^

WatsonU2Test - Test if two samples (circular data) have different means / variances.

  This non-parametric test assumes the data comes from a continuous distribution.

  USAGE

    [h,U2] = WatsonU2Test(group1,group2,alpha)

    group1         angles in radians for group 1
    group2         angles in radians for group 2
    alpha          optional significance level (default = 0.05)

    h              test result (1 = reject null hypothesis, 0 = accept)
    U2             test statistics

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [h,U2] = WatsonU2Test(group1,group2,alpha)
0002 
0003 %WatsonU2Test - Test if two samples (circular data) have different means / variances.
0004 %
0005 %  This non-parametric test assumes the data comes from a continuous distribution.
0006 %
0007 %  USAGE
0008 %
0009 %    [h,U2] = WatsonU2Test(group1,group2,alpha)
0010 %
0011 %    group1         angles in radians for group 1
0012 %    group2         angles in radians for group 2
0013 %    alpha          optional significance level (default = 0.05)
0014 %
0015 %    h              test result (1 = reject null hypothesis, 0 = accept)
0016 %    U2             test statistics
0017 %
0018 
0019 % Copyright (C) 2009-2011 by Michaƫl Zugaro
0020 %
0021 % This program is free software; you can redistribute it and/or modify
0022 % it under the terms of the GNU General Public License as published by
0023 % the Free Software Foundation; either version 3 of the License, or
0024 % (at your option) any later version.
0025 
0026 isradians(group1);
0027 isradians(group2);
0028 
0029 % Significance level
0030 if nargin < 3,
0031     alpha = 0.05;
0032 end
0033 
0034 group1 = group1(:);
0035 group2 = group2(:);
0036 
0037 % Number of angles in each group
0038 n1 = length(group1);
0039 n2 = length(group2);
0040 if n1 < 5 || n2 < 5,
0041     error('This test requires a minimum of 5 angles per group');
0042 end
0043 N = n1+n2;
0044 
0045 % Build an Nx2 matrix:
0046 %  column 1: all data in group 1 and group 2 in ascending order
0047 %  column 2: 0 if data comes from group 1, 1 otherwise
0048 data = [group1 zeros(size(group1));group2 ones(size(group2))];
0049 sorted = sortrows(data);
0050 % Build i and j, the respective cumulative functions for groups 1 and 2
0051 i = zeros(N,1);
0052 j = zeros(N,1);
0053 ingroup1 = sorted(:,2)==0;
0054 i(ingroup1) = 1;
0055 i = cumsum(i)/n1;
0056 j(~ingroup1) = 1;
0057 j = cumsum(j)/n2;
0058 % Compute dk
0059 dk = i-j;
0060 % Test statistic
0061 U2 = n1*n2/N^2*(sum(dk.^2)-sum(dk)^2/N);
0062 
0063 % Perform test
0064 
0065 % Number of elements in each group, modified to fit table entries
0066 k1 = min([n1 n2]);
0067 k2 = max([n1 n2]);
0068 if k1 > 10, k1 = inf; end
0069 if k2 > 12, k2 = inf; end
0070 % Critical values (from Kanji, 100 statistical tests, 1999)
0071 m1 = [5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 9 9 9 9 10 10 10 inf];
0072 m2 = [5 6 7 8 9 10 11 12 6 7 8 9 10 11 12 7 8 9 10 11 12 8 9 10 11 12 9 10 11 12 10 11 12 inf];
0073 if alpha == 0.01,
0074     a = [nan nan nan nan 0.28 0.289 0.297 0.261 nan 0.282 0.298 0.262 0.248 0.262 0.259 0.304 0.272 0.255 0.262 0.253 0.252 0.250 0.258 0.249 0.252 0.252 0.266 0.254 0.255 0.254 0.255 0.255 0.255 0.268];
0075 elseif alpha == 0.05,
0076     a = [0.225 0.242 0.2 0.215 0.191 0.196 0.19 0.186 0.206 0.194 0.196 0.193 0.19 0.187 0.183 0.199 0.182 0.182 0.187 0.184 0.186 0.184 0.186 0.185 0.184 0.185 0.187 0.186 0.185 0.185 0.185 0.186 0.185 0.187];
0077 else
0078     error('This test is implemented for alpha levels of 0.05 and 0.01 only.');
0079 end
0080 % Find critical value
0081 i1 = k1 == m1;
0082 i2 = k2 == m2;
0083 value = a(i1&i2);
0084 if isnan(value),
0085     error(['The test cannot be computed for n1=' int2str(n1) ', n2=' int2str(n2) 'and alpha=' num2str(alpha) '.']);
0086 end
0087 
0088 % Test result
0089 h = U2 >= value;
0090 
0091 disp(['Watson U2 test: Means:       m1=' num2str(CircularMean(group1)) ', m2=' num2str(CircularMean(group2))]);
0092 disp(['Watson U2 test: Variances:   v1=' num2str(CircularVariance(group1)) ', v2=' num2str(CircularVariance(group2))]);
0093 
0094 if h,
0095     message = '+++ The two groups have significantly different means/variances';
0096 else
0097     message = '--- The two groups do not have significantly different means/variances';
0098 end
0099 message = ['Watson U2 test: ' message ' (n1='  int2str(n1) ', n2=' int2str(n2) ', U2=' num2str(U2) ', critical=' num2str(value) ')'];
0100 disp(message);

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