% LMcs110203_4th.m
% Case Study 11.2.3
% Larsen & Marx (2006) Introduction to Mathematical Statistics. 4th Edition
% Page 654-655
% Written 11/23/10 by Eugene.Gallagher@umb.edu 2001, revised 2/18/2011
% Regression of finished weight of rods (y) vs. rough casting weight (x)
CAOPresent=[4 8 12.5 16 20 25 31 36 40 40]';
CAORecovered=[3.7 7.8 12.1 15.6 19.8 24.5 31.1 35.5 39.4 39.5]';
%
X=[ones(length(CAOPresent),1) CAOPresent];
Y=CAORecovered;
B=X\Y;
fprintf('\nThe Y instercept = %5.3f and slope = %5.3f\n',B(1),B(2));
Yest=X*B;
Resid=Y-Yest;
% sort by the independent variable so the lines are smooth
[Xs,k]=sort(X(:,2));
% sort the estimated values using the index vector k:
Ys=Yest(k);
chemist=[repmat('A',6,1);'B';repmat('A',3,1)];
i=find(chemist=='A');
j=find(chemist=='B');
plot(CAOPresent(i),Y(i),'ob',CAOPresent(j),Y(j),'sm',Xs,Ys,'-m');
xlabel('CaO Present'),ylabel('CaO Recovered')
figure(gcf)
pause
plot(CAOPresent(i),Resid(i),'ok',CAOPresent(j),Resid(j),'sm');
xlabel('CaO Present'),ylabel('Residual');
figure(gcf)
   x = CAOPresent(i);y=Resid(i);
       k = convhull(x,y);
       hold on, 
       plot(x(k), y(k), '-r'), axis([0 45 -0.2 0.6]);hold off
       title('Figure 11.2.6')