描述
用于单因子分析实验中,多个总体均值之间的比较。
Demo
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;
/***
* @author miaoyibo
*
*/
public class Anova {
Variance variance = new Variance();
Mean meanUtil = new Mean();
Sum sumUtil = new Sum();
private Map<String, double[]> map = new HashMap<String, double[]>();
// private List<double[]> list=new ArrayList<double[]>();
public void addE(String key, double[] e) {
map.put(key, e);
}
public Map<String, Double> getVariance() {
Map<String, Double> varianceMap = new HashMap<>();
for (Entry<String, double[]> e : map.entrySet()) {
varianceMap.put(e.getKey(), variance.evaluate(e.getValue()));
}
return varianceMap;
}
public Map<String, Double> getMean() {
Map<String, Double> meanMap = new HashMap<>();
for (Entry<String, double[]> e : map.entrySet()) {
meanMap.put(e.getKey(), meanUtil.evaluate(e.getValue()));
}
return meanMap;
}
public double getSumMean() {
double sum = 0;
int n = 0;
for (Entry<String, double[]> 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<String, double[]> 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<String, double[]> 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<String, double[]> 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);
}
}