% LMcs110205_4th.m
% Larsen & Marx Case Study 11.2.5, an example of logarithmic regression
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written by Eugene.Gallagher@umb.edu; written 11/23/10; revised 2/19/11
DATA=[360 90;165 105;21 21;23 26;11 14;18 28;18 21;150 105;45 68;45 75;18 46];
[r,c]=size(DATA);
X=[ones(r,1) log10(DATA(:,1))];Y=log10(DATA(:,2));
B=X\Y;
fprintf('\nThe Y intercept = %8.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(X(:,2),Y,'o',Xs,Ys,'-m','LineWidth',2,...
                       'MarkerEdgeColor','k',...
                       'MarkerFaceColor','b',...
                       'MarkerSize',10)
xlabel('Log_{10}(Locomotion begins (days))','FontSize',20),
ylabel('Log_{10}(Play begins (days))','FontSize',20)
title('Figure 11.2.9a','FontSize',22)
figure(gcf)
pause
% Generated a fine-scale estimated curve
xintrp=log10([10:0.1:370]');
[r,c]=size(xintrp);
xintrp=[ones(r,1) xintrp];
yestintrp=xintrp*B;
s=sprintf('y=%4.2f x^{%4.2f}',10^B(1),B(2))
plot(DATA(:,1),DATA(:,2),'o',10.^(xintrp(:,2)),10.^(yestintrp),'--r',...
    'LineWidth',2,...
                       'MarkerEdgeColor','k',...
                       'MarkerFaceColor','b',...
                       'MarkerSize',8)
axis([0 425 0 160])
xlabel('Locomotion begins (days)','FontSize',20),
ylabel('Play begins (days)','FontSize',20)
title('Figure 11.2.9','FontSize',22)
text(200,140,s,'FontSize',20)
figure(gcf)
pause
loglog(log10(DATA(:,1)),log10(DATA(:,2)),'o',...
    xintrp(:,2),yestintrp,'--r',...
    'LineWidth',2,...
                       'MarkerEdgeColor','k',...
                       'MarkerFaceColor','b',...
                       'MarkerSize',8)
axis(log10([10 150 10 400]))
xlabel('Locomotion begins (days)','FontSize',20),
ylabel('Play begins (days)','FontSize',20)
title('Figure 11.2.9b','FontSize',22)
text(200,140,s,'FontSize',20)
figure(gcf)
pause
plot(Yest,Resid,'ok');
xlabel('Expected value'),ylabel('Residual');
grid;figure(gcf)