使用Apache Maths进行多项式回归(Java)

时间:2015-02-17 14:11:53

标签: java apache regression

有人可以帮我用Apache Math库进行多项式回归(第2阶段)。

以下数据应该给出这个等式:39.79 x ^ 2 - 497.66 x + 997.45 (由Excel计算,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));

以下是前一代码提供的回归系数:

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

显然,我缺少了一些......

感谢您的帮助

的Dom

1 个答案:

答案 0 :(得分:6)

您的所有数据点都在x = 1。

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

而不是1.0应该是x值,如果它们从零均匀间隔而不是用于循环而ix而不是1.0。

相关问题