MATLAB: Calculate the distance between a linear regression line and the data points

distancelinear regression

Hello,
I would like to calculate the distance between a linear regression line and different data points.
This is my figure:
And this is my code for the linear regression:
x = age_a;
y = par_a;
m = age_b;
n = par_b;
s = polyfit(x,y,1);
f = polyval(s,x);
plot(x,y,'ob',x,f,'-',m,n,'og')
Is that correct so far?
Two questions/problems:
  1. How can I calculate the distance between the linear regression line and different data points?
  2. the regression line x-intervall should be big enough to calculate the distance of both data-groups (a and b) and not just of group a. (There are some green dots to the right and left of the line)
Thank you for the help!!!

Best Answer

If you have the x-values of all the data points of interest you can calculate the corresponding y-values of your line simply using, say
y = polyval(s,x);
where the x now represents the value of a green or blue point as required.
Then calculate
abs(y - ytrue)
for each point and sum the result over all the points. (ytrue is the actual y value of the point in question; y is the linear curve value).
Alternatively if you want a least squares distance you could use
sqrt((y-ytrue)^2 )
instead of the absolute value expression.