3

I am trying to get the best fit for an unknown set of 2D points. The points are centered points of rivers, and they don't come in a certain order. I've tried to use polynomial regression but I don't know what is the best polynomial order for different sets of data.

I've also tried cubic spline, but I don't want a line through all the points I have, I want an approximation of the best fit line through the points.

I would like to get something like this enter image description here even for lines that have more curves. This example is computed with polynomial regression, and it works fine.

Is there a way to do some smooth or regression algorithm that can get the best fit line even for a set of points like the following? points

    PolynomialRegression<double> pol;
    static int polynomOrder = <whateverPolynomOrderFitsBetter>;
    double error = 0.005f;
    std::vector<double> coeffs;
    pol.fitIt(x, y, polynomOrder, coeffs);
    // get fitted values
    for(std::size_t i = 0; i < points.size(); i++)
    {
        int order = polynomOrder;
        long double yFitted = 0;
        while(order >= 0)
        {
            yFitted += (coeffs[order] * pow(points[i].x, order) + error);
            order --;
        }
        points[i].y = yFitted;
    }

In my implementation with an 35 polynomial order this is all I can get check here to see an example, and also changing the polynomial order with higher values turns into Nan values for the coefficients.

I'm not sure if this is the best approach I can have.

Monica
  • 31
  • 4
  • are you looking for linear regression? – 463035818_is_not_an_ai Dec 19 '20 at 13:49
  • Nope, non-linear – Monica Dec 19 '20 at 14:01
  • what is non-linear? The "linear" in linear regression refers to the resulting system of equations. I understand you want to fit a straight line, and linear regression can do that (and much more) – 463035818_is_not_an_ai Dec 19 '20 at 14:20
  • I don't want to fit a straight line, that's why linear regression cannot work. I used polynomial regression but it doesn't match any time. Did you see the pictures I've attached? – Monica Dec 19 '20 at 14:24
  • ok, I misunderstood. No straight line, though "that's why linear regression cannot work" is not right. Anyhow, you should show what you tried and explain why it didnt work – 463035818_is_not_an_ai Dec 19 '20 at 14:25
  • Well as I researched until now, linear regression is used for straight lines, but maybe I misunderstood too. – Monica Dec 19 '20 at 14:28
  • I tried polynomial regression, as already mentioned and I have to add too high polynomial order so that the curve actually fits the points. (For example : the points from the second link) – Monica Dec 19 '20 at 14:29
  • actually cubic polynomial regression is a type of [linear regression](https://en.wikipedia.org/wiki/Linear_regression). As mentioned before, the "linear" in linear regression does not refer to the shape of the curve you want to fit – 463035818_is_not_an_ai Dec 19 '20 at 14:29
  • sorry for the confusion I added. I only read about cubic polynomial regression now. In any case, your issue seems to be the model and the fitting, not c++ code. Maybe you have more luck here: https://math.stackexchange.com/, though I suppose also there you'll need to show more details on what you already did – 463035818_is_not_an_ai Dec 19 '20 at 14:33
  • yeah, sure. I can try there too, but right now I'm not sure if I want to continue with polynomial regression, this is why I didn't show any code. I want to see other solutions, because I'm not sure this is the more effective way to do it. – Monica Dec 19 '20 at 14:37
  • did you browse existing question? Your question is currently too broad and what is wrong with your code is unclear. – 463035818_is_not_an_ai Dec 19 '20 at 14:38
  • yes, polynomial regression seamed to be the best at that moment but after using it I got confused, and I don't know if I should continue with it – Monica Dec 19 '20 at 14:48
  • Also, I've added details on what I've tried. – Monica Dec 19 '20 at 14:48

0 Answers0