@Test public void testTwoWayTestEqualCellsEqualVars() { logger.info("twoWayTestEqualCellsEqualVars"); TransposeDataCollection transposeDataCollection1 = new TransposeDataCollection(); transposeDataCollection1.put("StrainBright", new FlatDataCollection(Arrays.asList(new Object[]{26,14,41,16,28,29,92,31}))); transposeDataCollection1.put("StrainMixed", new FlatDataCollection(Arrays.asList(new Object[]{41,82,26,86,19,45,59,37}))); transposeDataCollection1.put("StrainDull", new FlatDataCollection(Arrays.asList(new Object[]{36,87,39,99,59,126,27,104}))); TransposeDataCollection transposeDataCollection2 = new TransposeDataCollection(); transposeDataCollection2.put("StrainBright", new FlatDataCollection(Arrays.asList(new Object[]{51,35,96,36,97,28,22,76}))); transposeDataCollection2.put("StrainMixed", new FlatDataCollection(Arrays.asList(new Object[]{39,114,104,92,130,87,122,64}))); transposeDataCollection2.put("StrainDull", new FlatDataCollection(Arrays.asList(new Object[]{42,133,92,144,156,68,144,142}))); TransposeDataCollection2D twoFactorDataCollection = new TransposeDataCollection2D(); twoFactorDataCollection.put("EnviromentFree", transposeDataCollection1); twoFactorDataCollection.put("EnviromentRestricted", transposeDataCollection2); double aLevel = 0.05; AssociativeArray2D outputTable = new AssociativeArray2D(); boolean expResult = true; boolean result = Anova.twoWayTestEqualCellsEqualVars(twoFactorDataCollection, aLevel, outputTable); assertEquals(expResult, result); }
public static boolean twoWayTestEqualCellsEqualVars(TransposeDataCollection2D twoFactorDataCollection, double aLevel, AssociativeArray2D outputTable) { Integer Itotal = twoFactorDataCollection.size(); Integer Jtotal = null; for(Map.Entry<Object, TransposeDataCollection> entry : twoFactorDataCollection.entrySet()) { if(Jtotal!=null) { if(Jtotal!=entry.getValue().size()) { throw new IllegalArgumentException("The cells must be of equal size."); } } else { Jtotal = entry.getValue().size(); if(Jtotal==0) { throw new IllegalArgumentException("The size of Jtotal must be larger than 0."); } } } int n=0; Map<Object, Integer> nidotdot = new HashMap<>(); Map<Object, Integer> ndotjdot = new HashMap<>(); Map<Object, Map<Object, Integer>> nijdot = new HashMap<>(); double Ydotdotdot=0.0; Map<Object, Double> Yidotdot = new HashMap<>(); Map<Object, Double> Ydotjdot = new HashMap<>(); Map<Object, Map<Object, Double>> Yijdot = new HashMap<>(); for(Map.Entry<Object, TransposeDataCollection> entry1 : twoFactorDataCollection.entrySet()) { Object IfactorAlevel = entry1.getKey(); TransposeDataCollection listOfBlevels = entry1.getValue(); if(!Yidotdot.containsKey(IfactorAlevel)) { Yidotdot.put(IfactorAlevel, 0.0); nidotdot.put(IfactorAlevel, 0); Yijdot.put(IfactorAlevel, new HashMap<>()); nijdot.put(IfactorAlevel, new HashMap<>()); } for(Map.Entry<Object, FlatDataCollection> entry2 : listOfBlevels.entrySet()) { Object JfactorBlevel = entry2.getKey(); FlatDataCollection flatDataCollection = entry2.getValue(); if(!Ydotjdot.containsKey(JfactorBlevel)) { Ydotjdot.put(JfactorBlevel, 0.0); ndotjdot.put(JfactorBlevel, 0); } if(!Yijdot.get(IfactorAlevel).containsKey(JfactorBlevel)) { Yijdot.get(IfactorAlevel).put(JfactorBlevel, 0.0); nijdot.get(IfactorAlevel).put(JfactorBlevel, 0); } Iterator<Double> it = flatDataCollection.iteratorDouble(); while(it.hasNext()) { Double value = it.next(); Ydotdotdot+=value; ++n; Yidotdot.put(IfactorAlevel, Yidotdot.get(IfactorAlevel)+value); nidotdot.put(IfactorAlevel, nidotdot.get(IfactorAlevel)+1); Ydotjdot.put(JfactorBlevel, Ydotjdot.get(JfactorBlevel)+value); ndotjdot.put(JfactorBlevel, ndotjdot.get(JfactorBlevel)+1); Yijdot.get(IfactorAlevel).put(JfactorBlevel, Yijdot.get(IfactorAlevel).get(JfactorBlevel) + value); nijdot.get(IfactorAlevel).put(JfactorBlevel, nijdot.get(IfactorAlevel).get(JfactorBlevel) + 1); } } } Ydotdotdot/=n; for(Map.Entry<Object, Double> entry : Yidotdot.entrySet()) { Object i = entry.getKey(); Yidotdot.put(i, entry.getValue()/nidotdot.get(i)); } for(Map.Entry<Object, Double> entry : Ydotjdot.entrySet()) { Object j = entry.getKey(); Ydotjdot.put(j, entry.getValue()/ndotjdot.get(j)); } for(Map.Entry<Object, Map<Object, Double>> entry1 : Yijdot.entrySet()) { Object i = entry1.getKey(); Map<Object, Double> listOfYj = entry1.getValue(); for(Map.Entry<Object, Double> entry2 : listOfYj.entrySet()) { Object j = entry2.getKey(); Double value = entry2.getValue(); Yijdot.get(i).put(j, value/nijdot.get(i).get(j)); } } double SSA=0; double SSB=0; double SSAB=0; double SST=0; for(Map.Entry<Object, TransposeDataCollection> entry1 : twoFactorDataCollection.entrySet()) { Object IfactorAlevel = entry1.getKey(); TransposeDataCollection listOfBlevels = entry1.getValue(); for(Map.Entry<Object, FlatDataCollection> entry2 : listOfBlevels.entrySet()) { Object JfactorBlevel = entry2.getKey(); FlatDataCollection flatDataCollection = entry2.getValue(); Iterator<Double> it = flatDataCollection.iteratorDouble(); while(it.hasNext()) { Double value = it.next(); SST+=Math.pow(value-Ydotdotdot,2); SSA+=Math.pow(Yidotdot.get(IfactorAlevel)-Ydotdotdot,2); SSB+=Math.pow(Ydotjdot.get(JfactorBlevel)-Ydotdotdot,2); SSAB+=Math.pow(Yijdot.get(IfactorAlevel).get(JfactorBlevel) -Yidotdot.get(IfactorAlevel) -Ydotjdot.get(JfactorBlevel) + Ydotdotdot ,2); } } } double SSE=SST-SSA-SSB-SSAB; double SSR=SSA+SSB+SSAB; int dfA=(Itotal-1); double MSA=SSA/dfA; int dfB=(Jtotal-1); double MSB=SSB/dfB; int dfAB=(Itotal-1)*(Jtotal-1); double MSAB=SSAB/dfAB; int dfE=(n-Itotal*Jtotal); double MSE=SSE/dfE; int dfR=dfA+dfB+dfAB; double MSR=SSR/dfR; double FA=MSA/MSE; double Apvalue=ContinuousDistributions.fCdf(FA,dfA,dfE); double FB=MSB/MSE; double Bpvalue=ContinuousDistributions.fCdf(FB,dfB,dfE); double FAB=MSAB/MSE; double ABpvalue=ContinuousDistributions.fCdf(FAB,dfAB,dfE); double FR=MSR/MSE; double Rpvalue=ContinuousDistributions.fCdf(FR,dfR,dfE); boolean rejectH0=false; double a=aLevel/2; if(Rpvalue<=a || Rpvalue>=(1-a)) { rejectH0=true; } if(outputTable!=null) { outputTable.clear(); outputTable.put2d("Model", "SSq", SSR); outputTable.put2d("Model", "DF", dfR); outputTable.put2d("Model", "MSq", MSR); outputTable.put2d("Model", "F", FR); outputTable.put2d("Model", "p", 1.0-Rpvalue); outputTable.put2d("AFactor", "SSq", SSA); outputTable.put2d("AFactor", "DF", dfA); outputTable.put2d("AFactor", "MSq", MSA); outputTable.put2d("AFactor", "F", FA); outputTable.put2d("AFactor", "p", 1.0-Apvalue); outputTable.put2d("BFactor", "SSq", SSB); outputTable.put2d("BFactor", "DF", dfB); outputTable.put2d("BFactor", "MSq", MSB); outputTable.put2d("BFactor", "F", FB); outputTable.put2d("BFactor", "p", 1.0-Bpvalue); outputTable.put2d("A*BFactor", "SSq", SSAB); outputTable.put2d("A*BFactor", "DF", dfAB); outputTable.put2d("A*BFactor", "MSq", MSAB); outputTable.put2d("A*BFactor", "F", FAB); outputTable.put2d("A*BFactor", "p", 1.0-ABpvalue); outputTable.put2d("Error", "SSq", SSE); outputTable.put2d("Error", "DF", dfE); outputTable.put2d("Error", "MSq", MSE); outputTable.put2d("Total", "SSq", SST); outputTable.put2d("Total", "DF", n-1); } return rejectH0; }
Anova { public static boolean twoWayTestEqualCellsEqualVars(TransposeDataCollection2D twoFactorDataCollection, double aLevel, AssociativeArray2D outputTable) { Integer Itotal = twoFactorDataCollection.size(); Integer Jtotal = null; for(Map.Entry<Object, TransposeDataCollection> entry : twoFactorDataCollection.entrySet()) { if(Jtotal!=null) { if(Jtotal!=entry.getValue().size()) { throw new IllegalArgumentException("The cells must be of equal size."); } } else { Jtotal = entry.getValue().size(); if(Jtotal==0) { throw new IllegalArgumentException("The size of Jtotal must be larger than 0."); } } } int n=0; Map<Object, Integer> nidotdot = new HashMap<>(); Map<Object, Integer> ndotjdot = new HashMap<>(); Map<Object, Map<Object, Integer>> nijdot = new HashMap<>(); double Ydotdotdot=0.0; Map<Object, Double> Yidotdot = new HashMap<>(); Map<Object, Double> Ydotjdot = new HashMap<>(); Map<Object, Map<Object, Double>> Yijdot = new HashMap<>(); for(Map.Entry<Object, TransposeDataCollection> entry1 : twoFactorDataCollection.entrySet()) { Object IfactorAlevel = entry1.getKey(); TransposeDataCollection listOfBlevels = entry1.getValue(); if(!Yidotdot.containsKey(IfactorAlevel)) { Yidotdot.put(IfactorAlevel, 0.0); nidotdot.put(IfactorAlevel, 0); Yijdot.put(IfactorAlevel, new HashMap<>()); nijdot.put(IfactorAlevel, new HashMap<>()); } for(Map.Entry<Object, FlatDataCollection> entry2 : listOfBlevels.entrySet()) { Object JfactorBlevel = entry2.getKey(); FlatDataCollection flatDataCollection = entry2.getValue(); if(!Ydotjdot.containsKey(JfactorBlevel)) { Ydotjdot.put(JfactorBlevel, 0.0); ndotjdot.put(JfactorBlevel, 0); } if(!Yijdot.get(IfactorAlevel).containsKey(JfactorBlevel)) { Yijdot.get(IfactorAlevel).put(JfactorBlevel, 0.0); nijdot.get(IfactorAlevel).put(JfactorBlevel, 0); } Iterator<Double> it = flatDataCollection.iteratorDouble(); while(it.hasNext()) { Double value = it.next(); Ydotdotdot+=value; ++n; Yidotdot.put(IfactorAlevel, Yidotdot.get(IfactorAlevel)+value); nidotdot.put(IfactorAlevel, nidotdot.get(IfactorAlevel)+1); Ydotjdot.put(JfactorBlevel, Ydotjdot.get(JfactorBlevel)+value); ndotjdot.put(JfactorBlevel, ndotjdot.get(JfactorBlevel)+1); Yijdot.get(IfactorAlevel).put(JfactorBlevel, Yijdot.get(IfactorAlevel).get(JfactorBlevel) + value); nijdot.get(IfactorAlevel).put(JfactorBlevel, nijdot.get(IfactorAlevel).get(JfactorBlevel) + 1); } } } Ydotdotdot/=n; for(Map.Entry<Object, Double> entry : Yidotdot.entrySet()) { Object i = entry.getKey(); Yidotdot.put(i, entry.getValue()/nidotdot.get(i)); } for(Map.Entry<Object, Double> entry : Ydotjdot.entrySet()) { Object j = entry.getKey(); Ydotjdot.put(j, entry.getValue()/ndotjdot.get(j)); } for(Map.Entry<Object, Map<Object, Double>> entry1 : Yijdot.entrySet()) { Object i = entry1.getKey(); Map<Object, Double> listOfYj = entry1.getValue(); for(Map.Entry<Object, Double> entry2 : listOfYj.entrySet()) { Object j = entry2.getKey(); Double value = entry2.getValue(); Yijdot.get(i).put(j, value/nijdot.get(i).get(j)); } } double SSA=0; double SSB=0; double SSAB=0; double SST=0; for(Map.Entry<Object, TransposeDataCollection> entry1 : twoFactorDataCollection.entrySet()) { Object IfactorAlevel = entry1.getKey(); TransposeDataCollection listOfBlevels = entry1.getValue(); for(Map.Entry<Object, FlatDataCollection> entry2 : listOfBlevels.entrySet()) { Object JfactorBlevel = entry2.getKey(); FlatDataCollection flatDataCollection = entry2.getValue(); Iterator<Double> it = flatDataCollection.iteratorDouble(); while(it.hasNext()) { Double value = it.next(); SST+=Math.pow(value-Ydotdotdot,2); SSA+=Math.pow(Yidotdot.get(IfactorAlevel)-Ydotdotdot,2); SSB+=Math.pow(Ydotjdot.get(JfactorBlevel)-Ydotdotdot,2); SSAB+=Math.pow(Yijdot.get(IfactorAlevel).get(JfactorBlevel) -Yidotdot.get(IfactorAlevel) -Ydotjdot.get(JfactorBlevel) + Ydotdotdot ,2); } } } double SSE=SST-SSA-SSB-SSAB; double SSR=SSA+SSB+SSAB; int dfA=(Itotal-1); double MSA=SSA/dfA; int dfB=(Jtotal-1); double MSB=SSB/dfB; int dfAB=(Itotal-1)*(Jtotal-1); double MSAB=SSAB/dfAB; int dfE=(n-Itotal*Jtotal); double MSE=SSE/dfE; int dfR=dfA+dfB+dfAB; double MSR=SSR/dfR; double FA=MSA/MSE; double Apvalue=ContinuousDistributions.fCdf(FA,dfA,dfE); double FB=MSB/MSE; double Bpvalue=ContinuousDistributions.fCdf(FB,dfB,dfE); double FAB=MSAB/MSE; double ABpvalue=ContinuousDistributions.fCdf(FAB,dfAB,dfE); double FR=MSR/MSE; double Rpvalue=ContinuousDistributions.fCdf(FR,dfR,dfE); boolean rejectH0=false; double a=aLevel/2; if(Rpvalue<=a || Rpvalue>=(1-a)) { rejectH0=true; } if(outputTable!=null) { outputTable.clear(); outputTable.put2d("Model", "SSq", SSR); outputTable.put2d("Model", "DF", dfR); outputTable.put2d("Model", "MSq", MSR); outputTable.put2d("Model", "F", FR); outputTable.put2d("Model", "p", 1.0-Rpvalue); outputTable.put2d("AFactor", "SSq", SSA); outputTable.put2d("AFactor", "DF", dfA); outputTable.put2d("AFactor", "MSq", MSA); outputTable.put2d("AFactor", "F", FA); outputTable.put2d("AFactor", "p", 1.0-Apvalue); outputTable.put2d("BFactor", "SSq", SSB); outputTable.put2d("BFactor", "DF", dfB); outputTable.put2d("BFactor", "MSq", MSB); outputTable.put2d("BFactor", "F", FB); outputTable.put2d("BFactor", "p", 1.0-Bpvalue); outputTable.put2d("A*BFactor", "SSq", SSAB); outputTable.put2d("A*BFactor", "DF", dfAB); outputTable.put2d("A*BFactor", "MSq", MSAB); outputTable.put2d("A*BFactor", "F", FAB); outputTable.put2d("A*BFactor", "p", 1.0-ABpvalue); outputTable.put2d("Error", "SSq", SSE); outputTable.put2d("Error", "DF", dfE); outputTable.put2d("Error", "MSq", MSE); outputTable.put2d("Total", "SSq", SST); outputTable.put2d("Total", "DF", n-1); } return rejectH0; } }
Anova { public static boolean twoWayTestEqualCellsEqualVars(TransposeDataCollection2D twoFactorDataCollection, double aLevel, AssociativeArray2D outputTable) { Integer Itotal = twoFactorDataCollection.size(); Integer Jtotal = null; for(Map.Entry<Object, TransposeDataCollection> entry : twoFactorDataCollection.entrySet()) { if(Jtotal!=null) { if(Jtotal!=entry.getValue().size()) { throw new IllegalArgumentException("The cells must be of equal size."); } } else { Jtotal = entry.getValue().size(); if(Jtotal==0) { throw new IllegalArgumentException("The size of Jtotal must be larger than 0."); } } } int n=0; Map<Object, Integer> nidotdot = new HashMap<>(); Map<Object, Integer> ndotjdot = new HashMap<>(); Map<Object, Map<Object, Integer>> nijdot = new HashMap<>(); double Ydotdotdot=0.0; Map<Object, Double> Yidotdot = new HashMap<>(); Map<Object, Double> Ydotjdot = new HashMap<>(); Map<Object, Map<Object, Double>> Yijdot = new HashMap<>(); for(Map.Entry<Object, TransposeDataCollection> entry1 : twoFactorDataCollection.entrySet()) { Object IfactorAlevel = entry1.getKey(); TransposeDataCollection listOfBlevels = entry1.getValue(); if(!Yidotdot.containsKey(IfactorAlevel)) { Yidotdot.put(IfactorAlevel, 0.0); nidotdot.put(IfactorAlevel, 0); Yijdot.put(IfactorAlevel, new HashMap<>()); nijdot.put(IfactorAlevel, new HashMap<>()); } for(Map.Entry<Object, FlatDataCollection> entry2 : listOfBlevels.entrySet()) { Object JfactorBlevel = entry2.getKey(); FlatDataCollection flatDataCollection = entry2.getValue(); if(!Ydotjdot.containsKey(JfactorBlevel)) { Ydotjdot.put(JfactorBlevel, 0.0); ndotjdot.put(JfactorBlevel, 0); } if(!Yijdot.get(IfactorAlevel).containsKey(JfactorBlevel)) { Yijdot.get(IfactorAlevel).put(JfactorBlevel, 0.0); nijdot.get(IfactorAlevel).put(JfactorBlevel, 0); } Iterator<Double> it = flatDataCollection.iteratorDouble(); while(it.hasNext()) { Double value = it.next(); Ydotdotdot+=value; ++n; Yidotdot.put(IfactorAlevel, Yidotdot.get(IfactorAlevel)+value); nidotdot.put(IfactorAlevel, nidotdot.get(IfactorAlevel)+1); Ydotjdot.put(JfactorBlevel, Ydotjdot.get(JfactorBlevel)+value); ndotjdot.put(JfactorBlevel, ndotjdot.get(JfactorBlevel)+1); Yijdot.get(IfactorAlevel).put(JfactorBlevel, Yijdot.get(IfactorAlevel).get(JfactorBlevel) + value); nijdot.get(IfactorAlevel).put(JfactorBlevel, nijdot.get(IfactorAlevel).get(JfactorBlevel) + 1); } } } Ydotdotdot/=n; for(Map.Entry<Object, Double> entry : Yidotdot.entrySet()) { Object i = entry.getKey(); Yidotdot.put(i, entry.getValue()/nidotdot.get(i)); } for(Map.Entry<Object, Double> entry : Ydotjdot.entrySet()) { Object j = entry.getKey(); Ydotjdot.put(j, entry.getValue()/ndotjdot.get(j)); } for(Map.Entry<Object, Map<Object, Double>> entry1 : Yijdot.entrySet()) { Object i = entry1.getKey(); Map<Object, Double> listOfYj = entry1.getValue(); for(Map.Entry<Object, Double> entry2 : listOfYj.entrySet()) { Object j = entry2.getKey(); Double value = entry2.getValue(); Yijdot.get(i).put(j, value/nijdot.get(i).get(j)); } } double SSA=0; double SSB=0; double SSAB=0; double SST=0; for(Map.Entry<Object, TransposeDataCollection> entry1 : twoFactorDataCollection.entrySet()) { Object IfactorAlevel = entry1.getKey(); TransposeDataCollection listOfBlevels = entry1.getValue(); for(Map.Entry<Object, FlatDataCollection> entry2 : listOfBlevels.entrySet()) { Object JfactorBlevel = entry2.getKey(); FlatDataCollection flatDataCollection = entry2.getValue(); Iterator<Double> it = flatDataCollection.iteratorDouble(); while(it.hasNext()) { Double value = it.next(); SST+=Math.pow(value-Ydotdotdot,2); SSA+=Math.pow(Yidotdot.get(IfactorAlevel)-Ydotdotdot,2); SSB+=Math.pow(Ydotjdot.get(JfactorBlevel)-Ydotdotdot,2); SSAB+=Math.pow(Yijdot.get(IfactorAlevel).get(JfactorBlevel) -Yidotdot.get(IfactorAlevel) -Ydotjdot.get(JfactorBlevel) + Ydotdotdot ,2); } } } double SSE=SST-SSA-SSB-SSAB; double SSR=SSA+SSB+SSAB; int dfA=(Itotal-1); double MSA=SSA/dfA; int dfB=(Jtotal-1); double MSB=SSB/dfB; int dfAB=(Itotal-1)*(Jtotal-1); double MSAB=SSAB/dfAB; int dfE=(n-Itotal*Jtotal); double MSE=SSE/dfE; int dfR=dfA+dfB+dfAB; double MSR=SSR/dfR; double FA=MSA/MSE; double Apvalue=ContinuousDistributions.fCdf(FA,dfA,dfE); double FB=MSB/MSE; double Bpvalue=ContinuousDistributions.fCdf(FB,dfB,dfE); double FAB=MSAB/MSE; double ABpvalue=ContinuousDistributions.fCdf(FAB,dfAB,dfE); double FR=MSR/MSE; double Rpvalue=ContinuousDistributions.fCdf(FR,dfR,dfE); boolean rejectH0=false; double a=aLevel/2; if(Rpvalue<=a || Rpvalue>=(1-a)) { rejectH0=true; } if(outputTable!=null) { outputTable.clear(); outputTable.put2d("Model", "SSq", SSR); outputTable.put2d("Model", "DF", dfR); outputTable.put2d("Model", "MSq", MSR); outputTable.put2d("Model", "F", FR); outputTable.put2d("Model", "p", 1.0-Rpvalue); outputTable.put2d("AFactor", "SSq", SSA); outputTable.put2d("AFactor", "DF", dfA); outputTable.put2d("AFactor", "MSq", MSA); outputTable.put2d("AFactor", "F", FA); outputTable.put2d("AFactor", "p", 1.0-Apvalue); outputTable.put2d("BFactor", "SSq", SSB); outputTable.put2d("BFactor", "DF", dfB); outputTable.put2d("BFactor", "MSq", MSB); outputTable.put2d("BFactor", "F", FB); outputTable.put2d("BFactor", "p", 1.0-Bpvalue); outputTable.put2d("A*BFactor", "SSq", SSAB); outputTable.put2d("A*BFactor", "DF", dfAB); outputTable.put2d("A*BFactor", "MSq", MSAB); outputTable.put2d("A*BFactor", "F", FAB); outputTable.put2d("A*BFactor", "p", 1.0-ABpvalue); outputTable.put2d("Error", "SSq", SSE); outputTable.put2d("Error", "DF", dfE); outputTable.put2d("Error", "MSq", MSE); outputTable.put2d("Total", "SSq", SST); outputTable.put2d("Total", "DF", n-1); } return rejectH0; }  }
Anova { public static boolean twoWayTestEqualCellsEqualVars(TransposeDataCollection2D twoFactorDataCollection, double aLevel, AssociativeArray2D outputTable) { Integer Itotal = twoFactorDataCollection.size(); Integer Jtotal = null; for(Map.Entry<Object, TransposeDataCollection> entry : twoFactorDataCollection.entrySet()) { if(Jtotal!=null) { if(Jtotal!=entry.getValue().size()) { throw new IllegalArgumentException("The cells must be of equal size."); } } else { Jtotal = entry.getValue().size(); if(Jtotal==0) { throw new IllegalArgumentException("The size of Jtotal must be larger than 0."); } } } int n=0; Map<Object, Integer> nidotdot = new HashMap<>(); Map<Object, Integer> ndotjdot = new HashMap<>(); Map<Object, Map<Object, Integer>> nijdot = new HashMap<>(); double Ydotdotdot=0.0; Map<Object, Double> Yidotdot = new HashMap<>(); Map<Object, Double> Ydotjdot = new HashMap<>(); Map<Object, Map<Object, Double>> Yijdot = new HashMap<>(); for(Map.Entry<Object, TransposeDataCollection> entry1 : twoFactorDataCollection.entrySet()) { Object IfactorAlevel = entry1.getKey(); TransposeDataCollection listOfBlevels = entry1.getValue(); if(!Yidotdot.containsKey(IfactorAlevel)) { Yidotdot.put(IfactorAlevel, 0.0); nidotdot.put(IfactorAlevel, 0); Yijdot.put(IfactorAlevel, new HashMap<>()); nijdot.put(IfactorAlevel, new HashMap<>()); } for(Map.Entry<Object, FlatDataCollection> entry2 : listOfBlevels.entrySet()) { Object JfactorBlevel = entry2.getKey(); FlatDataCollection flatDataCollection = entry2.getValue(); if(!Ydotjdot.containsKey(JfactorBlevel)) { Ydotjdot.put(JfactorBlevel, 0.0); ndotjdot.put(JfactorBlevel, 0); } if(!Yijdot.get(IfactorAlevel).containsKey(JfactorBlevel)) { Yijdot.get(IfactorAlevel).put(JfactorBlevel, 0.0); nijdot.get(IfactorAlevel).put(JfactorBlevel, 0); } Iterator<Double> it = flatDataCollection.iteratorDouble(); while(it.hasNext()) { Double value = it.next(); Ydotdotdot+=value; ++n; Yidotdot.put(IfactorAlevel, Yidotdot.get(IfactorAlevel)+value); nidotdot.put(IfactorAlevel, nidotdot.get(IfactorAlevel)+1); Ydotjdot.put(JfactorBlevel, Ydotjdot.get(JfactorBlevel)+value); ndotjdot.put(JfactorBlevel, ndotjdot.get(JfactorBlevel)+1); Yijdot.get(IfactorAlevel).put(JfactorBlevel, Yijdot.get(IfactorAlevel).get(JfactorBlevel) + value); nijdot.get(IfactorAlevel).put(JfactorBlevel, nijdot.get(IfactorAlevel).get(JfactorBlevel) + 1); } } } Ydotdotdot/=n; for(Map.Entry<Object, Double> entry : Yidotdot.entrySet()) { Object i = entry.getKey(); Yidotdot.put(i, entry.getValue()/nidotdot.get(i)); } for(Map.Entry<Object, Double> entry : Ydotjdot.entrySet()) { Object j = entry.getKey(); Ydotjdot.put(j, entry.getValue()/ndotjdot.get(j)); } for(Map.Entry<Object, Map<Object, Double>> entry1 : Yijdot.entrySet()) { Object i = entry1.getKey(); Map<Object, Double> listOfYj = entry1.getValue(); for(Map.Entry<Object, Double> entry2 : listOfYj.entrySet()) { Object j = entry2.getKey(); Double value = entry2.getValue(); Yijdot.get(i).put(j, value/nijdot.get(i).get(j)); } } double SSA=0; double SSB=0; double SSAB=0; double SST=0; for(Map.Entry<Object, TransposeDataCollection> entry1 : twoFactorDataCollection.entrySet()) { Object IfactorAlevel = entry1.getKey(); TransposeDataCollection listOfBlevels = entry1.getValue(); for(Map.Entry<Object, FlatDataCollection> entry2 : listOfBlevels.entrySet()) { Object JfactorBlevel = entry2.getKey(); FlatDataCollection flatDataCollection = entry2.getValue(); Iterator<Double> it = flatDataCollection.iteratorDouble(); while(it.hasNext()) { Double value = it.next(); SST+=Math.pow(value-Ydotdotdot,2); SSA+=Math.pow(Yidotdot.get(IfactorAlevel)-Ydotdotdot,2); SSB+=Math.pow(Ydotjdot.get(JfactorBlevel)-Ydotdotdot,2); SSAB+=Math.pow(Yijdot.get(IfactorAlevel).get(JfactorBlevel) -Yidotdot.get(IfactorAlevel) -Ydotjdot.get(JfactorBlevel) + Ydotdotdot ,2); } } } double SSE=SST-SSA-SSB-SSAB; double SSR=SSA+SSB+SSAB; int dfA=(Itotal-1); double MSA=SSA/dfA; int dfB=(Jtotal-1); double MSB=SSB/dfB; int dfAB=(Itotal-1)*(Jtotal-1); double MSAB=SSAB/dfAB; int dfE=(n-Itotal*Jtotal); double MSE=SSE/dfE; int dfR=dfA+dfB+dfAB; double MSR=SSR/dfR; double FA=MSA/MSE; double Apvalue=ContinuousDistributions.fCdf(FA,dfA,dfE); double FB=MSB/MSE; double Bpvalue=ContinuousDistributions.fCdf(FB,dfB,dfE); double FAB=MSAB/MSE; double ABpvalue=ContinuousDistributions.fCdf(FAB,dfAB,dfE); double FR=MSR/MSE; double Rpvalue=ContinuousDistributions.fCdf(FR,dfR,dfE); boolean rejectH0=false; double a=aLevel/2; if(Rpvalue<=a || Rpvalue>=(1-a)) { rejectH0=true; } if(outputTable!=null) { outputTable.clear(); outputTable.put2d("Model", "SSq", SSR); outputTable.put2d("Model", "DF", dfR); outputTable.put2d("Model", "MSq", MSR); outputTable.put2d("Model", "F", FR); outputTable.put2d("Model", "p", 1.0-Rpvalue); outputTable.put2d("AFactor", "SSq", SSA); outputTable.put2d("AFactor", "DF", dfA); outputTable.put2d("AFactor", "MSq", MSA); outputTable.put2d("AFactor", "F", FA); outputTable.put2d("AFactor", "p", 1.0-Apvalue); outputTable.put2d("BFactor", "SSq", SSB); outputTable.put2d("BFactor", "DF", dfB); outputTable.put2d("BFactor", "MSq", MSB); outputTable.put2d("BFactor", "F", FB); outputTable.put2d("BFactor", "p", 1.0-Bpvalue); outputTable.put2d("A*BFactor", "SSq", SSAB); outputTable.put2d("A*BFactor", "DF", dfAB); outputTable.put2d("A*BFactor", "MSq", MSAB); outputTable.put2d("A*BFactor", "F", FAB); outputTable.put2d("A*BFactor", "p", 1.0-ABpvalue); outputTable.put2d("Error", "SSq", SSE); outputTable.put2d("Error", "DF", dfE); outputTable.put2d("Error", "MSq", MSE); outputTable.put2d("Total", "SSq", SST); outputTable.put2d("Total", "DF", n-1); } return rejectH0; }  static boolean oneWayTestEqualVars(TransposeDataCollection transposeDataCollection, double aLevel, AssociativeArray2D outputTable); static boolean oneWayTestEqualVars(TransposeDataCollection transposeDataCollection, double aLevel); static boolean oneWayTestNotEqualVars(TransposeDataCollection transposeDataCollection, double aLevel, AssociativeArray2D outputTable); static boolean oneWayTestNotEqualVars(TransposeDataCollection transposeDataCollection, double aLevel); static boolean twoWayTestEqualCellsEqualVars(TransposeDataCollection2D twoFactorDataCollection, double aLevel, AssociativeArray2D outputTable); static boolean twoWayTestEqualCellsEqualVars(TransposeDataCollection2D twoFactorDataCollection, double aLevel); }
Anova { public static boolean twoWayTestEqualCellsEqualVars(TransposeDataCollection2D twoFactorDataCollection, double aLevel, AssociativeArray2D outputTable) { Integer Itotal = twoFactorDataCollection.size(); Integer Jtotal = null; for(Map.Entry<Object, TransposeDataCollection> entry : twoFactorDataCollection.entrySet()) { if(Jtotal!=null) { if(Jtotal!=entry.getValue().size()) { throw new IllegalArgumentException("The cells must be of equal size."); } } else { Jtotal = entry.getValue().size(); if(Jtotal==0) { throw new IllegalArgumentException("The size of Jtotal must be larger than 0."); } } } int n=0; Map<Object, Integer> nidotdot = new HashMap<>(); Map<Object, Integer> ndotjdot = new HashMap<>(); Map<Object, Map<Object, Integer>> nijdot = new HashMap<>(); double Ydotdotdot=0.0; Map<Object, Double> Yidotdot = new HashMap<>(); Map<Object, Double> Ydotjdot = new HashMap<>(); Map<Object, Map<Object, Double>> Yijdot = new HashMap<>(); for(Map.Entry<Object, TransposeDataCollection> entry1 : twoFactorDataCollection.entrySet()) { Object IfactorAlevel = entry1.getKey(); TransposeDataCollection listOfBlevels = entry1.getValue(); if(!Yidotdot.containsKey(IfactorAlevel)) { Yidotdot.put(IfactorAlevel, 0.0); nidotdot.put(IfactorAlevel, 0); Yijdot.put(IfactorAlevel, new HashMap<>()); nijdot.put(IfactorAlevel, new HashMap<>()); } for(Map.Entry<Object, FlatDataCollection> entry2 : listOfBlevels.entrySet()) { Object JfactorBlevel = entry2.getKey(); FlatDataCollection flatDataCollection = entry2.getValue(); if(!Ydotjdot.containsKey(JfactorBlevel)) { Ydotjdot.put(JfactorBlevel, 0.0); ndotjdot.put(JfactorBlevel, 0); } if(!Yijdot.get(IfactorAlevel).containsKey(JfactorBlevel)) { Yijdot.get(IfactorAlevel).put(JfactorBlevel, 0.0); nijdot.get(IfactorAlevel).put(JfactorBlevel, 0); } Iterator<Double> it = flatDataCollection.iteratorDouble(); while(it.hasNext()) { Double value = it.next(); Ydotdotdot+=value; ++n; Yidotdot.put(IfactorAlevel, Yidotdot.get(IfactorAlevel)+value); nidotdot.put(IfactorAlevel, nidotdot.get(IfactorAlevel)+1); Ydotjdot.put(JfactorBlevel, Ydotjdot.get(JfactorBlevel)+value); ndotjdot.put(JfactorBlevel, ndotjdot.get(JfactorBlevel)+1); Yijdot.get(IfactorAlevel).put(JfactorBlevel, Yijdot.get(IfactorAlevel).get(JfactorBlevel) + value); nijdot.get(IfactorAlevel).put(JfactorBlevel, nijdot.get(IfactorAlevel).get(JfactorBlevel) + 1); } } } Ydotdotdot/=n; for(Map.Entry<Object, Double> entry : Yidotdot.entrySet()) { Object i = entry.getKey(); Yidotdot.put(i, entry.getValue()/nidotdot.get(i)); } for(Map.Entry<Object, Double> entry : Ydotjdot.entrySet()) { Object j = entry.getKey(); Ydotjdot.put(j, entry.getValue()/ndotjdot.get(j)); } for(Map.Entry<Object, Map<Object, Double>> entry1 : Yijdot.entrySet()) { Object i = entry1.getKey(); Map<Object, Double> listOfYj = entry1.getValue(); for(Map.Entry<Object, Double> entry2 : listOfYj.entrySet()) { Object j = entry2.getKey(); Double value = entry2.getValue(); Yijdot.get(i).put(j, value/nijdot.get(i).get(j)); } } double SSA=0; double SSB=0; double SSAB=0; double SST=0; for(Map.Entry<Object, TransposeDataCollection> entry1 : twoFactorDataCollection.entrySet()) { Object IfactorAlevel = entry1.getKey(); TransposeDataCollection listOfBlevels = entry1.getValue(); for(Map.Entry<Object, FlatDataCollection> entry2 : listOfBlevels.entrySet()) { Object JfactorBlevel = entry2.getKey(); FlatDataCollection flatDataCollection = entry2.getValue(); Iterator<Double> it = flatDataCollection.iteratorDouble(); while(it.hasNext()) { Double value = it.next(); SST+=Math.pow(value-Ydotdotdot,2); SSA+=Math.pow(Yidotdot.get(IfactorAlevel)-Ydotdotdot,2); SSB+=Math.pow(Ydotjdot.get(JfactorBlevel)-Ydotdotdot,2); SSAB+=Math.pow(Yijdot.get(IfactorAlevel).get(JfactorBlevel) -Yidotdot.get(IfactorAlevel) -Ydotjdot.get(JfactorBlevel) + Ydotdotdot ,2); } } } double SSE=SST-SSA-SSB-SSAB; double SSR=SSA+SSB+SSAB; int dfA=(Itotal-1); double MSA=SSA/dfA; int dfB=(Jtotal-1); double MSB=SSB/dfB; int dfAB=(Itotal-1)*(Jtotal-1); double MSAB=SSAB/dfAB; int dfE=(n-Itotal*Jtotal); double MSE=SSE/dfE; int dfR=dfA+dfB+dfAB; double MSR=SSR/dfR; double FA=MSA/MSE; double Apvalue=ContinuousDistributions.fCdf(FA,dfA,dfE); double FB=MSB/MSE; double Bpvalue=ContinuousDistributions.fCdf(FB,dfB,dfE); double FAB=MSAB/MSE; double ABpvalue=ContinuousDistributions.fCdf(FAB,dfAB,dfE); double FR=MSR/MSE; double Rpvalue=ContinuousDistributions.fCdf(FR,dfR,dfE); boolean rejectH0=false; double a=aLevel/2; if(Rpvalue<=a || Rpvalue>=(1-a)) { rejectH0=true; } if(outputTable!=null) { outputTable.clear(); outputTable.put2d("Model", "SSq", SSR); outputTable.put2d("Model", "DF", dfR); outputTable.put2d("Model", "MSq", MSR); outputTable.put2d("Model", "F", FR); outputTable.put2d("Model", "p", 1.0-Rpvalue); outputTable.put2d("AFactor", "SSq", SSA); outputTable.put2d("AFactor", "DF", dfA); outputTable.put2d("AFactor", "MSq", MSA); outputTable.put2d("AFactor", "F", FA); outputTable.put2d("AFactor", "p", 1.0-Apvalue); outputTable.put2d("BFactor", "SSq", SSB); outputTable.put2d("BFactor", "DF", dfB); outputTable.put2d("BFactor", "MSq", MSB); outputTable.put2d("BFactor", "F", FB); outputTable.put2d("BFactor", "p", 1.0-Bpvalue); outputTable.put2d("A*BFactor", "SSq", SSAB); outputTable.put2d("A*BFactor", "DF", dfAB); outputTable.put2d("A*BFactor", "MSq", MSAB); outputTable.put2d("A*BFactor", "F", FAB); outputTable.put2d("A*BFactor", "p", 1.0-ABpvalue); outputTable.put2d("Error", "SSq", SSE); outputTable.put2d("Error", "DF", dfE); outputTable.put2d("Error", "MSq", MSE); outputTable.put2d("Total", "SSq", SST); outputTable.put2d("Total", "DF", n-1); } return rejectH0; }  static boolean oneWayTestEqualVars(TransposeDataCollection transposeDataCollection, double aLevel, AssociativeArray2D outputTable); static boolean oneWayTestEqualVars(TransposeDataCollection transposeDataCollection, double aLevel); static boolean oneWayTestNotEqualVars(TransposeDataCollection transposeDataCollection, double aLevel, AssociativeArray2D outputTable); static boolean oneWayTestNotEqualVars(TransposeDataCollection transposeDataCollection, double aLevel); static boolean twoWayTestEqualCellsEqualVars(TransposeDataCollection2D twoFactorDataCollection, double aLevel, AssociativeArray2D outputTable); static boolean twoWayTestEqualCellsEqualVars(TransposeDataCollection2D twoFactorDataCollection, double aLevel);  }