Tutorials › ANOVA › An Unequal-Group ANOVA Calculation

Sums of Squares and F Tests · Tutorial 41 of 595

An Unequal-Group ANOVA Calculation

Work through a one-way ANOVA with unequal group sizes, from the weighted grand mean to a checked sums-of-squares decomposition and F test.

Beginner 10 min read

What You'll Learn

  • Calculate the grand mean by weighting each group mean by its sample size.
  • Distinguish between-groups, within-groups, and total variation for unequal groups.
  • Check the sums-of-squares decomposition using both group and observation calculations.
  • Compute degrees of freedom, mean squares, and the one-way ANOVA F statistic.
  • Interpret the omnibus test in context and identify why an unweighted mean of group means can mislead.

Why Unequal Group Sizes Change the Calculation

In a one-way ANOVA, groups do not always contain the same number of observations. When their sizes differ, each group mean represents a different number of observations. The grand mean must therefore give more weight to a group with more observations. Using an unweighted average of the group means can shift the center of the data and lead to incorrect sums of squares.

This tutorial works through that weighting and follows it into a complete ANOVA calculation. The central check is that total variation equals between-groups variation plus within-groups variation. Once the sums of squares agree, they can be converted to mean squares and used to form the omnibus F statistic.

Definition: Suppose there are \(k\) groups, with \(n_i\) observations in group \(i\), where \(i=1,\ldots,k\). The total sample size is \(N=\sum_{i=1}^{k}n_i\), and the mean of group \(i\) is \(\bar{x}_i\). The observation-weighted grand mean is $$ \bar{x}_{\text{grand}}=\frac{\sum_{i=1}^{k}n_i\bar{x}_i}{N}. $$ Each group mean is weighted by its group size because it summarizes \(n_i\) observations.

The Model and the Calculation

The one-way ANOVA model for observation \(j\) in group \(i\) is \(Y_{ij}=\mu_i+\epsilon_{ij}\). Here \(\mu_i\) is the mean for group \(i\), and \(\epsilon_{ij}\) is its error. The usual F test assumes that errors are independent, normally distributed with mean zero, and have the same variance \(\sigma^2\) in every group. In a hypothetical randomized experiment, independent experimental units and random assignment can support the independence assumption; normal errors and equal variances remain model assumptions to assess in context. A small data set cannot establish those assumptions by itself.

The omnibus hypotheses are \(H_0:\mu_1=\mu_2=\cdots=\mu_k\) and \(H_a:\) not all group means are equal. For the calculation, define the three sums of squares as follows:

Key formulas: The total sum of squares measures the spread of all observations around the grand mean. The between-groups sum of squares measures the spread of group means around that grand mean, weighted by group sizes. The within-groups sum of squares measures the spread of observations around their own group means. $$ SS_{\text{total}}=\sum_{i=1}^{k}\sum_{j=1}^{n_i}(Y_{ij}-\bar{x}_{\text{grand}})^2,\qquad SS_{\text{between}}=\sum_{i=1}^{k}n_i(\bar{x}_i-\bar{x}_{\text{grand}})^2,\qquad SS_{\text{within}}=\sum_{i=1}^{k}\sum_{j=1}^{n_i}(Y_{ij}-\bar{x}_i)^2. $$

The decomposition \(SS_{\text{total}}=SS_{\text{between}}+SS_{\text{within}}\) applies whether or not group sizes are equal. The degrees of freedom are \(k-1\) for between groups, \(N-k\) for within groups, and \(N-1\) for total. Dividing each of the first two sums of squares by its degrees of freedom gives \(MS_{\text{between}}\) and \(MS_{\text{within}}\). The test statistic is \(F=MS_{\text{between}}/MS_{\text{within}}\); under \(H_0\) and the model assumptions, it has an \(F\) distribution with \(k-1\) and \(N-k\) degrees of freedom.

Why the Grand Mean Is Weighted

The grand mean is the mean of all \(N\) observations, not the mean of the \(k\) group means. Group \(i\) contributes a total of \(n_i\bar{x}_i\) to the sum of the observations. Adding those group totals and dividing by \(N\) gives the weighted formula directly:

$$ \bar{x}_{\text{grand}} =\frac{\sum_{i=1}^{k}\sum_{j=1}^{n_i}Y_{ij}}{N} =\frac{\sum_{i=1}^{k}n_i\bar{x}_i}{N}. $$

The same weights appear in \(SS_{\text{between}}\): a group mean based on four observations contributes four squared deviations, while one based on two observations contributes two. This is not an adjustment made for convenience; it reflects how many observations each group contributes to the data.

Worked Example: Finding the Grand Mean From Group Summaries

Suppose three hypothetical groups have sizes \(2,4,\) and \(3\), and sample means \(12,15,\) and \(18\) units, respectively. Find the grand mean and compare it with the unweighted mean of the group means.

There are \(N=2+4+3=9\) observations. The weighted grand mean is $$ \bar{x}_{\text{grand}}=\frac{2(12)+4(15)+3(18)}{9} =\frac{24+60+54}{9} =\frac{138}{9}=15.33\text{ units}. $$

The unweighted average of the three group means is \((12+15+18)/3=15.00\) units. It is not the mean of all nine observations: it gives the group of size 2 the same weight as the group of size 4. Here the difference is \(0.333\) units. The summaries do not provide the observations within each group, so they are enough to find \(SS_{\text{between}}\), but not \(SS_{\text{within}}\) or the full F statistic.

For example, the between-groups sum of squares is $$ SS_{\text{between}} =2(12-15.33\ldots)^2+4(15-15.33\ldots)^2+3(18-15.33\ldots)^2 =\frac{396}{9}=44.00\text{ units}^2. $$ The unequal sizes enter both through the weighted center and through the multipliers in the sum.

A Complete Unequal-Group ANOVA Calculation

Consider hypothetical measurements, in units, from three treatment groups. Suppose independent experimental units were assigned to treatments, and use the one-way model’s normal-error and equal-variance assumptions for this calculation. These assumptions are part of the analysis; the small samples below do not prove that they hold.

Worked Example: Checking the Decomposition and Computing F

The observations are Group A: \(4,6\); Group B: \(7,8,9\); and Group C: \(8,10,10,12\). Find the grand mean, all three sums of squares, the F statistic, and the test conclusion at \(\alpha=0.05\).

1. Group means and weighted grand mean. The group sizes are \(n_A=2\), \(n_B=3\), and \(n_C=4\), so \(N=9\). The means are \(\bar{x}_A=5\), \(\bar{x}_B=8\), and \(\bar{x}_C=10\). The total of all observations is \(10+24+40=74\), giving $$ \bar{x}_{\text{grand}}=\frac{2(5)+3(8)+4(10)}{9} =\frac{74}{9}=8.2222\text{ units}. $$ An unweighted average of the group means would be \((5+8+10)/3=7.667\), which is not the mean of the nine observations.

2. Between-groups sum of squares. Weight each squared difference between a group mean and the grand mean by that group’s size: $$ SS_{\text{between}} =2\left(5-\frac{74}{9}\right)^2 +3\left(8-\frac{74}{9}\right)^2 +4\left(10-\frac{74}{9}\right)^2 =\frac{2718}{81}=33.5556\text{ units}^2. $$

3. Within-groups sum of squares. Calculate deviations from each group’s own mean. For Group A, the contribution is \((4-5)^2+(6-5)^2=2\). For Group B, it is \((7-8)^2+(8-8)^2+(9-8)^2=2\). For Group C, it is \((8-10)^2+(10-10)^2+(10-10)^2+(12-10)^2=8\). Thus $$ SS_{\text{within}}=2+2+8=12.00\text{ units}^2. $$

4. Check against total variation. From the group means and within-group sums of squares, $$ SS_{\text{between}}+SS_{\text{within}} =33.5556+12.00=45.5556\text{ units}^2. $$ A direct calculation around the grand mean gives the same total. The group contributions to \(SS_{\text{total}}\), in order A, B, and C, are $$ \begin{aligned} A:&\quad (4-74/9)^2+(6-74/9)^2=1844/81\approx22.7654,\\ B:&\quad (7-74/9)^2+(8-74/9)^2+(9-74/9)^2=174/81\approx2.1481,\\ C:&\quad (8-74/9)^2+2(10-74/9)^2+(12-74/9)^2=1672/81\approx20.6420. \end{aligned} $$ Their sum is \((1844+174+1672)/81=3690/81=45.5556\text{ units}^2\), as required. The direct calculation and the decomposition therefore agree.

5. Mean squares, F statistic, and conclusion. There are \(k=3\) groups, so \(df_{\text{between}}=2\), \(df_{\text{within}}=9-3=6\), and \(df_{\text{total}}=8\). Then $$ MS_{\text{between}}=\frac{33.5556}{2}=16.7778,\qquad MS_{\text{within}}=\frac{12.00}{6}=2.000, \qquad F=\frac{16.7778}{2.000}=8.3889. $$

Under \(H_0\), this statistic is compared with an \(F\) distribution with \(2\) and \(6\) degrees of freedom. Its upper-tail p-value is approximately \(0.0183\). Since \(0.0183<0.05\), reject \(H_0\) at the 5% significance level. The data provide evidence that the three population treatment means are not all equal. The omnibus result does not identify which means differ.

A Second Calculation as a Numerical Check

A second data set is useful for checking that the unequal-size method is not specific to one set of values. The following example also shows how the weighted grand mean, sums of squares, and F statistic fit together when the group means are farther apart.

Worked Example: Repeating the Calculation With Different Group Sizes

Suppose hypothetical measurements, in units, are Group A: \(14,16\); Group B: \(15,17,16\); and Group C: \(18,20,19,21\). Assume independent experimental units and, for the ANOVA calculation, normally distributed errors with a common variance. Compute the F statistic and test at \(\alpha=0.05\).

The sizes are \(2,3,4\), and the means are \(15,16,19.5\). The total is \(30+48+78=156\), so \(N=9\) and $$ \bar{x}_{\text{grand}}=\frac{156}{9}=\frac{52}{3}=17.3333\text{ units}. $$ The between-groups sum of squares is $$ SS_{\text{between}} =2\left(15-\frac{52}{3}\right)^2 +3\left(16-\frac{52}{3}\right)^2 +4\left(19.5-\frac{52}{3}\right)^2 =\frac{315}{9}=35.00\text{ units}^2. $$

Within Group A, the sum of squared deviations is \((14-15)^2+(16-15)^2=2\). For Group B it is \((15-16)^2+(17-16)^2+(16-16)^2=2\). For Group C it is \((18-19.5)^2+(20-19.5)^2+(19-19.5)^2+(21-19.5)^2=5\). Therefore \(SS_{\text{within}}=2+2+5=9.00\text{ units}^2\), and the decomposition gives \(SS_{\text{total}}=35+9=44.00\text{ units}^2\).

The degrees of freedom are \(2\) between groups and \(6\) within groups. Thus \(MS_{\text{between}}=35/2=17.50\), \(MS_{\text{within}}=9/6=1.500\), and $$ F=\frac{17.50}{1.500}=11.6667. $$ For an \(F\) distribution with \(2\) and \(6\) degrees of freedom, the upper-tail p-value is approximately \(0.00856\). Because this is below \(0.05\), reject \(H_0\) at that significance level. This is evidence that the population means are not all equal; it does not establish that treatment caused the observed differences unless the design justifies a causal interpretation.

Why the Decomposition Holds

The identity behind the sums-of-squares check can be seen by writing each observation’s deviation from the grand mean as the sum of two deviations:

$$ Y_{ij}-\bar{x}_{\text{grand}} =(Y_{ij}-\bar{x}_i)+(\bar{x}_i-\bar{x}_{\text{grand}}). $$

Squaring and summing over observations produces the within-groups sum of squares, the between-groups sum of squares, and a cross-product term. For each group, the residuals around its own mean add to zero: \(\sum_{j=1}^{n_i}(Y_{ij}-\bar{x}_i)=0\). Consequently, the cross-product is zero: $$ 2\sum_{i=1}^{k}(\bar{x}_i-\bar{x}_{\text{grand}}) \sum_{j=1}^{n_i}(Y_{ij}-\bar{x}_i)=0. $$ The remaining terms are exactly \(SS_{\text{within}}\) and \(SS_{\text{between}}\). This verifies \(SS_{\text{total}}=SS_{\text{within}}+SS_{\text{between}}\) for unequal as well as equal group sizes.

Common Mistake and Practical Check

A common mistake is to average the group means without weighting, then use that value as the grand mean. The mistaken reasoning is that every group mean should count once because there are \(k\) groups. The correction is that the grand mean averages observations, not groups: group \(i\) contributes \(n_i\) observations, so use \(\sum_i n_i\bar{x}_i/N\). Then use those same \(n_i\) values in \(SS_{\text{between}}\).

A second useful check is to compute total variation in two ways when the raw observations are available: directly around the grand mean, and as \(SS_{\text{between}}+SS_{\text{within}}\). If the results differ, check the grand mean, the group-size weights, and whether each within-group deviation was calculated from its own group mean. Rounding can create tiny discrepancies, but it cannot explain a substantial mismatch.

Key takeaway: With unequal group sizes, calculate the grand mean from all observations, weight each group’s contribution by its size, and verify that total sum of squares equals between-groups plus within-groups sum of squares before forming the F ratio.

Check Your Understanding

Use the definitions and calculation steps above to answer the following questions.

  1. Two groups have sizes \(3\) and \(6\), with means \(10\) and \(13\). Calculate the observation-weighted grand mean.
  2. Why is the unweighted average of group means generally not the grand mean when group sizes differ?
  3. In a one-way ANOVA with \(k=4\) groups and \(N=18\) observations, what are the degrees of freedom for between groups, within groups, and total?
  4. What numerical equality should hold among \(SS_{\text{total}}\), \(SS_{\text{between}}\), and \(SS_{\text{within}}\)?
  5. An ANOVA calculation gives \(MS_{\text{between}}=12\) and \(MS_{\text{within}}=3\). What is the F statistic, and what does it compare?
Clinical Biostats