How to plot correct best fit line?

309 Views Asked by At

I'm working on a software where I'm plotting graphs and finding best fit line. I have used Least-Square Method and linear regression technique with y = mx + c

My problem is that when most of the X values of graph are equal (not all) at that time best fit line is not proper but when there is good variation it seems correct.

Following pictures are for reference, Orange line is graph and Green is best fit (please don't consider other grey lines)

1. points with variation
2. most X values are equal

What is the problem here I'm not getting? please help.

1

There are 1 best solutions below

4
On BEST ANSWER

Try fitting $x=m y + c$ instead.

Alternatively, if your $x$ and $y$ axis have the same units, try minimizing the perpendicular offsets (i.e. the euclidean distance between the point and the line) as opposed to the vertical offsets (as in standard least-squares fitting).