Could anybody help me make a polynomial regression (order 2) with the Apache Math library.

The following data should give this equation: 39.79 x^2 - 497.66 x + 997.45 (computed by Excel with r2 = 0.9998)

```
// coding style from http://commons.apache.org/proper/commons-math/userguide/fitting.html
double[] y = { 540.0, 160.0, -140.0, -360.0, -480.0, -560.0, -540.0, -440.0, -260.0, 0.0, 340.0};
final WeightedObservedPoints obs = new WeightedObservedPoints();
for (double figure:y){
obs.add(1.0, figure);
}
final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2);
final double[] coeff = fitter.fit(obs.toList());
System.out.println("coef="+Arrays.toString(coeff));
```

Here are the regression coefficients provided by the previous code:

```
coef=[-53.73522460839947, -52.22329678670934, -52.22329678670934]
```

Obviously, there's something I'm missing...

Thanks for your kind help

Dom

asked Mar 18 '23 00:03
#### Dominique

All your data points are at x = 1.

```
obs.add(1.0, figure);!!!!
```

instead of 1.0 there should be x value, if they are evenly spaced from zero than use for loop and ix instead of 1.0.

answered Mar 19 '23 13:03
#### Zielu

### Recent Activity

- Apple Pay - authorize.net returns error 153 only when live, sandbox works
- How to continue cursor loop even error occured in the loop
- python find all neighbours of a given node in a list of lists
- Fatal error: Call to a member function setColumn() on a non-object in Magento
- Count how many of each value from a field with MySQL and PHP
- Python 32-bit development on 64-bit Windows [closed]

If you love us? You can donate to us via Paypal or buy me a coffee
so we can maintain and grow! **Thank you!**