Example usage for org.apache.commons.math3.stat.inference OneWayAnova anovaPValue

List of usage examples for org.apache.commons.math3.stat.inference OneWayAnova anovaPValue

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.inference OneWayAnova anovaPValue.

Prototype

public double anovaPValue(final Collection<double[]> categoryData) throws NullArgumentException,
        DimensionMismatchException, ConvergenceException, MaxCountExceededException 

Source Link

Document

Computes the ANOVA P-value for a collection of double[] arrays.

Usage

From source file:coolmap.utils.statistics.test.CTest.java

public static void anova(CoolMapObject obj, Dimension direction, VNode... nodes) {
    try {/*from ww  w  . ja va  2s.  c  o  m*/
        ArrayList<VNode> leafNodes = new ArrayList<VNode>();
        if (direction == Dimension.ROW) {
            leafNodes.addAll(obj.getViewNodesRow());
        } else if (direction == Dimension.COLUMN) {
            leafNodes.addAll(obj.getViewNodesColumn());
        } else {
            return;
        }

        ArrayList<double[]> data = new ArrayList<double[]>();
        if (direction == Dimension.ROW) {
            for (VNode node : nodes) {
                data.add(extractRow(obj, node));
            }
        } else if (direction == Dimension.COLUMN) {
            for (VNode node : nodes) {
                data.add(extractColumn(obj, node));
            }
        }

        OneWayAnova anova = new OneWayAnova();
        double pValue = anova.anovaPValue(data);
        TestResult result = new TestResult("One-way ANOVA",
                "One way analysis of variance wiht alpha = 0.05. \nData: " + obj.getName() + "\nDirection: "
                        + direction + "\nGroups: " + Arrays.toString(nodes),
                pValue);
        CMConsole.log(result.toString());

    } catch (Exception e) {
        CMConsole.logError(
                "ANOVA error: " + " Dataset:" + obj + " Direction:" + direction + " Groups:" + nodes == null
                        ? null
                        : Arrays.toString(nodes));
    }
}

From source file:fNIRs.FNIRsStats.java

private static void runANOVATests() {
    System.out.println("Running ANOVA tests.");

    File groupFile = new File("c:/Users/nkolesar/Desktop/CS Seminar/fNIRs/sub19/ANOVA Testing/legitGroups");
    File hbFile = new File("C:/Users/nkolesar/Desktop/CS Seminar/fNIRs/sub19/ANOVA Testing/legitHb");
    int precision = 7;
    int numChunks = 10;

    GroupedChannels groups1 = new GroupedChannels(hbFile, groupFile);

    String groupName = "groupOne";
    runANOVATestsHelper(groups1, groupName, 0);
    runANOVATestsHelper(groups1, groupName, 1);

    LinkedList<double[]> container = new LinkedList<double[]>();
    container.add(toPrimitiveDoubleArray(groups1.getGroup(groupName).getData(0)));
    container.add(toPrimitiveDoubleArray(groups1.getGroup(groupName).getData(1)));

    OneWayAnova myANOVA = new OneWayAnova();
    double result = -1;
    result = myANOVA.anovaPValue(container);
    System.out.println("Result: " + result);

    // outputANOVAs(groups1,
    // groups1.getGroupNames(), groups1.getConditions(),
    // numChunks, precision);
    File outFile = new File("C:/Users/nkolesar/Desktop/CS Seminar/fNIRs/sub19/ANOVA Testing/outputFile.csv");
    writeANOVAs(outFile, groups1, groups1.getGroupNames(), groups1.getConditions(),
            // groupNames, conditions,
            numChunks, precision);/*from w w  w . ja va2  s  . c  o  m*/
    System.out.println("Done.");
}

From source file:fNIRs.FNIRsStats.java

public static void oldOutputANOVAs(GroupedChannels data, List<String> groupNames, List<Integer> conditions,
        int numChunks, int precision) {
    // get all condition-group sequences:
    ArrayList<GroupedChannels.TaggedDataSequence> allTDSs = data.getAllSelectedTDSs(groupNames, conditions);

    chunkData(allTDSs, numChunks); // COMMENT THIS LATER

    // calculate required widths for printed names and condition numbers:
    int nameWidth = longestLength(groupNames); // length of longest name
    int conditionWidth = // number of digits in the largest condition number
            String.valueOf(Collections.max(conditions)).length();
    // make sure the fields will be wide enough to hold the ANOVA values,
    // which will, in the widest case, consist of a 1 followed by a . and
    // precision 0s: // AM I USING "PRECISION" RIGHT?
    int idFieldWidth = nameWidth + conditionWidth + 2;
    if (idFieldWidth < precision + 2) { // for the "1."
        // if not, increase the condition width so the total field width is
        // large enough:
        idFieldWidth = precision + 2;/*from   www . j  ava  2s . c o  m*/
    }

    String idFieldFormat = "%-" + idFieldWidth + "s";

    // output the first row, containing identifying information for each
    // group-condition combination:
    // first, output proper-width placeholder for the identifier column:
    System.out.printf("%" + idFieldWidth + "s  ", ""); // TOO HACKY??
    // then, output all tds identifiers:
    for (GroupedChannels.TaggedDataSequence tds : allTDSs) {
        System.out.printf(idFieldFormat + "  ", tds.getGroupName() + " c" + tds.getCondition());
    }
    System.out.println(); // print newline
    // output ANOVA values line by line:
    OneWayAnova myANOVA = new OneWayAnova();
    for (GroupedChannels.TaggedDataSequence first : allTDSs) {
        // output tds identifier in first column:
        System.out.printf(idFieldFormat + "  ", first.getGroupName() + " c" + first.getCondition());
        // create Collection to send to the ANOVA object:
        LinkedList<double[]> dataSets = new LinkedList<double[]>();
        // convert first's data sequence to an array, then add it to
        // dataSets
        dataSets.add(toPrimitiveDoubleArray(first.getData()));
        dataSets.add(null); // placeholder for second's data sequence
        for (GroupedChannels.TaggedDataSequence second : allTDSs) {
            // convert and add second's data sequence to position one in
            // dataSets:
            dataSets.set(1, toPrimitiveDoubleArray(second.getData()));
            double result = -1; // not a valid ANOVA value so we know if
            // something went wrong
            try {
                result = myANOVA.anovaPValue(dataSets);
            } catch (Exception ex) {
                System.out.println();
                localError("unknown problem calculating ANOVA: " + ex.getMessage());
            }
            System.out.printf("%-" + idFieldWidth + "." + precision + "f  ", result);
            // dataSets.remove(1); // remove second's data from dataSets
        }
        System.out.println(); // print newline
    }
}

From source file:fNIRs.FNIRsStats.java

public static void oldWriteANOVAs(File outFile, GroupedChannels data, List<String> groupNames,
        List<Integer> conditions, int numChunks, int precision) {
    // open file for writing with a nice print stream object:
    PrintWriter ostream = makePWriter(outFile); // OKAY VAR NAME?
    // get all condition-group sequences:
    ArrayList<GroupedChannels.TaggedDataSequence> allTDSs = data.getAllSelectedTDSs(groupNames, conditions);

    // chunkData(allTDSs, numChunks); // COMMENT THIS LATER

    // calculate required widths for printed names and condition numbers:
    int nameWidth = longestLength(groupNames); // length of longest name
    int conditionWidth = // number of digits in the largest condition number
            String.valueOf(Collections.max(conditions)).length();
    // make sure the fields will be wide enough to hold the ANOVA values,
    // which will consist of a 0 or 1 followed by a . and precision 0s:
    int idFieldWidth = nameWidth + 2 + conditionWidth; // 2 == " c".length()
    if (idFieldWidth < precision + 2) { // 2 == "1.".length()
        // if not, increase the condition width so the total field width is
        // large enough:
        // System.out.println("ANOVA values are wider than identifiers.");
        idFieldWidth = precision + 2;//from  w  ww .j a v  a  2 s. c om
    }
    String idFieldFormat = "%-" + idFieldWidth + "s"; // format string

    // output the first row, containing identifying information for each
    // group-condition combination:
    // first, output proper-width placeholder for the identifier column:
    ostream.printf("%" + idFieldWidth + "s  ", ""); // TOO HACKY??
    // then, output all tds identifiers:
    for (GroupedChannels.TaggedDataSequence tds : allTDSs) {
        ostream.printf(idFieldFormat + "  ", tds.getGroupName() + " c" + tds.getCondition());
        // ostream.printf(idFieldFormat + "  ",
        // tds.getGroupName(),
        // tds.getCondition());
    }
    ostream.println(); // print newline
    // output ANOVA values line by line:
    OneWayAnova myANOVA = new OneWayAnova();
    for (GroupedChannels.TaggedDataSequence first : allTDSs) {
        // output tds identifier in first column:
        ostream.printf(idFieldFormat + "  ", first.getGroupName() + " c" + first.getCondition());
        // create Collection to send to the ANOVA object:
        LinkedList<double[]> dataSets = new LinkedList<double[]>();
        // convert first's data sequence to an array, then add it to
        // dataSets
        dataSets.add(toPrimitiveDoubleArray(first.getData()));
        dataSets.add(null); // placeholder for second's data sequence
        for (GroupedChannels.TaggedDataSequence second : allTDSs) {
            // convert and add second's data sequence to position one in
            // dataSets:
            dataSets.set(1, toPrimitiveDoubleArray(second.getData()));
            double result = -1; // not a valid ANOVA value so we know if
            // something went wrong
            try {
                result = myANOVA.anovaPValue(dataSets);
                // if (first == second) { // if the two TDSs are the same
                // TDS,
                // result = 1; // then the ANOVA value should be 1, even
                // though a divide-by-zero
                // }
            } catch (Exception ex) {
                ostream.println();
                localError("unknown problem calculating ANOVA: " + ex.getMessage());
            }
            if (result != result) { // if result is NaN
                System.out.println("NaN on " + first.getGroupName() + " c" + first.getCondition() + " and "
                        + second.getGroupName() + " c" + second.getCondition());
            }
            ostream.printf("%-" + idFieldWidth + "." + precision + "f  ", result);
        }
        ostream.println(); // print newline
    }
    ostream.close();
}

From source file:fNIRs.FNIRsStats.java

public static void writeANOVAs(File outFile, GroupedChannels data, List<String> groupNames,
        List<Integer> conditions, int numChunks, int precision) {
    // open file for writing with a nice print stream object:
    PrintWriter ostream = makePWriter(outFile); // OKAY VAR NAME?
    // get all condition-group sequences:
    ArrayList<GroupedChannels.TaggedDataSequence> allTDSs = data.getAllSelectedTDSs(groupNames, conditions);

    // chunkData(allTDSs, numChunks); // COMMENT THIS LATER

    int idFieldWidth = getIdFieldWidth(groupNames, conditions, precision);
    String idFieldFormat = "%-" + idFieldWidth + "s"; // format string
    String separator = " , "; // what goes between columns of output

    // output the first row, containing identifying information for each
    // group-condition combination:
    // first, output spaces to take the place of the identifier column:
    ostream.printf("%" + idFieldWidth + "s" + separator, ""); // TOO HACKY??
    // then, output all tds identifiers:
    for (GroupedChannels.TaggedDataSequence tds : allTDSs) {
        ostream.printf(idFieldFormat + separator, tds.getGroupName() + " c" + tds.getCondition());
    }/*from  w w  w .j  a v  a  2s  . c  o  m*/
    ostream.println(); // print newline
    // output ANOVA values line by line:
    OneWayAnova myANOVA = new OneWayAnova(); // this object will do the
    // ANOVAs
    for (GroupedChannels.TaggedDataSequence first : allTDSs) {
        // output tds identifier in first column:
        ostream.printf(idFieldFormat + separator, first.getGroupName() + " c" + first.getCondition());
        // create Collection to send to the ANOVA object:
        LinkedList<double[]> dataSets = new LinkedList<double[]>();
        // convert first's data sequence to an array, then add it to
        // dataSets
        dataSets.add(toPrimitiveDoubleArray(first.getData()));
        dataSets.add(null); // placeholder for second's data sequence
        for (GroupedChannels.TaggedDataSequence second : allTDSs) {
            // convert and add second's data sequence to position one in
            // dataSets:
            dataSets.set(1, toPrimitiveDoubleArray(second.getData()));
            double result = -1; // not a valid ANOVA value so we know if
            // something went wrong
            try {
                result = myANOVA.anovaPValue(dataSets);
                // if (first == second) { // if the two TDSs are the same
                // TDS,
                // result = 1; // then the ANOVA value should be 1, even
                // though a divide-by-zero
                // }
            } catch (Exception ex) {
                ostream.println();
                System.out.println("foo");
                localError("unknown problem calculating ANOVA: " + ex.getMessage());
                System.out.println(
                        "---------------------------------------------------------------------------------------------------------------------------------------");
                for (double[] set : dataSets) {
                    printDoubleAry(set);
                    System.out.println("---------------------------------------");
                }
            }
            if (result != result) { // if result is NaN
                System.out.println("NaN on " + first.getGroupName() + " c" + first.getCondition() + " and "
                        + second.getGroupName() + " c" + second.getCondition());
            }
            // AGAIN, SEE IF "PRECISON" == "NUMBER OF DECIMAL PLACES"
            ostream.printf("%-" + idFieldWidth + "." + precision + "f" + separator, result);
        }
        ostream.println(); // print newline
    }
    ostream.close();
}

From source file:fNIRs.FNIRsStats.java

public static void outputANOVAs(GroupedChannels data, List<String> groupNames, List<Integer> conditions,
        int numChunks, int precision) {
    // get all condition-group sequences:
    ArrayList<GroupedChannels.TaggedDataSequence> allTDSs = data.getAllSelectedTDSs(groupNames, conditions);

    chunkData(allTDSs, numChunks); // COMMENT THIS LATER

    int idFieldWidth = getIdFieldWidth(groupNames, conditions, precision);
    // create a format string for the group/condition combination identifier
    // fields in the output:
    String idFieldFormat = "%-" + idFieldWidth + "s"; // format string
    String separator = " , "; // what goes between columns of output

    // output the first row, containing identifying information for each
    // group-condition combination:
    // first, output proper-width placeholder for the identifier column:
    System.out.printf("%" + idFieldWidth + "s" + separator, ""); // TOO
    // HACKY??//from w w w .  j  a  va 2  s  .c  o  m
    // then, output all tds identifiers:
    for (GroupedChannels.TaggedDataSequence tds : allTDSs) {
        System.out.printf(idFieldFormat + separator, tds.getGroupName() + " c" + tds.getCondition());
        // System.out.printf(idFieldFormat + "  ",
        // tds.getGroupName(),
        // tds.getCondition());
    }
    System.out.println(); // print newline
    // output ANOVA values line by line:
    OneWayAnova myANOVA = new OneWayAnova();
    for (GroupedChannels.TaggedDataSequence first : allTDSs) {
        // output tds identifier in first column:
        System.out.printf(idFieldFormat + separator, first.getGroupName() + " c" + first.getCondition());
        // create Collection to send to the ANOVA object:
        LinkedList<double[]> dataSets = new LinkedList<double[]>();
        // convert first's data sequence to an array, then add it to
        // dataSets
        dataSets.add(toPrimitiveDoubleArray(first.getData()));
        dataSets.add(null); // placeholder for second's data sequence
        for (GroupedChannels.TaggedDataSequence second : allTDSs) {
            // convert and add second's data sequence to position one in
            // dataSets:
            // dataSets.add(second.getData().toArray());
            dataSets.set(1, toPrimitiveDoubleArray(second.getData()));
            double result = -1; // so it'll be obvious something's wrong
            // later if this doesn't get changed
            try {
                result = myANOVA.anovaPValue(dataSets);
            } catch (Exception ex) {
                System.out.println();
                localError("unknown problem calculating ANOVA: " + ex.getMessage());
            }
            System.out.printf("%-" + idFieldWidth + "." + precision + "f" + separator, result);
            // dataSets.remove(1); // remove second's data from dataSets
        }
        System.out.println(); // print newline
    }
}

From source file:org.apache.solr.client.solrj.io.eval.AnovaEvaluator.java

@Override
public Object doWork(Object... values) throws IOException {

    // at this point we know every incoming value is an array of BigDecimals

    List<double[]> anovaInput = Arrays.stream(values)
            // for each List, convert to double[]
            .map(value -> ((List<BigDecimal>) value).stream().mapToDouble(BigDecimal::doubleValue).toArray())
            // turn into List<double[]>
            .collect(Collectors.toList());

    OneWayAnova anova = new OneWayAnova();
    double p = anova.anovaPValue(anovaInput);
    double f = anova.anovaFValue(anovaInput);
    Map<String, Number> m = new HashMap<>();
    m.put("p-value", p);
    m.put("f-ratio", f);
    return new Tuple(m);
}