% LMex070402_4th.m
% Case 7.4.2 p. 490
% Example of one-sample t test in
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written by Eugene.Gallagher@umb.edu October 2010, revised 1/21/11
% http://alpha.es.umb.edu/faculty/edg/files/edgwebp.html
% Theorem 7.4.2: T=(Avg-mu_o)/(s/sqrt(n))
% The p value can for T can be determined
% using the wording indicating that a 1-tailed
% test is required: "Can it be concluded
% that children at this particular facility
% have a HIGHER risk of pica?
alpha=0.05;M=16
[D,t,df,pvalue,CI]=student1group(12,18.65, 5.049,M, alpha);
fprintf('The 1-tailed p value = %6.4g for t=%6.4f\n',pvalue/2,t);
fprintf(...
 'The blood lead difference of %4.2f mg/l had 95%% CI =[%4.2f %4.2f]\n',...
    D,CI(1),CI(2));
if pvalue/2<alpha
    fprintf(...
'Reject Ho: Evidence that children had lead levels higher than %4.2f\n',M);
fprintf(...
'The difference from ''normal (=%2.0f mg/l)'' was %3.1f mg/l +/- %3.1f mg/l\n',...
M,D,D-CI(1));
end
% plot Figure 7.4.4 using Figure 7.4.2 as a paradigm
clf
muj=0;
sigmaj=1; % sigmaj is the standard deviation;
df=11;
y=linspace(-4.2,4.2,100);
fyj=tpdf(y,df);
r95=tinv(0.95,df);
% Plot using ax1 handle, saved above,to save this graph
% on top of the previous graph.
h1=line(y,fyj,'Color','r','Linewidth',3);
set(h1,'linestyle','--','color','r','linewidth',3)
axis([-4 4 0 0.41]);
ax1=gca;
set(ax1,'Xtick',[0 r95],'XColor','k','FontSize',18);
set(ax1,'Ytick',0:.2:.4,'FontSize',18);
title('Figure 7.4.2','Fontsize',22);
hold on
% add labels
labs=['f_T_3(t)  '];
text(r95,0.1,'Area=0.05','Color','r','Fontsize',18)
text(0.2,0.39,'f_T_1_1(t)','Fontsize',18)
xf=r95:0.01:xmax;
yf=tpdf(xf,df);
tf=tpdf(t,df);
xtf=t:0.01:xmax;
ytf=tpdf(xtf,df);
fill([r95 xf xmax]',[0 yf 0]',[.8 .8 1],'FaceAlpha',0.5)
fill([t xtf xmax]',[0 ytf 0]',[1 .8 .8],'FaceAlpha',0.5)
s=sprintf('Area=%5.3f',pvalue/2);
text(t+.15,0.075,s,'Color','b','Fontsize',18)
figure(gcf);pause
hold off;
