% LMcs090201_4th.m
% Case Study 9.2.1 p. 557 in
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Examples of two sample tests
% Written by Eugene.Gallagher@umb.edu in 2001, revised 1/21/11, 2/27/11
% http://alpha.es.umb.edu/faculty/edg/files/edgwebp.html
Twain=[.225 .262 .217 .240 .230 .229 .235 .217]';
Snodgrass=[.209 .205 .196 .210 .202 .207 .224 .223 .220 .201]';
% boxplot
[T,C]=size(Twain);[S,CS]=size(Snodgrass);
DATA=[Twain;Snodgrass];
G=[ones(T,1);zeros(S,1)];
boxplot(DATA,G,'labels',{'Twain','Snodgrass'})
figure(gcf),pause
fprintf('The difference in means (Twain - Snodgrass) was %6.4f\n',...
    mean(Twain)-mean(Snodgrass));
[H,P,CI,STATS]  = ttest2(Twain,Snodgrass,0.05,'both','equal');
fprintf(...
    '\nThe 2-tailed p for Student''s t (=%4.2f, %d df) =%6.4f\n',...
    STATS.tstat,STATS.df,P);
fprintf('The pooled sd for the difference is %6.4f\n',STATS.sd)
fprintf(...
   'The lower and upper CI for the observed difference=%6.3f: [%6.3f %6.3f]\n',...
    mean(Twain)-mean(Snodgrass),CI(1),CI(2));
fprintf('\nThe unequal variance, or Welch t test\n');
[H,P,CI,STATS]  = ttest2(Twain,Snodgrass,0.05,'both','unequal');
fprintf(...
    '\nThe 2-tailed p for Welch''s t (=%4.2f, %5.2f df) =%6.4f\n',...
    STATS.tstat,STATS.df,P);
fprintf('The separate sd''s for the samples were %6.4f and %6.4f\n',STATS.sd)
fprintf(...
    'The lower and upper CI for the observed difference=%6.3f: [%6.3f %6.3f]\n',...
    mean(Twain)-mean(Snodgrass),CI(1),CI(2));
% Plot Figure 9..2.1 using Figure 4.3.6 (LMex040307_4th.m) as a model.
df=16;
X=-4.25:0.01:4.25;
Y = tpdf(X,df);
plot(X,Y,'--r');
axis([-4.25 4.25 0 0.4]);title('Figure 9.2.1','FontSize',22);
ax1=gca;
xlabel('Student''s t','FontSize',20),
ylabel('f_y(y)','FontSize',20);
ax1=gca;
set(ax1,'xtick',[-3.88 -2.9208 0 2.9208 3.88],'FontSize',18);
set(ax1,'ytick',0:.1:0.4,'FontSize',18)
hold on;
xfu=2.9208:0.01:4.25;yfu=tpdf(xfu,df);
fill([2.9208 xfu 4.25]',[0 yfu 0]',[.8 .8 1])
xfl=-4.25:0.005:-2.9208;yfl=tpdf(xfl,df);
fill([-4.25 xfl -2.9208]',[0 yfl 0]',[.8 .8 1])
text(-3.9,0.03, 'Area=0.005','FontSize',18);
text(2.9208,0.03,'Area=0.005','FontSize',18)
figure(gcf);pause
hold off;