Improving the fitting of data
2 views (last 30 days)
Show older comments
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/208306/example.png)
0 Comments
Answers (2)
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
Image Analyst
on 14 Mar 2019
Gina, so now Kevin and I are saying the same thing:
- your data is your data and can't "move", and
- 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
- 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
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.
0 Comments
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!