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.

using System;
using Imsl.Stat;

public class ANOVAFactorialEx2
{
	public static void  Main(String[] args)
	{
		int nSubscripts = 3, i;
		int[] nLevels = new int[]{3, 2, 10};
		double[] y = new double[]{   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 = 
			new String[]{"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 = new String[]{"A", "B", "A*B"};
		String[] mlabels = new String[]{"grand mean     ", 
            "A1             ", "A2             ", "A3             ",
            "B1             ", "B2             ", "A1*B1         ",
			"A1*B2          ", "A2*B1          ", "A2*B2          ",
			"A3*B1          ", "A3*B2          "};

		ANOVAFactorial af = 
			new ANOVAFactorial(nSubscripts, nLevels, y);

		Console.Out.WriteLine
			("P-value = " + af.Compute().ToString("0.000000"));
		
		Console.Out.WriteLine
			("\n          * * * Analysis of Variance * * *");
		double[] anova = af.GetANOVATable();
		for (i = 0; i < anova.Length; i++)
		{
			Console.Out.WriteLine
				(labels[i] + " " + anova[i].ToString("0.0000"));
		}
		
		Console.Out.WriteLine
			("\n          * * * Variation Due to the " + "Model * * *");
		Console.Out.WriteLine
			("Source\tDF\tSum of Squares\tMean Square" + 
             "\tProb. of Larger F");
		double[,] te = af.GetTestEffects();
		for (i = 0; i < te.GetLength(0); i++)
		{
			Console.Out.WriteLine(
				rlabels[i] + "\t" +
				te[i,0].ToString("0.0000") + "\t" +
				te[i,1].ToString("0.0000") + "\t" +
				te[i,2].ToString("0.0000") + "\t\t" +
				te[i,3].ToString("0.0000"));
		}
		
		Console.Out.WriteLine("\n* * * Subgroup Means * * *");
		double[] means = af.GetMeans();
		for (i = 0; i < means.Length; i++)
		{
			Console.Out.WriteLine
				(mlabels[i] + " " + means[i].ToString("0.0000"));
		}
	}
}

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                  4612.9333
sum of squares for error                     11586.0000
total (corrected) sum of squares             16198.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	3168.2667	14.7666		0.0003
A*B	2.0000	1178.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 C# source.