% LMex110304_4th.m
% Testing the equality of two slopes
% Using Larsen & Marx Theorem 11.3.9
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written by Eugene.Gallagher@umb.edu 11/27/10
X=[0:100:500]';
A=[100 250 304 403 446 482]';
B=[100 203 214 295 330 324]';
[rA,cA]=size(A);
BA=[ones(rA,1) X]\A;
Aest=[ones(rA,1) X]*BA;
BB=[ones(rA,1) X]\B;
Best=[ones(rA,1) X]*BB;
[rB,cB]=size(B);
plot(X,A,'s',X,B,'om',X,Aest,'g',...
    X,Best,'--m','LineWidth',2,...
                       'MarkerSize',12)
axis([-10 520 0 520]);
legend('Strain A: cross-bred', 'Strain B: inbred','Location','NorthWest')
xlabel('Day number','FontSize',16);
ylabel('Census count','FontSize',16)
title('Figure 11.3.5','FontSize',20)
figure(gcf)
pause
[T,pvalue,df,S,SE]=test2slopes(X,A,X,B);
CI95diffU=(0.742-0.452)+tinv(0.975,8)*S*sqrt(1/(2*(1.75e5)))
CI95diffU=(0.742-0.452)+tinv(0.975,8)*SE
CI95diffL=(0.742-0.452)-tinv(0.975,8)*S*sqrt(1/(2*(1.75e5)))
CI95diffL=(0.742-0.452)-tinv(0.975,8)*SE
half95CI=tinv(0.975,8)*S*sqrt(1/(2*(1.75e5)))
half95CI=tinv(0.975,8)*SE
fprintf('\nThe difference in slopes = %4.2g, with 95%%CI = [ %4.2g %4.2g ]\n',...
    BA(2)-BB(2),CI95diffL, CI95diffU)
fprintf('The pooled variance for the difference in slopes is %5.2f\n',S)
fprintf('Student''s t=%5.2f {%2.0f df} ',T,rA+rB-4)
fprintf('with 1-tailed p=%3.2g\n',pvalue/2)