%%
clear all
close all force
%

 disp('Dialysis: Repeated Measures')
 lw = 3; 
 set(0, 'DefaultAxesFontSize', 16);
 fs = 16;
 msize = 10;
%
%Eight patients each underwent three different methods of kidney
%dialysis. The following values were
%obtained for weight change in kilograms between dialysis
%sessions (Daugridas and Ing, 1994);

weich=[  2.90  2.97  2.67 ;...
  2.56  2.45  2.62 ;...
  2.88  2.76  1.84 ;...
  1.73  1.20  1.33 ;...
  2.50  2.16  1.27 ;...
  3.18  2.89  2.39 ;...
  2.83  2.87  2.39 ;...
  1.92  2.01  1.66 ];...

subject=[1 2 3 4 5 6 7 8  1 2 3 4 5 6 7 8  1 2 3 4 5 6 7 8];
treatment = [1 1 1 1 1 1 1 1  2 2 2 2 2 2 2 2  3 3 3 3 3 3 3 3];
[p table stats terms] = anovan(weich(:),{subject, treatment},'random',1, ...
'model','linear','varnames',{'Subject' 'Treatment'} )
SST = table{5,2}; SSE = table{4,2}; SSA =table{3,2}; SSB  = table{2,2};
SSW = SST - SSB;

%Display bar a bar graph of anova table
H=figure;
set(H,'Position',[400 400 400 400]);
y=[0 0 1 1];
hold on
h1=fill([0 SST SST 0],y,'c');
y=y+1;
h2=fill([0 SSB SSB  0],y,'y');

h3=fill([0 SSW SSW 0]+SSB,y,'r');
y=y+1;
h4=fill([0 SSA SSA 0]+SSB,y,'g');
h5=fill([0 SSE SSE 0]+SSB+SSA,y,'b');
hold off
legend([h1 h2 h3 h4 h5],'SST','SSB','SSW','SSA','SSE','Location','NorthWest')
title('Sums of Squares')
%print -depsc 'C:\BESTAT\ANOVA\ANOVAeps\splitkidney.eps'
axis tight
clear S Xbar SStra GLtra SSentro GLentro SStot GLtot SSt GLt MSt SSr F
clear y h1 h2 h3 h4 h5 Table


%%
clear all
close all force
%Eight patients each underwent three different methods of kidney
%dialysis. The following values were
%obtained for weight change in kilograms between dialysis
%sessions (Daugridas and Ing, 1994);



weich=[  2.90  2.97  2.67 ;...
  2.56  2.45  2.62 ;...
  2.88  2.76  1.84 ;...
  1.73  1.20  1.33 ;...
  2.50  2.16  1.27 ;...
  3.18  2.89  2.39 ;...
  2.83  2.87  2.39 ;...
  1.92  2.01  1.66 ];...
  
Mausphercnst(weich)

Table{9,4}=[];   
Table(1,1:4)={'Subject' 'Treatment 1' 'Treatment 2' 'Treatment 3'};
Table(2:9,1)={'1' '2' '3' '4' '5' '6' '7' '8'};
Table(2:9,2:4)=num2cell(weich);
H=figure('Position',[360 80 644 213]);
statdisptable(Table, 'Data', 'Weight Chnge', 'bmed2400')
set(H,'Position',[600 50 644 213])
anovarep(weich)



%%
close all force

%Comparing Dialysis Treatments.
wchange = [...
2.90 2.97 2.67; 2.56 2.45 2.62;...
2.88 2.76 1.84; 1.73 1.20 1.33;...
2.50 2.16 1.27; 3.18 2.89 2.39;...
2.83 2.87 2.39; 1.92 2.01 1.66];
subject = [1 2 3 4 5 6 7 8 ...
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8]';
treatment = [1 1 1 1 1 1 1 1 ...
2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3]';

%(a) wrong solution // independent treatments
[p1 table1 stats1] = anova1(wchange(:), treatment)
% H0 not rejected! p1 =    0.1616
% 
% table1 = 
%     'Source'    'SS'        'df'    'MS'        'F'         'Prob>F'
%     'Groups'    [1.2510]    [ 2]    [0.6255]    [1.9901]    [0.1616]
%     'Error'     [6.6004]    [21]    [0.3143]          []          []
%     'Total'     [7.8515]    [23]          []          []          []
% 
% stats1 = 
%     gnames: {3x1 cell}
%          n: [8 8 8]
%     source: 'anova1'
%      means: [2.5625 2.4137 2.0213]
%         df: 21
%          s: 0.5606

%%
% (b) correct solution block design
names = {'subject','treatment'};
[p2 table2 stats2] = anovan(wchange(:),{subject, treatment},...
 'model','linear', 'varnames',names)    

% H0 rejected, treatments differ.
%  p2 =
% 
%     0.0001
%     0.0028
% 
% 
% table2 = 
% 
%   Columns 1 through 5
% 
%     'Source'       'Sum Sq.'    'd.f.'    'Singular?'    'Mean Sq.'
%     'subject'      [ 5.6531]    [   7]    [        0]    [  0.8076]
%     'treatment'    [ 1.2510]    [   2]    [        0]    [  0.6255]
%     'Error'        [ 0.9474]    [  14]    [        0]    [  0.0677]
%     'Total'        [ 7.8515]    [  23]    [        0]            []
% 
%   Columns 6 through 7
% 
%     'F'          'Prob>F'     
%     [11.9341]    [6.0748e-005]
%     [ 9.2436]    [     0.0028]
%            []               []
%            []               []
% 
% stats2 = 
%          source: 'anovan'
%           resid: [24x1 double]
%          coeffs: [12x1 double]
%             Rtr: [10x10 double]
%        rowbasis: [10x12 double]
%             dfe: 14
%             mse: 0.0677
%     nullproject: [12x10 double]
%           terms: [2x2 double]
%         nlevels: [2x1 double]
%      continuous: [0 0]
%          vmeans: [2x1 double]
%        termcols: [3x1 double]
%      coeffnames: {12x1 cell}
%            vars: [12x2 double]
%        varnames: {2x1 cell}
%        grpnames: {2x1 cell}
          
multcompare(stats2,'dimension',2)

