compareTwoSamplesStat

PURPOSE ^

compareTwoSamplesStat Compare statistically the difference between two

SYNOPSIS ^

function [stats,pVals] = compareTwoSamplesStat(sample1,sample2,tests)

DESCRIPTION ^

compareTwoSamplesStat Compare statistically the difference between two
samples

 [stats,pVals] = compareTwoSamplesStat(sample1,sample2,tests)

 Does the Kolmogorov-Smirnov, rank-sum, chi-square, and T-tests

INPUTS
 sample1, sample2          Samples to compare
 tests                     {'test1', 'test2',...} (Default = all tests)
   Test Name                   Input
   Kolmogorov-Smirnov test     'ks'
   rank-sum test               'rankSum'
   chi-squre test              'chiSquare'
   T-test                      'tTest

OUTPUTS
 Output will be in order that tests are inputed. i.e. {'ks','rankSum'}
 stats                     stats
 pVals                     p values

 Markus Herrgard 8/14/06

 Combined test m-files into this m-file. Richard Que 11/20/09.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [stats,pVals] = compareTwoSamplesStat(sample1,sample2,tests)
0002 %compareTwoSamplesStat Compare statistically the difference between two
0003 %samples
0004 %
0005 % [stats,pVals] = compareTwoSamplesStat(sample1,sample2,tests)
0006 %
0007 % Does the Kolmogorov-Smirnov, rank-sum, chi-square, and T-tests
0008 %
0009 %INPUTS
0010 % sample1, sample2          Samples to compare
0011 % tests                     {'test1', 'test2',...} (Default = all tests)
0012 %   Test Name                   Input
0013 %   Kolmogorov-Smirnov test     'ks'
0014 %   rank-sum test               'rankSum'
0015 %   chi-squre test              'chiSquare'
0016 %   T-test                      'tTest
0017 %
0018 %OUTPUTS
0019 % Output will be in order that tests are inputed. i.e. {'ks','rankSum'}
0020 % stats                     stats
0021 % pVals                     p values
0022 %
0023 % Markus Herrgard 8/14/06
0024 %
0025 % Combined test m-files into this m-file. Richard Que 11/20/09.
0026 
0027 %Determine which tests to run
0028 
0029 if nargin<3
0030     tests = {'ks', 'rankSum', 'chiSquare', 'tTest'};
0031 end
0032 
0033 stats = [];
0034 pVals = [];
0035 [nVar,nSample] = size(sample1);
0036 
0037 for i=1:length(tests)
0038     switch lower(tests{i})
0039         case 'ks'
0040             for j = 1:nVar
0041                 [h,pLarger,statLarger] = kstest2(sample1(j,:),sample2(j,:),0.01,'larger');
0042                 [h,pSmaller,statSmaller] = kstest2(sample1(j,:),sample2(j,:),0.01,'smaller');
0043                 if (statLarger > statSmaller)
0044                     stat(j,1) = statLarger;
0045                     p(j,1) = pLarger;
0046                 else
0047                     stat(j,1) = -statSmaller;
0048                     p(j,1) = pSmaller;
0049                 end
0050             end
0051         case 'ranksum'
0052             for j = 1:nVar
0053                 [p(j,1),h,stats] = ranksum(sample1(j,:),sample2(j,:),'method','approximate');
0054                 stat(j,1) = -stats.zval;
0055             end
0056         case 'chisquare'
0057             warning off MATLAB:divideByZero
0058             nBin = round(nSample/50);
0059             for j = 1:nVar
0060                 counts = hist([sample1(j,:)' sample2(j,:)'],nBin)/nSample;
0061                 tmpStat = (counts(:,1)-counts(:,2)).^2./(counts(:,1)+counts(:,2));
0062                 tmpStat(isnan(tmpStat)) = 0;
0063                 stat(j,1) = sum(tmpStat);
0064             end
0065             p = chi2cdf(stat,nBin-1);
0066             warning on MATLAB:divideByZero
0067         case 'ttest'
0068             for j = 1:nVar
0069                 [h,p(j,1),tmp,tmpstats] = ttest2(sample1(j,:),sample2(j,:));
0070                 stat(j,1) = tmpstats.tstat;
0071             end
0072         otherwise
0073             fprintf('%s is not a valid option',tests{i});
0074     end
0075     stats = [stats stat];
0076     pVals = [pVals p];
0077 end

Generated on Thu 21-Jun-2012 15:39:23 by m2html © 2003