%LMcs130201_4th.m
% Larsen & Marx (2006) Introduction to Mathematical Statistics 4th edition
% Case Study 13.2.1 Page 778-779
% Written by Eugene.Gallagher@umb.edu 12/3/2010; revised 2/15/2011
% Calls Trujillo-Ortiz et al. adTukeyAOV2.m from Matlab file central
% Tukey's test for additivity.
DATA=[8 2 -2
11 1 0
9 12 6
16 11 2
24 19 11];
boxplot(DATA,'sym','r*','labels',...
{'Contact Desensitization','Demonstration','Live Modeling'});
figure(gcf);pause
plot(DATA');
ax1=gca;
set(ax1,'xtick',[1 2 3]);legend('A','B','C','D','E');
xlabel('Therapy');ylabel('HAT Score');
figure(gcf);pause
pause % Needed so can see the box plots
sumDATA=sum(DATA);
meanDATA=mean(DATA);
% Since the design is balanced, either anova2 or anovan can be used.
[p,table,stats]=anova2(DATA,1)
% This will produce the ANOVA table as a figure. The results are
% printed out in table. stats could be sued for multcompare
pause
% The data can also be analyzed using anovan, producing identical results.
% anovan allows labeling of the ANOVA table.
y=DATA(:);
% convert the data into columns; drop the NaN elements, if any
g1=repmat(['A';'B';'C';'D';'E'],3,1);
g2=[repmat('CD',5,1);repmat('DP',5,1);repmat('LM',5,1)];
i=~isnan(y);y=y(i);g1=g1(i,:);g2=g2(i,:);
[p,table,stats] = anovan(y,{g1 g2},'model','linear',...
    'varnames',{'Blocks';'Therapies'});

% This is Example 13.2.1, comparing treatments
[c,m,h] = multcompare(stats,'ctype','tukey-kramer','display','on',...
    'dimension',2)
fprintf('Pairwise Difference \tLower 95\tEstimate\tUpper 95\n');
fprintf('\t\t%1.0f - %1.0f\t\t\t   %4.1f\t\t%4.1f\t\t%4.1f\n',c')
figure(h);
title(' '),xlabel(' ');xlabel('HAT score');title('Case Study 13.2.1'),
figure(gcf)
pause;

% Since the treatments were not replicated within blocks, a test
% for additivity should be run. If replicates were available a formal
% block by interaction test could have been run. 
r1=repmat([1:5]',3,1);
r2=[repmat(1,5,1);repmat(2,5,1);repmat(3,5,1)];
X=[y r1 r2];
adTukeyAOV2(X,2,0.05)
