General linear model (GLM): unbalanced ANOVA with covariates
The general linear model (GLM) is ANOVA and regression in one tool. It fits a response to any mix of categorical factors (supplier, oven, shift), their interactions and continuous covariates (thickness, content, temperature) by least squares, and it gives correct tests when the data are unbalanced: different numbers of observations per cell, which is what real plant data almost always look like. Its key outputs are adjusted sums of squares and adjusted (least squares) means.
When to use it
- You have two or more factors and the cell sizes are unequal, because runs were lost or because the data come from production records.
- A continuous variable that you did not control, such as glass content or incoming hardness, also affects the response, and you want to compare the factor levels as if it were equal for all (analysis of covariance).
- You want factor tests and level comparisons that do not depend on the order in which you list the terms.
For one factor with similar group sizes, one-way ANOVA is simpler. For factors whose levels are a random sample (batches, operators), see the mixed effects model. With only continuous predictors, use regression analysis.
How it works
Each categorical factor with a levels becomes a − 1 coded columns (with effect coding, the columns sum to zero across levels); interactions are products of those columns; covariates enter as they are, usually centered. The model is then a multiple regression. For two factors and a covariate x:
y = μ + αᵢ + βⱼ + γ·(x − x̄) + εThe question is how much of the variation to credit to each term. When the design is unbalanced or a covariate is correlated with the factors, the terms overlap and there are two common answers:
- Sequential (Type I) SS: the reduction in error when the term is added, in the order you listed the terms. The first term gets credit for everything it shares with later terms, so the result depends on the order.
- Adjusted (Type III) SS: the reduction in error when the term is added last, after all the others:
SS_adj(term) = SSE(model without the term) − SSE(full model)Adjusted SS answer "does this term matter, given everything else in the model?" and do not depend on order. They are the usual basis for the F tests. (Type II SS, which adjust each term for all terms that do not contain it, equal Type III when the model has no interactions.)
Adjusted means, also called least squares means, are the model's predicted mean for each level with the other factors averaged equally and covariates set at their means. They are the means to compare, not the raw ones, which carry the imbalance with them.
Example
A plant molds brackets from sheet molding compound (SMC) bought from three suppliers and cures them in two ovens. From three weeks of production tests it has 18 flexural strength results (MPa), with the glass content (%) of each charge, which varies from lot to lot:
| Oven | Supplier | Glass (%) | Strength | Oven | Supplier | Glass (%) | Strength |
|---|---|---|---|---|---|---|---|
| 1 | A | 30.0 | 123.4 | 2 | A | 31.3 | 121.9 |
| 1 | A | 25.4 | 107.3 | 2 | A | 28.2 | 112.6 |
| 1 | A | 28.4 | 116.7 | 2 | B | 28.6 | 120.3 |
| 1 | A | 27.4 | 116.8 | 2 | B | 28.7 | 116.8 |
| 1 | B | 28.5 | 122.1 | 2 | B | 28.3 | 116.8 |
| 1 | B | 28.8 | 120.9 | 2 | B | 27.9 | 118.0 |
| 1 | C | 29.5 | 120.6 | 2 | C | 31.1 | 124.0 |
| 1 | C | 31.3 | 127.5 | 2 | C | 32.0 | 125.7 |
| 1 | C | 30.6 | 128.2 | 2 | C | 31.3 | 124.7 |
The cells are unbalanced (from 2 to 4 per oven and supplier), and supplier C's charges happen to carry more glass. A first model with the oven × supplier interaction shows it is not significant (F = 0.61, p = 0.562), so the engineer fits oven, supplier and glass content:
| Source | DF | Seq SS | Adj SS | Adj MS | F | P |
|---|---|---|---|---|---|---|
| Oven | 1 | 0.41 | 49.51 | 49.51 | 19.27 | 0.001 |
| Supplier | 2 | 239.54 | 41.47 | 20.73 | 8.07 | 0.005 |
| Glass content | 1 | 202.20 | 202.20 | 202.20 | 78.70 | < 0.001 |
| Error | 13 | 33.40 | 33.40 | 2.569 | ||
| Total | 17 | 475.54 |
S = 1.603 MPa, R² = 92.98%, R²(adj) = 90.82%. The glass content coefficient is 3.047 MPa per percentage point (SE 0.343).
Raw and adjusted means (adjusted to the mean glass content of 29.29%, with ovens weighted equally):
| Level | N | Mean glass (%) | Raw mean | Adjusted mean (SE) |
|---|---|---|---|---|
| Supplier A | 6 | 28.45 | 116.45 | 118.41 (0.71) |
| Supplier B | 6 | 28.47 | 119.15 | 122.29 (0.75) |
| Supplier C | 6 | 30.97 | 125.12 | 120.02 (0.87) |
| Oven 1 | 9 | 28.88 | 120.39 | 122.09 (0.57) |
| Oven 2 | 9 | 29.71 | 120.09 | 118.39 (0.57) |
How to read the results
- The raw means mislead twice. By raw means, supplier C is best by 6 MPa. Adjusted for its extra glass, which any supplier's material would benefit from, C falls behind B. The supplier comparison the plant cares about is B first, then C, then A.
- The oven effect was hidden. The two ovens have nearly the same raw mean (120.39 and 120.09 MPa), and the sequential SS for oven, listed first, is only 0.41. Adjusted for supplier mix and glass content, oven 1 is 3.7 MPa stronger (p = 0.001). Oven 2 received more of the strong supplier C material, which masked its weakness.
- Order matters only for Seq SS. Listing supplier first changes oven's sequential SS from 0.41 to 3.94; the adjusted SS do not change.
- The covariate is the biggest effect. Each extra point of glass adds about 3 MPa. Controlling glass content at the supplier would do more than changing suppliers.
Common mistakes
- Comparing raw means in unbalanced data. They mix the effect of the factor with the effects of whatever is unbalanced with it.
- Reading Seq SS as the importance of a term. They depend on the order of the terms.
- Adjusting for a covariate that the factor causes. If the oven itself changed the glass content, adjusting for it would remove part of the oven effect. Covariates should be measured before the treatment or be unaffected by it.
- Assuming the covariate slope is the same in every group without checking. Fit the factor × covariate interaction once; if it is significant, the groups need separate slopes.
- Extrapolating adjusted means. Adjusting supplier A to 31% glass is risky if A never shipped material above 30%.
Frequently asked questions
What is the difference between Type I and Type III sums of squares?
Type I (sequential) SS credit each term with what it adds in the order listed; Type III (adjusted) SS credit each term with what it adds after all the others. In balanced designs without covariates they are equal; otherwise use Type III for testing.
What are adjusted or least squares means?
The model's predicted means for each level of a factor, with the other factors averaged equally and covariates held at their means. They compare levels on an equal footing.
Is GLM the same as ANCOVA?
Analysis of covariance is a special case: a GLM with factors and one or more covariates. GLM also covers plain ANOVA, unbalanced designs and regression.
Can GLM handle random factors?
Only in a limited way. For batches, operators or parts that are random samples, a mixed effects model gives correct tests and variance components.