function  [S, F, pS, pF] = friedmantest( data  )
% ------------------------------------------------------------------------------------
%   FRIEDMAN TEST
%   Input:  data - data matrix in which rows are blocks and columns are treatments. 
%           
%   Output:
%           S - standardized R but adjusted for the ties
%           pS - p-value for testing equality of distributions (equality of locations)
%                against the alternative spacified by input alt
%           F - (b-1)S/(b(k-1)-S), better approximation.
%           pF - p-value frpm the statistics F
%                      
%   Comes with M-function ranks.m (ranking procedure)
%-------------------------------------------------------------------------------------
[b, k] = size(data);
ranksj = sum(ranks(data',0)');
a = sum(sum( ranks(data',0).^2 ));
c = b*k*(k+1)^2/4;
%--------------------------------------------------------------------
S = (k-1)* ( sum( ranksj.^2 ) - b*c )/(a -c);
F = (b-1)*S/(b*(k-1)-S);
%---------------p-values for S and F---------------------------------
pS= 1 - chi2cdf( S, k-1);
pF= 1 - fcdf( F,  k-1, (b-1)*(k-1));
%-------------------------------------------------------------------------------------
% Copyright (C) 2002 by BV
% Ver 0.1  10/10/2002
% Ver 0.2   9/18/2003
%--------------------------------------------------------------------
function  r = ranks(data, glob)
%--------------------------------------------------------------------
% RANK TRANSFORMATION of data on input
%    Input: data - data to be ranked, data is matrix
%           glob - how to rank data matrix. glob=1 ranks amm matrix components (global 
%                  ranking; glob ne 1 - rank by columns of data. Default glob=1.
%    Output:   r - rank-transformed data
% Finds ranks of a vector of observations and adjusts for ties (by averaging)
% 
% Comment. If ties are no present (ties in ranking are matching observations), 
% ranks can be found simply as the indices in sorting of the indices of the
% sorted data (sentence is OK), i.e.,  [a,b]=sort(data); [c,d]=sort(b);
% and the ranks are in 'd'. If there are ties, they are ranked from left to
% right and also from right to left, and then averaged!
%---------------------------------------------------------------------
if nargin < 2
    glob = 1;
end
    shape = size(data);
if glob == 1    
     data=data(:);
 end     
% Ties ranked from UptoDown
  [ irrelevant , indud ]  =  sort(data);
  [ irrelevant , rUD ]    =  sort(indud);
% Ties ranked from RtoL
  [ irrelevant , inddu ]  =  sort(flipud(data));
  [ irrelevant , rDU ]    =  sort(inddu);
% Averages ranks of ties, keeping ranks of no-tie-observations the same
r = (rUD + flipud(rDU))./2;
r = reshape(r,shape); 
%---------------------------------------------------------------------
% Copyright (C) 2002 by GaTech
% Ver 0.1   9/28/2002


