java实现方差分析(ANOVA)

文章目录

    • 场景
    • maven依赖
    • 用法
    • 实现代码

场景

用于单因子分析实验中,多个总体均值之间的比较。

maven依赖

	
		
			net.sf.jsci
			jsci
			1.2
		
		
		
			org.apache.commons
			commons-math3
			3.6.1
		

	

用法

package com.math.demo;

import com.math.statistics.Anova;
/***
 * 此分析为单侧检验,用于多个总体均值相等的检验;
 * 结论只能得出总体均值是否不全相等;
 * 如果希望更进一步:确定到底是哪几个均值之间存在差异,可以使用多重比较方法。
 * @author miaoyibo
 *
 */
public class AnovaDemo {

	public static void main(String[] args) {
		Anova anovo=new Anova();
		double[] a= {58,64,55,66,67};
		double[] b= {58,69,71,64,68};
		double[] c= {48,57,59,47,49};
		anovo.addE("a", a);
		anovo.addE("b", b);
		anovo.addE("c", c);
		System.out.println(anovo.PValue());

	}

}

实现代码

package com.math.statistics;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.math3.stat.descriptive.moment.Mean;
import org.apache.commons.math3.stat.descriptive.moment.Variance;
import org.apache.commons.math3.stat.descriptive.summary.Sum;

import JSci.maths.statistics.FDistribution;

public class Anova {

	Variance variance = new Variance();

	Mean meanUtil = new Mean();
	Sum sumUtil = new Sum();
	private Map map = new HashMap();

	// private List list=new ArrayList();

	public void addE(String key, double[] e) {
		map.put(key, e);
	}

	public Map getVariance() {
		Map varianceMap = new HashMap<>();
		for (Entry e : map.entrySet()) {
			varianceMap.put(e.getKey(), variance.evaluate(e.getValue()));
		}
		return varianceMap;
	}

	public Map getMean() {
		Map meanMap = new HashMap<>();
		for (Entry e : map.entrySet()) {
			meanMap.put(e.getKey(), meanUtil.evaluate(e.getValue()));
		}
		return meanMap;
	}

	public double getSumMean() {
		double sum = 0;
		int n = 0;
		for (Entry e : map.entrySet()) {
			sum = sum + sumUtil.evaluate(e.getValue());
			n = n + e.getValue().length;
		}

		return sum / n;
	}
	public int getSumNum() {
		int n = 0;
		for (Entry e : map.entrySet()) {
			int length = e.getValue().length;
			n = n + e.getValue().length;
		}

		return  n;
	}


	public double getMSTR() {
		double sumMean = getSumMean();
		double numerator=0;
		for (Entry e : map.entrySet()) {
			double mean=meanUtil.evaluate(e.getValue());
			numerator=numerator+e.getValue().length*(mean-sumMean)*(mean-sumMean);
		}
		return numerator/(map.keySet().size()-1);
	}

	public double getMSE() {
		double numerator=0;
		for (Entry e : map.entrySet()) {
			double v=variance.evaluate(e.getValue());
			numerator=numerator+(e.getValue().length-1)*v;
		}
		double denominator=getSumNum()-map.keySet().size();
		return numerator/denominator;
	}
	
	public double PValue() {
		double MSTR= getMSTR();
		double MSE=getMSE();
		double f=MSTR/MSE;
		double free1=map.keySet().size()-1;
		double free2=getSumNum()-map.keySet().size();
		FDistribution fd=new FDistribution(free1, free2);
		return 1-fd.cumulative(f);
	}
}

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