% Case study 1.2.4, p. 17 in
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written by Eugene.Gallagher 6/03, revised 3/17/11
IndexExp=[2.49
    2.57
    3.41
    1.25
    1.62
    3.83
    11.64
    6.41
    8.34];
Cancer=[147.1
    130.1
    129.9
    113.5
    137.5
    162.3
    207.5
    177.9
    210.3]
% Or from scratch
[r,c]=size(IndexExp); % find how many rows and columns;
% create a column vector of all 1's with the same
% number of rows as IndexExp. This is needed for the
% linear regression.
onex=ones(r,1)
% Create an X matrix with ones in the first column
% and the explanatory variable, IndexExp in the 2nd column;
X=[onex IndexExp]
% Do the leastsquares regression, solving for
% Y intercept B(1) and slope B(2))
B=X\Cancer
% Calculated the expected values
ExpY=X*B
% plot the data and expected values
plot(IndexExp,Cancer,'o',IndexExp,ExpY,'--r')
xlabel('Index of Exposure')
ylabel('Cancer deaths per 100,000 man years')
title('Figure 1.2.2')
figure(gcf)



