Why the Reference Level Matters
A categorical predictor in a model is represented through contrasts: numerical columns that encode how each category compares with a reference category. That reference is not a statistical finding. It is a choice of how to express the model’s comparisons. A deliberate, verified choice makes coefficients easier to explain and helps prevent errors when model output is interpreted.
This tutorial focuses on unordered factors in R with treatment contrasts and an intercept. R’s defaults can depend on the factor’s levels and the contrasts options in effect, so do not rely on an assumed ordering. Set the intended reference explicitly and inspect the model matrix that will be used. This factor-level detail is aligned with the SOA Exam PA (Predictive Analytics) syllabus.
How Treatment Coding Represents a Factor
Suppose \(G\) is an unordered factor with categories \(g_1,\ldots,g_K\), and \(g_1\) is the reference. In a model with an intercept, treatment coding uses \(K-1\) indicator columns. The reference category has a zero in each of those columns; each non-reference category has a one in its own column and zeros in the others.
For a model with one factor and no other predictors, the linear predictor is:
Here \(I(G=g_j)\) equals one when the observation is in category \(g_j\), and zero otherwise. For the reference category, every indicator is zero, so \(\eta=\beta_0\). For category \(g_j\), \(\eta=\beta_0+\beta_j\). Therefore \(\beta_j\) is the difference between that category’s linear predictor and the reference category’s linear predictor. In a linear model, this is a difference in fitted means; in a generalized linear model, it is a difference on the model’s linear-predictor scale.
This derivation also explains why changing the reference changes coefficient values. The category-specific fitted values need not change: only the category used as the comparison point, and therefore the coefficient representation, changes. This statement assumes the same model terms and data, and a full-rank design matrix.
Setting and Checking the Reference in R
For an unordered factor, relevel(x, ref = "...") moves the chosen reference to the first factor level. Under treatment contrasts, that first level is the baseline. It is good practice to check both the levels and the contrasts after setting the reference. Then check the model matrix, which shows the actual columns passed to the model.
Check that the variable is a factor and inspect levels(x). Choose the intended category using the problem context, not just its position or frequency.
Use x <- relevel(x, ref = "chosen category"). If creating the factor from character data, explicitly supply levels when a reproducible level order is important.
Inspect levels(x), contrasts(x), and model.matrix(~ x, data = data). Confirm that the expected reference has zeros in the factor columns and that the other categories have the expected indicator.
With treatment contrasts, the contrast matrix has one row per factor level and one column per non-reference level. The reference row is all zeros. R can use contrast settings other than treatment coding, so the presence of a factor alone does not guarantee this interpretation. Inspecting contrasts(x) and model.matrix() checks the coding actually in use. The examples below state the coding assumption explicitly.
Worked Example: Set a Reference and Read the Model Matrix
A hypothetical insurer groups policies into plan categories Core, Flex, and Premium. The analysis will use Core as the comparison category. Set the reference in R and determine the treatment-coded model-matrix row for each plan. Assume an intercept and treatment contrasts.
Step 1: Create the factor. Use an explicit level order so the intended categories are recorded clearly: plan <- factor(c("Core", "Flex", "Premium", "Flex"), levels = c("Core", "Flex", "Premium")). The levels are Core, Flex, and Premium.
Step 2: Set and confirm the reference. Run plan <- relevel(plan, ref = "Core"). Then levels(plan) returns Core, Flex, Premium, with Core first. Under the stated treatment-contrast assumption, Core is the reference.
Step 3: Identify the columns. The two non-reference categories produce columns planFlex and planPremium. The contrast matrix has rows Core, Flex, Premium and columns Flex, Premium:
Step 4: Verify observation rows. The data order is Core, Flex, Premium, Flex. Therefore the rows of model.matrix(~ plan), with columns (Intercept), planFlex, planPremium, are (1, 0, 0), (1, 1, 0), (1, 0, 1), and (1, 1, 0). The Core row has zeros in both factor columns, as required for the reference.
Method and trap avoided. The method is to set the reference, inspect the factor levels, and verify the model matrix. Merely seeing that Core appears first in the source data would not establish that it is the reference; the factor’s level order and contrast settings determine the coding.
Changing the Reference Changes Comparisons, Not the Group Means
For an additive model with an intercept and one unordered factor, each treatment-coded coefficient compares one non-reference group with the reference group. If the reference changes, the new intercept is the fitted value for the new reference, and each new category coefficient is the difference from that group. When the model includes other predictors, the same comparison applies with those predictors held fixed.
A reference change does not, by itself, improve fit or alter the information in the factor. Under the same model structure and a full-rank design matrix, fitted values remain the same because the new coefficients express the same category-specific fitted values in a different coordinate system.
Worked Example: Translate Coefficients After Changing the Reference
In a hypothetical analysis of annual service costs, the observed costs (in thousands of dollars) are North: 2, 4, 6; South: 5, 7, 9; and West: 1, 3, 5. Fit the interpretation of an intercept-plus-zone model first with North as the reference, then with West as the reference. Assume ordinary least squares and treatment contrasts.
Step 1: Calculate each group mean. North has mean \((2+4+6)/3=12/3=4\). South has mean \((5+7+9)/3=21/3=7\). West has mean \((1+3+5)/3=9/3=3\).
Step 2: Express the model with North as reference. In this one-factor model, the intercept is the North mean, 4. The South coefficient is \(7-4=3\), and the West coefficient is \(3-4=-1\). Thus the fitted values by zone are North: \(4\), South: \(4+3=7\), and West: \(4+(-1)=3\).
Step 3: Change the reference to West. The new intercept is the West mean, 3. The North coefficient is \(4-3=1\), and the South coefficient is \(7-3=4\). The fitted values are West: \(3\), North: \(3+1=4\), and South: \(3+4=7\).
Step 4: Check the model-matrix columns. With West as reference, the columns are (Intercept), zoneNorth, and zoneSouth. The rows for West, North, and South are respectively (1, 0, 0), (1, 1, 0), and (1, 0, 1). Each row, combined with the new coefficients, gives the same group fitted value calculated above.
Method and trap avoided. The method is to calculate the group means, then express each as the intercept plus its difference from the chosen baseline. A frequent error is to keep the old coefficients after changing the reference. That would give, for example, a North fitted value of \(3+3=6\) if the old South coefficient were incorrectly assigned to North; the correct North coefficient is \(4-3=1\), giving \(3+1=4\).
Choose a Baseline for a Clear Comparison
A useful baseline is a category that makes the intended comparisons easy to explain. It might be a standard product, a current practice, or a well-understood group. The reference should not be chosen just because it has the most observations, nor should it be chosen after looking for a result that appears more favorable. The choice determines the wording of coefficients, not whether a category is intrinsically better.
When the model includes an intercept and additional predictors, take care with the intercept’s interpretation. For a model such as lm(cost ~ plan + age, data = d), the intercept is the predicted cost for the reference plan when age is zero. If age zero is outside the meaningful range, the intercept may not be substantively useful, even though the factor contrasts are still comparisons against the reference plan at a fixed age. A reference-level choice does not resolve that separate interpretation issue.
Worked Example: Verify a Business-Meaningful Baseline
A hypothetical service study compares the categories Standard, Enhanced, and Legacy. The team wants every category effect stated relative to Standard. A factor was created with levels in the order Legacy, Enhanced, Standard, so the default first-level reference would not match the intended comparison. Show how to correct and verify it, assuming an intercept and treatment contrasts.
Step 1: Identify the issue. Before changing anything, levels(service_type) returns Legacy, Enhanced, Standard. Under treatment coding with an intercept, Legacy would be the baseline if those contrasts are in use. The data’s first observed value does not override the factor-level order.
Step 2: Set the intended reference. Run service_type <- relevel(service_type, ref = "Standard"). Now levels(service_type) returns Standard, Legacy, Enhanced: the reference is first, and the remaining levels follow.
Step 3: Check the contrasts and matrix. With treatment contrasts, the columns are service_typeLegacy and service_typeEnhanced. The rows for Standard, Legacy, and Enhanced are (0, 0), (1, 0), and (0, 1) in those two factor columns. If the observed data contain one record in each category in that order, model.matrix(~ service_type, data = d) has rows (1, 0, 0), (1, 1, 0), and (1, 0, 1), including the intercept column.
Step 4: State the interpretation. In a model with no other predictors, the intercept is the fitted value for Standard. The service_typeLegacy coefficient is the Legacy-minus-Standard difference, and the service_typeEnhanced coefficient is the Enhanced-minus-Standard difference. With other predictors, these differences are interpreted at the same values of those predictors.
Method and trap avoided. The method is to set the reference by its label, then verify the level order and matrix columns. A plausible-looking coefficient name is not enough: confirm that it represents the comparison intended by the analysis.
Common Mistakes and Practical Checks
The most important check is to compare the model matrix with the intended interpretation before explaining coefficients. In R, model.matrix(~ factor_name, data = d) displays the intercept and the factor columns for that formula. If the model has more terms, inspect the matrix for the full formula, since interactions or other predictors add columns and change how individual coefficients must be described.
- Assuming the first observed category is the baseline. The factor’s levels and contrast coding control the baseline, not the order of rows in the data.
- Assuming every model uses treatment contrasts. Contrast options can differ. Inspect contrasts(factor_name) and the model matrix before interpreting coefficient names as baseline comparisons.
- Changing the reference but not updating the explanation. A coefficient that used to mean South minus North does not keep that meaning after the reference changes.
- Thinking the reference is a modeling advantage. Under the same full-rank model, changing the reference changes the coefficient representation, not the fitted values.
- Choosing a baseline without a reason. A meaningful baseline makes the comparisons readable; it does not make the resulting category differences more valid.
In applied work, record the intended reference as part of the model specification and verify it after creating or modifying the factor. This is especially useful when factor levels have been explicitly ordered, or when a model is rerun after data preparation changes. If a category is not represented in the data being modeled, inspect the resulting design matrix and estimability before interpreting its coefficient; a requested comparison cannot be supported by absent observations.
Check Your Understanding
For each problem, assume an intercept and treatment contrasts unless stated otherwise. Verify the reference using the factor levels or model-matrix interpretation.
- A factor named metal has levels Bronze, Silver, Gold. You run metal <- relevel(metal, ref = "Gold"). What are the factor columns and the model-matrix factor entries for Bronze, Silver, and Gold? (Answer: The columns are metalBronze and metalSilver; Bronze is (1, 0), Silver is (0, 1), and reference Gold is (0, 0).)
- A one-factor linear model has group means A = 6, B = 9, and C = 2. If B is the reference, give the intercept and the coefficients for A and C. (Answer: Intercept 9; A coefficient \(6-9=-3\); C coefficient \(2-9=-7\).)
- A model with baseline East has an intercept of 120, a South coefficient of \(-15\), and a West coefficient of \(20\). Translate these values to a West reference and verify the three fitted group values. (Answer: New intercept 140; East coefficient \(-20\); South coefficient \(-35\); fitted values remain East 120, South 105, and West 140.)
- A factor has levels Basic, Plus, Max, and Basic is currently the reference. Write the R command to make Plus the reference, and name the expected factor columns. (Answer: Run channel <- relevel(channel, ref = "Plus"); the factor columns are channelBasic and channelMax.)
- A colleague says that a category’s treatment coefficient is its fitted value, regardless of the reference. Explain the correction for a model with an intercept. (Answer: The intercept is the fitted value for the reference category in a one-factor model; each non-reference coefficient is that category’s fitted-value difference from the reference. Its fitted value is the intercept plus its coefficient.)