MATLAB Answers

Is there any option to run a polyfit on a scatter plot?

조회 수: 9(최근 30일)
Wolfgang McCormack
Wolfgang McCormack 2021년 5월 28일
답변: Image Analyst 2021년 5월 29일
Hi all,
I have a scatter plot and there are some dots on that. Is there any option to get the X and Y of those points on the scatter plot? Furthermore, anyoption to run polyfit among those points directly on the scatter plot?
  댓글 수: 8
Star Strider
Star Strider 2021년 5월 29일
@Wolfgang McCormack I am doing my best to understand what the data are in the absence of the data themselves.
So they are actually something like this, then —
N = 25;
x = rand(1,N);
y = repmat(randn(6,1),1,N);
scatter(x, y, 'filled')
or this —
scatter(x, y(randi(6,1,N)), 'filled')

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


Cris LaPierre
Cris LaPierre 2021년 5월 29일
Polyfit is not going to return the X and Y values of those 6 points. It's going to return the polynomial coefficients for the equation that best fits the data. You then supply that equation with whatever X values you want to obtain the corresponding Y values. Using those, you can plot the fit line.
If you need the X and Y of the 6 groups, I'd suggest using something like kmeans clustering to identify 6 clusters and return their centroids first.
Use the centroids as inputs to polyfit.
Calculating and RMSE is fairly simple. You just need to do some math to calculate SSR and SST. See this answer.
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2021년 5월 29일
Share your data. It will be easier than trying to guess what is going on. Save your variables to a mat file and attach them to your post using the paperclip icon.

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

Image Analyst
Image Analyst 2021년 5월 29일
Chris suggests a nice trick. And attach your data like he says in his hidden comment (click link above to show it).
save('answers.mat', 'DataA', 'DataB');
Use the paperclip icon.
Another trick I've used when you have quantized data (multiple points with the same x value) is to add a very slight amount of noise to the x data. Add enough noise to make them unique and avoid the error polyfit throws, but not enough to change the formula it will find:
% Determine range of data.
minx = min(x)
maxx = max(x)
% Add a fraction of a percent of noise to x to make them unique.
xNoisy = x + 0.00001 * (maxx - minx);
% Determine the formula with the noisy x instead of the actual x.
% Below we will use a second order polynomial.
coefficients = polyfit(xNoisy, y, 2); % Fit a quadratic.
% Get estimated y from arbitrary x
estimatedY = coefficients(3) * thisX .^2 + coefficients(2) * thisX + coefficients(1);
Note that this will give a different formula than Chris's because this will consider how many points are in the cluster, so more points in a cluster will influence the line more, while Chris's uses the centroids of the clusters which ignores how many points are in the cluster. If you have about the same number of points in each cluster, it won't make much of a difference, but if some clusters have wildly different number of points than other clusters, then it could make a noticeable difference.




Community Treasure Hunt

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

Start Hunting!

Translated by