/* Simple program for Analysis of Variance (ANOVA) */ #include #include #define MAX_ROWS 10 #define MAX_COLS 10 main() { float value; float observations[MAX_ROWS][MAX_COLS]; float row_means[MAX_ROWS]; float col_means[MAX_ROWS]; float row_effects[MAX_ROWS]; float col_effects[MAX_ROWS]; float sum, grand_mean; float sst, ssa, ssb, sse; int i, j, rows, cols, numobservations; /* Find out size of input data */ scanf("%d %d", &rows, &cols); if( rows > MAX_ROWS ) { printf("Too many rows of data!!\n"); exit(0); } if( cols > MAX_COLS ) { printf("Too many columns of data!!\n"); exit(0); } /* Read in data values and echo them to user */ printf("Starting ANOVA of the following data...\n"); sum = 0; numobservations = 0; for( i = 0; i < rows; i++ ) { for( j = 0; j < cols; j++ ) { scanf("%f", &value); printf("%8.3f ", value); observations[i][j] = value; sum += value; numobservations++; } printf("\n"); } /* Compute the grand mean */ grand_mean = sum / numobservations; printf(" Grand Mean: %8.3f\n", grand_mean); /* Compute row means and row effects */ for( i = 0; i < rows; i++ ) { sum = 0; for( j = 0; j < cols; j++ ) { sum += observations[i][j]; } row_means[i] = sum / cols; printf(" Row %d mean: %8.3f\n", i, row_means[i]); row_effects[i] = row_means[i] - grand_mean; printf(" Row %d effect: %8.3f\n", i, row_effects[i]); } /* Compute column means and column effects */ for( j = 0; j < cols; j++ ) { sum = 0; for( i = 0; i < rows; i++ ) { sum += observations[i][j]; } col_means[j] = sum / rows; printf(" Column %d mean: %8.3f\n", j, col_means[j]); col_effects[j] = col_means[j] - grand_mean; printf(" Column %d effect: %8.3f\n", j, col_effects[j]); } /* Compute SST */ sst = 0; for( i = 0; i < rows; i++ ) { for( j = 0; j < cols; j++ ) { sst += (grand_mean - observations[i][j]) * (grand_mean - observations[i][j]); } } printf(" SST: %8.3f\n", sst); /* Compute SSA for rows */ ssa = 0.0; for( i = 0; i < rows; i++ ) { ssa += row_effects[i] * row_effects[i]; } ssa *= cols; printf(" SSA: %8.3f\n", ssa); /* Compute SSB for columns */ ssb = 0.0; for( j = 0; j < cols; j++ ) { ssb += col_effects[j] * col_effects[j]; } ssb *= rows; printf(" SSB: %8.3f\n", ssb); /* Compute SSE */ sse = sst - ssa - ssb; printf(" SSE: %8.3f\n", sse); /* Compute SSA/SST and SSB/SST */ printf("Factor A (rows) has SSA/SST = %8.3f%%\n", ssa * 100.0 / sst); printf("Factor B (columns) has SSB/SST = %8.3f%%\n", ssb * 100.0 / sst); printf("Error has SSE/SST = %8.3f%%\n", sse * 100.0 / sst); }