0001 function [stats,pVals] = compareTwoSamplesStat(sample1,sample2,tests)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
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