java 线性回归_Java实现一元线性回归

最近在写一个荧光图像分析软件,需要自己拟合方程。一元回归线公式的算法参考了《Java数值方法》,拟合度R^2(绝对系数)是自己写的,欢迎讨论。计算结果和Excel完全一致。

总共三个文件:

DataPoint.java

/**

* A data point for interpolation and regression.

*/

public class DataPoint

{

/** the x value */  public float x;

/** the y value */  public float y;

/**

* Constructor.

* @param x the x value

* @param y the y value

*/

public DataPoint(float x, float y)

{

this.x = x;

this.y = y;

}

}

/**

* A least-squares regression line function.

*/

import java.util.*;

import java.math.BigDecimal;

public class RegressionLine

//implements Evaluatable

{

/** sum of x */     private double sumX;

/** sum of y */     private double sumY;

/** sum of x*x */   private double sumXX;

/** sum of x*y */   private double sumXY;

/** sum of y*y */   private double sumYY;

/** sum of yi-y */   private double sumDeltaY;

/** sum of sumDeltaY^2 */   private double sumDeltaY2;

/**误差 */

private double sse;

private double sst;

private double E;

private String[] xy ;

private ArrayList listX ;

private ArrayList listY ;

private int XMin,XMax,YMin,YMax;

/** line coefficient a0 */  private float a0;

/** line coefficient a1 */  private float a1;

/** number of data points */        private int     pn ;

/** true if coefficients valid */   private boolean coefsValid;

/**

* Constructor.

*/

public RegressionLine() {

XMax = 0;

YMax = 0;

pn = 0;

xy =new String[2];

listX = new ArrayList();

listY = new ArrayList();

}

/**

* Constructor.

* @param data the array of data points

*/

public RegressionLine(DataPoint data[])

{

pn = 0;

xy =new String[2];

listX = new ArrayList();

listY = new ArrayList();

for (int i = 0; i <

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值