Example 2: Two-way Analysis of Variance

In this example, the same model and data is fit as in the example 1, but additional information is printed.


import java.text.*;
import com.imsl.stat.*;

public class ANOVAFactorialEx2 {

    public static void main(String args[]) {
        int nSubscripts = 3, i;
        int[] nLevels = {3, 2, 10};
        double[] y = {
            73.0, 102.0, 118.0, 104.0, 81.0, 107.0, 100.0, 87.0, 117.0, 111.0,
            90.0, 76.0, 90.0, 64.0, 86.0, 51.0, 72.0, 90.0, 95.0, 78.0,
            98.0, 74.0, 56.0, 111.0, 95.0, 88.0, 82.0, 77.0, 86.0, 92.0,
            107.0, 95.0, 97.0, 80.0, 98.0, 74.0, 74.0, 67.0, 89.0, 58.0,
            94.0, 79.0, 96.0, 98.0, 102.0, 102.0, 108.0, 91.0, 120.0, 105.0,
            49.0, 82.0, 73.0, 86.0, 81.0, 97.0, 106.0, 70.0, 61.0, 82.0
        };
        String[] labels = {
            "degrees of freedom for the model                 ",
            "degrees of freedom for error                    ",
            "total (corrected) degrees of freedom            ",
            "sum of squares for the model                 ",
            "sum of squares for error                    ",
            "total (corrected) sum of squares            ",
            "model mean square                              ",
            "error mean square                              ",
            "F-statistic                                      ",
            "p-value                                          ",
            "R-squared (in percent)                          ",
            "Adjusted R-squared (in percent)                 ",
            "est. standard deviation of the model error      ",
            "overall mean of y                               ",
            "coefficient of variation (in percent)           "
        };
        String[] rlabels = {"A", "B", "A*B"};
        String[] mlabels = {
            "grand mean     ", "A1             ", "A2             ",
            "A3             ", "B1             ", "B2             ",
            "A1*B1         ", "A1*B2          ", "A2*B1          ",
            "A2*B2          ", "A3*B1          ", "A3*B2          "
        };
        NumberFormat nf = NumberFormat.getInstance();
        ANOVAFactorial af = new ANOVAFactorial(nSubscripts, nLevels, y);

        nf.setMinimumFractionDigits(6);
        System.out.println("P-value = " + nf.format(af.compute()));

        nf.setMaximumFractionDigits(4);

        System.out.println("\n          * * * Analysis of Variance * * *");
        double[] anova = af.getANOVATable();
        for (i = 0; i < anova.length; i++) {
            System.out.println(labels[i] + " " + nf.format(anova[i]));
        }

        System.out.println("\n          * * * Variation Due to the "
                + "Model * * *");
        System.out.println("Source\tDF\tSum of Squares\tMean Square"
                + "\tProb. of Larger F");
        double[][] te = af.getTestEffects();
        for (i = 0; i < te.length; i++) {
            System.out.println(rlabels[i] + "\t" + nf.format(te[i][0]) + "\t"
                    + nf.format(te[i][1]) + "\t" + nf.format(te[i][2]) + "\t\t"
                    + nf.format(te[i][3]));
        }

        System.out.println("\n* * * Subgroup Means * * *");
        double[] means = af.getMeans();
        for (i = 0; i < means.length; i++) {
            System.out.println(mlabels[i] + " " + nf.format(means[i]));
        }
    }
}

Output

P-value = 0.002299

          * * * Analysis of Variance * * *
degrees of freedom for the model                  5.0000
degrees of freedom for error                     54.0000
total (corrected) degrees of freedom             59.0000
sum of squares for the model                  4,612.9333
sum of squares for error                     11,586.0000
total (corrected) sum of squares             16,198.9333
model mean square                               922.5867
error mean square                               214.5556
F-statistic                                       4.3000
p-value                                           0.0023
R-squared (in percent)                           28.4768
Adjusted R-squared (in percent)                  21.8543
est. standard deviation of the model error       14.6477
overall mean of y                                87.8667
coefficient of variation (in percent)            16.6704

          * * * Variation Due to the Model * * *
Source	DF	Sum of Squares	Mean Square	Prob. of Larger F
A	2.0000	266.5333	0.6211		0.5411
B	1.0000	3,168.2667	14.7666		0.0003
A*B	2.0000	1,178.1333	2.7455		0.0732

* * * Subgroup Means * * *
grand mean      87.8667
A1              89.6000
A2              84.9000
A3              89.1000
B1              95.1333
B2              80.6000
A1*B1          100.0000
A1*B2           79.2000
A2*B1           85.9000
A2*B2           83.9000
A3*B1           99.5000
A3*B2           78.7000
Link to Java source.