%regression analysis 1
clear;clc
data=dlmread('education_income.dat','',3,0);
income=data(:,2);
edu=data(:,3);

%make the linear regression
x=edu
y=income
p=polyfit(x,y,1)  %linear fit using polynomial method
%the fitted formula: y=p(1)*x + P(2)

%plot the line
x_fit=[12:22]   %define the x value of the fitted line
y_fit=p(1)*x_fit+p(2)  %predicted the Y-value using fitted function
plot(x_fit,y_fit,'-')

%obtain the correlation coefficient and annotate it
R=corrcoef(x,y)
text(11,110000,'R=0.91','Fontsize',20)

%make scattering plot
hold all
plot(x,y,'*b')
xlim([10,22])
ylim([20000,130000])
xlabel('Education (year)','Fontsize',14)
ylabel('Income($)','Fontsize',14)
title('Education-Income Relation','FontSize',18)
legend('Fitted Line','Scattering','Location','northwest')


