Example 2: One-way analysis of covariance model

This example fits a one-way analysis of covariance model and performs a test for parallelism using data discussed by Snedecor and Cochran (1967, Table 14.8.1, pages 438-443). The responses are weight gains (in pounds per day) of 40 pigs for four groups of pigs under varying treatments. Two covariates-initial age (in days) and initial weight (in pounds) are used. For each treatment, there are 10 pigs. Only the first 5 pigs from each treatment are shown here.

Treatment 1 Treatment 2 Treatment 3 Treatment 4
Age Wt. Gain Age Wt. Gain Age Wt. Gain Age Wt. Gain
78 61 1.4 78 74 1.61 78 80 1.67 77 62 1.4
90 59 1.79 99 75 1.31 83 61 1.41 71 55 1.47
94 76 1.72 80 64 1.12 79 62 1.73 78 62 1.37
71 50 1.47 75 48 1.35 70 47 1.23 70 43 1.15
99 61 1.26 94 62 1.29 85 59 1.49 95 57 1.22

For these data, the test for non-parallelism is not statistically significant (p = 0.901). The one-way analysis of covariance test for the treatment means adjusted for the covariates, assuming parallel slopes, is statistically significant at a stated significance level of \alpha = 0.05, (p = 0.04931). Multiple comparisons can be done using the least significant difference approach of comparing each pair of treatment groups with the two-sample student-t test. Since the adjusted means in the one-way analysis of covariance are correlated, the standard error for these comparisons must be computed using the variances and covariances in covm. The standard errors for these comparisons are fairly similar ranging from 0.0630 to 0.0638. The Student's t comparisons identify differences between groups 1 and 2, and 1 and 4 as being statistically significant with p-values of 0.01225 and 0.03854 respectively.


import com.imsl.stat.ANCOVA;

public class ANCOVAEx2 {

    public static void main(String[] args) {
        int j;
        int ngroup = 4;
        double x1[][] = {
            {78.0, 90.0, 94.0, 71.0, 99.0, 80.0, 83.0, 75.0, 62.0, 67.0},
            {78.0, 99.0, 80.0, 75.0, 94.0, 91.0, 75.0, 63.0, 62.0, 67.0},
            {78.0, 83.0, 79.0, 70.0, 85.0, 83.0, 71.0, 66.0, 67.0, 67.0},
            {77.0, 71.0, 78.0, 70.0, 95.0, 96.0, 71.0, 63.0, 62.0, 67.0}
        };
        double x2[][] = {
            {61.0, 59.0, 76.0, 50.0, 61.0, 54.0, 57.0, 45.0, 41.0, 40.0},
            {74.0, 75.0, 64.0, 48.0, 62.0, 42.0, 52.0, 43.0, 50.0, 40.0},
            {80.0, 61.0, 62.0, 47.0, 59.0, 42.0, 47.0, 42.0, 40.0, 40.0},
            {62.0, 55.0, 62.0, 43.0, 57.0, 51.0, 41.0, 40.0, 45.0, 39.0}
        };
        double y[][] = {
            {1.40, 1.79, 1.72, 1.47, 1.26, 1.28, 1.34, 1.55, 1.57, 1.26},
            {1.61, 1.31, 1.12, 1.35, 1.29, 1.24, 1.29, 1.43, 1.29, 1.26},
            {1.67, 1.41, 1.73, 1.23, 1.49, 1.22, 1.39, 1.39, 1.56, 1.36},
            {1.40, 1.47, 1.37, 1.15, 1.22, 1.48, 1.31, 1.27, 1.22, 1.36}
        };
        double x[][][] = {x1, x2};
        /* setup covariate input matrix */

        ANCOVA awc = new ANCOVA(y, x);

        double testpl[] = awc.compute();
        double aov[] = awc.getANCOVA();
        double adjAov[] = awc.getAdjustedANOVA();
        double xymean[][] = awc.getMeans();
        double covm[][] = awc.getVarCovAdjustedMeans();

        System.out.println("\n           * * * Test for Parallelism  * * *\n");
        System.out.println("                       Sum of     Mean         "
                + "F     Prob of");
        System.out.println("Source           DF    Squares   Square       "
                + "Test   Larger F");
        System.out.println("Extra due to");
        System.out.printf("Nonparallelism %3.0f %10.2f    %7.2f    %7.5f   "
                + "%5.3f\n", testpl[0], testpl[3], testpl[6], testpl[8],
                testpl[9]);
        System.out.println("Extra Assuming");
        System.out.printf("Nonparallelism %3.0f %10.2f    %7.2f\n",
                testpl[1], testpl[4], testpl[7]);
        System.out.println("Error Assuming");
        System.out.printf("Parallelism    %3.0f %10.2f   \n\n\n", testpl[2],
                testpl[5]);
        System.out.println("             * * * Analysis of Variance * * *\n");
        System.out.println("                  Sum of         "
                + "Mean                Prob of");
        System.out.println("Source   DF      Squares        Square    "
                + "Overall F  Larger F");
        System.out.printf("Model   %3.0f      %f     %f    %f    %5.4f\n",
                aov[0], aov[3], aov[6], aov[8], aov[9]);
        System.out.printf("Error   %3.0f      %f     %f      \n", aov[1],
                aov[4], aov[7]);
        System.out.printf("Total   %3.0f      %f  \n\n\n", aov[2], aov[5]);
        System.out.println("         * * * Adjusted Analysis of Variance  * "
                + "* *\n");
        System.out.println("                                Sum of        "
                + "F     Prob of");
        System.out.println("Source                    DF    Squares      "
                + "Test   Larger F");
        System.out.printf("Groups after Covariates %3.0f   %10.2f    %5.2f   "
                + " %7.5f\n", adjAov[0], adjAov[2], adjAov[4], adjAov[6]);
        System.out.printf("Covariates after Groups %3.0f   %10.2f    %5.2f   "
                + " %7.5f\n\n\n", adjAov[1], adjAov[3], adjAov[5], adjAov[7]);
        System.out.println("           * * * Group Means * * *\n");
        System.out.println("GROUP  | Unadjusted   |  Adjusted |  Std. Error");
        for (int i = 0; i < ngroup; i++) {
            double se = Math.sqrt(covm[i][i]);
            System.out.printf("  %d    |   %5.4f     |   %5.4f  |   %7.4f\n",
                    i + 1, xymean[i][ngroup - 1], xymean[i][ngroup], se);
        }
        System.out.println("\n\n      * * * Student-t Multiple Comparisons * "
                + "* *\n");
        System.out.println(" Groups  |    Diff   | Std. Error | Student-t | "
                + "P-Value");
        for (int i = 0; i < ngroup - 1; i++) {
            for (j = i + 1; j < ngroup; j++) {
                double delta = xymean[i][ngroup] - xymean[j][ngroup];
                double se = Math.sqrt(covm[i][i] + covm[j][j] - 2.0
                        * covm[i][j]);
                double t = delta / se;
                double df = xymean[i][0] + xymean[j][0] - 2;
                double pvalue = 1.0 - com.imsl.stat.Cdf.studentsT(t, df);
                System.out.printf(" %d vs %d  |  %7.4f  |  %7.4f   | %7.3f   "
                        + "| %7.5f\n", i + 1, j + 1, delta, se, t, pvalue);
            }
        }
    }
}

Output


           * * * Test for Parallelism  * * *

                       Sum of     Mean         F     Prob of
Source           DF    Squares   Square       Test   Larger F
Extra due to
Nonparallelism   6       0.05       0.01    0.35534   0.901
Extra Assuming
Nonparallelism  28       0.62       0.02
Error Assuming
Parallelism     34       0.67   


             * * * Analysis of Variance * * *

                  Sum of         Mean                Prob of
Source   DF      Squares        Square    Overall F  Larger F
Model     5      0.352517     0.070503    3.576390    0.0105
Error    34      0.670261     0.019714      
Total    39      1.022777  


         * * * Adjusted Analysis of Variance  * * *

                                Sum of        F     Prob of
Source                    DF    Squares      Test   Larger F
Groups after Covariates   3         0.17     2.90    0.04931
Covariates after Groups   2         0.17     4.44    0.01939


           * * * Group Means * * *

GROUP  | Unadjusted   |  Adjusted |  Std. Error
  1    |   1.4640     |   1.4614  |    0.0448
  2    |   1.3190     |   1.3068  |    0.0446
  3    |   1.4450     |   1.4429  |    0.0447
  4    |   1.3250     |   1.3418  |    0.0449


      * * * Student-t Multiple Comparisons * * *

 Groups  |    Diff   | Std. Error | Student-t | P-Value
 1 vs 2  |   0.1546  |   0.0630   |   2.455   | 0.01225
 1 vs 3  |   0.0185  |   0.0637   |   0.290   | 0.38750
 1 vs 4  |   0.1196  |   0.0638   |   1.875   | 0.03854
 2 vs 3  |  -0.1362  |   0.0632   |  -2.153   | 0.97743
 2 vs 4  |  -0.0350  |   0.0638   |  -0.549   | 0.70528
 3 vs 4  |   0.1011  |   0.0631   |   1.602   | 0.06330
Link to Java source.