
%LinearRegressionOrder - Math 151
%Objective is to illustrate difference in 
%linear regression when graphing x against y
%rather than y against x. There are two data
% sets here - d3 versus d4 and e3 versus e4.
%First read in data from a file
%   load correlat.mat
%then read in this file of commands for MATLAB
who,pause
plot(d3,d4,'+')
title('d3 versus d4'),pause
c=polyfit(d3,d4,1),pause
m=[min(d3) max(d3)],pause
x=polyval(c,m),pause
plot(d3,d4,'+',m,x),pause
corrcoef(d3,d4),pause
plot(d4,d3,'+')
title('d4 versus d3'),pause
c2=polyfit(d4,d3,1),pause
m2=[min(d4) max(d4)],pause
x2=polyval(c2,m2),pause
plot(d4,d3,'+',m2,x2)
title('d3 versus d4'),pause
corrcoef(d4,d3),pause                     
%Now invert the line from the d4 versus d3
%fit to plot both linear regressions on same
%graph
c2in=[1./c2(1)  -c2(2)/c2(1)],pause
x2=polyval(c2in,m),pause
plot(d3,d4,'+',m,x,m,x2)
title('both regression lines'),pause
% Now repeat all of above for e3 and e4 data set
plot(e3,e4,'+')
title('e3 versus e4'),pause
c=polyfit(e3,e4,1),pause
m=[min(e3) max(e3)],pause
x=polyval(c,m),pause
plot(e3,e4,'+',m,x),pause
corrcoef(e3,e4),pause
plot(e4,e3,'+')
title('e4 versus e3'),pause
c2=polyfit(e4,e3,1),pause
m3=[min(e4) max(e4)],pause
x3=polyval(c2,m3),pause
plot(e4,e3,'+',m3,x3),pause
corrcoef(e4,e3),pause
%Now invert the regression line
c2in=[1./c2(1)  -c2(2)/c2(1)],pause
x2=polyval(c2in,m),pause
plot(e3,e4,'+',m,x,m,x2)
title('both regression lines'),pause
