Improving the fitting of data

조회 수: 2 (최근 30일)
Gina Carts
Gina Carts 2019년 3월 13일
댓글: Image Analyst 2019년 3월 14일
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

답변 (2개)

Kevin Phung
Kevin Phung 2019년 3월 13일
편집: Kevin Phung 2019년 3월 13일
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
Kevin Phung
Kevin Phung 2019년 3월 13일
oops, i meant to put
plot(x,y,'ro',x,y2,'b')
Image Analyst
Image Analyst 2019년 3월 14일
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.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2019년 3월 13일
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!

Translated by