有谁可以帮我使用Apache Math库进行二次多项式回归?
以下数据应该给出这个方程: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