Microsoft KB Archive/829215

= Description of numeric improvement in Analysis ToolPak ANOVA tools in Excel =

Article ID: 829215

Article Last Modified on 1/10/2007

-

APPLIES TO


 * Microsoft Office Excel 2007
 * Microsoft Office Excel 2003

-



SUMMARY
This article describes numeric improvements in each of the three Analysis ToolPak ANOVA tools. This article also illustrates inaccurate results in Microsoft Excel 2002 and in earlier versions of Excel in extreme situations.



MORE INFORMATION
Many functions require the calculation of the sum of squared deviations about a mean. To do this accurately, Microsoft Office Excel 2003 and later versions of Excel use a two-pass procedure that finds the mean on the first pass, and then calculates squared deviations about the mean on the second pass.

In precise arithmetic, the same result occurs in earlier versions of Excel that use the &quot;calculator formula.&quot; This formula is so named because it was in widespread use when statisticians used calculators instead of computers. With the calculator formula, earlier versions of Excel sum the squares of the observations, and then subtract from this total the following quantity: ((sum of observations)^2) / number of observations This calculation occurs in a single pass through the data.

In finite precision arithmetic, the calculator formula is subject to roundoff errors in extreme cases. Excel 2002 and earlier versions of Excel use the calculator formula for most functions that require a sum of squared deviations about a mean (such as VAR, STDEV, SLOPE, and PEARSON). However, these versions of Excel also use the more numerically robust two-pass procedure for the CORREL, COVAR, and DEVSQ functions.

Experts in statistical computing recommend that you do not use the calculator formula. The calculator formula is presented as &quot;how not to do it&quot; in texts about statistical computing. Unfortunately, all three of the Analysis ToolPak (ATP) ANOVA tools make widespread use of the calculator formula or an equivalent single-pass approach in Excel 2002 and in earlier versions of Excel.

Excel 2003 and later versions of Excel use the two-pass procedure for all three ATP ANOVA models. This article discusses the following computational improvements in ATP's three ANOVA models:
 * Single Factor
 * Two-Factor with Replication
 * Two-Factor without Replication

This article discusses these models later.

Because Excel has always used the two-pass procedure with DEVSQ, this article makes frequent use of it to describe the improved procedures. These revised procedures either effectively call DEVSQ or use code whose functionality is exactly the same as DEVSQ's functionality.

For each ANOVA tool, ATP output contains a Summary table with values of Count, Sum, Average, and Variance, and an ANOVA table that has various sums of squares and values of SS, df, MS, F and P-value. Results in the summary table are calculated by calling Excel functions COUNT, SUM, AVERAGE, and VAR. Of these four functions, only VAR is subject to roundoff errors.

Excel 2002 and earlier versions of Excel implement VAR by using the calculator formula. The following article about VAR describes the improvements that occurred in Excel 2003 and in later versions of Excel. This article also lets you experiment with numeric data to see when roundoff errors are likely to occur in earlier versions of Excel.

For more information about VAR, click the following article number to view the article in the Microsoft Knowledge Base:

826112 Excel statistical functions: VAR

As this article discusses the three ANOVA models, it focuses on the ANOVA output tables. In each case, the Summary tables are well-behaved in Excel 2003 and in later versions of Excel. In Excel 2002 and in earlier versions of Excel, problems occur in the Variance column when data have extreme values.

However, this article includes the Summary tables in the model sections because these tables are useful for comparison when you review the modified examples in the Appendix.

Model 1: Single Factor
A simple example with data is as follows.

Excel 2002 and earlier versions of Excel use the following pseudocode to calculate the sums of squares: GrandSum = 0; GrandSumOfSqs = 0; GrandSampleMeanSqrd = 0; GrandMeanSqrd = 0; GrandSampleSize = 0;

For s = 1 to Number_of_Samples do  GrandSum = GrandSum + sum of observations in s-th sample; GrandSumOfSqs = GrandSumOfSqs + sum of squared observations in s-th sample; GrandSampleMeanSqrd = GrandSampleMeanSqrd + (sum of observations in s-th sample^2)/size of s-th sample; GrandSampleSize = GrandSampleSize + size of s-th sample Endfor;

GrandMeanSqrd = (GrandSum^2) / GrandSampleSize;

TotalSS = GrandSumOfSqs – GrandMeanSqrd; BetweenGroupsSS = GrandSampleMeanSqrd – GrandMeanSqrd; WithinGroupsSS = GrandSumOfSqs – GrandSampleMeanSqrd; This approach is essentially the calculator formula. This approach computes the sums of squares of observations, and then subtracts a quantity from them, just as VAR computes the sum of squares of the observations, and then subtracts ^2/. Similar pseudocode for the model 2 and model 3 has been omitted.

Again, for model 2 and model 3, sums of squares are calculated and a quantity is subtracted from the sum of squares as in the calculator formula. Unfortunately, basic statistics texts frequently suggest approaches for ANOVA such as the one that is shown earlier in this article.

Excel 2003 and later versions of Excel use a different approach to calculate the various entries in the SS column of the ANOVA table. For illustration, this article assumes that the numeric data in the earlier example appear in cells A2:C7 with missing data in cells B6 and B7.
 * Total SS is just DEVSQ applied to all the data, such as DEVSQ(A2:C7). DEVSQ works correctly even though data is missing.
 * Between Groups SS is Total SS minus the sum of DEVSQ applied to each column, such as DEVSQ(A2:A7) + DEVSQ(B2:B7) + DEVSQ(C2:C7).
 * Within Groups SS is Total SS minus Between Groups SS.

If entries in the SS column of the ANOVA table are calculated correctly, the accuracy of the other entries in the table follow.

Model 2: Two-Factor with Replication
A simple example with data is as follows.

Again, if entries in the SS column are calculated correctly, the accuracy of all the other entries in the ANOVA part of the output follows.

Here is the computational procedure for Excel 2003 and for later versions of Excel. This procedure uses DEVSQ to calculate the various entries in the SS column of the ANOVA table. For illustration, this example assumes that the numeric data appear in cells B2:D7.
 * Total SS is just DEVSQ applied to all the data, such as DEVSQ(B2:D7).
 * Sample SS is Total SS minus the sum of DEVSQ applied to each sample, such as DEVSQ(B2:D4) + DEVSQ(B5:D7).
 * Columns SS is Total SS minus the sum of DEVSQ applied to each column, such as DEVSQ(B2:B7) + DEVSQ(C2:C7) + DEVSQ(D2:D7).
 * Within SS is the sum of DEVSQ applied to each trial or group pair, such as DEVSQ(B2:B4) + DEVSQ(C2:C4) + DEVSQ(D2:D4) + DEVSQ(B5:B7) + DEVSQ(C5:C7) + DEVSQ(D5:D7).
 * Interaction SS equals Total SS minus Sample SS minus Columns SS minus Within SS.

Model 3: Two-Factor without Replication
A simple example with data is as follows.

If the values in the SS column are calculated correctly, the accuracy of all the other values in the ANOVA table follows.

Excel 2003 and later versions of Excel use the following computational procedure. The procedure uses DEVSQ to calculate the values in the SS column of the ANOVA table. For illustration, this example assumes that the range of cells shown in the earlier example is cells A1:D10. Therefore, the numeric data appear in cells B2:D10.
 * Total SS is just DEVSQ applied to all the data, such as DEVSQ(B2:D10).
 * Rows SS is Total SS minus the sum of DEVSQ applied to each row, such as DEVSQ(B2:D2) + DEVSQ(B3:D3) + DEVSQ(B4:D4) + DEVSQ(B5:D5) + DEVSQ(B6:D6) + DEVSQ(B7:D7) + DEVSQ(B8:D8) + DEVSQ(B9:D9) + DEVSQ(B10:D10).
 * Columns SS is Total SS minus the sum of DEVSQ applied to each column, such as DEVSQ(B2:B10) + DEVSQ(C2:C10) + DEVSQ(D2:D10).
 * Error SS is Total SS minus Rows SS minus Columns SS.

Results in Excel 2002 and in earlier versions of Excel
In extreme cases where there are many significant digits in the data but also a small variance, the calculator formula leads to inaccurate results. The Appendix that appears later in this article gives examples of roundoff problems in such extreme situations.

Results in Excel 2003 and in later versions of Excel
Excel 2003 and later versions of Excel use a procedure that makes two passes through the data. On the first pass, Excel 2003 and later versions of Excel calculate the sum and count of the data values. From these, Excel can calculate the sample mean (average).

On the second pass, Excel calculates the squared difference between each data point and the sample mean, and then sums these squared differences. As a result, the results in Excel 2003 and in later versions of Excel are more stable numerically.

Conclusions
A two-pass approach improves the numeric performance in all three ATP ANOVA tools in Excel 2003 and in later versions of Excel as compared to earlier versions of Excel. The results that you obtain by using Excel 2003 and later versions of Excel are never less accurate than the results that you obtain by using earlier versions of Excel.

In most practical cases, however, there is no difference between these results. This is because data do not typically exhibit the kind of unusual behavior that the following Appendix illustrates. Numeric instability is most likely to occur in earlier versions of Excel when data contains a high number of significant digits with relatively little variation between data values.

If you use an earlier version of Excel, and if you want to see whether Excel 2003 or a later version of Excel gives you different ANOVA results, compare the results that you obtain when you use the ANOVA tools in your earlier version of Excel with the results that you obtain when you use the procedures that use DEVSQ.

Note The procedures that use DEVSQ were described earlier in this article for the ANOVA table that is associated with each of the tools.

To verify that Variances are correct in the Summary table for each range, use DEVSQ/(COUNT – 1).

Appendix: Numeric examples of the performance of Excel 2002 and earlier versions of Excel
For each basic example from models 1, 2, and 3, this article previously presented the ATP tool's output. This included the Summary and ANOVA tables. Data was modified in each example to create a &quot;stressed&quot; example. This is done by adding 10^8 to each data value. Adding a constant such as 10^8 to each data value does not affect Variance in the Summary table (but will affect Average and Sum in obvious ways). It should also not affect any entry in the ANOVA table.

If you compare Variances in the Summary tables and SS in the ANOVA tables, you will notice that all of these are incorrectly calculated in all three of the following stressed models except for one entry in model 3 that is pointed to with &quot;<---&quot;.

In all the stressed cases, the ANOVA results that you obtain by using Excel 2003 and later versions of Excel agree with the earlier results in the basic cases (as they should).

ANOVA 3 stressed model with large data values
Keywords: kbexpertisebeginner kbfunctions kbprogramming kbfuncstat kbinfo KB829215

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.