%CBFCMRO2: Cerebral Blood Flow and Metabolic Rate of Oxygen. In the
% human brain, structures having high blood flow (e.g. grey matter) also tend
% to have increased metabolic rates, and structures containing low blood
% flow (e.g. white matter) have lower metabolic rates. This phenomenon is
% known as coupling, and it can be modeled by measuring cerebral blood
% flow (CBF) and comparing it with the variable cerebral metabolic rate of
% oxygen (CMRO2 ). This technique has been used to show how various brain
% regions differ in their metabolism characteristics and has been used as a
% fundamental basis for fMRI imaging. Fox and Raichle (1986) tested whether
% the measurements of CBF and CMRO2 were dynamically coupled in the
% measurements they collected.
%  
% Three sets of stimulated-state measurements and one set of resting state
% measurements were acquired for each of the 9 subjects undergoing sensory
% stimulation. The 3 stimulation levels (S1, S2, S3) differed only in stimulus
% duration. The measured response to tactile stimulation for each subject was
% the regional blood flow ratio (contralateral/ipsilateral). Data are provided
% in the "cfb" matrix below.
% Conduct the repeated measures analysis in a non-parametric
% fashion and test the hypothesis that population distributions of regional
% blood flow ratios are the same. If the null is rejected, conduct multiple
% comparisons.
  
  
  clear all
 cbf=[...
0.82	1.14	1.05	1.12;
1.05	1.45	1.38	1.42;
1.10	 1.45	1.49	1.58;
0.90	1.05	1.22	1.14;
1.02	1.27	1.27	1.39;
1.03	1.25	1.23	1.2;
1.04	1.30	1.33	1.29;
0.98	1.30	1.42	1.34;
1.04	1.29	1.23	1.24];

  %or cbf=xlsread ('cbfcmro2.xls');
  [S F pS pF] = friedmantest(cbf)
%   S =16.4157
%   F =12.4076
%   pS =9.3179e-004
%   pF =4.2382e-005


out=friedmanpairwise(cbf) 

%     1.0000    2.0000  -25.9653  -18.5000  -11.0347  
%     1.0000    3.0000  -24.9653  -17.5000  -10.0347
%     1.0000    4.0000  -25.4653  -18.0000  -10.5347
%     2.0000    3.0000    -6.4653    1.0000    8.4653
%     2.0000    4.0000    -6.9653    0.5000    7.9653
%     3.0000    4.0000    -7.9653   -0.5000    6.9653

% Note that 0 is not in 1-2, 1-3, and 1-4 intervals, so 1 is different from
% 2,3 4. On the other hand, 2,3 and 4 are not different


%% BUILT-IN MATLAB FUNCTIONS
 %cbf=xlsread ('cbfcmro2.xls');
  
cbf=[...
0.82	1.14	1.05	1.12;
1.05	1.45	1.38	1.42;
1.1	     1.45	 1.49	 1.58;
0.9	     1.05	 1.22	 1.14;
1.02	1.27	1.27	1.39;
1.03	1.25	1.23	1.2;
1.04	1.3    	 1.33	 1.29;
0.98	1.3	     1.42	 1.34;
1.04	1.29	1.23	1.24];

  
  
  [a b sta] = friedman(cbf) 
  %a is pvalue, b is table, and sta are stats for multcompare
  multcompare(sta)
   
  
  %9.3179e-004


%     'Source'     'SS'         'df'    'MS'        'Chi-sq' 
%     'Columns'    [27.0556]    [ 3]    [9.0185]    [16.4157]
%     'Error'      [17.4444]    [24]    [0.7269]           []
%     'Total'      [44.5000]    [35]          []           []
%  
%     'Prob>Chi-sq'
%     [9.3179e-004]
%                []
%                []


% sta = 
% 
%        source: 'friedman'
%             n: 9
%     meanranks: [1 3.0556 2.9444 3]
%         sigma: 1.2838

%     1.0000    2.0000   -3.6103   -2.0556   -0.5008
%     1.0000    3.0000   -3.4992   -1.9444   -0.3897
%     1.0000    4.0000   -3.5548   -2.0000   -0.4452
%     2.0000    3.0000   -1.4436    0.1111    1.6659
%     2.0000    4.0000   -1.4992    0.0556    1.6103
%     3.0000    4.0000   -1.6103   -0.0556    1.4992


  
% imandavenport
         [b,k]=size(cbf);
          rij=ranks(cbf',0)';
treat=repmat(1:k,b,1);
blocks=repmat((1:b)',1,k);
[p, atab stats] = anovan(rij(:), {treat(:), blocks(:)}, ...
                        'model',1, 'sstype',2, ...
                        'varnames',strvcat('Rows', 'Columns')) 
                    
              p =

    0.0000
    1.0000


atab = 

    'Source'     'Sum Sq.'    'd.f.'    'Singular?'    'Mean Sq.'    'F'          'Prob>F'    
    'Rows'       [27.0556]    [   3]    [        0]    [  9.0185]    [12.4076]    [4.2382e-05]
    'Columns'    [      0]    [   8]    [        0]    [       0]    [      0]    [         1]
    'Error'      [17.4444]    [  24]    [        0]    [  0.7269]           []              []
    'Total'      [44.5000]    [  35]    [        0]            []           []              []


      
