Why Unequal Group Sizes Require Care
A one-way ANOVA compares the means of several groups by comparing variation between the groups with variation among observations within the groups. When the groups have different sample sizes, the calculation is still systematic, but the group means cannot be treated as equally weighted. A mean based on many observations contributes more to the overall average than a mean based on only a few.
This tutorial works through that calculation from raw observations to the F statistic. The central bookkeeping rule is to keep each group’s sample size attached to its mean: it determines the grand mean and appears again in the between-group sum of squares.
The unequal-size grand mean is a weighted mean:
The three sums of squares are the total sum of squares, \(SS_{\text{Total}}=\sum_i\sum_j(x_{ij}-\bar{x})^2\); the between-group sum of squares, \(SS_{\text{Between}}=\sum_i n_i(\bar{x}_i-\bar{x})^2\); and the within-group sum of squares, \(SS_{\text{Within}}=\sum_i\sum_j(x_{ij}-\bar{x}_i)^2\). Their relationship is \(SS_{\text{Total}}=SS_{\text{Between}}+SS_{\text{Within}}\).
Why the Group Sizes Weight the Calculation
Each group mean represents a different number of observations. The overall mean must therefore give group \(i\) weight \(n_i\), not weight 1. The same logic applies to between-group variation: if a group mean is far from the grand mean, its contribution is \(n_i(\bar{x}_i-\bar{x})^2\). A group of 20 observations at that distance contributes ten times as much as a group of 2 observations at the same distance.
The degrees of freedom are \(k-1\) for between groups, \(N-k\) for within groups, and \(N-1\) in total. Divide each sum of squares by its degrees of freedom to obtain the corresponding mean square. The ANOVA F statistic is \(F=MS_{\text{Between}}/MS_{\text{Within}}\). These calculations form the ANOVA table; a larger F indicates that the between-group variation is large relative to the within-group variation.
Worked Calculation From Raw Observations
In each example, treat the observations as hypothetical. For the F-test model, suppose observations are independent and that the normality and common-variance assumptions are appropriate for the setting. The arithmetic alone cannot establish those assumptions; they are part of the model used to interpret the F statistic.
Worked Example: Three Groups of Different Sizes
Suppose three hypothetical groups have observations \(4,5,6\); \(6,7,8,9\); and \(8,9,10,11,12\), respectively. The sample sizes are \(3,4,5\). Calculate the ANOVA table entries and F statistic.
1. Group means and the weighted grand mean. The group means are \(\bar{x}_1=5\), \(\bar{x}_2=7.5\), and \(\bar{x}_3=10\). The total is \(N=3+4+5=12\), and the sum of all observations is \(15+30+50=95\). Therefore,
2. Within-group sum of squares. In the first group, squared deviations from 5 sum to \(1+0+1=2\). In the second group, squared deviations from 7.5 sum to \(2.25+0.25+0.25+2.25=5\). In the third group, squared deviations from 10 sum to \(4+1+0+1+4=10\). Thus \(SS_{\text{Within}}=2+5+10=17\).
3. Between-group sum of squares. Weight each squared distance from the grand mean by its group size:
4. Total and degrees of freedom. The decomposition gives \(SS_{\text{Total}}=47.9167+17=64.9167\). With \(k=3\) and \(N=12\), the degrees of freedom are \(2\) between groups, \(12-3=9\) within groups, and \(11\) in total. The degrees of freedom also add: \(2+9=11\).
5. Mean squares and F.
The calculation gives \(F\approx12.68\), with numerator degrees of freedom 2 and denominator degrees of freedom 9. This is the test statistic; deciding its significance requires evaluating it against an F distribution or obtaining a p-value.
Using the Decomposition as a Check
An unequal-size calculation has several opportunities for a weighting or arithmetic error. A useful check is to calculate the total sum of squares directly from all observations and compare it with the sum of the between- and within-group components. The equality is not a numerical coincidence: each observation’s deviation from the grand mean can be written as its deviation from its group mean plus the group mean’s deviation from the grand mean.
When the square is expanded and summed within group \(i\), the cross-product is zero because \(\sum_j(x_{ij}-\bar{x}_i)=0\). The remaining terms are the within-group squared deviations and \(n_i(\bar{x}_i-\bar{x})^2\). Summing over groups therefore gives \(SS_{\text{Total}}=SS_{\text{Within}}+SS_{\text{Between}}\).
Worked Example: Checking the Total Sum of Squares
Use the first example’s observations to calculate \(SS_{\text{Total}}\) directly. The grand mean is \(95/12\). The sum of squared observations is \(77+230+510=817\): these totals come from squaring and adding the observations in each group.
Using \(\sum(x_{ij}-\bar{x})^2=\sum x_{ij}^2-(\sum x_{ij})^2/N\),
This agrees with \(SS_{\text{Between}}+SS_{\text{Within}}=47.9167+17=64.9167\). If the two answers did not agree, likely causes would include using an unweighted grand mean, using the wrong center for a group’s within-group deviations, or making an arithmetic error.
A Second Unequal-Size Calculation
The next example keeps the groups unequal but has a different pattern of group means. It makes the weighting in the between-group sum of squares especially visible: the middle group is close to the grand mean, while the smaller and larger groups lie on opposite sides.
Worked Example: Computing the F Ratio With a Different Pattern
Suppose hypothetical observations are \(2,4\) in group 1; \(4,6,8\) in group 2; and \(7,9,11,13\) in group 3. The sample sizes are \(2,3,4\), and the group means are \(3,6,10\).
The grand mean is the total \(6+18+40=64\) divided by \(N=9\): \(\bar{x}=64/9=7.1111\). The within-group sums of squares are \(2\) for group 1, \(8\) for group 2, and \(20\) for group 3, so \(SS_{\text{Within}}=30\).
For the between-group component, retain the group-size weights:
Therefore \(SS_{\text{Total}}=70.8889+30=100.8889\). The degrees of freedom are \(k-1=2\), \(N-k=6\), and \(N-1=8\). The mean squares and F statistic are
The result is \(F\approx7.089\), with degrees of freedom 2 and 6. Notice that the group mean of 10 contributes \(4(10-7.1111)^2\), not merely \((10-7.1111)^2\): its group contains four observations.
Reading the Calculation in an ANOVA Table
The table organizes the calculation by source of variation. For the preceding example it would contain these entries:
| Source | Sum of squares | Degrees of freedom | Mean square | F |
|---|---|---|---|---|
| Between groups | 70.8889 | 2 | 35.4445 | 7.0889 |
| Within groups | 30.0000 | 6 | 5.0000 | |
| Total | 100.8889 | 8 | — | — |
The F statistic is the ratio of the two mean squares, not the ratio of the sums of squares. Dividing by degrees of freedom first accounts for the different numbers of independent pieces of information represented by the two sources. The total row is useful as a check, but it does not have a mean square or F statistic in this table.
Worked Example: A Compact Unequal-Size Calculation
A final hypothetical data set has observations \(10,12\) in group 1; \(13,14,15\) in group 2; and \(16,18\) in group 3. The respective sample sizes are \(2,3,2\), and the group means are \(11,14,17\). The total is \(N=7\), and the grand mean is \((22+42+34)/7=98/7=14\).
The between-group sum of squares is \(2(11-14)^2+3(14-14)^2+2(17-14)^2=18+0+18=36\). The within-group sums of squares are \(2\), \(2\), and \(2\), so \(SS_{\text{Within}}=6\) and \(SS_{\text{Total}}=42\).
There are \(k=3\) groups, so the degrees of freedom are \(2\) between groups, \(7-3=4\) within groups, and \(6\) total. Then \(MS_{\text{Between}}=36/2=18\) and \(MS_{\text{Within}}=6/4=1.5\), giving
The F statistic is 12, with numerator degrees of freedom 2 and denominator degrees of freedom 4. The sample sizes are unequal, but each step follows the same formulas; the actual group sizes determine the weights, total sample size, and degrees of freedom.
Common Mistake: Averaging the Group Means Equally
A tempting shortcut is to add the group means and divide by the number of groups. That gives the correct grand mean only when all group sizes are equal (or in special cases where the values happen to coincide). In the first example, the unweighted mean of the group means is \((5+7.5+10)/3=7.5\), whereas the correct grand mean is \(95/12=7.9167\). The unweighted answer gives the group of three observations the same influence as the group of five.
The correction is to calculate \(\bar{x}=\sum_i n_i\bar{x}_i/N\) and to use \(n_i\) in every between-group term. Then check that the degrees of freedom add to \(N-1\) and that the sums of squares satisfy \(SS_{\text{Total}}=SS_{\text{Between}}+SS_{\text{Within}}\). These checks catch many, though not all, arithmetic errors.
Check Your Understanding
Try these questions without looking back at the worked calculations.
- Three groups have sizes 2, 4, and 5, with means 8, 10, and 13. Write the weighted-grand-mean calculation.
- In \(SS_{\text{Between}}\), why is the squared difference for group \(i\) multiplied by \(n_i\)?
- For \(k=4\) groups and \(N=18\) observations, find the between-group, within-group, and total degrees of freedom.
- If \(SS_{\text{Between}}=36\), \(SS_{\text{Within}}=24\), and the corresponding degrees of freedom are 2 and 8, calculate both mean squares and F.
- A student divides the sum of three group means by 3 to obtain the grand mean, even though the groups have different sizes. Explain the error and give the correct method.