% LMcs110202_4th.m
% Case Study 11.2.2
% Larsen & Marx (2006) Introduction to Mathematical Statistics. 4th Edition
% Written by Eugene.Gallagher@umb.edu 11/23/2010, revised 2/18/2011
% Regression of Social Security costs vs. year from 1965 to 1992
Years=[5:5:30 32]';
Y=[17.1 29.6 63.6 117.1 186.4 246.5 285.1]';
% There is a typo in the 3rd and 4th editions. It is 246.5, not 346.5
X=[ones(length(Years),1) Years];
B=X\Y;
fprintf('\nThe Y intercept = %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);
plot(Years,Y,'+g',Xs,Ys,'-m');
xlabel('Years After 1960'),ylabel('Cost')
figure(gcf)
pause
plot(Years,Resid,'ok');axis([0 40 -25 35])
xlabel('Years After 1960'),ylabel('Residual');
figure(gcf)