Example 1: One-way analysis of covariance model

This example fits a one-way analysis of covariance model assuming parallelism using data discussed by Snedecor and Cochran (Table 14.6.1, pages 432-436). The responses are concentrations of cholesterol (in mg/100 ml) in the blood of two groups of women: women from Iowa and women from Nebraska. The age of a woman is the single covariate. The cholesterol concentrations and ages of the women according to state are shown in the following table. (There are 11 Iowa women and 19 Nebraska women in the study. Only the first 5 women from each state are shown here.)

Iowa Nebraska
Age Cholesterol Age Cholesterol
46 181 18 137
52 228 44 173
39 182 33 177
65 249 78 241
54 259 51 225

There is no evidence from the data to indicate that the regression lines for cholesterol concentration as a function of age are not parallel for Iowa and Nebraska women (p-value is 0.5425). The parallel line model suggests that Nebraska women may have higher cholesterol concentrations than Iowa women. The cholesterol concentrations (adjusted for age) are 195.5 for Iowa women versus 224.2 for Nebraska women. The difference is 28.7 with an estimated standard error of

\sqrt{170.4 + 97.4 - 2(2.9)}=16.1
using System;
using Imsl.Stat;
using Imsl.Math;

public class ANCOVAEx1
{
    public static void  Main(String[] args)
    {
        
        double[][] y = new double[][]{
            new double[]{181.0, 228.0, 182.0, 249.0, 259.0, 201.0, 121.0,
                            339.0, 224.0, 112.0, 189.0},
            new double[]{137.0, 173.0, 177.0, 241.0, 225.0, 223.0, 190.0,
                            257.0, 337.0, 189.0, 214.0, 140.0, 196.0, 262.0,
                            261.0, 356.0, 159.0, 191.0, 197.0}};
        double[][][] x = new double[1][][];
        for (int i = 0; i < 1; i++)
        {
            x[i] = new double[2][];
        }
        x[0][0] = new double[]{46.0, 52.0, 39.0, 65.0, 54.0, 33.0, 49.0, 76.0,
                                  71.0, 41.0, 58.0};
        x[0][1] = new double[]{18.0, 44.0, 33.0, 78.0, 51.0, 43.0, 44.0, 58.0,
                                  63.0, 19.0, 42.0, 30.0, 47.0, 58.0, 70.0,
                                  67.0, 31.0, 21.0, 56.0};
        ANCOVA awc = new ANCOVA(y, x);
        
        double[] testpl = awc.Compute();
        double[] aov = awc.GetANCOVA();
        
        Console.WriteLine("             * * * Analysis of Variance * * *\n");
        Console.WriteLine("                  Sum of         Mean            " +
            "    Prob of");
        Console.WriteLine("Source   DF      Squares        Square    " +
            "Overall F  Larger F");
        Console.WriteLine("Model  {0,3:0.#}     {1,10:0.##}      {2,9:0.##}" +
            "  {3,2:0.##}      {4,8:0.######}", aov[0], aov[3], aov[6], aov[8],
            aov[9]);
        Console.WriteLine("Error   {0,3:0.#}    {1,10:0.##}     {2,9:0.##} ",
            aov[1],    aov[4], aov[7]);
        Console.WriteLine("Total   {0,3:0.#}    {1,10:0.##} \n\n", aov[2],
            aov[5]);
        Console.WriteLine("           * * * Test for Parallelism  * * *\n");
        Console.WriteLine("                       Sum of     Mean     F     " +
            " Prob of");
        Console.WriteLine("Source           DF    Squares   Square   Test   " +
            " Larger F");
        Console.WriteLine("Extra due to");
        Console.WriteLine("Nonparallelism {0,3:0.#} {1,10:0.##}   {2,7:0.##}" +
            "   {3,7:0.#####} {4,5:0.####}", testpl[0], testpl[3], testpl[6],
            testpl[8], testpl[9]);
        Console.WriteLine("Extra Assuming");
        Console.WriteLine("Nonparallelism  {0,3:0.#} {1,10:0.##}  {2,7:0.##} ",
            testpl[1], testpl[4], testpl[7]);
        Console.WriteLine("Error Assuming");
        Console.WriteLine("Parallelism     {0,3:0.#} {1,10:0.##} \n\n",
            testpl[2], testpl[5]);
        PrintMatrixFormat pmf = new Imsl.Math.PrintMatrixFormat();
        pmf.NumberFormat = "0.###";
        new PrintMatrix("XY Mean Matrix\n").Print(pmf, awc.GetMeans());
        new PrintMatrix("\n\nVar./Covar. Matrix of Adjusted Group Means" +
            "\n").Print(pmf, awc.GetVarCovAdjustedMeans());
    }
}

Output

             * * * Analysis of Variance * * *

                  Sum of         Mean                Prob of
Source   DF      Squares        Square    Overall F  Larger F
Model    2       54432.75       27216.38  14.97      0.000042
Error    27      49103.91       1818.66 
Total    29     103536.67 


           * * * Test for Parallelism  * * *

                       Sum of     Mean     F      Prob of
Source           DF    Squares   Square   Test    Larger F
Extra due to
Nonparallelism   1     709.05    709.05   0.38093 0.5425
Extra Assuming
Nonparallelism   26   48394.86  1861.34 
Error Assuming
Parallelism      27   49103.91 


         XY Mean Matrix

   0     1        2        3     
0  11  53.091  207.727  195.521  
1  19  45.947  217.105  224.172  
2  30  48.567  213.667  213.667  



Var./Covar. Matrix of Adjusted Group Means

      0         1     
0   170.368  -2.915   
1  -2.915     97.407  


Link to C# source.