📄 test2r.m
字号:
function [pval, r] = test2r(x,y,method)%TEST2R Test for equal location of two samples using rank test% % [pval, ranksum] = test2r(x,y)% % This is the Wilcoxon-Mann-Whitney test. It is two sided. % If you want a one sided alternative then devide pval by 2.% The pvalue is computed using exact distribution, not% normal approximation.%% The probability is exact and might take time to% compute unless a third argument 'n' is given, in % case a normal approximation is used for the distribution% of the ranksum.% See also TEST2N.% Anders Holtsberg, 18-11-93% Copyright (c) Anders Holtsbergif nargin < 3 method = 'exact';endx = x(:);y = y(:);n = length(x);m = length(y);[z, I] = sort([x;y]);% --- Check for tiesJ = find(diff(z)==0);if ~isempty(J) if any((I(J) <= n) ~= (I(J+1) <= n)) fprintf('\nWarning: ties in data.\n'); fprintf('Method used is to remove all tied pairs.\n'); fprintf('This is not a sophisticated method.\n'); xx = x; yy = y; for i = 1:length(xx) j = min(find(yy == xx(i))); if ~isempty(j) xx(i) = NaN; yy(j) = NaN; end end xx(find(isnan(xx))) = []; yy(find(isnan(yy))) = []; [pval, r] = test2r(xx,yy); return endendJ = find(diff(z)<100000*eps*max(z));if ~isempty(J) if any((I(J) <= n) ~= (I(J+1) <= n)) fprintf('\nWarning: Nearly tie!\n'); endend% --- Compute the ranks of the x'sR = 1:m+n;r = sum(R(find(I<=n)));r = r - n*(n+1)/2;r = min(r,m*n-r);if method(1) == 'e' mP = ceil((m*n+1)/2); P = zeros(mP,n+1); P(1,:) = P(1,:) + 1; for i=1:m for j = 1:n P(:,j+1) = (P(:,j+1)*i + [zeros(min(i,mP),1); P(1:mP-i,j)]*j)/(i+j); end end P = P(:,n+1); pval = min(2*sum(P(1:r+1)),1);else % see B W Lindgren page 521 Z = (r + 0.5 - m*n/2) / sqrt(m*n*(m+n+1)/12); pval = min(1, 2*pnorm(Z));end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -