java实现简单线性回归—显著性检验

1、demo

package com.math.demo;

import java.math.BigDecimal;

import com.math.regression.RegressionEquation;
import com.math.regression.RegressionModel;
/***
 * 
 * @author miaoyibo
 *
 */
public class RegressionDemo {

	public static void main(String[] args) {
		double[] dependentValues= {58,105,88,118,117,137,157,169,149,202};
		double[] independentValues= {2,6,8,8,12,16,20,20,22,26};
		RegressionEquation re=new RegressionEquation(dependentValues, independentValues);
		RegressionModel regressionModel = re.getRegressionModel();
		System.out.println("b0=="+regressionModel.getB0());
		System.out.println("b1=="+regressionModel.getB1());
		System.out.println("coefficient of determination=="+regressionModel.getR());
		System.out.println("T test=="+new BigDecimal(+regressionModel.getP()));
	}

}

2、实现

package com.math.regression;

import org.apache.commons.math3.stat.descriptive.moment.Mean;

import JSci.maths.statistics.TDistribution;
/***
 * least squares method
 * @author miaoyibo
 *
 */
public class RegressionEquation {
	//因变量y
	private double[] dependentValues;
	//自变量x
	private double[] independentValues;
	Mean meanUtil = new Mean();
	double xmean;
	double ymean;
	double sse;
	double sst;
	public RegressionEquation(double[] dependentValues, double[] independentValues) {
		this.dependentValues = dependentValues;
		this.independentValues = independentValues;
		xmean=meanUtil.evaluate(independentValues);
		ymean=meanUtil.evaluate(dependentValues);
	}

	
	public RegressionModel getRegressionModel() {
		RegressionModel model=new RegressionModel();
		double[] regression = calRegression();
		double sse=0;
		double sst=0;
		for(int i=0;i0) {
			p=(1-cumulative)*2;
		}else {
			p=cumulative*2;
		}
		model.setP(p);
	}
}

package com.math.regression;

public class RegressionModel {
	//y轴截距
	private double b0;
	//斜率
	private double b1;
	//判定系数
	private double r;
	
	private double p;
	public double getB0() {
		return b0;
	}
	public void setB0(double b0) {
		this.b0 = b0;
	}
	public double getB1() {
		return b1;
	}
	public void setB1(double b1) {
		this.b1 = b1;
	}
	public double getR() {
		return r;
	}
	public void setR(double r) {
		this.r = r;
	}
	public double getP() {
		return p;
	}
	public void setP(double p) {
		this.p = p;
	}
	
	

}

你可能感兴趣的:(统计学,java,概率论)