% LMex110208_4th.m
% Using data from 8.2.11
% Use Case Study 11.2.3 as a model
DATA=[0.9 1.8
    0.76 1.36
    1.67 2.92
    1.44 2.61
    0.20    0.42
    0.16    0.49
    1.12    1.90
    1.04    2.38
    0.48    1.24
    1.33    2.80
    1.10    2.41
    1.56    2.80
    1.15    2.16];
[r,c]=size(DATA);
X=[ones(r,1) DATA(:,1)];
Y=DATA(:,2);
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);
plot(DATA(:,1),DATA(:,2),'sm',Xs,Ys,'-m');
xlabel('Plant Cover Diversity'),ylabel('Bird Species Diversity')
figure(gcf)
pause
plot(Yest,Resid,'ok');
xlabel('Predicted value'),ylabel('Residual');
figure(gcf)