Improving the fitting of data

2 views (last 30 days)
Gina Carts
Gina Carts on 13 Mar 2019
Commented: Image Analyst on 14 Mar 2019
I have a small group of people (n=15). For each person I have the age and a blood measurement. The relationship between the age and the blood measurement is linear (please look at the attached figure).
I make the assumption that my data can be described by the equation y= -ax+b (I want to keep it simple for now). I've been told that I can use Matlab to improve the fitting of these data (i.e. to bring the dots that look like outliers closer to the straight line). I have no idea how to do that though.
Any suggestion/help would be greatly appreciated.
example.png

Answers (2)

Kevin Phung
Kevin Phung on 13 Mar 2019
Edited: Kevin Phung on 13 Mar 2019
the polyfit and polyval functions will help you here:
also, just for the sake of principles, you should never try to fit your data to your model (bringing the dots closer to the line), but rather find a model that fits your data.
here's a quick example:
x = 1:10; %x data
y = [6 10 12 12 14 15 17 20 24 25]; %ydata
p = polyfit(x,y,1) % p returns the slope and y intercept (your a and your b)
y2 = polyval(p,x) %create a set of y values based on x, a, b.
figure
plot(x,b,'ro',x,y2,'b')
if a linear model doesnt work, you can change the degree of the polynomial with the 3rd argument of polyfit.
  3 Comments
Kevin Phung
Kevin Phung on 13 Mar 2019
oops, i meant to put
plot(x,y,'ro',x,y2,'b')
Image Analyst
Image Analyst on 14 Mar 2019
Gina, so now Kevin and I are saying the same thing:
  1. your data is your data and can't "move", and
  2. the linear fit that MATLAB gives you, and we showed you how to use, is already the best fit you can get for a line, and
  3. if you need a better fit you need to try a higher order polynomial.
So, what are you going to do? What I'd do is what I suggested in my answer, and that is to collect very many more data points before deciding on a model.

Sign in to comment.


Image Analyst
Image Analyst on 13 Mar 2019
I don't think the fit can be improved unless you assume a different model (like higher order polynomial or whatever). That looks like a linear fit like what you'd get from
coefficients = polyfit(x, y, 1)
a = -coefficients(1);
b = coefficients(2);
and the fit is what it is. You can't make the line get any closer to the points since the line it gives you is already the closest overall to the points that is possible.
Now if you want to go to a higher order, you could do
coefficients = polyfit(x, y, 3)
fittedY = polyval(coefficients, x)
You can see that it's no more complicated than the linear fit you wanted to "keep it simple". So just pick the best one. But you can't pick the best model on only 15 sample points. I'd get data from a few hundred patients/subjects and then look at the scatterplot and see what you think the best model would be.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!