%   Quade Test
%   D. Quade (1979), Using weighted rankings in the analysis of complete blocks with additive block effects.
%   Journal of the American Statistical Association 74, 680–683.
% Conover (1999, p. 375f):
% 
 %  Example 1: Six professional drivers evaluated 3 cars (A,B,C).
 % rankings are 1-10
 
       data = [7  8  9;    6  10  7;   6  8  8; ...
                    7  9  8;    7  10  9;   8  8  9];
          [b,k]=size(data)
          rij=ranks(data',0)'
          q= repmat(ranks(range(data')'), 1,k)
          kpl1over2 = (k+1)/2 * ones(size(data))
         
          sij = q.*(rij - kpl1over2)
          % 
treat=repmat(1:k,b,1)
blocks=repmat((1:b)',1,k)
[p, atab] = anovan(sij(:), {treat(:), blocks(:)}, ...
                        'model',1, 'sstype',2, ...
                        'varnames',strvcat('Treatments', 'Blocks'))
 %%  
 % or simply:
quadetest([7  8  9;    6  10  7;   6  8  8; ...
              7  9  8;    7  10  9;   8  8  9])

 
%%
% Example 2. (from Conover: Practical NP Statistics, 3rd Ed. iley, page
% 375)
% Numbers of five brands of a new hand lotion sold in seven stores
% during one week.
data =  [ 5,  4,  7, 10, 12; ...
               1,  3,  1,  0,  2; ...
              16, 12, 22, 22, 35; ...
               5,  4,  3,  5,  4; ...
              10,  9,  7, 13, 10; ...
              19, 18, 28, 37, 58; ...
              10,  7,  6,  8,  7];
           
     
          [b,k]=size(data);
          rij=ranks(data',0)';
          q= repmat(ranks(range(data')'), 1,k);
          kpl1over2 = (k+1)/2 * ones(size(data));
          sij = q.*(rij - kpl1over2);
          % 
treat=repmat(1:k,b,1);
blocks=repmat((1:b)',1,k);
[p, atab stats] = anovan(sij(:), {treat(:), blocks(:)}, ...
                        'model',1, 'sstype',2, ...
                        'varnames',strvcat('Brand', 'Store'));
                 

% p =
% 
%     0.0152
%     1.0000
% 
% 
% atab = 
% 
%     'Source'    'Sum Sq.'       'd.f.'    'Singular?'    'Mean Sq.'    'F'         'Prob>F'
%     'Brand'     [  532.3571]    [   4]    [        0]    [133.0893]    [3.8293]    [0.0152]
%     'Store'     [         0]    [   6]    [        0]    [       0]    [     0]    [     1]
%     'Error'     [  834.1429]    [  24]    [        0]    [ 34.7560]          []          []
%     'Total'     [1.3665e+03]    [  34]    [        0]            []          []          []
%   
                    
                    
% Output from STATXACT
%   F(0.05, 4, 24) = 2.77629
% 
%   Test Statistic:
%   F = (7 - 1)(532.357143) / (1366.5 - 532.357143) = 3.829252
% 
%   P(>F) = 0.015189

                    
                    