/** * Calculates the p-value of null Hypothesis * * @param flatDataCollection * @param median * @return * @throws IllegalArgumentException */ public static double getPvalue(FlatDataCollection flatDataCollection, double median) throws IllegalArgumentException { int n = 0; AssociativeArray Di = new AssociativeArray(); Iterator<Double> it = flatDataCollection.iteratorDouble(); while (it.hasNext()) { double delta = it.next() - median; if (delta == 0.0) { continue; // don't count it at all } String key = "+"; if (delta < 0) { key = "-"; } Di.put(key + String.valueOf(n), Math.abs(delta)); ++n; } if (n <= 0) { throw new IllegalArgumentException(); } // converts the values of the table with its Ranks Di.toFlatDataList(); Ranks.getRanksFromValues(Di); double W = 0.0; for (Map.Entry<Object, Object> entry : Di.entrySet()) { String key = entry.getKey().toString(); Double rank = TypeInference.toDouble(entry.getValue()); if (key.charAt(0) == '+') { W += rank; } } double pvalue = scoreToPvalue(W, n); return pvalue; }
/** * Calculates the score of Chisquare test. * * @param dataTable * @return * @throws IllegalArgumentException */ public static AssociativeArray getScore(DataTable2D dataTable) throws IllegalArgumentException { if (dataTable.isValid() == false) { throw new IllegalArgumentException(); } // Estimate marginal scores and sum Map<Object, Double> XdotJ = new HashMap<>(); Map<Object, Double> XIdot = new HashMap<>(); double Xdotdot = 0.0; for (Map.Entry<Object, AssociativeArray> entry1 : dataTable.entrySet()) { Object i = entry1.getKey(); AssociativeArray row = entry1.getValue(); for (Map.Entry<Object, Object> entry2 : row.entrySet()) { Object j = entry2.getKey(); Object value = entry2.getValue(); double v = Dataset.toDouble(value); // Summing the columns if (XdotJ.containsKey(j) == false) { XdotJ.put(j, v); } else { XdotJ.put(j, XdotJ.get(j) + v); } // Summing the rows if (XIdot.containsKey(i) == false) { XIdot.put(i, v); } else { XIdot.put(i, XIdot.get(i) + v); } Xdotdot += v; } } int k = XdotJ.size(); int n = XIdot.size(); // Calculating Chisquare score double ChisquareScore = 0.0; if (k == 2 && n == 2) { // if 2x2 then perform the Yates correction. Make this check outside the loops // to make it faster for (Map.Entry<Object, AssociativeArray> entry1 : dataTable.entrySet()) { Object i = entry1.getKey(); AssociativeArray row = entry1.getValue(); for (Map.Entry<Object, Object> entry2 : row.entrySet()) { Object j = entry2.getKey(); Object value = entry2.getValue(); double v = Dataset.toDouble(value); // expected value under null hypothesis double eij = XIdot.get(i) * XdotJ.get(j) / Xdotdot; if (eij == 0) { continue; } ChisquareScore += Math.pow((Math.abs(v - eij) - 0.5), 2) / eij; } } } else { for (Map.Entry<Object, AssociativeArray> entry1 : dataTable.entrySet()) { Object i = entry1.getKey(); AssociativeArray row = entry1.getValue(); for (Map.Entry<Object, Object> entry2 : row.entrySet()) { Object j = entry2.getKey(); Object value = entry2.getValue(); double v = Dataset.toDouble(value); // expected value under null hypothesis double eij = XIdot.get(i) * XdotJ.get(j) / Xdotdot; ChisquareScore += Math.pow((v - eij), 2) / eij; } } } XdotJ = null; XIdot = null; AssociativeArray result = new AssociativeArray(); result.put("k", k); result.put("n", n); result.put("score", ChisquareScore); return result; }