Example: ANOVA

This example computes a one-way analysis of variance for data discussed by Searle (1971, Table 5.1, pages 165-179). The responses are plant weights for 6 plants of 3 different types - 3 normal, 2 off-types, and 1 aberrant. The 3 normal plant weights are 101, 105, and 94. The 2 off-type plant weights are 84 and 88. The 1 aberrant plant weight is 32. Note in the results that for the group with only one response, the standard deviation is undefined and is set to NaN (not a number).

import com.imsl.stat.*;
import com.imsl.math.*;

public class ANOVAEx1 {

    public static void main(String args[]) {
        double y[][] = {
            {101, 105, 94},
            {84, 88},
            {32}
        };
        ANOVA anova = new ANOVA(y);
        double aov[] = anova.getArray();

        System.out.println("Degrees Of Freedom For Model = " + aov[0]);
        System.out.println("Degrees Of Freedom For Error = " + aov[1]);
        System.out.println("Total (Corrected) Degrees Of Freedom = " + aov[2]);
        System.out.println("Sum Of Squares For Model = " + aov[3]);
        System.out.println("Sum Of Squares For Error = " + aov[4]);
        System.out.println("Total (Corrected) Sum Of Squares = " + aov[5]);
        System.out.println("Model Mean Square = " + aov[6]);
        System.out.println("Error Mean Square = " + aov[7]);
        System.out.println("F statistic = " + aov[8]);
        System.out.println("P value= " + aov[9]);
        System.out.println("R Squared (in percent) = " + aov[10]);
        System.out.println("Adjusted R Squared (in percent) = " + aov[11]);
        System.out.println("Model Error Standard deviation = " + aov[12]);
        System.out.println("Mean Of Y = " + aov[13]);
        System.out.println("Coefficient Of Variation (in percent) = "
                + aov[14]);
        System.out.println("Total number of missing values = "
                + anova.getTotalMissing());

        PrintMatrixFormat pmf = new PrintMatrixFormat();
        String labels[] = {"Group", "N", "Mean", "Std. Deviation"};
        pmf.setColumnLabels(labels);
        pmf.setNumberFormat(null);
        new PrintMatrix("Group Information").print(pmf,
                anova.getGroupInformation());
    }
}

Output

Degrees Of Freedom For Model = 2.0
Degrees Of Freedom For Error = 3.0
Total (Corrected) Degrees Of Freedom = 5.0
Sum Of Squares For Model = 3480.0
Sum Of Squares For Error = 70.0
Total (Corrected) Sum Of Squares = 3550.0
Model Mean Square = 1740.0
Error Mean Square = 23.333333333333332
F statistic = 74.57142857142857
P value= 0.002768882525349784
R Squared (in percent) = 98.02816901408451
Adjusted R Squared (in percent) = 96.71361502347418
Model Error Standard deviation = 4.83045891539648
Mean Of Y = 84.0
Coefficient Of Variation (in percent) = 5.750546327852952
Total number of missing values = 0
            Group Information
   Group   N   Mean     Std. Deviation    
0   0.0   3.0  100.0  5.5677643628300215  
1   1.0   2.0   86.0  2.8284271247461903  
2   2.0   1.0   32.0  NaN                 

Link to Java source.