% HeightAnalysis - Math 151
% Objective is analyze the data collected on heights 
% before and after sleeping for class participants
% including using histograms and linear regressions 
% to analyze the data
%
% It is assumed that the data have already been read 
% in data from a file to an array called
%   fulldat
%
% then this file is run by typing 
%   HeightAnalysis
% The data are assumed in the order for each 
% measurement (with spaces as column delimiters):
%  Gender (1=female,2=male), age in years, Height at night,
%    Hours of sleep, Height in morning, Height in morning
%    minus Height at night
%
who,pause
% Put all data into array a
%a=fulldat;
%
% Do histogram analysis
%
hist(a(:,1),3)
title('Gender: 1=Female, 2=Male'),pause
hist(a(:,2),20)
title('Age'),pause
hist(a(:,4))
title('Hours sleep'),pause
hist(a(:,5),40)
title('Height after sleep'),pause
hist(a(:,6),40)
title('Height change overnight'),pause
%
% Do basic statistics
%
agemean=mean(a(:,2));agemedian=median(a(:,2));
sleepmean=mean(a(:,4));sleepmedian=median(a(:,4));
heightmean=mean(a(:,5));heightmedian=median(a(:,5));
hchangemean=mean(a(:,6));hchangemedian=median(a(:,6));
agemean,agemedian,pause
sleepmean,sleepmedian,pause
heightmean,heightmedian,pause
hchangemean,hchangemedian,pause
%
% Now create data sets for females (f) and males (m)
% and a combined data set (c) by eliminating "bad" data
% 
err=input('throw out heightchanges of magnitude greater than: ')
%
len=length(a);
iif=0;iim=0;ic=0;
for i=1:len
  if ((a(i,6) <= err) & (a(i,6) >= -err))
     if a(i,1) == 1
         iif=iif+1;
         f(iif,:)=a(i,:);
        else
      end;
      if a(i,1) == 2 
         iim=iim+1;
         m(iim,:)=a(i,:);
        else
      end;
      ic=ic+1;
      c(ic,:)=a(i,:);
    else
   end;
end;
%
% Now make histograms by gender
%
hist(f(:,5),40)
title('Height after sleep females'),pause
figure
hist(m(:,5),40)
title('Height after sleep males'),pause
hist(f(:,6),40)
title('Height change overnight females'),pause
figure
hist(m(:,6),40)
title('Height change overnight males'),pause
%
% Now make scatter plots of various data sets
%
y=polyfit(f(:,4),f(:,6),1)
z=[min(f(:,4)) max(f(:,4))]
x=polyval(y,z)
plot(f(:,4),f(:,6),'+',z,x)
title('height change vs hours sleep - females'),pause
corrcoef(f(:,4),f(:,6)),pause
figure
y=polyfit(m(:,4),m(:,6),1)
z=[min(m(:,4)) max(m(:,4))]
x=polyval(y,z)
plot(m(:,4),m(:,6),'+',z,x)
title('height change vs hours sleep - males'),pause
corrcoef(m(:,4),m(:,6)),pause

y=polyfit(f(:,3),f(:,6),1)
z=[min(f(:,3)) max(f(:,3))]
x=polyval(y,z)
plot(f(:,3),f(:,6),'+',z,x)
title('height change vs height before sleep - females'),pause
corrcoef(f(:,3),f(:,6)),pause
figure
y=polyfit(m(:,3),m(:,6),1)
z=[min(m(:,3)) max(m(:,3))]
x=polyval(y,z)
plot(m(:,3),m(:,6),'+',z,x)
title('height change vs height before sleep - males'),pause
corrcoef(m(:,3),m(:,6)),pause

