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 , (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.
using System; using Imsl.Stat; using Imsl.Math; public class ANCOVAEx2 { public static void Main(String[] args) { int j; int ngroup = 4; double[][] x1 = new double[][]{ new double[]{78.0, 90.0, 94.0, 71.0, 99.0, 80.0, 83.0, 75.0, 62.0, 67.0}, new double[]{78.0, 99.0, 80.0, 75.0, 94.0, 91.0, 75.0, 63.0, 62.0, 67.0}, new double[]{78.0, 83.0, 79.0, 70.0, 85.0, 83.0, 71.0, 66.0, 67.0, 67.0}, new double[]{77.0, 71.0, 78.0, 70.0, 95.0, 96.0, 71.0, 63.0, 62.0, 67.0}}; double[][] x2 = new double[][]{ new double[]{61.0, 59.0, 76.0, 50.0, 61.0, 54.0, 57.0, 45.0, 41.0, 40.0}, new double[]{74.0, 75.0, 64.0, 48.0, 62.0, 42.0, 52.0, 43.0, 50.0, 40.0}, new double[]{80.0, 61.0, 62.0, 47.0, 59.0, 42.0, 47.0, 42.0, 40.0, 40.0}, new double[]{62.0, 55.0, 62.0, 43.0, 57.0, 51.0, 41.0, 40.0, 45.0, 39.0}}; double[][] y = new double[][]{ new double[]{1.40, 1.79, 1.72, 1.47, 1.26, 1.28, 1.34, 1.55, 1.57, 1.26}, new double[]{1.61, 1.31, 1.12, 1.35, 1.29, 1.24, 1.29, 1.43, 1.29, 1.26}, new double[]{1.67, 1.41, 1.73, 1.23, 1.49, 1.22, 1.39, 1.39, 1.56, 1.36}, new double[]{1.40, 1.47, 1.37, 1.15, 1.22, 1.48, 1.31, 1.27, 1.22, 1.36}}; double[][][] x = new double[][][]{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(); Console.WriteLine("\n * * * 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]); 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,7:0.######} {2,7:0.######} {3,7:0.######} " + "{4,5:0.####}", aov[0], aov[3], aov[6], aov[8], aov[9]); Console.WriteLine("Error {0,3:0.#} {1,7:0.######} {2,7:0.######} ", aov[1], aov[4], aov[7]); Console.WriteLine("Total {0,3:0.#} {1,7:0.######} \n\n", aov[2], aov[5]); Console.WriteLine(" * * * Adjusted Analysis of Variance * " + "* *\n"); Console.WriteLine(" Sum of " + "F Prob of"); Console.WriteLine("Source DF Squares " + "Test Larger F"); Console.WriteLine("Groups after Covariates {0,3:0.#} {1,10:0.##}" + " {2,5:0.##} " + " {3,7:0.#####}", adjAov[0], adjAov[2], adjAov[4], adjAov[6]); Console.WriteLine("Covariates after Groups {0,3:0.#} {1,10:0.##}" + " {2,5:0.##} " + " {3,7:0.#####}\n\n", adjAov[1], adjAov[3], adjAov[5], adjAov[7]); Console.WriteLine(" * * * Group Means * * *\n"); Console.WriteLine("GROUP | Unadjusted | Adjusted | Std. Error"); for (int i = 0; i < ngroup; i++) { double se = Math.Sqrt(covm[i][i]); Console.WriteLine(" {0} | {1,5:0.####} | {2,5:0.####} " + "| {3,7:0.####}", i + 1, xymean[i][ngroup - 1], xymean[i][ngroup], se); } Console.WriteLine("\n\n * * * Student-t Multiple Comparisons * " + "* *\n"); Console.WriteLine(" 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 - Cdf.StudentsT(t, df); Console.WriteLine(" {0} vs {1} | {2,7:0.####} | " + "{3,7:0.####} | {4,7:0.####} " + "| {5,7:0.#####}", i + 1, j + 1, delta, se, t, pvalue); } } } }
* * * 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.57639 0.0105 Error 34 0.670261 0.019714 Total 39 1.022778 * * * Adjusted Analysis of Variance * * * Sum of F Prob of Source DF Squares Test Larger F Groups after Covariates 3 0.17 2.9 0.04931 Covariates after Groups 2 0.17 4.44 0.01939 * * * Group Means * * * GROUP | Unadjusted | Adjusted | Std. Error 1 | 1.464 | 1.4614 | 0.0448 2 | 1.319 | 1.3068 | 0.0446 3 | 1.445 | 1.4429 | 0.0447 4 | 1.325 | 1.3418 | 0.0449 * * * Student-t Multiple Comparisons * * * Groups | Diff | Std. Error | Student-t | P-Value 1 vs 2 | 0.1546 | 0.063 | 2.4548 | 0.01225 1 vs 3 | 0.0185 | 0.0637 | 0.2902 | 0.3875 1 vs 4 | 0.1196 | 0.0638 | 1.8752 | 0.03854 2 vs 3 | -0.1362 | 0.0632 | -2.1528 | 0.97743 2 vs 4 | -0.035 | 0.0638 | -0.5495 | 0.70528 3 vs 4 | 0.1011 | 0.0631 | 1.6018 | 0.0633Link to C# source.