% LMcs120401_4th.m
% Larsen & Marx (2006) Introduction to Mathematical Statistics 4th edition
% Case Study 12.4.1 Page 755-756
% Are there differences in infant walking times based on exercise?
% Written by Eugene.Gallagher@umb.edu 12/3/2010, revised 2/15/11
% There are at least 2 ways to analyze these data with Matlab & ANOVA
% Note that I'm using 'not a number' to balance the matrix.
DATA=[9 11 11.5 13.25
    9.5 10 12 11.5
    9.75 10 9 12
    10 11.75 11.5 13.5
    13 10.5 13.25 11.5
    9.5 15 13 NaN]
sumDATA=sum(DATA);
meanDATA=mean(DATA);
[p,table,stats] = anova1(DATA);
pause
multcompare(stats,'ctype','hsd');
% ANOVA1 can only be used for balanced data,i.e., data with an equal number
% of cases per treatment level
% ANOVAN is the more
% general approach, but the data have to be restructured so that they are
% all in one column.
pause
% ANOVA1 can only be used for balanced data,i.e., data with an equal number
% of cases per treatment level
% ANOVAN is the more
% general approach, but the data have to be restructured so that they are
% all in one column.
pause
y=DATA(:); % convert the data into columns; drop the NaN elements
group=repmat(1:4,6,1);group=group(:);i=~isnan(y);y=y(i);group=group(i);
[p,table,stats] = anovan(y,group,'varnames','Exercise');
multcompare(stats,'ctype','hsd');
% Levene test downloaded from Matlab Central
Levenetest([y group],0.05);
% Program the linear contrast from Definition 12.4.2 (confirmed with
% PASW oneway and UNIANOVA)
% By using fractional coefficients, the difference in means for the 
% contrasts will be of the right size.
LMatrix = [1	-1/3	-1/3	-1/3
		  1	-1	  0	 0
		1/2	 1/2 -1/2	-1/2  
		0	 0	 1	-1]
% Call's Gallagher's anova linear contrast function
anovalc(LMatrix, y, group, stats);
% Because of the two outliers, use a Kruskal-Wallis ANOVA to check results
[P,ANOVATAB,STATS] = kruskalwallis(y,group)
multcompare(STATS) 
