Polynomial regression with Apache Maths (Java)

和自甴很熟 提交于 2019-12-13 15:03:17

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/28563361/polynomial-regression-with-apache-maths-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!